Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * wm97xx-core.c  --  Touch screen driver core for Wolfson WM9705, WM9712
  4 *                    and WM9713 AC97 Codecs.
  5 *
  6 * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
  7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  8 * Parts Copyright : Ian Molton <spyro@f2s.com>
  9 *                   Andrew Zabolotny <zap@homelink.ru>
 10 *                   Russell King <rmk@arm.linux.org.uk>
 11 *
 
 
 
 
 
 12 * Notes:
 13 *
 14 *  Features:
 15 *       - supports WM9705, WM9712, WM9713
 16 *       - polling mode
 17 *       - continuous mode (arch-dependent)
 18 *       - adjustable rpu/dpp settings
 19 *       - adjustable pressure current
 20 *       - adjustable sample settle delay
 21 *       - 4 and 5 wire touchscreens (5 wire is WM9712 only)
 22 *       - pen down detection
 23 *       - battery monitor
 24 *       - sample AUX adcs
 25 *       - power management
 26 *       - codec GPIO
 27 *       - codec event notification
 28 * Todo
 29 *       - Support for async sampling control for noisy LCDs.
 
 30 */
 31
 32#include <linux/module.h>
 33#include <linux/moduleparam.h>
 34#include <linux/kernel.h>
 35#include <linux/init.h>
 36#include <linux/delay.h>
 37#include <linux/string.h>
 38#include <linux/proc_fs.h>
 39#include <linux/pm.h>
 40#include <linux/interrupt.h>
 41#include <linux/bitops.h>
 42#include <linux/mfd/wm97xx.h>
 43#include <linux/workqueue.h>
 44#include <linux/wm97xx.h>
 45#include <linux/uaccess.h>
 46#include <linux/io.h>
 47#include <linux/slab.h>
 48
 49#define TS_NAME			"wm97xx"
 50#define WM_CORE_VERSION		"1.00"
 51#define DEFAULT_PRESSURE	0xb0c0
 52
 53
 54/*
 55 * Touchscreen absolute values
 56 *
 57 * These parameters are used to help the input layer discard out of
 58 * range readings and reduce jitter etc.
 59 *
 60 *   o min, max:- indicate the min and max values your touch screen returns
 61 *   o fuzz:- use a higher number to reduce jitter
 62 *
 63 * The default values correspond to Mainstone II in QVGA mode
 64 *
 65 * Please read
 66 * Documentation/input/input-programming.rst for more details.
 67 */
 68
 69static int abs_x[3] = {150, 4000, 5};
 70module_param_array(abs_x, int, NULL, 0);
 71MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
 72
 73static int abs_y[3] = {200, 4000, 40};
 74module_param_array(abs_y, int, NULL, 0);
 75MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
 76
 77static int abs_p[3] = {0, 150, 4};
 78module_param_array(abs_p, int, NULL, 0);
 79MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
 80
 81/*
 82 * wm97xx IO access, all IO locking done by AC97 layer
 83 */
 84int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
 85{
 86	if (wm->ac97)
 87		return wm->ac97->bus->ops->read(wm->ac97, reg);
 88	else
 89		return -1;
 90}
 91EXPORT_SYMBOL_GPL(wm97xx_reg_read);
 92
 93void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
 94{
 95	/* cache digitiser registers */
 96	if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
 97		wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
 98
 99	/* cache gpio regs */
100	if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
101		wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
102
103	/* wm9713 irq reg */
104	if (reg == 0x5a)
105		wm->misc = val;
106
107	if (wm->ac97)
108		wm->ac97->bus->ops->write(wm->ac97, reg, val);
109}
110EXPORT_SYMBOL_GPL(wm97xx_reg_write);
111
112/**
113 * wm97xx_read_aux_adc - Read the aux adc.
114 * @wm: wm97xx device.
115 * @adcsel: codec ADC to be read
116 *
117 * Reads the selected AUX ADC.
118 */
119
120int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
121{
122	int power_adc = 0, auxval;
123	u16 power = 0;
124	int rc = 0;
125	int timeout = 0;
126
127	/* get codec */
128	mutex_lock(&wm->codec_mutex);
129
130	/* When the touchscreen is not in use, we may have to power up
131	 * the AUX ADC before we can use sample the AUX inputs->
132	 */
133	if (wm->id == WM9713_ID2 &&
134	    (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
135		power_adc = 1;
136		wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
137	}
138
139	/* Prepare the codec for AUX reading */
140	wm->codec->aux_prepare(wm);
141
142	/* Turn polling mode on to read AUX ADC */
143	wm->pen_probably_down = 1;
144
145	while (rc != RC_VALID && timeout++ < 5)
146		rc = wm->codec->poll_sample(wm, adcsel, &auxval);
147
148	if (power_adc)
149		wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
150
151	wm->codec->dig_restore(wm);
152
153	wm->pen_probably_down = 0;
154
155	if (timeout >= 5) {
156		dev_err(wm->dev,
157			"timeout reading auxadc %d, disabling digitiser\n",
158			adcsel);
159		wm->codec->dig_enable(wm, false);
160	}
161
162	mutex_unlock(&wm->codec_mutex);
163	return (rc == RC_VALID ? auxval & 0xfff : -EBUSY);
164}
165EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
166
167/**
168 * wm97xx_get_gpio - Get the status of a codec GPIO.
169 * @wm: wm97xx device.
170 * @gpio: gpio
171 *
172 * Get the status of a codec GPIO pin
173 */
174
175enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
176{
177	u16 status;
178	enum wm97xx_gpio_status ret;
179
180	mutex_lock(&wm->codec_mutex);
181	status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
182
183	if (status & gpio)
184		ret = WM97XX_GPIO_HIGH;
185	else
186		ret = WM97XX_GPIO_LOW;
187
188	mutex_unlock(&wm->codec_mutex);
189	return ret;
190}
191EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
192
193/**
194 * wm97xx_set_gpio - Set the status of a codec GPIO.
195 * @wm: wm97xx device.
196 * @gpio: gpio
197 * @status: status
198 *
199 * Set the status of a codec GPIO pin
200 */
201
202void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
203				enum wm97xx_gpio_status status)
204{
205	u16 reg;
206
207	mutex_lock(&wm->codec_mutex);
208	reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
209
210	if (status == WM97XX_GPIO_HIGH)
211		reg |= gpio;
212	else
213		reg &= ~gpio;
214
215	if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
216		wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
217	else
218		wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
219	mutex_unlock(&wm->codec_mutex);
220}
221EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
222
223/*
224 * Codec GPIO pin configuration, this sets pin direction, polarity,
225 * stickyness and wake up.
226 */
227void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
228		   enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
229		   enum wm97xx_gpio_wake wake)
230{
231	u16 reg;
232
233	mutex_lock(&wm->codec_mutex);
234	reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
235
236	if (pol == WM97XX_GPIO_POL_HIGH)
237		reg |= gpio;
238	else
239		reg &= ~gpio;
240
241	wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
242	reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
243
244	if (sticky == WM97XX_GPIO_STICKY)
245		reg |= gpio;
246	else
247		reg &= ~gpio;
248
249	wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
250	reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
251
252	if (wake == WM97XX_GPIO_WAKE)
253		reg |= gpio;
254	else
255		reg &= ~gpio;
256
257	wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
258	reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
259
260	if (dir == WM97XX_GPIO_IN)
261		reg |= gpio;
262	else
263		reg &= ~gpio;
264
265	wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
266	mutex_unlock(&wm->codec_mutex);
267}
268EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
269
270/*
271 * Configure the WM97XX_PRP value to use while system is suspended.
272 * If a value other than 0 is set then WM97xx pen detection will be
273 * left enabled in the configured mode while the system is in suspend,
274 * the device has users and suspend has not been disabled via the
275 * wakeup sysfs entries.
276 *
277 * @wm:   WM97xx device to configure
278 * @mode: WM97XX_PRP value to configure while suspended
279 */
280void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
281{
282	wm->suspend_mode = mode;
283	device_init_wakeup(&wm->input_dev->dev, mode != 0);
284}
285EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
286
287/*
288 * Codec PENDOWN irq handler
289 *
290 */
291static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
292{
293	struct wm97xx *wm = dev_id;
294	int pen_was_down = wm->pen_is_down;
295
296	/* do we need to enable the touch panel reader */
297	if (wm->id == WM9705_ID2) {
298		if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
299					WM97XX_PEN_DOWN)
300			wm->pen_is_down = 1;
301		else
302			wm->pen_is_down = 0;
303	} else {
304		u16 status, pol;
305		mutex_lock(&wm->codec_mutex);
306		status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
307		pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
308
309		if (WM97XX_GPIO_13 & pol & status) {
310			wm->pen_is_down = 1;
311			wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
312						~WM97XX_GPIO_13);
313		} else {
314			wm->pen_is_down = 0;
315			wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
316					 WM97XX_GPIO_13);
317		}
318
319		if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
320			wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
321						~WM97XX_GPIO_13) << 1);
322		else
323			wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
324						~WM97XX_GPIO_13);
325		mutex_unlock(&wm->codec_mutex);
326	}
327
328	/* If the system is not using continuous mode or it provides a
329	 * pen down operation then we need to schedule polls while the
330	 * pen is down.  Otherwise the machine driver is responsible
331	 * for scheduling reads.
332	 */
333	if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
334		if (wm->pen_is_down && !pen_was_down) {
335			/* Data is not available immediately on pen down */
336			queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
337		}
338
339		/* Let ts_reader report the pen up for debounce. */
340		if (!wm->pen_is_down && pen_was_down)
341			wm->pen_is_down = 1;
342	}
343
344	if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
345		wm->mach_ops->acc_pen_up(wm);
346
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347	return IRQ_HANDLED;
348}
349
350/*
351 * initialise pen IRQ handler and workqueue
352 */
353static int wm97xx_init_pen_irq(struct wm97xx *wm)
354{
355	u16 reg;
356
357	if (request_threaded_irq(wm->pen_irq, NULL, wm97xx_pen_interrupt,
358				 IRQF_SHARED | IRQF_ONESHOT,
359				 "wm97xx-pen", wm)) {
 
 
 
360		dev_err(wm->dev,
361			"Failed to register pen down interrupt, polling");
362		wm->pen_irq = 0;
363		return -EINVAL;
364	}
365
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);
374	}
375
376	return 0;
377}
378
379static int wm97xx_read_samples(struct wm97xx *wm)
380{
381	struct wm97xx_data data;
382	int rc;
383
384	mutex_lock(&wm->codec_mutex);
385
386	if (wm->mach_ops && wm->mach_ops->acc_enabled)
387		rc = wm->mach_ops->acc_pen_down(wm);
388	else
389		rc = wm->codec->poll_touch(wm, &data);
390
391	if (rc & RC_PENUP) {
392		if (wm->pen_is_down) {
393			wm->pen_is_down = 0;
394			dev_dbg(wm->dev, "pen up\n");
395			input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
396			input_report_key(wm->input_dev, BTN_TOUCH, 0);
397			input_sync(wm->input_dev);
398		} else if (!(rc & RC_AGAIN)) {
399			/* We need high frequency updates only while
400			* pen is down, the user never will be able to
401			* touch screen faster than a few times per
402			* second... On the other hand, when the user
403			* is actively working with the touchscreen we
404			* don't want to lose the quick response. So we
405			* will slowly increase sleep time after the
406			* pen is up and quicky restore it to ~one task
407			* switch when pen is down again.
408			*/
409			if (wm->ts_reader_interval < HZ / 10)
410				wm->ts_reader_interval++;
411		}
412
413	} else if (rc & RC_VALID) {
414		dev_dbg(wm->dev,
415			"pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
416			data.x >> 12, data.x & 0xfff, data.y >> 12,
417			data.y & 0xfff, data.p >> 12, data.p & 0xfff);
418
419		if (abs_x[0] > (data.x & 0xfff) ||
420		    abs_x[1] < (data.x & 0xfff) ||
421		    abs_y[0] > (data.y & 0xfff) ||
422		    abs_y[1] < (data.y & 0xfff)) {
423			dev_dbg(wm->dev, "Measurement out of range, dropping it\n");
424			rc = RC_AGAIN;
425			goto out;
426		}
427
428		input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
429		input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
430		input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
431		input_report_key(wm->input_dev, BTN_TOUCH, 1);
432		input_sync(wm->input_dev);
433		wm->pen_is_down = 1;
434		wm->ts_reader_interval = wm->ts_reader_min_interval;
435	} else if (rc & RC_PENDOWN) {
436		dev_dbg(wm->dev, "pen down\n");
437		wm->pen_is_down = 1;
438		wm->ts_reader_interval = wm->ts_reader_min_interval;
439	}
440
441out:
442	mutex_unlock(&wm->codec_mutex);
443	return rc;
444}
445
446/*
447* The touchscreen sample reader.
448*/
449static void wm97xx_ts_reader(struct work_struct *work)
450{
451	int rc;
452	struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
453
454	BUG_ON(!wm->codec);
455
456	do {
457		rc = wm97xx_read_samples(wm);
458	} while (rc & RC_AGAIN);
459
460	if (wm->pen_is_down || !wm->pen_irq)
461		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
462				   wm->ts_reader_interval);
463}
464
465/**
466 * wm97xx_ts_input_open - Open the touch screen input device.
467 * @idev:	Input device to be opened.
468 *
469 * Called by the input sub system to open a wm97xx touchscreen device.
470 * Starts the touchscreen thread and touch digitiser.
471 */
472static int wm97xx_ts_input_open(struct input_dev *idev)
473{
474	struct wm97xx *wm = input_get_drvdata(idev);
475
476	wm->ts_workq = alloc_ordered_workqueue("kwm97xx", 0);
477	if (wm->ts_workq == NULL) {
478		dev_err(wm->dev,
479			"Failed to create workqueue\n");
480		return -EINVAL;
481	}
482
483	/* start digitiser */
484	if (wm->mach_ops && wm->mach_ops->acc_enabled)
485		wm->codec->acc_enable(wm, 1);
486	wm->codec->dig_enable(wm, 1);
487
488	INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
 
489
490	wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
491	if (wm->ts_reader_min_interval < 1)
492		wm->ts_reader_min_interval = 1;
493	wm->ts_reader_interval = wm->ts_reader_min_interval;
494
495	wm->pen_is_down = 0;
496	if (wm->pen_irq)
497		wm97xx_init_pen_irq(wm);
498	else
499		dev_err(wm->dev, "No IRQ specified\n");
500
501	/* If we either don't have an interrupt for pen down events or
502	 * failed to acquire it then we need to poll.
503	 */
504	if (wm->pen_irq == 0)
505		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
506				   wm->ts_reader_interval);
507
508	return 0;
509}
510
511/**
512 * wm97xx_ts_input_close - Close the touch screen input device.
513 * @idev:	Input device to be closed.
514 *
515 * Called by the input sub system to close a wm97xx touchscreen
516 * device.  Kills the touchscreen thread and stops the touch
517 * digitiser.
518 */
519
520static void wm97xx_ts_input_close(struct input_dev *idev)
521{
522	struct wm97xx *wm = input_get_drvdata(idev);
523	u16 reg;
524
525	if (wm->pen_irq) {
526		/* Return the interrupt to GPIO usage (disabling it) */
527		if (wm->id != WM9705_ID2) {
528			BUG_ON(!wm->mach_ops->irq_gpio);
529			reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
530			wm97xx_reg_write(wm, AC97_MISC_AFE,
531					 reg | wm->mach_ops->irq_gpio);
532		}
533
534		free_irq(wm->pen_irq, wm);
535	}
536
537	wm->pen_is_down = 0;
538
 
 
 
 
539	/* ts_reader rearms itself so we need to explicitly stop it
540	 * before we destroy the workqueue.
541	 */
542	cancel_delayed_work_sync(&wm->ts_reader);
543
544	destroy_workqueue(wm->ts_workq);
545
546	/* stop digitiser */
547	wm->codec->dig_enable(wm, 0);
548	if (wm->mach_ops && wm->mach_ops->acc_enabled)
549		wm->codec->acc_enable(wm, 0);
550}
551
552static int wm97xx_register_touch(struct wm97xx *wm)
553{
554	struct wm97xx_pdata *pdata = dev_get_platdata(wm->dev);
555	int ret;
556
557	wm->input_dev = devm_input_allocate_device(wm->dev);
558	if (wm->input_dev == NULL)
559		return -ENOMEM;
560
561	/* set up touch configuration */
562	wm->input_dev->name = "wm97xx touchscreen";
563	wm->input_dev->phys = "wm97xx";
564	wm->input_dev->open = wm97xx_ts_input_open;
565	wm->input_dev->close = wm97xx_ts_input_close;
566
567	__set_bit(EV_ABS, wm->input_dev->evbit);
568	__set_bit(EV_KEY, wm->input_dev->evbit);
569	__set_bit(BTN_TOUCH, wm->input_dev->keybit);
570
571	input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
572			     abs_x[2], 0);
573	input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
574			     abs_y[2], 0);
575	input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
576			     abs_p[2], 0);
577
578	input_set_drvdata(wm->input_dev, wm);
579	wm->input_dev->dev.parent = wm->dev;
580
581	ret = input_register_device(wm->input_dev);
582	if (ret)
583		return ret;
584
585	/*
586	 * register our extended touch device (for machine specific
587	 * extensions)
588	 */
589	wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
590	if (!wm->touch_dev)
591		return -ENOMEM;
592
593	platform_set_drvdata(wm->touch_dev, wm);
594	wm->touch_dev->dev.parent = wm->dev;
595	wm->touch_dev->dev.platform_data = pdata;
596	ret = platform_device_add(wm->touch_dev);
597	if (ret < 0)
598		goto touch_reg_err;
599
600	return 0;
601touch_reg_err:
602	platform_device_put(wm->touch_dev);
603
604	return ret;
605}
606
607static void wm97xx_unregister_touch(struct wm97xx *wm)
608{
609	platform_device_unregister(wm->touch_dev);
610}
611
612static int _wm97xx_probe(struct wm97xx *wm)
613{
614	int id = 0;
615
616	mutex_init(&wm->codec_mutex);
617	dev_set_drvdata(wm->dev, wm);
 
 
 
618
619	/* check that we have a supported codec */
620	id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
621	if (id != WM97XX_ID1) {
622		dev_err(wm->dev,
623			"Device with vendor %04x is not a wm97xx\n", id);
624		return -ENODEV;
625	}
626
627	wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
628
629	wm->variant = WM97xx_GENERIC;
630
631	dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
632
633	switch (wm->id & 0xff) {
634#ifdef CONFIG_TOUCHSCREEN_WM9705
635	case 0x05:
636		wm->codec = &wm9705_codec;
637		break;
638#endif
639#ifdef CONFIG_TOUCHSCREEN_WM9712
640	case 0x12:
641		wm->codec = &wm9712_codec;
642		break;
643#endif
644#ifdef CONFIG_TOUCHSCREEN_WM9713
645	case 0x13:
646		wm->codec = &wm9713_codec;
647		break;
648#endif
649	default:
650		dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
651			wm->id & 0xff);
652		return -ENODEV;
 
653	}
654
655	/* set up physical characteristics */
656	wm->codec->phy_init(wm);
657
658	/* load gpio cache */
659	wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
660	wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
661	wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
662	wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
663	wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
664	wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
665
666	return wm97xx_register_touch(wm);
667}
 
 
 
