lm73.c 4.9 KB

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