lm77.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. lm77.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
  5. Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77
  6. is a temperature sensor and thermal window comparator with 0.5 deg
  7. resolution made by National Semiconductor. Complete datasheet can be
  8. obtained at their site:
  9. http://www.national.com/pf/LM/LM77.html
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/i2c.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-sysfs.h>
  29. #include <linux/err.h>
  30. #include <linux/mutex.h>
  31. /* Addresses to scan */
  32. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
  33. I2C_CLIENT_END };
  34. /* The LM77 registers */
  35. #define LM77_REG_TEMP 0x00
  36. #define LM77_REG_CONF 0x01
  37. #define LM77_REG_TEMP_HYST 0x02
  38. #define LM77_REG_TEMP_CRIT 0x03
  39. #define LM77_REG_TEMP_MIN 0x04
  40. #define LM77_REG_TEMP_MAX 0x05
  41. /* Each client has this additional data */
  42. struct lm77_data {
  43. struct device *hwmon_dev;
  44. struct mutex update_lock;
  45. char valid;
  46. unsigned long last_updated; /* In jiffies */
  47. int temp_input; /* Temperatures */
  48. int temp_crit;
  49. int temp_min;
  50. int temp_max;
  51. int temp_hyst;
  52. u8 alarms;
  53. };
  54. static int lm77_probe(struct i2c_client *client,
  55. const struct i2c_device_id *id);
  56. static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info);
  57. static void lm77_init_client(struct i2c_client *client);
  58. static int lm77_remove(struct i2c_client *client);
  59. static u16 lm77_read_value(struct i2c_client *client, u8 reg);
  60. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
  61. static struct lm77_data *lm77_update_device(struct device *dev);
  62. static const struct i2c_device_id lm77_id[] = {
  63. { "lm77", 0 },
  64. { }
  65. };
  66. MODULE_DEVICE_TABLE(i2c, lm77_id);
  67. /* This is the driver that will be inserted */
  68. static struct i2c_driver lm77_driver = {
  69. .class = I2C_CLASS_HWMON,
  70. .driver = {
  71. .name = "lm77",
  72. },
  73. .probe = lm77_probe,
  74. .remove = lm77_remove,
  75. .id_table = lm77_id,
  76. .detect = lm77_detect,
  77. .address_list = normal_i2c,
  78. };
  79. /* straight from the datasheet */
  80. #define LM77_TEMP_MIN (-55000)
  81. #define LM77_TEMP_MAX 125000
  82. /* In the temperature registers, the low 3 bits are not part of the
  83. temperature values; they are the status bits. */
  84. static inline s16 LM77_TEMP_TO_REG(int temp)
  85. {
  86. int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
  87. return (ntemp / 500) * 8;
  88. }
  89. static inline int LM77_TEMP_FROM_REG(s16 reg)
  90. {
  91. return (reg / 8) * 500;
  92. }
  93. /* sysfs stuff */
  94. /* read routines for temperature limits */
  95. #define show(value) \
  96. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  97. { \
  98. struct lm77_data *data = lm77_update_device(dev); \
  99. return sprintf(buf, "%d\n", data->value); \
  100. }
  101. show(temp_input);
  102. show(temp_crit);
  103. show(temp_min);
  104. show(temp_max);
  105. /* read routines for hysteresis values */
  106. static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  107. {
  108. struct lm77_data *data = lm77_update_device(dev);
  109. return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
  110. }
  111. static ssize_t show_temp_min_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  112. {
  113. struct lm77_data *data = lm77_update_device(dev);
  114. return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
  115. }
  116. static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  117. {
  118. struct lm77_data *data = lm77_update_device(dev);
  119. return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
  120. }
  121. /* write routines */
  122. #define set(value, reg) \
  123. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  124. { \
  125. struct i2c_client *client = to_i2c_client(dev); \
  126. struct lm77_data *data = i2c_get_clientdata(client); \
  127. long val = simple_strtol(buf, NULL, 10); \
  128. \
  129. mutex_lock(&data->update_lock); \
  130. data->value = val; \
  131. lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
  132. mutex_unlock(&data->update_lock); \
  133. return count; \
  134. }
  135. set(temp_min, LM77_REG_TEMP_MIN);
  136. set(temp_max, LM77_REG_TEMP_MAX);
  137. /* hysteresis is stored as a relative value on the chip, so it has to be
  138. converted first */
  139. static ssize_t set_temp_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  140. {
  141. struct i2c_client *client = to_i2c_client(dev);
  142. struct lm77_data *data = i2c_get_clientdata(client);
  143. unsigned long val = simple_strtoul(buf, NULL, 10);
  144. mutex_lock(&data->update_lock);
  145. data->temp_hyst = data->temp_crit - val;
  146. lm77_write_value(client, LM77_REG_TEMP_HYST,
  147. LM77_TEMP_TO_REG(data->temp_hyst));
  148. mutex_unlock(&data->update_lock);
  149. return count;
  150. }
  151. /* preserve hysteresis when setting T_crit */
  152. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  153. {
  154. struct i2c_client *client = to_i2c_client(dev);
  155. struct lm77_data *data = i2c_get_clientdata(client);
  156. long val = simple_strtoul(buf, NULL, 10);
  157. int oldcrithyst;
  158. mutex_lock(&data->update_lock);
  159. oldcrithyst = data->temp_crit - data->temp_hyst;
  160. data->temp_crit = val;
  161. data->temp_hyst = data->temp_crit - oldcrithyst;
  162. lm77_write_value(client, LM77_REG_TEMP_CRIT,
  163. LM77_TEMP_TO_REG(data->temp_crit));
  164. lm77_write_value(client, LM77_REG_TEMP_HYST,
  165. LM77_TEMP_TO_REG(data->temp_hyst));
  166. mutex_unlock(&data->update_lock);
  167. return count;
  168. }
  169. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  170. char *buf)
  171. {
  172. int bitnr = to_sensor_dev_attr(attr)->index;
  173. struct lm77_data *data = lm77_update_device(dev);
  174. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  175. }
  176. static DEVICE_ATTR(temp1_input, S_IRUGO,
  177. show_temp_input, NULL);
  178. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  179. show_temp_crit, set_temp_crit);
  180. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  181. show_temp_min, set_temp_min);
  182. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  183. show_temp_max, set_temp_max);
  184. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  185. show_temp_crit_hyst, set_temp_crit_hyst);
  186. static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
  187. show_temp_min_hyst, NULL);
  188. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
  189. show_temp_max_hyst, NULL);
  190. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
  191. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  192. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  193. static struct attribute *lm77_attributes[] = {
  194. &dev_attr_temp1_input.attr,
  195. &dev_attr_temp1_crit.attr,
  196. &dev_attr_temp1_min.attr,
  197. &dev_attr_temp1_max.attr,
  198. &dev_attr_temp1_crit_hyst.attr,
  199. &dev_attr_temp1_min_hyst.attr,
  200. &dev_attr_temp1_max_hyst.attr,
  201. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  202. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  203. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  204. NULL
  205. };
  206. static const struct attribute_group lm77_group = {
  207. .attrs = lm77_attributes,
  208. };
  209. /* Return 0 if detection is successful, -ENODEV otherwise */
  210. static int lm77_detect(struct i2c_client *new_client,
  211. struct i2c_board_info *info)
  212. {
  213. struct i2c_adapter *adapter = new_client->adapter;
  214. int i, cur, conf, hyst, crit, min, max;
  215. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  216. I2C_FUNC_SMBUS_WORD_DATA))
  217. return -ENODEV;
  218. /* Here comes the remaining detection. Since the LM77 has no
  219. register dedicated to identification, we have to rely on the
  220. following tricks:
  221. 1. the high 4 bits represent the sign and thus they should
  222. always be the same
  223. 2. the high 3 bits are unused in the configuration register
  224. 3. addresses 0x06 and 0x07 return the last read value
  225. 4. registers cycling over 8-address boundaries
  226. Word-sized registers are high-byte first. */
  227. /* addresses cycling */
  228. cur = i2c_smbus_read_word_data(new_client, 0);
  229. conf = i2c_smbus_read_byte_data(new_client, 1);
  230. hyst = i2c_smbus_read_word_data(new_client, 2);
  231. crit = i2c_smbus_read_word_data(new_client, 3);
  232. min = i2c_smbus_read_word_data(new_client, 4);
  233. max = i2c_smbus_read_word_data(new_client, 5);
  234. for (i = 8; i <= 0xff; i += 8) {
  235. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  236. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  237. || i2c_smbus_read_word_data(new_client, i + 3) != crit
  238. || i2c_smbus_read_word_data(new_client, i + 4) != min
  239. || i2c_smbus_read_word_data(new_client, i + 5) != max)
  240. return -ENODEV;
  241. }
  242. /* sign bits */
  243. if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
  244. || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
  245. || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
  246. || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
  247. || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
  248. return -ENODEV;
  249. /* unused bits */
  250. if (conf & 0xe0)
  251. return -ENODEV;
  252. /* 0x06 and 0x07 return the last read value */
  253. cur = i2c_smbus_read_word_data(new_client, 0);
  254. if (i2c_smbus_read_word_data(new_client, 6) != cur
  255. || i2c_smbus_read_word_data(new_client, 7) != cur)
  256. return -ENODEV;
  257. hyst = i2c_smbus_read_word_data(new_client, 2);
  258. if (i2c_smbus_read_word_data(new_client, 6) != hyst
  259. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  260. return -ENODEV;
  261. min = i2c_smbus_read_word_data(new_client, 4);
  262. if (i2c_smbus_read_word_data(new_client, 6) != min
  263. || i2c_smbus_read_word_data(new_client, 7) != min)
  264. return -ENODEV;
  265. strlcpy(info->type, "lm77", I2C_NAME_SIZE);
  266. return 0;
  267. }
  268. static int lm77_probe(struct i2c_client *new_client,
  269. const struct i2c_device_id *id)
  270. {
  271. struct lm77_data *data;
  272. int err;
  273. data = kzalloc(sizeof(struct lm77_data), GFP_KERNEL);
  274. if (!data) {
  275. err = -ENOMEM;
  276. goto exit;
  277. }
  278. i2c_set_clientdata(new_client, data);
  279. data->valid = 0;
  280. mutex_init(&data->update_lock);
  281. /* Initialize the LM77 chip */
  282. lm77_init_client(new_client);
  283. /* Register sysfs hooks */
  284. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm77_group)))
  285. goto exit_free;
  286. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  287. if (IS_ERR(data->hwmon_dev)) {
  288. err = PTR_ERR(data->hwmon_dev);
  289. goto exit_remove;
  290. }
  291. return 0;
  292. exit_remove:
  293. sysfs_remove_group(&new_client->dev.kobj, &lm77_group);
  294. exit_free:
  295. kfree(data);
  296. exit:
  297. return err;
  298. }
  299. static int lm77_remove(struct i2c_client *client)
  300. {
  301. struct lm77_data *data = i2c_get_clientdata(client);
  302. hwmon_device_unregister(data->hwmon_dev);
  303. sysfs_remove_group(&client->dev.kobj, &lm77_group);
  304. kfree(data);
  305. return 0;
  306. }
  307. /* All registers are word-sized, except for the configuration register.
  308. The LM77 uses the high-byte first convention. */
  309. static u16 lm77_read_value(struct i2c_client *client, u8 reg)
  310. {
  311. if (reg == LM77_REG_CONF)
  312. return i2c_smbus_read_byte_data(client, reg);
  313. else
  314. return swab16(i2c_smbus_read_word_data(client, reg));
  315. }
  316. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
  317. {
  318. if (reg == LM77_REG_CONF)
  319. return i2c_smbus_write_byte_data(client, reg, value);
  320. else
  321. return i2c_smbus_write_word_data(client, reg, swab16(value));
  322. }
  323. static void lm77_init_client(struct i2c_client *client)
  324. {
  325. /* Initialize the LM77 chip - turn off shutdown mode */
  326. int conf = lm77_read_value(client, LM77_REG_CONF);
  327. if (conf & 1)
  328. lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
  329. }
  330. static struct lm77_data *lm77_update_device(struct device *dev)
  331. {
  332. struct i2c_client *client = to_i2c_client(dev);
  333. struct lm77_data *data = i2c_get_clientdata(client);
  334. mutex_lock(&data->update_lock);
  335. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  336. || !data->valid) {
  337. dev_dbg(&client->dev, "Starting lm77 update\n");
  338. data->temp_input =
  339. LM77_TEMP_FROM_REG(lm77_read_value(client,
  340. LM77_REG_TEMP));
  341. data->temp_hyst =
  342. LM77_TEMP_FROM_REG(lm77_read_value(client,
  343. LM77_REG_TEMP_HYST));
  344. data->temp_crit =
  345. LM77_TEMP_FROM_REG(lm77_read_value(client,
  346. LM77_REG_TEMP_CRIT));
  347. data->temp_min =
  348. LM77_TEMP_FROM_REG(lm77_read_value(client,
  349. LM77_REG_TEMP_MIN));
  350. data->temp_max =
  351. LM77_TEMP_FROM_REG(lm77_read_value(client,
  352. LM77_REG_TEMP_MAX));
  353. data->alarms =
  354. lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
  355. data->last_updated = jiffies;
  356. data->valid = 1;
  357. }
  358. mutex_unlock(&data->update_lock);
  359. return data;
  360. }
  361. static int __init sensors_lm77_init(void)
  362. {
  363. return i2c_add_driver(&lm77_driver);
  364. }
  365. static void __exit sensors_lm77_exit(void)
  366. {
  367. i2c_del_driver(&lm77_driver);
  368. }
  369. MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
  370. MODULE_DESCRIPTION("LM77 driver");
  371. MODULE_LICENSE("GPL");
  372. module_init(sensors_lm77_init);
  373. module_exit(sensors_lm77_exit);