668
669static void wm97xx_remove_battery(struct wm97xx *wm)
670{
671	platform_device_unregister(wm->battery_dev);
672}
 
673
674static int wm97xx_add_battery(struct wm97xx *wm,
675			      struct wm97xx_batt_pdata *pdata)
676{
677	int ret;
678
679	wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
680	if (!wm->battery_dev)
681		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
682
 
 
 
 
 
 
683	platform_set_drvdata(wm->battery_dev, wm);
684	wm->battery_dev->dev.parent = wm->dev;
685	wm->battery_dev->dev.platform_data = pdata;
686	ret = platform_device_add(wm->battery_dev);
687	if (ret)
688		platform_device_put(wm->battery_dev);
689
690	return ret;
691}
692
693static int wm97xx_probe(struct device *dev)
694{
695	struct wm97xx *wm;
696	int ret;
697	struct wm97xx_pdata *pdata = dev_get_platdata(dev);
698
699	wm = devm_kzalloc(dev, sizeof(struct wm97xx), GFP_KERNEL);
700	if (!wm)
701		return -ENOMEM;
702
703	wm->dev = dev;
704	wm->ac97 = to_ac97_t(dev);
705
706	ret =  _wm97xx_probe(wm);
707	if (ret)
708		return ret;
709
710	ret = wm97xx_add_battery(wm, pdata ? pdata->batt_pdata : NULL);
 
 
 
 
 
 
 
 
 
 
711	if (ret < 0)
712		goto batt_err;
713
714	return ret;
715
716batt_err:
717	wm97xx_unregister_touch(wm);
 
 
 
 
 
 
 
 
 
 
 
 
718	return ret;
719}
720
721static int wm97xx_remove(struct device *dev)
722{
723	struct wm97xx *wm = dev_get_drvdata(dev);
724
725	wm97xx_remove_battery(wm);
726	wm97xx_unregister_touch(wm);
727
728	return 0;
729}
730
731static int wm97xx_mfd_probe(struct platform_device *pdev)
732{
733	struct wm97xx *wm;
734	struct wm97xx_platform_data *mfd_pdata = dev_get_platdata(&pdev->dev);
735	int ret;
736
737	wm = devm_kzalloc(&pdev->dev, sizeof(struct wm97xx), GFP_KERNEL);
738	if (!wm)
739		return -ENOMEM;
740
741	wm->dev = &pdev->dev;
742	wm->ac97 = mfd_pdata->ac97;
743
744	ret =  _wm97xx_probe(wm);
745	if (ret)
746		return ret;
747
748	ret = wm97xx_add_battery(wm, mfd_pdata->batt_pdata);
749	if (ret < 0)
750		goto batt_err;
751
752	return ret;
753
754batt_err:
755	wm97xx_unregister_touch(wm);
756	return ret;
757}
758
759static int wm97xx_mfd_remove(struct platform_device *pdev)
760{
761	wm97xx_remove(&pdev->dev);
762
763	return 0;
764}
765
766static int __maybe_unused wm97xx_suspend(struct device *dev)
 
