xref: /linux/drivers/input/touchscreen/tsc2007_core.c (revision 8a7c601e14576a22c2bbf7f67455ccf3f3d2737f)
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 	tc->z2 = tsc2007_xfer(tsc, READ_Z2);
65 
66 	/* Prepare for next touch reading - power down ADC, enable PENIRQ */
67 	tsc2007_xfer(tsc, PWRDOWN);
68 }
69 
70 u32 tsc2007_calculate_resistance(struct tsc2007 *tsc, struct ts_event *tc)
71 {
72 	u64 rt = 0;
73 
74 	/* range filtering */
75 	if (tc->x == MAX_12BIT)
76 		tc->x = 0;
77 
78 	if (likely(tc->x && tc->z1)) {
79 		/* compute touch resistance using equation #1 */
80 		rt = tc->z2 - tc->z1;
81 		rt *= tc->x;
82 		rt *= tsc->x_plate_ohms;
83 		rt = div_u64(rt, tc->z1);
84 		rt = (rt + 2047) >> 12;
85 	}
86 
87 	if (rt > U32_MAX)
88 		return U32_MAX;
89 	return (u32) rt;
90 }
91 
92 bool tsc2007_is_pen_down(struct tsc2007 *ts)
93 {
94 	/*
95 	 * NOTE: We can't rely on the pressure to determine the pen down
96 	 * state, even though this controller has a pressure sensor.
97 	 * The pressure value can fluctuate for quite a while after
98 	 * lifting the pen and in some cases may not even settle at the
99 	 * expected value.
100 	 *
101 	 * The only safe way to check for the pen up condition is in the
102 	 * work function by reading the pen signal state (it's a GPIO
103 	 * and IRQ). Unfortunately such callback is not always available,
104 	 * in that case we assume that the pen is down and expect caller
105 	 * to fall back on the pressure reading.
106 	 */
107 
108 	if (!ts->get_pendown_state)
109 		return true;
110 
111 	return ts->get_pendown_state(&ts->client->dev);
112 }
113 
114 static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
115 {
116 	struct tsc2007 *ts = handle;
117 	struct input_dev *input = ts->input;
118 	struct ts_event tc;
119 	u32 rt;
120 
121 	while (!ts->stopped && tsc2007_is_pen_down(ts)) {
122 
123 		/* pen is down, continue with the measurement */
124 
125 		mutex_lock(&ts->mlock);
126 		tsc2007_read_values(ts, &tc);
127 		mutex_unlock(&ts->mlock);
128 
129 		rt = tsc2007_calculate_resistance(ts, &tc);
130 
131 		if (!rt && !ts->get_pendown_state) {
132 			/*
133 			 * If pressure reported is 0 and we don't have
134 			 * callback to check pendown state, we have to
135 			 * assume that pen was lifted up.
136 			 */
137 			break;
138 		}
139 
140 		if (rt <= ts->max_rt) {
141 			dev_dbg(&ts->client->dev,
142 				"DOWN point(%4d,%4d), resistance (%4u)\n",
143 				tc.x, tc.y, rt);
144 
145 			rt = ts->max_rt - rt;
146 
147 			input_report_key(input, BTN_TOUCH, 1);
148 			touchscreen_report_pos(input, &ts->prop, tc.x, tc.y, false);
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 void tsc2007_stop(struct tsc2007 *ts)
178 {
179 	ts->stopped = true;
180 	mb();
181 	wake_up(&ts->wait);
182 
183 	if (ts->irq)
184 		disable_irq(ts->irq);
185 }
186 
187 static int tsc2007_open(struct input_dev *input_dev)
188 {
189 	struct tsc2007 *ts = input_get_drvdata(input_dev);
190 	int err;
191 
192 	ts->stopped = false;
193 	mb();
194 
195 	if (ts->irq)
196 		enable_irq(ts->irq);
197 
198 	/* Prepare for touch readings - power down ADC and enable PENIRQ */
199 	err = tsc2007_xfer(ts, PWRDOWN);
200 	if (err < 0) {
201 		tsc2007_stop(ts);
202 		return err;
203 	}
204 
205 	return 0;
206 }
207 
208 static void tsc2007_close(struct input_dev *input_dev)
209 {
210 	struct tsc2007 *ts = input_get_drvdata(input_dev);
211 
212 	tsc2007_stop(ts);
213 }
214 
215 static int tsc2007_get_pendown_state_gpio(struct device *dev)
216 {
217 	struct i2c_client *client = to_i2c_client(dev);
218 	struct tsc2007 *ts = i2c_get_clientdata(client);
219 
220 	return gpiod_get_value_cansleep(ts->gpiod);
221 }
222 
223 static int tsc2007_probe_properties(struct device *dev, struct tsc2007 *ts)
224 {
225 	u32 val32;
226 	u64 val64;
227 
228 	if (!device_property_read_u32(dev, "ti,max-rt", &val32))
229 		ts->max_rt = val32;
230 	else
231 		ts->max_rt = MAX_12BIT;
232 
233 	if (!device_property_read_u32(dev, "ti,fuzzx", &val32))
234 		ts->fuzzx = val32;
235 
236 	if (!device_property_read_u32(dev, "ti,fuzzy", &val32))
237 		ts->fuzzy = val32;
238 
239 	if (!device_property_read_u32(dev, "ti,fuzzz", &val32))
240 		ts->fuzzz = val32;
241 
242 	if (!device_property_read_u64(dev, "ti,poll-period", &val64))
243 		ts->poll_period = msecs_to_jiffies(val64);
244 	else
245 		ts->poll_period = msecs_to_jiffies(1);
246 
247 	if (!device_property_read_u32(dev, "ti,x-plate-ohms", &val32)) {
248 		ts->x_plate_ohms = val32;
249 	} else {
250 		dev_err(dev, "Missing ti,x-plate-ohms device property\n");
251 		return -EINVAL;
252 	}
253 
254 	ts->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
255 	if (IS_ERR(ts->gpiod))
256 		return PTR_ERR(ts->gpiod);
257 
258 	if (ts->gpiod)
259 		ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
260 	else
261 		dev_dbg(dev, "Pen down GPIO is not specified in properties\n");
262 
263 	return 0;
264 }
265 
266 static int tsc2007_probe_pdev(struct device *dev, struct tsc2007 *ts,
267 			      const struct tsc2007_platform_data *pdata,
268 			      const struct i2c_device_id *id)
269 {
270 	ts->model             = pdata->model;
271 	ts->x_plate_ohms      = pdata->x_plate_ohms;
272 	ts->max_rt            = pdata->max_rt ? : MAX_12BIT;
273 	ts->poll_period       = msecs_to_jiffies(pdata->poll_period ? : 1);
274 	ts->get_pendown_state = pdata->get_pendown_state;
275 	ts->clear_penirq      = pdata->clear_penirq;
276 	ts->fuzzx             = pdata->fuzzx;
277 	ts->fuzzy             = pdata->fuzzy;
278 	ts->fuzzz             = pdata->fuzzz;
279 
280 	if (pdata->x_plate_ohms == 0) {
281 		dev_err(dev, "x_plate_ohms is not set up in platform data\n");
282 		return -EINVAL;
283 	}
284 
285 	return 0;
286 }
287 
288 static void tsc2007_call_exit_platform_hw(void *data)
289 {
290 	struct device *dev = data;
291 	const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
292 
293 	pdata->exit_platform_hw();
294 }
295 
296 static int tsc2007_probe(struct i2c_client *client)
297 {
298 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
299 	const struct tsc2007_platform_data *pdata =
300 		dev_get_platdata(&client->dev);
301 	struct tsc2007 *ts;
302 	struct input_dev *input_dev;
303 	int err;
304 
305 	if (!i2c_check_functionality(client->adapter,
306 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
307 		return -EIO;
308 
309 	ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
310 	if (!ts)
311 		return -ENOMEM;
312 
313 	if (pdata)
314 		err = tsc2007_probe_pdev(&client->dev, ts, pdata, id);
315 	else
316 		err = tsc2007_probe_properties(&client->dev, ts);
317 	if (err)
318 		return err;
319 
320 	input_dev = devm_input_allocate_device(&client->dev);
321 	if (!input_dev)
322 		return -ENOMEM;
323 
324 	i2c_set_clientdata(client, ts);
325 
326 	ts->client = client;
327 	ts->irq = client->irq;
328 	ts->input = input_dev;
329 
330 	init_waitqueue_head(&ts->wait);
331 	mutex_init(&ts->mlock);
332 
333 	snprintf(ts->phys, sizeof(ts->phys),
334 		 "%s/input0", dev_name(&client->dev));
335 
336 	input_dev->name = "TSC2007 Touchscreen";
337 	input_dev->phys = ts->phys;
338 	input_dev->id.bustype = BUS_I2C;
339 
340 	input_dev->open = tsc2007_open;
341 	input_dev->close = tsc2007_close;
342 
343 	input_set_drvdata(input_dev, ts);
344 
345 	input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
346 	input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
347 	input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
348 	touchscreen_parse_properties(input_dev, false, &ts->prop);
349 	input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
350 			     ts->fuzzz, 0);
351 
352 	if (pdata) {
353 		if (pdata->exit_platform_hw) {
354 			err = devm_add_action(&client->dev,
355 					      tsc2007_call_exit_platform_hw,
356 					      &client->dev);
357 			if (err) {
358 				dev_err(&client->dev,
359 					"Failed to register exit_platform_hw action, %d\n",
360 					err);
361 				return err;
362 			}
363 		}
364 
365 		if (pdata->init_platform_hw)
366 			pdata->init_platform_hw();
367 	}
368 
369 	if (ts->irq) {
370 		err = devm_request_threaded_irq(&client->dev, ts->irq,
371 						NULL, tsc2007_soft_irq,
372 						IRQF_ONESHOT,
373 						client->dev.driver->name, ts);
374 		if (err) {
375 			dev_err(&client->dev, "Failed to request irq %d: %d\n",
376 				ts->irq, err);
377 			return err;
378 		}
379 
380 		tsc2007_stop(ts);
381 	}
382 
383 	/* power down the chip (TSC2007_SETUP does not ACK on I2C) */
384 	err = tsc2007_xfer(ts, PWRDOWN);
385 	if (err < 0) {
386 		dev_err(&client->dev,
387 			"Failed to setup chip: %d\n", err);
388 		return err;	/* chip does not respond */
389 	}
390 
391 	err = input_register_device(input_dev);
392 	if (err) {
393 		dev_err(&client->dev,
394 			"Failed to register input device: %d\n", err);
395 		return err;
396 	}
397 
398 	err =  tsc2007_iio_configure(ts);
399 	if (err) {
400 		dev_err(&client->dev,
401 			"Failed to register with IIO: %d\n", err);
402 		return err;
403 	}
404 
405 	return 0;
406 }
407 
408 static const struct i2c_device_id tsc2007_idtable[] = {
409 	{ "tsc2007" },
410 	{ }
411 };
412 
413 MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
414 
415 static const struct of_device_id tsc2007_of_match[] = {
416 	{ .compatible = "ti,tsc2007" },
417 	{ /* sentinel */ }
418 };
419 MODULE_DEVICE_TABLE(of, tsc2007_of_match);
420 
421 static struct i2c_driver tsc2007_driver = {
422 	.driver = {
423 		.name	= "tsc2007",
424 		.of_match_table = tsc2007_of_match,
425 	},
426 	.id_table	= tsc2007_idtable,
427 	.probe		= tsc2007_probe,
428 };
429 
430 module_i2c_driver(tsc2007_driver);
431 
432 MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
433 MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
434 MODULE_LICENSE("GPL");
435