s3c2410_ts.c 11 KB

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