xref: /linux/drivers/input/touchscreen/zinitix.c (revision f9e4825524aaf28af6b2097776616f27c31d6847)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/delay.h>
4 #include <linux/i2c.h>
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/input/touchscreen.h>
8 #include <linux/interrupt.h>
9 #include <linux/irq.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/property.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
16 
17 /* Register Map */
18 
19 #define ZINITIX_SWRESET_CMD			0x0000
20 #define ZINITIX_WAKEUP_CMD			0x0001
21 
22 #define ZINITIX_IDLE_CMD			0x0004
23 #define ZINITIX_SLEEP_CMD			0x0005
24 
25 #define ZINITIX_CLEAR_INT_STATUS_CMD		0x0003
26 #define ZINITIX_CALIBRATE_CMD			0x0006
27 #define ZINITIX_SAVE_STATUS_CMD			0x0007
28 #define ZINITIX_SAVE_CALIBRATION_CMD		0x0008
29 #define ZINITIX_RECALL_FACTORY_CMD		0x000f
30 
31 #define ZINITIX_THRESHOLD			0x0020
32 
33 #define ZINITIX_LARGE_PALM_REJECT_AREA_TH	0x003F
34 
35 #define ZINITIX_DEBUG_REG			0x0115 /* 0~7 */
36 
37 #define ZINITIX_TOUCH_MODE			0x0010
38 
39 #define ZINITIX_CHIP_REVISION			0x0011
40 #define ZINITIX_CHIP_BTX0X_MASK			0xF0F0
41 #define ZINITIX_CHIP_BT4X2			0x4020
42 #define ZINITIX_CHIP_BT4X3			0x4030
43 #define ZINITIX_CHIP_BT4X4			0x4040
44 
45 #define ZINITIX_FIRMWARE_VERSION		0x0012
46 
47 #define ZINITIX_USB_DETECT			0x116
48 
49 #define ZINITIX_MINOR_FW_VERSION		0x0121
50 
51 #define ZINITIX_VENDOR_ID			0x001C
52 #define ZINITIX_HW_ID				0x0014
53 
54 #define ZINITIX_DATA_VERSION_REG		0x0013
55 #define ZINITIX_SUPPORTED_FINGER_NUM		0x0015
56 #define ZINITIX_EEPROM_INFO			0x0018
57 #define ZINITIX_INITIAL_TOUCH_MODE		0x0019
58 
59 #define ZINITIX_TOTAL_NUMBER_OF_X		0x0060
60 #define ZINITIX_TOTAL_NUMBER_OF_Y		0x0061
61 
62 #define ZINITIX_DELAY_RAW_FOR_HOST		0x007f
63 
64 #define ZINITIX_BUTTON_SUPPORTED_NUM		0x00B0
65 #define ZINITIX_BUTTON_SENSITIVITY		0x00B2
66 #define ZINITIX_DUMMY_BUTTON_SENSITIVITY	0X00C8
67 
68 #define ZINITIX_X_RESOLUTION			0x00C0
69 #define ZINITIX_Y_RESOLUTION			0x00C1
70 
71 #define ZINITIX_POINT_STATUS_REG		0x0080
72 
73 #define ZINITIX_BT4X2_ICON_STATUS_REG		0x009A
74 #define ZINITIX_BT4X3_ICON_STATUS_REG		0x00A0
75 #define ZINITIX_BT4X4_ICON_STATUS_REG		0x00A0
76 #define ZINITIX_BT5XX_ICON_STATUS_REG		0x00AA
77 
78 #define ZINITIX_POINT_COORD_REG			(ZINITIX_POINT_STATUS_REG + 2)
79 
80 #define ZINITIX_AFE_FREQUENCY			0x0100
81 #define ZINITIX_DND_N_COUNT			0x0122
82 #define ZINITIX_DND_U_COUNT			0x0135
83 
84 #define ZINITIX_RAWDATA_REG			0x0200
85 
86 #define ZINITIX_EEPROM_INFO_REG			0x0018
87 
88 #define ZINITIX_INT_ENABLE_FLAG			0x00f0
89 #define ZINITIX_PERIODICAL_INTERRUPT_INTERVAL	0x00f1
90 
91 #define ZINITIX_BTN_WIDTH			0x016d
92 
93 #define ZINITIX_CHECKSUM_RESULT			0x012c
94 
95 #define ZINITIX_INIT_FLASH			0x01d0
96 #define ZINITIX_WRITE_FLASH			0x01d1
97 #define ZINITIX_READ_FLASH			0x01d2
98 
99 #define ZINITIX_INTERNAL_FLAG_02		0x011e
100 #define ZINITIX_INTERNAL_FLAG_03		0x011f
101 
102 #define ZINITIX_I2C_CHECKSUM_WCNT		0x016a
103 #define ZINITIX_I2C_CHECKSUM_RESULT		0x016c
104 
105 /* Interrupt & status register flags */
106 
107 #define BIT_PT_CNT_CHANGE			BIT(0)
108 #define BIT_DOWN				BIT(1)
109 #define BIT_MOVE				BIT(2)
110 #define BIT_UP					BIT(3)
111 #define BIT_PALM				BIT(4)
112 #define BIT_PALM_REJECT				BIT(5)
113 #define BIT_RESERVED_0				BIT(6)
114 #define BIT_RESERVED_1				BIT(7)
115 #define BIT_WEIGHT_CHANGE			BIT(8)
116 #define BIT_PT_NO_CHANGE			BIT(9)
117 #define BIT_REJECT				BIT(10)
118 #define BIT_PT_EXIST				BIT(11)
119 #define BIT_RESERVED_2				BIT(12)
120 #define BIT_ERROR				BIT(13)
121 #define BIT_DEBUG				BIT(14)
122 #define BIT_ICON_EVENT				BIT(15)
123 
124 #define SUB_BIT_EXIST				BIT(0)
125 #define SUB_BIT_DOWN				BIT(1)
126 #define SUB_BIT_MOVE				BIT(2)
127 #define SUB_BIT_UP				BIT(3)
128 #define SUB_BIT_UPDATE				BIT(4)
129 #define SUB_BIT_WAIT				BIT(5)
130 
131 #define DEFAULT_TOUCH_POINT_MODE		2
132 #define MAX_SUPPORTED_FINGER_NUM		5
133 #define MAX_SUPPORTED_BUTTON_NUM		8
134 
135 #define CHIP_ON_DELAY				15 // ms
136 #define FIRMWARE_ON_DELAY			40 // ms
137 
138 struct point_coord {
139 	__le16	x;
140 	__le16	y;
141 	u8	width;
142 	u8	sub_status;
143 	// currently unused, but needed as padding:
144 	u8	minor_width;
145 	u8	angle;
146 };
147 
148 struct touch_event {
149 	__le16	status;
150 	u8	finger_mask;
151 	u8	time_stamp;
152 	struct point_coord point_coord[MAX_SUPPORTED_FINGER_NUM];
153 };
154 
155 struct bt541_ts_data {
156 	struct i2c_client *client;
157 	struct input_dev *input_dev;
158 	struct touchscreen_properties prop;
159 	struct regulator_bulk_data supplies[2];
160 	u32 zinitix_mode;
161 	u32 keycodes[MAX_SUPPORTED_BUTTON_NUM];
162 	int num_keycodes;
163 	bool have_versioninfo;
164 	u16 chip_revision;
165 	u16 firmware_version;
166 	u16 regdata_version;
167 	u16 icon_status_reg;
168 };
169 
zinitix_read_data(struct i2c_client * client,u16 reg,void * values,size_t length)170 static int zinitix_read_data(struct i2c_client *client,
171 			     u16 reg, void *values, size_t length)
172 {
173 	__le16 reg_le = cpu_to_le16(reg);
174 	int ret;
175 
176 	/* A single i2c_transfer() transaction does not work here. */
177 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
178 	if (ret != sizeof(reg_le))
179 		return ret < 0 ? ret : -EIO;
180 
181 	ret = i2c_master_recv(client, (u8 *)values, length);
182 	if (ret != length)
183 		return ret < 0 ? ret : -EIO;
184 
185 	return 0;
186 }
187 
zinitix_write_u16(struct i2c_client * client,u16 reg,u16 value)188 static int zinitix_write_u16(struct i2c_client *client, u16 reg, u16 value)
189 {
190 	__le16 packet[2] = {cpu_to_le16(reg), cpu_to_le16(value)};
191 	int ret;
192 
193 	ret = i2c_master_send(client, (u8 *)packet, sizeof(packet));
194 	if (ret != sizeof(packet))
195 		return ret < 0 ? ret : -EIO;
196 
197 	return 0;
198 }
199 
zinitix_write_cmd(struct i2c_client * client,u16 reg)200 static int zinitix_write_cmd(struct i2c_client *client, u16 reg)
201 {
202 	__le16 reg_le = cpu_to_le16(reg);
203 	int ret;
204 
205 	ret = i2c_master_send(client, (u8 *)&reg_le, sizeof(reg_le));
206 	if (ret != sizeof(reg_le))
207 		return ret < 0 ? ret : -EIO;
208 
209 	return 0;
210 }
211 
zinitix_get_u16_reg(struct bt541_ts_data * bt541,u16 vreg)212 static u16 zinitix_get_u16_reg(struct bt541_ts_data *bt541, u16 vreg)
213 {
214 	struct i2c_client *client = bt541->client;
215 	int error;
216 	__le16 val;
217 
218 	error = zinitix_read_data(client, vreg, (void *)&val, 2);
219 	if (error)
220 		return U8_MAX;
221 
222 	return le16_to_cpu(val);
223 }
224 
zinitix_init_touch(struct bt541_ts_data * bt541)225 static int zinitix_init_touch(struct bt541_ts_data *bt541)
226 {
227 	struct i2c_client *client = bt541->client;
228 	int i;
229 	int error;
230 	u16 int_flags;
231 
232 	error = zinitix_write_cmd(client, ZINITIX_SWRESET_CMD);
233 	if (error) {
234 		dev_err(&client->dev, "Failed to write reset command\n");
235 		return error;
236 	}
237 
238 	/*
239 	 * Read and cache the chip revision and firmware version the first time
240 	 * we get here.
241 	 */
242 	if (!bt541->have_versioninfo) {
243 		bt541->chip_revision = zinitix_get_u16_reg(bt541,
244 						ZINITIX_CHIP_REVISION);
245 		bt541->firmware_version = zinitix_get_u16_reg(bt541,
246 						ZINITIX_FIRMWARE_VERSION);
247 		bt541->regdata_version = zinitix_get_u16_reg(bt541,
248 						ZINITIX_DATA_VERSION_REG);
249 		bt541->have_versioninfo = true;
250 
251 		dev_dbg(&client->dev,
252 			"chip revision %04x firmware version %04x regdata version %04x\n",
253 			bt541->chip_revision, bt541->firmware_version,
254 			bt541->regdata_version);
255 
256 		/*
257 		 * Determine the "icon" status register which varies by the
258 		 * chip.
259 		 */
260 		switch (bt541->chip_revision & ZINITIX_CHIP_BTX0X_MASK) {
261 		case ZINITIX_CHIP_BT4X2:
262 			bt541->icon_status_reg = ZINITIX_BT4X2_ICON_STATUS_REG;
263 			break;
264 
265 		case ZINITIX_CHIP_BT4X3:
266 			bt541->icon_status_reg = ZINITIX_BT4X3_ICON_STATUS_REG;
267 			break;
268 
269 		case ZINITIX_CHIP_BT4X4:
270 			bt541->icon_status_reg = ZINITIX_BT4X4_ICON_STATUS_REG;
271 			break;
272 
273 		default:
274 			bt541->icon_status_reg = ZINITIX_BT5XX_ICON_STATUS_REG;
275 			break;
276 		}
277 	}
278 
279 	error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG, 0x0);
280 	if (error) {
281 		dev_err(&client->dev,
282 			"Failed to reset interrupt enable flag\n");
283 		return error;
284 	}
285 
286 	/* initialize */
287 	error = zinitix_write_u16(client, ZINITIX_X_RESOLUTION,
288 				  bt541->prop.max_x);
289 	if (error)
290 		return error;
291 
292 	error = zinitix_write_u16(client, ZINITIX_Y_RESOLUTION,
293 				  bt541->prop.max_y);
294 	if (error)
295 		return error;
296 
297 	error = zinitix_write_u16(client, ZINITIX_SUPPORTED_FINGER_NUM,
298 				  MAX_SUPPORTED_FINGER_NUM);
299 	if (error)
300 		return error;
301 
302 	error = zinitix_write_u16(client, ZINITIX_BUTTON_SUPPORTED_NUM,
303 				  bt541->num_keycodes);
304 	if (error)
305 		return error;
306 
307 	error = zinitix_write_u16(client, ZINITIX_INITIAL_TOUCH_MODE,
308 				  bt541->zinitix_mode);
309 	if (error)
310 		return error;
311 
312 	error = zinitix_write_u16(client, ZINITIX_TOUCH_MODE,
313 				  bt541->zinitix_mode);
314 	if (error)
315 		return error;
316 
317 	int_flags = BIT_PT_CNT_CHANGE | BIT_DOWN | BIT_MOVE | BIT_UP;
318 	if (bt541->num_keycodes)
319 		int_flags |= BIT_ICON_EVENT;
320 
321 	error = zinitix_write_u16(client, ZINITIX_INT_ENABLE_FLAG, int_flags);
322 	if (error)
323 		return error;
324 
325 	/* clear queue */
326 	for (i = 0; i < 10; i++) {
327 		zinitix_write_cmd(client, ZINITIX_CLEAR_INT_STATUS_CMD);
328 		udelay(10);
329 	}
330 
331 	return 0;
332 }
333 
zinitix_init_regulators(struct bt541_ts_data * bt541)334 static int zinitix_init_regulators(struct bt541_ts_data *bt541)
335 {
336 	struct device *dev = &bt541->client->dev;
337 	int error;
338 
339 	/*
340 	 * Some older device trees have erroneous names for the regulators,
341 	 * so check if "vddo" is present and in that case use these names.
342 	 * Else use the proper supply names on the component.
343 	 */
344 	if (of_property_present(dev->of_node, "vddo-supply")) {
345 		bt541->supplies[0].supply = "vdd";
346 		bt541->supplies[1].supply = "vddo";
347 	} else {
348 		/* Else use the proper supply names */
349 		bt541->supplies[0].supply = "vcca";
350 		bt541->supplies[1].supply = "vdd";
351 	}
352 	error = devm_regulator_bulk_get(dev,
353 					ARRAY_SIZE(bt541->supplies),
354 					bt541->supplies);
355 	if (error < 0) {
356 		dev_err(dev, "Failed to get regulators: %d\n", error);
357 		return error;
358 	}
359 
360 	return 0;
361 }
362 
zinitix_send_power_on_sequence(struct bt541_ts_data * bt541)363 static int zinitix_send_power_on_sequence(struct bt541_ts_data *bt541)
364 {
365 	int error;
366 	struct i2c_client *client = bt541->client;
367 
368 	error = zinitix_write_u16(client, 0xc000, 0x0001);
369 	if (error) {
370 		dev_err(&client->dev,
371 			"Failed to send power sequence(vendor cmd enable)\n");
372 		return error;
373 	}
374 	udelay(10);
375 
376 	error = zinitix_write_cmd(client, 0xc004);
377 	if (error) {
378 		dev_err(&client->dev,
379 			"Failed to send power sequence (intn clear)\n");
380 		return error;
381 	}
382 	udelay(10);
383 
384 	error = zinitix_write_u16(client, 0xc002, 0x0001);
385 	if (error) {
386 		dev_err(&client->dev,
387 			"Failed to send power sequence (nvm init)\n");
388 		return error;
389 	}
390 	mdelay(2);
391 
392 	error = zinitix_write_u16(client, 0xc001, 0x0001);
393 	if (error) {
394 		dev_err(&client->dev,
395 			"Failed to send power sequence (program start)\n");
396 		return error;
397 	}
398 	msleep(FIRMWARE_ON_DELAY);
399 
400 	return 0;
401 }
402 
zinitix_report_finger(struct bt541_ts_data * bt541,int slot,const struct point_coord * p)403 static void zinitix_report_finger(struct bt541_ts_data *bt541, int slot,
404 				  const struct point_coord *p)
405 {
406 	u16 x, y;
407 
408 	if (unlikely(!(p->sub_status &
409 		       (SUB_BIT_UP | SUB_BIT_DOWN | SUB_BIT_MOVE)))) {
410 		dev_dbg(&bt541->client->dev, "unknown finger event %#02x\n",
411 			p->sub_status);
412 		return;
413 	}
414 
415 	x = le16_to_cpu(p->x);
416 	y = le16_to_cpu(p->y);
417 
418 	input_mt_slot(bt541->input_dev, slot);
419 	if (input_mt_report_slot_state(bt541->input_dev, MT_TOOL_FINGER,
420 				       !(p->sub_status & SUB_BIT_UP))) {
421 		touchscreen_report_pos(bt541->input_dev,
422 				       &bt541->prop, x, y, true);
423 		input_report_abs(bt541->input_dev,
424 				 ABS_MT_TOUCH_MAJOR, p->width);
425 		dev_dbg(&bt541->client->dev, "finger %d %s (%u, %u)\n",
426 			slot, p->sub_status & SUB_BIT_DOWN ? "down" : "move",
427 			x, y);
428 	} else {
429 		dev_dbg(&bt541->client->dev, "finger %d up (%u, %u)\n",
430 			slot, x, y);
431 	}
432 }
433 
zinitix_report_keys(struct bt541_ts_data * bt541,u16 icon_events)434 static void zinitix_report_keys(struct bt541_ts_data *bt541, u16 icon_events)
435 {
436 	int i;
437 
438 	for (i = 0; i < bt541->num_keycodes; i++)
439 		input_report_key(bt541->input_dev,
440 				 bt541->keycodes[i], icon_events & BIT(i));
441 }
442 
zinitix_ts_irq_handler(int irq,void * bt541_handler)443 static irqreturn_t zinitix_ts_irq_handler(int irq, void *bt541_handler)
444 {
445 	struct bt541_ts_data *bt541 = bt541_handler;
446 	struct i2c_client *client = bt541->client;
447 	struct touch_event touch_event;
448 	unsigned long finger_mask;
449 	__le16 icon_events;
450 	int error;
451 	int i;
452 
453 	memset(&touch_event, 0, sizeof(struct touch_event));
454 
455 	error = zinitix_read_data(bt541->client, ZINITIX_POINT_STATUS_REG,
456 				  &touch_event, sizeof(struct touch_event));
457 	if (error) {
458 		dev_err(&client->dev, "Failed to read in touchpoint struct\n");
459 		goto out;
460 	}
461 
462 	if (le16_to_cpu(touch_event.status) & BIT_ICON_EVENT) {
463 		error = zinitix_read_data(bt541->client, bt541->icon_status_reg,
464 					  &icon_events, sizeof(icon_events));
465 		if (error) {
466 			dev_err(&client->dev, "Failed to read icon events\n");
467 			goto out;
468 		}
469 
470 		zinitix_report_keys(bt541, le16_to_cpu(icon_events));
471 	}
472 
473 	finger_mask = touch_event.finger_mask;
474 	for_each_set_bit(i, &finger_mask, MAX_SUPPORTED_FINGER_NUM) {
475 		const struct point_coord *p = &touch_event.point_coord[i];
476 
477 		/* Only process contacts that are actually reported */
478 		if (p->sub_status & SUB_BIT_EXIST)
479 			zinitix_report_finger(bt541, i, p);
480 	}
481 
482 	input_mt_sync_frame(bt541->input_dev);
483 	input_sync(bt541->input_dev);
484 
485 out:
486 	zinitix_write_cmd(bt541->client, ZINITIX_CLEAR_INT_STATUS_CMD);
487 	return IRQ_HANDLED;
488 }
489 
zinitix_start(struct bt541_ts_data * bt541)490 static int zinitix_start(struct bt541_ts_data *bt541)
491 {
492 	int error;
493 
494 	error = regulator_bulk_enable(ARRAY_SIZE(bt541->supplies),
495 				      bt541->supplies);
496 	if (error) {
497 		dev_err(&bt541->client->dev,
498 			"Failed to enable regulators: %d\n", error);
499 		return error;
500 	}
501 
502 	msleep(CHIP_ON_DELAY);
503 
504 	error = zinitix_send_power_on_sequence(bt541);
505 	if (error) {
506 		dev_err(&bt541->client->dev,
507 			"Error while sending power-on sequence: %d\n", error);
508 		return error;
509 	}
510 
511 	error = zinitix_init_touch(bt541);
512 	if (error) {
513 		dev_err(&bt541->client->dev,
514 			"Error while configuring touch IC\n");
515 		return error;
516 	}
517 
518 	enable_irq(bt541->client->irq);
519 
520 	return 0;
521 }
522 
zinitix_stop(struct bt541_ts_data * bt541)523 static int zinitix_stop(struct bt541_ts_data *bt541)
524 {
525 	int error;
526 
527 	disable_irq(bt541->client->irq);
528 
529 	error = regulator_bulk_disable(ARRAY_SIZE(bt541->supplies),
530 				       bt541->supplies);
531 	if (error) {
532 		dev_err(&bt541->client->dev,
533 			"Failed to disable regulators: %d\n", error);
534 		return error;
535 	}
536 
537 	return 0;
538 }
539 
zinitix_input_open(struct input_dev * dev)540 static int zinitix_input_open(struct input_dev *dev)
541 {
542 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
543 
544 	return zinitix_start(bt541);
545 }
546 
zinitix_input_close(struct input_dev * dev)547 static void zinitix_input_close(struct input_dev *dev)
548 {
549 	struct bt541_ts_data *bt541 = input_get_drvdata(dev);
550 
551 	zinitix_stop(bt541);
552 }
553 
zinitix_init_input_dev(struct bt541_ts_data * bt541)554 static int zinitix_init_input_dev(struct bt541_ts_data *bt541)
555 {
556 	struct input_dev *input_dev;
557 	int error;
558 	int i;
559 
560 	input_dev = devm_input_allocate_device(&bt541->client->dev);
561 	if (!input_dev) {
562 		dev_err(&bt541->client->dev,
563 			"Failed to allocate input device.");
564 		return -ENOMEM;
565 	}
566 
567 	input_set_drvdata(input_dev, bt541);
568 	bt541->input_dev = input_dev;
569 
570 	input_dev->name = "Zinitix Capacitive TouchScreen";
571 	input_dev->phys = "input/ts";
572 	input_dev->id.bustype = BUS_I2C;
573 	input_dev->open = zinitix_input_open;
574 	input_dev->close = zinitix_input_close;
575 
576 	if (bt541->num_keycodes) {
577 		input_dev->keycode = bt541->keycodes;
578 		input_dev->keycodemax = bt541->num_keycodes;
579 		input_dev->keycodesize = sizeof(bt541->keycodes[0]);
580 		for (i = 0; i < bt541->num_keycodes; i++)
581 			input_set_capability(input_dev, EV_KEY, bt541->keycodes[i]);
582 	}
583 
584 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
585 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
586 	input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, 255, 0, 0);
587 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
588 
589 	touchscreen_parse_properties(input_dev, true, &bt541->prop);
590 	if (!bt541->prop.max_x || !bt541->prop.max_y) {
591 		dev_err(&bt541->client->dev,
592 			"Touchscreen-size-x and/or touchscreen-size-y not set in dts\n");
593 		return -EINVAL;
594 	}
595 
596 	error = input_mt_init_slots(input_dev, MAX_SUPPORTED_FINGER_NUM,
597 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
598 	if (error) {
599 		dev_err(&bt541->client->dev,
600 			"Failed to initialize MT slots: %d", error);
601 		return error;
602 	}
603 
604 	error = input_register_device(input_dev);
605 	if (error) {
606 		dev_err(&bt541->client->dev,
607 			"Failed to register input device: %d", error);
608 		return error;
609 	}
610 
611 	return 0;
612 }
613 
zinitix_ts_probe(struct i2c_client * client)614 static int zinitix_ts_probe(struct i2c_client *client)
615 {
616 	struct bt541_ts_data *bt541;
617 	int error;
618 
619 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
620 		dev_err(&client->dev,
621 			"Failed to assert adapter's support for plain I2C.\n");
622 		return -ENXIO;
623 	}
624 
625 	bt541 = devm_kzalloc(&client->dev, sizeof(*bt541), GFP_KERNEL);
626 	if (!bt541)
627 		return -ENOMEM;
628 
629 	bt541->client = client;
630 	i2c_set_clientdata(client, bt541);
631 
632 	error = zinitix_init_regulators(bt541);
633 	if (error) {
634 		dev_err(&client->dev,
635 			"Failed to initialize regulators: %d\n", error);
636 		return error;
637 	}
638 
639 	error = devm_request_threaded_irq(&client->dev, client->irq,
640 					  NULL, zinitix_ts_irq_handler,
641 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
642 					  client->name, bt541);
643 	if (error) {
644 		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
645 		return error;
646 	}
647 
648 	if (device_property_present(&client->dev, "linux,keycodes")) {
649 		bt541->num_keycodes = device_property_count_u32(&client->dev,
650 								"linux,keycodes");
651 		if (bt541->num_keycodes < 0) {
652 			dev_err(&client->dev, "Failed to count keys (%d)\n",
653 				bt541->num_keycodes);
654 			return bt541->num_keycodes;
655 		} else if (bt541->num_keycodes > ARRAY_SIZE(bt541->keycodes)) {
656 			dev_err(&client->dev, "Too many keys defined (%d)\n",
657 				bt541->num_keycodes);
658 			return -EINVAL;
659 		}
660 
661 		error = device_property_read_u32_array(&client->dev,
662 						       "linux,keycodes",
663 						       bt541->keycodes,
664 						       bt541->num_keycodes);
665 		if (error) {
666 			dev_err(&client->dev,
667 				"Unable to parse \"linux,keycodes\" property: %d\n",
668 				error);
669 			return error;
670 		}
671 	}
672 
673 	error = zinitix_init_input_dev(bt541);
674 	if (error) {
675 		dev_err(&client->dev,
676 			"Failed to initialize input device: %d\n", error);
677 		return error;
678 	}
679 
680 	error = device_property_read_u32(&client->dev, "zinitix,mode",
681 					 &bt541->zinitix_mode);
682 	if (error < 0) {
683 		/* fall back to mode 2 */
684 		bt541->zinitix_mode = DEFAULT_TOUCH_POINT_MODE;
685 	}
686 
687 	if (bt541->zinitix_mode != 2) {
688 		/*
689 		 * If there are devices that don't support mode 2, support
690 		 * for other modes (0, 1) will be needed.
691 		 */
692 		dev_err(&client->dev,
693 			"Malformed zinitix,mode property, must be 2 (supplied: %d)\n",
694 			bt541->zinitix_mode);
695 		return -EINVAL;
696 	}
697 
698 	return 0;
699 }
700 
zinitix_suspend(struct device * dev)701 static int zinitix_suspend(struct device *dev)
702 {
703 	struct i2c_client *client = to_i2c_client(dev);
704 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
705 
706 	mutex_lock(&bt541->input_dev->mutex);
707 
708 	if (input_device_enabled(bt541->input_dev))
709 		zinitix_stop(bt541);
710 
711 	mutex_unlock(&bt541->input_dev->mutex);
712 
713 	return 0;
714 }
715 
zinitix_resume(struct device * dev)716 static int zinitix_resume(struct device *dev)
717 {
718 	struct i2c_client *client = to_i2c_client(dev);
719 	struct bt541_ts_data *bt541 = i2c_get_clientdata(client);
720 	int ret = 0;
721 
722 	mutex_lock(&bt541->input_dev->mutex);
723 
724 	if (input_device_enabled(bt541->input_dev))
725 		ret = zinitix_start(bt541);
726 
727 	mutex_unlock(&bt541->input_dev->mutex);
728 
729 	return ret;
730 }
731 
732 static DEFINE_SIMPLE_DEV_PM_OPS(zinitix_pm_ops, zinitix_suspend, zinitix_resume);
733 
734 #ifdef CONFIG_OF
735 static const struct of_device_id zinitix_of_match[] = {
736 	{ .compatible = "zinitix,bt402" },
737 	{ .compatible = "zinitix,bt403" },
738 	{ .compatible = "zinitix,bt404" },
739 	{ .compatible = "zinitix,bt412" },
740 	{ .compatible = "zinitix,bt413" },
741 	{ .compatible = "zinitix,bt431" },
742 	{ .compatible = "zinitix,bt432" },
743 	{ .compatible = "zinitix,bt531" },
744 	{ .compatible = "zinitix,bt532" },
745 	{ .compatible = "zinitix,bt538" },
746 	{ .compatible = "zinitix,bt541" },
747 	{ .compatible = "zinitix,bt548" },
748 	{ .compatible = "zinitix,bt554" },
749 	{ .compatible = "zinitix,at100" },
750 	{ }
751 };
752 MODULE_DEVICE_TABLE(of, zinitix_of_match);
753 #endif
754 
755 static struct i2c_driver zinitix_ts_driver = {
756 	.probe = zinitix_ts_probe,
757 	.driver = {
758 		.name = "Zinitix-TS",
759 		.pm = pm_sleep_ptr(&zinitix_pm_ops),
760 		.of_match_table = of_match_ptr(zinitix_of_match),
761 	},
762 };
763 module_i2c_driver(zinitix_ts_driver);
764 
765 MODULE_AUTHOR("Michael Srba <Michael.Srba@seznam.cz>");
766 MODULE_DESCRIPTION("Zinitix touchscreen driver");
767 MODULE_LICENSE("GPL v2");
768