1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
4 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
7 #include <linux/bitops.h>
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/input.h>
12 #include <linux/input/mt.h>
13 #include <linux/input/touchscreen.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/property.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
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 - touch panel data structure
141 * @client: pointer to the i2c client
142 * @in_dev: pointer to the input device structure
143 * @props: the device coordinate transformation properties
144 * @regulator: pointer to the Regulator used for touch screen
145 * @cs_gpiod: chip select GPIO line
146 * @int_gpiod: touch interrupt GPIO line
147 * @touch_x_max: maximum X coordinate reported by the device
148 * @touch_y_max: maximum Y coordinate reported by the device
149 * @x_flip: indicates that the driver should invert X coordinate before
151 * @y_flip: indicates that the driver should invert Y coordinate before
153 * @touch_stopped: touch stop flag
155 * Touch panel device data structure
158 struct i2c_client
*client
;
159 struct input_dev
*in_dev
;
160 struct touchscreen_properties props
;
161 struct regulator
*regulator
;
162 struct gpio_desc
*cs_gpiod
;
163 struct gpio_desc
*int_gpiod
;
171 static int bu21013_read_block_data(struct bu21013_ts
*ts
, u8
*buf
)
175 for (i
= 0; i
< I2C_RETRY_COUNT
; i
++) {
176 ret
= i2c_smbus_read_i2c_block_data(ts
->client
,
177 BU21013_SENSORS_BTN_0_7_REG
,
178 LENGTH_OF_BUFFER
, buf
);
179 if (ret
== LENGTH_OF_BUFFER
)
186 static int bu21013_do_touch_report(struct bu21013_ts
*ts
)
188 struct input_dev
*input
= ts
->in_dev
;
189 struct input_mt_pos pos
[MAX_FINGERS
];
190 int slots
[MAX_FINGERS
];
191 u8 buf
[LENGTH_OF_BUFFER
];
192 bool has_x_sensors
, has_y_sensors
;
193 int finger_down_count
= 0;
196 if (bu21013_read_block_data(ts
, buf
) < 0)
199 has_x_sensors
= hweight32(buf
[0] & BU21013_SENSORS_EN_0_7
);
200 has_y_sensors
= hweight32(((buf
[1] & BU21013_SENSORS_EN_8_15
) |
201 ((buf
[2] & BU21013_SENSORS_EN_16_23
) << SHIFT_8
)) >> SHIFT_2
);
202 if (!has_x_sensors
|| !has_y_sensors
)
205 for (i
= 0; i
< MAX_FINGERS
; i
++) {
206 const u8
*data
= &buf
[4 * i
+ 3];
209 x
= data
[0] << SHIFT_2
| (data
[1] & MASK_BITS
);
210 y
= data
[2] << SHIFT_2
| (data
[3] & MASK_BITS
);
211 if (x
!= 0 && y
!= 0)
212 touchscreen_set_mt_pos(&pos
[finger_down_count
++],
216 if (finger_down_count
== 2 &&
217 (abs(pos
[0].x
- pos
[1].x
) < DELTA_MIN
||
218 abs(pos
[0].y
- pos
[1].y
) < DELTA_MIN
)) {
222 input_mt_assign_slots(input
, slots
, pos
, finger_down_count
, DELTA_MIN
);
223 for (i
= 0; i
< finger_down_count
; i
++) {
224 input_mt_slot(input
, slots
[i
]);
225 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
226 input_report_abs(input
, ABS_MT_POSITION_X
, pos
[i
].x
);
227 input_report_abs(input
, ABS_MT_POSITION_Y
, pos
[i
].y
);
230 input_mt_sync_frame(input
);
236 static irqreturn_t
bu21013_gpio_irq(int irq
, void *device_data
)
238 struct bu21013_ts
*ts
= device_data
;
243 error
= bu21013_do_touch_report(ts
);
245 dev_err(&ts
->client
->dev
, "%s failed\n", __func__
);
249 if (unlikely(ts
->touch_stopped
))
252 keep_polling
= ts
->int_gpiod
?
253 gpiod_get_value(ts
->int_gpiod
) : false;
255 usleep_range(2000, 2500);
256 } while (keep_polling
);
261 static int bu21013_init_chip(struct bu21013_ts
*ts
)
263 struct i2c_client
*client
= ts
->client
;
266 error
= i2c_smbus_write_byte_data(client
, BU21013_RESET_REG
,
267 BU21013_RESET_ENABLE
);
269 dev_err(&client
->dev
, "BU21013_RESET reg write failed\n");
274 error
= i2c_smbus_write_byte_data(client
, BU21013_SENSOR_0_7_REG
,
275 BU21013_SENSORS_EN_0_7
);
277 dev_err(&client
->dev
, "BU21013_SENSOR_0_7 reg write failed\n");
281 error
= i2c_smbus_write_byte_data(client
, BU21013_SENSOR_8_15_REG
,
282 BU21013_SENSORS_EN_8_15
);
284 dev_err(&client
->dev
, "BU21013_SENSOR_8_15 reg write failed\n");
288 error
= i2c_smbus_write_byte_data(client
, BU21013_SENSOR_16_23_REG
,
289 BU21013_SENSORS_EN_16_23
);
291 dev_err(&client
->dev
, "BU21013_SENSOR_16_23 reg write failed\n");
295 error
= i2c_smbus_write_byte_data(client
, BU21013_POS_MODE1_REG
,
296 BU21013_POS_MODE1_0
|
297 BU21013_POS_MODE1_1
);
299 dev_err(&client
->dev
, "BU21013_POS_MODE1 reg write failed\n");
303 error
= i2c_smbus_write_byte_data(client
, BU21013_POS_MODE2_REG
,
304 BU21013_POS_MODE2_ZERO
|
305 BU21013_POS_MODE2_AVG1
|
306 BU21013_POS_MODE2_AVG2
|
307 BU21013_POS_MODE2_EN_RAW
|
308 BU21013_POS_MODE2_MULTI
);
310 dev_err(&client
->dev
, "BU21013_POS_MODE2 reg write failed\n");
314 error
= i2c_smbus_write_byte_data(client
, BU21013_CLK_MODE_REG
,
315 BU21013_CLK_MODE_DIV
|
316 BU21013_CLK_MODE_CALIB
);
318 dev_err(&client
->dev
, "BU21013_CLK_MODE reg write failed\n");
322 error
= i2c_smbus_write_byte_data(client
, BU21013_IDLE_REG
,
324 BU21013_IDLE_INTERMIT_EN
);
326 dev_err(&client
->dev
, "BU21013_IDLE reg write failed\n");
330 error
= i2c_smbus_write_byte_data(client
, BU21013_INT_MODE_REG
,
331 BU21013_INT_MODE_LEVEL
);
333 dev_err(&client
->dev
, "BU21013_INT_MODE reg write failed\n");
337 error
= i2c_smbus_write_byte_data(client
, BU21013_FILTER_REG
,
341 dev_err(&client
->dev
, "BU21013_FILTER reg write failed\n");
345 error
= i2c_smbus_write_byte_data(client
, BU21013_TH_ON_REG
,
348 dev_err(&client
->dev
, "BU21013_TH_ON reg write failed\n");
352 error
= i2c_smbus_write_byte_data(client
, BU21013_TH_OFF_REG
,
353 BU21013_TH_OFF_4
| BU21013_TH_OFF_3
);
355 dev_err(&client
->dev
, "BU21013_TH_OFF reg write failed\n");
359 error
= i2c_smbus_write_byte_data(client
, BU21013_GAIN_REG
,
360 BU21013_GAIN_0
| BU21013_GAIN_1
);
362 dev_err(&client
->dev
, "BU21013_GAIN reg write failed\n");
366 error
= i2c_smbus_write_byte_data(client
, BU21013_OFFSET_MODE_REG
,
367 BU21013_OFFSET_MODE_DEFAULT
);
369 dev_err(&client
->dev
, "BU21013_OFFSET_MODE reg write failed\n");
373 error
= i2c_smbus_write_byte_data(client
, BU21013_XY_EDGE_REG
,
379 dev_err(&client
->dev
, "BU21013_XY_EDGE reg write failed\n");
383 error
= i2c_smbus_write_byte_data(client
, BU21013_DONE_REG
,
386 dev_err(&client
->dev
, "BU21013_REG_DONE reg write failed\n");
393 static void bu21013_power_off(void *_ts
)
395 struct bu21013_ts
*ts
= _ts
;
397 regulator_disable(ts
->regulator
);
400 static void bu21013_disable_chip(void *_ts
)
402 struct bu21013_ts
*ts
= _ts
;
404 gpiod_set_value(ts
->cs_gpiod
, 0);
407 static int bu21013_probe(struct i2c_client
*client
,
408 const struct i2c_device_id
*id
)
410 struct bu21013_ts
*ts
;
411 struct input_dev
*in_dev
;
412 struct input_absinfo
*info
;
413 u32 max_x
= 0, max_y
= 0;
416 if (!i2c_check_functionality(client
->adapter
,
417 I2C_FUNC_SMBUS_BYTE_DATA
)) {
418 dev_err(&client
->dev
, "i2c smbus byte data not supported\n");
423 dev_err(&client
->dev
, "No IRQ set up\n");
427 ts
= devm_kzalloc(&client
->dev
, sizeof(*ts
), GFP_KERNEL
);
433 ts
->x_flip
= device_property_read_bool(&client
->dev
, "rohm,flip-x");
434 ts
->y_flip
= device_property_read_bool(&client
->dev
, "rohm,flip-y");
436 in_dev
= devm_input_allocate_device(&client
->dev
);
438 dev_err(&client
->dev
, "device memory alloc failed\n");
442 input_set_drvdata(in_dev
, ts
);
444 /* register the device to input subsystem */
445 in_dev
->name
= DRIVER_TP
;
446 in_dev
->id
.bustype
= BUS_I2C
;
448 device_property_read_u32(&client
->dev
, "rohm,touch-max-x", &max_x
);
449 device_property_read_u32(&client
->dev
, "rohm,touch-max-y", &max_y
);
451 input_set_abs_params(in_dev
, ABS_MT_POSITION_X
, 0, max_x
, 0, 0);
452 input_set_abs_params(in_dev
, ABS_MT_POSITION_Y
, 0, max_y
, 0, 0);
454 touchscreen_parse_properties(in_dev
, true, &ts
->props
);
456 /* Adjust for the legacy "flip" properties, if present */
457 if (!ts
->props
.invert_x
&&
458 device_property_read_bool(&client
->dev
, "rohm,flip-x")) {
459 info
= &in_dev
->absinfo
[ABS_MT_POSITION_X
];
460 info
->maximum
-= info
->minimum
;
464 if (!ts
->props
.invert_y
&&
465 device_property_read_bool(&client
->dev
, "rohm,flip-y")) {
466 info
= &in_dev
->absinfo
[ABS_MT_POSITION_Y
];
467 info
->maximum
-= info
->minimum
;
471 error
= input_mt_init_slots(in_dev
, MAX_FINGERS
,
472 INPUT_MT_DIRECT
| INPUT_MT_TRACK
|
473 INPUT_MT_DROP_UNUSED
);
475 dev_err(&client
->dev
, "failed to initialize MT slots");
479 ts
->regulator
= devm_regulator_get(&client
->dev
, "avdd");
480 if (IS_ERR(ts
->regulator
)) {
481 dev_err(&client
->dev
, "regulator_get failed\n");
482 return PTR_ERR(ts
->regulator
);
485 error
= regulator_enable(ts
->regulator
);
487 dev_err(&client
->dev
, "regulator enable failed\n");
491 error
= devm_add_action_or_reset(&client
->dev
, bu21013_power_off
, ts
);
493 dev_err(&client
->dev
, "failed to install power off handler\n");
497 /* Named "CS" on the chip, DT binding is "reset" */
498 ts
->cs_gpiod
= devm_gpiod_get(&client
->dev
, "reset", GPIOD_OUT_HIGH
);
499 error
= PTR_ERR_OR_ZERO(ts
->cs_gpiod
);
501 if (error
!= -EPROBE_DEFER
)
502 dev_err(&client
->dev
, "failed to get CS GPIO\n");
505 gpiod_set_consumer_name(ts
->cs_gpiod
, "BU21013 CS");
507 error
= devm_add_action_or_reset(&client
->dev
,
508 bu21013_disable_chip
, ts
);
510 dev_err(&client
->dev
,
511 "failed to install chip disable handler\n");
515 /* Named "INT" on the chip, DT binding is "touch" */
516 ts
->int_gpiod
= devm_gpiod_get_optional(&client
->dev
,
518 error
= PTR_ERR_OR_ZERO(ts
->int_gpiod
);
520 if (error
!= -EPROBE_DEFER
)
521 dev_err(&client
->dev
, "failed to get INT GPIO\n");
526 gpiod_set_consumer_name(ts
->int_gpiod
, "BU21013 INT");
528 /* configure the touch panel controller */
529 error
= bu21013_init_chip(ts
);
531 dev_err(&client
->dev
, "error in bu21013 config\n");
535 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
536 NULL
, bu21013_gpio_irq
,
537 IRQF_ONESHOT
, DRIVER_TP
, ts
);
539 dev_err(&client
->dev
, "request irq %d failed\n",
544 error
= input_register_device(in_dev
);
546 dev_err(&client
->dev
, "failed to register input device\n");
550 i2c_set_clientdata(client
, ts
);
555 static int bu21013_remove(struct i2c_client
*client
)
557 struct bu21013_ts
*ts
= i2c_get_clientdata(client
);
559 /* Make sure IRQ will exit quickly even if there is contact */
560 ts
->touch_stopped
= true;
561 /* The resources will be freed by devm */
566 static int __maybe_unused
bu21013_suspend(struct device
*dev
)
568 struct i2c_client
*client
= to_i2c_client(dev
);
569 struct bu21013_ts
*ts
= i2c_get_clientdata(client
);
571 ts
->touch_stopped
= true;
573 disable_irq(client
->irq
);
575 if (!device_may_wakeup(&client
->dev
))
576 regulator_disable(ts
->regulator
);
581 static int __maybe_unused
bu21013_resume(struct device
*dev
)
583 struct i2c_client
*client
= to_i2c_client(dev
);
584 struct bu21013_ts
*ts
= i2c_get_clientdata(client
);
587 if (!device_may_wakeup(&client
->dev
)) {
588 error
= regulator_enable(ts
->regulator
);
590 dev_err(&client
->dev
,
591 "failed to re-enable regulator when resuming\n");
595 error
= bu21013_init_chip(ts
);
597 dev_err(&client
->dev
,
598 "failed to reinitialize chip when resuming\n");
603 ts
->touch_stopped
= false;
605 enable_irq(client
->irq
);
610 static SIMPLE_DEV_PM_OPS(bu21013_dev_pm_ops
, bu21013_suspend
, bu21013_resume
);
612 static const struct i2c_device_id bu21013_id
[] = {
616 MODULE_DEVICE_TABLE(i2c
, bu21013_id
);
618 static struct i2c_driver bu21013_driver
= {
621 .pm
= &bu21013_dev_pm_ops
,
623 .probe
= bu21013_probe
,
624 .remove
= bu21013_remove
,
625 .id_table
= bu21013_id
,
628 module_i2c_driver(bu21013_driver
);
630 MODULE_LICENSE("GPL v2");
631 MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
632 MODULE_DESCRIPTION("bu21013 touch screen controller driver");