ti_am335x_adc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. u8 channel_line[8];
  33. u8 channel_step[8];
  34. };
  35. static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
  36. {
  37. return readl(adc->mfd_tscadc->tscadc_base + reg);
  38. }
  39. static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
  40. unsigned int val)
  41. {
  42. writel(val, adc->mfd_tscadc->tscadc_base + reg);
  43. }
  44. static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
  45. {
  46. u32 step_en;
  47. step_en = ((1 << adc_dev->channels) - 1);
  48. step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
  49. return step_en;
  50. }
  51. static void tiadc_step_config(struct tiadc_device *adc_dev)
  52. {
  53. unsigned int stepconfig;
  54. int i, steps;
  55. /*
  56. * There are 16 configurable steps and 8 analog input
  57. * lines available which are shared between Touchscreen and ADC.
  58. *
  59. * Steps backwards i.e. from 16 towards 0 are used by ADC
  60. * depending on number of input lines needed.
  61. * Channel would represent which analog input
  62. * needs to be given to ADC to digitalize data.
  63. */
  64. steps = TOTAL_STEPS - adc_dev->channels;
  65. stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
  66. for (i = 0; i < adc_dev->channels; i++) {
  67. int chan;
  68. chan = adc_dev->channel_line[i];
  69. tiadc_writel(adc_dev, REG_STEPCONFIG(steps),
  70. stepconfig | STEPCONFIG_INP(chan));
  71. tiadc_writel(adc_dev, REG_STEPDELAY(steps),
  72. STEPCONFIG_OPENDLY);
  73. adc_dev->channel_step[i] = steps;
  74. steps++;
  75. }
  76. }
  77. static const char * const chan_name_ain[] = {
  78. "AIN0",
  79. "AIN1",
  80. "AIN2",
  81. "AIN3",
  82. "AIN4",
  83. "AIN5",
  84. "AIN6",
  85. "AIN7",
  86. };
  87. static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
  88. {
  89. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  90. struct iio_chan_spec *chan_array;
  91. struct iio_chan_spec *chan;
  92. int i;
  93. indio_dev->num_channels = channels;
  94. chan_array = kcalloc(channels,
  95. sizeof(struct iio_chan_spec), GFP_KERNEL);
  96. if (chan_array == NULL)
  97. return -ENOMEM;
  98. chan = chan_array;
  99. for (i = 0; i < channels; i++, chan++) {
  100. chan->type = IIO_VOLTAGE;
  101. chan->indexed = 1;
  102. chan->channel = adc_dev->channel_line[i];
  103. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  104. chan->datasheet_name = chan_name_ain[chan->channel];
  105. chan->scan_type.sign = 'u';
  106. chan->scan_type.realbits = 12;
  107. chan->scan_type.storagebits = 32;
  108. }
  109. indio_dev->channels = chan_array;
  110. return 0;
  111. }
  112. static void tiadc_channels_remove(struct iio_dev *indio_dev)
  113. {
  114. kfree(indio_dev->channels);
  115. }
  116. static int tiadc_read_raw(struct iio_dev *indio_dev,
  117. struct iio_chan_spec const *chan,
  118. int *val, int *val2, long mask)
  119. {
  120. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  121. int i, map_val;
  122. unsigned int fifo1count, read, stepid;
  123. u32 step = UINT_MAX;
  124. bool found = false;
  125. u32 step_en;
  126. unsigned long timeout = jiffies + usecs_to_jiffies
  127. (IDLE_TIMEOUT * adc_dev->channels);
  128. step_en = get_adc_step_mask(adc_dev);
  129. am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
  130. /* Wait for ADC sequencer to complete sampling */
  131. while (tiadc_readl(adc_dev, REG_ADCFSM) & SEQ_STATUS) {
  132. if (time_after(jiffies, timeout))
  133. return -EAGAIN;
  134. }
  135. map_val = chan->channel + TOTAL_CHANNELS;
  136. /*
  137. * When the sub-system is first enabled,
  138. * the sequencer will always start with the
  139. * lowest step (1) and continue until step (16).
  140. * For ex: If we have enabled 4 ADC channels and
  141. * currently use only 1 out of them, the
  142. * sequencer still configures all the 4 steps,
  143. * leading to 3 unwanted data.
  144. * Hence we need to flush out this data.
  145. */
  146. for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) {
  147. if (chan->channel == adc_dev->channel_line[i]) {
  148. step = adc_dev->channel_step[i];
  149. break;
  150. }
  151. }
  152. if (WARN_ON_ONCE(step == UINT_MAX))
  153. return -EINVAL;
  154. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  155. for (i = 0; i < fifo1count; i++) {
  156. read = tiadc_readl(adc_dev, REG_FIFO1);
  157. stepid = read & FIFOREAD_CHNLID_MASK;
  158. stepid = stepid >> 0x10;
  159. if (stepid == map_val) {
  160. read = read & FIFOREAD_DATA_MASK;
  161. found = true;
  162. *val = read;
  163. }
  164. }
  165. if (found == false)
  166. return -EBUSY;
  167. return IIO_VAL_INT;
  168. }
  169. static const struct iio_info tiadc_info = {
  170. .read_raw = &tiadc_read_raw,
  171. .driver_module = THIS_MODULE,
  172. };
  173. static int tiadc_probe(struct platform_device *pdev)
  174. {
  175. struct iio_dev *indio_dev;
  176. struct tiadc_device *adc_dev;
  177. struct device_node *node = pdev->dev.of_node;
  178. struct property *prop;
  179. const __be32 *cur;
  180. int err;
  181. u32 val;
  182. int channels = 0;
  183. if (!node) {
  184. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  185. return -EINVAL;
  186. }
  187. indio_dev = devm_iio_device_alloc(&pdev->dev,
  188. sizeof(struct tiadc_device));
  189. if (indio_dev == NULL) {
  190. dev_err(&pdev->dev, "failed to allocate iio device\n");
  191. return -ENOMEM;
  192. }
  193. adc_dev = iio_priv(indio_dev);
  194. adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev);
  195. of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
  196. adc_dev->channel_line[channels] = val;
  197. channels++;
  198. }
  199. adc_dev->channels = channels;
  200. indio_dev->dev.parent = &pdev->dev;
  201. indio_dev->name = dev_name(&pdev->dev);
  202. indio_dev->modes = INDIO_DIRECT_MODE;
  203. indio_dev->info = &tiadc_info;
  204. tiadc_step_config(adc_dev);
  205. err = tiadc_channel_init(indio_dev, adc_dev->channels);
  206. if (err < 0)
  207. return err;
  208. err = iio_device_register(indio_dev);
  209. if (err)
  210. goto err_free_channels;
  211. platform_set_drvdata(pdev, indio_dev);
  212. return 0;
  213. err_free_channels:
  214. tiadc_channels_remove(indio_dev);
  215. return err;
  216. }
  217. static int tiadc_remove(struct platform_device *pdev)
  218. {
  219. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  220. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  221. u32 step_en;
  222. iio_device_unregister(indio_dev);
  223. tiadc_channels_remove(indio_dev);
  224. step_en = get_adc_step_mask(adc_dev);
  225. am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
  226. return 0;
  227. }
  228. #ifdef CONFIG_PM
  229. static int tiadc_suspend(struct device *dev)
  230. {
  231. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  232. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  233. struct ti_tscadc_dev *tscadc_dev;
  234. unsigned int idle;
  235. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  236. if (!device_may_wakeup(tscadc_dev->dev)) {
  237. idle = tiadc_readl(adc_dev, REG_CTRL);
  238. idle &= ~(CNTRLREG_TSCSSENB);
  239. tiadc_writel(adc_dev, REG_CTRL, (idle |
  240. CNTRLREG_POWERDOWN));
  241. }
  242. return 0;
  243. }
  244. static int tiadc_resume(struct device *dev)
  245. {
  246. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  247. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  248. unsigned int restore;
  249. /* Make sure ADC is powered up */
  250. restore = tiadc_readl(adc_dev, REG_CTRL);
  251. restore &= ~(CNTRLREG_POWERDOWN);
  252. tiadc_writel(adc_dev, REG_CTRL, restore);
  253. tiadc_step_config(adc_dev);
  254. return 0;
  255. }
  256. static const struct dev_pm_ops tiadc_pm_ops = {
  257. .suspend = tiadc_suspend,
  258. .resume = tiadc_resume,
  259. };
  260. #define TIADC_PM_OPS (&tiadc_pm_ops)
  261. #else
  262. #define TIADC_PM_OPS NULL
  263. #endif
  264. static const struct of_device_id ti_adc_dt_ids[] = {
  265. { .compatible = "ti,am3359-adc", },
  266. { }
  267. };
  268. MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
  269. static struct platform_driver tiadc_driver = {
  270. .driver = {
  271. .name = "TI-am335x-adc",
  272. .owner = THIS_MODULE,
  273. .pm = TIADC_PM_OPS,
  274. .of_match_table = of_match_ptr(ti_adc_dt_ids),
  275. },
  276. .probe = tiadc_probe,
  277. .remove = tiadc_remove,
  278. };
  279. module_platform_driver(tiadc_driver);
  280. MODULE_DESCRIPTION("TI ADC controller driver");
  281. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  282. MODULE_LICENSE("GPL");