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>
16 #include <linux/module.h>
18 #define PEN_DOWN_INTR 0
20 #define RESET_DELAY 30
21 #define PENUP_TIMEOUT (10)
23 #define MASK_BITS 0x03
26 #define LENGTH_OF_BUFFER 11
27 #define I2C_RETRY_COUNT 5
29 #define BU21013_SENSORS_BTN_0_7_REG 0x70
30 #define BU21013_SENSORS_BTN_8_15_REG 0x71
31 #define BU21013_SENSORS_BTN_16_23_REG 0x72
32 #define BU21013_X1_POS_MSB_REG 0x73
33 #define BU21013_X1_POS_LSB_REG 0x74
34 #define BU21013_Y1_POS_MSB_REG 0x75
35 #define BU21013_Y1_POS_LSB_REG 0x76
36 #define BU21013_X2_POS_MSB_REG 0x77
37 #define BU21013_X2_POS_LSB_REG 0x78
38 #define BU21013_Y2_POS_MSB_REG 0x79
39 #define BU21013_Y2_POS_LSB_REG 0x7A
40 #define BU21013_INT_CLR_REG 0xE8
41 #define BU21013_INT_MODE_REG 0xE9
42 #define BU21013_GAIN_REG 0xEA
43 #define BU21013_OFFSET_MODE_REG 0xEB
44 #define BU21013_XY_EDGE_REG 0xEC
45 #define BU21013_RESET_REG 0xED
46 #define BU21013_CALIB_REG 0xEE
47 #define BU21013_DONE_REG 0xEF
48 #define BU21013_SENSOR_0_7_REG 0xF0
49 #define BU21013_SENSOR_8_15_REG 0xF1
50 #define BU21013_SENSOR_16_23_REG 0xF2
51 #define BU21013_POS_MODE1_REG 0xF3
52 #define BU21013_POS_MODE2_REG 0xF4
53 #define BU21013_CLK_MODE_REG 0xF5
54 #define BU21013_IDLE_REG 0xFA
55 #define BU21013_FILTER_REG 0xFB
56 #define BU21013_TH_ON_REG 0xFC
57 #define BU21013_TH_OFF_REG 0xFD
60 #define BU21013_RESET_ENABLE 0x01
62 #define BU21013_SENSORS_EN_0_7 0x3F
63 #define BU21013_SENSORS_EN_8_15 0xFC
64 #define BU21013_SENSORS_EN_16_23 0x1F
66 #define BU21013_POS_MODE1_0 0x02
67 #define BU21013_POS_MODE1_1 0x04
68 #define BU21013_POS_MODE1_2 0x08
70 #define BU21013_POS_MODE2_ZERO 0x01
71 #define BU21013_POS_MODE2_AVG1 0x02
72 #define BU21013_POS_MODE2_AVG2 0x04
73 #define BU21013_POS_MODE2_EN_XY 0x08
74 #define BU21013_POS_MODE2_EN_RAW 0x10
75 #define BU21013_POS_MODE2_MULTI 0x80
77 #define BU21013_CLK_MODE_DIV 0x01
78 #define BU21013_CLK_MODE_EXT 0x02
79 #define BU21013_CLK_MODE_CALIB 0x80
81 #define BU21013_IDLET_0 0x01
82 #define BU21013_IDLET_1 0x02
83 #define BU21013_IDLET_2 0x04
84 #define BU21013_IDLET_3 0x08
85 #define BU21013_IDLE_INTERMIT_EN 0x10
87 #define BU21013_DELTA_0_6 0x7F
88 #define BU21013_FILTER_EN 0x80
90 #define BU21013_INT_MODE_LEVEL 0x00
91 #define BU21013_INT_MODE_EDGE 0x01
93 #define BU21013_GAIN_0 0x01
94 #define BU21013_GAIN_1 0x02
95 #define BU21013_GAIN_2 0x04
97 #define BU21013_OFFSET_MODE_DEFAULT 0x00
98 #define BU21013_OFFSET_MODE_MOVE 0x01
99 #define BU21013_OFFSET_MODE_DISABLE 0x02
101 #define BU21013_TH_ON_0 0x01
102 #define BU21013_TH_ON_1 0x02
103 #define BU21013_TH_ON_2 0x04
104 #define BU21013_TH_ON_3 0x08
105 #define BU21013_TH_ON_4 0x10
106 #define BU21013_TH_ON_5 0x20
107 #define BU21013_TH_ON_6 0x40
108 #define BU21013_TH_ON_7 0x80
109 #define BU21013_TH_ON_MAX 0xFF
111 #define BU21013_TH_OFF_0 0x01
112 #define BU21013_TH_OFF_1 0x02
113 #define BU21013_TH_OFF_2 0x04
114 #define BU21013_TH_OFF_3 0x08
115 #define BU21013_TH_OFF_4 0x10
116 #define BU21013_TH_OFF_5 0x20
117 #define BU21013_TH_OFF_6 0x40
118 #define BU21013_TH_OFF_7 0x80
119 #define BU21013_TH_OFF_MAX 0xFF
121 #define BU21013_X_EDGE_0 0x01
122 #define BU21013_X_EDGE_1 0x02
123 #define BU21013_X_EDGE_2 0x04
124 #define BU21013_X_EDGE_3 0x08
125 #define BU21013_Y_EDGE_0 0x10
126 #define BU21013_Y_EDGE_1 0x20
127 #define BU21013_Y_EDGE_2 0x40
128 #define BU21013_Y_EDGE_3 0x80
130 #define BU21013_DONE 0x01
131 #define BU21013_NUMBER_OF_X_SENSORS (6)
132 #define BU21013_NUMBER_OF_Y_SENSORS (11)
134 #define DRIVER_TP "bu21013_tp"
137 * struct bu21013_ts_data - touch panel data structure
138 * @client: pointer to the i2c client
139 * @wait: variable to wait_queue_head_t structure
140 * @touch_stopped: touch stop flag
141 * @chip: pointer to the touch panel controller
142 * @in_dev: pointer to the input device structure
143 * @intr_pin: interrupt pin value
144 * @regulator: pointer to the Regulator used for touch screen
146 * Touch panel device data structure
148 struct bu21013_ts_data
{
149 struct i2c_client
*client
;
150 wait_queue_head_t wait
;
152 const struct bu21013_platform_device
*chip
;
153 struct input_dev
*in_dev
;
154 unsigned int intr_pin
;
155 struct regulator
*regulator
;
159 * bu21013_read_block_data(): read the touch co-ordinates
160 * @data: bu21013_ts_data structure pointer
163 * Read the touch co-ordinates using i2c read block into buffer
164 * and returns integer.
166 static int bu21013_read_block_data(struct bu21013_ts_data
*data
, u8
*buf
)
170 for (i
= 0; i
< I2C_RETRY_COUNT
; i
++) {
171 ret
= i2c_smbus_read_i2c_block_data
172 (data
->client
, BU21013_SENSORS_BTN_0_7_REG
,
173 LENGTH_OF_BUFFER
, buf
);
174 if (ret
== LENGTH_OF_BUFFER
)
181 * bu21013_do_touch_report(): Get the touch co-ordinates
182 * @data: bu21013_ts_data structure pointer
184 * Get the touch co-ordinates from touch sensor registers and writes
185 * into device structure and returns integer.
187 static int bu21013_do_touch_report(struct bu21013_ts_data
*data
)
189 u8 buf
[LENGTH_OF_BUFFER
];
190 unsigned int pos_x
[2], pos_y
[2];
191 bool has_x_sensors
, has_y_sensors
;
192 int finger_down_count
= 0;
198 if (bu21013_read_block_data(data
, buf
) < 0)
201 has_x_sensors
= hweight32(buf
[0] & BU21013_SENSORS_EN_0_7
);
202 has_y_sensors
= hweight32(((buf
[1] & BU21013_SENSORS_EN_8_15
) |
203 ((buf
[2] & BU21013_SENSORS_EN_16_23
) << SHIFT_8
)) >> SHIFT_2
);
204 if (!has_x_sensors
|| !has_y_sensors
)
207 for (i
= 0; i
< MAX_FINGERS
; i
++) {
208 const u8
*p
= &buf
[4 * i
+ 3];
209 unsigned int x
= p
[0] << SHIFT_2
| (p
[1] & MASK_BITS
);
210 unsigned int y
= p
[2] << SHIFT_2
| (p
[3] & MASK_BITS
);
211 if (x
== 0 || y
== 0)
213 pos_x
[finger_down_count
] = x
;
214 pos_y
[finger_down_count
] = y
;
218 if (finger_down_count
) {
219 if (finger_down_count
== 2 &&
220 (abs(pos_x
[0] - pos_x
[1]) < DELTA_MIN
||
221 abs(pos_y
[0] - pos_y
[1]) < DELTA_MIN
)) {
225 for (i
= 0; i
< finger_down_count
; i
++) {
226 if (data
->chip
->x_flip
)
227 pos_x
[i
] = data
->chip
->touch_x_max
- pos_x
[i
];
228 if (data
->chip
->y_flip
)
229 pos_y
[i
] = data
->chip
->touch_y_max
- pos_y
[i
];
231 input_report_abs(data
->in_dev
,
232 ABS_MT_POSITION_X
, pos_x
[i
]);
233 input_report_abs(data
->in_dev
,
234 ABS_MT_POSITION_Y
, pos_y
[i
]);
235 input_mt_sync(data
->in_dev
);
238 input_mt_sync(data
->in_dev
);
240 input_sync(data
->in_dev
);
245 * bu21013_gpio_irq() - gpio thread function for touch interrupt
247 * @device_data: void pointer
249 * This gpio thread function for touch interrupt
250 * and returns irqreturn_t.
252 static irqreturn_t
bu21013_gpio_irq(int irq
, void *device_data
)
254 struct bu21013_ts_data
*data
= device_data
;
255 struct i2c_client
*i2c
= data
->client
;
259 retval
= bu21013_do_touch_report(data
);
261 dev_err(&i2c
->dev
, "bu21013_do_touch_report failed\n");
265 data
->intr_pin
= data
->chip
->irq_read_val();
266 if (data
->intr_pin
== PEN_DOWN_INTR
)
267 wait_event_timeout(data
->wait
, data
->touch_stopped
,
268 msecs_to_jiffies(2));
269 } while (!data
->intr_pin
&& !data
->touch_stopped
);
275 * bu21013_init_chip() - power on sequence for the bu21013 controller
276 * @data: device structure pointer
278 * This function is used to power on
279 * the bu21013 controller and returns integer.
281 static int bu21013_init_chip(struct bu21013_ts_data
*data
)
284 struct i2c_client
*i2c
= data
->client
;
286 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_RESET_REG
,
287 BU21013_RESET_ENABLE
);
289 dev_err(&i2c
->dev
, "BU21013_RESET reg write failed\n");
294 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_0_7_REG
,
295 BU21013_SENSORS_EN_0_7
);
297 dev_err(&i2c
->dev
, "BU21013_SENSOR_0_7 reg write failed\n");
301 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_8_15_REG
,
302 BU21013_SENSORS_EN_8_15
);
304 dev_err(&i2c
->dev
, "BU21013_SENSOR_8_15 reg write failed\n");
308 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_16_23_REG
,
309 BU21013_SENSORS_EN_16_23
);
311 dev_err(&i2c
->dev
, "BU21013_SENSOR_16_23 reg write failed\n");
315 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_POS_MODE1_REG
,
316 (BU21013_POS_MODE1_0
| BU21013_POS_MODE1_1
));
318 dev_err(&i2c
->dev
, "BU21013_POS_MODE1 reg write failed\n");
322 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_POS_MODE2_REG
,
323 (BU21013_POS_MODE2_ZERO
| BU21013_POS_MODE2_AVG1
|
324 BU21013_POS_MODE2_AVG2
| BU21013_POS_MODE2_EN_RAW
|
325 BU21013_POS_MODE2_MULTI
));
327 dev_err(&i2c
->dev
, "BU21013_POS_MODE2 reg write failed\n");
331 if (data
->chip
->ext_clk
)
332 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_CLK_MODE_REG
,
333 (BU21013_CLK_MODE_EXT
| BU21013_CLK_MODE_CALIB
));
335 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_CLK_MODE_REG
,
336 (BU21013_CLK_MODE_DIV
| BU21013_CLK_MODE_CALIB
));
338 dev_err(&i2c
->dev
, "BU21013_CLK_MODE reg write failed\n");
342 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_IDLE_REG
,
343 (BU21013_IDLET_0
| BU21013_IDLE_INTERMIT_EN
));
345 dev_err(&i2c
->dev
, "BU21013_IDLE reg write failed\n");
349 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_INT_MODE_REG
,
350 BU21013_INT_MODE_LEVEL
);
352 dev_err(&i2c
->dev
, "BU21013_INT_MODE reg write failed\n");
356 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_FILTER_REG
,
360 dev_err(&i2c
->dev
, "BU21013_FILTER reg write failed\n");
364 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_TH_ON_REG
,
367 dev_err(&i2c
->dev
, "BU21013_TH_ON reg write failed\n");
371 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_TH_OFF_REG
,
372 BU21013_TH_OFF_4
| BU21013_TH_OFF_3
);
374 dev_err(&i2c
->dev
, "BU21013_TH_OFF reg write failed\n");
378 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_GAIN_REG
,
379 (BU21013_GAIN_0
| BU21013_GAIN_1
));
381 dev_err(&i2c
->dev
, "BU21013_GAIN reg write failed\n");
385 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_OFFSET_MODE_REG
,
386 BU21013_OFFSET_MODE_DEFAULT
);
388 dev_err(&i2c
->dev
, "BU21013_OFFSET_MODE reg write failed\n");
392 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_XY_EDGE_REG
,
393 (BU21013_X_EDGE_0
| BU21013_X_EDGE_2
|
394 BU21013_Y_EDGE_1
| BU21013_Y_EDGE_3
));
396 dev_err(&i2c
->dev
, "BU21013_XY_EDGE reg write failed\n");
400 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_DONE_REG
,
403 dev_err(&i2c
->dev
, "BU21013_REG_DONE reg write failed\n");
411 * bu21013_free_irq() - frees IRQ registered for touchscreen
412 * @bu21013_data: device structure pointer
414 * This function signals interrupt thread to stop processing and
417 static void bu21013_free_irq(struct bu21013_ts_data
*bu21013_data
)
419 bu21013_data
->touch_stopped
= true;
420 wake_up(&bu21013_data
->wait
);
421 free_irq(bu21013_data
->chip
->irq
, bu21013_data
);
425 * bu21013_probe() - initializes the i2c-client touchscreen driver
426 * @client: i2c client structure pointer
427 * @id: i2c device id pointer
429 * This function used to initializes the i2c-client touchscreen
430 * driver and returns integer.
432 static int __devinit
bu21013_probe(struct i2c_client
*client
,
433 const struct i2c_device_id
*id
)
435 struct bu21013_ts_data
*bu21013_data
;
436 struct input_dev
*in_dev
;
437 const struct bu21013_platform_device
*pdata
=
438 client
->dev
.platform_data
;
441 if (!i2c_check_functionality(client
->adapter
,
442 I2C_FUNC_SMBUS_BYTE_DATA
)) {
443 dev_err(&client
->dev
, "i2c smbus byte data not supported\n");
448 dev_err(&client
->dev
, "platform data not defined\n");
452 bu21013_data
= kzalloc(sizeof(struct bu21013_ts_data
), GFP_KERNEL
);
453 in_dev
= input_allocate_device();
454 if (!bu21013_data
|| !in_dev
) {
455 dev_err(&client
->dev
, "device memory alloc failed\n");
460 bu21013_data
->in_dev
= in_dev
;
461 bu21013_data
->chip
= pdata
;
462 bu21013_data
->client
= client
;
464 bu21013_data
->regulator
= regulator_get(&client
->dev
, "V-TOUCH");
465 if (IS_ERR(bu21013_data
->regulator
)) {
466 dev_err(&client
->dev
, "regulator_get failed\n");
467 error
= PTR_ERR(bu21013_data
->regulator
);
471 error
= regulator_enable(bu21013_data
->regulator
);
473 dev_err(&client
->dev
, "regulator enable failed\n");
474 goto err_put_regulator
;
477 bu21013_data
->touch_stopped
= false;
478 init_waitqueue_head(&bu21013_data
->wait
);
480 /* configure the gpio pins */
482 error
= pdata
->cs_en(pdata
->cs_pin
);
484 dev_err(&client
->dev
, "chip init failed\n");
485 goto err_disable_regulator
;
489 /* configure the touch panel controller */
490 error
= bu21013_init_chip(bu21013_data
);
492 dev_err(&client
->dev
, "error in bu21013 config\n");
496 /* register the device to input subsystem */
497 in_dev
->name
= DRIVER_TP
;
498 in_dev
->id
.bustype
= BUS_I2C
;
499 in_dev
->dev
.parent
= &client
->dev
;
501 __set_bit(EV_SYN
, in_dev
->evbit
);
502 __set_bit(EV_KEY
, in_dev
->evbit
);
503 __set_bit(EV_ABS
, in_dev
->evbit
);
505 input_set_abs_params(in_dev
, ABS_MT_POSITION_X
, 0,
506 pdata
->touch_x_max
, 0, 0);
507 input_set_abs_params(in_dev
, ABS_MT_POSITION_Y
, 0,
508 pdata
->touch_y_max
, 0, 0);
509 input_set_drvdata(in_dev
, bu21013_data
);
511 error
= request_threaded_irq(pdata
->irq
, NULL
, bu21013_gpio_irq
,
512 IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
513 DRIVER_TP
, bu21013_data
);
515 dev_err(&client
->dev
, "request irq %d failed\n", pdata
->irq
);
519 error
= input_register_device(in_dev
);
521 dev_err(&client
->dev
, "failed to register input device\n");
525 device_init_wakeup(&client
->dev
, pdata
->wakeup
);
526 i2c_set_clientdata(client
, bu21013_data
);
531 bu21013_free_irq(bu21013_data
);
533 pdata
->cs_dis(pdata
->cs_pin
);
534 err_disable_regulator
:
535 regulator_disable(bu21013_data
->regulator
);
537 regulator_put(bu21013_data
->regulator
);
539 input_free_device(in_dev
);
545 * bu21013_remove() - removes the i2c-client touchscreen driver
546 * @client: i2c client structure pointer
548 * This function uses to remove the i2c-client
549 * touchscreen driver and returns integer.
551 static int __devexit
bu21013_remove(struct i2c_client
*client
)
553 struct bu21013_ts_data
*bu21013_data
= i2c_get_clientdata(client
);
555 bu21013_free_irq(bu21013_data
);
557 bu21013_data
->chip
->cs_dis(bu21013_data
->chip
->cs_pin
);
559 input_unregister_device(bu21013_data
->in_dev
);
561 regulator_disable(bu21013_data
->regulator
);
562 regulator_put(bu21013_data
->regulator
);
566 device_init_wakeup(&client
->dev
, false);
573 * bu21013_suspend() - suspend the touch screen controller
574 * @dev: pointer to device structure
576 * This function is used to suspend the
577 * touch panel controller and returns integer
579 static int bu21013_suspend(struct device
*dev
)
581 struct bu21013_ts_data
*bu21013_data
= dev_get_drvdata(dev
);
582 struct i2c_client
*client
= bu21013_data
->client
;
584 bu21013_data
->touch_stopped
= true;
585 if (device_may_wakeup(&client
->dev
))
586 enable_irq_wake(bu21013_data
->chip
->irq
);
588 disable_irq(bu21013_data
->chip
->irq
);
590 regulator_disable(bu21013_data
->regulator
);
596 * bu21013_resume() - resume the touch screen controller
597 * @dev: pointer to device structure
599 * This function is used to resume the touch panel
600 * controller and returns integer.
602 static int bu21013_resume(struct device
*dev
)
604 struct bu21013_ts_data
*bu21013_data
= dev_get_drvdata(dev
);
605 struct i2c_client
*client
= bu21013_data
->client
;
608 retval
= regulator_enable(bu21013_data
->regulator
);
610 dev_err(&client
->dev
, "bu21013 regulator enable failed\n");
614 retval
= bu21013_init_chip(bu21013_data
);
616 dev_err(&client
->dev
, "bu21013 controller config failed\n");
620 bu21013_data
->touch_stopped
= false;
622 if (device_may_wakeup(&client
->dev
))
623 disable_irq_wake(bu21013_data
->chip
->irq
);
625 enable_irq(bu21013_data
->chip
->irq
);
630 static const struct dev_pm_ops bu21013_dev_pm_ops
= {
631 .suspend
= bu21013_suspend
,
632 .resume
= bu21013_resume
,
636 static const struct i2c_device_id bu21013_id
[] = {
640 MODULE_DEVICE_TABLE(i2c
, bu21013_id
);
642 static struct i2c_driver bu21013_driver
= {
645 .owner
= THIS_MODULE
,
647 .pm
= &bu21013_dev_pm_ops
,
650 .probe
= bu21013_probe
,
651 .remove
= __devexit_p(bu21013_remove
),
652 .id_table
= bu21013_id
,
656 * bu21013_init() - initializes the bu21013 touchscreen driver
658 * This function used to initializes the bu21013
659 * touchscreen driver and returns integer.
661 static int __init
bu21013_init(void)
663 return i2c_add_driver(&bu21013_driver
);
667 * bu21013_exit() - de-initializes the bu21013 touchscreen driver
669 * This function uses to de-initializes the bu21013
670 * touchscreen driver and returns none.
672 static void __exit
bu21013_exit(void)
674 i2c_del_driver(&bu21013_driver
);
677 module_init(bu21013_init
);
678 module_exit(bu21013_exit
);
680 MODULE_LICENSE("GPL v2");
681 MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
682 MODULE_DESCRIPTION("bu21013 touch screen controller driver");