ti_am335x_adc.c 7.8 KB

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