adt7410.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * adt7410.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/i2c.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #include <linux/delay.h>
  33. /*
  34. * ADT7410 registers definition
  35. */
  36. #define ADT7410_TEMPERATURE 0
  37. #define ADT7410_STATUS 2
  38. #define ADT7410_CONFIG 3
  39. #define ADT7410_T_ALARM_HIGH 4
  40. #define ADT7410_T_ALARM_LOW 6
  41. #define ADT7410_T_CRIT 8
  42. #define ADT7410_T_HYST 0xA
  43. /*
  44. * ADT7410 status
  45. */
  46. #define ADT7410_STAT_T_LOW (1 << 4)
  47. #define ADT7410_STAT_T_HIGH (1 << 5)
  48. #define ADT7410_STAT_T_CRIT (1 << 6)
  49. #define ADT7410_STAT_NOT_RDY (1 << 7)
  50. /*
  51. * ADT7410 config
  52. */
  53. #define ADT7410_FAULT_QUEUE_MASK (1 << 0 | 1 << 1)
  54. #define ADT7410_CT_POLARITY (1 << 2)
  55. #define ADT7410_INT_POLARITY (1 << 3)
  56. #define ADT7410_EVENT_MODE (1 << 4)
  57. #define ADT7410_MODE_MASK (1 << 5 | 1 << 6)
  58. #define ADT7410_FULL (0 << 5 | 0 << 6)
  59. #define ADT7410_PD (1 << 5 | 1 << 6)
  60. #define ADT7410_RESOLUTION (1 << 7)
  61. /*
  62. * ADT7410 masks
  63. */
  64. #define ADT7410_T13_VALUE_MASK 0xFFF8
  65. #define ADT7410_T_HYST_MASK 0xF
  66. /* straight from the datasheet */
  67. #define ADT7410_TEMP_MIN (-55000)
  68. #define ADT7410_TEMP_MAX 150000
  69. enum adt7410_type { /* keep sorted in alphabetical order */
  70. adt7410,
  71. };
  72. static const u8 ADT7410_REG_TEMP[4] = {
  73. ADT7410_TEMPERATURE, /* input */
  74. ADT7410_T_ALARM_HIGH, /* high */
  75. ADT7410_T_ALARM_LOW, /* low */
  76. ADT7410_T_CRIT, /* critical */
  77. };
  78. /* Each client has this additional data */
  79. struct adt7410_data {
  80. struct device *hwmon_dev;
  81. struct mutex update_lock;
  82. u8 config;
  83. u8 oldconfig;
  84. bool valid; /* true if registers valid */
  85. unsigned long last_updated; /* In jiffies */
  86. s16 temp[4]; /* Register values,
  87. 0 = input
  88. 1 = high
  89. 2 = low
  90. 3 = critical */
  91. u8 hyst; /* hysteresis offset */
  92. };
  93. /*
  94. * adt7410 register access by I2C
  95. */
  96. static int adt7410_temp_ready(struct i2c_client *client)
  97. {
  98. int i, status;
  99. for (i = 0; i < 6; i++) {
  100. status = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
  101. if (status < 0)
  102. return status;
  103. if (!(status & ADT7410_STAT_NOT_RDY))
  104. return 0;
  105. msleep(60);
  106. }
  107. return -ETIMEDOUT;
  108. }
  109. static struct adt7410_data *adt7410_update_device(struct device *dev)
  110. {
  111. struct i2c_client *client = to_i2c_client(dev);
  112. struct adt7410_data *data = i2c_get_clientdata(client);
  113. struct adt7410_data *ret = data;
  114. mutex_lock(&data->update_lock);
  115. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  116. || !data->valid) {
  117. int i, status;
  118. dev_dbg(&client->dev, "Starting update\n");
  119. status = adt7410_temp_ready(client); /* check for new value */
  120. if (unlikely(status)) {
  121. ret = ERR_PTR(status);
  122. goto abort;
  123. }
  124. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  125. status = i2c_smbus_read_word_swapped(client,
  126. ADT7410_REG_TEMP[i]);
  127. if (unlikely(status < 0)) {
  128. dev_dbg(dev,
  129. "Failed to read value: reg %d, error %d\n",
  130. ADT7410_REG_TEMP[i], status);
  131. ret = ERR_PTR(status);
  132. goto abort;
  133. }
  134. data->temp[i] = status;
  135. }
  136. status = i2c_smbus_read_byte_data(client, ADT7410_T_HYST);
  137. if (unlikely(status < 0)) {
  138. dev_dbg(dev,
  139. "Failed to read value: reg %d, error %d\n",
  140. ADT7410_T_HYST, status);
  141. ret = ERR_PTR(status);
  142. goto abort;
  143. }
  144. data->hyst = status;
  145. data->last_updated = jiffies;
  146. data->valid = true;
  147. }
  148. abort:
  149. mutex_unlock(&data->update_lock);
  150. return ret;
  151. }
  152. static s16 ADT7410_TEMP_TO_REG(long temp)
  153. {
  154. return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7410_TEMP_MIN,
  155. ADT7410_TEMP_MAX) * 128, 1000);
  156. }
  157. static int ADT7410_REG_TO_TEMP(struct adt7410_data *data, s16 reg)
  158. {
  159. /* in 13 bit mode, bits 0-2 are status flags - mask them out */
  160. if (!(data->config & ADT7410_RESOLUTION))
  161. reg &= ADT7410_T13_VALUE_MASK;
  162. /*
  163. * temperature is stored in twos complement format, in steps of
  164. * 1/128°C
  165. */
  166. return DIV_ROUND_CLOSEST(reg * 1000, 128);
  167. }
  168. /*-----------------------------------------------------------------------*/
  169. /* sysfs attributes for hwmon */
  170. static ssize_t adt7410_show_temp(struct device *dev,
  171. struct device_attribute *da, char *buf)
  172. {
  173. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  174. struct adt7410_data *data = adt7410_update_device(dev);
  175. if (IS_ERR(data))
  176. return PTR_ERR(data);
  177. return sprintf(buf, "%d\n", ADT7410_REG_TO_TEMP(data,
  178. data->temp[attr->index]));
  179. }
  180. static ssize_t adt7410_set_temp(struct device *dev,
  181. struct device_attribute *da,
  182. const char *buf, size_t count)
  183. {
  184. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  185. struct i2c_client *client = to_i2c_client(dev);
  186. struct adt7410_data *data = i2c_get_clientdata(client);
  187. int nr = attr->index;
  188. long temp;
  189. int ret;
  190. ret = kstrtol(buf, 10, &temp);
  191. if (ret)
  192. return ret;
  193. mutex_lock(&data->update_lock);
  194. data->temp[nr] = ADT7410_TEMP_TO_REG(temp);
  195. ret = i2c_smbus_write_word_swapped(client, ADT7410_REG_TEMP[nr],
  196. data->temp[nr]);
  197. if (ret)
  198. count = ret;
  199. mutex_unlock(&data->update_lock);
  200. return count;
  201. }
  202. static ssize_t adt7410_show_t_hyst(struct device *dev,
  203. struct device_attribute *da,
  204. char *buf)
  205. {
  206. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  207. struct adt7410_data *data;
  208. int nr = attr->index;
  209. int hyst;
  210. data = adt7410_update_device(dev);
  211. if (IS_ERR(data))
  212. return PTR_ERR(data);
  213. hyst = (data->hyst & ADT7410_T_HYST_MASK) * 1000;
  214. /*
  215. * hysteresis is stored as a 4 bit offset in the device, convert it
  216. * to an absolute value
  217. */
  218. if (nr == 2) /* min has positive offset, others have negative */
  219. hyst = -hyst;
  220. return sprintf(buf, "%d\n",
  221. ADT7410_REG_TO_TEMP(data, data->temp[nr]) - hyst);
  222. }
  223. static ssize_t adt7410_set_t_hyst(struct device *dev,
  224. struct device_attribute *da,
  225. const char *buf, size_t count)
  226. {
  227. struct i2c_client *client = to_i2c_client(dev);
  228. struct adt7410_data *data = i2c_get_clientdata(client);
  229. int limit, ret;
  230. long hyst;
  231. ret = kstrtol(buf, 10, &hyst);
  232. if (ret)
  233. return ret;
  234. /* convert absolute hysteresis value to a 4 bit delta value */
  235. limit = ADT7410_REG_TO_TEMP(data, data->temp[1]);
  236. hyst = clamp_val(hyst, ADT7410_TEMP_MIN, ADT7410_TEMP_MAX);
  237. data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000), 0,
  238. ADT7410_T_HYST_MASK);
  239. ret = i2c_smbus_write_byte_data(client, ADT7410_T_HYST, data->hyst);
  240. if (ret)
  241. return ret;
  242. return count;
  243. }
  244. static ssize_t adt7410_show_alarm(struct device *dev,
  245. struct device_attribute *da,
  246. char *buf)
  247. {
  248. struct i2c_client *client = to_i2c_client(dev);
  249. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  250. int ret;
  251. ret = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
  252. if (ret < 0)
  253. return ret;
  254. return sprintf(buf, "%d\n", !!(ret & attr->index));
  255. }
  256. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7410_show_temp, NULL, 0);
  257. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  258. adt7410_show_temp, adt7410_set_temp, 1);
  259. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  260. adt7410_show_temp, adt7410_set_temp, 2);
  261. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  262. adt7410_show_temp, adt7410_set_temp, 3);
  263. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  264. adt7410_show_t_hyst, adt7410_set_t_hyst, 1);
  265. static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
  266. adt7410_show_t_hyst, NULL, 2);
  267. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
  268. adt7410_show_t_hyst, NULL, 3);
  269. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, adt7410_show_alarm,
  270. NULL, ADT7410_STAT_T_LOW);
  271. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adt7410_show_alarm,
  272. NULL, ADT7410_STAT_T_HIGH);
  273. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, adt7410_show_alarm,
  274. NULL, ADT7410_STAT_T_CRIT);
  275. static struct attribute *adt7410_attributes[] = {
  276. &sensor_dev_attr_temp1_input.dev_attr.attr,
  277. &sensor_dev_attr_temp1_max.dev_attr.attr,
  278. &sensor_dev_attr_temp1_min.dev_attr.attr,
  279. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  280. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  281. &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
  282. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  283. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  284. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  285. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  286. NULL
  287. };
  288. static const struct attribute_group adt7410_group = {
  289. .attrs = adt7410_attributes,
  290. };
  291. /*-----------------------------------------------------------------------*/
  292. /* device probe and removal */
  293. static int adt7410_probe(struct i2c_client *client,
  294. const struct i2c_device_id *id)
  295. {
  296. struct adt7410_data *data;
  297. int ret;
  298. if (!i2c_check_functionality(client->adapter,
  299. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  300. return -ENODEV;
  301. data = devm_kzalloc(&client->dev, sizeof(struct adt7410_data),
  302. GFP_KERNEL);
  303. if (!data)
  304. return -ENOMEM;
  305. i2c_set_clientdata(client, data);
  306. mutex_init(&data->update_lock);
  307. /* configure as specified */
  308. ret = i2c_smbus_read_byte_data(client, ADT7410_CONFIG);
  309. if (ret < 0) {
  310. dev_dbg(&client->dev, "Can't read config? %d\n", ret);
  311. return ret;
  312. }
  313. data->oldconfig = ret;
  314. /*
  315. * Set to 16 bit resolution, continous conversion and comparator mode.
  316. */
  317. ret &= ~ADT7410_MODE_MASK;
  318. data->config = ret | ADT7410_FULL | ADT7410_RESOLUTION |
  319. ADT7410_EVENT_MODE;
  320. if (data->config != data->oldconfig) {
  321. ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
  322. data->config);
  323. if (ret)
  324. return ret;
  325. }
  326. dev_dbg(&client->dev, "Config %02x\n", data->config);
  327. /* Register sysfs hooks */
  328. ret = sysfs_create_group(&client->dev.kobj, &adt7410_group);
  329. if (ret)
  330. goto exit_restore;
  331. data->hwmon_dev = hwmon_device_register(&client->dev);
  332. if (IS_ERR(data->hwmon_dev)) {
  333. ret = PTR_ERR(data->hwmon_dev);
  334. goto exit_remove;
  335. }
  336. dev_info(&client->dev, "sensor '%s'\n", client->name);
  337. return 0;
  338. exit_remove:
  339. sysfs_remove_group(&client->dev.kobj, &adt7410_group);
  340. exit_restore:
  341. i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->oldconfig);
  342. return ret;
  343. }
  344. static int adt7410_remove(struct i2c_client *client)
  345. {
  346. struct adt7410_data *data = i2c_get_clientdata(client);
  347. hwmon_device_unregister(data->hwmon_dev);
  348. sysfs_remove_group(&client->dev.kobj, &adt7410_group);
  349. if (data->oldconfig != data->config)
  350. i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
  351. data->oldconfig);
  352. return 0;
  353. }
  354. static const struct i2c_device_id adt7410_ids[] = {
  355. { "adt7410", adt7410, },
  356. { "adt7420", adt7410, },
  357. { /* LIST END */ }
  358. };
  359. MODULE_DEVICE_TABLE(i2c, adt7410_ids);
  360. #ifdef CONFIG_PM_SLEEP
  361. static int adt7410_suspend(struct device *dev)
  362. {
  363. int ret;
  364. struct i2c_client *client = to_i2c_client(dev);
  365. struct adt7410_data *data = i2c_get_clientdata(client);
  366. ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
  367. data->config | ADT7410_PD);
  368. return ret;
  369. }
  370. static int adt7410_resume(struct device *dev)
  371. {
  372. int ret;
  373. struct i2c_client *client = to_i2c_client(dev);
  374. struct adt7410_data *data = i2c_get_clientdata(client);
  375. ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->config);
  376. return ret;
  377. }
  378. static SIMPLE_DEV_PM_OPS(adt7410_dev_pm_ops, adt7410_suspend, adt7410_resume);
  379. #define ADT7410_DEV_PM_OPS (&adt7410_dev_pm_ops)
  380. #else
  381. #define ADT7410_DEV_PM_OPS NULL
  382. #endif /* CONFIG_PM */
  383. static struct i2c_driver adt7410_driver = {
  384. .class = I2C_CLASS_HWMON,
  385. .driver = {
  386. .name = "adt7410",
  387. .pm = ADT7410_DEV_PM_OPS,
  388. },
  389. .probe = adt7410_probe,
  390. .remove = adt7410_remove,
  391. .id_table = adt7410_ids,
  392. .address_list = I2C_ADDRS(0x48, 0x49, 0x4a, 0x4b),
  393. };
  394. module_i2c_driver(adt7410_driver);
  395. MODULE_AUTHOR("Hartmut Knaack");
  396. MODULE_DESCRIPTION("ADT7410/ADT7420 driver");
  397. MODULE_LICENSE("GPL");