Path: blob/master/drivers/input/touchscreen/atmel-wm97xx.c
15109 views
/*1* Atmel AT91 and AVR32 continuous touch screen driver for Wolfson WM97xx AC972* codecs.3*4* Copyright (C) 2008 - 2009 Atmel Corporation5*6* This program is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License version 2 as published by8* the Free Software Foundation.9*/10#include <linux/module.h>11#include <linux/moduleparam.h>12#include <linux/kernel.h>13#include <linux/init.h>14#include <linux/delay.h>15#include <linux/irq.h>16#include <linux/interrupt.h>17#include <linux/wm97xx.h>18#include <linux/timer.h>19#include <linux/gpio.h>20#include <linux/io.h>21#include <linux/slab.h>2223#define AC97C_ICA 0x1024#define AC97C_CBRHR 0x3025#define AC97C_CBSR 0x3826#define AC97C_CBMR 0x3c27#define AC97C_IER 0x5428#define AC97C_IDR 0x582930#define AC97C_RXRDY (1 << 4)31#define AC97C_OVRUN (1 << 5)3233#define AC97C_CMR_SIZE_20 (0 << 16)34#define AC97C_CMR_SIZE_18 (1 << 16)35#define AC97C_CMR_SIZE_16 (2 << 16)36#define AC97C_CMR_SIZE_10 (3 << 16)37#define AC97C_CMR_CEM_LITTLE (1 << 18)38#define AC97C_CMR_CEM_BIG (0 << 18)39#define AC97C_CMR_CENA (1 << 21)4041#define AC97C_INT_CBEVT (1 << 4)4243#define AC97C_SR_CAEVT (1 << 3)4445#define AC97C_CH_MASK(slot) \46(0x7 << (3 * (slot - 3)))47#define AC97C_CH_ASSIGN(slot, channel) \48(AC97C_CHANNEL_##channel << (3 * (slot - 3)))49#define AC97C_CHANNEL_NONE 0x050#define AC97C_CHANNEL_B 0x25152#define ac97c_writel(chip, reg, val) \53__raw_writel((val), (chip)->regs + AC97C_##reg)54#define ac97c_readl(chip, reg) \55__raw_readl((chip)->regs + AC97C_##reg)5657#ifdef CONFIG_CPU_AT32AP700X58#define ATMEL_WM97XX_AC97C_IOMEM (0xfff02800)59#define ATMEL_WM97XX_AC97C_IRQ (29)60#define ATMEL_WM97XX_GPIO_DEFAULT (32+16) /* Pin 16 on port B. */61#else62#error Unknown CPU, this driver only supports AT32AP700X CPUs.63#endif6465struct continuous {66u16 id; /* codec id */67u8 code; /* continuous code */68u8 reads; /* number of coord reads per read cycle */69u32 speed; /* number of coords per second */70};7172#define WM_READS(sp) ((sp / HZ) + 1)7374static const struct continuous cinfo[] = {75{WM9705_ID2, 0, WM_READS(94), 94},76{WM9705_ID2, 1, WM_READS(188), 188},77{WM9705_ID2, 2, WM_READS(375), 375},78{WM9705_ID2, 3, WM_READS(750), 750},79{WM9712_ID2, 0, WM_READS(94), 94},80{WM9712_ID2, 1, WM_READS(188), 188},81{WM9712_ID2, 2, WM_READS(375), 375},82{WM9712_ID2, 3, WM_READS(750), 750},83{WM9713_ID2, 0, WM_READS(94), 94},84{WM9713_ID2, 1, WM_READS(120), 120},85{WM9713_ID2, 2, WM_READS(154), 154},86{WM9713_ID2, 3, WM_READS(188), 188},87};8889/* Continuous speed index. */90static int sp_idx;9192/*93* Pen sampling frequency (Hz) in continuous mode.94*/95static int cont_rate = 188;96module_param(cont_rate, int, 0);97MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");9899/*100* Pen down detection.101*102* This driver can either poll or use an interrupt to indicate a pen down103* event. If the irq request fails then it will fall back to polling mode.104*/105static int pen_int = 1;106module_param(pen_int, int, 0);107MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");108109/*110* Pressure readback.111*112* Set to 1 to read back pen down pressure.113*/114static int pressure;115module_param(pressure, int, 0);116MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");117118/*119* AC97 touch data slot.120*121* Touch screen readback data ac97 slot.122*/123static int ac97_touch_slot = 5;124module_param(ac97_touch_slot, int, 0);125MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");126127/*128* GPIO line number.129*130* Set to GPIO number where the signal from the WM97xx device is hooked up.131*/132static int atmel_gpio_line = ATMEL_WM97XX_GPIO_DEFAULT;133module_param(atmel_gpio_line, int, 0);134MODULE_PARM_DESC(atmel_gpio_line, "GPIO line number connected to WM97xx");135136struct atmel_wm97xx {137struct wm97xx *wm;138struct timer_list pen_timer;139void __iomem *regs;140unsigned long ac97c_irq;141unsigned long gpio_pen;142unsigned long gpio_irq;143unsigned short x;144unsigned short y;145};146147static irqreturn_t atmel_wm97xx_channel_b_interrupt(int irq, void *dev_id)148{149struct atmel_wm97xx *atmel_wm97xx = dev_id;150struct wm97xx *wm = atmel_wm97xx->wm;151int status = ac97c_readl(atmel_wm97xx, CBSR);152irqreturn_t retval = IRQ_NONE;153154if (status & AC97C_OVRUN) {155dev_dbg(&wm->touch_dev->dev, "AC97C overrun\n");156ac97c_readl(atmel_wm97xx, CBRHR);157retval = IRQ_HANDLED;158} else if (status & AC97C_RXRDY) {159u16 data;160u16 value;161u16 source;162u16 pen_down;163164data = ac97c_readl(atmel_wm97xx, CBRHR);165value = data & 0x0fff;166source = data & WM97XX_ADCSRC_MASK;167pen_down = (data & WM97XX_PEN_DOWN) >> 8;168169if (source == WM97XX_ADCSEL_X)170atmel_wm97xx->x = value;171if (source == WM97XX_ADCSEL_Y)172atmel_wm97xx->y = value;173174if (!pressure && source == WM97XX_ADCSEL_Y) {175input_report_abs(wm->input_dev, ABS_X, atmel_wm97xx->x);176input_report_abs(wm->input_dev, ABS_Y, atmel_wm97xx->y);177input_report_key(wm->input_dev, BTN_TOUCH, pen_down);178input_sync(wm->input_dev);179} else if (pressure && source == WM97XX_ADCSEL_PRES) {180input_report_abs(wm->input_dev, ABS_X, atmel_wm97xx->x);181input_report_abs(wm->input_dev, ABS_Y, atmel_wm97xx->y);182input_report_abs(wm->input_dev, ABS_PRESSURE, value);183input_report_key(wm->input_dev, BTN_TOUCH, value);184input_sync(wm->input_dev);185}186187retval = IRQ_HANDLED;188}189190return retval;191}192193static void atmel_wm97xx_acc_pen_up(struct wm97xx *wm)194{195struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(wm->touch_dev);196struct input_dev *input_dev = wm->input_dev;197int pen_down = gpio_get_value(atmel_wm97xx->gpio_pen);198199if (pen_down != 0) {200mod_timer(&atmel_wm97xx->pen_timer,201jiffies + msecs_to_jiffies(1));202} else {203if (pressure)204input_report_abs(input_dev, ABS_PRESSURE, 0);205input_report_key(input_dev, BTN_TOUCH, 0);206input_sync(input_dev);207}208}209210static void atmel_wm97xx_pen_timer(unsigned long data)211{212atmel_wm97xx_acc_pen_up((struct wm97xx *)data);213}214215static int atmel_wm97xx_acc_startup(struct wm97xx *wm)216{217struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(wm->touch_dev);218int idx = 0;219220if (wm->ac97 == NULL)221return -ENODEV;222223for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {224if (wm->id != cinfo[idx].id)225continue;226227sp_idx = idx;228229if (cont_rate <= cinfo[idx].speed)230break;231}232233wm->acc_rate = cinfo[sp_idx].code;234wm->acc_slot = ac97_touch_slot;235dev_info(&wm->touch_dev->dev, "atmel accelerated touchscreen driver, "236"%d samples/sec\n", cinfo[sp_idx].speed);237238if (pen_int) {239unsigned long reg;240241wm->pen_irq = atmel_wm97xx->gpio_irq;242243switch (wm->id) {244case WM9712_ID2: /* Fall through. */245case WM9713_ID2:246/*247* Use GPIO 13 (PEN_DOWN) to assert GPIO line 3248* (PENDOWN).249*/250wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,251WM97XX_GPIO_POL_HIGH,252WM97XX_GPIO_STICKY,253WM97XX_GPIO_WAKE);254wm97xx_config_gpio(wm, WM97XX_GPIO_3, WM97XX_GPIO_OUT,255WM97XX_GPIO_POL_HIGH,256WM97XX_GPIO_NOTSTICKY,257WM97XX_GPIO_NOWAKE);258case WM9705_ID2: /* Fall through. */259/*260* Enable touch data slot in AC97 controller channel B.261*/262reg = ac97c_readl(atmel_wm97xx, ICA);263reg &= ~AC97C_CH_MASK(wm->acc_slot);264reg |= AC97C_CH_ASSIGN(wm->acc_slot, B);265ac97c_writel(atmel_wm97xx, ICA, reg);266267/*268* Enable channel and interrupt for RXRDY and OVERRUN.269*/270ac97c_writel(atmel_wm97xx, CBMR, AC97C_CMR_CENA271| AC97C_CMR_CEM_BIG272| AC97C_CMR_SIZE_16273| AC97C_OVRUN274| AC97C_RXRDY);275/* Dummy read to empty RXRHR. */276ac97c_readl(atmel_wm97xx, CBRHR);277/*278* Enable interrupt for channel B in the AC97279* controller.280*/281ac97c_writel(atmel_wm97xx, IER, AC97C_INT_CBEVT);282break;283default:284dev_err(&wm->touch_dev->dev, "pen down irq not "285"supported on this device\n");286pen_int = 0;287break;288}289}290291return 0;292}293294static void atmel_wm97xx_acc_shutdown(struct wm97xx *wm)295{296if (pen_int) {297struct atmel_wm97xx *atmel_wm97xx =298platform_get_drvdata(wm->touch_dev);299unsigned long ica;300301switch (wm->id & 0xffff) {302case WM9705_ID2: /* Fall through. */303case WM9712_ID2: /* Fall through. */304case WM9713_ID2:305/* Disable slot and turn off channel B interrupts. */306ica = ac97c_readl(atmel_wm97xx, ICA);307ica &= ~AC97C_CH_MASK(wm->acc_slot);308ac97c_writel(atmel_wm97xx, ICA, ica);309ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);310ac97c_writel(atmel_wm97xx, CBMR, 0);311wm->pen_irq = 0;312break;313default:314dev_err(&wm->touch_dev->dev, "unknown codec\n");315break;316}317}318}319320static void atmel_wm97xx_irq_enable(struct wm97xx *wm, int enable)321{322/* Intentionally left empty. */323}324325static struct wm97xx_mach_ops atmel_mach_ops = {326.acc_enabled = 1,327.acc_pen_up = atmel_wm97xx_acc_pen_up,328.acc_startup = atmel_wm97xx_acc_startup,329.acc_shutdown = atmel_wm97xx_acc_shutdown,330.irq_enable = atmel_wm97xx_irq_enable,331.irq_gpio = WM97XX_GPIO_3,332};333334static int __init atmel_wm97xx_probe(struct platform_device *pdev)335{336struct wm97xx *wm = platform_get_drvdata(pdev);337struct atmel_wm97xx *atmel_wm97xx;338int ret;339340atmel_wm97xx = kzalloc(sizeof(struct atmel_wm97xx), GFP_KERNEL);341if (!atmel_wm97xx) {342dev_dbg(&pdev->dev, "out of memory\n");343return -ENOMEM;344}345346atmel_wm97xx->wm = wm;347atmel_wm97xx->regs = (void *)ATMEL_WM97XX_AC97C_IOMEM;348atmel_wm97xx->ac97c_irq = ATMEL_WM97XX_AC97C_IRQ;349atmel_wm97xx->gpio_pen = atmel_gpio_line;350atmel_wm97xx->gpio_irq = gpio_to_irq(atmel_wm97xx->gpio_pen);351352setup_timer(&atmel_wm97xx->pen_timer, atmel_wm97xx_pen_timer,353(unsigned long)wm);354355ret = request_irq(atmel_wm97xx->ac97c_irq,356atmel_wm97xx_channel_b_interrupt,357IRQF_SHARED, "atmel-wm97xx-ch-b", atmel_wm97xx);358if (ret) {359dev_dbg(&pdev->dev, "could not request ac97c irq\n");360goto err;361}362363platform_set_drvdata(pdev, atmel_wm97xx);364365ret = wm97xx_register_mach_ops(wm, &atmel_mach_ops);366if (ret)367goto err_irq;368369return ret;370371err_irq:372free_irq(atmel_wm97xx->ac97c_irq, atmel_wm97xx);373err:374platform_set_drvdata(pdev, NULL);375kfree(atmel_wm97xx);376return ret;377}378379static int __exit atmel_wm97xx_remove(struct platform_device *pdev)380{381struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);382struct wm97xx *wm = atmel_wm97xx->wm;383384ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);385free_irq(atmel_wm97xx->ac97c_irq, atmel_wm97xx);386del_timer_sync(&atmel_wm97xx->pen_timer);387wm97xx_unregister_mach_ops(wm);388platform_set_drvdata(pdev, NULL);389kfree(atmel_wm97xx);390391return 0;392}393394#ifdef CONFIG_PM395static int atmel_wm97xx_suspend(struct platform_device *pdev, pm_message_t msg)396{397struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);398399ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);400disable_irq(atmel_wm97xx->gpio_irq);401del_timer_sync(&atmel_wm97xx->pen_timer);402403return 0;404}405406static int atmel_wm97xx_resume(struct platform_device *pdev)407{408struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);409struct wm97xx *wm = atmel_wm97xx->wm;410411if (wm->input_dev->users) {412enable_irq(atmel_wm97xx->gpio_irq);413ac97c_writel(atmel_wm97xx, IER, AC97C_INT_CBEVT);414}415416return 0;417}418#else419#define atmel_wm97xx_suspend NULL420#define atmel_wm97xx_resume NULL421#endif422423static struct platform_driver atmel_wm97xx_driver = {424.remove = __exit_p(atmel_wm97xx_remove),425.driver = {426.name = "wm97xx-touch",427},428.suspend = atmel_wm97xx_suspend,429.resume = atmel_wm97xx_resume,430};431432static int __init atmel_wm97xx_init(void)433{434return platform_driver_probe(&atmel_wm97xx_driver, atmel_wm97xx_probe);435}436module_init(atmel_wm97xx_init);437438static void __exit atmel_wm97xx_exit(void)439{440platform_driver_unregister(&atmel_wm97xx_driver);441}442module_exit(atmel_wm97xx_exit);443444MODULE_AUTHOR("Hans-Christian Egtvedt <[email protected]>");445MODULE_DESCRIPTION("wm97xx continuous touch driver for Atmel AT91 and AVR32");446MODULE_LICENSE("GPL");447448449