lm92.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. /* The LM92 registers */
  54. #define LM92_REG_CONFIG 0x01 /* 8-bit, RW */
  55. #define LM92_REG_TEMP 0x00 /* 16-bit, RO */
  56. #define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */
  57. #define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */
  58. #define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */
  59. #define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */
  60. #define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */
  61. /*
  62. * The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
  63. * left-justified in 16-bit registers. No rounding is done, with such
  64. * a resolution it's just not worth it. Note that the MAX6635 doesn't
  65. * make use of the 4 lower bits for limits (i.e. effective resolution
  66. * for limits is 1 degree Celsius).
  67. */
  68. static inline int TEMP_FROM_REG(s16 reg)
  69. {
  70. return reg / 8 * 625 / 10;
  71. }
  72. static inline s16 TEMP_TO_REG(long val)
  73. {
  74. val = clamp_val(val, -60000, 160000);
  75. return val * 10 / 625 * 8;
  76. }
  77. /* Alarm flags are stored in the 3 LSB of the temperature register */
  78. static inline u8 ALARMS_FROM_REG(s16 reg)
  79. {
  80. return reg & 0x0007;
  81. }
  82. enum temp_index {
  83. t_input,
  84. t_crit,
  85. t_min,
  86. t_max,
  87. t_hyst,
  88. t_num_regs
  89. };
  90. static const u8 regs[t_num_regs] = {
  91. [t_input] = LM92_REG_TEMP,
  92. [t_crit] = LM92_REG_TEMP_CRIT,
  93. [t_min] = LM92_REG_TEMP_LOW,
  94. [t_max] = LM92_REG_TEMP_HIGH,
  95. [t_hyst] = LM92_REG_TEMP_HYST,
  96. };
  97. /* Client data (each client gets its own) */
  98. struct lm92_data {
  99. struct i2c_client *client;
  100. struct mutex update_lock;
  101. char valid; /* zero until following fields are valid */
  102. unsigned long last_updated; /* in jiffies */
  103. /* registers values */
  104. s16 temp[t_num_regs]; /* index with enum temp_index */
  105. };
  106. /*
  107. * Sysfs attributes and callback functions
  108. */
  109. static struct lm92_data *lm92_update_device(struct device *dev)
  110. {
  111. struct lm92_data *data = dev_get_drvdata(dev);
  112. struct i2c_client *client = data->client;
  113. int i;
  114. mutex_lock(&data->update_lock);
  115. if (time_after(jiffies, data->last_updated + HZ)
  116. || !data->valid) {
  117. dev_dbg(&client->dev, "Updating lm92 data\n");
  118. for (i = 0; i < t_num_regs; i++) {
  119. data->temp[i] =
  120. i2c_smbus_read_word_swapped(client, regs[i]);
  121. }
  122. data->last_updated = jiffies;
  123. data->valid = 1;
  124. }
  125. mutex_unlock(&data->update_lock);
  126. return data;
  127. }
  128. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  129. char *buf)
  130. {
  131. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  132. struct lm92_data *data = lm92_update_device(dev);
  133. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  134. }
  135. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  136. const char *buf, size_t count)
  137. {
  138. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  139. struct lm92_data *data = dev_get_drvdata(dev);
  140. struct i2c_client *client = data->client;
  141. int nr = attr->index;
  142. long val;
  143. int err;
  144. err = kstrtol(buf, 10, &val);
  145. if (err)
  146. return err;
  147. mutex_lock(&data->update_lock);
  148. data->temp[nr] = TEMP_TO_REG(val);
  149. i2c_smbus_write_word_swapped(client, regs[nr], data->temp[nr]);
  150. mutex_unlock(&data->update_lock);
  151. return count;
  152. }
  153. static ssize_t show_temp_hyst(struct device *dev,
  154. struct device_attribute *devattr, char *buf)
  155. {
  156. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  157. struct lm92_data *data = lm92_update_device(dev);
  158. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])
  159. - TEMP_FROM_REG(data->temp[t_hyst]));
  160. }
  161. static ssize_t show_temp_min_hyst(struct device *dev,
  162. struct device_attribute *attr, char *buf)
  163. {
  164. struct lm92_data *data = lm92_update_device(dev);
  165. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[t_min])
  166. + TEMP_FROM_REG(data->temp[t_hyst]));
  167. }
  168. static ssize_t set_temp_hyst(struct device *dev,
  169. struct device_attribute *devattr,
  170. const char *buf, size_t count)
  171. {
  172. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  173. struct lm92_data *data = dev_get_drvdata(dev);
  174. struct i2c_client *client = data->client;
  175. long val;
  176. int err;
  177. err = kstrtol(buf, 10, &val);
  178. if (err)
  179. return err;
  180. val = clamp_val(val, -120000, 220000);
  181. mutex_lock(&data->update_lock);
  182. data->temp[t_hyst] =
  183. TEMP_TO_REG(TEMP_FROM_REG(data->temp[attr->index]) - val);
  184. i2c_smbus_write_word_swapped(client, LM92_REG_TEMP_HYST,
  185. data->temp[t_hyst]);
  186. mutex_unlock(&data->update_lock);
  187. return count;
  188. }
  189. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct lm92_data *data = lm92_update_device(dev);
  193. return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp[t_input]));
  194. }
  195. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  196. char *buf)
  197. {
  198. int bitnr = to_sensor_dev_attr(attr)->index;
  199. struct lm92_data *data = lm92_update_device(dev);
  200. return sprintf(buf, "%d\n", (data->temp[t_input] >> bitnr) & 1);
  201. }
  202. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
  203. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp, set_temp,
  204. t_crit);
  205. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst,
  206. set_temp_hyst, t_crit);
  207. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp,
  208. t_min);
  209. static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp_min_hyst, NULL);
  210. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp,
  211. t_max);
  212. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp_hyst, NULL, t_max);
  213. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  214. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
  215. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  216. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  217. /*
  218. * Detection and registration
  219. */
  220. static void lm92_init_client(struct i2c_client *client)
  221. {
  222. u8 config;
  223. /* Start the conversions if needed */
  224. config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
  225. if (config & 0x01)
  226. i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
  227. config & 0xFE);
  228. }
  229. /*
  230. * The MAX6635 has no identification register, so we have to use tricks
  231. * to identify it reliably. This is somewhat slow.
  232. * Note that we do NOT rely on the 2 MSB of the configuration register
  233. * always reading 0, as suggested by the datasheet, because it was once
  234. * reported not to be true.
  235. */
  236. static int max6635_check(struct i2c_client *client)
  237. {
  238. u16 temp_low, temp_high, temp_hyst, temp_crit;
  239. u8 conf;
  240. int i;
  241. /*
  242. * No manufacturer ID register, so a read from this address will
  243. * always return the last read value.
  244. */
  245. temp_low = i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW);
  246. if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_low)
  247. return 0;
  248. temp_high = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH);
  249. if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_high)
  250. return 0;
  251. /* Limits are stored as integer values (signed, 9-bit). */
  252. if ((temp_low & 0x7f00) || (temp_high & 0x7f00))
  253. return 0;
  254. temp_hyst = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST);
  255. temp_crit = i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT);
  256. if ((temp_hyst & 0x7f00) || (temp_crit & 0x7f00))
  257. return 0;
  258. /*
  259. * Registers addresses were found to cycle over 16-byte boundaries.
  260. * We don't test all registers with all offsets so as to save some
  261. * reads and time, but this should still be sufficient to dismiss
  262. * non-MAX6635 chips.
  263. */
  264. conf = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
  265. for (i = 16; i < 96; i *= 2) {
  266. if (temp_hyst != i2c_smbus_read_word_data(client,
  267. LM92_REG_TEMP_HYST + i - 16)
  268. || temp_crit != i2c_smbus_read_word_data(client,
  269. LM92_REG_TEMP_CRIT + i)
  270. || temp_low != i2c_smbus_read_word_data(client,
  271. LM92_REG_TEMP_LOW + i + 16)
  272. || temp_high != i2c_smbus_read_word_data(client,
  273. LM92_REG_TEMP_HIGH + i + 32)
  274. || conf != i2c_smbus_read_byte_data(client,
  275. LM92_REG_CONFIG + i))
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. static struct attribute *lm92_attrs[] = {
  281. &sensor_dev_attr_temp1_input.dev_attr.attr,
  282. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  283. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  284. &sensor_dev_attr_temp1_min.dev_attr.attr,
  285. &dev_attr_temp1_min_hyst.attr,
  286. &sensor_dev_attr_temp1_max.dev_attr.attr,
  287. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  288. &dev_attr_alarms.attr,
  289. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  290. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  291. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  292. NULL
  293. };
  294. ATTRIBUTE_GROUPS(lm92);
  295. /* Return 0 if detection is successful, -ENODEV otherwise */
  296. static int lm92_detect(struct i2c_client *new_client,
  297. struct i2c_board_info *info)
  298. {
  299. struct i2c_adapter *adapter = new_client->adapter;
  300. u8 config;
  301. u16 man_id;
  302. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  303. | I2C_FUNC_SMBUS_WORD_DATA))
  304. return -ENODEV;
  305. config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG);
  306. man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID);
  307. if ((config & 0xe0) == 0x00 && man_id == 0x0180)
  308. pr_info("lm92: Found National Semiconductor LM92 chip\n");
  309. else if (max6635_check(new_client))
  310. pr_info("lm92: Found Maxim MAX6635 chip\n");
  311. else
  312. return -ENODEV;
  313. strlcpy(info->type, "lm92", I2C_NAME_SIZE);
  314. return 0;
  315. }
  316. static int lm92_probe(struct i2c_client *new_client,
  317. const struct i2c_device_id *id)
  318. {
  319. struct device *hwmon_dev;
  320. struct lm92_data *data;
  321. data = devm_kzalloc(&new_client->dev, sizeof(struct lm92_data),
  322. GFP_KERNEL);
  323. if (!data)
  324. return -ENOMEM;
  325. data->client = new_client;
  326. mutex_init(&data->update_lock);
  327. /* Initialize the chipset */
  328. lm92_init_client(new_client);
  329. hwmon_dev = devm_hwmon_device_register_with_groups(&new_client->dev,
  330. new_client->name,
  331. data, lm92_groups);
  332. return PTR_ERR_OR_ZERO(hwmon_dev);
  333. }
  334. /*
  335. * Module and driver stuff
  336. */
  337. static const struct i2c_device_id lm92_id[] = {
  338. { "lm92", 0 },
  339. /* max6635 could be added here */
  340. { }
  341. };
  342. MODULE_DEVICE_TABLE(i2c, lm92_id);
  343. static struct i2c_driver lm92_driver = {
  344. .class = I2C_CLASS_HWMON,
  345. .driver = {
  346. .name = "lm92",
  347. },
  348. .probe = lm92_probe,
  349. .id_table = lm92_id,
  350. .detect = lm92_detect,
  351. .address_list = normal_i2c,
  352. };
  353. module_i2c_driver(lm92_driver);
  354. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  355. MODULE_DESCRIPTION("LM92/MAX6635 driver");
  356. MODULE_LICENSE("GPL");