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>
17 #include <linux/gpio.h>
19 #define PEN_DOWN_INTR 0
21 #define RESET_DELAY 30
22 #define PENUP_TIMEOUT (10)
24 #define MASK_BITS 0x03
27 #define LENGTH_OF_BUFFER 11
28 #define I2C_RETRY_COUNT 5
30 #define BU21013_SENSORS_BTN_0_7_REG 0x70
31 #define BU21013_SENSORS_BTN_8_15_REG 0x71
32 #define BU21013_SENSORS_BTN_16_23_REG 0x72
33 #define BU21013_X1_POS_MSB_REG 0x73
34 #define BU21013_X1_POS_LSB_REG 0x74
35 #define BU21013_Y1_POS_MSB_REG 0x75
36 #define BU21013_Y1_POS_LSB_REG 0x76
37 #define BU21013_X2_POS_MSB_REG 0x77
38 #define BU21013_X2_POS_LSB_REG 0x78
39 #define BU21013_Y2_POS_MSB_REG 0x79
40 #define BU21013_Y2_POS_LSB_REG 0x7A
41 #define BU21013_INT_CLR_REG 0xE8
42 #define BU21013_INT_MODE_REG 0xE9
43 #define BU21013_GAIN_REG 0xEA
44 #define BU21013_OFFSET_MODE_REG 0xEB
45 #define BU21013_XY_EDGE_REG 0xEC
46 #define BU21013_RESET_REG 0xED
47 #define BU21013_CALIB_REG 0xEE
48 #define BU21013_DONE_REG 0xEF
49 #define BU21013_SENSOR_0_7_REG 0xF0
50 #define BU21013_SENSOR_8_15_REG 0xF1
51 #define BU21013_SENSOR_16_23_REG 0xF2
52 #define BU21013_POS_MODE1_REG 0xF3
53 #define BU21013_POS_MODE2_REG 0xF4
54 #define BU21013_CLK_MODE_REG 0xF5
55 #define BU21013_IDLE_REG 0xFA
56 #define BU21013_FILTER_REG 0xFB
57 #define BU21013_TH_ON_REG 0xFC
58 #define BU21013_TH_OFF_REG 0xFD
61 #define BU21013_RESET_ENABLE 0x01
63 #define BU21013_SENSORS_EN_0_7 0x3F
64 #define BU21013_SENSORS_EN_8_15 0xFC
65 #define BU21013_SENSORS_EN_16_23 0x1F
67 #define BU21013_POS_MODE1_0 0x02
68 #define BU21013_POS_MODE1_1 0x04
69 #define BU21013_POS_MODE1_2 0x08
71 #define BU21013_POS_MODE2_ZERO 0x01
72 #define BU21013_POS_MODE2_AVG1 0x02
73 #define BU21013_POS_MODE2_AVG2 0x04
74 #define BU21013_POS_MODE2_EN_XY 0x08
75 #define BU21013_POS_MODE2_EN_RAW 0x10
76 #define BU21013_POS_MODE2_MULTI 0x80
78 #define BU21013_CLK_MODE_DIV 0x01
79 #define BU21013_CLK_MODE_EXT 0x02
80 #define BU21013_CLK_MODE_CALIB 0x80
82 #define BU21013_IDLET_0 0x01
83 #define BU21013_IDLET_1 0x02
84 #define BU21013_IDLET_2 0x04
85 #define BU21013_IDLET_3 0x08
86 #define BU21013_IDLE_INTERMIT_EN 0x10
88 #define BU21013_DELTA_0_6 0x7F
89 #define BU21013_FILTER_EN 0x80
91 #define BU21013_INT_MODE_LEVEL 0x00
92 #define BU21013_INT_MODE_EDGE 0x01
94 #define BU21013_GAIN_0 0x01
95 #define BU21013_GAIN_1 0x02
96 #define BU21013_GAIN_2 0x04
98 #define BU21013_OFFSET_MODE_DEFAULT 0x00
99 #define BU21013_OFFSET_MODE_MOVE 0x01
100 #define BU21013_OFFSET_MODE_DISABLE 0x02
102 #define BU21013_TH_ON_0 0x01
103 #define BU21013_TH_ON_1 0x02
104 #define BU21013_TH_ON_2 0x04
105 #define BU21013_TH_ON_3 0x08
106 #define BU21013_TH_ON_4 0x10
107 #define BU21013_TH_ON_5 0x20
108 #define BU21013_TH_ON_6 0x40
109 #define BU21013_TH_ON_7 0x80
110 #define BU21013_TH_ON_MAX 0xFF
112 #define BU21013_TH_OFF_0 0x01
113 #define BU21013_TH_OFF_1 0x02
114 #define BU21013_TH_OFF_2 0x04
115 #define BU21013_TH_OFF_3 0x08
116 #define BU21013_TH_OFF_4 0x10
117 #define BU21013_TH_OFF_5 0x20
118 #define BU21013_TH_OFF_6 0x40
119 #define BU21013_TH_OFF_7 0x80
120 #define BU21013_TH_OFF_MAX 0xFF
122 #define BU21013_X_EDGE_0 0x01
123 #define BU21013_X_EDGE_1 0x02
124 #define BU21013_X_EDGE_2 0x04
125 #define BU21013_X_EDGE_3 0x08
126 #define BU21013_Y_EDGE_0 0x10
127 #define BU21013_Y_EDGE_1 0x20
128 #define BU21013_Y_EDGE_2 0x40
129 #define BU21013_Y_EDGE_3 0x80
131 #define BU21013_DONE 0x01
132 #define BU21013_NUMBER_OF_X_SENSORS (6)
133 #define BU21013_NUMBER_OF_Y_SENSORS (11)
135 #define DRIVER_TP "bu21013_tp"
138 * struct bu21013_ts_data - touch panel data structure
139 * @client: pointer to the i2c client
140 * @wait: variable to wait_queue_head_t structure
141 * @touch_stopped: touch stop flag
142 * @chip: pointer to the touch panel controller
143 * @in_dev: pointer to the input device structure
144 * @intr_pin: interrupt pin value
145 * @regulator: pointer to the Regulator used for touch screen
147 * Touch panel device data structure
149 struct bu21013_ts_data {
150 struct i2c_client *client;
151 wait_queue_head_t wait;
152 const struct bu21013_platform_device *chip;
153 struct input_dev *in_dev;
154 struct regulator *regulator;
156 unsigned int intr_pin;
161 * bu21013_read_block_data(): read the touch co-ordinates
162 * @data: bu21013_ts_data structure pointer
165 * Read the touch co-ordinates using i2c read block into buffer
166 * and returns integer.
168 static int bu21013_read_block_data(struct bu21013_ts_data *data, u8 *buf)
172 for (i = 0; i < I2C_RETRY_COUNT; i++) {
173 ret = i2c_smbus_read_i2c_block_data
174 (data->client, BU21013_SENSORS_BTN_0_7_REG,
175 LENGTH_OF_BUFFER, buf);
176 if (ret == LENGTH_OF_BUFFER)
183 * bu21013_do_touch_report(): Get the touch co-ordinates
184 * @data: bu21013_ts_data structure pointer
186 * Get the touch co-ordinates from touch sensor registers and writes
187 * into device structure and returns integer.
189 static int bu21013_do_touch_report(struct bu21013_ts_data *data)
191 u8 buf[LENGTH_OF_BUFFER];
192 unsigned int pos_x[2], pos_y[2];
193 bool has_x_sensors, has_y_sensors;
194 int finger_down_count = 0;
200 if (bu21013_read_block_data(data, buf) < 0)
203 has_x_sensors = hweight32(buf[0] & BU21013_SENSORS_EN_0_7);
204 has_y_sensors = hweight32(((buf[1] & BU21013_SENSORS_EN_8_15) |
205 ((buf[2] & BU21013_SENSORS_EN_16_23) << SHIFT_8)) >> SHIFT_2);
206 if (!has_x_sensors || !has_y_sensors)
209 for (i = 0; i < MAX_FINGERS; i++) {
210 const u8 *p = &buf[4 * i + 3];
211 unsigned int x = p[0] << SHIFT_2 | (p[1] & MASK_BITS);
212 unsigned int y = p[2] << SHIFT_2 | (p[3] & MASK_BITS);
213 if (x == 0 || y == 0)
215 pos_x[finger_down_count] = x;
216 pos_y[finger_down_count] = y;
220 if (finger_down_count) {
221 if (finger_down_count == 2 &&
222 (abs(pos_x[0] - pos_x[1]) < DELTA_MIN ||
223 abs(pos_y[0] - pos_y[1]) < DELTA_MIN)) {
227 for (i = 0; i < finger_down_count; i++) {
228 if (data->chip->x_flip)
229 pos_x[i] = data->chip->touch_x_max - pos_x[i];
230 if (data->chip->y_flip)
231 pos_y[i] = data->chip->touch_y_max - pos_y[i];
233 input_report_abs(data->in_dev,
234 ABS_MT_POSITION_X, pos_x[i]);
235 input_report_abs(data->in_dev,
236 ABS_MT_POSITION_Y, pos_y[i]);
237 input_mt_sync(data->in_dev);
240 input_mt_sync(data->in_dev);
242 input_sync(data->in_dev);
247 * bu21013_gpio_irq() - gpio thread function for touch interrupt
249 * @device_data: void pointer
251 * This gpio thread function for touch interrupt
252 * and returns irqreturn_t.
254 static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
256 struct bu21013_ts_data *data = device_data;
257 struct i2c_client *i2c = data->client;
261 retval = bu21013_do_touch_report(data);
263 dev_err(&i2c->dev, "bu21013_do_touch_report failed\n");
267 data->intr_pin = gpio_get_value(data->chip->touch_pin);
268 if (data->intr_pin == PEN_DOWN_INTR)
269 wait_event_timeout(data->wait, data->touch_stopped,
270 msecs_to_jiffies(2));
271 } while (!data->intr_pin && !data->touch_stopped);
277 * bu21013_init_chip() - power on sequence for the bu21013 controller
278 * @data: device structure pointer
280 * This function is used to power on
281 * the bu21013 controller and returns integer.
283 static int bu21013_init_chip(struct bu21013_ts_data *data)
286 struct i2c_client *i2c = data->client;
288 retval = i2c_smbus_write_byte_data(i2c, BU21013_RESET_REG,
289 BU21013_RESET_ENABLE);
291 dev_err(&i2c->dev, "BU21013_RESET reg write failed\n");
296 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_0_7_REG,
297 BU21013_SENSORS_EN_0_7);
299 dev_err(&i2c->dev, "BU21013_SENSOR_0_7 reg write failed\n");
303 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_8_15_REG,
304 BU21013_SENSORS_EN_8_15);
306 dev_err(&i2c->dev, "BU21013_SENSOR_8_15 reg write failed\n");
310 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_16_23_REG,
311 BU21013_SENSORS_EN_16_23);
313 dev_err(&i2c->dev, "BU21013_SENSOR_16_23 reg write failed\n");
317 retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE1_REG,
318 (BU21013_POS_MODE1_0 | BU21013_POS_MODE1_1));
320 dev_err(&i2c->dev, "BU21013_POS_MODE1 reg write failed\n");
324 retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE2_REG,
325 (BU21013_POS_MODE2_ZERO | BU21013_POS_MODE2_AVG1 |
326 BU21013_POS_MODE2_AVG2 | BU21013_POS_MODE2_EN_RAW |
327 BU21013_POS_MODE2_MULTI));
329 dev_err(&i2c->dev, "BU21013_POS_MODE2 reg write failed\n");
333 if (data->chip->ext_clk)
334 retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
335 (BU21013_CLK_MODE_EXT | BU21013_CLK_MODE_CALIB));
337 retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
338 (BU21013_CLK_MODE_DIV | BU21013_CLK_MODE_CALIB));
340 dev_err(&i2c->dev, "BU21013_CLK_MODE reg write failed\n");
344 retval = i2c_smbus_write_byte_data(i2c, BU21013_IDLE_REG,
345 (BU21013_IDLET_0 | BU21013_IDLE_INTERMIT_EN));
347 dev_err(&i2c->dev, "BU21013_IDLE reg write failed\n");
351 retval = i2c_smbus_write_byte_data(i2c, BU21013_INT_MODE_REG,
352 BU21013_INT_MODE_LEVEL);
354 dev_err(&i2c->dev, "BU21013_INT_MODE reg write failed\n");
358 retval = i2c_smbus_write_byte_data(i2c, BU21013_FILTER_REG,
362 dev_err(&i2c->dev, "BU21013_FILTER reg write failed\n");
366 retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_ON_REG,
369 dev_err(&i2c->dev, "BU21013_TH_ON reg write failed\n");
373 retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_OFF_REG,
374 BU21013_TH_OFF_4 | BU21013_TH_OFF_3);
376 dev_err(&i2c->dev, "BU21013_TH_OFF reg write failed\n");
380 retval = i2c_smbus_write_byte_data(i2c, BU21013_GAIN_REG,
381 (BU21013_GAIN_0 | BU21013_GAIN_1));
383 dev_err(&i2c->dev, "BU21013_GAIN reg write failed\n");
387 retval = i2c_smbus_write_byte_data(i2c, BU21013_OFFSET_MODE_REG,
388 BU21013_OFFSET_MODE_DEFAULT);
390 dev_err(&i2c->dev, "BU21013_OFFSET_MODE reg write failed\n");
394 retval = i2c_smbus_write_byte_data(i2c, BU21013_XY_EDGE_REG,
395 (BU21013_X_EDGE_0 | BU21013_X_EDGE_2 |
396 BU21013_Y_EDGE_1 | BU21013_Y_EDGE_3));
398 dev_err(&i2c->dev, "BU21013_XY_EDGE reg write failed\n");
402 retval = i2c_smbus_write_byte_data(i2c, BU21013_DONE_REG,
405 dev_err(&i2c->dev, "BU21013_REG_DONE reg write failed\n");
413 * bu21013_free_irq() - frees IRQ registered for touchscreen
414 * @bu21013_data: device structure pointer
416 * This function signals interrupt thread to stop processing and
419 static void bu21013_free_irq(struct bu21013_ts_data *bu21013_data)
421 bu21013_data->touch_stopped = true;
422 wake_up(&bu21013_data->wait);
423 free_irq(bu21013_data->irq, bu21013_data);
427 * bu21013_cs_disable() - deconfigures the touch panel controller
428 * @bu21013_data: device structure pointer
430 * This function is used to deconfigure the chip selection
431 * for touch panel controller.
433 static void bu21013_cs_disable(struct bu21013_ts_data *bu21013_data)
437 error = gpio_direction_output(bu21013_data->chip->cs_pin, 0);
439 dev_warn(&bu21013_data->client->dev,
440 "%s: gpio direction failed, error: %d\n",
443 gpio_set_value(bu21013_data->chip->cs_pin, 0);
445 gpio_free(bu21013_data->chip->cs_pin);
450 * bu21013_probe() - initializes the i2c-client touchscreen driver
451 * @client: i2c client structure pointer
452 * @id: i2c device id pointer
454 * This function used to initializes the i2c-client touchscreen
455 * driver and returns integer.
457 static int bu21013_probe(struct i2c_client *client,
458 const struct i2c_device_id *id)
460 struct bu21013_ts_data *bu21013_data;
461 struct input_dev *in_dev;
462 const struct bu21013_platform_device *pdata =
463 client->dev.platform_data;
466 if (!i2c_check_functionality(client->adapter,
467 I2C_FUNC_SMBUS_BYTE_DATA)) {
468 dev_err(&client->dev, "i2c smbus byte data not supported\n");
473 dev_err(&client->dev, "platform data not defined\n");
477 if (!gpio_is_valid(pdata->touch_pin)) {
478 dev_err(&client->dev, "invalid touch_pin supplied\n");
482 bu21013_data = kzalloc(sizeof(struct bu21013_ts_data), GFP_KERNEL);
483 in_dev = input_allocate_device();
484 if (!bu21013_data || !in_dev) {
485 dev_err(&client->dev, "device memory alloc failed\n");
490 bu21013_data->in_dev = in_dev;
491 bu21013_data->chip = pdata;
492 bu21013_data->client = client;
493 bu21013_data->irq = gpio_to_irq(pdata->touch_pin);
495 bu21013_data->regulator = regulator_get(&client->dev, "avdd");
496 if (IS_ERR(bu21013_data->regulator)) {
497 dev_err(&client->dev, "regulator_get failed\n");
498 error = PTR_ERR(bu21013_data->regulator);
502 error = regulator_enable(bu21013_data->regulator);
504 dev_err(&client->dev, "regulator enable failed\n");
505 goto err_put_regulator;
508 bu21013_data->touch_stopped = false;
509 init_waitqueue_head(&bu21013_data->wait);
511 /* configure the gpio pins */
512 error = gpio_request_one(pdata->cs_pin, GPIOF_OUT_INIT_HIGH,
515 dev_err(&client->dev, "Unable to request gpio reset_pin\n");
516 goto err_disable_regulator;
519 /* configure the touch panel controller */
520 error = bu21013_init_chip(bu21013_data);
522 dev_err(&client->dev, "error in bu21013 config\n");
526 /* register the device to input subsystem */
527 in_dev->name = DRIVER_TP;
528 in_dev->id.bustype = BUS_I2C;
529 in_dev->dev.parent = &client->dev;
531 __set_bit(EV_SYN, in_dev->evbit);
532 __set_bit(EV_KEY, in_dev->evbit);
533 __set_bit(EV_ABS, in_dev->evbit);
535 input_set_abs_params(in_dev, ABS_MT_POSITION_X, 0,
536 pdata->touch_x_max, 0, 0);
537 input_set_abs_params(in_dev, ABS_MT_POSITION_Y, 0,
538 pdata->touch_y_max, 0, 0);
539 input_set_drvdata(in_dev, bu21013_data);
541 error = request_threaded_irq(bu21013_data->irq, NULL, bu21013_gpio_irq,
542 IRQF_TRIGGER_FALLING | IRQF_SHARED |
544 DRIVER_TP, bu21013_data);
546 dev_err(&client->dev, "request irq %d failed\n",
551 error = input_register_device(in_dev);
553 dev_err(&client->dev, "failed to register input device\n");
557 device_init_wakeup(&client->dev, pdata->wakeup);
558 i2c_set_clientdata(client, bu21013_data);
563 bu21013_free_irq(bu21013_data);
565 bu21013_cs_disable(bu21013_data);
566 err_disable_regulator:
567 regulator_disable(bu21013_data->regulator);
569 regulator_put(bu21013_data->regulator);
571 input_free_device(in_dev);
577 * bu21013_remove() - removes the i2c-client touchscreen driver
578 * @client: i2c client structure pointer
580 * This function uses to remove the i2c-client
581 * touchscreen driver and returns integer.
583 static int bu21013_remove(struct i2c_client *client)
585 struct bu21013_ts_data *bu21013_data = i2c_get_clientdata(client);
587 bu21013_free_irq(bu21013_data);
589 bu21013_cs_disable(bu21013_data);
591 input_unregister_device(bu21013_data->in_dev);
593 regulator_disable(bu21013_data->regulator);
594 regulator_put(bu21013_data->regulator);
598 device_init_wakeup(&client->dev, false);
605 * bu21013_suspend() - suspend the touch screen controller
606 * @dev: pointer to device structure
608 * This function is used to suspend the
609 * touch panel controller and returns integer
611 static int bu21013_suspend(struct device *dev)
613 struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
614 struct i2c_client *client = bu21013_data->client;
616 bu21013_data->touch_stopped = true;
617 if (device_may_wakeup(&client->dev))
618 enable_irq_wake(bu21013_data->irq);
620 disable_irq(bu21013_data->irq);
622 regulator_disable(bu21013_data->regulator);
628 * bu21013_resume() - resume the touch screen controller
629 * @dev: pointer to device structure
631 * This function is used to resume the touch panel
632 * controller and returns integer.
634 static int bu21013_resume(struct device *dev)
636 struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
637 struct i2c_client *client = bu21013_data->client;
640 retval = regulator_enable(bu21013_data->regulator);
642 dev_err(&client->dev, "bu21013 regulator enable failed\n");
646 retval = bu21013_init_chip(bu21013_data);
648 dev_err(&client->dev, "bu21013 controller config failed\n");
652 bu21013_data->touch_stopped = false;
654 if (device_may_wakeup(&client->dev))
655 disable_irq_wake(bu21013_data->irq);
657 enable_irq(bu21013_data->irq);
662 static const struct dev_pm_ops bu21013_dev_pm_ops = {
663 .suspend = bu21013_suspend,
664 .resume = bu21013_resume,
668 static const struct i2c_device_id bu21013_id[] = {
672 MODULE_DEVICE_TABLE(i2c, bu21013_id);
674 static struct i2c_driver bu21013_driver = {
677 .owner = THIS_MODULE,
679 .pm = &bu21013_dev_pm_ops,
682 .probe = bu21013_probe,
683 .remove = bu21013_remove,
684 .id_table = bu21013_id,
687 module_i2c_driver(bu21013_driver);
689 MODULE_LICENSE("GPL v2");
690 MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
691 MODULE_DESCRIPTION("bu21013 touch screen controller driver");