drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / input / touchscreen / ektf2127.c
blob46a0611fac8228bcd8ad6e2f9498835c83fb8227
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for ELAN eKTF2127 i2c touchscreen controller
5 * For this driver the layout of the Chipone icn8318 i2c
6 * touchscreencontroller is used.
8 * Author:
9 * Michel Verlaan <michel.verl@gmail.com>
10 * Siebren Vroegindeweij <siebren.vroegindeweij@hotmail.com>
12 * Original chipone_icn8318 driver:
13 * Hans de Goede <hdegoede@redhat.com>
16 #include <linux/bits.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/input.h>
21 #include <linux/input/mt.h>
22 #include <linux/input/touchscreen.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/delay.h>
27 /* Packet header defines (first byte of data send / received) */
28 #define EKTF2127_NOISE 0x40
29 #define EKTF2127_RESPONSE 0x52
30 #define EKTF2127_REQUEST 0x53
31 #define EKTF2127_HELLO 0x55
32 #define EKTF2127_REPORT2 0x5a
33 #define EKTF2127_REPORT 0x5d
34 #define EKTF2127_CALIB_DONE 0x66
36 /* Register defines (second byte of data send / received) */
37 #define EKTF2127_ENV_NOISY 0x41
38 #define EKTF2127_HEIGHT 0x60
39 #define EKTF2127_WIDTH 0x63
41 /* 2 bytes header + 5 * 3 bytes coordinates + 3 bytes pressure info + footer */
42 #define EKTF2127_TOUCH_REPORT_SIZE 21
43 #define EKTF2127_MAX_TOUCHES 5
45 struct ektf2127_ts {
46 struct i2c_client *client;
47 struct input_dev *input;
48 struct gpio_desc *power_gpios;
49 struct touchscreen_properties prop;
50 int status_shift;
53 struct ektf2127_i2c_chip_data {
54 int status_shift;
57 static void ektf2127_parse_coordinates(const u8 *buf, unsigned int touch_count,
58 struct input_mt_pos *touches)
60 int index = 0;
61 int i;
63 for (i = 0; i < touch_count; i++) {
64 index = 2 + i * 3;
66 touches[i].x = (buf[index] & 0x0f);
67 touches[i].x <<= 8;
68 touches[i].x |= buf[index + 2];
70 touches[i].y = (buf[index] & 0xf0);
71 touches[i].y <<= 4;
72 touches[i].y |= buf[index + 1];
76 static void ektf2127_report_event(struct ektf2127_ts *ts, const u8 *buf)
78 struct input_mt_pos touches[EKTF2127_MAX_TOUCHES];
79 int slots[EKTF2127_MAX_TOUCHES];
80 unsigned int touch_count, i;
82 touch_count = buf[1] & 0x07;
83 if (touch_count > EKTF2127_MAX_TOUCHES) {
84 dev_err(&ts->client->dev,
85 "Too many touches %d > %d\n",
86 touch_count, EKTF2127_MAX_TOUCHES);
87 touch_count = EKTF2127_MAX_TOUCHES;
90 ektf2127_parse_coordinates(buf, touch_count, touches);
91 input_mt_assign_slots(ts->input, slots, touches,
92 touch_count, 0);
94 for (i = 0; i < touch_count; i++) {
95 input_mt_slot(ts->input, slots[i]);
96 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, true);
97 touchscreen_report_pos(ts->input, &ts->prop,
98 touches[i].x, touches[i].y, true);
101 input_mt_sync_frame(ts->input);
102 input_sync(ts->input);
105 static void ektf2127_report2_contact(struct ektf2127_ts *ts, int slot,
106 const u8 *buf, bool active)
108 input_mt_slot(ts->input, slot);
109 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, active);
111 if (active) {
112 int x = (buf[0] & 0xf0) << 4 | buf[1];
113 int y = (buf[0] & 0x0f) << 8 | buf[2];
115 touchscreen_report_pos(ts->input, &ts->prop, x, y, true);
119 static void ektf2127_report2_event(struct ektf2127_ts *ts, const u8 *buf)
121 ektf2127_report2_contact(ts, 0, &buf[1], !!(buf[7] & BIT(ts->status_shift)));
122 ektf2127_report2_contact(ts, 1, &buf[4], !!(buf[7] & BIT(ts->status_shift + 1)));
124 input_mt_sync_frame(ts->input);
125 input_sync(ts->input);
128 static irqreturn_t ektf2127_irq(int irq, void *dev_id)
130 struct ektf2127_ts *ts = dev_id;
131 struct device *dev = &ts->client->dev;
132 char buf[EKTF2127_TOUCH_REPORT_SIZE];
133 int ret;
135 ret = i2c_master_recv(ts->client, buf, EKTF2127_TOUCH_REPORT_SIZE);
136 if (ret != EKTF2127_TOUCH_REPORT_SIZE) {
137 dev_err(dev, "Error reading touch data: %d\n", ret);
138 goto out;
141 switch (buf[0]) {
142 case EKTF2127_REPORT:
143 ektf2127_report_event(ts, buf);
144 break;
146 case EKTF2127_REPORT2:
147 ektf2127_report2_event(ts, buf);
148 break;
150 case EKTF2127_NOISE:
151 if (buf[1] == EKTF2127_ENV_NOISY)
152 dev_dbg(dev, "Environment is electrically noisy\n");
153 break;
155 case EKTF2127_HELLO:
156 case EKTF2127_CALIB_DONE:
157 break;
159 default:
160 dev_err(dev, "Unexpected packet header byte %#02x\n", buf[0]);
161 break;
164 out:
165 return IRQ_HANDLED;
168 static int ektf2127_start(struct input_dev *dev)
170 struct ektf2127_ts *ts = input_get_drvdata(dev);
172 enable_irq(ts->client->irq);
173 gpiod_set_value_cansleep(ts->power_gpios, 1);
175 return 0;
178 static void ektf2127_stop(struct input_dev *dev)
180 struct ektf2127_ts *ts = input_get_drvdata(dev);
182 disable_irq(ts->client->irq);
183 gpiod_set_value_cansleep(ts->power_gpios, 0);
186 static int ektf2127_suspend(struct device *dev)
188 struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
190 mutex_lock(&ts->input->mutex);
191 if (input_device_enabled(ts->input))
192 ektf2127_stop(ts->input);
193 mutex_unlock(&ts->input->mutex);
195 return 0;
198 static int ektf2127_resume(struct device *dev)
200 struct ektf2127_ts *ts = i2c_get_clientdata(to_i2c_client(dev));
202 mutex_lock(&ts->input->mutex);
203 if (input_device_enabled(ts->input))
204 ektf2127_start(ts->input);
205 mutex_unlock(&ts->input->mutex);
207 return 0;
210 static DEFINE_SIMPLE_DEV_PM_OPS(ektf2127_pm_ops, ektf2127_suspend,
211 ektf2127_resume);
213 static int ektf2127_query_dimension(struct i2c_client *client, bool width)
215 struct device *dev = &client->dev;
216 const char *what = width ? "width" : "height";
217 u8 what_code = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
218 u8 buf[4];
219 int ret;
220 int error;
222 /* Request dimension */
223 buf[0] = EKTF2127_REQUEST;
224 buf[1] = width ? EKTF2127_WIDTH : EKTF2127_HEIGHT;
225 buf[2] = 0x00;
226 buf[3] = 0x00;
227 ret = i2c_master_send(client, buf, sizeof(buf));
228 if (ret != sizeof(buf)) {
229 error = ret < 0 ? ret : -EIO;
230 dev_err(dev, "Failed to request %s: %d\n", what, error);
231 return error;
234 msleep(20);
236 /* Read response */
237 ret = i2c_master_recv(client, buf, sizeof(buf));
238 if (ret != sizeof(buf)) {
239 error = ret < 0 ? ret : -EIO;
240 dev_err(dev, "Failed to receive %s data: %d\n", what, error);
241 return error;
244 if (buf[0] != EKTF2127_RESPONSE || buf[1] != what_code) {
245 dev_err(dev, "Unexpected %s data: %#02x %#02x\n",
246 what, buf[0], buf[1]);
247 return -EIO;
250 return (((buf[3] & 0xf0) << 4) | buf[2]) - 1;
253 static int ektf2127_probe(struct i2c_client *client)
255 struct device *dev = &client->dev;
256 const struct ektf2127_i2c_chip_data *chip_data;
257 struct ektf2127_ts *ts;
258 struct input_dev *input;
259 u8 buf[4];
260 int max_x, max_y;
261 int error;
263 if (!client->irq) {
264 dev_err(dev, "Error no irq specified\n");
265 return -EINVAL;
268 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
269 if (!ts)
270 return -ENOMEM;
272 /* This requests the gpio *and* turns on the touchscreen controller */
273 ts->power_gpios = devm_gpiod_get(dev, "power", GPIOD_OUT_HIGH);
274 if (IS_ERR(ts->power_gpios))
275 return dev_err_probe(dev, PTR_ERR(ts->power_gpios), "Error getting power gpio\n");
277 input = devm_input_allocate_device(dev);
278 if (!input)
279 return -ENOMEM;
281 input->name = client->name;
282 input->id.bustype = BUS_I2C;
283 input->open = ektf2127_start;
284 input->close = ektf2127_stop;
286 ts->client = client;
288 /* Read hello (ignore result, depends on initial power state) */
289 msleep(20);
290 i2c_master_recv(ts->client, buf, sizeof(buf));
292 /* Read resolution from chip */
293 max_x = ektf2127_query_dimension(client, true);
294 if (max_x < 0)
295 return max_x;
297 max_y = ektf2127_query_dimension(client, false);
298 if (max_y < 0)
299 return max_y;
301 input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
302 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
303 touchscreen_parse_properties(input, true, &ts->prop);
305 error = input_mt_init_slots(input, EKTF2127_MAX_TOUCHES,
306 INPUT_MT_DIRECT |
307 INPUT_MT_DROP_UNUSED |
308 INPUT_MT_TRACK);
309 if (error)
310 return error;
312 ts->input = input;
314 chip_data = i2c_get_match_data(client);
315 if (!chip_data)
316 return dev_err_probe(&client->dev, -EINVAL, "missing chip data\n");
318 ts->status_shift = chip_data->status_shift;
320 input_set_drvdata(input, ts);
322 error = devm_request_threaded_irq(dev, client->irq,
323 NULL, ektf2127_irq,
324 IRQF_ONESHOT, client->name, ts);
325 if (error) {
326 dev_err(dev, "Error requesting irq: %d\n", error);
327 return error;
330 /* Stop device till opened */
331 ektf2127_stop(ts->input);
333 error = input_register_device(input);
334 if (error)
335 return error;
337 i2c_set_clientdata(client, ts);
339 return 0;
342 static const struct ektf2127_i2c_chip_data ektf2127_data = {
343 .status_shift = 1,
346 static const struct ektf2127_i2c_chip_data ektf2232_data = {
347 .status_shift = 0,
350 #ifdef CONFIG_OF
351 static const struct of_device_id ektf2127_of_match[] = {
352 { .compatible = "elan,ektf2127", .data = &ektf2127_data},
353 { .compatible = "elan,ektf2132", .data = &ektf2127_data},
354 { .compatible = "elan,ektf2232", .data = &ektf2232_data},
357 MODULE_DEVICE_TABLE(of, ektf2127_of_match);
358 #endif
360 static const struct i2c_device_id ektf2127_i2c_id[] = {
361 { .name = "ektf2127", .driver_data = (long)&ektf2127_data },
362 { .name = "ektf2132", .driver_data = (long)&ektf2127_data },
363 { .name = "ektf2232", .driver_data = (long)&ektf2232_data },
366 MODULE_DEVICE_TABLE(i2c, ektf2127_i2c_id);
368 static struct i2c_driver ektf2127_driver = {
369 .driver = {
370 .name = "elan_ektf2127",
371 .pm = pm_sleep_ptr(&ektf2127_pm_ops),
372 .of_match_table = of_match_ptr(ektf2127_of_match),
374 .probe = ektf2127_probe,
375 .id_table = ektf2127_i2c_id,
377 module_i2c_driver(ektf2127_driver);
379 MODULE_DESCRIPTION("ELAN eKTF2127/eKTF2132 I2C Touchscreen Driver");
380 MODULE_AUTHOR("Michel Verlaan, Siebren Vroegindeweij");
381 MODULE_LICENSE("GPL");