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