lm92.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lm92 - Hardware monitoring driver
  4. * Copyright (C) 2005-2008 Jean Delvare <jdelvare@suse.de>
  5. *
  6. * Based on the lm90 driver, with some ideas taken from the lm_sensors
  7. * lm92 driver as well.
  8. *
  9. * The LM92 is a sensor chip made by National Semiconductor. It reports
  10. * its own temperature with a 0.0625 deg resolution and a 0.33 deg
  11. * accuracy. Complete datasheet can be obtained from National's website
  12. * at:
  13. * http://www.national.com/pf/LM/LM92.html
  14. *
  15. * This driver also supports the MAX6635 sensor chip made by Maxim.
  16. * This chip is compatible with the LM92, but has a lesser accuracy
  17. * (1.0 deg). Complete datasheet can be obtained from Maxim's website
  18. * at:
  19. * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074
  20. *
  21. * Since the LM92 was the first chipset supported by this driver, most
  22. * comments will refer to this chipset, but are actually general and
  23. * concern all supported chipsets, unless mentioned otherwise.
  24. *
  25. * Support could easily be added for the National Semiconductor LM76
  26. * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible
  27. * with the LM92.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/i2c.h>
  33. #include <linux/hwmon.h>
  34. #include <linux/hwmon-sysfs.h>
  35. #include <linux/err.h>
  36. #include <linux/mutex.h>
  37. #include <linux/jiffies.h>
  38. /*
  39. * The LM92 and MAX6635 have 2 two-state pins for address selection,
  40. * resulting in 4 possible addresses.
  41. */
  42. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
  43. I2C_CLIENT_END };
  44. enum chips { lm92, max6635 };
  45. /* The LM92 registers */
  46. #define LM92_REG_CONFIG 0x01 /* 8-bit, RW */
  47. #define LM92_REG_TEMP 0x00 /* 16-bit, RO */
  48. #define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */
  49. #define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */
  50. #define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */
  51. #define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */
  52. #define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */
  53. /*
  54. * The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
  55. * left-justified in 16-bit registers. No rounding is done, with such
  56. * a resolution it's just not worth it. Note that the MAX6635 doesn't
  57. * make use of the 4 lower bits for limits (i.e. effective resolution
  58. * for limits is 1 degree Celsius).
  59. */
  60. static inline int TEMP_FROM_REG(s16 reg)
  61. {
  62. return reg / 8 * 625 / 10;
  63. }
  64. static inline s16 TEMP_TO_REG(long val)
  65. {
  66. val = clamp_val(val, -60000, 160000);
  67. return val * 10 / 625 * 8;
  68. }
  69. /* Alarm flags are stored in the 3 LSB of the temperature register */
  70. static inline u8 ALARMS_FROM_REG(s16 reg)
  71. {
  72. return reg & 0x0007;
  73. }
  74. enum temp_index {
  75. t_input,
  76. t_crit,
  77. t_min,
  78. t_max,
  79. t_hyst,
  80. t_num_regs
  81. };
  82. static const u8 regs[t_num_regs] = {
  83. [t_input] = LM92_REG_TEMP,
  84. [t_crit] = LM92_REG_TEMP_CRIT,
  85. [t_min] = LM92_REG_TEMP_LOW,
  86. [t_max] = LM92_REG_TEMP_HIGH,
  87. [t_hyst] = LM92_REG_TEMP_HYST,
  88. };
  89. /* Client data (each client gets its own) */
  90. struct lm92_data {
  91. struct i2c_client *client;
  92. struct mutex update_lock;
  93. char valid; /* zero until following fields are valid */
  94. unsigned long last_updated; /* in jiffies */
  95. /* registers values */
  96. s16 temp[t_num_regs]; /* index with enum temp_index */
  97. };
  98. /*
  99. * Sysfs attributes and callback functions
  100. */
  101. static struct lm92_data *lm92_update_device(struct device *dev)
  102. {
  103. struct lm92_data *data = dev_get_drvdata(dev);
  104. struct i2c_client *client = data->client;
  105. int i;
  106. mutex_lock(&data->update_lock);
  107. if (time_after(jiffies, data->last_updated + HZ) ||
  108. !data->valid) {
  109. dev_dbg(&client->dev, "Updating lm92 data\n");
  110. for (i = 0; i < t_num_regs; i++) {
  111. data->temp[i] =
  112. i2c_smbus_read_word_swapped(client, regs[i]);
  113. }
  114. data->last_updated = jiffies;
  115. data->valid = 1;
  116. }
  117. mutex_unlock(&data->update_lock);
  118. return data;
  119. }
  120. static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
  121. char *buf)
  122. {
  123. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  124. struct lm92_data *data = lm92_update_device(dev);
  125. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  126. }
  127. static ssize_t temp_store(struct device *dev,
  128. struct device_attribute *devattr, const char *buf,
  129. size_t count)
  130. {
  131. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  132. struct lm92_data *data = dev_get_drvdata(dev);
  133. struct i2c_client *client = data->client;
  134. int nr = attr->index;
  135. long val;
  136. int err;
  137. err = kstrtol(buf, 10, &val);
  138. if (err)
  139. return err;
  140. mutex_lock(&data->update_lock);
  141. data->temp[nr] = TEMP_TO_REG(val);
  142. i2c_smbus_write_word_swapped(client, regs[nr], data->temp[nr]);
  143. mutex_unlock(&data->update_lock);
  144. return count;
  145. }
  146. static ssize_t temp_hyst_show(struct device *dev,
  147. struct device_attribute *devattr, char *buf)
  148. {
  149. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  150. struct lm92_data *data = lm92_update_device(dev);
  151. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])
  152. - TEMP_FROM_REG(data->temp[t_hyst]));
  153. }
  154. static ssize_t temp1_min_hyst_show(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. struct lm92_data *data = lm92_update_device(dev);
  158. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[t_min])
  159. + TEMP_FROM_REG(data->temp[t_hyst]));
  160. }
  161. static ssize_t temp_hyst_store(struct device *dev,
  162. struct device_attribute *devattr,
  163. const char *buf, size_t count)
  164. {
  165. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  166. struct lm92_data *data = dev_get_drvdata(dev);
  167. struct i2c_client *client = data->client;
  168. long val;
  169. int err;
  170. err = kstrtol(buf, 10, &val);
  171. if (err)
  172. return err;
  173. val = clamp_val(val, -120000, 220000);
  174. mutex_lock(&data->update_lock);
  175. data->temp[t_hyst] =
  176. TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
  177. i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
  178. data->temp[t_hyst]);
  179. mutex_unlock(&data->update_lock);
  180. return count;
  181. }
  182. static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
  183. char *buf)
  184. {
  185. struct lm92_data *data = lm92_update_device(dev);
  186. return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp[t_input]));
  187. }
  188. static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
  189. char *buf)
  190. {
  191. int bitnr = to_sensor_dev_attr(attr)->index;
  192. struct lm92_data *data = lm92_update_device(dev);
  193. return sprintf(buf, "%d\n", (data->temp[t_input] >> bitnr) & 1);
  194. }
  195. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
  196. static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_crit);
  197. static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_hyst, t_crit);
  198. static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, t_min);
  199. static DEVICE_ATTR_RO(temp1_min_hyst);
  200. static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_max);
  201. static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, temp_hyst, t_max);
  202. static DEVICE_ATTR_RO(alarms);
  203. static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 2);
  204. static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, alarm, 0);
  205. static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 1);
  206. /*
  207. * Detection and registration
  208. */
  209. static void lm92_init_client(struct i2c_client *client)
  210. {
  211. u8 config;
  212. /* Start the conversions if needed */
  213. config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
  214. if (config & 0x01)
  215. i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
  216. config & 0xFE);
  217. }
  218. static struct attribute *lm92_attrs[] = {
  219. &sensor_dev_attr_temp1_input.dev_attr.attr,
  220. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  221. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  222. &sensor_dev_attr_temp1_min.dev_attr.attr,
  223. &dev_attr_temp1_min_hyst.attr,
  224. &sensor_dev_attr_temp1_max.dev_attr.attr,
  225. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  226. &dev_attr_alarms.attr,
  227. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  228. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  229. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  230. NULL
  231. };
  232. ATTRIBUTE_GROUPS(lm92);
  233. /* Return 0 if detection is successful, -ENODEV otherwise */
  234. static int lm92_detect(struct i2c_client *new_client,
  235. struct i2c_board_info *info)
  236. {
  237. struct i2c_adapter *adapter = new_client->adapter;
  238. u8 config;
  239. u16 man_id;
  240. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  241. | I2C_FUNC_SMBUS_WORD_DATA))
  242. return -ENODEV;
  243. config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG);
  244. man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID);
  245. if ((config & 0xe0) == 0x00 && man_id == 0x0180)
  246. pr_info("lm92: Found National Semiconductor LM92 chip\n");
  247. else
  248. return -ENODEV;
  249. strlcpy(info->type, "lm92", I2C_NAME_SIZE);
  250. return 0;
  251. }
  252. static int lm92_probe(struct i2c_client *new_client)
  253. {
  254. struct device *hwmon_dev;
  255. struct lm92_data *data;
  256. data = devm_kzalloc(&new_client->dev, sizeof(struct lm92_data),
  257. GFP_KERNEL);
  258. if (!data)
  259. return -ENOMEM;
  260. data->client = new_client;
  261. mutex_init(&data->update_lock);
  262. /* Initialize the chipset */
  263. lm92_init_client(new_client);
  264. hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
  265. new_client->name,
  266. data, lm92_groups);
  267. return PTR_ERR_OR_ZERO(hwmon_dev);
  268. }
  269. /*
  270. * Module and driver stuff
  271. */
  272. static const struct i2c_device_id lm92_id[] = {
  273. { "lm92", lm92 },
  274. { "max6635", max6635 },
  275. { }
  276. };
  277. MODULE_DEVICE_TABLE(i2c, lm92_id);
  278. static struct i2c_driver lm92_driver = {
  279. .class = I2C_CLASS_HWMON,
  280. .driver = {
  281. .name = "lm92",
  282. },
  283. .probe_new = lm92_probe,
  284. .id_table = lm92_id,
  285. .detect = lm92_detect,
  286. .address_list = normal_i2c,
  287. };
  288. module_i2c_driver(lm92_driver);
  289. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  290. MODULE_DESCRIPTION("LM92/MAX6635 driver");
  291. MODULE_LICENSE("GPL");