2  * wm9713.c  --  Codec touch driver for Wolfson WM9713 AC97 Codec.
 
   4  * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
 
   5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
 
   6  * Parts Copyright : Ian Molton <spyro@f2s.com>
 
   7  *                   Andrew Zabolotny <zap@homelink.ru>
 
   8  *                   Russell King <rmk@arm.linux.org.uk>
 
  10  *  This program is free software; you can redistribute  it and/or modify it
 
  11  *  under  the terms of  the GNU General  Public License as published by the
 
  12  *  Free Software Foundation;  either version 2 of the  License, or (at your
 
  13  *  option) any later version.
 
  17 #include <linux/module.h>
 
  18 #include <linux/moduleparam.h>
 
  19 #include <linux/kernel.h>
 
  20 #include <linux/input.h>
 
  21 #include <linux/delay.h>
 
  22 #include <linux/bitops.h>
 
  23 #include <linux/wm97xx.h>
 
  25 #define TS_NAME                 "wm97xx"
 
  26 #define WM9713_VERSION          "1.00"
 
  27 #define DEFAULT_PRESSURE        0xb0c0
 
  34  * Set internal pull up for pen detect.
 
  36  * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
 
  37  * i.e. pull up resistance = 64k Ohms / rpu.
 
  39  * Adjust this value if you are having problems with pen detect not
 
  40  * detecting any down event.
 
  43 module_param(rpu, int, 0);
 
  44 MODULE_PARM_DESC(rpu, "Set internal pull up resitor for pen detect.");
 
  47  * Set current used for pressure measurement.
 
  49  * Set pil = 2 to use 400uA
 
  50  *     pil = 1 to use 200uA and
 
  51  *     pil = 0 to disable pressure measurement.
 
  53  * This is used to increase the range of values returned by the adc
 
  54  * when measureing touchpanel pressure.
 
  57 module_param(pil, int, 0);
 
  58 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
 
  61  * Set threshold for pressure measurement.
 
  63  * Pen down pressure below threshold is ignored.
 
  65 static int pressure = DEFAULT_PRESSURE & 0xfff;
 
  66 module_param(pressure, int, 0);
 
  67 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
 
  70  * Set adc sample delay.
 
  72  * For accurate touchpanel measurements, some settling time may be
 
  73  * required between the switch matrix applying a voltage across the
 
  74  * touchpanel plate and the ADC sampling the signal.
 
  76  * This delay can be set by setting delay = n, where n is the array
 
  77  * position of the delay in the array delay_table below.
 
  78  * Long delays > 1ms are supported for completeness, but are not
 
  82 module_param(delay, int, 0);
 
  83 MODULE_PARM_DESC(delay, "Set adc sample delay.");
 
  86  * Set five_wire = 1 to use a 5 wire touchscreen.
 
  88  * NOTE: Five wire mode does not allow for readback of pressure.
 
  91 module_param(five_wire, int, 0);
 
  92 MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
 
  95  * Set adc mask function.
 
  97  * Sources of glitch noise, such as signals driving an LCD display, may feed
 
  98  * through to the touch screen plates and affect measurement accuracy. In
 
  99  * order to minimise this, a signal may be applied to the MASK pin to delay or
 
 100  * synchronise the sampling.
 
 102  * 0 = No delay or sync
 
 103  * 1 = High on pin stops conversions
 
 104  * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
 
 105  * 3 = Edge triggered, edge on pin starts conversion after delay param
 
 108 module_param(mask, int, 0);
 
 109 MODULE_PARM_DESC(mask, "Set adc mask function.");
 
 112  * Coordinate Polling Enable.
 
 114  * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
 
 118 module_param(coord, int, 0);
 
 119 MODULE_PARM_DESC(coord, "Polling coordinate mode");
 
 122  * ADC sample delay times in uS
 
 124 static const int delay_table[] = {
 
 125         21,    /* 1 AC97 Link frames */
 
 140         0      /* No delay, switch matrix always on */
 
 144  * Delay after issuing a POLL command.
 
 146  * The delay is 3 AC97 link frames + the touchpanel settling delay
 
 148 static inline void poll_delay(int d)
 
 150         udelay(3 * AC97_LINK_FRAME + delay_table[d]);
 
 154  * set up the physical settings of the WM9713
 
 156 static void wm9713_phy_init(struct wm97xx *wm)
 
 158         u16 dig1 = 0, dig2, dig3;
 
 161         dig2 = WM97XX_DELAY(4) | WM97XX_SLT(5);
 
 162         dig3 = WM9712_RPU(1);
 
 167                 dig3 |= WM9712_RPU(rpu);
 
 168                 dev_info(wm->dev, "setting pen detect pull-up to %d Ohms\n",
 
 172         /* Five wire panel? */
 
 175                 dev_info(wm->dev, "setting 5-wire touchscreen mode.");
 
 179                                  "Pressure measurement not supported in 5 "
 
 180                                  "wire mode, disabling\n");
 
 185         /* touchpanel pressure */
 
 189                          "setting pressure measurement current to 400uA.");
 
 192                          "setting pressure measurement current to 200uA.");
 
 196         /* sample settling delay */
 
 197         if (delay < 0 || delay > 15) {
 
 198                 dev_info(wm->dev, "supplied delay out of range.");
 
 200                 dev_info(wm->dev, "setting adc sample delay to %d u Secs.",
 
 204         dig2 |= WM97XX_DELAY(delay);
 
 207         dig3 |= ((mask & 0x3) << 4);
 
 211         wm->misc = wm97xx_reg_read(wm, 0x5a);
 
 213         wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
 
 214         wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
 
 215         wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
 
 216         wm97xx_reg_write(wm, AC97_GPIO_STICKY, 0x0);
 
 219 static void wm9713_dig_enable(struct wm97xx *wm, int enable)
 
 224                 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
 
 225                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val & 0x7fff);
 
 226                 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] |
 
 228                 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
 
 230                 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] &
 
 231                                         ~WM97XX_PRP_DET_DIG);
 
 232                 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
 
 233                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val | 0x8000);
 
 237 static void wm9713_dig_restore(struct wm97xx *wm)
 
 239         wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig_save[0]);
 
 240         wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig_save[1]);
 
 241         wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig_save[2]);
 
 244 static void wm9713_aux_prepare(struct wm97xx *wm)
 
 246         memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
 
 247         wm97xx_reg_write(wm, AC97_WM9713_DIG1, 0);
 
 248         wm97xx_reg_write(wm, AC97_WM9713_DIG2, 0);
 
 249         wm97xx_reg_write(wm, AC97_WM9713_DIG3, WM97XX_PRP_DET_DIG);
 
 252 static inline int is_pden(struct wm97xx *wm)
 
 254         return wm->dig[2] & WM9713_PDEN;
 
 258  * Read a sample from the WM9713 adc in polling mode.
 
 260 static int wm9713_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
 
 263         int timeout = 5 * delay;
 
 265         if (!wm->pen_probably_down) {
 
 266                 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
 
 267                 if (!(data & WM97XX_PEN_DOWN))
 
 269                 wm->pen_probably_down = 1;
 
 272         /* set up digitiser */
 
 274                 adcsel = 1 << ((adcsel & 0x7fff) + 3);
 
 276         dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
 
 277         dig1 &= ~WM9713_ADCSEL_MASK;
 
 279         if (wm->mach_ops && wm->mach_ops->pre_sample)
 
 280                 wm->mach_ops->pre_sample(adcsel);
 
 281         wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1 | adcsel | WM9713_POLL);
 
 283         /* wait 3 AC97 time slots + delay for conversion */
 
 286         /* wait for POLL to go low */
 
 287         while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL) &&
 
 289                 udelay(AC97_LINK_FRAME);
 
 294                 /* If PDEN is set, we can get a timeout when pen goes up */
 
 296                         wm->pen_probably_down = 0;
 
 298                         dev_dbg(wm->dev, "adc sample timeout");
 
 302         *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
 
 303         if (wm->mach_ops && wm->mach_ops->post_sample)
 
 304                 wm->mach_ops->post_sample(adcsel);
 
 306         /* check we have correct sample */
 
 307         if ((*sample & WM97XX_ADCSRC_MASK) != ffs(adcsel >> 1) << 12) {
 
 308                 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
 
 309                         *sample & WM97XX_ADCSRC_MASK);
 
 313         if (!(*sample & WM97XX_PEN_DOWN)) {
 
 314                 wm->pen_probably_down = 0;
 
 322  * Read a coordinate from the WM9713 adc in polling mode.
 
 324 static int wm9713_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
 
 327         int timeout = 5 * delay;
 
 329         if (!wm->pen_probably_down) {
 
 330                 u16 val = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
 
 331                 if (!(val & WM97XX_PEN_DOWN))
 
 333                 wm->pen_probably_down = 1;
 
 336         /* set up digitiser */
 
 337         dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
 
 338         dig1 &= ~WM9713_ADCSEL_MASK;
 
 340                 dig1 |= WM9713_ADCSEL_PRES;
 
 342         if (wm->mach_ops && wm->mach_ops->pre_sample)
 
 343                 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
 
 344         wm97xx_reg_write(wm, AC97_WM9713_DIG1,
 
 345                          dig1 | WM9713_POLL | WM9713_COO);
 
 347         /* wait 3 AC97 time slots + delay for conversion */
 
 349         data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
 
 350         /* wait for POLL to go low */
 
 351         while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL)
 
 353                 udelay(AC97_LINK_FRAME);
 
 358                 /* If PDEN is set, we can get a timeout when pen goes up */
 
 360                         wm->pen_probably_down = 0;
 
 362                         dev_dbg(wm->dev, "adc sample timeout");
 
 367         data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
 
 369                 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
 
 371                 data->p = DEFAULT_PRESSURE;
 
 373         if (wm->mach_ops && wm->mach_ops->post_sample)
 
 374                 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
 
 376         /* check we have correct sample */
 
 377         if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
 
 379         if (pil && !(data->p & WM97XX_ADCSEL_PRES))
 
 382         if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
 
 383                 wm->pen_probably_down = 0;
 
 392  * Sample the WM9713 touchscreen in polling mode
 
 394 static int wm9713_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
 
 399                 rc = wm9713_poll_coord(wm, data);
 
 403                 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_X, &data->x);
 
 406                 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_Y, &data->y);
 
 410                         rc = wm9713_poll_sample(wm, WM9713_ADCSEL_PRES,
 
 415                         data->p = DEFAULT_PRESSURE;
 
 421  * Enable WM9713 continuous mode, i.e. touch data is streamed across
 
 424 static int wm9713_acc_enable(struct wm97xx *wm, int enable)
 
 426         u16 dig1, dig2, dig3;
 
 435                 if (wm->mach_ops->acc_startup &&
 
 436                         (ret = wm->mach_ops->acc_startup(wm)) < 0)
 
 439                 dig1 &= ~WM9713_ADCSEL_MASK;
 
 440                 dig1 |= WM9713_CTC | WM9713_COO | WM9713_ADCSEL_X |
 
 443                         dig1 |= WM9713_ADCSEL_PRES;
 
 444                 dig2 &= ~(WM97XX_DELAY_MASK | WM97XX_SLT_MASK  |
 
 445                         WM97XX_CM_RATE_MASK);
 
 446                 dig2 |= WM97XX_SLEN | WM97XX_DELAY(delay) |
 
 447                 WM97XX_SLT(wm->acc_slot) | WM97XX_RATE(wm->acc_rate);
 
 450                 dig1 &= ~(WM9713_CTC | WM9713_COO);
 
 451                 dig2 &= ~WM97XX_SLEN;
 
 452                 dig3 &= ~WM9713_PDEN;
 
 453                 if (wm->mach_ops->acc_shutdown)
 
 454                         wm->mach_ops->acc_shutdown(wm);
 
 457         wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
 
 458         wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
 
 459         wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
 
 464 struct wm97xx_codec_drv wm9713_codec = {
 
 467         .poll_sample = wm9713_poll_sample,
 
 468         .poll_touch = wm9713_poll_touch,
 
 469         .acc_enable = wm9713_acc_enable,
 
 470         .phy_init = wm9713_phy_init,
 
 471         .dig_enable = wm9713_dig_enable,
 
 472         .dig_restore = wm9713_dig_restore,
 
 473         .aux_prepare = wm9713_aux_prepare,
 
 475 EXPORT_SYMBOL_GPL(wm9713_codec);
 
 477 /* Module information */
 
 478 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
 
 479 MODULE_DESCRIPTION("WM9713 Touch Screen Driver");
 
 480 MODULE_LICENSE("GPL");