xref: /linux/drivers/input/touchscreen/raydium_i2c_ts.c (revision ffd01c3bcc1af4d8c3e7949152af0d9fe3d1fda5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Raydium touchscreen I2C driver.
4  *
5  * Copyright (C) 2012-2014, Raydium Semiconductor Corporation.
6  *
7  * Raydium reserves the right to make changes without further notice
8  * to the materials described herein. Raydium does not assume any
9  * liability arising out of the application described herein.
10  *
11  * Contact Raydium Semiconductor Corporation at www.rad-ic.com
12  */
13 
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/input.h>
20 #include <linux/input/mt.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/pm_wakeirq.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/unaligned.h>
28 
29 /* Slave I2C mode */
30 #define RM_BOOT_BLDR		0x02
31 #define RM_BOOT_MAIN		0x03
32 
33 /* I2C bootoloader commands */
34 #define RM_CMD_BOOT_PAGE_WRT	0x0B		/* send bl page write */
35 #define RM_CMD_BOOT_WRT		0x11		/* send bl write */
36 #define RM_CMD_BOOT_ACK		0x22		/* send ack*/
37 #define RM_CMD_BOOT_CHK		0x33		/* send data check */
38 #define RM_CMD_BOOT_READ	0x44		/* send wait bl data ready*/
39 
40 #define RM_BOOT_RDY		0xFF		/* bl data ready */
41 #define RM_BOOT_CMD_READHWID	0x0E		/* read hwid */
42 
43 /* I2C main commands */
44 #define RM_CMD_QUERY_BANK	0x2B
45 #define RM_CMD_DATA_BANK	0x4D
46 #define RM_CMD_ENTER_SLEEP	0x4E
47 #define RM_CMD_BANK_SWITCH	0xAA
48 
49 #define RM_RESET_MSG_ADDR	0x40000004
50 
51 #define RM_MAX_READ_SIZE	56
52 #define RM_PACKET_CRC_SIZE	2
53 
54 /* Touch relative info */
55 #define RM_MAX_RETRIES		3
56 #define RM_RETRY_DELAY_MS	20
57 #define RM_MAX_TOUCH_NUM	10
58 #define RM_BOOT_DELAY_MS	100
59 
60 /* Offsets in contact data */
61 #define RM_CONTACT_STATE_POS	0
62 #define RM_CONTACT_X_POS	1
63 #define RM_CONTACT_Y_POS	3
64 #define RM_CONTACT_PRESSURE_POS	5
65 #define RM_CONTACT_WIDTH_X_POS	6
66 #define RM_CONTACT_WIDTH_Y_POS	7
67 
68 /* Bootloader relative info */
69 #define RM_BL_WRT_CMD_SIZE	3	/* bl flash wrt cmd size */
70 #define RM_BL_WRT_PKG_SIZE	32	/* bl wrt pkg size */
71 #define RM_BL_WRT_LEN		(RM_BL_WRT_PKG_SIZE + RM_BL_WRT_CMD_SIZE)
72 #define RM_FW_PAGE_SIZE		128
73 #define RM_MAX_FW_RETRIES	30
74 #define RM_MAX_FW_SIZE		0xD000
75 
76 #define RM_POWERON_DELAY_USEC	500
77 #define RM_RESET_DELAY_MSEC	50
78 
79 enum raydium_bl_cmd {
80 	BL_HEADER = 0,
81 	BL_PAGE_STR,
82 	BL_PKG_IDX,
83 	BL_DATA_STR,
84 };
85 
86 enum raydium_bl_ack {
87 	RAYDIUM_ACK_NULL = 0,
88 	RAYDIUM_WAIT_READY,
89 	RAYDIUM_PATH_READY,
90 };
91 
92 enum raydium_boot_mode {
93 	RAYDIUM_TS_MAIN = 0,
94 	RAYDIUM_TS_BLDR,
95 };
96 
97 /* Response to RM_CMD_DATA_BANK request */
98 struct raydium_data_info {
99 	__le32 data_bank_addr;
100 	u8 pkg_size;
101 	u8 tp_info_size;
102 };
103 
104 struct raydium_info {
105 	__le32 hw_ver;		/*device version */
106 	u8 main_ver;
107 	u8 sub_ver;
108 	__le16 ft_ver;		/* test version */
109 	u8 x_num;
110 	u8 y_num;
111 	__le16 x_max;
112 	__le16 y_max;
113 	u8 x_res;		/* units/mm */
114 	u8 y_res;		/* units/mm */
115 };
116 
117 /* struct raydium_data - represents state of Raydium touchscreen device */
118 struct raydium_data {
119 	struct i2c_client *client;
120 	struct input_dev *input;
121 
122 	struct regulator *avdd;
123 	struct regulator *vccio;
124 	struct gpio_desc *reset_gpio;
125 
126 	struct raydium_info info;
127 
128 	struct mutex sysfs_mutex;
129 
130 	u8 *report_data;
131 
132 	u32 data_bank_addr;
133 	u8 report_size;
134 	u8 contact_size;
135 	u8 pkg_size;
136 
137 	enum raydium_boot_mode boot_mode;
138 };
139 
140 /*
141  * Header to be sent for RM_CMD_BANK_SWITCH command. This is used by
142  * raydium_i2c_{read|send} below.
143  */
144 struct __packed raydium_bank_switch_header {
145 	u8 cmd;
146 	__be32 be_addr;
147 };
148 
149 static int raydium_i2c_xfer(struct i2c_client *client, u32 addr,
150 			    struct i2c_msg *xfer, size_t xfer_count)
151 {
152 	int ret;
153 	/*
154 	 * If address is greater than 255, then RM_CMD_BANK_SWITCH needs to be
155 	 * sent first. Else, skip the header i.e. xfer[0].
156 	 */
157 	int xfer_start_idx = (addr > 0xff) ? 0 : 1;
158 	xfer_count -= xfer_start_idx;
159 
160 	ret = i2c_transfer(client->adapter, &xfer[xfer_start_idx], xfer_count);
161 	if (likely(ret == xfer_count))
162 		return 0;
163 
164 	return ret < 0 ? ret : -EIO;
165 }
166 
167 static int raydium_i2c_send(struct i2c_client *client,
168 			    u32 addr, const void *data, size_t len)
169 {
170 	int tries = 0;
171 	int error;
172 	u8 reg_addr = addr & 0xff;
173 
174 	u8 *tx_buf __free(kfree) = kmalloc(len + 1, GFP_KERNEL);
175 	if (!tx_buf)
176 		return -ENOMEM;
177 
178 	tx_buf[0] = reg_addr;
179 	memcpy(tx_buf + 1, data, len);
180 
181 	do {
182 		struct raydium_bank_switch_header header = {
183 			.cmd = RM_CMD_BANK_SWITCH,
184 			.be_addr = cpu_to_be32(addr),
185 		};
186 
187 		/*
188 		 * Perform as a single i2c_transfer transaction to ensure that
189 		 * no other I2C transactions are initiated on the bus to any
190 		 * other device in between. Initiating transacations to other
191 		 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
192 		 * issues. This is also why regmap infrastructure cannot be used
193 		 * for this driver. Regmap handles page(bank) switch and reads
194 		 * as separate i2c_transfer() operations. This can result in
195 		 * problems if the Raydium device is on a shared I2C bus.
196 		 */
197 		struct i2c_msg xfer[] = {
198 			{
199 				.addr = client->addr,
200 				.len = sizeof(header),
201 				.buf = (u8 *)&header,
202 			},
203 			{
204 				.addr = client->addr,
205 				.len = len + 1,
206 				.buf = tx_buf,
207 			},
208 		};
209 
210 		error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
211 		if (likely(!error))
212 			return 0;
213 
214 		msleep(RM_RETRY_DELAY_MS);
215 	} while (++tries < RM_MAX_RETRIES);
216 
217 	dev_err(&client->dev, "%s failed: %d\n", __func__, error);
218 	return error;
219 }
220 
221 static int raydium_i2c_read(struct i2c_client *client,
222 			    u32 addr, void *data, size_t len)
223 {
224 	int error;
225 
226 	while (len) {
227 		u8 reg_addr = addr & 0xff;
228 		struct raydium_bank_switch_header header = {
229 			.cmd = RM_CMD_BANK_SWITCH,
230 			.be_addr = cpu_to_be32(addr),
231 		};
232 		size_t xfer_len = min_t(size_t, len, RM_MAX_READ_SIZE);
233 
234 		/*
235 		 * Perform as a single i2c_transfer transaction to ensure that
236 		 * no other I2C transactions are initiated on the bus to any
237 		 * other device in between. Initiating transacations to other
238 		 * devices after RM_CMD_BANK_SWITCH is sent is known to cause
239 		 * issues. This is also why regmap infrastructure cannot be used
240 		 * for this driver. Regmap handles page(bank) switch and writes
241 		 * as separate i2c_transfer() operations. This can result in
242 		 * problems if the Raydium device is on a shared I2C bus.
243 		 */
244 		struct i2c_msg xfer[] = {
245 			{
246 				.addr = client->addr,
247 				.len = sizeof(header),
248 				.buf = (u8 *)&header,
249 			},
250 			{
251 				.addr = client->addr,
252 				.len = 1,
253 				.buf = &reg_addr,
254 			},
255 			{
256 				.addr = client->addr,
257 				.len = xfer_len,
258 				.buf = data,
259 				.flags = I2C_M_RD,
260 			}
261 		};
262 
263 		error = raydium_i2c_xfer(client, addr, xfer, ARRAY_SIZE(xfer));
264 		if (unlikely(error))
265 			return error;
266 
267 		len -= xfer_len;
268 		data += xfer_len;
269 		addr += xfer_len;
270 	}
271 
272 	return 0;
273 }
274 
275 static int raydium_i2c_sw_reset(struct i2c_client *client)
276 {
277 	const u8 soft_rst_cmd = 0x01;
278 	int error;
279 
280 	error = raydium_i2c_send(client, RM_RESET_MSG_ADDR, &soft_rst_cmd,
281 				 sizeof(soft_rst_cmd));
282 	if (error) {
283 		dev_err(&client->dev, "software reset failed: %d\n", error);
284 		return error;
285 	}
286 
287 	msleep(RM_RESET_DELAY_MSEC);
288 
289 	return 0;
290 }
291 
292 static int raydium_i2c_query_ts_bootloader_info(struct raydium_data *ts)
293 {
294 	struct i2c_client *client = ts->client;
295 	static const u8 get_hwid[] = { RM_BOOT_CMD_READHWID,
296 				       0x10, 0xc0, 0x01, 0x00, 0x04, 0x00 };
297 	u8 rbuf[5] = { 0 };
298 	u32 hw_ver;
299 	int error;
300 
301 	error = raydium_i2c_send(client, RM_CMD_BOOT_WRT,
302 				 get_hwid, sizeof(get_hwid));
303 	if (error) {
304 		dev_err(&client->dev, "WRT HWID command failed: %d\n", error);
305 		return error;
306 	}
307 
308 	error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, rbuf, 1);
309 	if (error) {
310 		dev_err(&client->dev, "Ack HWID command failed: %d\n", error);
311 		return error;
312 	}
313 
314 	error = raydium_i2c_read(client, RM_CMD_BOOT_CHK, rbuf, sizeof(rbuf));
315 	if (error) {
316 		dev_err(&client->dev, "Read HWID command failed: %d (%4ph)\n",
317 			error, rbuf + 1);
318 		hw_ver = 0xffffffffUL;
319 	} else {
320 		hw_ver = get_unaligned_be32(rbuf + 1);
321 	}
322 
323 	ts->info.hw_ver = cpu_to_le32(hw_ver);
324 	ts->info.main_ver = 0xff;
325 	ts->info.sub_ver = 0xff;
326 
327 	return error;
328 }
329 
330 static int raydium_i2c_query_ts_info(struct raydium_data *ts)
331 {
332 	struct i2c_client *client = ts->client;
333 	struct raydium_data_info data_info;
334 	__le32 query_bank_addr;
335 
336 	int error, retry_cnt;
337 
338 	for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
339 		error = raydium_i2c_read(client, RM_CMD_DATA_BANK,
340 					 &data_info, sizeof(data_info));
341 		if (error)
342 			continue;
343 
344 		/*
345 		 * Warn user if we already allocated memory for reports and
346 		 * then the size changed (due to firmware update?) and keep
347 		 * old size instead.
348 		 */
349 		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
350 			dev_warn(&client->dev,
351 				 "report size changes, was: %d, new: %d\n",
352 				 ts->pkg_size, data_info.pkg_size);
353 		} else {
354 			ts->pkg_size = data_info.pkg_size;
355 			ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
356 		}
357 
358 		ts->contact_size = data_info.tp_info_size;
359 		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
360 
361 		dev_dbg(&client->dev,
362 			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
363 			ts->data_bank_addr, ts->report_size, ts->contact_size);
364 
365 		error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
366 					 &query_bank_addr,
367 					 sizeof(query_bank_addr));
368 		if (error)
369 			continue;
370 
371 		error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
372 					 &ts->info, sizeof(ts->info));
373 		if (error)
374 			continue;
375 
376 		return 0;
377 	}
378 
379 	dev_err(&client->dev, "failed to query device parameters: %d\n", error);
380 	return error;
381 }
382 
383 static int raydium_i2c_check_fw_status(struct raydium_data *ts)
384 {
385 	struct i2c_client *client = ts->client;
386 	static const u8 bl_ack = 0x62;
387 	static const u8 main_ack = 0x66;
388 	u8 buf[4];
389 	int error;
390 
391 	error = raydium_i2c_read(client, RM_CMD_BOOT_READ, buf, sizeof(buf));
392 	if (!error) {
393 		if (buf[0] == bl_ack)
394 			ts->boot_mode = RAYDIUM_TS_BLDR;
395 		else if (buf[0] == main_ack)
396 			ts->boot_mode = RAYDIUM_TS_MAIN;
397 		return 0;
398 	}
399 
400 	return error;
401 }
402 
403 static int raydium_i2c_initialize(struct raydium_data *ts)
404 {
405 	struct i2c_client *client = ts->client;
406 	int error, retry_cnt;
407 
408 	for (retry_cnt = 0; retry_cnt < RM_MAX_RETRIES; retry_cnt++) {
409 		/* Wait for Hello packet */
410 		msleep(RM_BOOT_DELAY_MS);
411 
412 		error = raydium_i2c_check_fw_status(ts);
413 		if (error) {
414 			dev_err(&client->dev,
415 				"failed to read 'hello' packet: %d\n", error);
416 			continue;
417 		}
418 
419 		if (ts->boot_mode == RAYDIUM_TS_BLDR ||
420 		    ts->boot_mode == RAYDIUM_TS_MAIN) {
421 			break;
422 		}
423 	}
424 
425 	if (error)
426 		ts->boot_mode = RAYDIUM_TS_BLDR;
427 
428 	if (ts->boot_mode == RAYDIUM_TS_BLDR)
429 		raydium_i2c_query_ts_bootloader_info(ts);
430 	else
431 		raydium_i2c_query_ts_info(ts);
432 
433 	return error;
434 }
435 
436 static int raydium_i2c_bl_chk_state(struct i2c_client *client,
437 				    enum raydium_bl_ack state)
438 {
439 	static const u8 ack_ok[] = { 0xFF, 0x39, 0x30, 0x30, 0x54 };
440 	u8 rbuf[sizeof(ack_ok)];
441 	u8 retry;
442 	int error;
443 
444 	for (retry = 0; retry < RM_MAX_FW_RETRIES; retry++) {
445 		switch (state) {
446 		case RAYDIUM_ACK_NULL:
447 			return 0;
448 
449 		case RAYDIUM_WAIT_READY:
450 			error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
451 						 &rbuf[0], 1);
452 			if (!error && rbuf[0] == RM_BOOT_RDY)
453 				return 0;
454 
455 			break;
456 
457 		case RAYDIUM_PATH_READY:
458 			error = raydium_i2c_read(client, RM_CMD_BOOT_CHK,
459 						 rbuf, sizeof(rbuf));
460 			if (!error && !memcmp(rbuf, ack_ok, sizeof(ack_ok)))
461 				return 0;
462 
463 			break;
464 
465 		default:
466 			dev_err(&client->dev, "%s: invalid target state %d\n",
467 				__func__, state);
468 			return -EINVAL;
469 		}
470 
471 		msleep(20);
472 	}
473 
474 	return -ETIMEDOUT;
475 }
476 
477 static int raydium_i2c_write_object(struct i2c_client *client,
478 				    const void *data, size_t len,
479 				    enum raydium_bl_ack state)
480 {
481 	int error;
482 	static const u8 cmd[] = { 0xFF, 0x39 };
483 
484 	error = raydium_i2c_send(client, RM_CMD_BOOT_WRT, data, len);
485 	if (error) {
486 		dev_err(&client->dev, "WRT obj command failed: %d\n",
487 			error);
488 		return error;
489 	}
490 
491 	error = raydium_i2c_send(client, RM_CMD_BOOT_ACK, cmd, sizeof(cmd));
492 	if (error) {
493 		dev_err(&client->dev, "Ack obj command failed: %d\n", error);
494 		return error;
495 	}
496 
497 	error = raydium_i2c_bl_chk_state(client, state);
498 	if (error) {
499 		dev_err(&client->dev, "BL check state failed: %d\n", error);
500 		return error;
501 	}
502 	return 0;
503 }
504 
505 static int raydium_i2c_boot_trigger(struct i2c_client *client)
506 {
507 	static const u8 cmd[7][6] = {
508 		{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0xD7 },
509 		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
510 		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0x00 },
511 		{ 0x08, 0x04, 0x09, 0x00, 0x50, 0xA5 },
512 		{ 0x08, 0x0C, 0x09, 0x00, 0x50, 0x00 },
513 		{ 0x06, 0x01, 0x00, 0x00, 0x00, 0x00 },
514 		{ 0x02, 0xA2, 0x00, 0x00, 0x00, 0x00 },
515 	};
516 	int i;
517 	int error;
518 
519 	for (i = 0; i < 7; i++) {
520 		error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
521 						 RAYDIUM_WAIT_READY);
522 		if (error) {
523 			dev_err(&client->dev,
524 				"boot trigger failed at step %d: %d\n",
525 				i, error);
526 			return error;
527 		}
528 	}
529 
530 	return 0;
531 }
532 
533 static int raydium_i2c_fw_trigger(struct i2c_client *client)
534 {
535 	static const u8 cmd[5][11] = {
536 		{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0xD7, 0, 0, 0 },
537 		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
538 		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
539 		{ 0, 0x09, 0x71, 0x04, 0x09, 0x00, 0x50, 0xA5, 0, 0, 0 },
540 		{ 0, 0x09, 0x71, 0x0C, 0x09, 0x00, 0x50, 0x00, 0, 0, 0 },
541 	};
542 	int i;
543 	int error;
544 
545 	for (i = 0; i < 5; i++) {
546 		error = raydium_i2c_write_object(client, cmd[i], sizeof(cmd[i]),
547 						 RAYDIUM_ACK_NULL);
548 		if (error) {
549 			dev_err(&client->dev,
550 				"fw trigger failed at step %d: %d\n",
551 				i, error);
552 			return error;
553 		}
554 	}
555 
556 	return 0;
557 }
558 
559 static int raydium_i2c_check_path(struct i2c_client *client)
560 {
561 	static const u8 cmd[] = { 0x09, 0x00, 0x09, 0x00, 0x50, 0x10, 0x00 };
562 	int error;
563 
564 	error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
565 					 RAYDIUM_PATH_READY);
566 	if (error) {
567 		dev_err(&client->dev, "check path command failed: %d\n", error);
568 		return error;
569 	}
570 
571 	return 0;
572 }
573 
574 static int raydium_i2c_enter_bl(struct i2c_client *client)
575 {
576 	static const u8 cal_cmd[] = { 0x00, 0x01, 0x52 };
577 	int error;
578 
579 	error = raydium_i2c_write_object(client, cal_cmd, sizeof(cal_cmd),
580 					 RAYDIUM_ACK_NULL);
581 	if (error) {
582 		dev_err(&client->dev, "enter bl command failed: %d\n", error);
583 		return error;
584 	}
585 
586 	msleep(RM_BOOT_DELAY_MS);
587 	return 0;
588 }
589 
590 static int raydium_i2c_leave_bl(struct i2c_client *client)
591 {
592 	static const u8 leave_cmd[] = { 0x05, 0x00 };
593 	int error;
594 
595 	error = raydium_i2c_write_object(client, leave_cmd, sizeof(leave_cmd),
596 					 RAYDIUM_ACK_NULL);
597 	if (error) {
598 		dev_err(&client->dev, "leave bl command failed: %d\n", error);
599 		return error;
600 	}
601 
602 	msleep(RM_BOOT_DELAY_MS);
603 	return 0;
604 }
605 
606 static int raydium_i2c_write_checksum(struct i2c_client *client,
607 				      size_t length, u16 checksum)
608 {
609 	u8 checksum_cmd[] = { 0x00, 0x05, 0x6D, 0x00, 0x00, 0x00, 0x00 };
610 	int error;
611 
612 	put_unaligned_le16(length, &checksum_cmd[3]);
613 	put_unaligned_le16(checksum, &checksum_cmd[5]);
614 
615 	error = raydium_i2c_write_object(client,
616 					 checksum_cmd, sizeof(checksum_cmd),
617 					 RAYDIUM_ACK_NULL);
618 	if (error) {
619 		dev_err(&client->dev, "failed to write checksum: %d\n",
620 			error);
621 		return error;
622 	}
623 
624 	return 0;
625 }
626 
627 static int raydium_i2c_disable_watch_dog(struct i2c_client *client)
628 {
629 	static const u8 cmd[] = { 0x0A, 0xAA };
630 	int error;
631 
632 	error = raydium_i2c_write_object(client, cmd, sizeof(cmd),
633 					 RAYDIUM_WAIT_READY);
634 	if (error) {
635 		dev_err(&client->dev, "disable watchdog command failed: %d\n",
636 			error);
637 		return error;
638 	}
639 
640 	return 0;
641 }
642 
643 static int raydium_i2c_fw_write_page(struct i2c_client *client,
644 				     u16 page_idx, const void *data, size_t len)
645 {
646 	u8 buf[RM_BL_WRT_LEN];
647 	size_t xfer_len;
648 	int error;
649 	int i;
650 
651 	BUILD_BUG_ON((RM_FW_PAGE_SIZE % RM_BL_WRT_PKG_SIZE) != 0);
652 
653 	for (i = 0; i < RM_FW_PAGE_SIZE / RM_BL_WRT_PKG_SIZE; i++) {
654 		buf[BL_HEADER] = RM_CMD_BOOT_PAGE_WRT;
655 		buf[BL_PAGE_STR] = page_idx ? 0xff : 0;
656 		buf[BL_PKG_IDX] = i + 1;
657 
658 		xfer_len = min_t(size_t, len, RM_BL_WRT_PKG_SIZE);
659 		memcpy(&buf[BL_DATA_STR], data, xfer_len);
660 		if (len < RM_BL_WRT_PKG_SIZE)
661 			memset(&buf[BL_DATA_STR + xfer_len], 0xff,
662 				RM_BL_WRT_PKG_SIZE - xfer_len);
663 
664 		error = raydium_i2c_write_object(client, buf, RM_BL_WRT_LEN,
665 						 RAYDIUM_WAIT_READY);
666 		if (error) {
667 			dev_err(&client->dev,
668 				"page write command failed for page %d, chunk %d: %d\n",
669 				page_idx, i, error);
670 			return error;
671 		}
672 
673 		data += xfer_len;
674 		len -= xfer_len;
675 	}
676 
677 	return error;
678 }
679 
680 static u16 raydium_calc_chksum(const u8 *buf, u16 len)
681 {
682 	u16 checksum = 0;
683 	u16 i;
684 
685 	for (i = 0; i < len; i++)
686 		checksum += buf[i];
687 
688 	return checksum;
689 }
690 
691 static int raydium_i2c_do_update_firmware(struct raydium_data *ts,
692 					 const struct firmware *fw)
693 {
694 	struct i2c_client *client = ts->client;
695 	const void *data;
696 	size_t data_len;
697 	size_t len;
698 	int page_nr;
699 	int i;
700 	int error;
701 	u16 fw_checksum;
702 
703 	if (fw->size == 0 || fw->size > RM_MAX_FW_SIZE) {
704 		dev_err(&client->dev, "Invalid firmware length\n");
705 		return -EINVAL;
706 	}
707 
708 	error = raydium_i2c_check_fw_status(ts);
709 	if (error) {
710 		dev_err(&client->dev, "Unable to access IC %d\n", error);
711 		return error;
712 	}
713 
714 	if (ts->boot_mode == RAYDIUM_TS_MAIN) {
715 		for (i = 0; i < RM_MAX_RETRIES; i++) {
716 			error = raydium_i2c_enter_bl(client);
717 			if (!error) {
718 				error = raydium_i2c_check_fw_status(ts);
719 				if (error) {
720 					dev_err(&client->dev,
721 						"unable to access IC: %d\n",
722 						error);
723 					return error;
724 				}
725 
726 				if (ts->boot_mode == RAYDIUM_TS_BLDR)
727 					break;
728 			}
729 		}
730 
731 		if (ts->boot_mode == RAYDIUM_TS_MAIN) {
732 			dev_err(&client->dev,
733 				"failed to jump to boot loader: %d\n",
734 				error);
735 			return -EIO;
736 		}
737 	}
738 
739 	error = raydium_i2c_disable_watch_dog(client);
740 	if (error)
741 		return error;
742 
743 	error = raydium_i2c_check_path(client);
744 	if (error)
745 		return error;
746 
747 	error = raydium_i2c_boot_trigger(client);
748 	if (error) {
749 		dev_err(&client->dev, "send boot trigger fail: %d\n", error);
750 		return error;
751 	}
752 
753 	msleep(RM_BOOT_DELAY_MS);
754 
755 	data = fw->data;
756 	data_len = fw->size;
757 	page_nr = 0;
758 
759 	while (data_len) {
760 		len = min_t(size_t, data_len, RM_FW_PAGE_SIZE);
761 
762 		error = raydium_i2c_fw_write_page(client, page_nr++, data, len);
763 		if (error)
764 			return error;
765 
766 		msleep(20);
767 
768 		data += len;
769 		data_len -= len;
770 	}
771 
772 	error = raydium_i2c_leave_bl(client);
773 	if (error) {
774 		dev_err(&client->dev,
775 			"failed to leave boot loader: %d\n", error);
776 		return error;
777 	}
778 
779 	dev_dbg(&client->dev, "left boot loader mode\n");
780 	msleep(RM_BOOT_DELAY_MS);
781 
782 	error = raydium_i2c_check_fw_status(ts);
783 	if (error) {
784 		dev_err(&client->dev,
785 			"failed to check fw status after write: %d\n",
786 			error);
787 		return error;
788 	}
789 
790 	if (ts->boot_mode != RAYDIUM_TS_MAIN) {
791 		dev_err(&client->dev,
792 			"failed to switch to main fw after writing firmware: %d\n",
793 			error);
794 		return -EINVAL;
795 	}
796 
797 	error = raydium_i2c_fw_trigger(client);
798 	if (error) {
799 		dev_err(&client->dev, "failed to trigger fw: %d\n", error);
800 		return error;
801 	}
802 
803 	fw_checksum = raydium_calc_chksum(fw->data, fw->size);
804 
805 	error = raydium_i2c_write_checksum(client, fw->size, fw_checksum);
806 	if (error)
807 		return error;
808 
809 	return 0;
810 }
811 
812 static int raydium_i2c_fw_update(struct raydium_data *ts)
813 {
814 	struct i2c_client *client = ts->client;
815 	int error;
816 
817 	const char *fw_file __free(kfree) =
818 		kasprintf(GFP_KERNEL, "raydium_%#04x.fw",
819 			  le32_to_cpu(ts->info.hw_ver));
820 	if (!fw_file)
821 		return -ENOMEM;
822 
823 	dev_dbg(&client->dev, "firmware name: %s\n", fw_file);
824 
825 	const struct firmware *fw __free(firmware) = NULL;
826 	error = request_firmware(&fw, fw_file, &client->dev);
827 	if (error) {
828 		dev_err(&client->dev, "Unable to open firmware %s\n", fw_file);
829 		return error;
830 	}
831 
832 	disable_irq(client->irq);
833 
834 	error = raydium_i2c_do_update_firmware(ts, fw);
835 	if (error) {
836 		dev_err(&client->dev, "firmware update failed: %d\n", error);
837 		ts->boot_mode = RAYDIUM_TS_BLDR;
838 		goto out_enable_irq;
839 	}
840 
841 	error = raydium_i2c_initialize(ts);
842 	if (error) {
843 		dev_err(&client->dev,
844 			"failed to initialize device after firmware update: %d\n",
845 			error);
846 		ts->boot_mode = RAYDIUM_TS_BLDR;
847 		goto out_enable_irq;
848 	}
849 
850 	ts->boot_mode = RAYDIUM_TS_MAIN;
851 
852 out_enable_irq:
853 	enable_irq(client->irq);
854 	msleep(100);
855 
856 	return error;
857 }
858 
859 static void raydium_mt_event(struct raydium_data *ts)
860 {
861 	int i;
862 
863 	for (i = 0; i < ts->report_size / ts->contact_size; i++) {
864 		u8 *contact = &ts->report_data[ts->contact_size * i];
865 		bool state = contact[RM_CONTACT_STATE_POS];
866 		u8 wx, wy;
867 
868 		input_mt_slot(ts->input, i);
869 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER, state);
870 
871 		if (!state)
872 			continue;
873 
874 		input_report_abs(ts->input, ABS_MT_POSITION_X,
875 				get_unaligned_le16(&contact[RM_CONTACT_X_POS]));
876 		input_report_abs(ts->input, ABS_MT_POSITION_Y,
877 				get_unaligned_le16(&contact[RM_CONTACT_Y_POS]));
878 		input_report_abs(ts->input, ABS_MT_PRESSURE,
879 				contact[RM_CONTACT_PRESSURE_POS]);
880 
881 		wx = contact[RM_CONTACT_WIDTH_X_POS];
882 		wy = contact[RM_CONTACT_WIDTH_Y_POS];
883 
884 		input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR, max(wx, wy));
885 		input_report_abs(ts->input, ABS_MT_TOUCH_MINOR, min(wx, wy));
886 	}
887 
888 	input_mt_sync_frame(ts->input);
889 	input_sync(ts->input);
890 }
891 
892 static irqreturn_t raydium_i2c_irq(int irq, void *_dev)
893 {
894 	struct raydium_data *ts = _dev;
895 	int error;
896 	u16 fw_crc;
897 	u16 calc_crc;
898 
899 	if (ts->boot_mode != RAYDIUM_TS_MAIN)
900 		goto out;
901 
902 	error = raydium_i2c_read(ts->client, ts->data_bank_addr,
903 				 ts->report_data, ts->pkg_size);
904 	if (error)
905 		goto out;
906 
907 	fw_crc = get_unaligned_le16(&ts->report_data[ts->report_size]);
908 	calc_crc = raydium_calc_chksum(ts->report_data, ts->report_size);
909 	if (unlikely(fw_crc != calc_crc)) {
910 		dev_warn(&ts->client->dev,
911 			 "%s: invalid packet crc %#04x vs %#04x\n",
912 			 __func__, calc_crc, fw_crc);
913 		goto out;
914 	}
915 
916 	raydium_mt_event(ts);
917 
918 out:
919 	return IRQ_HANDLED;
920 }
921 
922 static ssize_t raydium_i2c_fw_ver_show(struct device *dev,
923 				       struct device_attribute *attr, char *buf)
924 {
925 	struct i2c_client *client = to_i2c_client(dev);
926 	struct raydium_data *ts = i2c_get_clientdata(client);
927 
928 	return sprintf(buf, "%d.%d\n", ts->info.main_ver, ts->info.sub_ver);
929 }
930 
931 static ssize_t raydium_i2c_hw_ver_show(struct device *dev,
932 				       struct device_attribute *attr, char *buf)
933 {
934 	struct i2c_client *client = to_i2c_client(dev);
935 	struct raydium_data *ts = i2c_get_clientdata(client);
936 
937 	return sprintf(buf, "%#04x\n", le32_to_cpu(ts->info.hw_ver));
938 }
939 
940 static ssize_t raydium_i2c_boot_mode_show(struct device *dev,
941 					  struct device_attribute *attr,
942 					  char *buf)
943 {
944 	struct i2c_client *client = to_i2c_client(dev);
945 	struct raydium_data *ts = i2c_get_clientdata(client);
946 
947 	return sprintf(buf, "%s\n",
948 		       ts->boot_mode == RAYDIUM_TS_MAIN ?
949 				"Normal" : "Recovery");
950 }
951 
952 static ssize_t raydium_i2c_update_fw_store(struct device *dev,
953 					   struct device_attribute *attr,
954 					   const char *buf, size_t count)
955 {
956 	struct i2c_client *client = to_i2c_client(dev);
957 	struct raydium_data *ts = i2c_get_clientdata(client);
958 	int error;
959 
960 	scoped_guard(mutex_intr, &ts->sysfs_mutex) {
961 		error = raydium_i2c_fw_update(ts);
962 		return error ?: count;
963 	}
964 
965 	return -EINTR;
966 }
967 
968 static ssize_t raydium_i2c_calibrate_store(struct device *dev,
969 					   struct device_attribute *attr,
970 					   const char *buf, size_t count)
971 {
972 	struct i2c_client *client = to_i2c_client(dev);
973 	struct raydium_data *ts = i2c_get_clientdata(client);
974 	static const u8 cal_cmd[] = { 0x00, 0x01, 0x9E };
975 	int error;
976 
977 	scoped_guard(mutex_intr, &ts->sysfs_mutex) {
978 		error = raydium_i2c_write_object(client,
979 						 cal_cmd, sizeof(cal_cmd),
980 						 RAYDIUM_WAIT_READY);
981 		if (error) {
982 			dev_err(&client->dev,
983 				"calibrate command failed: %d\n", error);
984 			return error;
985 		}
986 
987 		return count;
988 	}
989 
990 	return -EINTR;
991 }
992 
993 static DEVICE_ATTR(fw_version, S_IRUGO, raydium_i2c_fw_ver_show, NULL);
994 static DEVICE_ATTR(hw_version, S_IRUGO, raydium_i2c_hw_ver_show, NULL);
995 static DEVICE_ATTR(boot_mode, S_IRUGO, raydium_i2c_boot_mode_show, NULL);
996 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, raydium_i2c_update_fw_store);
997 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, raydium_i2c_calibrate_store);
998 
999 static struct attribute *raydium_i2c_attrs[] = {
1000 	&dev_attr_update_fw.attr,
1001 	&dev_attr_boot_mode.attr,
1002 	&dev_attr_fw_version.attr,
1003 	&dev_attr_hw_version.attr,
1004 	&dev_attr_calibrate.attr,
1005 	NULL
1006 };
1007 ATTRIBUTE_GROUPS(raydium_i2c);
1008 
1009 static int raydium_i2c_power_on(struct raydium_data *ts)
1010 {
1011 	int error;
1012 
1013 	if (!ts->reset_gpio)
1014 		return 0;
1015 
1016 	gpiod_set_value_cansleep(ts->reset_gpio, 1);
1017 
1018 	error = regulator_enable(ts->avdd);
1019 	if (error) {
1020 		dev_err(&ts->client->dev,
1021 			"failed to enable avdd regulator: %d\n", error);
1022 		goto release_reset_gpio;
1023 	}
1024 
1025 	error = regulator_enable(ts->vccio);
1026 	if (error) {
1027 		regulator_disable(ts->avdd);
1028 		dev_err(&ts->client->dev,
1029 			"failed to enable vccio regulator: %d\n", error);
1030 		goto release_reset_gpio;
1031 	}
1032 
1033 	udelay(RM_POWERON_DELAY_USEC);
1034 
1035 release_reset_gpio:
1036 	gpiod_set_value_cansleep(ts->reset_gpio, 0);
1037 
1038 	if (error)
1039 		return error;
1040 
1041 	msleep(RM_RESET_DELAY_MSEC);
1042 
1043 	return 0;
1044 }
1045 
1046 static void raydium_i2c_power_off(void *_data)
1047 {
1048 	struct raydium_data *ts = _data;
1049 
1050 	if (ts->reset_gpio) {
1051 		gpiod_set_value_cansleep(ts->reset_gpio, 1);
1052 		regulator_disable(ts->vccio);
1053 		regulator_disable(ts->avdd);
1054 	}
1055 }
1056 
1057 static int raydium_i2c_probe(struct i2c_client *client)
1058 {
1059 	union i2c_smbus_data dummy;
1060 	struct raydium_data *ts;
1061 	int error;
1062 
1063 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1064 		dev_err(&client->dev,
1065 			"i2c check functionality error (need I2C_FUNC_I2C)\n");
1066 		return -ENXIO;
1067 	}
1068 
1069 	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
1070 	if (!ts)
1071 		return -ENOMEM;
1072 
1073 	mutex_init(&ts->sysfs_mutex);
1074 
1075 	ts->client = client;
1076 	i2c_set_clientdata(client, ts);
1077 
1078 	ts->avdd = devm_regulator_get(&client->dev, "avdd");
1079 	if (IS_ERR(ts->avdd))
1080 		return dev_err_probe(&client->dev, PTR_ERR(ts->avdd),
1081 				     "Failed to get 'avdd' regulator\n");
1082 
1083 	ts->vccio = devm_regulator_get(&client->dev, "vccio");
1084 	if (IS_ERR(ts->vccio))
1085 		return dev_err_probe(&client->dev, PTR_ERR(ts->vccio),
1086 				     "Failed to get 'vccio' regulator\n");
1087 
1088 	ts->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1089 						 GPIOD_OUT_LOW);
1090 	if (IS_ERR(ts->reset_gpio))
1091 		return dev_err_probe(&client->dev, PTR_ERR(ts->reset_gpio),
1092 				     "Failed to get reset gpio\n");
1093 
1094 	error = raydium_i2c_power_on(ts);
1095 	if (error)
1096 		return error;
1097 
1098 	error = devm_add_action_or_reset(&client->dev,
1099 					 raydium_i2c_power_off, ts);
1100 	if (error) {
1101 		dev_err(&client->dev,
1102 			"failed to install power off action: %d\n", error);
1103 		return error;
1104 	}
1105 
1106 	/* Make sure there is something at this address */
1107 	if (i2c_smbus_xfer(client->adapter, client->addr, 0,
1108 			   I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) {
1109 		dev_err(&client->dev, "nothing at this address\n");
1110 		return -ENXIO;
1111 	}
1112 
1113 	error = raydium_i2c_initialize(ts);
1114 	if (error) {
1115 		dev_err(&client->dev, "failed to initialize: %d\n", error);
1116 		return error;
1117 	}
1118 
1119 	ts->report_data = devm_kmalloc(&client->dev,
1120 				       ts->pkg_size, GFP_KERNEL);
1121 	if (!ts->report_data)
1122 		return -ENOMEM;
1123 
1124 	ts->input = devm_input_allocate_device(&client->dev);
1125 	if (!ts->input) {
1126 		dev_err(&client->dev, "Failed to allocate input device\n");
1127 		return -ENOMEM;
1128 	}
1129 
1130 	ts->input->name = "Raydium Touchscreen";
1131 	ts->input->id.bustype = BUS_I2C;
1132 
1133 	input_set_abs_params(ts->input, ABS_MT_POSITION_X,
1134 			     0, le16_to_cpu(ts->info.x_max), 0, 0);
1135 	input_set_abs_params(ts->input, ABS_MT_POSITION_Y,
1136 			     0, le16_to_cpu(ts->info.y_max), 0, 0);
1137 	input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->info.x_res);
1138 	input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->info.y_res);
1139 
1140 	input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0);
1141 	input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0);
1142 
1143 	error = input_mt_init_slots(ts->input, RM_MAX_TOUCH_NUM,
1144 				    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
1145 	if (error) {
1146 		dev_err(&client->dev,
1147 			"failed to initialize MT slots: %d\n", error);
1148 		return error;
1149 	}
1150 
1151 	error = input_register_device(ts->input);
1152 	if (error) {
1153 		dev_err(&client->dev,
1154 			"unable to register input device: %d\n", error);
1155 		return error;
1156 	}
1157 
1158 	error = devm_request_threaded_irq(&client->dev, client->irq,
1159 					  NULL, raydium_i2c_irq,
1160 					  IRQF_ONESHOT, client->name, ts);
1161 	if (error) {
1162 		dev_err(&client->dev, "Failed to register interrupt\n");
1163 		return error;
1164 	}
1165 
1166 	return 0;
1167 }
1168 
1169 static void raydium_enter_sleep(struct i2c_client *client)
1170 {
1171 	static const u8 sleep_cmd[] = { 0x5A, 0xff, 0x00, 0x0f };
1172 	int error;
1173 
1174 	error = raydium_i2c_send(client, RM_CMD_ENTER_SLEEP,
1175 				 sleep_cmd, sizeof(sleep_cmd));
1176 	if (error)
1177 		dev_err(&client->dev,
1178 			"sleep command failed: %d\n", error);
1179 }
1180 
1181 static int raydium_i2c_suspend(struct device *dev)
1182 {
1183 	struct i2c_client *client = to_i2c_client(dev);
1184 	struct raydium_data *ts = i2c_get_clientdata(client);
1185 
1186 	/* Sleep is not available in BLDR recovery mode */
1187 	if (ts->boot_mode != RAYDIUM_TS_MAIN)
1188 		return -EBUSY;
1189 
1190 	disable_irq(client->irq);
1191 
1192 	if (device_may_wakeup(dev)) {
1193 		raydium_enter_sleep(client);
1194 	} else {
1195 		raydium_i2c_power_off(ts);
1196 	}
1197 
1198 	return 0;
1199 }
1200 
1201 static int raydium_i2c_resume(struct device *dev)
1202 {
1203 	struct i2c_client *client = to_i2c_client(dev);
1204 	struct raydium_data *ts = i2c_get_clientdata(client);
1205 
1206 	if (device_may_wakeup(dev)) {
1207 		raydium_i2c_sw_reset(client);
1208 	} else {
1209 		raydium_i2c_power_on(ts);
1210 		raydium_i2c_initialize(ts);
1211 	}
1212 
1213 	enable_irq(client->irq);
1214 
1215 	return 0;
1216 }
1217 
1218 static DEFINE_SIMPLE_DEV_PM_OPS(raydium_i2c_pm_ops,
1219 				raydium_i2c_suspend, raydium_i2c_resume);
1220 
1221 static const struct i2c_device_id raydium_i2c_id[] = {
1222 	{ "raydium_i2c" },
1223 	{ "rm32380" },
1224 	{ /* sentinel */ }
1225 };
1226 MODULE_DEVICE_TABLE(i2c, raydium_i2c_id);
1227 
1228 #ifdef CONFIG_ACPI
1229 static const struct acpi_device_id raydium_acpi_id[] = {
1230 	{ "RAYD0001", 0 },
1231 	{ /* sentinel */ }
1232 };
1233 MODULE_DEVICE_TABLE(acpi, raydium_acpi_id);
1234 #endif
1235 
1236 #ifdef CONFIG_OF
1237 static const struct of_device_id raydium_of_match[] = {
1238 	{ .compatible = "raydium,rm32380", },
1239 	{ /* sentinel */ }
1240 };
1241 MODULE_DEVICE_TABLE(of, raydium_of_match);
1242 #endif
1243 
1244 static struct i2c_driver raydium_i2c_driver = {
1245 	.probe = raydium_i2c_probe,
1246 	.id_table = raydium_i2c_id,
1247 	.driver = {
1248 		.name = "raydium_ts",
1249 		.dev_groups = raydium_i2c_groups,
1250 		.pm = pm_sleep_ptr(&raydium_i2c_pm_ops),
1251 		.acpi_match_table = ACPI_PTR(raydium_acpi_id),
1252 		.of_match_table = of_match_ptr(raydium_of_match),
1253 	},
1254 };
1255 module_i2c_driver(raydium_i2c_driver);
1256 
1257 MODULE_AUTHOR("Raydium");
1258 MODULE_DESCRIPTION("Raydium I2c Touchscreen driver");
1259 MODULE_LICENSE("GPL v2");
1260