adt7410.c 13 KB

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