aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/corgi_ts.c
blob: d0e13fc4a88c0391976859d7561a69f381c17eac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 *  Touchscreen driver for Sharp SL-C7xx and SL-Cxx00 models
 *
 *  Copyright (c) 2004-2005 Richard Purdie
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 */


#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/irq.h>

#include <asm/arch/sharpsl.h>
#include <asm/arch/hardware.h>
#include <asm/arch/pxa-regs.h>
#include <asm/arch/pxa2xx-gpio.h>


#define PWR_MODE_ACTIVE		0
#define PWR_MODE_SUSPEND	1

#define X_AXIS_MAX		3830
#define X_AXIS_MIN		150
#define Y_AXIS_MAX		3830
#define Y_AXIS_MIN		190
#define PRESSURE_MIN		0
#define PRESSURE_MAX		15000

struct ts_event {
	short pressure;
	short x;
	short y;
};

struct corgi_ts {
	struct input_dev *input;
	struct timer_list timer;
	struct ts_event tc;
	int pendown;
	int power_mode;
	int irq_gpio;
	struct corgits_machinfo *machinfo;
};

#ifdef CONFIG_PXA25x
#define CCNT(a)		asm volatile ("mrc p14, 0, %0, C1, C0, 0" : "=r"(a))
#define PMNC_GET(x)	asm volatile ("mrc p14, 0, %0, C0, C0, 0" : "=r"(x))
#define PMNC_SET(x)	asm volatile ("mcr p14, 0, %0, C0, C0, 0" : : "r"(x))
#endif
#ifdef CONFIG_PXA27x
#define CCNT(a)		asm volatile ("mrc p14, 0, %0, C1, C1, 0" : "=r"(a))
#define PMNC_GET(x)	asm volatile ("mrc p14, 0, %0, C0, C1, 0" : "=r"(x))
#define PMNC_SET(x)	asm volatile ("mcr p14, 0, %0, C0, C1, 0" : : "r"(x))
#endif

/* ADS7846 Touch Screen Controller bit definitions */
#define ADSCTRL_PD0		(1u << 0)	/* PD0 */
#define ADSCTRL_PD1		(1u << 1)	/* PD1 */
#define ADSCTRL_DFR		(1u << 2)	/* SER/DFR */
#define ADSCTRL_MOD		(1u << 3)	/* Mode */
#define ADSCTRL_ADR_SH	4	/* Address setting */
#define ADSCTRL_STS		(1u << 7)	/* Start Bit */

/* External Functions */
extern unsigned int get_clk_frequency_khz(int info);

static unsigned long calc_waittime(struct corgi_ts *corgi_ts)
{
	unsigned long hsync_invperiod = corgi_ts->machinfo->get_hsync_invperiod();

	if (hsync_invperiod)
		return get_clk_frequency_khz(0)*1000/hsync_invperiod;
	else
		return 0;
}

static int sync_receive_data_send_cmd(struct corgi_ts *corgi_ts, int doRecive, int doSend,
		unsigned int address, unsigned long wait_time)
{
	unsigned long timer1 = 0, timer2, pmnc = 0;
	int pos = 0;

	if (wait_time && doSend) {
		PMNC_GET(pmnc);
		if (!(pmnc & 0x01))
			PMNC_SET(0x01);

		/* polling HSync */
		corgi_ts->machinfo->wait_hsync();
		/* get CCNT */
		CCNT(timer1);
	}

	if (doRecive)
		pos = corgi_ssp_ads7846_get();

	if (doSend) {
		int cmd = ADSCTRL_PD0 | ADSCTRL_PD1 | (address << ADSCTRL_ADR_SH) | ADSCTRL_STS;
		/* dummy command */
		corgi_ssp_ads7846_put(cmd);
		corgi_ssp_ads7846_get();

		if (wait_time) {
			/* Wait after HSync */
			CCNT(timer2);
			if (timer2-timer1 > wait_time) {
				/* too slow - timeout, try again */
				corgi_ts->machinfo->wait_hsync();
				/* get CCNT */
				CCNT(timer1);
				/* Wait after HSync */
				CCNT(timer2);
			}
			while (timer2 - timer1 < wait_time)
				CCNT(timer2);
		}
		corgi_ssp_ads7846_put(cmd);
		if (wait_time && !(pmnc & 0x01))
			PMNC_SET(pmnc);
	}
	return pos;
}

