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