lm77.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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/i2c-sensor.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/err.h>
  30. /* Addresses to scan */
  31. static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, I2C_CLIENT_END };
  32. /* Insmod parameters */
  33. SENSORS_INSMOD_1(lm77);
  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 i2c_client client;
  44. struct class_device *class_dev;
  45. struct semaphore update_lock;
  46. char valid;
  47. unsigned long last_updated; /* In jiffies */
  48. int temp_input; /* Temperatures */
  49. int temp_crit;
  50. int temp_min;
  51. int temp_max;
  52. int temp_hyst;
  53. u8 alarms;
  54. };
  55. static int lm77_attach_adapter(struct i2c_adapter *adapter);
  56. static int lm77_detect(struct i2c_adapter *adapter, int address, int kind);
  57. static void lm77_init_client(struct i2c_client *client);
  58. static int lm77_detach_client(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. /* This is the driver that will be inserted */
  63. static struct i2c_driver lm77_driver = {
  64. .owner = THIS_MODULE,
  65. .name = "lm77",
  66. .flags = I2C_DF_NOTIFY,
  67. .attach_adapter = lm77_attach_adapter,
  68. .detach_client = lm77_detach_client,
  69. };
  70. /* straight from the datasheet */
  71. #define LM77_TEMP_MIN (-55000)
  72. #define LM77_TEMP_MAX 125000
  73. /* In the temperature registers, the low 3 bits are not part of the
  74. temperature values; they are the status bits. */
  75. static inline u16 LM77_TEMP_TO_REG(int temp)
  76. {
  77. int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
  78. return (u16)((ntemp / 500) * 8);
  79. }
  80. static inline int LM77_TEMP_FROM_REG(u16 reg)
  81. {
  82. return ((int)reg / 8) * 500;
  83. }
  84. /* sysfs stuff */
  85. /* read routines for temperature limits */
  86. #define show(value) \
  87. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  88. { \
  89. struct lm77_data *data = lm77_update_device(dev); \
  90. return sprintf(buf, "%d\n", data->value); \
  91. }
  92. show(temp_input);
  93. show(temp_crit);
  94. show(temp_min);
  95. show(temp_max);
  96. show(alarms);
  97. /* read routines for hysteresis values */
  98. static ssize_t show_temp_crit_hyst(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->temp_crit - data->temp_hyst);
  102. }
  103. static ssize_t show_temp_min_hyst(struct device *dev, struct device_attribute *attr, char *buf)
  104. {
  105. struct lm77_data *data = lm77_update_device(dev);
  106. return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
  107. }
  108. static ssize_t show_temp_max_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_max - data->temp_hyst);
  112. }
  113. /* write routines */
  114. #define set(value, reg) \
  115. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  116. { \
  117. struct i2c_client *client = to_i2c_client(dev); \
  118. struct lm77_data *data = i2c_get_clientdata(client); \
  119. long val = simple_strtoul(buf, NULL, 10); \
  120. \
  121. down(&data->update_lock); \
  122. data->value = val; \
  123. lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
  124. up(&data->update_lock); \
  125. return count; \
  126. }
  127. set(temp_min, LM77_REG_TEMP_MIN);
  128. set(temp_max, LM77_REG_TEMP_MAX);
  129. /* hysteresis is stored as a relative value on the chip, so it has to be
  130. converted first */
  131. static ssize_t set_temp_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  132. {
  133. struct i2c_client *client = to_i2c_client(dev);
  134. struct lm77_data *data = i2c_get_clientdata(client);
  135. unsigned long val = simple_strtoul(buf, NULL, 10);
  136. down(&data->update_lock);
  137. data->temp_hyst = data->temp_crit - val;
  138. lm77_write_value(client, LM77_REG_TEMP_HYST,
  139. LM77_TEMP_TO_REG(data->temp_hyst));
  140. up(&data->update_lock);
  141. return count;
  142. }
  143. /* preserve hysteresis when setting T_crit */
  144. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  145. {
  146. struct i2c_client *client = to_i2c_client(dev);
  147. struct lm77_data *data = i2c_get_clientdata(client);
  148. long val = simple_strtoul(buf, NULL, 10);
  149. int oldcrithyst;
  150. down(&data->update_lock);
  151. oldcrithyst = data->temp_crit - data->temp_hyst;
  152. data->temp_crit = val;
  153. data->temp_hyst = data->temp_crit - oldcrithyst;
  154. lm77_write_value(client, LM77_REG_TEMP_CRIT,
  155. LM77_TEMP_TO_REG(data->temp_crit));
  156. lm77_write_value(client, LM77_REG_TEMP_HYST,
  157. LM77_TEMP_TO_REG(data->temp_hyst));
  158. up(&data->update_lock);
  159. return count;
  160. }
  161. static DEVICE_ATTR(temp1_input, S_IRUGO,
  162. show_temp_input, NULL);
  163. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  164. show_temp_crit, set_temp_crit);
  165. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  166. show_temp_min, set_temp_min);
  167. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  168. show_temp_max, set_temp_max);
  169. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  170. show_temp_crit_hyst, set_temp_crit_hyst);
  171. static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
  172. show_temp_min_hyst, NULL);
  173. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
  174. show_temp_max_hyst, NULL);
  175. static DEVICE_ATTR(alarms, S_IRUGO,
  176. show_alarms, NULL);
  177. static int lm77_attach_adapter(struct i2c_adapter *adapter)
  178. {
  179. if (!(adapter->class & I2C_CLASS_HWMON))
  180. return 0;
  181. return i2c_detect(adapter, &addr_data, lm77_detect);
  182. }
  183. /* This function is called by i2c_detect */
  184. static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
  185. {
  186. struct i2c_client *new_client;
  187. struct lm77_data *data;
  188. int err = 0;
  189. const char *name = "";
  190. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  191. I2C_FUNC_SMBUS_WORD_DATA))
  192. goto exit;
  193. /* OK. For now, we presume we have a valid client. We now create the
  194. client structure, even though we cannot fill it completely yet.
  195. But it allows us to access lm77_{read,write}_value. */
  196. if (!(data = kmalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
  197. err = -ENOMEM;
  198. goto exit;
  199. }
  200. memset(data, 0, sizeof(struct lm77_data));
  201. new_client = &data->client;
  202. i2c_set_clientdata(new_client, data);
  203. new_client->addr = address;
  204. new_client->adapter = adapter;
  205. new_client->driver = &lm77_driver;
  206. new_client->flags = 0;
  207. /* Here comes the remaining detection. Since the LM77 has no
  208. register dedicated to identification, we have to rely on the
  209. following tricks:
  210. 1. the high 4 bits represent the sign and thus they should
  211. always be the same
  212. 2. the high 3 bits are unused in the configuration register
  213. 3. addresses 0x06 and 0x07 return the last read value
  214. 4. registers cycling over 8-address boundaries
  215. Word-sized registers are high-byte first. */
  216. if (kind < 0) {
  217. int i, cur, conf, hyst, crit, min, max;
  218. /* addresses cycling */
  219. cur = i2c_smbus_read_word_data(new_client, 0);
  220. conf = i2c_smbus_read_byte_data(new_client, 1);
  221. hyst = i2c_smbus_read_word_data(new_client, 2);
  222. crit = i2c_smbus_read_word_data(new_client, 3);
  223. min = i2c_smbus_read_word_data(new_client, 4);
  224. max = i2c_smbus_read_word_data(new_client, 5);
  225. for (i = 8; i <= 0xff; i += 8)
  226. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  227. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  228. || i2c_smbus_read_word_data(new_client, i + 3) != crit
  229. || i2c_smbus_read_word_data(new_client, i + 4) != min
  230. || i2c_smbus_read_word_data(new_client, i + 5) != max)
  231. goto exit_free;
  232. /* sign bits */
  233. if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
  234. || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
  235. || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
  236. || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
  237. || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
  238. goto exit_free;
  239. /* unused bits */
  240. if (conf & 0xe0)
  241. goto exit_free;
  242. /* 0x06 and 0x07 return the last read value */
  243. cur = i2c_smbus_read_word_data(new_client, 0);
  244. if (i2c_smbus_read_word_data(new_client, 6) != cur
  245. || i2c_smbus_read_word_data(new_client, 7) != cur)
  246. goto exit_free;
  247. hyst = i2c_smbus_read_word_data(new_client, 2);
  248. if (i2c_smbus_read_word_data(new_client, 6) != hyst
  249. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  250. goto exit_free;
  251. min = i2c_smbus_read_word_data(new_client, 4);
  252. if (i2c_smbus_read_word_data(new_client, 6) != min
  253. || i2c_smbus_read_word_data(new_client, 7) != min)
  254. goto exit_free;
  255. }
  256. /* Determine the chip type - only one kind supported! */
  257. if (kind <= 0)
  258. kind = lm77;
  259. if (kind == lm77) {
  260. name = "lm77";
  261. }
  262. /* Fill in the remaining client fields and put it into the global list */
  263. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  264. data->valid = 0;
  265. init_MUTEX(&data->update_lock);
  266. /* Tell the I2C layer a new client has arrived */
  267. if ((err = i2c_attach_client(new_client)))
  268. goto exit_free;
  269. /* Initialize the LM77 chip */
  270. lm77_init_client(new_client);
  271. /* Register sysfs hooks */
  272. data->class_dev = hwmon_device_register(&new_client->dev);
  273. if (IS_ERR(data->class_dev)) {
  274. err = PTR_ERR(data->class_dev);
  275. goto exit_detach;
  276. }
  277. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  278. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  279. device_create_file(&new_client->dev, &dev_attr_temp1_min);
  280. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  281. device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
  282. device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
  283. device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
  284. device_create_file(&new_client->dev, &dev_attr_alarms);
  285. return 0;
  286. exit_detach:
  287. i2c_detach_client(new_client);
  288. exit_free:
  289. kfree(data);
  290. exit:
  291. return err;
  292. }
  293. static int lm77_detach_client(struct i2c_client *client)
  294. {
  295. struct lm77_data *data = i2c_get_clientdata(client);
  296. hwmon_device_unregister(data->class_dev);
  297. i2c_detach_client(client);
  298. kfree(data);
  299. return 0;
  300. }
  301. /* All registers are word-sized, except for the configuration register.
  302. The LM77 uses the high-byte first convention. */
  303. static u16 lm77_read_value(struct i2c_client *client, u8 reg)
  304. {
  305. if (reg == LM77_REG_CONF)
  306. return i2c_smbus_read_byte_data(client, reg);
  307. else
  308. return swab16(i2c_smbus_read_word_data(client, reg));
  309. }
  310. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
  311. {
  312. if (reg == LM77_REG_CONF)
  313. return i2c_smbus_write_byte_data(client, reg, value);
  314. else
  315. return i2c_smbus_write_word_data(client, reg, swab16(value));
  316. }
  317. static void lm77_init_client(struct i2c_client *client)
  318. {
  319. /* Initialize the LM77 chip - turn off shutdown mode */
  320. int conf = lm77_read_value(client, LM77_REG_CONF);
  321. if (conf & 1)
  322. lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
  323. }
  324. static struct lm77_data *lm77_update_device(struct device *dev)
  325. {
  326. struct i2c_client *client = to_i2c_client(dev);
  327. struct lm77_data *data = i2c_get_clientdata(client);
  328. down(&data->update_lock);
  329. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  330. || !data->valid) {
  331. dev_dbg(&client->dev, "Starting lm77 update\n");
  332. data->temp_input =
  333. LM77_TEMP_FROM_REG(lm77_read_value(client,
  334. LM77_REG_TEMP));
  335. data->temp_hyst =
  336. LM77_TEMP_FROM_REG(lm77_read_value(client,
  337. LM77_REG_TEMP_HYST));
  338. data->temp_crit =
  339. LM77_TEMP_FROM_REG(lm77_read_value(client,
  340. LM77_REG_TEMP_CRIT));
  341. data->temp_min =
  342. LM77_TEMP_FROM_REG(lm77_read_value(client,
  343. LM77_REG_TEMP_MIN));
  344. data->temp_max =
  345. LM77_TEMP_FROM_REG(lm77_read_value(client,
  346. LM77_REG_TEMP_MAX));
  347. data->alarms =
  348. lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
  349. data->last_updated = jiffies;
  350. data->valid = 1;
  351. }
  352. up(&data->update_lock);
  353. return data;
  354. }
  355. static int __init sensors_lm77_init(void)
  356. {
  357. return i2c_add_driver(&lm77_driver);
  358. }
  359. static void __exit sensors_lm77_exit(void)
  360. {
  361. i2c_del_driver(&lm77_driver);
  362. }
  363. MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
  364. MODULE_DESCRIPTION("LM77 driver");
  365. MODULE_LICENSE("GPL");
  366. module_init(sensors_lm77_init);
  367. module_exit(sensors_lm77_exit);