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