rn5t618-adc.c 6.4 KB

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