xref: /linux/drivers/input/touchscreen/zinitix.c (revision 5a94296bc02ac616336da7b5332b86d2ca8827f0)
126822652SMichael Srba // SPDX-License-Identifier: GPL-2.0-only
226822652SMichael Srba 
326822652SMichael Srba #include <linux/delay.h>
426822652SMichael Srba #include <linux/i2c.h>
526822652SMichael Srba #include <linux/input.h>
626822652SMichael Srba #include <linux/input/mt.h>
726822652SMichael Srba #include <linux/input/touchscreen.h>
826822652SMichael Srba #include <linux/interrupt.h>
926822652SMichael Srba #include <linux/irq.h>
1026822652SMichael Srba #include <linux/kernel.h>
1126822652SMichael Srba #include <linux/module.h>
1226822652SMichael Srba #include <linux/of.h>
1326822652SMichael Srba #include <linux/regulator/consumer.h>
1426822652SMichael Srba #include <linux/slab.h>
1526822652SMichael Srba 
1626822652SMichael Srba /* Register Map */
1726822652SMichael Srba 
1826822652SMichael Srba #define BT541_SWRESET_CMD			0x0000
1926822652SMichael Srba #define BT541_WAKEUP_CMD			0x0001
2026822652SMichael Srba 
2126822652SMichael Srba #define BT541_IDLE_CMD				0x0004
2226822652SMichael Srba #define BT541_SLEEP_CMD				0x0005
2326822652SMichael Srba 
2426822652SMichael Srba #define BT541_CLEAR_INT_STATUS_CMD		0x0003
2526822652SMichael Srba #define BT541_CALIBRATE_CMD			0x0006
2626822652SMichael Srba #define BT541_SAVE_STATUS_CMD			0x0007
2726822652SMichael Srba #define BT541_SAVE_CALIBRATION_CMD		0x0008
2826822652SMichael Srba #define BT541_RECALL_FACTORY_CMD		0x000f
2926822652SMichael Srba 
3026822652SMichael Srba #define BT541_THRESHOLD				0x0020
3126822652SMichael Srba 
3226822652SMichael Srba #define BT541_LARGE_PALM_REJECT_AREA_TH		0x003F
3326822652SMichael Srba 
3426822652SMichael Srba #define BT541_DEBUG_REG				0x0115 /* 0~7 */
3526822652SMichael Srba 
3626822652SMichael Srba #define BT541_TOUCH_MODE			0x0010
3726822652SMichael Srba #define BT541_CHIP_REVISION			0x0011
3826822652SMichael Srba #define BT541_FIRMWARE_VERSION			0x0012
3926822652SMichael Srba 
4026822652SMichael Srba #define ZINITIX_USB_DETECT			0x116
4126822652SMichael Srba 
4226822652SMichael Srba #define BT541_MINOR_FW_VERSION			0x0121
4326822652SMichael Srba 
4426822652SMichael Srba #define BT541_VENDOR_ID				0x001C
4526822652SMichael Srba #define BT541_HW_ID				0x0014
4626822652SMichael Srba 
4726822652SMichael Srba #define BT541_DATA_VERSION_REG			0x0013
4826822652SMichael Srba #define BT541_SUPPORTED_FINGER_NUM		0x0015
4926822652SMichael Srba #define BT541_EEPROM_INFO			0x0018
5026822652SMichael Srba #define BT541_INITIAL_TOUCH_MODE		0x0019
5126822652SMichael Srba 
5226822652SMichael Srba #define BT541_TOTAL_NUMBER_OF_X			0x0060
5326822652SMichael Srba #define BT541_TOTAL_NUMBER_OF_Y			0x0061
5426822652SMichael Srba 
5526822652SMichael Srba #define BT541_DELAY_RAW_FOR_HOST		0x007f
5626822652SMichael Srba 
5726822652SMichael Srba #define BT541_BUTTON_SUPPORTED_NUM		0x00B0
5826822652SMichael Srba #define BT541_BUTTON_SENSITIVITY		0x00B2
5926822652SMichael Srba #define BT541_DUMMY_BUTTON_SENSITIVITY		0X00C8
6026822652SMichael Srba 
6126822652SMichael Srba #define BT541_X_RESOLUTION			0x00C0
6226822652SMichael Srba #define BT541_Y_RESOLUTION			0x00C1
6326822652SMichael Srba 
6426822652SMichael Srba #define BT541_POINT_STATUS_REG			0x0080
6526822652SMichael Srba #define BT541_ICON_STATUS_REG			0x00AA
6626822652SMichael Srba 
6726822652SMichael Srba #define BT541_POINT_COORD_REG			(BT541_POINT_STATUS_REG + 2)
6826822652SMichael Srba 
6926822652SMichael Srba #define BT541_AFE_FREQUENCY			0x0100
7026822652SMichael Srba #define BT541_DND_N_COUNT			0x0122
7126822652SMichael Srba #define BT541_DND_U_COUNT			0x0135
7226822652SMichael Srba 
7326822652SMichael Srba #define BT541_RAWDATA_REG			0x0200
7426822652SMichael Srba 
7526822652SMichael Srba #define BT541_EEPROM_INFO_REG			0x0018
7626822652SMichael Srba 
7726822652SMichael Srba #define BT541_INT_ENABLE_FLAG			0x00f0
7826822652SMichael Srba #define BT541_PERIODICAL_INTERRUPT_INTERVAL	0x00f1
7926822652SMichael Srba 
8026822652SMichael Srba #define BT541_BTN_WIDTH				0x016d
8126822652SMichael Srba 
8226822652SMichael Srba #define BT541_CHECKSUM_RESULT			0x012c
8326822652SMichael Srba 
8426822652SMichael Srba #define BT541_INIT_FLASH			0x01d0
8526822652SMichael Srba #define BT541_WRITE_FLASH			0x01d1
8626822652SMichael Srba #define BT541_READ_FLASH			0x01d2
8726822652SMichael Srba 
8826822652SMichael Srba #define ZINITIX_INTERNAL_FLAG_02		0x011e
8926822652SMichael Srba #define ZINITIX_INTERNAL_FLAG_03		0x011f
9026822652SMichael Srba 
9126822652SMichael Srba #define ZINITIX_I2C_CHECKSUM_WCNT		0x016a
9226822652SMichael Srba #define ZINITIX_I2C_CHECKSUM_RESULT		0x016c
9326822652SMichael Srba 
9426822652SMichael Srba /* Interrupt & status register flags */
9526822652SMichael Srba 
9626822652SMichael Srba #define BIT_PT_CNT_CHANGE			BIT(0)
9726822652SMichael Srba #define BIT_DOWN				BIT(1)
9826822652SMichael Srba #define BIT_MOVE				BIT(2)
9926822652SMichael Srba #define BIT_UP					BIT(3)
10026822652SMichael Srba #define BIT_PALM				BIT(4)
10126822652SMichael Srba #define BIT_PALM_REJECT				BIT(5)
10226822652SMichael Srba #define BIT_RESERVED_0				BIT(6)
10326822652SMichael Srba #define BIT_RESERVED_1				BIT(7)
10426822652SMichael Srba #define BIT_WEIGHT_CHANGE			BIT(8)
10526822652SMichael Srba #define BIT_PT_NO_CHANGE			BIT(9)
10626822652SMichael Srba #define BIT_REJECT				BIT(10)
10726822652SMichael Srba #define BIT_PT_EXIST				BIT(11)
10826822652SMichael Srba #define BIT_RESERVED_2				BIT(12)
10926822652SMichael Srba #define BIT_ERROR				BIT(13)
11026822652SMichael Srba #define BIT_DEBUG				BIT(14)
11126822652SMichael Srba #define BIT_ICON_EVENT				BIT(15)
11226822652SMichael Srba 
11326822652SMichael Srba #define SUB_BIT_EXIST				BIT(0)
11426822652SMichael Srba #define SUB_BIT_DOWN				BIT(1)
11526822652SMichael Srba #define SUB_BIT_MOVE				BIT(2)
11626822652SMichael Srba #define SUB_BIT_UP				BIT(3)
11726822652SMichael Srba #define SUB_BIT_UPDATE				BIT(4)
11826822652SMichael Srba #define SUB_BIT_WAIT				BIT(5)
11926822652SMichael Srba 
12026822652SMichael Srba #define DEFAULT_TOUCH_POINT_MODE		2
12126822652SMichael Srba #define MAX_SUPPORTED_FINGER_NUM		5
12226822652SMichael Srba 
12326822652SMichael Srba #define CHIP_ON_DELAY				15 // ms
12426822652SMichael Srba #define FIRMWARE_ON_DELAY			40 // ms
12526822652SMichael Srba 
12626822652SMichael Srba struct point_coord {
12726822652SMichael Srba 	__le16	x;
12826822652SMichael Srba 	__le16	y;
12926822652SMichael Srba 	u8	width;
13026822652SMichael Srba 	u8	sub_status;
13126822652SMichael Srba 	// currently unused, but needed as padding:
13226822652SMichael Srba 	u8	minor_width;
13326822652SMichael Srba 	u8	angle;
13426822652SMichael Srba };
13526822652SMichael Srba 
13626822652SMichael Srba struct touch_event {
13726822652SMichael Srba 	__le16	status;
13826822652SMichael Srba 	u8	finger_cnt;
13926822652SMichael Srba 	u8	time_stamp;
14026822652SMichael Srba 	struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM];
14126822652SMichael Srba };
14226822652SMichael Srba 
14326822652SMichael Srba struct bt541_ts_data {
14426822652SMichael Srba 	struct i2c_client *client;
14526822652SMichael Srba 	struct input_dev *input_dev;
14626822652SMichael Srba 	struct touchscreen_properties prop;
14726822652SMichael Srba 	struct regulator_bulk_data supplies[2];
14826822652SMichael Srba 	u32 zinitix_mode;
14926822652SMichael Srba };
15026822652SMichael Srba 
15126822652SMichael Srba static int zinitix_read_data(struct i2c_client *client,
15226822652SMichael Srba 			     u16 reg, void *values, size_t length)
15326822652SMichael Srba {
15426822652SMichael Srba 	__le16 reg_le = cpu_to_le16(reg);
15526822652SMichael Srba 	int ret;
15626822652SMichael Srba 
15726822652SMichael Srba 	/* A single i2c_transfer() transaction does not work here. */
15826822652SMichael Srba 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
15926822652SMichael Srba 	if (ret != sizeof(reg_le))
16026822652SMichael Srba 		return ret < 0 ? ret : -EIO;
16126822652SMichael Srba 
16226822652SMichael Srba 	ret = i2c_master_recv(client, (u8 *)values, length);
16326822652SMichael Srba 	if (ret != length)
164a374c19fSYang Li 		return ret < 0 ? ret : -EIO;
16526822652SMichael Srba 
16626822652SMichael Srba 	return 0;
16726822652SMichael Srba }
16826822652SMichael Srba 
16926822652SMichael Srba static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value)
17026822652SMichael Srba {
17126822652SMichael Srba 	__le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)};
17226822652SMichael Srba 	int ret;
17326822652SMichael Srba 
17426822652SMichael Srba 	ret = i2c_master_send(client, (u8 *)packet, sizeof(packet));
17526822652SMichael Srba 	if (ret != sizeof(packet))
17626822652SMichael Srba 		return ret < 0 ? ret : -EIO;
17726822652SMichael Srba 
17826822652SMichael Srba 	return 0;
17926822652SMichael Srba }
18026822652SMichael Srba 
18126822652SMichael Srba static int zinitix_write_cmd(struct i2c_client *client, u16 reg)
18226822652SMichael Srba {
18326822652SMichael Srba 	__le16 reg_le = cpu_to_le16(reg);
18426822652SMichael Srba 	int ret;
18526822652SMichael Srba 
18626822652SMichael Srba 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
18726822652SMichael Srba 	if (ret != sizeof(reg_le))
18826822652SMichael Srba 		return ret < 0 ? ret : -EIO;
18926822652SMichael Srba 
19026822652SMichael Srba 	return 0;
19126822652SMichael Srba }
19226822652SMichael Srba 
193836f308cSDmitry Torokhov static int zinitix_init_touch(struct bt541_ts_data *bt541)
19426822652SMichael Srba {
19526822652SMichael Srba 	struct i2c_client *client = bt541->client;
19626822652SMichael Srba 	int i;
19726822652SMichael Srba 	int error;
19826822652SMichael Srba 
19926822652SMichael Srba 	error = zinitix_write_cmd(client, BT541_SWRESET_CMD);
20026822652SMichael Srba 	if (error) {
20126822652SMichael Srba 		dev_err(&client->dev, "Failed to write reset command\n");
20226822652SMichael Srba 		return error;
20326822652SMichael Srba 	}
20426822652SMichael Srba 
20526822652SMichael Srba 	error = zinitix_write_u16(client, BT541_INT_ENABLE_FLAG, 0x0);
20626822652SMichael Srba 	if (error) {
20726822652SMichael Srba 		dev_err(&client->dev,
20826822652SMichael Srba 			"Failed to reset interrupt enable flag\n");
20926822652SMichael Srba 		return error;
21026822652SMichael Srba 	}
21126822652SMichael Srba 
21226822652SMichael Srba 	/* initialize */
21326822652SMichael Srba 	error = zinitix_write_u16(client, BT541_X_RESOLUTION,
21426822652SMichael Srba 				  bt541->prop.max_x);
21526822652SMichael Srba 	if (error)
21626822652SMichael Srba 		return error;
21726822652SMichael Srba 
21826822652SMichael Srba 	error = zinitix_write_u16(client, BT541_Y_RESOLUTION,
21926822652SMichael Srba 				  bt541->prop.max_y);
22026822652SMichael Srba 	if (error)
22126822652SMichael Srba 		return error;
22226822652SMichael Srba 
22326822652SMichael Srba 	error = zinitix_write_u16(client, BT541_SUPPORTED_FINGER_NUM,
22426822652SMichael Srba 				  MAX_SUPPORTED_FINGER_NUM);
22526822652SMichael Srba 	if (error)
22626822652SMichael Srba 		return error;
22726822652SMichael Srba 
22826822652SMichael Srba 	error = zinitix_write_u16(client, BT541_INITIAL_TOUCH_MODE,
22926822652SMichael Srba 				  bt541->zinitix_mode);
23026822652SMichael Srba 	if (error)
23126822652SMichael Srba 		return error;
23226822652SMichael Srba 
23326822652SMichael Srba 	error = zinitix_write_u16(client, BT541_TOUCH_MODE,
23426822652SMichael Srba 				  bt541->zinitix_mode);
23526822652SMichael Srba 	if (error)
23626822652SMichael Srba 		return error;
23726822652SMichael Srba 
23826822652SMichael Srba 	error = zinitix_write_u16(client, BT541_INT_ENABLE_FLAG,
23926822652SMichael Srba 				  BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE |
24026822652SMichael Srba 					BIT_UP);
24126822652SMichael Srba 	if (error)
24226822652SMichael Srba 		return error;
24326822652SMichael Srba 
24426822652SMichael Srba 	/* clear queue */
24526822652SMichael Srba 	for (i = 0; i < 10; i++) {
24626822652SMichael Srba 		zinitix_write_cmd(client, BT541_CLEAR_INT_STATUS_CMD);
24726822652SMichael Srba 		udelay(10);
24826822652SMichael Srba 	}
24926822652SMichael Srba 
25026822652SMichael Srba 	return 0;
25126822652SMichael Srba }
25226822652SMichael Srba 
25326822652SMichael Srba static int zinitix_init_regulators(struct bt541_ts_data *bt541)
25426822652SMichael Srba {
25526822652SMichael Srba 	struct i2c_client *client = bt541->client;
25626822652SMichael Srba 	int error;
25726822652SMichael Srba 
25826822652SMichael Srba 	bt541->supplies[0].supply = "vdd";
25926822652SMichael Srba 	bt541->supplies[1].supply = "vddo";
26026822652SMichael Srba 	error = devm_regulator_bulk_get(&client->dev,
26126822652SMichael Srba 					ARRAY_SIZE(bt541->supplies),
26226822652SMichael Srba 					bt541->supplies);
26326822652SMichael Srba 	if (error < 0) {
26426822652SMichael Srba 		dev_err(&client->dev, "Failed to get regulators: %d\n", error);
26526822652SMichael Srba 		return error;
26626822652SMichael Srba 	}
26726822652SMichael Srba 
26826822652SMichael Srba 	return 0;
26926822652SMichael Srba }
27026822652SMichael Srba 
27126822652SMichael Srba static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
27226822652SMichael Srba {
27326822652SMichael Srba 	int error;
27426822652SMichael Srba 	struct i2c_client *client = bt541->client;
27526822652SMichael Srba 
27626822652SMichael Srba 	error = zinitix_write_u16(client, 0xc000, 0x0001);
27726822652SMichael Srba 	if (error) {
27826822652SMichael Srba 		dev_err(&client->dev,
27926822652SMichael Srba 			"Failed to send power sequence(vendor cmd enable)\n");
28026822652SMichael Srba 		return error;
28126822652SMichael Srba 	}
28226822652SMichael Srba 	udelay(10);
28326822652SMichael Srba 
28426822652SMichael Srba 	error = zinitix_write_cmd(client, 0xc004);
28526822652SMichael Srba 	if (error) {
28626822652SMichael Srba 		dev_err(&client->dev,
28726822652SMichael Srba 			"Failed to send power sequence (intn clear)\n");
28826822652SMichael Srba 		return error;
28926822652SMichael Srba 	}
29026822652SMichael Srba 	udelay(10);
29126822652SMichael Srba 
29226822652SMichael Srba 	error = zinitix_write_u16(client, 0xc002, 0x0001);
29326822652SMichael Srba 	if (error) {
29426822652SMichael Srba 		dev_err(&client->dev,
29526822652SMichael Srba 			"Failed to send power sequence (nvm init)\n");
29626822652SMichael Srba 		return error;
29726822652SMichael Srba 	}
29826822652SMichael Srba 	mdelay(2);
29926822652SMichael Srba 
30026822652SMichael Srba 	error = zinitix_write_u16(client, 0xc001, 0x0001);
30126822652SMichael Srba 	if (error) {
30226822652SMichael Srba 		dev_err(&client->dev,
30326822652SMichael Srba 			"Failed to send power sequence (program start)\n");
30426822652SMichael Srba 		return error;
30526822652SMichael Srba 	}
30626822652SMichael Srba 	msleep(FIRMWARE_ON_DELAY);
30726822652SMichael Srba 
30826822652SMichael Srba 	return 0;
30926822652SMichael Srba }
31026822652SMichael Srba 
31126822652SMichael Srba static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
31226822652SMichael Srba 				  const struct point_coord *p)
31326822652SMichael Srba {
31426822652SMichael Srba 	input_mt_slot(bt541->input_dev, slot);
31526822652SMichael Srba 	input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER, true);
31626822652SMichael Srba 	touchscreen_report_pos(bt541->input_dev, &bt541->prop,
31726822652SMichael Srba 			       le16_to_cpu(p->x), le16_to_cpu(p->y), true);
31826822652SMichael Srba 	input_report_abs(bt541->input_dev, ABS_MT_TOUCH_MAJOR, p->width);
31926822652SMichael Srba }
32026822652SMichael Srba 
32126822652SMichael Srba static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
32226822652SMichael Srba {
32326822652SMichael Srba 	struct bt541_ts_data *bt541 = bt541_handler;
32426822652SMichael Srba 	struct i2c_client *client = bt541->client;
32526822652SMichael Srba 	struct touch_event touch_event;
32626822652SMichael Srba 	int error;
32726822652SMichael Srba 	int i;
32826822652SMichael Srba 
32926822652SMichael Srba 	memset(&touch_event, 0, sizeof(struct touch_event));
33026822652SMichael Srba 
33126822652SMichael Srba 	error = zinitix_read_data(bt541->client, BT541_POINT_STATUS_REG,
33226822652SMichael Srba 				  &touch_event, sizeof(struct touch_event));
33326822652SMichael Srba 	if (error) {
33426822652SMichael Srba 		dev_err(&client->dev, "Failed to read in touchpoint struct\n");
33526822652SMichael Srba 		goto out;
33626822652SMichael Srba 	}
33726822652SMichael Srba 
33826822652SMichael Srba 	for (i = 0; i < MAX_SUPPORTED_FINGER_NUM; i++)
33926822652SMichael Srba 		if (touch_event.point_coord[i].sub_status & SUB_BIT_EXIST)
34026822652SMichael Srba 			zinitix_report_finger(bt541, i,
34126822652SMichael Srba 					      &touch_event.point_coord[i]);
34226822652SMichael Srba 
34326822652SMichael Srba 	input_mt_sync_frame(bt541->input_dev);
34426822652SMichael Srba 	input_sync(bt541->input_dev);
34526822652SMichael Srba 
34626822652SMichael Srba out:
34726822652SMichael Srba 	zinitix_write_cmd(bt541->client, BT541_CLEAR_INT_STATUS_CMD);
34826822652SMichael Srba 	return IRQ_HANDLED;
34926822652SMichael Srba }
35026822652SMichael Srba 
35126822652SMichael Srba static int zinitix_start(struct bt541_ts_data *bt541)
35226822652SMichael Srba {
35326822652SMichael Srba 	int error;
35426822652SMichael Srba 
35526822652SMichael Srba 	error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
35626822652SMichael Srba 				      bt541->supplies);
35726822652SMichael Srba 	if (error) {
35826822652SMichael Srba 		dev_err(&bt541->client->dev,
35926822652SMichael Srba 			"Failed to enable regulators: %d\n", error);
36026822652SMichael Srba 		return error;
36126822652SMichael Srba 	}
36226822652SMichael Srba 
36326822652SMichael Srba 	msleep(CHIP_ON_DELAY);
36426822652SMichael Srba 
36526822652SMichael Srba 	error = zinitix_send_power_on_sequence(bt541);
36626822652SMichael Srba 	if (error) {
36726822652SMichael Srba 		dev_err(&bt541->client->dev,
36826822652SMichael Srba 			"Error while sending power-on sequence: %d\n", error);
36926822652SMichael Srba 		return error;
37026822652SMichael Srba 	}
37126822652SMichael Srba 
37226822652SMichael Srba 	error = zinitix_init_touch(bt541);
37326822652SMichael Srba 	if (error) {
37426822652SMichael Srba 		dev_err(&bt541->client->dev,
37526822652SMichael Srba 			"Error while configuring touch IC\n");
37626822652SMichael Srba 		return error;
37726822652SMichael Srba 	}
37826822652SMichael Srba 
37926822652SMichael Srba 	enable_irq(bt541->client->irq);
38026822652SMichael Srba 
38126822652SMichael Srba 	return 0;
38226822652SMichael Srba }
38326822652SMichael Srba 
38426822652SMichael Srba static int zinitix_stop(struct bt541_ts_data *bt541)
38526822652SMichael Srba {
38626822652SMichael Srba 	int error;
38726822652SMichael Srba 
38826822652SMichael Srba 	disable_irq(bt541->client->irq);
38926822652SMichael Srba 
39026822652SMichael Srba 	error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
39126822652SMichael Srba 				       bt541->supplies);
39226822652SMichael Srba 	if (error) {
39326822652SMichael Srba 		dev_err(&bt541->client->dev,
39426822652SMichael Srba 			"Failed to disable regulators: %d\n", error);
39526822652SMichael Srba 		return error;
39626822652SMichael Srba 	}
39726822652SMichael Srba 
39826822652SMichael Srba 	return 0;
39926822652SMichael Srba }
40026822652SMichael Srba 
40126822652SMichael Srba static int zinitix_input_open(struct input_dev *dev)
40226822652SMichael Srba {
40326822652SMichael Srba 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
40426822652SMichael Srba 
40526822652SMichael Srba 	return zinitix_start(bt541);
40626822652SMichael Srba }
40726822652SMichael Srba 
40826822652SMichael Srba static void zinitix_input_close(struct input_dev *dev)
40926822652SMichael Srba {
41026822652SMichael Srba 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
41126822652SMichael Srba 
41226822652SMichael Srba 	zinitix_stop(bt541);
41326822652SMichael Srba }
41426822652SMichael Srba 
41526822652SMichael Srba static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
41626822652SMichael Srba {
41726822652SMichael Srba 	struct input_dev *input_dev;
41826822652SMichael Srba 	int error;
41926822652SMichael Srba 
42026822652SMichael Srba 	input_dev = devm_input_allocate_device(&bt541->client->dev);
42126822652SMichael Srba 	if (!input_dev) {
42226822652SMichael Srba 		dev_err(&bt541->client->dev,
42326822652SMichael Srba 			"Failed to allocate input device.");
42426822652SMichael Srba 		return -ENOMEM;
42526822652SMichael Srba 	}
42626822652SMichael Srba 
42726822652SMichael Srba 	input_set_drvdata(input_dev, bt541);
42826822652SMichael Srba 	bt541->input_dev = input_dev;
42926822652SMichael Srba 
43026822652SMichael Srba 	input_dev->name = "Zinitix Capacitive TouchScreen";
43126822652SMichael Srba 	input_dev->phys = "input/ts";
43226822652SMichael Srba 	input_dev->id.bustype = BUS_I2C;
43326822652SMichael Srba 	input_dev->open = zinitix_input_open;
43426822652SMichael Srba 	input_dev->close = zinitix_input_close;
43526822652SMichael Srba 
43626822652SMichael Srba 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
43726822652SMichael Srba 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
43826822652SMichael Srba 	input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
43926822652SMichael Srba 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
44026822652SMichael Srba 
44126822652SMichael Srba 	touchscreen_parse_properties(input_dev, true, &bt541->prop);
44226822652SMichael Srba 	if (!bt541->prop.max_x || !bt541->prop.max_y) {
44326822652SMichael Srba 		dev_err(&bt541->client->dev,
44426822652SMichael Srba 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
44526822652SMichael Srba 		return -EINVAL;
44626822652SMichael Srba 	}
44726822652SMichael Srba 
44826822652SMichael Srba 	error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
44926822652SMichael Srba 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
45026822652SMichael Srba 	if (error) {
45126822652SMichael Srba 		dev_err(&bt541->client->dev,
45226822652SMichael Srba 			"Failed to initialize MT slots: %d", error);
45326822652SMichael Srba 		return error;
45426822652SMichael Srba 	}
45526822652SMichael Srba 
45626822652SMichael Srba 	error = input_register_device(input_dev);
45726822652SMichael Srba 	if (error) {
45826822652SMichael Srba 		dev_err(&bt541->client->dev,
45926822652SMichael Srba 			"Failed to register input device: %d", error);
46026822652SMichael Srba 		return error;
46126822652SMichael Srba 	}
46226822652SMichael Srba 
46326822652SMichael Srba 	return 0;
46426822652SMichael Srba }
46526822652SMichael Srba 
46626822652SMichael Srba static int zinitix_ts_probe(struct i2c_client *client)
46726822652SMichael Srba {
46826822652SMichael Srba 	struct bt541_ts_data *bt541;
46926822652SMichael Srba 	int error;
47026822652SMichael Srba 
47126822652SMichael Srba 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
47226822652SMichael Srba 		dev_err(&client->dev,
47326822652SMichael Srba 			"Failed to assert adapter's support for plain I2C.\n");
47426822652SMichael Srba 		return -ENXIO;
47526822652SMichael Srba 	}
47626822652SMichael Srba 
47726822652SMichael Srba 	bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
47826822652SMichael Srba 	if (!bt541)
47926822652SMichael Srba 		return -ENOMEM;
48026822652SMichael Srba 
48126822652SMichael Srba 	bt541->client = client;
48226822652SMichael Srba 	i2c_set_clientdata(client, bt541);
48326822652SMichael Srba 
48426822652SMichael Srba 	error = zinitix_init_regulators(bt541);
48526822652SMichael Srba 	if (error) {
48626822652SMichael Srba 		dev_err(&client->dev,
48726822652SMichael Srba 			"Failed to initialize regulators: %d\n", error);
48826822652SMichael Srba 		return error;
48926822652SMichael Srba 	}
49026822652SMichael Srba 
49126822652SMichael Srba 	error = zinitix_init_input_dev(bt541);
49226822652SMichael Srba 	if (error) {
49326822652SMichael Srba 		dev_err(&client->dev,
49426822652SMichael Srba 			"Failed to initialize input device: %d\n", error);
49526822652SMichael Srba 		return error;
49626822652SMichael Srba 	}
49726822652SMichael Srba 
49826822652SMichael Srba 	error = device_property_read_u32(&client->dev, "zinitix,mode",
49926822652SMichael Srba 					 &bt541->zinitix_mode);
50026822652SMichael Srba 	if (error < 0) {
50126822652SMichael Srba 		/* fall back to mode 2 */
50226822652SMichael Srba 		bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
50326822652SMichael Srba 	}
50426822652SMichael Srba 
50526822652SMichael Srba 	if (bt541->zinitix_mode != 2) {
50626822652SMichael Srba 		/*
50726822652SMichael Srba 		 * If there are devices that don't support mode 2, support
50826822652SMichael Srba 		 * for other modes (0, 1) will be needed.
50926822652SMichael Srba 		 */
51026822652SMichael Srba 		dev_err(&client->dev,
51126822652SMichael Srba 			"Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
51226822652SMichael Srba 			bt541->zinitix_mode);
51326822652SMichael Srba 		return -EINVAL;
51426822652SMichael Srba 	}
51526822652SMichael Srba 
51626822652SMichael Srba 	error = devm_request_threaded_irq(&client->dev, client->irq,
51726822652SMichael Srba 					  NULL, zinitix_ts_irq_handler,
518*bcd9730aSBarry Song 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
519*bcd9730aSBarry Song 					  client->name, bt541);
52026822652SMichael Srba 	if (error) {
52126822652SMichael Srba 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
52226822652SMichael Srba 		return error;
52326822652SMichael Srba 	}
52426822652SMichael Srba 
52526822652SMichael Srba 	return 0;
52626822652SMichael Srba }
52726822652SMichael Srba 
52826822652SMichael Srba static int __maybe_unused zinitix_suspend(struct device *dev)
52926822652SMichael Srba {
53026822652SMichael Srba 	struct i2c_client *client = to_i2c_client(dev);
53126822652SMichael Srba 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
53226822652SMichael Srba 
53326822652SMichael Srba 	mutex_lock(&bt541->input_dev->mutex);
53426822652SMichael Srba 
535d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(bt541->input_dev))
53626822652SMichael Srba 		zinitix_stop(bt541);
53726822652SMichael Srba 
53826822652SMichael Srba 	mutex_unlock(&bt541->input_dev->mutex);
53926822652SMichael Srba 
54026822652SMichael Srba 	return 0;
54126822652SMichael Srba }
54226822652SMichael Srba 
54326822652SMichael Srba static int __maybe_unused zinitix_resume(struct device *dev)
54426822652SMichael Srba {
54526822652SMichael Srba 	struct i2c_client *client = to_i2c_client(dev);
54626822652SMichael Srba 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
54726822652SMichael Srba 	int ret = 0;
54826822652SMichael Srba 
54926822652SMichael Srba 	mutex_lock(&bt541->input_dev->mutex);
55026822652SMichael Srba 
551d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(bt541->input_dev))
55226822652SMichael Srba 		ret = zinitix_start(bt541);
55326822652SMichael Srba 
55426822652SMichael Srba 	mutex_unlock(&bt541->input_dev->mutex);
55526822652SMichael Srba 
55626822652SMichael Srba 	return ret;
55726822652SMichael Srba }
55826822652SMichael Srba 
55926822652SMichael Srba static SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
56026822652SMichael Srba 
56126822652SMichael Srba #ifdef CONFIG_OF
56226822652SMichael Srba static const struct of_device_id zinitix_of_match[] = {
56326822652SMichael Srba 	{ .compatible = "zinitix,bt541" },
56426822652SMichael Srba 	{ }
56526822652SMichael Srba };
56626822652SMichael Srba MODULE_DEVICE_TABLE(of, zinitix_of_match);
56726822652SMichael Srba #endif
56826822652SMichael Srba 
56926822652SMichael Srba static struct i2c_driver zinitix_ts_driver = {
57026822652SMichael Srba 	.probe_new = zinitix_ts_probe,
57126822652SMichael Srba 	.driver = {
57226822652SMichael Srba 		.name = "Zinitix-TS",
57326822652SMichael Srba 		.pm = &zinitix_pm_ops,
57426822652SMichael Srba 		.of_match_table = of_match_ptr(zinitix_of_match),
57526822652SMichael Srba 	},
57626822652SMichael Srba };
57726822652SMichael Srba module_i2c_driver(zinitix_ts_driver);
57826822652SMichael Srba 
57926822652SMichael Srba MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
58026822652SMichael Srba MODULE_DESCRIPTION("Zinitix touchscreen driver");
58126822652SMichael Srba MODULE_LICENSE("GPL v2");
582