767{
768	struct wm97xx *wm = dev_get_drvdata(dev);
769	u16 reg;
770	int suspend_mode;
771
772	if (device_may_wakeup(&wm->input_dev->dev))
773		suspend_mode = wm->suspend_mode;
774	else
775		suspend_mode = 0;
776
777	mutex_lock(&wm->input_dev->mutex);
778	if (input_device_enabled(wm->input_dev))
779		cancel_delayed_work_sync(&wm->ts_reader);
780
781	/* Power down the digitiser (bypassing the cache for resume) */
782	reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
783	reg &= ~WM97XX_PRP_DET_DIG;
784	if (input_device_enabled(wm->input_dev))
785		reg |= suspend_mode;
786	wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
787
788	/* WM9713 has an additional power bit - turn it off if there
789	 * are no users or if suspend mode is zero. */
790	if (wm->id == WM9713_ID2 &&
791	    (!input_device_enabled(wm->input_dev) || !suspend_mode)) {
792		reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
793		wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
794	}
795	mutex_unlock(&wm->input_dev->mutex);
796
797	return 0;
798}
799
800static int __maybe_unused wm97xx_resume(struct device *dev)
801{
802	struct wm97xx *wm = dev_get_drvdata(dev);
803
804	mutex_lock(&wm->input_dev->mutex);
805	/* restore digitiser and gpios */
806	if (wm->id == WM9713_ID2) {
807		wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
808		wm97xx_reg_write(wm, 0x5a, wm->misc);
809		if (input_device_enabled(wm->input_dev)) {
810			u16 reg;
811			reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
812			wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
813		}
814	}
815
816	wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
817	wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
818
819	wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
820	wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
821	wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
822	wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
823	wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
824	wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
825
826	if (input_device_enabled(wm->input_dev) && !wm->pen_irq) {
827		wm->ts_reader_interval = wm->ts_reader_min_interval;
828		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
829				   wm->ts_reader_interval);
830	}
831	mutex_unlock(&wm->input_dev->mutex);
832
833	return 0;
834}
835
836static SIMPLE_DEV_PM_OPS(wm97xx_pm_ops, wm97xx_suspend, wm97xx_resume);
 
 
 
