imx6ul_tsc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Freescale i.MX6UL touchscreen controller driver
  4. //
  5. // Copyright (C) 2015 Freescale Semiconductor, Inc.
  6. #include <linux/errno.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/input.h>
  11. #include <linux/slab.h>
  12. #include <linux/completion.h>
  13. #include <linux/delay.h>
  14. #include <linux/of.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/io.h>
  19. #include <linux/log2.h>
  20. /* ADC configuration registers field define */
  21. #define ADC_AIEN (0x1 << 7)
  22. #define ADC_CONV_DISABLE 0x1F
  23. #define ADC_AVGE (0x1 << 5)
  24. #define ADC_CAL (0x1 << 7)
  25. #define ADC_CALF 0x2
  26. #define ADC_12BIT_MODE (0x2 << 2)
  27. #define ADC_CONV_MODE_MASK (0x3 << 2)
  28. #define ADC_IPG_CLK 0x00
  29. #define ADC_INPUT_CLK_MASK 0x3
  30. #define ADC_CLK_DIV_8 (0x03 << 5)
  31. #define ADC_CLK_DIV_MASK (0x3 << 5)
  32. #define ADC_SHORT_SAMPLE_MODE (0x0 << 4)
  33. #define ADC_SAMPLE_MODE_MASK (0x1 << 4)
  34. #define ADC_HARDWARE_TRIGGER (0x1 << 13)
  35. #define ADC_AVGS_SHIFT 14
  36. #define ADC_AVGS_MASK (0x3 << 14)
  37. #define SELECT_CHANNEL_4 0x04
  38. #define SELECT_CHANNEL_1 0x01
  39. #define DISABLE_CONVERSION_INT (0x0 << 7)
  40. /* ADC registers */
  41. #define REG_ADC_HC0 0x00
  42. #define REG_ADC_HC1 0x04
  43. #define REG_ADC_HC2 0x08
  44. #define REG_ADC_HC3 0x0C
  45. #define REG_ADC_HC4 0x10
  46. #define REG_ADC_HS 0x14
  47. #define REG_ADC_R0 0x18
  48. #define REG_ADC_CFG 0x2C
  49. #define REG_ADC_GC 0x30
  50. #define REG_ADC_GS 0x34
  51. #define ADC_TIMEOUT msecs_to_jiffies(100)
  52. /* TSC registers */
  53. #define REG_TSC_BASIC_SETING 0x00
  54. #define REG_TSC_PRE_CHARGE_TIME 0x10
  55. #define REG_TSC_FLOW_CONTROL 0x20
  56. #define REG_TSC_MEASURE_VALUE 0x30
  57. #define REG_TSC_INT_EN 0x40
  58. #define REG_TSC_INT_SIG_EN 0x50
  59. #define REG_TSC_INT_STATUS 0x60
  60. #define REG_TSC_DEBUG_MODE 0x70
  61. #define REG_TSC_DEBUG_MODE2 0x80
  62. /* TSC configuration registers field define */
  63. #define DETECT_4_WIRE_MODE (0x0 << 4)
  64. #define AUTO_MEASURE 0x1
  65. #define MEASURE_SIGNAL 0x1
  66. #define DETECT_SIGNAL (0x1 << 4)
  67. #define VALID_SIGNAL (0x1 << 8)
  68. #define MEASURE_INT_EN 0x1
  69. #define MEASURE_SIG_EN 0x1
  70. #define VALID_SIG_EN (0x1 << 8)
  71. #define DE_GLITCH_2 (0x2 << 29)
  72. #define START_SENSE (0x1 << 12)
  73. #define TSC_DISABLE (0x1 << 16)
  74. #define DETECT_MODE 0x2
  75. struct imx6ul_tsc {
  76. struct device *dev;
  77. struct input_dev *input;
  78. void __iomem *tsc_regs;
  79. void __iomem *adc_regs;
  80. struct clk *tsc_clk;
  81. struct clk *adc_clk;
  82. struct gpio_desc *xnur_gpio;
  83. u32 measure_delay_time;
  84. u32 pre_charge_time;
  85. bool average_enable;
  86. u32 average_select;
  87. struct completion completion;
  88. };
  89. /*
  90. * TSC module need ADC to get the measure value. So
  91. * before config TSC, we should initialize ADC module.
  92. */
  93. static int imx6ul_adc_init(struct imx6ul_tsc *tsc)
  94. {
  95. u32 adc_hc = 0;
  96. u32 adc_gc;
  97. u32 adc_gs;
  98. u32 adc_cfg;
  99. unsigned long timeout;
  100. reinit_completion(&tsc->completion);
  101. adc_cfg = readl(tsc->adc_regs + REG_ADC_CFG);
  102. adc_cfg &= ~(ADC_CONV_MODE_MASK | ADC_INPUT_CLK_MASK);
  103. adc_cfg |= ADC_12BIT_MODE | ADC_IPG_CLK;
  104. adc_cfg &= ~(ADC_CLK_DIV_MASK | ADC_SAMPLE_MODE_MASK);
  105. adc_cfg |= ADC_CLK_DIV_8 | ADC_SHORT_SAMPLE_MODE;
  106. if (tsc->average_enable) {
  107. adc_cfg &= ~ADC_AVGS_MASK;
  108. adc_cfg |= (tsc->average_select) << ADC_AVGS_SHIFT;
  109. }
  110. adc_cfg &= ~ADC_HARDWARE_TRIGGER;
  111. writel(adc_cfg, tsc->adc_regs + REG_ADC_CFG);
  112. /* enable calibration interrupt */
  113. adc_hc |= ADC_AIEN;
  114. adc_hc |= ADC_CONV_DISABLE;
  115. writel(adc_hc, tsc->adc_regs + REG_ADC_HC0);
  116. /* start ADC calibration */
  117. adc_gc = readl(tsc->adc_regs + REG_ADC_GC);
  118. adc_gc |= ADC_CAL;
  119. if (tsc->average_enable)
  120. adc_gc |= ADC_AVGE;
  121. writel(adc_gc, tsc->adc_regs + REG_ADC_GC);
  122. timeout = wait_for_completion_timeout
  123. (&tsc->completion, ADC_TIMEOUT);
  124. if (timeout == 0) {
  125. dev_err(tsc->dev, "Timeout for adc calibration\n");
  126. return -ETIMEDOUT;
  127. }
  128. adc_gs = readl(tsc->adc_regs + REG_ADC_GS);
  129. if (adc_gs & ADC_CALF) {
  130. dev_err(tsc->dev, "ADC calibration failed\n");
  131. return -EINVAL;
  132. }
  133. /* TSC need the ADC work in hardware trigger */
  134. adc_cfg = readl(tsc->adc_regs + REG_ADC_CFG);
  135. adc_cfg |= ADC_HARDWARE_TRIGGER;
  136. writel(adc_cfg, tsc->adc_regs + REG_ADC_CFG);
  137. return 0;
  138. }
  139. /*
  140. * This is a TSC workaround. Currently TSC misconnect two
  141. * ADC channels, this function remap channel configure for
  142. * hardware trigger.
  143. */
  144. static void imx6ul_tsc_channel_config(struct imx6ul_tsc *tsc)
  145. {
  146. u32 adc_hc0, adc_hc1, adc_hc2, adc_hc3, adc_hc4;
  147. adc_hc0 = DISABLE_CONVERSION_INT;
  148. writel(adc_hc0, tsc->adc_regs + REG_ADC_HC0);
  149. adc_hc1 = DISABLE_CONVERSION_INT | SELECT_CHANNEL_4;
  150. writel(adc_hc1, tsc->adc_regs + REG_ADC_HC1);
  151. adc_hc2 = DISABLE_CONVERSION_INT;
  152. writel(adc_hc2, tsc->adc_regs + REG_ADC_HC2);
  153. adc_hc3 = DISABLE_CONVERSION_INT | SELECT_CHANNEL_1;
  154. writel(adc_hc3, tsc->adc_regs + REG_ADC_HC3);
  155. adc_hc4 = DISABLE_CONVERSION_INT;
  156. writel(adc_hc4, tsc->adc_regs + REG_ADC_HC4);
  157. }
  158. /*
  159. * TSC setting, confige the pre-charge time and measure delay time.
  160. * different touch screen may need different pre-charge time and
  161. * measure delay time.
  162. */
  163. static void imx6ul_tsc_set(struct imx6ul_tsc *tsc)
  164. {
  165. u32 basic_setting = 0;
  166. u32 start;
  167. basic_setting |= tsc->measure_delay_time << 8;
  168. basic_setting |= DETECT_4_WIRE_MODE | AUTO_MEASURE;
  169. writel(basic_setting, tsc->tsc_regs + REG_TSC_BASIC_SETING);
  170. writel(DE_GLITCH_2, tsc->tsc_regs + REG_TSC_DEBUG_MODE2);
  171. writel(tsc->pre_charge_time, tsc->tsc_regs + REG_TSC_PRE_CHARGE_TIME);
  172. writel(MEASURE_INT_EN, tsc->tsc_regs + REG_TSC_INT_EN);
  173. writel(MEASURE_SIG_EN | VALID_SIG_EN,
  174. tsc->tsc_regs + REG_TSC_INT_SIG_EN);
  175. /* start sense detection */
  176. start = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
  177. start |= START_SENSE;
  178. start &= ~TSC_DISABLE;
  179. writel(start, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
  180. }
  181. static int imx6ul_tsc_init(struct imx6ul_tsc *tsc)
  182. {
  183. int err;
  184. err = imx6ul_adc_init(tsc);
  185. if (err)
  186. return err;
  187. imx6ul_tsc_channel_config(tsc);
  188. imx6ul_tsc_set(tsc);
  189. return 0;
  190. }
  191. static void imx6ul_tsc_disable(struct imx6ul_tsc *tsc)
  192. {
  193. u32 tsc_flow;
  194. u32 adc_cfg;
  195. /* TSC controller enters to idle status */
  196. tsc_flow = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
  197. tsc_flow |= TSC_DISABLE;
  198. writel(tsc_flow, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
  199. /* ADC controller enters to stop mode */
  200. adc_cfg = readl(tsc->adc_regs + REG_ADC_HC0);
  201. adc_cfg |= ADC_CONV_DISABLE;
  202. writel(adc_cfg, tsc->adc_regs + REG_ADC_HC0);
  203. }
  204. /* Delay some time (max 2ms), wait the pre-charge done. */
  205. static bool tsc_wait_detect_mode(struct imx6ul_tsc *tsc)
  206. {
  207. unsigned long timeout = jiffies + msecs_to_jiffies(2);
  208. u32 state_machine;
  209. u32 debug_mode2;
  210. do {
  211. if (time_after(jiffies, timeout))
  212. return false;
  213. usleep_range(200, 400);
  214. debug_mode2 = readl(tsc->tsc_regs + REG_TSC_DEBUG_MODE2);
  215. state_machine = (debug_mode2 >> 20) & 0x7;
  216. } while (state_machine != DETECT_MODE);
  217. usleep_range(200, 400);
  218. return true;
  219. }
  220. static irqreturn_t tsc_irq_fn(int irq, void *dev_id)
  221. {
  222. struct imx6ul_tsc *tsc = dev_id;
  223. u32 status;
  224. u32 value;
  225. u32 x, y;
  226. u32 start;
  227. status = readl(tsc->tsc_regs + REG_TSC_INT_STATUS);
  228. /* write 1 to clear the bit measure-signal */
  229. writel(MEASURE_SIGNAL | DETECT_SIGNAL,
  230. tsc->tsc_regs + REG_TSC_INT_STATUS);
  231. /* It's a HW self-clean bit. Set this bit and start sense detection */
  232. start = readl(tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
  233. start |= START_SENSE;
  234. writel(start, tsc->tsc_regs + REG_TSC_FLOW_CONTROL);
  235. if (status & MEASURE_SIGNAL) {
  236. value = readl(tsc->tsc_regs + REG_TSC_MEASURE_VALUE);
  237. x = (value >> 16) & 0x0fff;
  238. y = value & 0x0fff;
  239. /*
  240. * In detect mode, we can get the xnur gpio value,
  241. * otherwise assume contact is stiull active.
  242. */
  243. if (!tsc_wait_detect_mode(tsc) ||
  244. gpiod_get_value_cansleep(tsc->xnur_gpio)) {
  245. input_report_key(tsc->input, BTN_TOUCH, 1);
  246. input_report_abs(tsc->input, ABS_X, x);
  247. input_report_abs(tsc->input, ABS_Y, y);
  248. } else {
  249. input_report_key(tsc->input, BTN_TOUCH, 0);
  250. }
  251. input_sync(tsc->input);
  252. }
  253. return IRQ_HANDLED;
  254. }
  255. static irqreturn_t adc_irq_fn(int irq, void *dev_id)
  256. {
  257. struct imx6ul_tsc *tsc = dev_id;
  258. u32 coco;
  259. coco = readl(tsc->adc_regs + REG_ADC_HS);
  260. if (coco & 0x01) {
  261. readl(tsc->adc_regs + REG_ADC_R0);
  262. complete(&tsc->completion);
  263. }
  264. return IRQ_HANDLED;
  265. }
  266. static int imx6ul_tsc_start(struct imx6ul_tsc *tsc)
  267. {
  268. int err;
  269. err = clk_prepare_enable(tsc->adc_clk);
  270. if (err) {
  271. dev_err(tsc->dev,
  272. "Could not prepare or enable the adc clock: %d\n",
  273. err);
  274. return err;
  275. }
  276. err = clk_prepare_enable(tsc->tsc_clk);
  277. if (err) {
  278. dev_err(tsc->dev,
  279. "Could not prepare or enable the tsc clock: %d\n",
  280. err);
  281. goto disable_adc_clk;
  282. }
  283. err = imx6ul_tsc_init(tsc);
  284. if (err)
  285. goto disable_tsc_clk;
  286. return 0;
  287. disable_tsc_clk:
  288. clk_disable_unprepare(tsc->tsc_clk);
  289. disable_adc_clk:
  290. clk_disable_unprepare(tsc->adc_clk);
  291. return err;
  292. }
  293. static void imx6ul_tsc_stop(struct imx6ul_tsc *tsc)
  294. {
  295. imx6ul_tsc_disable(tsc);
  296. clk_disable_unprepare(tsc->tsc_clk);
  297. clk_disable_unprepare(tsc->adc_clk);
  298. }
  299. static int imx6ul_tsc_open(struct input_dev *input_dev)
  300. {
  301. struct imx6ul_tsc *tsc = input_get_drvdata(input_dev);
  302. return imx6ul_tsc_start(tsc);
  303. }
  304. static void imx6ul_tsc_close(struct input_dev *input_dev)
  305. {
  306. struct imx6ul_tsc *tsc = input_get_drvdata(input_dev);
  307. imx6ul_tsc_stop(tsc);
  308. }
  309. static int imx6ul_tsc_probe(struct platform_device *pdev)
  310. {
  311. struct device_node *np = pdev->dev.of_node;
  312. struct imx6ul_tsc *tsc;
  313. struct input_dev *input_dev;
  314. int err;
  315. int tsc_irq;
  316. int adc_irq;
  317. u32 average_samples;
  318. tsc = devm_kzalloc(&pdev->dev, sizeof(*tsc), GFP_KERNEL);
  319. if (!tsc)
  320. return -ENOMEM;
  321. input_dev = devm_input_allocate_device(&pdev->dev);
  322. if (!input_dev)
  323. return -ENOMEM;
  324. input_dev->name = "iMX6UL Touchscreen Controller";
  325. input_dev->id.bustype = BUS_HOST;
  326. input_dev->open = imx6ul_tsc_open;
  327. input_dev->close = imx6ul_tsc_close;
  328. input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
  329. input_set_abs_params(input_dev, ABS_X, 0, 0xFFF, 0, 0);
  330. input_set_abs_params(input_dev, ABS_Y, 0, 0xFFF, 0, 0);
  331. input_set_drvdata(input_dev, tsc);
  332. tsc->dev = &pdev->dev;
  333. tsc->input = input_dev;
  334. init_completion(&tsc->completion);
  335. tsc->xnur_gpio = devm_gpiod_get(&pdev->dev, "xnur", GPIOD_IN);
  336. if (IS_ERR(tsc->xnur_gpio)) {
  337. err = PTR_ERR(tsc->xnur_gpio);
  338. dev_err(&pdev->dev,
  339. "failed to request GPIO tsc_X- (xnur): %d\n", err);
  340. return err;
  341. }
  342. tsc->tsc_regs = devm_platform_ioremap_resource(pdev, 0);
  343. if (IS_ERR(tsc->tsc_regs)) {
  344. err = PTR_ERR(tsc->tsc_regs);
  345. dev_err(&pdev->dev, "failed to remap tsc memory: %d\n", err);
  346. return err;
  347. }
  348. tsc->adc_regs = devm_platform_ioremap_resource(pdev, 1);
  349. if (IS_ERR(tsc->adc_regs)) {
  350. err = PTR_ERR(tsc->adc_regs);
  351. dev_err(&pdev->dev, "failed to remap adc memory: %d\n", err);
  352. return err;
  353. }
  354. tsc->tsc_clk = devm_clk_get(&pdev->dev, "tsc");
  355. if (IS_ERR(tsc->tsc_clk)) {
  356. err = PTR_ERR(tsc->tsc_clk);
  357. dev_err(&pdev->dev, "failed getting tsc clock: %d\n", err);
  358. return err;
  359. }
  360. tsc->adc_clk = devm_clk_get(&pdev->dev, "adc");
  361. if (IS_ERR(tsc->adc_clk)) {
  362. err = PTR_ERR(tsc->adc_clk);
  363. dev_err(&pdev->dev, "failed getting adc clock: %d\n", err);
  364. return err;
  365. }
  366. tsc_irq = platform_get_irq(pdev, 0);
  367. if (tsc_irq < 0)
  368. return tsc_irq;
  369. adc_irq = platform_get_irq(pdev, 1);
  370. if (adc_irq < 0)
  371. return adc_irq;
  372. err = devm_request_threaded_irq(tsc->dev, tsc_irq,
  373. NULL, tsc_irq_fn, IRQF_ONESHOT,
  374. dev_name(&pdev->dev), tsc);
  375. if (err) {
  376. dev_err(&pdev->dev,
  377. "failed requesting tsc irq %d: %d\n",
  378. tsc_irq, err);
  379. return err;
  380. }
  381. err = devm_request_irq(tsc->dev, adc_irq, adc_irq_fn, 0,
  382. dev_name(&pdev->dev), tsc);
  383. if (err) {
  384. dev_err(&pdev->dev,
  385. "failed requesting adc irq %d: %d\n",
  386. adc_irq, err);
  387. return err;
  388. }
  389. err = of_property_read_u32(np, "measure-delay-time",
  390. &tsc->measure_delay_time);
  391. if (err)
  392. tsc->measure_delay_time = 0xffff;
  393. err = of_property_read_u32(np, "pre-charge-time",
  394. &tsc->pre_charge_time);
  395. if (err)
  396. tsc->pre_charge_time = 0xfff;
  397. err = of_property_read_u32(np, "touchscreen-average-samples",
  398. &average_samples);
  399. if (err)
  400. average_samples = 1;
  401. switch (average_samples) {
  402. case 1:
  403. tsc->average_enable = false;
  404. tsc->average_select = 0; /* value unused; initialize anyway */
  405. break;
  406. case 4:
  407. case 8:
  408. case 16:
  409. case 32:
  410. tsc->average_enable = true;
  411. tsc->average_select = ilog2(average_samples) - 2;
  412. break;
  413. default:
  414. dev_err(&pdev->dev,
  415. "touchscreen-average-samples (%u) must be 1, 4, 8, 16 or 32\n",
  416. average_samples);
  417. return -EINVAL;
  418. }
  419. err = input_register_device(tsc->input);
  420. if (err) {
  421. dev_err(&pdev->dev,
  422. "failed to register input device: %d\n", err);
  423. return err;
  424. }
  425. platform_set_drvdata(pdev, tsc);
  426. return 0;
  427. }
  428. static int __maybe_unused imx6ul_tsc_suspend(struct device *dev)
  429. {
  430. struct platform_device *pdev = to_platform_device(dev);
  431. struct imx6ul_tsc *tsc = platform_get_drvdata(pdev);
  432. struct input_dev *input_dev = tsc->input;
  433. mutex_lock(&input_dev->mutex);
  434. if (input_device_enabled(input_dev))
  435. imx6ul_tsc_stop(tsc);
  436. mutex_unlock(&input_dev->mutex);
  437. return 0;
  438. }
  439. static int __maybe_unused imx6ul_tsc_resume(struct device *dev)
  440. {
  441. struct platform_device *pdev = to_platform_device(dev);
  442. struct imx6ul_tsc *tsc = platform_get_drvdata(pdev);
  443. struct input_dev *input_dev = tsc->input;
  444. int retval = 0;
  445. mutex_lock(&input_dev->mutex);
  446. if (input_device_enabled(input_dev))
  447. retval = imx6ul_tsc_start(tsc);
  448. mutex_unlock(&input_dev->mutex);
  449. return retval;
  450. }
  451. static SIMPLE_DEV_PM_OPS(imx6ul_tsc_pm_ops,
  452. imx6ul_tsc_suspend, imx6ul_tsc_resume);
  453. static const struct of_device_id imx6ul_tsc_match[] = {
  454. { .compatible = "fsl,imx6ul-tsc", },
  455. { /* sentinel */ }
  456. };
  457. MODULE_DEVICE_TABLE(of, imx6ul_tsc_match);
  458. static struct platform_driver imx6ul_tsc_driver = {
  459. .driver = {
  460. .name = "imx6ul-tsc",
  461. .of_match_table = imx6ul_tsc_match,
  462. .pm = &imx6ul_tsc_pm_ops,
  463. },
  464. .probe = imx6ul_tsc_probe,
  465. };
  466. module_platform_driver(imx6ul_tsc_driver);
  467. MODULE_AUTHOR("Haibo Chen <[email protected]>");
  468. MODULE_DESCRIPTION("Freescale i.MX6UL Touchscreen controller driver");
  469. MODULE_LICENSE("GPL v2");