lm77.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, I2C_CLIENT_END };
  33. /* Insmod parameters */
  34. I2C_CLIENT_INSMOD_1(lm77);
  35. /* The LM77 registers */
  36. #define LM77_REG_TEMP 0x00
  37. #define LM77_REG_CONF 0x01
  38. #define LM77_REG_TEMP_HYST 0x02
  39. #define LM77_REG_TEMP_CRIT 0x03
  40. #define LM77_REG_TEMP_MIN 0x04
  41. #define LM77_REG_TEMP_MAX 0x05
  42. /* Each client has this additional data */
  43. struct lm77_data {
  44. struct i2c_client client;
  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_attach_adapter(struct i2c_adapter *adapter);
  57. static int lm77_detect(struct i2c_adapter *adapter, int address, int kind);
  58. static void lm77_init_client(struct i2c_client *client);
  59. static int lm77_detach_client(struct i2c_client *client);
  60. static u16 lm77_read_value(struct i2c_client *client, u8 reg);
  61. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
  62. static struct lm77_data *lm77_update_device(struct device *dev);
  63. /* This is the driver that will be inserted */
  64. static struct i2c_driver lm77_driver = {
  65. .driver = {
  66. .name = "lm77",
  67. },
  68. .attach_adapter = lm77_attach_adapter,
  69. .detach_client = lm77_detach_client,
  70. };
  71. /* straight from the datasheet */
  72. #define LM77_TEMP_MIN (-55000)
  73. #define LM77_TEMP_MAX 125000
  74. /* In the temperature registers, the low 3 bits are not part of the
  75. temperature values; they are the status bits. */
  76. static inline s16 LM77_TEMP_TO_REG(int temp)
  77. {
  78. int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
  79. return (ntemp / 500) * 8;
  80. }
  81. static inline int LM77_TEMP_FROM_REG(s16 reg)
  82. {
  83. return (reg / 8) * 500;
  84. }
  85. /* sysfs stuff */
  86. /* read routines for temperature limits */
  87. #define show(value) \
  88. static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
  89. { \
  90. struct lm77_data *data = lm77_update_device(dev); \
  91. return sprintf(buf, "%d\n", data->value); \
  92. }
  93. show(temp_input);
  94. show(temp_crit);
  95. show(temp_min);
  96. show(temp_max);
  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_strtol(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 ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  162. char *buf)
  163. {
  164. int bitnr = to_sensor_dev_attr(attr)->index;
  165. struct lm77_data *data = lm77_update_device(dev);
  166. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  167. }
  168. static DEVICE_ATTR(temp1_input, S_IRUGO,
  169. show_temp_input, NULL);
  170. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  171. show_temp_crit, set_temp_crit);
  172. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  173. show_temp_min, set_temp_min);
  174. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  175. show_temp_max, set_temp_max);
  176. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  177. show_temp_crit_hyst, set_temp_crit_hyst);
  178. static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
  179. show_temp_min_hyst, NULL);
  180. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
  181. show_temp_max_hyst, NULL);
  182. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
  183. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  184. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  185. static int lm77_attach_adapter(struct i2c_adapter *adapter)
  186. {
  187. if (!(adapter->class & I2C_CLASS_HWMON))
  188. return 0;
  189. return i2c_probe(adapter, &addr_data, lm77_detect);
  190. }
  191. static struct attribute *lm77_attributes[] = {
  192. &dev_attr_temp1_input.attr,
  193. &dev_attr_temp1_crit.attr,
  194. &dev_attr_temp1_min.attr,
  195. &dev_attr_temp1_max.attr,
  196. &dev_attr_temp1_crit_hyst.attr,
  197. &dev_attr_temp1_min_hyst.attr,
  198. &dev_attr_temp1_max_hyst.attr,
  199. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  200. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  201. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  202. NULL
  203. };
  204. static const struct attribute_group lm77_group = {
  205. .attrs = lm77_attributes,
  206. };
  207. /* This function is called by i2c_probe */
  208. static int lm77_detect(struct i2c_adapter *adapter, int address, int kind)
  209. {
  210. struct i2c_client *new_client;
  211. struct lm77_data *data;
  212. int err = 0;
  213. const char *name = "";
  214. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  215. I2C_FUNC_SMBUS_WORD_DATA))
  216. goto exit;
  217. /* OK. For now, we presume we have a valid client. We now create the
  218. client structure, even though we cannot fill it completely yet.
  219. But it allows us to access lm77_{read,write}_value. */
  220. if (!(data = kzalloc(sizeof(struct lm77_data), GFP_KERNEL))) {
  221. err = -ENOMEM;
  222. goto exit;
  223. }
  224. new_client = &data->client;
  225. i2c_set_clientdata(new_client, data);
  226. new_client->addr = address;
  227. new_client->adapter = adapter;
  228. new_client->driver = &lm77_driver;
  229. new_client->flags = 0;
  230. /* Here comes the remaining detection. Since the LM77 has no
  231. register dedicated to identification, we have to rely on the
  232. following tricks:
  233. 1. the high 4 bits represent the sign and thus they should
  234. always be the same
  235. 2. the high 3 bits are unused in the configuration register
  236. 3. addresses 0x06 and 0x07 return the last read value
  237. 4. registers cycling over 8-address boundaries
  238. Word-sized registers are high-byte first. */
  239. if (kind < 0) {
  240. int i, cur, conf, hyst, crit, min, max;
  241. /* addresses cycling */
  242. cur = i2c_smbus_read_word_data(new_client, 0);
  243. conf = i2c_smbus_read_byte_data(new_client, 1);
  244. hyst = i2c_smbus_read_word_data(new_client, 2);
  245. crit = i2c_smbus_read_word_data(new_client, 3);
  246. min = i2c_smbus_read_word_data(new_client, 4);
  247. max = i2c_smbus_read_word_data(new_client, 5);
  248. for (i = 8; i <= 0xff; i += 8)
  249. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  250. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  251. || i2c_smbus_read_word_data(new_client, i + 3) != crit
  252. || i2c_smbus_read_word_data(new_client, i + 4) != min
  253. || i2c_smbus_read_word_data(new_client, i + 5) != max)
  254. goto exit_free;
  255. /* sign bits */
  256. if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
  257. || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
  258. || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
  259. || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
  260. || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
  261. goto exit_free;
  262. /* unused bits */
  263. if (conf & 0xe0)
  264. goto exit_free;
  265. /* 0x06 and 0x07 return the last read value */
  266. cur = i2c_smbus_read_word_data(new_client, 0);
  267. if (i2c_smbus_read_word_data(new_client, 6) != cur
  268. || i2c_smbus_read_word_data(new_client, 7) != cur)
  269. goto exit_free;
  270. hyst = i2c_smbus_read_word_data(new_client, 2);
  271. if (i2c_smbus_read_word_data(new_client, 6) != hyst
  272. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  273. goto exit_free;
  274. min = i2c_smbus_read_word_data(new_client, 4);
  275. if (i2c_smbus_read_word_data(new_client, 6) != min
  276. || i2c_smbus_read_word_data(new_client, 7) != min)
  277. goto exit_free;
  278. }
  279. /* Determine the chip type - only one kind supported! */
  280. if (kind <= 0)
  281. kind = lm77;
  282. if (kind == lm77) {
  283. name = "lm77";
  284. }
  285. /* Fill in the remaining client fields and put it into the global list */
  286. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  287. data->valid = 0;
  288. mutex_init(&data->update_lock);
  289. /* Tell the I2C layer a new client has arrived */
  290. if ((err = i2c_attach_client(new_client)))
  291. goto exit_free;
  292. /* Initialize the LM77 chip */
  293. lm77_init_client(new_client);
  294. /* Register sysfs hooks */
  295. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm77_group)))
  296. goto exit_detach;
  297. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  298. if (IS_ERR(data->hwmon_dev)) {
  299. err = PTR_ERR(data->hwmon_dev);
  300. goto exit_remove;
  301. }
  302. return 0;
  303. exit_remove:
  304. sysfs_remove_group(&new_client->dev.kobj, &lm77_group);
  305. exit_detach:
  306. i2c_detach_client(new_client);
  307. exit_free:
  308. kfree(data);
  309. exit:
  310. return err;
  311. }
  312. static int lm77_detach_client(struct i2c_client *client)
  313. {
  314. struct lm77_data *data = i2c_get_clientdata(client);
  315. hwmon_device_unregister(data->hwmon_dev);
  316. sysfs_remove_group(&client->dev.kobj, &lm77_group);
  317. i2c_detach_client(client);
  318. kfree(data);
  319. return 0;
  320. }
  321. /* All registers are word-sized, except for the configuration register.
  322. The LM77 uses the high-byte first convention. */
  323. static u16 lm77_read_value(struct i2c_client *client, u8 reg)
  324. {
  325. if (reg == LM77_REG_CONF)
  326. return i2c_smbus_read_byte_data(client, reg);
  327. else
  328. return swab16(i2c_smbus_read_word_data(client, reg));
  329. }
  330. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
  331. {
  332. if (reg == LM77_REG_CONF)
  333. return i2c_smbus_write_byte_data(client, reg, value);
  334. else
  335. return i2c_smbus_write_word_data(client, reg, swab16(value));
  336. }
  337. static void lm77_init_client(struct i2c_client *client)
  338. {
  339. /* Initialize the LM77 chip - turn off shutdown mode */
  340. int conf = lm77_read_value(client, LM77_REG_CONF);
  341. if (conf & 1)
  342. lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
  343. }
  344. static struct lm77_data *lm77_update_device(struct device *dev)
  345. {
  346. struct i2c_client *client = to_i2c_client(dev);
  347. struct lm77_data *data = i2c_get_clientdata(client);
  348. mutex_lock(&data->update_lock);
  349. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  350. || !data->valid) {
  351. dev_dbg(&client->dev, "Starting lm77 update\n");
  352. data->temp_input =
  353. LM77_TEMP_FROM_REG(lm77_read_value(client,
  354. LM77_REG_TEMP));
  355. data->temp_hyst =
  356. LM77_TEMP_FROM_REG(lm77_read_value(client,
  357. LM77_REG_TEMP_HYST));
  358. data->temp_crit =
  359. LM77_TEMP_FROM_REG(lm77_read_value(client,
  360. LM77_REG_TEMP_CRIT));
  361. data->temp_min =
  362. LM77_TEMP_FROM_REG(lm77_read_value(client,
  363. LM77_REG_TEMP_MIN));
  364. data->temp_max =
  365. LM77_TEMP_FROM_REG(lm77_read_value(client,
  366. LM77_REG_TEMP_MAX));
  367. data->alarms =
  368. lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
  369. data->last_updated = jiffies;
  370. data->valid = 1;
  371. }
  372. mutex_unlock(&data->update_lock);
  373. return data;
  374. }
  375. static int __init sensors_lm77_init(void)
  376. {
  377. return i2c_add_driver(&lm77_driver);
  378. }
  379. static void __exit sensors_lm77_exit(void)
  380. {
  381. i2c_del_driver(&lm77_driver);
  382. }
  383. MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
  384. MODULE_DESCRIPTION("LM77 driver");
  385. MODULE_LICENSE("GPL");
  386. module_init(sensors_lm77_init);
  387. module_exit(sensors_lm77_exit);