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