837
838/*
839 * Machine specific operations
840 */
841int wm97xx_register_mach_ops(struct wm97xx *wm,
842			     struct wm97xx_mach_ops *mach_ops)
843{
844	mutex_lock(&wm->codec_mutex);
845	if (wm->mach_ops) {
846		mutex_unlock(&wm->codec_mutex);
847		return -EINVAL;
848	}
849	wm->mach_ops = mach_ops;
850	mutex_unlock(&wm->codec_mutex);
851
852	return 0;
853}
854EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
855
856void wm97xx_unregister_mach_ops(struct wm97xx *wm)
857{
858	mutex_lock(&wm->codec_mutex);
859	wm->mach_ops = NULL;
860	mutex_unlock(&wm->codec_mutex);
861}
862EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
863
864static struct device_driver wm97xx_driver = {
865	.name =		"wm97xx-ts",
866#ifdef CONFIG_AC97_BUS
867	.bus =		&ac97_bus_type,
868#endif
869	.owner =	THIS_MODULE,
870	.probe =	wm97xx_probe,
871	.remove =	wm97xx_remove,
872	.pm =		&wm97xx_pm_ops,
873};
874
875static struct platform_driver wm97xx_mfd_driver = {
876	.driver = {
877		.name =		"wm97xx-ts",
878		.pm =		&wm97xx_pm_ops,
879	},
880	.probe =	wm97xx_mfd_probe,
881	.remove =	wm97xx_mfd_remove,
882};
883
884static int __init wm97xx_init(void)
885{
886	int ret;
887
888	ret = platform_driver_register(&wm97xx_mfd_driver);
889	if (ret)
890		return ret;
891
892	if (IS_BUILTIN(CONFIG_AC97_BUS))
893		ret =  driver_register(&wm97xx_driver);
894	return ret;
895}
896
897static void __exit wm97xx_exit(void)
898{
899	if (IS_BUILTIN(CONFIG_AC97_BUS))
900		driver_unregister(&wm97xx_driver);
901	platform_driver_unregister(&wm97xx_mfd_driver);
902}
903
904module_init(wm97xx_init);
905module_exit(wm97xx_exit);
906
907/* Module information */
908MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
909MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
910MODULE_LICENSE("GPL");
v3.1
 
  1/*
  2 * wm97xx-core.c  --  Touch screen driver core for Wolfson WM9705, WM9712
  3 *                    and WM9713 AC97 Codecs.
  4 *
  5 * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
  6 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  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 * Notes:
 17 *
 18 *  Features:
 19 *       - supports WM9705, WM9712, WM9713
 20 *       - polling mode
 21 *       - continuous mode (arch-dependent)
 22 *       - adjustable rpu/dpp settings
 23 *       - adjustable pressure current
 24 *       - adjustable sample settle delay
 25 *       - 4 and 5 wire touchscreens (5 wire is WM9712 only)
 26 *       - pen down detection
 27 *       - battery monitor
 28 *       - sample AUX adcs
 29 *       - power management
 30 *       - codec GPIO
 31 *       - codec event notification
 32 * Todo
 33 *       - Support for async sampling control for noisy LCDs.
 34 *
 35 */
 36
 37#include <linux/module.h>
 38#include <linux/moduleparam.h>
 39#include <linux/kernel.h>
 40#include <linux/init.h>
 41#include <linux/delay.h>
 42#include <linux/string.h>
 43#include <linux/proc_fs.h>
 44#include <linux/pm.h>
 45#include <linux/interrupt.h>
 46#include <linux/bitops.h>
 
 47#include <linux/workqueue.h>
 48#include <linux/wm97xx.h>
 49#include <linux/uaccess.h>
 50#include <linux/io.h>
 51#include <linux/slab.h>
 52
 53#define TS_NAME			"wm97xx"
 54#define WM_CORE_VERSION		"1.00"
 55#define DEFAULT_PRESSURE	0xb0c0
 56
 57
 58/*
 59 * Touchscreen absolute values
 60 *
 61 * These parameters are used to help the input layer discard out of
 62 * range readings and reduce jitter etc.
 63 *
 64 *   o min, max:- indicate the min and max values your touch screen returns
 65 *   o fuzz:- use a higher number to reduce jitter
 66 *
 67 * The default values correspond to Mainstone II in QVGA mode
 68 *
 69 * Please read
 70 * Documentation/input/input-programming.txt for more details.
 71 */
 72
 73static int abs_x[3] = {350, 3900, 5};
 74module_param_array(abs_x, int, NULL, 0);
 75MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
 76
 77static int abs_y[3] = {320, 3750, 40};
 78module_param_array(abs_y, int, NULL, 0);
 79MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
 80
 81static int abs_p[3] = {0, 150, 4};
 82module_param_array(abs_p, int, NULL, 0);
 83MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
 84
 85/*
 86 * wm97xx IO access, all IO locking done by AC97 layer
 87 */
 88int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
 89{
 90	if (wm->ac97)
 91		return wm->ac97->bus->ops->read(wm->ac97, reg);
 92	else
 93		return -1;
 94}
 95EXPORT_SYMBOL_GPL(wm97xx_reg_read);
 96
 97void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
 98{
 99	/* cache digitiser registers */
100	if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
101		wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
102
103	/* cache gpio regs */
104	if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
105		wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
106
107	/* wm9713 irq reg */
108	if (reg == 0x5a)
109		wm->misc = val;
110
111	if (wm->ac97)
112		wm->ac97->bus->ops->write(wm->ac97, reg, val);
113}
114EXPORT_SYMBOL_GPL(wm97xx_reg_write);
115
116/**
117 * wm97xx_read_aux_adc - Read the aux adc.
118 * @wm: wm97xx device.
119 * @adcsel: codec ADC to be read
120 *
121 * Reads the selected AUX ADC.
122 */
123
124int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
125{
126	int power_adc = 0, auxval;
127	u16 power = 0;
128	int rc = 0;
129	int timeout = 0;
130
131	/* get codec */
132	mutex_lock(&wm->codec_mutex);
133
134	/* When the touchscreen is not in use, we may have to power up
135	 * the AUX ADC before we can use sample the AUX inputs->
136	 */
137	if (wm->id == WM9713_ID2 &&
138	    (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
139		power_adc = 1;
140		wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
141	}
142
143	/* Prepare the codec for AUX reading */
144	wm->codec->aux_prepare(wm);
145
146	/* Turn polling mode on to read AUX ADC */
147	wm->pen_probably_down = 1;
148
149	while (rc != RC_VALID && timeout++ < 5)
150		rc = wm->codec->poll_sample(wm, adcsel, &auxval);
151
152	if (power_adc)
153		wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
154
155	wm->codec->dig_restore(wm);
156
157	wm->pen_probably_down = 0;
158
159	if (timeout >= 5) {
160		dev_err(wm->dev,
161			"timeout reading auxadc %d, disabling digitiser\n",
162			adcsel);
163		wm->codec->dig_enable(wm, false);
164	}
165
166	mutex_unlock(&wm->codec_mutex);
167	return (rc == RC_VALID ? auxval & 0xfff : -EBUSY);
168}
169EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
170
171/**
172 * wm97xx_get_gpio - Get the status of a codec GPIO.
173 * @wm: wm97xx device.
174 * @gpio: gpio
175 *
176 * Get the status of a codec GPIO pin
177 */
178
179enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
180{
181	u16 status;
182	enum wm97xx_gpio_status ret;
183
184	mutex_lock(&wm->codec_mutex);
185	status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
186
187	if (status & gpio)
188		ret = WM97XX_GPIO_HIGH;
189	else
190		ret = WM97XX_GPIO_LOW;
191
192	mutex_unlock(&wm->codec_mutex);
193	return ret;
194}
195EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
196
197/**
198 * wm97xx_set_gpio - Set the status of a codec GPIO.
199 * @wm: wm97xx device.
200 * @gpio: gpio
201 *
202 *
203 * Set the status of a codec GPIO pin
204 */
205
206void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
207				enum wm97xx_gpio_status status)
208{
209	u16 reg;
210
211	mutex_lock(&wm->codec_mutex);
212	reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
213
214	if (status == WM97XX_GPIO_HIGH)
215		reg |= gpio;
216	else
217		reg &= ~gpio;
218
219	if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
220		wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
221	else
222		wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
223	mutex_unlock(&wm->codec_mutex);
224}
225EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
226
227/*
228 * Codec GPIO pin configuration, this sets pin direction, polarity,
229 * stickyness and wake up.
230 */
231void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
232		   enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
233		   enum wm97xx_gpio_wake wake)
234{
235	u16 reg;
236
237	mutex_lock(&wm->codec_mutex);
238	reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
239
240	if (pol == WM97XX_GPIO_POL_HIGH)
241		reg |= gpio;
242	else
243		reg &= ~gpio;
244
245	wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
246	reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
247
248	if (sticky == WM97XX_GPIO_STICKY)
249		reg |= gpio;
250	else
251		reg &= ~gpio;
252
253	wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
254	reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
255
256	if (wake == WM97XX_GPIO_WAKE)
257		reg |= gpio;
258	else
259		reg &= ~gpio;
260
261	wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
262	reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
263
264	if (dir == WM97XX_GPIO_IN)
265		reg |= gpio;
266	else
267		reg &= ~gpio;
268
269	wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
270	mutex_unlock(&wm->codec_mutex);
271}
272EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
273
274/*
275 * Configure the WM97XX_PRP value to use while system is suspended.
276 * If a value other than 0 is set then WM97xx pen detection will be
277 * left enabled in the configured mode while the system is in suspend,
278 * the device has users and suspend has not been disabled via the
279 * wakeup sysfs entries.
280 *
281 * @wm:   WM97xx device to configure
282 * @mode: WM97XX_PRP value to configure while suspended
283 */
284void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
285{
286	wm->suspend_mode = mode;
287	device_init_wakeup(&wm->input_dev->dev, mode != 0);
288}
289EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
290
291/*
292 * Handle a pen down interrupt.
 
293 */
294static void wm97xx_pen_irq_worker(struct work_struct *work)
295{
296	struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
297	int pen_was_down = wm->pen_is_down;
298
299	/* do we need to enable the touch panel reader */
300	if (wm->id == WM9705_ID2) {
301		if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
302					WM97XX_PEN_DOWN)
303			wm->pen_is_down = 1;
304		else
305			wm->pen_is_down = 0;
306	} else {
307		u16 status, pol;
308		mutex_lock(&wm->codec_mutex);
309		status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
310		pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
311
312		if (WM97XX_GPIO_13 & pol & status) {
313			wm->pen_is_down = 1;
314			wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
315						~WM97XX_GPIO_13);
316		} else {
317			wm->pen_is_down = 0;
318			wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
319					 WM97XX_GPIO_13);
320		}
321
322		if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
323			wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
324						~WM97XX_GPIO_13) << 1);
325		else
326			wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
327						~WM97XX_GPIO_13);
328		mutex_unlock(&wm->codec_mutex);
329	}
330
331	/* If the system is not using continuous mode or it provides a
332	 * pen down operation then we need to schedule polls while the
333	 * pen is down.  Otherwise the machine driver is responsible
334	 * for scheduling reads.
335	 */
336	if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
337		if (wm->pen_is_down && !pen_was_down) {
338			/* Data is not available immediately on pen down */
339			queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
340		}
341
342		/* Let ts_reader report the pen up for debounce. */
343		if (!wm->pen_is_down && pen_was_down)
344			wm->pen_is_down = 1;
345	}
346
347	if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
348		wm->mach_ops->acc_pen_up(wm);
349
350	wm->mach_ops->irq_enable(wm, 1);
351}
352
353/*
354 * Codec PENDOWN irq handler
355 *
356 * We have to disable the codec interrupt in the handler because it
357 * can take up to 1ms to clear the interrupt source. We schedule a task
358 * in a work queue to do the actual interaction with the chip.  The
359 * interrupt is then enabled again in the slow handler when the source
360 * has been cleared.
361 */
362static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
363{
364	struct wm97xx *wm = dev_id;
365
366	if (!work_pending(&wm->pen_event_work)) {
367		wm->mach_ops->irq_enable(wm, 0);
368		queue_work(wm->ts_workq, &wm->pen_event_work);
369	}
370
371	return IRQ_HANDLED;
372}
373
374/*
375 * initialise pen IRQ handler and workqueue
376 */
377static int wm97xx_init_pen_irq(struct wm97xx *wm)
378{
379	u16 reg;
380
381	/* If an interrupt is supplied an IRQ enable operation must also be
382	 * provided. */
383	BUG_ON(!wm->mach_ops->irq_enable);
384
385	if (request_irq(wm->pen_irq, wm97xx_pen_interrupt, IRQF_SHARED,
386			"wm97xx-pen", wm)) {
387		dev_err(wm->dev,
388			"Failed to register pen down interrupt, polling");
389		wm->pen_irq = 0;
390		return -EINVAL;
391	}
392
393	/* Configure GPIO as interrupt source on WM971x */
394	if (wm->id != WM9705_ID2) {
395		BUG_ON(!wm->mach_ops->irq_gpio);
396		reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
397		wm97xx_reg_write(wm, AC97_MISC_AFE,
398				reg & ~(wm->mach_ops->irq_gpio));
399		reg = wm97xx_reg_read(wm, 0x5a);
400		wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
401	}
402
403	return 0;
404}
405
406static int wm97xx_read_samples(struct wm97xx *wm)
407{
408	struct wm97xx_data data;
409	int rc;
410
411	mutex_lock(&wm->codec_mutex);
412
413	if (wm->mach_ops && wm->mach_ops->acc_enabled)
414		rc = wm->mach_ops->acc_pen_down(wm);
415	else
416		rc = wm->codec->poll_touch(wm, &data);
417
418	if (rc & RC_PENUP) {
419		if (wm->pen_is_down) {
420			wm->pen_is_down = 0;
421			dev_dbg(wm->dev, "pen up\n");
422			input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
423			input_report_key(wm->input_dev, BTN_TOUCH, 0);
424			input_sync(wm->input_dev);
425		} else if (!(rc & RC_AGAIN)) {
426			/* We need high frequency updates only while
427			* pen is down, the user never will be able to
428			* touch screen faster than a few times per
429			* second... On the other hand, when the user
430			* is actively working with the touchscreen we
431			* don't want to lose the quick response. So we
432			* will slowly increase sleep time after the
433			* pen is up and quicky restore it to ~one task
434			* switch when pen is down again.
435			*/
436			if (wm->ts_reader_interval < HZ / 10)
437				wm->ts_reader_interval++;
438		}
439
440	} else if (rc & RC_VALID) {
441		dev_dbg(wm->dev,
442			"pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
443			data.x >> 12, data.x & 0xfff, data.y >> 12,
444			data.y & 0xfff, data.p >> 12, data.p & 0xfff);
 
 
 
 
 
 
 
 
 
 
445		input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
446		input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
447		input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
448		input_report_key(wm->input_dev, BTN_TOUCH, 1);
449		input_sync(wm->input_dev);
450		wm->pen_is_down = 1;
451		wm->ts_reader_interval = wm->ts_reader_min_interval;
452	} else if (rc & RC_PENDOWN) {
453		dev_dbg(wm->dev, "pen down\n");
454		wm->pen_is_down = 1;
455		wm->ts_reader_interval = wm->ts_reader_min_interval;
456	}
457
 
458	mutex_unlock(&wm->codec_mutex);
459	return rc;
460}
461
462/*
463* The touchscreen sample reader.
464*/
465static void wm97xx_ts_reader(struct work_struct *work)
466{
467	int rc;
468	struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
469
470	BUG_ON(!wm->codec);
471
472	do {
473		rc = wm97xx_read_samples(wm);
474	} while (rc & RC_AGAIN);
475
476	if (wm->pen_is_down || !wm->pen_irq)
477		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
478				   wm->ts_reader_interval);
479}
480
481/**
482 * wm97xx_ts_input_open - Open the touch screen input device.
483 * @idev:	Input device to be opened.
484 *
485 * Called by the input sub system to open a wm97xx touchscreen device.
486 * Starts the touchscreen thread and touch digitiser.
487 */
488static int wm97xx_ts_input_open(struct input_dev *idev)
489{
490	struct wm97xx *wm = input_get_drvdata(idev);
491
492	wm->ts_workq = create_singlethread_workqueue("kwm97xx");
493	if (wm->ts_workq == NULL) {
494		dev_err(wm->dev,
495			"Failed to create workqueue\n");
496		return -EINVAL;
497	}
498
499	/* start digitiser */
500	if (wm->mach_ops && wm->mach_ops->acc_enabled)
501		wm->codec->acc_enable(wm, 1);
502	wm->codec->dig_enable(wm, 1);
503
504	INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
505	INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
506
507	wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
508	if (wm->ts_reader_min_interval < 1)
509		wm->ts_reader_min_interval = 1;
510	wm->ts_reader_interval = wm->ts_reader_min_interval;
511
512	wm->pen_is_down = 0;
513	if (wm->pen_irq)
514		wm97xx_init_pen_irq(wm);
515	else
516		dev_err(wm->dev, "No IRQ specified\n");
517
518	/* If we either don't have an interrupt for pen down events or
519	 * failed to acquire it then we need to poll.
520	 */
521	if (wm->pen_irq == 0)
522		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
523				   wm->ts_reader_interval);
524
525	return 0;
526}
527
528/**
529 * wm97xx_ts_input_close - Close the touch screen input device.
530 * @idev:	Input device to be closed.
531 *
532 * Called by the input sub system to close a wm97xx touchscreen
533 * device.  Kills the touchscreen thread and stops the touch
534 * digitiser.
535 */
536
537static void wm97xx_ts_input_close(struct input_dev *idev)
538{
539	struct wm97xx *wm = input_get_drvdata(idev);
540	u16 reg;
541
542	if (wm->pen_irq) {
543		/* Return the interrupt to GPIO usage (disabling it) */
544		if (wm->id != WM9705_ID2) {
545			BUG_ON(!wm->mach_ops->irq_gpio);
546			reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
547			wm97xx_reg_write(wm, AC97_MISC_AFE,
548					 reg | wm->mach_ops->irq_gpio);
549		}
550
551		free_irq(wm->pen_irq, wm);
552	}
553
554	wm->pen_is_down = 0;
555
556	/* Balance out interrupt disables/enables */
557	if (cancel_work_sync(&wm->pen_event_work))
558		wm->mach_ops->irq_enable(wm, 1);
559
560	/* ts_reader rearms itself so we need to explicitly stop it
561	 * before we destroy the workqueue.
562	 */
563	cancel_delayed_work_sync(&wm->ts_reader);
564
565	destroy_workqueue(wm->ts_workq);
566
567	/* stop digitiser */
568	wm->codec->dig_enable(wm, 0);
569	if (wm->mach_ops && wm->mach_ops->acc_enabled)
570		wm->codec->acc_enable(wm, 0);
571}
572
573static int wm97xx_probe(struct device *dev)
574{
575	struct wm97xx *wm;
576	struct wm97xx_pdata *pdata = dev->platform_data;
577	int ret = 0, id = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
579	wm = kzalloc(sizeof(struct wm97xx), GFP_KERNEL);
580	if (!wm)
 
 
 
 
581		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582	mutex_init(&wm->codec_mutex);
583
584	wm->dev = dev;
585	dev_set_drvdata(dev, wm);
586	wm->ac97 = to_ac97_t(dev);
587
588	/* check that we have a supported codec */
589	id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
590	if (id != WM97XX_ID1) {
591		dev_err(dev, "Device with vendor %04x is not a wm97xx\n", id);
592		ret = -ENODEV;
593		goto alloc_err;
594	}
595
596	wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
597
598	wm->variant = WM97xx_GENERIC;
599
600	dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
601
602	switch (wm->id & 0xff) {
603#ifdef CONFIG_TOUCHSCREEN_WM9705
604	case 0x05:
605		wm->codec = &wm9705_codec;
606		break;
607#endif
608#ifdef CONFIG_TOUCHSCREEN_WM9712
609	case 0x12:
610		wm->codec = &wm9712_codec;
611		break;
612#endif
613#ifdef CONFIG_TOUCHSCREEN_WM9713
614	case 0x13:
615		wm->codec = &wm9713_codec;
616		break;
617#endif
618	default:
619		dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
620			wm->id & 0xff);
621		ret = -ENODEV;
622		goto alloc_err;
623	}
624
625	/* set up physical characteristics */
626	wm->codec->phy_init(wm);
627
628	/* load gpio cache */
629	wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
630	wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
631	wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
632	wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
633	wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
634	wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
635
636	wm->input_dev = input_allocate_device();
637	if (wm->input_dev == NULL) {
638		ret = -ENOMEM;
639		goto alloc_err;
640	}
641
642	/* set up touch configuration */
643	wm->input_dev->name = "wm97xx touchscreen";
644	wm->input_dev->phys = "wm97xx";
645	wm->input_dev->open = wm97xx_ts_input_open;
646	wm->input_dev->close = wm97xx_ts_input_close;
647
648	__set_bit(EV_ABS, wm->input_dev->evbit);
649	__set_bit(EV_KEY, wm->input_dev->evbit);
650	__set_bit(BTN_TOUCH, wm->input_dev->keybit);
 
