ti_am335x_tscadc.c 6.4 KB

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