2 * Copyright (C) ST-Ericsson SA 2010
3 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
4 * License terms:GNU General Public License (GPL) version 2
7 #include <linux/kernel.h>
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
10 #include <linux/i2c.h>
11 #include <linux/workqueue.h>
12 #include <linux/input.h>
13 #include <linux/input/bu21013.h>
14 #include <linux/slab.h>
15 #include <linux/regulator/consumer.h>
17 #define PEN_DOWN_INTR 0
19 #define RESET_DELAY 30
20 #define PENUP_TIMEOUT (10)
22 #define MASK_BITS 0x03
25 #define LENGTH_OF_BUFFER 11
26 #define I2C_RETRY_COUNT 5
28 #define BU21013_SENSORS_BTN_0_7_REG 0x70
29 #define BU21013_SENSORS_BTN_8_15_REG 0x71
30 #define BU21013_SENSORS_BTN_16_23_REG 0x72
31 #define BU21013_X1_POS_MSB_REG 0x73
32 #define BU21013_X1_POS_LSB_REG 0x74
33 #define BU21013_Y1_POS_MSB_REG 0x75
34 #define BU21013_Y1_POS_LSB_REG 0x76
35 #define BU21013_X2_POS_MSB_REG 0x77
36 #define BU21013_X2_POS_LSB_REG 0x78
37 #define BU21013_Y2_POS_MSB_REG 0x79
38 #define BU21013_Y2_POS_LSB_REG 0x7A
39 #define BU21013_INT_CLR_REG 0xE8
40 #define BU21013_INT_MODE_REG 0xE9
41 #define BU21013_GAIN_REG 0xEA
42 #define BU21013_OFFSET_MODE_REG 0xEB
43 #define BU21013_XY_EDGE_REG 0xEC
44 #define BU21013_RESET_REG 0xED
45 #define BU21013_CALIB_REG 0xEE
46 #define BU21013_DONE_REG 0xEF
47 #define BU21013_SENSOR_0_7_REG 0xF0
48 #define BU21013_SENSOR_8_15_REG 0xF1
49 #define BU21013_SENSOR_16_23_REG 0xF2
50 #define BU21013_POS_MODE1_REG 0xF3
51 #define BU21013_POS_MODE2_REG 0xF4
52 #define BU21013_CLK_MODE_REG 0xF5
53 #define BU21013_IDLE_REG 0xFA
54 #define BU21013_FILTER_REG 0xFB
55 #define BU21013_TH_ON_REG 0xFC
56 #define BU21013_TH_OFF_REG 0xFD
59 #define BU21013_RESET_ENABLE 0x01
61 #define BU21013_SENSORS_EN_0_7 0x3F
62 #define BU21013_SENSORS_EN_8_15 0xFC
63 #define BU21013_SENSORS_EN_16_23 0x1F
65 #define BU21013_POS_MODE1_0 0x02
66 #define BU21013_POS_MODE1_1 0x04
67 #define BU21013_POS_MODE1_2 0x08
69 #define BU21013_POS_MODE2_ZERO 0x01
70 #define BU21013_POS_MODE2_AVG1 0x02
71 #define BU21013_POS_MODE2_AVG2 0x04
72 #define BU21013_POS_MODE2_EN_XY 0x08
73 #define BU21013_POS_MODE2_EN_RAW 0x10
74 #define BU21013_POS_MODE2_MULTI 0x80
76 #define BU21013_CLK_MODE_DIV 0x01
77 #define BU21013_CLK_MODE_EXT 0x02
78 #define BU21013_CLK_MODE_CALIB 0x80
80 #define BU21013_IDLET_0 0x01
81 #define BU21013_IDLET_1 0x02
82 #define BU21013_IDLET_2 0x04
83 #define BU21013_IDLET_3 0x08
84 #define BU21013_IDLE_INTERMIT_EN 0x10
86 #define BU21013_DELTA_0_6 0x7F
87 #define BU21013_FILTER_EN 0x80
89 #define BU21013_INT_MODE_LEVEL 0x00
90 #define BU21013_INT_MODE_EDGE 0x01
92 #define BU21013_GAIN_0 0x01
93 #define BU21013_GAIN_1 0x02
94 #define BU21013_GAIN_2 0x04
96 #define BU21013_OFFSET_MODE_DEFAULT 0x00
97 #define BU21013_OFFSET_MODE_MOVE 0x01
98 #define BU21013_OFFSET_MODE_DISABLE 0x02
100 #define BU21013_TH_ON_0 0x01
101 #define BU21013_TH_ON_1 0x02
102 #define BU21013_TH_ON_2 0x04
103 #define BU21013_TH_ON_3 0x08
104 #define BU21013_TH_ON_4 0x10
105 #define BU21013_TH_ON_5 0x20
106 #define BU21013_TH_ON_6 0x40
107 #define BU21013_TH_ON_7 0x80
108 #define BU21013_TH_ON_MAX 0xFF
110 #define BU21013_TH_OFF_0 0x01
111 #define BU21013_TH_OFF_1 0x02
112 #define BU21013_TH_OFF_2 0x04
113 #define BU21013_TH_OFF_3 0x08
114 #define BU21013_TH_OFF_4 0x10
115 #define BU21013_TH_OFF_5 0x20
116 #define BU21013_TH_OFF_6 0x40
117 #define BU21013_TH_OFF_7 0x80
118 #define BU21013_TH_OFF_MAX 0xFF
120 #define BU21013_X_EDGE_0 0x01
121 #define BU21013_X_EDGE_1 0x02
122 #define BU21013_X_EDGE_2 0x04
123 #define BU21013_X_EDGE_3 0x08
124 #define BU21013_Y_EDGE_0 0x10
125 #define BU21013_Y_EDGE_1 0x20
126 #define BU21013_Y_EDGE_2 0x40
127 #define BU21013_Y_EDGE_3 0x80
129 #define BU21013_DONE 0x01
130 #define BU21013_NUMBER_OF_X_SENSORS (6)
131 #define BU21013_NUMBER_OF_Y_SENSORS (11)
133 #define DRIVER_TP "bu21013_tp"
136 * struct bu21013_ts_data - touch panel data structure
137 * @client: pointer to the i2c client
138 * @wait: variable to wait_queue_head_t structure
139 * @touch_stopped: touch stop flag
140 * @chip: pointer to the touch panel controller
141 * @in_dev: pointer to the input device structure
142 * @intr_pin: interrupt pin value
143 * @regulator: pointer to the Regulator used for touch screen
145 * Touch panel device data structure
147 struct bu21013_ts_data
{
148 struct i2c_client
*client
;
149 wait_queue_head_t wait
;
151 const struct bu21013_platform_device
*chip
;
152 struct input_dev
*in_dev
;
153 unsigned int intr_pin
;
154 struct regulator
*regulator
;
158 * bu21013_read_block_data(): read the touch co-ordinates
159 * @data: bu21013_ts_data structure pointer
162 * Read the touch co-ordinates using i2c read block into buffer
163 * and returns integer.
165 static int bu21013_read_block_data(struct bu21013_ts_data
*data
, u8
*buf
)
169 for (i
= 0; i
< I2C_RETRY_COUNT
; i
++) {
170 ret
= i2c_smbus_read_i2c_block_data
171 (data
->client
, BU21013_SENSORS_BTN_0_7_REG
,
172 LENGTH_OF_BUFFER
, buf
);
173 if (ret
== LENGTH_OF_BUFFER
)
180 * bu21013_do_touch_report(): Get the touch co-ordinates
181 * @data: bu21013_ts_data structure pointer
183 * Get the touch co-ordinates from touch sensor registers and writes
184 * into device structure and returns integer.
186 static int bu21013_do_touch_report(struct bu21013_ts_data
*data
)
188 u8 buf
[LENGTH_OF_BUFFER
];
189 unsigned int pos_x
[2], pos_y
[2];
190 bool has_x_sensors
, has_y_sensors
;
191 int finger_down_count
= 0;
197 if (bu21013_read_block_data(data
, buf
) < 0)
200 has_x_sensors
= hweight32(buf
[0] & BU21013_SENSORS_EN_0_7
);
201 has_y_sensors
= hweight32(((buf
[1] & BU21013_SENSORS_EN_8_15
) |
202 ((buf
[2] & BU21013_SENSORS_EN_16_23
) << SHIFT_8
)) >> SHIFT_2
);
203 if (!has_x_sensors
|| !has_y_sensors
)
206 for (i
= 0; i
< MAX_FINGERS
; i
++) {
207 const u8
*p
= &buf
[4 * i
+ 3];
208 unsigned int x
= p
[0] << SHIFT_2
| (p
[1] & MASK_BITS
);
209 unsigned int y
= p
[2] << SHIFT_2
| (p
[3] & MASK_BITS
);
210 if (x
== 0 || y
== 0)
212 pos_x
[finger_down_count
] = x
;
213 pos_y
[finger_down_count
] = y
;
217 if (finger_down_count
) {
218 if (finger_down_count
== 2 &&
219 (abs(pos_x
[0] - pos_x
[1]) < DELTA_MIN
||
220 abs(pos_y
[0] - pos_y
[1]) < DELTA_MIN
)) {
224 for (i
= 0; i
< finger_down_count
; i
++) {
225 if (data
->chip
->x_flip
)
226 pos_x
[i
] = data
->chip
->touch_x_max
- pos_x
[i
];
227 if (data
->chip
->y_flip
)
228 pos_y
[i
] = data
->chip
->touch_y_max
- pos_y
[i
];
230 input_report_abs(data
->in_dev
,
231 ABS_MT_POSITION_X
, pos_x
[i
]);
232 input_report_abs(data
->in_dev
,
233 ABS_MT_POSITION_Y
, pos_y
[i
]);
234 input_mt_sync(data
->in_dev
);
237 input_mt_sync(data
->in_dev
);
239 input_sync(data
->in_dev
);
244 * bu21013_gpio_irq() - gpio thread function for touch interrupt
246 * @device_data: void pointer
248 * This gpio thread function for touch interrupt
249 * and returns irqreturn_t.
251 static irqreturn_t
bu21013_gpio_irq(int irq
, void *device_data
)
253 struct bu21013_ts_data
*data
= device_data
;
254 struct i2c_client
*i2c
= data
->client
;
258 retval
= bu21013_do_touch_report(data
);
260 dev_err(&i2c
->dev
, "bu21013_do_touch_report failed\n");
264 data
->intr_pin
= data
->chip
->irq_read_val();
265 if (data
->intr_pin
== PEN_DOWN_INTR
)
266 wait_event_timeout(data
->wait
, data
->touch_stopped
,
267 msecs_to_jiffies(2));
268 } while (!data
->intr_pin
&& !data
->touch_stopped
);
274 * bu21013_init_chip() - power on sequence for the bu21013 controller
275 * @data: device structure pointer
277 * This function is used to power on
278 * the bu21013 controller and returns integer.
280 static int bu21013_init_chip(struct bu21013_ts_data
*data
)
283 struct i2c_client
*i2c
= data
->client
;
285 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_RESET_REG
,
286 BU21013_RESET_ENABLE
);
288 dev_err(&i2c
->dev
, "BU21013_RESET reg write failed\n");
293 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_0_7_REG
,
294 BU21013_SENSORS_EN_0_7
);
296 dev_err(&i2c
->dev
, "BU21013_SENSOR_0_7 reg write failed\n");
300 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_8_15_REG
,
301 BU21013_SENSORS_EN_8_15
);
303 dev_err(&i2c
->dev
, "BU21013_SENSOR_8_15 reg write failed\n");
307 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_16_23_REG
,
308 BU21013_SENSORS_EN_16_23
);
310 dev_err(&i2c
->dev
, "BU21013_SENSOR_16_23 reg write failed\n");
314 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_POS_MODE1_REG
,
315 (BU21013_POS_MODE1_0
| BU21013_POS_MODE1_1
));
317 dev_err(&i2c
->dev
, "BU21013_POS_MODE1 reg write failed\n");
321 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_POS_MODE2_REG
,
322 (BU21013_POS_MODE2_ZERO
| BU21013_POS_MODE2_AVG1
|
323 BU21013_POS_MODE2_AVG2
| BU21013_POS_MODE2_EN_RAW
|
324 BU21013_POS_MODE2_MULTI
));
326 dev_err(&i2c
->dev
, "BU21013_POS_MODE2 reg write failed\n");
330 if (data
->chip
->ext_clk
)
331 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_CLK_MODE_REG
,
332 (BU21013_CLK_MODE_EXT
| BU21013_CLK_MODE_CALIB
));
334 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_CLK_MODE_REG
,
335 (BU21013_CLK_MODE_DIV
| BU21013_CLK_MODE_CALIB
));
337 dev_err(&i2c
->dev
, "BU21013_CLK_MODE reg write failed\n");
341 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_IDLE_REG
,
342 (BU21013_IDLET_0
| BU21013_IDLE_INTERMIT_EN
));
344 dev_err(&i2c
->dev
, "BU21013_IDLE reg write failed\n");
348 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_INT_MODE_REG
,
349 BU21013_INT_MODE_LEVEL
);
351 dev_err(&i2c
->dev
, "BU21013_INT_MODE reg write failed\n");
355 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_FILTER_REG
,
359 dev_err(&i2c
->dev
, "BU21013_FILTER reg write failed\n");
363 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_TH_ON_REG
,
366 dev_err(&i2c
->dev
, "BU21013_TH_ON reg write failed\n");
370 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_TH_OFF_REG
,
371 BU21013_TH_OFF_4
| BU21013_TH_OFF_3
);
373 dev_err(&i2c
->dev
, "BU21013_TH_OFF reg write failed\n");
377 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_GAIN_REG
,
378 (BU21013_GAIN_0
| BU21013_GAIN_1
));
380 dev_err(&i2c
->dev
, "BU21013_GAIN reg write failed\n");
384 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_OFFSET_MODE_REG
,
385 BU21013_OFFSET_MODE_DEFAULT
);
387 dev_err(&i2c
->dev
, "BU21013_OFFSET_MODE reg write failed\n");
391 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_XY_EDGE_REG
,
392 (BU21013_X_EDGE_0
| BU21013_X_EDGE_2
|
393 BU21013_Y_EDGE_1
| BU21013_Y_EDGE_3
));
395 dev_err(&i2c
->dev
, "BU21013_XY_EDGE reg write failed\n");
399 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_DONE_REG
,
402 dev_err(&i2c
->dev
, "BU21013_REG_DONE reg write failed\n");
410 * bu21013_free_irq() - frees IRQ registered for touchscreen
411 * @bu21013_data: device structure pointer
413 * This function signals interrupt thread to stop processing and
416 static void bu21013_free_irq(struct bu21013_ts_data
*bu21013_data
)
418 bu21013_data
->touch_stopped
= true;
419 wake_up(&bu21013_data
->wait
);
420 free_irq(bu21013_data
->chip
->irq
, bu21013_data
);
424 * bu21013_probe() - initializes the i2c-client touchscreen driver
425 * @client: i2c client structure pointer
426 * @id: i2c device id pointer
428 * This function used to initializes the i2c-client touchscreen
429 * driver and returns integer.
431 static int __devinit
bu21013_probe(struct i2c_client
*client
,
432 const struct i2c_device_id
*id
)
434 struct bu21013_ts_data
*bu21013_data
;
435 struct input_dev
*in_dev
;
436 const struct bu21013_platform_device
*pdata
=
437 client
->dev
.platform_data
;
440 if (!i2c_check_functionality(client
->adapter
,
441 I2C_FUNC_SMBUS_BYTE_DATA
)) {
442 dev_err(&client
->dev
, "i2c smbus byte data not supported\n");
447 dev_err(&client
->dev
, "platform data not defined\n");
451 bu21013_data
= kzalloc(sizeof(struct bu21013_ts_data
), GFP_KERNEL
);
452 in_dev
= input_allocate_device();
453 if (!bu21013_data
|| !in_dev
) {
454 dev_err(&client
->dev
, "device memory alloc failed\n");
459 bu21013_data
->in_dev
= in_dev
;
460 bu21013_data
->chip
= pdata
;
461 bu21013_data
->client
= client
;
463 bu21013_data
->regulator
= regulator_get(&client
->dev
, "V-TOUCH");
464 if (IS_ERR(bu21013_data
->regulator
)) {
465 dev_err(&client
->dev
, "regulator_get failed\n");
466 error
= PTR_ERR(bu21013_data
->regulator
);
470 error
= regulator_enable(bu21013_data
->regulator
);
472 dev_err(&client
->dev
, "regulator enable failed\n");
473 goto err_put_regulator
;
476 bu21013_data
->touch_stopped
= false;
477 init_waitqueue_head(&bu21013_data
->wait
);
479 /* configure the gpio pins */
481 error
= pdata
->cs_en(pdata
->cs_pin
);
483 dev_err(&client
->dev
, "chip init failed\n");
484 goto err_disable_regulator
;
488 /* configure the touch panel controller */
489 error
= bu21013_init_chip(bu21013_data
);
491 dev_err(&client
->dev
, "error in bu21013 config\n");
495 /* register the device to input subsystem */
496 in_dev
->name
= DRIVER_TP
;
497 in_dev
->id
.bustype
= BUS_I2C
;
498 in_dev
->dev
.parent
= &client
->dev
;
500 __set_bit(EV_SYN
, in_dev
->evbit
);
501 __set_bit(EV_KEY
, in_dev
->evbit
);
502 __set_bit(EV_ABS
, in_dev
->evbit
);
504 input_set_abs_params(in_dev
, ABS_MT_POSITION_X
, 0,
505 pdata
->touch_x_max
, 0, 0);
506 input_set_abs_params(in_dev
, ABS_MT_POSITION_Y
, 0,
507 pdata
->touch_y_max
, 0, 0);
508 input_set_drvdata(in_dev
, bu21013_data
);
510 error
= request_threaded_irq(pdata
->irq
, NULL
, bu21013_gpio_irq
,
511 IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
512 DRIVER_TP
, bu21013_data
);
514 dev_err(&client
->dev
, "request irq %d failed\n", pdata
->irq
);
518 error
= input_register_device(in_dev
);
520 dev_err(&client
->dev
, "failed to register input device\n");
524 device_init_wakeup(&client
->dev
, pdata
->wakeup
);
525 i2c_set_clientdata(client
, bu21013_data
);
530 bu21013_free_irq(bu21013_data
);
532 pdata
->cs_dis(pdata
->cs_pin
);
533 err_disable_regulator
:
534 regulator_disable(bu21013_data
->regulator
);
536 regulator_put(bu21013_data
->regulator
);
538 input_free_device(in_dev
);
544 * bu21013_remove() - removes the i2c-client touchscreen driver
545 * @client: i2c client structure pointer
547 * This function uses to remove the i2c-client
548 * touchscreen driver and returns integer.
550 static int __devexit
bu21013_remove(struct i2c_client
*client
)
552 struct bu21013_ts_data
*bu21013_data
= i2c_get_clientdata(client
);
554 bu21013_free_irq(bu21013_data
);
556 bu21013_data
->chip
->cs_dis(bu21013_data
->chip
->cs_pin
);
558 input_unregister_device(bu21013_data
->in_dev
);
560 regulator_disable(bu21013_data
->regulator
);
561 regulator_put(bu21013_data
->regulator
);
565 device_init_wakeup(&client
->dev
, false);
572 * bu21013_suspend() - suspend the touch screen controller
573 * @dev: pointer to device structure
575 * This function is used to suspend the
576 * touch panel controller and returns integer
578 static int bu21013_suspend(struct device
*dev
)
580 struct bu21013_ts_data
*bu21013_data
= dev_get_drvdata(dev
);
581 struct i2c_client
*client
= bu21013_data
->client
;
583 bu21013_data
->touch_stopped
= true;
584 if (device_may_wakeup(&client
->dev
))
585 enable_irq_wake(bu21013_data
->chip
->irq
);
587 disable_irq(bu21013_data
->chip
->irq
);
589 regulator_disable(bu21013_data
->regulator
);
595 * bu21013_resume() - resume the touch screen controller
596 * @dev: pointer to device structure
598 * This function is used to resume the touch panel
599 * controller and returns integer.
601 static int bu21013_resume(struct device
*dev
)
603 struct bu21013_ts_data
*bu21013_data
= dev_get_drvdata(dev
);
604 struct i2c_client
*client
= bu21013_data
->client
;
607 retval
= regulator_enable(bu21013_data
->regulator
);
609 dev_err(&client
->dev
, "bu21013 regulator enable failed\n");
613 retval
= bu21013_init_chip(bu21013_data
);
615 dev_err(&client
->dev
, "bu21013 controller config failed\n");
619 bu21013_data
->touch_stopped
= false;
621 if (device_may_wakeup(&client
->dev
))
622 disable_irq_wake(bu21013_data
->chip
->irq
);
624 enable_irq(bu21013_data
->chip
->irq
);
629 static const struct dev_pm_ops bu21013_dev_pm_ops
= {
630 .suspend
= bu21013_suspend
,
631 .resume
= bu21013_resume
,
635 static const struct i2c_device_id bu21013_id
[] = {
639 MODULE_DEVICE_TABLE(i2c
, bu21013_id
);
641 static struct i2c_driver bu21013_driver
= {
644 .owner
= THIS_MODULE
,
646 .pm
= &bu21013_dev_pm_ops
,
649 .probe
= bu21013_probe
,
650 .remove
= __devexit_p(bu21013_remove
),
651 .id_table
= bu21013_id
,
655 * bu21013_init() - initializes the bu21013 touchscreen driver
657 * This function used to initializes the bu21013
658 * touchscreen driver and returns integer.
660 static int __init
bu21013_init(void)
662 return i2c_add_driver(&bu21013_driver
);
666 * bu21013_exit() - de-initializes the bu21013 touchscreen driver
668 * This function uses to de-initializes the bu21013
669 * touchscreen driver and returns none.
671 static void __exit
bu21013_exit(void)
673 i2c_del_driver(&bu21013_driver
);
676 module_init(bu21013_init
);
677 module_exit(bu21013_exit
);
679 MODULE_LICENSE("GPL v2");
680 MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
681 MODULE_DESCRIPTION("bu21013 touch screen controller driver");