PCI: altera: Fix altera_pcie_link_is_up()
[linux/fpc-iii.git] / drivers / input / touchscreen / goodix.c
blob7bf2597ce44c63e1616b217f538aca23ec05d2fa
1 /*
2 * Driver for Goodix Touchscreens
4 * Copyright (c) 2014 Red Hat Inc.
6 * This code is based on gt9xx.c authored by andrew@goodix.com:
8 * 2010 - 2012 Goodix Technology.
9 */
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; version 2 of the License.
17 #include <linux/kernel.h>
18 #include <linux/dmi.h>
19 #include <linux/i2c.h>
20 #include <linux/input.h>
21 #include <linux/input/mt.h>
22 #include <linux/module.h>
23 #include <linux/delay.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/acpi.h>
28 #include <linux/of.h>
29 #include <asm/unaligned.h>
31 struct goodix_ts_data {
32 struct i2c_client *client;
33 struct input_dev *input_dev;
34 int abs_x_max;
35 int abs_y_max;
36 unsigned int max_touch_num;
37 unsigned int int_trigger_type;
38 bool rotated_screen;
41 #define GOODIX_MAX_HEIGHT 4096
42 #define GOODIX_MAX_WIDTH 4096
43 #define GOODIX_INT_TRIGGER 1
44 #define GOODIX_CONTACT_SIZE 8
45 #define GOODIX_MAX_CONTACTS 10
47 #define GOODIX_CONFIG_MAX_LENGTH 240
49 /* Register defines */
50 #define GOODIX_READ_COOR_ADDR 0x814E
51 #define GOODIX_REG_CONFIG_DATA 0x8047
52 #define GOODIX_REG_ID 0x8140
54 #define RESOLUTION_LOC 1
55 #define MAX_CONTACTS_LOC 5
56 #define TRIGGER_LOC 6
58 static const unsigned long goodix_irq_flags[] = {
59 IRQ_TYPE_EDGE_RISING,
60 IRQ_TYPE_EDGE_FALLING,
61 IRQ_TYPE_LEVEL_LOW,
62 IRQ_TYPE_LEVEL_HIGH,
66 * Those tablets have their coordinates origin at the bottom right
67 * of the tablet, as if rotated 180 degrees
69 static const struct dmi_system_id rotated_screen[] = {
70 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
72 .ident = "WinBook TW100",
73 .matches = {
74 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
75 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
79 .ident = "WinBook TW700",
80 .matches = {
81 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
82 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
85 #endif
89 /**
90 * goodix_i2c_read - read data from a register of the i2c slave device.
92 * @client: i2c device.
93 * @reg: the register to read from.
94 * @buf: raw write data buffer.
95 * @len: length of the buffer to write
97 static int goodix_i2c_read(struct i2c_client *client,
98 u16 reg, u8 *buf, int len)
100 struct i2c_msg msgs[2];
101 u16 wbuf = cpu_to_be16(reg);
102 int ret;
104 msgs[0].flags = 0;
105 msgs[0].addr = client->addr;
106 msgs[0].len = 2;
107 msgs[0].buf = (u8 *)&wbuf;
109 msgs[1].flags = I2C_M_RD;
110 msgs[1].addr = client->addr;
111 msgs[1].len = len;
112 msgs[1].buf = buf;
114 ret = i2c_transfer(client->adapter, msgs, 2);
115 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
118 static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
120 int touch_num;
121 int error;
123 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR, data,
124 GOODIX_CONTACT_SIZE + 1);
125 if (error) {
126 dev_err(&ts->client->dev, "I2C transfer error: %d\n", error);
127 return error;
130 if (!(data[0] & 0x80))
131 return -EAGAIN;
133 touch_num = data[0] & 0x0f;
134 if (touch_num > ts->max_touch_num)
135 return -EPROTO;
137 if (touch_num > 1) {
138 data += 1 + GOODIX_CONTACT_SIZE;
139 error = goodix_i2c_read(ts->client,
140 GOODIX_READ_COOR_ADDR +
141 1 + GOODIX_CONTACT_SIZE,
142 data,
143 GOODIX_CONTACT_SIZE * (touch_num - 1));
144 if (error)
145 return error;
148 return touch_num;
151 static void goodix_ts_report_touch(struct goodix_ts_data *ts, u8 *coor_data)
153 int id = coor_data[0] & 0x0F;
154 int input_x = get_unaligned_le16(&coor_data[1]);
155 int input_y = get_unaligned_le16(&coor_data[3]);
156 int input_w = get_unaligned_le16(&coor_data[5]);
158 if (ts->rotated_screen) {
159 input_x = ts->abs_x_max - input_x;
160 input_y = ts->abs_y_max - input_y;
163 input_mt_slot(ts->input_dev, id);
164 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
165 input_report_abs(ts->input_dev, ABS_MT_POSITION_X, input_x);
166 input_report_abs(ts->input_dev, ABS_MT_POSITION_Y, input_y);
167 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
168 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
172 * goodix_process_events - Process incoming events
174 * @ts: our goodix_ts_data pointer
176 * Called when the IRQ is triggered. Read the current device state, and push
177 * the input events to the user space.
179 static void goodix_process_events(struct goodix_ts_data *ts)
181 u8 point_data[1 + GOODIX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
182 int touch_num;
183 int i;
185 touch_num = goodix_ts_read_input_report(ts, point_data);
186 if (touch_num < 0)
187 return;
189 for (i = 0; i < touch_num; i++)
190 goodix_ts_report_touch(ts,
191 &point_data[1 + GOODIX_CONTACT_SIZE * i]);
193 input_mt_sync_frame(ts->input_dev);
194 input_sync(ts->input_dev);
198 * goodix_ts_irq_handler - The IRQ handler
200 * @irq: interrupt number.
201 * @dev_id: private data pointer.
203 static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
205 static const u8 end_cmd[] = {
206 GOODIX_READ_COOR_ADDR >> 8,
207 GOODIX_READ_COOR_ADDR & 0xff,
210 struct goodix_ts_data *ts = dev_id;
212 goodix_process_events(ts);
214 if (i2c_master_send(ts->client, end_cmd, sizeof(end_cmd)) < 0)
215 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
217 return IRQ_HANDLED;
221 * goodix_read_config - Read the embedded configuration of the panel
223 * @ts: our goodix_ts_data pointer
225 * Must be called during probe
227 static void goodix_read_config(struct goodix_ts_data *ts)
229 u8 config[GOODIX_CONFIG_MAX_LENGTH];
230 int error;
232 error = goodix_i2c_read(ts->client, GOODIX_REG_CONFIG_DATA,
233 config,
234 GOODIX_CONFIG_MAX_LENGTH);
235 if (error) {
236 dev_warn(&ts->client->dev,
237 "Error reading config (%d), using defaults\n",
238 error);
239 ts->abs_x_max = GOODIX_MAX_WIDTH;
240 ts->abs_y_max = GOODIX_MAX_HEIGHT;
241 ts->int_trigger_type = GOODIX_INT_TRIGGER;
242 ts->max_touch_num = GOODIX_MAX_CONTACTS;
243 return;
246 ts->abs_x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
247 ts->abs_y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
248 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
249 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
250 if (!ts->abs_x_max || !ts->abs_y_max || !ts->max_touch_num) {
251 dev_err(&ts->client->dev,
252 "Invalid config, using defaults\n");
253 ts->abs_x_max = GOODIX_MAX_WIDTH;
254 ts->abs_y_max = GOODIX_MAX_HEIGHT;
255 ts->max_touch_num = GOODIX_MAX_CONTACTS;
258 ts->rotated_screen = dmi_check_system(rotated_screen);
259 if (ts->rotated_screen)
260 dev_dbg(&ts->client->dev,
261 "Applying '180 degrees rotated screen' quirk\n");
265 * goodix_read_version - Read goodix touchscreen version
267 * @client: the i2c client
268 * @version: output buffer containing the version on success
269 * @id: output buffer containing the id on success
271 static int goodix_read_version(struct i2c_client *client, u16 *version, u16 *id)
273 int error;
274 u8 buf[6];
275 char id_str[5];
277 error = goodix_i2c_read(client, GOODIX_REG_ID, buf, sizeof(buf));
278 if (error) {
279 dev_err(&client->dev, "read version failed: %d\n", error);
280 return error;
283 memcpy(id_str, buf, 4);
284 id_str[4] = 0;
285 if (kstrtou16(id_str, 10, id))
286 *id = 0x1001;
288 *version = get_unaligned_le16(&buf[4]);
290 dev_info(&client->dev, "ID %d, version: %04x\n", *id, *version);
292 return 0;
296 * goodix_i2c_test - I2C test function to check if the device answers.
298 * @client: the i2c client
300 static int goodix_i2c_test(struct i2c_client *client)
302 int retry = 0;
303 int error;
304 u8 test;
306 while (retry++ < 2) {
307 error = goodix_i2c_read(client, GOODIX_REG_CONFIG_DATA,
308 &test, 1);
309 if (!error)
310 return 0;
312 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
313 retry, error);
314 msleep(20);
317 return error;
321 * goodix_request_input_dev - Allocate, populate and register the input device
323 * @ts: our goodix_ts_data pointer
324 * @version: device firmware version
325 * @id: device ID
327 * Must be called during probe
329 static int goodix_request_input_dev(struct goodix_ts_data *ts, u16 version,
330 u16 id)
332 int error;
334 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
335 if (!ts->input_dev) {
336 dev_err(&ts->client->dev, "Failed to allocate input device.");
337 return -ENOMEM;
340 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_X,
341 0, ts->abs_x_max, 0, 0);
342 input_set_abs_params(ts->input_dev, ABS_MT_POSITION_Y,
343 0, ts->abs_y_max, 0, 0);
344 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
345 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
347 input_mt_init_slots(ts->input_dev, ts->max_touch_num,
348 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
350 ts->input_dev->name = "Goodix Capacitive TouchScreen";
351 ts->input_dev->phys = "input/ts";
352 ts->input_dev->id.bustype = BUS_I2C;
353 ts->input_dev->id.vendor = 0x0416;
354 ts->input_dev->id.product = id;
355 ts->input_dev->id.version = version;
357 error = input_register_device(ts->input_dev);
358 if (error) {
359 dev_err(&ts->client->dev,
360 "Failed to register input device: %d", error);
361 return error;
364 return 0;
367 static int goodix_ts_probe(struct i2c_client *client,
368 const struct i2c_device_id *id)
370 struct goodix_ts_data *ts;
371 unsigned long irq_flags;
372 int error;
373 u16 version_info, id_info;
375 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
377 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
378 dev_err(&client->dev, "I2C check functionality failed.\n");
379 return -ENXIO;
382 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
383 if (!ts)
384 return -ENOMEM;
386 ts->client = client;
387 i2c_set_clientdata(client, ts);
389 error = goodix_i2c_test(client);
390 if (error) {
391 dev_err(&client->dev, "I2C communication failure: %d\n", error);
392 return error;
395 error = goodix_read_version(client, &version_info, &id_info);
396 if (error) {
397 dev_err(&client->dev, "Read version failed.\n");
398 return error;
401 goodix_read_config(ts);
403 error = goodix_request_input_dev(ts, version_info, id_info);
404 if (error)
405 return error;
407 irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
408 error = devm_request_threaded_irq(&ts->client->dev, client->irq,
409 NULL, goodix_ts_irq_handler,
410 irq_flags, client->name, ts);
411 if (error) {
412 dev_err(&client->dev, "request IRQ failed: %d\n", error);
413 return error;
416 return 0;
419 static const struct i2c_device_id goodix_ts_id[] = {
420 { "GDIX1001:00", 0 },
423 MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
425 #ifdef CONFIG_ACPI
426 static const struct acpi_device_id goodix_acpi_match[] = {
427 { "GDIX1001", 0 },
428 { "GDIX1002", 0 },
431 MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
432 #endif
434 #ifdef CONFIG_OF
435 static const struct of_device_id goodix_of_match[] = {
436 { .compatible = "goodix,gt911" },
437 { .compatible = "goodix,gt9110" },
438 { .compatible = "goodix,gt912" },
439 { .compatible = "goodix,gt927" },
440 { .compatible = "goodix,gt9271" },
441 { .compatible = "goodix,gt928" },
442 { .compatible = "goodix,gt967" },
445 MODULE_DEVICE_TABLE(of, goodix_of_match);
446 #endif
448 static struct i2c_driver goodix_ts_driver = {
449 .probe = goodix_ts_probe,
450 .id_table = goodix_ts_id,
451 .driver = {
452 .name = "Goodix-TS",
453 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
454 .of_match_table = of_match_ptr(goodix_of_match),
457 module_i2c_driver(goodix_ts_driver);
459 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
460 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
461 MODULE_DESCRIPTION("Goodix touchscreen driver");
462 MODULE_LICENSE("GPL v2");