1 /*
2  * drivers/input/touchscreen/tsc2007.c
3  *
4  * Copyright (c) 2008 MtekVision Co., Ltd.
5  *	Kwangwoo Lee <kwlee@mtekvision.com>
6  *
7  * Using code from:
8  *  - ads7846.c
9  *	Copyright (c) 2005 David Brownell
10  *	Copyright (c) 2006 Nokia Corporation
11  *  - corgi_ts.c
12  *	Copyright (C) 2004-2005 Richard Purdie
13  *  - omap_ts.[hc], ads7846.h, ts_osk.c
14  *	Copyright (C) 2002 MontaVista Software
15  *	Copyright (C) 2004 Texas Instruments
16  *	Copyright (C) 2005 Dirk Behme
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License version 2 as
20  *  published by the Free Software Foundation.
21  */
22 
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
27 #include <linux/i2c.h>
28 #include <linux/of_device.h>
29 #include <linux/of_gpio.h>
30 #include <linux/platform_data/tsc2007.h>
31 #include "tsc2007.h"
32 
33 int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
34 {
35 	s32 data;
36 	u16 val;
37 
38 	data = i2c_smbus_read_word_data(tsc->client, cmd);
39 	if (data < 0) {
40 		dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
41 		return data;
42 	}
43 
44 	/* The protocol and raw data format from i2c interface:
45 	 * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
46 	 * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
47 	 */
48 	val = swab16(data) >> 4;
49 
50 	dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
51 
52 	return val;
53 }
54 
55 static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
56 {
57 	/* y- still on; turn on only y+ (and ADC) */
58 	tc->y = tsc2007_xfer(tsc, READ_Y);
59 
60 	/* turn y- off, x+ on, then leave in lowpower */
61 	tc->x = tsc2007_xfer(tsc, READ_X);
62 
63 	/* turn y+ off, x- on; we'll use formula #1 */
64 	tc->z1 = tsc2007_xfer(tsc, READ_Z1);
65 	tc->z2 = tsc2007_xfer(tsc, READ_Z2);
66 
67 	/* Prepare for next touch reading - power down ADC, enable PENIRQ */
68 	tsc2007_xfer(tsc, PWRDOWN);
69 }
70 
71 u32 tsc2007_calculate_resistance(struct tsc2007 *tsc, struct ts_event *tc)
72 {
73 	u32 rt = 0;
74 
75 	/* range filtering */
76 	if (tc->x == MAX_12BIT)
77 		tc->x = 0;
78 
79 	if (likely(tc->x && tc->z1)) {
80 		/* compute touch resistance using equation #1 */
81 		rt = tc->z2 - tc->z1;
82 		rt *= tc->x;
83 		rt *= tsc->x_plate_ohms;
84 		rt /= tc->z1;
85 		rt = (rt + 2047) >> 12;
86 	}
87 
88 	return rt;
89 }
90 
91 bool tsc2007_is_pen_down(struct tsc2007 *ts)
92 {
93 	/*
94 	 * NOTE: We can't rely on the pressure to determine the pen down
95 	 * state, even though this controller has a pressure sensor.
96 	 * The pressure value can fluctuate for quite a while after
97 	 * lifting the pen and in some cases may not even settle at the
98 	 * expected value.
99 	 *
100 	 * The only safe way to check for the pen up condition is in the
101 	 * work function by reading the pen signal state (it's a GPIO
102 	 * and IRQ). Unfortunately such callback is not always available,
103 	 * in that case we assume that the pen is down and expect caller
104 	 * to fall back on the pressure reading.
105 	 */
106 
107 	if (!ts->get_pendown_state)
108 		return true;
109 
110 	return ts->get_pendown_state(&ts->client->dev);
111 }
112 
113 static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
114 {
115 	struct tsc2007 *ts = handle;
116 	struct input_dev *input = ts->input;
117 	struct ts_event tc;
118 	u32 rt;
119 
120 	while (!ts->stopped && tsc2007_is_pen_down(ts)) {
121 
122 		/* pen is down, continue with the measurement */
123 
124 		mutex_lock(&ts->mlock);
125 		tsc2007_read_values(ts, &tc);
126 		mutex_unlock(&ts->mlock);
127 
128 		rt = tsc2007_calculate_resistance(ts, &tc);
129 
130 		if (!rt && !ts->get_pendown_state) {
131 			/*
132 			 * If pressure reported is 0 and we don't have
133 			 * callback to check pendown state, we have to
134 			 * assume that pen was lifted up.
135 			 */
136 			break;
137 		}
138 
139 		if (rt <= ts->max_rt) {
140 			dev_dbg(&ts->client->dev,
141 				"DOWN point(%4d,%4d), resistance (%4u)\n",
142 				tc.x, tc.y, rt);
143 
144 			rt = ts->max_rt - rt;
145 
146 			input_report_key(input, BTN_TOUCH, 1);
147 			input_report_abs(input, ABS_X, tc.x);
148 			input_report_abs(input, ABS_Y, tc.y);
149 			input_report_abs(input, ABS_PRESSURE, rt);
150 
151 			input_sync(input);
152 
153 		} else {
154 			/*
155 			 * Sample found inconsistent by debouncing or pressure is
156 			 * beyond the maximum. Don't report it to user space,
157 			 * repeat at least once more the measurement.
158 			 */
159 			dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
160 		}
161 
162 		wait_event_timeout(ts->wait, ts->stopped, ts->poll_period);
163 	}
164 
165 	dev_dbg(&ts->client->dev, "UP\n");
166 
167 	input_report_key(input, BTN_TOUCH, 0);
168 	input_report_abs(input, ABS_PRESSURE, 0);
169 	input_sync(input);
170 
171 	if (ts->clear_penirq)
172 		ts->clear_penirq();
173 
174 	return IRQ_HANDLED;
175 }
176 
177 static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
178 {
179 	struct tsc2007 *ts = handle;
180 
181 	if (tsc2007_is_pen_down(ts))
182 		return IRQ_WAKE_THREAD;
183 
184 	if (ts->clear_penirq)
185 		ts->clear_penirq();
186 
187 	return IRQ_HANDLED;
188 }
189 
190 static void tsc2007_stop(struct tsc2007 *ts)
191 {
192 	ts->stopped = true;
193 	mb();
194 	wake_up(&ts->wait);
195 
196 	disable_irq(ts->irq);
197 }
198 
199 static int tsc2007_open(struct input_dev *input_dev)
200 {
201 	struct tsc2007 *ts = input_get_drvdata(input_dev);
202 	int err;
203 
204 	ts->stopped = false;
205 	mb();
206 
207 	enable_irq(ts->irq);
208 
209 	/* Prepare for touch readings - power down ADC and enable PENIRQ */
210 	err = tsc2007_xfer(ts, PWRDOWN);
211 	if (err < 0) {
212 		tsc2007_stop(ts);
213 		return err;
214 	}
215 
216 	return 0;
217 }
218 
219 static void tsc2007_close(struct input_dev *input_dev)
220 {
221 	struct tsc2007 *ts = input_get_drvdata(input_dev);
222 
223 	tsc2007_stop(ts);
224 }
225 
226 #ifdef CONFIG_OF
227 static int tsc2007_get_pendown_state_gpio(struct device *dev)
228 {
229 	struct i2c_client *client = to_i2c_client(dev);
230 	struct tsc2007 *ts = i2c_get_clientdata(client);
231 
232 	return !gpio_get_value(ts->gpio);
233 }
234 
235 static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
236 {
237 	struct device_node *np = client->dev.of_node;
238 	u32 val32;
239 	u64 val64;
240 
241 	if (!np) {
242 		dev_err(&client->dev, "missing device tree data\n");
243 		return -EINVAL;
244 	}
245 
246 	if (!of_property_read_u32(np, "ti,max-rt", &val32))
247 		ts->max_rt = val32;
248 	else
249 		ts->max_rt = MAX_12BIT;
250 
251 	if (!of_property_read_u32(np, "ti,fuzzx", &val32))
252 		ts->fuzzx = val32;
253 
254 	if (!of_property_read_u32(np, "ti,fuzzy", &val32))
255 		ts->fuzzy = val32;
256 
257 	if (!of_property_read_u32(np, "ti,fuzzz", &val32))
258 		ts->fuzzz = val32;
259 
260 	if (!of_property_read_u64(np, "ti,poll-period", &val64))
261 		ts->poll_period = msecs_to_jiffies(val64);
262 	else
263 		ts->poll_period = msecs_to_jiffies(1);
264 
265 	if (!of_property_read_u32(np, "ti,x-plate-ohms", &val32)) {
266 		ts->x_plate_ohms = val32;
267 	} else {
268 		dev_err(&client->dev, "missing ti,x-plate-ohms devicetree property.");
269 		return -EINVAL;
270 	}
271 
272 	ts->gpio = of_get_gpio(np, 0);
273 	if (gpio_is_valid(ts->gpio))
274 		ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
275 	else
276 		dev_warn(&client->dev,
277 			 "GPIO not specified in DT (of_get_gpio returned %d)\n",
278 			 ts->gpio);
279 
280 	return 0;
281 }
282 #else
283 static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
284 {
285 	dev_err(&client->dev, "platform data is required!\n");
286 	return -EINVAL;
287 }
288 #endif
289 
290 static int tsc2007_probe_pdev(struct i2c_client *client, struct tsc2007 *ts,
291 			      const struct tsc2007_platform_data *pdata,
292 			      const struct i2c_device_id *id)
293 {
294 	ts->model             = pdata->model;
295 	ts->x_plate_ohms      = pdata->x_plate_ohms;
296 	ts->max_rt            = pdata->max_rt ? : MAX_12BIT;
297 	ts->poll_period       = msecs_to_jiffies(pdata->poll_period ? : 1);
298 	ts->get_pendown_state = pdata->get_pendown_state;
299 	ts->clear_penirq      = pdata->clear_penirq;
300 	ts->fuzzx             = pdata->fuzzx;
301 	ts->fuzzy             = pdata->fuzzy;
302 	ts->fuzzz             = pdata->fuzzz;
303 
304 	if (pdata->x_plate_ohms == 0) {
305 		dev_err(&client->dev, "x_plate_ohms is not set up in platform data");
306 		return -EINVAL;
307 	}
308 
309 	return 0;
310 }
311 
312 static void tsc2007_call_exit_platform_hw(void *data)
313 {
314 	struct device *dev = data;
315 	const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
316 
317 	pdata->exit_platform_hw();
318 }
319 
320 static int tsc2007_probe(struct i2c_client *client,
321 			 const struct i2c_device_id *id)
322 {
323 	const struct tsc2007_platform_data *pdata =
324 		dev_get_platdata(&client->dev);
325 	struct tsc2007 *ts;
326 	struct input_dev *input_dev;
327 	int err;
328 
329 	if (!i2c_check_functionality(client->adapter,
330 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
331 		return -EIO;
332 
333 	ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
334 	if (!ts)
335 		return -ENOMEM;
336 
337 	if (pdata)
338 		err = tsc2007_probe_pdev(client, ts, pdata, id);
339 	else
340 		err = tsc2007_probe_dt(client, ts);
341 	if (err)
342 		return err;
343 
344 	input_dev = devm_input_allocate_device(&client->dev);
345 	if (!input_dev)
346 		return -ENOMEM;
347 
348 	i2c_set_clientdata(client, ts);
349 
350 	ts->client = client;
351 	ts->irq = client->irq;
352 	ts->input = input_dev;
353 
354 	init_waitqueue_head(&ts->wait);
355 	mutex_init(&ts->mlock);
356 
357 	snprintf(ts->phys, sizeof(ts->phys),
358 		 "%s/input0", dev_name(&client->dev));
359 
360 	input_dev->name = "TSC2007 Touchscreen";
361 	input_dev->phys = ts->phys;
362 	input_dev->id.bustype = BUS_I2C;
363 
364 	input_dev->open = tsc2007_open;
365 	input_dev->close = tsc2007_close;
366 
367 	input_set_drvdata(input_dev, ts);
368 
369 	input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
370 
371 	input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
372 	input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
373 	input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
374 			     ts->fuzzz, 0);
375 
376 	if (pdata) {
377 		if (pdata->exit_platform_hw) {
378 			err = devm_add_action(&client->dev,
379 					      tsc2007_call_exit_platform_hw,
380 					      &client->dev);
381 			if (err) {
382 				dev_err(&client->dev,
383 					"Failed to register exit_platform_hw action, %d\n",
384 					err);
385 				return err;
386 			}
387 		}
388 
389 		if (pdata->init_platform_hw)
390 			pdata->init_platform_hw();
391 	}
392 
393 	err = devm_request_threaded_irq(&client->dev, ts->irq,
394 					tsc2007_hard_irq, tsc2007_soft_irq,
395 					IRQF_ONESHOT,
396 					client->dev.driver->name, ts);
397 	if (err) {
398 		dev_err(&client->dev, "Failed to request irq %d: %d\n",
399 			ts->irq, err);
400 		return err;
401 	}
402 
403 	tsc2007_stop(ts);
404 
405 	/* power down the chip (TSC2007_SETUP does not ACK on I2C) */
406 	err = tsc2007_xfer(ts, PWRDOWN);
407 	if (err < 0) {
408 		dev_err(&client->dev,
409 			"Failed to setup chip: %d\n", err);
410 		return err;	/* chip does not respond */
411 	}
412 
413 	err = input_register_device(input_dev);
414 	if (err) {
415 		dev_err(&client->dev,
416 			"Failed to register input device: %d\n", err);
417 		return err;
418 	}
419 
420 	err =  tsc2007_iio_configure(ts);
421 	if (err) {
422 		dev_err(&client->dev,
423 			"Failed to register with IIO: %d\n", err);
424 		return err;
425 	}
426 
427 	return 0;
428 }
429 
430 static const struct i2c_device_id tsc2007_idtable[] = {
431 	{ "tsc2007", 0 },
432 	{ }
433 };
434 
435 MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
436 
437 #ifdef CONFIG_OF
438 static const struct of_device_id tsc2007_of_match[] = {
439 	{ .compatible = "ti,tsc2007" },
440 	{ /* sentinel */ }
441 };
442 MODULE_DEVICE_TABLE(of, tsc2007_of_match);
443 #endif
444 
445 static struct i2c_driver tsc2007_driver = {
446 	.driver = {
447 		.name	= "tsc2007",
448 		.of_match_table = of_match_ptr(tsc2007_of_match),
449 	},
450 	.id_table	= tsc2007_idtable,
451 	.probe		= tsc2007_probe,
452 };
453 
454 module_i2c_driver(tsc2007_driver);
455 
456 MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
457 MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
458 MODULE_LICENSE("GPL");
459