ti_am335x_tscadc.c 6.8 KB

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