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>
16 #define PEN_DOWN_INTR 0
18 #define RESET_DELAY 30
19 #define PENUP_TIMEOUT (10)
21 #define MASK_BITS 0x03
24 #define LENGTH_OF_BUFFER 11
25 #define I2C_RETRY_COUNT 5
27 #define BU21013_SENSORS_BTN_0_7_REG 0x70
28 #define BU21013_SENSORS_BTN_8_15_REG 0x71
29 #define BU21013_SENSORS_BTN_16_23_REG 0x72
30 #define BU21013_X1_POS_MSB_REG 0x73
31 #define BU21013_X1_POS_LSB_REG 0x74
32 #define BU21013_Y1_POS_MSB_REG 0x75
33 #define BU21013_Y1_POS_LSB_REG 0x76
34 #define BU21013_X2_POS_MSB_REG 0x77
35 #define BU21013_X2_POS_LSB_REG 0x78
36 #define BU21013_Y2_POS_MSB_REG 0x79
37 #define BU21013_Y2_POS_LSB_REG 0x7A
38 #define BU21013_INT_CLR_REG 0xE8
39 #define BU21013_INT_MODE_REG 0xE9
40 #define BU21013_GAIN_REG 0xEA
41 #define BU21013_OFFSET_MODE_REG 0xEB
42 #define BU21013_XY_EDGE_REG 0xEC
43 #define BU21013_RESET_REG 0xED
44 #define BU21013_CALIB_REG 0xEE
45 #define BU21013_DONE_REG 0xEF
46 #define BU21013_SENSOR_0_7_REG 0xF0
47 #define BU21013_SENSOR_8_15_REG 0xF1
48 #define BU21013_SENSOR_16_23_REG 0xF2
49 #define BU21013_POS_MODE1_REG 0xF3
50 #define BU21013_POS_MODE2_REG 0xF4
51 #define BU21013_CLK_MODE_REG 0xF5
52 #define BU21013_IDLE_REG 0xFA
53 #define BU21013_FILTER_REG 0xFB
54 #define BU21013_TH_ON_REG 0xFC
55 #define BU21013_TH_OFF_REG 0xFD
58 #define BU21013_RESET_ENABLE 0x01
60 #define BU21013_SENSORS_EN_0_7 0x3F
61 #define BU21013_SENSORS_EN_8_15 0xFC
62 #define BU21013_SENSORS_EN_16_23 0x1F
64 #define BU21013_POS_MODE1_0 0x02
65 #define BU21013_POS_MODE1_1 0x04
66 #define BU21013_POS_MODE1_2 0x08
68 #define BU21013_POS_MODE2_ZERO 0x01
69 #define BU21013_POS_MODE2_AVG1 0x02
70 #define BU21013_POS_MODE2_AVG2 0x04
71 #define BU21013_POS_MODE2_EN_XY 0x08
72 #define BU21013_POS_MODE2_EN_RAW 0x10
73 #define BU21013_POS_MODE2_MULTI 0x80
75 #define BU21013_CLK_MODE_DIV 0x01
76 #define BU21013_CLK_MODE_EXT 0x02
77 #define BU21013_CLK_MODE_CALIB 0x80
79 #define BU21013_IDLET_0 0x01
80 #define BU21013_IDLET_1 0x02
81 #define BU21013_IDLET_2 0x04
82 #define BU21013_IDLET_3 0x08
83 #define BU21013_IDLE_INTERMIT_EN 0x10
85 #define BU21013_DELTA_0_6 0x7F
86 #define BU21013_FILTER_EN 0x80
88 #define BU21013_INT_MODE_LEVEL 0x00
89 #define BU21013_INT_MODE_EDGE 0x01
91 #define BU21013_GAIN_0 0x01
92 #define BU21013_GAIN_1 0x02
93 #define BU21013_GAIN_2 0x04
95 #define BU21013_OFFSET_MODE_DEFAULT 0x00
96 #define BU21013_OFFSET_MODE_MOVE 0x01
97 #define BU21013_OFFSET_MODE_DISABLE 0x02
99 #define BU21013_TH_ON_0 0x01
100 #define BU21013_TH_ON_1 0x02
101 #define BU21013_TH_ON_2 0x04
102 #define BU21013_TH_ON_3 0x08
103 #define BU21013_TH_ON_4 0x10
104 #define BU21013_TH_ON_5 0x20
105 #define BU21013_TH_ON_6 0x40
106 #define BU21013_TH_ON_7 0x80
107 #define BU21013_TH_ON_MAX 0xFF
109 #define BU21013_TH_OFF_0 0x01
110 #define BU21013_TH_OFF_1 0x02
111 #define BU21013_TH_OFF_2 0x04
112 #define BU21013_TH_OFF_3 0x08
113 #define BU21013_TH_OFF_4 0x10
114 #define BU21013_TH_OFF_5 0x20
115 #define BU21013_TH_OFF_6 0x40
116 #define BU21013_TH_OFF_7 0x80
117 #define BU21013_TH_OFF_MAX 0xFF
119 #define BU21013_X_EDGE_0 0x01
120 #define BU21013_X_EDGE_1 0x02
121 #define BU21013_X_EDGE_2 0x04
122 #define BU21013_X_EDGE_3 0x08
123 #define BU21013_Y_EDGE_0 0x10
124 #define BU21013_Y_EDGE_1 0x20
125 #define BU21013_Y_EDGE_2 0x40
126 #define BU21013_Y_EDGE_3 0x80
128 #define BU21013_DONE 0x01
129 #define BU21013_NUMBER_OF_X_SENSORS (6)
130 #define BU21013_NUMBER_OF_Y_SENSORS (11)
132 #define DRIVER_TP "bu21013_tp"
135 * struct bu21013_ts_data - touch panel data structure
136 * @client: pointer to the i2c client
137 * @wait: variable to wait_queue_head_t structure
138 * @touch_stopped: touch stop flag
139 * @chip: pointer to the touch panel controller
140 * @in_dev: pointer to the input device structure
141 * @intr_pin: interrupt pin value
143 * Touch panel device data structure
145 struct bu21013_ts_data
{
146 struct i2c_client
*client
;
147 wait_queue_head_t wait
;
149 const struct bu21013_platform_device
*chip
;
150 struct input_dev
*in_dev
;
151 unsigned int intr_pin
;
155 * bu21013_read_block_data(): read the touch co-ordinates
156 * @data: bu21013_ts_data structure pointer
159 * Read the touch co-ordinates using i2c read block into buffer
160 * and returns integer.
162 static int bu21013_read_block_data(struct bu21013_ts_data
*data
, u8
*buf
)
166 for (i
= 0; i
< I2C_RETRY_COUNT
; i
++) {
167 ret
= i2c_smbus_read_i2c_block_data
168 (data
->client
, BU21013_SENSORS_BTN_0_7_REG
,
169 LENGTH_OF_BUFFER
, buf
);
170 if (ret
== LENGTH_OF_BUFFER
)
177 * bu21013_do_touch_report(): Get the touch co-ordinates
178 * @data: bu21013_ts_data structure pointer
180 * Get the touch co-ordinates from touch sensor registers and writes
181 * into device structure and returns integer.
183 static int bu21013_do_touch_report(struct bu21013_ts_data
*data
)
185 u8 buf
[LENGTH_OF_BUFFER
];
186 unsigned int pos_x
[2], pos_y
[2];
187 bool has_x_sensors
, has_y_sensors
;
188 int finger_down_count
= 0;
194 if (bu21013_read_block_data(data
, buf
) < 0)
197 has_x_sensors
= hweight32(buf
[0] & BU21013_SENSORS_EN_0_7
);
198 has_y_sensors
= hweight32(((buf
[1] & BU21013_SENSORS_EN_8_15
) |
199 ((buf
[2] & BU21013_SENSORS_EN_16_23
) << SHIFT_8
)) >> SHIFT_2
);
200 if (!has_x_sensors
|| !has_y_sensors
)
203 for (i
= 0; i
< MAX_FINGERS
; i
++) {
204 const u8
*p
= &buf
[4 * i
+ 3];
205 unsigned int x
= p
[0] << SHIFT_2
| (p
[1] & MASK_BITS
);
206 unsigned int y
= p
[2] << SHIFT_2
| (p
[3] & MASK_BITS
);
207 if (x
== 0 || y
== 0)
209 pos_x
[finger_down_count
] = x
;
210 pos_y
[finger_down_count
] = y
;
214 if (finger_down_count
) {
215 if (finger_down_count
== 2 &&
216 (abs(pos_x
[0] - pos_x
[1]) < DELTA_MIN
||
217 abs(pos_y
[0] - pos_y
[1]) < DELTA_MIN
)) {
221 for (i
= 0; i
< finger_down_count
; i
++) {
222 if (data
->chip
->x_flip
)
223 pos_x
[i
] = data
->chip
->touch_x_max
- pos_x
[i
];
224 if (data
->chip
->y_flip
)
225 pos_y
[i
] = data
->chip
->touch_y_max
- pos_y
[i
];
227 input_report_abs(data
->in_dev
,
228 ABS_MT_POSITION_X
, pos_x
[i
]);
229 input_report_abs(data
->in_dev
,
230 ABS_MT_POSITION_Y
, pos_y
[i
]);
231 input_mt_sync(data
->in_dev
);
234 input_mt_sync(data
->in_dev
);
236 input_sync(data
->in_dev
);
241 * bu21013_gpio_irq() - gpio thread function for touch interrupt
243 * @device_data: void pointer
245 * This gpio thread function for touch interrupt
246 * and returns irqreturn_t.
248 static irqreturn_t
bu21013_gpio_irq(int irq
, void *device_data
)
250 struct bu21013_ts_data
*data
= device_data
;
251 struct i2c_client
*i2c
= data
->client
;
255 retval
= bu21013_do_touch_report(data
);
257 dev_err(&i2c
->dev
, "bu21013_do_touch_report failed\n");
261 data
->intr_pin
= data
->chip
->irq_read_val();
262 if (data
->intr_pin
== PEN_DOWN_INTR
)
263 wait_event_timeout(data
->wait
, data
->touch_stopped
,
264 msecs_to_jiffies(2));
265 } while (!data
->intr_pin
&& !data
->touch_stopped
);
271 * bu21013_init_chip() - power on sequence for the bu21013 controller
272 * @data: device structure pointer
274 * This function is used to power on
275 * the bu21013 controller and returns integer.
277 static int bu21013_init_chip(struct bu21013_ts_data
*data
)
280 struct i2c_client
*i2c
= data
->client
;
282 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_RESET_REG
,
283 BU21013_RESET_ENABLE
);
285 dev_err(&i2c
->dev
, "BU21013_RESET reg write failed\n");
290 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_0_7_REG
,
291 BU21013_SENSORS_EN_0_7
);
293 dev_err(&i2c
->dev
, "BU21013_SENSOR_0_7 reg write failed\n");
297 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_8_15_REG
,
298 BU21013_SENSORS_EN_8_15
);
300 dev_err(&i2c
->dev
, "BU21013_SENSOR_8_15 reg write failed\n");
304 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_SENSOR_16_23_REG
,
305 BU21013_SENSORS_EN_16_23
);
307 dev_err(&i2c
->dev
, "BU21013_SENSOR_16_23 reg write failed\n");
311 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_POS_MODE1_REG
,
312 (BU21013_POS_MODE1_0
| BU21013_POS_MODE1_1
));
314 dev_err(&i2c
->dev
, "BU21013_POS_MODE1 reg write failed\n");
318 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_POS_MODE2_REG
,
319 (BU21013_POS_MODE2_ZERO
| BU21013_POS_MODE2_AVG1
|
320 BU21013_POS_MODE2_AVG2
| BU21013_POS_MODE2_EN_RAW
|
321 BU21013_POS_MODE2_MULTI
));
323 dev_err(&i2c
->dev
, "BU21013_POS_MODE2 reg write failed\n");
327 if (data
->chip
->ext_clk
)
328 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_CLK_MODE_REG
,
329 (BU21013_CLK_MODE_EXT
| BU21013_CLK_MODE_CALIB
));
331 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_CLK_MODE_REG
,
332 (BU21013_CLK_MODE_DIV
| BU21013_CLK_MODE_CALIB
));
334 dev_err(&i2c
->dev
, "BU21013_CLK_MODE reg write failed\n");
338 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_IDLE_REG
,
339 (BU21013_IDLET_0
| BU21013_IDLE_INTERMIT_EN
));
341 dev_err(&i2c
->dev
, "BU21013_IDLE reg write failed\n");
345 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_INT_MODE_REG
,
346 BU21013_INT_MODE_LEVEL
);
348 dev_err(&i2c
->dev
, "BU21013_INT_MODE reg write failed\n");
352 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_FILTER_REG
,
356 dev_err(&i2c
->dev
, "BU21013_FILTER reg write failed\n");
360 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_TH_ON_REG
,
363 dev_err(&i2c
->dev
, "BU21013_TH_ON reg write failed\n");
367 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_TH_OFF_REG
,
368 BU21013_TH_OFF_4
| BU21013_TH_OFF_3
);
370 dev_err(&i2c
->dev
, "BU21013_TH_OFF reg write failed\n");
374 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_GAIN_REG
,
375 (BU21013_GAIN_0
| BU21013_GAIN_1
));
377 dev_err(&i2c
->dev
, "BU21013_GAIN reg write failed\n");
381 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_OFFSET_MODE_REG
,
382 BU21013_OFFSET_MODE_DEFAULT
);
384 dev_err(&i2c
->dev
, "BU21013_OFFSET_MODE reg write failed\n");
388 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_XY_EDGE_REG
,
389 (BU21013_X_EDGE_0
| BU21013_X_EDGE_2
|
390 BU21013_Y_EDGE_1
| BU21013_Y_EDGE_3
));
392 dev_err(&i2c
->dev
, "BU21013_XY_EDGE reg write failed\n");
396 retval
= i2c_smbus_write_byte_data(i2c
, BU21013_DONE_REG
,
399 dev_err(&i2c
->dev
, "BU21013_REG_DONE reg write failed\n");
407 * bu21013_free_irq() - frees IRQ registered for touchscreen
408 * @bu21013_data: device structure pointer
410 * This function signals interrupt thread to stop processing and
413 static void bu21013_free_irq(struct bu21013_ts_data
*bu21013_data
)
415 bu21013_data
->touch_stopped
= true;
416 wake_up(&bu21013_data
->wait
);
417 free_irq(bu21013_data
->chip
->irq
, bu21013_data
);
421 * bu21013_probe() - initializes the i2c-client touchscreen driver
422 * @client: i2c client structure pointer
423 * @id: i2c device id pointer
425 * This function used to initializes the i2c-client touchscreen
426 * driver and returns integer.
428 static int __devinit
bu21013_probe(struct i2c_client
*client
,
429 const struct i2c_device_id
*id
)
431 struct bu21013_ts_data
*bu21013_data
;
432 struct input_dev
*in_dev
;
433 const struct bu21013_platform_device
*pdata
=
434 client
->dev
.platform_data
;
437 if (!i2c_check_functionality(client
->adapter
,
438 I2C_FUNC_SMBUS_BYTE_DATA
)) {
439 dev_err(&client
->dev
, "i2c smbus byte data not supported\n");
444 dev_err(&client
->dev
, "platform data not defined\n");
448 bu21013_data
= kzalloc(sizeof(struct bu21013_ts_data
), GFP_KERNEL
);
449 in_dev
= input_allocate_device();
450 if (!bu21013_data
|| !in_dev
) {
451 dev_err(&client
->dev
, "device memory alloc failed\n");
456 bu21013_data
->in_dev
= in_dev
;
457 bu21013_data
->chip
= pdata
;
458 bu21013_data
->client
= client
;
459 bu21013_data
->touch_stopped
= false;
460 init_waitqueue_head(&bu21013_data
->wait
);
462 /* configure the gpio pins */
464 error
= pdata
->cs_en(pdata
->cs_pin
);
466 dev_err(&client
->dev
, "chip init failed\n");
471 /* configure the touch panel controller */
472 error
= bu21013_init_chip(bu21013_data
);
474 dev_err(&client
->dev
, "error in bu21013 config\n");
478 /* register the device to input subsystem */
479 in_dev
->name
= DRIVER_TP
;
480 in_dev
->id
.bustype
= BUS_I2C
;
481 in_dev
->dev
.parent
= &client
->dev
;
483 __set_bit(EV_SYN
, in_dev
->evbit
);
484 __set_bit(EV_KEY
, in_dev
->evbit
);
485 __set_bit(EV_ABS
, in_dev
->evbit
);
487 input_set_abs_params(in_dev
, ABS_MT_POSITION_X
, 0,
488 pdata
->x_max_res
, 0, 0);
489 input_set_abs_params(in_dev
, ABS_MT_POSITION_Y
, 0,
490 pdata
->y_max_res
, 0, 0);
491 input_set_drvdata(in_dev
, bu21013_data
);
493 error
= request_threaded_irq(pdata
->irq
, NULL
, bu21013_gpio_irq
,
494 IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
495 DRIVER_TP
, bu21013_data
);
497 dev_err(&client
->dev
, "request irq %d failed\n", pdata
->irq
);
501 error
= input_register_device(in_dev
);
503 dev_err(&client
->dev
, "failed to register input device\n");
507 device_init_wakeup(&client
->dev
, pdata
->wakeup
);
508 i2c_set_clientdata(client
, bu21013_data
);
513 bu21013_free_irq(bu21013_data
);
515 pdata
->cs_dis(pdata
->cs_pin
);
517 input_free_device(in_dev
);
523 * bu21013_remove() - removes the i2c-client touchscreen driver
524 * @client: i2c client structure pointer
526 * This function uses to remove the i2c-client
527 * touchscreen driver and returns integer.
529 static int __devexit
bu21013_remove(struct i2c_client
*client
)
531 struct bu21013_ts_data
*bu21013_data
= i2c_get_clientdata(client
);
533 bu21013_free_irq(bu21013_data
);
535 bu21013_data
->chip
->cs_dis(bu21013_data
->chip
->cs_pin
);
537 input_unregister_device(bu21013_data
->in_dev
);
540 device_init_wakeup(&client
->dev
, false);
547 * bu21013_suspend() - suspend the touch screen controller
548 * @dev: pointer to device structure
550 * This function is used to suspend the
551 * touch panel controller and returns integer
553 static int bu21013_suspend(struct device
*dev
)
555 struct bu21013_ts_data
*bu21013_data
= dev_get_drvdata(dev
);
556 struct i2c_client
*client
= bu21013_data
->client
;
558 bu21013_data
->touch_stopped
= true;
559 if (device_may_wakeup(&client
->dev
))
560 enable_irq_wake(bu21013_data
->chip
->irq
);
562 disable_irq(bu21013_data
->chip
->irq
);
568 * bu21013_resume() - resume the touch screen controller
569 * @dev: pointer to device structure
571 * This function is used to resume the touch panel
572 * controller and returns integer.
574 static int bu21013_resume(struct device
*dev
)
576 struct bu21013_ts_data
*bu21013_data
= dev_get_drvdata(dev
);
577 struct i2c_client
*client
= bu21013_data
->client
;
580 retval
= bu21013_init_chip(bu21013_data
);
582 dev_err(&client
->dev
, "bu21013 controller config failed\n");
586 bu21013_data
->touch_stopped
= false;
588 if (device_may_wakeup(&client
->dev
))
589 disable_irq_wake(bu21013_data
->chip
->irq
);
591 enable_irq(bu21013_data
->chip
->irq
);
596 static const struct dev_pm_ops bu21013_dev_pm_ops
= {
597 .suspend
= bu21013_suspend
,
598 .resume
= bu21013_resume
,
602 static const struct i2c_device_id bu21013_id
[] = {
606 MODULE_DEVICE_TABLE(i2c
, bu21013_id
);
608 static struct i2c_driver bu21013_driver
= {
611 .owner
= THIS_MODULE
,
613 .pm
= &bu21013_dev_pm_ops
,
616 .probe
= bu21013_probe
,
617 .remove
= __devexit_p(bu21013_remove
),
618 .id_table
= bu21013_id
,
622 * bu21013_init() - initializes the bu21013 touchscreen driver
624 * This function used to initializes the bu21013
625 * touchscreen driver and returns integer.
627 static int __init
bu21013_init(void)
629 return i2c_add_driver(&bu21013_driver
);
633 * bu21013_exit() - de-initializes the bu21013 touchscreen driver
635 * This function uses to de-initializes the bu21013
636 * touchscreen driver and returns none.
638 static void __exit
bu21013_exit(void)
640 i2c_del_driver(&bu21013_driver
);
643 module_init(bu21013_init
);
644 module_exit(bu21013_exit
);
646 MODULE_LICENSE("GPL v2");
647 MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
648 MODULE_DESCRIPTION("bu21013 touch screen controller driver");