ti_am335x_adc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * TI ADC MFD driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/io.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/iio/machine.h>
  27. #include <linux/iio/driver.h>
  28. #include <linux/mfd/ti_am335x_tscadc.h>
  29. struct tiadc_device {
  30. struct ti_tscadc_dev *mfd_tscadc;
  31. int channels;
  32. };
  33. static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
  34. {
  35. return readl(adc->mfd_tscadc->tscadc_base + reg);
  36. }
  37. static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
  38. unsigned int val)
  39. {
  40. writel(val, adc->mfd_tscadc->tscadc_base + reg);
  41. }
  42. static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
  43. {
  44. u32 step_en;
  45. step_en = ((1 << adc_dev->channels) - 1);
  46. step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
  47. return step_en;
  48. }
  49. static void tiadc_step_config(struct tiadc_device *adc_dev)
  50. {
  51. unsigned int stepconfig;
  52. int i, channels = 0, steps;
  53. u32 step_en;
  54. /*
  55. * There are 16 configurable steps and 8 analog input
  56. * lines available which are shared between Touchscreen and ADC.
  57. *
  58. * Steps backwards i.e. from 16 towards 0 are used by ADC
  59. * depending on number of input lines needed.
  60. * Channel would represent which analog input
  61. * needs to be given to ADC to digitalize data.
  62. */
  63. steps = TOTAL_STEPS - adc_dev->channels;
  64. channels = TOTAL_CHANNELS - adc_dev->channels;
  65. stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
  66. for (i = steps; i < TOTAL_STEPS; i++) {
  67. tiadc_writel(adc_dev, REG_STEPCONFIG(i),
  68. stepconfig | STEPCONFIG_INP(channels));
  69. tiadc_writel(adc_dev, REG_STEPDELAY(i),
  70. STEPCONFIG_OPENDLY);
  71. channels++;
  72. }
  73. step_en = get_adc_step_mask(adc_dev);
  74. am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
  75. }
  76. static const char * const chan_name_ain[] = {
  77. "AIN0",
  78. "AIN1",
  79. "AIN2",
  80. "AIN3",
  81. "AIN4",
  82. "AIN5",
  83. "AIN6",
  84. "AIN7",
  85. };
  86. static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
  87. {
  88. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  89. struct iio_chan_spec *chan_array;
  90. struct iio_chan_spec *chan;
  91. int i;
  92. indio_dev->num_channels = channels;
  93. chan_array = kcalloc(channels,
  94. sizeof(struct iio_chan_spec), GFP_KERNEL);
  95. if (chan_array == NULL)
  96. return -ENOMEM;
  97. chan = chan_array;
  98. for (i = 0; i < channels; i++, chan++) {
  99. chan->type = IIO_VOLTAGE;
  100. chan->indexed = 1;
  101. chan->channel = i;
  102. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  103. chan->datasheet_name = chan_name_ain[i];
  104. chan->scan_type.sign = 'u';
  105. chan->scan_type.realbits = 12;
  106. chan->scan_type.storagebits = 32;
  107. }
  108. indio_dev->channels = chan_array;
  109. return 0;
  110. }
  111. static void tiadc_channels_remove(struct iio_dev *indio_dev)
  112. {
  113. kfree(indio_dev->channels);
  114. }
  115. static int tiadc_read_raw(struct iio_dev *indio_dev,
  116. struct iio_chan_spec const *chan,
  117. int *val, int *val2, long mask)
  118. {
  119. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  120. int i;
  121. unsigned int fifo1count, readx1;
  122. /*
  123. * When the sub-system is first enabled,
  124. * the sequencer will always start with the
  125. * lowest step (1) and continue until step (16).
  126. * For ex: If we have enabled 4 ADC channels and
  127. * currently use only 1 out of them, the
  128. * sequencer still configures all the 4 steps,
  129. * leading to 3 unwanted data.
  130. * Hence we need to flush out this data.
  131. */
  132. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  133. for (i = 0; i < fifo1count; i++) {
  134. readx1 = tiadc_readl(adc_dev, REG_FIFO1);
  135. if (i == chan->channel)
  136. *val = readx1 & 0xfff;
  137. }
  138. am335x_tsc_se_update(adc_dev->mfd_tscadc);
  139. return IIO_VAL_INT;
  140. }
  141. static const struct iio_info tiadc_info = {
  142. .read_raw = &tiadc_read_raw,
  143. };
  144. static int tiadc_probe(struct platform_device *pdev)
  145. {
  146. struct iio_dev *indio_dev;
  147. struct tiadc_device *adc_dev;
  148. struct device_node *node = pdev->dev.of_node;
  149. int err;
  150. u32 val32;
  151. if (!node) {
  152. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  153. return -EINVAL;
  154. }
  155. indio_dev = iio_device_alloc(sizeof(struct tiadc_device));
  156. if (indio_dev == NULL) {
  157. dev_err(&pdev->dev, "failed to allocate iio device\n");
  158. err = -ENOMEM;
  159. goto err_ret;
  160. }
  161. adc_dev = iio_priv(indio_dev);
  162. adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
  163. err = of_property_read_u32(node,
  164. "ti,adc-channels", &val32);
  165. if (err < 0)
  166. goto err_free_device;
  167. adc_dev->channels = val32;
  168. indio_dev->dev.parent = &pdev->dev;
  169. indio_dev->name = dev_name(&pdev->dev);
  170. indio_dev->modes = INDIO_DIRECT_MODE;
  171. indio_dev->info = &tiadc_info;
  172. tiadc_step_config(adc_dev);
  173. err = tiadc_channel_init(indio_dev, adc_dev->channels);
  174. if (err < 0)
  175. goto err_free_device;
  176. err = iio_device_register(indio_dev);
  177. if (err)
  178. goto err_free_channels;
  179. platform_set_drvdata(pdev, indio_dev);
  180. return 0;
  181. err_free_channels:
  182. tiadc_channels_remove(indio_dev);
  183. err_free_device:
  184. iio_device_free(indio_dev);
  185. err_ret:
  186. return err;
  187. }
  188. static int tiadc_remove(struct platform_device *pdev)
  189. {
  190. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  191. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  192. u32 step_en;
  193. iio_device_unregister(indio_dev);
  194. tiadc_channels_remove(indio_dev);
  195. step_en = get_adc_step_mask(adc_dev);
  196. am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
  197. iio_device_free(indio_dev);
  198. return 0;
  199. }
  200. #ifdef CONFIG_PM
  201. static int tiadc_suspend(struct device *dev)
  202. {
  203. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  204. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  205. struct ti_tscadc_dev *tscadc_dev;
  206. unsigned int idle;
  207. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  208. if (!device_may_wakeup(tscadc_dev->dev)) {
  209. idle = tiadc_readl(adc_dev, REG_CTRL);
  210. idle &= ~(CNTRLREG_TSCSSENB);
  211. tiadc_writel(adc_dev, REG_CTRL, (idle |
  212. CNTRLREG_POWERDOWN));
  213. }
  214. return 0;
  215. }
  216. static int tiadc_resume(struct device *dev)
  217. {
  218. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  219. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  220. unsigned int restore;
  221. /* Make sure ADC is powered up */
  222. restore = tiadc_readl(adc_dev, REG_CTRL);
  223. restore &= ~(CNTRLREG_POWERDOWN);
  224. tiadc_writel(adc_dev, REG_CTRL, restore);
  225. tiadc_step_config(adc_dev);
  226. return 0;
  227. }
  228. static const struct dev_pm_ops tiadc_pm_ops = {
  229. .suspend = tiadc_suspend,
  230. .resume = tiadc_resume,
  231. };
  232. #define TIADC_PM_OPS (&tiadc_pm_ops)
  233. #else
  234. #define TIADC_PM_OPS NULL
  235. #endif
  236. static const struct of_device_id ti_adc_dt_ids[] = {
  237. { .compatible = "ti,am3359-adc", },
  238. { }
  239. };
  240. MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
  241. static struct platform_driver tiadc_driver = {
  242. .driver = {
  243. .name = "TI-am335x-adc",
  244. .owner = THIS_MODULE,
  245. .pm = TIADC_PM_OPS,
  246. .of_match_table = of_match_ptr(ti_adc_dt_ids),
  247. },
  248. .probe = tiadc_probe,
  249. .remove = tiadc_remove,
  250. };
  251. module_platform_driver(tiadc_driver);
  252. MODULE_DESCRIPTION("TI ADC controller driver");
  253. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  254. MODULE_LICENSE("GPL");