1 // SPDX-License-Identifier: GPL-2.0
3 * UCSI driver for Cypress CCGx Type-C controller
5 * Copyright (C) 2017-2018 NVIDIA Corporation. All rights reserved.
6 * Author: Ajay Gupta <ajayg@nvidia.com>
8 * Some code borrowed from drivers/usb/typec/ucsi/ucsi_acpi.c
10 #include <linux/acpi.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
20 #include <asm/unaligned.h>
24 BOOT, /* bootloader */
25 FW1, /* FW partition-1 (contains secondary fw) */
26 FW2, /* FW partition-2 (contains primary fw) */
30 #define CCGX_RAB_DEVICE_MODE 0x0000
31 #define CCGX_RAB_INTR_REG 0x0006
32 #define DEV_INT BIT(0)
33 #define PORT0_INT BIT(1)
34 #define PORT1_INT BIT(2)
35 #define UCSI_READ_INT BIT(7)
36 #define CCGX_RAB_JUMP_TO_BOOT 0x0007
39 #define CCGX_RAB_RESET_REQ 0x0008
41 #define CMD_RESET_I2C 0x0
42 #define CMD_RESET_DEV 0x1
43 #define CCGX_RAB_ENTER_FLASHING 0x000A
44 #define FLASH_ENTER_SIG 'P'
45 #define CCGX_RAB_VALIDATE_FW 0x000B
46 #define CCGX_RAB_FLASH_ROW_RW 0x000C
48 #define FLASH_RD_CMD 0x0
49 #define FLASH_WR_CMD 0x1
50 #define FLASH_FWCT1_WR_CMD 0x2
51 #define FLASH_FWCT2_WR_CMD 0x3
52 #define FLASH_FWCT_SIG_WR_CMD 0x4
53 #define CCGX_RAB_READ_ALL_VER 0x0010
54 #define CCGX_RAB_READ_FW2_VER 0x0020
55 #define CCGX_RAB_UCSI_CONTROL 0x0039
56 #define CCGX_RAB_UCSI_CONTROL_START BIT(0)
57 #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1)
58 #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff))
59 #define REG_FLASH_RW_MEM 0x0200
60 #define DEV_REG_IDX CCGX_RAB_DEVICE_MODE
61 #define CCGX_RAB_PDPORT_ENABLE 0x002C
62 #define PDPORT_1 BIT(0)
63 #define PDPORT_2 BIT(1)
64 #define CCGX_RAB_RESPONSE 0x007E
65 #define ASYNC_EVENT BIT(7)
67 /* CCGx events & async msg codes */
68 #define RESET_COMPLETE 0x80
69 #define EVENT_INDEX RESET_COMPLETE
70 #define PORT_CONNECT_DET 0x84
71 #define PORT_DISCONNECT_DET 0x85
72 #define ROLE_SWAP_COMPELETE 0x87
75 #define CYACD_LINE_SIZE 527
76 #define CCG4_ROW_SIZE 256
77 #define FW1_METADATA_ROW 0x1FF
78 #define FW2_METADATA_ROW 0x1FE
79 #define FW_CFG_TABLE_SIG_SIZE 256
81 static int secondary_fw_min_ver = 41;
83 enum enum_flash_mode {
84 SECONDARY_BL, /* update secondary using bootloader */
85 PRIMARY, /* update primary using secondary */
86 SECONDARY, /* update secondary using primary */
87 FLASH_NOT_NEEDED, /* update not required */
91 static const char * const ccg_fw_names[] = {
98 #define CCG_DEVINFO_FWMODE_SHIFT (0)
99 #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT)
100 #define CCG_DEVINFO_PDPORTS_SHIFT (2)
101 #define CCG_DEVINFO_PDPORTS_MASK (0x3 << CCG_DEVINFO_PDPORTS_SHIFT)
108 struct version_format {
112 #define CCG_VERSION_PATCH(x) ((x) << 16)
113 #define CCG_VERSION(x) ((x) << 24)
114 #define CCG_VERSION_MIN_SHIFT (0)
115 #define CCG_VERSION_MIN_MASK (0xf << CCG_VERSION_MIN_SHIFT)
116 #define CCG_VERSION_MAJ_SHIFT (4)
117 #define CCG_VERSION_MAJ_MASK (0xf << CCG_VERSION_MAJ_SHIFT)
121 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue
122 * of missing interrupt when a device is connected for runtime resume
124 #define CCG_FW_BUILD_NVIDIA (('n' << 8) | 'v')
125 #define CCG_OLD_FW_VERSION (CCG_VERSION(0x31) | CCG_VERSION_PATCH(10))
127 struct version_info {
128 struct version_format base;
129 struct version_format app;
132 struct fw_config_table {
138 struct version_format base;
139 struct version_format app;
140 u8 primary_fw_digest[32];
146 /* CCGx response codes */
150 FLASH_DATA_AVAILABLE = 0x03,
152 FLASH_UPDATE_FAIL = 0x07,
155 CMD_NOT_SUPPORT = 0x0A,
156 TRANSACTION_FAIL = 0x0C,
162 #define CCG_EVENT_MAX (EVENT_INDEX + 43)
168 u32 delay; /* ms delay for cmd timeout */
180 struct i2c_client *client;
181 struct ccg_dev_info info;
182 /* version info for boot, primary and secondary */
183 struct version_info version[FW2 + 1];
185 /* CCG HPI communication flags */
187 #define RESET_PENDING 0
188 #define DEV_CMD_PENDING 1
189 struct ccg_resp dev_resp;
193 struct work_struct work;
194 struct mutex lock; /* to sync between user and driver thread */
196 /* fw build with vendor information */
198 struct work_struct pm_work;
201 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
203 struct i2c_client *client = uc->client;
204 const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
205 unsigned char buf[2];
206 struct i2c_msg msgs[] = {
208 .addr = client->addr,
214 .addr = client->addr,
219 u32 rlen, rem_len = len, max_read_len = len;
222 /* check any max_read_len limitation on i2c adapter */
223 if (quirks && quirks->max_read_len)
224 max_read_len = quirks->max_read_len;
226 pm_runtime_get_sync(uc->dev);
227 while (rem_len > 0) {
228 msgs[1].buf = &data[len - rem_len];
229 rlen = min_t(u16, rem_len, max_read_len);
231 put_unaligned_le16(rab, buf);
232 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
234 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
235 pm_runtime_put_sync(uc->dev);
242 pm_runtime_put_sync(uc->dev);
246 static int ccg_write(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
248 struct i2c_client *client = uc->client;
250 struct i2c_msg msgs[] = {
252 .addr = client->addr,
258 buf = kzalloc(len + sizeof(rab), GFP_KERNEL);
262 put_unaligned_le16(rab, buf);
263 memcpy(buf + sizeof(rab), data, len);
265 msgs[0].len = len + sizeof(rab);
268 pm_runtime_get_sync(uc->dev);
269 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
271 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
272 pm_runtime_put_sync(uc->dev);
277 pm_runtime_put_sync(uc->dev);
282 static int ucsi_ccg_init(struct ucsi_ccg *uc)
284 unsigned int count = 10;
288 data = CCGX_RAB_UCSI_CONTROL_STOP;
289 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
293 data = CCGX_RAB_UCSI_CONTROL_START;
294 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
299 * Flush CCGx RESPONSE queue by acking interrupts. Above ucsi control
300 * register write will push response which must be cleared.
303 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
310 status = ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
314 usleep_range(10000, 11000);
320 static int ucsi_ccg_send_data(struct ucsi_ccg *uc)
322 u8 *ppm = (u8 *)uc->ppm.data;
326 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, message_out));
327 status = ccg_write(uc, rab, ppm +
328 offsetof(struct ucsi_data, message_out),
329 sizeof(uc->ppm.data->message_out));
333 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, ctrl));
334 return ccg_write(uc, rab, ppm + offsetof(struct ucsi_data, ctrl),
335 sizeof(uc->ppm.data->ctrl));
338 static int ucsi_ccg_recv_data(struct ucsi_ccg *uc)
340 u8 *ppm = (u8 *)uc->ppm.data;
344 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, cci));
345 status = ccg_read(uc, rab, ppm + offsetof(struct ucsi_data, cci),
346 sizeof(uc->ppm.data->cci));
350 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, message_in));
351 return ccg_read(uc, rab, ppm + offsetof(struct ucsi_data, message_in),
352 sizeof(uc->ppm.data->message_in));
355 static int ucsi_ccg_ack_interrupt(struct ucsi_ccg *uc)
360 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
364 return ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
367 static int ucsi_ccg_sync(struct ucsi_ppm *ppm)
369 struct ucsi_ccg *uc = container_of(ppm, struct ucsi_ccg, ppm);
372 status = ucsi_ccg_recv_data(uc);
376 /* ack interrupt to allow next command to run */
377 return ucsi_ccg_ack_interrupt(uc);
380 static int ucsi_ccg_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
382 struct ucsi_ccg *uc = container_of(ppm, struct ucsi_ccg, ppm);
384 ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
385 return ucsi_ccg_send_data(uc);
388 static irqreturn_t ccg_irq_handler(int irq, void *data)
390 struct ucsi_ccg *uc = data;
392 ucsi_notify(uc->ucsi);
397 static void ccg_pm_workaround_work(struct work_struct *pm_work)
399 struct ucsi_ccg *uc = container_of(pm_work, struct ucsi_ccg, pm_work);
401 ucsi_notify(uc->ucsi);
404 static int get_fw_info(struct ucsi_ccg *uc)
408 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)(&uc->version),
409 sizeof(uc->version));
413 uc->fw_version = CCG_VERSION(uc->version[FW2].app.ver) |
414 CCG_VERSION_PATCH(uc->version[FW2].app.patch);
416 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
424 static inline bool invalid_async_evt(int code)
426 return (code >= CCG_EVENT_MAX) || (code < EVENT_INDEX);
429 static void ccg_process_response(struct ucsi_ccg *uc)
431 struct device *dev = uc->dev;
433 if (uc->dev_resp.code & ASYNC_EVENT) {
434 if (uc->dev_resp.code == RESET_COMPLETE) {
435 if (test_bit(RESET_PENDING, &uc->flags))
436 uc->cmd_resp = uc->dev_resp.code;
439 if (invalid_async_evt(uc->dev_resp.code))
440 dev_err(dev, "invalid async evt %d\n",
443 if (test_bit(DEV_CMD_PENDING, &uc->flags)) {
444 uc->cmd_resp = uc->dev_resp.code;
445 clear_bit(DEV_CMD_PENDING, &uc->flags);
447 dev_err(dev, "dev resp 0x%04x but no cmd pending\n",
453 static int ccg_read_response(struct ucsi_ccg *uc)
455 unsigned long target = jiffies + msecs_to_jiffies(1000);
456 struct device *dev = uc->dev;
460 /* wait for interrupt status to get updated */
462 status = ccg_read(uc, CCGX_RAB_INTR_REG, &intval,
467 if (intval & DEV_INT)
469 usleep_range(500, 600);
470 } while (time_is_after_jiffies(target));
472 if (time_is_before_jiffies(target)) {
473 dev_err(dev, "response timeout error\n");
477 status = ccg_read(uc, CCGX_RAB_RESPONSE, (u8 *)&uc->dev_resp,
478 sizeof(uc->dev_resp));
482 status = ccg_write(uc, CCGX_RAB_INTR_REG, &intval, sizeof(intval));
489 /* Caller must hold uc->lock */
490 static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd)
492 struct device *dev = uc->dev;
495 switch (cmd->reg & 0xF000) {
497 set_bit(DEV_CMD_PENDING, &uc->flags);
500 dev_err(dev, "invalid cmd register\n");
504 ret = ccg_write(uc, cmd->reg, (u8 *)&cmd->data, cmd->len);
510 ret = ccg_read_response(uc);
512 dev_err(dev, "response read error\n");
513 switch (cmd->reg & 0xF000) {
515 clear_bit(DEV_CMD_PENDING, &uc->flags);
518 dev_err(dev, "invalid cmd register\n");
523 ccg_process_response(uc);
528 static int ccg_cmd_enter_flashing(struct ucsi_ccg *uc)
533 cmd.reg = CCGX_RAB_ENTER_FLASHING;
534 cmd.data = FLASH_ENTER_SIG;
538 mutex_lock(&uc->lock);
540 ret = ccg_send_command(uc, &cmd);
542 mutex_unlock(&uc->lock);
544 if (ret != CMD_SUCCESS) {
545 dev_err(uc->dev, "enter flashing failed ret=%d\n", ret);
552 static int ccg_cmd_reset(struct ucsi_ccg *uc)
559 cmd.reg = CCGX_RAB_RESET_REQ;
561 p[1] = CMD_RESET_DEV;
565 mutex_lock(&uc->lock);
567 set_bit(RESET_PENDING, &uc->flags);
569 ret = ccg_send_command(uc, &cmd);
570 if (ret != RESET_COMPLETE)
576 clear_bit(RESET_PENDING, &uc->flags);
578 mutex_unlock(&uc->lock);
583 static int ccg_cmd_port_control(struct ucsi_ccg *uc, bool enable)
588 cmd.reg = CCGX_RAB_PDPORT_ENABLE;
590 cmd.data = (uc->port_num == 1) ?
591 PDPORT_1 : (PDPORT_1 | PDPORT_2);
597 mutex_lock(&uc->lock);
599 ret = ccg_send_command(uc, &cmd);
601 mutex_unlock(&uc->lock);
603 if (ret != CMD_SUCCESS) {
604 dev_err(uc->dev, "port control failed ret=%d\n", ret);
610 static int ccg_cmd_jump_boot_mode(struct ucsi_ccg *uc, int bl_mode)
615 cmd.reg = CCGX_RAB_JUMP_TO_BOOT;
620 cmd.data = TO_ALT_FW;
625 mutex_lock(&uc->lock);
627 set_bit(RESET_PENDING, &uc->flags);
629 ret = ccg_send_command(uc, &cmd);
630 if (ret != RESET_COMPLETE)
636 clear_bit(RESET_PENDING, &uc->flags);
638 mutex_unlock(&uc->lock);
644 ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row,
645 const void *data, u8 fcmd)
647 struct i2c_client *client = uc->client;
649 u8 buf[CCG4_ROW_SIZE + 2];
653 /* Copy the data into the flash read/write memory. */
654 put_unaligned_le16(REG_FLASH_RW_MEM, buf);
656 memcpy(buf + 2, data, CCG4_ROW_SIZE);
658 mutex_lock(&uc->lock);
660 ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2);
661 if (ret != CCG4_ROW_SIZE + 2) {
662 dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret);
663 mutex_unlock(&uc->lock);
664 return ret < 0 ? ret : -EIO;
667 /* Use the FLASH_ROW_READ_WRITE register to trigger */
668 /* writing of data to the desired flash row */
670 cmd.reg = CCGX_RAB_FLASH_ROW_RW;
673 put_unaligned_le16(row, &p[2]);
676 if (fcmd == FLASH_FWCT_SIG_WR_CMD)
680 ret = ccg_send_command(uc, &cmd);
682 mutex_unlock(&uc->lock);
684 if (ret != CMD_SUCCESS) {
685 dev_err(uc->dev, "write flash row failed ret=%d\n", ret);
692 static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid)
697 cmd.reg = CCGX_RAB_VALIDATE_FW;
702 mutex_lock(&uc->lock);
704 ret = ccg_send_command(uc, &cmd);
706 mutex_unlock(&uc->lock);
708 if (ret != CMD_SUCCESS)
714 static bool ccg_check_vendor_version(struct ucsi_ccg *uc,
715 struct version_format *app,
716 struct fw_config_table *fw_cfg)
718 struct device *dev = uc->dev;
720 /* Check if the fw build is for supported vendors */
721 if (le16_to_cpu(app->build) != uc->fw_build) {
722 dev_info(dev, "current fw is not from supported vendor\n");
726 /* Check if the new fw build is for supported vendors */
727 if (le16_to_cpu(fw_cfg->app.build) != uc->fw_build) {
728 dev_info(dev, "new fw is not from supported vendor\n");
734 static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name,
735 struct version_format *app)
737 const struct firmware *fw = NULL;
738 struct device *dev = uc->dev;
739 struct fw_config_table fw_cfg;
740 u32 cur_version, new_version;
741 bool is_later = false;
743 if (reject_firmware(&fw, fw_name, dev) != 0) {
744 dev_err(dev, "error: Failed to open cyacd file %s\n", fw_name);
750 * last part of fw image is fw cfg table and signature
752 if (fw->size < sizeof(fw_cfg) + FW_CFG_TABLE_SIG_SIZE)
753 goto out_release_firmware;
755 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size -
756 sizeof(fw_cfg) - FW_CFG_TABLE_SIG_SIZE, sizeof(fw_cfg));
758 if (fw_cfg.identity != ('F' | 'W' << 8 | 'C' << 16 | 'T' << 24)) {
759 dev_info(dev, "not a signed image\n");
760 goto out_release_firmware;
763 /* compare input version with FWCT version */
764 cur_version = le16_to_cpu(app->build) | CCG_VERSION_PATCH(app->patch) |
765 CCG_VERSION(app->ver);
767 new_version = le16_to_cpu(fw_cfg.app.build) |
768 CCG_VERSION_PATCH(fw_cfg.app.patch) |
769 CCG_VERSION(fw_cfg.app.ver);
771 if (!ccg_check_vendor_version(uc, app, &fw_cfg))
772 goto out_release_firmware;
774 if (new_version > cur_version)
777 out_release_firmware:
778 release_firmware(fw);
782 static int ccg_fw_update_needed(struct ucsi_ccg *uc,
783 enum enum_flash_mode *mode)
785 struct device *dev = uc->dev;
787 struct version_info version[3];
789 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
792 dev_err(dev, "read device mode failed\n");
796 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)version,
799 dev_err(dev, "read device mode failed\n");
803 if (memcmp(&version[FW1], "\0\0\0\0\0\0\0\0",
804 sizeof(struct version_info)) == 0) {
805 dev_info(dev, "secondary fw is not flashed\n");
806 *mode = SECONDARY_BL;
807 } else if (le16_to_cpu(version[FW1].base.build) <
808 secondary_fw_min_ver) {
809 dev_info(dev, "secondary fw version is too low (< %d)\n",
810 secondary_fw_min_ver);
812 } else if (memcmp(&version[FW2], "\0\0\0\0\0\0\0\0",
813 sizeof(struct version_info)) == 0) {
814 dev_info(dev, "primary fw is not flashed\n");
816 } else if (ccg_check_fw_version(uc, ccg_fw_names[PRIMARY],
817 &version[FW2].app)) {
818 dev_info(dev, "found primary fw with later version\n");
821 dev_info(dev, "secondary and primary fw are the latest\n");
822 *mode = FLASH_NOT_NEEDED;
827 static int do_flash(struct ucsi_ccg *uc, enum enum_flash_mode mode)
829 struct device *dev = uc->dev;
830 const struct firmware *fw = NULL;
833 int err, row, len, line_sz, line_cnt = 0;
834 unsigned long start_time = jiffies;
835 struct fw_config_table fw_cfg;
836 u8 fw_cfg_sig[FW_CFG_TABLE_SIG_SIZE];
839 err = reject_firmware(&fw, ccg_fw_names[mode], dev);
841 dev_err(dev, "request %s failed err=%d\n",
842 ccg_fw_names[mode], err);
846 if (((uc->info.mode & CCG_DEVINFO_FWMODE_MASK) >>
847 CCG_DEVINFO_FWMODE_SHIFT) == FW2) {
848 err = ccg_cmd_port_control(uc, false);
851 err = ccg_cmd_jump_boot_mode(uc, 0);
856 eof = fw->data + fw->size;
860 * last part of fw image is fw cfg table and signature
862 if (fw->size < sizeof(fw_cfg) + sizeof(fw_cfg_sig))
865 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size -
866 sizeof(fw_cfg) - sizeof(fw_cfg_sig), sizeof(fw_cfg));
868 if (fw_cfg.identity != ('F' | ('W' << 8) | ('C' << 16) | ('T' << 24))) {
869 dev_info(dev, "not a signed image\n");
872 eof = fw->data + fw->size - sizeof(fw_cfg) - sizeof(fw_cfg_sig);
874 memcpy((uint8_t *)&fw_cfg_sig,
875 fw->data + fw->size - sizeof(fw_cfg_sig), sizeof(fw_cfg_sig));
877 /* flash fw config table and signature first */
878 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg,
883 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg + CCG4_ROW_SIZE,
888 err = ccg_cmd_write_flash_row(uc, 0, &fw_cfg_sig,
889 FLASH_FWCT_SIG_WR_CMD);
894 wr_buf = kzalloc(CCG4_ROW_SIZE + 4, GFP_KERNEL);
900 err = ccg_cmd_enter_flashing(uc);
904 /*****************************************************************
905 * CCG firmware image (.cyacd) file line format
907 * :00rrrrllll[dd....]cc/r/n
910 * rrrr is row number to flash (4 char)
911 * llll is data len to flash (4 char)
912 * dd is a data field represents one byte of data (512 char)
913 * cc is checksum (2 char)
916 * Total length: 3 + 4 + 4 + 512 + 2 + 2 = 527
918 *****************************************************************/
920 p = strnchr(fw->data, fw->size, ':');
922 s = strnchr(p + 1, eof - p - 1, ':');
929 if (line_sz != CYACD_LINE_SIZE) {
930 dev_err(dev, "Bad FW format line_sz=%d\n", line_sz);
935 if (hex2bin(wr_buf, p + 3, CCG4_ROW_SIZE + 4)) {
940 row = get_unaligned_be16(wr_buf);
941 len = get_unaligned_be16(&wr_buf[2]);
943 if (len != CCG4_ROW_SIZE) {
948 err = ccg_cmd_write_flash_row(uc, row, wr_buf + 4,
957 dev_info(dev, "total %d row flashed. time: %dms\n",
958 line_cnt, jiffies_to_msecs(jiffies - start_time));
960 err = ccg_cmd_validate_fw(uc, (mode == PRIMARY) ? FW2 : FW1);
962 dev_err(dev, "%s validation failed err=%d\n",
963 (mode == PRIMARY) ? "FW2" : "FW1", err);
965 dev_info(dev, "%s validated\n",
966 (mode == PRIMARY) ? "FW2" : "FW1");
968 err = ccg_cmd_port_control(uc, false);
972 err = ccg_cmd_reset(uc);
976 err = ccg_cmd_port_control(uc, true);
984 release_firmware(fw);
988 /*******************************************************************************
989 * CCG4 has two copies of the firmware in addition to the bootloader.
990 * If the device is running FW1, FW2 can be updated with the new version.
991 * Dual firmware mode allows the CCG device to stay in a PD contract and support
992 * USB PD and Type-C functionality while a firmware update is in progress.
993 ******************************************************************************/
994 static int ccg_fw_update(struct ucsi_ccg *uc, enum enum_flash_mode flash_mode)
998 while (flash_mode != FLASH_NOT_NEEDED) {
999 err = do_flash(uc, flash_mode);
1002 err = ccg_fw_update_needed(uc, &flash_mode);
1006 dev_info(uc->dev, "CCG FW update successful\n");
1011 static int ccg_restart(struct ucsi_ccg *uc)
1013 struct device *dev = uc->dev;
1016 status = ucsi_ccg_init(uc);
1018 dev_err(dev, "ucsi_ccg_start fail, err=%d\n", status);
1022 status = request_threaded_irq(uc->irq, NULL, ccg_irq_handler,
1023 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1026 dev_err(dev, "request_threaded_irq failed - %d\n", status);
1030 uc->ucsi = ucsi_register_ppm(dev, &uc->ppm);
1031 if (IS_ERR(uc->ucsi)) {
1032 dev_err(uc->dev, "ucsi_register_ppm failed\n");
1033 return PTR_ERR(uc->ucsi);
1039 static void ccg_update_firmware(struct work_struct *work)
1041 struct ucsi_ccg *uc = container_of(work, struct ucsi_ccg, work);
1042 enum enum_flash_mode flash_mode;
1045 status = ccg_fw_update_needed(uc, &flash_mode);
1049 if (flash_mode != FLASH_NOT_NEEDED) {
1050 ucsi_unregister_ppm(uc->ucsi);
1051 free_irq(uc->irq, uc);
1053 ccg_fw_update(uc, flash_mode);
1058 static ssize_t do_flash_store(struct device *dev,
1059 struct device_attribute *attr,
1060 const char *buf, size_t n)
1062 struct ucsi_ccg *uc = i2c_get_clientdata(to_i2c_client(dev));
1065 if (kstrtobool(buf, &flash))
1071 if (uc->fw_build == 0x0) {
1072 dev_err(dev, "fail to flash FW due to missing FW build info\n");
1076 schedule_work(&uc->work);
1080 static DEVICE_ATTR_WO(do_flash);
1082 static struct attribute *ucsi_ccg_sysfs_attrs[] = {
1083 &dev_attr_do_flash.attr,
1087 static struct attribute_group ucsi_ccg_attr_group = {
1088 .attrs = ucsi_ccg_sysfs_attrs,
1091 static int ucsi_ccg_probe(struct i2c_client *client,
1092 const struct i2c_device_id *id)
1094 struct device *dev = &client->dev;
1095 struct ucsi_ccg *uc;
1099 uc = devm_kzalloc(dev, sizeof(*uc), GFP_KERNEL);
1103 uc->ppm.data = devm_kzalloc(dev, sizeof(struct ucsi_data), GFP_KERNEL);
1107 uc->ppm.cmd = ucsi_ccg_cmd;
1108 uc->ppm.sync = ucsi_ccg_sync;
1110 uc->client = client;
1111 mutex_init(&uc->lock);
1112 INIT_WORK(&uc->work, ccg_update_firmware);
1113 INIT_WORK(&uc->pm_work, ccg_pm_workaround_work);
1115 /* Only fail FW flashing when FW build information is not provided */
1116 status = device_property_read_u16(dev, "ccgx,firmware-build",
1119 dev_err(uc->dev, "failed to get FW build information\n");
1121 /* reset ccg device and initialize ucsi */
1122 status = ucsi_ccg_init(uc);
1124 dev_err(uc->dev, "ucsi_ccg_init failed - %d\n", status);
1128 status = get_fw_info(uc);
1130 dev_err(uc->dev, "get_fw_info failed - %d\n", status);
1136 if (uc->info.mode & CCG_DEVINFO_PDPORTS_MASK)
1139 status = request_threaded_irq(client->irq, NULL, ccg_irq_handler,
1140 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1143 dev_err(uc->dev, "request_threaded_irq failed - %d\n", status);
1147 uc->irq = client->irq;
1149 uc->ucsi = ucsi_register_ppm(dev, &uc->ppm);
1150 if (IS_ERR(uc->ucsi)) {
1151 dev_err(uc->dev, "ucsi_register_ppm failed\n");
1152 return PTR_ERR(uc->ucsi);
1155 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, version));
1156 status = ccg_read(uc, rab, (u8 *)(uc->ppm.data) +
1157 offsetof(struct ucsi_data, version),
1158 sizeof(uc->ppm.data->version));
1160 ucsi_unregister_ppm(uc->ucsi);
1164 i2c_set_clientdata(client, uc);
1166 status = sysfs_create_group(&uc->dev->kobj, &ucsi_ccg_attr_group);
1168 dev_err(uc->dev, "cannot create sysfs group: %d\n", status);
1170 pm_runtime_set_active(uc->dev);
1171 pm_runtime_enable(uc->dev);
1172 pm_runtime_use_autosuspend(uc->dev);
1173 pm_runtime_set_autosuspend_delay(uc->dev, 5000);
1174 pm_runtime_idle(uc->dev);
1179 static int ucsi_ccg_remove(struct i2c_client *client)
1181 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1183 cancel_work_sync(&uc->pm_work);
1184 cancel_work_sync(&uc->work);
1185 ucsi_unregister_ppm(uc->ucsi);
1186 pm_runtime_disable(uc->dev);
1187 free_irq(uc->irq, uc);
1188 sysfs_remove_group(&uc->dev->kobj, &ucsi_ccg_attr_group);
1193 static const struct i2c_device_id ucsi_ccg_device_id[] = {
1197 MODULE_DEVICE_TABLE(i2c, ucsi_ccg_device_id);
1199 static int ucsi_ccg_resume(struct device *dev)
1201 struct i2c_client *client = to_i2c_client(dev);
1202 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1204 return ucsi_resume(uc->ucsi);
1207 static int ucsi_ccg_runtime_suspend(struct device *dev)
1212 static int ucsi_ccg_runtime_resume(struct device *dev)
1214 struct i2c_client *client = to_i2c_client(dev);
1215 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1218 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue
1219 * of missing interrupt when a device is connected for runtime resume.
1220 * Schedule a work to call ISR as a workaround.
1222 if (uc->fw_build == CCG_FW_BUILD_NVIDIA &&
1223 uc->fw_version <= CCG_OLD_FW_VERSION)
1224 schedule_work(&uc->pm_work);
1229 static const struct dev_pm_ops ucsi_ccg_pm = {
1230 .resume = ucsi_ccg_resume,
1231 .runtime_suspend = ucsi_ccg_runtime_suspend,
1232 .runtime_resume = ucsi_ccg_runtime_resume,
1235 static struct i2c_driver ucsi_ccg_driver = {
1240 .probe = ucsi_ccg_probe,
1241 .remove = ucsi_ccg_remove,
1242 .id_table = ucsi_ccg_device_id,
1245 module_i2c_driver(ucsi_ccg_driver);
1247 MODULE_AUTHOR("Ajay Gupta <ajayg@nvidia.com>");
1248 MODULE_DESCRIPTION("UCSI driver for Cypress CCGx Type-C controller");
1249 MODULE_LICENSE("GPL v2");