diff options
author | Jean Delvare <khali@linux-fr.org> | 2008-07-16 13:30:16 -0400 |
---|---|---|
committer | Jean Delvare <khali@mahadeva.delvare> | 2008-07-16 13:30:16 -0400 |
commit | ccf37488322429bf8709f2227f3d48466add2b6b (patch) | |
tree | 1e65b455354e2c7c5e78952a0748622937503e98 /drivers/hwmon/thmc50.c | |
parent | 8fb597bb6ec80d53836229bf3576c7b848b909e3 (diff) |
hwmon: (thmc50) Convert to a new-style i2c driver
The new-style thmc50 driver implements the optional detect()
callback to cover the use cases of the legacy driver.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Cc: Krzysztof Helt <krzysztof.h1@wp.pl>
Diffstat (limited to 'drivers/hwmon/thmc50.c')
-rw-r--r-- | drivers/hwmon/thmc50.c | 107 |
1 files changed, 53 insertions, 54 deletions
diff --git a/drivers/hwmon/thmc50.c b/drivers/hwmon/thmc50.c index 76a3859c3fbe..3b01001108c1 100644 --- a/drivers/hwmon/thmc50.c +++ b/drivers/hwmon/thmc50.c | |||
@@ -60,7 +60,6 @@ static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B }; | |||
60 | 60 | ||
61 | /* Each client has this additional data */ | 61 | /* Each client has this additional data */ |
62 | struct thmc50_data { | 62 | struct thmc50_data { |
63 | struct i2c_client client; | ||
64 | struct device *hwmon_dev; | 63 | struct device *hwmon_dev; |
65 | 64 | ||
66 | struct mutex update_lock; | 65 | struct mutex update_lock; |
@@ -77,17 +76,31 @@ struct thmc50_data { | |||
77 | u8 alarms; | 76 | u8 alarms; |
78 | }; | 77 | }; |
79 | 78 | ||
80 | static int thmc50_attach_adapter(struct i2c_adapter *adapter); | 79 | static int thmc50_detect(struct i2c_client *client, int kind, |
81 | static int thmc50_detach_client(struct i2c_client *client); | 80 | struct i2c_board_info *info); |
81 | static int thmc50_probe(struct i2c_client *client, | ||
82 | const struct i2c_device_id *id); | ||
83 | static int thmc50_remove(struct i2c_client *client); | ||
82 | static void thmc50_init_client(struct i2c_client *client); | 84 | static void thmc50_init_client(struct i2c_client *client); |
83 | static struct thmc50_data *thmc50_update_device(struct device *dev); | 85 | static struct thmc50_data *thmc50_update_device(struct device *dev); |
84 | 86 | ||
87 | static const struct i2c_device_id thmc50_id[] = { | ||
88 | { "adm1022", adm1022 }, | ||
89 | { "thmc50", thmc50 }, | ||
90 | { } | ||
91 | }; | ||
92 | MODULE_DEVICE_TABLE(i2c, thmc50_id); | ||
93 | |||
85 | static struct i2c_driver thmc50_driver = { | 94 | static struct i2c_driver thmc50_driver = { |
95 | .class = I2C_CLASS_HWMON, | ||
86 | .driver = { | 96 | .driver = { |
87 | .name = "thmc50", | 97 | .name = "thmc50", |
88 | }, | 98 | }, |
89 | .attach_adapter = thmc50_attach_adapter, | 99 | .probe = thmc50_probe, |
90 | .detach_client = thmc50_detach_client, | 100 | .remove = thmc50_remove, |
101 | .id_table = thmc50_id, | ||
102 | .detect = thmc50_detect, | ||
103 | .address_data = &addr_data, | ||
91 | }; | 104 | }; |
92 | 105 | ||
93 | static ssize_t show_analog_out(struct device *dev, | 106 | static ssize_t show_analog_out(struct device *dev, |
@@ -250,39 +263,23 @@ static const struct attribute_group temp3_group = { | |||
250 | .attrs = temp3_attributes, | 263 | .attrs = temp3_attributes, |
251 | }; | 264 | }; |
252 | 265 | ||
253 | static int thmc50_detect(struct i2c_adapter *adapter, int address, int kind) | 266 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
267 | static int thmc50_detect(struct i2c_client *client, int kind, | ||
268 | struct i2c_board_info *info) | ||
254 | { | 269 | { |
255 | unsigned company; | 270 | unsigned company; |
256 | unsigned revision; | 271 | unsigned revision; |
257 | unsigned config; | 272 | unsigned config; |
258 | struct i2c_client *client; | 273 | struct i2c_adapter *adapter = client->adapter; |
259 | struct thmc50_data *data; | ||
260 | struct device *dev; | ||
261 | int err = 0; | 274 | int err = 0; |
262 | const char *type_name; | 275 | const char *type_name; |
263 | 276 | ||
264 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | 277 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
265 | pr_debug("thmc50: detect failed, " | 278 | pr_debug("thmc50: detect failed, " |
266 | "smbus byte data not supported!\n"); | 279 | "smbus byte data not supported!\n"); |
267 | goto exit; | 280 | return -ENODEV; |
268 | } | ||
269 | |||
270 | /* OK. For now, we presume we have a valid client. We now create the | ||
271 | client structure, even though we cannot fill it completely yet. | ||
272 | But it allows us to access thmc50 registers. */ | ||
273 | if (!(data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL))) { | ||
274 | pr_debug("thmc50: detect failed, kzalloc failed!\n"); | ||
275 | err = -ENOMEM; | ||
276 | goto exit; | ||
277 | } | 281 | } |
278 | 282 | ||
279 | client = &data->client; | ||
280 | i2c_set_clientdata(client, data); | ||
281 | client->addr = address; | ||
282 | client->adapter = adapter; | ||
283 | client->driver = &thmc50_driver; | ||
284 | dev = &client->dev; | ||
285 | |||
286 | pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n", | 283 | pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n", |
287 | client->addr, i2c_adapter_id(client->adapter)); | 284 | client->addr, i2c_adapter_id(client->adapter)); |
288 | 285 | ||
@@ -307,21 +304,22 @@ static int thmc50_detect(struct i2c_adapter *adapter, int address, int kind) | |||
307 | } | 304 | } |
308 | if (err == -ENODEV) { | 305 | if (err == -ENODEV) { |
309 | pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n"); | 306 | pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n"); |
310 | goto exit_free; | 307 | return err; |
311 | } | 308 | } |
312 | data->type = kind; | ||
313 | 309 | ||
314 | if (kind == adm1022) { | 310 | if (kind == adm1022) { |
315 | int id = i2c_adapter_id(client->adapter); | 311 | int id = i2c_adapter_id(client->adapter); |
316 | int i; | 312 | int i; |
317 | 313 | ||
318 | type_name = "adm1022"; | 314 | type_name = "adm1022"; |
319 | data->has_temp3 = (config >> 7) & 1; /* config MSB */ | ||
320 | for (i = 0; i + 1 < adm1022_temp3_num; i += 2) | 315 | for (i = 0; i + 1 < adm1022_temp3_num; i += 2) |
321 | if (adm1022_temp3[i] == id && | 316 | if (adm1022_temp3[i] == id && |
322 | adm1022_temp3[i + 1] == address) { | 317 | adm1022_temp3[i + 1] == client->addr) { |
323 | /* enable 2nd remote temp */ | 318 | /* enable 2nd remote temp */ |
324 | data->has_temp3 = 1; | 319 | config |= (1 << 7); |
320 | i2c_smbus_write_byte_data(client, | ||
321 | THMC50_REG_CONF, | ||
322 | config); | ||
325 | break; | 323 | break; |
326 | } | 324 | } |
327 | } else { | 325 | } else { |
@@ -330,19 +328,33 @@ static int thmc50_detect(struct i2c_adapter *adapter, int address, int kind) | |||
330 | pr_debug("thmc50: Detected %s (version %x, revision %x)\n", | 328 | pr_debug("thmc50: Detected %s (version %x, revision %x)\n", |
331 | type_name, (revision >> 4) - 0xc, revision & 0xf); | 329 | type_name, (revision >> 4) - 0xc, revision & 0xf); |
332 | 330 | ||
333 | /* Fill in the remaining client fields & put it into the global list */ | 331 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
334 | strlcpy(client->name, type_name, I2C_NAME_SIZE); | ||
335 | mutex_init(&data->update_lock); | ||
336 | 332 | ||
337 | /* Tell the I2C layer a new client has arrived */ | 333 | return 0; |
338 | if ((err = i2c_attach_client(client))) | 334 | } |
339 | goto exit_free; | 335 | |
336 | static int thmc50_probe(struct i2c_client *client, | ||
337 | const struct i2c_device_id *id) | ||
338 | { | ||
339 | struct thmc50_data *data; | ||
340 | int err; | ||
341 | |||
342 | data = kzalloc(sizeof(struct thmc50_data), GFP_KERNEL); | ||
343 | if (!data) { | ||
344 | pr_debug("thmc50: detect failed, kzalloc failed!\n"); | ||
345 | err = -ENOMEM; | ||
346 | goto exit; | ||
347 | } | ||
348 | |||
349 | i2c_set_clientdata(client, data); | ||
350 | data->type = id->driver_data; | ||
351 | mutex_init(&data->update_lock); | ||
340 | 352 | ||
341 | thmc50_init_client(client); | 353 | thmc50_init_client(client); |
342 | 354 | ||
343 | /* Register sysfs hooks */ | 355 | /* Register sysfs hooks */ |
344 | if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group))) | 356 | if ((err = sysfs_create_group(&client->dev.kobj, &thmc50_group))) |
345 | goto exit_detach; | 357 | goto exit_free; |
346 | 358 | ||
347 | /* Register ADM1022 sysfs hooks */ | 359 | /* Register ADM1022 sysfs hooks */ |
348 | if (data->has_temp3) | 360 | if (data->has_temp3) |
@@ -364,34 +376,21 @@ exit_remove_sysfs: | |||
364 | sysfs_remove_group(&client->dev.kobj, &temp3_group); | 376 | sysfs_remove_group(&client->dev.kobj, &temp3_group); |
365 | exit_remove_sysfs_thmc50: | 377 | exit_remove_sysfs_thmc50: |
366 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); | 378 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); |
367 | exit_detach: | ||
368 | i2c_detach_client(client); | ||
369 | exit_free: | 379 | exit_free: |
370 | kfree(data); | 380 | kfree(data); |
371 | exit: | 381 | exit: |
372 | return err; | 382 | return err; |
373 | } | 383 | } |
374 | 384 | ||
375 | static int thmc50_attach_adapter(struct i2c_adapter *adapter) | 385 | static int thmc50_remove(struct i2c_client *client) |
376 | { | ||
377 | if (!(adapter->class & I2C_CLASS_HWMON)) | ||
378 | return 0; | ||
379 | return i2c_probe(adapter, &addr_data, thmc50_detect); | ||
380 | } | ||
381 | |||
382 | static int thmc50_detach_client(struct i2c_client *client) | ||
383 | { | 386 | { |
384 | struct thmc50_data *data = i2c_get_clientdata(client); | 387 | struct thmc50_data *data = i2c_get_clientdata(client); |
385 | int err; | ||
386 | 388 | ||
387 | hwmon_device_unregister(data->hwmon_dev); | 389 | hwmon_device_unregister(data->hwmon_dev); |
388 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); | 390 | sysfs_remove_group(&client->dev.kobj, &thmc50_group); |
389 | if (data->has_temp3) | 391 | if (data->has_temp3) |
390 | sysfs_remove_group(&client->dev.kobj, &temp3_group); | 392 | sysfs_remove_group(&client->dev.kobj, &temp3_group); |
391 | 393 | ||
392 | if ((err = i2c_detach_client(client))) | ||
393 | return err; | ||
394 | |||
395 | kfree(data); | 394 | kfree(data); |
396 | 395 | ||
397 | return 0; | 396 | return 0; |
@@ -412,8 +411,8 @@ static void thmc50_init_client(struct i2c_client *client) | |||
412 | } | 411 | } |
413 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); | 412 | config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF); |
414 | config |= 0x1; /* start the chip if it is in standby mode */ | 413 | config |= 0x1; /* start the chip if it is in standby mode */ |
415 | if (data->has_temp3) | 414 | if (data->type == adm1022 && (config & (1 << 7))) |
416 | config |= 0x80; /* enable 2nd remote temp */ | 415 | data->has_temp3 = 1; |
417 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); | 416 | i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config); |
418 | } | 417 | } |
419 | 418 | ||