rn5t618-adc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ADC driver for the RICOH RN5T618 power management chip family
  4. *
  5. * Copyright (C) 2019 Andreas Kemnade
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/mfd/rn5t618.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/completion.h>
  16. #include <linux/regmap.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/slab.h>
  19. #define RN5T618_ADC_CONVERSION_TIMEOUT (msecs_to_jiffies(500))
  20. #define RN5T618_REFERENCE_VOLT 2500
  21. /* mask for selecting channels for single conversion */
  22. #define RN5T618_ADCCNT3_CHANNEL_MASK 0x7
  23. /* average 4-time conversion mode */
  24. #define RN5T618_ADCCNT3_AVG BIT(3)
  25. /* set for starting a single conversion, gets cleared by hw when done */
  26. #define RN5T618_ADCCNT3_GODONE BIT(4)
  27. /* automatic conversion, period is in ADCCNT2, selected channels are
  28. * in ADCCNT1
  29. */
  30. #define RN5T618_ADCCNT3_AUTO BIT(5)
  31. #define RN5T618_ADCEND_IRQ BIT(0)
  32. struct rn5t618_adc_data {
  33. struct device *dev;
  34. struct rn5t618 *rn5t618;
  35. struct completion conv_completion;
  36. int irq;
  37. };
  38. struct rn5t618_channel_ratios {
  39. u16 numerator;
  40. u16 denominator;
  41. };
  42. enum rn5t618_channels {
  43. LIMMON = 0,
  44. VBAT,
  45. VADP,
  46. VUSB,
  47. VSYS,
  48. VTHM,
  49. AIN1,
  50. AIN0
  51. };
  52. static const struct rn5t618_channel_ratios rn5t618_ratios[8] = {
  53. [LIMMON] = {50, 32}, /* measured across 20mOhm, amplified by 32 */
  54. [VBAT] = {2, 1},
  55. [VADP] = {3, 1},
  56. [VUSB] = {3, 1},
  57. [VSYS] = {3, 1},
  58. [VTHM] = {1, 1},
  59. [AIN1] = {1, 1},
  60. [AIN0] = {1, 1},
  61. };
  62. static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
  63. {
  64. u8 data[2];
  65. int ret;
  66. ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
  67. if (ret < 0)
  68. return ret;
  69. *val = (data[0] << 4) | (data[1] & 0xF);
  70. return 0;
  71. }
  72. static irqreturn_t rn5t618_adc_irq(int irq, void *data)
  73. {
  74. struct rn5t618_adc_data *adc = data;
  75. unsigned int r = 0;
  76. int ret;
  77. /* clear low & high threshold irqs */
  78. regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
  79. regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
  80. ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
  81. if (ret < 0)
  82. dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
  83. regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
  84. if (r & RN5T618_ADCEND_IRQ)
  85. complete(&adc->conv_completion);
  86. return IRQ_HANDLED;
  87. }
  88. static int rn5t618_adc_read(struct iio_dev *iio_dev,
  89. const struct iio_chan_spec *chan,
  90. int *val, int *val2, long mask)
  91. {
  92. struct rn5t618_adc_data *adc = iio_priv(iio_dev);
  93. u16 raw;
  94. int ret;
  95. if (mask == IIO_CHAN_INFO_SCALE) {
  96. *val = RN5T618_REFERENCE_VOLT *
  97. rn5t618_ratios[chan->channel].numerator;
  98. *val2 = rn5t618_ratios[chan->channel].denominator * 4095;
  99. return IIO_VAL_FRACTIONAL;
  100. }
  101. /* select channel */
  102. ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
  103. RN5T618_ADCCNT3_CHANNEL_MASK,
  104. chan->channel);
  105. if (ret < 0)
  106. return ret;
  107. ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3,
  108. RN5T618_ADCEND_IRQ);
  109. if (ret < 0)
  110. return ret;
  111. ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
  112. RN5T618_ADCCNT3_AVG,
  113. mask == IIO_CHAN_INFO_AVERAGE_RAW ?
  114. RN5T618_ADCCNT3_AVG : 0);
  115. if (ret < 0)
  116. return ret;
  117. init_completion(&adc->conv_completion);
  118. /* single conversion */
  119. ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
  120. RN5T618_ADCCNT3_GODONE,
  121. RN5T618_ADCCNT3_GODONE);
  122. if (ret < 0)
  123. return ret;
  124. ret = wait_for_completion_timeout(&adc->conv_completion,
  125. RN5T618_ADC_CONVERSION_TIMEOUT);
  126. if (ret == 0) {
  127. dev_warn(adc->dev, "timeout waiting for adc result\n");
  128. return -ETIMEDOUT;
  129. }
  130. ret = rn5t618_read_adc_reg(adc->rn5t618,
  131. RN5T618_ILIMDATAH + 2 * chan->channel,
  132. &raw);
  133. if (ret < 0)
  134. return ret;
  135. *val = raw;
  136. return IIO_VAL_INT;
  137. }
  138. static const struct iio_info rn5t618_adc_iio_info = {
  139. .read_raw = &rn5t618_adc_read,
  140. };
  141. #define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
  142. .type = _type, \
  143. .channel = _channel, \
  144. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  145. BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
  146. BIT(IIO_CHAN_INFO_SCALE), \
  147. .datasheet_name = _name, \
  148. .indexed = 1. \
  149. }
  150. static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
  151. RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
  152. RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
  153. RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
  154. RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
  155. RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
  156. RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
  157. RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
  158. RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
  159. };
  160. static int rn5t618_adc_probe(struct platform_device *pdev)
  161. {
  162. int ret;
  163. struct iio_dev *iio_dev;
  164. struct rn5t618_adc_data *adc;
  165. struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
  166. iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  167. if (!iio_dev) {
  168. dev_err(&pdev->dev, "failed allocating iio device\n");
  169. return -ENOMEM;
  170. }
  171. adc = iio_priv(iio_dev);
  172. adc->dev = &pdev->dev;
  173. adc->rn5t618 = rn5t618;
  174. if (rn5t618->irq_data)
  175. adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
  176. RN5T618_IRQ_ADC);
  177. if (adc->irq <= 0) {
  178. dev_err(&pdev->dev, "get virq failed\n");
  179. return -EINVAL;
  180. }
  181. init_completion(&adc->conv_completion);
  182. iio_dev->name = dev_name(&pdev->dev);
  183. iio_dev->info = &rn5t618_adc_iio_info;
  184. iio_dev->modes = INDIO_DIRECT_MODE;
  185. iio_dev->channels = rn5t618_adc_iio_channels;
  186. iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
  187. /* stop any auto-conversion */
  188. ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
  189. if (ret < 0)
  190. return ret;
  191. platform_set_drvdata(pdev, iio_dev);
  192. ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
  193. rn5t618_adc_irq,
  194. IRQF_ONESHOT, dev_name(adc->dev),
  195. adc);
  196. if (ret < 0) {
  197. dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
  198. return ret;
  199. }
  200. return devm_iio_device_register(adc->dev, iio_dev);
  201. }
  202. static struct platform_driver rn5t618_adc_driver = {
  203. .driver = {
  204. .name = "rn5t618-adc",
  205. },
  206. .probe = rn5t618_adc_probe,
  207. };
  208. module_platform_driver(rn5t618_adc_driver);
  209. MODULE_ALIAS("platform:rn5t618-adc");
  210. MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
  211. MODULE_LICENSE("GPL");