2 * Driver for ELAN eKTF2127 i2c touchscreen controller
4 * For this driver the layout of the Chipone icn8318 i2c
5 * touchscreencontroller is used.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 * Michel Verlaan <michel.verl@gmail.com>
14 * Siebren Vroegindeweij <siebren.vroegindeweij@hotmail.com>
16 * Original chipone_icn8318 driver:
17 * Hans de Goede <hdegoede@redhat.com>
20 #include <linux/gpio/consumer.h>
21 #include <linux/interrupt.h>
22 #include <linux/i2c.h>
23 #include <linux/input.h>
24 #include <linux/input/mt.h>
25 #include <linux/input/touchscreen.h>
26 #include <linux/module.h>
28 #include <linux/delay.h>
30 /* Packet header defines (first byte of data send / received) */
31 #define EKTF2127_NOISE 0x40
32 #define EKTF2127_RESPONSE 0x52
33 #define EKTF2127_REQUEST 0x53
34 #define EKTF2127_HELLO 0x55
35 #define EKTF2127_REPORT 0x5d
36 #define EKTF2127_CALIB_DONE 0x66
38 /* Register defines (second byte of data send / received) */
39 #define EKTF2127_ENV_NOISY 0x41
40 #define EKTF2127_HEIGHT 0x60
41 #define EKTF2127_WIDTH 0x63
43 /* 2 bytes header + 5 * 3 bytes coordinates + 3 bytes pressure info + footer */
44 #define EKTF2127_TOUCH_REPORT_SIZE 21
45 #define EKTF2127_MAX_TOUCHES 5
48 struct i2c_client
*client
;
49 struct input_dev
*input
;
50 struct gpio_desc
*power_gpios
;
51 struct touchscreen_properties prop
;
54 static void ektf2127_parse_coordinates(const u8
*buf
, unsigned int touch_count
,
55 struct input_mt_pos
*touches
)
60 for (i
= 0; i
< touch_count
; i
++) {
63 touches
[i
].x
= (buf
[index
] & 0x0f);
65 touches
[i
].x
|= buf
[index
+ 2];
67 touches
[i
].y
= (buf
[index
] & 0xf0);
69 touches
[i
].y
|= buf
[index
+ 1];
73 static void ektf2127_report_event(struct ektf2127_ts
*ts
, const u8
*buf
)
75 struct input_mt_pos touches
[EKTF2127_MAX_TOUCHES
];
76 int slots
[EKTF2127_MAX_TOUCHES
];
77 unsigned int touch_count
, i
;
79 touch_count
= buf
[1] & 0x07;
80 if (touch_count
> EKTF2127_MAX_TOUCHES
) {
81 dev_err(&ts
->client
->dev
,
82 "Too many touches %d > %d\n",
83 touch_count
, EKTF2127_MAX_TOUCHES
);
84 touch_count
= EKTF2127_MAX_TOUCHES
;
87 ektf2127_parse_coordinates(buf
, touch_count
, touches
);
88 input_mt_assign_slots(ts
->input
, slots
, touches
,
91 for (i
= 0; i
< touch_count
; i
++) {
92 input_mt_slot(ts
->input
, slots
[i
]);
93 input_mt_report_slot_state(ts
->input
, MT_TOOL_FINGER
, true);
94 touchscreen_report_pos(ts
->input
, &ts
->prop
,
95 touches
[i
].x
, touches
[i
].y
, true);
98 input_mt_sync_frame(ts
->input
);
99 input_sync(ts
->input
);
102 static irqreturn_t
ektf2127_irq(int irq
, void *dev_id
)
104 struct ektf2127_ts
*ts
= dev_id
;
105 struct device
*dev
= &ts
->client
->dev
;
106 char buf
[EKTF2127_TOUCH_REPORT_SIZE
];
109 ret
= i2c_master_recv(ts
->client
, buf
, EKTF2127_TOUCH_REPORT_SIZE
);
110 if (ret
!= EKTF2127_TOUCH_REPORT_SIZE
) {
111 dev_err(dev
, "Error reading touch data: %d\n", ret
);
116 case EKTF2127_REPORT
:
117 ektf2127_report_event(ts
, buf
);
121 if (buf
[1] == EKTF2127_ENV_NOISY
)
122 dev_dbg(dev
, "Environment is electrically noisy\n");
126 case EKTF2127_CALIB_DONE
:
130 dev_err(dev
, "Unexpected packet header byte %#02x\n", buf
[0]);
138 static int ektf2127_start(struct input_dev
*dev
)
140 struct ektf2127_ts
*ts
= input_get_drvdata(dev
);
142 enable_irq(ts
->client
->irq
);
143 gpiod_set_value_cansleep(ts
->power_gpios
, 1);
148 static void ektf2127_stop(struct input_dev
*dev
)
150 struct ektf2127_ts
*ts
= input_get_drvdata(dev
);
152 disable_irq(ts
->client
->irq
);
153 gpiod_set_value_cansleep(ts
->power_gpios
, 0);
156 static int __maybe_unused
ektf2127_suspend(struct device
*dev
)
158 struct ektf2127_ts
*ts
= i2c_get_clientdata(to_i2c_client(dev
));
160 mutex_lock(&ts
->input
->mutex
);
161 if (ts
->input
->users
)
162 ektf2127_stop(ts
->input
);
163 mutex_unlock(&ts
->input
->mutex
);
168 static int __maybe_unused
ektf2127_resume(struct device
*dev
)
170 struct ektf2127_ts
*ts
= i2c_get_clientdata(to_i2c_client(dev
));
172 mutex_lock(&ts
->input
->mutex
);
173 if (ts
->input
->users
)
174 ektf2127_start(ts
->input
);
175 mutex_unlock(&ts
->input
->mutex
);
180 static SIMPLE_DEV_PM_OPS(ektf2127_pm_ops
, ektf2127_suspend
,
183 static int ektf2127_query_dimension(struct i2c_client
*client
, bool width
)
185 struct device
*dev
= &client
->dev
;
186 const char *what
= width
? "width" : "height";
187 u8 what_code
= width
? EKTF2127_WIDTH
: EKTF2127_HEIGHT
;
192 /* Request dimension */
193 buf
[0] = EKTF2127_REQUEST
;
194 buf
[1] = width
? EKTF2127_WIDTH
: EKTF2127_HEIGHT
;
197 ret
= i2c_master_send(client
, buf
, sizeof(buf
));
198 if (ret
!= sizeof(buf
)) {
199 error
= ret
< 0 ? ret
: -EIO
;
200 dev_err(dev
, "Failed to request %s: %d\n", what
, error
);
207 ret
= i2c_master_recv(client
, buf
, sizeof(buf
));
208 if (ret
!= sizeof(buf
)) {
209 error
= ret
< 0 ? ret
: -EIO
;
210 dev_err(dev
, "Failed to receive %s data: %d\n", what
, error
);
214 if (buf
[0] != EKTF2127_RESPONSE
|| buf
[1] != what_code
) {
215 dev_err(dev
, "Unexpected %s data: %#02x %#02x\n",
216 what
, buf
[0], buf
[1]);
220 return (((buf
[3] & 0xf0) << 4) | buf
[2]) - 1;
223 static int ektf2127_probe(struct i2c_client
*client
,
224 const struct i2c_device_id
*id
)
226 struct device
*dev
= &client
->dev
;
227 struct ektf2127_ts
*ts
;
228 struct input_dev
*input
;
234 dev_err(dev
, "Error no irq specified\n");
238 ts
= devm_kzalloc(dev
, sizeof(*ts
), GFP_KERNEL
);
242 /* This requests the gpio *and* turns on the touchscreen controller */
243 ts
->power_gpios
= devm_gpiod_get(dev
, "power", GPIOD_OUT_HIGH
);
244 if (IS_ERR(ts
->power_gpios
)) {
245 error
= PTR_ERR(ts
->power_gpios
);
246 if (error
!= -EPROBE_DEFER
)
247 dev_err(dev
, "Error getting power gpio: %d\n", error
);
251 input
= devm_input_allocate_device(dev
);
255 input
->name
= client
->name
;
256 input
->id
.bustype
= BUS_I2C
;
257 input
->open
= ektf2127_start
;
258 input
->close
= ektf2127_stop
;
262 /* Read hello (ignore result, depends on initial power state) */
264 i2c_master_recv(ts
->client
, buf
, sizeof(buf
));
266 /* Read resolution from chip */
267 max_x
= ektf2127_query_dimension(client
, true);
271 max_y
= ektf2127_query_dimension(client
, false);
275 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, max_x
, 0, 0);
276 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, max_y
, 0, 0);
277 touchscreen_parse_properties(input
, true, &ts
->prop
);
279 error
= input_mt_init_slots(input
, EKTF2127_MAX_TOUCHES
,
281 INPUT_MT_DROP_UNUSED
|
287 input_set_drvdata(input
, ts
);
289 error
= devm_request_threaded_irq(dev
, client
->irq
,
291 IRQF_ONESHOT
, client
->name
, ts
);
293 dev_err(dev
, "Error requesting irq: %d\n", error
);
297 /* Stop device till opened */
298 ektf2127_stop(ts
->input
);
300 error
= input_register_device(input
);
304 i2c_set_clientdata(client
, ts
);
310 static const struct of_device_id ektf2127_of_match
[] = {
311 { .compatible
= "elan,ektf2127" },
314 MODULE_DEVICE_TABLE(of
, ektf2127_of_match
);
317 static const struct i2c_device_id ektf2127_i2c_id
[] = {
321 MODULE_DEVICE_TABLE(i2c
, ektf2127_i2c_id
);
323 static struct i2c_driver ektf2127_driver
= {
325 .name
= "elan_ektf2127",
326 .pm
= &ektf2127_pm_ops
,
327 .of_match_table
= of_match_ptr(ektf2127_of_match
),
329 .probe
= ektf2127_probe
,
330 .id_table
= ektf2127_i2c_id
,
332 module_i2c_driver(ektf2127_driver
);
334 MODULE_DESCRIPTION("ELAN eKTF2127 I2C Touchscreen Driver");
335 MODULE_AUTHOR("Michel Verlaan, Siebren Vroegindeweij");
336 MODULE_LICENSE("GPL");