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 
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 
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 
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 
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 
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 
175f1443404SH. Nikolaus Schaller static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
176f1443404SH. Nikolaus Schaller {
177f1443404SH. Nikolaus Schaller 	struct tsc2007 *ts = handle;
178f1443404SH. Nikolaus Schaller 
179f1443404SH. Nikolaus Schaller 	if (tsc2007_is_pen_down(ts))
180f1443404SH. Nikolaus Schaller 		return IRQ_WAKE_THREAD;
181f1443404SH. Nikolaus Schaller 
182f1443404SH. Nikolaus Schaller 	if (ts->clear_penirq)
183f1443404SH. Nikolaus Schaller 		ts->clear_penirq();
184f1443404SH. Nikolaus Schaller 
185f1443404SH. Nikolaus Schaller 	return IRQ_HANDLED;
186f1443404SH. Nikolaus Schaller }
187f1443404SH. Nikolaus Schaller 
188f1443404SH. Nikolaus Schaller static void tsc2007_stop(struct tsc2007 *ts)
189f1443404SH. Nikolaus Schaller {
190f1443404SH. Nikolaus Schaller 	ts->stopped = true;
191f1443404SH. Nikolaus Schaller 	mb();
192f1443404SH. Nikolaus Schaller 	wake_up(&ts->wait);
193f1443404SH. Nikolaus Schaller 
194f1443404SH. Nikolaus Schaller 	disable_irq(ts->irq);
195f1443404SH. Nikolaus Schaller }
196f1443404SH. Nikolaus Schaller 
197f1443404SH. Nikolaus Schaller static int tsc2007_open(struct input_dev *input_dev)
198f1443404SH. Nikolaus Schaller {
199f1443404SH. Nikolaus Schaller 	struct tsc2007 *ts = input_get_drvdata(input_dev);
200f1443404SH. Nikolaus Schaller 	int err;
201f1443404SH. Nikolaus Schaller 
202f1443404SH. Nikolaus Schaller 	ts->stopped = false;
203f1443404SH. Nikolaus Schaller 	mb();
204f1443404SH. Nikolaus Schaller 
205f1443404SH. Nikolaus Schaller 	enable_irq(ts->irq);
206f1443404SH. Nikolaus Schaller 
207f1443404SH. Nikolaus Schaller 	/* Prepare for touch readings - power down ADC and enable PENIRQ */
208f1443404SH. Nikolaus Schaller 	err = tsc2007_xfer(ts, PWRDOWN);
209f1443404SH. Nikolaus Schaller 	if (err < 0) {
210f1443404SH. Nikolaus Schaller 		tsc2007_stop(ts);
211f1443404SH. Nikolaus Schaller 		return err;
212f1443404SH. Nikolaus Schaller 	}
213f1443404SH. Nikolaus Schaller 
214f1443404SH. Nikolaus Schaller 	return 0;
215f1443404SH. Nikolaus Schaller }
216f1443404SH. Nikolaus Schaller 
217f1443404SH. Nikolaus Schaller static void tsc2007_close(struct input_dev *input_dev)
218f1443404SH. Nikolaus Schaller {
219f1443404SH. Nikolaus Schaller 	struct tsc2007 *ts = input_get_drvdata(input_dev);
220f1443404SH. Nikolaus Schaller 
221f1443404SH. Nikolaus Schaller 	tsc2007_stop(ts);
222f1443404SH. Nikolaus Schaller }
223f1443404SH. Nikolaus Schaller 
224f1443404SH. Nikolaus Schaller static int tsc2007_get_pendown_state_gpio(struct device *dev)
225f1443404SH. Nikolaus Schaller {
226f1443404SH. Nikolaus Schaller 	struct i2c_client *client = to_i2c_client(dev);
227f1443404SH. Nikolaus Schaller 	struct tsc2007 *ts = i2c_get_clientdata(client);
228f1443404SH. Nikolaus Schaller 
229cee451c9SAndy Shevchenko 	return gpiod_get_value(ts->gpiod);
230f1443404SH. Nikolaus Schaller }
231f1443404SH. Nikolaus Schaller 
232e512a9e9SAndy Shevchenko static int tsc2007_probe_properties(struct device *dev, struct tsc2007 *ts)
233f1443404SH. Nikolaus Schaller {
234f1443404SH. Nikolaus Schaller 	u32 val32;
235f1443404SH. Nikolaus Schaller 	u64 val64;
236f1443404SH. Nikolaus Schaller 
237e512a9e9SAndy Shevchenko 	if (!device_property_read_u32(dev, "ti,max-rt", &val32))
238f1443404SH. Nikolaus Schaller 		ts->max_rt = val32;
239f1443404SH. Nikolaus Schaller 	else
240f1443404SH. Nikolaus Schaller 		ts->max_rt = MAX_12BIT;
241f1443404SH. Nikolaus Schaller 
242e512a9e9SAndy Shevchenko 	if (!device_property_read_u32(dev, "ti,fuzzx", &val32))
243f1443404SH. Nikolaus Schaller 		ts->fuzzx = val32;
244f1443404SH. Nikolaus Schaller 
245e512a9e9SAndy Shevchenko 	if (!device_property_read_u32(dev, "ti,fuzzy", &val32))
246f1443404SH. Nikolaus Schaller 		ts->fuzzy = val32;
247f1443404SH. Nikolaus Schaller 
248e512a9e9SAndy Shevchenko 	if (!device_property_read_u32(dev, "ti,fuzzz", &val32))
249f1443404SH. Nikolaus Schaller 		ts->fuzzz = val32;
250f1443404SH. Nikolaus Schaller 
251e512a9e9SAndy Shevchenko 	if (!device_property_read_u64(dev, "ti,poll-period", &val64))
252f1443404SH. Nikolaus Schaller 		ts->poll_period = msecs_to_jiffies(val64);
253f1443404SH. Nikolaus Schaller 	else
254f1443404SH. Nikolaus Schaller 		ts->poll_period = msecs_to_jiffies(1);
255f1443404SH. Nikolaus Schaller 
256e512a9e9SAndy Shevchenko 	if (!device_property_read_u32(dev, "ti,x-plate-ohms", &val32)) {
257f1443404SH. Nikolaus Schaller 		ts->x_plate_ohms = val32;
258f1443404SH. Nikolaus Schaller 	} else {
259e512a9e9SAndy Shevchenko 		dev_err(dev, "Missing ti,x-plate-ohms device property\n");
260f1443404SH. Nikolaus Schaller 		return -EINVAL;
261f1443404SH. Nikolaus Schaller 	}
262f1443404SH. Nikolaus Schaller 
263e512a9e9SAndy Shevchenko 	ts->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
264cee451c9SAndy Shevchenko 	if (IS_ERR(ts->gpiod))
265cee451c9SAndy Shevchenko 		return PTR_ERR(ts->gpiod);
266cee451c9SAndy Shevchenko 
267cee451c9SAndy Shevchenko 	if (ts->gpiod)
268f1443404SH. Nikolaus Schaller 		ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
269f1443404SH. Nikolaus Schaller 	else
270e512a9e9SAndy Shevchenko 		dev_warn(dev, "Pen down GPIO is not specified in properties\n");
271f1443404SH. Nikolaus Schaller 
272f1443404SH. Nikolaus Schaller 	return 0;
273f1443404SH. Nikolaus Schaller }
274f1443404SH. Nikolaus Schaller 
275e512a9e9SAndy Shevchenko static int tsc2007_probe_pdev(struct device *dev, struct tsc2007 *ts,
276f1443404SH. Nikolaus Schaller 			      const struct tsc2007_platform_data *pdata,
277f1443404SH. Nikolaus Schaller 			      const struct i2c_device_id *id)
278f1443404SH. Nikolaus Schaller {
279f1443404SH. Nikolaus Schaller 	ts->model             = pdata->model;
280f1443404SH. Nikolaus Schaller 	ts->x_plate_ohms      = pdata->x_plate_ohms;
281f1443404SH. Nikolaus Schaller 	ts->max_rt            = pdata->max_rt ? : MAX_12BIT;
282f1443404SH. Nikolaus Schaller 	ts->poll_period       = msecs_to_jiffies(pdata->poll_period ? : 1);
283f1443404SH. Nikolaus Schaller 	ts->get_pendown_state = pdata->get_pendown_state;
284f1443404SH. Nikolaus Schaller 	ts->clear_penirq      = pdata->clear_penirq;
285f1443404SH. Nikolaus Schaller 	ts->fuzzx             = pdata->fuzzx;
286f1443404SH. Nikolaus Schaller 	ts->fuzzy             = pdata->fuzzy;
287f1443404SH. Nikolaus Schaller 	ts->fuzzz             = pdata->fuzzz;
288f1443404SH. Nikolaus Schaller 
289f1443404SH. Nikolaus Schaller 	if (pdata->x_plate_ohms == 0) {
290e512a9e9SAndy Shevchenko 		dev_err(dev, "x_plate_ohms is not set up in platform data\n");
291f1443404SH. Nikolaus Schaller 		return -EINVAL;
292f1443404SH. Nikolaus Schaller 	}
293f1443404SH. Nikolaus Schaller 
294f1443404SH. Nikolaus Schaller 	return 0;
295f1443404SH. Nikolaus Schaller }
296f1443404SH. Nikolaus Schaller 
297f1443404SH. Nikolaus Schaller static void tsc2007_call_exit_platform_hw(void *data)
298f1443404SH. Nikolaus Schaller {
299f1443404SH. Nikolaus Schaller 	struct device *dev = data;
300f1443404SH. Nikolaus Schaller 	const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
301f1443404SH. Nikolaus Schaller 
302f1443404SH. Nikolaus Schaller 	pdata->exit_platform_hw();
303f1443404SH. Nikolaus Schaller }
304f1443404SH. Nikolaus Schaller 
305*4e9d70a9SUwe Kleine-König static int tsc2007_probe(struct i2c_client *client)
306f1443404SH. Nikolaus Schaller {
307*4e9d70a9SUwe Kleine-König 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
308f1443404SH. Nikolaus Schaller 	const struct tsc2007_platform_data *pdata =
309f1443404SH. Nikolaus Schaller 		dev_get_platdata(&client->dev);
310f1443404SH. Nikolaus Schaller 	struct tsc2007 *ts;
311f1443404SH. Nikolaus Schaller 	struct input_dev *input_dev;
312f1443404SH. Nikolaus Schaller 	int err;
313f1443404SH. Nikolaus Schaller 
314f1443404SH. Nikolaus Schaller 	if (!i2c_check_functionality(client->adapter,
315f1443404SH. Nikolaus Schaller 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
316f1443404SH. Nikolaus Schaller 		return -EIO;
317f1443404SH. Nikolaus Schaller 
318f1443404SH. Nikolaus Schaller 	ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
319f1443404SH. Nikolaus Schaller 	if (!ts)
320f1443404SH. Nikolaus Schaller 		return -ENOMEM;
321f1443404SH. Nikolaus Schaller 
322f1443404SH. Nikolaus Schaller 	if (pdata)
323e512a9e9SAndy Shevchenko 		err = tsc2007_probe_pdev(&client->dev, ts, pdata, id);
324f1443404SH. Nikolaus Schaller 	else
325e512a9e9SAndy Shevchenko 		err = tsc2007_probe_properties(&client->dev, ts);
326f1443404SH. Nikolaus Schaller 	if (err)
327f1443404SH. Nikolaus Schaller 		return err;
328f1443404SH. Nikolaus Schaller 
329f1443404SH. Nikolaus Schaller 	input_dev = devm_input_allocate_device(&client->dev);
330f1443404SH. Nikolaus Schaller 	if (!input_dev)
331f1443404SH. Nikolaus Schaller 		return -ENOMEM;
332f1443404SH. Nikolaus Schaller 
333f1443404SH. Nikolaus Schaller 	i2c_set_clientdata(client, ts);
334f1443404SH. Nikolaus Schaller 
335f1443404SH. Nikolaus Schaller 	ts->client = client;
336f1443404SH. Nikolaus Schaller 	ts->irq = client->irq;
337f1443404SH. Nikolaus Schaller 	ts->input = input_dev;
338f1443404SH. Nikolaus Schaller 
339f1443404SH. Nikolaus Schaller 	init_waitqueue_head(&ts->wait);
340f1443404SH. Nikolaus Schaller 	mutex_init(&ts->mlock);
341f1443404SH. Nikolaus Schaller 
342f1443404SH. Nikolaus Schaller 	snprintf(ts->phys, sizeof(ts->phys),
343f1443404SH. Nikolaus Schaller 		 "%s/input0", dev_name(&client->dev));
344f1443404SH. Nikolaus Schaller 
345f1443404SH. Nikolaus Schaller 	input_dev->name = "TSC2007 Touchscreen";
346f1443404SH. Nikolaus Schaller 	input_dev->phys = ts->phys;
347f1443404SH. Nikolaus Schaller 	input_dev->id.bustype = BUS_I2C;
348f1443404SH. Nikolaus Schaller 
349f1443404SH. Nikolaus Schaller 	input_dev->open = tsc2007_open;
350f1443404SH. Nikolaus Schaller 	input_dev->close = tsc2007_close;
351f1443404SH. Nikolaus Schaller 
352f1443404SH. Nikolaus Schaller 	input_set_drvdata(input_dev, ts);
353f1443404SH. Nikolaus Schaller 
354c61ebe83SDmitry Torokhov 	input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
355f1443404SH. Nikolaus Schaller 
356f1443404SH. Nikolaus Schaller 	input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
357f1443404SH. Nikolaus Schaller 	input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
358f1443404SH. Nikolaus Schaller 	input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
359f1443404SH. Nikolaus Schaller 			     ts->fuzzz, 0);
360f1443404SH. Nikolaus Schaller 
361f1443404SH. Nikolaus Schaller 	if (pdata) {
362f1443404SH. Nikolaus Schaller 		if (pdata->exit_platform_hw) {
363f1443404SH. Nikolaus Schaller 			err = devm_add_action(&client->dev,
364f1443404SH. Nikolaus Schaller 					      tsc2007_call_exit_platform_hw,
365f1443404SH. Nikolaus Schaller 					      &client->dev);
366f1443404SH. Nikolaus Schaller 			if (err) {
367f1443404SH. Nikolaus Schaller 				dev_err(&client->dev,
368f1443404SH. Nikolaus Schaller 					"Failed to register exit_platform_hw action, %d\n",
369f1443404SH. Nikolaus Schaller 					err);
370f1443404SH. Nikolaus Schaller 				return err;
371f1443404SH. Nikolaus Schaller 			}
372f1443404SH. Nikolaus Schaller 		}
373f1443404SH. Nikolaus Schaller 
374f1443404SH. Nikolaus Schaller 		if (pdata->init_platform_hw)
375f1443404SH. Nikolaus Schaller 			pdata->init_platform_hw();
376f1443404SH. Nikolaus Schaller 	}
377f1443404SH. Nikolaus Schaller 
378f1443404SH. Nikolaus Schaller 	err = devm_request_threaded_irq(&client->dev, ts->irq,
379f1443404SH. Nikolaus Schaller 					tsc2007_hard_irq, tsc2007_soft_irq,
380f1443404SH. Nikolaus Schaller 					IRQF_ONESHOT,
381f1443404SH. Nikolaus Schaller 					client->dev.driver->name, ts);
382f1443404SH. Nikolaus Schaller 	if (err) {
383f1443404SH. Nikolaus Schaller 		dev_err(&client->dev, "Failed to request irq %d: %d\n",
384f1443404SH. Nikolaus Schaller 			ts->irq, err);
385f1443404SH. Nikolaus Schaller 		return err;
386f1443404SH. Nikolaus Schaller 	}
387f1443404SH. Nikolaus Schaller 
388f1443404SH. Nikolaus Schaller 	tsc2007_stop(ts);
389f1443404SH. Nikolaus Schaller 
390f1443404SH. Nikolaus Schaller 	/* power down the chip (TSC2007_SETUP does not ACK on I2C) */
391f1443404SH. Nikolaus Schaller 	err = tsc2007_xfer(ts, PWRDOWN);
392f1443404SH. Nikolaus Schaller 	if (err < 0) {
393f1443404SH. Nikolaus Schaller 		dev_err(&client->dev,
394f1443404SH. Nikolaus Schaller 			"Failed to setup chip: %d\n", err);
395f1443404SH. Nikolaus Schaller 		return err;	/* chip does not respond */
396f1443404SH. Nikolaus Schaller 	}
397f1443404SH. Nikolaus Schaller 
398f1443404SH. Nikolaus Schaller 	err = input_register_device(input_dev);
399f1443404SH. Nikolaus Schaller 	if (err) {
400f1443404SH. Nikolaus Schaller 		dev_err(&client->dev,
401f1443404SH. Nikolaus Schaller 			"Failed to register input device: %d\n", err);
402f1443404SH. Nikolaus Schaller 		return err;
403f1443404SH. Nikolaus Schaller 	}
404f1443404SH. Nikolaus Schaller 
405f1443404SH. Nikolaus Schaller 	err =  tsc2007_iio_configure(ts);
406f1443404SH. Nikolaus Schaller 	if (err) {
407f1443404SH. Nikolaus Schaller 		dev_err(&client->dev,
408f1443404SH. Nikolaus Schaller 			"Failed to register with IIO: %d\n", err);
409f1443404SH. Nikolaus Schaller 		return err;
410f1443404SH. Nikolaus Schaller 	}
411f1443404SH. Nikolaus Schaller 
412f1443404SH. Nikolaus Schaller 	return 0;
413f1443404SH. Nikolaus Schaller }
414f1443404SH. Nikolaus Schaller 
415f1443404SH. Nikolaus Schaller static const struct i2c_device_id tsc2007_idtable[] = {
416f1443404SH. Nikolaus Schaller 	{ "tsc2007", 0 },
417f1443404SH. Nikolaus Schaller 	{ }
418f1443404SH. Nikolaus Schaller };
419f1443404SH. Nikolaus Schaller 
420f1443404SH. Nikolaus Schaller MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
421f1443404SH. Nikolaus Schaller 
422f1443404SH. Nikolaus Schaller static const struct of_device_id tsc2007_of_match[] = {
423f1443404SH. Nikolaus Schaller 	{ .compatible = "ti,tsc2007" },
424f1443404SH. Nikolaus Schaller 	{ /* sentinel */ }
425f1443404SH. Nikolaus Schaller };
426f1443404SH. Nikolaus Schaller MODULE_DEVICE_TABLE(of, tsc2007_of_match);
427f1443404SH. Nikolaus Schaller 
428f1443404SH. Nikolaus Schaller static struct i2c_driver tsc2007_driver = {
429f1443404SH. Nikolaus Schaller 	.driver = {
430f1443404SH. Nikolaus Schaller 		.name	= "tsc2007",
431e512a9e9SAndy Shevchenko 		.of_match_table = tsc2007_of_match,
432f1443404SH. Nikolaus Schaller 	},
433f1443404SH. Nikolaus Schaller 	.id_table	= tsc2007_idtable,
434*4e9d70a9SUwe Kleine-König 	.probe_new	= tsc2007_probe,
435f1443404SH. Nikolaus Schaller };
436f1443404SH. Nikolaus Schaller 
437f1443404SH. Nikolaus Schaller module_i2c_driver(tsc2007_driver);
438f1443404SH. Nikolaus Schaller 
439f1443404SH. Nikolaus Schaller MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
440f1443404SH. Nikolaus Schaller MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
441f1443404SH. Nikolaus Schaller MODULE_LICENSE("GPL");
442