2 * wm97xx-core.c -- Touch screen driver core for Wolfson WM9705, WM9712
3 * and WM9713 AC97 Codecs.
5 * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood
7 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8 * Parts Copyright : Ian Molton <spyro@f2s.com>
9 * Andrew Zabolotny <zap@homelink.ru>
10 * Russell King <rmk@arm.linux.org.uk>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
20 * - supports WM9705, WM9712, WM9713
22 * - continuous mode (arch-dependent)
23 * - adjustable rpu/dpp settings
24 * - adjustable pressure current
25 * - adjustable sample settle delay
26 * - 4 and 5 wire touchscreens (5 wire is WM9712 only)
27 * - pen down detection
32 * - codec event notification
34 * - Support for async sampling control for noisy LCDs.
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/version.h>
41 #include <linux/kernel.h>
42 #include <linux/init.h>
43 #include <linux/delay.h>
44 #include <linux/string.h>
45 #include <linux/proc_fs.h>
47 #include <linux/interrupt.h>
48 #include <linux/bitops.h>
49 #include <linux/workqueue.h>
50 #include <linux/wm97xx.h>
51 #include <linux/uaccess.h>
54 #define TS_NAME "wm97xx"
55 #define WM_CORE_VERSION "1.00"
56 #define DEFAULT_PRESSURE 0xb0c0
60 * Touchscreen absolute values
62 * These parameters are used to help the input layer discard out of
63 * range readings and reduce jitter etc.
65 * o min, max:- indicate the min and max values your touch screen returns
66 * o fuzz:- use a higher number to reduce jitter
68 * The default values correspond to Mainstone II in QVGA mode
71 * Documentation/input/input-programming.txt for more details.
74 static int abs_x[3] = {350, 3900, 5};
75 module_param_array(abs_x, int, NULL, 0);
76 MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
78 static int abs_y[3] = {320, 3750, 40};
79 module_param_array(abs_y, int, NULL, 0);
80 MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
82 static int abs_p[3] = {0, 150, 4};
83 module_param_array(abs_p, int, NULL, 0);
84 MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
87 * wm97xx IO access, all IO locking done by AC97 layer
89 int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
92 return wm->ac97->bus->ops->read(wm->ac97, reg);
96 EXPORT_SYMBOL_GPL(wm97xx_reg_read);
98 void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
100 /* cache digitiser registers */
101 if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
102 wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
104 /* cache gpio regs */
105 if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
106 wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
113 wm->ac97->bus->ops->write(wm->ac97, reg, val);
115 EXPORT_SYMBOL_GPL(wm97xx_reg_write);
118 * wm97xx_read_aux_adc - Read the aux adc.
119 * @wm: wm97xx device.
120 * @adcsel: codec ADC to be read
122 * Reads the selected AUX ADC.
125 int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
127 int power_adc = 0, auxval;
131 mutex_lock(&wm->codec_mutex);
133 /* When the touchscreen is not in use, we may have to power up
134 * the AUX ADC before we can use sample the AUX inputs->
136 if (wm->id == WM9713_ID2 &&
137 (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
139 wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
142 /* Prepare the codec for AUX reading */
143 wm->codec->aux_prepare(wm);
145 /* Turn polling mode on to read AUX ADC */
146 wm->pen_probably_down = 1;
147 wm->codec->poll_sample(wm, adcsel, &auxval);
150 wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
152 wm->codec->dig_restore(wm);
154 wm->pen_probably_down = 0;
156 mutex_unlock(&wm->codec_mutex);
157 return auxval & 0xfff;
159 EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
162 * wm97xx_get_gpio - Get the status of a codec GPIO.
163 * @wm: wm97xx device.
166 * Get the status of a codec GPIO pin
169 enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
172 enum wm97xx_gpio_status ret;
174 mutex_lock(&wm->codec_mutex);
175 status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
178 ret = WM97XX_GPIO_HIGH;
180 ret = WM97XX_GPIO_LOW;
182 mutex_unlock(&wm->codec_mutex);
185 EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
188 * wm97xx_set_gpio - Set the status of a codec GPIO.
189 * @wm: wm97xx device.
193 * Set the status of a codec GPIO pin
196 void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
197 enum wm97xx_gpio_status status)
201 mutex_lock(&wm->codec_mutex);
202 reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
204 if (status & WM97XX_GPIO_HIGH)
209 if (wm->id == WM9712_ID2)
210 wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
212 wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
213 mutex_unlock(&wm->codec_mutex);
215 EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
218 * Codec GPIO pin configuration, this sets pin direction, polarity,
219 * stickyness and wake up.
221 void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
222 enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
223 enum wm97xx_gpio_wake wake)
227 mutex_lock(&wm->codec_mutex);
228 reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
230 if (pol == WM97XX_GPIO_POL_HIGH)
235 wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
236 reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
238 if (sticky == WM97XX_GPIO_STICKY)
243 wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
244 reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
246 if (wake == WM97XX_GPIO_WAKE)
251 wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
252 reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
254 if (dir == WM97XX_GPIO_IN)
259 wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
260 mutex_unlock(&wm->codec_mutex);
262 EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
265 * Handle a pen down interrupt.
267 static void wm97xx_pen_irq_worker(struct work_struct *work)
269 struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
270 int pen_was_down = wm->pen_is_down;
272 /* do we need to enable the touch panel reader */
273 if (wm->id == WM9705_ID2) {
274 if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
281 mutex_lock(&wm->codec_mutex);
282 status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
283 pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
285 if (WM97XX_GPIO_13 & pol & status) {
287 wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
291 wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
295 if (wm->id == WM9712_ID2)
296 wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
297 ~WM97XX_GPIO_13) << 1);
299 wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
301 mutex_unlock(&wm->codec_mutex);
304 /* If the system is not using continuous mode or it provides a
305 * pen down operation then we need to schedule polls while the
306 * pen is down. Otherwise the machine driver is responsible
307 * for scheduling reads.
309 if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
310 if (wm->pen_is_down && !pen_was_down) {
311 /* Data is not availiable immediately on pen down */
312 queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
315 /* Let ts_reader report the pen up for debounce. */
316 if (!wm->pen_is_down && pen_was_down)
320 if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
321 wm->mach_ops->acc_pen_up(wm);
323 wm->mach_ops->irq_enable(wm, 1);
327 * Codec PENDOWN irq handler
329 * We have to disable the codec interrupt in the handler because it
330 * can take upto 1ms to clear the interrupt source. We schedule a task
331 * in a work queue to do the actual interaction with the chip (it
332 * doesn't matter if we end up reenqueing it before it is executed
333 * since we don't touch the chip until it has run). The interrupt is
334 * then enabled again in the slow handler when the source has been
337 static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
339 struct wm97xx *wm = dev_id;
341 wm->mach_ops->irq_enable(wm, 0);
342 queue_work(wm->ts_workq, &wm->pen_event_work);
348 * initialise pen IRQ handler and workqueue
350 static int wm97xx_init_pen_irq(struct wm97xx *wm)
354 /* If an interrupt is supplied an IRQ enable operation must also be
356 BUG_ON(!wm->mach_ops->irq_enable);
358 if (request_irq(wm->pen_irq, wm97xx_pen_interrupt, IRQF_SHARED,
361 "Failed to register pen down interrupt, polling");
366 /* Configure GPIO as interrupt source on WM971x */
367 if (wm->id != WM9705_ID2) {
368 BUG_ON(!wm->mach_ops->irq_gpio);
369 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
370 wm97xx_reg_write(wm, AC97_MISC_AFE,
371 reg & ~(wm->mach_ops->irq_gpio));
372 reg = wm97xx_reg_read(wm, 0x5a);
373 wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
379 static int wm97xx_read_samples(struct wm97xx *wm)
381 struct wm97xx_data data;
384 mutex_lock(&wm->codec_mutex);
386 if (wm->mach_ops && wm->mach_ops->acc_enabled)
387 rc = wm->mach_ops->acc_pen_down(wm);
389 rc = wm->codec->poll_touch(wm, &data);
392 if (wm->pen_is_down) {
394 dev_dbg(wm->dev, "pen up\n");
395 input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
396 input_sync(wm->input_dev);
397 } else if (!(rc & RC_AGAIN)) {
398 /* We need high frequency updates only while
399 * pen is down, the user never will be able to
400 * touch screen faster than a few times per
401 * second... On the other hand, when the user
402 * is actively working with the touchscreen we
403 * don't want to lose the quick response. So we
404 * will slowly increase sleep time after the
405 * pen is up and quicky restore it to ~one task
406 * switch when pen is down again.
408 if (wm->ts_reader_interval < HZ / 10)
409 wm->ts_reader_interval++;
412 } else if (rc & RC_VALID) {
414 "pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
415 data.x >> 12, data.x & 0xfff, data.y >> 12,
416 data.y & 0xfff, data.p >> 12, data.p & 0xfff);
417 input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
418 input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
419 input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
420 input_sync(wm->input_dev);
422 wm->ts_reader_interval = wm->ts_reader_min_interval;
423 } else if (rc & RC_PENDOWN) {
424 dev_dbg(wm->dev, "pen down\n");
426 wm->ts_reader_interval = wm->ts_reader_min_interval;
429 mutex_unlock(&wm->codec_mutex);
434 * The touchscreen sample reader.
436 static void wm97xx_ts_reader(struct work_struct *work)
439 struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
444 rc = wm97xx_read_samples(wm);
445 } while (rc & RC_AGAIN);
447 if (wm->pen_is_down || !wm->pen_irq)
448 queue_delayed_work(wm->ts_workq, &wm->ts_reader,
449 wm->ts_reader_interval);
453 * wm97xx_ts_input_open - Open the touch screen input device.
454 * @idev: Input device to be opened.
456 * Called by the input sub system to open a wm97xx touchscreen device.
457 * Starts the touchscreen thread and touch digitiser.
459 static int wm97xx_ts_input_open(struct input_dev *idev)
461 struct wm97xx *wm = input_get_drvdata(idev);
463 wm->ts_workq = create_singlethread_workqueue("kwm97xx");
464 if (wm->ts_workq == NULL) {
466 "Failed to create workqueue\n");
470 /* start digitiser */
471 if (wm->mach_ops && wm->mach_ops->acc_enabled)
472 wm->codec->acc_enable(wm, 1);
473 wm->codec->dig_enable(wm, 1);
475 INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
476 INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
478 wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
479 if (wm->ts_reader_min_interval < 1)
480 wm->ts_reader_min_interval = 1;
481 wm->ts_reader_interval = wm->ts_reader_min_interval;
485 wm97xx_init_pen_irq(wm);
487 dev_err(wm->dev, "No IRQ specified\n");
489 /* If we either don't have an interrupt for pen down events or
490 * failed to acquire it then we need to poll.
492 if (wm->pen_irq == 0)
493 queue_delayed_work(wm->ts_workq, &wm->ts_reader,
494 wm->ts_reader_interval);
500 * wm97xx_ts_input_close - Close the touch screen input device.
501 * @idev: Input device to be closed.
503 * Called by the input sub system to close a wm97xx touchscreen
504 * device. Kills the touchscreen thread and stops the touch
508 static void wm97xx_ts_input_close(struct input_dev *idev)
510 struct wm97xx *wm = input_get_drvdata(idev);
514 /* Return the interrupt to GPIO usage (disabling it) */
515 if (wm->id != WM9705_ID2) {
516 BUG_ON(!wm->mach_ops->irq_gpio);
517 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
518 wm97xx_reg_write(wm, AC97_MISC_AFE,
519 reg | wm->mach_ops->irq_gpio);
522 free_irq(wm->pen_irq, wm);
527 /* Balance out interrupt disables/enables */
528 if (cancel_work_sync(&wm->pen_event_work))
529 wm->mach_ops->irq_enable(wm, 1);
531 /* ts_reader rearms itself so we need to explicitly stop it
532 * before we destroy the workqueue.
534 cancel_delayed_work_sync(&wm->ts_reader);
536 destroy_workqueue(wm->ts_workq);
539 wm->codec->dig_enable(wm, 0);
540 if (wm->mach_ops && wm->mach_ops->acc_enabled)
541 wm->codec->acc_enable(wm, 0);
544 static int wm97xx_probe(struct device *dev)
549 wm = kzalloc(sizeof(struct wm97xx), GFP_KERNEL);
552 mutex_init(&wm->codec_mutex);
555 dev->driver_data = wm;
556 wm->ac97 = to_ac97_t(dev);
558 /* check that we have a supported codec */
559 id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
560 if (id != WM97XX_ID1) {
561 dev_err(dev, "Device with vendor %04x is not a wm97xx\n", id);
566 wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
568 dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
570 switch (wm->id & 0xff) {
571 #ifdef CONFIG_TOUCHSCREEN_WM9705
573 wm->codec = &wm9705_codec;
576 #ifdef CONFIG_TOUCHSCREEN_WM9712
578 wm->codec = &wm9712_codec;
581 #ifdef CONFIG_TOUCHSCREEN_WM9713
583 wm->codec = &wm9713_codec;
587 dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
593 wm->input_dev = input_allocate_device();
594 if (wm->input_dev == NULL) {
599 /* set up touch configuration */
600 wm->input_dev->name = "wm97xx touchscreen";
601 wm->input_dev->open = wm97xx_ts_input_open;
602 wm->input_dev->close = wm97xx_ts_input_close;
603 set_bit(EV_ABS, wm->input_dev->evbit);
604 set_bit(ABS_X, wm->input_dev->absbit);
605 set_bit(ABS_Y, wm->input_dev->absbit);
606 set_bit(ABS_PRESSURE, wm->input_dev->absbit);
607 input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
609 input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
611 input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
613 input_set_drvdata(wm->input_dev, wm);
614 wm->input_dev->dev.parent = dev;
615 ret = input_register_device(wm->input_dev);
619 /* set up physical characteristics */
620 wm->codec->phy_init(wm);
622 /* load gpio cache */
623 wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
624 wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
625 wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
626 wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
627 wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
628 wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
630 /* register our battery device */
631 wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
632 if (!wm->battery_dev) {
636 platform_set_drvdata(wm->battery_dev, wm);
637 wm->battery_dev->dev.parent = dev;
638 ret = platform_device_add(wm->battery_dev);
642 /* register our extended touch device (for machine specific
644 wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
645 if (!wm->touch_dev) {
649 platform_set_drvdata(wm->touch_dev, wm);
650 wm->touch_dev->dev.parent = dev;
651 ret = platform_device_add(wm->touch_dev);
658 platform_device_put(wm->touch_dev);
660 platform_device_unregister(wm->battery_dev);
661 wm->battery_dev = NULL;
663 platform_device_put(wm->battery_dev);
665 input_unregister_device(wm->input_dev);
666 wm->input_dev = NULL;
668 input_free_device(wm->input_dev);
675 static int wm97xx_remove(struct device *dev)
677 struct wm97xx *wm = dev_get_drvdata(dev);
679 platform_device_unregister(wm->battery_dev);
680 platform_device_unregister(wm->touch_dev);
681 input_unregister_device(wm->input_dev);
688 static int wm97xx_suspend(struct device *dev, pm_message_t state)
690 struct wm97xx *wm = dev_get_drvdata(dev);
692 if (wm->input_dev->users)
693 cancel_delayed_work_sync(&wm->ts_reader);
698 static int wm97xx_resume(struct device *dev)
700 struct wm97xx *wm = dev_get_drvdata(dev);
702 /* restore digitiser and gpios */
703 if (wm->id == WM9713_ID2) {
704 wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
705 wm97xx_reg_write(wm, 0x5a, wm->misc);
706 if (wm->input_dev->users) {
708 reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
709 wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
713 wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
714 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
716 wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
717 wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
718 wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
719 wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
720 wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
721 wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
723 if (wm->input_dev->users && !wm->pen_irq) {
724 wm->ts_reader_interval = wm->ts_reader_min_interval;
725 queue_delayed_work(wm->ts_workq, &wm->ts_reader,
726 wm->ts_reader_interval);
733 #define wm97xx_suspend NULL
734 #define wm97xx_resume NULL
738 * Machine specific operations
740 int wm97xx_register_mach_ops(struct wm97xx *wm,
741 struct wm97xx_mach_ops *mach_ops)
743 mutex_lock(&wm->codec_mutex);
745 mutex_unlock(&wm->codec_mutex);
748 wm->mach_ops = mach_ops;
749 mutex_unlock(&wm->codec_mutex);
753 EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
755 void wm97xx_unregister_mach_ops(struct wm97xx *wm)
757 mutex_lock(&wm->codec_mutex);
759 mutex_unlock(&wm->codec_mutex);
761 EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
763 static struct device_driver wm97xx_driver = {
765 .bus = &ac97_bus_type,
766 .owner = THIS_MODULE,
767 .probe = wm97xx_probe,
768 .remove = wm97xx_remove,
769 .suspend = wm97xx_suspend,
770 .resume = wm97xx_resume,
773 static int __init wm97xx_init(void)
775 return driver_register(&wm97xx_driver);
778 static void __exit wm97xx_exit(void)
780 driver_unregister(&wm97xx_driver);
783 module_init(wm97xx_init);
784 module_exit(wm97xx_exit);
786 /* Module information */
787 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
788 MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
789 MODULE_LICENSE("GPL");