blob: 8e31b4bc62754daeb73b41cd365dc7a343e7571e [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Bastien Noceraca96ea82014-10-31 09:26:16 -07002/*
3 * Driver for Goodix Touchscreens
4 *
5 * Copyright (c) 2014 Red Hat Inc.
Karsten Merkerad48cf52015-12-17 17:02:53 -08006 * Copyright (c) 2015 K. Merker <merker@debian.org>
Bastien Noceraca96ea82014-10-31 09:26:16 -07007 *
8 * This code is based on gt9xx.c authored by andrew@goodix.com:
9 *
10 * 2010 - 2012 Goodix Technology.
11 */
12
Bastien Noceraca96ea82014-10-31 09:26:16 -070013
14#include <linux/kernel.h>
Bastien Nocera8b5a3592015-07-24 09:08:53 -070015#include <linux/dmi.h>
Irina Tirdea68caf8582015-12-17 16:05:42 -080016#include <linux/firmware.h>
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080017#include <linux/gpio/consumer.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070018#include <linux/i2c.h>
19#include <linux/input.h>
20#include <linux/input/mt.h>
Marcin Niestrojfafef982018-01-26 11:08:59 -080021#include <linux/input/touchscreen.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070022#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/irq.h>
25#include <linux/interrupt.h>
Jagan Tekiae97fb52019-04-03 15:20:26 -070026#include <linux/regulator/consumer.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070027#include <linux/slab.h>
Aleksei Mamlin771d8f12015-03-06 16:43:38 -080028#include <linux/acpi.h>
29#include <linux/of.h>
Bastien Noceraca96ea82014-10-31 09:26:16 -070030#include <asm/unaligned.h>
31
Marcin Niestroj25309002017-10-24 11:06:41 -070032struct goodix_ts_data;
33
Hans de Goede49db3992020-03-24 11:12:10 -070034enum goodix_irq_pin_access_method {
35 IRQ_PIN_ACCESS_NONE,
36 IRQ_PIN_ACCESS_GPIO,
Hans de Goedea7d4b172020-03-24 11:23:21 -070037 IRQ_PIN_ACCESS_ACPI_GPIO,
Hans de Goedec5fca482020-03-24 11:24:31 -070038 IRQ_PIN_ACCESS_ACPI_METHOD,
Hans de Goede49db3992020-03-24 11:12:10 -070039};
40
Marcin Niestroj25309002017-10-24 11:06:41 -070041struct goodix_chip_data {
42 u16 config_addr;
43 int config_len;
44 int (*check_config)(struct goodix_ts_data *, const struct firmware *);
45};
46
Bastien Noceraca96ea82014-10-31 09:26:16 -070047struct goodix_ts_data {
48 struct i2c_client *client;
49 struct input_dev *input_dev;
Marcin Niestroj25309002017-10-24 11:06:41 -070050 const struct goodix_chip_data *chip;
Marcin Niestrojfafef982018-01-26 11:08:59 -080051 struct touchscreen_properties prop;
Bastien Noceraca96ea82014-10-31 09:26:16 -070052 unsigned int max_touch_num;
53 unsigned int int_trigger_type;
Jagan Tekiae97fb52019-04-03 15:20:26 -070054 struct regulator *avdd28;
55 struct regulator *vddio;
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080056 struct gpio_desc *gpiod_int;
57 struct gpio_desc *gpiod_rst;
Hans de Goedea7d4b172020-03-24 11:23:21 -070058 int gpio_count;
59 int gpio_int_idx;
Irina Tirdea68caf8582015-12-17 16:05:42 -080060 u16 id;
61 u16 version;
62 const char *cfg_name;
Hans de Goede1921dac2020-03-24 11:23:04 -070063 bool reset_controller_at_probe;
Hans de Goedee070a97b2020-03-24 11:12:35 -070064 bool load_cfg_from_disk;
Irina Tirdea68caf8582015-12-17 16:05:42 -080065 struct completion firmware_loading_complete;
Irina Tirdea5ab09d62015-12-17 16:43:39 -080066 unsigned long irq_flags;
Hans de Goede49db3992020-03-24 11:12:10 -070067 enum goodix_irq_pin_access_method irq_pin_access_method;
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -070068 unsigned int contact_size;
Bastien Noceraca96ea82014-10-31 09:26:16 -070069};
70
Irina Tirdeaec6e1b42015-12-17 15:57:34 -080071#define GOODIX_GPIO_INT_NAME "irq"
72#define GOODIX_GPIO_RST_NAME "reset"
73
Bastien Noceraca96ea82014-10-31 09:26:16 -070074#define GOODIX_MAX_HEIGHT 4096
75#define GOODIX_MAX_WIDTH 4096
76#define GOODIX_INT_TRIGGER 1
77#define GOODIX_CONTACT_SIZE 8
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -070078#define GOODIX_MAX_CONTACT_SIZE 9
Bastien Noceraca96ea82014-10-31 09:26:16 -070079#define GOODIX_MAX_CONTACTS 10
80
81#define GOODIX_CONFIG_MAX_LENGTH 240
Irina Tirdeaa779fbc2015-12-17 15:55:21 -080082#define GOODIX_CONFIG_911_LENGTH 186
83#define GOODIX_CONFIG_967_LENGTH 228
Bastien Noceraca96ea82014-10-31 09:26:16 -070084
85/* Register defines */
Irina Tirdea5ab09d62015-12-17 16:43:39 -080086#define GOODIX_REG_COMMAND 0x8040
87#define GOODIX_CMD_SCREEN_OFF 0x05
88
Bastien Noceraca96ea82014-10-31 09:26:16 -070089#define GOODIX_READ_COOR_ADDR 0x814E
Marcin Niestroj25309002017-10-24 11:06:41 -070090#define GOODIX_GT1X_REG_CONFIG_DATA 0x8050
91#define GOODIX_GT9X_REG_CONFIG_DATA 0x8047
Irina Tirdeae70b0302015-06-09 11:04:40 -070092#define GOODIX_REG_ID 0x8140
Bastien Noceraca96ea82014-10-31 09:26:16 -070093
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -070094#define GOODIX_BUFFER_STATUS_READY BIT(7)
95#define GOODIX_BUFFER_STATUS_TIMEOUT 20
96
Bastien Noceraca96ea82014-10-31 09:26:16 -070097#define RESOLUTION_LOC 1
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -080098#define MAX_CONTACTS_LOC 5
Bastien Noceraca96ea82014-10-31 09:26:16 -070099#define TRIGGER_LOC 6
100
Marcin Niestroj25309002017-10-24 11:06:41 -0700101static int goodix_check_cfg_8(struct goodix_ts_data *ts,
102 const struct firmware *cfg);
103static int goodix_check_cfg_16(struct goodix_ts_data *ts,
104 const struct firmware *cfg);
105
106static const struct goodix_chip_data gt1x_chip_data = {
107 .config_addr = GOODIX_GT1X_REG_CONFIG_DATA,
108 .config_len = GOODIX_CONFIG_MAX_LENGTH,
109 .check_config = goodix_check_cfg_16,
110};
111
112static const struct goodix_chip_data gt911_chip_data = {
113 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
114 .config_len = GOODIX_CONFIG_911_LENGTH,
115 .check_config = goodix_check_cfg_8,
116};
117
118static const struct goodix_chip_data gt967_chip_data = {
119 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
120 .config_len = GOODIX_CONFIG_967_LENGTH,
121 .check_config = goodix_check_cfg_8,
122};
123
124static const struct goodix_chip_data gt9x_chip_data = {
125 .config_addr = GOODIX_GT9X_REG_CONFIG_DATA,
126 .config_len = GOODIX_CONFIG_MAX_LENGTH,
127 .check_config = goodix_check_cfg_8,
128};
129
Bastien Noceraca96ea82014-10-31 09:26:16 -0700130static const unsigned long goodix_irq_flags[] = {
131 IRQ_TYPE_EDGE_RISING,
132 IRQ_TYPE_EDGE_FALLING,
133 IRQ_TYPE_LEVEL_LOW,
134 IRQ_TYPE_LEVEL_HIGH,
135};
136
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700137/*
138 * Those tablets have their coordinates origin at the bottom right
139 * of the tablet, as if rotated 180 degrees
140 */
141static const struct dmi_system_id rotated_screen[] = {
142#if defined(CONFIG_DMI) && defined(CONFIG_X86)
143 {
Hans de Goededf5b5e52019-12-02 09:36:15 -0800144 .ident = "Teclast X89",
145 .matches = {
146 /* tPAD is too generic, also match on bios date */
147 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
148 DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
149 DMI_MATCH(DMI_BIOS_DATE, "12/19/2014"),
150 },
151 },
152 {
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700153 .ident = "WinBook TW100",
154 .matches = {
155 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
156 DMI_MATCH(DMI_PRODUCT_NAME, "TW100")
157 }
158 },
159 {
160 .ident = "WinBook TW700",
161 .matches = {
162 DMI_MATCH(DMI_SYS_VENDOR, "WinBook"),
163 DMI_MATCH(DMI_PRODUCT_NAME, "TW700")
164 },
165 },
166#endif
167 {}
168};
169
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700170static const struct dmi_system_id nine_bytes_report[] = {
171#if defined(CONFIG_DMI) && defined(CONFIG_X86)
172 {
173 .ident = "Lenovo YogaBook",
174 /* YB1-X91L/F and YB1-X90L/F */
175 .matches = {
176 DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X9")
177 }
178 },
179#endif
180 {}
181};
182
Sergei A. Trusov1dd5ddc2020-03-05 19:53:06 -0800183/*
184 * Those tablets have their x coordinate inverted
185 */
186static const struct dmi_system_id inverted_x_screen[] = {
187#if defined(CONFIG_DMI) && defined(CONFIG_X86)
188 {
189 .ident = "Cube I15-TC",
190 .matches = {
191 DMI_MATCH(DMI_SYS_VENDOR, "Cube"),
192 DMI_MATCH(DMI_PRODUCT_NAME, "I15-TC")
193 },
194 },
195#endif
196 {}
197};
198
Bastien Noceraca96ea82014-10-31 09:26:16 -0700199/**
200 * goodix_i2c_read - read data from a register of the i2c slave device.
201 *
202 * @client: i2c device.
203 * @reg: the register to read from.
204 * @buf: raw write data buffer.
205 * @len: length of the buffer to write
206 */
207static int goodix_i2c_read(struct i2c_client *client,
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700208 u16 reg, u8 *buf, int len)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700209{
210 struct i2c_msg msgs[2];
Marcin Niestrojfafef982018-01-26 11:08:59 -0800211 __be16 wbuf = cpu_to_be16(reg);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700212 int ret;
213
214 msgs[0].flags = 0;
215 msgs[0].addr = client->addr;
216 msgs[0].len = 2;
Irina Tirdea0dfb35b2015-06-09 11:01:38 -0700217 msgs[0].buf = (u8 *)&wbuf;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700218
219 msgs[1].flags = I2C_M_RD;
220 msgs[1].addr = client->addr;
221 msgs[1].len = len;
222 msgs[1].buf = buf;
223
224 ret = i2c_transfer(client->adapter, msgs, 2);
225 return ret < 0 ? ret : (ret != ARRAY_SIZE(msgs) ? -EIO : 0);
226}
227
Irina Tirdea68caf8582015-12-17 16:05:42 -0800228/**
229 * goodix_i2c_write - write data to a register of the i2c slave device.
230 *
231 * @client: i2c device.
232 * @reg: the register to write to.
233 * @buf: raw data buffer to write.
234 * @len: length of the buffer to write
235 */
236static int goodix_i2c_write(struct i2c_client *client, u16 reg, const u8 *buf,
237 unsigned len)
238{
239 u8 *addr_buf;
240 struct i2c_msg msg;
241 int ret;
242
243 addr_buf = kmalloc(len + 2, GFP_KERNEL);
244 if (!addr_buf)
245 return -ENOMEM;
246
247 addr_buf[0] = reg >> 8;
248 addr_buf[1] = reg & 0xFF;
249 memcpy(&addr_buf[2], buf, len);
250
251 msg.flags = 0;
252 msg.addr = client->addr;
253 msg.buf = addr_buf;
254 msg.len = len + 2;
255
256 ret = i2c_transfer(client->adapter, &msg, 1);
257 kfree(addr_buf);
258 return ret < 0 ? ret : (ret != 1 ? -EIO : 0);
259}
260
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800261static int goodix_i2c_write_u8(struct i2c_client *client, u16 reg, u8 value)
262{
263 return goodix_i2c_write(client, reg, &value, sizeof(value));
264}
265
Marcin Niestroj25309002017-10-24 11:06:41 -0700266static const struct goodix_chip_data *goodix_get_chip_data(u16 id)
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800267{
268 switch (id) {
Marcin Niestroj25309002017-10-24 11:06:41 -0700269 case 1151:
Jagan Tekia5f50c52019-04-03 16:05:34 -0700270 case 5663:
Guido Günther84ef1b32019-02-16 23:03:13 -0800271 case 5688:
Marcin Niestroj25309002017-10-24 11:06:41 -0700272 return &gt1x_chip_data;
273
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800274 case 911:
275 case 9271:
276 case 9110:
277 case 927:
278 case 928:
Marcin Niestroj25309002017-10-24 11:06:41 -0700279 return &gt911_chip_data;
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800280
281 case 912:
282 case 967:
Marcin Niestroj25309002017-10-24 11:06:41 -0700283 return &gt967_chip_data;
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800284
285 default:
Marcin Niestroj25309002017-10-24 11:06:41 -0700286 return &gt9x_chip_data;
Irina Tirdeaa779fbc2015-12-17 15:55:21 -0800287 }
288}
289
Bastien Noceraca96ea82014-10-31 09:26:16 -0700290static int goodix_ts_read_input_report(struct goodix_ts_data *ts, u8 *data)
291{
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700292 unsigned long max_timeout;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700293 int touch_num;
294 int error;
295
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700296 /*
297 * The 'buffer status' bit, which indicates that the data is valid, is
298 * not set as soon as the interrupt is raised, but slightly after.
299 * This takes around 10 ms to happen, so we poll for 20 ms.
300 */
301 max_timeout = jiffies + msecs_to_jiffies(GOODIX_BUFFER_STATUS_TIMEOUT);
302 do {
303 error = goodix_i2c_read(ts->client, GOODIX_READ_COOR_ADDR,
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700304 data, ts->contact_size + 1);
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700305 if (error) {
306 dev_err(&ts->client->dev, "I2C transfer error: %d\n",
307 error);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700308 return error;
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700309 }
Bastien Noceraca96ea82014-10-31 09:26:16 -0700310
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700311 if (data[0] & GOODIX_BUFFER_STATUS_READY) {
312 touch_num = data[0] & 0x0f;
313 if (touch_num > ts->max_touch_num)
314 return -EPROTO;
315
316 if (touch_num > 1) {
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700317 data += 1 + ts->contact_size;
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700318 error = goodix_i2c_read(ts->client,
319 GOODIX_READ_COOR_ADDR +
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700320 1 + ts->contact_size,
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700321 data,
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700322 ts->contact_size *
Paul Cercueil9b5db7aa2017-10-13 11:04:48 -0700323 (touch_num - 1));
324 if (error)
325 return error;
326 }
327
328 return touch_num;
329 }
330
331 usleep_range(1000, 2000); /* Poll every 1 - 2 ms */
332 } while (time_before(jiffies, max_timeout));
333
334 /*
335 * The Goodix panel will send spurious interrupts after a
336 * 'finger up' event, which will always cause a timeout.
337 */
338 return 0;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700339}
340
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700341static void goodix_ts_report_touch_8b(struct goodix_ts_data *ts, u8 *coor_data)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700342{
343 int id = coor_data[0] & 0x0F;
344 int input_x = get_unaligned_le16(&coor_data[1]);
345 int input_y = get_unaligned_le16(&coor_data[3]);
346 int input_w = get_unaligned_le16(&coor_data[5]);
347
348 input_mt_slot(ts->input_dev, id);
349 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
Marcin Niestrojfafef982018-01-26 11:08:59 -0800350 touchscreen_report_pos(ts->input_dev, &ts->prop,
351 input_x, input_y, true);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700352 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
353 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
354}
355
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700356static void goodix_ts_report_touch_9b(struct goodix_ts_data *ts, u8 *coor_data)
357{
358 int id = coor_data[1] & 0x0F;
359 int input_x = get_unaligned_le16(&coor_data[3]);
360 int input_y = get_unaligned_le16(&coor_data[5]);
361 int input_w = get_unaligned_le16(&coor_data[7]);
362
363 input_mt_slot(ts->input_dev, id);
364 input_mt_report_slot_state(ts->input_dev, MT_TOOL_FINGER, true);
365 touchscreen_report_pos(ts->input_dev, &ts->prop,
366 input_x, input_y, true);
367 input_report_abs(ts->input_dev, ABS_MT_TOUCH_MAJOR, input_w);
368 input_report_abs(ts->input_dev, ABS_MT_WIDTH_MAJOR, input_w);
369}
370
Bastien Noceraca96ea82014-10-31 09:26:16 -0700371/**
372 * goodix_process_events - Process incoming events
373 *
374 * @ts: our goodix_ts_data pointer
375 *
376 * Called when the IRQ is triggered. Read the current device state, and push
377 * the input events to the user space.
378 */
379static void goodix_process_events(struct goodix_ts_data *ts)
380{
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700381 u8 point_data[1 + GOODIX_MAX_CONTACT_SIZE * GOODIX_MAX_CONTACTS];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700382 int touch_num;
383 int i;
384
385 touch_num = goodix_ts_read_input_report(ts, point_data);
386 if (touch_num < 0)
387 return;
388
Sergei A. Trusov4a54fee2017-09-06 17:29:24 -0700389 /*
390 * Bit 4 of the first byte reports the status of the capacitive
391 * Windows/Home button.
392 */
393 input_report_key(ts->input_dev, KEY_LEFTMETA, point_data[0] & BIT(4));
394
Bastien Noceraca96ea82014-10-31 09:26:16 -0700395 for (i = 0; i < touch_num; i++)
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -0700396 if (ts->contact_size == 9)
397 goodix_ts_report_touch_9b(ts,
398 &point_data[1 + ts->contact_size * i]);
399 else
400 goodix_ts_report_touch_8b(ts,
401 &point_data[1 + ts->contact_size * i]);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700402
403 input_mt_sync_frame(ts->input_dev);
404 input_sync(ts->input_dev);
405}
406
407/**
408 * goodix_ts_irq_handler - The IRQ handler
409 *
410 * @irq: interrupt number.
411 * @dev_id: private data pointer.
412 */
413static irqreturn_t goodix_ts_irq_handler(int irq, void *dev_id)
414{
Bastien Noceraca96ea82014-10-31 09:26:16 -0700415 struct goodix_ts_data *ts = dev_id;
416
417 goodix_process_events(ts);
418
Irina Tirdea5d655b32015-12-17 16:47:42 -0800419 if (goodix_i2c_write_u8(ts->client, GOODIX_READ_COOR_ADDR, 0) < 0)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700420 dev_err(&ts->client->dev, "I2C write end_cmd error\n");
421
422 return IRQ_HANDLED;
423}
424
Irina Tirdea5ab09d62015-12-17 16:43:39 -0800425static void goodix_free_irq(struct goodix_ts_data *ts)
426{
427 devm_free_irq(&ts->client->dev, ts->client->irq, ts);
428}
429
430static int goodix_request_irq(struct goodix_ts_data *ts)
431{
432 return devm_request_threaded_irq(&ts->client->dev, ts->client->irq,
433 NULL, goodix_ts_irq_handler,
434 ts->irq_flags, ts->client->name, ts);
435}
436
Marcin Niestroj25309002017-10-24 11:06:41 -0700437static int goodix_check_cfg_8(struct goodix_ts_data *ts,
438 const struct firmware *cfg)
Irina Tirdea68caf8582015-12-17 16:05:42 -0800439{
Marcin Niestroj25309002017-10-24 11:06:41 -0700440 int i, raw_cfg_len = cfg->size - 2;
Irina Tirdea68caf8582015-12-17 16:05:42 -0800441 u8 check_sum = 0;
442
Irina Tirdea68caf8582015-12-17 16:05:42 -0800443 for (i = 0; i < raw_cfg_len; i++)
444 check_sum += cfg->data[i];
445 check_sum = (~check_sum) + 1;
446 if (check_sum != cfg->data[raw_cfg_len]) {
447 dev_err(&ts->client->dev,
448 "The checksum of the config fw is not correct");
449 return -EINVAL;
450 }
451
452 if (cfg->data[raw_cfg_len + 1] != 1) {
453 dev_err(&ts->client->dev,
454 "Config fw must have Config_Fresh register set");
455 return -EINVAL;
456 }
457
458 return 0;
459}
460
Marcin Niestroj25309002017-10-24 11:06:41 -0700461static int goodix_check_cfg_16(struct goodix_ts_data *ts,
462 const struct firmware *cfg)
463{
464 int i, raw_cfg_len = cfg->size - 3;
465 u16 check_sum = 0;
466
467 for (i = 0; i < raw_cfg_len; i += 2)
468 check_sum += get_unaligned_be16(&cfg->data[i]);
469 check_sum = (~check_sum) + 1;
470 if (check_sum != get_unaligned_be16(&cfg->data[raw_cfg_len])) {
471 dev_err(&ts->client->dev,
472 "The checksum of the config fw is not correct");
473 return -EINVAL;
474 }
475
476 if (cfg->data[raw_cfg_len + 2] != 1) {
477 dev_err(&ts->client->dev,
478 "Config fw must have Config_Fresh register set");
479 return -EINVAL;
480 }
481
482 return 0;
483}
484
485/**
486 * goodix_check_cfg - Checks if config fw is valid
487 *
488 * @ts: goodix_ts_data pointer
489 * @cfg: firmware config data
490 */
491static int goodix_check_cfg(struct goodix_ts_data *ts,
492 const struct firmware *cfg)
493{
494 if (cfg->size > GOODIX_CONFIG_MAX_LENGTH) {
495 dev_err(&ts->client->dev,
496 "The length of the config fw is not correct");
497 return -EINVAL;
498 }
499
500 return ts->chip->check_config(ts, cfg);
501}
502
Irina Tirdea68caf8582015-12-17 16:05:42 -0800503/**
504 * goodix_send_cfg - Write fw config to device
505 *
506 * @ts: goodix_ts_data pointer
507 * @cfg: config firmware to write to device
508 */
509static int goodix_send_cfg(struct goodix_ts_data *ts,
510 const struct firmware *cfg)
511{
512 int error;
513
514 error = goodix_check_cfg(ts, cfg);
515 if (error)
516 return error;
517
Marcin Niestroj25309002017-10-24 11:06:41 -0700518 error = goodix_i2c_write(ts->client, ts->chip->config_addr, cfg->data,
Irina Tirdea68caf8582015-12-17 16:05:42 -0800519 cfg->size);
520 if (error) {
521 dev_err(&ts->client->dev, "Failed to write config data: %d",
522 error);
523 return error;
524 }
525 dev_dbg(&ts->client->dev, "Config sent successfully.");
526
527 /* Let the firmware reconfigure itself, so sleep for 10ms */
528 usleep_range(10000, 11000);
529
530 return 0;
531}
532
Hans de Goede49db3992020-03-24 11:12:10 -0700533static int goodix_irq_direction_output(struct goodix_ts_data *ts,
534 int value)
535{
Hans de Goedec5fca482020-03-24 11:24:31 -0700536 struct device *dev = &ts->client->dev;
537 acpi_status status;
538
Hans de Goede49db3992020-03-24 11:12:10 -0700539 switch (ts->irq_pin_access_method) {
540 case IRQ_PIN_ACCESS_NONE:
541 dev_err(&ts->client->dev,
542 "%s called without an irq_pin_access_method set\n",
543 __func__);
544 return -EINVAL;
545 case IRQ_PIN_ACCESS_GPIO:
546 return gpiod_direction_output(ts->gpiod_int, value);
Hans de Goedea7d4b172020-03-24 11:23:21 -0700547 case IRQ_PIN_ACCESS_ACPI_GPIO:
548 /*
549 * The IRQ pin triggers on a falling edge, so its gets marked
550 * as active-low, use output_raw to avoid the value inversion.
551 */
552 return gpiod_direction_output_raw(ts->gpiod_int, value);
Hans de Goedec5fca482020-03-24 11:24:31 -0700553 case IRQ_PIN_ACCESS_ACPI_METHOD:
554 status = acpi_execute_simple_method(ACPI_HANDLE(dev),
555 "INTO", value);
556 return ACPI_SUCCESS(status) ? 0 : -EIO;
Hans de Goede49db3992020-03-24 11:12:10 -0700557 }
558
559 return -EINVAL; /* Never reached */
560}
561
562static int goodix_irq_direction_input(struct goodix_ts_data *ts)
563{
Hans de Goedec5fca482020-03-24 11:24:31 -0700564 struct device *dev = &ts->client->dev;
565 acpi_status status;
566
Hans de Goede49db3992020-03-24 11:12:10 -0700567 switch (ts->irq_pin_access_method) {
568 case IRQ_PIN_ACCESS_NONE:
569 dev_err(&ts->client->dev,
570 "%s called without an irq_pin_access_method set\n",
571 __func__);
572 return -EINVAL;
573 case IRQ_PIN_ACCESS_GPIO:
Hans de Goedea7d4b172020-03-24 11:23:21 -0700574 case IRQ_PIN_ACCESS_ACPI_GPIO:
Hans de Goede49db3992020-03-24 11:12:10 -0700575 return gpiod_direction_input(ts->gpiod_int);
Hans de Goedec5fca482020-03-24 11:24:31 -0700576 case IRQ_PIN_ACCESS_ACPI_METHOD:
577 status = acpi_evaluate_object(ACPI_HANDLE(dev), "INTI",
578 NULL, NULL);
579 return ACPI_SUCCESS(status) ? 0 : -EIO;
Hans de Goede49db3992020-03-24 11:12:10 -0700580 }
581
582 return -EINVAL; /* Never reached */
583}
584
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800585static int goodix_int_sync(struct goodix_ts_data *ts)
586{
587 int error;
588
Hans de Goede49db3992020-03-24 11:12:10 -0700589 error = goodix_irq_direction_output(ts, 0);
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800590 if (error)
591 return error;
592
593 msleep(50); /* T5: 50ms */
594
Hans de Goede49db3992020-03-24 11:12:10 -0700595 error = goodix_irq_direction_input(ts);
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800596 if (error)
597 return error;
598
599 return 0;
600}
601
602/**
603 * goodix_reset - Reset device during power on
604 *
605 * @ts: goodix_ts_data pointer
606 */
607static int goodix_reset(struct goodix_ts_data *ts)
608{
609 int error;
610
611 /* begin select I2C slave addr */
612 error = gpiod_direction_output(ts->gpiod_rst, 0);
613 if (error)
614 return error;
615
616 msleep(20); /* T2: > 10ms */
617
618 /* HIGH: 0x28/0x29, LOW: 0xBA/0xBB */
Hans de Goede49db3992020-03-24 11:12:10 -0700619 error = goodix_irq_direction_output(ts, ts->client->addr == 0x14);
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800620 if (error)
621 return error;
622
623 usleep_range(100, 2000); /* T3: > 100us */
624
625 error = gpiod_direction_output(ts->gpiod_rst, 1);
626 if (error)
627 return error;
628
629 usleep_range(6000, 10000); /* T4: > 5ms */
630
631 /* end select I2C slave addr */
632 error = gpiod_direction_input(ts->gpiod_rst);
633 if (error)
634 return error;
635
636 error = goodix_int_sync(ts);
637 if (error)
638 return error;
639
640 return 0;
641}
642
Hans de Goedea7d4b172020-03-24 11:23:21 -0700643#if defined CONFIG_X86 && defined CONFIG_ACPI
Hans de Goede67abd9e2020-03-24 11:23:35 -0700644#include <asm/cpu_device_id.h>
645#include <asm/intel-family.h>
646
647static const struct x86_cpu_id baytrail_cpu_ids[] = {
648 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT, X86_FEATURE_ANY, },
649 {}
650};
651
652static inline bool is_byt(void)
653{
654 const struct x86_cpu_id *id = x86_match_cpu(baytrail_cpu_ids);
655
656 return !!id;
657}
658
Hans de Goedea7d4b172020-03-24 11:23:21 -0700659static const struct acpi_gpio_params first_gpio = { 0, 0, false };
660static const struct acpi_gpio_params second_gpio = { 1, 0, false };
661
662static const struct acpi_gpio_mapping acpi_goodix_int_first_gpios[] = {
663 { GOODIX_GPIO_INT_NAME "-gpios", &first_gpio, 1 },
664 { GOODIX_GPIO_RST_NAME "-gpios", &second_gpio, 1 },
665 { },
666};
667
668static const struct acpi_gpio_mapping acpi_goodix_int_last_gpios[] = {
669 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
670 { GOODIX_GPIO_INT_NAME "-gpios", &second_gpio, 1 },
671 { },
672};
673
Hans de Goedec5fca482020-03-24 11:24:31 -0700674static const struct acpi_gpio_mapping acpi_goodix_reset_only_gpios[] = {
675 { GOODIX_GPIO_RST_NAME "-gpios", &first_gpio, 1 },
676 { },
677};
678
Hans de Goedea7d4b172020-03-24 11:23:21 -0700679static int goodix_resource(struct acpi_resource *ares, void *data)
680{
681 struct goodix_ts_data *ts = data;
682 struct device *dev = &ts->client->dev;
683 struct acpi_resource_gpio *gpio;
684
685 switch (ares->type) {
686 case ACPI_RESOURCE_TYPE_GPIO:
687 gpio = &ares->data.gpio;
688 if (gpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT) {
689 if (ts->gpio_int_idx == -1) {
690 ts->gpio_int_idx = ts->gpio_count;
691 } else {
692 dev_err(dev, "More then one GpioInt resource, ignoring ACPI GPIO resources\n");
693 ts->gpio_int_idx = -2;
694 }
695 }
696 ts->gpio_count++;
697 break;
698 default:
699 break;
700 }
701
702 return 0;
703}
704
705/*
706 * This function gets called in case we fail to get the irq GPIO directly
707 * because the ACPI tables lack GPIO-name to APCI _CRS index mappings
708 * (no _DSD UUID daffd814-6eba-4d8c-8a91-bc9bbf4aa301 data).
709 * In that case we add our own mapping and then goodix_get_gpio_config()
710 * retries to get the GPIOs based on the added mapping.
711 */
712static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
713{
714 const struct acpi_gpio_mapping *gpio_mapping = NULL;
715 struct device *dev = &ts->client->dev;
716 LIST_HEAD(resources);
717 int ret;
718
719 ts->gpio_count = 0;
720 ts->gpio_int_idx = -1;
721 ret = acpi_dev_get_resources(ACPI_COMPANION(dev), &resources,
722 goodix_resource, ts);
723 if (ret < 0) {
724 dev_err(dev, "Error getting ACPI resources: %d\n", ret);
725 return ret;
726 }
727
728 acpi_dev_free_resource_list(&resources);
729
730 if (ts->gpio_count == 2 && ts->gpio_int_idx == 0) {
731 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
732 gpio_mapping = acpi_goodix_int_first_gpios;
733 } else if (ts->gpio_count == 2 && ts->gpio_int_idx == 1) {
734 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
735 gpio_mapping = acpi_goodix_int_last_gpios;
Hans de Goedec5fca482020-03-24 11:24:31 -0700736 } else if (ts->gpio_count == 1 && ts->gpio_int_idx == -1 &&
737 acpi_has_method(ACPI_HANDLE(dev), "INTI") &&
738 acpi_has_method(ACPI_HANDLE(dev), "INTO")) {
739 dev_info(dev, "Using ACPI INTI and INTO methods for IRQ pin access\n");
740 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_METHOD;
741 gpio_mapping = acpi_goodix_reset_only_gpios;
Hans de Goede67abd9e2020-03-24 11:23:35 -0700742 } else if (is_byt() && ts->gpio_count == 2 && ts->gpio_int_idx == -1) {
743 dev_info(dev, "No ACPI GpioInt resource, assuming that the GPIO order is reset, int\n");
744 ts->irq_pin_access_method = IRQ_PIN_ACCESS_ACPI_GPIO;
745 gpio_mapping = acpi_goodix_int_last_gpios;
Hans de Goedea7d4b172020-03-24 11:23:21 -0700746 } else {
747 dev_warn(dev, "Unexpected ACPI resources: gpio_count %d, gpio_int_idx %d\n",
748 ts->gpio_count, ts->gpio_int_idx);
749 return -EINVAL;
750 }
751
752 return devm_acpi_dev_add_driver_gpios(dev, gpio_mapping);
753}
754#else
755static int goodix_add_acpi_gpio_mappings(struct goodix_ts_data *ts)
756{
757 return -EINVAL;
758}
759#endif /* CONFIG_X86 && CONFIG_ACPI */
760
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800761/**
762 * goodix_get_gpio_config - Get GPIO config from ACPI/DT
763 *
764 * @ts: goodix_ts_data pointer
765 */
766static int goodix_get_gpio_config(struct goodix_ts_data *ts)
767{
768 int error;
769 struct device *dev;
770 struct gpio_desc *gpiod;
Hans de Goedea7d4b172020-03-24 11:23:21 -0700771 bool added_acpi_mappings = false;
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800772
773 if (!ts->client)
774 return -EINVAL;
775 dev = &ts->client->dev;
776
Jagan Tekiae97fb52019-04-03 15:20:26 -0700777 ts->avdd28 = devm_regulator_get(dev, "AVDD28");
778 if (IS_ERR(ts->avdd28)) {
779 error = PTR_ERR(ts->avdd28);
780 if (error != -EPROBE_DEFER)
781 dev_err(dev,
782 "Failed to get AVDD28 regulator: %d\n", error);
783 return error;
784 }
785
786 ts->vddio = devm_regulator_get(dev, "VDDIO");
787 if (IS_ERR(ts->vddio)) {
788 error = PTR_ERR(ts->vddio);
789 if (error != -EPROBE_DEFER)
790 dev_err(dev,
791 "Failed to get VDDIO regulator: %d\n", error);
792 return error;
793 }
794
Hans de Goedea7d4b172020-03-24 11:23:21 -0700795retry_get_irq_gpio:
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800796 /* Get the interrupt GPIO pin number */
797 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_INT_NAME, GPIOD_IN);
798 if (IS_ERR(gpiod)) {
799 error = PTR_ERR(gpiod);
800 if (error != -EPROBE_DEFER)
801 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
802 GOODIX_GPIO_INT_NAME, error);
803 return error;
804 }
Hans de Goedea7d4b172020-03-24 11:23:21 -0700805 if (!gpiod && has_acpi_companion(dev) && !added_acpi_mappings) {
806 added_acpi_mappings = true;
807 if (goodix_add_acpi_gpio_mappings(ts) == 0)
808 goto retry_get_irq_gpio;
809 }
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800810
811 ts->gpiod_int = gpiod;
812
813 /* Get the reset line GPIO pin number */
814 gpiod = devm_gpiod_get_optional(dev, GOODIX_GPIO_RST_NAME, GPIOD_IN);
815 if (IS_ERR(gpiod)) {
816 error = PTR_ERR(gpiod);
817 if (error != -EPROBE_DEFER)
818 dev_dbg(dev, "Failed to get %s GPIO: %d\n",
819 GOODIX_GPIO_RST_NAME, error);
820 return error;
821 }
822
823 ts->gpiod_rst = gpiod;
824
Hans de Goedea7d4b172020-03-24 11:23:21 -0700825 switch (ts->irq_pin_access_method) {
826 case IRQ_PIN_ACCESS_ACPI_GPIO:
827 /*
828 * We end up here if goodix_add_acpi_gpio_mappings() has
829 * called devm_acpi_dev_add_driver_gpios() because the ACPI
830 * tables did not contain name to index mappings.
831 * Check that we successfully got both GPIOs after we've
832 * added our own acpi_gpio_mapping and if we did not get both
833 * GPIOs reset irq_pin_access_method to IRQ_PIN_ACCESS_NONE.
834 */
835 if (!ts->gpiod_int || !ts->gpiod_rst)
836 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
837 break;
Hans de Goedec5fca482020-03-24 11:24:31 -0700838 case IRQ_PIN_ACCESS_ACPI_METHOD:
839 if (!ts->gpiod_rst)
840 ts->irq_pin_access_method = IRQ_PIN_ACCESS_NONE;
841 break;
Hans de Goedea7d4b172020-03-24 11:23:21 -0700842 default:
843 if (ts->gpiod_int && ts->gpiod_rst) {
844 ts->reset_controller_at_probe = true;
845 ts->load_cfg_from_disk = true;
846 ts->irq_pin_access_method = IRQ_PIN_ACCESS_GPIO;
847 }
Hans de Goedee070a97b2020-03-24 11:12:35 -0700848 }
Hans de Goede49db3992020-03-24 11:12:10 -0700849
Irina Tirdeaec6e1b42015-12-17 15:57:34 -0800850 return 0;
851}
852
Bastien Noceraca96ea82014-10-31 09:26:16 -0700853/**
854 * goodix_read_config - Read the embedded configuration of the panel
855 *
856 * @ts: our goodix_ts_data pointer
857 *
858 * Must be called during probe
859 */
860static void goodix_read_config(struct goodix_ts_data *ts)
861{
862 u8 config[GOODIX_CONFIG_MAX_LENGTH];
Marcin Niestrojfafef982018-01-26 11:08:59 -0800863 int x_max, y_max;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700864 int error;
865
Marcin Niestroj25309002017-10-24 11:06:41 -0700866 error = goodix_i2c_read(ts->client, ts->chip->config_addr,
867 config, ts->chip->config_len);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700868 if (error) {
Marcin Niestrojfafef982018-01-26 11:08:59 -0800869 dev_warn(&ts->client->dev, "Error reading config: %d\n",
Bastien Noceraca96ea82014-10-31 09:26:16 -0700870 error);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700871 ts->int_trigger_type = GOODIX_INT_TRIGGER;
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800872 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700873 return;
874 }
875
Aleksei Mamlina7ac7c92015-03-06 16:38:16 -0800876 ts->int_trigger_type = config[TRIGGER_LOC] & 0x03;
877 ts->max_touch_num = config[MAX_CONTACTS_LOC] & 0x0f;
Bastien Nocera8b5a3592015-07-24 09:08:53 -0700878
Marcin Niestrojfafef982018-01-26 11:08:59 -0800879 x_max = get_unaligned_le16(&config[RESOLUTION_LOC]);
880 y_max = get_unaligned_le16(&config[RESOLUTION_LOC + 2]);
881 if (x_max && y_max) {
882 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_X, x_max - 1);
883 input_abs_set_max(ts->input_dev, ABS_MT_POSITION_Y, y_max - 1);
Karsten Merker57c80e82015-12-17 17:08:31 -0800884 }
Bastien Noceraca96ea82014-10-31 09:26:16 -0700885}
886
Bastien Noceraca96ea82014-10-31 09:26:16 -0700887/**
888 * goodix_read_version - Read goodix touchscreen version
889 *
Irina Tirdea68caf8582015-12-17 16:05:42 -0800890 * @ts: our goodix_ts_data pointer
Bastien Noceraca96ea82014-10-31 09:26:16 -0700891 */
Irina Tirdea68caf8582015-12-17 16:05:42 -0800892static int goodix_read_version(struct goodix_ts_data *ts)
Bastien Noceraca96ea82014-10-31 09:26:16 -0700893{
894 int error;
895 u8 buf[6];
Irina Tirdeae70b0302015-06-09 11:04:40 -0700896 char id_str[5];
Bastien Noceraca96ea82014-10-31 09:26:16 -0700897
Irina Tirdea68caf8582015-12-17 16:05:42 -0800898 error = goodix_i2c_read(ts->client, GOODIX_REG_ID, buf, sizeof(buf));
Bastien Noceraca96ea82014-10-31 09:26:16 -0700899 if (error) {
Irina Tirdea68caf8582015-12-17 16:05:42 -0800900 dev_err(&ts->client->dev, "read version failed: %d\n", error);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700901 return error;
902 }
903
Irina Tirdeae70b0302015-06-09 11:04:40 -0700904 memcpy(id_str, buf, 4);
905 id_str[4] = 0;
Irina Tirdea68caf8582015-12-17 16:05:42 -0800906 if (kstrtou16(id_str, 10, &ts->id))
907 ts->id = 0x1001;
Bastien Noceraca96ea82014-10-31 09:26:16 -0700908
Irina Tirdea68caf8582015-12-17 16:05:42 -0800909 ts->version = get_unaligned_le16(&buf[4]);
Irina Tirdeae70b0302015-06-09 11:04:40 -0700910
Irina Tirdea68caf8582015-12-17 16:05:42 -0800911 dev_info(&ts->client->dev, "ID %d, version: %04x\n", ts->id,
912 ts->version);
Bastien Noceraca96ea82014-10-31 09:26:16 -0700913
914 return 0;
915}
916
917/**
918 * goodix_i2c_test - I2C test function to check if the device answers.
919 *
920 * @client: the i2c client
921 */
922static int goodix_i2c_test(struct i2c_client *client)
923{
924 int retry = 0;
925 int error;
926 u8 test;
927
928 while (retry++ < 2) {
Marcin Niestroj25309002017-10-24 11:06:41 -0700929 error = goodix_i2c_read(client, GOODIX_REG_ID,
Bastien Noceraca96ea82014-10-31 09:26:16 -0700930 &test, 1);
931 if (!error)
932 return 0;
933
934 dev_err(&client->dev, "i2c test failed attempt %d: %d\n",
935 retry, error);
936 msleep(20);
937 }
938
939 return error;
940}
941
942/**
Irina Tirdea68caf8582015-12-17 16:05:42 -0800943 * goodix_configure_dev - Finish device initialization
944 *
945 * @ts: our goodix_ts_data pointer
946 *
947 * Must be called from probe to finish initialization of the device.
948 * Contains the common initialization code for both devices that
949 * declare gpio pins and devices that do not. It is either called
950 * directly from probe or from request_firmware_wait callback.
951 */
952static int goodix_configure_dev(struct goodix_ts_data *ts)
953{
954 int error;
Irina Tirdea68caf8582015-12-17 16:05:42 -0800955
Marcin Niestrojfafef982018-01-26 11:08:59 -0800956 ts->int_trigger_type = GOODIX_INT_TRIGGER;
957 ts->max_touch_num = GOODIX_MAX_CONTACTS;
Karsten Merkerad48cf52015-12-17 17:02:53 -0800958
Marcin Niestrojfafef982018-01-26 11:08:59 -0800959 ts->input_dev = devm_input_allocate_device(&ts->client->dev);
960 if (!ts->input_dev) {
961 dev_err(&ts->client->dev, "Failed to allocate input device.");
962 return -ENOMEM;
963 }
964
965 ts->input_dev->name = "Goodix Capacitive TouchScreen";
966 ts->input_dev->phys = "input/ts";
967 ts->input_dev->id.bustype = BUS_I2C;
968 ts->input_dev->id.vendor = 0x0416;
969 ts->input_dev->id.product = ts->id;
970 ts->input_dev->id.version = ts->version;
971
972 /* Capacitive Windows/Home button on some devices */
973 input_set_capability(ts->input_dev, EV_KEY, KEY_LEFTMETA);
974
975 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_X);
976 input_set_capability(ts->input_dev, EV_ABS, ABS_MT_POSITION_Y);
977 input_set_abs_params(ts->input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
978 input_set_abs_params(ts->input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
979
980 /* Read configuration and apply touchscreen parameters */
Irina Tirdea68caf8582015-12-17 16:05:42 -0800981 goodix_read_config(ts);
982
Marcin Niestrojfafef982018-01-26 11:08:59 -0800983 /* Try overriding touchscreen parameters via device properties */
984 touchscreen_parse_properties(ts->input_dev, true, &ts->prop);
985
986 if (!ts->prop.max_x || !ts->prop.max_y || !ts->max_touch_num) {
Guido Güntherc1c00aa2019-02-16 23:04:43 -0800987 dev_err(&ts->client->dev,
988 "Invalid config (%d, %d, %d), using defaults\n",
989 ts->prop.max_x, ts->prop.max_y, ts->max_touch_num);
Marcin Niestrojfafef982018-01-26 11:08:59 -0800990 ts->prop.max_x = GOODIX_MAX_WIDTH - 1;
991 ts->prop.max_y = GOODIX_MAX_HEIGHT - 1;
992 ts->max_touch_num = GOODIX_MAX_CONTACTS;
993 input_abs_set_max(ts->input_dev,
994 ABS_MT_POSITION_X, ts->prop.max_x);
995 input_abs_set_max(ts->input_dev,
996 ABS_MT_POSITION_Y, ts->prop.max_y);
997 }
998
999 if (dmi_check_system(rotated_screen)) {
1000 ts->prop.invert_x = true;
1001 ts->prop.invert_y = true;
1002 dev_dbg(&ts->client->dev,
1003 "Applying '180 degrees rotated screen' quirk\n");
1004 }
1005
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -07001006 if (dmi_check_system(nine_bytes_report)) {
1007 ts->contact_size = 9;
1008
1009 dev_dbg(&ts->client->dev,
1010 "Non-standard 9-bytes report format quirk\n");
1011 }
1012
Sergei A. Trusov1dd5ddc2020-03-05 19:53:06 -08001013 if (dmi_check_system(inverted_x_screen)) {
1014 ts->prop.invert_x = true;
1015 dev_dbg(&ts->client->dev,
1016 "Applying 'inverted x screen' quirk\n");
1017 }
1018
Marcin Niestrojfafef982018-01-26 11:08:59 -08001019 error = input_mt_init_slots(ts->input_dev, ts->max_touch_num,
1020 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1021 if (error) {
1022 dev_err(&ts->client->dev,
1023 "Failed to initialize MT slots: %d", error);
Irina Tirdea68caf8582015-12-17 16:05:42 -08001024 return error;
Marcin Niestrojfafef982018-01-26 11:08:59 -08001025 }
1026
1027 error = input_register_device(ts->input_dev);
1028 if (error) {
1029 dev_err(&ts->client->dev,
1030 "Failed to register input device: %d", error);
1031 return error;
1032 }
Irina Tirdea68caf8582015-12-17 16:05:42 -08001033
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001034 ts->irq_flags = goodix_irq_flags[ts->int_trigger_type] | IRQF_ONESHOT;
1035 error = goodix_request_irq(ts);
Irina Tirdea68caf8582015-12-17 16:05:42 -08001036 if (error) {
1037 dev_err(&ts->client->dev, "request IRQ failed: %d\n", error);
1038 return error;
1039 }
1040
1041 return 0;
1042}
1043
1044/**
1045 * goodix_config_cb - Callback to finish device init
1046 *
1047 * @ts: our goodix_ts_data pointer
1048 *
1049 * request_firmware_wait callback that finishes
1050 * initialization of the device.
1051 */
1052static void goodix_config_cb(const struct firmware *cfg, void *ctx)
1053{
1054 struct goodix_ts_data *ts = ctx;
1055 int error;
1056
1057 if (cfg) {
1058 /* send device configuration to the firmware */
1059 error = goodix_send_cfg(ts, cfg);
1060 if (error)
1061 goto err_release_cfg;
1062 }
1063
1064 goodix_configure_dev(ts);
1065
1066err_release_cfg:
1067 release_firmware(cfg);
1068 complete_all(&ts->firmware_loading_complete);
1069}
1070
Jagan Tekiae97fb52019-04-03 15:20:26 -07001071static void goodix_disable_regulators(void *arg)
1072{
1073 struct goodix_ts_data *ts = arg;
1074
1075 regulator_disable(ts->vddio);
1076 regulator_disable(ts->avdd28);
1077}
1078
Bastien Noceraca96ea82014-10-31 09:26:16 -07001079static int goodix_ts_probe(struct i2c_client *client,
1080 const struct i2c_device_id *id)
1081{
1082 struct goodix_ts_data *ts;
Bastien Noceraca96ea82014-10-31 09:26:16 -07001083 int error;
Bastien Noceraca96ea82014-10-31 09:26:16 -07001084
1085 dev_dbg(&client->dev, "I2C Address: 0x%02x\n", client->addr);
1086
1087 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1088 dev_err(&client->dev, "I2C check functionality failed.\n");
1089 return -ENXIO;
1090 }
1091
1092 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1093 if (!ts)
1094 return -ENOMEM;
1095
1096 ts->client = client;
1097 i2c_set_clientdata(client, ts);
Irina Tirdea68caf8582015-12-17 16:05:42 -08001098 init_completion(&ts->firmware_loading_complete);
Yauhen Kharuzhybd3b8482019-09-16 16:32:24 -07001099 ts->contact_size = GOODIX_CONTACT_SIZE;
Bastien Noceraca96ea82014-10-31 09:26:16 -07001100
Irina Tirdeaec6e1b42015-12-17 15:57:34 -08001101 error = goodix_get_gpio_config(ts);
1102 if (error)
1103 return error;
1104
Jagan Tekiae97fb52019-04-03 15:20:26 -07001105 /* power up the controller */
1106 error = regulator_enable(ts->avdd28);
1107 if (error) {
1108 dev_err(&client->dev,
1109 "Failed to enable AVDD28 regulator: %d\n",
1110 error);
1111 return error;
1112 }
1113
1114 error = regulator_enable(ts->vddio);
1115 if (error) {
1116 dev_err(&client->dev,
1117 "Failed to enable VDDIO regulator: %d\n",
1118 error);
1119 regulator_disable(ts->avdd28);
1120 return error;
1121 }
1122
1123 error = devm_add_action_or_reset(&client->dev,
1124 goodix_disable_regulators, ts);
1125 if (error)
1126 return error;
1127
Hans de Goede1921dac2020-03-24 11:23:04 -07001128 if (ts->reset_controller_at_probe) {
Irina Tirdeaec6e1b42015-12-17 15:57:34 -08001129 /* reset the controller */
1130 error = goodix_reset(ts);
1131 if (error) {
1132 dev_err(&client->dev, "Controller reset failed.\n");
1133 return error;
1134 }
1135 }
1136
Bastien Noceraca96ea82014-10-31 09:26:16 -07001137 error = goodix_i2c_test(client);
1138 if (error) {
1139 dev_err(&client->dev, "I2C communication failure: %d\n", error);
1140 return error;
1141 }
1142
Irina Tirdea68caf8582015-12-17 16:05:42 -08001143 error = goodix_read_version(ts);
Bastien Noceraca96ea82014-10-31 09:26:16 -07001144 if (error) {
1145 dev_err(&client->dev, "Read version failed.\n");
1146 return error;
1147 }
1148
Marcin Niestroj25309002017-10-24 11:06:41 -07001149 ts->chip = goodix_get_chip_data(ts->id);
Irina Tirdeaa779fbc2015-12-17 15:55:21 -08001150
Hans de Goedee070a97b2020-03-24 11:12:35 -07001151 if (ts->load_cfg_from_disk) {
Irina Tirdea68caf8582015-12-17 16:05:42 -08001152 /* update device config */
1153 ts->cfg_name = devm_kasprintf(&client->dev, GFP_KERNEL,
1154 "goodix_%d_cfg.bin", ts->id);
1155 if (!ts->cfg_name)
1156 return -ENOMEM;
Bastien Noceraca96ea82014-10-31 09:26:16 -07001157
Irina Tirdea68caf8582015-12-17 16:05:42 -08001158 error = request_firmware_nowait(THIS_MODULE, true, ts->cfg_name,
1159 &client->dev, GFP_KERNEL, ts,
1160 goodix_config_cb);
1161 if (error) {
1162 dev_err(&client->dev,
1163 "Failed to invoke firmware loader: %d\n",
1164 error);
1165 return error;
1166 }
Bastien Noceraca96ea82014-10-31 09:26:16 -07001167
Irina Tirdea68caf8582015-12-17 16:05:42 -08001168 return 0;
1169 } else {
1170 error = goodix_configure_dev(ts);
1171 if (error)
1172 return error;
Bastien Noceraca96ea82014-10-31 09:26:16 -07001173 }
1174
1175 return 0;
1176}
1177
Irina Tirdea68caf8582015-12-17 16:05:42 -08001178static int goodix_ts_remove(struct i2c_client *client)
1179{
1180 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1181
Hans de Goedee070a97b2020-03-24 11:12:35 -07001182 if (ts->load_cfg_from_disk)
Irina Tirdea68caf8582015-12-17 16:05:42 -08001183 wait_for_completion(&ts->firmware_loading_complete);
1184
1185 return 0;
1186}
1187
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001188static int __maybe_unused goodix_suspend(struct device *dev)
1189{
1190 struct i2c_client *client = to_i2c_client(dev);
1191 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1192 int error;
1193
Hans de Goedee070a97b2020-03-24 11:12:35 -07001194 if (ts->load_cfg_from_disk)
1195 wait_for_completion(&ts->firmware_loading_complete);
1196
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001197 /* We need gpio pins to suspend/resume */
Hans de Goede49db3992020-03-24 11:12:10 -07001198 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
Hans de Goedefaec44b2018-01-12 00:36:48 -08001199 disable_irq(client->irq);
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001200 return 0;
Hans de Goedefaec44b2018-01-12 00:36:48 -08001201 }
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001202
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001203 /* Free IRQ as IRQ pin is used as output in the suspend sequence */
1204 goodix_free_irq(ts);
1205
1206 /* Output LOW on the INT pin for 5 ms */
Hans de Goede49db3992020-03-24 11:12:10 -07001207 error = goodix_irq_direction_output(ts, 0);
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001208 if (error) {
1209 goodix_request_irq(ts);
1210 return error;
1211 }
1212
1213 usleep_range(5000, 6000);
1214
1215 error = goodix_i2c_write_u8(ts->client, GOODIX_REG_COMMAND,
1216 GOODIX_CMD_SCREEN_OFF);
1217 if (error) {
1218 dev_err(&ts->client->dev, "Screen off command failed\n");
Hans de Goede49db3992020-03-24 11:12:10 -07001219 goodix_irq_direction_input(ts);
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001220 goodix_request_irq(ts);
1221 return -EAGAIN;
1222 }
1223
1224 /*
1225 * The datasheet specifies that the interval between sending screen-off
1226 * command and wake-up should be longer than 58 ms. To avoid waking up
1227 * sooner, delay 58ms here.
1228 */
1229 msleep(58);
1230 return 0;
1231}
1232
1233static int __maybe_unused goodix_resume(struct device *dev)
1234{
1235 struct i2c_client *client = to_i2c_client(dev);
1236 struct goodix_ts_data *ts = i2c_get_clientdata(client);
1237 int error;
1238
Hans de Goede49db3992020-03-24 11:12:10 -07001239 if (ts->irq_pin_access_method == IRQ_PIN_ACCESS_NONE) {
Hans de Goedefaec44b2018-01-12 00:36:48 -08001240 enable_irq(client->irq);
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001241 return 0;
Hans de Goedefaec44b2018-01-12 00:36:48 -08001242 }
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001243
1244 /*
1245 * Exit sleep mode by outputting HIGH level to INT pin
1246 * for 2ms~5ms.
1247 */
Hans de Goede49db3992020-03-24 11:12:10 -07001248 error = goodix_irq_direction_output(ts, 1);
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001249 if (error)
1250 return error;
1251
1252 usleep_range(2000, 5000);
1253
1254 error = goodix_int_sync(ts);
1255 if (error)
1256 return error;
1257
1258 error = goodix_request_irq(ts);
1259 if (error)
1260 return error;
1261
1262 return 0;
1263}
1264
1265static SIMPLE_DEV_PM_OPS(goodix_pm_ops, goodix_suspend, goodix_resume);
1266
Bastien Noceraca96ea82014-10-31 09:26:16 -07001267static const struct i2c_device_id goodix_ts_id[] = {
1268 { "GDIX1001:00", 0 },
1269 { }
1270};
Javier Martinez Canillas2e9e9102015-07-30 10:38:52 -07001271MODULE_DEVICE_TABLE(i2c, goodix_ts_id);
Bastien Noceraca96ea82014-10-31 09:26:16 -07001272
Aleksei Mamlin771d8f12015-03-06 16:43:38 -08001273#ifdef CONFIG_ACPI
Bastien Noceraca96ea82014-10-31 09:26:16 -07001274static const struct acpi_device_id goodix_acpi_match[] = {
1275 { "GDIX1001", 0 },
Ethan Lee5ca4d1a2018-05-31 16:13:17 -07001276 { "GDIX1002", 0 },
Bastien Noceraca96ea82014-10-31 09:26:16 -07001277 { }
1278};
1279MODULE_DEVICE_TABLE(acpi, goodix_acpi_match);
Aleksei Mamlin771d8f12015-03-06 16:43:38 -08001280#endif
1281
1282#ifdef CONFIG_OF
1283static const struct of_device_id goodix_of_match[] = {
Marcin Niestroj25309002017-10-24 11:06:41 -07001284 { .compatible = "goodix,gt1151" },
Jagan Tekia5f50c52019-04-03 16:05:34 -07001285 { .compatible = "goodix,gt5663" },
Guido Günther84ef1b32019-02-16 23:03:13 -08001286 { .compatible = "goodix,gt5688" },
Aleksei Mamlin771d8f12015-03-06 16:43:38 -08001287 { .compatible = "goodix,gt911" },
1288 { .compatible = "goodix,gt9110" },
1289 { .compatible = "goodix,gt912" },
1290 { .compatible = "goodix,gt927" },
1291 { .compatible = "goodix,gt9271" },
1292 { .compatible = "goodix,gt928" },
1293 { .compatible = "goodix,gt967" },
1294 { }
1295};
1296MODULE_DEVICE_TABLE(of, goodix_of_match);
1297#endif
Bastien Noceraca96ea82014-10-31 09:26:16 -07001298
1299static struct i2c_driver goodix_ts_driver = {
1300 .probe = goodix_ts_probe,
Irina Tirdea68caf8582015-12-17 16:05:42 -08001301 .remove = goodix_ts_remove,
Bastien Noceraca96ea82014-10-31 09:26:16 -07001302 .id_table = goodix_ts_id,
1303 .driver = {
1304 .name = "Goodix-TS",
Aleksei Mamlin771d8f12015-03-06 16:43:38 -08001305 .acpi_match_table = ACPI_PTR(goodix_acpi_match),
1306 .of_match_table = of_match_ptr(goodix_of_match),
Irina Tirdea5ab09d62015-12-17 16:43:39 -08001307 .pm = &goodix_pm_ops,
Bastien Noceraca96ea82014-10-31 09:26:16 -07001308 },
1309};
1310module_i2c_driver(goodix_ts_driver);
1311
1312MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
1313MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>");
1314MODULE_DESCRIPTION("Goodix touchscreen driver");
1315MODULE_LICENSE("GPL v2");