1*26822652SMichael Srba // SPDX-License-Identifier: GPL-2.0-only
2*26822652SMichael Srba 
3*26822652SMichael Srba #include <linux/delay.h>
4*26822652SMichael Srba #include <linux/i2c.h>
5*26822652SMichael Srba #include <linux/input.h>
6*26822652SMichael Srba #include <linux/input/mt.h>
7*26822652SMichael Srba #include <linux/input/touchscreen.h>
8*26822652SMichael Srba #include <linux/interrupt.h>
9*26822652SMichael Srba #include <linux/irq.h>
10*26822652SMichael Srba #include <linux/kernel.h>
11*26822652SMichael Srba #include <linux/module.h>
12*26822652SMichael Srba #include <linux/of.h>
13*26822652SMichael Srba #include <linux/regulator/consumer.h>
14*26822652SMichael Srba #include <linux/slab.h>
15*26822652SMichael Srba 
16*26822652SMichael Srba /* Register Map */
17*26822652SMichael Srba 
18*26822652SMichael Srba #define BT541_SWRESET_CMD			0x0000
19*26822652SMichael Srba #define BT541_WAKEUP_CMD			0x0001
20*26822652SMichael Srba 
21*26822652SMichael Srba #define BT541_IDLE_CMD				0x0004
22*26822652SMichael Srba #define BT541_SLEEP_CMD				0x0005
23*26822652SMichael Srba 
24*26822652SMichael Srba #define BT541_CLEAR_INT_STATUS_CMD		0x0003
25*26822652SMichael Srba #define BT541_CALIBRATE_CMD			0x0006
26*26822652SMichael Srba #define BT541_SAVE_STATUS_CMD			0x0007
27*26822652SMichael Srba #define BT541_SAVE_CALIBRATION_CMD		0x0008
28*26822652SMichael Srba #define BT541_RECALL_FACTORY_CMD		0x000f
29*26822652SMichael Srba 
30*26822652SMichael Srba #define BT541_THRESHOLD				0x0020
31*26822652SMichael Srba 
32*26822652SMichael Srba #define BT541_LARGE_PALM_REJECT_AREA_TH		0x003F
33*26822652SMichael Srba 
34*26822652SMichael Srba #define BT541_DEBUG_REG				0x0115 /* 0~7 */
35*26822652SMichael Srba 
36*26822652SMichael Srba #define BT541_TOUCH_MODE			0x0010
37*26822652SMichael Srba #define BT541_CHIP_REVISION			0x0011
38*26822652SMichael Srba #define BT541_FIRMWARE_VERSION			0x0012
39*26822652SMichael Srba 
40*26822652SMichael Srba #define ZINITIX_USB_DETECT			0x116
41*26822652SMichael Srba 
42*26822652SMichael Srba #define BT541_MINOR_FW_VERSION			0x0121
43*26822652SMichael Srba 
44*26822652SMichael Srba #define BT541_VENDOR_ID				0x001C
45*26822652SMichael Srba #define BT541_HW_ID				0x0014
46*26822652SMichael Srba 
47*26822652SMichael Srba #define BT541_DATA_VERSION_REG			0x0013
48*26822652SMichael Srba #define BT541_SUPPORTED_FINGER_NUM		0x0015
49*26822652SMichael Srba #define BT541_EEPROM_INFO			0x0018
50*26822652SMichael Srba #define BT541_INITIAL_TOUCH_MODE		0x0019
51*26822652SMichael Srba 
52*26822652SMichael Srba #define BT541_TOTAL_NUMBER_OF_X			0x0060
53*26822652SMichael Srba #define BT541_TOTAL_NUMBER_OF_Y			0x0061
54*26822652SMichael Srba 
55*26822652SMichael Srba #define BT541_DELAY_RAW_FOR_HOST		0x007f
56*26822652SMichael Srba 
57*26822652SMichael Srba #define BT541_BUTTON_SUPPORTED_NUM		0x00B0
58*26822652SMichael Srba #define BT541_BUTTON_SENSITIVITY		0x00B2
59*26822652SMichael Srba #define BT541_DUMMY_BUTTON_SENSITIVITY		0X00C8
60*26822652SMichael Srba 
61*26822652SMichael Srba #define BT541_X_RESOLUTION			0x00C0
62*26822652SMichael Srba #define BT541_Y_RESOLUTION			0x00C1
63*26822652SMichael Srba 
64*26822652SMichael Srba #define BT541_POINT_STATUS_REG			0x0080
65*26822652SMichael Srba #define BT541_ICON_STATUS_REG			0x00AA
66*26822652SMichael Srba 
67*26822652SMichael Srba #define BT541_POINT_COORD_REG			(BT541_POINT_STATUS_REG + 2)
68*26822652SMichael Srba 
69*26822652SMichael Srba #define BT541_AFE_FREQUENCY			0x0100
70*26822652SMichael Srba #define BT541_DND_N_COUNT			0x0122
71*26822652SMichael Srba #define BT541_DND_U_COUNT			0x0135
72*26822652SMichael Srba 
73*26822652SMichael Srba #define BT541_RAWDATA_REG			0x0200
74*26822652SMichael Srba 
75*26822652SMichael Srba #define BT541_EEPROM_INFO_REG			0x0018
76*26822652SMichael Srba 
77*26822652SMichael Srba #define BT541_INT_ENABLE_FLAG			0x00f0
78*26822652SMichael Srba #define BT541_PERIODICAL_INTERRUPT_INTERVAL	0x00f1
79*26822652SMichael Srba 
80*26822652SMichael Srba #define BT541_BTN_WIDTH				0x016d
81*26822652SMichael Srba 
82*26822652SMichael Srba #define BT541_CHECKSUM_RESULT			0x012c
83*26822652SMichael Srba 
84*26822652SMichael Srba #define BT541_INIT_FLASH			0x01d0
85*26822652SMichael Srba #define BT541_WRITE_FLASH			0x01d1
86*26822652SMichael Srba #define BT541_READ_FLASH			0x01d2
87*26822652SMichael Srba 
88*26822652SMichael Srba #define ZINITIX_INTERNAL_FLAG_02		0x011e
89*26822652SMichael Srba #define ZINITIX_INTERNAL_FLAG_03		0x011f
90*26822652SMichael Srba 
91*26822652SMichael Srba #define ZINITIX_I2C_CHECKSUM_WCNT		0x016a
92*26822652SMichael Srba #define ZINITIX_I2C_CHECKSUM_RESULT		0x016c
93*26822652SMichael Srba 
94*26822652SMichael Srba /* Interrupt & status register flags */
95*26822652SMichael Srba 
96*26822652SMichael Srba #define BIT_PT_CNT_CHANGE			BIT(0)
97*26822652SMichael Srba #define BIT_DOWN				BIT(1)
98*26822652SMichael Srba #define BIT_MOVE				BIT(2)
99*26822652SMichael Srba #define BIT_UP					BIT(3)
100*26822652SMichael Srba #define BIT_PALM				BIT(4)
101*26822652SMichael Srba #define BIT_PALM_REJECT				BIT(5)
102*26822652SMichael Srba #define BIT_RESERVED_0				BIT(6)
103*26822652SMichael Srba #define BIT_RESERVED_1				BIT(7)
104*26822652SMichael Srba #define BIT_WEIGHT_CHANGE			BIT(8)
105*26822652SMichael Srba #define BIT_PT_NO_CHANGE			BIT(9)
106*26822652SMichael Srba #define BIT_REJECT				BIT(10)
107*26822652SMichael Srba #define BIT_PT_EXIST				BIT(11)
108*26822652SMichael Srba #define BIT_RESERVED_2				BIT(12)
109*26822652SMichael Srba #define BIT_ERROR				BIT(13)
110*26822652SMichael Srba #define BIT_DEBUG				BIT(14)
111*26822652SMichael Srba #define BIT_ICON_EVENT				BIT(15)
112*26822652SMichael Srba 
113*26822652SMichael Srba #define SUB_BIT_EXIST				BIT(0)
114*26822652SMichael Srba #define SUB_BIT_DOWN				BIT(1)
115*26822652SMichael Srba #define SUB_BIT_MOVE				BIT(2)
116*26822652SMichael Srba #define SUB_BIT_UP				BIT(3)
117*26822652SMichael Srba #define SUB_BIT_UPDATE				BIT(4)
118*26822652SMichael Srba #define SUB_BIT_WAIT				BIT(5)
119*26822652SMichael Srba 
120*26822652SMichael Srba #define DEFAULT_TOUCH_POINT_MODE		2
121*26822652SMichael Srba #define MAX_SUPPORTED_FINGER_NUM		5
122*26822652SMichael Srba 
123*26822652SMichael Srba #define CHIP_ON_DELAY				15 // ms
124*26822652SMichael Srba #define FIRMWARE_ON_DELAY			40 // ms
125*26822652SMichael Srba 
126*26822652SMichael Srba struct point_coord {
127*26822652SMichael Srba 	__le16	x;
128*26822652SMichael Srba 	__le16	y;
129*26822652SMichael Srba 	u8	width;
130*26822652SMichael Srba 	u8	sub_status;
131*26822652SMichael Srba 	// currently unused, but needed as padding:
132*26822652SMichael Srba 	u8	minor_width;
133*26822652SMichael Srba 	u8	angle;
134*26822652SMichael Srba };
135*26822652SMichael Srba 
136*26822652SMichael Srba struct touch_event {
137*26822652SMichael Srba 	__le16	status;
138*26822652SMichael Srba 	u8	finger_cnt;
139*26822652SMichael Srba 	u8	time_stamp;
140*26822652SMichael Srba 	struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM];
141*26822652SMichael Srba };
142*26822652SMichael Srba 
143*26822652SMichael Srba struct bt541_ts_data {
144*26822652SMichael Srba 	struct i2c_client *client;
145*26822652SMichael Srba 	struct input_dev *input_dev;
146*26822652SMichael Srba 	struct touchscreen_properties prop;
147*26822652SMichael Srba 	struct regulator_bulk_data supplies[2];
148*26822652SMichael Srba 	u32 zinitix_mode;
149*26822652SMichael Srba };
150*26822652SMichael Srba 
151*26822652SMichael Srba static int zinitix_read_data(struct i2c_client *client,
152*26822652SMichael Srba 			     u16 reg, void *values, size_t length)
153*26822652SMichael Srba {
154*26822652SMichael Srba 	__le16 reg_le = cpu_to_le16(reg);
155*26822652SMichael Srba 	int ret;
156*26822652SMichael Srba 
157*26822652SMichael Srba 	/* A single i2c_transfer() transaction does not work here. */
158*26822652SMichael Srba 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
159*26822652SMichael Srba 	if (ret != sizeof(reg_le))
160*26822652SMichael Srba 		return ret < 0 ? ret : -EIO;
161*26822652SMichael Srba 
162*26822652SMichael Srba 	ret = i2c_master_recv(client, (u8 *)values, length);
163*26822652SMichael Srba 	if (ret != length)
164*26822652SMichael Srba 		return ret < 0 ? ret : -EIO; ;
165*26822652SMichael Srba 
166*26822652SMichael Srba 	return 0;
167*26822652SMichael Srba }
168*26822652SMichael Srba 
169*26822652SMichael Srba static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value)
170*26822652SMichael Srba {
171*26822652SMichael Srba 	__le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)};
172*26822652SMichael Srba 	int ret;
173*26822652SMichael Srba 
174*26822652SMichael Srba 	ret = i2c_master_send(client, (u8 *)packet, sizeof(packet));
175*26822652SMichael Srba 	if (ret != sizeof(packet))
176*26822652SMichael Srba 		return ret < 0 ? ret : -EIO;
177*26822652SMichael Srba 
178*26822652SMichael Srba 	return 0;
179*26822652SMichael Srba }
180*26822652SMichael Srba 
181*26822652SMichael Srba static int zinitix_write_cmd(struct i2c_client *client, u16 reg)
182*26822652SMichael Srba {
183*26822652SMichael Srba 	__le16 reg_le = cpu_to_le16(reg);
184*26822652SMichael Srba 	int ret;
185*26822652SMichael Srba 
186*26822652SMichael Srba 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
187*26822652SMichael Srba 	if (ret != sizeof(reg_le))
188*26822652SMichael Srba 		return ret < 0 ? ret : -EIO;
189*26822652SMichael Srba 
190*26822652SMichael Srba 	return 0;
191*26822652SMichael Srba }
192*26822652SMichael Srba 
193*26822652SMichael Srba static bool zinitix_init_touch(struct bt541_ts_data *bt541)
194*26822652SMichael Srba {
195*26822652SMichael Srba 	struct i2c_client *client = bt541->client;
196*26822652SMichael Srba 	int i;
197*26822652SMichael Srba 	int error;
198*26822652SMichael Srba 
199*26822652SMichael Srba 	error = zinitix_write_cmd(client, BT541_SWRESET_CMD);
200*26822652SMichael Srba 	if (error) {
201*26822652SMichael Srba 		dev_err(&client->dev, "Failed to write reset command\n");
202*26822652SMichael Srba 		return error;
203*26822652SMichael Srba 	}
204*26822652SMichael Srba 
205*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_INT_ENABLE_FLAG, 0x0);
206*26822652SMichael Srba 	if (error) {
207*26822652SMichael Srba 		dev_err(&client->dev,
208*26822652SMichael Srba 			"Failed to reset interrupt enable flag\n");
209*26822652SMichael Srba 		return error;
210*26822652SMichael Srba 	}
211*26822652SMichael Srba 
212*26822652SMichael Srba 	/* initialize */
213*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_X_RESOLUTION,
214*26822652SMichael Srba 				  bt541->prop.max_x);
215*26822652SMichael Srba 	if (error)
216*26822652SMichael Srba 		return error;
217*26822652SMichael Srba 
218*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_Y_RESOLUTION,
219*26822652SMichael Srba 				  bt541->prop.max_y);
220*26822652SMichael Srba 	if (error)
221*26822652SMichael Srba 		return error;
222*26822652SMichael Srba 
223*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_SUPPORTED_FINGER_NUM,
224*26822652SMichael Srba 				  MAX_SUPPORTED_FINGER_NUM);
225*26822652SMichael Srba 	if (error)
226*26822652SMichael Srba 		return error;
227*26822652SMichael Srba 
228*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_INITIAL_TOUCH_MODE,
229*26822652SMichael Srba 				  bt541->zinitix_mode);
230*26822652SMichael Srba 	if (error)
231*26822652SMichael Srba 		return error;
232*26822652SMichael Srba 
233*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_TOUCH_MODE,
234*26822652SMichael Srba 				  bt541->zinitix_mode);
235*26822652SMichael Srba 	if (error)
236*26822652SMichael Srba 		return error;
237*26822652SMichael Srba 
238*26822652SMichael Srba 	error = zinitix_write_u16(client, BT541_INT_ENABLE_FLAG,
239*26822652SMichael Srba 				  BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE |
240*26822652SMichael Srba 					BIT_UP);
241*26822652SMichael Srba 	if (error)
242*26822652SMichael Srba 		return error;
243*26822652SMichael Srba 
244*26822652SMichael Srba 	/* clear queue */
245*26822652SMichael Srba 	for (i = 0; i < 10; i++) {
246*26822652SMichael Srba 		zinitix_write_cmd(client, BT541_CLEAR_INT_STATUS_CMD);
247*26822652SMichael Srba 		udelay(10);
248*26822652SMichael Srba 	}
249*26822652SMichael Srba 
250*26822652SMichael Srba 	return 0;
251*26822652SMichael Srba }
252*26822652SMichael Srba 
253*26822652SMichael Srba static int zinitix_init_regulators(struct bt541_ts_data *bt541)
254*26822652SMichael Srba {
255*26822652SMichael Srba 	struct i2c_client *client = bt541->client;
256*26822652SMichael Srba 	int error;
257*26822652SMichael Srba 
258*26822652SMichael Srba 	bt541->supplies[0].supply = "vdd";
259*26822652SMichael Srba 	bt541->supplies[1].supply = "vddo";
260*26822652SMichael Srba 	error = devm_regulator_bulk_get(&client->dev,
261*26822652SMichael Srba 					ARRAY_SIZE(bt541->supplies),
262*26822652SMichael Srba 					bt541->supplies);
263*26822652SMichael Srba 	if (error < 0) {
264*26822652SMichael Srba 		dev_err(&client->dev, "Failed to get regulators: %d\n", error);
265*26822652SMichael Srba 		return error;
266*26822652SMichael Srba 	}
267*26822652SMichael Srba 
268*26822652SMichael Srba 	return 0;
269*26822652SMichael Srba }
270*26822652SMichael Srba 
271*26822652SMichael Srba static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
272*26822652SMichael Srba {
273*26822652SMichael Srba 	int error;
274*26822652SMichael Srba 	struct i2c_client *client = bt541->client;
275*26822652SMichael Srba 
276*26822652SMichael Srba 	error = zinitix_write_u16(client, 0xc000, 0x0001);
277*26822652SMichael Srba 	if (error) {
278*26822652SMichael Srba 		dev_err(&client->dev,
279*26822652SMichael Srba 			"Failed to send power sequence(vendor cmd enable)\n");
280*26822652SMichael Srba 		return error;
281*26822652SMichael Srba 	}
282*26822652SMichael Srba 	udelay(10);
283*26822652SMichael Srba 
284*26822652SMichael Srba 	error = zinitix_write_cmd(client, 0xc004);
285*26822652SMichael Srba 	if (error) {
286*26822652SMichael Srba 		dev_err(&client->dev,
287*26822652SMichael Srba 			"Failed to send power sequence (intn clear)\n");
288*26822652SMichael Srba 		return error;
289*26822652SMichael Srba 	}
290*26822652SMichael Srba 	udelay(10);
291*26822652SMichael Srba 
292*26822652SMichael Srba 	error = zinitix_write_u16(client, 0xc002, 0x0001);
293*26822652SMichael Srba 	if (error) {
294*26822652SMichael Srba 		dev_err(&client->dev,
295*26822652SMichael Srba 			"Failed to send power sequence (nvm init)\n");
296*26822652SMichael Srba 		return error;
297*26822652SMichael Srba 	}
298*26822652SMichael Srba 	mdelay(2);
299*26822652SMichael Srba 
300*26822652SMichael Srba 	error = zinitix_write_u16(client, 0xc001, 0x0001);
301*26822652SMichael Srba 	if (error) {
302*26822652SMichael Srba 		dev_err(&client->dev,
303*26822652SMichael Srba 			"Failed to send power sequence (program start)\n");
304*26822652SMichael Srba 		return error;
305*26822652SMichael Srba 	}
306*26822652SMichael Srba 	msleep(FIRMWARE_ON_DELAY);
307*26822652SMichael Srba 
308*26822652SMichael Srba 	return 0;
309*26822652SMichael Srba }
310*26822652SMichael Srba 
311*26822652SMichael Srba static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
312*26822652SMichael Srba 				  const struct point_coord *p)
313*26822652SMichael Srba {
314*26822652SMichael Srba 	input_mt_slot(bt541->input_dev, slot);
315*26822652SMichael Srba 	input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER, true);
316*26822652SMichael Srba 	touchscreen_report_pos(bt541->input_dev, &bt541->prop,
317*26822652SMichael Srba 			       le16_to_cpu(p->x), le16_to_cpu(p->y), true);
318*26822652SMichael Srba 	input_report_abs(bt541->input_dev, ABS_MT_TOUCH_MAJOR, p->width);
319*26822652SMichael Srba }
320*26822652SMichael Srba 
321*26822652SMichael Srba static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
322*26822652SMichael Srba {
323*26822652SMichael Srba 	struct bt541_ts_data *bt541 = bt541_handler;
324*26822652SMichael Srba 	struct i2c_client *client = bt541->client;
325*26822652SMichael Srba 	struct touch_event touch_event;
326*26822652SMichael Srba 	int error;
327*26822652SMichael Srba 	int i;
328*26822652SMichael Srba 
329*26822652SMichael Srba 	memset(&touch_event, 0, sizeof(struct touch_event));
330*26822652SMichael Srba 
331*26822652SMichael Srba 	error = zinitix_read_data(bt541->client, BT541_POINT_STATUS_REG,
332*26822652SMichael Srba 				  &touch_event, sizeof(struct touch_event));
333*26822652SMichael Srba 	if (error) {
334*26822652SMichael Srba 		dev_err(&client->dev, "Failed to read in touchpoint struct\n");
335*26822652SMichael Srba 		goto out;
336*26822652SMichael Srba 	}
337*26822652SMichael Srba 
338*26822652SMichael Srba 	for (i = 0; i < MAX_SUPPORTED_FINGER_NUM; i++)
339*26822652SMichael Srba 		if (touch_event.point_coord[i].sub_status & SUB_BIT_EXIST)
340*26822652SMichael Srba 			zinitix_report_finger(bt541, i,
341*26822652SMichael Srba 					      &touch_event.point_coord[i]);
342*26822652SMichael Srba 
343*26822652SMichael Srba 	input_mt_sync_frame(bt541->input_dev);
344*26822652SMichael Srba 	input_sync(bt541->input_dev);
345*26822652SMichael Srba 
346*26822652SMichael Srba out:
347*26822652SMichael Srba 	zinitix_write_cmd(bt541->client, BT541_CLEAR_INT_STATUS_CMD);
348*26822652SMichael Srba 	return IRQ_HANDLED;
349*26822652SMichael Srba }
350*26822652SMichael Srba 
351*26822652SMichael Srba static int zinitix_start(struct bt541_ts_data *bt541)
352*26822652SMichael Srba {
353*26822652SMichael Srba 	int error;
354*26822652SMichael Srba 
355*26822652SMichael Srba 	error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
356*26822652SMichael Srba 				      bt541->supplies);
357*26822652SMichael Srba 	if (error) {
358*26822652SMichael Srba 		dev_err(&bt541->client->dev,
359*26822652SMichael Srba 			"Failed to enable regulators: %d\n", error);
360*26822652SMichael Srba 		return error;
361*26822652SMichael Srba 	}
362*26822652SMichael Srba 
363*26822652SMichael Srba 	msleep(CHIP_ON_DELAY);
364*26822652SMichael Srba 
365*26822652SMichael Srba 	error = zinitix_send_power_on_sequence(bt541);
366*26822652SMichael Srba 	if (error) {
367*26822652SMichael Srba 		dev_err(&bt541->client->dev,
368*26822652SMichael Srba 			"Error while sending power-on sequence: %d\n", error);
369*26822652SMichael Srba 		return error;
370*26822652SMichael Srba 	}
371*26822652SMichael Srba 
372*26822652SMichael Srba 	error = zinitix_init_touch(bt541);
373*26822652SMichael Srba 	if (error) {
374*26822652SMichael Srba 		dev_err(&bt541->client->dev,
375*26822652SMichael Srba 			"Error while configuring touch IC\n");
376*26822652SMichael Srba 		return error;
377*26822652SMichael Srba 	}
378*26822652SMichael Srba 
379*26822652SMichael Srba 	enable_irq(bt541->client->irq);
380*26822652SMichael Srba 
381*26822652SMichael Srba 	return 0;
382*26822652SMichael Srba }
383*26822652SMichael Srba 
384*26822652SMichael Srba static int zinitix_stop(struct bt541_ts_data *bt541)
385*26822652SMichael Srba {
386*26822652SMichael Srba 	int error;
387*26822652SMichael Srba 
388*26822652SMichael Srba 	disable_irq(bt541->client->irq);
389*26822652SMichael Srba 
390*26822652SMichael Srba 	error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
391*26822652SMichael Srba 				       bt541->supplies);
392*26822652SMichael Srba 	if (error) {
393*26822652SMichael Srba 		dev_err(&bt541->client->dev,
394*26822652SMichael Srba 			"Failed to disable regulators: %d\n", error);
395*26822652SMichael Srba 		return error;
396*26822652SMichael Srba 	}
397*26822652SMichael Srba 
398*26822652SMichael Srba 	return 0;
399*26822652SMichael Srba }
400*26822652SMichael Srba 
401*26822652SMichael Srba static int zinitix_input_open(struct input_dev *dev)
402*26822652SMichael Srba {
403*26822652SMichael Srba 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
404*26822652SMichael Srba 
405*26822652SMichael Srba 	return zinitix_start(bt541);
406*26822652SMichael Srba }
407*26822652SMichael Srba 
408*26822652SMichael Srba static void zinitix_input_close(struct input_dev *dev)
409*26822652SMichael Srba {
410*26822652SMichael Srba 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
411*26822652SMichael Srba 
412*26822652SMichael Srba 	zinitix_stop(bt541);
413*26822652SMichael Srba }
414*26822652SMichael Srba 
415*26822652SMichael Srba static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
416*26822652SMichael Srba {
417*26822652SMichael Srba 	struct input_dev *input_dev;
418*26822652SMichael Srba 	int error;
419*26822652SMichael Srba 
420*26822652SMichael Srba 	input_dev = devm_input_allocate_device(&bt541->client->dev);
421*26822652SMichael Srba 	if (!input_dev) {
422*26822652SMichael Srba 		dev_err(&bt541->client->dev,
423*26822652SMichael Srba 			"Failed to allocate input device.");
424*26822652SMichael Srba 		return -ENOMEM;
425*26822652SMichael Srba 	}
426*26822652SMichael Srba 
427*26822652SMichael Srba 	input_set_drvdata(input_dev, bt541);
428*26822652SMichael Srba 	bt541->input_dev = input_dev;
429*26822652SMichael Srba 
430*26822652SMichael Srba 	input_dev->name = "Zinitix Capacitive TouchScreen";
431*26822652SMichael Srba 	input_dev->phys = "input/ts";
432*26822652SMichael Srba 	input_dev->id.bustype = BUS_I2C;
433*26822652SMichael Srba 	input_dev->open = zinitix_input_open;
434*26822652SMichael Srba 	input_dev->close = zinitix_input_close;
435*26822652SMichael Srba 
436*26822652SMichael Srba 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
437*26822652SMichael Srba 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
438*26822652SMichael Srba 	input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
439*26822652SMichael Srba 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
440*26822652SMichael Srba 
441*26822652SMichael Srba 	touchscreen_parse_properties(input_dev, true, &bt541->prop);
442*26822652SMichael Srba 	if (!bt541->prop.max_x || !bt541->prop.max_y) {
443*26822652SMichael Srba 		dev_err(&bt541->client->dev,
444*26822652SMichael Srba 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
445*26822652SMichael Srba 		return -EINVAL;
446*26822652SMichael Srba 	}
447*26822652SMichael Srba 
448*26822652SMichael Srba 	error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
449*26822652SMichael Srba 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
450*26822652SMichael Srba 	if (error) {
451*26822652SMichael Srba 		dev_err(&bt541->client->dev,
452*26822652SMichael Srba 			"Failed to initialize MT slots: %d", error);
453*26822652SMichael Srba 		return error;
454*26822652SMichael Srba 	}
455*26822652SMichael Srba 
456*26822652SMichael Srba 	error = input_register_device(input_dev);
457*26822652SMichael Srba 	if (error) {
458*26822652SMichael Srba 		dev_err(&bt541->client->dev,
459*26822652SMichael Srba 			"Failed to register input device: %d", error);
460*26822652SMichael Srba 		return error;
461*26822652SMichael Srba 	}
462*26822652SMichael Srba 
463*26822652SMichael Srba 	return 0;
464*26822652SMichael Srba }
465*26822652SMichael Srba 
466*26822652SMichael Srba static int zinitix_ts_probe(struct i2c_client *client)
467*26822652SMichael Srba {
468*26822652SMichael Srba 	struct bt541_ts_data *bt541;
469*26822652SMichael Srba 	int error;
470*26822652SMichael Srba 
471*26822652SMichael Srba 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
472*26822652SMichael Srba 		dev_err(&client->dev,
473*26822652SMichael Srba 			"Failed to assert adapter's support for plain I2C.\n");
474*26822652SMichael Srba 		return -ENXIO;
475*26822652SMichael Srba 	}
476*26822652SMichael Srba 
477*26822652SMichael Srba 	bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
478*26822652SMichael Srba 	if (!bt541)
479*26822652SMichael Srba 		return -ENOMEM;
480*26822652SMichael Srba 
481*26822652SMichael Srba 	bt541->client = client;
482*26822652SMichael Srba 	i2c_set_clientdata(client, bt541);
483*26822652SMichael Srba 
484*26822652SMichael Srba 	error = zinitix_init_regulators(bt541);
485*26822652SMichael Srba 	if (error) {
486*26822652SMichael Srba 		dev_err(&client->dev,
487*26822652SMichael Srba 			"Failed to initialize regulators: %d\n", error);
488*26822652SMichael Srba 		return error;
489*26822652SMichael Srba 	}
490*26822652SMichael Srba 
491*26822652SMichael Srba 	error = zinitix_init_input_dev(bt541);
492*26822652SMichael Srba 	if (error) {
493*26822652SMichael Srba 		dev_err(&client->dev,
494*26822652SMichael Srba 			"Failed to initialize input device: %d\n", error);
495*26822652SMichael Srba 		return error;
496*26822652SMichael Srba 	}
497*26822652SMichael Srba 
498*26822652SMichael Srba 	error = device_property_read_u32(&client->dev, "zinitix,mode",
499*26822652SMichael Srba 					 &bt541->zinitix_mode);
500*26822652SMichael Srba 	if (error < 0) {
501*26822652SMichael Srba 		/* fall back to mode 2 */
502*26822652SMichael Srba 		bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
503*26822652SMichael Srba 	}
504*26822652SMichael Srba 
505*26822652SMichael Srba 	if (bt541->zinitix_mode != 2) {
506*26822652SMichael Srba 		/*
507*26822652SMichael Srba 		 * If there are devices that don't support mode 2, support
508*26822652SMichael Srba 		 * for other modes (0, 1) will be needed.
509*26822652SMichael Srba 		 */
510*26822652SMichael Srba 		dev_err(&client->dev,
511*26822652SMichael Srba 			"Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
512*26822652SMichael Srba 			bt541->zinitix_mode);
513*26822652SMichael Srba 		return -EINVAL;
514*26822652SMichael Srba 	}
515*26822652SMichael Srba 
516*26822652SMichael Srba 	irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
517*26822652SMichael Srba 	error = devm_request_threaded_irq(&client->dev, client->irq,
518*26822652SMichael Srba 					  NULL, zinitix_ts_irq_handler,
519*26822652SMichael Srba 					  IRQF_ONESHOT, client->name, bt541);
520*26822652SMichael Srba 	if (error) {
521*26822652SMichael Srba 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
522*26822652SMichael Srba 		return error;
523*26822652SMichael Srba 	}
524*26822652SMichael Srba 
525*26822652SMichael Srba 	return 0;
526*26822652SMichael Srba }
527*26822652SMichael Srba 
528*26822652SMichael Srba static int __maybe_unused zinitix_suspend(struct device *dev)
529*26822652SMichael Srba {
530*26822652SMichael Srba 	struct i2c_client *client = to_i2c_client(dev);
531*26822652SMichael Srba 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
532*26822652SMichael Srba 
533*26822652SMichael Srba 	mutex_lock(&bt541->input_dev->mutex);
534*26822652SMichael Srba 
535*26822652SMichael Srba 	if (bt541->input_dev->users)
536*26822652SMichael Srba 		zinitix_stop(bt541);
537*26822652SMichael Srba 
538*26822652SMichael Srba 	mutex_unlock(&bt541->input_dev->mutex);
539*26822652SMichael Srba 
540*26822652SMichael Srba 	return 0;
541*26822652SMichael Srba }
542*26822652SMichael Srba 
543*26822652SMichael Srba static int __maybe_unused zinitix_resume(struct device *dev)
544*26822652SMichael Srba {
545*26822652SMichael Srba 	struct i2c_client *client = to_i2c_client(dev);
546*26822652SMichael Srba 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
547*26822652SMichael Srba 	int ret = 0;
548*26822652SMichael Srba 
549*26822652SMichael Srba 	mutex_lock(&bt541->input_dev->mutex);
550*26822652SMichael Srba 
551*26822652SMichael Srba 	if (bt541->input_dev->users)
552*26822652SMichael Srba 		ret = zinitix_start(bt541);
553*26822652SMichael Srba 
554*26822652SMichael Srba 	mutex_unlock(&bt541->input_dev->mutex);
555*26822652SMichael Srba 
556*26822652SMichael Srba 	return ret;
557*26822652SMichael Srba }
558*26822652SMichael Srba 
559*26822652SMichael Srba static SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
560*26822652SMichael Srba 
561*26822652SMichael Srba #ifdef CONFIG_OF
562*26822652SMichael Srba static const struct of_device_id zinitix_of_match[] = {
563*26822652SMichael Srba 	{ .compatible = "zinitix,bt541" },
564*26822652SMichael Srba 	{ }
565*26822652SMichael Srba };
566*26822652SMichael Srba MODULE_DEVICE_TABLE(of, zinitix_of_match);
567*26822652SMichael Srba #endif
568*26822652SMichael Srba 
569*26822652SMichael Srba static struct i2c_driver zinitix_ts_driver = {
570*26822652SMichael Srba 	.probe_new = zinitix_ts_probe,
571*26822652SMichael Srba 	.driver = {
572*26822652SMichael Srba 		.name = "Zinitix-TS",
573*26822652SMichael Srba 		.pm = &zinitix_pm_ops,
574*26822652SMichael Srba 		.of_match_table = of_match_ptr(zinitix_of_match),
575*26822652SMichael Srba 	},
576*26822652SMichael Srba };
577*26822652SMichael Srba module_i2c_driver(zinitix_ts_driver);
578*26822652SMichael Srba 
579*26822652SMichael Srba MODULE_AUTHOR("Michael Srba <[email protected]>");
580*26822652SMichael Srba MODULE_DESCRIPTION("Zinitix touchscreen driver");
581*26822652SMichael Srba MODULE_LICENSE("GPL v2");
582