ad5592r-base.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * AD5592R Digital <-> Analog converters driver
  3. *
  4. * Copyright 2014-2016 Analog Devices Inc.
  5. * Author: Paul Cercueil <paul.cercueil@analog.com>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/delay.h>
  11. #include <linux/iio/iio.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/gpio.h>
  19. #include <linux/property.h>
  20. #include <dt-bindings/iio/adi,ad5592r.h>
  21. #include "ad5592r-base.h"
  22. static int ad5592r_gpio_get(struct gpio_chip *chip, unsigned offset)
  23. {
  24. struct ad5592r_state *st = gpiochip_get_data(chip);
  25. int ret = 0;
  26. u8 val;
  27. mutex_lock(&st->gpio_lock);
  28. if (st->gpio_out & BIT(offset))
  29. val = st->gpio_val;
  30. else
  31. ret = st->ops->gpio_read(st, &val);
  32. mutex_unlock(&st->gpio_lock);
  33. if (ret < 0)
  34. return ret;
  35. return !!(val & BIT(offset));
  36. }
  37. static void ad5592r_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  38. {
  39. struct ad5592r_state *st = gpiochip_get_data(chip);
  40. mutex_lock(&st->gpio_lock);
  41. if (value)
  42. st->gpio_val |= BIT(offset);
  43. else
  44. st->gpio_val &= ~BIT(offset);
  45. st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val);
  46. mutex_unlock(&st->gpio_lock);
  47. }
  48. static int ad5592r_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  49. {
  50. struct ad5592r_state *st = gpiochip_get_data(chip);
  51. int ret;
  52. mutex_lock(&st->gpio_lock);
  53. st->gpio_out &= ~BIT(offset);
  54. st->gpio_in |= BIT(offset);
  55. ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out);
  56. if (ret < 0)
  57. goto err_unlock;
  58. ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in);
  59. err_unlock:
  60. mutex_unlock(&st->gpio_lock);
  61. return ret;
  62. }
  63. static int ad5592r_gpio_direction_output(struct gpio_chip *chip,
  64. unsigned offset, int value)
  65. {
  66. struct ad5592r_state *st = gpiochip_get_data(chip);
  67. int ret;
  68. mutex_lock(&st->gpio_lock);
  69. if (value)
  70. st->gpio_val |= BIT(offset);
  71. else
  72. st->gpio_val &= ~BIT(offset);
  73. st->gpio_in &= ~BIT(offset);
  74. st->gpio_out |= BIT(offset);
  75. ret = st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val);
  76. if (ret < 0)
  77. goto err_unlock;
  78. ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out);
  79. if (ret < 0)
  80. goto err_unlock;
  81. ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in);
  82. err_unlock:
  83. mutex_unlock(&st->gpio_lock);
  84. return ret;
  85. }
  86. static int ad5592r_gpio_request(struct gpio_chip *chip, unsigned offset)
  87. {
  88. struct ad5592r_state *st = gpiochip_get_data(chip);
  89. if (!(st->gpio_map & BIT(offset))) {
  90. dev_err(st->dev, "GPIO %d is reserved by alternate function\n",
  91. offset);
  92. return -ENODEV;
  93. }
  94. return 0;
  95. }
  96. static int ad5592r_gpio_init(struct ad5592r_state *st)
  97. {
  98. if (!st->gpio_map)
  99. return 0;
  100. st->gpiochip.label = dev_name(st->dev);
  101. st->gpiochip.base = -1;
  102. st->gpiochip.ngpio = 8;
  103. st->gpiochip.parent = st->dev;
  104. st->gpiochip.can_sleep = true;
  105. st->gpiochip.direction_input = ad5592r_gpio_direction_input;
  106. st->gpiochip.direction_output = ad5592r_gpio_direction_output;
  107. st->gpiochip.get = ad5592r_gpio_get;
  108. st->gpiochip.set = ad5592r_gpio_set;
  109. st->gpiochip.request = ad5592r_gpio_request;
  110. st->gpiochip.owner = THIS_MODULE;
  111. mutex_init(&st->gpio_lock);
  112. return gpiochip_add_data(&st->gpiochip, st);
  113. }
  114. static void ad5592r_gpio_cleanup(struct ad5592r_state *st)
  115. {
  116. if (st->gpio_map)
  117. gpiochip_remove(&st->gpiochip);
  118. }
  119. static int ad5592r_reset(struct ad5592r_state *st)
  120. {
  121. struct gpio_desc *gpio;
  122. struct iio_dev *iio_dev = iio_priv_to_dev(st);
  123. gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW);
  124. if (IS_ERR(gpio))
  125. return PTR_ERR(gpio);
  126. if (gpio) {
  127. udelay(1);
  128. gpiod_set_value(gpio, 1);
  129. } else {
  130. mutex_lock(&iio_dev->mlock);
  131. /* Writing this magic value resets the device */
  132. st->ops->reg_write(st, AD5592R_REG_RESET, 0xdac);
  133. mutex_unlock(&iio_dev->mlock);
  134. }
  135. udelay(250);
  136. return 0;
  137. }
  138. static int ad5592r_get_vref(struct ad5592r_state *st)
  139. {
  140. int ret;
  141. if (st->reg) {
  142. ret = regulator_get_voltage(st->reg);
  143. if (ret < 0)
  144. return ret;
  145. return ret / 1000;
  146. } else {
  147. return 2500;
  148. }
  149. }
  150. static int ad5592r_set_channel_modes(struct ad5592r_state *st)
  151. {
  152. const struct ad5592r_rw_ops *ops = st->ops;
  153. int ret;
  154. unsigned i;
  155. struct iio_dev *iio_dev = iio_priv_to_dev(st);
  156. u8 pulldown = 0, tristate = 0, dac = 0, adc = 0;
  157. u16 read_back;
  158. for (i = 0; i < st->num_channels; i++) {
  159. switch (st->channel_modes[i]) {
  160. case CH_MODE_DAC:
  161. dac |= BIT(i);
  162. break;
  163. case CH_MODE_ADC:
  164. adc |= BIT(i);
  165. break;
  166. case CH_MODE_DAC_AND_ADC:
  167. dac |= BIT(i);
  168. adc |= BIT(i);
  169. break;
  170. case CH_MODE_GPIO:
  171. st->gpio_map |= BIT(i);
  172. st->gpio_in |= BIT(i); /* Default to input */
  173. break;
  174. case CH_MODE_UNUSED:
  175. /* fall-through */
  176. default:
  177. switch (st->channel_offstate[i]) {
  178. case CH_OFFSTATE_OUT_TRISTATE:
  179. tristate |= BIT(i);
  180. break;
  181. case CH_OFFSTATE_OUT_LOW:
  182. st->gpio_out |= BIT(i);
  183. break;
  184. case CH_OFFSTATE_OUT_HIGH:
  185. st->gpio_out |= BIT(i);
  186. st->gpio_val |= BIT(i);
  187. break;
  188. case CH_OFFSTATE_PULLDOWN:
  189. /* fall-through */
  190. default:
  191. pulldown |= BIT(i);
  192. break;
  193. }
  194. }
  195. }
  196. mutex_lock(&iio_dev->mlock);
  197. /* Pull down unused pins to GND */
  198. ret = ops->reg_write(st, AD5592R_REG_PULLDOWN, pulldown);
  199. if (ret)
  200. goto err_unlock;
  201. ret = ops->reg_write(st, AD5592R_REG_TRISTATE, tristate);
  202. if (ret)
  203. goto err_unlock;
  204. /* Configure pins that we use */
  205. ret = ops->reg_write(st, AD5592R_REG_DAC_EN, dac);
  206. if (ret)
  207. goto err_unlock;
  208. ret = ops->reg_write(st, AD5592R_REG_ADC_EN, adc);
  209. if (ret)
  210. goto err_unlock;
  211. ret = ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val);
  212. if (ret)
  213. goto err_unlock;
  214. ret = ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out);
  215. if (ret)
  216. goto err_unlock;
  217. ret = ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in);
  218. if (ret)
  219. goto err_unlock;
  220. /* Verify that we can read back at least one register */
  221. ret = ops->reg_read(st, AD5592R_REG_ADC_EN, &read_back);
  222. if (!ret && (read_back & 0xff) != adc)
  223. ret = -EIO;
  224. err_unlock:
  225. mutex_unlock(&iio_dev->mlock);
  226. return ret;
  227. }
  228. static int ad5592r_reset_channel_modes(struct ad5592r_state *st)
  229. {
  230. int i;
  231. for (i = 0; i < ARRAY_SIZE(st->channel_modes); i++)
  232. st->channel_modes[i] = CH_MODE_UNUSED;
  233. return ad5592r_set_channel_modes(st);
  234. }
  235. static int ad5592r_write_raw(struct iio_dev *iio_dev,
  236. struct iio_chan_spec const *chan, int val, int val2, long mask)
  237. {
  238. struct ad5592r_state *st = iio_priv(iio_dev);
  239. int ret;
  240. switch (mask) {
  241. case IIO_CHAN_INFO_RAW:
  242. if (val >= (1 << chan->scan_type.realbits) || val < 0)
  243. return -EINVAL;
  244. if (!chan->output)
  245. return -EINVAL;
  246. mutex_lock(&iio_dev->mlock);
  247. ret = st->ops->write_dac(st, chan->channel, val);
  248. if (!ret)
  249. st->cached_dac[chan->channel] = val;
  250. mutex_unlock(&iio_dev->mlock);
  251. return ret;
  252. case IIO_CHAN_INFO_SCALE:
  253. if (chan->type == IIO_VOLTAGE) {
  254. bool gain;
  255. if (val == st->scale_avail[0][0] &&
  256. val2 == st->scale_avail[0][1])
  257. gain = false;
  258. else if (val == st->scale_avail[1][0] &&
  259. val2 == st->scale_avail[1][1])
  260. gain = true;
  261. else
  262. return -EINVAL;
  263. mutex_lock(&iio_dev->mlock);
  264. ret = st->ops->reg_read(st, AD5592R_REG_CTRL,
  265. &st->cached_gp_ctrl);
  266. if (ret < 0) {
  267. mutex_unlock(&iio_dev->mlock);
  268. return ret;
  269. }
  270. if (chan->output) {
  271. if (gain)
  272. st->cached_gp_ctrl |=
  273. AD5592R_REG_CTRL_DAC_RANGE;
  274. else
  275. st->cached_gp_ctrl &=
  276. ~AD5592R_REG_CTRL_DAC_RANGE;
  277. } else {
  278. if (gain)
  279. st->cached_gp_ctrl |=
  280. AD5592R_REG_CTRL_ADC_RANGE;
  281. else
  282. st->cached_gp_ctrl &=
  283. ~AD5592R_REG_CTRL_ADC_RANGE;
  284. }
  285. ret = st->ops->reg_write(st, AD5592R_REG_CTRL,
  286. st->cached_gp_ctrl);
  287. mutex_unlock(&iio_dev->mlock);
  288. return ret;
  289. }
  290. break;
  291. default:
  292. return -EINVAL;
  293. }
  294. return 0;
  295. }
  296. static int ad5592r_read_raw(struct iio_dev *iio_dev,
  297. struct iio_chan_spec const *chan,
  298. int *val, int *val2, long m)
  299. {
  300. struct ad5592r_state *st = iio_priv(iio_dev);
  301. u16 read_val;
  302. int ret;
  303. switch (m) {
  304. case IIO_CHAN_INFO_RAW:
  305. mutex_lock(&iio_dev->mlock);
  306. if (!chan->output) {
  307. ret = st->ops->read_adc(st, chan->channel, &read_val);
  308. if (ret)
  309. goto unlock;
  310. if ((read_val >> 12 & 0x7) != (chan->channel & 0x7)) {
  311. dev_err(st->dev, "Error while reading channel %u\n",
  312. chan->channel);
  313. ret = -EIO;
  314. goto unlock;
  315. }
  316. read_val &= GENMASK(11, 0);
  317. } else {
  318. read_val = st->cached_dac[chan->channel];
  319. }
  320. dev_dbg(st->dev, "Channel %u read: 0x%04hX\n",
  321. chan->channel, read_val);
  322. *val = (int) read_val;
  323. ret = IIO_VAL_INT;
  324. break;
  325. case IIO_CHAN_INFO_SCALE:
  326. *val = ad5592r_get_vref(st);
  327. if (chan->type == IIO_TEMP) {
  328. s64 tmp = *val * (3767897513LL / 25LL);
  329. *val = div_s64_rem(tmp, 1000000000LL, val2);
  330. ret = IIO_VAL_INT_PLUS_MICRO;
  331. } else {
  332. int mult;
  333. mutex_lock(&iio_dev->mlock);
  334. if (chan->output)
  335. mult = !!(st->cached_gp_ctrl &
  336. AD5592R_REG_CTRL_DAC_RANGE);
  337. else
  338. mult = !!(st->cached_gp_ctrl &
  339. AD5592R_REG_CTRL_ADC_RANGE);
  340. *val *= ++mult;
  341. *val2 = chan->scan_type.realbits;
  342. ret = IIO_VAL_FRACTIONAL_LOG2;
  343. }
  344. break;
  345. case IIO_CHAN_INFO_OFFSET:
  346. ret = ad5592r_get_vref(st);
  347. mutex_lock(&iio_dev->mlock);
  348. if (st->cached_gp_ctrl & AD5592R_REG_CTRL_ADC_RANGE)
  349. *val = (-34365 * 25) / ret;
  350. else
  351. *val = (-75365 * 25) / ret;
  352. ret = IIO_VAL_INT;
  353. break;
  354. default:
  355. ret = -EINVAL;
  356. }
  357. unlock:
  358. mutex_unlock(&iio_dev->mlock);
  359. return ret;
  360. }
  361. static int ad5592r_write_raw_get_fmt(struct iio_dev *indio_dev,
  362. struct iio_chan_spec const *chan, long mask)
  363. {
  364. switch (mask) {
  365. case IIO_CHAN_INFO_SCALE:
  366. return IIO_VAL_INT_PLUS_NANO;
  367. default:
  368. return IIO_VAL_INT_PLUS_MICRO;
  369. }
  370. return -EINVAL;
  371. }
  372. static const struct iio_info ad5592r_info = {
  373. .read_raw = ad5592r_read_raw,
  374. .write_raw = ad5592r_write_raw,
  375. .write_raw_get_fmt = ad5592r_write_raw_get_fmt,
  376. .driver_module = THIS_MODULE,
  377. };
  378. static ssize_t ad5592r_show_scale_available(struct iio_dev *iio_dev,
  379. uintptr_t private,
  380. const struct iio_chan_spec *chan,
  381. char *buf)
  382. {
  383. struct ad5592r_state *st = iio_priv(iio_dev);
  384. return sprintf(buf, "%d.%09u %d.%09u\n",
  385. st->scale_avail[0][0], st->scale_avail[0][1],
  386. st->scale_avail[1][0], st->scale_avail[1][1]);
  387. }
  388. static struct iio_chan_spec_ext_info ad5592r_ext_info[] = {
  389. {
  390. .name = "scale_available",
  391. .read = ad5592r_show_scale_available,
  392. .shared = true,
  393. },
  394. {},
  395. };
  396. static void ad5592r_setup_channel(struct iio_dev *iio_dev,
  397. struct iio_chan_spec *chan, bool output, unsigned id)
  398. {
  399. chan->type = IIO_VOLTAGE;
  400. chan->indexed = 1;
  401. chan->output = output;
  402. chan->channel = id;
  403. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  404. chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
  405. chan->scan_type.sign = 'u';
  406. chan->scan_type.realbits = 12;
  407. chan->scan_type.storagebits = 16;
  408. chan->ext_info = ad5592r_ext_info;
  409. }
  410. static int ad5592r_alloc_channels(struct ad5592r_state *st)
  411. {
  412. unsigned i, curr_channel = 0,
  413. num_channels = st->num_channels;
  414. struct iio_dev *iio_dev = iio_priv_to_dev(st);
  415. struct iio_chan_spec *channels;
  416. struct fwnode_handle *child;
  417. u32 reg, tmp;
  418. int ret;
  419. device_for_each_child_node(st->dev, child) {
  420. ret = fwnode_property_read_u32(child, "reg", &reg);
  421. if (ret || reg >= ARRAY_SIZE(st->channel_modes))
  422. continue;
  423. ret = fwnode_property_read_u32(child, "adi,mode", &tmp);
  424. if (!ret)
  425. st->channel_modes[reg] = tmp;
  426. fwnode_property_read_u32(child, "adi,off-state", &tmp);
  427. if (!ret)
  428. st->channel_offstate[reg] = tmp;
  429. }
  430. channels = devm_kzalloc(st->dev,
  431. (1 + 2 * num_channels) * sizeof(*channels), GFP_KERNEL);
  432. if (!channels)
  433. return -ENOMEM;
  434. for (i = 0; i < num_channels; i++) {
  435. switch (st->channel_modes[i]) {
  436. case CH_MODE_DAC:
  437. ad5592r_setup_channel(iio_dev, &channels[curr_channel],
  438. true, i);
  439. curr_channel++;
  440. break;
  441. case CH_MODE_ADC:
  442. ad5592r_setup_channel(iio_dev, &channels[curr_channel],
  443. false, i);
  444. curr_channel++;
  445. break;
  446. case CH_MODE_DAC_AND_ADC:
  447. ad5592r_setup_channel(iio_dev, &channels[curr_channel],
  448. true, i);
  449. curr_channel++;
  450. ad5592r_setup_channel(iio_dev, &channels[curr_channel],
  451. false, i);
  452. curr_channel++;
  453. break;
  454. default:
  455. continue;
  456. }
  457. }
  458. channels[curr_channel].type = IIO_TEMP;
  459. channels[curr_channel].channel = 8;
  460. channels[curr_channel].info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  461. BIT(IIO_CHAN_INFO_SCALE) |
  462. BIT(IIO_CHAN_INFO_OFFSET);
  463. curr_channel++;
  464. iio_dev->num_channels = curr_channel;
  465. iio_dev->channels = channels;
  466. return 0;
  467. }
  468. static void ad5592r_init_scales(struct ad5592r_state *st, int vref_mV)
  469. {
  470. s64 tmp = (s64)vref_mV * 1000000000LL >> 12;
  471. st->scale_avail[0][0] =
  472. div_s64_rem(tmp, 1000000000LL, &st->scale_avail[0][1]);
  473. st->scale_avail[1][0] =
  474. div_s64_rem(tmp * 2, 1000000000LL, &st->scale_avail[1][1]);
  475. }
  476. int ad5592r_probe(struct device *dev, const char *name,
  477. const struct ad5592r_rw_ops *ops)
  478. {
  479. struct iio_dev *iio_dev;
  480. struct ad5592r_state *st;
  481. int ret;
  482. iio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  483. if (!iio_dev)
  484. return -ENOMEM;
  485. st = iio_priv(iio_dev);
  486. st->dev = dev;
  487. st->ops = ops;
  488. st->num_channels = 8;
  489. dev_set_drvdata(dev, iio_dev);
  490. st->reg = devm_regulator_get_optional(dev, "vref");
  491. if (IS_ERR(st->reg)) {
  492. if ((PTR_ERR(st->reg) != -ENODEV) && dev->of_node)
  493. return PTR_ERR(st->reg);
  494. st->reg = NULL;
  495. } else {
  496. ret = regulator_enable(st->reg);
  497. if (ret)
  498. return ret;
  499. }
  500. iio_dev->dev.parent = dev;
  501. iio_dev->name = name;
  502. iio_dev->info = &ad5592r_info;
  503. iio_dev->modes = INDIO_DIRECT_MODE;
  504. ad5592r_init_scales(st, ad5592r_get_vref(st));
  505. ret = ad5592r_reset(st);
  506. if (ret)
  507. goto error_disable_reg;
  508. ret = ops->reg_write(st, AD5592R_REG_PD,
  509. (st->reg == NULL) ? AD5592R_REG_PD_EN_REF : 0);
  510. if (ret)
  511. goto error_disable_reg;
  512. ret = ad5592r_alloc_channels(st);
  513. if (ret)
  514. goto error_disable_reg;
  515. ret = ad5592r_set_channel_modes(st);
  516. if (ret)
  517. goto error_reset_ch_modes;
  518. ret = iio_device_register(iio_dev);
  519. if (ret)
  520. goto error_reset_ch_modes;
  521. ret = ad5592r_gpio_init(st);
  522. if (ret)
  523. goto error_dev_unregister;
  524. return 0;
  525. error_dev_unregister:
  526. iio_device_unregister(iio_dev);
  527. error_reset_ch_modes:
  528. ad5592r_reset_channel_modes(st);
  529. error_disable_reg:
  530. if (st->reg)
  531. regulator_disable(st->reg);
  532. return ret;
  533. }
  534. EXPORT_SYMBOL_GPL(ad5592r_probe);
  535. int ad5592r_remove(struct device *dev)
  536. {
  537. struct iio_dev *iio_dev = dev_get_drvdata(dev);
  538. struct ad5592r_state *st = iio_priv(iio_dev);
  539. iio_device_unregister(iio_dev);
  540. ad5592r_reset_channel_modes(st);
  541. ad5592r_gpio_cleanup(st);
  542. if (st->reg)
  543. regulator_disable(st->reg);
  544. return 0;
  545. }
  546. EXPORT_SYMBOL_GPL(ad5592r_remove);
  547. MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>");
  548. MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters");
  549. MODULE_LICENSE("GPL v2");