lm92.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * lm92 - Hardware monitoring driver
  3. * Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
  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. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  41. */
  42. #include <linux/module.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/i2c.h>
  46. #include <linux/hwmon.h>
  47. #include <linux/hwmon-sysfs.h>
  48. #include <linux/err.h>
  49. #include <linux/mutex.h>
  50. /* The LM92 and MAX6635 have 2 two-state pins for address selection,
  51. resulting in 4 possible addresses. */
  52. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
  53. I2C_CLIENT_END };
  54. /* Insmod parameters */
  55. I2C_CLIENT_INSMOD_1(lm92);
  56. /* The LM92 registers */
  57. #define LM92_REG_CONFIG 0x01 /* 8-bit, RW */
  58. #define LM92_REG_TEMP 0x00 /* 16-bit, RO */
  59. #define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */
  60. #define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */
  61. #define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */
  62. #define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */
  63. #define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */
  64. /* The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius,
  65. left-justified in 16-bit registers. No rounding is done, with such
  66. a resolution it's just not worth it. Note that the MAX6635 doesn't
  67. make use of the 4 lower bits for limits (i.e. effective resolution
  68. for limits is 1 degree Celsius). */
  69. static inline int TEMP_FROM_REG(s16 reg)
  70. {
  71. return reg / 8 * 625 / 10;
  72. }
  73. static inline s16 TEMP_TO_REG(int val)
  74. {
  75. if (val <= -60000)
  76. return -60000 * 10 / 625 * 8;
  77. if (val >= 160000)
  78. return 160000 * 10 / 625 * 8;
  79. return val * 10 / 625 * 8;
  80. }
  81. /* Alarm flags are stored in the 3 LSB of the temperature register */
  82. static inline u8 ALARMS_FROM_REG(s16 reg)
  83. {
  84. return reg & 0x0007;
  85. }
  86. /* Driver data (common to all clients) */
  87. static struct i2c_driver lm92_driver;
  88. /* Client data (each client gets its own) */
  89. struct lm92_data {
  90. struct i2c_client client;
  91. struct device *hwmon_dev;
  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 temp1_input, temp1_crit, temp1_min, temp1_max, temp1_hyst;
  97. };
  98. /*
  99. * Sysfs attributes and callback functions
  100. */
  101. static struct lm92_data *lm92_update_device(struct device *dev)
  102. {
  103. struct i2c_client *client = to_i2c_client(dev);
  104. struct lm92_data *data = i2c_get_clientdata(client);
  105. mutex_lock(&data->update_lock);
  106. if (time_after(jiffies, data->last_updated + HZ)
  107. || !data->valid) {
  108. dev_dbg(&client->dev, "Updating lm92 data\n");
  109. data->temp1_input = swab16(i2c_smbus_read_word_data(client,
  110. LM92_REG_TEMP));
  111. data->temp1_hyst = swab16(i2c_smbus_read_word_data(client,
  112. LM92_REG_TEMP_HYST));
  113. data->temp1_crit = swab16(i2c_smbus_read_word_data(client,
  114. LM92_REG_TEMP_CRIT));
  115. data->temp1_min = swab16(i2c_smbus_read_word_data(client,
  116. LM92_REG_TEMP_LOW));
  117. data->temp1_max = swab16(i2c_smbus_read_word_data(client,
  118. LM92_REG_TEMP_HIGH));
  119. data->last_updated = jiffies;
  120. data->valid = 1;
  121. }
  122. mutex_unlock(&data->update_lock);
  123. return data;
  124. }
  125. #define show_temp(value) \
  126. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  127. { \
  128. struct lm92_data *data = lm92_update_device(dev); \
  129. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
  130. }
  131. show_temp(temp1_input);
  132. show_temp(temp1_crit);
  133. show_temp(temp1_min);
  134. show_temp(temp1_max);
  135. #define set_temp(value, reg) \
  136. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \
  137. size_t count) \
  138. { \
  139. struct i2c_client *client = to_i2c_client(dev); \
  140. struct lm92_data *data = i2c_get_clientdata(client); \
  141. long val = simple_strtol(buf, NULL, 10); \
  142. \
  143. mutex_lock(&data->update_lock); \
  144. data->value = TEMP_TO_REG(val); \
  145. i2c_smbus_write_word_data(client, reg, swab16(data->value)); \
  146. mutex_unlock(&data->update_lock); \
  147. return count; \
  148. }
  149. set_temp(temp1_crit, LM92_REG_TEMP_CRIT);
  150. set_temp(temp1_min, LM92_REG_TEMP_LOW);
  151. set_temp(temp1_max, LM92_REG_TEMP_HIGH);
  152. static ssize_t show_temp1_crit_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  153. {
  154. struct lm92_data *data = lm92_update_device(dev);
  155. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_crit)
  156. - TEMP_FROM_REG(data->temp1_hyst));
  157. }
  158. static ssize_t show_temp1_max_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  159. {
  160. struct lm92_data *data = lm92_update_device(dev);
  161. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_max)
  162. - TEMP_FROM_REG(data->temp1_hyst));
  163. }
  164. static ssize_t show_temp1_min_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  165. {
  166. struct lm92_data *data = lm92_update_device(dev);
  167. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp1_min)
  168. + TEMP_FROM_REG(data->temp1_hyst));
  169. }
  170. static ssize_t set_temp1_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf,
  171. size_t count)
  172. {
  173. struct i2c_client *client = to_i2c_client(dev);
  174. struct lm92_data *data = i2c_get_clientdata(client);
  175. long val = simple_strtol(buf, NULL, 10);
  176. mutex_lock(&data->update_lock);
  177. data->temp1_hyst = TEMP_FROM_REG(data->temp1_crit) - val;
  178. i2c_smbus_write_word_data(client, LM92_REG_TEMP_HYST,
  179. swab16(TEMP_TO_REG(data->temp1_hyst)));
  180. mutex_unlock(&data->update_lock);
  181. return count;
  182. }
  183. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  184. {
  185. struct lm92_data *data = lm92_update_device(dev);
  186. return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->temp1_input));
  187. }
  188. static ssize_t show_alarm(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->temp1_input >> bitnr) & 1);
  194. }
  195. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL);
  196. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp1_crit,
  197. set_temp1_crit);
  198. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp1_crit_hyst,
  199. set_temp1_crit_hyst);
  200. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp1_min,
  201. set_temp1_min);
  202. static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp1_min_hyst, NULL);
  203. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_max,
  204. set_temp1_max);
  205. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp1_max_hyst, NULL);
  206. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  207. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
  208. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  209. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  210. /*
  211. * Detection and registration
  212. */
  213. static void lm92_init_client(struct i2c_client *client)
  214. {
  215. u8 config;
  216. /* Start the conversions if needed */
  217. config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
  218. if (config & 0x01)
  219. i2c_smbus_write_byte_data(client, LM92_REG_CONFIG,
  220. config & 0xFE);
  221. }
  222. /* The MAX6635 has no identification register, so we have to use tricks
  223. to identify it reliably. This is somewhat slow.
  224. Note that we do NOT rely on the 2 MSB of the configuration register
  225. always reading 0, as suggested by the datasheet, because it was once
  226. reported not to be true. */
  227. static int max6635_check(struct i2c_client *client)
  228. {
  229. u16 temp_low, temp_high, temp_hyst, temp_crit;
  230. u8 conf;
  231. int i;
  232. /* No manufacturer ID register, so a read from this address will
  233. always return the last read value. */
  234. temp_low = i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW);
  235. if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_low)
  236. return 0;
  237. temp_high = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH);
  238. if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_high)
  239. return 0;
  240. /* Limits are stored as integer values (signed, 9-bit). */
  241. if ((temp_low & 0x7f00) || (temp_high & 0x7f00))
  242. return 0;
  243. temp_hyst = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST);
  244. temp_crit = i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT);
  245. if ((temp_hyst & 0x7f00) || (temp_crit & 0x7f00))
  246. return 0;
  247. /* Registers addresses were found to cycle over 16-byte boundaries.
  248. We don't test all registers with all offsets so as to save some
  249. reads and time, but this should still be sufficient to dismiss
  250. non-MAX6635 chips. */
  251. conf = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG);
  252. for (i=16; i<96; i*=2) {
  253. if (temp_hyst != i2c_smbus_read_word_data(client,
  254. LM92_REG_TEMP_HYST + i - 16)
  255. || temp_crit != i2c_smbus_read_word_data(client,
  256. LM92_REG_TEMP_CRIT + i)
  257. || temp_low != i2c_smbus_read_word_data(client,
  258. LM92_REG_TEMP_LOW + i + 16)
  259. || temp_high != i2c_smbus_read_word_data(client,
  260. LM92_REG_TEMP_HIGH + i + 32)
  261. || conf != i2c_smbus_read_byte_data(client,
  262. LM92_REG_CONFIG + i))
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. static struct attribute *lm92_attributes[] = {
  268. &dev_attr_temp1_input.attr,
  269. &dev_attr_temp1_crit.attr,
  270. &dev_attr_temp1_crit_hyst.attr,
  271. &dev_attr_temp1_min.attr,
  272. &dev_attr_temp1_min_hyst.attr,
  273. &dev_attr_temp1_max.attr,
  274. &dev_attr_temp1_max_hyst.attr,
  275. &dev_attr_alarms.attr,
  276. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  277. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  278. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  279. NULL
  280. };
  281. static const struct attribute_group lm92_group = {
  282. .attrs = lm92_attributes,
  283. };
  284. /* The following function does more than just detection. If detection
  285. succeeds, it also registers the new chip. */
  286. static int lm92_detect(struct i2c_adapter *adapter, int address, int kind)
  287. {
  288. struct i2c_client *new_client;
  289. struct lm92_data *data;
  290. int err = 0;
  291. char *name;
  292. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  293. | I2C_FUNC_SMBUS_WORD_DATA))
  294. goto exit;
  295. if (!(data = kzalloc(sizeof(struct lm92_data), GFP_KERNEL))) {
  296. err = -ENOMEM;
  297. goto exit;
  298. }
  299. /* Fill in enough client fields so that we can read from the chip,
  300. which is required for identication */
  301. new_client = &data->client;
  302. i2c_set_clientdata(new_client, data);
  303. new_client->addr = address;
  304. new_client->adapter = adapter;
  305. new_client->driver = &lm92_driver;
  306. new_client->flags = 0;
  307. /* A negative kind means that the driver was loaded with no force
  308. parameter (default), so we must identify the chip. */
  309. if (kind < 0) {
  310. u8 config = i2c_smbus_read_byte_data(new_client,
  311. LM92_REG_CONFIG);
  312. u16 man_id = i2c_smbus_read_word_data(new_client,
  313. LM92_REG_MAN_ID);
  314. if ((config & 0xe0) == 0x00
  315. && man_id == 0x0180) {
  316. pr_info("lm92: Found National Semiconductor LM92 chip\n");
  317. kind = lm92;
  318. } else
  319. if (max6635_check(new_client)) {
  320. pr_info("lm92: Found Maxim MAX6635 chip\n");
  321. kind = lm92; /* No separate prefix */
  322. }
  323. else
  324. goto exit_free;
  325. } else
  326. if (kind == 0) /* Default to an LM92 if forced */
  327. kind = lm92;
  328. /* Give it the proper name */
  329. if (kind == lm92) {
  330. name = "lm92";
  331. } else { /* Supposedly cannot happen */
  332. dev_dbg(&new_client->dev, "Kind out of range?\n");
  333. goto exit_free;
  334. }
  335. /* Fill in the remaining client fields */
  336. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  337. data->valid = 0;
  338. mutex_init(&data->update_lock);
  339. /* Tell the i2c subsystem a new client has arrived */
  340. if ((err = i2c_attach_client(new_client)))
  341. goto exit_free;
  342. /* Initialize the chipset */
  343. lm92_init_client(new_client);
  344. /* Register sysfs hooks */
  345. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm92_group)))
  346. goto exit_detach;
  347. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  348. if (IS_ERR(data->hwmon_dev)) {
  349. err = PTR_ERR(data->hwmon_dev);
  350. goto exit_remove;
  351. }
  352. return 0;
  353. exit_remove:
  354. sysfs_remove_group(&new_client->dev.kobj, &lm92_group);
  355. exit_detach:
  356. i2c_detach_client(new_client);
  357. exit_free:
  358. kfree(data);
  359. exit:
  360. return err;
  361. }
  362. static int lm92_attach_adapter(struct i2c_adapter *adapter)
  363. {
  364. if (!(adapter->class & I2C_CLASS_HWMON))
  365. return 0;
  366. return i2c_probe(adapter, &addr_data, lm92_detect);
  367. }
  368. static int lm92_detach_client(struct i2c_client *client)
  369. {
  370. struct lm92_data *data = i2c_get_clientdata(client);
  371. int err;
  372. hwmon_device_unregister(data->hwmon_dev);
  373. sysfs_remove_group(&client->dev.kobj, &lm92_group);
  374. if ((err = i2c_detach_client(client)))
  375. return err;
  376. kfree(data);
  377. return 0;
  378. }
  379. /*
  380. * Module and driver stuff
  381. */
  382. static struct i2c_driver lm92_driver = {
  383. .driver = {
  384. .name = "lm92",
  385. },
  386. .attach_adapter = lm92_attach_adapter,
  387. .detach_client = lm92_detach_client,
  388. };
  389. static int __init sensors_lm92_init(void)
  390. {
  391. return i2c_add_driver(&lm92_driver);
  392. }
  393. static void __exit sensors_lm92_exit(void)
  394. {
  395. i2c_del_driver(&lm92_driver);
  396. }
  397. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  398. MODULE_DESCRIPTION("LM92/MAX6635 driver");
  399. MODULE_LICENSE("GPL");
  400. module_init(sensors_lm92_init);
  401. module_exit(sensors_lm92_exit);