lm77.c 11 KB

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