lm77.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * lm77.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. *
  5. * Copyright (c) 2004 Andras BALI <drewie@freemail.hu>
  6. *
  7. * Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>. The LM77
  8. * is a temperature sensor and thermal window comparator with 0.5 deg
  9. * resolution made by National Semiconductor. Complete datasheet can be
  10. * obtained at their site:
  11. * http://www.national.com/pf/LM/LM77.html
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/i2c.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. /* Addresses to scan */
  37. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
  38. I2C_CLIENT_END };
  39. /* The LM77 registers */
  40. #define LM77_REG_TEMP 0x00
  41. #define LM77_REG_CONF 0x01
  42. #define LM77_REG_TEMP_HYST 0x02
  43. #define LM77_REG_TEMP_CRIT 0x03
  44. #define LM77_REG_TEMP_MIN 0x04
  45. #define LM77_REG_TEMP_MAX 0x05
  46. /* Each client has this additional data */
  47. struct lm77_data {
  48. struct device *hwmon_dev;
  49. struct mutex update_lock;
  50. char valid;
  51. unsigned long last_updated; /* In jiffies */
  52. int temp_input; /* Temperatures */
  53. int temp_crit;
  54. int temp_min;
  55. int temp_max;
  56. int temp_hyst;
  57. u8 alarms;
  58. };
  59. static int lm77_probe(struct i2c_client *client,
  60. const struct i2c_device_id *id);
  61. static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info);
  62. static void lm77_init_client(struct i2c_client *client);
  63. static int lm77_remove(struct i2c_client *client);
  64. static u16 lm77_read_value(struct i2c_client *client, u8 reg);
  65. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value);
  66. static struct lm77_data *lm77_update_device(struct device *dev);
  67. static const struct i2c_device_id lm77_id[] = {
  68. { "lm77", 0 },
  69. { }
  70. };
  71. MODULE_DEVICE_TABLE(i2c, lm77_id);
  72. /* This is the driver that will be inserted */
  73. static struct i2c_driver lm77_driver = {
  74. .class = I2C_CLASS_HWMON,
  75. .driver = {
  76. .name = "lm77",
  77. },
  78. .probe = lm77_probe,
  79. .remove = lm77_remove,
  80. .id_table = lm77_id,
  81. .detect = lm77_detect,
  82. .address_list = normal_i2c,
  83. };
  84. /* straight from the datasheet */
  85. #define LM77_TEMP_MIN (-55000)
  86. #define LM77_TEMP_MAX 125000
  87. /*
  88. * In the temperature registers, the low 3 bits are not part of the
  89. * temperature values; they are the status bits.
  90. */
  91. static inline s16 LM77_TEMP_TO_REG(int temp)
  92. {
  93. int ntemp = SENSORS_LIMIT(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
  94. return (ntemp / 500) * 8;
  95. }
  96. static inline int LM77_TEMP_FROM_REG(s16 reg)
  97. {
  98. return (reg / 8) * 500;
  99. }
  100. /* sysfs stuff */
  101. /* read routines for temperature limits */
  102. #define show(value) \
  103. static ssize_t show_##value(struct device *dev, \
  104. struct device_attribute *attr, \
  105. char *buf) \
  106. { \
  107. struct lm77_data *data = lm77_update_device(dev); \
  108. return sprintf(buf, "%d\n", data->value); \
  109. }
  110. show(temp_input);
  111. show(temp_crit);
  112. show(temp_min);
  113. show(temp_max);
  114. /* read routines for hysteresis values */
  115. static ssize_t show_temp_crit_hyst(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct lm77_data *data = lm77_update_device(dev);
  119. return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
  120. }
  121. static ssize_t show_temp_min_hyst(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct lm77_data *data = lm77_update_device(dev);
  125. return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
  126. }
  127. static ssize_t show_temp_max_hyst(struct device *dev,
  128. struct device_attribute *attr, char *buf)
  129. {
  130. struct lm77_data *data = lm77_update_device(dev);
  131. return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
  132. }
  133. /* write routines */
  134. #define set(value, reg) \
  135. static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
  136. const char *buf, size_t count) \
  137. { \
  138. struct i2c_client *client = to_i2c_client(dev); \
  139. struct lm77_data *data = i2c_get_clientdata(client); \
  140. long val; \
  141. int err = kstrtol(buf, 10, &val); \
  142. if (err) \
  143. return err; \
  144. \
  145. mutex_lock(&data->update_lock); \
  146. data->value = val; \
  147. lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value)); \
  148. mutex_unlock(&data->update_lock); \
  149. return count; \
  150. }
  151. set(temp_min, LM77_REG_TEMP_MIN);
  152. set(temp_max, LM77_REG_TEMP_MAX);
  153. /*
  154. * hysteresis is stored as a relative value on the chip, so it has to be
  155. * converted first
  156. */
  157. static ssize_t set_temp_crit_hyst(struct device *dev,
  158. struct device_attribute *attr,
  159. const char *buf, size_t count)
  160. {
  161. struct i2c_client *client = to_i2c_client(dev);
  162. struct lm77_data *data = i2c_get_clientdata(client);
  163. unsigned long val;
  164. int err;
  165. err = kstrtoul(buf, 10, &val);
  166. if (err)
  167. return err;
  168. mutex_lock(&data->update_lock);
  169. data->temp_hyst = data->temp_crit - val;
  170. lm77_write_value(client, LM77_REG_TEMP_HYST,
  171. LM77_TEMP_TO_REG(data->temp_hyst));
  172. mutex_unlock(&data->update_lock);
  173. return count;
  174. }
  175. /* preserve hysteresis when setting T_crit */
  176. static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  177. const char *buf, size_t count)
  178. {
  179. struct i2c_client *client = to_i2c_client(dev);
  180. struct lm77_data *data = i2c_get_clientdata(client);
  181. int oldcrithyst;
  182. unsigned long val;
  183. int err;
  184. err = kstrtoul(buf, 10, &val);
  185. if (err)
  186. return err;
  187. mutex_lock(&data->update_lock);
  188. oldcrithyst = data->temp_crit - data->temp_hyst;
  189. data->temp_crit = val;
  190. data->temp_hyst = data->temp_crit - oldcrithyst;
  191. lm77_write_value(client, LM77_REG_TEMP_CRIT,
  192. LM77_TEMP_TO_REG(data->temp_crit));
  193. lm77_write_value(client, LM77_REG_TEMP_HYST,
  194. LM77_TEMP_TO_REG(data->temp_hyst));
  195. mutex_unlock(&data->update_lock);
  196. return count;
  197. }
  198. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  199. char *buf)
  200. {
  201. int bitnr = to_sensor_dev_attr(attr)->index;
  202. struct lm77_data *data = lm77_update_device(dev);
  203. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  204. }
  205. static DEVICE_ATTR(temp1_input, S_IRUGO,
  206. show_temp_input, NULL);
  207. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  208. show_temp_crit, set_temp_crit);
  209. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  210. show_temp_min, set_temp_min);
  211. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  212. show_temp_max, set_temp_max);
  213. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  214. show_temp_crit_hyst, set_temp_crit_hyst);
  215. static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
  216. show_temp_min_hyst, NULL);
  217. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
  218. show_temp_max_hyst, NULL);
  219. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
  220. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  221. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  222. static struct attribute *lm77_attributes[] = {
  223. &dev_attr_temp1_input.attr,
  224. &dev_attr_temp1_crit.attr,
  225. &dev_attr_temp1_min.attr,
  226. &dev_attr_temp1_max.attr,
  227. &dev_attr_temp1_crit_hyst.attr,
  228. &dev_attr_temp1_min_hyst.attr,
  229. &dev_attr_temp1_max_hyst.attr,
  230. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  231. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  232. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  233. NULL
  234. };
  235. static const struct attribute_group lm77_group = {
  236. .attrs = lm77_attributes,
  237. };
  238. /* Return 0 if detection is successful, -ENODEV otherwise */
  239. static int lm77_detect(struct i2c_client *new_client,
  240. struct i2c_board_info *info)
  241. {
  242. struct i2c_adapter *adapter = new_client->adapter;
  243. int i, cur, conf, hyst, crit, min, max;
  244. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  245. I2C_FUNC_SMBUS_WORD_DATA))
  246. return -ENODEV;
  247. /*
  248. * Here comes the remaining detection. Since the LM77 has no
  249. * register dedicated to identification, we have to rely on the
  250. * following tricks:
  251. *
  252. * 1. the high 4 bits represent the sign and thus they should
  253. * always be the same
  254. * 2. the high 3 bits are unused in the configuration register
  255. * 3. addresses 0x06 and 0x07 return the last read value
  256. * 4. registers cycling over 8-address boundaries
  257. *
  258. * Word-sized registers are high-byte first.
  259. */
  260. /* addresses cycling */
  261. cur = i2c_smbus_read_word_data(new_client, 0);
  262. conf = i2c_smbus_read_byte_data(new_client, 1);
  263. hyst = i2c_smbus_read_word_data(new_client, 2);
  264. crit = i2c_smbus_read_word_data(new_client, 3);
  265. min = i2c_smbus_read_word_data(new_client, 4);
  266. max = i2c_smbus_read_word_data(new_client, 5);
  267. for (i = 8; i <= 0xff; i += 8) {
  268. if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
  269. || i2c_smbus_read_word_data(new_client, i + 2) != hyst
  270. || i2c_smbus_read_word_data(new_client, i + 3) != crit
  271. || i2c_smbus_read_word_data(new_client, i + 4) != min
  272. || i2c_smbus_read_word_data(new_client, i + 5) != max)
  273. return -ENODEV;
  274. }
  275. /* sign bits */
  276. if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
  277. || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
  278. || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
  279. || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
  280. || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
  281. return -ENODEV;
  282. /* unused bits */
  283. if (conf & 0xe0)
  284. return -ENODEV;
  285. /* 0x06 and 0x07 return the last read value */
  286. cur = i2c_smbus_read_word_data(new_client, 0);
  287. if (i2c_smbus_read_word_data(new_client, 6) != cur
  288. || i2c_smbus_read_word_data(new_client, 7) != cur)
  289. return -ENODEV;
  290. hyst = i2c_smbus_read_word_data(new_client, 2);
  291. if (i2c_smbus_read_word_data(new_client, 6) != hyst
  292. || i2c_smbus_read_word_data(new_client, 7) != hyst)
  293. return -ENODEV;
  294. min = i2c_smbus_read_word_data(new_client, 4);
  295. if (i2c_smbus_read_word_data(new_client, 6) != min
  296. || i2c_smbus_read_word_data(new_client, 7) != min)
  297. return -ENODEV;
  298. strlcpy(info->type, "lm77", I2C_NAME_SIZE);
  299. return 0;
  300. }
  301. static int lm77_probe(struct i2c_client *new_client,
  302. const struct i2c_device_id *id)
  303. {
  304. struct lm77_data *data;
  305. int err;
  306. data = kzalloc(sizeof(struct lm77_data), GFP_KERNEL);
  307. if (!data) {
  308. err = -ENOMEM;
  309. goto exit;
  310. }
  311. i2c_set_clientdata(new_client, data);
  312. data->valid = 0;
  313. mutex_init(&data->update_lock);
  314. /* Initialize the LM77 chip */
  315. lm77_init_client(new_client);
  316. /* Register sysfs hooks */
  317. err = sysfs_create_group(&new_client->dev.kobj, &lm77_group);
  318. if (err)
  319. goto exit_free;
  320. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  321. if (IS_ERR(data->hwmon_dev)) {
  322. err = PTR_ERR(data->hwmon_dev);
  323. goto exit_remove;
  324. }
  325. return 0;
  326. exit_remove:
  327. sysfs_remove_group(&new_client->dev.kobj, &lm77_group);
  328. exit_free:
  329. kfree(data);
  330. exit:
  331. return err;
  332. }
  333. static int lm77_remove(struct i2c_client *client)
  334. {
  335. struct lm77_data *data = i2c_get_clientdata(client);
  336. hwmon_device_unregister(data->hwmon_dev);
  337. sysfs_remove_group(&client->dev.kobj, &lm77_group);
  338. kfree(data);
  339. return 0;
  340. }
  341. /*
  342. * All registers are word-sized, except for the configuration register.
  343. * The LM77 uses the high-byte first convention.
  344. */
  345. static u16 lm77_read_value(struct i2c_client *client, u8 reg)
  346. {
  347. if (reg == LM77_REG_CONF)
  348. return i2c_smbus_read_byte_data(client, reg);
  349. else
  350. return i2c_smbus_read_word_swapped(client, reg);
  351. }
  352. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
  353. {
  354. if (reg == LM77_REG_CONF)
  355. return i2c_smbus_write_byte_data(client, reg, value);
  356. else
  357. return i2c_smbus_write_word_swapped(client, reg, value);
  358. }
  359. static void lm77_init_client(struct i2c_client *client)
  360. {
  361. /* Initialize the LM77 chip - turn off shutdown mode */
  362. int conf = lm77_read_value(client, LM77_REG_CONF);
  363. if (conf & 1)
  364. lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
  365. }
  366. static struct lm77_data *lm77_update_device(struct device *dev)
  367. {
  368. struct i2c_client *client = to_i2c_client(dev);
  369. struct lm77_data *data = i2c_get_clientdata(client);
  370. mutex_lock(&data->update_lock);
  371. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  372. || !data->valid) {
  373. dev_dbg(&client->dev, "Starting lm77 update\n");
  374. data->temp_input =
  375. LM77_TEMP_FROM_REG(lm77_read_value(client,
  376. LM77_REG_TEMP));
  377. data->temp_hyst =
  378. LM77_TEMP_FROM_REG(lm77_read_value(client,
  379. LM77_REG_TEMP_HYST));
  380. data->temp_crit =
  381. LM77_TEMP_FROM_REG(lm77_read_value(client,
  382. LM77_REG_TEMP_CRIT));
  383. data->temp_min =
  384. LM77_TEMP_FROM_REG(lm77_read_value(client,
  385. LM77_REG_TEMP_MIN));
  386. data->temp_max =
  387. LM77_TEMP_FROM_REG(lm77_read_value(client,
  388. LM77_REG_TEMP_MAX));
  389. data->alarms =
  390. lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
  391. data->last_updated = jiffies;
  392. data->valid = 1;
  393. }
  394. mutex_unlock(&data->update_lock);
  395. return data;
  396. }
  397. module_i2c_driver(lm77_driver);
  398. MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
  399. MODULE_DESCRIPTION("LM77 driver");
  400. MODULE_LICENSE("GPL");