lm77.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/err.h>
  29. #include <linux/mutex.h>
  30. /* Addresses to scan */
  31. static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, I2C_CLIENT_END };
  32. /* Insmod parameters */
  33. I2C_CLIENT_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 mutex 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. .driver = {
  65. .name = "lm77",
  66. },
  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 s16 LM77_TEMP_TO_REG(int temp)
  76. {
  77. int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
  78. return (ntemp / 500) * 8;
  79. }
  80. static inline int LM77_TEMP_FROM_REG(s16 reg)
  81. {
  82. return (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. mutex_lock(&data->update_lock); \
  122. data->value = val; \
  123. lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
  124. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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_probe(adapter, &addr_data, lm77_detect);
  182. }
  183. /* This function is called by i2c_probe */
  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 = kzalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
  197. err = -ENOMEM;
  198. goto exit;
  199. }
  200. new_client = &data->client;
  201. i2c_set_clientdata(new_client, data);
  202. new_client->addr = address;
  203. new_client->adapter = adapter;
  204. new_client->driver = &lm77_driver;
  205. new_client->flags = 0;
  206. /* Here comes the remaining detection. Since the LM77 has no
  207. register dedicated to identification, we have to rely on the
  208. following tricks:
  209. 1. the high 4 bits represent the sign and thus they should
  210. always be the same
  211. 2. the high 3 bits are unused in the configuration register
  212. 3. addresses 0x06 and 0x07 return the last read value
  213. 4. registers cycling over 8-address boundaries
  214. Word-sized registers are high-byte first. */
  215. if (kind < 0) {
  216. int i, cur, conf, hyst, crit, min, max;
  217. /* addresses cycling */
  218. cur = i2c_smbus_read_word_data(new_client, 0);
  219. conf = i2c_smbus_read_byte_data(new_client, 1);
  220. hyst = i2c_smbus_read_word_data(new_client, 2);
  221. crit = i2c_smbus_read_word_data(new_client, 3);
  222. min = i2c_smbus_read_word_data(new_client, 4);
  223. max = i2c_smbus_read_word_data(new_client, 5);
  224. for (i = 8; i <= 0xff; i += 8)
  225. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  226. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  227. || i2c_smbus_read_word_data(new_client, i + 3) != crit
  228. || i2c_smbus_read_word_data(new_client, i + 4) != min
  229. || i2c_smbus_read_word_data(new_client, i + 5) != max)
  230. goto exit_free;
  231. /* sign bits */
  232. if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
  233. || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
  234. || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
  235. || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
  236. || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
  237. goto exit_free;
  238. /* unused bits */
  239. if (conf & 0xe0)
  240. goto exit_free;
  241. /* 0x06 and 0x07 return the last read value */
  242. cur = i2c_smbus_read_word_data(new_client, 0);
  243. if (i2c_smbus_read_word_data(new_client, 6) != cur
  244. || i2c_smbus_read_word_data(new_client, 7) != cur)
  245. goto exit_free;
  246. hyst = i2c_smbus_read_word_data(new_client, 2);
  247. if (i2c_smbus_read_word_data(new_client, 6) != hyst
  248. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  249. goto exit_free;
  250. min = i2c_smbus_read_word_data(new_client, 4);
  251. if (i2c_smbus_read_word_data(new_client, 6) != min
  252. || i2c_smbus_read_word_data(new_client, 7) != min)
  253. goto exit_free;
  254. }
  255. /* Determine the chip type - only one kind supported! */
  256. if (kind <= 0)
  257. kind = lm77;
  258. if (kind == lm77) {
  259. name = "lm77";
  260. }
  261. /* Fill in the remaining client fields and put it into the global list */
  262. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  263. data->valid = 0;
  264. mutex_init(&data->update_lock);
  265. /* Tell the I2C layer a new client has arrived */
  266. if ((err = i2c_attach_client(new_client)))
  267. goto exit_free;
  268. /* Initialize the LM77 chip */
  269. lm77_init_client(new_client);
  270. /* Register sysfs hooks */
  271. data->class_dev = hwmon_device_register(&new_client->dev);
  272. if (IS_ERR(data->class_dev)) {
  273. err = PTR_ERR(data->class_dev);
  274. goto exit_detach;
  275. }
  276. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  277. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  278. device_create_file(&new_client->dev, &dev_attr_temp1_min);
  279. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  280. device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
  281. device_create_file(&new_client->dev, &dev_attr_temp1_min_hyst);
  282. device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
  283. device_create_file(&new_client->dev, &dev_attr_alarms);
  284. return 0;
  285. exit_detach:
  286. i2c_detach_client(new_client);
  287. exit_free:
  288. kfree(data);
  289. exit:
  290. return err;
  291. }
  292. static int lm77_detach_client(struct i2c_client *client)
  293. {
  294. struct lm77_data *data = i2c_get_clientdata(client);
  295. hwmon_device_unregister(data->class_dev);
  296. i2c_detach_client(client);
  297. kfree(data);
  298. return 0;
  299. }
  300. /* All registers are word-sized, except for the configuration register.
  301. The LM77 uses the high-byte first convention. */
  302. static u16 lm77_read_value(struct i2c_client *client, u8 reg)
  303. {
  304. if (reg == LM77_REG_CONF)
  305. return i2c_smbus_read_byte_data(client, reg);
  306. else
  307. return swab16(i2c_smbus_read_word_data(client, reg));
  308. }
  309. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
  310. {
  311. if (reg == LM77_REG_CONF)
  312. return i2c_smbus_write_byte_data(client, reg, value);
  313. else
  314. return i2c_smbus_write_word_data(client, reg, swab16(value));
  315. }
  316. static void lm77_init_client(struct i2c_client *client)
  317. {
  318. /* Initialize the LM77 chip - turn off shutdown mode */
  319. int conf = lm77_read_value(client, LM77_REG_CONF);
  320. if (conf & 1)
  321. lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
  322. }
  323. static struct lm77_data *lm77_update_device(struct device *dev)
  324. {
  325. struct i2c_client *client = to_i2c_client(dev);
  326. struct lm77_data *data = i2c_get_clientdata(client);
  327. mutex_lock(&data->update_lock);
  328. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  329. || !data->valid) {
  330. dev_dbg(&client->dev, "Starting lm77 update\n");
  331. data->temp_input =
  332. LM77_TEMP_FROM_REG(lm77_read_value(client,
  333. LM77_REG_TEMP));
  334. data->temp_hyst =
  335. LM77_TEMP_FROM_REG(lm77_read_value(client,
  336. LM77_REG_TEMP_HYST));
  337. data->temp_crit =
  338. LM77_TEMP_FROM_REG(lm77_read_value(client,
  339. LM77_REG_TEMP_CRIT));
  340. data->temp_min =
  341. LM77_TEMP_FROM_REG(lm77_read_value(client,
  342. LM77_REG_TEMP_MIN));
  343. data->temp_max =
  344. LM77_TEMP_FROM_REG(lm77_read_value(client,
  345. LM77_REG_TEMP_MAX));
  346. data->alarms =
  347. lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
  348. data->last_updated = jiffies;
  349. data->valid = 1;
  350. }
  351. mutex_unlock(&data->update_lock);
  352. return data;
  353. }
  354. static int __init sensors_lm77_init(void)
  355. {
  356. return i2c_add_driver(&lm77_driver);
  357. }
  358. static void __exit sensors_lm77_exit(void)
  359. {
  360. i2c_del_driver(&lm77_driver);
  361. }
  362. MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
  363. MODULE_DESCRIPTION("LM77 driver");
  364. MODULE_LICENSE("GPL");
  365. module_init(sensors_lm77_init);
  366. module_exit(sensors_lm77_exit);