npcm_adc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2019 Nuvoton Technology corporation.
  3. #include <linux/clk.h>
  4. #include <linux/device.h>
  5. #include <linux/mfd/syscon.h>
  6. #include <linux/io.h>
  7. #include <linux/iio/iio.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/property.h>
  14. #include <linux/regmap.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/reset.h>
  19. struct npcm_adc_info {
  20. u32 data_mask;
  21. u32 internal_vref;
  22. u32 res_bits;
  23. };
  24. struct npcm_adc {
  25. bool int_status;
  26. u32 adc_sample_hz;
  27. struct device *dev;
  28. void __iomem *regs;
  29. struct clk *adc_clk;
  30. wait_queue_head_t wq;
  31. struct regulator *vref;
  32. struct reset_control *reset;
  33. /*
  34. * Lock to protect the device state during a potential concurrent
  35. * read access from userspace. Reading a raw value requires a sequence
  36. * of register writes, then a wait for a event and finally a register
  37. * read, during which userspace could issue another read request.
  38. * This lock protects a read access from ocurring before another one
  39. * has finished.
  40. */
  41. struct mutex lock;
  42. const struct npcm_adc_info *data;
  43. };
  44. /* ADC registers */
  45. #define NPCM_ADCCON 0x00
  46. #define NPCM_ADCDATA 0x04
  47. /* ADCCON Register Bits */
  48. #define NPCM_ADCCON_ADC_INT_EN BIT(21)
  49. #define NPCM_ADCCON_REFSEL BIT(19)
  50. #define NPCM_ADCCON_ADC_INT_ST BIT(18)
  51. #define NPCM_ADCCON_ADC_EN BIT(17)
  52. #define NPCM_ADCCON_ADC_RST BIT(16)
  53. #define NPCM_ADCCON_ADC_CONV BIT(13)
  54. #define NPCM_ADCCON_CH_MASK GENMASK(27, 24)
  55. #define NPCM_ADCCON_CH(x) ((x) << 24)
  56. #define NPCM_ADCCON_DIV_SHIFT 1
  57. #define NPCM_ADCCON_DIV_MASK GENMASK(8, 1)
  58. #define NPCM_ADC_ENABLE (NPCM_ADCCON_ADC_EN | NPCM_ADCCON_ADC_INT_EN)
  59. /* ADC General Definition */
  60. static const struct npcm_adc_info npxm7xx_adc_info = {
  61. .data_mask = GENMASK(9, 0),
  62. .internal_vref = 2048,
  63. .res_bits = 10,
  64. };
  65. static const struct npcm_adc_info npxm8xx_adc_info = {
  66. .data_mask = GENMASK(11, 0),
  67. .internal_vref = 1229,
  68. .res_bits = 12,
  69. };
  70. #define NPCM_ADC_CHAN(ch) { \
  71. .type = IIO_VOLTAGE, \
  72. .indexed = 1, \
  73. .channel = ch, \
  74. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  75. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  76. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  77. }
  78. static const struct iio_chan_spec npcm_adc_iio_channels[] = {
  79. NPCM_ADC_CHAN(0),
  80. NPCM_ADC_CHAN(1),
  81. NPCM_ADC_CHAN(2),
  82. NPCM_ADC_CHAN(3),
  83. NPCM_ADC_CHAN(4),
  84. NPCM_ADC_CHAN(5),
  85. NPCM_ADC_CHAN(6),
  86. NPCM_ADC_CHAN(7),
  87. };
  88. static irqreturn_t npcm_adc_isr(int irq, void *data)
  89. {
  90. u32 regtemp;
  91. struct iio_dev *indio_dev = data;
  92. struct npcm_adc *info = iio_priv(indio_dev);
  93. regtemp = ioread32(info->regs + NPCM_ADCCON);
  94. if (regtemp & NPCM_ADCCON_ADC_INT_ST) {
  95. iowrite32(regtemp, info->regs + NPCM_ADCCON);
  96. wake_up_interruptible(&info->wq);
  97. info->int_status = true;
  98. }
  99. return IRQ_HANDLED;
  100. }
  101. static int npcm_adc_read(struct npcm_adc *info, int *val, u8 channel)
  102. {
  103. int ret;
  104. u32 regtemp;
  105. /* Select ADC channel */
  106. regtemp = ioread32(info->regs + NPCM_ADCCON);
  107. regtemp &= ~NPCM_ADCCON_CH_MASK;
  108. info->int_status = false;
  109. iowrite32(regtemp | NPCM_ADCCON_CH(channel) |
  110. NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON);
  111. ret = wait_event_interruptible_timeout(info->wq, info->int_status,
  112. msecs_to_jiffies(10));
  113. if (ret == 0) {
  114. regtemp = ioread32(info->regs + NPCM_ADCCON);
  115. if (regtemp & NPCM_ADCCON_ADC_CONV) {
  116. /* if conversion failed - reset ADC module */
  117. reset_control_assert(info->reset);
  118. msleep(100);
  119. reset_control_deassert(info->reset);
  120. msleep(100);
  121. /* Enable ADC and start conversion module */
  122. iowrite32(NPCM_ADC_ENABLE | NPCM_ADCCON_ADC_CONV,
  123. info->regs + NPCM_ADCCON);
  124. dev_err(info->dev, "RESET ADC Complete\n");
  125. }
  126. return -ETIMEDOUT;
  127. }
  128. if (ret < 0)
  129. return ret;
  130. *val = ioread32(info->regs + NPCM_ADCDATA);
  131. *val &= info->data->data_mask;
  132. return 0;
  133. }
  134. static int npcm_adc_read_raw(struct iio_dev *indio_dev,
  135. struct iio_chan_spec const *chan, int *val,
  136. int *val2, long mask)
  137. {
  138. int ret;
  139. int vref_uv;
  140. struct npcm_adc *info = iio_priv(indio_dev);
  141. switch (mask) {
  142. case IIO_CHAN_INFO_RAW:
  143. mutex_lock(&info->lock);
  144. ret = npcm_adc_read(info, val, chan->channel);
  145. mutex_unlock(&info->lock);
  146. if (ret) {
  147. dev_err(info->dev, "NPCM ADC read failed\n");
  148. return ret;
  149. }
  150. return IIO_VAL_INT;
  151. case IIO_CHAN_INFO_SCALE:
  152. if (!IS_ERR(info->vref)) {
  153. vref_uv = regulator_get_voltage(info->vref);
  154. *val = vref_uv / 1000;
  155. } else {
  156. *val = info->data->internal_vref;
  157. }
  158. *val2 = info->data->res_bits;
  159. return IIO_VAL_FRACTIONAL_LOG2;
  160. case IIO_CHAN_INFO_SAMP_FREQ:
  161. *val = info->adc_sample_hz;
  162. return IIO_VAL_INT;
  163. default:
  164. return -EINVAL;
  165. }
  166. return 0;
  167. }
  168. static const struct iio_info npcm_adc_iio_info = {
  169. .read_raw = &npcm_adc_read_raw,
  170. };
  171. static const struct of_device_id npcm_adc_match[] = {
  172. { .compatible = "nuvoton,npcm750-adc", .data = &npxm7xx_adc_info},
  173. { .compatible = "nuvoton,npcm845-adc", .data = &npxm8xx_adc_info},
  174. { /* sentinel */ }
  175. };
  176. MODULE_DEVICE_TABLE(of, npcm_adc_match);
  177. static int npcm_adc_probe(struct platform_device *pdev)
  178. {
  179. int ret;
  180. int irq;
  181. u32 div;
  182. u32 reg_con;
  183. struct npcm_adc *info;
  184. struct iio_dev *indio_dev;
  185. struct device *dev = &pdev->dev;
  186. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
  187. if (!indio_dev)
  188. return -ENOMEM;
  189. info = iio_priv(indio_dev);
  190. info->data = device_get_match_data(dev);
  191. if (!info->data)
  192. return -EINVAL;
  193. mutex_init(&info->lock);
  194. info->dev = &pdev->dev;
  195. info->regs = devm_platform_ioremap_resource(pdev, 0);
  196. if (IS_ERR(info->regs))
  197. return PTR_ERR(info->regs);
  198. info->reset = devm_reset_control_get(&pdev->dev, NULL);
  199. if (IS_ERR(info->reset))
  200. return PTR_ERR(info->reset);
  201. info->adc_clk = devm_clk_get(&pdev->dev, NULL);
  202. if (IS_ERR(info->adc_clk)) {
  203. dev_warn(&pdev->dev, "ADC clock failed: can't read clk\n");
  204. return PTR_ERR(info->adc_clk);
  205. }
  206. /* calculate ADC clock sample rate */
  207. reg_con = ioread32(info->regs + NPCM_ADCCON);
  208. div = reg_con & NPCM_ADCCON_DIV_MASK;
  209. div = div >> NPCM_ADCCON_DIV_SHIFT;
  210. info->adc_sample_hz = clk_get_rate(info->adc_clk) / ((div + 1) * 2);
  211. irq = platform_get_irq(pdev, 0);
  212. if (irq <= 0) {
  213. ret = -EINVAL;
  214. goto err_disable_clk;
  215. }
  216. ret = devm_request_irq(&pdev->dev, irq, npcm_adc_isr, 0,
  217. "NPCM_ADC", indio_dev);
  218. if (ret < 0) {
  219. dev_err(dev, "failed requesting interrupt\n");
  220. goto err_disable_clk;
  221. }
  222. reg_con = ioread32(info->regs + NPCM_ADCCON);
  223. info->vref = devm_regulator_get_optional(&pdev->dev, "vref");
  224. if (!IS_ERR(info->vref)) {
  225. ret = regulator_enable(info->vref);
  226. if (ret) {
  227. dev_err(&pdev->dev, "Can't enable ADC reference voltage\n");
  228. goto err_disable_clk;
  229. }
  230. iowrite32(reg_con & ~NPCM_ADCCON_REFSEL,
  231. info->regs + NPCM_ADCCON);
  232. } else {
  233. /*
  234. * Any error which is not ENODEV indicates the regulator
  235. * has been specified and so is a failure case.
  236. */
  237. if (PTR_ERR(info->vref) != -ENODEV) {
  238. ret = PTR_ERR(info->vref);
  239. goto err_disable_clk;
  240. }
  241. /* Use internal reference */
  242. iowrite32(reg_con | NPCM_ADCCON_REFSEL,
  243. info->regs + NPCM_ADCCON);
  244. }
  245. init_waitqueue_head(&info->wq);
  246. reg_con = ioread32(info->regs + NPCM_ADCCON);
  247. reg_con |= NPCM_ADC_ENABLE;
  248. /* Enable the ADC Module */
  249. iowrite32(reg_con, info->regs + NPCM_ADCCON);
  250. /* Start ADC conversion */
  251. iowrite32(reg_con | NPCM_ADCCON_ADC_CONV, info->regs + NPCM_ADCCON);
  252. platform_set_drvdata(pdev, indio_dev);
  253. indio_dev->name = dev_name(&pdev->dev);
  254. indio_dev->info = &npcm_adc_iio_info;
  255. indio_dev->modes = INDIO_DIRECT_MODE;
  256. indio_dev->channels = npcm_adc_iio_channels;
  257. indio_dev->num_channels = ARRAY_SIZE(npcm_adc_iio_channels);
  258. ret = iio_device_register(indio_dev);
  259. if (ret) {
  260. dev_err(&pdev->dev, "Couldn't register the device.\n");
  261. goto err_iio_register;
  262. }
  263. pr_info("NPCM ADC driver probed\n");
  264. return 0;
  265. err_iio_register:
  266. iowrite32(reg_con & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON);
  267. if (!IS_ERR(info->vref))
  268. regulator_disable(info->vref);
  269. err_disable_clk:
  270. clk_disable_unprepare(info->adc_clk);
  271. return ret;
  272. }
  273. static int npcm_adc_remove(struct platform_device *pdev)
  274. {
  275. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  276. struct npcm_adc *info = iio_priv(indio_dev);
  277. u32 regtemp;
  278. iio_device_unregister(indio_dev);
  279. regtemp = ioread32(info->regs + NPCM_ADCCON);
  280. iowrite32(regtemp & ~NPCM_ADCCON_ADC_EN, info->regs + NPCM_ADCCON);
  281. if (!IS_ERR(info->vref))
  282. regulator_disable(info->vref);
  283. clk_disable_unprepare(info->adc_clk);
  284. return 0;
  285. }
  286. static struct platform_driver npcm_adc_driver = {
  287. .probe = npcm_adc_probe,
  288. .remove = npcm_adc_remove,
  289. .driver = {
  290. .name = "npcm_adc",
  291. .of_match_table = npcm_adc_match,
  292. },
  293. };
  294. module_platform_driver(npcm_adc_driver);
  295. MODULE_DESCRIPTION("Nuvoton NPCM ADC Driver");
  296. MODULE_AUTHOR("Tomer Maimon <[email protected]>");
  297. MODULE_LICENSE("GPL v2");