651
652	input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
653			     abs_x[2], 0);
654	input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
655			     abs_y[2], 0);
656	input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
657			     abs_p[2], 0);
658
659	input_set_drvdata(wm->input_dev, wm);
660	wm->input_dev->dev.parent = dev;
661
662	ret = input_register_device(wm->input_dev);
663	if (ret < 0)
664		goto dev_alloc_err;
665
666	/* register our battery device */
667	wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
668	if (!wm->battery_dev) {
669		ret = -ENOMEM;
670		goto batt_err;
671	}
672	platform_set_drvdata(wm->battery_dev, wm);
673	wm->battery_dev->dev.parent = dev;
674	wm->battery_dev->dev.platform_data = pdata;
675	ret = platform_device_add(wm->battery_dev);
676	if (ret < 0)
677		goto batt_reg_err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
678
679	/* register our extended touch device (for machine specific
680	 * extensions) */
681	wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
682	if (!wm->touch_dev) {
683		ret = -ENOMEM;
684		goto touch_err;
685	}
686	platform_set_drvdata(wm->touch_dev, wm);
687	wm->touch_dev->dev.parent = dev;
688	wm->touch_dev->dev.platform_data = pdata;
689	ret = platform_device_add(wm->touch_dev);
690	if (ret < 0)
691		goto touch_reg_err;
692
693	return ret;
694
695 touch_reg_err:
696	platform_device_put(wm->touch_dev);
697 touch_err:
698	platform_device_del(wm->battery_dev);
699 batt_reg_err:
700	platform_device_put(wm->battery_dev);
701 batt_err:
702	input_unregister_device(wm->input_dev);
703	wm->input_dev = NULL;
704 dev_alloc_err:
705	input_free_device(wm->input_dev);
706 alloc_err:
707	kfree(wm);
708
709	return ret;
710}
711
712static int wm97xx_remove(struct device *dev)
713{
714	struct wm97xx *wm = dev_get_drvdata(dev);
715
716	platform_device_unregister(wm->battery_dev);
717	platform_device_unregister(wm->touch_dev);
718	input_unregister_device(wm->input_dev);
719	kfree(wm);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
720
721	return 0;
722}
723
724#ifdef CONFIG_PM
725static int wm97xx_suspend(struct device *dev, pm_message_t state)
726{
727	struct wm97xx *wm = dev_get_drvdata(dev);
728	u16 reg;
729	int suspend_mode;
730
731	if (device_may_wakeup(&wm->input_dev->dev))
732		suspend_mode = wm->suspend_mode;
733	else
734		suspend_mode = 0;
735
736	if (wm->input_dev->users)
 
737		cancel_delayed_work_sync(&wm->ts_reader);
738
739	/* Power down the digitiser (bypassing the cache for resume) */
740	reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
741	reg &= ~WM97XX_PRP_DET_DIG;
742	if (wm->input_dev->users)
743		reg |= suspend_mode;
744	wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
745
746	/* WM9713 has an additional power bit - turn it off if there
747	 * are no users or if suspend mode is zero. */
748	if (wm->id == WM9713_ID2 &&
749	    (!wm->input_dev->users || !suspend_mode)) {
750		reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
751		wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
752	}
 
753
754	return 0;
755}
756
757static int wm97xx_resume(struct device *dev)
758{
759	struct wm97xx *wm = dev_get_drvdata(dev);
760
 
761	/* restore digitiser and gpios */
762	if (wm->id == WM9713_ID2) {
763		wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
764		wm97xx_reg_write(wm, 0x5a, wm->misc);
765		if (wm->input_dev->users) {
766			u16 reg;
767			reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
768			wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
769		}
770	}
771
772	wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
773	wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
774
775	wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
776	wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
777	wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
778	wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
779	wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
780	wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
781
782	if (wm->input_dev->users && !wm->pen_irq) {
783		wm->ts_reader_interval = wm->ts_reader_min_interval;
784		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
785				   wm->ts_reader_interval);
786	}
 
