xref: /linux/drivers/input/touchscreen/stmfts.c (revision adf313f41a0d6495ac9b313d012a291c6dad6b21)
1f8c5938fSAndi Shyti // SPDX-License-Identifier: GPL-2.0
2f8c5938fSAndi Shyti // STMicroelectronics FTS Touchscreen device driver
3f8c5938fSAndi Shyti //
4f8c5938fSAndi Shyti // Copyright (c) 2017 Samsung Electronics Co., Ltd.
5*adf313f4SAndi Shyti // Copyright (c) 2017 Andi Shyti <andi@etezian.org>
678bcac7bSAndi Shyti 
778bcac7bSAndi Shyti #include <linux/delay.h>
878bcac7bSAndi Shyti #include <linux/i2c.h>
978bcac7bSAndi Shyti #include <linux/input/mt.h>
1078bcac7bSAndi Shyti #include <linux/input/touchscreen.h>
1178bcac7bSAndi Shyti #include <linux/interrupt.h>
1278bcac7bSAndi Shyti #include <linux/irq.h>
1378bcac7bSAndi Shyti #include <linux/leds.h>
1478bcac7bSAndi Shyti #include <linux/module.h>
1578bcac7bSAndi Shyti #include <linux/pm_runtime.h>
1678bcac7bSAndi Shyti #include <linux/regulator/consumer.h>
1778bcac7bSAndi Shyti 
1878bcac7bSAndi Shyti /* I2C commands */
1978bcac7bSAndi Shyti #define STMFTS_READ_INFO			0x80
2078bcac7bSAndi Shyti #define STMFTS_READ_STATUS			0x84
2178bcac7bSAndi Shyti #define STMFTS_READ_ONE_EVENT			0x85
2278bcac7bSAndi Shyti #define STMFTS_READ_ALL_EVENT			0x86
2378bcac7bSAndi Shyti #define STMFTS_LATEST_EVENT			0x87
2478bcac7bSAndi Shyti #define STMFTS_SLEEP_IN				0x90
2578bcac7bSAndi Shyti #define STMFTS_SLEEP_OUT			0x91
2678bcac7bSAndi Shyti #define STMFTS_MS_MT_SENSE_OFF			0x92
2778bcac7bSAndi Shyti #define STMFTS_MS_MT_SENSE_ON			0x93
2878bcac7bSAndi Shyti #define STMFTS_SS_HOVER_SENSE_OFF		0x94
2978bcac7bSAndi Shyti #define STMFTS_SS_HOVER_SENSE_ON		0x95
3078bcac7bSAndi Shyti #define STMFTS_MS_KEY_SENSE_OFF			0x9a
3178bcac7bSAndi Shyti #define STMFTS_MS_KEY_SENSE_ON			0x9b
3278bcac7bSAndi Shyti #define STMFTS_SYSTEM_RESET			0xa0
3378bcac7bSAndi Shyti #define STMFTS_CLEAR_EVENT_STACK		0xa1
3478bcac7bSAndi Shyti #define STMFTS_FULL_FORCE_CALIBRATION		0xa2
3578bcac7bSAndi Shyti #define STMFTS_MS_CX_TUNING			0xa3
3678bcac7bSAndi Shyti #define STMFTS_SS_CX_TUNING			0xa4
3778bcac7bSAndi Shyti 
3878bcac7bSAndi Shyti /* events */
3978bcac7bSAndi Shyti #define STMFTS_EV_NO_EVENT			0x00
4078bcac7bSAndi Shyti #define STMFTS_EV_MULTI_TOUCH_DETECTED		0x02
4178bcac7bSAndi Shyti #define STMFTS_EV_MULTI_TOUCH_ENTER		0x03
4278bcac7bSAndi Shyti #define STMFTS_EV_MULTI_TOUCH_LEAVE		0x04
4378bcac7bSAndi Shyti #define STMFTS_EV_MULTI_TOUCH_MOTION		0x05
4478bcac7bSAndi Shyti #define STMFTS_EV_HOVER_ENTER			0x07
4578bcac7bSAndi Shyti #define STMFTS_EV_HOVER_LEAVE			0x08
4678bcac7bSAndi Shyti #define STMFTS_EV_HOVER_MOTION			0x09
4778bcac7bSAndi Shyti #define STMFTS_EV_KEY_STATUS			0x0e
4878bcac7bSAndi Shyti #define STMFTS_EV_ERROR				0x0f
4978bcac7bSAndi Shyti #define STMFTS_EV_CONTROLLER_READY		0x10
5078bcac7bSAndi Shyti #define STMFTS_EV_SLEEP_OUT_CONTROLLER_READY	0x11
5178bcac7bSAndi Shyti #define STMFTS_EV_STATUS			0x16
5278bcac7bSAndi Shyti #define STMFTS_EV_DEBUG				0xdb
5378bcac7bSAndi Shyti 
5478bcac7bSAndi Shyti /* multi touch related event masks */
5578bcac7bSAndi Shyti #define STMFTS_MASK_EVENT_ID			0x0f
5678bcac7bSAndi Shyti #define STMFTS_MASK_TOUCH_ID			0xf0
5778bcac7bSAndi Shyti #define STMFTS_MASK_LEFT_EVENT			0x0f
5878bcac7bSAndi Shyti #define STMFTS_MASK_X_MSB			0x0f
5978bcac7bSAndi Shyti #define STMFTS_MASK_Y_LSB			0xf0
6078bcac7bSAndi Shyti 
6178bcac7bSAndi Shyti /* key related event masks */
6278bcac7bSAndi Shyti #define STMFTS_MASK_KEY_NO_TOUCH		0x00
6378bcac7bSAndi Shyti #define STMFTS_MASK_KEY_MENU			0x01
6478bcac7bSAndi Shyti #define STMFTS_MASK_KEY_BACK			0x02
6578bcac7bSAndi Shyti 
6678bcac7bSAndi Shyti #define STMFTS_EVENT_SIZE	8
6778bcac7bSAndi Shyti #define STMFTS_STACK_DEPTH	32
6878bcac7bSAndi Shyti #define STMFTS_DATA_MAX_SIZE	(STMFTS_EVENT_SIZE * STMFTS_STACK_DEPTH)
6978bcac7bSAndi Shyti #define STMFTS_MAX_FINGERS	10
7078bcac7bSAndi Shyti #define STMFTS_DEV_NAME		"stmfts"
7178bcac7bSAndi Shyti 
7278bcac7bSAndi Shyti enum stmfts_regulators {
7378bcac7bSAndi Shyti 	STMFTS_REGULATOR_VDD,
7478bcac7bSAndi Shyti 	STMFTS_REGULATOR_AVDD,
7578bcac7bSAndi Shyti };
7678bcac7bSAndi Shyti 
7778bcac7bSAndi Shyti struct stmfts_data {
7878bcac7bSAndi Shyti 	struct i2c_client *client;
7978bcac7bSAndi Shyti 	struct input_dev *input;
8078bcac7bSAndi Shyti 	struct led_classdev led_cdev;
8178bcac7bSAndi Shyti 	struct mutex mutex;
8278bcac7bSAndi Shyti 
8378bcac7bSAndi Shyti 	struct touchscreen_properties prop;
8478bcac7bSAndi Shyti 
8578bcac7bSAndi Shyti 	struct regulator_bulk_data regulators[2];
8678bcac7bSAndi Shyti 
8778bcac7bSAndi Shyti 	/*
8878bcac7bSAndi Shyti 	 * Presence of ledvdd will be used also to check
8978bcac7bSAndi Shyti 	 * whether the LED is supported.
9078bcac7bSAndi Shyti 	 */
9178bcac7bSAndi Shyti 	struct regulator *ledvdd;
9278bcac7bSAndi Shyti 
9378bcac7bSAndi Shyti 	u16 chip_id;
9478bcac7bSAndi Shyti 	u8 chip_ver;
9578bcac7bSAndi Shyti 	u16 fw_ver;
9678bcac7bSAndi Shyti 	u8 config_id;
9778bcac7bSAndi Shyti 	u8 config_ver;
9878bcac7bSAndi Shyti 
9978bcac7bSAndi Shyti 	u8 data[STMFTS_DATA_MAX_SIZE];
10078bcac7bSAndi Shyti 
10178bcac7bSAndi Shyti 	struct completion cmd_done;
10278bcac7bSAndi Shyti 
10378bcac7bSAndi Shyti 	bool use_key;
10478bcac7bSAndi Shyti 	bool led_status;
10578bcac7bSAndi Shyti 	bool hover_enabled;
10678bcac7bSAndi Shyti 	bool running;
10778bcac7bSAndi Shyti };
10878bcac7bSAndi Shyti 
10978bcac7bSAndi Shyti static void stmfts_brightness_set(struct led_classdev *led_cdev,
11078bcac7bSAndi Shyti 					enum led_brightness value)
11178bcac7bSAndi Shyti {
11278bcac7bSAndi Shyti 	struct stmfts_data *sdata = container_of(led_cdev,
11378bcac7bSAndi Shyti 					struct stmfts_data, led_cdev);
11478bcac7bSAndi Shyti 	int err;
11578bcac7bSAndi Shyti 
11678bcac7bSAndi Shyti 	if (value == sdata->led_status || !sdata->ledvdd)
11778bcac7bSAndi Shyti 		return;
11878bcac7bSAndi Shyti 
11978bcac7bSAndi Shyti 	if (!value) {
12078bcac7bSAndi Shyti 		regulator_disable(sdata->ledvdd);
12178bcac7bSAndi Shyti 	} else {
12278bcac7bSAndi Shyti 		err = regulator_enable(sdata->ledvdd);
12378bcac7bSAndi Shyti 		if (err)
12478bcac7bSAndi Shyti 			dev_warn(&sdata->client->dev,
12578bcac7bSAndi Shyti 				 "failed to disable ledvdd regulator: %d\n",
12678bcac7bSAndi Shyti 				 err);
12778bcac7bSAndi Shyti 	}
12878bcac7bSAndi Shyti 
12978bcac7bSAndi Shyti 	sdata->led_status = value;
13078bcac7bSAndi Shyti }
13178bcac7bSAndi Shyti 
13278bcac7bSAndi Shyti static enum led_brightness stmfts_brightness_get(struct led_classdev *led_cdev)
13378bcac7bSAndi Shyti {
13478bcac7bSAndi Shyti 	struct stmfts_data *sdata = container_of(led_cdev,
13578bcac7bSAndi Shyti 						struct stmfts_data, led_cdev);
13678bcac7bSAndi Shyti 
13778bcac7bSAndi Shyti 	return !!regulator_is_enabled(sdata->ledvdd);
13878bcac7bSAndi Shyti }
13978bcac7bSAndi Shyti 
14078bcac7bSAndi Shyti /*
14178bcac7bSAndi Shyti  * We can't simply use i2c_smbus_read_i2c_block_data because we
14278bcac7bSAndi Shyti  * need to read more than 255 bytes (
14378bcac7bSAndi Shyti  */
14478bcac7bSAndi Shyti static int stmfts_read_events(struct stmfts_data *sdata)
14578bcac7bSAndi Shyti {
14678bcac7bSAndi Shyti 	u8 cmd = STMFTS_READ_ALL_EVENT;
14778bcac7bSAndi Shyti 	struct i2c_msg msgs[2] = {
14878bcac7bSAndi Shyti 		{
14978bcac7bSAndi Shyti 			.addr	= sdata->client->addr,
15078bcac7bSAndi Shyti 			.len	= 1,
15178bcac7bSAndi Shyti 			.buf	= &cmd,
15278bcac7bSAndi Shyti 		},
15378bcac7bSAndi Shyti 		{
15478bcac7bSAndi Shyti 			.addr	= sdata->client->addr,
15578bcac7bSAndi Shyti 			.flags	= I2C_M_RD,
15678bcac7bSAndi Shyti 			.len	= STMFTS_DATA_MAX_SIZE,
15778bcac7bSAndi Shyti 			.buf	= sdata->data,
15878bcac7bSAndi Shyti 		},
15978bcac7bSAndi Shyti 	};
16078bcac7bSAndi Shyti 	int ret;
16178bcac7bSAndi Shyti 
16278bcac7bSAndi Shyti 	ret = i2c_transfer(sdata->client->adapter, msgs, ARRAY_SIZE(msgs));
16378bcac7bSAndi Shyti 	if (ret < 0)
16478bcac7bSAndi Shyti 		return ret;
16578bcac7bSAndi Shyti 
16678bcac7bSAndi Shyti 	return ret == ARRAY_SIZE(msgs) ? 0 : -EIO;
16778bcac7bSAndi Shyti }
16878bcac7bSAndi Shyti 
16978bcac7bSAndi Shyti static void stmfts_report_contact_event(struct stmfts_data *sdata,
17078bcac7bSAndi Shyti 					const u8 event[])
17178bcac7bSAndi Shyti {
17278bcac7bSAndi Shyti 	u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4;
17378bcac7bSAndi Shyti 	u16 x = event[1] | ((event[2] & STMFTS_MASK_X_MSB) << 8);
17478bcac7bSAndi Shyti 	u16 y = (event[2] >> 4) | (event[3] << 4);
17578bcac7bSAndi Shyti 	u8 maj = event[4];
17678bcac7bSAndi Shyti 	u8 min = event[5];
17778bcac7bSAndi Shyti 	u8 orientation = event[6];
17878bcac7bSAndi Shyti 	u8 area = event[7];
17978bcac7bSAndi Shyti 
18078bcac7bSAndi Shyti 	input_mt_slot(sdata->input, slot_id);
18178bcac7bSAndi Shyti 
18278bcac7bSAndi Shyti 	input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, true);
18378bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_MT_POSITION_X, x);
18478bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_MT_POSITION_Y, y);
18578bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_MT_TOUCH_MAJOR, maj);
18678bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_MT_TOUCH_MINOR, min);
18778bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_MT_PRESSURE, area);
18878bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_MT_ORIENTATION, orientation);
18978bcac7bSAndi Shyti 
19078bcac7bSAndi Shyti 	input_sync(sdata->input);
19178bcac7bSAndi Shyti }
19278bcac7bSAndi Shyti 
19378bcac7bSAndi Shyti static void stmfts_report_contact_release(struct stmfts_data *sdata,
19478bcac7bSAndi Shyti 					  const u8 event[])
19578bcac7bSAndi Shyti {
19678bcac7bSAndi Shyti 	u8 slot_id = (event[0] & STMFTS_MASK_TOUCH_ID) >> 4;
19778bcac7bSAndi Shyti 
19878bcac7bSAndi Shyti 	input_mt_slot(sdata->input, slot_id);
19978bcac7bSAndi Shyti 	input_mt_report_slot_state(sdata->input, MT_TOOL_FINGER, false);
20078bcac7bSAndi Shyti 
20178bcac7bSAndi Shyti 	input_sync(sdata->input);
20278bcac7bSAndi Shyti }
20378bcac7bSAndi Shyti 
20478bcac7bSAndi Shyti static void stmfts_report_hover_event(struct stmfts_data *sdata,
20578bcac7bSAndi Shyti 				      const u8 event[])
20678bcac7bSAndi Shyti {
20778bcac7bSAndi Shyti 	u16 x = (event[2] << 4) | (event[4] >> 4);
20878bcac7bSAndi Shyti 	u16 y = (event[3] << 4) | (event[4] & STMFTS_MASK_Y_LSB);
20978bcac7bSAndi Shyti 	u8 z = event[5];
21078bcac7bSAndi Shyti 
21178bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_X, x);
21278bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_Y, y);
21378bcac7bSAndi Shyti 	input_report_abs(sdata->input, ABS_DISTANCE, z);
21478bcac7bSAndi Shyti 
21578bcac7bSAndi Shyti 	input_sync(sdata->input);
21678bcac7bSAndi Shyti }
21778bcac7bSAndi Shyti 
21878bcac7bSAndi Shyti static void stmfts_report_key_event(struct stmfts_data *sdata, const u8 event[])
21978bcac7bSAndi Shyti {
22078bcac7bSAndi Shyti 	switch (event[2]) {
22178bcac7bSAndi Shyti 	case 0:
22278bcac7bSAndi Shyti 		input_report_key(sdata->input, KEY_BACK, 0);
22378bcac7bSAndi Shyti 		input_report_key(sdata->input, KEY_MENU, 0);
22478bcac7bSAndi Shyti 		break;
22578bcac7bSAndi Shyti 
22678bcac7bSAndi Shyti 	case STMFTS_MASK_KEY_BACK:
22778bcac7bSAndi Shyti 		input_report_key(sdata->input, KEY_BACK, 1);
22878bcac7bSAndi Shyti 		break;
22978bcac7bSAndi Shyti 
23078bcac7bSAndi Shyti 	case STMFTS_MASK_KEY_MENU:
23178bcac7bSAndi Shyti 		input_report_key(sdata->input, KEY_MENU, 1);
23278bcac7bSAndi Shyti 		break;
23378bcac7bSAndi Shyti 
23478bcac7bSAndi Shyti 	default:
23578bcac7bSAndi Shyti 		dev_warn(&sdata->client->dev,
23678bcac7bSAndi Shyti 			 "unknown key event: %#02x\n", event[2]);
23778bcac7bSAndi Shyti 		break;
23878bcac7bSAndi Shyti 	}
23978bcac7bSAndi Shyti 
24078bcac7bSAndi Shyti 	input_sync(sdata->input);
24178bcac7bSAndi Shyti }
24278bcac7bSAndi Shyti 
24378bcac7bSAndi Shyti static void stmfts_parse_events(struct stmfts_data *sdata)
24478bcac7bSAndi Shyti {
24578bcac7bSAndi Shyti 	int i;
24678bcac7bSAndi Shyti 
24778bcac7bSAndi Shyti 	for (i = 0; i < STMFTS_STACK_DEPTH; i++) {
24878bcac7bSAndi Shyti 		u8 *event = &sdata->data[i * STMFTS_EVENT_SIZE];
24978bcac7bSAndi Shyti 
25078bcac7bSAndi Shyti 		switch (event[0]) {
25178bcac7bSAndi Shyti 
25278bcac7bSAndi Shyti 		case STMFTS_EV_CONTROLLER_READY:
25378bcac7bSAndi Shyti 		case STMFTS_EV_SLEEP_OUT_CONTROLLER_READY:
25478bcac7bSAndi Shyti 		case STMFTS_EV_STATUS:
25578bcac7bSAndi Shyti 			complete(&sdata->cmd_done);
25678bcac7bSAndi Shyti 			/* fall through */
25778bcac7bSAndi Shyti 
25878bcac7bSAndi Shyti 		case STMFTS_EV_NO_EVENT:
25978bcac7bSAndi Shyti 		case STMFTS_EV_DEBUG:
26078bcac7bSAndi Shyti 			return;
26178bcac7bSAndi Shyti 		}
26278bcac7bSAndi Shyti 
26378bcac7bSAndi Shyti 		switch (event[0] & STMFTS_MASK_EVENT_ID) {
26478bcac7bSAndi Shyti 
26578bcac7bSAndi Shyti 		case STMFTS_EV_MULTI_TOUCH_ENTER:
26678bcac7bSAndi Shyti 		case STMFTS_EV_MULTI_TOUCH_MOTION:
26778bcac7bSAndi Shyti 			stmfts_report_contact_event(sdata, event);
26878bcac7bSAndi Shyti 			break;
26978bcac7bSAndi Shyti 
27078bcac7bSAndi Shyti 		case STMFTS_EV_MULTI_TOUCH_LEAVE:
27178bcac7bSAndi Shyti 			stmfts_report_contact_release(sdata, event);
27278bcac7bSAndi Shyti 			break;
27378bcac7bSAndi Shyti 
27478bcac7bSAndi Shyti 		case STMFTS_EV_HOVER_ENTER:
27578bcac7bSAndi Shyti 		case STMFTS_EV_HOVER_LEAVE:
27678bcac7bSAndi Shyti 		case STMFTS_EV_HOVER_MOTION:
27778bcac7bSAndi Shyti 			stmfts_report_hover_event(sdata, event);
27878bcac7bSAndi Shyti 			break;
27978bcac7bSAndi Shyti 
28078bcac7bSAndi Shyti 		case STMFTS_EV_KEY_STATUS:
28178bcac7bSAndi Shyti 			stmfts_report_key_event(sdata, event);
28278bcac7bSAndi Shyti 			break;
28378bcac7bSAndi Shyti 
28478bcac7bSAndi Shyti 		case STMFTS_EV_ERROR:
28578bcac7bSAndi Shyti 			dev_warn(&sdata->client->dev,
28678bcac7bSAndi Shyti 					"error code: 0x%x%x%x%x%x%x",
28778bcac7bSAndi Shyti 					event[6], event[5], event[4],
28878bcac7bSAndi Shyti 					event[3], event[2], event[1]);
28978bcac7bSAndi Shyti 			break;
29078bcac7bSAndi Shyti 
29178bcac7bSAndi Shyti 		default:
29278bcac7bSAndi Shyti 			dev_err(&sdata->client->dev,
29378bcac7bSAndi Shyti 				"unknown event %#02x\n", event[0]);
29478bcac7bSAndi Shyti 		}
29578bcac7bSAndi Shyti 	}
29678bcac7bSAndi Shyti }
29778bcac7bSAndi Shyti 
29878bcac7bSAndi Shyti static irqreturn_t stmfts_irq_handler(int irq, void *dev)
29978bcac7bSAndi Shyti {
30078bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev;
30178bcac7bSAndi Shyti 	int err;
30278bcac7bSAndi Shyti 
30378bcac7bSAndi Shyti 	mutex_lock(&sdata->mutex);
30478bcac7bSAndi Shyti 
30578bcac7bSAndi Shyti 	err = stmfts_read_events(sdata);
30678bcac7bSAndi Shyti 	if (unlikely(err))
30778bcac7bSAndi Shyti 		dev_err(&sdata->client->dev,
30878bcac7bSAndi Shyti 			"failed to read events: %d\n", err);
30978bcac7bSAndi Shyti 	else
31078bcac7bSAndi Shyti 		stmfts_parse_events(sdata);
31178bcac7bSAndi Shyti 
31278bcac7bSAndi Shyti 	mutex_unlock(&sdata->mutex);
31378bcac7bSAndi Shyti 	return IRQ_HANDLED;
31478bcac7bSAndi Shyti }
31578bcac7bSAndi Shyti 
31678bcac7bSAndi Shyti static int stmfts_command(struct stmfts_data *sdata, const u8 cmd)
31778bcac7bSAndi Shyti {
31878bcac7bSAndi Shyti 	int err;
31978bcac7bSAndi Shyti 
32078bcac7bSAndi Shyti 	reinit_completion(&sdata->cmd_done);
32178bcac7bSAndi Shyti 
32278bcac7bSAndi Shyti 	err = i2c_smbus_write_byte(sdata->client, cmd);
32378bcac7bSAndi Shyti 	if (err)
32478bcac7bSAndi Shyti 		return err;
32578bcac7bSAndi Shyti 
32678bcac7bSAndi Shyti 	if (!wait_for_completion_timeout(&sdata->cmd_done,
32778bcac7bSAndi Shyti 					 msecs_to_jiffies(1000)))
32878bcac7bSAndi Shyti 		return -ETIMEDOUT;
32978bcac7bSAndi Shyti 
33078bcac7bSAndi Shyti 	return 0;
33178bcac7bSAndi Shyti }
33278bcac7bSAndi Shyti 
33378bcac7bSAndi Shyti static int stmfts_input_open(struct input_dev *dev)
33478bcac7bSAndi Shyti {
33578bcac7bSAndi Shyti 	struct stmfts_data *sdata = input_get_drvdata(dev);
33678bcac7bSAndi Shyti 	int err;
33778bcac7bSAndi Shyti 
33878bcac7bSAndi Shyti 	err = pm_runtime_get_sync(&sdata->client->dev);
33978bcac7bSAndi Shyti 	if (err < 0)
34078bcac7bSAndi Shyti 		return err;
34178bcac7bSAndi Shyti 
34278bcac7bSAndi Shyti 	err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_ON);
34378bcac7bSAndi Shyti 	if (err)
34478bcac7bSAndi Shyti 		return err;
34578bcac7bSAndi Shyti 
34678bcac7bSAndi Shyti 	mutex_lock(&sdata->mutex);
34778bcac7bSAndi Shyti 	sdata->running = true;
34878bcac7bSAndi Shyti 
34978bcac7bSAndi Shyti 	if (sdata->hover_enabled) {
35078bcac7bSAndi Shyti 		err = i2c_smbus_write_byte(sdata->client,
35178bcac7bSAndi Shyti 					   STMFTS_SS_HOVER_SENSE_ON);
35278bcac7bSAndi Shyti 		if (err)
35378bcac7bSAndi Shyti 			dev_warn(&sdata->client->dev,
35478bcac7bSAndi Shyti 				 "failed to enable hover\n");
35578bcac7bSAndi Shyti 	}
35678bcac7bSAndi Shyti 	mutex_unlock(&sdata->mutex);
35778bcac7bSAndi Shyti 
35878bcac7bSAndi Shyti 	if (sdata->use_key) {
35978bcac7bSAndi Shyti 		err = i2c_smbus_write_byte(sdata->client,
36078bcac7bSAndi Shyti 					   STMFTS_MS_KEY_SENSE_ON);
36178bcac7bSAndi Shyti 		if (err)
36278bcac7bSAndi Shyti 			/* I can still use only the touch screen */
36378bcac7bSAndi Shyti 			dev_warn(&sdata->client->dev,
36478bcac7bSAndi Shyti 				 "failed to enable touchkey\n");
36578bcac7bSAndi Shyti 	}
36678bcac7bSAndi Shyti 
36778bcac7bSAndi Shyti 	return 0;
36878bcac7bSAndi Shyti }
36978bcac7bSAndi Shyti 
37078bcac7bSAndi Shyti static void stmfts_input_close(struct input_dev *dev)
37178bcac7bSAndi Shyti {
37278bcac7bSAndi Shyti 	struct stmfts_data *sdata = input_get_drvdata(dev);
37378bcac7bSAndi Shyti 	int err;
37478bcac7bSAndi Shyti 
37578bcac7bSAndi Shyti 	err = i2c_smbus_write_byte(sdata->client, STMFTS_MS_MT_SENSE_OFF);
37678bcac7bSAndi Shyti 	if (err)
37778bcac7bSAndi Shyti 		dev_warn(&sdata->client->dev,
37878bcac7bSAndi Shyti 			 "failed to disable touchscreen: %d\n", err);
37978bcac7bSAndi Shyti 
38078bcac7bSAndi Shyti 	mutex_lock(&sdata->mutex);
38178bcac7bSAndi Shyti 
38278bcac7bSAndi Shyti 	sdata->running = false;
38378bcac7bSAndi Shyti 
38478bcac7bSAndi Shyti 	if (sdata->hover_enabled) {
38578bcac7bSAndi Shyti 		err = i2c_smbus_write_byte(sdata->client,
38678bcac7bSAndi Shyti 					   STMFTS_SS_HOVER_SENSE_OFF);
38778bcac7bSAndi Shyti 		if (err)
38878bcac7bSAndi Shyti 			dev_warn(&sdata->client->dev,
38978bcac7bSAndi Shyti 				 "failed to disable hover: %d\n", err);
39078bcac7bSAndi Shyti 	}
39178bcac7bSAndi Shyti 	mutex_unlock(&sdata->mutex);
39278bcac7bSAndi Shyti 
39378bcac7bSAndi Shyti 	if (sdata->use_key) {
39478bcac7bSAndi Shyti 		err = i2c_smbus_write_byte(sdata->client,
39578bcac7bSAndi Shyti 					   STMFTS_MS_KEY_SENSE_OFF);
39678bcac7bSAndi Shyti 		if (err)
39778bcac7bSAndi Shyti 			dev_warn(&sdata->client->dev,
39878bcac7bSAndi Shyti 				 "failed to disable touchkey: %d\n", err);
39978bcac7bSAndi Shyti 	}
40078bcac7bSAndi Shyti 
40178bcac7bSAndi Shyti 	pm_runtime_put_sync(&sdata->client->dev);
40278bcac7bSAndi Shyti }
40378bcac7bSAndi Shyti 
40478bcac7bSAndi Shyti static ssize_t stmfts_sysfs_chip_id(struct device *dev,
40578bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
40678bcac7bSAndi Shyti {
40778bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
40878bcac7bSAndi Shyti 
40978bcac7bSAndi Shyti 	return sprintf(buf, "%#x\n", sdata->chip_id);
41078bcac7bSAndi Shyti }
41178bcac7bSAndi Shyti 
41278bcac7bSAndi Shyti static ssize_t stmfts_sysfs_chip_version(struct device *dev,
41378bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
41478bcac7bSAndi Shyti {
41578bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
41678bcac7bSAndi Shyti 
41778bcac7bSAndi Shyti 	return sprintf(buf, "%u\n", sdata->chip_ver);
41878bcac7bSAndi Shyti }
41978bcac7bSAndi Shyti 
42078bcac7bSAndi Shyti static ssize_t stmfts_sysfs_fw_ver(struct device *dev,
42178bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
42278bcac7bSAndi Shyti {
42378bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
42478bcac7bSAndi Shyti 
42578bcac7bSAndi Shyti 	return sprintf(buf, "%u\n", sdata->fw_ver);
42678bcac7bSAndi Shyti }
42778bcac7bSAndi Shyti 
42878bcac7bSAndi Shyti static ssize_t stmfts_sysfs_config_id(struct device *dev,
42978bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
43078bcac7bSAndi Shyti {
43178bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
43278bcac7bSAndi Shyti 
43378bcac7bSAndi Shyti 	return sprintf(buf, "%#x\n", sdata->config_id);
43478bcac7bSAndi Shyti }
43578bcac7bSAndi Shyti 
43678bcac7bSAndi Shyti static ssize_t stmfts_sysfs_config_version(struct device *dev,
43778bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
43878bcac7bSAndi Shyti {
43978bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
44078bcac7bSAndi Shyti 
44178bcac7bSAndi Shyti 	return sprintf(buf, "%u\n", sdata->config_ver);
44278bcac7bSAndi Shyti }
44378bcac7bSAndi Shyti 
44478bcac7bSAndi Shyti static ssize_t stmfts_sysfs_read_status(struct device *dev,
44578bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
44678bcac7bSAndi Shyti {
44778bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
44878bcac7bSAndi Shyti 	u8 status[4];
44978bcac7bSAndi Shyti 	int err;
45078bcac7bSAndi Shyti 
45178bcac7bSAndi Shyti 	err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_STATUS,
45278bcac7bSAndi Shyti 					    sizeof(status), status);
45378bcac7bSAndi Shyti 	if (err)
45478bcac7bSAndi Shyti 		return err;
45578bcac7bSAndi Shyti 
45678bcac7bSAndi Shyti 	return sprintf(buf, "%#02x\n", status[0]);
45778bcac7bSAndi Shyti }
45878bcac7bSAndi Shyti 
45978bcac7bSAndi Shyti static ssize_t stmfts_sysfs_hover_enable_read(struct device *dev,
46078bcac7bSAndi Shyti 				struct device_attribute *attr, char *buf)
46178bcac7bSAndi Shyti {
46278bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
46378bcac7bSAndi Shyti 
46478bcac7bSAndi Shyti 	return sprintf(buf, "%u\n", sdata->hover_enabled);
46578bcac7bSAndi Shyti }
46678bcac7bSAndi Shyti 
46778bcac7bSAndi Shyti static ssize_t stmfts_sysfs_hover_enable_write(struct device *dev,
46878bcac7bSAndi Shyti 				struct device_attribute *attr,
46978bcac7bSAndi Shyti 				const char *buf, size_t len)
47078bcac7bSAndi Shyti {
47178bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
47278bcac7bSAndi Shyti 	unsigned long value;
47378bcac7bSAndi Shyti 	int err = 0;
47478bcac7bSAndi Shyti 
47578bcac7bSAndi Shyti 	if (kstrtoul(buf, 0, &value))
47678bcac7bSAndi Shyti 		return -EINVAL;
47778bcac7bSAndi Shyti 
47878bcac7bSAndi Shyti 	mutex_lock(&sdata->mutex);
47978bcac7bSAndi Shyti 
48078bcac7bSAndi Shyti 	if (value & sdata->hover_enabled)
48178bcac7bSAndi Shyti 		goto out;
48278bcac7bSAndi Shyti 
48378bcac7bSAndi Shyti 	if (sdata->running)
48478bcac7bSAndi Shyti 		err = i2c_smbus_write_byte(sdata->client,
48578bcac7bSAndi Shyti 					   value ? STMFTS_SS_HOVER_SENSE_ON :
48678bcac7bSAndi Shyti 						   STMFTS_SS_HOVER_SENSE_OFF);
48778bcac7bSAndi Shyti 
48878bcac7bSAndi Shyti 	if (!err)
48978bcac7bSAndi Shyti 		sdata->hover_enabled = !!value;
49078bcac7bSAndi Shyti 
49178bcac7bSAndi Shyti out:
49278bcac7bSAndi Shyti 	mutex_unlock(&sdata->mutex);
49378bcac7bSAndi Shyti 
49478bcac7bSAndi Shyti 	return len;
49578bcac7bSAndi Shyti }
49678bcac7bSAndi Shyti 
49778bcac7bSAndi Shyti static DEVICE_ATTR(chip_id, 0444, stmfts_sysfs_chip_id, NULL);
49878bcac7bSAndi Shyti static DEVICE_ATTR(chip_version, 0444, stmfts_sysfs_chip_version, NULL);
49978bcac7bSAndi Shyti static DEVICE_ATTR(fw_ver, 0444, stmfts_sysfs_fw_ver, NULL);
50078bcac7bSAndi Shyti static DEVICE_ATTR(config_id, 0444, stmfts_sysfs_config_id, NULL);
50178bcac7bSAndi Shyti static DEVICE_ATTR(config_version, 0444, stmfts_sysfs_config_version, NULL);
50278bcac7bSAndi Shyti static DEVICE_ATTR(status, 0444, stmfts_sysfs_read_status, NULL);
50378bcac7bSAndi Shyti static DEVICE_ATTR(hover_enable, 0644, stmfts_sysfs_hover_enable_read,
50478bcac7bSAndi Shyti 					stmfts_sysfs_hover_enable_write);
50578bcac7bSAndi Shyti 
50678bcac7bSAndi Shyti static struct attribute *stmfts_sysfs_attrs[] = {
50778bcac7bSAndi Shyti 	&dev_attr_chip_id.attr,
50878bcac7bSAndi Shyti 	&dev_attr_chip_version.attr,
50978bcac7bSAndi Shyti 	&dev_attr_fw_ver.attr,
51078bcac7bSAndi Shyti 	&dev_attr_config_id.attr,
51178bcac7bSAndi Shyti 	&dev_attr_config_version.attr,
51278bcac7bSAndi Shyti 	&dev_attr_status.attr,
51378bcac7bSAndi Shyti 	&dev_attr_hover_enable.attr,
51478bcac7bSAndi Shyti 	NULL
51578bcac7bSAndi Shyti };
51678bcac7bSAndi Shyti 
51778bcac7bSAndi Shyti static struct attribute_group stmfts_attribute_group = {
51878bcac7bSAndi Shyti 	.attrs = stmfts_sysfs_attrs
51978bcac7bSAndi Shyti };
52078bcac7bSAndi Shyti 
52178bcac7bSAndi Shyti static int stmfts_power_on(struct stmfts_data *sdata)
52278bcac7bSAndi Shyti {
52378bcac7bSAndi Shyti 	int err;
52478bcac7bSAndi Shyti 	u8 reg[8];
52578bcac7bSAndi Shyti 
52678bcac7bSAndi Shyti 	err = regulator_bulk_enable(ARRAY_SIZE(sdata->regulators),
52778bcac7bSAndi Shyti 				    sdata->regulators);
52878bcac7bSAndi Shyti 	if (err)
52978bcac7bSAndi Shyti 		return err;
53078bcac7bSAndi Shyti 
53178bcac7bSAndi Shyti 	/*
53278bcac7bSAndi Shyti 	 * The datasheet does not specify the power on time, but considering
53378bcac7bSAndi Shyti 	 * that the reset time is < 10ms, I sleep 20ms to be sure
53478bcac7bSAndi Shyti 	 */
53578bcac7bSAndi Shyti 	msleep(20);
53678bcac7bSAndi Shyti 
53778bcac7bSAndi Shyti 	err = i2c_smbus_read_i2c_block_data(sdata->client, STMFTS_READ_INFO,
53878bcac7bSAndi Shyti 					    sizeof(reg), reg);
53978bcac7bSAndi Shyti 	if (err < 0)
54078bcac7bSAndi Shyti 		return err;
54178bcac7bSAndi Shyti 	if (err != sizeof(reg))
54278bcac7bSAndi Shyti 		return -EIO;
54378bcac7bSAndi Shyti 
54478bcac7bSAndi Shyti 	sdata->chip_id = be16_to_cpup((__be16 *)&reg[6]);
54578bcac7bSAndi Shyti 	sdata->chip_ver = reg[0];
54678bcac7bSAndi Shyti 	sdata->fw_ver = be16_to_cpup((__be16 *)&reg[2]);
54778bcac7bSAndi Shyti 	sdata->config_id = reg[4];
54878bcac7bSAndi Shyti 	sdata->config_ver = reg[5];
54978bcac7bSAndi Shyti 
55078bcac7bSAndi Shyti 	enable_irq(sdata->client->irq);
55178bcac7bSAndi Shyti 
55278bcac7bSAndi Shyti 	msleep(50);
55378bcac7bSAndi Shyti 
55478bcac7bSAndi Shyti 	err = stmfts_command(sdata, STMFTS_SYSTEM_RESET);
55578bcac7bSAndi Shyti 	if (err)
55678bcac7bSAndi Shyti 		return err;
55778bcac7bSAndi Shyti 
55878bcac7bSAndi Shyti 	err = stmfts_command(sdata, STMFTS_SLEEP_OUT);
55978bcac7bSAndi Shyti 	if (err)
56078bcac7bSAndi Shyti 		return err;
56178bcac7bSAndi Shyti 
56278bcac7bSAndi Shyti 	/* optional tuning */
56378bcac7bSAndi Shyti 	err = stmfts_command(sdata, STMFTS_MS_CX_TUNING);
56478bcac7bSAndi Shyti 	if (err)
56578bcac7bSAndi Shyti 		dev_warn(&sdata->client->dev,
56678bcac7bSAndi Shyti 			 "failed to perform mutual auto tune: %d\n", err);
56778bcac7bSAndi Shyti 
56878bcac7bSAndi Shyti 	/* optional tuning */
56978bcac7bSAndi Shyti 	err = stmfts_command(sdata, STMFTS_SS_CX_TUNING);
57078bcac7bSAndi Shyti 	if (err)
57178bcac7bSAndi Shyti 		dev_warn(&sdata->client->dev,
57278bcac7bSAndi Shyti 			 "failed to perform self auto tune: %d\n", err);
57378bcac7bSAndi Shyti 
57478bcac7bSAndi Shyti 	err = stmfts_command(sdata, STMFTS_FULL_FORCE_CALIBRATION);
57578bcac7bSAndi Shyti 	if (err)
57678bcac7bSAndi Shyti 		return err;
57778bcac7bSAndi Shyti 
57878bcac7bSAndi Shyti 	/*
57978bcac7bSAndi Shyti 	 * At this point no one is using the touchscreen
58078bcac7bSAndi Shyti 	 * and I don't really care about the return value
58178bcac7bSAndi Shyti 	 */
58278bcac7bSAndi Shyti 	(void) i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
58378bcac7bSAndi Shyti 
58478bcac7bSAndi Shyti 	return 0;
58578bcac7bSAndi Shyti }
58678bcac7bSAndi Shyti 
58778bcac7bSAndi Shyti static void stmfts_power_off(void *data)
58878bcac7bSAndi Shyti {
58978bcac7bSAndi Shyti 	struct stmfts_data *sdata = data;
59078bcac7bSAndi Shyti 
59178bcac7bSAndi Shyti 	disable_irq(sdata->client->irq);
59278bcac7bSAndi Shyti 	regulator_bulk_disable(ARRAY_SIZE(sdata->regulators),
59378bcac7bSAndi Shyti 						sdata->regulators);
59478bcac7bSAndi Shyti }
59578bcac7bSAndi Shyti 
59678bcac7bSAndi Shyti /* This function is void because I don't want to prevent using the touch key
59778bcac7bSAndi Shyti  * only because the LEDs don't get registered
59878bcac7bSAndi Shyti  */
59978bcac7bSAndi Shyti static int stmfts_enable_led(struct stmfts_data *sdata)
60078bcac7bSAndi Shyti {
60178bcac7bSAndi Shyti 	int err;
60278bcac7bSAndi Shyti 
60378bcac7bSAndi Shyti 	/* get the regulator for powering the leds on */
60478bcac7bSAndi Shyti 	sdata->ledvdd = devm_regulator_get(&sdata->client->dev, "ledvdd");
60578bcac7bSAndi Shyti 	if (IS_ERR(sdata->ledvdd))
60678bcac7bSAndi Shyti 		return PTR_ERR(sdata->ledvdd);
60778bcac7bSAndi Shyti 
60878bcac7bSAndi Shyti 	sdata->led_cdev.name = STMFTS_DEV_NAME;
60978bcac7bSAndi Shyti 	sdata->led_cdev.max_brightness = LED_ON;
61078bcac7bSAndi Shyti 	sdata->led_cdev.brightness = LED_OFF;
61178bcac7bSAndi Shyti 	sdata->led_cdev.brightness_set = stmfts_brightness_set;
61278bcac7bSAndi Shyti 	sdata->led_cdev.brightness_get = stmfts_brightness_get;
61378bcac7bSAndi Shyti 
61478bcac7bSAndi Shyti 	err = devm_led_classdev_register(&sdata->client->dev, &sdata->led_cdev);
61578bcac7bSAndi Shyti 	if (err) {
61678bcac7bSAndi Shyti 		devm_regulator_put(sdata->ledvdd);
61778bcac7bSAndi Shyti 		return err;
61878bcac7bSAndi Shyti 	}
61978bcac7bSAndi Shyti 
62078bcac7bSAndi Shyti 	return 0;
62178bcac7bSAndi Shyti }
62278bcac7bSAndi Shyti 
62378bcac7bSAndi Shyti static int stmfts_probe(struct i2c_client *client,
62478bcac7bSAndi Shyti 			const struct i2c_device_id *id)
62578bcac7bSAndi Shyti {
62678bcac7bSAndi Shyti 	int err;
62778bcac7bSAndi Shyti 	struct stmfts_data *sdata;
62878bcac7bSAndi Shyti 
62978bcac7bSAndi Shyti 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
63078bcac7bSAndi Shyti 						I2C_FUNC_SMBUS_BYTE_DATA |
63178bcac7bSAndi Shyti 						I2C_FUNC_SMBUS_I2C_BLOCK))
63278bcac7bSAndi Shyti 		return -ENODEV;
63378bcac7bSAndi Shyti 
63478bcac7bSAndi Shyti 	sdata = devm_kzalloc(&client->dev, sizeof(*sdata), GFP_KERNEL);
63578bcac7bSAndi Shyti 	if (!sdata)
63678bcac7bSAndi Shyti 		return -ENOMEM;
63778bcac7bSAndi Shyti 
63878bcac7bSAndi Shyti 	i2c_set_clientdata(client, sdata);
63978bcac7bSAndi Shyti 
64078bcac7bSAndi Shyti 	sdata->client = client;
64178bcac7bSAndi Shyti 	mutex_init(&sdata->mutex);
64278bcac7bSAndi Shyti 	init_completion(&sdata->cmd_done);
64378bcac7bSAndi Shyti 
64478bcac7bSAndi Shyti 	sdata->regulators[STMFTS_REGULATOR_VDD].supply = "vdd";
64578bcac7bSAndi Shyti 	sdata->regulators[STMFTS_REGULATOR_AVDD].supply = "avdd";
64678bcac7bSAndi Shyti 	err = devm_regulator_bulk_get(&client->dev,
64778bcac7bSAndi Shyti 				      ARRAY_SIZE(sdata->regulators),
64878bcac7bSAndi Shyti 				      sdata->regulators);
64978bcac7bSAndi Shyti 	if (err)
65078bcac7bSAndi Shyti 		return err;
65178bcac7bSAndi Shyti 
65278bcac7bSAndi Shyti 	sdata->input = devm_input_allocate_device(&client->dev);
65378bcac7bSAndi Shyti 	if (!sdata->input)
65478bcac7bSAndi Shyti 		return -ENOMEM;
65578bcac7bSAndi Shyti 
65678bcac7bSAndi Shyti 	sdata->input->name = STMFTS_DEV_NAME;
65778bcac7bSAndi Shyti 	sdata->input->id.bustype = BUS_I2C;
65878bcac7bSAndi Shyti 	sdata->input->open = stmfts_input_open;
65978bcac7bSAndi Shyti 	sdata->input->close = stmfts_input_close;
66078bcac7bSAndi Shyti 
661c9bfb2f0SChanwoo Choi 	input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_X);
662c9bfb2f0SChanwoo Choi 	input_set_capability(sdata->input, EV_ABS, ABS_MT_POSITION_Y);
66378bcac7bSAndi Shyti 	touchscreen_parse_properties(sdata->input, true, &sdata->prop);
66478bcac7bSAndi Shyti 
66578bcac7bSAndi Shyti 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
66678bcac7bSAndi Shyti 	input_set_abs_params(sdata->input, ABS_MT_TOUCH_MINOR, 0, 255, 0, 0);
66778bcac7bSAndi Shyti 	input_set_abs_params(sdata->input, ABS_MT_ORIENTATION, 0, 255, 0, 0);
66878bcac7bSAndi Shyti 	input_set_abs_params(sdata->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
66978bcac7bSAndi Shyti 	input_set_abs_params(sdata->input, ABS_DISTANCE, 0, 255, 0, 0);
67078bcac7bSAndi Shyti 
67178bcac7bSAndi Shyti 	sdata->use_key = device_property_read_bool(&client->dev,
67278bcac7bSAndi Shyti 						   "touch-key-connected");
67378bcac7bSAndi Shyti 	if (sdata->use_key) {
67478bcac7bSAndi Shyti 		input_set_capability(sdata->input, EV_KEY, KEY_MENU);
67578bcac7bSAndi Shyti 		input_set_capability(sdata->input, EV_KEY, KEY_BACK);
67678bcac7bSAndi Shyti 	}
67778bcac7bSAndi Shyti 
67878bcac7bSAndi Shyti 	err = input_mt_init_slots(sdata->input,
67978bcac7bSAndi Shyti 				  STMFTS_MAX_FINGERS, INPUT_MT_DIRECT);
68078bcac7bSAndi Shyti 	if (err)
68178bcac7bSAndi Shyti 		return err;
68278bcac7bSAndi Shyti 
68378bcac7bSAndi Shyti 	input_set_drvdata(sdata->input, sdata);
68478bcac7bSAndi Shyti 
685cba04cdfSAndi Shyti 	/*
686cba04cdfSAndi Shyti 	 * stmfts_power_on expects interrupt to be disabled, but
687cba04cdfSAndi Shyti 	 * at this point the device is still off and I do not trust
688cba04cdfSAndi Shyti 	 * the status of the irq line that can generate some spurious
689cba04cdfSAndi Shyti 	 * interrupts. To be on the safe side it's better to not enable
690cba04cdfSAndi Shyti 	 * the interrupts during their request.
691cba04cdfSAndi Shyti 	 */
692cba04cdfSAndi Shyti 	irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
69378bcac7bSAndi Shyti 	err = devm_request_threaded_irq(&client->dev, client->irq,
69478bcac7bSAndi Shyti 					NULL, stmfts_irq_handler,
69578bcac7bSAndi Shyti 					IRQF_ONESHOT,
69678bcac7bSAndi Shyti 					"stmfts_irq", sdata);
69778bcac7bSAndi Shyti 	if (err)
69878bcac7bSAndi Shyti 		return err;
69978bcac7bSAndi Shyti 
70078bcac7bSAndi Shyti 	dev_dbg(&client->dev, "initializing ST-Microelectronics FTS...\n");
70178bcac7bSAndi Shyti 
70278bcac7bSAndi Shyti 	err = stmfts_power_on(sdata);
70378bcac7bSAndi Shyti 	if (err)
70478bcac7bSAndi Shyti 		return err;
70578bcac7bSAndi Shyti 
70678bcac7bSAndi Shyti 	err = devm_add_action_or_reset(&client->dev, stmfts_power_off, sdata);
70778bcac7bSAndi Shyti 	if (err)
70878bcac7bSAndi Shyti 		return err;
70978bcac7bSAndi Shyti 
71078bcac7bSAndi Shyti 	err = input_register_device(sdata->input);
71178bcac7bSAndi Shyti 	if (err)
71278bcac7bSAndi Shyti 		return err;
71378bcac7bSAndi Shyti 
71478bcac7bSAndi Shyti 	if (sdata->use_key) {
71578bcac7bSAndi Shyti 		err = stmfts_enable_led(sdata);
71678bcac7bSAndi Shyti 		if (err) {
71778bcac7bSAndi Shyti 			/*
71878bcac7bSAndi Shyti 			 * Even if the LEDs have failed to be initialized and
71978bcac7bSAndi Shyti 			 * used in the driver, I can still use the device even
72078bcac7bSAndi Shyti 			 * without LEDs. The ledvdd regulator pointer will be
72178bcac7bSAndi Shyti 			 * used as a flag.
72278bcac7bSAndi Shyti 			 */
72378bcac7bSAndi Shyti 			dev_warn(&client->dev, "unable to use touchkey leds\n");
72478bcac7bSAndi Shyti 			sdata->ledvdd = NULL;
72578bcac7bSAndi Shyti 		}
72678bcac7bSAndi Shyti 	}
72778bcac7bSAndi Shyti 
728c2690bd6SAndi Shyti 	err = devm_device_add_group(&client->dev, &stmfts_attribute_group);
72978bcac7bSAndi Shyti 	if (err)
73078bcac7bSAndi Shyti 		return err;
73178bcac7bSAndi Shyti 
73278bcac7bSAndi Shyti 	pm_runtime_enable(&client->dev);
7331d960003SMarek Szyprowski 	device_enable_async_suspend(&client->dev);
73478bcac7bSAndi Shyti 
73578bcac7bSAndi Shyti 	return 0;
73678bcac7bSAndi Shyti }
73778bcac7bSAndi Shyti 
73878bcac7bSAndi Shyti static int stmfts_remove(struct i2c_client *client)
73978bcac7bSAndi Shyti {
74078bcac7bSAndi Shyti 	pm_runtime_disable(&client->dev);
74178bcac7bSAndi Shyti 
74278bcac7bSAndi Shyti 	return 0;
74378bcac7bSAndi Shyti }
74478bcac7bSAndi Shyti 
745d4c9354bSArnd Bergmann static int __maybe_unused stmfts_runtime_suspend(struct device *dev)
74678bcac7bSAndi Shyti {
74778bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
74878bcac7bSAndi Shyti 	int ret;
74978bcac7bSAndi Shyti 
75078bcac7bSAndi Shyti 	ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_IN);
75178bcac7bSAndi Shyti 	if (ret)
75278bcac7bSAndi Shyti 		dev_warn(dev, "failed to suspend device: %d\n", ret);
75378bcac7bSAndi Shyti 
75478bcac7bSAndi Shyti 	return ret;
75578bcac7bSAndi Shyti }
75678bcac7bSAndi Shyti 
757d4c9354bSArnd Bergmann static int __maybe_unused stmfts_runtime_resume(struct device *dev)
75878bcac7bSAndi Shyti {
75978bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
76078bcac7bSAndi Shyti 	int ret;
76178bcac7bSAndi Shyti 
76278bcac7bSAndi Shyti 	ret = i2c_smbus_write_byte(sdata->client, STMFTS_SLEEP_OUT);
76378bcac7bSAndi Shyti 	if (ret)
76478bcac7bSAndi Shyti 		dev_err(dev, "failed to resume device: %d\n", ret);
76578bcac7bSAndi Shyti 
76678bcac7bSAndi Shyti 	return ret;
76778bcac7bSAndi Shyti }
76878bcac7bSAndi Shyti 
76978bcac7bSAndi Shyti static int __maybe_unused stmfts_suspend(struct device *dev)
77078bcac7bSAndi Shyti {
77178bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
77278bcac7bSAndi Shyti 
77378bcac7bSAndi Shyti 	stmfts_power_off(sdata);
77478bcac7bSAndi Shyti 
77578bcac7bSAndi Shyti 	return 0;
77678bcac7bSAndi Shyti }
77778bcac7bSAndi Shyti 
77878bcac7bSAndi Shyti static int __maybe_unused stmfts_resume(struct device *dev)
77978bcac7bSAndi Shyti {
78078bcac7bSAndi Shyti 	struct stmfts_data *sdata = dev_get_drvdata(dev);
78178bcac7bSAndi Shyti 
78278bcac7bSAndi Shyti 	return stmfts_power_on(sdata);
78378bcac7bSAndi Shyti }
78478bcac7bSAndi Shyti 
78578bcac7bSAndi Shyti static const struct dev_pm_ops stmfts_pm_ops = {
78678bcac7bSAndi Shyti 	SET_SYSTEM_SLEEP_PM_OPS(stmfts_suspend, stmfts_resume)
78778bcac7bSAndi Shyti 	SET_RUNTIME_PM_OPS(stmfts_runtime_suspend, stmfts_runtime_resume, NULL)
78878bcac7bSAndi Shyti };
78978bcac7bSAndi Shyti 
79078bcac7bSAndi Shyti #ifdef CONFIG_OF
79178bcac7bSAndi Shyti static const struct of_device_id stmfts_of_match[] = {
79278bcac7bSAndi Shyti 	{ .compatible = "st,stmfts", },
79378bcac7bSAndi Shyti 	{ },
79478bcac7bSAndi Shyti };
79578bcac7bSAndi Shyti MODULE_DEVICE_TABLE(of, stmfts_of_match);
79678bcac7bSAndi Shyti #endif
79778bcac7bSAndi Shyti 
79878bcac7bSAndi Shyti static const struct i2c_device_id stmfts_id[] = {
79978bcac7bSAndi Shyti 	{ "stmfts", 0 },
80078bcac7bSAndi Shyti 	{ },
80178bcac7bSAndi Shyti };
80278bcac7bSAndi Shyti MODULE_DEVICE_TABLE(i2c, stmfts_id);
80378bcac7bSAndi Shyti 
80478bcac7bSAndi Shyti static struct i2c_driver stmfts_driver = {
80578bcac7bSAndi Shyti 	.driver = {
80678bcac7bSAndi Shyti 		.name = STMFTS_DEV_NAME,
80778bcac7bSAndi Shyti 		.of_match_table = of_match_ptr(stmfts_of_match),
80878bcac7bSAndi Shyti 		.pm = &stmfts_pm_ops,
8091d960003SMarek Szyprowski 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
81078bcac7bSAndi Shyti 	},
81178bcac7bSAndi Shyti 	.probe = stmfts_probe,
81278bcac7bSAndi Shyti 	.remove = stmfts_remove,
81378bcac7bSAndi Shyti 	.id_table = stmfts_id,
81478bcac7bSAndi Shyti };
81578bcac7bSAndi Shyti 
81678bcac7bSAndi Shyti module_i2c_driver(stmfts_driver);
81778bcac7bSAndi Shyti 
81878bcac7bSAndi Shyti MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
81978bcac7bSAndi Shyti MODULE_DESCRIPTION("STMicroelectronics FTS Touch Screen");
82078bcac7bSAndi Shyti MODULE_LICENSE("GPL v2");
821