Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
32int 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
54static 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
70u32 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
92bool 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
114static 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 /* Serialize access between the ISR and IIO reads. */
126 scoped_guard(mutex, &ts->mlock) {
127 tsc2007_read_values(ts, &tc);
128 }
129
130 rt = tsc2007_calculate_resistance(ts, &tc);
131
132 if (!rt && !ts->get_pendown_state) {
133 /*
134 * If pressure reported is 0 and we don't have
135 * callback to check pendown state, we have to
136 * assume that pen was lifted up.
137 */
138 break;
139 }
140
141 if (rt <= ts->max_rt) {
142 dev_dbg(&ts->client->dev,
143 "DOWN point(%4d,%4d), resistance (%4u)\n",
144 tc.x, tc.y, rt);
145
146 rt = ts->max_rt - rt;
147
148 input_report_key(input, BTN_TOUCH, 1);
149 touchscreen_report_pos(input, &ts->prop, tc.x, tc.y, false);
150 input_report_abs(input, ABS_PRESSURE, rt);
151
152 input_sync(input);
153
154 } else {
155 /*
156 * Sample found inconsistent by debouncing or pressure is
157 * beyond the maximum. Don't report it to user space,
158 * repeat at least once more the measurement.
159 */
160 dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
161 }
162
163 wait_event_timeout(ts->wait, ts->stopped, ts->poll_period);
164 }
165
166 dev_dbg(&ts->client->dev, "UP\n");
167
168 input_report_key(input, BTN_TOUCH, 0);
169 input_report_abs(input, ABS_PRESSURE, 0);
170 input_sync(input);
171
172 if (ts->clear_penirq)
173 ts->clear_penirq();
174
175 return IRQ_HANDLED;
176}
177
178static void tsc2007_stop(struct tsc2007 *ts)
179{
180 ts->stopped = true;
181 mb();
182 wake_up(&ts->wait);
183
184 if (ts->irq)
185 disable_irq(ts->irq);
186}
187
188static int tsc2007_open(struct input_dev *input_dev)
189{
190 struct tsc2007 *ts = input_get_drvdata(input_dev);
191 int err;
192
193 ts->stopped = false;
194 mb();
195
196 if (ts->irq)
197 enable_irq(ts->irq);
198
199 /* Prepare for touch readings - power down ADC and enable PENIRQ */
200 err = tsc2007_xfer(ts, PWRDOWN);
201 if (err < 0) {
202 tsc2007_stop(ts);
203 return err;
204 }
205
206 return 0;
207}
208
209static void tsc2007_close(struct input_dev *input_dev)
210{
211 struct tsc2007 *ts = input_get_drvdata(input_dev);
212
213 tsc2007_stop(ts);
214}
215
216static int tsc2007_get_pendown_state_gpio(struct device *dev)
217{
218 struct i2c_client *client = to_i2c_client(dev);
219 struct tsc2007 *ts = i2c_get_clientdata(client);
220
221 return gpiod_get_value_cansleep(ts->gpiod);
222}
223
224static int tsc2007_probe_properties(struct device *dev, struct tsc2007 *ts)
225{
226 u32 val32;
227 u64 val64;
228
229 if (!device_property_read_u32(dev, "ti,max-rt", &val32))
230 ts->max_rt = val32;
231 else
232 ts->max_rt = MAX_12BIT;
233
234 if (!device_property_read_u32(dev, "ti,fuzzx", &val32))
235 ts->fuzzx = val32;
236
237 if (!device_property_read_u32(dev, "ti,fuzzy", &val32))
238 ts->fuzzy = val32;
239
240 if (!device_property_read_u32(dev, "ti,fuzzz", &val32))
241 ts->fuzzz = val32;
242
243 if (!device_property_read_u64(dev, "ti,poll-period", &val64))
244 ts->poll_period = msecs_to_jiffies(val64);
245 else
246 ts->poll_period = msecs_to_jiffies(1);
247
248 if (!device_property_read_u32(dev, "ti,x-plate-ohms", &val32)) {
249 ts->x_plate_ohms = val32;
250 } else {
251 dev_err(dev, "Missing ti,x-plate-ohms device property\n");
252 return -EINVAL;
253 }
254
255 ts->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
256 if (IS_ERR(ts->gpiod))
257 return PTR_ERR(ts->gpiod);
258
259 if (ts->gpiod)
260 ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
261 else
262 dev_dbg(dev, "Pen down GPIO is not specified in properties\n");
263
264 return 0;
265}
266
267static int tsc2007_probe_pdev(struct device *dev, struct tsc2007 *ts,
268 const struct tsc2007_platform_data *pdata,
269 const struct i2c_device_id *id)
270{
271 ts->model = pdata->model;
272 ts->x_plate_ohms = pdata->x_plate_ohms;
273 ts->max_rt = pdata->max_rt ? : MAX_12BIT;
274 ts->poll_period = msecs_to_jiffies(pdata->poll_period ? : 1);
275 ts->get_pendown_state = pdata->get_pendown_state;
276 ts->clear_penirq = pdata->clear_penirq;
277 ts->fuzzx = pdata->fuzzx;
278 ts->fuzzy = pdata->fuzzy;
279 ts->fuzzz = pdata->fuzzz;
280
281 if (pdata->x_plate_ohms == 0) {
282 dev_err(dev, "x_plate_ohms is not set up in platform data\n");
283 return -EINVAL;
284 }
285
286 return 0;
287}
288
289static void tsc2007_call_exit_platform_hw(void *data)
290{
291 struct device *dev = data;
292 const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
293
294 pdata->exit_platform_hw();
295}
296
297static int tsc2007_probe(struct i2c_client *client)
298{
299 const struct i2c_device_id *id = i2c_client_get_device_id(client);
300 const struct tsc2007_platform_data *pdata =
301 dev_get_platdata(&client->dev);
302 struct tsc2007 *ts;
303 struct input_dev *input_dev;
304 int err;
305
306 if (!i2c_check_functionality(client->adapter,
307 I2C_FUNC_SMBUS_READ_WORD_DATA))
308 return -EIO;
309
310 ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
311 if (!ts)
312 return -ENOMEM;
313
314 if (pdata)
315 err = tsc2007_probe_pdev(&client->dev, ts, pdata, id);
316 else
317 err = tsc2007_probe_properties(&client->dev, ts);
318 if (err)
319 return err;
320
321 input_dev = devm_input_allocate_device(&client->dev);
322 if (!input_dev)
323 return -ENOMEM;
324
325 i2c_set_clientdata(client, ts);
326
327 ts->client = client;
328 ts->irq = client->irq;
329 ts->input = input_dev;
330
331 init_waitqueue_head(&ts->wait);
332 mutex_init(&ts->mlock);
333
334 snprintf(ts->phys, sizeof(ts->phys),
335 "%s/input0", dev_name(&client->dev));
336
337 input_dev->name = "TSC2007 Touchscreen";
338 input_dev->phys = ts->phys;
339 input_dev->id.bustype = BUS_I2C;
340
341 input_dev->open = tsc2007_open;
342 input_dev->close = tsc2007_close;
343
344 input_set_drvdata(input_dev, ts);
345
346 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
347 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
348 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
349 touchscreen_parse_properties(input_dev, false, &ts->prop);
350 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
351 ts->fuzzz, 0);
352
353 if (pdata) {
354 if (pdata->exit_platform_hw) {
355 err = devm_add_action(&client->dev,
356 tsc2007_call_exit_platform_hw,
357 &client->dev);
358 if (err) {
359 dev_err(&client->dev,
360 "Failed to register exit_platform_hw action, %d\n",
361 err);
362 return err;
363 }
364 }
365
366 if (pdata->init_platform_hw)
367 pdata->init_platform_hw();
368 }
369
370 if (ts->irq) {
371 err = devm_request_threaded_irq(&client->dev, ts->irq,
372 NULL, tsc2007_soft_irq,
373 IRQF_ONESHOT,
374 client->dev.driver->name, ts);
375 if (err) {
376 dev_err(&client->dev, "Failed to request irq %d: %d\n",
377 ts->irq, err);
378 return err;
379 }
380
381 tsc2007_stop(ts);
382 }
383
384 /* power down the chip (TSC2007_SETUP does not ACK on I2C) */
385 err = tsc2007_xfer(ts, PWRDOWN);
386 if (err < 0) {
387 dev_err(&client->dev,
388 "Failed to setup chip: %d\n", err);
389 return err; /* chip does not respond */
390 }
391
392 err = input_register_device(input_dev);
393 if (err) {
394 dev_err(&client->dev,
395 "Failed to register input device: %d\n", err);
396 return err;
397 }
398
399 err = tsc2007_iio_configure(ts);
400 if (err) {
401 dev_err(&client->dev,
402 "Failed to register with IIO: %d\n", err);
403 return err;
404 }
405
406 return 0;
407}
408
409static const struct i2c_device_id tsc2007_idtable[] = {
410 { "tsc2007" },
411 { }
412};
413
414MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
415
416static const struct of_device_id tsc2007_of_match[] = {
417 { .compatible = "ti,tsc2007" },
418 { /* sentinel */ }
419};
420MODULE_DEVICE_TABLE(of, tsc2007_of_match);
421
422static struct i2c_driver tsc2007_driver = {
423 .driver = {
424 .name = "tsc2007",
425 .of_match_table = tsc2007_of_match,
426 },
427 .id_table = tsc2007_idtable,
428 .probe = tsc2007_probe,
429};
430
431module_i2c_driver(tsc2007_driver);
432
433MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
434MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
435MODULE_LICENSE("GPL");