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