lm73.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * Chris Verges <kg4ysn@gmail.com>
  12. *
  13. * This software program is licensed subject to the GNU General Public License
  14. * (GPL).Version 2,June 1991, available at
  15. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.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 /* 0x0190, byte-swapped */
  34. #define DRVNAME "lm73"
  35. #define LM73_TEMP_MIN (-256000 / 250)
  36. #define LM73_TEMP_MAX (255750 / 250)
  37. #define LM73_CTRL_RES_SHIFT 5
  38. #define LM73_CTRL_RES_MASK (BIT(5) | BIT(6))
  39. #define LM73_CTRL_TO_MASK BIT(7)
  40. #define LM73_CTRL_HI_SHIFT 2
  41. #define LM73_CTRL_LO_SHIFT 1
  42. static const unsigned short lm73_convrates[] = {
  43. 14, /* 11-bits (0.25000 C/LSB): RES1 Bit = 0, RES0 Bit = 0 */
  44. 28, /* 12-bits (0.12500 C/LSB): RES1 Bit = 0, RES0 Bit = 1 */
  45. 56, /* 13-bits (0.06250 C/LSB): RES1 Bit = 1, RES0 Bit = 0 */
  46. 112, /* 14-bits (0.03125 C/LSB): RES1 Bit = 1, RES0 Bit = 1 */
  47. };
  48. struct lm73_data {
  49. struct i2c_client *client;
  50. struct mutex lock;
  51. u8 ctrl; /* control register value */
  52. };
  53. /*-----------------------------------------------------------------------*/
  54. static ssize_t set_temp(struct device *dev, struct device_attribute *da,
  55. const char *buf, size_t count)
  56. {
  57. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  58. struct lm73_data *data = dev_get_drvdata(dev);
  59. long temp;
  60. short value;
  61. s32 err;
  62. int status = kstrtol(buf, 10, &temp);
  63. if (status < 0)
  64. return status;
  65. /* Write value */
  66. value = clamp_val(temp / 250, LM73_TEMP_MIN, LM73_TEMP_MAX) << 5;
  67. err = i2c_smbus_write_word_swapped(data->client, attr->index, value);
  68. return (err < 0) ? err : count;
  69. }
  70. static ssize_t show_temp(struct device *dev, struct device_attribute *da,
  71. char *buf)
  72. {
  73. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  74. struct lm73_data *data = dev_get_drvdata(dev);
  75. int temp;
  76. s32 err = i2c_smbus_read_word_swapped(data->client, attr->index);
  77. if (err < 0)
  78. return err;
  79. /* use integer division instead of equivalent right shift to
  80. guarantee arithmetic shift and preserve the sign */
  81. temp = (((s16) err) * 250) / 32;
  82. return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
  83. }
  84. static ssize_t set_convrate(struct device *dev, struct device_attribute *da,
  85. const char *buf, size_t count)
  86. {
  87. struct lm73_data *data = dev_get_drvdata(dev);
  88. unsigned long convrate;
  89. s32 err;
  90. int res = 0;
  91. err = kstrtoul(buf, 10, &convrate);
  92. if (err < 0)
  93. return err;
  94. /*
  95. * Convert the desired conversion rate into register bits.
  96. * res is already initialized, and everything past the second-to-last
  97. * value in the array is treated as belonging to the last value
  98. * in the array.
  99. */
  100. while (res < (ARRAY_SIZE(lm73_convrates) - 1) &&
  101. convrate > lm73_convrates[res])
  102. res++;
  103. mutex_lock(&data->lock);
  104. data->ctrl &= LM73_CTRL_TO_MASK;
  105. data->ctrl |= res << LM73_CTRL_RES_SHIFT;
  106. err = i2c_smbus_write_byte_data(data->client, LM73_REG_CTRL,
  107. data->ctrl);
  108. mutex_unlock(&data->lock);
  109. if (err < 0)
  110. return err;
  111. return count;
  112. }
  113. static ssize_t show_convrate(struct device *dev, struct device_attribute *da,
  114. char *buf)
  115. {
  116. struct lm73_data *data = dev_get_drvdata(dev);
  117. int res;
  118. res = (data->ctrl & LM73_CTRL_RES_MASK) >> LM73_CTRL_RES_SHIFT;
  119. return scnprintf(buf, PAGE_SIZE, "%hu\n", lm73_convrates[res]);
  120. }
  121. static ssize_t show_maxmin_alarm(struct device *dev,
  122. struct device_attribute *da, char *buf)
  123. {
  124. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  125. struct lm73_data *data = dev_get_drvdata(dev);
  126. s32 ctrl;
  127. mutex_lock(&data->lock);
  128. ctrl = i2c_smbus_read_byte_data(data->client, LM73_REG_CTRL);
  129. if (ctrl < 0)
  130. goto abort;
  131. data->ctrl = ctrl;
  132. mutex_unlock(&data->lock);
  133. return scnprintf(buf, PAGE_SIZE, "%d\n", (ctrl >> attr->index) & 1);
  134. abort:
  135. mutex_unlock(&data->lock);
  136. return ctrl;
  137. }
  138. /*-----------------------------------------------------------------------*/
  139. /* sysfs attributes for hwmon */
  140. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  141. show_temp, set_temp, LM73_REG_MAX);
  142. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  143. show_temp, set_temp, LM73_REG_MIN);
  144. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  145. show_temp, NULL, LM73_REG_INPUT);
  146. static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
  147. show_convrate, set_convrate, 0);
  148. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
  149. show_maxmin_alarm, NULL, LM73_CTRL_HI_SHIFT);
  150. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
  151. show_maxmin_alarm, NULL, LM73_CTRL_LO_SHIFT);
  152. static struct attribute *lm73_attrs[] = {
  153. &sensor_dev_attr_temp1_input.dev_attr.attr,
  154. &sensor_dev_attr_temp1_max.dev_attr.attr,
  155. &sensor_dev_attr_temp1_min.dev_attr.attr,
  156. &sensor_dev_attr_update_interval.dev_attr.attr,
  157. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  158. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  159. NULL
  160. };
  161. ATTRIBUTE_GROUPS(lm73);
  162. /*-----------------------------------------------------------------------*/
  163. /* device probe and removal */
  164. static int
  165. lm73_probe(struct i2c_client *client, const struct i2c_device_id *id)
  166. {
  167. struct device *dev = &client->dev;
  168. struct device *hwmon_dev;
  169. struct lm73_data *data;
  170. int ctrl;
  171. data = devm_kzalloc(dev, sizeof(struct lm73_data), GFP_KERNEL);
  172. if (!data)
  173. return -ENOMEM;
  174. data->client = client;
  175. mutex_init(&data->lock);
  176. ctrl = i2c_smbus_read_byte_data(client, LM73_REG_CTRL);
  177. if (ctrl < 0)
  178. return ctrl;
  179. data->ctrl = ctrl;
  180. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  181. data, lm73_groups);
  182. if (IS_ERR(hwmon_dev))
  183. return PTR_ERR(hwmon_dev);
  184. dev_info(dev, "sensor '%s'\n", client->name);
  185. return 0;
  186. }
  187. static const struct i2c_device_id lm73_ids[] = {
  188. { "lm73", 0 },
  189. { /* LIST END */ }
  190. };
  191. MODULE_DEVICE_TABLE(i2c, lm73_ids);
  192. /* Return 0 if detection is successful, -ENODEV otherwise */
  193. static int lm73_detect(struct i2c_client *new_client,
  194. struct i2c_board_info *info)
  195. {
  196. struct i2c_adapter *adapter = new_client->adapter;
  197. int id, ctrl, conf;
  198. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  199. I2C_FUNC_SMBUS_WORD_DATA))
  200. return -ENODEV;
  201. /*
  202. * Do as much detection as possible with byte reads first, as word
  203. * reads can confuse other devices.
  204. */
  205. ctrl = i2c_smbus_read_byte_data(new_client, LM73_REG_CTRL);
  206. if (ctrl < 0 || (ctrl & 0x10))
  207. return -ENODEV;
  208. conf = i2c_smbus_read_byte_data(new_client, LM73_REG_CONF);
  209. if (conf < 0 || (conf & 0x0c))
  210. return -ENODEV;
  211. id = i2c_smbus_read_byte_data(new_client, LM73_REG_ID);
  212. if (id < 0 || id != (LM73_ID & 0xff))
  213. return -ENODEV;
  214. /* Check device ID */
  215. id = i2c_smbus_read_word_data(new_client, LM73_REG_ID);
  216. if (id < 0 || id != LM73_ID)
  217. return -ENODEV;
  218. strlcpy(info->type, "lm73", I2C_NAME_SIZE);
  219. return 0;
  220. }
  221. static struct i2c_driver lm73_driver = {
  222. .class = I2C_CLASS_HWMON,
  223. .driver = {
  224. .name = "lm73",
  225. },
  226. .probe = lm73_probe,
  227. .id_table = lm73_ids,
  228. .detect = lm73_detect,
  229. .address_list = normal_i2c,
  230. };
  231. module_i2c_driver(lm73_driver);
  232. MODULE_AUTHOR("Guillaume Ligneul <guillaume.ligneul@gmail.com>");
  233. MODULE_DESCRIPTION("LM73 driver");
  234. MODULE_LICENSE("GPL");