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 <asm/unaligned.h>
22 BOOT, /* bootloader */
23 FW1, /* FW partition-1 (contains secondary fw) */
24 FW2, /* FW partition-2 (contains primary fw) */
28 #define CCGX_RAB_DEVICE_MODE 0x0000
29 #define CCGX_RAB_INTR_REG 0x0006
30 #define DEV_INT BIT(0)
31 #define PORT0_INT BIT(1)
32 #define PORT1_INT BIT(2)
33 #define UCSI_READ_INT BIT(7)
34 #define CCGX_RAB_JUMP_TO_BOOT 0x0007
37 #define CCGX_RAB_RESET_REQ 0x0008
39 #define CMD_RESET_I2C 0x0
40 #define CMD_RESET_DEV 0x1
41 #define CCGX_RAB_ENTER_FLASHING 0x000A
42 #define FLASH_ENTER_SIG 'P'
43 #define CCGX_RAB_VALIDATE_FW 0x000B
44 #define CCGX_RAB_FLASH_ROW_RW 0x000C
46 #define FLASH_RD_CMD 0x0
47 #define FLASH_WR_CMD 0x1
48 #define FLASH_FWCT1_WR_CMD 0x2
49 #define FLASH_FWCT2_WR_CMD 0x3
50 #define FLASH_FWCT_SIG_WR_CMD 0x4
51 #define CCGX_RAB_READ_ALL_VER 0x0010
52 #define CCGX_RAB_READ_FW2_VER 0x0020
53 #define CCGX_RAB_UCSI_CONTROL 0x0039
54 #define CCGX_RAB_UCSI_CONTROL_START BIT(0)
55 #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1)
56 #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff))
57 #define REG_FLASH_RW_MEM 0x0200
58 #define DEV_REG_IDX CCGX_RAB_DEVICE_MODE
59 #define CCGX_RAB_PDPORT_ENABLE 0x002C
60 #define PDPORT_1 BIT(0)
61 #define PDPORT_2 BIT(1)
62 #define CCGX_RAB_RESPONSE 0x007E
63 #define ASYNC_EVENT BIT(7)
65 /* CCGx events & async msg codes */
66 #define RESET_COMPLETE 0x80
67 #define EVENT_INDEX RESET_COMPLETE
68 #define PORT_CONNECT_DET 0x84
69 #define PORT_DISCONNECT_DET 0x85
70 #define ROLE_SWAP_COMPELETE 0x87
73 #define CYACD_LINE_SIZE 527
74 #define CCG4_ROW_SIZE 256
75 #define FW1_METADATA_ROW 0x1FF
76 #define FW2_METADATA_ROW 0x1FE
77 #define FW_CFG_TABLE_SIG_SIZE 256
79 static int secondary_fw_min_ver = 41;
81 enum enum_flash_mode {
82 SECONDARY_BL, /* update secondary using bootloader */
83 PRIMARY, /* update primary using secondary */
84 SECONDARY, /* update secondary using primary */
85 FLASH_NOT_NEEDED, /* update not required */
89 static const char * const ccg_fw_names[] = {
96 #define CCG_DEVINFO_FWMODE_SHIFT (0)
97 #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT)
98 #define CCG_DEVINFO_PDPORTS_SHIFT (2)
99 #define CCG_DEVINFO_PDPORTS_MASK (0x3 << CCG_DEVINFO_PDPORTS_SHIFT)
106 struct version_format {
110 #define CCG_VERSION_MIN_SHIFT (0)
111 #define CCG_VERSION_MIN_MASK (0xf << CCG_VERSION_MIN_SHIFT)
112 #define CCG_VERSION_MAJ_SHIFT (4)
113 #define CCG_VERSION_MAJ_MASK (0xf << CCG_VERSION_MAJ_SHIFT)
116 struct version_info {
117 struct version_format base;
118 struct version_format app;
121 struct fw_config_table {
127 struct version_format base;
128 struct version_format app;
129 u8 primary_fw_digest[32];
135 /* CCGx response codes */
139 FLASH_DATA_AVAILABLE = 0x03,
141 FLASH_UPDATE_FAIL = 0x07,
144 CMD_NOT_SUPPORT = 0x0A,
145 TRANSACTION_FAIL = 0x0C,
151 #define CCG_EVENT_MAX (EVENT_INDEX + 43)
157 u32 delay; /* ms delay for cmd timeout */
169 struct i2c_client *client;
170 struct ccg_dev_info info;
171 /* version info for boot, primary and secondary */
172 struct version_info version[FW2 + 1];
173 /* CCG HPI communication flags */
175 #define RESET_PENDING 0
176 #define DEV_CMD_PENDING 1
177 struct ccg_resp dev_resp;
181 struct work_struct work;
182 struct mutex lock; /* to sync between user and driver thread */
184 /* fw build with vendor information */
188 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
190 struct i2c_client *client = uc->client;
191 const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
192 unsigned char buf[2];
193 struct i2c_msg msgs[] = {
195 .addr = client->addr,
201 .addr = client->addr,
206 u32 rlen, rem_len = len, max_read_len = len;
209 /* check any max_read_len limitation on i2c adapter */
210 if (quirks && quirks->max_read_len)
211 max_read_len = quirks->max_read_len;
213 while (rem_len > 0) {
214 msgs[1].buf = &data[len - rem_len];
215 rlen = min_t(u16, rem_len, max_read_len);
217 put_unaligned_le16(rab, buf);
218 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
220 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
230 static int ccg_write(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
232 struct i2c_client *client = uc->client;
234 struct i2c_msg msgs[] = {
236 .addr = client->addr,
242 buf = kzalloc(len + sizeof(rab), GFP_KERNEL);
246 put_unaligned_le16(rab, buf);
247 memcpy(buf + sizeof(rab), data, len);
249 msgs[0].len = len + sizeof(rab);
252 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
254 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
263 static int ucsi_ccg_init(struct ucsi_ccg *uc)
265 unsigned int count = 10;
269 data = CCGX_RAB_UCSI_CONTROL_STOP;
270 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
274 data = CCGX_RAB_UCSI_CONTROL_START;
275 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
280 * Flush CCGx RESPONSE queue by acking interrupts. Above ucsi control
281 * register write will push response which must be cleared.
284 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
291 status = ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
295 usleep_range(10000, 11000);
301 static int ucsi_ccg_send_data(struct ucsi_ccg *uc)
303 u8 *ppm = (u8 *)uc->ppm.data;
307 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, message_out));
308 status = ccg_write(uc, rab, ppm +
309 offsetof(struct ucsi_data, message_out),
310 sizeof(uc->ppm.data->message_out));
314 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, ctrl));
315 return ccg_write(uc, rab, ppm + offsetof(struct ucsi_data, ctrl),
316 sizeof(uc->ppm.data->ctrl));
319 static int ucsi_ccg_recv_data(struct ucsi_ccg *uc)
321 u8 *ppm = (u8 *)uc->ppm.data;
325 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, cci));
326 status = ccg_read(uc, rab, ppm + offsetof(struct ucsi_data, cci),
327 sizeof(uc->ppm.data->cci));
331 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, message_in));
332 return ccg_read(uc, rab, ppm + offsetof(struct ucsi_data, message_in),
333 sizeof(uc->ppm.data->message_in));
336 static int ucsi_ccg_ack_interrupt(struct ucsi_ccg *uc)
341 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
345 return ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
348 static int ucsi_ccg_sync(struct ucsi_ppm *ppm)
350 struct ucsi_ccg *uc = container_of(ppm, struct ucsi_ccg, ppm);
353 status = ucsi_ccg_recv_data(uc);
357 /* ack interrupt to allow next command to run */
358 return ucsi_ccg_ack_interrupt(uc);
361 static int ucsi_ccg_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
363 struct ucsi_ccg *uc = container_of(ppm, struct ucsi_ccg, ppm);
365 ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
366 return ucsi_ccg_send_data(uc);
369 static irqreturn_t ccg_irq_handler(int irq, void *data)
371 struct ucsi_ccg *uc = data;
373 ucsi_notify(uc->ucsi);
378 static int get_fw_info(struct ucsi_ccg *uc)
382 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)(&uc->version),
383 sizeof(uc->version));
387 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
395 static inline bool invalid_async_evt(int code)
397 return (code >= CCG_EVENT_MAX) || (code < EVENT_INDEX);
400 static void ccg_process_response(struct ucsi_ccg *uc)
402 struct device *dev = uc->dev;
404 if (uc->dev_resp.code & ASYNC_EVENT) {
405 if (uc->dev_resp.code == RESET_COMPLETE) {
406 if (test_bit(RESET_PENDING, &uc->flags))
407 uc->cmd_resp = uc->dev_resp.code;
410 if (invalid_async_evt(uc->dev_resp.code))
411 dev_err(dev, "invalid async evt %d\n",
414 if (test_bit(DEV_CMD_PENDING, &uc->flags)) {
415 uc->cmd_resp = uc->dev_resp.code;
416 clear_bit(DEV_CMD_PENDING, &uc->flags);
418 dev_err(dev, "dev resp 0x%04x but no cmd pending\n",
424 static int ccg_read_response(struct ucsi_ccg *uc)
426 unsigned long target = jiffies + msecs_to_jiffies(1000);
427 struct device *dev = uc->dev;
431 /* wait for interrupt status to get updated */
433 status = ccg_read(uc, CCGX_RAB_INTR_REG, &intval,
438 if (intval & DEV_INT)
440 usleep_range(500, 600);
441 } while (time_is_after_jiffies(target));
443 if (time_is_before_jiffies(target)) {
444 dev_err(dev, "response timeout error\n");
448 status = ccg_read(uc, CCGX_RAB_RESPONSE, (u8 *)&uc->dev_resp,
449 sizeof(uc->dev_resp));
453 status = ccg_write(uc, CCGX_RAB_INTR_REG, &intval, sizeof(intval));
460 /* Caller must hold uc->lock */
461 static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd)
463 struct device *dev = uc->dev;
466 switch (cmd->reg & 0xF000) {
468 set_bit(DEV_CMD_PENDING, &uc->flags);
471 dev_err(dev, "invalid cmd register\n");
475 ret = ccg_write(uc, cmd->reg, (u8 *)&cmd->data, cmd->len);
481 ret = ccg_read_response(uc);
483 dev_err(dev, "response read error\n");
484 switch (cmd->reg & 0xF000) {
486 clear_bit(DEV_CMD_PENDING, &uc->flags);
489 dev_err(dev, "invalid cmd register\n");
494 ccg_process_response(uc);
499 static int ccg_cmd_enter_flashing(struct ucsi_ccg *uc)
504 cmd.reg = CCGX_RAB_ENTER_FLASHING;
505 cmd.data = FLASH_ENTER_SIG;
509 mutex_lock(&uc->lock);
511 ret = ccg_send_command(uc, &cmd);
513 mutex_unlock(&uc->lock);
515 if (ret != CMD_SUCCESS) {
516 dev_err(uc->dev, "enter flashing failed ret=%d\n", ret);
523 static int ccg_cmd_reset(struct ucsi_ccg *uc)
530 cmd.reg = CCGX_RAB_RESET_REQ;
532 p[1] = CMD_RESET_DEV;
536 mutex_lock(&uc->lock);
538 set_bit(RESET_PENDING, &uc->flags);
540 ret = ccg_send_command(uc, &cmd);
541 if (ret != RESET_COMPLETE)
547 clear_bit(RESET_PENDING, &uc->flags);
549 mutex_unlock(&uc->lock);
554 static int ccg_cmd_port_control(struct ucsi_ccg *uc, bool enable)
559 cmd.reg = CCGX_RAB_PDPORT_ENABLE;
561 cmd.data = (uc->port_num == 1) ?
562 PDPORT_1 : (PDPORT_1 | PDPORT_2);
568 mutex_lock(&uc->lock);
570 ret = ccg_send_command(uc, &cmd);
572 mutex_unlock(&uc->lock);
574 if (ret != CMD_SUCCESS) {
575 dev_err(uc->dev, "port control failed ret=%d\n", ret);
581 static int ccg_cmd_jump_boot_mode(struct ucsi_ccg *uc, int bl_mode)
586 cmd.reg = CCGX_RAB_JUMP_TO_BOOT;
591 cmd.data = TO_ALT_FW;
596 mutex_lock(&uc->lock);
598 set_bit(RESET_PENDING, &uc->flags);
600 ret = ccg_send_command(uc, &cmd);
601 if (ret != RESET_COMPLETE)
607 clear_bit(RESET_PENDING, &uc->flags);
609 mutex_unlock(&uc->lock);
615 ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row,
616 const void *data, u8 fcmd)
618 struct i2c_client *client = uc->client;
620 u8 buf[CCG4_ROW_SIZE + 2];
624 /* Copy the data into the flash read/write memory. */
625 put_unaligned_le16(REG_FLASH_RW_MEM, buf);
627 memcpy(buf + 2, data, CCG4_ROW_SIZE);
629 mutex_lock(&uc->lock);
631 ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2);
632 if (ret != CCG4_ROW_SIZE + 2) {
633 dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret);
634 mutex_unlock(&uc->lock);
635 return ret < 0 ? ret : -EIO;
638 /* Use the FLASH_ROW_READ_WRITE register to trigger */
639 /* writing of data to the desired flash row */
641 cmd.reg = CCGX_RAB_FLASH_ROW_RW;
644 put_unaligned_le16(row, &p[2]);
647 if (fcmd == FLASH_FWCT_SIG_WR_CMD)
651 ret = ccg_send_command(uc, &cmd);
653 mutex_unlock(&uc->lock);
655 if (ret != CMD_SUCCESS) {
656 dev_err(uc->dev, "write flash row failed ret=%d\n", ret);
663 static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid)
668 cmd.reg = CCGX_RAB_VALIDATE_FW;
673 mutex_lock(&uc->lock);
675 ret = ccg_send_command(uc, &cmd);
677 mutex_unlock(&uc->lock);
679 if (ret != CMD_SUCCESS)
685 static bool ccg_check_vendor_version(struct ucsi_ccg *uc,
686 struct version_format *app,
687 struct fw_config_table *fw_cfg)
689 struct device *dev = uc->dev;
691 /* Check if the fw build is for supported vendors */
692 if (le16_to_cpu(app->build) != uc->fw_build) {
693 dev_info(dev, "current fw is not from supported vendor\n");
697 /* Check if the new fw build is for supported vendors */
698 if (le16_to_cpu(fw_cfg->app.build) != uc->fw_build) {
699 dev_info(dev, "new fw is not from supported vendor\n");
705 static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name,
706 struct version_format *app)
708 const struct firmware *fw = NULL;
709 struct device *dev = uc->dev;
710 struct fw_config_table fw_cfg;
711 u32 cur_version, new_version;
712 bool is_later = false;
714 if (request_firmware(&fw, fw_name, dev) != 0) {
715 dev_err(dev, "error: Failed to open cyacd file %s\n", fw_name);
721 * last part of fw image is fw cfg table and signature
723 if (fw->size < sizeof(fw_cfg) + FW_CFG_TABLE_SIG_SIZE)
724 goto out_release_firmware;
726 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size -
727 sizeof(fw_cfg) - FW_CFG_TABLE_SIG_SIZE, sizeof(fw_cfg));
729 if (fw_cfg.identity != ('F' | 'W' << 8 | 'C' << 16 | 'T' << 24)) {
730 dev_info(dev, "not a signed image\n");
731 goto out_release_firmware;
734 /* compare input version with FWCT version */
735 cur_version = le16_to_cpu(app->build) | app->patch << 16 |
738 new_version = le16_to_cpu(fw_cfg.app.build) | fw_cfg.app.patch << 16 |
739 fw_cfg.app.ver << 24;
741 if (!ccg_check_vendor_version(uc, app, &fw_cfg))
742 goto out_release_firmware;
744 if (new_version > cur_version)
747 out_release_firmware:
748 release_firmware(fw);
752 static int ccg_fw_update_needed(struct ucsi_ccg *uc,
753 enum enum_flash_mode *mode)
755 struct device *dev = uc->dev;
757 struct version_info version[3];
759 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
762 dev_err(dev, "read device mode failed\n");
766 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)version,
769 dev_err(dev, "read device mode failed\n");
773 if (memcmp(&version[FW1], "\0\0\0\0\0\0\0\0",
774 sizeof(struct version_info)) == 0) {
775 dev_info(dev, "secondary fw is not flashed\n");
776 *mode = SECONDARY_BL;
777 } else if (le16_to_cpu(version[FW1].base.build) <
778 secondary_fw_min_ver) {
779 dev_info(dev, "secondary fw version is too low (< %d)\n",
780 secondary_fw_min_ver);
782 } else if (memcmp(&version[FW2], "\0\0\0\0\0\0\0\0",
783 sizeof(struct version_info)) == 0) {
784 dev_info(dev, "primary fw is not flashed\n");
786 } else if (ccg_check_fw_version(uc, ccg_fw_names[PRIMARY],
787 &version[FW2].app)) {
788 dev_info(dev, "found primary fw with later version\n");
791 dev_info(dev, "secondary and primary fw are the latest\n");
792 *mode = FLASH_NOT_NEEDED;
797 static int do_flash(struct ucsi_ccg *uc, enum enum_flash_mode mode)
799 struct device *dev = uc->dev;
800 const struct firmware *fw = NULL;
803 int err, row, len, line_sz, line_cnt = 0;
804 unsigned long start_time = jiffies;
805 struct fw_config_table fw_cfg;
806 u8 fw_cfg_sig[FW_CFG_TABLE_SIG_SIZE];
809 err = request_firmware(&fw, ccg_fw_names[mode], dev);
811 dev_err(dev, "request %s failed err=%d\n",
812 ccg_fw_names[mode], err);
816 if (((uc->info.mode & CCG_DEVINFO_FWMODE_MASK) >>
817 CCG_DEVINFO_FWMODE_SHIFT) == FW2) {
818 err = ccg_cmd_port_control(uc, false);
821 err = ccg_cmd_jump_boot_mode(uc, 0);
826 eof = fw->data + fw->size;
830 * last part of fw image is fw cfg table and signature
832 if (fw->size < sizeof(fw_cfg) + sizeof(fw_cfg_sig))
835 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size -
836 sizeof(fw_cfg) - sizeof(fw_cfg_sig), sizeof(fw_cfg));
838 if (fw_cfg.identity != ('F' | ('W' << 8) | ('C' << 16) | ('T' << 24))) {
839 dev_info(dev, "not a signed image\n");
842 eof = fw->data + fw->size - sizeof(fw_cfg) - sizeof(fw_cfg_sig);
844 memcpy((uint8_t *)&fw_cfg_sig,
845 fw->data + fw->size - sizeof(fw_cfg_sig), sizeof(fw_cfg_sig));
847 /* flash fw config table and signature first */
848 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg,
853 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg + CCG4_ROW_SIZE,
858 err = ccg_cmd_write_flash_row(uc, 0, &fw_cfg_sig,
859 FLASH_FWCT_SIG_WR_CMD);
864 wr_buf = kzalloc(CCG4_ROW_SIZE + 4, GFP_KERNEL);
870 err = ccg_cmd_enter_flashing(uc);
874 /*****************************************************************
875 * CCG firmware image (.cyacd) file line format
877 * :00rrrrllll[dd....]cc/r/n
880 * rrrr is row number to flash (4 char)
881 * llll is data len to flash (4 char)
882 * dd is a data field represents one byte of data (512 char)
883 * cc is checksum (2 char)
886 * Total length: 3 + 4 + 4 + 512 + 2 + 2 = 527
888 *****************************************************************/
890 p = strnchr(fw->data, fw->size, ':');
892 s = strnchr(p + 1, eof - p - 1, ':');
899 if (line_sz != CYACD_LINE_SIZE) {
900 dev_err(dev, "Bad FW format line_sz=%d\n", line_sz);
905 if (hex2bin(wr_buf, p + 3, CCG4_ROW_SIZE + 4)) {
910 row = get_unaligned_be16(wr_buf);
911 len = get_unaligned_be16(&wr_buf[2]);
913 if (len != CCG4_ROW_SIZE) {
918 err = ccg_cmd_write_flash_row(uc, row, wr_buf + 4,
927 dev_info(dev, "total %d row flashed. time: %dms\n",
928 line_cnt, jiffies_to_msecs(jiffies - start_time));
930 err = ccg_cmd_validate_fw(uc, (mode == PRIMARY) ? FW2 : FW1);
932 dev_err(dev, "%s validation failed err=%d\n",
933 (mode == PRIMARY) ? "FW2" : "FW1", err);
935 dev_info(dev, "%s validated\n",
936 (mode == PRIMARY) ? "FW2" : "FW1");
938 err = ccg_cmd_port_control(uc, false);
942 err = ccg_cmd_reset(uc);
946 err = ccg_cmd_port_control(uc, true);
954 release_firmware(fw);
958 /*******************************************************************************
959 * CCG4 has two copies of the firmware in addition to the bootloader.
960 * If the device is running FW1, FW2 can be updated with the new version.
961 * Dual firmware mode allows the CCG device to stay in a PD contract and support
962 * USB PD and Type-C functionality while a firmware update is in progress.
963 ******************************************************************************/
964 static int ccg_fw_update(struct ucsi_ccg *uc, enum enum_flash_mode flash_mode)
968 while (flash_mode != FLASH_NOT_NEEDED) {
969 err = do_flash(uc, flash_mode);
972 err = ccg_fw_update_needed(uc, &flash_mode);
976 dev_info(uc->dev, "CCG FW update successful\n");
981 static int ccg_restart(struct ucsi_ccg *uc)
983 struct device *dev = uc->dev;
986 status = ucsi_ccg_init(uc);
988 dev_err(dev, "ucsi_ccg_start fail, err=%d\n", status);
992 status = request_threaded_irq(uc->irq, NULL, ccg_irq_handler,
993 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
996 dev_err(dev, "request_threaded_irq failed - %d\n", status);
1000 uc->ucsi = ucsi_register_ppm(dev, &uc->ppm);
1001 if (IS_ERR(uc->ucsi)) {
1002 dev_err(uc->dev, "ucsi_register_ppm failed\n");
1003 return PTR_ERR(uc->ucsi);
1009 static void ccg_update_firmware(struct work_struct *work)
1011 struct ucsi_ccg *uc = container_of(work, struct ucsi_ccg, work);
1012 enum enum_flash_mode flash_mode;
1015 status = ccg_fw_update_needed(uc, &flash_mode);
1019 if (flash_mode != FLASH_NOT_NEEDED) {
1020 ucsi_unregister_ppm(uc->ucsi);
1021 free_irq(uc->irq, uc);
1023 ccg_fw_update(uc, flash_mode);
1028 static ssize_t do_flash_store(struct device *dev,
1029 struct device_attribute *attr,
1030 const char *buf, size_t n)
1032 struct ucsi_ccg *uc = i2c_get_clientdata(to_i2c_client(dev));
1035 if (kstrtobool(buf, &flash))
1041 if (uc->fw_build == 0x0) {
1042 dev_err(dev, "fail to flash FW due to missing FW build info\n");
1046 schedule_work(&uc->work);
1050 static DEVICE_ATTR_WO(do_flash);
1052 static struct attribute *ucsi_ccg_sysfs_attrs[] = {
1053 &dev_attr_do_flash.attr,
1057 static struct attribute_group ucsi_ccg_attr_group = {
1058 .attrs = ucsi_ccg_sysfs_attrs,
1061 static int ucsi_ccg_probe(struct i2c_client *client,
1062 const struct i2c_device_id *id)
1064 struct device *dev = &client->dev;
1065 struct ucsi_ccg *uc;
1069 uc = devm_kzalloc(dev, sizeof(*uc), GFP_KERNEL);
1073 uc->ppm.data = devm_kzalloc(dev, sizeof(struct ucsi_data), GFP_KERNEL);
1077 uc->ppm.cmd = ucsi_ccg_cmd;
1078 uc->ppm.sync = ucsi_ccg_sync;
1080 uc->client = client;
1081 mutex_init(&uc->lock);
1082 INIT_WORK(&uc->work, ccg_update_firmware);
1084 /* Only fail FW flashing when FW build information is not provided */
1085 status = device_property_read_u16(dev, "ccgx,firmware-build",
1088 dev_err(uc->dev, "failed to get FW build information\n");
1090 /* reset ccg device and initialize ucsi */
1091 status = ucsi_ccg_init(uc);
1093 dev_err(uc->dev, "ucsi_ccg_init failed - %d\n", status);
1097 status = get_fw_info(uc);
1099 dev_err(uc->dev, "get_fw_info failed - %d\n", status);
1105 if (uc->info.mode & CCG_DEVINFO_PDPORTS_MASK)
1108 status = request_threaded_irq(client->irq, NULL, ccg_irq_handler,
1109 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1112 dev_err(uc->dev, "request_threaded_irq failed - %d\n", status);
1116 uc->irq = client->irq;
1118 uc->ucsi = ucsi_register_ppm(dev, &uc->ppm);
1119 if (IS_ERR(uc->ucsi)) {
1120 dev_err(uc->dev, "ucsi_register_ppm failed\n");
1121 return PTR_ERR(uc->ucsi);
1124 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, version));
1125 status = ccg_read(uc, rab, (u8 *)(uc->ppm.data) +
1126 offsetof(struct ucsi_data, version),
1127 sizeof(uc->ppm.data->version));
1129 ucsi_unregister_ppm(uc->ucsi);
1133 i2c_set_clientdata(client, uc);
1135 status = sysfs_create_group(&uc->dev->kobj, &ucsi_ccg_attr_group);
1137 dev_err(uc->dev, "cannot create sysfs group: %d\n", status);
1142 static int ucsi_ccg_remove(struct i2c_client *client)
1144 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1146 cancel_work_sync(&uc->work);
1147 ucsi_unregister_ppm(uc->ucsi);
1148 free_irq(uc->irq, uc);
1149 sysfs_remove_group(&uc->dev->kobj, &ucsi_ccg_attr_group);
1154 static const struct i2c_device_id ucsi_ccg_device_id[] = {
1158 MODULE_DEVICE_TABLE(i2c, ucsi_ccg_device_id);
1160 static struct i2c_driver ucsi_ccg_driver = {
1164 .probe = ucsi_ccg_probe,
1165 .remove = ucsi_ccg_remove,
1166 .id_table = ucsi_ccg_device_id,
1169 module_i2c_driver(ucsi_ccg_driver);
1171 MODULE_AUTHOR("Ajay Gupta <ajayg@nvidia.com>");
1172 MODULE_DESCRIPTION("UCSI driver for Cypress CCGx Type-C controller");
1173 MODULE_LICENSE("GPL v2");