lm77.c 9.9 KB

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