GNU Linux-libre 4.14.332-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                 goto out;
348
349         err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
350         if (err)
351                 goto out;
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 out:
375         pm_runtime_put_noidle(&sdata->client->dev);
376         return err;
377 }
378
379 static void stmfts_input_close(struct input_dev *dev)
380 {
381         struct stmfts_data *sdata = input_get_drvdata(dev);
382         int err;
383
384         err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_OFF);
385         if (err)
386                 dev_warn(&sdata->client->dev,
387                          "failed to disable touchscreen: %d\n", err);
388
389         mutex_lock(&sdata->mutex);
390
391         sdata->running = false;
392
393         if (sdata->hover_enabled) {
394                 err = i2c_smbus_write_byte(sdata->client,
395                                            STMFTS_SS_HOVER_SENSE_OFF);
396                 if (err)
397                         dev_warn(&sdata->client->dev,
398                                  "failed to disable hover: %d\n", err);
399         }
400         mutex_unlock(&sdata->mutex);
401
402         if (sdata->use_key) {
403                 err = i2c_smbus_write_byte(sdata->client,
404                                            STMFTS_MS_KEY_SENSE_OFF);
405                 if (err)
406                         dev_warn(&sdata->client->dev,
407                                  "failed to disable touchkey: %d\n", err);
408         }
409
410         pm_runtime_put_sync(&sdata->client->dev);
411 }
412
413 static ssize_t stmfts_sysfs_chip_id(struct device *dev,
414                                 struct device_attribute *attr, char *buf)
415 {
416         struct stmfts_data *sdata = dev_get_drvdata(dev);
417
418         return sprintf(buf, "%#x\n", sdata->chip_id);
419 }
420
421 static ssize_t stmfts_sysfs_chip_version(struct device *dev,
422                                 struct device_attribute *attr, char *buf)
423 {
424         struct stmfts_data *sdata = dev_get_drvdata(dev);
425
426         return sprintf(buf, "%u\n", sdata->chip_ver);
427 }
428
429 static ssize_t stmfts_sysfs_fw_ver(struct device *dev,
430                                 struct device_attribute *attr, char *buf)
431 {
432         struct stmfts_data *sdata = dev_get_drvdata(dev);
433
434         return sprintf(buf, "%u\n", sdata->fw_ver);
435 }
436
437 static ssize_t stmfts_sysfs_config_id(struct device *dev,
438                                 struct device_attribute *attr, char *buf)
439 {
440         struct stmfts_data *sdata = dev_get_drvdata(dev);
441
442         return sprintf(buf, "%#x\n", sdata->config_id);
443 }
444
445 static ssize_t stmfts_sysfs_config_version(struct device *dev,
446                                 struct device_attribute *attr, char *buf)
447 {
448         struct stmfts_data *sdata = dev_get_drvdata(dev);
449
450         return sprintf(buf, "%u\n", sdata->config_ver);
451 }
452
453 static ssize_t stmfts_sysfs_read_status(struct device *dev,
454                                 struct device_attribute *attr, char *buf)
455 {
456         struct stmfts_data *sdata = dev_get_drvdata(dev);
457         u8 status[4];
458         int err;
459
460         err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_STATUS,
461                                             sizeof(status), status);
462         if (err)
463                 return err;
464
465         return sprintf(buf, "%#02x\n", status[0]);
466 }
467
468 static ssize_t stmfts_sysfs_hover_enable_read(struct device *dev,
469                                 struct device_attribute *attr, char *buf)
470 {
471         struct stmfts_data *sdata = dev_get_drvdata(dev);
472
473         return sprintf(buf, "%u\n", sdata->hover_enabled);
474 }
475
476 static ssize_t stmfts_sysfs_hover_enable_write(struct device *dev,
477                                 struct device_attribute *attr,
478                                 const char *buf, size_t len)
479 {
480         struct stmfts_data *sdata = dev_get_drvdata(dev);
481         unsigned long value;
482         int err = 0;
483
484         if (kstrtoul(buf, 0, &value))
485                 return -EINVAL;
486
487         mutex_lock(&sdata->mutex);
488
489         if (value && sdata->hover_enabled)
490                 goto out;
491
492         if (sdata->running)
493                 err = i2c_smbus_write_byte(sdata->client,
494                                            value ? STMFTS_SS_HOVER_SENSE_ON :
495                                                    STMFTS_SS_HOVER_SENSE_OFF);
496
497         if (!err)
498                 sdata->hover_enabled = !!value;
499
500 out:
501         mutex_unlock(&sdata->mutex);
502
503         return len;
504 }
505
506 static DEVICE_ATTR(chip_id, 0444, stmfts_sysfs_chip_id, NULL);
507 static DEVICE_ATTR(chip_version, 0444, stmfts_sysfs_chip_version, NULL);
508 static DEVICE_ATTR(fw_ver, 0444, stmfts_sysfs_fw_ver, NULL);
509 static DEVICE_ATTR(config_id, 0444, stmfts_sysfs_config_id, NULL);
510 static DEVICE_ATTR(config_version, 0444, stmfts_sysfs_config_version, NULL);
511 static DEVICE_ATTR(status, 0444, stmfts_sysfs_read_status, NULL);
512 static DEVICE_ATTR(hover_enable, 0644, stmfts_sysfs_hover_enable_read,
513                                         stmfts_sysfs_hover_enable_write);
514
515 static struct attribute *stmfts_sysfs_attrs[] = {
516         &dev_attr_chip_id.attr,
517         &dev_attr_chip_version.attr,
518         &dev_attr_fw_ver.attr,
519         &dev_attr_config_id.attr,
520         &dev_attr_config_version.attr,
521         &dev_attr_status.attr,
522         &dev_attr_hover_enable.attr,
523         NULL
524 };
525
526 static struct attribute_group stmfts_attribute_group = {
527         .attrs = stmfts_sysfs_attrs
528 };
529
530 static int stmfts_power_on(struct stmfts_data *sdata)
531 {
532         int err;
533         u8 reg[8];
534
535         err = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
536                                     sdata->regulators);
537         if (err)
538                 return err;
539
540         /*
541          * The datasheet does not specify the power on time, but considering
542          * that the reset time is < 10ms, I sleep 20ms to be sure
543          */
544         msleep(20);
545
546         err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_INFO,
547                                             sizeof(reg), reg);
548         if (err < 0)
549                 return err;
550         if (err != sizeof(reg))
551                 return -EIO;
552
553         sdata->chip_id = be16_to_cpup((__be16 *)&reg[6]);
554         sdata->chip_ver = reg[0];
555         sdata->fw_ver = be16_to_cpup((__be16 *)&reg[2]);
556         sdata->config_id = reg[4];
557         sdata->config_ver = reg[5];
558
559         enable_irq(sdata->client->irq);
560
561         msleep(50);
562
563         err = stmfts_command(sdata, STMFTS_SYSTEM_RESET);
564         if (err)
565                 return err;
566
567         err = stmfts_command(sdata, STMFTS_SLEEP_OUT);
568         if (err)
569                 return err;
570
571         /* optional tuning */
572         err = stmfts_command(sdata, STMFTS_MS_CX_TUNING);
573         if (err)
574                 dev_warn(&sdata->client->dev,
575                          "failed to perform mutual auto tune: %d\n", err);
576
577         /* optional tuning */
578         err = stmfts_command(sdata, STMFTS_SS_CX_TUNING);
579         if (err)
580                 dev_warn(&sdata->client->dev,
581                          "failed to perform self auto tune: %d\n", err);
582
583         err = stmfts_command(sdata, STMFTS_FULL_FORCE_CALIBRATION);
584         if (err)
585                 return err;
586
587         /*
588          * At this point no one is using the touchscreen
589          * and I don't really care about the return value
590          */
591         (void) i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
592
593         return 0;
594 }
595
596 static void stmfts_power_off(void *data)
597 {
598         struct stmfts_data *sdata = data;
599
600         disable_irq(sdata->client->irq);
601         regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
602                                                 sdata->regulators);
603 }
604
605 /* This function is void because I don't want to prevent using the touch key
606  * only because the LEDs don't get registered
607  */
608 static int stmfts_enable_led(struct stmfts_data *sdata)
609 {
610         int err;
611
612         /* get the regulator for powering the leds on */
613         sdata->ledvdd = devm_regulator_get(&sdata->client->dev, "ledvdd");
614         if (IS_ERR(sdata->ledvdd))
615                 return PTR_ERR(sdata->ledvdd);
616
617         sdata->led_cdev.name = STMFTS_DEV_NAME;
618         sdata->led_cdev.max_brightness = LED_ON;
619         sdata->led_cdev.brightness = LED_OFF;
620         sdata->led_cdev.brightness_set_blocking = stmfts_brightness_set;
621         sdata->led_cdev.brightness_get = stmfts_brightness_get;
622
623         err = devm_led_classdev_register(&sdata->client->dev, &sdata->led_cdev);
624         if (err) {
625                 devm_regulator_put(sdata->ledvdd);
626                 return err;
627         }
628
629         return 0;
630 }
631
632 static int stmfts_probe(struct i2c_client *client,
633                         const struct i2c_device_id *id)
634 {
635         int err;
636         struct stmfts_data *sdata;
637
638         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
639                                                 I2C_FUNC_SMBUS_BYTE_DATA |
640                                                 I2C_FUNC_SMBUS_I2C_BLOCK))
641                 return -ENODEV;
642
643         sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
644         if (!sdata)
645                 return -ENOMEM;
646
647         i2c_set_clientdata(client, sdata);
648
649         sdata->client = client;
650         mutex_init(&sdata->mutex);
651         init_completion(&sdata->cmd_done);
652
653         sdata->regulators[STMFTS_REGULATOR_VDD].supply = "vdd";
654         sdata->regulators[STMFTS_REGULATOR_AVDD].supply = "avdd";
655         err = devm_regulator_bulk_get(&client->dev,
656                                       ARRAY_SIZE(sdata->regulators),
657                                       sdata->regulators);
658         if (err)
659                 return err;
660
661         sdata->input = devm_input_allocate_device(&client->dev);
662         if (!sdata->input)
663                 return -ENOMEM;
664
665         sdata->input->name = STMFTS_DEV_NAME;
666         sdata->input->id.bustype = BUS_I2C;
667         sdata->input->open = stmfts_input_open;
668         sdata->input->close = stmfts_input_close;
669
670         input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_X);
671         input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_Y);
672         touchscreen_parse_properties(sdata->input, true, &sdata->prop);
673
674         input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
675         input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
676         input_set_abs_params(sdata->input, ABS_MT_ORIENTATION, 0, 255, 0, 0);
677         input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
678         input_set_abs_params(sdata->input, ABS_DISTANCE, 0, 255, 0, 0);
679
680         sdata->use_key = device_property_read_bool(&client->dev,
681                                                    "touch-key-connected");
682         if (sdata->use_key) {
683                 input_set_capability(sdata->input, EV_KEY, KEY_MENU);
684                 input_set_capability(sdata->input, EV_KEY, KEY_BACK);
685         }
686
687         err = input_mt_init_slots(sdata->input,
688                                   STMFTS_MAX_FINGERS, INPUT_MT_DIRECT);
689         if (err)
690                 return err;
691
692         input_set_drvdata(sdata->input, sdata);
693
694         /*
695          * stmfts_power_on expects interrupt to be disabled, but
696          * at this point the device is still off and I do not trust
697          * the status of the irq line that can generate some spurious
698          * interrupts. To be on the safe side it's better to not enable
699          * the interrupts during their request.
700          */
701         irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
702         err = devm_request_threaded_irq(&client->dev, client->irq,
703                                         NULL, stmfts_irq_handler,
704                                         IRQF_ONESHOT,
705                                         "stmfts_irq", sdata);
706         if (err)
707                 return err;
708
709         dev_dbg(&client->dev, "initializing ST-Microelectronics FTS...\n");
710
711         err = stmfts_power_on(sdata);
712         if (err)
713                 return err;
714
715         err = devm_add_action_or_reset(&client->dev, stmfts_power_off, sdata);
716         if (err)
717                 return err;
718
719         err = input_register_device(sdata->input);
720         if (err)
721                 return err;
722
723         if (sdata->use_key) {
724                 err = stmfts_enable_led(sdata);
725                 if (err) {
726                         /*
727                          * Even if the LEDs have failed to be initialized and
728                          * used in the driver, I can still use the device even
729                          * without LEDs. The ledvdd regulator pointer will be
730                          * used as a flag.
731                          */
732                         dev_warn(&client->dev, "unable to use touchkey leds\n");
733                         sdata->ledvdd = NULL;
734                 }
735         }
736
737         err = sysfs_create_group(&sdata->client->dev.kobj,
738                                  &stmfts_attribute_group);
739         if (err)
740                 return err;
741
742         pm_runtime_enable(&client->dev);
743
744         return 0;
745 }
746
747 static int stmfts_remove(struct i2c_client *client)
748 {
749         pm_runtime_disable(&client->dev);
750         sysfs_remove_group(&client->dev.kobj, &stmfts_attribute_group);
751
752         return 0;
753 }
754
755 static int __maybe_unused stmfts_runtime_suspend(struct device *dev)
756 {
757         struct stmfts_data *sdata = dev_get_drvdata(dev);
758         int ret;
759
760         ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
761         if (ret)
762                 dev_warn(dev, "failed to suspend device: %d\n", ret);
763
764         return ret;
765 }
766
767 static int __maybe_unused stmfts_runtime_resume(struct device *dev)
768 {
769         struct stmfts_data *sdata = dev_get_drvdata(dev);
770         int ret;
771
772         ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_OUT);
773         if (ret)
774                 dev_err(dev, "failed to resume device: %d\n", ret);
775
776         return ret;
777 }
778
779 static int __maybe_unused stmfts_suspend(struct device *dev)
780 {
781         struct stmfts_data *sdata = dev_get_drvdata(dev);
782
783         stmfts_power_off(sdata);
784
785         return 0;
786 }
787
788 static int __maybe_unused stmfts_resume(struct device *dev)
789 {
790         struct stmfts_data *sdata = dev_get_drvdata(dev);
791
792         return stmfts_power_on(sdata);
793 }
794
795 static const struct dev_pm_ops stmfts_pm_ops = {
796         SET_SYSTEM_SLEEP_PM_OPS(stmfts_suspend, stmfts_resume)
797         SET_RUNTIME_PM_OPS(stmfts_runtime_suspend, stmfts_runtime_resume, NULL)
798 };
799
800 #ifdef CONFIG_OF
801 static const struct of_device_id stmfts_of_match[] = {
802         { .compatible = "st,stmfts", },
803         { },
804 };
805 MODULE_DEVICE_TABLE(of, stmfts_of_match);
806 #endif
807
808 static const struct i2c_device_id stmfts_id[] = {
809         { "stmfts", 0 },
810         { },
811 };
812 MODULE_DEVICE_TABLE(i2c, stmfts_id);
813
814 static struct i2c_driver stmfts_driver = {
815         .driver = {
816                 .name = STMFTS_DEV_NAME,
817                 .of_match_table = of_match_ptr(stmfts_of_match),
818                 .pm = &stmfts_pm_ops,
819         },
820         .probe = stmfts_probe,
821         .remove = stmfts_remove,
822         .id_table = stmfts_id,
823 };
824
825 module_i2c_driver(stmfts_driver);
826
827 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
828 MODULE_DESCRIPTION("STMicroelectronics FTS Touch Screen");
829 MODULE_LICENSE("GPL v2");