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