lm92.c 13 KB

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