GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / input / touchscreen / silead.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3  * Copyright (C) 2014-2015, Intel Corporation
4  *
5  * Derived from:
6  *  gslX68X.c
7  *  Copyright (C) 2010-2015, Shanghai Sileadinc Co.Ltd
8  *
9  * -------------------------------------------------------------------------
10  */
11
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/acpi.h>
15 #include <linux/interrupt.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/delay.h>
18 #include <linux/firmware.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/input/touchscreen.h>
22 #include <linux/pm.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/irq.h>
25 #include <linux/regulator/consumer.h>
26
27 #include <asm/unaligned.h>
28
29 #define SILEAD_TS_NAME          "silead_ts"
30
31 #define SILEAD_REG_RESET        0xE0
32 #define SILEAD_REG_DATA         0x80
33 #define SILEAD_REG_TOUCH_NR     0x80
34 #define SILEAD_REG_POWER        0xBC
35 #define SILEAD_REG_CLOCK        0xE4
36 #define SILEAD_REG_STATUS       0xB0
37 #define SILEAD_REG_ID           0xFC
38 #define SILEAD_REG_MEM_CHECK    0xB0
39
40 #define SILEAD_STATUS_OK        0x5A5A5A5A
41 #define SILEAD_TS_DATA_LEN      44
42 #define SILEAD_CLOCK            0x04
43
44 #define SILEAD_CMD_RESET        0x88
45 #define SILEAD_CMD_START        0x00
46
47 #define SILEAD_POINT_DATA_LEN   0x04
48 #define SILEAD_POINT_Y_OFF      0x00
49 #define SILEAD_POINT_Y_MSB_OFF  0x01
50 #define SILEAD_POINT_X_OFF      0x02
51 #define SILEAD_POINT_X_MSB_OFF  0x03
52 #define SILEAD_EXTRA_DATA_MASK  0xF0
53
54 #define SILEAD_CMD_SLEEP_MIN    10000
55 #define SILEAD_CMD_SLEEP_MAX    20000
56 #define SILEAD_POWER_SLEEP      20
57 #define SILEAD_STARTUP_SLEEP    30
58
59 #define SILEAD_MAX_FINGERS      10
60
61 enum silead_ts_power {
62         SILEAD_POWER_ON  = 1,
63         SILEAD_POWER_OFF = 0
64 };
65
66 struct silead_ts_data {
67         struct i2c_client *client;
68         struct gpio_desc *gpio_power;
69         struct input_dev *input;
70         struct regulator_bulk_data regulators[2];
71         char fw_name[64];
72         struct touchscreen_properties prop;
73         u32 max_fingers;
74         u32 chip_id;
75         struct input_mt_pos pos[SILEAD_MAX_FINGERS];
76         int slots[SILEAD_MAX_FINGERS];
77         int id[SILEAD_MAX_FINGERS];
78 };
79
80 struct silead_fw_data {
81         u32 offset;
82         u32 val;
83 };
84
85 static int silead_ts_request_input_dev(struct silead_ts_data *data)
86 {
87         struct device *dev = &data->client->dev;
88         int error;
89
90         data->input = devm_input_allocate_device(dev);
91         if (!data->input) {
92                 dev_err(dev,
93                         "Failed to allocate input device\n");
94                 return -ENOMEM;
95         }
96
97         input_set_abs_params(data->input, ABS_MT_POSITION_X, 0, 4095, 0, 0);
98         input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0, 4095, 0, 0);
99         touchscreen_parse_properties(data->input, true, &data->prop);
100
101         input_mt_init_slots(data->input, data->max_fingers,
102                             INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED |
103                             INPUT_MT_TRACK);
104
105         if (device_property_read_bool(dev, "silead,home-button"))
106                 input_set_capability(data->input, EV_KEY, KEY_LEFTMETA);
107
108         data->input->name = SILEAD_TS_NAME;
109         data->input->phys = "input/ts";
110         data->input->id.bustype = BUS_I2C;
111
112         error = input_register_device(data->input);
113         if (error) {
114                 dev_err(dev, "Failed to register input device: %d\n", error);
115                 return error;
116         }
117
118         return 0;
119 }
120
121 static void silead_ts_set_power(struct i2c_client *client,
122                                 enum silead_ts_power state)
123 {
124         struct silead_ts_data *data = i2c_get_clientdata(client);
125
126         if (data->gpio_power) {
127                 gpiod_set_value_cansleep(data->gpio_power, state);
128                 msleep(SILEAD_POWER_SLEEP);
129         }
130 }
131
132 static void silead_ts_read_data(struct i2c_client *client)
133 {
134         struct silead_ts_data *data = i2c_get_clientdata(client);
135         struct input_dev *input = data->input;
136         struct device *dev = &client->dev;
137         u8 *bufp, buf[SILEAD_TS_DATA_LEN];
138         int touch_nr, softbutton, error, i;
139         bool softbutton_pressed = false;
140
141         error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
142                                               SILEAD_TS_DATA_LEN, buf);
143         if (error < 0) {
144                 dev_err(dev, "Data read error %d\n", error);
145                 return;
146         }
147
148         if (buf[0] > data->max_fingers) {
149                 dev_warn(dev, "More touches reported then supported %d > %d\n",
150                          buf[0], data->max_fingers);
151                 buf[0] = data->max_fingers;
152         }
153
154         touch_nr = 0;
155         bufp = buf + SILEAD_POINT_DATA_LEN;
156         for (i = 0; i < buf[0]; i++, bufp += SILEAD_POINT_DATA_LEN) {
157                 softbutton = (bufp[SILEAD_POINT_Y_MSB_OFF] &
158                               SILEAD_EXTRA_DATA_MASK) >> 4;
159
160                 if (softbutton) {
161                         /*
162                          * For now only respond to softbutton == 0x01, some
163                          * tablets *without* a capacative button send 0x04
164                          * when crossing the edges of the screen.
165                          */
166                         if (softbutton == 0x01)
167                                 softbutton_pressed = true;
168
169                         continue;
170                 }
171
172                 /*
173                  * Bits 4-7 are the touch id, note not all models have
174                  * hardware touch ids so atm we don't use these.
175                  */
176                 data->id[touch_nr] = (bufp[SILEAD_POINT_X_MSB_OFF] &
177                                       SILEAD_EXTRA_DATA_MASK) >> 4;
178                 touchscreen_set_mt_pos(&data->pos[touch_nr], &data->prop,
179                         get_unaligned_le16(&bufp[SILEAD_POINT_X_OFF]) & 0xfff,
180                         get_unaligned_le16(&bufp[SILEAD_POINT_Y_OFF]) & 0xfff);
181                 touch_nr++;
182         }
183
184         input_mt_assign_slots(input, data->slots, data->pos, touch_nr, 0);
185
186         for (i = 0; i < touch_nr; i++) {
187                 input_mt_slot(input, data->slots[i]);
188                 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
189                 input_report_abs(input, ABS_MT_POSITION_X, data->pos[i].x);
190                 input_report_abs(input, ABS_MT_POSITION_Y, data->pos[i].y);
191
192                 dev_dbg(dev, "x=%d y=%d hw_id=%d sw_id=%d\n", data->pos[i].x,
193                         data->pos[i].y, data->id[i], data->slots[i]);
194         }
195
196         input_mt_sync_frame(input);
197         input_report_key(input, KEY_LEFTMETA, softbutton_pressed);
198         input_sync(input);
199 }
200
201 static int silead_ts_init(struct i2c_client *client)
202 {
203         struct silead_ts_data *data = i2c_get_clientdata(client);
204         int error;
205
206         error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
207                                           SILEAD_CMD_RESET);
208         if (error)
209                 goto i2c_write_err;
210         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
211
212         error = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
213                                         data->max_fingers);
214         if (error)
215                 goto i2c_write_err;
216         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
217
218         error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
219                                           SILEAD_CLOCK);
220         if (error)
221                 goto i2c_write_err;
222         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
223
224         error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
225                                           SILEAD_CMD_START);
226         if (error)
227                 goto i2c_write_err;
228         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
229
230         return 0;
231
232 i2c_write_err:
233         dev_err(&client->dev, "Registers clear error %d\n", error);
234         return error;
235 }
236
237 static int silead_ts_reset(struct i2c_client *client)
238 {
239         int error;
240
241         error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
242                                           SILEAD_CMD_RESET);
243         if (error)
244                 goto i2c_write_err;
245         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
246
247         error = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK,
248                                           SILEAD_CLOCK);
249         if (error)
250                 goto i2c_write_err;
251         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
252
253         error = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
254                                           SILEAD_CMD_START);
255         if (error)
256                 goto i2c_write_err;
257         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
258
259         return 0;
260
261 i2c_write_err:
262         dev_err(&client->dev, "Chip reset error %d\n", error);
263         return error;
264 }
265
266 static int silead_ts_startup(struct i2c_client *client)
267 {
268         int error;
269
270         error = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
271         if (error) {
272                 dev_err(&client->dev, "Startup error %d\n", error);
273                 return error;
274         }
275
276         msleep(SILEAD_STARTUP_SLEEP);
277
278         return 0;
279 }
280
281 static int silead_ts_load_fw(struct i2c_client *client)
282 {
283         struct device *dev = &client->dev;
284         struct silead_ts_data *data = i2c_get_clientdata(client);
285         unsigned int fw_size, i;
286         const struct firmware *fw;
287         struct silead_fw_data *fw_data;
288         int error;
289
290         dev_dbg(dev, "Firmware file name: %s", data->fw_name);
291
292         error = reject_firmware(&fw, data->fw_name, dev);
293         if (error) {
294                 dev_err(dev, "Firmware request error %d\n", error);
295                 return error;
296         }
297
298         fw_size = fw->size / sizeof(*fw_data);
299         fw_data = (struct silead_fw_data *)fw->data;
300
301         for (i = 0; i < fw_size; i++) {
302                 error = i2c_smbus_write_i2c_block_data(client,
303                                                        fw_data[i].offset,
304                                                        4,
305                                                        (u8 *)&fw_data[i].val);
306                 if (error) {
307                         dev_err(dev, "Firmware load error %d\n", error);
308                         break;
309                 }
310         }
311
312         release_firmware(fw);
313         return error ?: 0;
314 }
315
316 static u32 silead_ts_get_status(struct i2c_client *client)
317 {
318         int error;
319         __le32 status;
320
321         error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS,
322                                               sizeof(status), (u8 *)&status);
323         if (error < 0) {
324                 dev_err(&client->dev, "Status read error %d\n", error);
325                 return error;
326         }
327
328         return le32_to_cpu(status);
329 }
330
331 static int silead_ts_get_id(struct i2c_client *client)
332 {
333         struct silead_ts_data *data = i2c_get_clientdata(client);
334         __le32 chip_id;
335         int error;
336
337         error = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID,
338                                               sizeof(chip_id), (u8 *)&chip_id);
339         if (error < 0)
340                 return error;
341
342         data->chip_id = le32_to_cpu(chip_id);
343         dev_info(&client->dev, "Silead chip ID: 0x%8X", data->chip_id);
344
345         return 0;
346 }
347
348 static int silead_ts_setup(struct i2c_client *client)
349 {
350         int error;
351         u32 status;
352
353         /*
354          * Some buggy BIOS-es bring up the chip in a stuck state where it
355          * blocks the I2C bus. The following steps are necessary to
356          * unstuck the chip / bus:
357          * 1. Turn off the Silead chip.
358          * 2. Try to do an I2C transfer with the chip, this will fail in
359          *    response to which the I2C-bus-driver will call:
360          *    i2c_recover_bus() which will unstuck the I2C-bus. Note the
361          *    unstuck-ing of the I2C bus only works if we first drop the
362          *    chip off the bus by turning it off.
363          * 3. Turn the chip back on.
364          *
365          * On the x86/ACPI systems were this problem is seen, step 1. and
366          * 3. require making ACPI calls and dealing with ACPI Power
367          * Resources. The workaround below runtime-suspends the chip to
368          * turn it off, leaving it up to the ACPI subsystem to deal with
369          * this.
370          */
371
372         if (device_property_read_bool(&client->dev,
373                                       "silead,stuck-controller-bug")) {
374                 pm_runtime_set_active(&client->dev);
375                 pm_runtime_enable(&client->dev);
376                 pm_runtime_allow(&client->dev);
377
378                 pm_runtime_suspend(&client->dev);
379
380                 dev_warn(&client->dev, FW_BUG "Stuck I2C bus: please ignore the next 'controller timed out' error\n");
381                 silead_ts_get_id(client);
382
383                 /* The forbid will also resume the device */
384                 pm_runtime_forbid(&client->dev);
385                 pm_runtime_disable(&client->dev);
386         }
387
388         silead_ts_set_power(client, SILEAD_POWER_OFF);
389         silead_ts_set_power(client, SILEAD_POWER_ON);
390
391         error = silead_ts_get_id(client);
392         if (error) {
393                 dev_err(&client->dev, "Chip ID read error %d\n", error);
394                 return error;
395         }
396
397         error = silead_ts_init(client);
398         if (error)
399                 return error;
400
401         error = silead_ts_reset(client);
402         if (error)
403                 return error;
404
405         error = silead_ts_load_fw(client);
406         if (error)
407                 return error;
408
409         error = silead_ts_startup(client);
410         if (error)
411                 return error;
412
413         status = silead_ts_get_status(client);
414         if (status != SILEAD_STATUS_OK) {
415                 dev_err(&client->dev,
416                         "Initialization error, status: 0x%X\n", status);
417                 return -ENODEV;
418         }
419
420         return 0;
421 }
422
423 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
424 {
425         struct silead_ts_data *data = id;
426         struct i2c_client *client = data->client;
427
428         silead_ts_read_data(client);
429
430         return IRQ_HANDLED;
431 }
432
433 static void silead_ts_read_props(struct i2c_client *client)
434 {
435         struct silead_ts_data *data = i2c_get_clientdata(client);
436         struct device *dev = &client->dev;
437         const char *str;
438         int error;
439
440         error = device_property_read_u32(dev, "silead,max-fingers",
441                                          &data->max_fingers);
442         if (error) {
443                 dev_dbg(dev, "Max fingers read error %d\n", error);
444                 data->max_fingers = 5; /* Most devices handle up-to 5 fingers */
445         }
446
447         error = device_property_read_string(dev, "firmware-name", &str);
448         if (!error)
449                 /*(DEBLOBBED)*/;
450         else
451                 dev_dbg(dev, "Firmware file name read error. Using default.");
452 }
453
454 #ifdef CONFIG_ACPI
455 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
456                                          const struct i2c_device_id *id)
457 {
458         const struct acpi_device_id *acpi_id;
459         struct device *dev = &data->client->dev;
460         int i;
461
462         if (ACPI_HANDLE(dev)) {
463                 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
464                 if (!acpi_id)
465                         return -ENODEV;
466
467                 snprintf(data->fw_name, sizeof(data->fw_name),
468                          "/*(DEBLOBBED)*/", acpi_id->id);
469
470                 for (i = 0; i < strlen(data->fw_name); i++)
471                         data->fw_name[i] = tolower(data->fw_name[i]);
472         } else {
473                 snprintf(data->fw_name, sizeof(data->fw_name),
474                          "/*(DEBLOBBED)*/", id->name);
475         }
476
477         return 0;
478 }
479 #else
480 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
481                                          const struct i2c_device_id *id)
482 {
483         snprintf(data->fw_name, sizeof(data->fw_name),
484                  "/*(DEBLOBBED)*/", id->name);
485         return 0;
486 }
487 #endif
488
489 static void silead_disable_regulator(void *arg)
490 {
491         struct silead_ts_data *data = arg;
492
493         regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators);
494 }
495
496 static int silead_ts_probe(struct i2c_client *client,
497                            const struct i2c_device_id *id)
498 {
499         struct silead_ts_data *data;
500         struct device *dev = &client->dev;
501         int error;
502
503         if (!i2c_check_functionality(client->adapter,
504                                      I2C_FUNC_I2C |
505                                      I2C_FUNC_SMBUS_READ_I2C_BLOCK |
506                                      I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
507                 dev_err(dev, "I2C functionality check failed\n");
508                 return -ENXIO;
509         }
510
511         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
512         if (!data)
513                 return -ENOMEM;
514
515         i2c_set_clientdata(client, data);
516         data->client = client;
517
518         error = silead_ts_set_default_fw_name(data, id);
519         if (error)
520                 return error;
521
522         silead_ts_read_props(client);
523
524         /* We must have the IRQ provided by DT or ACPI subsytem */
525         if (client->irq <= 0)
526                 return -ENODEV;
527
528         data->regulators[0].supply = "vddio";
529         data->regulators[1].supply = "avdd";
530         error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators),
531                                         data->regulators);
532         if (error)
533                 return error;
534
535         /*
536          * Enable regulators at probe and disable them at remove, we need
537          * to keep the chip powered otherwise it forgets its firmware.
538          */
539         error = regulator_bulk_enable(ARRAY_SIZE(data->regulators),
540                                       data->regulators);
541         if (error)
542                 return error;
543
544         error = devm_add_action_or_reset(dev, silead_disable_regulator, data);
545         if (error)
546                 return error;
547
548         /* Power GPIO pin */
549         data->gpio_power = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
550         if (IS_ERR(data->gpio_power)) {
551                 if (PTR_ERR(data->gpio_power) != -EPROBE_DEFER)
552                         dev_err(dev, "Shutdown GPIO request failed\n");
553                 return PTR_ERR(data->gpio_power);
554         }
555
556         error = silead_ts_setup(client);
557         if (error)
558                 return error;
559
560         error = silead_ts_request_input_dev(data);
561         if (error)
562                 return error;
563
564         error = devm_request_threaded_irq(dev, client->irq,
565                                           NULL, silead_ts_threaded_irq_handler,
566                                           IRQF_ONESHOT, client->name, data);
567         if (error) {
568                 if (error != -EPROBE_DEFER)
569                         dev_err(dev, "IRQ request failed %d\n", error);
570                 return error;
571         }
572
573         return 0;
574 }
575
576 static int __maybe_unused silead_ts_suspend(struct device *dev)
577 {
578         struct i2c_client *client = to_i2c_client(dev);
579
580         disable_irq(client->irq);
581         silead_ts_set_power(client, SILEAD_POWER_OFF);
582         return 0;
583 }
584
585 static int __maybe_unused silead_ts_resume(struct device *dev)
586 {
587         struct i2c_client *client = to_i2c_client(dev);
588         bool second_try = false;
589         int error, status;
590
591         silead_ts_set_power(client, SILEAD_POWER_ON);
592
593  retry:
594         error = silead_ts_reset(client);
595         if (error)
596                 return error;
597
598         if (second_try) {
599                 error = silead_ts_load_fw(client);
600                 if (error)
601                         return error;
602         }
603
604         error = silead_ts_startup(client);
605         if (error)
606                 return error;
607
608         status = silead_ts_get_status(client);
609         if (status != SILEAD_STATUS_OK) {
610                 if (!second_try) {
611                         second_try = true;
612                         dev_dbg(dev, "Reloading firmware after unsuccessful resume\n");
613                         goto retry;
614                 }
615                 dev_err(dev, "Resume error, status: 0x%02x\n", status);
616                 return -ENODEV;
617         }
618
619         enable_irq(client->irq);
620
621         return 0;
622 }
623
624 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
625
626 static const struct i2c_device_id silead_ts_id[] = {
627         { "gsl1680", 0 },
628         { "gsl1688", 0 },
629         { "gsl3670", 0 },
630         { "gsl3675", 0 },
631         { "gsl3692", 0 },
632         { "mssl1680", 0 },
633         { }
634 };
635 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
636
637 #ifdef CONFIG_ACPI
638 static const struct acpi_device_id silead_ts_acpi_match[] = {
639         { "GSL1680", 0 },
640         { "GSL1688", 0 },
641         { "GSL3670", 0 },
642         { "GSL3675", 0 },
643         { "GSL3692", 0 },
644         { "MSSL1680", 0 },
645         { "MSSL0001", 0 },
646         { "MSSL0002", 0 },
647         { "MSSL0017", 0 },
648         { }
649 };
650 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
651 #endif
652
653 #ifdef CONFIG_OF
654 static const struct of_device_id silead_ts_of_match[] = {
655         { .compatible = "silead,gsl1680" },
656         { .compatible = "silead,gsl1688" },
657         { .compatible = "silead,gsl3670" },
658         { .compatible = "silead,gsl3675" },
659         { .compatible = "silead,gsl3692" },
660         { },
661 };
662 MODULE_DEVICE_TABLE(of, silead_ts_of_match);
663 #endif
664
665 static struct i2c_driver silead_ts_driver = {
666         .probe = silead_ts_probe,
667         .id_table = silead_ts_id,
668         .driver = {
669                 .name = SILEAD_TS_NAME,
670                 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
671                 .of_match_table = of_match_ptr(silead_ts_of_match),
672                 .pm = &silead_ts_pm,
673         },
674 };
675 module_i2c_driver(silead_ts_driver);
676
677 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
678 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
679 MODULE_LICENSE("GPL");