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