lm77.c 13 KB

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