adc128d818.c 14 KB

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