1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/interrupt.h>
26#include <linux/slab.h>
27#include <linux/input.h>
28#include <linux/jiffies.h>
29#include <linux/i2c.h>
30#include <linux/mutex.h>
31#include <linux/delay.h>
32#include <linux/gpio.h>
33#include <linux/input/auo-pixcir-ts.h>
34#include <linux/of.h>
35#include <linux/of_gpio.h>
36
37
38
39
40
41
42
43
44#define AUO_PIXCIR_REG_X1_LSB 0x00
45#define AUO_PIXCIR_REG_X1_MSB 0x01
46#define AUO_PIXCIR_REG_Y1_LSB 0x02
47#define AUO_PIXCIR_REG_Y1_MSB 0x03
48#define AUO_PIXCIR_REG_X2_LSB 0x04
49#define AUO_PIXCIR_REG_X2_MSB 0x05
50#define AUO_PIXCIR_REG_Y2_LSB 0x06
51#define AUO_PIXCIR_REG_Y2_MSB 0x07
52
53#define AUO_PIXCIR_REG_STRENGTH 0x0d
54#define AUO_PIXCIR_REG_STRENGTH_X1_LSB 0x0e
55#define AUO_PIXCIR_REG_STRENGTH_X1_MSB 0x0f
56
57#define AUO_PIXCIR_REG_RAW_DATA_X 0x2b
58#define AUO_PIXCIR_REG_RAW_DATA_Y 0x4f
59
60#define AUO_PIXCIR_REG_X_SENSITIVITY 0x6f
61#define AUO_PIXCIR_REG_Y_SENSITIVITY 0x70
62#define AUO_PIXCIR_REG_INT_SETTING 0x71
63#define AUO_PIXCIR_REG_INT_WIDTH 0x72
64#define AUO_PIXCIR_REG_POWER_MODE 0x73
65
66#define AUO_PIXCIR_REG_VERSION 0x77
67#define AUO_PIXCIR_REG_CALIBRATE 0x78
68
69#define AUO_PIXCIR_REG_TOUCHAREA_X1 0x1e
70#define AUO_PIXCIR_REG_TOUCHAREA_Y1 0x1f
71#define AUO_PIXCIR_REG_TOUCHAREA_X2 0x20
72#define AUO_PIXCIR_REG_TOUCHAREA_Y2 0x21
73
74#define AUO_PIXCIR_REG_EEPROM_CALIB_X 0x42
75#define AUO_PIXCIR_REG_EEPROM_CALIB_Y 0xad
76
77#define AUO_PIXCIR_INT_TPNUM_MASK 0xe0
78#define AUO_PIXCIR_INT_TPNUM_SHIFT 5
79#define AUO_PIXCIR_INT_RELEASE (1 << 4)
80#define AUO_PIXCIR_INT_ENABLE (1 << 3)
81#define AUO_PIXCIR_INT_POL_HIGH (1 << 2)
82#define AUO_PIXCIR_INT_MODE_MASK 0x03
83
84
85
86
87
88
89
90#define AUO_PIXCIR_POWER_ACTIVE 0x00
91#define AUO_PIXCIR_POWER_SLEEP 0x01
92#define AUO_PIXCIR_POWER_DEEP_SLEEP 0x02
93#define AUO_PIXCIR_POWER_MASK 0x03
94
95#define AUO_PIXCIR_POWER_ALLOW_SLEEP (1 << 2)
96#define AUO_PIXCIR_POWER_IDLE_TIME(ms) ((ms & 0xf) << 4)
97
98#define AUO_PIXCIR_CALIBRATE 0x03
99
100#define AUO_PIXCIR_EEPROM_CALIB_X_LEN 62
101#define AUO_PIXCIR_EEPROM_CALIB_Y_LEN 36
102
103#define AUO_PIXCIR_RAW_DATA_X_LEN 18
104#define AUO_PIXCIR_RAW_DATA_Y_LEN 11
105
106#define AUO_PIXCIR_STRENGTH_ENABLE (1 << 0)
107
108
109#define AUO_PIXCIR_REPORT_POINTS 2
110#define AUO_PIXCIR_MAX_AREA 0xff
111#define AUO_PIXCIR_PENUP_TIMEOUT_MS 10
112
113struct auo_pixcir_ts {
114 struct i2c_client *client;
115 struct input_dev *input;
116 const struct auo_pixcir_ts_platdata *pdata;
117 char phys[32];
118
119
120 bool touch_ind_mode;
121
122 wait_queue_head_t wait;
123 bool stopped;
124};
125
126struct auo_point_t {
127 int coord_x;
128 int coord_y;
129 int area_major;
130 int area_minor;
131 int orientation;
132};
133
134static int auo_pixcir_collect_data(struct auo_pixcir_ts *ts,
135 struct auo_point_t *point)
136{
137 struct i2c_client *client = ts->client;
138 const struct auo_pixcir_ts_platdata *pdata = ts->pdata;
139 uint8_t raw_coord[8];
140 uint8_t raw_area[4];
141 int i, ret;
142
143
144 ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_X1_LSB,
145 8, raw_coord);
146 if (ret < 0) {
147 dev_err(&client->dev, "failed to read coordinate, %d\n", ret);
148 return ret;
149 }
150
151
152 ret = i2c_smbus_read_i2c_block_data(client, AUO_PIXCIR_REG_TOUCHAREA_X1,
153 4, raw_area);
154 if (ret < 0) {
155 dev_err(&client->dev, "could not read touch area, %d\n", ret);
156 return ret;
157 }
158
159 for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
160 point[i].coord_x =
161 raw_coord[4 * i + 1] << 8 | raw_coord[4 * i];
162 point[i].coord_y =
163 raw_coord[4 * i + 3] << 8 | raw_coord[4 * i + 2];
164
165 if (point[i].coord_x > pdata->x_max ||
166 point[i].coord_y > pdata->y_max) {
167 dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
168 point[i].coord_x, point[i].coord_y);
169 point[i].coord_x = point[i].coord_y = 0;
170 }
171
172
173 point[i].area_major = max(raw_area[2 * i], raw_area[2 * i + 1]);
174 point[i].area_minor = min(raw_area[2 * i], raw_area[2 * i + 1]);
175 point[i].orientation = raw_area[2 * i] > raw_area[2 * i + 1];
176 }
177
178 return 0;
179}
180
181static irqreturn_t auo_pixcir_interrupt(int irq, void *dev_id)
182{
183 struct auo_pixcir_ts *ts = dev_id;
184 const struct auo_pixcir_ts_platdata *pdata = ts->pdata;
185 struct auo_point_t point[AUO_PIXCIR_REPORT_POINTS];
186 int i;
187 int ret;
188 int fingers = 0;
189 int abs = -1;
190
191 while (!ts->stopped) {
192
193
194 if (ts->touch_ind_mode) {
195 if (gpio_get_value(pdata->gpio_int) == 0) {
196 input_mt_sync(ts->input);
197 input_report_key(ts->input, BTN_TOUCH, 0);
198 input_sync(ts->input);
199 break;
200 }
201 }
202
203 ret = auo_pixcir_collect_data(ts, point);
204 if (ret < 0) {
205
206 if (!ts->touch_ind_mode)
207 break;
208
209 wait_event_timeout(ts->wait, ts->stopped,
210 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
211 continue;
212 }
213
214 for (i = 0; i < AUO_PIXCIR_REPORT_POINTS; i++) {
215 if (point[i].coord_x > 0 || point[i].coord_y > 0) {
216 input_report_abs(ts->input, ABS_MT_POSITION_X,
217 point[i].coord_x);
218 input_report_abs(ts->input, ABS_MT_POSITION_Y,
219 point[i].coord_y);
220 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
221 point[i].area_major);
222 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
223 point[i].area_minor);
224 input_report_abs(ts->input, ABS_MT_ORIENTATION,
225 point[i].orientation);
226 input_mt_sync(ts->input);
227
228
229 if (fingers == 0)
230 abs = i;
231
232
233
234
235 fingers++;
236 }
237 }
238
239 input_report_key(ts->input, BTN_TOUCH, fingers > 0);
240
241 if (abs > -1) {
242 input_report_abs(ts->input, ABS_X, point[abs].coord_x);
243 input_report_abs(ts->input, ABS_Y, point[abs].coord_y);
244 }
245
246 input_sync(ts->input);
247
248
249 if (!ts->touch_ind_mode)
250 break;
251
252 wait_event_timeout(ts->wait, ts->stopped,
253 msecs_to_jiffies(AUO_PIXCIR_PENUP_TIMEOUT_MS));
254 }
255
256 return IRQ_HANDLED;
257}
258
259
260
261
262
263
264
265
266static int auo_pixcir_power_mode(struct auo_pixcir_ts *ts, int mode)
267{
268 struct i2c_client *client = ts->client;
269 int ret;
270
271 ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_POWER_MODE);
272 if (ret < 0) {
273 dev_err(&client->dev, "unable to read reg %Xh, %d\n",
274 AUO_PIXCIR_REG_POWER_MODE, ret);
275 return ret;
276 }
277
278 ret &= ~AUO_PIXCIR_POWER_MASK;
279 ret |= mode;
280
281 ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_POWER_MODE, ret);
282 if (ret) {
283 dev_err(&client->dev, "unable to write reg %Xh, %d\n",
284 AUO_PIXCIR_REG_POWER_MODE, ret);
285 return ret;
286 }
287
288 return 0;
289}
290
291static int auo_pixcir_int_config(struct auo_pixcir_ts *ts,
292 int int_setting)
293{
294 struct i2c_client *client = ts->client;
295 const struct auo_pixcir_ts_platdata *pdata = ts->pdata;
296 int ret;
297
298 ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
299 if (ret < 0) {
300 dev_err(&client->dev, "unable to read reg %Xh, %d\n",
301 AUO_PIXCIR_REG_INT_SETTING, ret);
302 return ret;
303 }
304
305 ret &= ~AUO_PIXCIR_INT_MODE_MASK;
306 ret |= int_setting;
307 ret |= AUO_PIXCIR_INT_POL_HIGH;
308
309 ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
310 ret);
311 if (ret < 0) {
312 dev_err(&client->dev, "unable to write reg %Xh, %d\n",
313 AUO_PIXCIR_REG_INT_SETTING, ret);
314 return ret;
315 }
316
317 ts->touch_ind_mode = pdata->int_setting == AUO_PIXCIR_INT_TOUCH_IND;
318
319 return 0;
320}
321
322
323static int auo_pixcir_int_toggle(struct auo_pixcir_ts *ts, bool enable)
324{
325 struct i2c_client *client = ts->client;
326 int ret;
327
328 ret = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_INT_SETTING);
329 if (ret < 0) {
330 dev_err(&client->dev, "unable to read reg %Xh, %d\n",
331 AUO_PIXCIR_REG_INT_SETTING, ret);
332 return ret;
333 }
334
335 if (enable)
336 ret |= AUO_PIXCIR_INT_ENABLE;
337 else
338 ret &= ~AUO_PIXCIR_INT_ENABLE;
339
340 ret = i2c_smbus_write_byte_data(client, AUO_PIXCIR_REG_INT_SETTING,
341 ret);
342 if (ret < 0) {
343 dev_err(&client->dev, "unable to write reg %Xh, %d\n",
344 AUO_PIXCIR_REG_INT_SETTING, ret);
345 return ret;
346 }
347
348 return 0;
349}
350
351static int auo_pixcir_start(struct auo_pixcir_ts *ts)
352{
353 struct i2c_client *client = ts->client;
354 int ret;
355
356 ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_ACTIVE);
357 if (ret < 0) {
358 dev_err(&client->dev, "could not set power mode, %d\n",
359 ret);
360 return ret;
361 }
362
363 ts->stopped = false;
364 mb();
365 enable_irq(client->irq);
366
367 ret = auo_pixcir_int_toggle(ts, 1);
368 if (ret < 0) {
369 dev_err(&client->dev, "could not enable interrupt, %d\n",
370 ret);
371 disable_irq(client->irq);
372 return ret;
373 }
374
375 return 0;
376}
377
378static int auo_pixcir_stop(struct auo_pixcir_ts *ts)
379{
380 struct i2c_client *client = ts->client;
381 int ret;
382
383 ret = auo_pixcir_int_toggle(ts, 0);
384 if (ret < 0) {
385 dev_err(&client->dev, "could not disable interrupt, %d\n",
386 ret);
387 return ret;
388 }
389
390
391 disable_irq(client->irq);
392 ts->stopped = true;
393 mb();
394 wake_up(&ts->wait);
395
396 return auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_DEEP_SLEEP);
397}
398
399static int auo_pixcir_input_open(struct input_dev *dev)
400{
401 struct auo_pixcir_ts *ts = input_get_drvdata(dev);
402
403 return auo_pixcir_start(ts);
404}
405
406static void auo_pixcir_input_close(struct input_dev *dev)
407{
408 struct auo_pixcir_ts *ts = input_get_drvdata(dev);
409
410 auo_pixcir_stop(ts);
411
412 return;
413}
414
415static int __maybe_unused auo_pixcir_suspend(struct device *dev)
416{
417 struct i2c_client *client = to_i2c_client(dev);
418 struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
419 struct input_dev *input = ts->input;
420 int ret = 0;
421
422 mutex_lock(&input->mutex);
423
424
425
426
427 if (device_may_wakeup(&client->dev)) {
428
429 if (!input->users) {
430 ret = auo_pixcir_start(ts);
431 if (ret)
432 goto unlock;
433 }
434
435 enable_irq_wake(client->irq);
436 ret = auo_pixcir_power_mode(ts, AUO_PIXCIR_POWER_SLEEP);
437 } else if (input->users) {
438 ret = auo_pixcir_stop(ts);
439 }
440
441unlock:
442 mutex_unlock(&input->mutex);
443
444 return ret;
445}
446
447static int __maybe_unused auo_pixcir_resume(struct device *dev)
448{
449 struct i2c_client *client = to_i2c_client(dev);
450 struct auo_pixcir_ts *ts = i2c_get_clientdata(client);
451 struct input_dev *input = ts->input;
452 int ret = 0;
453
454 mutex_lock(&input->mutex);
455
456 if (device_may_wakeup(&client->dev)) {
457 disable_irq_wake(client->irq);
458
459
460 if (!input->users) {
461 ret = auo_pixcir_stop(ts);
462 if (ret)
463 goto unlock;
464 }
465
466
467 } else if (input->users) {
468 ret = auo_pixcir_start(ts);
469 }
470
471unlock:
472 mutex_unlock(&input->mutex);
473
474 return ret;
475}
476
477static SIMPLE_DEV_PM_OPS(auo_pixcir_pm_ops,
478 auo_pixcir_suspend, auo_pixcir_resume);
479
480#ifdef CONFIG_OF
481static struct auo_pixcir_ts_platdata *auo_pixcir_parse_dt(struct device *dev)
482{
483 struct auo_pixcir_ts_platdata *pdata;
484 struct device_node *np = dev->of_node;
485
486 if (!np)
487 return ERR_PTR(-ENOENT);
488
489 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
490 if (!pdata) {
491 dev_err(dev, "failed to allocate platform data\n");
492 return ERR_PTR(-ENOMEM);
493 }
494
495 pdata->gpio_int = of_get_gpio(np, 0);
496 if (!gpio_is_valid(pdata->gpio_int)) {
497 dev_err(dev, "failed to get interrupt gpio\n");
498 return ERR_PTR(-EINVAL);
499 }
500
501 pdata->gpio_rst = of_get_gpio(np, 1);
502 if (!gpio_is_valid(pdata->gpio_rst)) {
503 dev_err(dev, "failed to get reset gpio\n");
504 return ERR_PTR(-EINVAL);
505 }
506
507 if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
508 dev_err(dev, "failed to get x-size property\n");
509 return ERR_PTR(-EINVAL);
510 }
511
512 if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
513 dev_err(dev, "failed to get y-size property\n");
514 return ERR_PTR(-EINVAL);
515 }
516
517
518 pdata->int_setting = AUO_PIXCIR_INT_TOUCH_IND;
519
520 return pdata;
521}
522#else
523static struct auo_pixcir_ts_platdata *auo_pixcir_parse_dt(struct device *dev)
524{
525 return ERR_PTR(-EINVAL);
526}
527#endif
528
529static void auo_pixcir_reset(void *data)
530{
531 struct auo_pixcir_ts *ts = data;
532
533 gpio_set_value(ts->pdata->gpio_rst, 0);
534}
535
536static int auo_pixcir_probe(struct i2c_client *client,
537 const struct i2c_device_id *id)
538{
539 const struct auo_pixcir_ts_platdata *pdata;
540 struct auo_pixcir_ts *ts;
541 struct input_dev *input_dev;
542 int version;
543 int error;
544
545 pdata = dev_get_platdata(&client->dev);
546 if (!pdata) {
547 pdata = auo_pixcir_parse_dt(&client->dev);
548 if (IS_ERR(pdata))
549 return PTR_ERR(pdata);
550 }
551
552 ts = devm_kzalloc(&client->dev,
553 sizeof(struct auo_pixcir_ts), GFP_KERNEL);
554 if (!ts)
555 return -ENOMEM;
556
557 input_dev = devm_input_allocate_device(&client->dev);
558 if (!input_dev) {
559 dev_err(&client->dev, "could not allocate input device\n");
560 return -ENOMEM;
561 }
562
563 ts->pdata = pdata;
564 ts->client = client;
565 ts->input = input_dev;
566 ts->touch_ind_mode = 0;
567 ts->stopped = true;
568 init_waitqueue_head(&ts->wait);
569
570 snprintf(ts->phys, sizeof(ts->phys),
571 "%s/input0", dev_name(&client->dev));
572
573 input_dev->name = "AUO-Pixcir touchscreen";
574 input_dev->phys = ts->phys;
575 input_dev->id.bustype = BUS_I2C;
576
577 input_dev->open = auo_pixcir_input_open;
578 input_dev->close = auo_pixcir_input_close;
579
580 __set_bit(EV_ABS, input_dev->evbit);
581 __set_bit(EV_KEY, input_dev->evbit);
582
583 __set_bit(BTN_TOUCH, input_dev->keybit);
584
585
586 input_set_abs_params(input_dev, ABS_X, 0, pdata->x_max, 0, 0);
587 input_set_abs_params(input_dev, ABS_Y, 0, pdata->y_max, 0, 0);
588
589
590 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
591 pdata->x_max, 0, 0);
592 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
593 pdata->y_max, 0, 0);
594 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
595 AUO_PIXCIR_MAX_AREA, 0, 0);
596 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
597 AUO_PIXCIR_MAX_AREA, 0, 0);
598 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
599
600 input_set_drvdata(ts->input, ts);
601
602 error = devm_gpio_request_one(&client->dev, pdata->gpio_int,
603 GPIOF_DIR_IN, "auo_pixcir_ts_int");
604 if (error) {
605 dev_err(&client->dev, "request of gpio %d failed, %d\n",
606 pdata->gpio_int, error);
607 return error;
608 }
609
610 error = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
611 GPIOF_DIR_OUT | GPIOF_INIT_HIGH,
612 "auo_pixcir_ts_rst");
613 if (error) {
614 dev_err(&client->dev, "request of gpio %d failed, %d\n",
615 pdata->gpio_rst, error);
616 return error;
617 }
618
619 error = devm_add_action(&client->dev, auo_pixcir_reset, ts);
620 if (error) {
621 auo_pixcir_reset(ts);
622 dev_err(&client->dev, "failed to register reset action, %d\n",
623 error);
624 return error;
625 }
626
627 msleep(200);
628
629 version = i2c_smbus_read_byte_data(client, AUO_PIXCIR_REG_VERSION);
630 if (version < 0) {
631 error = version;
632 return error;
633 }
634
635 dev_info(&client->dev, "firmware version 0x%X\n", version);
636
637 error = auo_pixcir_int_config(ts, pdata->int_setting);
638 if (error)
639 return error;
640
641 error = devm_request_threaded_irq(&client->dev, client->irq,
642 NULL, auo_pixcir_interrupt,
643 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
644 input_dev->name, ts);
645 if (error) {
646 dev_err(&client->dev, "irq %d requested failed, %d\n",
647 client->irq, error);
648 return error;
649 }
650
651
652 error = auo_pixcir_stop(ts);
653 if (error)
654 return error;
655
656 error = input_register_device(input_dev);
657 if (error) {
658 dev_err(&client->dev, "could not register input device, %d\n",
659 error);
660 return error;
661 }
662
663 i2c_set_clientdata(client, ts);
664
665 return 0;
666}
667
668static const struct i2c_device_id auo_pixcir_idtable[] = {
669 { "auo_pixcir_ts", 0 },
670 { }
671};
672MODULE_DEVICE_TABLE(i2c, auo_pixcir_idtable);
673
674#ifdef CONFIG_OF
675static const struct of_device_id auo_pixcir_ts_dt_idtable[] = {
676 { .compatible = "auo,auo_pixcir_ts" },
677 {},
678};
679MODULE_DEVICE_TABLE(of, auo_pixcir_ts_dt_idtable);
680#endif
681
682static struct i2c_driver auo_pixcir_driver = {
683 .driver = {
684 .name = "auo_pixcir_ts",
685 .pm = &auo_pixcir_pm_ops,
686 .of_match_table = of_match_ptr(auo_pixcir_ts_dt_idtable),
687 },
688 .probe = auo_pixcir_probe,
689 .id_table = auo_pixcir_idtable,
690};
691
692module_i2c_driver(auo_pixcir_driver);
693
694MODULE_DESCRIPTION("AUO-PIXCIR touchscreen driver");
695MODULE_LICENSE("GPL v2");
696MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
697