lm73.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 /* 0x0190, byte-swapped */
  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. s32 err;
  45. int status = kstrtol(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. err = i2c_smbus_write_word_swapped(client, attr->index, value);
  52. return (err < 0) ? err : 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. int temp;
  60. s32 err = i2c_smbus_read_word_swapped(client, attr->index);
  61. if (err < 0)
  62. return err;
  63. /* use integer division instead of equivalent right shift to
  64. guarantee arithmetic shift and preserve the sign */
  65. temp = (((s16) err) * 250) / 32;
  66. return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
  67. }
  68. /*-----------------------------------------------------------------------*/
  69. /* sysfs attributes for hwmon */
  70. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  71. show_temp, set_temp, LM73_REG_MAX);
  72. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  73. show_temp, set_temp, LM73_REG_MIN);
  74. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  75. show_temp, NULL, LM73_REG_INPUT);
  76. static struct attribute *lm73_attributes[] = {
  77. &sensor_dev_attr_temp1_input.dev_attr.attr,
  78. &sensor_dev_attr_temp1_max.dev_attr.attr,
  79. &sensor_dev_attr_temp1_min.dev_attr.attr,
  80. NULL
  81. };
  82. static const struct attribute_group lm73_group = {
  83. .attrs = lm73_attributes,
  84. };
  85. /*-----------------------------------------------------------------------*/
  86. /* device probe and removal */
  87. static int
  88. lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
  89. {
  90. struct device *hwmon_dev;
  91. int status;
  92. /* Register sysfs hooks */
  93. status = sysfs_create_group(&client->dev.kobj, &lm73_group);
  94. if (status)
  95. return status;
  96. hwmon_dev = hwmon_device_register(&client->dev);
  97. if (IS_ERR(hwmon_dev)) {
  98. status = PTR_ERR(hwmon_dev);
  99. goto exit_remove;
  100. }
  101. i2c_set_clientdata(client, hwmon_dev);
  102. dev_info(&client->dev, "%s: sensor '%s'\n",
  103. dev_name(hwmon_dev), client->name);
  104. return 0;
  105. exit_remove:
  106. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  107. return status;
  108. }
  109. static int lm73_remove(struct i2c_client *client)
  110. {
  111. struct device *hwmon_dev = i2c_get_clientdata(client);
  112. hwmon_device_unregister(hwmon_dev);
  113. sysfs_remove_group(&client->dev.kobj, &lm73_group);
  114. return 0;
  115. }
  116. static const struct i2c_device_id lm73_ids[] = {
  117. { "lm73", 0 },
  118. { /* LIST END */ }
  119. };
  120. MODULE_DEVICE_TABLE(i2c, lm73_ids);
  121. /* Return 0 if detection is successful, -ENODEV otherwise */
  122. static int lm73_detect(struct i2c_client *new_client,
  123. struct i2c_board_info *info)
  124. {
  125. struct i2c_adapter *adapter = new_client->adapter;
  126. int id, ctrl, conf;
  127. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  128. I2C_FUNC_SMBUS_WORD_DATA))
  129. return -ENODEV;
  130. /*
  131. * Do as much detection as possible with byte reads first, as word
  132. * reads can confuse other devices.
  133. */
  134. ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
  135. if (ctrl < 0 || (ctrl & 0x10))
  136. return -ENODEV;
  137. conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
  138. if (conf < 0 || (conf & 0x0c))
  139. return -ENODEV;
  140. id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
  141. if (id < 0 || id != (LM73_ID & 0xff))
  142. return -ENODEV;
  143. /* Check device ID */
  144. id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
  145. if (id < 0 || id != LM73_ID)
  146. return -ENODEV;
  147. strlcpy(info->type, "lm73", I2C_NAME_SIZE);
  148. return 0;
  149. }
  150. static struct i2c_driver lm73_driver = {
  151. .class = I2C_CLASS_HWMON,
  152. .driver = {
  153. .name = "lm73",
  154. },
  155. .probe = lm73_probe,
  156. .remove = lm73_remove,
  157. .id_table = lm73_ids,
  158. .detect = lm73_detect,
  159. .address_list = normal_i2c,
  160. };
  161. module_i2c_driver(lm73_driver);
  162. MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
  163. MODULE_DESCRIPTION("LM73 driver");
  164. MODULE_LICENSE("GPL");