Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Pixcir I2C touchscreen controllers.
4 *
5 * Copyright (C) 2010-2011 Pixcir, Inc.
6 */
7
8#include <asm/unaligned.h>
9#include <linux/delay.h>
10#include <linux/gpio/consumer.h>
11#include <linux/i2c.h>
12#include <linux/input.h>
13#include <linux/input/mt.h>
14#include <linux/input/touchscreen.h>
15#include <linux/interrupt.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/slab.h>
19
20#define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
21
22/*
23 * Register map
24 */
25#define PIXCIR_REG_POWER_MODE 51
26#define PIXCIR_REG_INT_MODE 52
27
28/*
29 * Power modes:
30 * active: max scan speed
31 * idle: lower scan speed with automatic transition to active on touch
32 * halt: datasheet says sleep but this is more like halt as the chip
33 * clocks are cut and it can only be brought out of this mode
34 * using the RESET pin.
35 */
36enum pixcir_power_mode {
37 PIXCIR_POWER_ACTIVE,
38 PIXCIR_POWER_IDLE,
39 PIXCIR_POWER_HALT,
40};
41
42#define PIXCIR_POWER_MODE_MASK 0x03
43#define PIXCIR_POWER_ALLOW_IDLE (1UL << 2)
44
45/*
46 * Interrupt modes:
47 * periodical: interrupt is asserted periodicaly
48 * diff coordinates: interrupt is asserted when coordinates change
49 * level on touch: interrupt level asserted during touch
50 * pulse on touch: interrupt pulse asserted during touch
51 *
52 */
53enum pixcir_int_mode {
54 PIXCIR_INT_PERIODICAL,
55 PIXCIR_INT_DIFF_COORD,
56 PIXCIR_INT_LEVEL_TOUCH,
57 PIXCIR_INT_PULSE_TOUCH,
58};
59
60#define PIXCIR_INT_MODE_MASK 0x03
61#define PIXCIR_INT_ENABLE (1UL << 3)
62#define PIXCIR_INT_POL_HIGH (1UL << 2)
63
64/**
65 * struct pixcir_i2c_chip_data - chip related data
66 * @max_fingers: Max number of fingers reported simultaneously by h/w
67 * @has_hw_ids: Hardware supports finger tracking IDs
68 *
69 */
70struct pixcir_i2c_chip_data {
71 u8 max_fingers;
72 bool has_hw_ids;
73};
74
75struct pixcir_i2c_ts_data {
76 struct i2c_client *client;
77 struct input_dev *input;
78 struct gpio_desc *gpio_attb;
79 struct gpio_desc *gpio_reset;
80 struct gpio_desc *gpio_enable;
81 struct gpio_desc *gpio_wake;
82 const struct pixcir_i2c_chip_data *chip;
83 struct touchscreen_properties prop;
84 bool running;
85};
86
87struct pixcir_report_data {
88 int num_touches;
89 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
90 int ids[PIXCIR_MAX_SLOTS];
91};
92
93static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
94 struct pixcir_report_data *report)
95{
96 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
97 u8 wrbuf[1] = { 0 };
98 u8 *bufptr;
99 u8 touch;
100 int ret, i;
101 int readsize;
102 const struct pixcir_i2c_chip_data *chip = tsdata->chip;
103
104 memset(report, 0, sizeof(struct pixcir_report_data));
105
106 i = chip->has_hw_ids ? 1 : 0;
107 readsize = 2 + tsdata->chip->max_fingers * (4 + i);
108 if (readsize > sizeof(rdbuf))
109 readsize = sizeof(rdbuf);
110
111 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
112 if (ret != sizeof(wrbuf)) {
113 dev_err(&tsdata->client->dev,
114 "%s: i2c_master_send failed(), ret=%d\n",
115 __func__, ret);
116 return;
117 }
118
119 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
120 if (ret != readsize) {
121 dev_err(&tsdata->client->dev,
122 "%s: i2c_master_recv failed(), ret=%d\n",
123 __func__, ret);
124 return;
125 }
126
127 touch = rdbuf[0] & 0x7;
128 if (touch > tsdata->chip->max_fingers)
129 touch = tsdata->chip->max_fingers;
130
131 report->num_touches = touch;
132 bufptr = &rdbuf[2];
133
134 for (i = 0; i < touch; i++) {
135 touchscreen_set_mt_pos(&report->pos[i], &tsdata->prop,
136 get_unaligned_le16(bufptr),
137 get_unaligned_le16(bufptr + 2));
138 if (chip->has_hw_ids) {
139 report->ids[i] = bufptr[4];
140 bufptr = bufptr + 5;
141 } else {
142 bufptr = bufptr + 4;
143 }
144 }
145}
146
147static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
148 struct pixcir_report_data *report)
149{
150 int slots[PIXCIR_MAX_SLOTS];
151 int n, i, slot;
152 struct device *dev = &ts->client->dev;
153 const struct pixcir_i2c_chip_data *chip = ts->chip;
154
155 n = report->num_touches;
156 if (n > PIXCIR_MAX_SLOTS)
157 n = PIXCIR_MAX_SLOTS;
158
159 if (!ts->chip->has_hw_ids)
160 input_mt_assign_slots(ts->input, slots, report->pos, n, 0);
161
162 for (i = 0; i < n; i++) {
163 if (chip->has_hw_ids) {
164 slot = input_mt_get_slot_by_key(ts->input,
165 report->ids[i]);
166 if (slot < 0) {
167 dev_dbg(dev, "no free slot for id 0x%x\n",
168 report->ids[i]);
169 continue;
170 }
171 } else {
172 slot = slots[i];
173 }
174
175 input_mt_slot(ts->input, slot);
176 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
177
178 input_report_abs(ts->input, ABS_MT_POSITION_X,
179 report->pos[i].x);
180 input_report_abs(ts->input, ABS_MT_POSITION_Y,
181 report->pos[i].y);
182
183 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
184 i, slot, report->pos[i].x, report->pos[i].y);
185 }
186
187 input_mt_sync_frame(ts->input);
188 input_sync(ts->input);
189}
190
191static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
192{
193 struct pixcir_i2c_ts_data *tsdata = dev_id;
194 struct pixcir_report_data report;
195
196 while (tsdata->running) {
197 /* parse packet */
198 pixcir_ts_parse(tsdata, &report);
199
200 /* report it */
201 pixcir_ts_report(tsdata, &report);
202
203 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
204 if (report.num_touches) {
205 /*
206 * Last report with no finger up?
207 * Do it now then.
208 */
209 input_mt_sync_frame(tsdata->input);
210 input_sync(tsdata->input);
211 }
212 break;
213 }
214
215 msleep(20);
216 }
217
218 return IRQ_HANDLED;
219}
220
221static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
222{
223 if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
224 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
225 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
226 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
227 /* wait for controller ready. 100ms guess. */
228 msleep(100);
229 }
230}
231
232static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
233 enum pixcir_power_mode mode)
234{
235 struct device *dev = &ts->client->dev;
236 int ret;
237
238 if (mode == PIXCIR_POWER_ACTIVE || mode == PIXCIR_POWER_IDLE) {
239 if (ts->gpio_wake)
240 gpiod_set_value_cansleep(ts->gpio_wake, 1);
241 }
242
243 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
244 if (ret < 0) {
245 dev_err(dev, "%s: can't read reg %d : %d\n",
246 __func__, PIXCIR_REG_POWER_MODE, ret);
247 return ret;
248 }
249
250 ret &= ~PIXCIR_POWER_MODE_MASK;
251 ret |= mode;
252
253 /* Always AUTO_IDLE */
254 ret |= PIXCIR_POWER_ALLOW_IDLE;
255
256 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
257 if (ret < 0) {
258 dev_err(dev, "%s: can't write reg %d : %d\n",
259 __func__, PIXCIR_REG_POWER_MODE, ret);
260 return ret;
261 }
262
263 if (mode == PIXCIR_POWER_HALT) {
264 if (ts->gpio_wake)
265 gpiod_set_value_cansleep(ts->gpio_wake, 0);
266 }
267
268 return 0;
269}
270
271/*
272 * Set the interrupt mode for the device i.e. ATTB line behaviour
273 *
274 * @polarity : 1 for active high, 0 for active low.
275 */
276static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
277 enum pixcir_int_mode mode, bool polarity)
278{
279 struct device *dev = &ts->client->dev;
280 int ret;
281
282 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
283 if (ret < 0) {
284 dev_err(dev, "%s: can't read reg %d : %d\n",
285 __func__, PIXCIR_REG_INT_MODE, ret);
286 return ret;
287 }
288
289 ret &= ~PIXCIR_INT_MODE_MASK;
290 ret |= mode;
291
292 if (polarity)
293 ret |= PIXCIR_INT_POL_HIGH;
294 else
295 ret &= ~PIXCIR_INT_POL_HIGH;
296
297 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
298 if (ret < 0) {
299 dev_err(dev, "%s: can't write reg %d : %d\n",
300 __func__, PIXCIR_REG_INT_MODE, ret);
301 return ret;
302 }
303
304 return 0;
305}
306
307/*
308 * Enable/disable interrupt generation
309 */
310static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
311{
312 struct device *dev = &ts->client->dev;
313 int ret;
314
315 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
316 if (ret < 0) {
317 dev_err(dev, "%s: can't read reg %d : %d\n",
318 __func__, PIXCIR_REG_INT_MODE, ret);
319 return ret;
320 }
321
322 if (enable)
323 ret |= PIXCIR_INT_ENABLE;
324 else
325 ret &= ~PIXCIR_INT_ENABLE;
326
327 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
328 if (ret < 0) {
329 dev_err(dev, "%s: can't write reg %d : %d\n",
330 __func__, PIXCIR_REG_INT_MODE, ret);
331 return ret;
332 }
333
334 return 0;
335}
336
337static int pixcir_start(struct pixcir_i2c_ts_data *ts)
338{
339 struct device *dev = &ts->client->dev;
340 int error;
341
342 if (ts->gpio_enable) {
343 gpiod_set_value_cansleep(ts->gpio_enable, 1);
344 msleep(100);
345 }
346
347 /* LEVEL_TOUCH interrupt with active low polarity */
348 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
349 if (error) {
350 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
351 return error;
352 }
353
354 ts->running = true;
355 mb(); /* Update status before IRQ can fire */
356
357 /* enable interrupt generation */
358 error = pixcir_int_enable(ts, true);
359 if (error) {
360 dev_err(dev, "Failed to enable interrupt generation: %d\n",
361 error);
362 return error;
363 }
364
365 return 0;
366}
367
368static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
369{
370 int error;
371
372 /* Disable interrupt generation */
373 error = pixcir_int_enable(ts, false);
374 if (error) {
375 dev_err(&ts->client->dev,
376 "Failed to disable interrupt generation: %d\n",
377 error);
378 return error;
379 }
380
381 /* Exit ISR if running, no more report parsing */
382 ts->running = false;
383 mb(); /* update status before we synchronize irq */
384
385 /* Wait till running ISR is complete */
386 synchronize_irq(ts->client->irq);
387
388 if (ts->gpio_enable)
389 gpiod_set_value_cansleep(ts->gpio_enable, 0);
390
391 return 0;
392}
393
394static int pixcir_input_open(struct input_dev *dev)
395{
396 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
397
398 return pixcir_start(ts);
399}
400
401static void pixcir_input_close(struct input_dev *dev)
402{
403 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
404
405 pixcir_stop(ts);
406}
407
408static int pixcir_i2c_ts_suspend(struct device *dev)
409{
410 struct i2c_client *client = to_i2c_client(dev);
411 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
412 struct input_dev *input = ts->input;
413 int ret = 0;
414
415 mutex_lock(&input->mutex);
416
417 if (device_may_wakeup(&client->dev)) {
418 if (!input_device_enabled(input)) {
419 ret = pixcir_start(ts);
420 if (ret) {
421 dev_err(dev, "Failed to start\n");
422 goto unlock;
423 }
424 }
425 } else if (input_device_enabled(input)) {
426 ret = pixcir_stop(ts);
427 }
428
429unlock:
430 mutex_unlock(&input->mutex);
431
432 return ret;
433}
434
435static int pixcir_i2c_ts_resume(struct device *dev)
436{
437 struct i2c_client *client = to_i2c_client(dev);
438 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
439 struct input_dev *input = ts->input;
440 int ret = 0;
441
442 mutex_lock(&input->mutex);
443
444 if (device_may_wakeup(&client->dev)) {
445 if (!input_device_enabled(input)) {
446 ret = pixcir_stop(ts);
447 if (ret) {
448 dev_err(dev, "Failed to stop\n");
449 goto unlock;
450 }
451 }
452 } else if (input_device_enabled(input)) {
453 ret = pixcir_start(ts);
454 }
455
456unlock:
457 mutex_unlock(&input->mutex);
458
459 return ret;
460}
461
462static DEFINE_SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
463 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
464
465static int pixcir_i2c_ts_probe(struct i2c_client *client)
466{
467 const struct i2c_device_id *id = i2c_client_get_device_id(client);
468 struct device *dev = &client->dev;
469 struct pixcir_i2c_ts_data *tsdata;
470 struct input_dev *input;
471 int error;
472
473 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
474 if (!tsdata)
475 return -ENOMEM;
476
477 tsdata->chip = device_get_match_data(dev);
478 if (!tsdata->chip && id)
479 tsdata->chip = (const void *)id->driver_data;
480 if (!tsdata->chip) {
481 dev_err(dev, "can't locate chip data\n");
482 return -EINVAL;
483 }
484
485 input = devm_input_allocate_device(dev);
486 if (!input) {
487 dev_err(dev, "Failed to allocate input device\n");
488 return -ENOMEM;
489 }
490
491 tsdata->client = client;
492 tsdata->input = input;
493
494 input->name = client->name;
495 input->id.bustype = BUS_I2C;
496 input->open = pixcir_input_open;
497 input->close = pixcir_input_close;
498
499 input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
500 input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
501 touchscreen_parse_properties(input, true, &tsdata->prop);
502 if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
503 !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
504 dev_err(dev, "Touchscreen size is not specified\n");
505 return -EINVAL;
506 }
507
508 error = input_mt_init_slots(input, tsdata->chip->max_fingers,
509 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
510 if (error) {
511 dev_err(dev, "Error initializing Multi-Touch slots\n");
512 return error;
513 }
514
515 input_set_drvdata(input, tsdata);
516
517 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
518 if (IS_ERR(tsdata->gpio_attb))
519 return dev_err_probe(dev, PTR_ERR(tsdata->gpio_attb),
520 "Failed to request ATTB gpio\n");
521
522 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
523 GPIOD_OUT_LOW);
524 if (IS_ERR(tsdata->gpio_reset))
525 return dev_err_probe(dev, PTR_ERR(tsdata->gpio_reset),
526 "Failed to request RESET gpio\n");
527
528 tsdata->gpio_wake = devm_gpiod_get_optional(dev, "wake",
529 GPIOD_OUT_HIGH);
530 if (IS_ERR(tsdata->gpio_wake))
531 return dev_err_probe(dev, PTR_ERR(tsdata->gpio_wake),
532 "Failed to get wake gpio\n");
533
534 tsdata->gpio_enable = devm_gpiod_get_optional(dev, "enable",
535 GPIOD_OUT_HIGH);
536 if (IS_ERR(tsdata->gpio_enable))
537 return dev_err_probe(dev, PTR_ERR(tsdata->gpio_enable),
538 "Failed to get enable gpio\n");
539
540 if (tsdata->gpio_enable)
541 msleep(100);
542
543 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
544 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
545 client->name, tsdata);
546 if (error) {
547 dev_err(dev, "failed to request irq %d\n", client->irq);
548 return error;
549 }
550
551 pixcir_reset(tsdata);
552
553 /* Always be in IDLE mode to save power, device supports auto wake */
554 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
555 if (error) {
556 dev_err(dev, "Failed to set IDLE mode\n");
557 return error;
558 }
559
560 /* Stop device till opened */
561 error = pixcir_stop(tsdata);
562 if (error)
563 return error;
564
565 error = input_register_device(input);
566 if (error)
567 return error;
568
569 i2c_set_clientdata(client, tsdata);
570
571 return 0;
572}
573
574static const struct pixcir_i2c_chip_data pixcir_ts_data = {
575 .max_fingers = 2,
576 /* no hw id support */
577};
578
579static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
580 .max_fingers = 5,
581 .has_hw_ids = true,
582};
583
584static const struct i2c_device_id pixcir_i2c_ts_id[] = {
585 { "pixcir_ts", (unsigned long) &pixcir_ts_data },
586 { "pixcir_tangoc", (unsigned long) &pixcir_tangoc_data },
587 { }
588};
589MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
590
591#ifdef CONFIG_OF
592static const struct of_device_id pixcir_of_match[] = {
593 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
594 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
595 { }
596};
597MODULE_DEVICE_TABLE(of, pixcir_of_match);
598#endif
599
600static struct i2c_driver pixcir_i2c_ts_driver = {
601 .driver = {
602 .name = "pixcir_ts",
603 .pm = pm_sleep_ptr(&pixcir_dev_pm_ops),
604 .of_match_table = of_match_ptr(pixcir_of_match),
605 },
606 .probe = pixcir_i2c_ts_probe,
607 .id_table = pixcir_i2c_ts_id,
608};
609
610module_i2c_driver(pixcir_i2c_ts_driver);
611
612MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
613MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
614MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Pixcir I2C touchscreen controllers.
4 *
5 * Copyright (C) 2010-2011 Pixcir, Inc.
6 */
7
8#include <linux/delay.h>
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/slab.h>
12#include <linux/i2c.h>
13#include <linux/input.h>
14#include <linux/input/mt.h>
15#include <linux/input/touchscreen.h>
16#include <linux/gpio.h>
17#include <linux/gpio/consumer.h>
18#include <linux/of_device.h>
19#include <linux/platform_data/pixcir_i2c_ts.h>
20#include <asm/unaligned.h>
21
22#define PIXCIR_MAX_SLOTS 5 /* Max fingers supported by driver */
23
24struct pixcir_i2c_ts_data {
25 struct i2c_client *client;
26 struct input_dev *input;
27 struct gpio_desc *gpio_attb;
28 struct gpio_desc *gpio_reset;
29 struct gpio_desc *gpio_enable;
30 struct gpio_desc *gpio_wake;
31 const struct pixcir_i2c_chip_data *chip;
32 struct touchscreen_properties prop;
33 int max_fingers; /* Max fingers supported in this instance */
34 bool running;
35};
36
37struct pixcir_report_data {
38 int num_touches;
39 struct input_mt_pos pos[PIXCIR_MAX_SLOTS];
40 int ids[PIXCIR_MAX_SLOTS];
41};
42
43static void pixcir_ts_parse(struct pixcir_i2c_ts_data *tsdata,
44 struct pixcir_report_data *report)
45{
46 u8 rdbuf[2 + PIXCIR_MAX_SLOTS * 5];
47 u8 wrbuf[1] = { 0 };
48 u8 *bufptr;
49 u8 touch;
50 int ret, i;
51 int readsize;
52 const struct pixcir_i2c_chip_data *chip = tsdata->chip;
53
54 memset(report, 0, sizeof(struct pixcir_report_data));
55
56 i = chip->has_hw_ids ? 1 : 0;
57 readsize = 2 + tsdata->max_fingers * (4 + i);
58 if (readsize > sizeof(rdbuf))
59 readsize = sizeof(rdbuf);
60
61 ret = i2c_master_send(tsdata->client, wrbuf, sizeof(wrbuf));
62 if (ret != sizeof(wrbuf)) {
63 dev_err(&tsdata->client->dev,
64 "%s: i2c_master_send failed(), ret=%d\n",
65 __func__, ret);
66 return;
67 }
68
69 ret = i2c_master_recv(tsdata->client, rdbuf, readsize);
70 if (ret != readsize) {
71 dev_err(&tsdata->client->dev,
72 "%s: i2c_master_recv failed(), ret=%d\n",
73 __func__, ret);
74 return;
75 }
76
77 touch = rdbuf[0] & 0x7;
78 if (touch > tsdata->max_fingers)
79 touch = tsdata->max_fingers;
80
81 report->num_touches = touch;
82 bufptr = &rdbuf[2];
83
84 for (i = 0; i < touch; i++) {
85 touchscreen_set_mt_pos(&report->pos[i], &tsdata->prop,
86 get_unaligned_le16(bufptr),
87 get_unaligned_le16(bufptr + 2));
88 if (chip->has_hw_ids) {
89 report->ids[i] = bufptr[4];
90 bufptr = bufptr + 5;
91 } else {
92 bufptr = bufptr + 4;
93 }
94 }
95}
96
97static void pixcir_ts_report(struct pixcir_i2c_ts_data *ts,
98 struct pixcir_report_data *report)
99{
100 int slots[PIXCIR_MAX_SLOTS];
101 int n, i, slot;
102 struct device *dev = &ts->client->dev;
103 const struct pixcir_i2c_chip_data *chip = ts->chip;
104
105 n = report->num_touches;
106 if (n > PIXCIR_MAX_SLOTS)
107 n = PIXCIR_MAX_SLOTS;
108
109 if (!ts->chip->has_hw_ids)
110 input_mt_assign_slots(ts->input, slots, report->pos, n, 0);
111
112 for (i = 0; i < n; i++) {
113 if (chip->has_hw_ids) {
114 slot = input_mt_get_slot_by_key(ts->input,
115 report->ids[i]);
116 if (slot < 0) {
117 dev_dbg(dev, "no free slot for id 0x%x\n",
118 report->ids[i]);
119 continue;
120 }
121 } else {
122 slot = slots[i];
123 }
124
125 input_mt_slot(ts->input, slot);
126 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
127
128 input_report_abs(ts->input, ABS_MT_POSITION_X,
129 report->pos[i].x);
130 input_report_abs(ts->input, ABS_MT_POSITION_Y,
131 report->pos[i].y);
132
133 dev_dbg(dev, "%d: slot %d, x %d, y %d\n",
134 i, slot, report->pos[i].x, report->pos[i].y);
135 }
136
137 input_mt_sync_frame(ts->input);
138 input_sync(ts->input);
139}
140
141static irqreturn_t pixcir_ts_isr(int irq, void *dev_id)
142{
143 struct pixcir_i2c_ts_data *tsdata = dev_id;
144 struct pixcir_report_data report;
145
146 while (tsdata->running) {
147 /* parse packet */
148 pixcir_ts_parse(tsdata, &report);
149
150 /* report it */
151 pixcir_ts_report(tsdata, &report);
152
153 if (gpiod_get_value_cansleep(tsdata->gpio_attb)) {
154 if (report.num_touches) {
155 /*
156 * Last report with no finger up?
157 * Do it now then.
158 */
159 input_mt_sync_frame(tsdata->input);
160 input_sync(tsdata->input);
161 }
162 break;
163 }
164
165 msleep(20);
166 }
167
168 return IRQ_HANDLED;
169}
170
171static void pixcir_reset(struct pixcir_i2c_ts_data *tsdata)
172{
173 if (!IS_ERR_OR_NULL(tsdata->gpio_reset)) {
174 gpiod_set_value_cansleep(tsdata->gpio_reset, 1);
175 ndelay(100); /* datasheet section 1.2.3 says 80ns min. */
176 gpiod_set_value_cansleep(tsdata->gpio_reset, 0);
177 /* wait for controller ready. 100ms guess. */
178 msleep(100);
179 }
180}
181
182static int pixcir_set_power_mode(struct pixcir_i2c_ts_data *ts,
183 enum pixcir_power_mode mode)
184{
185 struct device *dev = &ts->client->dev;
186 int ret;
187
188 if (mode == PIXCIR_POWER_ACTIVE || mode == PIXCIR_POWER_IDLE) {
189 if (ts->gpio_wake)
190 gpiod_set_value_cansleep(ts->gpio_wake, 1);
191 }
192
193 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_POWER_MODE);
194 if (ret < 0) {
195 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
196 __func__, PIXCIR_REG_POWER_MODE, ret);
197 return ret;
198 }
199
200 ret &= ~PIXCIR_POWER_MODE_MASK;
201 ret |= mode;
202
203 /* Always AUTO_IDLE */
204 ret |= PIXCIR_POWER_ALLOW_IDLE;
205
206 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_POWER_MODE, ret);
207 if (ret < 0) {
208 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
209 __func__, PIXCIR_REG_POWER_MODE, ret);
210 return ret;
211 }
212
213 if (mode == PIXCIR_POWER_HALT) {
214 if (ts->gpio_wake)
215 gpiod_set_value_cansleep(ts->gpio_wake, 0);
216 }
217
218 return 0;
219}
220
221/*
222 * Set the interrupt mode for the device i.e. ATTB line behaviour
223 *
224 * @polarity : 1 for active high, 0 for active low.
225 */
226static int pixcir_set_int_mode(struct pixcir_i2c_ts_data *ts,
227 enum pixcir_int_mode mode, bool polarity)
228{
229 struct device *dev = &ts->client->dev;
230 int ret;
231
232 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
233 if (ret < 0) {
234 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
235 __func__, PIXCIR_REG_INT_MODE, ret);
236 return ret;
237 }
238
239 ret &= ~PIXCIR_INT_MODE_MASK;
240 ret |= mode;
241
242 if (polarity)
243 ret |= PIXCIR_INT_POL_HIGH;
244 else
245 ret &= ~PIXCIR_INT_POL_HIGH;
246
247 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
248 if (ret < 0) {
249 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
250 __func__, PIXCIR_REG_INT_MODE, ret);
251 return ret;
252 }
253
254 return 0;
255}
256
257/*
258 * Enable/disable interrupt generation
259 */
260static int pixcir_int_enable(struct pixcir_i2c_ts_data *ts, bool enable)
261{
262 struct device *dev = &ts->client->dev;
263 int ret;
264
265 ret = i2c_smbus_read_byte_data(ts->client, PIXCIR_REG_INT_MODE);
266 if (ret < 0) {
267 dev_err(dev, "%s: can't read reg 0x%x : %d\n",
268 __func__, PIXCIR_REG_INT_MODE, ret);
269 return ret;
270 }
271
272 if (enable)
273 ret |= PIXCIR_INT_ENABLE;
274 else
275 ret &= ~PIXCIR_INT_ENABLE;
276
277 ret = i2c_smbus_write_byte_data(ts->client, PIXCIR_REG_INT_MODE, ret);
278 if (ret < 0) {
279 dev_err(dev, "%s: can't write reg 0x%x : %d\n",
280 __func__, PIXCIR_REG_INT_MODE, ret);
281 return ret;
282 }
283
284 return 0;
285}
286
287static int pixcir_start(struct pixcir_i2c_ts_data *ts)
288{
289 struct device *dev = &ts->client->dev;
290 int error;
291
292 if (ts->gpio_enable) {
293 gpiod_set_value_cansleep(ts->gpio_enable, 1);
294 msleep(100);
295 }
296
297 /* LEVEL_TOUCH interrupt with active low polarity */
298 error = pixcir_set_int_mode(ts, PIXCIR_INT_LEVEL_TOUCH, 0);
299 if (error) {
300 dev_err(dev, "Failed to set interrupt mode: %d\n", error);
301 return error;
302 }
303
304 ts->running = true;
305 mb(); /* Update status before IRQ can fire */
306
307 /* enable interrupt generation */
308 error = pixcir_int_enable(ts, true);
309 if (error) {
310 dev_err(dev, "Failed to enable interrupt generation: %d\n",
311 error);
312 return error;
313 }
314
315 return 0;
316}
317
318static int pixcir_stop(struct pixcir_i2c_ts_data *ts)
319{
320 int error;
321
322 /* Disable interrupt generation */
323 error = pixcir_int_enable(ts, false);
324 if (error) {
325 dev_err(&ts->client->dev,
326 "Failed to disable interrupt generation: %d\n",
327 error);
328 return error;
329 }
330
331 /* Exit ISR if running, no more report parsing */
332 ts->running = false;
333 mb(); /* update status before we synchronize irq */
334
335 /* Wait till running ISR is complete */
336 synchronize_irq(ts->client->irq);
337
338 if (ts->gpio_enable)
339 gpiod_set_value_cansleep(ts->gpio_enable, 0);
340
341 return 0;
342}
343
344static int pixcir_input_open(struct input_dev *dev)
345{
346 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
347
348 return pixcir_start(ts);
349}
350
351static void pixcir_input_close(struct input_dev *dev)
352{
353 struct pixcir_i2c_ts_data *ts = input_get_drvdata(dev);
354
355 pixcir_stop(ts);
356}
357
358static int __maybe_unused pixcir_i2c_ts_suspend(struct device *dev)
359{
360 struct i2c_client *client = to_i2c_client(dev);
361 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
362 struct input_dev *input = ts->input;
363 int ret = 0;
364
365 mutex_lock(&input->mutex);
366
367 if (device_may_wakeup(&client->dev)) {
368 if (!input->users) {
369 ret = pixcir_start(ts);
370 if (ret) {
371 dev_err(dev, "Failed to start\n");
372 goto unlock;
373 }
374 }
375 } else if (input->users) {
376 ret = pixcir_stop(ts);
377 }
378
379unlock:
380 mutex_unlock(&input->mutex);
381
382 return ret;
383}
384
385static int __maybe_unused pixcir_i2c_ts_resume(struct device *dev)
386{
387 struct i2c_client *client = to_i2c_client(dev);
388 struct pixcir_i2c_ts_data *ts = i2c_get_clientdata(client);
389 struct input_dev *input = ts->input;
390 int ret = 0;
391
392 mutex_lock(&input->mutex);
393
394 if (device_may_wakeup(&client->dev)) {
395 if (!input->users) {
396 ret = pixcir_stop(ts);
397 if (ret) {
398 dev_err(dev, "Failed to stop\n");
399 goto unlock;
400 }
401 }
402 } else if (input->users) {
403 ret = pixcir_start(ts);
404 }
405
406unlock:
407 mutex_unlock(&input->mutex);
408
409 return ret;
410}
411
412static SIMPLE_DEV_PM_OPS(pixcir_dev_pm_ops,
413 pixcir_i2c_ts_suspend, pixcir_i2c_ts_resume);
414
415#ifdef CONFIG_OF
416static const struct of_device_id pixcir_of_match[];
417
418static int pixcir_parse_dt(struct device *dev,
419 struct pixcir_i2c_ts_data *tsdata)
420{
421 tsdata->chip = of_device_get_match_data(dev);
422 if (!tsdata->chip)
423 return -EINVAL;
424
425 return 0;
426}
427#else
428static int pixcir_parse_dt(struct device *dev,
429 struct pixcir_i2c_ts_data *tsdata)
430{
431 return -EINVAL;
432}
433#endif
434
435static int pixcir_i2c_ts_probe(struct i2c_client *client,
436 const struct i2c_device_id *id)
437{
438 const struct pixcir_ts_platform_data *pdata =
439 dev_get_platdata(&client->dev);
440 struct device *dev = &client->dev;
441 struct pixcir_i2c_ts_data *tsdata;
442 struct input_dev *input;
443 int error;
444
445 tsdata = devm_kzalloc(dev, sizeof(*tsdata), GFP_KERNEL);
446 if (!tsdata)
447 return -ENOMEM;
448
449 if (pdata) {
450 tsdata->chip = &pdata->chip;
451 } else if (dev->of_node) {
452 error = pixcir_parse_dt(dev, tsdata);
453 if (error)
454 return error;
455 } else {
456 dev_err(dev, "platform data not defined\n");
457 return -EINVAL;
458 }
459
460 if (!tsdata->chip->max_fingers) {
461 dev_err(dev, "Invalid max_fingers in chip data\n");
462 return -EINVAL;
463 }
464
465 input = devm_input_allocate_device(dev);
466 if (!input) {
467 dev_err(dev, "Failed to allocate input device\n");
468 return -ENOMEM;
469 }
470
471 tsdata->client = client;
472 tsdata->input = input;
473
474 input->name = client->name;
475 input->id.bustype = BUS_I2C;
476 input->open = pixcir_input_open;
477 input->close = pixcir_input_close;
478 input->dev.parent = dev;
479
480 if (pdata) {
481 input_set_abs_params(input, ABS_MT_POSITION_X, 0, pdata->x_max, 0, 0);
482 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, pdata->y_max, 0, 0);
483 } else {
484 input_set_capability(input, EV_ABS, ABS_MT_POSITION_X);
485 input_set_capability(input, EV_ABS, ABS_MT_POSITION_Y);
486 touchscreen_parse_properties(input, true, &tsdata->prop);
487 if (!input_abs_get_max(input, ABS_MT_POSITION_X) ||
488 !input_abs_get_max(input, ABS_MT_POSITION_Y)) {
489 dev_err(dev, "Touchscreen size is not specified\n");
490 return -EINVAL;
491 }
492 }
493
494 tsdata->max_fingers = tsdata->chip->max_fingers;
495 if (tsdata->max_fingers > PIXCIR_MAX_SLOTS) {
496 tsdata->max_fingers = PIXCIR_MAX_SLOTS;
497 dev_info(dev, "Limiting maximum fingers to %d\n",
498 tsdata->max_fingers);
499 }
500
501 error = input_mt_init_slots(input, tsdata->max_fingers,
502 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
503 if (error) {
504 dev_err(dev, "Error initializing Multi-Touch slots\n");
505 return error;
506 }
507
508 input_set_drvdata(input, tsdata);
509
510 tsdata->gpio_attb = devm_gpiod_get(dev, "attb", GPIOD_IN);
511 if (IS_ERR(tsdata->gpio_attb)) {
512 error = PTR_ERR(tsdata->gpio_attb);
513 dev_err(dev, "Failed to request ATTB gpio: %d\n", error);
514 return error;
515 }
516
517 tsdata->gpio_reset = devm_gpiod_get_optional(dev, "reset",
518 GPIOD_OUT_LOW);
519 if (IS_ERR(tsdata->gpio_reset)) {
520 error = PTR_ERR(tsdata->gpio_reset);
521 dev_err(dev, "Failed to request RESET gpio: %d\n", error);
522 return error;
523 }
524
525 tsdata->gpio_wake = devm_gpiod_get_optional(dev, "wake",
526 GPIOD_OUT_HIGH);
527 if (IS_ERR(tsdata->gpio_wake)) {
528 error = PTR_ERR(tsdata->gpio_wake);
529 if (error != -EPROBE_DEFER)
530 dev_err(dev, "Failed to get wake gpio: %d\n", error);
531 return error;
532 }
533
534 tsdata->gpio_enable = devm_gpiod_get_optional(dev, "enable",
535 GPIOD_OUT_HIGH);
536 if (IS_ERR(tsdata->gpio_enable)) {
537 error = PTR_ERR(tsdata->gpio_enable);
538 if (error != -EPROBE_DEFER)
539 dev_err(dev, "Failed to get enable gpio: %d\n", error);
540 return error;
541 }
542
543 if (tsdata->gpio_enable)
544 msleep(100);
545
546 error = devm_request_threaded_irq(dev, client->irq, NULL, pixcir_ts_isr,
547 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
548 client->name, tsdata);
549 if (error) {
550 dev_err(dev, "failed to request irq %d\n", client->irq);
551 return error;
552 }
553
554 pixcir_reset(tsdata);
555
556 /* Always be in IDLE mode to save power, device supports auto wake */
557 error = pixcir_set_power_mode(tsdata, PIXCIR_POWER_IDLE);
558 if (error) {
559 dev_err(dev, "Failed to set IDLE mode\n");
560 return error;
561 }
562
563 /* Stop device till opened */
564 error = pixcir_stop(tsdata);
565 if (error)
566 return error;
567
568 error = input_register_device(input);
569 if (error)
570 return error;
571
572 i2c_set_clientdata(client, tsdata);
573
574 return 0;
575}
576
577static const struct i2c_device_id pixcir_i2c_ts_id[] = {
578 { "pixcir_ts", 0 },
579 { "pixcir_tangoc", 0 },
580 { }
581};
582MODULE_DEVICE_TABLE(i2c, pixcir_i2c_ts_id);
583
584#ifdef CONFIG_OF
585static const struct pixcir_i2c_chip_data pixcir_ts_data = {
586 .max_fingers = 2,
587 /* no hw id support */
588};
589
590static const struct pixcir_i2c_chip_data pixcir_tangoc_data = {
591 .max_fingers = 5,
592 .has_hw_ids = true,
593};
594
595static const struct of_device_id pixcir_of_match[] = {
596 { .compatible = "pixcir,pixcir_ts", .data = &pixcir_ts_data },
597 { .compatible = "pixcir,pixcir_tangoc", .data = &pixcir_tangoc_data },
598 { }
599};
600MODULE_DEVICE_TABLE(of, pixcir_of_match);
601#endif
602
603static struct i2c_driver pixcir_i2c_ts_driver = {
604 .driver = {
605 .name = "pixcir_ts",
606 .pm = &pixcir_dev_pm_ops,
607 .of_match_table = of_match_ptr(pixcir_of_match),
608 },
609 .probe = pixcir_i2c_ts_probe,
610 .id_table = pixcir_i2c_ts_id,
611};
612
613module_i2c_driver(pixcir_i2c_ts_driver);
614
615MODULE_AUTHOR("Jianchun Bian <jcbian@pixcir.com.cn>, Dequan Meng <dqmeng@pixcir.com.cn>");
616MODULE_DESCRIPTION("Pixcir I2C Touchscreen Driver");
617MODULE_LICENSE("GPL");