ti_am335x_adc.c 6.3 KB

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