static int read_xydata(struct corgi_ts *corgi_ts)
{
	unsigned int x, y, z1, z2;
	unsigned long flags, wait_time;

	/* critical section */
	local_irq_save(flags);
	corgi_ssp_ads7846_lock();
	wait_time = calc_waittime(corgi_ts);

	/* Y-axis */
	sync_receive_data_send_cmd(corgi_ts, 0, 1, 1u, wait_time);

	/* Y-axis */
	sync_receive_data_send_cmd(corgi_ts, 1, 1, 1u, wait_time);

	/* X-axis */
	y = sync_receive_data_send_cmd(corgi_ts, 1, 1, 5u, wait_time);

	/* Z1 */
	x = sync_receive_data_send_cmd(corgi_ts, 1, 1, 3u, wait_time);

	/* Z2 */
	z1 = sync_receive_data_send_cmd(corgi_ts, 1, 1, 4u, wait_time);
	z2 = sync_receive_data_send_cmd(corgi_ts, 1, 0, 4u, wait_time);

	/* Power-Down Enable */
	corgi_ssp_ads7846_put((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
	corgi_ssp_ads7846_get();

	corgi_ssp_ads7846_unlock();
	local_irq_restore(flags);

	if (x== 0 || y == 0 || z1 == 0 || (x * (z2 - z1) / z1) >= 15000) {
		corgi_ts->tc.pressure = 0;
		return 0;
	}

	corgi_ts->tc.x = x;
	corgi_ts->tc.y = y;
	corgi_ts->tc.pressure = (x * (z2 - z1)) / z1;
	return 1;
}

static void new_data(struct corgi_ts *corgi_ts)
{
	struct input_dev *dev = corgi_ts->input;

	if (corgi_ts->power_mode != PWR_MODE_ACTIVE)
		return;

	if (!corgi_ts->tc.pressure && corgi_ts->pendown == 0)
		return;

	input_report_abs(dev, ABS_X, corgi_ts->tc.x);
	input_report_abs(dev, ABS_Y, corgi_ts->tc.y);
	input_report_abs(dev, ABS_PRESSURE, corgi_ts->tc.pressure);
	input_report_key(dev, BTN_TOUCH, corgi_ts->pendown);
	input_sync(dev);
}

static void ts_interrupt_main(struct corgi_ts *corgi_ts, int isTimer)
{
	if ((GPLR(IRQ_TO_GPIO(corgi_ts->irq_gpio)) & GPIO_bit(IRQ_TO_GPIO(corgi_ts->irq_gpio))) == 0) {
		/* Disable Interrupt */
		set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_NONE);
		if (read_xydata(corgi_ts)) {
			corgi_ts->pendown = 1;
			new_data(corgi_ts);
		}
		mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
	} else {
		if (corgi_ts->pendown == 1 || corgi_ts->pendown == 2) {
			mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
			corgi_ts->pendown++;
			return;
		}

		if (corgi_ts->pendown) {
			corgi_ts->tc.pressure = 0;
			new_data(corgi_ts);
		}

		/* Enable Falling Edge */
		set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_EDGE_FALLING);
		corgi_ts->pendown = 0;
	}
}

static void corgi_ts_timer(unsigned long data)
{
	struct corgi_ts *corgits_data = (struct corgi_ts *) data;

	ts_interrupt_main(corgits_data, 1);
}

static irqreturn_t ts_interrupt(int irq, void *dev_id)
{
	struct corgi_ts *corgits_data = dev_id;

	ts_interrupt_main(corgits_data, 0);
	return IRQ_HANDLED;
}

#ifdef CONFIG_PM
static int corgits_suspend(struct platform_device *dev, pm_message_t state)
{
	struct corgi_ts *corgi_ts = platform_get_drvdata(dev);

	if (corgi_ts->pendown) {
		del_timer_sync(&corgi_ts->timer);
		corgi_ts->tc.pressure = 0;
		new_data(corgi_ts);
		corgi_ts->pendown = 0;
	}
	corgi_ts->power_mode = PWR_MODE_SUSPEND;

	corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);

	return 0;
}

