lm77.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/i2c.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. /* Addresses to scan */
  33. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
  34. I2C_CLIENT_END };
  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. enum temp_index {
  43. t_input = 0,
  44. t_crit,
  45. t_min,
  46. t_max,
  47. t_hyst,
  48. t_num_temp
  49. };
  50. static const u8 temp_regs[t_num_temp] = {
  51. [t_input] = LM77_REG_TEMP,
  52. [t_min] = LM77_REG_TEMP_MIN,
  53. [t_max] = LM77_REG_TEMP_MAX,
  54. [t_crit] = LM77_REG_TEMP_CRIT,
  55. [t_hyst] = LM77_REG_TEMP_HYST,
  56. };
  57. /* Each client has this additional data */
  58. struct lm77_data {
  59. struct i2c_client *client;
  60. struct mutex update_lock;
  61. char valid;
  62. unsigned long last_updated; /* In jiffies */
  63. int temp[t_num_temp]; /* index using temp_index */
  64. u8 alarms;
  65. };
  66. /* straight from the datasheet */
  67. #define LM77_TEMP_MIN (-55000)
  68. #define LM77_TEMP_MAX 125000
  69. /*
  70. * In the temperature registers, the low 3 bits are not part of the
  71. * temperature values; they are the status bits.
  72. */
  73. static inline s16 LM77_TEMP_TO_REG(int temp)
  74. {
  75. int ntemp = clamp_val(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
  76. return (ntemp / 500) * 8;
  77. }
  78. static inline int LM77_TEMP_FROM_REG(s16 reg)
  79. {
  80. return (reg / 8) * 500;
  81. }
  82. /*
  83. * All registers are word-sized, except for the configuration register.
  84. * The LM77 uses the high-byte first convention.
  85. */
  86. static u16 lm77_read_value(struct i2c_client *client, u8 reg)
  87. {
  88. if (reg == LM77_REG_CONF)
  89. return i2c_smbus_read_byte_data(client, reg);
  90. else
  91. return i2c_smbus_read_word_swapped(client, reg);
  92. }
  93. static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
  94. {
  95. if (reg == LM77_REG_CONF)
  96. return i2c_smbus_write_byte_data(client, reg, value);
  97. else
  98. return i2c_smbus_write_word_swapped(client, reg, value);
  99. }
  100. static struct lm77_data *lm77_update_device(struct device *dev)
  101. {
  102. struct lm77_data *data = dev_get_drvdata(dev);
  103. struct i2c_client *client = data->client;
  104. int i;
  105. mutex_lock(&data->update_lock);
  106. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  107. || !data->valid) {
  108. dev_dbg(&client->dev, "Starting lm77 update\n");
  109. for (i = 0; i < t_num_temp; i++) {
  110. data->temp[i] =
  111. LM77_TEMP_FROM_REG(lm77_read_value(client,
  112. temp_regs[i]));
  113. }
  114. data->alarms =
  115. lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
  116. data->last_updated = jiffies;
  117. data->valid = 1;
  118. }
  119. mutex_unlock(&data->update_lock);
  120. return data;
  121. }
  122. /* sysfs stuff */
  123. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  124. char *buf)
  125. {
  126. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  127. struct lm77_data *data = lm77_update_device(dev);
  128. return sprintf(buf, "%d\n", data->temp[attr->index]);
  129. }
  130. static ssize_t show_temp_hyst(struct device *dev,
  131. struct device_attribute *devattr, char *buf)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct lm77_data *data = lm77_update_device(dev);
  135. int nr = attr->index;
  136. int temp;
  137. temp = nr == t_min ? data->temp[nr] + data->temp[t_hyst] :
  138. data->temp[nr] - data->temp[t_hyst];
  139. return sprintf(buf, "%d\n", temp);
  140. }
  141. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  142. const char *buf, size_t count)
  143. {
  144. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  145. struct lm77_data *data = dev_get_drvdata(dev);
  146. struct i2c_client *client = data->client;
  147. int nr = attr->index;
  148. long val;
  149. int err;
  150. err = kstrtol(buf, 10, &val);
  151. if (err)
  152. return err;
  153. mutex_lock(&data->update_lock);
  154. data->temp[nr] = val;
  155. lm77_write_value(client, temp_regs[nr], LM77_TEMP_TO_REG(val));
  156. mutex_unlock(&data->update_lock);
  157. return count;
  158. }
  159. /*
  160. * hysteresis is stored as a relative value on the chip, so it has to be
  161. * converted first.
  162. */
  163. static ssize_t set_temp_hyst(struct device *dev,
  164. struct device_attribute *devattr,
  165. const char *buf, size_t count)
  166. {
  167. struct lm77_data *data = dev_get_drvdata(dev);
  168. struct i2c_client *client = data->client;
  169. unsigned long val;
  170. int err;
  171. err = kstrtoul(buf, 10, &val);
  172. if (err)
  173. return err;
  174. mutex_lock(&data->update_lock);
  175. data->temp[t_hyst] = data->temp[t_crit] - val;
  176. lm77_write_value(client, LM77_REG_TEMP_HYST,
  177. LM77_TEMP_TO_REG(data->temp[t_hyst]));
  178. mutex_unlock(&data->update_lock);
  179. return count;
  180. }
  181. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  182. char *buf)
  183. {
  184. int bitnr = to_sensor_dev_attr(attr)->index;
  185. struct lm77_data *data = lm77_update_device(dev);
  186. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  187. }
  188. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
  189. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp, set_temp,
  190. t_crit);
  191. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp,
  192. t_min);
  193. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp,
  194. t_max);
  195. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst,
  196. set_temp_hyst, t_crit);
  197. static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp_hyst, NULL, t_min);
  198. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp_hyst, NULL, t_max);
  199. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
  200. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
  201. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
  202. static struct attribute *lm77_attrs[] = {
  203. &sensor_dev_attr_temp1_input.dev_attr.attr,
  204. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  205. &sensor_dev_attr_temp1_min.dev_attr.attr,
  206. &sensor_dev_attr_temp1_max.dev_attr.attr,
  207. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  208. &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
  209. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  210. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  211. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  212. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  213. NULL
  214. };
  215. ATTRIBUTE_GROUPS(lm77);
  216. /* Return 0 if detection is successful, -ENODEV otherwise */
  217. static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info)
  218. {
  219. struct i2c_adapter *adapter = client->adapter;
  220. int i, cur, conf, hyst, crit, min, max;
  221. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  222. I2C_FUNC_SMBUS_WORD_DATA))
  223. return -ENODEV;
  224. /*
  225. * Here comes the remaining detection. Since the LM77 has no
  226. * register dedicated to identification, we have to rely on the
  227. * following tricks:
  228. *
  229. * 1. the high 4 bits represent the sign and thus they should
  230. * always be the same
  231. * 2. the high 3 bits are unused in the configuration register
  232. * 3. addresses 0x06 and 0x07 return the last read value
  233. * 4. registers cycling over 8-address boundaries
  234. *
  235. * Word-sized registers are high-byte first.
  236. */
  237. /* addresses cycling */
  238. cur = i2c_smbus_read_word_data(client, 0);
  239. conf = i2c_smbus_read_byte_data(client, 1);
  240. hyst = i2c_smbus_read_word_data(client, 2);
  241. crit = i2c_smbus_read_word_data(client, 3);
  242. min = i2c_smbus_read_word_data(client, 4);
  243. max = i2c_smbus_read_word_data(client, 5);
  244. for (i = 8; i <= 0xff; i += 8) {
  245. if (i2c_smbus_read_byte_data(client, i + 1) != conf
  246. || i2c_smbus_read_word_data(client, i + 2) != hyst
  247. || i2c_smbus_read_word_data(client, i + 3) != crit
  248. || i2c_smbus_read_word_data(client, i + 4) != min
  249. || i2c_smbus_read_word_data(client, i + 5) != max)
  250. return -ENODEV;
  251. }
  252. /* sign bits */
  253. if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
  254. || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
  255. || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
  256. || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
  257. || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
  258. return -ENODEV;
  259. /* unused bits */
  260. if (conf & 0xe0)
  261. return -ENODEV;
  262. /* 0x06 and 0x07 return the last read value */
  263. cur = i2c_smbus_read_word_data(client, 0);
  264. if (i2c_smbus_read_word_data(client, 6) != cur
  265. || i2c_smbus_read_word_data(client, 7) != cur)
  266. return -ENODEV;
  267. hyst = i2c_smbus_read_word_data(client, 2);
  268. if (i2c_smbus_read_word_data(client, 6) != hyst
  269. || i2c_smbus_read_word_data(client, 7) != hyst)
  270. return -ENODEV;
  271. min = i2c_smbus_read_word_data(client, 4);
  272. if (i2c_smbus_read_word_data(client, 6) != min
  273. || i2c_smbus_read_word_data(client, 7) != min)
  274. return -ENODEV;
  275. strlcpy(info->type, "lm77", I2C_NAME_SIZE);
  276. return 0;
  277. }
  278. static void lm77_init_client(struct i2c_client *client)
  279. {
  280. /* Initialize the LM77 chip - turn off shutdown mode */
  281. int conf = lm77_read_value(client, LM77_REG_CONF);
  282. if (conf & 1)
  283. lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
  284. }
  285. static int lm77_probe(struct i2c_client *client, const struct i2c_device_id *id)
  286. {
  287. struct device *dev = &client->dev;
  288. struct device *hwmon_dev;
  289. struct lm77_data *data;
  290. data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL);
  291. if (!data)
  292. return -ENOMEM;
  293. data->client = client;
  294. mutex_init(&data->update_lock);
  295. /* Initialize the LM77 chip */
  296. lm77_init_client(client);
  297. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  298. data, lm77_groups);
  299. return PTR_ERR_OR_ZERO(hwmon_dev);
  300. }
  301. static const struct i2c_device_id lm77_id[] = {
  302. { "lm77", 0 },
  303. { }
  304. };
  305. MODULE_DEVICE_TABLE(i2c, lm77_id);
  306. /* This is the driver that will be inserted */
  307. static struct i2c_driver lm77_driver = {
  308. .class = I2C_CLASS_HWMON,
  309. .driver = {
  310. .name = "lm77",
  311. },
  312. .probe = lm77_probe,
  313. .id_table = lm77_id,
  314. .detect = lm77_detect,
  315. .address_list = normal_i2c,
  316. };
  317. module_i2c_driver(lm77_driver);
  318. MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
  319. MODULE_DESCRIPTION("LM77 driver");
  320. MODULE_LICENSE("GPL");