xref: /openbmc/linux/drivers/i2c/busses/i2c-cp2615.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
14a769542SBence Csókás // SPDX-License-Identifier: GPL-2.0-or-later
24a769542SBence Csókás /*
34a769542SBence Csókás  * i2c support for Silicon Labs' CP2615 Digital Audio Bridge
44a769542SBence Csókás  *
54a769542SBence Csókás  * (c) 2021, Bence Csókás <bence98@sch.bme.hu>
64a769542SBence Csókás  */
74a769542SBence Csókás 
84a769542SBence Csókás #include <linux/errno.h>
94a769542SBence Csókás #include <linux/i2c.h>
104a769542SBence Csókás #include <linux/kernel.h>
114a769542SBence Csókás #include <linux/module.h>
124a769542SBence Csókás #include <linux/string.h>
134a769542SBence Csókás #include <linux/usb.h>
144a769542SBence Csókás 
154a769542SBence Csókás /** CP2615 I/O Protocol implementation */
164a769542SBence Csókás 
174a769542SBence Csókás #define CP2615_VID 0x10c4
184a769542SBence Csókás #define CP2615_PID 0xeac1
194a769542SBence Csókás 
204a769542SBence Csókás #define IOP_EP_IN  0x82
214a769542SBence Csókás #define IOP_EP_OUT 0x02
224a769542SBence Csókás #define IOP_IFN 1
234a769542SBence Csókás #define IOP_ALTSETTING 2
244a769542SBence Csókás 
254a769542SBence Csókás #define MAX_IOP_SIZE 64
264a769542SBence Csókás #define MAX_IOP_PAYLOAD_SIZE (MAX_IOP_SIZE - 6)
274a769542SBence Csókás #define MAX_I2C_SIZE (MAX_IOP_PAYLOAD_SIZE - 4)
284a769542SBence Csókás 
294a769542SBence Csókás enum cp2615_iop_msg_type {
304a769542SBence Csókás 	iop_GetAccessoryInfo = 0xD100,
314a769542SBence Csókás 	iop_AccessoryInfo = 0xA100,
324a769542SBence Csókás 	iop_GetPortConfiguration = 0xD203,
334a769542SBence Csókás 	iop_PortConfiguration = 0xA203,
344a769542SBence Csókás 	iop_DoI2cTransfer = 0xD400,
354a769542SBence Csókás 	iop_I2cTransferResult = 0xA400,
364a769542SBence Csókás 	iop_GetSerialState = 0xD501,
374a769542SBence Csókás 	iop_SerialState = 0xA501
384a769542SBence Csókás };
394a769542SBence Csókás 
404a769542SBence Csókás struct __packed cp2615_iop_msg {
414a769542SBence Csókás 	__be16 preamble, length, msg;
424a769542SBence Csókás 	u8 data[MAX_IOP_PAYLOAD_SIZE];
434a769542SBence Csókás };
444a769542SBence Csókás 
454a769542SBence Csókás #define PART_ID_A01 0x1400
464a769542SBence Csókás #define PART_ID_A02 0x1500
474a769542SBence Csókás 
484a769542SBence Csókás struct __packed cp2615_iop_accessory_info {
494a769542SBence Csókás 	__be16 part_id, option_id, proto_ver;
504a769542SBence Csókás };
514a769542SBence Csókás 
524a769542SBence Csókás struct __packed cp2615_i2c_transfer {
534a769542SBence Csókás 	u8 tag, i2caddr, read_len, write_len;
544a769542SBence Csókás 	u8 data[MAX_I2C_SIZE];
554a769542SBence Csókás };
564a769542SBence Csókás 
574a769542SBence Csókás /* Possible values for struct cp2615_i2c_transfer_result.status */
584a769542SBence Csókás enum cp2615_i2c_status {
594a769542SBence Csókás 	/* Writing to the internal EEPROM failed, because it is locked */
604a769542SBence Csókás 	CP2615_CFG_LOCKED = -6,
614a769542SBence Csókás 	/* read_len or write_len out of range */
624a769542SBence Csókás 	CP2615_INVALID_PARAM = -4,
634a769542SBence Csókás 	/* I2C slave did not ACK in time */
644a769542SBence Csókás 	CP2615_TIMEOUT,
654a769542SBence Csókás 	/* I2C bus busy */
664a769542SBence Csókás 	CP2615_BUS_BUSY,
674a769542SBence Csókás 	/* I2C bus error (ie. device NAK'd the request) */
684a769542SBence Csókás 	CP2615_BUS_ERROR,
694a769542SBence Csókás 	CP2615_SUCCESS
704a769542SBence Csókás };
714a769542SBence Csókás 
724a769542SBence Csókás struct __packed cp2615_i2c_transfer_result {
734a769542SBence Csókás 	u8 tag, i2caddr;
744a769542SBence Csókás 	s8 status;
754a769542SBence Csókás 	u8 read_len;
764a769542SBence Csókás 	u8 data[MAX_I2C_SIZE];
774a769542SBence Csókás };
784a769542SBence Csókás 
cp2615_init_iop_msg(struct cp2615_iop_msg * ret,enum cp2615_iop_msg_type msg,const void * data,size_t data_len)794a769542SBence Csókás static int cp2615_init_iop_msg(struct cp2615_iop_msg *ret, enum cp2615_iop_msg_type msg,
804a769542SBence Csókás 			const void *data, size_t data_len)
814a769542SBence Csókás {
824a769542SBence Csókás 	if (data_len > MAX_IOP_PAYLOAD_SIZE)
834a769542SBence Csókás 		return -EFBIG;
844a769542SBence Csókás 
854a769542SBence Csókás 	if (!ret)
864a769542SBence Csókás 		return -EINVAL;
874a769542SBence Csókás 
884a769542SBence Csókás 	ret->preamble = 0x2A2A;
894a769542SBence Csókás 	ret->length = htons(data_len + 6);
904a769542SBence Csókás 	ret->msg = htons(msg);
914a769542SBence Csókás 	if (data && data_len)
924a769542SBence Csókás 		memcpy(&ret->data, data, data_len);
934a769542SBence Csókás 	return 0;
944a769542SBence Csókás }
954a769542SBence Csókás 
cp2615_init_i2c_msg(struct cp2615_iop_msg * ret,const struct cp2615_i2c_transfer * data)964a769542SBence Csókás static int cp2615_init_i2c_msg(struct cp2615_iop_msg *ret, const struct cp2615_i2c_transfer *data)
974a769542SBence Csókás {
984a769542SBence Csókás 	return cp2615_init_iop_msg(ret, iop_DoI2cTransfer, data, 4 + data->write_len);
994a769542SBence Csókás }
1004a769542SBence Csókás 
1014a769542SBence Csókás /* Translates status codes to Linux errno's */
cp2615_check_status(enum cp2615_i2c_status status)1024a769542SBence Csókás static int cp2615_check_status(enum cp2615_i2c_status status)
1034a769542SBence Csókás {
1044a769542SBence Csókás 	switch (status) {
1054a769542SBence Csókás 	case CP2615_SUCCESS:
1064a769542SBence Csókás 			return 0;
1074a769542SBence Csókás 	case CP2615_BUS_ERROR:
1084a769542SBence Csókás 		return -ENXIO;
1094a769542SBence Csókás 	case CP2615_BUS_BUSY:
1104a769542SBence Csókás 		return -EAGAIN;
1114a769542SBence Csókás 	case CP2615_TIMEOUT:
1124a769542SBence Csókás 		return -ETIMEDOUT;
1134a769542SBence Csókás 	case CP2615_INVALID_PARAM:
1144a769542SBence Csókás 		return -EINVAL;
1154a769542SBence Csókás 	case CP2615_CFG_LOCKED:
1164a769542SBence Csókás 		return -EPERM;
1174a769542SBence Csókás 	}
1184a769542SBence Csókás 	/* Unknown error code */
1194a769542SBence Csókás 	return -EPROTO;
1204a769542SBence Csókás }
1214a769542SBence Csókás 
1224a769542SBence Csókás /** Driver code */
1234a769542SBence Csókás 
1244a769542SBence Csókás static int
cp2615_i2c_send(struct usb_interface * usbif,struct cp2615_i2c_transfer * i2c_w)1254a769542SBence Csókás cp2615_i2c_send(struct usb_interface *usbif, struct cp2615_i2c_transfer *i2c_w)
1264a769542SBence Csókás {
1274a769542SBence Csókás 	struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
1284a769542SBence Csókás 	struct usb_device *usbdev = interface_to_usbdev(usbif);
1294a769542SBence Csókás 	int res = cp2615_init_i2c_msg(msg, i2c_w);
1304a769542SBence Csókás 
1314a769542SBence Csókás 	if (!res)
1324a769542SBence Csókás 		res = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, IOP_EP_OUT),
1334a769542SBence Csókás 				   msg, ntohs(msg->length), NULL, 0);
1344a769542SBence Csókás 	kfree(msg);
1354a769542SBence Csókás 	return res;
1364a769542SBence Csókás }
1374a769542SBence Csókás 
1384a769542SBence Csókás static int
cp2615_i2c_recv(struct usb_interface * usbif,unsigned char tag,void * buf)1394a769542SBence Csókás cp2615_i2c_recv(struct usb_interface *usbif, unsigned char tag, void *buf)
1404a769542SBence Csókás {
1414a769542SBence Csókás 	struct usb_device *usbdev = interface_to_usbdev(usbif);
142*22695837SDan Carpenter 	struct cp2615_iop_msg *msg;
143*22695837SDan Carpenter 	struct cp2615_i2c_transfer_result *i2c_r;
144*22695837SDan Carpenter 	int res;
1454a769542SBence Csókás 
146*22695837SDan Carpenter 	msg = kzalloc(sizeof(*msg), GFP_KERNEL);
147*22695837SDan Carpenter 	if (!msg)
148*22695837SDan Carpenter 		return -ENOMEM;
149*22695837SDan Carpenter 
150*22695837SDan Carpenter 	res = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, IOP_EP_IN), msg,
151*22695837SDan Carpenter 			   sizeof(struct cp2615_iop_msg), NULL, 0);
1524a769542SBence Csókás 	if (res < 0) {
1534a769542SBence Csókás 		kfree(msg);
1544a769542SBence Csókás 		return res;
1554a769542SBence Csókás 	}
1564a769542SBence Csókás 
157*22695837SDan Carpenter 	i2c_r = (struct cp2615_i2c_transfer_result *)&msg->data;
1584a769542SBence Csókás 	if (msg->msg != htons(iop_I2cTransferResult) || i2c_r->tag != tag) {
1594a769542SBence Csókás 		kfree(msg);
1604a769542SBence Csókás 		return -EIO;
1614a769542SBence Csókás 	}
1624a769542SBence Csókás 
1634a769542SBence Csókás 	res = cp2615_check_status(i2c_r->status);
1644a769542SBence Csókás 	if (!res)
1654a769542SBence Csókás 		memcpy(buf, &i2c_r->data, i2c_r->read_len);
1664a769542SBence Csókás 
1674a769542SBence Csókás 	kfree(msg);
1684a769542SBence Csókás 	return res;
1694a769542SBence Csókás }
1704a769542SBence Csókás 
1714a769542SBence Csókás /* Checks if the IOP is functional by querying the part's ID */
cp2615_check_iop(struct usb_interface * usbif)1724a769542SBence Csókás static int cp2615_check_iop(struct usb_interface *usbif)
1734a769542SBence Csókás {
1744a769542SBence Csókás 	struct cp2615_iop_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
1754a769542SBence Csókás 	struct cp2615_iop_accessory_info *info = (struct cp2615_iop_accessory_info *)&msg->data;
1764a769542SBence Csókás 	struct usb_device *usbdev = interface_to_usbdev(usbif);
1774a769542SBence Csókás 	int res = cp2615_init_iop_msg(msg, iop_GetAccessoryInfo, NULL, 0);
1784a769542SBence Csókás 
1794a769542SBence Csókás 	if (res)
1804a769542SBence Csókás 		goto out;
1814a769542SBence Csókás 
1824a769542SBence Csókás 	res = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, IOP_EP_OUT),
1834a769542SBence Csókás 				   msg, ntohs(msg->length), NULL, 0);
1844a769542SBence Csókás 	if (res)
1854a769542SBence Csókás 		goto out;
1864a769542SBence Csókás 
1874a769542SBence Csókás 	res = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, IOP_EP_IN),
1884a769542SBence Csókás 			       msg, sizeof(struct cp2615_iop_msg), NULL, 0);
1894a769542SBence Csókás 	if (res)
1904a769542SBence Csókás 		goto out;
1914a769542SBence Csókás 
1924a769542SBence Csókás 	if (msg->msg != htons(iop_AccessoryInfo)) {
1934a769542SBence Csókás 		res = -EIO;
1944a769542SBence Csókás 		goto out;
1954a769542SBence Csókás 	}
1964a769542SBence Csókás 
1974a769542SBence Csókás 	switch (ntohs(info->part_id)) {
1984a769542SBence Csókás 	case PART_ID_A01:
1994a769542SBence Csókás 		dev_dbg(&usbif->dev, "Found A01 part. (WARNING: errata exists!)\n");
2004a769542SBence Csókás 		break;
2014a769542SBence Csókás 	case PART_ID_A02:
2024a769542SBence Csókás 		dev_dbg(&usbif->dev, "Found good A02 part.\n");
2034a769542SBence Csókás 		break;
2044a769542SBence Csókás 	default:
2054a769542SBence Csókás 		dev_warn(&usbif->dev, "Unknown part ID %04X\n", ntohs(info->part_id));
2064a769542SBence Csókás 	}
2074a769542SBence Csókás 
2084a769542SBence Csókás out:
2094a769542SBence Csókás 	kfree(msg);
2104a769542SBence Csókás 	return res;
2114a769542SBence Csókás }
2124a769542SBence Csókás 
2134a769542SBence Csókás static int
cp2615_i2c_master_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)2144a769542SBence Csókás cp2615_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2154a769542SBence Csókás {
2164a769542SBence Csókás 	struct usb_interface *usbif = adap->algo_data;
2174a769542SBence Csókás 	int i = 0, ret = 0;
2184a769542SBence Csókás 	struct i2c_msg *msg;
2194a769542SBence Csókás 	struct cp2615_i2c_transfer i2c_w = {0};
2204a769542SBence Csókás 
2214a769542SBence Csókás 	dev_dbg(&usbif->dev, "Doing %d I2C transactions\n", num);
2224a769542SBence Csókás 
2234a769542SBence Csókás 	for (; !ret && i < num; i++) {
2244a769542SBence Csókás 		msg = &msgs[i];
2254a769542SBence Csókás 
2264a769542SBence Csókás 		i2c_w.tag = 0xdd;
2274a769542SBence Csókás 		i2c_w.i2caddr = i2c_8bit_addr_from_msg(msg);
2284a769542SBence Csókás 		if (msg->flags & I2C_M_RD) {
2294a769542SBence Csókás 			i2c_w.read_len = msg->len;
2304a769542SBence Csókás 			i2c_w.write_len = 0;
2314a769542SBence Csókás 		} else {
2324a769542SBence Csókás 			i2c_w.read_len = 0;
2334a769542SBence Csókás 			i2c_w.write_len = msg->len;
2344a769542SBence Csókás 			memcpy(&i2c_w.data, msg->buf, i2c_w.write_len);
2354a769542SBence Csókás 		}
2364a769542SBence Csókás 		ret = cp2615_i2c_send(usbif, &i2c_w);
2374a769542SBence Csókás 		if (ret)
2384a769542SBence Csókás 			break;
2394a769542SBence Csókás 		ret = cp2615_i2c_recv(usbif, i2c_w.tag, msg->buf);
2404a769542SBence Csókás 	}
2414a769542SBence Csókás 	if (ret < 0)
2424a769542SBence Csókás 		return ret;
2434a769542SBence Csókás 	return i;
2444a769542SBence Csókás }
2454a769542SBence Csókás 
2464a769542SBence Csókás static u32
cp2615_i2c_func(struct i2c_adapter * adap)2474a769542SBence Csókás cp2615_i2c_func(struct i2c_adapter *adap)
2484a769542SBence Csókás {
2494a769542SBence Csókás 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2504a769542SBence Csókás }
2514a769542SBence Csókás 
2524a769542SBence Csókás static const struct i2c_algorithm cp2615_i2c_algo = {
2534a769542SBence Csókás 	.master_xfer	= cp2615_i2c_master_xfer,
2544a769542SBence Csókás 	.functionality	= cp2615_i2c_func,
2554a769542SBence Csókás };
2564a769542SBence Csókás 
2574a769542SBence Csókás /*
2584a769542SBence Csókás  * This chip has some limitations: one is that the USB endpoint
2594a769542SBence Csókás  * can only receive 64 bytes/transfer, that leaves 54 bytes for
2604a769542SBence Csókás  * the I2C transfer. On top of that, EITHER read_len OR write_len
2614a769542SBence Csókás  * may be zero, but not both. If both are non-zero, the adapter
2624a769542SBence Csókás  * issues a write followed by a read. And the chip does not
2634a769542SBence Csókás  * support repeated START between the write and read phases.
2644a769542SBence Csókás  */
2654a769542SBence Csókás static struct i2c_adapter_quirks cp2615_i2c_quirks = {
2664a769542SBence Csókás 	.max_write_len = MAX_I2C_SIZE,
2674a769542SBence Csókás 	.max_read_len = MAX_I2C_SIZE,
2684a769542SBence Csókás 	.flags = I2C_AQ_COMB_WRITE_THEN_READ | I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START,
2694a769542SBence Csókás 	.max_comb_1st_msg_len = MAX_I2C_SIZE,
2704a769542SBence Csókás 	.max_comb_2nd_msg_len = MAX_I2C_SIZE
2714a769542SBence Csókás };
2724a769542SBence Csókás 
2734a769542SBence Csókás static void
cp2615_i2c_remove(struct usb_interface * usbif)2744a769542SBence Csókás cp2615_i2c_remove(struct usb_interface *usbif)
2754a769542SBence Csókás {
2764a769542SBence Csókás 	struct i2c_adapter *adap = usb_get_intfdata(usbif);
2774a769542SBence Csókás 
2784a769542SBence Csókás 	usb_set_intfdata(usbif, NULL);
2794a769542SBence Csókás 	i2c_del_adapter(adap);
2804a769542SBence Csókás }
2814a769542SBence Csókás 
2824a769542SBence Csókás static int
cp2615_i2c_probe(struct usb_interface * usbif,const struct usb_device_id * id)2834a769542SBence Csókás cp2615_i2c_probe(struct usb_interface *usbif, const struct usb_device_id *id)
2844a769542SBence Csókás {
2854a769542SBence Csókás 	int ret = 0;
2864a769542SBence Csókás 	struct i2c_adapter *adap;
2874a769542SBence Csókás 	struct usb_device *usbdev = interface_to_usbdev(usbif);
2884a769542SBence Csókás 
2894a769542SBence Csókás 	ret = usb_set_interface(usbdev, IOP_IFN, IOP_ALTSETTING);
2904a769542SBence Csókás 	if (ret)
2914a769542SBence Csókás 		return ret;
2924a769542SBence Csókás 
2934a769542SBence Csókás 	ret = cp2615_check_iop(usbif);
2944a769542SBence Csókás 	if (ret)
2954a769542SBence Csókás 		return ret;
2964a769542SBence Csókás 
2974a769542SBence Csókás 	adap = devm_kzalloc(&usbif->dev, sizeof(struct i2c_adapter), GFP_KERNEL);
2984a769542SBence Csókás 	if (!adap)
2994a769542SBence Csókás 		return -ENOMEM;
3004a769542SBence Csókás 
3014a769542SBence Csókás 	strncpy(adap->name, usbdev->serial, sizeof(adap->name) - 1);
3024a769542SBence Csókás 	adap->owner = THIS_MODULE;
3034a769542SBence Csókás 	adap->dev.parent = &usbif->dev;
3044a769542SBence Csókás 	adap->dev.of_node = usbif->dev.of_node;
3054a769542SBence Csókás 	adap->timeout = HZ;
3064a769542SBence Csókás 	adap->algo = &cp2615_i2c_algo;
3074a769542SBence Csókás 	adap->quirks = &cp2615_i2c_quirks;
3084a769542SBence Csókás 	adap->algo_data = usbif;
3094a769542SBence Csókás 
3104a769542SBence Csókás 	ret = i2c_add_adapter(adap);
3114a769542SBence Csókás 	if (ret)
3124a769542SBence Csókás 		return ret;
3134a769542SBence Csókás 
3144a769542SBence Csókás 	usb_set_intfdata(usbif, adap);
3154a769542SBence Csókás 	return 0;
3164a769542SBence Csókás }
3174a769542SBence Csókás 
3184a769542SBence Csókás static const struct usb_device_id id_table[] = {
3194a769542SBence Csókás 	{ USB_DEVICE_INTERFACE_NUMBER(CP2615_VID, CP2615_PID, IOP_IFN) },
3204a769542SBence Csókás 	{ }
3214a769542SBence Csókás };
3224a769542SBence Csókás 
3234a769542SBence Csókás MODULE_DEVICE_TABLE(usb, id_table);
3244a769542SBence Csókás 
3254a769542SBence Csókás static struct usb_driver cp2615_i2c_driver = {
3264a769542SBence Csókás 	.name = "i2c-cp2615",
3274a769542SBence Csókás 	.probe = cp2615_i2c_probe,
3284a769542SBence Csókás 	.disconnect = cp2615_i2c_remove,
3294a769542SBence Csókás 	.id_table = id_table,
3304a769542SBence Csókás };
3314a769542SBence Csókás 
3324a769542SBence Csókás module_usb_driver(cp2615_i2c_driver);
3334a769542SBence Csókás 
3344a769542SBence Csókás MODULE_AUTHOR("Bence Csókás <bence98@sch.bme.hu>");
3354a769542SBence Csókás MODULE_DESCRIPTION("CP2615 I2C bus driver");
3364a769542SBence Csókás MODULE_LICENSE("GPL");
337