ti_am335x_tscadc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * TI Touch Screen / 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/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/clk.h>
  21. #include <linux/regmap.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/mfd/ti_am335x_tscadc.h>
  25. #include <linux/input/ti_am335x_tsc.h>
  26. #include <linux/platform_data/ti_am335x_adc.h>
  27. static unsigned int tscadc_readl(struct ti_tscadc_dev *tsadc, unsigned int reg)
  28. {
  29. unsigned int val;
  30. regmap_read(tsadc->regmap_tscadc, reg, &val);
  31. return val;
  32. }
  33. static void tscadc_writel(struct ti_tscadc_dev *tsadc, unsigned int reg,
  34. unsigned int val)
  35. {
  36. regmap_write(tsadc->regmap_tscadc, reg, val);
  37. }
  38. static const struct regmap_config tscadc_regmap_config = {
  39. .name = "ti_tscadc",
  40. .reg_bits = 32,
  41. .reg_stride = 4,
  42. .val_bits = 32,
  43. };
  44. void am335x_tsc_se_update(struct ti_tscadc_dev *tsadc)
  45. {
  46. tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
  47. }
  48. EXPORT_SYMBOL_GPL(am335x_tsc_se_update);
  49. void am335x_tsc_se_set(struct ti_tscadc_dev *tsadc, u32 val)
  50. {
  51. spin_lock(&tsadc->reg_lock);
  52. tsadc->reg_se_cache |= val;
  53. spin_unlock(&tsadc->reg_lock);
  54. am335x_tsc_se_update(tsadc);
  55. }
  56. EXPORT_SYMBOL_GPL(am335x_tsc_se_set);
  57. void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
  58. {
  59. spin_lock(&tsadc->reg_lock);
  60. tsadc->reg_se_cache &= ~val;
  61. spin_unlock(&tsadc->reg_lock);
  62. am335x_tsc_se_update(tsadc);
  63. }
  64. EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
  65. static void tscadc_idle_config(struct ti_tscadc_dev *config)
  66. {
  67. unsigned int idleconfig;
  68. idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
  69. STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
  70. tscadc_writel(config, REG_IDLECONFIG, idleconfig);
  71. }
  72. static int ti_tscadc_probe(struct platform_device *pdev)
  73. {
  74. struct ti_tscadc_dev *tscadc;
  75. struct resource *res;
  76. struct clk *clk;
  77. struct mfd_tscadc_board *pdata = pdev->dev.platform_data;
  78. struct mfd_cell *cell;
  79. int err, ctrl;
  80. int clk_value, clock_rate;
  81. int tsc_wires, adc_channels = 0, total_channels;
  82. if (!pdata) {
  83. dev_err(&pdev->dev, "Could not find platform data\n");
  84. return -EINVAL;
  85. }
  86. if (pdata->adc_init)
  87. adc_channels = pdata->adc_init->adc_channels;
  88. tsc_wires = pdata->tsc_init->wires;
  89. total_channels = tsc_wires + adc_channels;
  90. if (total_channels > 8) {
  91. dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
  92. return -EINVAL;
  93. }
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. if (!res) {
  96. dev_err(&pdev->dev, "no memory resource defined.\n");
  97. return -EINVAL;
  98. }
  99. /* Allocate memory for device */
  100. tscadc = devm_kzalloc(&pdev->dev,
  101. sizeof(struct ti_tscadc_dev), GFP_KERNEL);
  102. if (!tscadc) {
  103. dev_err(&pdev->dev, "failed to allocate memory.\n");
  104. return -ENOMEM;
  105. }
  106. tscadc->dev = &pdev->dev;
  107. err = platform_get_irq(pdev, 0);
  108. if (err < 0) {
  109. dev_err(&pdev->dev, "no irq ID is specified.\n");
  110. goto ret;
  111. } else
  112. tscadc->irq = err;
  113. res = devm_request_mem_region(&pdev->dev,
  114. res->start, resource_size(res), pdev->name);
  115. if (!res) {
  116. dev_err(&pdev->dev, "failed to reserve registers.\n");
  117. return -EBUSY;
  118. }
  119. tscadc->tscadc_base = devm_ioremap(&pdev->dev,
  120. res->start, resource_size(res));
  121. if (!tscadc->tscadc_base) {
  122. dev_err(&pdev->dev, "failed to map registers.\n");
  123. return -ENOMEM;
  124. }
  125. tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
  126. tscadc->tscadc_base, &tscadc_regmap_config);
  127. if (IS_ERR(tscadc->regmap_tscadc)) {
  128. dev_err(&pdev->dev, "regmap init failed\n");
  129. err = PTR_ERR(tscadc->regmap_tscadc);
  130. goto ret;
  131. }
  132. spin_lock_init(&tscadc->reg_lock);
  133. pm_runtime_enable(&pdev->dev);
  134. pm_runtime_get_sync(&pdev->dev);
  135. /*
  136. * The TSC_ADC_Subsystem has 2 clock domains
  137. * OCP_CLK and ADC_CLK.
  138. * The ADC clock is expected to run at target of 3MHz,
  139. * and expected to capture 12-bit data at a rate of 200 KSPS.
  140. * The TSC_ADC_SS controller design assumes the OCP clock is
  141. * at least 6x faster than the ADC clock.
  142. */
  143. clk = clk_get(&pdev->dev, "adc_tsc_fck");
  144. if (IS_ERR(clk)) {
  145. dev_err(&pdev->dev, "failed to get TSC fck\n");
  146. err = PTR_ERR(clk);
  147. goto err_disable_clk;
  148. }
  149. clock_rate = clk_get_rate(clk);
  150. clk_put(clk);
  151. clk_value = clock_rate / ADC_CLK;
  152. if (clk_value < MAX_CLK_DIV) {
  153. dev_err(&pdev->dev, "clock input less than min clock requirement\n");
  154. err = -EINVAL;
  155. goto err_disable_clk;
  156. }
  157. /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
  158. clk_value = clk_value - 1;
  159. tscadc_writel(tscadc, REG_CLKDIV, clk_value);
  160. /* Set the control register bits */
  161. ctrl = CNTRLREG_STEPCONFIGWRT |
  162. CNTRLREG_TSCENB |
  163. CNTRLREG_STEPID |
  164. CNTRLREG_4WIRE;
  165. tscadc_writel(tscadc, REG_CTRL, ctrl);
  166. /* Set register bits for Idle Config Mode */
  167. tscadc_idle_config(tscadc);
  168. /* Enable the TSC module enable bit */
  169. ctrl = tscadc_readl(tscadc, REG_CTRL);
  170. ctrl |= CNTRLREG_TSCSSENB;
  171. tscadc_writel(tscadc, REG_CTRL, ctrl);
  172. /* TSC Cell */
  173. cell = &tscadc->cells[TSC_CELL];
  174. cell->name = "tsc";
  175. cell->of_compatible = "ti,am3359-tsc";
  176. cell->platform_data = &tscadc;
  177. cell->pdata_size = sizeof(tscadc);
  178. /* ADC Cell */
  179. cell = &tscadc->cells[ADC_CELL];
  180. cell->name = "tiadc";
  181. cell->platform_data = &tscadc;
  182. cell->pdata_size = sizeof(tscadc);
  183. err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
  184. TSCADC_CELLS, NULL, 0, NULL);
  185. if (err < 0)
  186. goto err_disable_clk;
  187. device_init_wakeup(&pdev->dev, true);
  188. platform_set_drvdata(pdev, tscadc);
  189. return 0;
  190. err_disable_clk:
  191. pm_runtime_put_sync(&pdev->dev);
  192. pm_runtime_disable(&pdev->dev);
  193. ret:
  194. return err;
  195. }
  196. static int ti_tscadc_remove(struct platform_device *pdev)
  197. {
  198. struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
  199. tscadc_writel(tscadc, REG_SE, 0x00);
  200. pm_runtime_put_sync(&pdev->dev);
  201. pm_runtime_disable(&pdev->dev);
  202. mfd_remove_devices(tscadc->dev);
  203. return 0;
  204. }
  205. #ifdef CONFIG_PM
  206. static int tscadc_suspend(struct device *dev)
  207. {
  208. struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
  209. tscadc_writel(tscadc_dev, REG_SE, 0x00);
  210. pm_runtime_put_sync(dev);
  211. return 0;
  212. }
  213. static int tscadc_resume(struct device *dev)
  214. {
  215. struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
  216. unsigned int restore, ctrl;
  217. pm_runtime_get_sync(dev);
  218. /* context restore */
  219. ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_TSCENB |
  220. CNTRLREG_STEPID | CNTRLREG_4WIRE;
  221. tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
  222. tscadc_idle_config(tscadc_dev);
  223. am335x_tsc_se_update(tscadc_dev);
  224. restore = tscadc_readl(tscadc_dev, REG_CTRL);
  225. tscadc_writel(tscadc_dev, REG_CTRL,
  226. (restore | CNTRLREG_TSCSSENB));
  227. return 0;
  228. }
  229. static const struct dev_pm_ops tscadc_pm_ops = {
  230. .suspend = tscadc_suspend,
  231. .resume = tscadc_resume,
  232. };
  233. #define TSCADC_PM_OPS (&tscadc_pm_ops)
  234. #else
  235. #define TSCADC_PM_OPS NULL
  236. #endif
  237. static struct platform_driver ti_tscadc_driver = {
  238. .driver = {
  239. .name = "ti_tscadc",
  240. .owner = THIS_MODULE,
  241. .pm = TSCADC_PM_OPS,
  242. },
  243. .probe = ti_tscadc_probe,
  244. .remove = ti_tscadc_remove,
  245. };
  246. module_platform_driver(ti_tscadc_driver);
  247. MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
  248. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  249. MODULE_LICENSE("GPL");