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