ad7291.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
  3. *
  4. * Copyright 2010-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/slab.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/events.h>
  21. #include <linux/platform_data/ad7291.h>
  22. /*
  23. * Simplified handling
  24. *
  25. * If no events enabled - single polled channel read
  26. * If event enabled direct reads disable unless channel
  27. * is in the read mask.
  28. *
  29. * The noise-delayed bit as per datasheet suggestion is always enabled.
  30. */
  31. /*
  32. * AD7291 registers definition
  33. */
  34. #define AD7291_COMMAND 0x00
  35. #define AD7291_VOLTAGE 0x01
  36. #define AD7291_T_SENSE 0x02
  37. #define AD7291_T_AVERAGE 0x03
  38. #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
  39. #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
  40. #define AD7291_HYST(x) ((x) * 3 + 0x6)
  41. #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
  42. #define AD7291_T_ALERT_STATUS 0x20
  43. #define AD7291_BITS 12
  44. #define AD7291_VOLTAGE_LIMIT_COUNT 8
  45. /*
  46. * AD7291 command
  47. */
  48. #define AD7291_AUTOCYCLE BIT(0)
  49. #define AD7291_RESET BIT(1)
  50. #define AD7291_ALERT_CLEAR BIT(2)
  51. #define AD7291_ALERT_POLARITY BIT(3)
  52. #define AD7291_EXT_REF BIT(4)
  53. #define AD7291_NOISE_DELAY BIT(5)
  54. #define AD7291_T_SENSE_MASK BIT(7)
  55. #define AD7291_VOLTAGE_MASK GENMASK(15, 8)
  56. #define AD7291_VOLTAGE_OFFSET 8
  57. /*
  58. * AD7291 value masks
  59. */
  60. #define AD7291_VALUE_MASK GENMASK(11, 0)
  61. /*
  62. * AD7291 alert register bits
  63. */
  64. #define AD7291_T_LOW BIT(0)
  65. #define AD7291_T_HIGH BIT(1)
  66. #define AD7291_T_AVG_LOW BIT(2)
  67. #define AD7291_T_AVG_HIGH BIT(3)
  68. #define AD7291_V_LOW(x) BIT((x) * 2)
  69. #define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
  70. struct ad7291_chip_info {
  71. struct i2c_client *client;
  72. struct regulator *reg;
  73. u16 command;
  74. u16 c_mask; /* Active voltage channels for events */
  75. struct mutex state_lock;
  76. };
  77. static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
  78. {
  79. struct i2c_client *client = chip->client;
  80. int ret = 0;
  81. ret = i2c_smbus_read_word_swapped(client, reg);
  82. if (ret < 0) {
  83. dev_err(&client->dev, "I2C read error\n");
  84. return ret;
  85. }
  86. *data = ret;
  87. return 0;
  88. }
  89. static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
  90. {
  91. return i2c_smbus_write_word_swapped(chip->client, reg, data);
  92. }
  93. static irqreturn_t ad7291_event_handler(int irq, void *private)
  94. {
  95. struct iio_dev *indio_dev = private;
  96. struct ad7291_chip_info *chip = iio_priv(private);
  97. u16 t_status, v_status;
  98. u16 command;
  99. int i;
  100. s64 timestamp = iio_get_time_ns(indio_dev);
  101. if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
  102. return IRQ_HANDLED;
  103. if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
  104. return IRQ_HANDLED;
  105. if (!(t_status || v_status))
  106. return IRQ_HANDLED;
  107. command = chip->command | AD7291_ALERT_CLEAR;
  108. ad7291_i2c_write(chip, AD7291_COMMAND, command);
  109. command = chip->command & ~AD7291_ALERT_CLEAR;
  110. ad7291_i2c_write(chip, AD7291_COMMAND, command);
  111. /* For now treat t_sense and t_sense_average the same */
  112. if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
  113. iio_push_event(indio_dev,
  114. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  115. 0,
  116. IIO_EV_TYPE_THRESH,
  117. IIO_EV_DIR_FALLING),
  118. timestamp);
  119. if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
  120. iio_push_event(indio_dev,
  121. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  122. 0,
  123. IIO_EV_TYPE_THRESH,
  124. IIO_EV_DIR_RISING),
  125. timestamp);
  126. for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
  127. if (v_status & AD7291_V_LOW(i))
  128. iio_push_event(indio_dev,
  129. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
  130. i,
  131. IIO_EV_TYPE_THRESH,
  132. IIO_EV_DIR_FALLING),
  133. timestamp);
  134. if (v_status & AD7291_V_HIGH(i))
  135. iio_push_event(indio_dev,
  136. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
  137. i,
  138. IIO_EV_TYPE_THRESH,
  139. IIO_EV_DIR_RISING),
  140. timestamp);
  141. }
  142. return IRQ_HANDLED;
  143. }
  144. static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
  145. enum iio_event_direction dir,
  146. enum iio_event_info info)
  147. {
  148. unsigned int offset;
  149. switch (chan->type) {
  150. case IIO_VOLTAGE:
  151. offset = chan->channel;
  152. break;
  153. case IIO_TEMP:
  154. offset = AD7291_VOLTAGE_OFFSET;
  155. break;
  156. default:
  157. return 0;
  158. }
  159. switch (info) {
  160. case IIO_EV_INFO_VALUE:
  161. if (dir == IIO_EV_DIR_FALLING)
  162. return AD7291_DATA_HIGH(offset);
  163. else
  164. return AD7291_DATA_LOW(offset);
  165. case IIO_EV_INFO_HYSTERESIS:
  166. return AD7291_HYST(offset);
  167. default:
  168. break;
  169. }
  170. return 0;
  171. }
  172. static int ad7291_read_event_value(struct iio_dev *indio_dev,
  173. const struct iio_chan_spec *chan,
  174. enum iio_event_type type,
  175. enum iio_event_direction dir,
  176. enum iio_event_info info,
  177. int *val, int *val2)
  178. {
  179. struct ad7291_chip_info *chip = iio_priv(indio_dev);
  180. int ret;
  181. u16 uval;
  182. ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
  183. &uval);
  184. if (ret < 0)
  185. return ret;
  186. if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
  187. *val = uval & AD7291_VALUE_MASK;
  188. else
  189. *val = sign_extend32(uval, 11);
  190. return IIO_VAL_INT;
  191. }
  192. static int ad7291_write_event_value(struct iio_dev *indio_dev,
  193. const struct iio_chan_spec *chan,
  194. enum iio_event_type type,
  195. enum iio_event_direction dir,
  196. enum iio_event_info info,
  197. int val, int val2)
  198. {
  199. struct ad7291_chip_info *chip = iio_priv(indio_dev);
  200. if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
  201. if (val > AD7291_VALUE_MASK || val < 0)
  202. return -EINVAL;
  203. } else {
  204. if (val > 2047 || val < -2048)
  205. return -EINVAL;
  206. }
  207. return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
  208. val);
  209. }
  210. static int ad7291_read_event_config(struct iio_dev *indio_dev,
  211. const struct iio_chan_spec *chan,
  212. enum iio_event_type type,
  213. enum iio_event_direction dir)
  214. {
  215. struct ad7291_chip_info *chip = iio_priv(indio_dev);
  216. /*
  217. * To be enabled the channel must simply be on. If any are enabled
  218. * we are in continuous sampling mode
  219. */
  220. switch (chan->type) {
  221. case IIO_VOLTAGE:
  222. return !!(chip->c_mask & BIT(15 - chan->channel));
  223. case IIO_TEMP:
  224. /* always on */
  225. return 1;
  226. default:
  227. return -EINVAL;
  228. }
  229. }
  230. static int ad7291_write_event_config(struct iio_dev *indio_dev,
  231. const struct iio_chan_spec *chan,
  232. enum iio_event_type type,
  233. enum iio_event_direction dir,
  234. int state)
  235. {
  236. int ret = 0;
  237. struct ad7291_chip_info *chip = iio_priv(indio_dev);
  238. unsigned int mask;
  239. u16 regval;
  240. mutex_lock(&chip->state_lock);
  241. regval = chip->command;
  242. /*
  243. * To be enabled the channel must simply be on. If any are enabled
  244. * use continuous sampling mode.
  245. * Possible to disable temp as well but that makes single read tricky.
  246. */
  247. mask = BIT(15 - chan->channel);
  248. switch (chan->type) {
  249. case IIO_VOLTAGE:
  250. if ((!state) && (chip->c_mask & mask))
  251. chip->c_mask &= ~mask;
  252. else if (state && (!(chip->c_mask & mask)))
  253. chip->c_mask |= mask;
  254. else
  255. break;
  256. regval &= ~AD7291_AUTOCYCLE;
  257. regval |= chip->c_mask;
  258. if (chip->c_mask) /* Enable autocycle? */
  259. regval |= AD7291_AUTOCYCLE;
  260. ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
  261. if (ret < 0)
  262. goto error_ret;
  263. chip->command = regval;
  264. break;
  265. default:
  266. ret = -EINVAL;
  267. }
  268. error_ret:
  269. mutex_unlock(&chip->state_lock);
  270. return ret;
  271. }
  272. static int ad7291_read_raw(struct iio_dev *indio_dev,
  273. struct iio_chan_spec const *chan,
  274. int *val,
  275. int *val2,
  276. long mask)
  277. {
  278. int ret;
  279. struct ad7291_chip_info *chip = iio_priv(indio_dev);
  280. u16 regval;
  281. switch (mask) {
  282. case IIO_CHAN_INFO_RAW:
  283. switch (chan->type) {
  284. case IIO_VOLTAGE:
  285. mutex_lock(&chip->state_lock);
  286. /* If in autocycle mode drop through */
  287. if (chip->command & AD7291_AUTOCYCLE) {
  288. mutex_unlock(&chip->state_lock);
  289. return -EBUSY;
  290. }
  291. /* Enable this channel alone */
  292. regval = chip->command & (~AD7291_VOLTAGE_MASK);
  293. regval |= BIT(15 - chan->channel);
  294. ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
  295. if (ret < 0) {
  296. mutex_unlock(&chip->state_lock);
  297. return ret;
  298. }
  299. /* Read voltage */
  300. ret = i2c_smbus_read_word_swapped(chip->client,
  301. AD7291_VOLTAGE);
  302. if (ret < 0) {
  303. mutex_unlock(&chip->state_lock);
  304. return ret;
  305. }
  306. *val = ret & AD7291_VALUE_MASK;
  307. mutex_unlock(&chip->state_lock);
  308. return IIO_VAL_INT;
  309. case IIO_TEMP:
  310. /* Assumes tsense bit of command register always set */
  311. ret = i2c_smbus_read_word_swapped(chip->client,
  312. AD7291_T_SENSE);
  313. if (ret < 0)
  314. return ret;
  315. *val = sign_extend32(ret, 11);
  316. return IIO_VAL_INT;
  317. default:
  318. return -EINVAL;
  319. }
  320. case IIO_CHAN_INFO_AVERAGE_RAW:
  321. ret = i2c_smbus_read_word_swapped(chip->client,
  322. AD7291_T_AVERAGE);
  323. if (ret < 0)
  324. return ret;
  325. *val = sign_extend32(ret, 11);
  326. return IIO_VAL_INT;
  327. case IIO_CHAN_INFO_SCALE:
  328. switch (chan->type) {
  329. case IIO_VOLTAGE:
  330. if (chip->reg) {
  331. int vref;
  332. vref = regulator_get_voltage(chip->reg);
  333. if (vref < 0)
  334. return vref;
  335. *val = vref / 1000;
  336. } else {
  337. *val = 2500;
  338. }
  339. *val2 = AD7291_BITS;
  340. return IIO_VAL_FRACTIONAL_LOG2;
  341. case IIO_TEMP:
  342. /*
  343. * One LSB of the ADC corresponds to 0.25 deg C.
  344. * The temperature reading is in 12-bit twos
  345. * complement format
  346. */
  347. *val = 250;
  348. return IIO_VAL_INT;
  349. default:
  350. return -EINVAL;
  351. }
  352. default:
  353. return -EINVAL;
  354. }
  355. }
  356. static const struct iio_event_spec ad7291_events[] = {
  357. {
  358. .type = IIO_EV_TYPE_THRESH,
  359. .dir = IIO_EV_DIR_RISING,
  360. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  361. BIT(IIO_EV_INFO_ENABLE),
  362. }, {
  363. .type = IIO_EV_TYPE_THRESH,
  364. .dir = IIO_EV_DIR_FALLING,
  365. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  366. BIT(IIO_EV_INFO_ENABLE),
  367. }, {
  368. .type = IIO_EV_TYPE_THRESH,
  369. .dir = IIO_EV_DIR_EITHER,
  370. .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
  371. },
  372. };
  373. #define AD7291_VOLTAGE_CHAN(_chan) \
  374. { \
  375. .type = IIO_VOLTAGE, \
  376. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  377. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  378. .indexed = 1, \
  379. .channel = _chan, \
  380. .event_spec = ad7291_events, \
  381. .num_event_specs = ARRAY_SIZE(ad7291_events), \
  382. }
  383. static const struct iio_chan_spec ad7291_channels[] = {
  384. AD7291_VOLTAGE_CHAN(0),
  385. AD7291_VOLTAGE_CHAN(1),
  386. AD7291_VOLTAGE_CHAN(2),
  387. AD7291_VOLTAGE_CHAN(3),
  388. AD7291_VOLTAGE_CHAN(4),
  389. AD7291_VOLTAGE_CHAN(5),
  390. AD7291_VOLTAGE_CHAN(6),
  391. AD7291_VOLTAGE_CHAN(7),
  392. {
  393. .type = IIO_TEMP,
  394. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  395. BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
  396. BIT(IIO_CHAN_INFO_SCALE),
  397. .indexed = 1,
  398. .channel = 0,
  399. .event_spec = ad7291_events,
  400. .num_event_specs = ARRAY_SIZE(ad7291_events),
  401. }
  402. };
  403. static const struct iio_info ad7291_info = {
  404. .read_raw = &ad7291_read_raw,
  405. .read_event_config = &ad7291_read_event_config,
  406. .write_event_config = &ad7291_write_event_config,
  407. .read_event_value = &ad7291_read_event_value,
  408. .write_event_value = &ad7291_write_event_value,
  409. .driver_module = THIS_MODULE,
  410. };
  411. static int ad7291_probe(struct i2c_client *client,
  412. const struct i2c_device_id *id)
  413. {
  414. struct ad7291_platform_data *pdata = client->dev.platform_data;
  415. struct ad7291_chip_info *chip;
  416. struct iio_dev *indio_dev;
  417. int ret;
  418. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  419. if (!indio_dev)
  420. return -ENOMEM;
  421. chip = iio_priv(indio_dev);
  422. if (pdata && pdata->use_external_ref) {
  423. chip->reg = devm_regulator_get(&client->dev, "vref");
  424. if (IS_ERR(chip->reg))
  425. return PTR_ERR(chip->reg);
  426. ret = regulator_enable(chip->reg);
  427. if (ret)
  428. return ret;
  429. }
  430. mutex_init(&chip->state_lock);
  431. /* this is only used for device removal purposes */
  432. i2c_set_clientdata(client, indio_dev);
  433. chip->client = client;
  434. chip->command = AD7291_NOISE_DELAY |
  435. AD7291_T_SENSE_MASK | /* Tsense always enabled */
  436. AD7291_ALERT_POLARITY; /* set irq polarity low level */
  437. if (pdata && pdata->use_external_ref)
  438. chip->command |= AD7291_EXT_REF;
  439. indio_dev->name = id->name;
  440. indio_dev->channels = ad7291_channels;
  441. indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
  442. indio_dev->dev.parent = &client->dev;
  443. indio_dev->dev.of_node = client->dev.of_node;
  444. indio_dev->info = &ad7291_info;
  445. indio_dev->modes = INDIO_DIRECT_MODE;
  446. ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
  447. if (ret) {
  448. ret = -EIO;
  449. goto error_disable_reg;
  450. }
  451. ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
  452. if (ret) {
  453. ret = -EIO;
  454. goto error_disable_reg;
  455. }
  456. if (client->irq > 0) {
  457. ret = request_threaded_irq(client->irq,
  458. NULL,
  459. &ad7291_event_handler,
  460. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  461. id->name,
  462. indio_dev);
  463. if (ret)
  464. goto error_disable_reg;
  465. }
  466. ret = iio_device_register(indio_dev);
  467. if (ret)
  468. goto error_unreg_irq;
  469. return 0;
  470. error_unreg_irq:
  471. if (client->irq)
  472. free_irq(client->irq, indio_dev);
  473. error_disable_reg:
  474. if (chip->reg)
  475. regulator_disable(chip->reg);
  476. return ret;
  477. }
  478. static int ad7291_remove(struct i2c_client *client)
  479. {
  480. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  481. struct ad7291_chip_info *chip = iio_priv(indio_dev);
  482. iio_device_unregister(indio_dev);
  483. if (client->irq)
  484. free_irq(client->irq, indio_dev);
  485. if (chip->reg)
  486. regulator_disable(chip->reg);
  487. return 0;
  488. }
  489. static const struct i2c_device_id ad7291_id[] = {
  490. { "ad7291", 0 },
  491. {}
  492. };
  493. MODULE_DEVICE_TABLE(i2c, ad7291_id);
  494. static struct i2c_driver ad7291_driver = {
  495. .driver = {
  496. .name = KBUILD_MODNAME,
  497. },
  498. .probe = ad7291_probe,
  499. .remove = ad7291_remove,
  500. .id_table = ad7291_id,
  501. };
  502. module_i2c_driver(ad7291_driver);
  503. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  504. MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
  505. MODULE_LICENSE("GPL v2");