summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/tsc2007.c
blob: f59fcff2b6f413230408619b6afbf5576c80e6f9 (plain)
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
/*
 * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file tsc2007.c
 *
 * @brief Driver for TI's tsc2007 I2C Touch Screen Controller.
 *
 * This driver is based on the driver written by Bill Gatliff
 *  Copyright (C) 2005 Bill Gatliff <bgat at billgatliff.com>
 *  Changes for 2.6.20 kernel by Nicholas Chen <nchen at cs.umd.edu>
 *
 * @ingroup touchscreen
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/string.h>
#include <linux/bcd.h>
#include <linux/list.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/kthread.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <asm/mach/irq.h>

#define DRIVER_NAME "TSC2007"

enum tsc2007_pd {
	PD_POWERDOWN = 0,	/* penirq */
	PD_IREFOFF_ADCON = 1,	/* no penirq */
	PD_IREFON_ADCOFF = 2,	/* penirq */
	PD_IREFON_ADCON = 3,	/* no penirq */
	PD_PENIRQ_ARM = PD_IREFON_ADCOFF,
	PD_PENIRQ_DISARM = PD_IREFON_ADCON,
};

enum tsc2007_m {
	M_12BIT = 0,
	M_8BIT = 1
};

enum tsc2007_cmd {
	MEAS_TEMP0 = 0,
	MEAS_IN1 = 2,
	MEAS_XPOS = 12,
	MEAS_YPOS = 13,
	MEAS_Z1POS = 14,
	MEAS_Z2POS = 15
};

#define tsc2007_CMD(cn, pdn, m) (((cn) << 4) | ((pdn) << 2) | ((m) << 1))

#define ADC_MAX ((1 << 12) - 1)

struct tsc2007_data {
	struct i2c_client *client;
	struct input_dev *idev;
	struct timer_list penirq_timer;
	struct task_struct *tstask;
	u32 ts_thread_cnt;
	struct completion penirq_completion;
	struct completion penup_completion;
	enum tsc2007_m m;
	int penirq;
};

static int tsc2007_read(struct tsc2007_data *data,
			enum tsc2007_cmd cmd, enum tsc2007_pd pd, int *val)
{
	unsigned char c;
	unsigned char d[2];
	int ret;

	c = tsc2007_CMD(cmd, pd, data->m);

	ret = i2c_master_send(data->client, &c, 1);

	if (ret < 0)
		goto err;

	udelay(20);
	ret = i2c_master_recv(data->client, d, data->m == M_12BIT ? 2 : 1);
	if (ret < 0)
		goto err;

	if (val) {
		*val = d[0];
		*val <<= 4;
		if (data->m == M_12BIT)
			*val += (d[1] >> 4);
	}

	return 0;
err:
	return -ENODEV;
}

static inline int tsc2007_read_xpos(struct tsc2007_data *d, enum
				    tsc2007_pd pd, int *x)
{
	return tsc2007_read(d, MEAS_XPOS, pd, x);
}

static inline int tsc2007_read_ypos(struct tsc2007_data *d, enum
				    tsc2007_pd pd, int *y)
{
	return tsc2007_read(d, MEAS_YPOS, pd, y);
}

static inline int tsc2007_read_pressure(struct tsc2007_data *d, enum
					tsc2007_pd pd, int *p)
{
	return tsc2007_read(d, MEAS_Z1POS, pd, p);
}

static inline int tsc2007_powerdown(struct tsc2007_data *d)
{
	/* we don't have a distinct powerdown command,
	   so do a benign read with the PD bits cleared */
	return tsc2007_read(d, MEAS_IN1, PD_POWERDOWN, 0);
}

#define PENUP_TIMEOUT		10

static irqreturn_t tsc2007_penirq(int irq, void *v)
{
	struct tsc2007_data *d = v;

	disable_irq(d->penirq);
	complete(&d->penirq_completion);
	return IRQ_HANDLED;
}

static void tsc2007_pen_up(unsigned long v)
{
	struct tsc2007_data *d = (struct tsc2007_data *)v;

	complete(&d->penup_completion);
	return;
}

static inline void tsc2007_restart_pen_up_timer(struct tsc2007_data *d)
{
	mod_timer(&d->penirq_timer, jiffies + (PENUP_TIMEOUT * HZ) / 1000);
}

