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 #include <linux/of_gpio.h>
21 #define PEN_DOWN_INTR 0
23 #define RESET_DELAY 30
24 #define PENUP_TIMEOUT (10)
26 #define MASK_BITS 0x03
29 #define LENGTH_OF_BUFFER 11
30 #define I2C_RETRY_COUNT 5
32 #define BU21013_SENSORS_BTN_0_7_REG 0x70
33 #define BU21013_SENSORS_BTN_8_15_REG 0x71
34 #define BU21013_SENSORS_BTN_16_23_REG 0x72
35 #define BU21013_X1_POS_MSB_REG 0x73
36 #define BU21013_X1_POS_LSB_REG 0x74
37 #define BU21013_Y1_POS_MSB_REG 0x75
38 #define BU21013_Y1_POS_LSB_REG 0x76
39 #define BU21013_X2_POS_MSB_REG 0x77
40 #define BU21013_X2_POS_LSB_REG 0x78
41 #define BU21013_Y2_POS_MSB_REG 0x79
42 #define BU21013_Y2_POS_LSB_REG 0x7A
43 #define BU21013_INT_CLR_REG 0xE8
44 #define BU21013_INT_MODE_REG 0xE9
45 #define BU21013_GAIN_REG 0xEA
46 #define BU21013_OFFSET_MODE_REG 0xEB
47 #define BU21013_XY_EDGE_REG 0xEC
48 #define BU21013_RESET_REG 0xED
49 #define BU21013_CALIB_REG 0xEE
50 #define BU21013_DONE_REG 0xEF
51 #define BU21013_SENSOR_0_7_REG 0xF0
52 #define BU21013_SENSOR_8_15_REG 0xF1
53 #define BU21013_SENSOR_16_23_REG 0xF2
54 #define BU21013_POS_MODE1_REG 0xF3
55 #define BU21013_POS_MODE2_REG 0xF4
56 #define BU21013_CLK_MODE_REG 0xF5
57 #define BU21013_IDLE_REG 0xFA
58 #define BU21013_FILTER_REG 0xFB
59 #define BU21013_TH_ON_REG 0xFC
60 #define BU21013_TH_OFF_REG 0xFD
63 #define BU21013_RESET_ENABLE 0x01
65 #define BU21013_SENSORS_EN_0_7 0x3F
66 #define BU21013_SENSORS_EN_8_15 0xFC
67 #define BU21013_SENSORS_EN_16_23 0x1F
69 #define BU21013_POS_MODE1_0 0x02
70 #define BU21013_POS_MODE1_1 0x04
71 #define BU21013_POS_MODE1_2 0x08
73 #define BU21013_POS_MODE2_ZERO 0x01
74 #define BU21013_POS_MODE2_AVG1 0x02
75 #define BU21013_POS_MODE2_AVG2 0x04
76 #define BU21013_POS_MODE2_EN_XY 0x08
77 #define BU21013_POS_MODE2_EN_RAW 0x10
78 #define BU21013_POS_MODE2_MULTI 0x80
80 #define BU21013_CLK_MODE_DIV 0x01
81 #define BU21013_CLK_MODE_EXT 0x02
82 #define BU21013_CLK_MODE_CALIB 0x80
84 #define BU21013_IDLET_0 0x01
85 #define BU21013_IDLET_1 0x02
86 #define BU21013_IDLET_2 0x04
87 #define BU21013_IDLET_3 0x08
88 #define BU21013_IDLE_INTERMIT_EN 0x10
90 #define BU21013_DELTA_0_6 0x7F
91 #define BU21013_FILTER_EN 0x80
93 #define BU21013_INT_MODE_LEVEL 0x00
94 #define BU21013_INT_MODE_EDGE 0x01
96 #define BU21013_GAIN_0 0x01
97 #define BU21013_GAIN_1 0x02
98 #define BU21013_GAIN_2 0x04
100 #define BU21013_OFFSET_MODE_DEFAULT 0x00
101 #define BU21013_OFFSET_MODE_MOVE 0x01
102 #define BU21013_OFFSET_MODE_DISABLE 0x02
104 #define BU21013_TH_ON_0 0x01
105 #define BU21013_TH_ON_1 0x02
106 #define BU21013_TH_ON_2 0x04
107 #define BU21013_TH_ON_3 0x08
108 #define BU21013_TH_ON_4 0x10
109 #define BU21013_TH_ON_5 0x20
110 #define BU21013_TH_ON_6 0x40
111 #define BU21013_TH_ON_7 0x80
112 #define BU21013_TH_ON_MAX 0xFF
114 #define BU21013_TH_OFF_0 0x01
115 #define BU21013_TH_OFF_1 0x02
116 #define BU21013_TH_OFF_2 0x04
117 #define BU21013_TH_OFF_3 0x08
118 #define BU21013_TH_OFF_4 0x10
119 #define BU21013_TH_OFF_5 0x20
120 #define BU21013_TH_OFF_6 0x40
121 #define BU21013_TH_OFF_7 0x80
122 #define BU21013_TH_OFF_MAX 0xFF
124 #define BU21013_X_EDGE_0 0x01
125 #define BU21013_X_EDGE_1 0x02
126 #define BU21013_X_EDGE_2 0x04
127 #define BU21013_X_EDGE_3 0x08
128 #define BU21013_Y_EDGE_0 0x10
129 #define BU21013_Y_EDGE_1 0x20
130 #define BU21013_Y_EDGE_2 0x40
131 #define BU21013_Y_EDGE_3 0x80
133 #define BU21013_DONE 0x01
134 #define BU21013_NUMBER_OF_X_SENSORS (6)
135 #define BU21013_NUMBER_OF_Y_SENSORS (11)
137 #define DRIVER_TP "bu21013_tp"
140 * struct bu21013_ts_data - touch panel data structure
141 * @client: pointer to the i2c client
142 * @wait: variable to wait_queue_head_t structure
143 * @touch_stopped: touch stop flag
144 * @chip: pointer to the touch panel controller
145 * @in_dev: pointer to the input device structure
146 * @intr_pin: interrupt pin value
147 * @regulator: pointer to the Regulator used for touch screen
149 * Touch panel device data structure
151 struct bu21013_ts_data {
152 struct i2c_client *client;
153 wait_queue_head_t wait;
154 const struct bu21013_platform_device *chip;
155 struct input_dev *in_dev;
156 struct regulator *regulator;
158 unsigned int intr_pin;
163 * bu21013_read_block_data(): read the touch co-ordinates
164 * @data: bu21013_ts_data structure pointer
167 * Read the touch co-ordinates using i2c read block into buffer
168 * and returns integer.
170 static int bu21013_read_block_data(struct bu21013_ts_data *data, u8 *buf)
174 for (i = 0; i < I2C_RETRY_COUNT; i++) {
175 ret = i2c_smbus_read_i2c_block_data
176 (data->client, BU21013_SENSORS_BTN_0_7_REG,
177 LENGTH_OF_BUFFER, buf);
178 if (ret == LENGTH_OF_BUFFER)
185 * bu21013_do_touch_report(): Get the touch co-ordinates
186 * @data: bu21013_ts_data structure pointer
188 * Get the touch co-ordinates from touch sensor registers and writes
189 * into device structure and returns integer.
191 static int bu21013_do_touch_report(struct bu21013_ts_data *data)
193 u8 buf[LENGTH_OF_BUFFER];
194 unsigned int pos_x[2], pos_y[2];
195 bool has_x_sensors, has_y_sensors;
196 int finger_down_count = 0;
202 if (bu21013_read_block_data(data, buf) < 0)
205 has_x_sensors = hweight32(buf[0] & BU21013_SENSORS_EN_0_7);
206 has_y_sensors = hweight32(((buf[1] & BU21013_SENSORS_EN_8_15) |
207 ((buf[2] & BU21013_SENSORS_EN_16_23) << SHIFT_8)) >> SHIFT_2);
208 if (!has_x_sensors || !has_y_sensors)
211 for (i = 0; i < MAX_FINGERS; i++) {
212 const u8 *p = &buf[4 * i + 3];
213 unsigned int x = p[0] << SHIFT_2 | (p[1] & MASK_BITS);
214 unsigned int y = p[2] << SHIFT_2 | (p[3] & MASK_BITS);
215 if (x == 0 || y == 0)
217 pos_x[finger_down_count] = x;
218 pos_y[finger_down_count] = y;
222 if (finger_down_count) {
223 if (finger_down_count == 2 &&
224 (abs(pos_x[0] - pos_x[1]) < DELTA_MIN ||
225 abs(pos_y[0] - pos_y[1]) < DELTA_MIN)) {
229 for (i = 0; i < finger_down_count; i++) {
230 if (data->chip->x_flip)
231 pos_x[i] = data->chip->touch_x_max - pos_x[i];
232 if (data->chip->y_flip)
233 pos_y[i] = data->chip->touch_y_max - pos_y[i];
235 input_report_abs(data->in_dev,
236 ABS_MT_POSITION_X, pos_x[i]);
237 input_report_abs(data->in_dev,
238 ABS_MT_POSITION_Y, pos_y[i]);
239 input_mt_sync(data->in_dev);
242 input_mt_sync(data->in_dev);
244 input_sync(data->in_dev);
249 * bu21013_gpio_irq() - gpio thread function for touch interrupt
251 * @device_data: void pointer
253 * This gpio thread function for touch interrupt
254 * and returns irqreturn_t.
256 static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
258 struct bu21013_ts_data *data = device_data;
259 struct i2c_client *i2c = data->client;
263 retval = bu21013_do_touch_report(data);
265 dev_err(&i2c->dev, "bu21013_do_touch_report failed\n");
269 data->intr_pin = gpio_get_value(data->chip->touch_pin);
270 if (data->intr_pin == PEN_DOWN_INTR)
271 wait_event_timeout(data->wait, data->touch_stopped,
272 msecs_to_jiffies(2));
273 } while (!data->intr_pin && !data->touch_stopped);
279 * bu21013_init_chip() - power on sequence for the bu21013 controller
280 * @data: device structure pointer
282 * This function is used to power on
283 * the bu21013 controller and returns integer.
285 static int bu21013_init_chip(struct bu21013_ts_data *data)
288 struct i2c_client *i2c = data->client;
290 retval = i2c_smbus_write_byte_data(i2c, BU21013_RESET_REG,
291 BU21013_RESET_ENABLE);
293 dev_err(&i2c->dev, "BU21013_RESET reg write failed\n");
298 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_0_7_REG,
299 BU21013_SENSORS_EN_0_7);
301 dev_err(&i2c->dev, "BU21013_SENSOR_0_7 reg write failed\n");
305 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_8_15_REG,
306 BU21013_SENSORS_EN_8_15);
308 dev_err(&i2c->dev, "BU21013_SENSOR_8_15 reg write failed\n");
312 retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_16_23_REG,
313 BU21013_SENSORS_EN_16_23);
315 dev_err(&i2c->dev, "BU21013_SENSOR_16_23 reg write failed\n");
319 retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE1_REG,
320 (BU21013_POS_MODE1_0 | BU21013_POS_MODE1_1));
322 dev_err(&i2c->dev, "BU21013_POS_MODE1 reg write failed\n");
326 retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE2_REG,
327 (BU21013_POS_MODE2_ZERO | BU21013_POS_MODE2_AVG1 |
328 BU21013_POS_MODE2_AVG2 | BU21013_POS_MODE2_EN_RAW |
329 BU21013_POS_MODE2_MULTI));
331 dev_err(&i2c->dev, "BU21013_POS_MODE2 reg write failed\n");
335 if (data->chip->ext_clk)
336 retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
337 (BU21013_CLK_MODE_EXT | BU21013_CLK_MODE_CALIB));
339 retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
340 (BU21013_CLK_MODE_DIV | BU21013_CLK_MODE_CALIB));
342 dev_err(&i2c->dev, "BU21013_CLK_MODE reg write failed\n");
346 retval = i2c_smbus_write_byte_data(i2c, BU21013_IDLE_REG,
347 (BU21013_IDLET_0 | BU21013_IDLE_INTERMIT_EN));
349 dev_err(&i2c->dev, "BU21013_IDLE reg write failed\n");
353 retval = i2c_smbus_write_byte_data(i2c, BU21013_INT_MODE_REG,
354 BU21013_INT_MODE_LEVEL);
356 dev_err(&i2c->dev, "BU21013_INT_MODE reg write failed\n");
360 retval = i2c_smbus_write_byte_data(i2c, BU21013_FILTER_REG,
364 dev_err(&i2c->dev, "BU21013_FILTER reg write failed\n");
368 retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_ON_REG,
371 dev_err(&i2c->dev, "BU21013_TH_ON reg write failed\n");
375 retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_OFF_REG,
376 BU21013_TH_OFF_4 | BU21013_TH_OFF_3);
378 dev_err(&i2c->dev, "BU21013_TH_OFF reg write failed\n");
382 retval = i2c_smbus_write_byte_data(i2c, BU21013_GAIN_REG,
383 (BU21013_GAIN_0 | BU21013_GAIN_1));
385 dev_err(&i2c->dev, "BU21013_GAIN reg write failed\n");
389 retval = i2c_smbus_write_byte_data(i2c, BU21013_OFFSET_MODE_REG,
390 BU21013_OFFSET_MODE_DEFAULT);
392 dev_err(&i2c->dev, "BU21013_OFFSET_MODE reg write failed\n");
396 retval = i2c_smbus_write_byte_data(i2c, BU21013_XY_EDGE_REG,
397 (BU21013_X_EDGE_0 | BU21013_X_EDGE_2 |
398 BU21013_Y_EDGE_1 | BU21013_Y_EDGE_3));
400 dev_err(&i2c->dev, "BU21013_XY_EDGE reg write failed\n");
404 retval = i2c_smbus_write_byte_data(i2c, BU21013_DONE_REG,
407 dev_err(&i2c->dev, "BU21013_REG_DONE reg write failed\n");
415 * bu21013_free_irq() - frees IRQ registered for touchscreen
416 * @bu21013_data: device structure pointer
418 * This function signals interrupt thread to stop processing and
421 static void bu21013_free_irq(struct bu21013_ts_data *bu21013_data)
423 bu21013_data->touch_stopped = true;
424 wake_up(&bu21013_data->wait);
425 free_irq(bu21013_data->irq, bu21013_data);
429 * bu21013_cs_disable() - deconfigures the touch panel controller
430 * @bu21013_data: device structure pointer
432 * This function is used to deconfigure the chip selection
433 * for touch panel controller.
435 static void bu21013_cs_disable(struct bu21013_ts_data *bu21013_data)
439 error = gpio_direction_output(bu21013_data->chip->cs_pin, 0);
441 dev_warn(&bu21013_data->client->dev,
442 "%s: gpio direction failed, error: %d\n",
445 gpio_set_value(bu21013_data->chip->cs_pin, 0);
447 gpio_free(bu21013_data->chip->cs_pin);
451 static const struct bu21013_platform_device *
452 bu21013_parse_dt(struct device *dev)
454 struct device_node *np = dev->of_node;
455 struct bu21013_platform_device *pdata;
458 dev_err(dev, "no device tree or platform data\n");
459 return ERR_PTR(-EINVAL);
462 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
464 return ERR_PTR(-ENOMEM);
466 pdata->y_flip = pdata->x_flip = false;
468 pdata->x_flip = of_property_read_bool(np, "rohm,flip-x");
469 pdata->y_flip = of_property_read_bool(np, "rohm,flip-y");
471 of_property_read_u32(np, "rohm,touch-max-x", &pdata->touch_x_max);
472 of_property_read_u32(np, "rohm,touch-max-y", &pdata->touch_y_max);
474 pdata->touch_pin = of_get_named_gpio(np, "touch-gpio", 0);
475 pdata->cs_pin = of_get_named_gpio(np, "reset-gpio", 0);
477 pdata->ext_clk = false;
482 static inline const struct bu21013_platform_device *
483 bu21013_parse_dt(struct device *dev)
485 dev_err(dev, "no platform data available\n");
486 return ERR_PTR(-EINVAL);
491 * bu21013_probe() - initializes the i2c-client touchscreen driver
492 * @client: i2c client structure pointer
493 * @id: i2c device id pointer
495 * This function used to initializes the i2c-client touchscreen
496 * driver and returns integer.
498 static int bu21013_probe(struct i2c_client *client,
499 const struct i2c_device_id *id)
501 const struct bu21013_platform_device *pdata =
502 dev_get_platdata(&client->dev);
503 struct bu21013_ts_data *bu21013_data;
504 struct input_dev *in_dev;
507 if (!i2c_check_functionality(client->adapter,
508 I2C_FUNC_SMBUS_BYTE_DATA)) {
509 dev_err(&client->dev, "i2c smbus byte data not supported\n");
514 pdata = bu21013_parse_dt(&client->dev);
516 return PTR_ERR(pdata);
519 if (!gpio_is_valid(pdata->touch_pin)) {
520 dev_err(&client->dev, "invalid touch_pin supplied\n");
524 bu21013_data = kzalloc(sizeof(struct bu21013_ts_data), GFP_KERNEL);
525 in_dev = input_allocate_device();
526 if (!bu21013_data || !in_dev) {
527 dev_err(&client->dev, "device memory alloc failed\n");
532 bu21013_data->in_dev = in_dev;
533 bu21013_data->chip = pdata;
534 bu21013_data->client = client;
535 bu21013_data->irq = gpio_to_irq(pdata->touch_pin);
537 bu21013_data->regulator = regulator_get(&client->dev, "avdd");
538 if (IS_ERR(bu21013_data->regulator)) {
539 dev_err(&client->dev, "regulator_get failed\n");
540 error = PTR_ERR(bu21013_data->regulator);
544 error = regulator_enable(bu21013_data->regulator);
546 dev_err(&client->dev, "regulator enable failed\n");
547 goto err_put_regulator;
550 bu21013_data->touch_stopped = false;
551 init_waitqueue_head(&bu21013_data->wait);
553 /* configure the gpio pins */
554 error = gpio_request_one(pdata->cs_pin, GPIOF_OUT_INIT_HIGH,
557 dev_err(&client->dev, "Unable to request gpio reset_pin\n");
558 goto err_disable_regulator;
561 /* configure the touch panel controller */
562 error = bu21013_init_chip(bu21013_data);
564 dev_err(&client->dev, "error in bu21013 config\n");
568 /* register the device to input subsystem */
569 in_dev->name = DRIVER_TP;
570 in_dev->id.bustype = BUS_I2C;
571 in_dev->dev.parent = &client->dev;
573 __set_bit(EV_SYN, in_dev->evbit);
574 __set_bit(EV_KEY, in_dev->evbit);
575 __set_bit(EV_ABS, in_dev->evbit);
577 input_set_abs_params(in_dev, ABS_MT_POSITION_X, 0,
578 pdata->touch_x_max, 0, 0);
579 input_set_abs_params(in_dev, ABS_MT_POSITION_Y, 0,
580 pdata->touch_y_max, 0, 0);
581 input_set_drvdata(in_dev, bu21013_data);
583 error = request_threaded_irq(bu21013_data->irq, NULL, bu21013_gpio_irq,
584 IRQF_TRIGGER_FALLING | IRQF_SHARED |
586 DRIVER_TP, bu21013_data);
588 dev_err(&client->dev, "request irq %d failed\n",
593 error = input_register_device(in_dev);
595 dev_err(&client->dev, "failed to register input device\n");
599 device_init_wakeup(&client->dev, pdata->wakeup);
600 i2c_set_clientdata(client, bu21013_data);
605 bu21013_free_irq(bu21013_data);
607 bu21013_cs_disable(bu21013_data);
608 err_disable_regulator:
609 regulator_disable(bu21013_data->regulator);
611 regulator_put(bu21013_data->regulator);
613 input_free_device(in_dev);
619 * bu21013_remove() - removes the i2c-client touchscreen driver
620 * @client: i2c client structure pointer
622 * This function uses to remove the i2c-client
623 * touchscreen driver and returns integer.
625 static int bu21013_remove(struct i2c_client *client)
627 struct bu21013_ts_data *bu21013_data = i2c_get_clientdata(client);
629 bu21013_free_irq(bu21013_data);
631 bu21013_cs_disable(bu21013_data);
633 input_unregister_device(bu21013_data->in_dev);
635 regulator_disable(bu21013_data->regulator);
636 regulator_put(bu21013_data->regulator);
640 device_init_wakeup(&client->dev, false);
647 * bu21013_suspend() - suspend the touch screen controller
648 * @dev: pointer to device structure
650 * This function is used to suspend the
651 * touch panel controller and returns integer
653 static int bu21013_suspend(struct device *dev)
655 struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
656 struct i2c_client *client = bu21013_data->client;
658 bu21013_data->touch_stopped = true;
659 if (device_may_wakeup(&client->dev))
660 enable_irq_wake(bu21013_data->irq);
662 disable_irq(bu21013_data->irq);
664 regulator_disable(bu21013_data->regulator);
670 * bu21013_resume() - resume the touch screen controller
671 * @dev: pointer to device structure
673 * This function is used to resume the touch panel
674 * controller and returns integer.
676 static int bu21013_resume(struct device *dev)
678 struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
679 struct i2c_client *client = bu21013_data->client;
682 retval = regulator_enable(bu21013_data->regulator);
684 dev_err(&client->dev, "bu21013 regulator enable failed\n");
688 retval = bu21013_init_chip(bu21013_data);
690 dev_err(&client->dev, "bu21013 controller config failed\n");
694 bu21013_data->touch_stopped = false;
696 if (device_may_wakeup(&client->dev))
697 disable_irq_wake(bu21013_data->irq);
699 enable_irq(bu21013_data->irq);
704 static const struct dev_pm_ops bu21013_dev_pm_ops = {
705 .suspend = bu21013_suspend,
706 .resume = bu21013_resume,
710 static const struct i2c_device_id bu21013_id[] = {
714 MODULE_DEVICE_TABLE(i2c, bu21013_id);
716 static struct i2c_driver bu21013_driver = {
720 .pm = &bu21013_dev_pm_ops,
723 .probe = bu21013_probe,
724 .remove = bu21013_remove,
725 .id_table = bu21013_id,
728 module_i2c_driver(bu21013_driver);
730 MODULE_LICENSE("GPL v2");
731 MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
732 MODULE_DESCRIPTION("bu21013 touch screen controller driver");