lm77.c 13 KB

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