GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / input / touchscreen / stmfts.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  * Author: Andi Shyti <andi.shyti@samsung.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * STMicroelectronics FTS Touchscreen device driver
10  */
11
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/input/mt.h>
15 #include <linux/input/touchscreen.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/leds.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22
23 /* I2C commands */
24 #define STMFTS_READ_INFO                        0x80
25 #define STMFTS_READ_STATUS                      0x84
26 #define STMFTS_READ_ONE_EVENT                   0x85
27 #define STMFTS_READ_ALL_EVENT                   0x86
28 #define STMFTS_LATEST_EVENT                     0x87
29 #define STMFTS_SLEEP_IN                         0x90
30 #define STMFTS_SLEEP_OUT                        0x91
31 #define STMFTS_MS_MT_SENSE_OFF                  0x92
32 #define STMFTS_MS_MT_SENSE_ON                   0x93
33 #define STMFTS_SS_HOVER_SENSE_OFF               0x94
34 #define STMFTS_SS_HOVER_SENSE_ON                0x95
35 #define STMFTS_MS_KEY_SENSE_OFF                 0x9a
36 #define STMFTS_MS_KEY_SENSE_ON                  0x9b
37 #define STMFTS_SYSTEM_RESET                     0xa0
38 #define STMFTS_CLEAR_EVENT_STACK                0xa1
39 #define STMFTS_FULL_FORCE_CALIBRATION           0xa2
40 #define STMFTS_MS_CX_TUNING                     0xa3
41 #define STMFTS_SS_CX_TUNING                     0xa4
42
43 /* events */
44 #define STMFTS_EV_NO_EVENT                      0x00
45 #define STMFTS_EV_MULTI_TOUCH_DETECTED          0x02
46 #define STMFTS_EV_MULTI_TOUCH_ENTER             0x03
47 #define STMFTS_EV_MULTI_TOUCH_LEAVE             0x04
48 #define STMFTS_EV_MULTI_TOUCH_MOTION            0x05
49 #define STMFTS_EV_HOVER_ENTER                   0x07
50 #define STMFTS_EV_HOVER_LEAVE                   0x08
51 #define STMFTS_EV_HOVER_MOTION                  0x09
52 #define STMFTS_EV_KEY_STATUS                    0x0e
53 #define STMFTS_EV_ERROR                         0x0f
54 #define STMFTS_EV_CONTROLLER_READY              0x10
55 #define STMFTS_EV_SLEEP_OUT_CONTROLLER_READY    0x11
56 #define STMFTS_EV_STATUS                        0x16
57 #define STMFTS_EV_DEBUG                         0xdb
58
59 /* multi touch related event masks */
60 #define STMFTS_MASK_EVENT_ID                    0x0f
61 #define STMFTS_MASK_TOUCH_ID                    0xf0
62 #define STMFTS_MASK_LEFT_EVENT                  0x0f
63 #define STMFTS_MASK_X_MSB                       0x0f
64 #define STMFTS_MASK_Y_LSB                       0xf0
65
66 /* key related event masks */
67 #define STMFTS_MASK_KEY_NO_TOUCH                0x00
68 #define STMFTS_MASK_KEY_MENU                    0x01
69 #define STMFTS_MASK_KEY_BACK                    0x02
70
71 #define STMFTS_EVENT_SIZE       8
72 #define STMFTS_STACK_DEPTH      32
73 #define STMFTS_DATA_MAX_SIZE    (STMFTS_EVENT_SIZE * STMFTS_STACK_DEPTH)
74 #define STMFTS_MAX_FINGERS      10
75 #define STMFTS_DEV_NAME         "stmfts"
76
77 enum stmfts_regulators {
78         STMFTS_REGULATOR_VDD,
79         STMFTS_REGULATOR_AVDD,
80 };
81
82 struct stmfts_data {
83         struct i2c_client *client;
84         struct input_dev *input;
85         struct led_classdev led_cdev;
86         struct mutex mutex;
87
88         struct touchscreen_properties prop;
89
90         struct regulator_bulk_data regulators[2];
91
92         /*
93          * Presence of ledvdd will be used also to check
94          * whether the LED is supported.
95          */
96         struct regulator *ledvdd;
97
98         u16 chip_id;
99         u8 chip_ver;
100         u16 fw_ver;
101         u8 config_id;
102         u8 config_ver;
103
104         u8 data[STMFTS_DATA_MAX_SIZE];
105
106         struct completion cmd_done;
107
108         bool use_key;
109         bool led_status;
110         bool hover_enabled;
111         bool running;
112 };
113
114 static int stmfts_brightness_set(struct led_classdev *led_cdev,
115                                         enum led_brightness value)
116 {
117         struct stmfts_data *sdata = container_of(led_cdev,
118                                         struct stmfts_data, led_cdev);
119         int err;
120
121         if (value != sdata->led_status && sdata->ledvdd) {
122                 if (!value) {
123                         regulator_disable(sdata->ledvdd);
124                 } else {
125                         err = regulator_enable(sdata->ledvdd);
126                         if (err) {
127                                 dev_warn(&sdata->client->dev,
128                                          "failed to disable ledvdd regulator: %d\n",
129                                          err);
130                                 return err;
131                         }
132                 }
133                 sdata->led_status = value;
134         }
135
136         return 0;
137 }
138
139 static enum led_brightness stmfts_brightness_get(struct led_classdev *led_cdev)
140 {
141         struct stmfts_data *sdata = container_of(led_cdev,
142                                                 struct stmfts_data, led_cdev);
143
144         return !!regulator_is_enabled(sdata->ledvdd);
145 }
146
147 /*
148  * We can't simply use i2c_smbus_read_i2c_block_data because we
149  * need to read more than 255 bytes (
150  */
151 static int stmfts_read_events(struct stmfts_data *sdata)
152 {
153         u8 cmd = STMFTS_READ_ALL_EVENT;
154         struct i2c_msg msgs[2] = {
155                 {
156                         .addr   = sdata->client->addr,
157                         .len    = 1,
158                         .buf    = &cmd,
159                 },
160                 {
161                         .addr   = sdata->client->addr,
162                         .flags  = I2C_M_RD,
163                         .len    = STMFTS_DATA_MAX_SIZE,
164                         .buf    = sdata->data,
165                 },
166         };
167         int ret;
168
169         ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs));
170         if (ret < 0)
171                 return ret;
172
173         return ret == ARRAY_SIZE(msgs) ? 0 : -EIO;
174 }
175
176 static void stmfts_report_contact_event(struct stmfts_data *sdata,
177                                         const u8 event[])
178 {
179         u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4;
180         u16 x = event[1] | ((event[2] & STMFTS_MASK_X_MSB) << 8);
181         u16 y = (event[2] >> 4) | (event[3] << 4);
182         u8 maj = event[4];
183         u8 min = event[5];
184         u8 orientation = event[6];
185         u8 area = event[7];
186
187         input_mt_slot(sdata->input, slot_id);
188
189         input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true);
190         input_report_abs(sdata->input, ABS_MT_POSITION_X, x);
191         input_report_abs(sdata->input, ABS_MT_POSITION_Y, y);
192         input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, maj);
193         input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, min);
194         input_report_abs(sdata->input, ABS_MT_PRESSURE, area);
195         input_report_abs(sdata->input, ABS_MT_ORIENTATION, orientation);
196
197         input_sync(sdata->input);
198 }
199
200 static void stmfts_report_contact_release(struct stmfts_data *sdata,
201                                           const u8 event[])
202 {
203         u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4;
204
205         input_mt_slot(sdata->input, slot_id);
206         input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, false);
207
208         input_sync(sdata->input);
209 }
210
211 static void stmfts_report_hover_event(struct stmfts_data *sdata,
212                                       const u8 event[])
213 {
214         u16 x = (event[2] << 4) | (event[4] >> 4);
215         u16 y = (event[3] << 4) | (event[4] & STMFTS_MASK_Y_LSB);
216         u8 z = event[5];
217
218         input_report_abs(sdata->input, ABS_X, x);
219         input_report_abs(sdata->input, ABS_Y, y);
220         input_report_abs(sdata->input, ABS_DISTANCE, z);
221
222         input_sync(sdata->input);
223 }
224
225 static void stmfts_report_key_event(struct stmfts_data *sdata, const u8 event[])
226 {
227         switch (event[2]) {
228         case 0:
229                 input_report_key(sdata->input, KEY_BACK, 0);
230                 input_report_key(sdata->input, KEY_MENU, 0);
231                 break;
232
233         case STMFTS_MASK_KEY_BACK:
234                 input_report_key(sdata->input, KEY_BACK, 1);
235                 break;
236
237         case STMFTS_MASK_KEY_MENU:
238                 input_report_key(sdata->input, KEY_MENU, 1);
239                 break;
240
241         default:
242                 dev_warn(&sdata->client->dev,
243                          "unknown key event: %#02x\n", event[2]);
244                 break;
245         }
246
247         input_sync(sdata->input);
248 }
249
250 static void stmfts_parse_events(struct stmfts_data *sdata)
251 {
252         int i;
253
254         for (i = 0; i < STMFTS_STACK_DEPTH; i++) {
255                 u8 *event = &sdata->data[i * STMFTS_EVENT_SIZE];
256
257                 switch (event[0]) {
258
259                 case STMFTS_EV_CONTROLLER_READY:
260                 case STMFTS_EV_SLEEP_OUT_CONTROLLER_READY:
261                 case STMFTS_EV_STATUS:
262                         complete(&sdata->cmd_done);
263                         /* fall through */
264
265                 case STMFTS_EV_NO_EVENT:
266                 case STMFTS_EV_DEBUG:
267                         return;
268                 }
269
270                 switch (event[0] & STMFTS_MASK_EVENT_ID) {
271
272                 case STMFTS_EV_MULTI_TOUCH_ENTER:
273                 case STMFTS_EV_MULTI_TOUCH_MOTION:
274                         stmfts_report_contact_event(sdata, event);
275                         break;
276
277                 case STMFTS_EV_MULTI_TOUCH_LEAVE:
278                         stmfts_report_contact_release(sdata, event);
279                         break;
280
281                 case STMFTS_EV_HOVER_ENTER:
282                 case STMFTS_EV_HOVER_LEAVE:
283                 case STMFTS_EV_HOVER_MOTION:
284                         stmfts_report_hover_event(sdata, event);
285                         break;
286
287                 case STMFTS_EV_KEY_STATUS:
288                         stmfts_report_key_event(sdata, event);
289                         break;
290
291                 case STMFTS_EV_ERROR:
292                         dev_warn(&sdata->client->dev,
293                                         "error code: 0x%x%x%x%x%x%x",
294                                         event[6], event[5], event[4],
295                                         event[3], event[2], event[1]);
296                         break;
297
298                 default:
299                         dev_err(&sdata->client->dev,
300                                 "unknown event %#02x\n", event[0]);
301                 }
302         }
303 }
304
305 static irqreturn_t stmfts_irq_handler(int irq, void *dev)
306 {
307         struct stmfts_data *sdata = dev;
308         int err;
309
310         mutex_lock(&sdata->mutex);
311
312         err = stmfts_read_events(sdata);
313         if (unlikely(err))
314                 dev_err(&sdata->client->dev,
315                         "failed to read events: %d\n", err);
316         else
317                 stmfts_parse_events(sdata);
318
319         mutex_unlock(&sdata->mutex);
320         return IRQ_HANDLED;
321 }
322
323 static int stmfts_command(struct stmfts_data *sdata, const u8 cmd)
324 {
325         int err;
326
327         reinit_completion(&sdata->cmd_done);
328
329         err = i2c_smbus_write_byte(sdata->client, cmd);
330         if (err)
331                 return err;
332
333         if (!wait_for_completion_timeout(&sdata->cmd_done,
334                                          msecs_to_jiffies(1000)))
335                 return -ETIMEDOUT;
336
337         return 0;
338 }
339
340 static int stmfts_input_open(struct input_dev *dev)
341 {
342         struct stmfts_data *sdata = input_get_drvdata(dev);
343         int err;
344
345         err = pm_runtime_get_sync(&sdata->client->dev);
346         if (err < 0)
347                 return err;
348
349         err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
350         if (err)
351                 return err;
352
353         mutex_lock(&sdata->mutex);
354         sdata->running = true;
355
356         if (sdata->hover_enabled) {
357                 err = i2c_smbus_write_byte(sdata->client,
358                                            STMFTS_SS_HOVER_SENSE_ON);
359                 if (err)
360                         dev_warn(&sdata->client->dev,
361                                  "failed to enable hover\n");
362         }
363         mutex_unlock(&sdata->mutex);
364
365         if (sdata->use_key) {
366                 err = i2c_smbus_write_byte(sdata->client,
367                                            STMFTS_MS_KEY_SENSE_ON);
368                 if (err)
369                         /* I can still use only the touch screen */
370                         dev_warn(&sdata->client->dev,
371                                  "failed to enable touchkey\n");
372         }
373
374         return 0;
375 }
376
377 static void stmfts_input_close(struct input_dev *dev)
378 {
379         struct stmfts_data *sdata = input_get_drvdata(dev);
380         int err;
381
382         err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_OFF);
383         if (err)
384                 dev_warn(&sdata->client->dev,
385                          "failed to disable touchscreen: %d\n", err);
386
387         mutex_lock(&sdata->mutex);
388
389         sdata->running = false;
390
391         if (sdata->hover_enabled) {
392                 err = i2c_smbus_write_byte(sdata->client,
393                                            STMFTS_SS_HOVER_SENSE_OFF);
394                 if (err)
395                         dev_warn(&sdata->client->dev,
396                                  "failed to disable hover: %d\n", err);
397         }
398         mutex_unlock(&sdata->mutex);
399
400         if (sdata->use_key) {
401                 err = i2c_smbus_write_byte(sdata->client,
402                                            STMFTS_MS_KEY_SENSE_OFF);
403                 if (err)
404                         dev_warn(&sdata->client->dev,
405                                  "failed to disable touchkey: %d\n", err);
406         }
407
408         pm_runtime_put_sync(&sdata->client->dev);
409 }
410
411 static ssize_t stmfts_sysfs_chip_id(struct device *dev,
412                                 struct device_attribute *attr, char *buf)
413 {
414         struct stmfts_data *sdata = dev_get_drvdata(dev);
415
416         return sprintf(buf, "%#x\n", sdata->chip_id);
417 }
418
419 static ssize_t stmfts_sysfs_chip_version(struct device *dev,
420                                 struct device_attribute *attr, char *buf)
421 {
422         struct stmfts_data *sdata = dev_get_drvdata(dev);
423
424         return sprintf(buf, "%u\n", sdata->chip_ver);
425 }
426
427 static ssize_t stmfts_sysfs_fw_ver(struct device *dev,
428                                 struct device_attribute *attr, char *buf)
429 {
430         struct stmfts_data *sdata = dev_get_drvdata(dev);
431
432         return sprintf(buf, "%u\n", sdata->fw_ver);
433 }
434
435 static ssize_t stmfts_sysfs_config_id(struct device *dev,
436                                 struct device_attribute *attr, char *buf)
437 {
438         struct stmfts_data *sdata = dev_get_drvdata(dev);
439
440         return sprintf(buf, "%#x\n", sdata->config_id);
441 }
442
443 static ssize_t stmfts_sysfs_config_version(struct device *dev,
444                                 struct device_attribute *attr, char *buf)
445 {
446         struct stmfts_data *sdata = dev_get_drvdata(dev);
447
448         return sprintf(buf, "%u\n", sdata->config_ver);
449 }
450
451 static ssize_t stmfts_sysfs_read_status(struct device *dev,
452                                 struct device_attribute *attr, char *buf)
453 {
454         struct stmfts_data *sdata = dev_get_drvdata(dev);
455         u8 status[4];
456         int err;
457
458         err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_STATUS,
459                                             sizeof(status), status);
460         if (err)
461                 return err;
462
463         return sprintf(buf, "%#02x\n", status[0]);
464 }
465
466 static ssize_t stmfts_sysfs_hover_enable_read(struct device *dev,
467                                 struct device_attribute *attr, char *buf)
468 {
469         struct stmfts_data *sdata = dev_get_drvdata(dev);
470
471         return sprintf(buf, "%u\n", sdata->hover_enabled);
472 }
473
474 static ssize_t stmfts_sysfs_hover_enable_write(struct device *dev,
475                                 struct device_attribute *attr,
476                                 const char *buf, size_t len)
477 {
478         struct stmfts_data *sdata = dev_get_drvdata(dev);
479         unsigned long value;
480         int err = 0;
481
482         if (kstrtoul(buf, 0, &value))
483                 return -EINVAL;
484
485         mutex_lock(&sdata->mutex);
486
487         if (value && sdata->hover_enabled)
488                 goto out;
489
490         if (sdata->running)
491                 err = i2c_smbus_write_byte(sdata->client,
492                                            value ? STMFTS_SS_HOVER_SENSE_ON :
493                                                    STMFTS_SS_HOVER_SENSE_OFF);
494
495         if (!err)
496                 sdata->hover_enabled = !!value;
497
498 out:
499         mutex_unlock(&sdata->mutex);
500
501         return len;
502 }
503
504 static DEVICE_ATTR(chip_id, 0444, stmfts_sysfs_chip_id, NULL);
505 static DEVICE_ATTR(chip_version, 0444, stmfts_sysfs_chip_version, NULL);
506 static DEVICE_ATTR(fw_ver, 0444, stmfts_sysfs_fw_ver, NULL);
507 static DEVICE_ATTR(config_id, 0444, stmfts_sysfs_config_id, NULL);
508 static DEVICE_ATTR(config_version, 0444, stmfts_sysfs_config_version, NULL);
509 static DEVICE_ATTR(status, 0444, stmfts_sysfs_read_status, NULL);
510 static DEVICE_ATTR(hover_enable, 0644, stmfts_sysfs_hover_enable_read,
511                                         stmfts_sysfs_hover_enable_write);
512
513 static struct attribute *stmfts_sysfs_attrs[] = {
514         &dev_attr_chip_id.attr,
515         &dev_attr_chip_version.attr,
516         &dev_attr_fw_ver.attr,
517         &dev_attr_config_id.attr,
518         &dev_attr_config_version.attr,
519         &dev_attr_status.attr,
520         &dev_attr_hover_enable.attr,
521         NULL
522 };
523
524 static struct attribute_group stmfts_attribute_group = {
525         .attrs = stmfts_sysfs_attrs
526 };
527
528 static int stmfts_power_on(struct stmfts_data *sdata)
529 {
530         int err;
531         u8 reg[8];
532
533         err = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
534                                     sdata->regulators);
535         if (err)
536                 return err;
537
538         /*
539          * The datasheet does not specify the power on time, but considering
540          * that the reset time is < 10ms, I sleep 20ms to be sure
541          */
542         msleep(20);
543
544         err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_INFO,
545                                             sizeof(reg), reg);
546         if (err < 0)
547                 return err;
548         if (err != sizeof(reg))
549                 return -EIO;
550
551         sdata->chip_id = be16_to_cpup((__be16 *)&reg[6]);
552         sdata->chip_ver = reg[0];
553         sdata->fw_ver = be16_to_cpup((__be16 *)&reg[2]);
554         sdata->config_id = reg[4];
555         sdata->config_ver = reg[5];
556
557         enable_irq(sdata->client->irq);
558
559         msleep(50);
560
561         err = stmfts_command(sdata, STMFTS_SYSTEM_RESET);
562         if (err)
563                 return err;
564
565         err = stmfts_command(sdata, STMFTS_SLEEP_OUT);
566         if (err)
567                 return err;
568
569         /* optional tuning */
570         err = stmfts_command(sdata, STMFTS_MS_CX_TUNING);
571         if (err)
572                 dev_warn(&sdata->client->dev,
573                          "failed to perform mutual auto tune: %d\n", err);
574
575         /* optional tuning */
576         err = stmfts_command(sdata, STMFTS_SS_CX_TUNING);
577         if (err)
578                 dev_warn(&sdata->client->dev,
579                          "failed to perform self auto tune: %d\n", err);
580
581         err = stmfts_command(sdata, STMFTS_FULL_FORCE_CALIBRATION);
582         if (err)
583                 return err;
584
585         /*
586          * At this point no one is using the touchscreen
587          * and I don't really care about the return value
588          */
589         (void) i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
590
591         return 0;
592 }
593
594 static void stmfts_power_off(void *data)
595 {
596         struct stmfts_data *sdata = data;
597
598         disable_irq(sdata->client->irq);
599         regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
600                                                 sdata->regulators);
601 }
602
603 /* This function is void because I don't want to prevent using the touch key
604  * only because the LEDs don't get registered
605  */
606 static int stmfts_enable_led(struct stmfts_data *sdata)
607 {
608         int err;
609
610         /* get the regulator for powering the leds on */
611         sdata->ledvdd = devm_regulator_get(&sdata->client->dev, "ledvdd");
612         if (IS_ERR(sdata->ledvdd))
613                 return PTR_ERR(sdata->ledvdd);
614
615         sdata->led_cdev.name = STMFTS_DEV_NAME;
616         sdata->led_cdev.max_brightness = LED_ON;
617         sdata->led_cdev.brightness = LED_OFF;
618         sdata->led_cdev.brightness_set_blocking = stmfts_brightness_set;
619         sdata->led_cdev.brightness_get = stmfts_brightness_get;
620
621         err = devm_led_classdev_register(&sdata->client->dev, &sdata->led_cdev);
622         if (err) {
623                 devm_regulator_put(sdata->ledvdd);
624                 return err;
625         }
626
627         return 0;
628 }
629
630 static int stmfts_probe(struct i2c_client *client,
631                         const struct i2c_device_id *id)
632 {
633         int err;
634         struct stmfts_data *sdata;
635
636         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
637                                                 I2C_FUNC_SMBUS_BYTE_DATA |
638                                                 I2C_FUNC_SMBUS_I2C_BLOCK))
639                 return -ENODEV;
640
641         sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
642         if (!sdata)
643                 return -ENOMEM;
644
645         i2c_set_clientdata(client, sdata);
646
647         sdata->client = client;
648         mutex_init(&sdata->mutex);
649         init_completion(&sdata->cmd_done);
650
651         sdata->regulators[STMFTS_REGULATOR_VDD].supply = "vdd";
652         sdata->regulators[STMFTS_REGULATOR_AVDD].supply = "avdd";
653         err = devm_regulator_bulk_get(&client->dev,
654                                       ARRAY_SIZE(sdata->regulators),
655                                       sdata->regulators);
656         if (err)
657                 return err;
658
659         sdata->input = devm_input_allocate_device(&client->dev);
660         if (!sdata->input)
661                 return -ENOMEM;
662
663         sdata->input->name = STMFTS_DEV_NAME;
664         sdata->input->id.bustype = BUS_I2C;
665         sdata->input->open = stmfts_input_open;
666         sdata->input->close = stmfts_input_close;
667
668         input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_X);
669         input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_Y);
670         touchscreen_parse_properties(sdata->input, true, &sdata->prop);
671
672         input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
673         input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
674         input_set_abs_params(sdata->input, ABS_MT_ORIENTATION, 0, 255, 0, 0);
675         input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
676         input_set_abs_params(sdata->input, ABS_DISTANCE, 0, 255, 0, 0);
677
678         sdata->use_key = device_property_read_bool(&client->dev,
679                                                    "touch-key-connected");
680         if (sdata->use_key) {
681                 input_set_capability(sdata->input, EV_KEY, KEY_MENU);
682                 input_set_capability(sdata->input, EV_KEY, KEY_BACK);
683         }
684
685         err = input_mt_init_slots(sdata->input,
686                                   STMFTS_MAX_FINGERS, INPUT_MT_DIRECT);
687         if (err)
688                 return err;
689
690         input_set_drvdata(sdata->input, sdata);
691
692         /*
693          * stmfts_power_on expects interrupt to be disabled, but
694          * at this point the device is still off and I do not trust
695          * the status of the irq line that can generate some spurious
696          * interrupts. To be on the safe side it's better to not enable
697          * the interrupts during their request.
698          */
699         irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
700         err = devm_request_threaded_irq(&client->dev, client->irq,
701                                         NULL, stmfts_irq_handler,
702                                         IRQF_ONESHOT,
703                                         "stmfts_irq", sdata);
704         if (err)
705                 return err;
706
707         dev_dbg(&client->dev, "initializing ST-Microelectronics FTS...\n");
708
709         err = stmfts_power_on(sdata);
710         if (err)
711                 return err;
712
713         err = devm_add_action_or_reset(&client->dev, stmfts_power_off, sdata);
714         if (err)
715                 return err;
716
717         err = input_register_device(sdata->input);
718         if (err)
719                 return err;
720
721         if (sdata->use_key) {
722                 err = stmfts_enable_led(sdata);
723                 if (err) {
724                         /*
725                          * Even if the LEDs have failed to be initialized and
726                          * used in the driver, I can still use the device even
727                          * without LEDs. The ledvdd regulator pointer will be
728                          * used as a flag.
729                          */
730                         dev_warn(&client->dev, "unable to use touchkey leds\n");
731                         sdata->ledvdd = NULL;
732                 }
733         }
734
735         err = sysfs_create_group(&sdata->client->dev.kobj,
736                                  &stmfts_attribute_group);
737         if (err)
738                 return err;
739
740         pm_runtime_enable(&client->dev);
741
742         return 0;
743 }
744
745 static int stmfts_remove(struct i2c_client *client)
746 {
747         pm_runtime_disable(&client->dev);
748         sysfs_remove_group(&client->dev.kobj, &stmfts_attribute_group);
749
750         return 0;
751 }
752
753 static int __maybe_unused stmfts_runtime_suspend(struct device *dev)
754 {
755         struct stmfts_data *sdata = dev_get_drvdata(dev);
756         int ret;
757
758         ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
759         if (ret)
760                 dev_warn(dev, "failed to suspend device: %d\n", ret);
761
762         return ret;
763 }
764
765 static int __maybe_unused stmfts_runtime_resume(struct device *dev)
766 {
767         struct stmfts_data *sdata = dev_get_drvdata(dev);
768         int ret;
769
770         ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_OUT);
771         if (ret)
772                 dev_err(dev, "failed to resume device: %d\n", ret);
773
774         return ret;
775 }
776
777 static int __maybe_unused stmfts_suspend(struct device *dev)
778 {
779         struct stmfts_data *sdata = dev_get_drvdata(dev);
780
781         stmfts_power_off(sdata);
782
783         return 0;
784 }
785
786 static int __maybe_unused stmfts_resume(struct device *dev)
787 {
788         struct stmfts_data *sdata = dev_get_drvdata(dev);
789
790         return stmfts_power_on(sdata);
791 }
792
793 static const struct dev_pm_ops stmfts_pm_ops = {
794         SET_SYSTEM_SLEEP_PM_OPS(stmfts_suspend, stmfts_resume)
795         SET_RUNTIME_PM_OPS(stmfts_runtime_suspend, stmfts_runtime_resume, NULL)
796 };
797
798 #ifdef CONFIG_OF
799 static const struct of_device_id stmfts_of_match[] = {
800         { .compatible = "st,stmfts", },
801         { },
802 };
803 MODULE_DEVICE_TABLE(of, stmfts_of_match);
804 #endif
805
806 static const struct i2c_device_id stmfts_id[] = {
807         { "stmfts", 0 },
808         { },
809 };
810 MODULE_DEVICE_TABLE(i2c, stmfts_id);
811
812 static struct i2c_driver stmfts_driver = {
813         .driver = {
814                 .name = STMFTS_DEV_NAME,
815                 .of_match_table = of_match_ptr(stmfts_of_match),
816                 .pm = &stmfts_pm_ops,
817         },
818         .probe = stmfts_probe,
819         .remove = stmfts_remove,
820         .id_table = stmfts_id,
821 };
822
823 module_i2c_driver(stmfts_driver);
824
825 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
826 MODULE_DESCRIPTION("STMicroelectronics FTS Touch Screen");
827 MODULE_LICENSE("GPL v2");