Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i2c: Add Intel USBIO I2C driver

Add a a driver for the I2C auxbus child device of the Intel USBIO USB
IO-expander used by the MIPI cameras on various new (Meteor Lake and
later) Intel laptops.

Co-developed-by: Hans de Goede <hansg@kernel.org>
Signed-off-by: Hans de Goede <hansg@kernel.org>
Signed-off-by: Israel Cepeda <israel.a.cepeda.lopez@intel.com>
Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Link: https://lore.kernel.org/r/20250911181343.77398-4-hansg@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Israel Cepeda and committed by
Greg Kroah-Hartman
daf16134 c122451c

+333
+1
MAINTAINERS
··· 12694 12694 R: Sakari Ailus <sakari.ailus@linux.intel.com> 12695 12695 S: Maintained 12696 12696 F: drivers/gpio/gpio-usbio.c 12697 + F: drivers/i2c/busses/i2c-usbio.c 12697 12698 F: drivers/usb/misc/usbio.c 12698 12699 F: include/linux/usb/usbio.h 12699 12700
+11
drivers/i2c/busses/Kconfig
··· 1357 1357 This driver can also be built as a module. If so, the module 1358 1358 will be called i2c-ljca. 1359 1359 1360 + config I2C_USBIO 1361 + tristate "Intel USBIO I2C Adapter support" 1362 + depends on USB_USBIO 1363 + default USB_USBIO 1364 + help 1365 + Select this option to enable I2C driver for the INTEL 1366 + USBIO driver stack. 1367 + 1368 + This driver can also be built as a module. If so, the module 1369 + will be called i2c_usbio. 1370 + 1360 1371 config I2C_CP2615 1361 1372 tristate "Silicon Labs CP2615 USB sound card and I2C adapter" 1362 1373 depends on USB
+1
drivers/i2c/busses/Makefile
··· 135 135 obj-$(CONFIG_I2C_DIOLAN_U2C) += i2c-diolan-u2c.o 136 136 obj-$(CONFIG_I2C_DLN2) += i2c-dln2.o 137 137 obj-$(CONFIG_I2C_LJCA) += i2c-ljca.o 138 + obj-$(CONFIG_I2C_USBIO) += i2c-usbio.o 138 139 obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o 139 140 obj-$(CONFIG_I2C_PARPORT) += i2c-parport.o 140 141 obj-$(CONFIG_I2C_PCI1XXXX) += i2c-mchp-pci1xxxx.o
+320
drivers/i2c/busses/i2c-usbio.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (c) 2025 Intel Corporation. 4 + * Copyright (c) 2025 Red Hat, Inc. 5 + */ 6 + 7 + #include <linux/auxiliary_bus.h> 8 + #include <linux/dev_printk.h> 9 + #include <linux/device.h> 10 + #include <linux/i2c.h> 11 + #include <linux/types.h> 12 + #include <linux/usb/usbio.h> 13 + 14 + #define I2C_RW_OVERHEAD (sizeof(struct usbio_bulk_packet) + sizeof(struct usbio_i2c_rw)) 15 + 16 + struct usbio_i2c { 17 + struct i2c_adapter adap; 18 + struct auxiliary_device *adev; 19 + struct usbio_i2c_rw *rwbuf; 20 + unsigned long quirks; 21 + u32 speed; 22 + u16 txbuf_len; 23 + u16 rxbuf_len; 24 + }; 25 + 26 + static const struct acpi_device_id usbio_i2c_acpi_hids[] = { 27 + { "INTC1008" }, /* MTL */ 28 + { "INTC10B3" }, /* ARL */ 29 + { "INTC10B6" }, /* LNL */ 30 + { "INTC10E3" }, /* PTL */ 31 + { } 32 + }; 33 + 34 + static const u32 usbio_i2c_speeds[] = { 35 + I2C_MAX_STANDARD_MODE_FREQ, 36 + I2C_MAX_FAST_MODE_FREQ, 37 + I2C_MAX_FAST_MODE_PLUS_FREQ, 38 + I2C_MAX_HIGH_SPEED_MODE_FREQ 39 + }; 40 + 41 + static void usbio_i2c_uninit(struct i2c_adapter *adap, struct i2c_msg *msg) 42 + { 43 + struct usbio_i2c *i2c = i2c_get_adapdata(adap); 44 + struct usbio_i2c_uninit ubuf; 45 + 46 + ubuf.busid = i2c->adev->id; 47 + ubuf.config = cpu_to_le16(msg->addr); 48 + 49 + usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_UNINIT, true, 50 + &ubuf, sizeof(ubuf), NULL, 0); 51 + } 52 + 53 + static int usbio_i2c_init(struct i2c_adapter *adap, struct i2c_msg *msg) 54 + { 55 + struct usbio_i2c *i2c = i2c_get_adapdata(adap); 56 + struct usbio_i2c_init ibuf; 57 + void *reply_buf; 58 + u16 reply_len; 59 + int ret; 60 + 61 + ibuf.busid = i2c->adev->id; 62 + ibuf.config = cpu_to_le16(msg->addr); 63 + ibuf.speed = cpu_to_le32(i2c->speed); 64 + 65 + if (i2c->quirks & USBIO_QUIRK_I2C_NO_INIT_ACK) { 66 + reply_buf = NULL; 67 + reply_len = 0; 68 + } else { 69 + reply_buf = &ibuf; 70 + reply_len = sizeof(ibuf); 71 + } 72 + 73 + ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_INIT, true, 74 + &ibuf, sizeof(ibuf), reply_buf, reply_len); 75 + if (ret != sizeof(ibuf)) 76 + return (ret < 0) ? ret : -EIO; 77 + 78 + return 0; 79 + } 80 + 81 + static int usbio_i2c_read(struct i2c_adapter *adap, struct i2c_msg *msg) 82 + { 83 + struct usbio_i2c *i2c = i2c_get_adapdata(adap); 84 + u16 rxchunk = i2c->rxbuf_len - I2C_RW_OVERHEAD; 85 + struct usbio_i2c_rw *rbuf = i2c->rwbuf; 86 + int ret; 87 + 88 + rbuf->busid = i2c->adev->id; 89 + rbuf->config = cpu_to_le16(msg->addr); 90 + rbuf->size = cpu_to_le16(msg->len); 91 + 92 + if (msg->len > rxchunk) { 93 + /* Need to split the input buffer */ 94 + u16 len = 0; 95 + 96 + do { 97 + if (msg->len - len < rxchunk) 98 + rxchunk = msg->len - len; 99 + 100 + ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, 101 + USBIO_I2CCMD_READ, true, 102 + rbuf, len == 0 ? sizeof(*rbuf) : 0, 103 + rbuf, sizeof(*rbuf) + rxchunk); 104 + if (ret < 0) 105 + return ret; 106 + 107 + memcpy(&msg->buf[len], rbuf->data, rxchunk); 108 + len += rxchunk; 109 + } while (msg->len > len); 110 + 111 + return 0; 112 + } 113 + 114 + ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_READ, true, 115 + rbuf, sizeof(*rbuf), rbuf, sizeof(*rbuf) + msg->len); 116 + if (ret != sizeof(*rbuf) + msg->len) 117 + return (ret < 0) ? ret : -EIO; 118 + 119 + memcpy(msg->buf, rbuf->data, msg->len); 120 + 121 + return 0; 122 + } 123 + 124 + static int usbio_i2c_write(struct i2c_adapter *adap, struct i2c_msg *msg) 125 + { 126 + struct usbio_i2c *i2c = i2c_get_adapdata(adap); 127 + u16 txchunk = i2c->txbuf_len - I2C_RW_OVERHEAD; 128 + struct usbio_i2c_rw *wbuf = i2c->rwbuf; 129 + int ret; 130 + 131 + if (msg->len > txchunk) { 132 + /* Need to split the output buffer */ 133 + u16 len = 0; 134 + 135 + do { 136 + wbuf->busid = i2c->adev->id; 137 + wbuf->config = cpu_to_le16(msg->addr); 138 + 139 + if (i2c->quirks & USBIO_QUIRK_I2C_USE_CHUNK_LEN) 140 + wbuf->size = cpu_to_le16(txchunk); 141 + else 142 + wbuf->size = cpu_to_le16(msg->len); 143 + 144 + memcpy(wbuf->data, &msg->buf[len], txchunk); 145 + len += txchunk; 146 + 147 + ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, 148 + USBIO_I2CCMD_WRITE, msg->len == len, 149 + wbuf, sizeof(*wbuf) + txchunk, 150 + wbuf, sizeof(*wbuf)); 151 + if (ret < 0) 152 + return ret; 153 + 154 + if (msg->len - len < txchunk) 155 + txchunk = msg->len - len; 156 + } while (msg->len > len); 157 + 158 + return 0; 159 + } 160 + 161 + wbuf->busid = i2c->adev->id; 162 + wbuf->config = cpu_to_le16(msg->addr); 163 + wbuf->size = cpu_to_le16(msg->len); 164 + memcpy(wbuf->data, msg->buf, msg->len); 165 + 166 + ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_WRITE, true, 167 + wbuf, sizeof(*wbuf) + msg->len, wbuf, sizeof(*wbuf)); 168 + if (ret != sizeof(*wbuf) || le16_to_cpu(wbuf->size) != msg->len) 169 + return (ret < 0) ? ret : -EIO; 170 + 171 + return 0; 172 + } 173 + 174 + static int usbio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 175 + { 176 + struct usbio_i2c *i2c = i2c_get_adapdata(adap); 177 + int ret; 178 + 179 + usbio_acquire(i2c->adev); 180 + 181 + ret = usbio_i2c_init(adap, msgs); 182 + if (ret) 183 + goto out_release; 184 + 185 + for (int i = 0; i < num; ret = ++i) { 186 + if (msgs[i].flags & I2C_M_RD) 187 + ret = usbio_i2c_read(adap, &msgs[i]); 188 + else 189 + ret = usbio_i2c_write(adap, &msgs[i]); 190 + 191 + if (ret) 192 + break; 193 + } 194 + 195 + usbio_i2c_uninit(adap, msgs); 196 + 197 + out_release: 198 + usbio_release(i2c->adev); 199 + 200 + return ret; 201 + } 202 + 203 + static u32 usbio_i2c_func(struct i2c_adapter *adap) 204 + { 205 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 206 + } 207 + 208 + static const struct i2c_adapter_quirks usbio_i2c_quirks = { 209 + .flags = I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START, 210 + .max_read_len = SZ_4K, 211 + .max_write_len = SZ_4K, 212 + }; 213 + 214 + static const struct i2c_adapter_quirks usbio_i2c_quirks_max_rw_len52 = { 215 + .flags = I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START, 216 + .max_read_len = 52, 217 + .max_write_len = 52, 218 + }; 219 + 220 + static const struct i2c_algorithm usbio_i2c_algo = { 221 + .master_xfer = usbio_i2c_xfer, 222 + .functionality = usbio_i2c_func, 223 + }; 224 + 225 + static int usbio_i2c_probe(struct auxiliary_device *adev, 226 + const struct auxiliary_device_id *adev_id) 227 + { 228 + struct usbio_i2c_bus_desc *i2c_desc; 229 + struct device *dev = &adev->dev; 230 + struct usbio_i2c *i2c; 231 + u32 max_speed; 232 + int ret; 233 + 234 + i2c_desc = dev_get_platdata(dev); 235 + if (!i2c_desc) 236 + return -EINVAL; 237 + 238 + i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL); 239 + if (!i2c) 240 + return -ENOMEM; 241 + 242 + i2c->adev = adev; 243 + 244 + usbio_acpi_bind(i2c->adev, usbio_i2c_acpi_hids); 245 + usbio_get_txrxbuf_len(i2c->adev, &i2c->txbuf_len, &i2c->rxbuf_len); 246 + 247 + i2c->rwbuf = devm_kzalloc(dev, max(i2c->txbuf_len, i2c->rxbuf_len), GFP_KERNEL); 248 + if (!i2c->rwbuf) 249 + return -ENOMEM; 250 + 251 + i2c->quirks = usbio_get_quirks(i2c->adev); 252 + 253 + max_speed = usbio_i2c_speeds[i2c_desc->caps & USBIO_I2C_BUS_MODE_CAP_MASK]; 254 + if (max_speed < I2C_MAX_FAST_MODE_FREQ && 255 + (i2c->quirks & USBIO_QUIRK_I2C_ALLOW_400KHZ)) 256 + max_speed = I2C_MAX_FAST_MODE_FREQ; 257 + 258 + i2c->speed = i2c_acpi_find_bus_speed(dev); 259 + if (!i2c->speed) 260 + i2c->speed = I2C_MAX_STANDARD_MODE_FREQ; 261 + else if (i2c->speed > max_speed) { 262 + dev_warn(dev, "Invalid speed %u adjusting to bus max %u\n", 263 + i2c->speed, max_speed); 264 + i2c->speed = max_speed; 265 + } 266 + 267 + i2c->adap.owner = THIS_MODULE; 268 + i2c->adap.class = I2C_CLASS_HWMON; 269 + i2c->adap.dev.parent = dev; 270 + i2c->adap.algo = &usbio_i2c_algo; 271 + 272 + if (i2c->quirks & USBIO_QUIRK_I2C_MAX_RW_LEN_52) 273 + i2c->adap.quirks = &usbio_i2c_quirks_max_rw_len52; 274 + else 275 + i2c->adap.quirks = &usbio_i2c_quirks; 276 + 277 + snprintf(i2c->adap.name, sizeof(i2c->adap.name), "%s.%d", 278 + USBIO_I2C_CLIENT, i2c->adev->id); 279 + 280 + device_set_node(&i2c->adap.dev, dev_fwnode(&adev->dev)); 281 + 282 + auxiliary_set_drvdata(adev, i2c); 283 + i2c_set_adapdata(&i2c->adap, i2c); 284 + 285 + ret = i2c_add_adapter(&i2c->adap); 286 + if (ret) 287 + return ret; 288 + 289 + if (has_acpi_companion(&i2c->adap.dev)) 290 + acpi_dev_clear_dependencies(ACPI_COMPANION(&i2c->adap.dev)); 291 + 292 + return 0; 293 + } 294 + 295 + static void usbio_i2c_remove(struct auxiliary_device *adev) 296 + { 297 + struct usbio_i2c *i2c = auxiliary_get_drvdata(adev); 298 + 299 + i2c_del_adapter(&i2c->adap); 300 + } 301 + 302 + static const struct auxiliary_device_id usbio_i2c_id_table[] = { 303 + { "usbio.usbio-i2c" }, 304 + { } 305 + }; 306 + MODULE_DEVICE_TABLE(auxiliary, usbio_i2c_id_table); 307 + 308 + static struct auxiliary_driver usbio_i2c_driver = { 309 + .name = USBIO_I2C_CLIENT, 310 + .probe = usbio_i2c_probe, 311 + .remove = usbio_i2c_remove, 312 + .id_table = usbio_i2c_id_table 313 + }; 314 + module_auxiliary_driver(usbio_i2c_driver); 315 + 316 + MODULE_DESCRIPTION("Intel USBIO I2C driver"); 317 + MODULE_AUTHOR("Israel Cepeda <israel.a.cepeda.lopez@intel.com>"); 318 + MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>"); 319 + MODULE_LICENSE("GPL"); 320 + MODULE_IMPORT_NS("USBIO");