Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (lm75) add new-style driver binding

More LM75 updates:

- Teach the LM75 driver to use new-style driver binding:

* Create a second driver struct, using new-style driver binding
methods cribbed from the legacy code.

* Add a MODULE_DEVICE_TABLE (for "newER-style binding")

* The legacy probe logic delegates its work to this new code.

* The legacy driver now uses the name "lm75_legacy".

- More careful initialization. Chips are put into 9-bit mode so
the current interconversion routines will never fail.

- Save the original chip configuration, and restore it on exit.
(Among other things, this normally turns off the mode where
the chip is constantly sampling ... and thus saves power.)

So the new-style code should catch all chips that boards declare,
while the legacy code catches others. This particular coexistence
strategy may need some work yet ... legacy modes might best be set
up explicitly by some tool not unlike "sensors-detect". (Or else
completely eradicated...)

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Acked-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Mark M. Hoffman <mhoffman@lightlink.com>

authored by

David Brownell and committed by
Mark M. Hoffman
9ebd3d82 01a52397

+165 -48
+6 -1
drivers/hwmon/Kconfig
··· 406 406 - TelCom (now Microchip) TCN75 407 407 - Texas Instruments TMP100, TMP101, TMP75, TMP175, TMP275 408 408 409 - Most of these chips will require a "force" module parameter. 409 + This driver supports driver model based binding through board 410 + specific I2C device tables. 411 + 412 + It also supports the "legacy" style of driver binding. To use 413 + that with some chips which don't replicate LM75 quirks exactly, 414 + you may need the "force" module parameter. 410 415 411 416 This driver can also be built as a module. If so, the module 412 417 will be called lm75.
+159 -47
drivers/hwmon/lm75.c
··· 32 32 33 33 /* 34 34 * This driver handles the LM75 and compatible digital temperature sensors. 35 - * Compatibles include at least the DS75, DS1775, MCP980x, STDS75, TCN75, 36 - * TMP100, TMP101, TMP75, TMP175, and TMP275. 35 + * Only types which are _not_ listed in I2C_CLIENT_INSMOD_*() need to be 36 + * listed here. We start at 9 since I2C_CLIENT_INSMOD_*() currently allow 37 + * definition of up to 8 chip types (plus zero). 37 38 */ 39 + 40 + enum lm75_type { /* keep sorted in alphabetical order */ 41 + ds1775 = 9, 42 + ds75, 43 + /* lm75 -- in I2C_CLIENT_INSMOD_1() */ 44 + lm75a, 45 + max6625, 46 + max6626, 47 + mcp980x, 48 + stds75, 49 + tcn75, 50 + tmp100, 51 + tmp101, 52 + tmp175, 53 + tmp275, 54 + tmp75, 55 + }; 38 56 39 57 /* Addresses scanned by legacy style driver binding */ 40 58 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, ··· 72 54 73 55 /* Each client has this additional data */ 74 56 struct lm75_data { 75 - struct i2c_client client; 57 + struct i2c_client *client; 76 58 struct device *hwmon_dev; 77 59 struct mutex update_lock; 60 + u8 orig_conf; 78 61 char valid; /* !=0 if registers are valid */ 79 62 unsigned long last_updated; /* In jiffies */ 80 63 u16 temp[3]; /* Register values, ··· 84 65 2 = hyst */ 85 66 }; 86 67 87 - static void lm75_init_client(struct i2c_client *client); 88 68 static int lm75_read_value(struct i2c_client *client, u8 reg); 89 69 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); 90 70 static struct lm75_data *lm75_update_device(struct device *dev); ··· 138 120 139 121 /*-----------------------------------------------------------------------*/ 140 122 123 + /* "New style" I2C driver binding -- following the driver model */ 124 + 125 + static int 126 + lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) 127 + { 128 + struct lm75_data *data; 129 + int status; 130 + u8 set_mask, clr_mask; 131 + int new; 132 + 133 + if (!i2c_check_functionality(client->adapter, 134 + I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) 135 + return -EIO; 136 + 137 + data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL); 138 + if (!data) 139 + return -ENOMEM; 140 + 141 + i2c_set_clientdata(client, data); 142 + 143 + data->client = client; 144 + mutex_init(&data->update_lock); 145 + 146 + /* Set to LM75 resolution (9 bits, 1/2 degree C) and range. 147 + * Then tweak to be more precise when appropriate. 148 + */ 149 + set_mask = 0; 150 + clr_mask = (1 << 0) /* continuous conversions */ 151 + | (1 << 6) | (1 << 5); /* 9-bit mode */ 152 + 153 + /* configure as specified */ 154 + status = lm75_read_value(client, LM75_REG_CONF); 155 + if (status < 0) { 156 + dev_dbg(&client->dev, "Can't read config? %d\n", status); 157 + goto exit_free; 158 + } 159 + data->orig_conf = status; 160 + new = status & ~clr_mask; 161 + new |= set_mask; 162 + if (status != new) 163 + lm75_write_value(client, LM75_REG_CONF, new); 164 + dev_dbg(&client->dev, "Config %02x\n", new); 165 + 166 + /* Register sysfs hooks */ 167 + status = sysfs_create_group(&client->dev.kobj, &lm75_group); 168 + if (status) 169 + goto exit_free; 170 + 171 + data->hwmon_dev = hwmon_device_register(&client->dev); 172 + if (IS_ERR(data->hwmon_dev)) { 173 + status = PTR_ERR(data->hwmon_dev); 174 + goto exit_remove; 175 + } 176 + 177 + dev_info(&client->dev, "%s: sensor '%s'\n", 178 + data->hwmon_dev->bus_id, client->name); 179 + 180 + return 0; 181 + 182 + exit_remove: 183 + sysfs_remove_group(&client->dev.kobj, &lm75_group); 184 + exit_free: 185 + i2c_set_clientdata(client, NULL); 186 + kfree(data); 187 + return status; 188 + } 189 + 190 + static int lm75_remove(struct i2c_client *client) 191 + { 192 + struct lm75_data *data = i2c_get_clientdata(client); 193 + 194 + hwmon_device_unregister(data->hwmon_dev); 195 + sysfs_remove_group(&client->dev.kobj, &lm75_group); 196 + lm75_write_value(client, LM75_REG_CONF, data->orig_conf); 197 + i2c_set_clientdata(client, NULL); 198 + kfree(data); 199 + return 0; 200 + } 201 + 202 + static const struct i2c_device_id lm75_ids[] = { 203 + { "ds1775", ds1775, }, 204 + { "ds75", ds75, }, 205 + { "lm75", lm75, }, 206 + { "lm75a", lm75a, }, 207 + { "max6625", max6625, }, 208 + { "max6626", max6626, }, 209 + { "mcp980x", mcp980x, }, 210 + { "stds75", stds75, }, 211 + { "tcn75", tcn75, }, 212 + { "tmp100", tmp100, }, 213 + { "tmp101", tmp101, }, 214 + { "tmp175", tmp175, }, 215 + { "tmp275", tmp275, }, 216 + { "tmp75", tmp75, }, 217 + { /* LIST END */ } 218 + }; 219 + MODULE_DEVICE_TABLE(i2c, lm75_ids); 220 + 221 + static struct i2c_driver lm75_driver = { 222 + .driver = { 223 + .name = "lm75", 224 + }, 225 + .probe = lm75_probe, 226 + .remove = lm75_remove, 227 + .id_table = lm75_ids, 228 + }; 229 + 230 + /*-----------------------------------------------------------------------*/ 231 + 141 232 /* "Legacy" I2C driver binding */ 142 233 143 - static struct i2c_driver lm75_driver; 234 + static struct i2c_driver lm75_legacy_driver; 144 235 145 236 /* This function is called by i2c_probe */ 146 237 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) 147 238 { 148 239 int i; 149 240 struct i2c_client *new_client; 150 - struct lm75_data *data; 151 241 int err = 0; 152 242 153 243 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | ··· 265 139 /* OK. For now, we presume we have a valid address. We create the 266 140 client structure, even though there may be no sensor present. 267 141 But it allows us to use i2c_smbus_read_*_data() calls. */ 268 - if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) { 142 + new_client = kzalloc(sizeof *new_client, GFP_KERNEL); 143 + if (!new_client) { 269 144 err = -ENOMEM; 270 145 goto exit; 271 146 } 272 147 273 - new_client = &data->client; 274 - i2c_set_clientdata(new_client, data); 275 148 new_client->addr = address; 276 149 new_client->adapter = adapter; 277 - new_client->driver = &lm75_driver; 150 + new_client->driver = &lm75_legacy_driver; 278 151 new_client->flags = 0; 279 152 280 153 /* Now, we do the remaining detection. There is no identification- ··· 314 189 goto exit_free; 315 190 } 316 191 317 - /* NOTE: we treat "force=..." and "force_lm75=..." the same. */ 192 + /* NOTE: we treat "force=..." and "force_lm75=..." the same. 193 + * Only new-style driver binding distinguishes chip types. 194 + */ 318 195 strlcpy(new_client->name, "lm75", I2C_NAME_SIZE); 319 196 320 - /* Fill in the remaining client fields and put it into the global list */ 321 - data->valid = 0; 322 - mutex_init(&data->update_lock); 323 - 324 197 /* Tell the I2C layer a new client has arrived */ 325 - if ((err = i2c_attach_client(new_client))) 198 + err = i2c_attach_client(new_client); 199 + if (err) 326 200 goto exit_free; 327 201 328 - /* Initialize the LM75 chip */ 329 - lm75_init_client(new_client); 330 - 331 - /* Register sysfs hooks */ 332 - if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group))) 202 + err = lm75_probe(new_client, NULL); 203 + if (err < 0) 333 204 goto exit_detach; 334 - 335 - data->hwmon_dev = hwmon_device_register(&new_client->dev); 336 - if (IS_ERR(data->hwmon_dev)) { 337 - err = PTR_ERR(data->hwmon_dev); 338 - goto exit_remove; 339 - } 340 205 341 206 return 0; 342 207 343 - exit_remove: 344 - sysfs_remove_group(&new_client->dev.kobj, &lm75_group); 345 208 exit_detach: 346 209 i2c_detach_client(new_client); 347 210 exit_free: 348 - kfree(data); 211 + kfree(new_client); 349 212 exit: 350 213 return err; 351 214 } ··· 347 234 348 235 static int lm75_detach_client(struct i2c_client *client) 349 236 { 350 - struct lm75_data *data = i2c_get_clientdata(client); 351 - hwmon_device_unregister(data->hwmon_dev); 352 - sysfs_remove_group(&client->dev.kobj, &lm75_group); 237 + lm75_remove(client); 353 238 i2c_detach_client(client); 354 - kfree(data); 239 + kfree(client); 355 240 return 0; 356 241 } 357 242 358 - static struct i2c_driver lm75_driver = { 243 + static struct i2c_driver lm75_legacy_driver = { 359 244 .driver = { 360 - .name = "lm75", 245 + .name = "lm75_legacy", 361 246 }, 362 247 .attach_adapter = lm75_attach_adapter, 363 248 .detach_client = lm75_detach_client, ··· 385 274 return i2c_smbus_write_byte_data(client, reg, value); 386 275 else 387 276 return i2c_smbus_write_word_data(client, reg, swab16(value)); 388 - } 389 - 390 - static void lm75_init_client(struct i2c_client *client) 391 - { 392 - int reg; 393 - 394 - /* Enable if in shutdown mode */ 395 - reg = lm75_read_value(client, LM75_REG_CONF); 396 - if (reg >= 0 && (reg & 0x01)) 397 - lm75_write_value(client, LM75_REG_CONF, reg & 0xfe); 398 277 } 399 278 400 279 static struct lm75_data *lm75_update_device(struct device *dev) ··· 424 323 425 324 static int __init sensors_lm75_init(void) 426 325 { 427 - return i2c_add_driver(&lm75_driver); 326 + int status; 327 + 328 + status = i2c_add_driver(&lm75_driver); 329 + if (status < 0) 330 + return status; 331 + 332 + status = i2c_add_driver(&lm75_legacy_driver); 333 + if (status < 0) 334 + i2c_del_driver(&lm75_driver); 335 + 336 + return status; 428 337 } 429 338 430 339 static void __exit sensors_lm75_exit(void) 431 340 { 341 + i2c_del_driver(&lm75_legacy_driver); 432 342 i2c_del_driver(&lm75_driver); 433 343 } 434 344