lm92.c 10 KB

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