787
788	return 0;
789}
790
791#else
792#define wm97xx_suspend		NULL
793#define wm97xx_resume		NULL
794#endif
795
796/*
797 * Machine specific operations
798 */
799int wm97xx_register_mach_ops(struct wm97xx *wm,
800			     struct wm97xx_mach_ops *mach_ops)
801{
802	mutex_lock(&wm->codec_mutex);
803	if (wm->mach_ops) {
804		mutex_unlock(&wm->codec_mutex);
805		return -EINVAL;
806	}
807	wm->mach_ops = mach_ops;
808	mutex_unlock(&wm->codec_mutex);
809
810	return 0;
811}
812EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
813
814void wm97xx_unregister_mach_ops(struct wm97xx *wm)
815{
816	mutex_lock(&wm->codec_mutex);
817	wm->mach_ops = NULL;
818	mutex_unlock(&wm->codec_mutex);
819}
820EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
821
822static struct device_driver wm97xx_driver = {
823	.name =		"wm97xx-ts",
 
824	.bus =		&ac97_bus_type,
 
825	.owner =	THIS_MODULE,
826	.probe =	wm97xx_probe,
827	.remove =	wm97xx_remove,
828	.suspend =	wm97xx_suspend,
829	.resume =	wm97xx_resume,
 
 
 
 
 
 
 
 
830};
831
832static int __init wm97xx_init(void)
833{
834	return driver_register(&wm97xx_driver);
 
 
 
 
 
 
 
 
835}
836
837static void __exit wm97xx_exit(void)
838{
839	driver_unregister(&wm97xx_driver);
 
 
840}
841
842module_init(wm97xx_init);
843module_exit(wm97xx_exit);
844
845/* Module information */
846MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
847MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
848MODULE_LICENSE("GPL");