ti_am335x_tscadc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI Touch Screen / ADC MFD driver
  4. *
  5. * Copyright (C) 2012 Texas Instruments Incorporated - https://www.ti.com/
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/clk.h>
  12. #include <linux/regmap.h>
  13. #include <linux/mfd/core.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/sched.h>
  18. #include <linux/mfd/ti_am335x_tscadc.h>
  19. static const struct regmap_config tscadc_regmap_config = {
  20. .name = "ti_tscadc",
  21. .reg_bits = 32,
  22. .reg_stride = 4,
  23. .val_bits = 32,
  24. };
  25. void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tscadc, u32 val)
  26. {
  27. unsigned long flags;
  28. spin_lock_irqsave(&tscadc->reg_lock, flags);
  29. tscadc->reg_se_cache |= val;
  30. if (tscadc->adc_waiting)
  31. wake_up(&tscadc->reg_se_wait);
  32. else if (!tscadc->adc_in_use)
  33. regmap_write(tscadc->regmap, REG_SE, tscadc->reg_se_cache);
  34. spin_unlock_irqrestore(&tscadc->reg_lock, flags);
  35. }
  36. EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);
  37. static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tscadc)
  38. {
  39. DEFINE_WAIT(wait);
  40. u32 reg;
  41. regmap_read(tscadc->regmap, REG_ADCFSM, &reg);
  42. if (reg & SEQ_STATUS) {
  43. tscadc->adc_waiting = true;
  44. prepare_to_wait(&tscadc->reg_se_wait, &wait,
  45. TASK_UNINTERRUPTIBLE);
  46. spin_unlock_irq(&tscadc->reg_lock);
  47. schedule();
  48. spin_lock_irq(&tscadc->reg_lock);
  49. finish_wait(&tscadc->reg_se_wait, &wait);
  50. /*
  51. * Sequencer should either be idle or
  52. * busy applying the charge step.
  53. */
  54. regmap_read(tscadc->regmap, REG_ADCFSM, &reg);
  55. WARN_ON((reg & SEQ_STATUS) && !(reg & CHARGE_STEP));
  56. tscadc->adc_waiting = false;
  57. }
  58. tscadc->adc_in_use = true;
  59. }
  60. void am335x_tsc_se_set_once(struct ti_tscadc_dev *tscadc, u32 val)
  61. {
  62. spin_lock_irq(&tscadc->reg_lock);
  63. am335x_tscadc_need_adc(tscadc);
  64. regmap_write(tscadc->regmap, REG_SE, val);
  65. spin_unlock_irq(&tscadc->reg_lock);
  66. }
  67. EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
  68. void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tscadc)
  69. {
  70. unsigned long flags;
  71. spin_lock_irqsave(&tscadc->reg_lock, flags);
  72. tscadc->adc_in_use = false;
  73. regmap_write(tscadc->regmap, REG_SE, tscadc->reg_se_cache);
  74. spin_unlock_irqrestore(&tscadc->reg_lock, flags);
  75. }
  76. EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done);
  77. void am335x_tsc_se_clr(struct ti_tscadc_dev *tscadc, u32 val)
  78. {
  79. unsigned long flags;
  80. spin_lock_irqsave(&tscadc->reg_lock, flags);
  81. tscadc->reg_se_cache &= ~val;
  82. regmap_write(tscadc->regmap, REG_SE, tscadc->reg_se_cache);
  83. spin_unlock_irqrestore(&tscadc->reg_lock, flags);
  84. }
  85. EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
  86. static void tscadc_idle_config(struct ti_tscadc_dev *tscadc)
  87. {
  88. unsigned int idleconfig;
  89. idleconfig = STEPCONFIG_INM_ADCREFM | STEPCONFIG_INP_ADCREFM;
  90. if (ti_adc_with_touchscreen(tscadc))
  91. idleconfig |= STEPCONFIG_YNN | STEPCONFIG_YPN;
  92. regmap_write(tscadc->regmap, REG_IDLECONFIG, idleconfig);
  93. }
  94. static int ti_tscadc_probe(struct platform_device *pdev)
  95. {
  96. struct ti_tscadc_dev *tscadc;
  97. struct resource *res;
  98. struct clk *clk;
  99. struct device_node *node;
  100. struct mfd_cell *cell;
  101. struct property *prop;
  102. const __be32 *cur;
  103. bool use_tsc = false, use_mag = false;
  104. u32 val;
  105. int err;
  106. int tscmag_wires = 0, adc_channels = 0, cell_idx = 0, total_channels;
  107. int readouts = 0, mag_tracks = 0;
  108. /* Allocate memory for device */
  109. tscadc = devm_kzalloc(&pdev->dev, sizeof(*tscadc), GFP_KERNEL);
  110. if (!tscadc)
  111. return -ENOMEM;
  112. tscadc->dev = &pdev->dev;
  113. if (!pdev->dev.of_node) {
  114. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  115. return -EINVAL;
  116. }
  117. tscadc->data = of_device_get_match_data(&pdev->dev);
  118. if (ti_adc_with_touchscreen(tscadc)) {
  119. node = of_get_child_by_name(pdev->dev.of_node, "tsc");
  120. of_property_read_u32(node, "ti,wires", &tscmag_wires);
  121. err = of_property_read_u32(node, "ti,coordinate-readouts",
  122. &readouts);
  123. if (err < 0)
  124. of_property_read_u32(node, "ti,coordiante-readouts",
  125. &readouts);
  126. of_node_put(node);
  127. if (tscmag_wires)
  128. use_tsc = true;
  129. } else {
  130. /*
  131. * When adding support for the magnetic stripe reader, here is
  132. * the place to look for the number of tracks used from device
  133. * tree. Let's default to 0 for now.
  134. */
  135. mag_tracks = 0;
  136. tscmag_wires = mag_tracks * 2;
  137. if (tscmag_wires)
  138. use_mag = true;
  139. }
  140. node = of_get_child_by_name(pdev->dev.of_node, "adc");
  141. of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
  142. adc_channels++;
  143. if (val > 7) {
  144. dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
  145. val);
  146. of_node_put(node);
  147. return -EINVAL;
  148. }
  149. }
  150. of_node_put(node);
  151. total_channels = tscmag_wires + adc_channels;
  152. if (total_channels > 8) {
  153. dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
  154. return -EINVAL;
  155. }
  156. if (total_channels == 0) {
  157. dev_err(&pdev->dev, "Need at least one channel.\n");
  158. return -EINVAL;
  159. }
  160. if (use_tsc && (readouts * 2 + 2 + adc_channels > 16)) {
  161. dev_err(&pdev->dev, "Too many step configurations requested\n");
  162. return -EINVAL;
  163. }
  164. err = platform_get_irq(pdev, 0);
  165. if (err < 0)
  166. return err;
  167. else
  168. tscadc->irq = err;
  169. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  170. tscadc->tscadc_base = devm_ioremap_resource(&pdev->dev, res);
  171. if (IS_ERR(tscadc->tscadc_base))
  172. return PTR_ERR(tscadc->tscadc_base);
  173. tscadc->tscadc_phys_base = res->start;
  174. tscadc->regmap = devm_regmap_init_mmio(&pdev->dev,
  175. tscadc->tscadc_base,
  176. &tscadc_regmap_config);
  177. if (IS_ERR(tscadc->regmap)) {
  178. dev_err(&pdev->dev, "regmap init failed\n");
  179. return PTR_ERR(tscadc->regmap);
  180. }
  181. spin_lock_init(&tscadc->reg_lock);
  182. init_waitqueue_head(&tscadc->reg_se_wait);
  183. pm_runtime_enable(&pdev->dev);
  184. pm_runtime_get_sync(&pdev->dev);
  185. /*
  186. * The TSC_ADC_Subsystem has 2 clock domains: OCP_CLK and ADC_CLK.
  187. * ADCs produce a 12-bit sample every 15 ADC_CLK cycles.
  188. * am33xx ADCs expect to capture 200ksps.
  189. * am47xx ADCs expect to capture 867ksps.
  190. * We need ADC clocks respectively running at 3MHz and 13MHz.
  191. * These frequencies are valid since TSC_ADC_SS controller design
  192. * assumes the OCP clock is at least 6x faster than the ADC clock.
  193. */
  194. clk = devm_clk_get(&pdev->dev, NULL);
  195. if (IS_ERR(clk)) {
  196. dev_err(&pdev->dev, "failed to get fck\n");
  197. err = PTR_ERR(clk);
  198. goto err_disable_clk;
  199. }
  200. tscadc->clk_div = (clk_get_rate(clk) / tscadc->data->target_clk_rate) - 1;
  201. regmap_write(tscadc->regmap, REG_CLKDIV, tscadc->clk_div);
  202. /*
  203. * Set the control register bits. tscadc->ctrl stores the configuration
  204. * of the CTRL register but not the subsystem enable bit which must be
  205. * added manually when timely.
  206. */
  207. tscadc->ctrl = CNTRLREG_STEPID;
  208. if (ti_adc_with_touchscreen(tscadc)) {
  209. tscadc->ctrl |= CNTRLREG_TSC_STEPCONFIGWRT;
  210. if (use_tsc) {
  211. tscadc->ctrl |= CNTRLREG_TSC_ENB;
  212. if (tscmag_wires == 5)
  213. tscadc->ctrl |= CNTRLREG_TSC_5WIRE;
  214. else
  215. tscadc->ctrl |= CNTRLREG_TSC_4WIRE;
  216. }
  217. } else {
  218. tscadc->ctrl |= CNTRLREG_MAG_PREAMP_PWRDOWN |
  219. CNTRLREG_MAG_PREAMP_BYPASS;
  220. }
  221. regmap_write(tscadc->regmap, REG_CTRL, tscadc->ctrl);
  222. tscadc_idle_config(tscadc);
  223. /* Enable the TSC module enable bit */
  224. regmap_write(tscadc->regmap, REG_CTRL, tscadc->ctrl | CNTRLREG_SSENB);
  225. /* TSC or MAG Cell */
  226. if (use_tsc || use_mag) {
  227. cell = &tscadc->cells[cell_idx++];
  228. cell->name = tscadc->data->secondary_feature_name;
  229. cell->of_compatible = tscadc->data->secondary_feature_compatible;
  230. cell->platform_data = &tscadc;
  231. cell->pdata_size = sizeof(tscadc);
  232. }
  233. /* ADC Cell */
  234. if (adc_channels > 0) {
  235. cell = &tscadc->cells[cell_idx++];
  236. cell->name = tscadc->data->adc_feature_name;
  237. cell->of_compatible = tscadc->data->adc_feature_compatible;
  238. cell->platform_data = &tscadc;
  239. cell->pdata_size = sizeof(tscadc);
  240. }
  241. err = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO,
  242. tscadc->cells, cell_idx, NULL, 0, NULL);
  243. if (err < 0)
  244. goto err_disable_clk;
  245. platform_set_drvdata(pdev, tscadc);
  246. return 0;
  247. err_disable_clk:
  248. pm_runtime_put_sync(&pdev->dev);
  249. pm_runtime_disable(&pdev->dev);
  250. return err;
  251. }
  252. static int ti_tscadc_remove(struct platform_device *pdev)
  253. {
  254. struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
  255. regmap_write(tscadc->regmap, REG_SE, 0x00);
  256. pm_runtime_put_sync(&pdev->dev);
  257. pm_runtime_disable(&pdev->dev);
  258. mfd_remove_devices(tscadc->dev);
  259. return 0;
  260. }
  261. static int __maybe_unused ti_tscadc_can_wakeup(struct device *dev, void *data)
  262. {
  263. return device_may_wakeup(dev);
  264. }
  265. static int __maybe_unused tscadc_suspend(struct device *dev)
  266. {
  267. struct ti_tscadc_dev *tscadc = dev_get_drvdata(dev);
  268. regmap_write(tscadc->regmap, REG_SE, 0x00);
  269. if (device_for_each_child(dev, NULL, ti_tscadc_can_wakeup)) {
  270. u32 ctrl;
  271. regmap_read(tscadc->regmap, REG_CTRL, &ctrl);
  272. ctrl &= ~(CNTRLREG_POWERDOWN);
  273. ctrl |= CNTRLREG_SSENB;
  274. regmap_write(tscadc->regmap, REG_CTRL, ctrl);
  275. }
  276. pm_runtime_put_sync(dev);
  277. return 0;
  278. }
  279. static int __maybe_unused tscadc_resume(struct device *dev)
  280. {
  281. struct ti_tscadc_dev *tscadc = dev_get_drvdata(dev);
  282. pm_runtime_get_sync(dev);
  283. regmap_write(tscadc->regmap, REG_CLKDIV, tscadc->clk_div);
  284. regmap_write(tscadc->regmap, REG_CTRL, tscadc->ctrl);
  285. tscadc_idle_config(tscadc);
  286. regmap_write(tscadc->regmap, REG_CTRL, tscadc->ctrl | CNTRLREG_SSENB);
  287. return 0;
  288. }
  289. static SIMPLE_DEV_PM_OPS(tscadc_pm_ops, tscadc_suspend, tscadc_resume);
  290. static const struct ti_tscadc_data tscdata = {
  291. .adc_feature_name = "TI-am335x-adc",
  292. .adc_feature_compatible = "ti,am3359-adc",
  293. .secondary_feature_name = "TI-am335x-tsc",
  294. .secondary_feature_compatible = "ti,am3359-tsc",
  295. .target_clk_rate = TSC_ADC_CLK,
  296. };
  297. static const struct ti_tscadc_data magdata = {
  298. .adc_feature_name = "TI-am43xx-adc",
  299. .adc_feature_compatible = "ti,am4372-adc",
  300. .secondary_feature_name = "TI-am43xx-mag",
  301. .secondary_feature_compatible = "ti,am4372-mag",
  302. .target_clk_rate = MAG_ADC_CLK,
  303. };
  304. static const struct of_device_id ti_tscadc_dt_ids[] = {
  305. { .compatible = "ti,am3359-tscadc", .data = &tscdata },
  306. { .compatible = "ti,am4372-magadc", .data = &magdata },
  307. { }
  308. };
  309. MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
  310. static struct platform_driver ti_tscadc_driver = {
  311. .driver = {
  312. .name = "ti_am3359-tscadc",
  313. .pm = &tscadc_pm_ops,
  314. .of_match_table = ti_tscadc_dt_ids,
  315. },
  316. .probe = ti_tscadc_probe,
  317. .remove = ti_tscadc_remove,
  318. };
  319. module_platform_driver(ti_tscadc_driver);
  320. MODULE_DESCRIPTION("TI touchscreen/magnetic stripe reader/ADC MFD controller driver");
  321. MODULE_AUTHOR("Rachna Patil <[email protected]>");
  322. MODULE_LICENSE("GPL");