rn5t618-adc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
  118. RN5T618_ADCCNT3_GODONE,
  119. RN5T618_ADCCNT3_GODONE);
  120. if (ret < 0)
  121. return ret;
  122. ret = wait_for_completion_timeout(&adc->conv_completion,
  123. RN5T618_ADC_CONVERSION_TIMEOUT);
  124. if (ret == 0) {
  125. dev_warn(adc->dev, "timeout waiting for adc result\n");
  126. return -ETIMEDOUT;
  127. }
  128. ret = rn5t618_read_adc_reg(adc->rn5t618,
  129. RN5T618_ILIMDATAH + 2 * chan->channel,
  130. &raw);
  131. if (ret < 0)
  132. return ret;
  133. *val = raw;
  134. return IIO_VAL_INT;
  135. }
  136. static const struct iio_info rn5t618_adc_iio_info = {
  137. .read_raw = &rn5t618_adc_read,
  138. };
  139. #define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
  140. .type = _type, \
  141. .channel = _channel, \
  142. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  143. BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
  144. BIT(IIO_CHAN_INFO_SCALE), \
  145. .datasheet_name = _name, \
  146. .indexed = 1. \
  147. }
  148. static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
  149. RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
  150. RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
  151. RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
  152. RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
  153. RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
  154. RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
  155. RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
  156. RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
  157. };
  158. static struct iio_map rn5t618_maps[] = {
  159. IIO_MAP("VADP", "rn5t618-power", "vadp"),
  160. IIO_MAP("VUSB", "rn5t618-power", "vusb"),
  161. { /* sentinel */ }
  162. };
  163. static int rn5t618_adc_probe(struct platform_device *pdev)
  164. {
  165. int ret;
  166. struct iio_dev *iio_dev;
  167. struct rn5t618_adc_data *adc;
  168. struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
  169. iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
  170. if (!iio_dev) {
  171. dev_err(&pdev->dev, "failed allocating iio device\n");
  172. return -ENOMEM;
  173. }
  174. adc = iio_priv(iio_dev);
  175. adc->dev = &pdev->dev;
  176. adc->rn5t618 = rn5t618;
  177. if (rn5t618->irq_data)
  178. adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
  179. RN5T618_IRQ_ADC);
  180. if (adc->irq <= 0) {
  181. dev_err(&pdev->dev, "get virq failed\n");
  182. return -EINVAL;
  183. }
  184. init_completion(&adc->conv_completion);
  185. iio_dev->name = dev_name(&pdev->dev);
  186. iio_dev->info = &rn5t618_adc_iio_info;
  187. iio_dev->modes = INDIO_DIRECT_MODE;
  188. iio_dev->channels = rn5t618_adc_iio_channels;
  189. iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
  190. /* stop any auto-conversion */
  191. ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
  192. if (ret < 0)
  193. return ret;
  194. platform_set_drvdata(pdev, iio_dev);
  195. ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
  196. rn5t618_adc_irq,
  197. IRQF_ONESHOT, dev_name(adc->dev),
  198. adc);
  199. if (ret < 0) {
  200. dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
  201. return ret;
  202. }
  203. ret = devm_iio_map_array_register(adc->dev, iio_dev, rn5t618_maps);
  204. if (ret < 0)
  205. return ret;
  206. return devm_iio_device_register(adc->dev, iio_dev);
  207. }
  208. static struct platform_driver rn5t618_adc_driver = {
  209. .driver = {
  210. .name = "rn5t618-adc",
  211. },
  212. .probe = rn5t618_adc_probe,
  213. };
  214. module_platform_driver(rn5t618_adc_driver);
  215. MODULE_ALIAS("platform:rn5t618-adc");
  216. MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
  217. MODULE_LICENSE("GPL");