s3c2410_ts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Samsung S3C24XX touchscreen driver
  4. *
  5. * Copyright 2004 Arnaud Patard <[email protected]>
  6. * Copyright 2008 Ben Dooks <[email protected]>
  7. * Copyright 2009 Simtec Electronics <[email protected]>
  8. *
  9. * Additional work by Herbert Pötzl <[email protected]> and
  10. * Harald Welte <[email protected]>
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/input.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/soc/samsung/s3c-adc.h>
  22. #include <linux/platform_data/touchscreen-s3c2410.h>
  23. #define S3C2410_ADCCON (0x00)
  24. #define S3C2410_ADCTSC (0x04)
  25. #define S3C2410_ADCDLY (0x08)
  26. #define S3C2410_ADCDAT0 (0x0C)
  27. #define S3C2410_ADCDAT1 (0x10)
  28. #define S3C64XX_ADCUPDN (0x14)
  29. #define S3C2443_ADCMUX (0x18)
  30. #define S3C64XX_ADCCLRINT (0x18)
  31. #define S5P_ADCMUX (0x1C)
  32. #define S3C64XX_ADCCLRINTPNDNUP (0x20)
  33. /* ADCTSC Register Bits */
  34. #define S3C2443_ADCTSC_UD_SEN (1 << 8)
  35. #define S3C2410_ADCTSC_YM_SEN (1<<7)
  36. #define S3C2410_ADCTSC_YP_SEN (1<<6)
  37. #define S3C2410_ADCTSC_XM_SEN (1<<5)
  38. #define S3C2410_ADCTSC_XP_SEN (1<<4)
  39. #define S3C2410_ADCTSC_PULL_UP_DISABLE (1<<3)
  40. #define S3C2410_ADCTSC_AUTO_PST (1<<2)
  41. #define S3C2410_ADCTSC_XY_PST(x) (((x)&0x3)<<0)
  42. /* ADCDAT0 Bits */
  43. #define S3C2410_ADCDAT0_UPDOWN (1<<15)
  44. #define S3C2410_ADCDAT0_AUTO_PST (1<<14)
  45. #define S3C2410_ADCDAT0_XY_PST (0x3<<12)
  46. #define S3C2410_ADCDAT0_XPDATA_MASK (0x03FF)
  47. /* ADCDAT1 Bits */
  48. #define S3C2410_ADCDAT1_UPDOWN (1<<15)
  49. #define S3C2410_ADCDAT1_AUTO_PST (1<<14)
  50. #define S3C2410_ADCDAT1_XY_PST (0x3<<12)
  51. #define S3C2410_ADCDAT1_YPDATA_MASK (0x03FF)
  52. #define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))
  53. #define INT_DOWN (0)
  54. #define INT_UP (1 << 8)
  55. #define WAIT4INT (S3C2410_ADCTSC_YM_SEN | \
  56. S3C2410_ADCTSC_YP_SEN | \
  57. S3C2410_ADCTSC_XP_SEN | \
  58. S3C2410_ADCTSC_XY_PST(3))
  59. #define AUTOPST (S3C2410_ADCTSC_YM_SEN | \
  60. S3C2410_ADCTSC_YP_SEN | \
  61. S3C2410_ADCTSC_XP_SEN | \
  62. S3C2410_ADCTSC_AUTO_PST | \
  63. S3C2410_ADCTSC_XY_PST(0))
  64. #define FEAT_PEN_IRQ (1 << 0) /* HAS ADCCLRINTPNDNUP */
  65. /* Per-touchscreen data. */
  66. /**
  67. * struct s3c2410ts - driver touchscreen state.
  68. * @client: The ADC client we registered with the core driver.
  69. * @dev: The device we are bound to.
  70. * @input: The input device we registered with the input subsystem.
  71. * @clock: The clock for the adc.
  72. * @io: Pointer to the IO base.
  73. * @xp: The accumulated X position data.
  74. * @yp: The accumulated Y position data.
  75. * @irq_tc: The interrupt number for pen up/down interrupt
  76. * @count: The number of samples collected.
  77. * @shift: The log2 of the maximum count to read in one go.
  78. * @features: The features supported by the TSADC MOdule.
  79. */
  80. struct s3c2410ts {
  81. struct s3c_adc_client *client;
  82. struct device *dev;
  83. struct input_dev *input;
  84. struct clk *clock;
  85. void __iomem *io;
  86. unsigned long xp;
  87. unsigned long yp;
  88. int irq_tc;
  89. int count;
  90. int shift;
  91. int features;
  92. };
  93. static struct s3c2410ts ts;
  94. /**
  95. * get_down - return the down state of the pen
  96. * @data0: The data read from ADCDAT0 register.
  97. * @data1: The data read from ADCDAT1 register.
  98. *
  99. * Return non-zero if both readings show that the pen is down.
  100. */
  101. static inline bool get_down(unsigned long data0, unsigned long data1)
  102. {
  103. /* returns true if both data values show stylus down */
  104. return (!(data0 & S3C2410_ADCDAT0_UPDOWN) &&
  105. !(data1 & S3C2410_ADCDAT0_UPDOWN));
  106. }
  107. static void touch_timer_fire(struct timer_list *unused)
  108. {
  109. unsigned long data0;
  110. unsigned long data1;
  111. bool down;
  112. data0 = readl(ts.io + S3C2410_ADCDAT0);
  113. data1 = readl(ts.io + S3C2410_ADCDAT1);
  114. down = get_down(data0, data1);
  115. if (down) {
  116. if (ts.count == (1 << ts.shift)) {
  117. ts.xp >>= ts.shift;
  118. ts.yp >>= ts.shift;
  119. dev_dbg(ts.dev, "%s: X=%lu, Y=%lu, count=%d\n",
  120. __func__, ts.xp, ts.yp, ts.count);
  121. input_report_abs(ts.input, ABS_X, ts.xp);
  122. input_report_abs(ts.input, ABS_Y, ts.yp);
  123. input_report_key(ts.input, BTN_TOUCH, 1);
  124. input_sync(ts.input);
  125. ts.xp = 0;
  126. ts.yp = 0;
  127. ts.count = 0;
  128. }
  129. s3c_adc_start(ts.client, 0, 1 << ts.shift);
  130. } else {
  131. ts.xp = 0;
  132. ts.yp = 0;
  133. ts.count = 0;
  134. input_report_key(ts.input, BTN_TOUCH, 0);
  135. input_sync(ts.input);
  136. writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
  137. }
  138. }
  139. static DEFINE_TIMER(touch_timer, touch_timer_fire);
  140. /**
  141. * stylus_irq - touchscreen stylus event interrupt
  142. * @irq: The interrupt number
  143. * @dev_id: The device ID.
  144. *
  145. * Called when the IRQ_TC is fired for a pen up or down event.
  146. */
  147. static irqreturn_t stylus_irq(int irq, void *dev_id)
  148. {
  149. unsigned long data0;
  150. unsigned long data1;
  151. bool down;
  152. data0 = readl(ts.io + S3C2410_ADCDAT0);
  153. data1 = readl(ts.io + S3C2410_ADCDAT1);
  154. down = get_down(data0, data1);
  155. /* TODO we should never get an interrupt with down set while
  156. * the timer is running, but maybe we ought to verify that the
  157. * timer isn't running anyways. */
  158. if (down)
  159. s3c_adc_start(ts.client, 0, 1 << ts.shift);
  160. else
  161. dev_dbg(ts.dev, "%s: count=%d\n", __func__, ts.count);
  162. if (ts.features & FEAT_PEN_IRQ) {
  163. /* Clear pen down/up interrupt */
  164. writel(0x0, ts.io + S3C64XX_ADCCLRINTPNDNUP);
  165. }
  166. return IRQ_HANDLED;
  167. }
  168. /**
  169. * s3c24xx_ts_conversion - ADC conversion callback
  170. * @client: The client that was registered with the ADC core.
  171. * @data0: The reading from ADCDAT0.
  172. * @data1: The reading from ADCDAT1.
  173. * @left: The number of samples left.
  174. *
  175. * Called when a conversion has finished.
  176. */
  177. static void s3c24xx_ts_conversion(struct s3c_adc_client *client,
  178. unsigned data0, unsigned data1,
  179. unsigned *left)
  180. {
  181. dev_dbg(ts.dev, "%s: %d,%d\n", __func__, data0, data1);
  182. ts.xp += data0;
  183. ts.yp += data1;
  184. ts.count++;
  185. /* From tests, it seems that it is unlikely to get a pen-up
  186. * event during the conversion process which means we can
  187. * ignore any pen-up events with less than the requisite
  188. * count done.
  189. *
  190. * In several thousand conversions, no pen-ups where detected
  191. * before count completed.
  192. */
  193. }
  194. /**
  195. * s3c24xx_ts_select - ADC selection callback.
  196. * @client: The client that was registered with the ADC core.
  197. * @select: The reason for select.
  198. *
  199. * Called when the ADC core selects (or deslects) us as a client.
  200. */
  201. static void s3c24xx_ts_select(struct s3c_adc_client *client, unsigned select)
  202. {
  203. if (select) {
  204. writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,
  205. ts.io + S3C2410_ADCTSC);
  206. } else {
  207. mod_timer(&touch_timer, jiffies+1);
  208. writel(WAIT4INT | INT_UP, ts.io + S3C2410_ADCTSC);
  209. }
  210. }
  211. /**
  212. * s3c2410ts_probe - device core probe entry point
  213. * @pdev: The device we are being bound to.
  214. *
  215. * Initialise, find and allocate any resources we need to run and then
  216. * register with the ADC and input systems.
  217. */
  218. static int s3c2410ts_probe(struct platform_device *pdev)
  219. {
  220. struct s3c2410_ts_mach_info *info;
  221. struct device *dev = &pdev->dev;
  222. struct input_dev *input_dev;
  223. struct resource *res;
  224. int ret = -EINVAL;
  225. /* Initialise input stuff */
  226. memset(&ts, 0, sizeof(struct s3c2410ts));
  227. ts.dev = dev;
  228. info = dev_get_platdata(dev);
  229. if (!info) {
  230. dev_err(dev, "no platform data, cannot attach\n");
  231. return -EINVAL;
  232. }
  233. dev_dbg(dev, "initialising touchscreen\n");
  234. ts.clock = clk_get(dev, "adc");
  235. if (IS_ERR(ts.clock)) {
  236. dev_err(dev, "cannot get adc clock source\n");
  237. return -ENOENT;
  238. }
  239. ret = clk_prepare_enable(ts.clock);
  240. if (ret) {
  241. dev_err(dev, "Failed! to enabled clocks\n");
  242. goto err_clk_get;
  243. }
  244. dev_dbg(dev, "got and enabled clocks\n");
  245. ts.irq_tc = ret = platform_get_irq(pdev, 0);
  246. if (ret < 0) {
  247. dev_err(dev, "no resource for interrupt\n");
  248. goto err_clk;
  249. }
  250. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  251. if (!res) {
  252. dev_err(dev, "no resource for registers\n");
  253. ret = -ENOENT;
  254. goto err_clk;
  255. }
  256. ts.io = ioremap(res->start, resource_size(res));
  257. if (ts.io == NULL) {
  258. dev_err(dev, "cannot map registers\n");
  259. ret = -ENOMEM;
  260. goto err_clk;
  261. }
  262. /* inititalise the gpio */
  263. if (info->cfg_gpio)
  264. info->cfg_gpio(to_platform_device(ts.dev));
  265. ts.client = s3c_adc_register(pdev, s3c24xx_ts_select,
  266. s3c24xx_ts_conversion, 1);
  267. if (IS_ERR(ts.client)) {
  268. dev_err(dev, "failed to register adc client\n");
  269. ret = PTR_ERR(ts.client);
  270. goto err_iomap;
  271. }
  272. /* Initialise registers */
  273. if ((info->delay & 0xffff) > 0)
  274. writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
  275. writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
  276. input_dev = input_allocate_device();
  277. if (!input_dev) {
  278. dev_err(dev, "Unable to allocate the input device !!\n");
  279. ret = -ENOMEM;
  280. goto err_iomap;
  281. }
  282. ts.input = input_dev;
  283. ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  284. ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  285. input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0);
  286. input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0);
  287. ts.input->name = "S3C24XX TouchScreen";
  288. ts.input->id.bustype = BUS_HOST;
  289. ts.input->id.vendor = 0xDEAD;
  290. ts.input->id.product = 0xBEEF;
  291. ts.input->id.version = 0x0102;
  292. ts.shift = info->oversampling_shift;
  293. ts.features = platform_get_device_id(pdev)->driver_data;
  294. ret = request_irq(ts.irq_tc, stylus_irq, 0,
  295. "s3c2410_ts_pen", ts.input);
  296. if (ret) {
  297. dev_err(dev, "cannot get TC interrupt\n");
  298. goto err_inputdev;
  299. }
  300. dev_info(dev, "driver attached, registering input device\n");
  301. /* All went ok, so register to the input system */
  302. ret = input_register_device(ts.input);
  303. if (ret < 0) {
  304. dev_err(dev, "failed to register input device\n");
  305. ret = -EIO;
  306. goto err_tcirq;
  307. }
  308. return 0;
  309. err_tcirq:
  310. free_irq(ts.irq_tc, ts.input);
  311. err_inputdev:
  312. input_free_device(ts.input);
  313. err_iomap:
  314. iounmap(ts.io);
  315. err_clk:
  316. clk_disable_unprepare(ts.clock);
  317. del_timer_sync(&touch_timer);
  318. err_clk_get:
  319. clk_put(ts.clock);
  320. return ret;
  321. }
  322. /**
  323. * s3c2410ts_remove - device core removal entry point
  324. * @pdev: The device we are being removed from.
  325. *
  326. * Free up our state ready to be removed.
  327. */
  328. static int s3c2410ts_remove(struct platform_device *pdev)
  329. {
  330. free_irq(ts.irq_tc, ts.input);
  331. del_timer_sync(&touch_timer);
  332. clk_disable_unprepare(ts.clock);
  333. clk_put(ts.clock);
  334. input_unregister_device(ts.input);
  335. iounmap(ts.io);
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM
  339. static int s3c2410ts_suspend(struct device *dev)
  340. {
  341. writel(TSC_SLEEP, ts.io + S3C2410_ADCTSC);
  342. disable_irq(ts.irq_tc);
  343. clk_disable(ts.clock);
  344. return 0;
  345. }
  346. static int s3c2410ts_resume(struct device *dev)
  347. {
  348. struct platform_device *pdev = to_platform_device(dev);
  349. struct s3c2410_ts_mach_info *info = dev_get_platdata(&pdev->dev);
  350. clk_enable(ts.clock);
  351. enable_irq(ts.irq_tc);
  352. /* Initialise registers */
  353. if ((info->delay & 0xffff) > 0)
  354. writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);
  355. writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);
  356. return 0;
  357. }
  358. static const struct dev_pm_ops s3c_ts_pmops = {
  359. .suspend = s3c2410ts_suspend,
  360. .resume = s3c2410ts_resume,
  361. };
  362. #endif
  363. static const struct platform_device_id s3cts_driver_ids[] = {
  364. { "s3c2410-ts", 0 },
  365. { "s3c2440-ts", 0 },
  366. { "s3c64xx-ts", FEAT_PEN_IRQ },
  367. { }
  368. };
  369. MODULE_DEVICE_TABLE(platform, s3cts_driver_ids);
  370. static struct platform_driver s3c_ts_driver = {
  371. .driver = {
  372. .name = "samsung-ts",
  373. #ifdef CONFIG_PM
  374. .pm = &s3c_ts_pmops,
  375. #endif
  376. },
  377. .id_table = s3cts_driver_ids,
  378. .probe = s3c2410ts_probe,
  379. .remove = s3c2410ts_remove,
  380. };
  381. module_platform_driver(s3c_ts_driver);
  382. MODULE_AUTHOR("Arnaud Patard <[email protected]>, "
  383. "Ben Dooks <[email protected]>, "
  384. "Simtec Electronics <[email protected]>");
  385. MODULE_DESCRIPTION("S3C24XX Touchscreen driver");
  386. MODULE_LICENSE("GPL v2");