OSDN Git Service

Add generic driver for Silead tochscreens
[android-x86/kernel.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 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/acpi.h>
22 #include <linux/interrupt.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/delay.h>
25 #include <linux/firmware.h>
26 #include <linux/input.h>
27 #include <linux/input/mt.h>
28 #include <linux/pm.h>
29 #include <linux/irq.h>
30
31 #define SILEAD_TS_NAME "silead_ts"
32
33 #define SILEAD_REG_RESET        0xE0
34 #define SILEAD_REG_DATA         0x80
35 #define SILEAD_REG_TOUCH_NR     0x80
36 #define SILEAD_REG_POWER        0xBC
37 #define SILEAD_REG_CLOCK        0xE4
38 #define SILEAD_REG_STATUS       0xB0
39 #define SILEAD_REG_ID           0xFC
40 #define SILEAD_REG_MEM_CHECK    0xB0
41
42 #define SILEAD_STATUS_OK        0x5A5A5A5A
43 #define SILEAD_TS_DATA_LEN      44
44 #define SILEAD_CLOCK            0x04
45
46 #define SILEAD_CMD_RESET        0x88
47 #define SILEAD_CMD_START        0x00
48
49 #define SILEAD_POINT_DATA_LEN   0x04
50 #define SILEAD_POINT_X_OFF      0x02
51 #define SILEAD_POINT_ID_OFF     0x03
52 #define SILEAD_X_HSB_MASK       0xF0
53 #define SILEAD_TOUCH_ID_MASK    0x0F
54
55 #define SILEAD_DP_X_MAX         "resolution-x"
56 #define SILEAD_DP_Y_MAX         "resolution-y"
57 #define SILEAD_DP_MAX_FINGERS   "max-fingers"
58 #define SILEAD_DP_FW_NAME       "fw-name"
59 #define SILEAD_PWR_GPIO_NAME    "power"
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 enum silead_ts_power {
67         SILEAD_POWER_ON  = 1,
68         SILEAD_POWER_OFF = 0
69 };
70
71 struct silead_ts_data {
72         struct i2c_client *client;
73         struct gpio_desc *gpio_power;
74         struct input_dev *input;
75         const char *custom_fw_name;
76         char fw_name[I2C_NAME_SIZE];
77         u16 x_max;
78         u16 y_max;
79         u8 max_fingers;
80         u32 chip_id;
81 };
82
83 struct silead_fw_data {
84         u32 offset;
85         u32 val;
86 };
87
88 static int silead_ts_request_input_dev(struct silead_ts_data *data)
89 {
90         struct device *dev = &data->client->dev;
91         int ret;
92
93         data->input = devm_input_allocate_device(dev);
94         if (!data->input) {
95                 dev_err(dev,
96                         "Failed to allocate input device\n");
97                 return -ENOMEM;
98         }
99
100         input_set_capability(data->input, EV_ABS, ABS_X);
101         input_set_capability(data->input, EV_ABS, ABS_Y);
102
103         input_set_abs_params(data->input, ABS_MT_POSITION_X, 0,
104                              data->x_max, 0, 0);
105         input_set_abs_params(data->input, ABS_MT_POSITION_Y, 0,
106                              data->y_max, 0, 0);
107
108         input_mt_init_slots(data->input, data->max_fingers,
109                             INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
110
111         data->input->name = SILEAD_TS_NAME;
112         data->input->phys = "input/ts";
113         data->input->id.bustype = BUS_I2C;
114
115         ret = input_register_device(data->input);
116         if (ret) {
117                 dev_err(dev, "Failed to register input device: %d\n", ret);
118                 return ret;
119         }
120
121         return 0;
122 }
123
124 static void silead_ts_report_touch(struct silead_ts_data *data, u16 x, u16 y,
125                                    u8 id)
126 {
127         input_mt_slot(data->input, id);
128         input_mt_report_slot_state(data->input, MT_TOOL_FINGER, true);
129         input_report_abs(data->input, ABS_MT_POSITION_X, x);
130         input_report_abs(data->input, ABS_MT_POSITION_Y, y);
131 }
132
133 static void silead_ts_set_power(struct i2c_client *client,
134                                 enum silead_ts_power state)
135 {
136         struct silead_ts_data *data = i2c_get_clientdata(client);
137
138         gpiod_set_value_cansleep(data->gpio_power, state);
139         msleep(SILEAD_POWER_SLEEP);
140 }
141
142 static void silead_ts_read_data(struct i2c_client *client)
143 {
144         struct silead_ts_data *data = i2c_get_clientdata(client);
145         struct device *dev = &client->dev;
146         u8 buf[SILEAD_TS_DATA_LEN];
147         int x, y, id, touch_nr, ret, i, offset;
148
149         ret = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_DATA,
150                                             SILEAD_TS_DATA_LEN, buf);
151         if (ret < 0) {
152                 dev_err(dev, "Data read error %d\n", ret);
153                 return;
154         }
155
156         touch_nr = buf[0];
157
158         if (touch_nr < 0)
159                 return;
160
161         dev_dbg(dev, "Touch number: %d\n", touch_nr);
162
163         for (i = 1; i <= touch_nr; i++) {
164                 offset = i * SILEAD_POINT_DATA_LEN;
165
166                 /* The last 4 bits are the touch id */
167                 id = buf[offset + SILEAD_POINT_ID_OFF] & SILEAD_TOUCH_ID_MASK;
168
169                 /* The 1st 4 bits are part of X */
170                 buf[offset + SILEAD_POINT_ID_OFF] =
171                         (buf[offset + SILEAD_POINT_ID_OFF] & SILEAD_X_HSB_MASK)
172                         >> 4;
173
174                 y = le16_to_cpup((__le16 *)(buf + offset));
175                 x = le16_to_cpup((__le16 *)(buf + offset + SILEAD_POINT_X_OFF));
176
177                 dev_dbg(dev, "x=%d y=%d id=%d\n", x, y, id);
178                 silead_ts_report_touch(data, x, y, id);
179         }
180
181         input_mt_sync_frame(data->input);
182         input_sync(data->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 ret;
189
190         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
191                                         SILEAD_CMD_RESET);
192         if (ret)
193                 goto i2c_write_err;
194         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
195
196         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_TOUCH_NR,
197                                         data->max_fingers);
198         if (ret)
199                 goto i2c_write_err;
200         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
201
202         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK, SILEAD_CLOCK);
203         if (ret)
204                 goto i2c_write_err;
205         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
206
207         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
208                                         SILEAD_CMD_START);
209         if (ret)
210                 goto i2c_write_err;
211         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
212
213         return 0;
214
215 i2c_write_err:
216         dev_err(&client->dev, "Registers clear error %d\n", ret);
217         return ret;
218 }
219
220 static int silead_ts_reset(struct i2c_client *client)
221 {
222         int ret;
223
224         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET,
225                                         SILEAD_CMD_RESET);
226         if (ret)
227                 goto i2c_write_err;
228         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
229
230         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_CLOCK, SILEAD_CLOCK);
231         if (ret)
232                 goto i2c_write_err;
233         usleep_range(SILEAD_CMD_SLEEP_MIN, SILEAD_CMD_SLEEP_MAX);
234
235         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_POWER,
236                                         SILEAD_CMD_START);
237         if (ret)
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", ret);
245         return ret;
246 }
247
248 static int silead_ts_startup(struct i2c_client *client)
249 {
250         int ret;
251
252         ret = i2c_smbus_write_byte_data(client, SILEAD_REG_RESET, 0x00);
253         if (ret) {
254                 dev_err(&client->dev, "Startup error %d\n", ret);
255                 return ret;
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 ret;
270
271         dev_dbg(dev, "Firmware file name: %s", data->fw_name);
272
273         if (data->custom_fw_name)
274                 ret = request_firmware(&fw, data->custom_fw_name, dev);
275         else
276                 ret = request_firmware(&fw, data->fw_name, dev);
277
278         if (ret) {
279                 dev_err(dev, "Firmware request error %d\n", ret);
280                 return ret;
281         }
282
283         fw_size = fw->size / sizeof(*fw_data);
284         fw_data = (struct silead_fw_data *)fw->data;
285
286         for (i = 0; i < fw_size; i++) {
287                 ret = i2c_smbus_write_i2c_block_data(client, fw_data[i].offset,
288                                                      4, (u8 *)&fw_data[i].val);
289                 if (ret) {
290                         dev_err(dev, "Firmware load error %d\n", ret);
291                         goto release_fw_err;
292                 }
293         }
294
295         release_firmware(fw);
296         return 0;
297
298 release_fw_err:
299         release_firmware(fw);
300         return ret;
301 }
302
303 static u32 silead_ts_get_status(struct i2c_client *client)
304 {
305         int ret;
306         u32 status;
307
308         ret = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_STATUS, 4,
309                                             (u8 *)&status);
310         if (ret < 0) {
311                 dev_err(&client->dev, "Status read error %d\n", ret);
312                 return ret;
313         }
314
315         return status;
316 }
317
318 static int silead_ts_get_id(struct i2c_client *client)
319 {
320         struct silead_ts_data *data = i2c_get_clientdata(client);
321         int ret;
322
323         ret = i2c_smbus_read_i2c_block_data(client, SILEAD_REG_ID, 4,
324                                             (u8 *)&data->chip_id);
325         if (ret < 0) {
326                 dev_err(&client->dev, "Chip ID read error %d\n", ret);
327                 return ret;
328         }
329
330         return 0;
331 }
332
333 static int silead_ts_setup(struct i2c_client *client)
334 {
335         struct silead_ts_data *data = i2c_get_clientdata(client);
336         struct device *dev = &client->dev;
337         int ret;
338         u32 status;
339
340         silead_ts_set_power(client, SILEAD_POWER_OFF);
341         silead_ts_set_power(client, SILEAD_POWER_ON);
342
343         ret = silead_ts_get_id(client);
344         if (ret)
345                 return ret;
346         dev_dbg(dev, "Chip ID: 0x%8X", data->chip_id);
347
348         ret = silead_ts_init(client);
349         if (ret)
350                 return ret;
351
352         ret = silead_ts_reset(client);
353         if (ret)
354                 return ret;
355
356         ret = silead_ts_load_fw(client);
357         if (ret)
358                 return ret;
359
360         ret = silead_ts_startup(client);
361         if (ret)
362                 return ret;
363
364         status = silead_ts_get_status(client);
365         if (status != SILEAD_STATUS_OK) {
366                 dev_err(dev, "Initialization error, status: 0x%X\n", status);
367                 return -ENODEV;
368         }
369
370         return 0;
371 }
372
373 static irqreturn_t silead_ts_threaded_irq_handler(int irq, void *id)
374 {
375         struct silead_ts_data *data = (struct silead_ts_data *)id;
376         struct i2c_client *client = data->client;
377
378         silead_ts_read_data(client);
379
380         return IRQ_HANDLED;
381 }
382
383 static int silead_ts_read_props(struct i2c_client *client)
384 {
385         struct silead_ts_data *data = i2c_get_clientdata(client);
386         struct device *dev = &client->dev;
387         int ret;
388
389         ret = device_property_read_u16(dev, SILEAD_DP_X_MAX, &data->x_max);
390         if (ret) {
391                 dev_err(dev, "Resolution X read error %d\n", ret);
392                 goto error;
393         }
394
395         ret = device_property_read_u16(dev, SILEAD_DP_Y_MAX, &data->y_max);
396         if (ret) {
397                 dev_err(dev, "Resolution Y read error %d\n", ret);
398                 goto error;
399         }
400
401         ret = device_property_read_u8(dev, SILEAD_DP_MAX_FINGERS,
402                                       &data->max_fingers);
403         if (ret) {
404                 dev_err(dev, "Max fingers read error %d\n", ret);
405                 goto error;
406         }
407
408         ret = device_property_read_string(dev, SILEAD_DP_FW_NAME,
409                                           &data->custom_fw_name);
410         if (ret)
411                 dev_info(dev, "Firmware file name read error. Using default.");
412
413         dev_dbg(dev, "X max = %d, Y max = %d, max fingers = %d",
414                 data->x_max, data->y_max, data->max_fingers);
415
416         return 0;
417
418 error:
419         return ret;
420 }
421
422 #ifdef CONFIG_ACPI
423 static const struct acpi_device_id silead_ts_acpi_match[];
424
425 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
426                                          const struct i2c_device_id *id)
427 {
428         const struct acpi_device_id *acpi_id;
429         struct device *dev = &data->client->dev;
430         int i;
431
432         if (ACPI_HANDLE(dev)) {
433                 acpi_id = acpi_match_device(silead_ts_acpi_match, dev);
434                 if (!acpi_id)
435                         return -ENODEV;
436
437                 sprintf(data->fw_name, "%s.fw", acpi_id->id);
438
439                 for (i = 0; i < strlen(data->fw_name); i++)
440                         data->fw_name[i] = tolower(data->fw_name[i]);
441         } else {
442                 sprintf(data->fw_name, "%s.fw", id->name);
443         }
444
445         return 0;
446 }
447 #else
448 static int silead_ts_set_default_fw_name(struct silead_ts_data *data,
449                                          const struct i2c_device_id *id)
450 {
451         sprintf(data->fw_name, "%s.fw", id->name);
452         return 0;
453 }
454 #endif
455
456 static int silead_ts_probe(struct i2c_client *client,
457                            const struct i2c_device_id *id)
458 {
459         struct silead_ts_data *data;
460         struct device *dev = &client->dev;
461         int ret;
462
463         if (!i2c_check_functionality(client->adapter,
464                                      I2C_FUNC_I2C |
465                                      I2C_FUNC_SMBUS_READ_I2C_BLOCK |
466                                      I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
467                 dev_err(dev, "I2C functionality check failed\n");
468                 return -ENXIO;
469         }
470
471         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
472         if (!data)
473                 return -ENOMEM;
474
475         i2c_set_clientdata(client, data);
476         data->client = client;
477
478         ret = silead_ts_set_default_fw_name(data, id);
479         if (ret)
480                 return ret;
481
482         /* If the IRQ is not filled by DT or ACPI subsytem
483          * we can't continue without it */
484         if (client->irq <= 0)
485                 return -ENODEV;
486
487         /* Power GPIO pin */
488         data->gpio_power = devm_gpiod_get(dev, SILEAD_PWR_GPIO_NAME,
489                                           GPIOD_OUT_LOW);
490         if (IS_ERR(data->gpio_power)) {
491                 dev_err(dev, "Shutdown GPIO request failed\n");
492                 return PTR_ERR(data->gpio_power);
493         }
494
495         ret = silead_ts_read_props(client);
496         if (ret)
497                 return ret;
498
499         ret = silead_ts_setup(client);
500         if (ret)
501                 return ret;
502
503         ret = silead_ts_request_input_dev(data);
504         if (ret)
505                 return ret;
506
507         ret = devm_request_threaded_irq(dev, client->irq, NULL,
508                                         silead_ts_threaded_irq_handler,
509                                         IRQF_ONESHOT | IRQ_TYPE_EDGE_RISING,
510                                         client->name, data);
511         if (ret) {
512                 dev_err(dev, "IRQ request failed %d\n", ret);
513                 return ret;
514         }
515
516         dev_dbg(dev, "Probing succeded\n");
517         return 0;
518 }
519
520 #ifdef CONFIG_PM_SLEEP
521 static int silead_ts_suspend(struct device *dev)
522 {
523         struct i2c_client *client = to_i2c_client(dev);
524
525         silead_ts_set_power(client, SILEAD_POWER_OFF);
526         return 0;
527 }
528
529 static int silead_ts_resume(struct device *dev)
530 {
531         struct i2c_client *client = to_i2c_client(dev);
532         int ret, status;
533
534         silead_ts_set_power(client, SILEAD_POWER_ON);
535
536         ret = silead_ts_reset(client);
537         if (ret)
538                 return ret;
539
540         ret = silead_ts_startup(client);
541         if (ret)
542                 return ret;
543
544         status = silead_ts_get_status(client);
545         if (status != SILEAD_STATUS_OK) {
546                 dev_err(dev, "Resume error, status: 0x%X\n", status);
547                 return -ENODEV;
548         }
549
550         return 0;
551 }
552
553 static SIMPLE_DEV_PM_OPS(silead_ts_pm, silead_ts_suspend, silead_ts_resume);
554 #endif
555
556 static const struct i2c_device_id silead_ts_id[] = {
557         { "gsl1680", 0 },
558         { "gsl1688", 0 },
559         { "gsl3670", 0 },
560         { "gsl3675", 0 },
561         { "gsl3692", 0 },
562         { }
563 };
564 MODULE_DEVICE_TABLE(i2c, silead_ts_id);
565
566 #ifdef CONFIG_ACPI
567 static const struct acpi_device_id silead_ts_acpi_match[] = {
568         { "GSL1680", 0 },
569         { "GSL1688", 0 },
570         { "GSL3670", 0 },
571         { "GSL3675", 0 },
572         { "GSL3692", 0 },
573         { }
574 };
575 MODULE_DEVICE_TABLE(acpi, silead_ts_acpi_match);
576 #endif
577
578 static struct i2c_driver silead_ts_driver = {
579         .probe = silead_ts_probe,
580         .id_table = silead_ts_id,
581         .driver = {
582                 .name = SILEAD_TS_NAME,
583                 .owner = THIS_MODULE,
584 #ifdef CONFIG_ACPI
585                 .acpi_match_table = ACPI_PTR(silead_ts_acpi_match),
586 #endif
587 #ifdef CONFIG_PM_SLEEP
588                 .pm = &silead_ts_pm,
589 #endif
590         },
591 };
592 module_i2c_driver(silead_ts_driver);
593
594 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
595 MODULE_DESCRIPTION("Silead I2C touchscreen driver");
596 MODULE_LICENSE("GPL");