static int corgits_resume(struct platform_device *dev)
{
	struct corgi_ts *corgi_ts = platform_get_drvdata(dev);

	corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
	/* Enable Falling Edge */
	set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_EDGE_FALLING);
	corgi_ts->power_mode = PWR_MODE_ACTIVE;

	return 0;
}
#else
#define corgits_suspend		NULL
#define corgits_resume		NULL
#endif

static int __init corgits_probe(struct platform_device *pdev)
{
	struct corgi_ts *corgi_ts;
	struct input_dev *input_dev;
	int err = -ENOMEM;

	corgi_ts = kzalloc(sizeof(struct corgi_ts), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!corgi_ts || !input_dev)
		goto fail1;

	platform_set_drvdata(pdev, corgi_ts);

	corgi_ts->machinfo = pdev->dev.platform_data;
	corgi_ts->irq_gpio = platform_get_irq(pdev, 0);

	if (corgi_ts->irq_gpio < 0) {
		err = -ENODEV;
		goto fail1;
	}

	corgi_ts->input = input_dev;

	init_timer(&corgi_ts->timer);
	corgi_ts->timer.data = (unsigned long) corgi_ts;
	corgi_ts->timer.function = corgi_ts_timer;

	input_dev->name = "Corgi Touchscreen";
	input_dev->phys = "corgits/input0";
	input_dev->id.bustype = BUS_HOST;
	input_dev->id.vendor = 0x0001;
	input_dev->id.product = 0x0002;
	input_dev->id.version = 0x0100;
	input_dev->dev.parent = &pdev->dev;

	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
	input_set_abs_params(input_dev, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
	input_set_abs_params(input_dev, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
	input_set_abs_params(input_dev, ABS_PRESSURE, PRESSURE_MIN, PRESSURE_MAX, 0, 0);

	pxa_gpio_mode(IRQ_TO_GPIO(corgi_ts->irq_gpio) | GPIO_IN);

	/* Initiaize ADS7846 Difference Reference mode */
	corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
	mdelay(5);
	corgi_ssp_ads7846_putget((3u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
	mdelay(5);
	corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
	mdelay(5);
	corgi_ssp_ads7846_putget((5u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
	mdelay(5);

	if (request_irq(corgi_ts->irq_gpio, ts_interrupt, IRQF_DISABLED, "ts", corgi_ts)) {
		err = -EBUSY;
		goto fail1;
	}

	err = input_register_device(corgi_ts->input);
	if (err)
		goto fail2;

	corgi_ts->power_mode = PWR_MODE_ACTIVE;

	/* Enable Falling Edge */
	set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_EDGE_FALLING);

	return 0;

 fail2:	free_irq(corgi_ts->irq_gpio, corgi_ts);
 fail1:	input_free_device(input_dev);
	kfree(corgi_ts);
	return err;
}

static int corgits_remove(struct platform_device *pdev)
{
	struct corgi_ts *corgi_ts = platform_get_drvdata(pdev);

	free_irq(corgi_ts->irq_gpio, corgi_ts);
	del_timer_sync(&corgi_ts->timer);
	corgi_ts->machinfo->put_hsync();
	input_unregister_device(corgi_ts->input);
	kfree(corgi_ts);
	return 0;
}

static struct platform_driver corgits_driver = {
	.probe		= corgits_probe,
	.remove		= corgits_remove,
	.suspend	= corgits_suspend,
	.resume		= corgits_resume,
	.driver		= {
		.name	= "corgi-ts",
		.owner	= THIS_MODULE,
	},
};

static int __devinit corgits_init(void)
{
	return platform_driver_register(&corgits_driver);
}

static void __exit corgits_exit(void)
{
	platform_driver_unregister(&corgits_driver);
}

module_init(corgits_init);
module_exit(corgits_exit);

MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
MODULE_DESCRIPTION("Corgi TouchScreen Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:corgi-ts");