1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * i2c support for Silicon Labs' CP2615 Digital Audio Bridge
8 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/string.h>
13 #include <linux/usb.h>
15 /** CP2615 I/O Protocol implementation */
17 #define CP2615_VID 0x10c4
18 #define CP2615_PID 0xeac1
20 #define IOP_EP_IN 0x82
21 #define IOP_EP_OUT 0x02
23 #define IOP_ALTSETTING 2
25 #define MAX_IOP_SIZE 64
26 #define MAX_IOP_PAYLOAD_SIZE (MAX_IOP_SIZE - 6)
27 #define MAX_I2C_SIZE (MAX_IOP_PAYLOAD_SIZE - 4)
29 enum cp2615_iop_msg_type {
30 iop_GetAccessoryInfo = 0xD100,
31 iop_AccessoryInfo = 0xA100,
32 iop_GetPortConfiguration = 0xD203,
33 iop_PortConfiguration = 0xA203,
34 iop_DoI2cTransfer = 0xD400,
35 iop_I2cTransferResult = 0xA400,
36 iop_GetSerialState = 0xD501,
37 iop_SerialState = 0xA501
40 struct __packed cp2615_iop_msg {
41 __be16 preamble, length, msg;
42 u8 data[MAX_IOP_PAYLOAD_SIZE];
45 #define PART_ID_A01 0x1400
46 #define PART_ID_A02 0x1500
48 struct __packed cp2615_iop_accessory_info {
49 __be16 part_id, option_id, proto_ver;
52 struct __packed cp2615_i2c_transfer {
53 u8 tag, i2caddr, read_len, write_len;
54 u8 data[MAX_I2C_SIZE];
57 /* Possible values for struct cp2615_i2c_transfer_result.status */
58 enum cp2615_i2c_status {
59 /* Writing to the internal EEPROM failed, because it is locked */
60 CP2615_CFG_LOCKED = -6,
61 /* read_len or write_len out of range */
62 CP2615_INVALID_PARAM = -4,
63 /* I2C slave did not ACK in time */
67 /* I2C bus error (ie. device NAK'd the request) */
72 struct __packed cp2615_i2c_transfer_result {
76 u8 data[MAX_I2C_SIZE];
79 static int cp2615_init_iop_msg(struct cp2615_iop_msg *ret, enum cp2615_iop_msg_type msg,
80 const void *data, size_t data_len)
82 if (data_len > MAX_IOP_PAYLOAD_SIZE)
88 ret->preamble = 0x2A2A;
89 ret->length = htons(data_len + 6);
90 ret->msg = htons(msg);
92 memcpy(&ret->data, data, data_len);
96 static int cp2615_init_i2c_msg(struct cp2615_iop_msg *ret, const struct cp2615_i2c_transfer *data)
98 return cp2615_init_iop_msg(ret, iop_DoI2cTransfer, data, 4 + data->write_len);
101 /* Translates status codes to Linux errno's */
102 static int cp2615_check_status(enum cp2615_i2c_status status)
107 case CP2615_BUS_ERROR:
109 case CP2615_BUS_BUSY:
113 case CP2615_INVALID_PARAM:
115 case CP2615_CFG_LOCKED:
118 /* Unknown error code */
125 cp2615_i2c_send(struct usb_interface *usbif, struct cp2615_i2c_transfer *i2c_w)
127 struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
128 struct usb_device *usbdev = interface_to_usbdev(usbif);
129 int res = cp2615_init_i2c_msg(msg, i2c_w);
132 res = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, IOP_EP_OUT),
133 msg, ntohs(msg->length), NULL, 0);
139 cp2615_i2c_recv(struct usb_interface *usbif, unsigned char tag, void *buf)
141 struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
142 struct cp2615_i2c_transfer_result *i2c_r = (struct cp2615_i2c_transfer_result *)&msg->data;
143 struct usb_device *usbdev = interface_to_usbdev(usbif);
144 int res = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, IOP_EP_IN),
145 msg, sizeof(struct cp2615_iop_msg), NULL, 0);
152 if (msg->msg != htons(iop_I2cTransferResult) || i2c_r->tag != tag) {
157 res = cp2615_check_status(i2c_r->status);
159 memcpy(buf, &i2c_r->data, i2c_r->read_len);
165 /* Checks if the IOP is functional by querying the part's ID */
166 static int cp2615_check_iop(struct usb_interface *usbif)
168 struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
169 struct cp2615_iop_accessory_info *info = (struct cp2615_iop_accessory_info *)&msg->data;
170 struct usb_device *usbdev = interface_to_usbdev(usbif);
171 int res = cp2615_init_iop_msg(msg, iop_GetAccessoryInfo, NULL, 0);
176 res = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, IOP_EP_OUT),
177 msg, ntohs(msg->length), NULL, 0);
181 res = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, IOP_EP_IN),
182 msg, sizeof(struct cp2615_iop_msg), NULL, 0);
186 if (msg->msg != htons(iop_AccessoryInfo)) {
191 switch (ntohs(info->part_id)) {
193 dev_dbg(&usbif->dev, "Found A01 part. (WARNING: errata exists!)\n");
196 dev_dbg(&usbif->dev, "Found good A02 part.\n");
199 dev_warn(&usbif->dev, "Unknown part ID %04X\n", ntohs(info->part_id));
208 cp2615_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
210 struct usb_interface *usbif = adap->algo_data;
213 struct cp2615_i2c_transfer i2c_w = {0};
215 dev_dbg(&usbif->dev, "Doing %d I2C transactions\n", num);
217 for (; !ret && i < num; i++) {
221 i2c_w.i2caddr = i2c_8bit_addr_from_msg(msg);
222 if (msg->flags & I2C_M_RD) {
223 i2c_w.read_len = msg->len;
227 i2c_w.write_len = msg->len;
228 memcpy(&i2c_w.data, msg->buf, i2c_w.write_len);
230 ret = cp2615_i2c_send(usbif, &i2c_w);
233 ret = cp2615_i2c_recv(usbif, i2c_w.tag, msg->buf);
241 cp2615_i2c_func(struct i2c_adapter *adap)
243 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
246 static const struct i2c_algorithm cp2615_i2c_algo = {
247 .master_xfer = cp2615_i2c_master_xfer,
248 .functionality = cp2615_i2c_func,
252 * This chip has some limitations: one is that the USB endpoint
253 * can only receive 64 bytes/transfer, that leaves 54 bytes for
254 * the I2C transfer. On top of that, EITHER read_len OR write_len
255 * may be zero, but not both. If both are non-zero, the adapter
256 * issues a write followed by a read. And the chip does not
257 * support repeated START between the write and read phases.
259 static struct i2c_adapter_quirks cp2615_i2c_quirks = {
260 .max_write_len = MAX_I2C_SIZE,
261 .max_read_len = MAX_I2C_SIZE,
262 .flags = I2C_AQ_COMB_WRITE_THEN_READ | I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START,
263 .max_comb_1st_msg_len = MAX_I2C_SIZE,
264 .max_comb_2nd_msg_len = MAX_I2C_SIZE
268 cp2615_i2c_remove(struct usb_interface *usbif)
270 struct i2c_adapter *adap = usb_get_intfdata(usbif);
272 usb_set_intfdata(usbif, NULL);
273 i2c_del_adapter(adap);
277 cp2615_i2c_probe(struct usb_interface *usbif, const struct usb_device_id *id)
280 struct i2c_adapter *adap;
281 struct usb_device *usbdev = interface_to_usbdev(usbif);
283 ret = usb_set_interface(usbdev, IOP_IFN, IOP_ALTSETTING);
287 ret = cp2615_check_iop(usbif);
291 adap = devm_kzalloc(&usbif->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
295 strncpy(adap->name, usbdev->serial, sizeof(adap->name) - 1);
296 adap->owner = THIS_MODULE;
297 adap->dev.parent = &usbif->dev;
298 adap->dev.of_node = usbif->dev.of_node;
300 adap->algo = &cp2615_i2c_algo;
301 adap->quirks = &cp2615_i2c_quirks;
302 adap->algo_data = usbif;
304 ret = i2c_add_adapter(adap);
308 usb_set_intfdata(usbif, adap);
312 static const struct usb_device_id id_table[] = {
313 { USB_DEVICE_INTERFACE_NUMBER(CP2615_VID, CP2615_PID, IOP_IFN) },
317 MODULE_DEVICE_TABLE(usb, id_table);
319 static struct usb_driver cp2615_i2c_driver = {
320 .name = "i2c-cp2615",
321 .probe = cp2615_i2c_probe,
322 .disconnect = cp2615_i2c_remove,
323 .id_table = id_table,
326 module_usb_driver(cp2615_i2c_driver);
329 MODULE_DESCRIPTION("CP2615 I2C bus driver");
330 MODULE_LICENSE("GPL");