lm73.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * LM73 Sensor driver
  3. * Based on LM75
  4. *
  5. * Copyright (C) 2007, CenoSYS (www.cenosys.com).
  6. * Copyright (C) 2009, Bollore telecom (www.bolloretelecom.eu).
  7. *
  8. * Guillaume Ligneul <guillaume.ligneul@gmail.com>
  9. * Adrien Demarez <adrien.demarez@bolloretelecom.eu>
  10. * Jeremy Laine <jeremy.laine@bolloretelecom.eu>
  11. *
  12. * This software program is licensed subject to the GNU General Public License
  13. * (GPL).Version 2,June 1991, available at
  14. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. /* Addresses scanned */
  24. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c,
  25. 0x4d, 0x4e, I2C_CLIENT_END };
  26. /* LM73 registers */
  27. #define LM73_REG_INPUT 0x00
  28. #define LM73_REG_CONF 0x01
  29. #define LM73_REG_MAX 0x02
  30. #define LM73_REG_MIN 0x03
  31. #define LM73_REG_CTRL 0x04
  32. #define LM73_REG_ID 0x07
  33. #define LM73_ID 0x9001 /* or 0x190 after a swab16() */
  34. #define DRVNAME "lm73"
  35. #define LM73_TEMP_MIN (-40)
  36. #define LM73_TEMP_MAX 150
  37. /*-----------------------------------------------------------------------*/
  38. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  39. const char *buf, size_t count)
  40. {
  41. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  42. struct i2c_client *client = to_i2c_client(dev);
  43. long temp;
  44. short value;
  45. int status = strict_strtol(buf, 10, &temp);
  46. if (status < 0)
  47. return status;
  48. /* Write value */
  49. value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
  50. (LM73_TEMP_MAX*4)) << 5;
  51. i2c_smbus_write_word_data(client, attr->index, swab16(value));
  52. return count;
  53. }
  54. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  55. char *buf)
  56. {
  57. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  58. struct i2c_client *client = to_i2c_client(dev);
  59. /* use integer division instead of equivalent right shift to
  60. guarantee arithmetic shift and preserve the sign */
  61. int temp = ((s16) (swab16(i2c_smbus_read_word_data(client,
  62. attr->index)))*250) / 32;
  63. return sprintf(buf, "%d\n", temp);
  64. }
  65. /*-----------------------------------------------------------------------*/
  66. /* sysfs attributes for hwmon */
  67. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  68. show_temp, set_temp, LM73_REG_MAX);
  69. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  70. show_temp, set_temp, LM73_REG_MIN);
  71. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  72. show_temp, NULL, LM73_REG_INPUT);
  73. static struct attribute *lm73_attributes[] = {
  74. &sensor_dev_attr_temp1_input.dev_attr.attr,
  75. &sensor_dev_attr_temp1_max.dev_attr.attr,
  76. &sensor_dev_attr_temp1_min.dev_attr.attr,
  77. NULL
  78. };
  79. static const struct attribute_group lm73_group = {
  80. .attrs = lm73_attributes,
  81. };
  82. /*-----------------------------------------------------------------------*/
  83. /* device probe and removal */
  84. static int
  85. lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
  86. {
  87. struct device *hwmon_dev;
  88. int status;
  89. /* Register sysfs hooks */
  90. status = sysfs_create_group(&client->dev.kobj, &lm73_group);
  91. if (status)
  92. return status;
  93. hwmon_dev = hwmon_device_register(&client->dev);
  94. if (IS_ERR(hwmon_dev)) {
  95. status = PTR_ERR(hwmon_dev);
  96. goto exit_remove;
  97. }
  98. i2c_set_clientdata(client, hwmon_dev);
  99. dev_info(&client->dev, "%s: sensor '%s'\n",
  100. dev_name(hwmon_dev), client->name);
  101. return 0;
  102. exit_remove:
  103. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  104. return status;
  105. }
  106. static int lm73_remove(struct i2c_client *client)
  107. {
  108. struct device *hwmon_dev = i2c_get_clientdata(client);
  109. hwmon_device_unregister(hwmon_dev);
  110. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  111. i2c_set_clientdata(client, NULL);
  112. return 0;
  113. }
  114. static const struct i2c_device_id lm73_ids[] = {
  115. { "lm73", 0 },
  116. { /* LIST END */ }
  117. };
  118. MODULE_DEVICE_TABLE(i2c, lm73_ids);
  119. /* Return 0 if detection is successful, -ENODEV otherwise */
  120. static int lm73_detect(struct i2c_client *new_client,
  121. struct i2c_board_info *info)
  122. {
  123. struct i2c_adapter *adapter = new_client->adapter;
  124. u16 id;
  125. u8 ctrl;
  126. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  127. I2C_FUNC_SMBUS_WORD_DATA))
  128. return -ENODEV;
  129. /* Check device ID */
  130. id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
  131. ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
  132. if ((id != LM73_ID) || (ctrl & 0x10))
  133. return -ENODEV;
  134. strlcpy(info->type, "lm73", I2C_NAME_SIZE);
  135. return 0;
  136. }
  137. static struct i2c_driver lm73_driver = {
  138. .class = I2C_CLASS_HWMON,
  139. .driver = {
  140. .name = "lm73",
  141. },
  142. .probe = lm73_probe,
  143. .remove = lm73_remove,
  144. .id_table = lm73_ids,
  145. .detect = lm73_detect,
  146. .address_list = normal_i2c,
  147. };
  148. /* module glue */
  149. static int __init sensors_lm73_init(void)
  150. {
  151. return i2c_add_driver(&lm73_driver);
  152. }
  153. static void __exit sensors_lm73_exit(void)
  154. {
  155. i2c_del_driver(&lm73_driver);
  156. }
  157. MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
  158. MODULE_DESCRIPTION("LM73 driver");
  159. MODULE_LICENSE("GPL");
  160. module_init(sensors_lm73_init);
  161. module_exit(sensors_lm73_exit);