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