1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
/*
* Copyright (C) ST-Ericsson SA 2010
* Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
* License terms:GNU General Public License (GPL) version 2
*/
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/workqueue.h>
#include <linux/input.h>
#include <linux/input/bu21013.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#define PEN_DOWN_INTR 0
#define MAX_FINGERS 2
#define RESET_DELAY 30
#define PENUP_TIMEOUT (10)
#define DELTA_MIN 16
#define MASK_BITS 0x03
#define SHIFT_8 8
#define SHIFT_2 2
#define LENGTH_OF_BUFFER 11
#define I2C_RETRY_COUNT 5
#define BU21013_SENSORS_BTN_0_7_REG 0x70
#define BU21013_SENSORS_BTN_8_15_REG 0x71
#define BU21013_SENSORS_BTN_16_23_REG 0x72
#define BU21013_X1_POS_MSB_REG 0x73
#define BU21013_X1_POS_LSB_REG 0x74
#define BU21013_Y1_POS_MSB_REG 0x75
#define BU21013_Y1_POS_LSB_REG 0x76
#define BU21013_X2_POS_MSB_REG 0x77
#define BU21013_X2_POS_LSB_REG 0x78
#define BU21013_Y2_POS_MSB_REG 0x79
#define BU21013_Y2_POS_LSB_REG 0x7A
#define BU21013_INT_CLR_REG 0xE8
#define BU21013_INT_MODE_REG 0xE9
#define BU21013_GAIN_REG 0xEA
#define BU21013_OFFSET_MODE_REG 0xEB
#define BU21013_XY_EDGE_REG 0xEC
#define BU21013_RESET_REG 0xED
#define BU21013_CALIB_REG 0xEE
#define BU21013_DONE_REG 0xEF
#define BU21013_SENSOR_0_7_REG 0xF0
#define BU21013_SENSOR_8_15_REG 0xF1
#define BU21013_SENSOR_16_23_REG 0xF2
#define BU21013_POS_MODE1_REG 0xF3
#define BU21013_POS_MODE2_REG 0xF4
#define BU21013_CLK_MODE_REG 0xF5
#define BU21013_IDLE_REG 0xFA
#define BU21013_FILTER_REG 0xFB
#define BU21013_TH_ON_REG 0xFC
#define BU21013_TH_OFF_REG 0xFD
#define BU21013_RESET_ENABLE 0x01
#define BU21013_SENSORS_EN_0_7 0x3F
#define BU21013_SENSORS_EN_8_15 0xFC
#define BU21013_SENSORS_EN_16_23 0x1F
#define BU21013_POS_MODE1_0 0x02
#define BU21013_POS_MODE1_1 0x04
#define BU21013_POS_MODE1_2 0x08
#define BU21013_POS_MODE2_ZERO 0x01
#define BU21013_POS_MODE2_AVG1 0x02
#define BU21013_POS_MODE2_AVG2 0x04
#define BU21013_POS_MODE2_EN_XY 0x08
#define BU21013_POS_MODE2_EN_RAW 0x10
#define BU21013_POS_MODE2_MULTI 0x80
#define BU21013_CLK_MODE_DIV 0x01
#define BU21013_CLK_MODE_EXT 0x02
#define BU21013_CLK_MODE_CALIB 0x80
#define BU21013_IDLET_0 0x01
#define BU21013_IDLET_1 0x02
#define BU21013_IDLET_2 0x04
#define BU21013_IDLET_3 0x08
#define BU21013_IDLE_INTERMIT_EN 0x10
#define BU21013_DELTA_0_6 0x7F
#define BU21013_FILTER_EN 0x80
#define BU21013_INT_MODE_LEVEL 0x00
#define BU21013_INT_MODE_EDGE 0x01
#define BU21013_GAIN_0 0x01
#define BU21013_GAIN_1 0x02
#define BU21013_GAIN_2 0x04
#define BU21013_OFFSET_MODE_DEFAULT 0x00
#define BU21013_OFFSET_MODE_MOVE 0x01
#define BU21013_OFFSET_MODE_DISABLE 0x02
#define BU21013_TH_ON_0 0x01
#define BU21013_TH_ON_1 0x02
#define BU21013_TH_ON_2 0x04
#define BU21013_TH_ON_3 0x08
#define BU21013_TH_ON_4 0x10
#define BU21013_TH_ON_5 0x20
#define BU21013_TH_ON_6 0x40
#define BU21013_TH_ON_7 0x80
#define BU21013_TH_ON_MAX 0xFF
#define BU21013_TH_OFF_0 0x01
#define BU21013_TH_OFF_1 0x02
#define BU21013_TH_OFF_2 0x04
#define BU21013_TH_OFF_3 0x08
#define BU21013_TH_OFF_4 0x10
#define BU21013_TH_OFF_5 0x20
#define BU21013_TH_OFF_6 0x40
#define BU21013_TH_OFF_7 0x80
#define BU21013_TH_OFF_MAX 0xFF
#define BU21013_X_EDGE_0 0x01
#define BU21013_X_EDGE_1 0x02
#define BU21013_X_EDGE_2 0x04
#define BU21013_X_EDGE_3 0x08
#define BU21013_Y_EDGE_0 0x10
#define BU21013_Y_EDGE_1 0x20
#define BU21013_Y_EDGE_2 0x40
#define BU21013_Y_EDGE_3 0x80
#define BU21013_DONE 0x01
#define BU21013_NUMBER_OF_X_SENSORS (6)
#define BU21013_NUMBER_OF_Y_SENSORS (11)
#define DRIVER_TP "bu21013_tp"
/**
* struct bu21013_ts_data - touch panel data structure
* @client: pointer to the i2c client
* @wait: variable to wait_queue_head_t structure
* @touch_stopped: touch stop flag
* @chip: pointer to the touch panel controller
* @in_dev: pointer to the input device structure
* @intr_pin: interrupt pin value
* @regulator: pointer to the Regulator used for touch screen
*
* Touch panel device data structure
*/
struct bu21013_ts_data {
struct i2c_client *client;
wait_queue_head_t wait;
const struct bu21013_platform_device *chip;
struct input_dev *in_dev;
struct regulator *regulator;
unsigned int irq;
unsigned int intr_pin;
bool touch_stopped;
};
/**
* bu21013_read_block_data(): read the touch co-ordinates
* @data: bu21013_ts_data structure pointer
* @buf: byte pointer
*
* Read the touch co-ordinates using i2c read block into buffer
* and returns integer.
*/
static int bu21013_read_block_data(struct bu21013_ts_data *data, u8 *buf)
{
int ret, i;
for (i = 0; i < I2C_RETRY_COUNT; i++) {
ret = i2c_smbus_read_i2c_block_data
(data->client, BU21013_SENSORS_BTN_0_7_REG,
LENGTH_OF_BUFFER, buf);
if (ret == LENGTH_OF_BUFFER)
return 0;
}
return -EINVAL;
}
/**
* bu21013_do_touch_report(): Get the touch co-ordinates
* @data: bu21013_ts_data structure pointer
*
* Get the touch co-ordinates from touch sensor registers and writes
* into device structure and returns integer.
*/
static int bu21013_do_touch_report(struct bu21013_ts_data *data)
{
u8 buf[LENGTH_OF_BUFFER];
unsigned int pos_x[2], pos_y[2];
bool has_x_sensors, has_y_sensors;
int finger_down_count = 0;
int i;
if (data == NULL)
return -EINVAL;
if (bu21013_read_block_data(data, buf) < 0)
return -EINVAL;
has_x_sensors = hweight32(buf[0] & BU21013_SENSORS_EN_0_7);
has_y_sensors = hweight32(((buf[1] & BU21013_SENSORS_EN_8_15) |
((buf[2] & BU21013_SENSORS_EN_16_23) << SHIFT_8)) >> SHIFT_2);
if (!has_x_sensors || !has_y_sensors)
return 0;
for (i = 0; i < MAX_FINGERS; i++) {
const u8 *p = &buf[4 * i + 3];
unsigned int x = p[0] << SHIFT_2 | (p[1] & MASK_BITS);
unsigned int y = p[2] << SHIFT_2 | (p[3] & MASK_BITS);
if (x == 0 || y == 0)
continue;
pos_x[finger_down_count] = x;
pos_y[finger_down_count] = y;
finger_down_count++;
}
if (finger_down_count) {
if (finger_down_count == 2 &&
(abs(pos_x[0] - pos_x[1]) < DELTA_MIN ||
abs(pos_y[0] - pos_y[1]) < DELTA_MIN)) {
return 0;
}
for (i = 0; i < finger_down_count; i++) {
if (data->chip->x_flip)
pos_x[i] = data->chip->touch_x_max - pos_x[i];
if (data->chip->y_flip)
pos_y[i] = data->chip->touch_y_max - pos_y[i];
input_report_abs(data->in_dev,
ABS_MT_POSITION_X, pos_x[i]);
input_report_abs(data->in_dev,
ABS_MT_POSITION_Y, pos_y[i]);
input_mt_sync(data->in_dev);
}
} else
input_mt_sync(data->in_dev);
input_sync(data->in_dev);
return 0;
}
/**
* bu21013_gpio_irq() - gpio thread function for touch interrupt
* @irq: irq value
* @device_data: void pointer
*
* This gpio thread function for touch interrupt
* and returns irqreturn_t.
*/
static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
{
struct bu21013_ts_data *data = device_data;
struct i2c_client *i2c = data->client;
int retval;
do {
retval = bu21013_do_touch_report(data);
if (retval < 0) {
dev_err(&i2c->dev, "bu21013_do_touch_report failed\n");
return IRQ_NONE;
}
data->intr_pin = gpio_get_value(data->chip->touch_pin);
if (data->intr_pin == PEN_DOWN_INTR)
wait_event_timeout(data->wait, data->touch_stopped,
msecs_to_jiffies(2));
} while (!data->intr_pin && !data->touch_stopped);
return IRQ_HANDLED;
}
/**
* bu21013_init_chip() - power on sequence for the bu21013 controller
* @data: device structur
|