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