lm77.c 14 KB

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