static int tsc2007ts_thread(void *v)
{
	struct tsc2007_data *d = v;

	if (d->ts_thread_cnt)
		return -EINVAL;
	d->ts_thread_cnt = 1;

	while (1) {
		unsigned int x = 0, y = 0, p = 0;

		/* Wait for an Pen down interrupt */
		wait_for_completion(&d->penirq_completion);
		tsc2007_read_xpos(d, PD_PENIRQ_DISARM, &x);
		tsc2007_read_ypos(d, PD_PENIRQ_DISARM, &y);
		tsc2007_read_pressure(d, PD_PENIRQ_DISARM, &p);
		input_report_abs(d->idev, ABS_X, 4096 - x);
		input_report_abs(d->idev, ABS_Y, 4096 - y);
		input_report_abs(d->idev, ABS_PRESSURE, p);
		input_sync(d->idev);

		while (p > 10) {
			tsc2007_restart_pen_up_timer(d);
			wait_for_completion_interruptible(&d->penup_completion);
			/* Pen Down */
			tsc2007_read_xpos(d, PD_PENIRQ_DISARM, &x);
			tsc2007_read_ypos(d, PD_PENIRQ_DISARM, &y);
			tsc2007_read_pressure(d, PD_PENIRQ_DISARM, &p);
			if (p <= 10)
				break;

			input_report_abs(d->idev, ABS_X, 4096 - x);
			input_report_abs(d->idev, ABS_Y, 4096 - y);
			input_report_abs(d->idev, ABS_PRESSURE, p);
			input_sync(d->idev);
		};

		/* Pen Up */
		input_report_abs(d->idev, ABS_X, 4096 - x);
		input_report_abs(d->idev, ABS_Y, 4096 - y);
		input_report_abs(d->idev, ABS_PRESSURE, 0);
		input_sync(d->idev);

		tsc2007_read(d, MEAS_TEMP0, PD_PENIRQ_ARM, 0);
		enable_irq(d->penirq);

		if (kthread_should_stop())
			break;
	}

	d->ts_thread_cnt = 0;
	return 0;
}

static int tsc2007_idev_open(struct input_dev *idev)
{
	struct tsc2007_data *d = idev->private;
	int ret = 0;

	d->penirq_timer.data = (unsigned long)d;
	d->penirq_timer.function = tsc2007_pen_up;

	d->tstask = kthread_run(tsc2007ts_thread, d, DRIVER_NAME "tsd");
	if (IS_ERR(d->tstask))
		ret = PTR_ERR(d->tstask);

	return ret;
}

static int tsc2007_driver_register(struct tsc2007_data *data)
{
	struct input_dev *idev;
	int ret = 0;

	init_timer(&data->penirq_timer);
	data->penirq_timer.data = (unsigned long)data;
	data->penirq_timer.function = tsc2007_pen_up;

	if (data->penirq) {
		ret =
		    request_irq(data->penirq, tsc2007_penirq, IRQT_LOW,
				DRIVER_NAME, data);
		if (!ret) {
			printk(KERN_INFO "%s: Registering Touchscreen device\n",
			       __func__);
			init_completion(&data->penirq_completion);
			init_completion(&data->penup_completion);
		} else {
			printk(KERN_ERR "%s: Cannot grab irq %d\n",
			       __func__, data->penirq);
		}
	}
	idev = input_allocate_device();
	data->idev = idev;
	idev->private = data;
	idev->name = DRIVER_NAME;
	idev->evbit[0] = BIT(EV_ABS);
	idev->open = tsc2007_idev_open;
	idev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
	input_set_abs_params(idev, ABS_X, 0, ADC_MAX, 0, 0);
	input_set_abs_params(idev, ABS_Y, 0, ADC_MAX, 0, 0);
	input_set_abs_params(idev, ABS_PRESSURE, 0, 0, 0, 0);

	if (!ret)
		ret = input_register_device(idev);

	return ret;
}

static int tsc2007_i2c_remove(struct i2c_client *client)
{
	int err;
	struct tsc2007_data *d = i2c_get_clientdata(client);

	kthread_stop(d->tstask);
	free_irq(d->penirq, d);
	input_unregister_device(d->idev);

	err = i2c_detach_client(client);
	if (err) {
		dev_err(&client->dev, "Client deregistration failed, "
			"client not detached.\n");
		return err;
	}

	return 0;
}

static int tsc2007_i2c_probe(struct i2c_client *client)
{
	struct tsc2007_data *data;
	int err = 0;

	data = kzalloc(sizeof(struct tsc2007_data), GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	i2c_set_clientdata(client, data);
	data->client = client;
	data->penirq = client->irq;

	err = tsc2007_powerdown(data);
	if (err >= 0) {
		data->m = M_12BIT;

		err = tsc2007_driver_register(data);
		if (err < 0)
			goto exit;

		return 0;
	}

exit:
	return err;
}

static struct i2c_driver tsc2007_driver = {
	.driver = {
		   .name = DRIVER_NAME,
		   },
	.probe = tsc2007_i2c_probe,
	.remove = tsc2007_i2c_remove,
	.command = NULL,
};

static int __init tsc2007_init(void)
{
	return i2c_add_driver(&tsc2007_driver);
}

static void __exit tsc2007_exit(void)
{
	i2c_del_driver(&tsc2007_driver);
}

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("tsc2007 Touch Screen Controller driver");
MODULE_LICENSE("GPL");

module_init(tsc2007_init);
module_exit(tsc2007_exit);