ti_am335x_adc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/mfd/ti_am335x_tscadc.h>
  25. #include <linux/platform_data/ti_am335x_adc.h>
  26. struct tiadc_device {
  27. struct ti_tscadc_dev *mfd_tscadc;
  28. int channels;
  29. };
  30. static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg)
  31. {
  32. return readl(adc->mfd_tscadc->tscadc_base + reg);
  33. }
  34. static void tiadc_writel(struct tiadc_device *adc, unsigned int reg,
  35. unsigned int val)
  36. {
  37. writel(val, adc->mfd_tscadc->tscadc_base + reg);
  38. }
  39. static u32 get_adc_step_mask(struct tiadc_device *adc_dev)
  40. {
  41. u32 step_en;
  42. step_en = ((1 << adc_dev->channels) - 1);
  43. step_en <<= TOTAL_STEPS - adc_dev->channels + 1;
  44. return step_en;
  45. }
  46. static void tiadc_step_config(struct tiadc_device *adc_dev)
  47. {
  48. unsigned int stepconfig;
  49. int i, channels = 0, steps;
  50. u32 step_en;
  51. /*
  52. * There are 16 configurable steps and 8 analog input
  53. * lines available which are shared between Touchscreen and ADC.
  54. *
  55. * Steps backwards i.e. from 16 towards 0 are used by ADC
  56. * depending on number of input lines needed.
  57. * Channel would represent which analog input
  58. * needs to be given to ADC to digitalize data.
  59. */
  60. steps = TOTAL_STEPS - adc_dev->channels;
  61. channels = TOTAL_CHANNELS - adc_dev->channels;
  62. stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1;
  63. for (i = (steps + 1); i <= TOTAL_STEPS; i++) {
  64. tiadc_writel(adc_dev, REG_STEPCONFIG(i),
  65. stepconfig | STEPCONFIG_INP(channels));
  66. tiadc_writel(adc_dev, REG_STEPDELAY(i),
  67. STEPCONFIG_OPENDLY);
  68. channels++;
  69. }
  70. step_en = get_adc_step_mask(adc_dev);
  71. am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en);
  72. }
  73. static int tiadc_channel_init(struct iio_dev *indio_dev, int channels)
  74. {
  75. struct iio_chan_spec *chan_array;
  76. int i;
  77. indio_dev->num_channels = channels;
  78. chan_array = kcalloc(indio_dev->num_channels,
  79. sizeof(struct iio_chan_spec), GFP_KERNEL);
  80. if (chan_array == NULL)
  81. return -ENOMEM;
  82. for (i = 0; i < (indio_dev->num_channels); i++) {
  83. struct iio_chan_spec *chan = chan_array + i;
  84. chan->type = IIO_VOLTAGE;
  85. chan->indexed = 1;
  86. chan->channel = i;
  87. chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  88. }
  89. indio_dev->channels = chan_array;
  90. return indio_dev->num_channels;
  91. }
  92. static void tiadc_channels_remove(struct iio_dev *indio_dev)
  93. {
  94. kfree(indio_dev->channels);
  95. }
  96. static int tiadc_read_raw(struct iio_dev *indio_dev,
  97. struct iio_chan_spec const *chan,
  98. int *val, int *val2, long mask)
  99. {
  100. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  101. int i;
  102. unsigned int fifo1count, readx1;
  103. /*
  104. * When the sub-system is first enabled,
  105. * the sequencer will always start with the
  106. * lowest step (1) and continue until step (16).
  107. * For ex: If we have enabled 4 ADC channels and
  108. * currently use only 1 out of them, the
  109. * sequencer still configures all the 4 steps,
  110. * leading to 3 unwanted data.
  111. * Hence we need to flush out this data.
  112. */
  113. fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT);
  114. for (i = 0; i < fifo1count; i++) {
  115. readx1 = tiadc_readl(adc_dev, REG_FIFO1);
  116. if (i == chan->channel)
  117. *val = readx1 & 0xfff;
  118. }
  119. am335x_tsc_se_update(adc_dev->mfd_tscadc);
  120. return IIO_VAL_INT;
  121. }
  122. static const struct iio_info tiadc_info = {
  123. .read_raw = &tiadc_read_raw,
  124. };
  125. static int tiadc_probe(struct platform_device *pdev)
  126. {
  127. struct iio_dev *indio_dev;
  128. struct tiadc_device *adc_dev;
  129. struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
  130. struct mfd_tscadc_board *pdata;
  131. int err;
  132. pdata = tscadc_dev->dev->platform_data;
  133. if (!pdata || !pdata->adc_init) {
  134. dev_err(&pdev->dev, "Could not find platform data\n");
  135. return -EINVAL;
  136. }
  137. indio_dev = iio_device_alloc(sizeof(struct tiadc_device));
  138. if (indio_dev == NULL) {
  139. dev_err(&pdev->dev, "failed to allocate iio device\n");
  140. err = -ENOMEM;
  141. goto err_ret;
  142. }
  143. adc_dev = iio_priv(indio_dev);
  144. adc_dev->mfd_tscadc = tscadc_dev;
  145. adc_dev->channels = pdata->adc_init->adc_channels;
  146. indio_dev->dev.parent = &pdev->dev;
  147. indio_dev->name = dev_name(&pdev->dev);
  148. indio_dev->modes = INDIO_DIRECT_MODE;
  149. indio_dev->info = &tiadc_info;
  150. tiadc_step_config(adc_dev);
  151. err = tiadc_channel_init(indio_dev, adc_dev->channels);
  152. if (err < 0)
  153. goto err_free_device;
  154. err = iio_device_register(indio_dev);
  155. if (err)
  156. goto err_free_channels;
  157. platform_set_drvdata(pdev, indio_dev);
  158. return 0;
  159. err_free_channels:
  160. tiadc_channels_remove(indio_dev);
  161. err_free_device:
  162. iio_device_free(indio_dev);
  163. err_ret:
  164. return err;
  165. }
  166. static int tiadc_remove(struct platform_device *pdev)
  167. {
  168. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  169. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  170. u32 step_en;
  171. iio_device_unregister(indio_dev);
  172. tiadc_channels_remove(indio_dev);
  173. step_en = get_adc_step_mask(adc_dev);
  174. am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
  175. iio_device_free(indio_dev);
  176. return 0;
  177. }
  178. #ifdef CONFIG_PM
  179. static int tiadc_suspend(struct device *dev)
  180. {
  181. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  182. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  183. struct ti_tscadc_dev *tscadc_dev;
  184. unsigned int idle;
  185. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  186. if (!device_may_wakeup(tscadc_dev->dev)) {
  187. idle = tiadc_readl(adc_dev, REG_CTRL);
  188. idle &= ~(CNTRLREG_TSCSSENB);
  189. tiadc_writel(adc_dev, REG_CTRL, (idle |
  190. CNTRLREG_POWERDOWN));
  191. }
  192. return 0;
  193. }
  194. static int tiadc_resume(struct device *dev)
  195. {
  196. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  197. struct tiadc_device *adc_dev = iio_priv(indio_dev);
  198. unsigned int restore;
  199. /* Make sure ADC is powered up */
  200. restore = tiadc_readl(adc_dev, REG_CTRL);
  201. restore &= ~(CNTRLREG_POWERDOWN);
  202. tiadc_writel(adc_dev, REG_CTRL, restore);
  203. tiadc_step_config(adc_dev);
  204. return 0;
  205. }
  206. static const struct dev_pm_ops tiadc_pm_ops = {
  207. .suspend = tiadc_suspend,
  208. .resume = tiadc_resume,
  209. };
  210. #define TIADC_PM_OPS (&tiadc_pm_ops)
  211. #else
  212. #define TIADC_PM_OPS NULL
  213. #endif
  214. static struct platform_driver tiadc_driver = {
  215. .driver = {
  216. .name = "tiadc",
  217. .owner = THIS_MODULE,
  218. .pm = TIADC_PM_OPS,
  219. },
  220. .probe = tiadc_probe,
  221. .remove = tiadc_remove,
  222. };
  223. module_platform_driver(tiadc_driver);
  224. MODULE_DESCRIPTION("TI ADC controller driver");
  225. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  226. MODULE_LICENSE("GPL");