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