1 /* 2 * wm9713.c -- Codec touch driver for Wolfson WM9713 AC97 Codec. 3 * 4 * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC. 5 * Author: Liam Girdwood 6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com 7 * Parts Copyright : Ian Molton <spyro@f2s.com> 8 * Andrew Zabolotny <zap@homelink.ru> 9 * Russell King <rmk@arm.linux.org.uk> 10 * 11 * This program is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the 13 * Free Software Foundation; either version 2 of the License, or (at your 14 * option) any later version. 15 * 16 */ 17 18 #include <linux/module.h> 19 #include <linux/moduleparam.h> 20 #include <linux/version.h> 21 #include <linux/kernel.h> 22 #include <linux/input.h> 23 #include <linux/delay.h> 24 #include <linux/bitops.h> 25 #include <linux/wm97xx.h> 26 27 #define TS_NAME "wm97xx" 28 #define WM9713_VERSION "1.00" 29 #define DEFAULT_PRESSURE 0xb0c0 30 31 /* 32 * Module parameters 33 */ 34 35 /* 36 * Set internal pull up for pen detect. 37 * 38 * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive) 39 * i.e. pull up resistance = 64k Ohms / rpu. 40 * 41 * Adjust this value if you are having problems with pen detect not 42 * detecting any down event. 43 */ 44 static int rpu = 8; 45 module_param(rpu, int, 0); 46 MODULE_PARM_DESC(rpu, "Set internal pull up resitor for pen detect."); 47 48 /* 49 * Set current used for pressure measurement. 50 * 51 * Set pil = 2 to use 400uA 52 * pil = 1 to use 200uA and 53 * pil = 0 to disable pressure measurement. 54 * 55 * This is used to increase the range of values returned by the adc 56 * when measureing touchpanel pressure. 57 */ 58 static int pil; 59 module_param(pil, int, 0); 60 MODULE_PARM_DESC(pil, "Set current used for pressure measurement."); 61 62 /* 63 * Set threshold for pressure measurement. 64 * 65 * Pen down pressure below threshold is ignored. 66 */ 67 static int pressure = DEFAULT_PRESSURE & 0xfff; 68 module_param(pressure, int, 0); 69 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement."); 70 71 /* 72 * Set adc sample delay. 73 * 74 * For accurate touchpanel measurements, some settling time may be 75 * required between the switch matrix applying a voltage across the 76 * touchpanel plate and the ADC sampling the signal. 77 * 78 * This delay can be set by setting delay = n, where n is the array 79 * position of the delay in the array delay_table below. 80 * Long delays > 1ms are supported for completeness, but are not 81 * recommended. 82 */ 83 static int delay = 4; 84 module_param(delay, int, 0); 85 MODULE_PARM_DESC(delay, "Set adc sample delay."); 86 87 /* 88 * Set adc mask function. 89 * 90 * Sources of glitch noise, such as signals driving an LCD display, may feed 91 * through to the touch screen plates and affect measurement accuracy. In 92 * order to minimise this, a signal may be applied to the MASK pin to delay or 93 * synchronise the sampling. 94 * 95 * 0 = No delay or sync 96 * 1 = High on pin stops conversions 97 * 2 = Edge triggered, edge on pin delays conversion by delay param (above) 98 * 3 = Edge triggered, edge on pin starts conversion after delay param 99 */ 100 static int mask; 101 module_param(mask, int, 0); 102 MODULE_PARM_DESC(mask, "Set adc mask function."); 103 104 /* 105 * Coordinate Polling Enable. 106 * 107 * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together 108 * for every poll. 109 */ 110 static int coord; 111 module_param(coord, int, 0); 112 MODULE_PARM_DESC(coord, "Polling coordinate mode"); 113 114 /* 115 * ADC sample delay times in uS 116 */ 117 static const int delay_table[] = { 118 21, /* 1 AC97 Link frames */ 119 42, /* 2 */ 120 84, /* 4 */ 121 167, /* 8 */ 122 333, /* 16 */ 123 667, /* 32 */ 124 1000, /* 48 */ 125 1333, /* 64 */ 126 2000, /* 96 */ 127 2667, /* 128 */ 128 3333, /* 160 */ 129 4000, /* 192 */ 130 4667, /* 224 */ 131 5333, /* 256 */ 132 6000, /* 288 */ 133 0 /* No delay, switch matrix always on */ 134 }; 135 136 /* 137 * Delay after issuing a POLL command. 138 * 139 * The delay is 3 AC97 link frames + the touchpanel settling delay 140 */ 141 static inline void poll_delay(int d) 142 { 143 udelay(3 * AC97_LINK_FRAME + delay_table[d]); 144 } 145 146 /* 147 * set up the physical settings of the WM9713 148 */ 149 static void wm9713_phy_init(struct wm97xx *wm) 150 { 151 u16 dig1 = 0, dig2, dig3; 152 153 /* default values */ 154 dig2 = WM97XX_DELAY(4) | WM97XX_SLT(5); 155 dig3 = WM9712_RPU(1); 156 157 /* rpu */ 158 if (rpu) { 159 dig3 &= 0xffc0; 160 dig3 |= WM9712_RPU(rpu); 161 dev_info(wm->dev, "setting pen detect pull-up to %d Ohms\n", 162 64000 / rpu); 163 } 164 165 /* touchpanel pressure */ 166 if (pil == 2) { 167 dig3 |= WM9712_PIL; 168 dev_info(wm->dev, 169 "setting pressure measurement current to 400uA."); 170 } else if (pil) 171 dev_info(wm->dev, 172 "setting pressure measurement current to 200uA."); 173 if (!pil) 174 pressure = 0; 175 176 /* sample settling delay */ 177 if (delay < 0 || delay > 15) { 178 dev_info(wm->dev, "supplied delay out of range."); 179 delay = 4; 180 dev_info(wm->dev, "setting adc sample delay to %d u Secs.", 181 delay_table[delay]); 182 } 183 dig2 &= 0xff0f; 184 dig2 |= WM97XX_DELAY(delay); 185 186 /* mask */ 187 dig3 |= ((mask & 0x3) << 4); 188 if (coord) 189 dig3 |= WM9713_WAIT; 190 191 wm->misc = wm97xx_reg_read(wm, 0x5a); 192 193 wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1); 194 wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2); 195 wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3); 196 wm97xx_reg_write(wm, AC97_GPIO_STICKY, 0x0); 197 } 198 199 static void wm9713_dig_enable(struct wm97xx *wm, int enable) 200 { 201 u16 val; 202 203 if (enable) { 204 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID); 205 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val & 0x7fff); 206 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] | 207 WM97XX_PRP_DET_DIG); 208 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */ 209 } else { 210 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] & 211 ~WM97XX_PRP_DET_DIG); 212 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID); 213 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val | 0x8000); 214 } 215 } 216 217 static void wm9713_dig_restore(struct wm97xx *wm) 218 { 219 wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig_save[0]); 220 wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig_save[1]); 221 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig_save[2]); 222 } 223 224 static void wm9713_aux_prepare(struct wm97xx *wm) 225 { 226 memcpy(wm->dig_save, wm->dig, sizeof(wm->dig)); 227 wm97xx_reg_write(wm, AC97_WM9713_DIG1, 0); 228 wm97xx_reg_write(wm, AC97_WM9713_DIG2, 0); 229 wm97xx_reg_write(wm, AC97_WM9713_DIG3, WM97XX_PRP_DET_DIG); 230 } 231 232 static inline int is_pden(struct wm97xx *wm) 233 { 234 return wm->dig[2] & WM9713_PDEN; 235 } 236 237 /* 238 * Read a sample from the WM9713 adc in polling mode. 239 */ 240 static int wm9713_poll_sample(struct wm97xx *wm, int adcsel, int *sample) 241 { 242 u16 dig1; 243 int timeout = 5 * delay; 244 245 if (!wm->pen_probably_down) { 246 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); 247 if (!(data & WM97XX_PEN_DOWN)) 248 return RC_PENUP; 249 wm->pen_probably_down = 1; 250 } 251 252 /* set up digitiser */ 253 if (adcsel & 0x8000) 254 adcsel = 1 << ((adcsel & 0x7fff) + 3); 255 256 dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1); 257 dig1 &= ~WM9713_ADCSEL_MASK; 258 259 if (wm->mach_ops && wm->mach_ops->pre_sample) 260 wm->mach_ops->pre_sample(adcsel); 261 wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1 | adcsel | WM9713_POLL); 262 263 /* wait 3 AC97 time slots + delay for conversion */ 264 poll_delay(delay); 265 266 /* wait for POLL to go low */ 267 while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL) && 268 timeout) { 269 udelay(AC97_LINK_FRAME); 270 timeout--; 271 } 272 273 if (timeout <= 0) { 274 /* If PDEN is set, we can get a timeout when pen goes up */ 275 if (is_pden(wm)) 276 wm->pen_probably_down = 0; 277 else 278 dev_dbg(wm->dev, "adc sample timeout"); 279 return RC_PENUP; 280 } 281 282 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); 283 if (wm->mach_ops && wm->mach_ops->post_sample) 284 wm->mach_ops->post_sample(adcsel); 285 286 /* check we have correct sample */ 287 if ((*sample & WM97XX_ADCSRC_MASK) != ffs(adcsel >> 1) << 12) { 288 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel, 289 *sample & WM97XX_ADCSRC_MASK); 290 return RC_PENUP; 291 } 292 293 if (!(*sample & WM97XX_PEN_DOWN)) { 294 wm->pen_probably_down = 0; 295 return RC_PENUP; 296 } 297 298 return RC_VALID; 299 } 300 301 /* 302 * Read a coordinate from the WM9713 adc in polling mode. 303 */ 304 static int wm9713_poll_coord(struct wm97xx *wm, struct wm97xx_data *data) 305 { 306 u16 dig1; 307 int timeout = 5 * delay; 308 309 if (!wm->pen_probably_down) { 310 u16 val = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); 311 if (!(val & WM97XX_PEN_DOWN)) 312 return RC_PENUP; 313 wm->pen_probably_down = 1; 314 } 315 316 /* set up digitiser */ 317 dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1); 318 dig1 &= ~WM9713_ADCSEL_MASK; 319 if (pil) 320 dig1 |= WM9713_ADCSEL_PRES; 321 322 if (wm->mach_ops && wm->mach_ops->pre_sample) 323 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y); 324 wm97xx_reg_write(wm, AC97_WM9713_DIG1, 325 dig1 | WM9713_POLL | WM9713_COO); 326 327 /* wait 3 AC97 time slots + delay for conversion */ 328 poll_delay(delay); 329 data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); 330 /* wait for POLL to go low */ 331 while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL) 332 && timeout) { 333 udelay(AC97_LINK_FRAME); 334 timeout--; 335 } 336 337 if (timeout <= 0) { 338 /* If PDEN is set, we can get a timeout when pen goes up */ 339 if (is_pden(wm)) 340 wm->pen_probably_down = 0; 341 else 342 dev_dbg(wm->dev, "adc sample timeout"); 343 return RC_PENUP; 344 } 345 346 /* read back data */ 347 data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); 348 if (pil) 349 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); 350 else 351 data->p = DEFAULT_PRESSURE; 352 353 if (wm->mach_ops && wm->mach_ops->post_sample) 354 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y); 355 356 /* check we have correct sample */ 357 if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y)) 358 goto err; 359 if (pil && !(data->p & WM97XX_ADCSEL_PRES)) 360 goto err; 361 362 if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) { 363 wm->pen_probably_down = 0; 364 return RC_PENUP; 365 } 366 return RC_VALID; 367 err: 368 return 0; 369 } 370 371 /* 372 * Sample the WM9713 touchscreen in polling mode 373 */ 374 static int wm9713_poll_touch(struct wm97xx *wm, struct wm97xx_data *data) 375 { 376 int rc; 377 378 if (coord) { 379 rc = wm9713_poll_coord(wm, data); 380 if (rc != RC_VALID) 381 return rc; 382 } else { 383 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_X, &data->x); 384 if (rc != RC_VALID) 385 return rc; 386 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_Y, &data->y); 387 if (rc != RC_VALID) 388 return rc; 389 if (pil) { 390 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_PRES, 391 &data->p); 392 if (rc != RC_VALID) 393 return rc; 394 } else 395 data->p = DEFAULT_PRESSURE; 396 } 397 return RC_VALID; 398 } 399 400 /* 401 * Enable WM9713 continuous mode, i.e. touch data is streamed across 402 * an AC97 slot 403 */ 404 static int wm9713_acc_enable(struct wm97xx *wm, int enable) 405 { 406 u16 dig1, dig2, dig3; 407 int ret = 0; 408 409 dig1 = wm->dig[0]; 410 dig2 = wm->dig[1]; 411 dig3 = wm->dig[2]; 412 413 if (enable) { 414 /* continous mode */ 415 if (wm->mach_ops->acc_startup && 416 (ret = wm->mach_ops->acc_startup(wm)) < 0) 417 return ret; 418 419 dig1 &= ~WM9713_ADCSEL_MASK; 420 dig1 |= WM9713_CTC | WM9713_COO | WM9713_ADCSEL_X | 421 WM9713_ADCSEL_Y; 422 if (pil) 423 dig1 |= WM9713_ADCSEL_PRES; 424 dig2 &= ~(WM97XX_DELAY_MASK | WM97XX_SLT_MASK | 425 WM97XX_CM_RATE_MASK); 426 dig2 |= WM97XX_SLEN | WM97XX_DELAY(delay) | 427 WM97XX_SLT(wm->acc_slot) | WM97XX_RATE(wm->acc_rate); 428 dig3 |= WM9713_PDEN; 429 } else { 430 dig1 &= ~(WM9713_CTC | WM9713_COO); 431 dig2 &= ~WM97XX_SLEN; 432 dig3 &= ~WM9713_PDEN; 433 if (wm->mach_ops->acc_shutdown) 434 wm->mach_ops->acc_shutdown(wm); 435 } 436 437 wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1); 438 wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2); 439 wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3); 440 441 return ret; 442 } 443 444 struct wm97xx_codec_drv wm9713_codec = { 445 .id = WM9713_ID2, 446 .name = "wm9713", 447 .poll_sample = wm9713_poll_sample, 448 .poll_touch = wm9713_poll_touch, 449 .acc_enable = wm9713_acc_enable, 450 .phy_init = wm9713_phy_init, 451 .dig_enable = wm9713_dig_enable, 452 .dig_restore = wm9713_dig_restore, 453 .aux_prepare = wm9713_aux_prepare, 454 }; 455 EXPORT_SYMBOL_GPL(wm9713_codec); 456 457 /* Module information */ 458 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>"); 459 MODULE_DESCRIPTION("WM9713 Touch Screen Driver"); 460 MODULE_LICENSE("GPL"); 461