diff options
author | Jean Delvare <khali@linux-fr.org> | 2008-07-16 13:30:14 -0400 |
---|---|---|
committer | Jean Delvare <khali@mahadeva.delvare> | 2008-07-16 13:30:14 -0400 |
commit | 8c8bacc883610b672d3f08dc6ebf1f17e495f5b9 (patch) | |
tree | fe74da507723e7998923663ca8ecdb9a51693e63 /drivers/hwmon/lm80.c | |
parent | a189dd62d328db7bf8ba68de6a948fdbc93dca25 (diff) |
hwmon: (lm80) Convert to a new-style i2c driver
The new-style lm80 driver implements the optional detect() callback
to cover the use cases of the legacy driver.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'drivers/hwmon/lm80.c')
-rw-r--r-- | drivers/hwmon/lm80.c | 94 |
1 files changed, 43 insertions, 51 deletions
diff --git a/drivers/hwmon/lm80.c b/drivers/hwmon/lm80.c index 26c91c9d4769..bcffc1899403 100644 --- a/drivers/hwmon/lm80.c +++ b/drivers/hwmon/lm80.c | |||
@@ -108,7 +108,6 @@ static inline long TEMP_FROM_REG(u16 temp) | |||
108 | */ | 108 | */ |
109 | 109 | ||
110 | struct lm80_data { | 110 | struct lm80_data { |
111 | struct i2c_client client; | ||
112 | struct device *hwmon_dev; | 111 | struct device *hwmon_dev; |
113 | struct mutex update_lock; | 112 | struct mutex update_lock; |
114 | char valid; /* !=0 if following fields are valid */ | 113 | char valid; /* !=0 if following fields are valid */ |
@@ -132,10 +131,12 @@ struct lm80_data { | |||
132 | * Functions declaration | 131 | * Functions declaration |
133 | */ | 132 | */ |
134 | 133 | ||
135 | static int lm80_attach_adapter(struct i2c_adapter *adapter); | 134 | static int lm80_probe(struct i2c_client *client, |
136 | static int lm80_detect(struct i2c_adapter *adapter, int address, int kind); | 135 | const struct i2c_device_id *id); |
136 | static int lm80_detect(struct i2c_client *client, int kind, | ||
137 | struct i2c_board_info *info); | ||
137 | static void lm80_init_client(struct i2c_client *client); | 138 | static void lm80_init_client(struct i2c_client *client); |
138 | static int lm80_detach_client(struct i2c_client *client); | 139 | static int lm80_remove(struct i2c_client *client); |
139 | static struct lm80_data *lm80_update_device(struct device *dev); | 140 | static struct lm80_data *lm80_update_device(struct device *dev); |
140 | static int lm80_read_value(struct i2c_client *client, u8 reg); | 141 | static int lm80_read_value(struct i2c_client *client, u8 reg); |
141 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); | 142 | static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); |
@@ -144,12 +145,22 @@ static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value); | |||
144 | * Driver data (common to all clients) | 145 | * Driver data (common to all clients) |
145 | */ | 146 | */ |
146 | 147 | ||
148 | static const struct i2c_device_id lm80_id[] = { | ||
149 | { "lm80", lm80 }, | ||
150 | { } | ||
151 | }; | ||
152 | MODULE_DEVICE_TABLE(i2c, lm80_id); | ||
153 | |||
147 | static struct i2c_driver lm80_driver = { | 154 | static struct i2c_driver lm80_driver = { |
155 | .class = I2C_CLASS_HWMON, | ||
148 | .driver = { | 156 | .driver = { |
149 | .name = "lm80", | 157 | .name = "lm80", |
150 | }, | 158 | }, |
151 | .attach_adapter = lm80_attach_adapter, | 159 | .probe = lm80_probe, |
152 | .detach_client = lm80_detach_client, | 160 | .remove = lm80_remove, |
161 | .id_table = lm80_id, | ||
162 | .detect = lm80_detect, | ||
163 | .address_data = &addr_data, | ||
153 | }; | 164 | }; |
154 | 165 | ||
155 | /* | 166 | /* |
@@ -383,13 +394,6 @@ static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13); | |||
383 | * Real code | 394 | * Real code |
384 | */ | 395 | */ |
385 | 396 | ||
386 | static int lm80_attach_adapter(struct i2c_adapter *adapter) | ||
387 | { | ||
388 | if (!(adapter->class & I2C_CLASS_HWMON)) | ||
389 | return 0; | ||
390 | return i2c_probe(adapter, &addr_data, lm80_detect); | ||
391 | } | ||
392 | |||
393 | static struct attribute *lm80_attributes[] = { | 397 | static struct attribute *lm80_attributes[] = { |
394 | &sensor_dev_attr_in0_min.dev_attr.attr, | 398 | &sensor_dev_attr_in0_min.dev_attr.attr, |
395 | &sensor_dev_attr_in1_min.dev_attr.attr, | 399 | &sensor_dev_attr_in1_min.dev_attr.attr, |
@@ -442,53 +446,46 @@ static const struct attribute_group lm80_group = { | |||
442 | .attrs = lm80_attributes, | 446 | .attrs = lm80_attributes, |
443 | }; | 447 | }; |
444 | 448 | ||
445 | static int lm80_detect(struct i2c_adapter *adapter, int address, int kind) | 449 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
450 | static int lm80_detect(struct i2c_client *client, int kind, | ||
451 | struct i2c_board_info *info) | ||
446 | { | 452 | { |
453 | struct i2c_adapter *adapter = client->adapter; | ||
447 | int i, cur; | 454 | int i, cur; |
448 | struct i2c_client *client; | ||
449 | struct lm80_data *data; | ||
450 | int err = 0; | ||
451 | const char *name; | ||
452 | 455 | ||
453 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | 456 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
454 | goto exit; | 457 | return -ENODEV; |
455 | |||
456 | /* OK. For now, we presume we have a valid client. We now create the | ||
457 | client structure, even though we cannot fill it completely yet. | ||
458 | But it allows us to access lm80_{read,write}_value. */ | ||
459 | if (!(data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL))) { | ||
460 | err = -ENOMEM; | ||
461 | goto exit; | ||
462 | } | ||
463 | |||
464 | client = &data->client; | ||
465 | i2c_set_clientdata(client, data); | ||
466 | client->addr = address; | ||
467 | client->adapter = adapter; | ||
468 | client->driver = &lm80_driver; | ||
469 | 458 | ||
470 | /* Now, we do the remaining detection. It is lousy. */ | 459 | /* Now, we do the remaining detection. It is lousy. */ |
471 | if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0) | 460 | if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0) |
472 | goto error_free; | 461 | return -ENODEV; |
473 | for (i = 0x2a; i <= 0x3d; i++) { | 462 | for (i = 0x2a; i <= 0x3d; i++) { |
474 | cur = i2c_smbus_read_byte_data(client, i); | 463 | cur = i2c_smbus_read_byte_data(client, i); |
475 | if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur) | 464 | if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur) |
476 | || (i2c_smbus_read_byte_data(client, i + 0x80) != cur) | 465 | || (i2c_smbus_read_byte_data(client, i + 0x80) != cur) |
477 | || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur)) | 466 | || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur)) |
478 | goto error_free; | 467 | return -ENODEV; |
479 | } | 468 | } |
480 | 469 | ||
481 | /* Determine the chip type - only one kind supported! */ | 470 | strlcpy(info->type, "lm80", I2C_NAME_SIZE); |
482 | kind = lm80; | ||
483 | name = "lm80"; | ||
484 | 471 | ||
485 | /* Fill in the remaining client fields */ | 472 | return 0; |
486 | strlcpy(client->name, name, I2C_NAME_SIZE); | 473 | } |
487 | mutex_init(&data->update_lock); | ||
488 | 474 | ||
489 | /* Tell the I2C layer a new client has arrived */ | 475 | static int lm80_probe(struct i2c_client *client, |
490 | if ((err = i2c_attach_client(client))) | 476 | const struct i2c_device_id *id) |
491 | goto error_free; | 477 | { |
478 | struct lm80_data *data; | ||
479 | int err; | ||
480 | |||
481 | data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL); | ||
482 | if (!data) { | ||
483 | err = -ENOMEM; | ||
484 | goto exit; | ||
485 | } | ||
486 | |||
487 | i2c_set_clientdata(client, data); | ||
488 | mutex_init(&data->update_lock); | ||
492 | 489 | ||
493 | /* Initialize the LM80 chip */ | 490 | /* Initialize the LM80 chip */ |
494 | lm80_init_client(client); | 491 | lm80_init_client(client); |
@@ -499,7 +496,7 @@ static int lm80_detect(struct i2c_adapter *adapter, int address, int kind) | |||
499 | 496 | ||
500 | /* Register sysfs hooks */ | 497 | /* Register sysfs hooks */ |
501 | if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group))) | 498 | if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group))) |
502 | goto error_detach; | 499 | goto error_free; |
503 | 500 | ||
504 | data->hwmon_dev = hwmon_device_register(&client->dev); | 501 | data->hwmon_dev = hwmon_device_register(&client->dev); |
505 | if (IS_ERR(data->hwmon_dev)) { | 502 | if (IS_ERR(data->hwmon_dev)) { |
@@ -511,23 +508,18 @@ static int lm80_detect(struct i2c_adapter *adapter, int address, int kind) | |||
511 | 508 | ||
512 | error_remove: | 509 | error_remove: |
513 | sysfs_remove_group(&client->dev.kobj, &lm80_group); | 510 | sysfs_remove_group(&client->dev.kobj, &lm80_group); |
514 | error_detach: | ||
515 | i2c_detach_client(client); | ||
516 | error_free: | 511 | error_free: |
517 | kfree(data); | 512 | kfree(data); |
518 | exit: | 513 | exit: |
519 | return err; | 514 | return err; |
520 | } | 515 | } |
521 | 516 | ||
522 | static int lm80_detach_client(struct i2c_client *client) | 517 | static int lm80_remove(struct i2c_client *client) |
523 | { | 518 | { |
524 | struct lm80_data *data = i2c_get_clientdata(client); | 519 | struct lm80_data *data = i2c_get_clientdata(client); |
525 | int err; | ||
526 | 520 | ||
527 | hwmon_device_unregister(data->hwmon_dev); | 521 | hwmon_device_unregister(data->hwmon_dev); |
528 | sysfs_remove_group(&client->dev.kobj, &lm80_group); | 522 | sysfs_remove_group(&client->dev.kobj, &lm80_group); |
529 | if ((err = i2c_detach_client(client))) | ||
530 | return err; | ||
531 | 523 | ||
532 | kfree(data); | 524 | kfree(data); |
533 | return 0; | 525 | return 0; |