lm73.c 7.5 KB

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