adt7x10.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * adt7x10.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * This driver handles the ADT7410 and compatible digital temperature sensors.
  5. * Hartmut Knaack <knaack.h@gmx.de> 2012-07-22
  6. * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
  7. * and adt7410.c from iio-staging by Sonic Zhang <sonic.zhang@analog.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  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. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-sysfs.h>
  29. #include <linux/err.h>
  30. #include <linux/mutex.h>
  31. #include <linux/delay.h>
  32. #include <linux/interrupt.h>
  33. #include "adt7x10.h"
  34. /*
  35. * ADT7X10 status
  36. */
  37. #define ADT7X10_STAT_T_LOW (1 << 4)
  38. #define ADT7X10_STAT_T_HIGH (1 << 5)
  39. #define ADT7X10_STAT_T_CRIT (1 << 6)
  40. #define ADT7X10_STAT_NOT_RDY (1 << 7)
  41. /*
  42. * ADT7X10 config
  43. */
  44. #define ADT7X10_FAULT_QUEUE_MASK (1 << 0 | 1 << 1)
  45. #define ADT7X10_CT_POLARITY (1 << 2)
  46. #define ADT7X10_INT_POLARITY (1 << 3)
  47. #define ADT7X10_EVENT_MODE (1 << 4)
  48. #define ADT7X10_MODE_MASK (1 << 5 | 1 << 6)
  49. #define ADT7X10_FULL (0 << 5 | 0 << 6)
  50. #define ADT7X10_PD (1 << 5 | 1 << 6)
  51. #define ADT7X10_RESOLUTION (1 << 7)
  52. /*
  53. * ADT7X10 masks
  54. */
  55. #define ADT7X10_T13_VALUE_MASK 0xFFF8
  56. #define ADT7X10_T_HYST_MASK 0xF
  57. /* straight from the datasheet */
  58. #define ADT7X10_TEMP_MIN (-55000)
  59. #define ADT7X10_TEMP_MAX 150000
  60. /* Each client has this additional data */
  61. struct adt7x10_data {
  62. const struct adt7x10_ops *ops;
  63. const char *name;
  64. struct device *hwmon_dev;
  65. struct mutex update_lock;
  66. u8 config;
  67. u8 oldconfig;
  68. bool valid; /* true if registers valid */
  69. unsigned long last_updated; /* In jiffies */
  70. s16 temp[4]; /* Register values,
  71. 0 = input
  72. 1 = high
  73. 2 = low
  74. 3 = critical */
  75. u8 hyst; /* hysteresis offset */
  76. };
  77. static int adt7x10_read_byte(struct device *dev, u8 reg)
  78. {
  79. struct adt7x10_data *d = dev_get_drvdata(dev);
  80. return d->ops->read_byte(dev, reg);
  81. }
  82. static int adt7x10_write_byte(struct device *dev, u8 reg, u8 data)
  83. {
  84. struct adt7x10_data *d = dev_get_drvdata(dev);
  85. return d->ops->write_byte(dev, reg, data);
  86. }
  87. static int adt7x10_read_word(struct device *dev, u8 reg)
  88. {
  89. struct adt7x10_data *d = dev_get_drvdata(dev);
  90. return d->ops->read_word(dev, reg);
  91. }
  92. static int adt7x10_write_word(struct device *dev, u8 reg, u16 data)
  93. {
  94. struct adt7x10_data *d = dev_get_drvdata(dev);
  95. return d->ops->write_word(dev, reg, data);
  96. }
  97. static const u8 ADT7X10_REG_TEMP[4] = {
  98. ADT7X10_TEMPERATURE, /* input */
  99. ADT7X10_T_ALARM_HIGH, /* high */
  100. ADT7X10_T_ALARM_LOW, /* low */
  101. ADT7X10_T_CRIT, /* critical */
  102. };
  103. static irqreturn_t adt7x10_irq_handler(int irq, void *private)
  104. {
  105. struct device *dev = private;
  106. int status;
  107. status = adt7x10_read_byte(dev, ADT7X10_STATUS);
  108. if (status < 0)
  109. return IRQ_HANDLED;
  110. if (status & ADT7X10_STAT_T_HIGH)
  111. sysfs_notify(&dev->kobj, NULL, "temp1_max_alarm");
  112. if (status & ADT7X10_STAT_T_LOW)
  113. sysfs_notify(&dev->kobj, NULL, "temp1_min_alarm");
  114. if (status & ADT7X10_STAT_T_CRIT)
  115. sysfs_notify(&dev->kobj, NULL, "temp1_crit_alarm");
  116. return IRQ_HANDLED;
  117. }
  118. static int adt7x10_temp_ready(struct device *dev)
  119. {
  120. int i, status;
  121. for (i = 0; i < 6; i++) {
  122. status = adt7x10_read_byte(dev, ADT7X10_STATUS);
  123. if (status < 0)
  124. return status;
  125. if (!(status & ADT7X10_STAT_NOT_RDY))
  126. return 0;
  127. msleep(60);
  128. }
  129. return -ETIMEDOUT;
  130. }
  131. static int adt7x10_update_temp(struct device *dev)
  132. {
  133. struct adt7x10_data *data = dev_get_drvdata(dev);
  134. int ret = 0;
  135. mutex_lock(&data->update_lock);
  136. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  137. || !data->valid) {
  138. int temp;
  139. dev_dbg(dev, "Starting update\n");
  140. ret = adt7x10_temp_ready(dev); /* check for new value */
  141. if (ret)
  142. goto abort;
  143. temp = adt7x10_read_word(dev, ADT7X10_REG_TEMP[0]);
  144. if (temp < 0) {
  145. ret = temp;
  146. dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
  147. ADT7X10_REG_TEMP[0], ret);
  148. goto abort;
  149. }
  150. data->temp[0] = temp;
  151. data->last_updated = jiffies;
  152. data->valid = true;
  153. }
  154. abort:
  155. mutex_unlock(&data->update_lock);
  156. return ret;
  157. }
  158. static int adt7x10_fill_cache(struct device *dev)
  159. {
  160. struct adt7x10_data *data = dev_get_drvdata(dev);
  161. int ret;
  162. int i;
  163. for (i = 1; i < ARRAY_SIZE(data->temp); i++) {
  164. ret = adt7x10_read_word(dev, ADT7X10_REG_TEMP[i]);
  165. if (ret < 0) {
  166. dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
  167. ADT7X10_REG_TEMP[i], ret);
  168. return ret;
  169. }
  170. data->temp[i] = ret;
  171. }
  172. ret = adt7x10_read_byte(dev, ADT7X10_T_HYST);
  173. if (ret < 0) {
  174. dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
  175. ADT7X10_T_HYST, ret);
  176. return ret;
  177. }
  178. data->hyst = ret;
  179. return 0;
  180. }
  181. static s16 ADT7X10_TEMP_TO_REG(long temp)
  182. {
  183. return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7X10_TEMP_MIN,
  184. ADT7X10_TEMP_MAX) * 128, 1000);
  185. }
  186. static int ADT7X10_REG_TO_TEMP(struct adt7x10_data *data, s16 reg)
  187. {
  188. /* in 13 bit mode, bits 0-2 are status flags - mask them out */
  189. if (!(data->config & ADT7X10_RESOLUTION))
  190. reg &= ADT7X10_T13_VALUE_MASK;
  191. /*
  192. * temperature is stored in twos complement format, in steps of
  193. * 1/128°C
  194. */
  195. return DIV_ROUND_CLOSEST(reg * 1000, 128);
  196. }
  197. /*-----------------------------------------------------------------------*/
  198. /* sysfs attributes for hwmon */
  199. static ssize_t adt7x10_show_temp(struct device *dev,
  200. struct device_attribute *da,
  201. char *buf)
  202. {
  203. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  204. struct adt7x10_data *data = dev_get_drvdata(dev);
  205. if (attr->index == 0) {
  206. int ret;
  207. ret = adt7x10_update_temp(dev);
  208. if (ret)
  209. return ret;
  210. }
  211. return sprintf(buf, "%d\n", ADT7X10_REG_TO_TEMP(data,
  212. data->temp[attr->index]));
  213. }
  214. static ssize_t adt7x10_set_temp(struct device *dev,
  215. struct device_attribute *da,
  216. const char *buf, size_t count)
  217. {
  218. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  219. struct adt7x10_data *data = dev_get_drvdata(dev);
  220. int nr = attr->index;
  221. long temp;
  222. int ret;
  223. ret = kstrtol(buf, 10, &temp);
  224. if (ret)
  225. return ret;
  226. mutex_lock(&data->update_lock);
  227. data->temp[nr] = ADT7X10_TEMP_TO_REG(temp);
  228. ret = adt7x10_write_word(dev, ADT7X10_REG_TEMP[nr], data->temp[nr]);
  229. if (ret)
  230. count = ret;
  231. mutex_unlock(&data->update_lock);
  232. return count;
  233. }
  234. static ssize_t adt7x10_show_t_hyst(struct device *dev,
  235. struct device_attribute *da,
  236. char *buf)
  237. {
  238. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  239. struct adt7x10_data *data = dev_get_drvdata(dev);
  240. int nr = attr->index;
  241. int hyst;
  242. hyst = (data->hyst & ADT7X10_T_HYST_MASK) * 1000;
  243. /*
  244. * hysteresis is stored as a 4 bit offset in the device, convert it
  245. * to an absolute value
  246. */
  247. if (nr == 2) /* min has positive offset, others have negative */
  248. hyst = -hyst;
  249. return sprintf(buf, "%d\n",
  250. ADT7X10_REG_TO_TEMP(data, data->temp[nr]) - hyst);
  251. }
  252. static ssize_t adt7x10_set_t_hyst(struct device *dev,
  253. struct device_attribute *da,
  254. const char *buf, size_t count)
  255. {
  256. struct adt7x10_data *data = dev_get_drvdata(dev);
  257. int limit, ret;
  258. long hyst;
  259. ret = kstrtol(buf, 10, &hyst);
  260. if (ret)
  261. return ret;
  262. /* convert absolute hysteresis value to a 4 bit delta value */
  263. limit = ADT7X10_REG_TO_TEMP(data, data->temp[1]);
  264. hyst = clamp_val(hyst, ADT7X10_TEMP_MIN, ADT7X10_TEMP_MAX);
  265. data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000),
  266. 0, ADT7X10_T_HYST_MASK);
  267. ret = adt7x10_write_byte(dev, ADT7X10_T_HYST, data->hyst);
  268. if (ret)
  269. return ret;
  270. return count;
  271. }
  272. static ssize_t adt7x10_show_alarm(struct device *dev,
  273. struct device_attribute *da,
  274. char *buf)
  275. {
  276. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  277. int ret;
  278. ret = adt7x10_read_byte(dev, ADT7X10_STATUS);
  279. if (ret < 0)
  280. return ret;
  281. return sprintf(buf, "%d\n", !!(ret & attr->index));
  282. }
  283. static ssize_t adt7x10_show_name(struct device *dev,
  284. struct device_attribute *da,
  285. char *buf)
  286. {
  287. struct adt7x10_data *data = dev_get_drvdata(dev);
  288. return sprintf(buf, "%s\n", data->name);
  289. }
  290. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7x10_show_temp, NULL, 0);
  291. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  292. adt7x10_show_temp, adt7x10_set_temp, 1);
  293. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  294. adt7x10_show_temp, adt7x10_set_temp, 2);
  295. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  296. adt7x10_show_temp, adt7x10_set_temp, 3);
  297. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  298. adt7x10_show_t_hyst, adt7x10_set_t_hyst, 1);
  299. static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
  300. adt7x10_show_t_hyst, NULL, 2);
  301. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
  302. adt7x10_show_t_hyst, NULL, 3);
  303. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, adt7x10_show_alarm,
  304. NULL, ADT7X10_STAT_T_LOW);
  305. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adt7x10_show_alarm,
  306. NULL, ADT7X10_STAT_T_HIGH);
  307. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, adt7x10_show_alarm,
  308. NULL, ADT7X10_STAT_T_CRIT);
  309. static DEVICE_ATTR(name, S_IRUGO, adt7x10_show_name, NULL);
  310. static struct attribute *adt7x10_attributes[] = {
  311. &sensor_dev_attr_temp1_input.dev_attr.attr,
  312. &sensor_dev_attr_temp1_max.dev_attr.attr,
  313. &sensor_dev_attr_temp1_min.dev_attr.attr,
  314. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  315. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  316. &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
  317. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  318. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  319. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  320. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  321. NULL
  322. };
  323. static const struct attribute_group adt7x10_group = {
  324. .attrs = adt7x10_attributes,
  325. };
  326. int adt7x10_probe(struct device *dev, const char *name, int irq,
  327. const struct adt7x10_ops *ops)
  328. {
  329. struct adt7x10_data *data;
  330. int ret;
  331. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  332. if (!data)
  333. return -ENOMEM;
  334. data->ops = ops;
  335. data->name = name;
  336. dev_set_drvdata(dev, data);
  337. mutex_init(&data->update_lock);
  338. /* configure as specified */
  339. ret = adt7x10_read_byte(dev, ADT7X10_CONFIG);
  340. if (ret < 0) {
  341. dev_dbg(dev, "Can't read config? %d\n", ret);
  342. return ret;
  343. }
  344. data->oldconfig = ret;
  345. /*
  346. * Set to 16 bit resolution, continous conversion and comparator mode.
  347. */
  348. data->config = data->oldconfig;
  349. data->config &= ~(ADT7X10_MODE_MASK | ADT7X10_CT_POLARITY |
  350. ADT7X10_INT_POLARITY);
  351. data->config |= ADT7X10_FULL | ADT7X10_RESOLUTION | ADT7X10_EVENT_MODE;
  352. if (data->config != data->oldconfig) {
  353. ret = adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
  354. if (ret)
  355. return ret;
  356. }
  357. dev_dbg(dev, "Config %02x\n", data->config);
  358. ret = adt7x10_fill_cache(dev);
  359. if (ret)
  360. goto exit_restore;
  361. /* Register sysfs hooks */
  362. ret = sysfs_create_group(&dev->kobj, &adt7x10_group);
  363. if (ret)
  364. goto exit_restore;
  365. /*
  366. * The I2C device will already have it's own 'name' attribute, but for
  367. * the SPI device we need to register it. name will only be non NULL if
  368. * the device doesn't register the 'name' attribute on its own.
  369. */
  370. if (name) {
  371. ret = device_create_file(dev, &dev_attr_name);
  372. if (ret)
  373. goto exit_remove;
  374. }
  375. data->hwmon_dev = hwmon_device_register(dev);
  376. if (IS_ERR(data->hwmon_dev)) {
  377. ret = PTR_ERR(data->hwmon_dev);
  378. goto exit_remove_name;
  379. }
  380. if (irq > 0) {
  381. ret = request_threaded_irq(irq, NULL, adt7x10_irq_handler,
  382. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  383. dev_name(dev), dev);
  384. if (ret)
  385. goto exit_hwmon_device_unregister;
  386. }
  387. return 0;
  388. exit_hwmon_device_unregister:
  389. hwmon_device_unregister(data->hwmon_dev);
  390. exit_remove_name:
  391. if (name)
  392. device_remove_file(dev, &dev_attr_name);
  393. exit_remove:
  394. sysfs_remove_group(&dev->kobj, &adt7x10_group);
  395. exit_restore:
  396. adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
  397. return ret;
  398. }
  399. EXPORT_SYMBOL_GPL(adt7x10_probe);
  400. int adt7x10_remove(struct device *dev, int irq)
  401. {
  402. struct adt7x10_data *data = dev_get_drvdata(dev);
  403. if (irq > 0)
  404. free_irq(irq, dev);
  405. hwmon_device_unregister(data->hwmon_dev);
  406. if (data->name)
  407. device_remove_file(dev, &dev_attr_name);
  408. sysfs_remove_group(&dev->kobj, &adt7x10_group);
  409. if (data->oldconfig != data->config)
  410. adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
  411. return 0;
  412. }
  413. EXPORT_SYMBOL_GPL(adt7x10_remove);
  414. #ifdef CONFIG_PM_SLEEP
  415. static int adt7x10_suspend(struct device *dev)
  416. {
  417. struct adt7x10_data *data = dev_get_drvdata(dev);
  418. return adt7x10_write_byte(dev, ADT7X10_CONFIG,
  419. data->config | ADT7X10_PD);
  420. }
  421. static int adt7x10_resume(struct device *dev)
  422. {
  423. struct adt7x10_data *data = dev_get_drvdata(dev);
  424. return adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
  425. }
  426. SIMPLE_DEV_PM_OPS(adt7x10_dev_pm_ops, adt7x10_suspend, adt7x10_resume);
  427. EXPORT_SYMBOL_GPL(adt7x10_dev_pm_ops);
  428. #endif /* CONFIG_PM_SLEEP */
  429. MODULE_AUTHOR("Hartmut Knaack");
  430. MODULE_DESCRIPTION("ADT7410/ADT7420, ADT7310/ADT7320 common code");
  431. MODULE_LICENSE("GPL");