adc128d818.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Driver for TI ADC128D818 System Monitor with Temperature Sensor
  3. *
  4. * Copyright (c) 2014 Guenter Roeck
  5. *
  6. * Derived from lm80.c
  7. * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  8. * and Philip Edelbrock <phil@netroedge.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/err.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/mutex.h>
  29. /* Addresses to scan
  30. * The chip also supports addresses 0x35..0x37. Don't scan those addresses
  31. * since they are also used by some EEPROMs, which may result in false
  32. * positives.
  33. */
  34. static const unsigned short normal_i2c[] = {
  35. 0x1d, 0x1e, 0x1f, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
  36. /* registers */
  37. #define ADC128_REG_IN_MAX(nr) (0x2a + (nr) * 2)
  38. #define ADC128_REG_IN_MIN(nr) (0x2b + (nr) * 2)
  39. #define ADC128_REG_IN(nr) (0x20 + (nr))
  40. #define ADC128_REG_TEMP 0x27
  41. #define ADC128_REG_TEMP_MAX 0x38
  42. #define ADC128_REG_TEMP_HYST 0x39
  43. #define ADC128_REG_CONFIG 0x00
  44. #define ADC128_REG_ALARM 0x01
  45. #define ADC128_REG_MASK 0x03
  46. #define ADC128_REG_CONV_RATE 0x07
  47. #define ADC128_REG_ONESHOT 0x09
  48. #define ADC128_REG_SHUTDOWN 0x0a
  49. #define ADC128_REG_CONFIG_ADV 0x0b
  50. #define ADC128_REG_BUSY_STATUS 0x0c
  51. #define ADC128_REG_MAN_ID 0x3e
  52. #define ADC128_REG_DEV_ID 0x3f
  53. struct adc128_data {
  54. struct i2c_client *client;
  55. struct regulator *regulator;
  56. int vref; /* Reference voltage in mV */
  57. struct mutex update_lock;
  58. bool valid; /* true if following fields are valid */
  59. unsigned long last_updated; /* In jiffies */
  60. u16 in[3][7]; /* Register value, normalized to 12 bit
  61. * 0: input voltage
  62. * 1: min limit
  63. * 2: max limit
  64. */
  65. s16 temp[3]; /* Register value, normalized to 9 bit
  66. * 0: sensor 1: limit 2: hyst
  67. */
  68. u8 alarms; /* alarm register value */
  69. };
  70. static struct adc128_data *adc128_update_device(struct device *dev)
  71. {
  72. struct adc128_data *data = dev_get_drvdata(dev);
  73. struct i2c_client *client = data->client;
  74. struct adc128_data *ret = data;
  75. int i, rv;
  76. mutex_lock(&data->update_lock);
  77. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  78. for (i = 0; i < 7; i++) {
  79. rv = i2c_smbus_read_word_swapped(client,
  80. ADC128_REG_IN(i));
  81. if (rv < 0)
  82. goto abort;
  83. data->in[0][i] = rv >> 4;
  84. rv = i2c_smbus_read_byte_data(client,
  85. ADC128_REG_IN_MIN(i));
  86. if (rv < 0)
  87. goto abort;
  88. data->in[1][i] = rv << 4;
  89. rv = i2c_smbus_read_byte_data(client,
  90. ADC128_REG_IN_MAX(i));
  91. if (rv < 0)
  92. goto abort;
  93. data->in[2][i] = rv << 4;
  94. }
  95. rv = i2c_smbus_read_word_swapped(client, ADC128_REG_TEMP);
  96. if (rv < 0)
  97. goto abort;
  98. data->temp[0] = rv >> 7;
  99. rv = i2c_smbus_read_byte_data(client, ADC128_REG_TEMP_MAX);
  100. if (rv < 0)
  101. goto abort;
  102. data->temp[1] = rv << 1;
  103. rv = i2c_smbus_read_byte_data(client, ADC128_REG_TEMP_HYST);
  104. if (rv < 0)
  105. goto abort;
  106. data->temp[2] = rv << 1;
  107. rv = i2c_smbus_read_byte_data(client, ADC128_REG_ALARM);
  108. if (rv < 0)
  109. goto abort;
  110. data->alarms |= rv;
  111. data->last_updated = jiffies;
  112. data->valid = true;
  113. }
  114. goto done;
  115. abort:
  116. ret = ERR_PTR(rv);
  117. data->valid = false;
  118. done:
  119. mutex_unlock(&data->update_lock);
  120. return ret;
  121. }
  122. static ssize_t adc128_show_in(struct device *dev, struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct adc128_data *data = adc128_update_device(dev);
  126. int index = to_sensor_dev_attr_2(attr)->index;
  127. int nr = to_sensor_dev_attr_2(attr)->nr;
  128. int val;
  129. if (IS_ERR(data))
  130. return PTR_ERR(data);
  131. val = DIV_ROUND_CLOSEST(data->in[index][nr] * data->vref, 4095);
  132. return sprintf(buf, "%d\n", val);
  133. }
  134. static ssize_t adc128_set_in(struct device *dev, struct device_attribute *attr,
  135. const char *buf, size_t count)
  136. {
  137. struct adc128_data *data = dev_get_drvdata(dev);
  138. int index = to_sensor_dev_attr_2(attr)->index;
  139. int nr = to_sensor_dev_attr_2(attr)->nr;
  140. u8 reg, regval;
  141. long val;
  142. int err;
  143. err = kstrtol(buf, 10, &val);
  144. if (err < 0)
  145. return err;
  146. mutex_lock(&data->update_lock);
  147. /* 10 mV LSB on limit registers */
  148. regval = clamp_val(DIV_ROUND_CLOSEST(val, 10), 0, 255);
  149. data->in[index][nr] = regval << 4;
  150. reg = index == 1 ? ADC128_REG_IN_MIN(nr) : ADC128_REG_IN_MAX(nr);
  151. i2c_smbus_write_byte_data(data->client, reg, regval);
  152. mutex_unlock(&data->update_lock);
  153. return count;
  154. }
  155. static ssize_t adc128_show_temp(struct device *dev,
  156. struct device_attribute *attr, char *buf)
  157. {
  158. struct adc128_data *data = adc128_update_device(dev);
  159. int index = to_sensor_dev_attr(attr)->index;
  160. int temp;
  161. if (IS_ERR(data))
  162. return PTR_ERR(data);
  163. temp = (data->temp[index] << 7) >> 7; /* sign extend */
  164. return sprintf(buf, "%d\n", temp * 500);/* 0.5 degrees C resolution */
  165. }
  166. static ssize_t adc128_set_temp(struct device *dev,
  167. struct device_attribute *attr,
  168. const char *buf, size_t count)
  169. {
  170. struct adc128_data *data = dev_get_drvdata(dev);
  171. int index = to_sensor_dev_attr(attr)->index;
  172. long val;
  173. int err;
  174. s8 regval;
  175. err = kstrtol(buf, 10, &val);
  176. if (err < 0)
  177. return err;
  178. mutex_lock(&data->update_lock);
  179. regval = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
  180. data->temp[index] = regval << 1;
  181. i2c_smbus_write_byte_data(data->client,
  182. index == 1 ? ADC128_REG_TEMP_MAX
  183. : ADC128_REG_TEMP_HYST,
  184. regval);
  185. mutex_unlock(&data->update_lock);
  186. return count;
  187. }
  188. static ssize_t adc128_show_alarm(struct device *dev,
  189. struct device_attribute *attr, char *buf)
  190. {
  191. struct adc128_data *data = adc128_update_device(dev);
  192. int mask = 1 << to_sensor_dev_attr(attr)->index;
  193. u8 alarms;
  194. if (IS_ERR(data))
  195. return PTR_ERR(data);
  196. /*
  197. * Clear an alarm after reporting it to user space. If it is still
  198. * active, the next update sequence will set the alarm bit again.
  199. */
  200. alarms = data->alarms;
  201. data->alarms &= ~mask;
  202. return sprintf(buf, "%u\n", !!(alarms & mask));
  203. }
  204. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO,
  205. adc128_show_in, NULL, 0, 0);
  206. static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO,
  207. adc128_show_in, adc128_set_in, 0, 1);
  208. static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO,
  209. adc128_show_in, adc128_set_in, 0, 2);
  210. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO,
  211. adc128_show_in, NULL, 1, 0);
  212. static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO,
  213. adc128_show_in, adc128_set_in, 1, 1);
  214. static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO,
  215. adc128_show_in, adc128_set_in, 1, 2);
  216. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO,
  217. adc128_show_in, NULL, 2, 0);
  218. static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO,
  219. adc128_show_in, adc128_set_in, 2, 1);
  220. static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO,
  221. adc128_show_in, adc128_set_in, 2, 2);
  222. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO,
  223. adc128_show_in, NULL, 3, 0);
  224. static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO,
  225. adc128_show_in, adc128_set_in, 3, 1);
  226. static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO,
  227. adc128_show_in, adc128_set_in, 3, 2);
  228. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO,
  229. adc128_show_in, NULL, 4, 0);
  230. static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO,
  231. adc128_show_in, adc128_set_in, 4, 1);
  232. static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO,
  233. adc128_show_in, adc128_set_in, 4, 2);
  234. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO,
  235. adc128_show_in, NULL, 5, 0);
  236. static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO,
  237. adc128_show_in, adc128_set_in, 5, 1);
  238. static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO,
  239. adc128_show_in, adc128_set_in, 5, 2);
  240. static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO,
  241. adc128_show_in, NULL, 6, 0);
  242. static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO,
  243. adc128_show_in, adc128_set_in, 6, 1);
  244. static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO,
  245. adc128_show_in, adc128_set_in, 6, 2);
  246. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adc128_show_temp, NULL, 0);
  247. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  248. adc128_show_temp, adc128_set_temp, 1);
  249. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  250. adc128_show_temp, adc128_set_temp, 2);
  251. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, adc128_show_alarm, NULL, 0);
  252. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, adc128_show_alarm, NULL, 1);
  253. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, adc128_show_alarm, NULL, 2);
  254. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, adc128_show_alarm, NULL, 3);
  255. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, adc128_show_alarm, NULL, 4);
  256. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, adc128_show_alarm, NULL, 5);
  257. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, adc128_show_alarm, NULL, 6);
  258. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adc128_show_alarm, NULL, 7);
  259. static struct attribute *adc128_attrs[] = {
  260. &sensor_dev_attr_in0_min.dev_attr.attr,
  261. &sensor_dev_attr_in1_min.dev_attr.attr,
  262. &sensor_dev_attr_in2_min.dev_attr.attr,
  263. &sensor_dev_attr_in3_min.dev_attr.attr,
  264. &sensor_dev_attr_in4_min.dev_attr.attr,
  265. &sensor_dev_attr_in5_min.dev_attr.attr,
  266. &sensor_dev_attr_in6_min.dev_attr.attr,
  267. &sensor_dev_attr_in0_max.dev_attr.attr,
  268. &sensor_dev_attr_in1_max.dev_attr.attr,
  269. &sensor_dev_attr_in2_max.dev_attr.attr,
  270. &sensor_dev_attr_in3_max.dev_attr.attr,
  271. &sensor_dev_attr_in4_max.dev_attr.attr,
  272. &sensor_dev_attr_in5_max.dev_attr.attr,
  273. &sensor_dev_attr_in6_max.dev_attr.attr,
  274. &sensor_dev_attr_in0_input.dev_attr.attr,
  275. &sensor_dev_attr_in1_input.dev_attr.attr,
  276. &sensor_dev_attr_in2_input.dev_attr.attr,
  277. &sensor_dev_attr_in3_input.dev_attr.attr,
  278. &sensor_dev_attr_in4_input.dev_attr.attr,
  279. &sensor_dev_attr_in5_input.dev_attr.attr,
  280. &sensor_dev_attr_in6_input.dev_attr.attr,
  281. &sensor_dev_attr_temp1_input.dev_attr.attr,
  282. &sensor_dev_attr_temp1_max.dev_attr.attr,
  283. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  284. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  285. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  286. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  287. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  288. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  289. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  290. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  291. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  292. NULL
  293. };
  294. ATTRIBUTE_GROUPS(adc128);
  295. static int adc128_detect(struct i2c_client *client, struct i2c_board_info *info)
  296. {
  297. int man_id, dev_id;
  298. if (!i2c_check_functionality(client->adapter,
  299. I2C_FUNC_SMBUS_BYTE_DATA |
  300. I2C_FUNC_SMBUS_WORD_DATA))
  301. return -ENODEV;
  302. man_id = i2c_smbus_read_byte_data(client, ADC128_REG_MAN_ID);
  303. dev_id = i2c_smbus_read_byte_data(client, ADC128_REG_DEV_ID);
  304. if (man_id != 0x01 || dev_id != 0x09)
  305. return -ENODEV;
  306. /* Check unused bits for confirmation */
  307. if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG) & 0xf4)
  308. return -ENODEV;
  309. if (i2c_smbus_read_byte_data(client, ADC128_REG_CONV_RATE) & 0xfe)
  310. return -ENODEV;
  311. if (i2c_smbus_read_byte_data(client, ADC128_REG_ONESHOT) & 0xfe)
  312. return -ENODEV;
  313. if (i2c_smbus_read_byte_data(client, ADC128_REG_SHUTDOWN) & 0xfe)
  314. return -ENODEV;
  315. if (i2c_smbus_read_byte_data(client, ADC128_REG_CONFIG_ADV) & 0xf8)
  316. return -ENODEV;
  317. if (i2c_smbus_read_byte_data(client, ADC128_REG_BUSY_STATUS) & 0xfc)
  318. return -ENODEV;
  319. strlcpy(info->type, "adc128d818", I2C_NAME_SIZE);
  320. return 0;
  321. }
  322. static int adc128_init_client(struct adc128_data *data)
  323. {
  324. struct i2c_client *client = data->client;
  325. int err;
  326. /*
  327. * Reset chip to defaults.
  328. * This makes most other initializations unnecessary.
  329. */
  330. err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x80);
  331. if (err)
  332. return err;
  333. /* Start monitoring */
  334. err = i2c_smbus_write_byte_data(client, ADC128_REG_CONFIG, 0x01);
  335. if (err)
  336. return err;
  337. /* If external vref is selected, configure the chip to use it */
  338. if (data->regulator) {
  339. err = i2c_smbus_write_byte_data(client,
  340. ADC128_REG_CONFIG_ADV, 0x01);
  341. if (err)
  342. return err;
  343. }
  344. return 0;
  345. }
  346. static int adc128_probe(struct i2c_client *client,
  347. const struct i2c_device_id *id)
  348. {
  349. struct device *dev = &client->dev;
  350. struct regulator *regulator;
  351. struct device *hwmon_dev;
  352. struct adc128_data *data;
  353. int err, vref;
  354. data = devm_kzalloc(dev, sizeof(struct adc128_data), GFP_KERNEL);
  355. if (!data)
  356. return -ENOMEM;
  357. /* vref is optional. If specified, is used as chip reference voltage */
  358. regulator = devm_regulator_get_optional(dev, "vref");
  359. if (!IS_ERR(regulator)) {
  360. data->regulator = regulator;
  361. err = regulator_enable(regulator);
  362. if (err < 0)
  363. return err;
  364. vref = regulator_get_voltage(regulator);
  365. if (vref < 0) {
  366. err = vref;
  367. goto error;
  368. }
  369. data->vref = DIV_ROUND_CLOSEST(vref, 1000);
  370. } else {
  371. data->vref = 2560; /* 2.56V, in mV */
  372. }
  373. data->client = client;
  374. i2c_set_clientdata(client, data);
  375. mutex_init(&data->update_lock);
  376. /* Initialize the chip */
  377. err = adc128_init_client(data);
  378. if (err < 0)
  379. goto error;
  380. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  381. data, adc128_groups);
  382. if (IS_ERR(hwmon_dev)) {
  383. err = PTR_ERR(hwmon_dev);
  384. goto error;
  385. }
  386. return 0;
  387. error:
  388. if (data->regulator)
  389. regulator_disable(data->regulator);
  390. return err;
  391. }
  392. static int adc128_remove(struct i2c_client *client)
  393. {
  394. struct adc128_data *data = i2c_get_clientdata(client);
  395. if (data->regulator)
  396. regulator_disable(data->regulator);
  397. return 0;
  398. }
  399. static const struct i2c_device_id adc128_id[] = {
  400. { "adc128d818", 0 },
  401. { }
  402. };
  403. MODULE_DEVICE_TABLE(i2c, adc128_id);
  404. static struct i2c_driver adc128_driver = {
  405. .class = I2C_CLASS_HWMON,
  406. .driver = {
  407. .name = "adc128d818",
  408. },
  409. .probe = adc128_probe,
  410. .remove = adc128_remove,
  411. .id_table = adc128_id,
  412. .detect = adc128_detect,
  413. .address_list = normal_i2c,
  414. };
  415. module_i2c_driver(adc128_driver);
  416. MODULE_AUTHOR("Guenter Roeck");
  417. MODULE_DESCRIPTION("Driver for ADC128D818");
  418. MODULE_LICENSE("GPL");