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/i2c.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/platform_device.h>
17 #include <asm/unaligned.h>
24 struct i2c_client *client;
27 #define CCGX_RAB_INTR_REG 0x06
28 #define CCGX_RAB_UCSI_CONTROL 0x39
29 #define CCGX_RAB_UCSI_CONTROL_START BIT(0)
30 #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1)
31 #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff))
33 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
35 struct i2c_client *client = uc->client;
36 const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
38 struct i2c_msg msgs[] = {
51 u32 rlen, rem_len = len, max_read_len = len;
54 /* check any max_read_len limitation on i2c adapter */
55 if (quirks && quirks->max_read_len)
56 max_read_len = quirks->max_read_len;
59 msgs[1].buf = &data[len - rem_len];
60 rlen = min_t(u16, rem_len, max_read_len);
62 put_unaligned_le16(rab, buf);
63 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
65 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
75 static int ccg_write(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
77 struct i2c_client *client = uc->client;
79 struct i2c_msg msgs[] = {
87 buf = kzalloc(len + sizeof(rab), GFP_KERNEL);
91 put_unaligned_le16(rab, buf);
92 memcpy(buf + sizeof(rab), data, len);
94 msgs[0].len = len + sizeof(rab);
97 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
99 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
108 static int ucsi_ccg_init(struct ucsi_ccg *uc)
110 unsigned int count = 10;
114 data = CCGX_RAB_UCSI_CONTROL_STOP;
115 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
119 data = CCGX_RAB_UCSI_CONTROL_START;
120 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
125 * Flush CCGx RESPONSE queue by acking interrupts. Above ucsi control
126 * register write will push response which must be cleared.
129 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
136 status = ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
140 usleep_range(10000, 11000);
146 static int ucsi_ccg_send_data(struct ucsi_ccg *uc)
148 u8 *ppm = (u8 *)uc->ppm.data;
152 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, message_out));
153 status = ccg_write(uc, rab, ppm +
154 offsetof(struct ucsi_data, message_out),
155 sizeof(uc->ppm.data->message_out));
159 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, ctrl));
160 return ccg_write(uc, rab, ppm + offsetof(struct ucsi_data, ctrl),
161 sizeof(uc->ppm.data->ctrl));
164 static int ucsi_ccg_recv_data(struct ucsi_ccg *uc)
166 u8 *ppm = (u8 *)uc->ppm.data;
170 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, cci));
171 status = ccg_read(uc, rab, ppm + offsetof(struct ucsi_data, cci),
172 sizeof(uc->ppm.data->cci));
176 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, message_in));
177 return ccg_read(uc, rab, ppm + offsetof(struct ucsi_data, message_in),
178 sizeof(uc->ppm.data->message_in));
181 static int ucsi_ccg_ack_interrupt(struct ucsi_ccg *uc)
186 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
190 return ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
193 static int ucsi_ccg_sync(struct ucsi_ppm *ppm)
195 struct ucsi_ccg *uc = container_of(ppm, struct ucsi_ccg, ppm);
198 status = ucsi_ccg_recv_data(uc);
202 /* ack interrupt to allow next command to run */
203 return ucsi_ccg_ack_interrupt(uc);
206 static int ucsi_ccg_cmd(struct ucsi_ppm *ppm, struct ucsi_control *ctrl)
208 struct ucsi_ccg *uc = container_of(ppm, struct ucsi_ccg, ppm);
210 ppm->data->ctrl.raw_cmd = ctrl->raw_cmd;
211 return ucsi_ccg_send_data(uc);
214 static irqreturn_t ccg_irq_handler(int irq, void *data)
216 struct ucsi_ccg *uc = data;
218 ucsi_notify(uc->ucsi);
223 static int ucsi_ccg_probe(struct i2c_client *client,
224 const struct i2c_device_id *id)
226 struct device *dev = &client->dev;
231 uc = devm_kzalloc(dev, sizeof(*uc), GFP_KERNEL);
235 uc->ppm.data = devm_kzalloc(dev, sizeof(struct ucsi_data), GFP_KERNEL);
239 uc->ppm.cmd = ucsi_ccg_cmd;
240 uc->ppm.sync = ucsi_ccg_sync;
244 /* reset ccg device and initialize ucsi */
245 status = ucsi_ccg_init(uc);
247 dev_err(uc->dev, "ucsi_ccg_init failed - %d\n", status);
251 status = devm_request_threaded_irq(dev, client->irq, NULL,
253 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
256 dev_err(uc->dev, "request_threaded_irq failed - %d\n", status);
260 uc->ucsi = ucsi_register_ppm(dev, &uc->ppm);
261 if (IS_ERR(uc->ucsi)) {
262 dev_err(uc->dev, "ucsi_register_ppm failed\n");
263 return PTR_ERR(uc->ucsi);
266 rab = CCGX_RAB_UCSI_DATA_BLOCK(offsetof(struct ucsi_data, version));
267 status = ccg_read(uc, rab, (u8 *)(uc->ppm.data) +
268 offsetof(struct ucsi_data, version),
269 sizeof(uc->ppm.data->version));
271 ucsi_unregister_ppm(uc->ucsi);
275 i2c_set_clientdata(client, uc);
279 static int ucsi_ccg_remove(struct i2c_client *client)
281 struct ucsi_ccg *uc = i2c_get_clientdata(client);
283 ucsi_unregister_ppm(uc->ucsi);
288 static const struct i2c_device_id ucsi_ccg_device_id[] = {
292 MODULE_DEVICE_TABLE(i2c, ucsi_ccg_device_id);
294 static struct i2c_driver ucsi_ccg_driver = {
298 .probe = ucsi_ccg_probe,
299 .remove = ucsi_ccg_remove,
300 .id_table = ucsi_ccg_device_id,
303 module_i2c_driver(ucsi_ccg_driver);
305 MODULE_AUTHOR("Ajay Gupta <ajayg@nvidia.com>");
306 MODULE_DESCRIPTION("UCSI driver for Cypress CCGx Type-C controller");
307 MODULE_LICENSE("GPL v2");