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