lm95241.c (93776a8ec746cf9d32c36e5a5b23d28d8be28826) lm95241.c (797eaa4b0242a41cb6bc913890b9ec22ec8788ed)
1/*
2 * lm95241.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2008 Davide Rizzo <elpa-rizzo@gmail.com>
5 *
6 * Based on the max1619 driver. The LM95241 is a sensor chip made by National
7 * Semiconductors.
8 * It reports up to three temperatures (its own plus up to

--- 73 unchanged lines hidden (view full) ---

82#define MANUFACTURER_ID 0x01
83#define DEFAULT_REVISION 0xA4
84
85/* Conversions and various macros */
86#define TEMP_FROM_REG(val_h, val_l) (((val_h) & 0x80 ? (val_h) - 0x100 : \
87 (val_h)) * 1000 + (val_l) * 1000 / 256)
88
89/* Functions declaration */
1/*
2 * lm95241.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2008 Davide Rizzo <elpa-rizzo@gmail.com>
5 *
6 * Based on the max1619 driver. The LM95241 is a sensor chip made by National
7 * Semiconductors.
8 * It reports up to three temperatures (its own plus up to

--- 73 unchanged lines hidden (view full) ---

82#define MANUFACTURER_ID 0x01
83#define DEFAULT_REVISION 0xA4
84
85/* Conversions and various macros */
86#define TEMP_FROM_REG(val_h, val_l) (((val_h) & 0x80 ? (val_h) - 0x100 : \
87 (val_h)) * 1000 + (val_l) * 1000 / 256)
88
89/* Functions declaration */
90static int lm95241_attach_adapter(struct i2c_adapter *adapter);
91static int lm95241_detect(struct i2c_adapter *adapter, int address,
92 int kind);
93static void lm95241_init_client(struct i2c_client *client);
90static void lm95241_init_client(struct i2c_client *client);
94static int lm95241_detach_client(struct i2c_client *client);
95static struct lm95241_data *lm95241_update_device(struct device *dev);
96
91static struct lm95241_data *lm95241_update_device(struct device *dev);
92
97/* Driver data (common to all clients) */
98static struct i2c_driver lm95241_driver = {
99 .driver = {
100 .name = "lm95241",
101 },
102 .attach_adapter = lm95241_attach_adapter,
103 .detach_client = lm95241_detach_client,
104};
105
106/* Client data (each client gets its own) */
107struct lm95241_data {
93/* Client data (each client gets its own) */
94struct lm95241_data {
108 struct i2c_client client;
109 struct device *hwmon_dev;
110 struct mutex update_lock;
111 unsigned long last_updated, rate; /* in jiffies */
112 char valid; /* zero until following fields are valid */
113 /* registers values */
114 u8 local_h, local_l; /* local */
115 u8 remote1_h, remote1_l; /* remote1 */
116 u8 remote2_h, remote2_l; /* remote2 */

--- 201 unchanged lines hidden (view full) ---

318 &dev_attr_rate.attr,
319 NULL
320};
321
322static const struct attribute_group lm95241_group = {
323 .attrs = lm95241_attributes,
324};
325
95 struct device *hwmon_dev;
96 struct mutex update_lock;
97 unsigned long last_updated, rate; /* in jiffies */
98 char valid; /* zero until following fields are valid */
99 /* registers values */
100 u8 local_h, local_l; /* local */
101 u8 remote1_h, remote1_l; /* remote1 */
102 u8 remote2_h, remote2_l; /* remote2 */

--- 201 unchanged lines hidden (view full) ---

304 &dev_attr_rate.attr,
305 NULL
306};
307
308static const struct attribute_group lm95241_group = {
309 .attrs = lm95241_attributes,
310};
311
326/* Init/exit code */
327static int lm95241_attach_adapter(struct i2c_adapter *adapter)
312/* Return 0 if detection is successful, -ENODEV otherwise */
313static int lm95241_detect(struct i2c_client *new_client, int kind,
314 struct i2c_board_info *info)
328{
315{
329 if (!(adapter->class & I2C_CLASS_HWMON))
330 return 0;
331 return i2c_probe(adapter, &addr_data, lm95241_detect);
332}
333
334/*
335 * The following function does more than just detection. If detection
336 * succeeds, it also registers the new chip.
337 */
338static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
339{
340 struct i2c_client *new_client;
341 struct lm95241_data *data;
342 int err = 0;
316 struct i2c_adapter *adapter = new_client->adapter;
317 int address = new_client->addr;
343 const char *name = "";
344
345 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
318 const char *name = "";
319
320 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
346 goto exit;
321 return -ENODEV;
347
322
348 data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
349 if (!data) {
350 err = -ENOMEM;
351 goto exit;
352 }
353
354 /* The common I2C client data is placed right before the
355 LM95241-specific data. */
356 new_client = &data->client;
357 i2c_set_clientdata(new_client, data);
358 new_client->addr = address;
359 new_client->adapter = adapter;
360 new_client->driver = &lm95241_driver;
361 new_client->flags = 0;
362
363 /*
364 * Now we do the remaining detection. A negative kind means that
365 * the driver was loaded with no force parameter (default), so we
366 * must both detect and identify the chip. A zero kind means that
367 * the driver was loaded with the force parameter, the detection
368 * step shall be skipped. A positive kind means that the driver
369 * was loaded with the force parameter and a given kind of chip is
370 * requested, so both the detection and the identification steps
371 * are skipped.
372 */
373 if (kind < 0) { /* detection */
374 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
375 != MANUFACTURER_ID)
376 || (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
377 < DEFAULT_REVISION)) {
378 dev_dbg(&adapter->dev,
379 "LM95241 detection failed at 0x%02x.\n",
380 address);
323 /*
324 * Now we do the remaining detection. A negative kind means that
325 * the driver was loaded with no force parameter (default), so we
326 * must both detect and identify the chip. A zero kind means that
327 * the driver was loaded with the force parameter, the detection
328 * step shall be skipped. A positive kind means that the driver
329 * was loaded with the force parameter and a given kind of chip is
330 * requested, so both the detection and the identification steps
331 * are skipped.
332 */
333 if (kind < 0) { /* detection */
334 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
335 != MANUFACTURER_ID)
336 || (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
337 < DEFAULT_REVISION)) {
338 dev_dbg(&adapter->dev,
339 "LM95241 detection failed at 0x%02x.\n",
340 address);
381 goto exit_free;
341 return -ENODEV;
382 }
383 }
384
385 if (kind <= 0) { /* identification */
386 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
387 == MANUFACTURER_ID)
388 && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
389 >= DEFAULT_REVISION)) {
390
391 kind = lm95241;
392
393 if (kind <= 0) { /* identification failed */
394 dev_info(&adapter->dev, "Unsupported chip\n");
342 }
343 }
344
345 if (kind <= 0) { /* identification */
346 if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
347 == MANUFACTURER_ID)
348 && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
349 >= DEFAULT_REVISION)) {
350
351 kind = lm95241;
352
353 if (kind <= 0) { /* identification failed */
354 dev_info(&adapter->dev, "Unsupported chip\n");
395 goto exit_free;
355 return -ENODEV;
396 }
397 }
398 }
399
356 }
357 }
358 }
359
360 /* Fill the i2c board info */
400 if (kind == lm95241)
401 name = "lm95241";
361 if (kind == lm95241)
362 name = "lm95241";
363 strlcpy(info->type, name, I2C_NAME_SIZE);
364 return 0;
365}
402
366
403 /* We can fill in the remaining client fields */
404 strlcpy(new_client->name, name, I2C_NAME_SIZE);
405 data->valid = 0;
406 mutex_init(&data->update_lock);
367static int lm95241_probe(struct i2c_client *new_client,
368 const struct i2c_device_id *id)
369{
370 struct lm95241_data *data;
371 int err;
407
372
408 /* Tell the I2C layer a new client has arrived */
409 err = i2c_attach_client(new_client);
410 if (err)
411 goto exit_free;
373 data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
374 if (!data) {
375 err = -ENOMEM;
376 goto exit;
377 }
412
378
379 i2c_set_clientdata(new_client, data);
380 mutex_init(&data->update_lock);
381
413 /* Initialize the LM95241 chip */
414 lm95241_init_client(new_client);
415
416 /* Register sysfs hooks */
417 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
418 if (err)
382 /* Initialize the LM95241 chip */
383 lm95241_init_client(new_client);
384
385 /* Register sysfs hooks */
386 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
387 if (err)
419 goto exit_detach;
388 goto exit_free;
420
421 data->hwmon_dev = hwmon_device_register(&new_client->dev);
422 if (IS_ERR(data->hwmon_dev)) {
423 err = PTR_ERR(data->hwmon_dev);
424 goto exit_remove_files;
425 }
426
427 return 0;
428
429exit_remove_files:
430 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
389
390 data->hwmon_dev = hwmon_device_register(&new_client->dev);
391 if (IS_ERR(data->hwmon_dev)) {
392 err = PTR_ERR(data->hwmon_dev);
393 goto exit_remove_files;
394 }
395
396 return 0;
397
398exit_remove_files:
399 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
431exit_detach:
432 i2c_detach_client(new_client);
433exit_free:
434 kfree(data);
435exit:
436 return err;
437}
438
439static void lm95241_init_client(struct i2c_client *client)
440{

--- 10 unchanged lines hidden (view full) ---

451 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
452 R1FE_MASK | R2FE_MASK);
453 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
454 data->trutherm);
455 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
456 data->model);
457}
458
400exit_free:
401 kfree(data);
402exit:
403 return err;
404}
405
406static void lm95241_init_client(struct i2c_client *client)
407{

--- 10 unchanged lines hidden (view full) ---

418 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
419 R1FE_MASK | R2FE_MASK);
420 i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
421 data->trutherm);
422 i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
423 data->model);
424}
425
459static int lm95241_detach_client(struct i2c_client *client)
426static int lm95241_remove(struct i2c_client *client)
460{
461 struct lm95241_data *data = i2c_get_clientdata(client);
427{
428 struct lm95241_data *data = i2c_get_clientdata(client);
462 int err;
463
464 hwmon_device_unregister(data->hwmon_dev);
465 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
466
429
430 hwmon_device_unregister(data->hwmon_dev);
431 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
432
467 err = i2c_detach_client(client);
468 if (err)
469 return err;
470
433 i2c_set_clientdata(client, NULL);
471 kfree(data);
472 return 0;
473}
474
475static struct lm95241_data *lm95241_update_device(struct device *dev)
476{
477 struct i2c_client *client = to_i2c_client(dev);
478 struct lm95241_data *data = i2c_get_clientdata(client);

--- 25 unchanged lines hidden (view full) ---

504 data->valid = 1;
505 }
506
507 mutex_unlock(&data->update_lock);
508
509 return data;
510}
511
434 kfree(data);
435 return 0;
436}
437
438static struct lm95241_data *lm95241_update_device(struct device *dev)
439{
440 struct i2c_client *client = to_i2c_client(dev);
441 struct lm95241_data *data = i2c_get_clientdata(client);

--- 25 unchanged lines hidden (view full) ---

467 data->valid = 1;
468 }
469
470 mutex_unlock(&data->update_lock);
471
472 return data;
473}
474
475/* Driver data (common to all clients) */
476static const struct i2c_device_id lm95241_id[] = {
477 { "lm95241", lm95241 },
478 { }
479};
480MODULE_DEVICE_TABLE(i2c, lm95241_id);
481
482static struct i2c_driver lm95241_driver = {
483 .class = I2C_CLASS_HWMON,
484 .driver = {
485 .name = "lm95241",
486 },
487 .probe = lm95241_probe,
488 .remove = lm95241_remove,
489 .id_table = lm95241_id,
490 .detect = lm95241_detect,
491 .address_data = &addr_data,
492};
493
512static int __init sensors_lm95241_init(void)
513{
514 return i2c_add_driver(&lm95241_driver);
515}
516
517static void __exit sensors_lm95241_exit(void)
518{
519 i2c_del_driver(&lm95241_driver);
520}
521
522MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
523MODULE_DESCRIPTION("LM95241 sensor driver");
524MODULE_LICENSE("GPL");
525
526module_init(sensors_lm95241_init);
527module_exit(sensors_lm95241_exit);
494static int __init sensors_lm95241_init(void)
495{
496 return i2c_add_driver(&lm95241_driver);
497}
498
499static void __exit sensors_lm95241_exit(void)
500{
501 i2c_del_driver(&lm95241_driver);
502}
503
504MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
505MODULE_DESCRIPTION("LM95241 sensor driver");
506MODULE_LICENSE("GPL");
507
508module_init(sensors_lm95241_init);
509module_exit(sensors_lm95241_exit);