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 { "INTC10D2" }, /* MTL-CVF */ 31 { "INTC10E3" }, /* PTL */ 32 { } 33 }; 34 35 static const u32 usbio_i2c_speeds[] = { 36 I2C_MAX_STANDARD_MODE_FREQ, 37 I2C_MAX_FAST_MODE_FREQ, 38 I2C_MAX_FAST_MODE_PLUS_FREQ, 39 I2C_MAX_HIGH_SPEED_MODE_FREQ 40 }; 41 42 static void usbio_i2c_uninit(struct i2c_adapter *adap, struct i2c_msg *msg) 43 { 44 struct usbio_i2c *i2c = i2c_get_adapdata(adap); 45 struct usbio_i2c_uninit ubuf; 46 47 ubuf.busid = i2c->adev->id; 48 ubuf.config = cpu_to_le16(msg->addr); 49 50 usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_UNINIT, true, 51 &ubuf, sizeof(ubuf), NULL, 0); 52 } 53 54 static int usbio_i2c_init(struct i2c_adapter *adap, struct i2c_msg *msg) 55 { 56 struct usbio_i2c *i2c = i2c_get_adapdata(adap); 57 struct usbio_i2c_init ibuf; 58 void *reply_buf; 59 u16 reply_len; 60 int ret; 61 62 ibuf.busid = i2c->adev->id; 63 ibuf.config = cpu_to_le16(msg->addr); 64 ibuf.speed = cpu_to_le32(i2c->speed); 65 66 if (i2c->quirks & USBIO_QUIRK_I2C_NO_INIT_ACK) { 67 reply_buf = NULL; 68 reply_len = 0; 69 } else { 70 reply_buf = &ibuf; 71 reply_len = sizeof(ibuf); 72 } 73 74 ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_INIT, true, 75 &ibuf, sizeof(ibuf), reply_buf, reply_len); 76 if (ret != sizeof(ibuf)) 77 return (ret < 0) ? ret : -EIO; 78 79 return 0; 80 } 81 82 static int usbio_i2c_read(struct i2c_adapter *adap, struct i2c_msg *msg) 83 { 84 struct usbio_i2c *i2c = i2c_get_adapdata(adap); 85 u16 rxchunk = i2c->rxbuf_len - I2C_RW_OVERHEAD; 86 struct usbio_i2c_rw *rbuf = i2c->rwbuf; 87 int ret; 88 89 rbuf->busid = i2c->adev->id; 90 rbuf->config = cpu_to_le16(msg->addr); 91 rbuf->size = cpu_to_le16(msg->len); 92 93 if (msg->len > rxchunk) { 94 /* Need to split the input buffer */ 95 u16 len = 0; 96 97 do { 98 if (msg->len - len < rxchunk) 99 rxchunk = msg->len - len; 100 101 ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, 102 USBIO_I2CCMD_READ, true, 103 rbuf, len == 0 ? sizeof(*rbuf) : 0, 104 rbuf, sizeof(*rbuf) + rxchunk); 105 if (ret < 0) 106 return ret; 107 108 memcpy(&msg->buf[len], rbuf->data, rxchunk); 109 len += rxchunk; 110 } while (msg->len > len); 111 112 return 0; 113 } 114 115 ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_READ, true, 116 rbuf, sizeof(*rbuf), rbuf, sizeof(*rbuf) + msg->len); 117 if (ret != sizeof(*rbuf) + msg->len) 118 return (ret < 0) ? ret : -EIO; 119 120 memcpy(msg->buf, rbuf->data, msg->len); 121 122 return 0; 123 } 124 125 static int usbio_i2c_write(struct i2c_adapter *adap, struct i2c_msg *msg) 126 { 127 struct usbio_i2c *i2c = i2c_get_adapdata(adap); 128 u16 txchunk = i2c->txbuf_len - I2C_RW_OVERHEAD; 129 struct usbio_i2c_rw *wbuf = i2c->rwbuf; 130 int ret; 131 132 if (msg->len > txchunk) { 133 /* Need to split the output buffer */ 134 u16 len = 0; 135 136 do { 137 wbuf->busid = i2c->adev->id; 138 wbuf->config = cpu_to_le16(msg->addr); 139 140 if (i2c->quirks & USBIO_QUIRK_I2C_USE_CHUNK_LEN) 141 wbuf->size = cpu_to_le16(txchunk); 142 else 143 wbuf->size = cpu_to_le16(msg->len); 144 145 memcpy(wbuf->data, &msg->buf[len], txchunk); 146 len += txchunk; 147 148 ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, 149 USBIO_I2CCMD_WRITE, msg->len == len, 150 wbuf, sizeof(*wbuf) + txchunk, 151 wbuf, sizeof(*wbuf)); 152 if (ret < 0) 153 return ret; 154 155 if (msg->len - len < txchunk) 156 txchunk = msg->len - len; 157 } while (msg->len > len); 158 159 return 0; 160 } 161 162 wbuf->busid = i2c->adev->id; 163 wbuf->config = cpu_to_le16(msg->addr); 164 wbuf->size = cpu_to_le16(msg->len); 165 memcpy(wbuf->data, msg->buf, msg->len); 166 167 ret = usbio_bulk_msg(i2c->adev, USBIO_PKTTYPE_I2C, USBIO_I2CCMD_WRITE, true, 168 wbuf, sizeof(*wbuf) + msg->len, wbuf, sizeof(*wbuf)); 169 if (ret != sizeof(*wbuf) || le16_to_cpu(wbuf->size) != msg->len) 170 return (ret < 0) ? ret : -EIO; 171 172 return 0; 173 } 174 175 static int usbio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 176 { 177 struct usbio_i2c *i2c = i2c_get_adapdata(adap); 178 int ret; 179 180 usbio_acquire(i2c->adev); 181 182 ret = usbio_i2c_init(adap, msgs); 183 if (ret) 184 goto out_release; 185 186 for (int i = 0; i < num; ret = ++i) { 187 if (msgs[i].flags & I2C_M_RD) 188 ret = usbio_i2c_read(adap, &msgs[i]); 189 else 190 ret = usbio_i2c_write(adap, &msgs[i]); 191 192 if (ret) 193 break; 194 } 195 196 usbio_i2c_uninit(adap, msgs); 197 198 out_release: 199 usbio_release(i2c->adev); 200 201 return ret; 202 } 203 204 static u32 usbio_i2c_func(struct i2c_adapter *adap) 205 { 206 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 207 } 208 209 static const struct i2c_adapter_quirks usbio_i2c_quirks = { 210 .flags = I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START, 211 .max_read_len = SZ_4K, 212 .max_write_len = SZ_4K, 213 }; 214 215 static const struct i2c_adapter_quirks usbio_i2c_quirks_max_rw_len52 = { 216 .flags = I2C_AQ_NO_ZERO_LEN | I2C_AQ_NO_REP_START, 217 .max_read_len = 52, 218 .max_write_len = 52, 219 }; 220 221 static const struct i2c_algorithm usbio_i2c_algo = { 222 .master_xfer = usbio_i2c_xfer, 223 .functionality = usbio_i2c_func, 224 }; 225 226 static int usbio_i2c_probe(struct auxiliary_device *adev, 227 const struct auxiliary_device_id *adev_id) 228 { 229 struct usbio_i2c_bus_desc *i2c_desc; 230 struct device *dev = &adev->dev; 231 struct usbio_i2c *i2c; 232 u32 max_speed; 233 int ret; 234 235 i2c_desc = dev_get_platdata(dev); 236 if (!i2c_desc) 237 return -EINVAL; 238 239 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL); 240 if (!i2c) 241 return -ENOMEM; 242 243 i2c->adev = adev; 244 245 usbio_acpi_bind(i2c->adev, usbio_i2c_acpi_hids); 246 usbio_get_txrxbuf_len(i2c->adev, &i2c->txbuf_len, &i2c->rxbuf_len); 247 248 i2c->rwbuf = devm_kzalloc(dev, max(i2c->txbuf_len, i2c->rxbuf_len), GFP_KERNEL); 249 if (!i2c->rwbuf) 250 return -ENOMEM; 251 252 i2c->quirks = usbio_get_quirks(i2c->adev); 253 254 max_speed = usbio_i2c_speeds[i2c_desc->caps & USBIO_I2C_BUS_MODE_CAP_MASK]; 255 if (max_speed < I2C_MAX_FAST_MODE_FREQ && 256 (i2c->quirks & USBIO_QUIRK_I2C_ALLOW_400KHZ)) 257 max_speed = I2C_MAX_FAST_MODE_FREQ; 258 259 i2c->speed = i2c_acpi_find_bus_speed(dev); 260 if (!i2c->speed) 261 i2c->speed = I2C_MAX_STANDARD_MODE_FREQ; 262 else if (i2c->speed > max_speed) { 263 dev_warn(dev, "Invalid speed %u adjusting to bus max %u\n", 264 i2c->speed, max_speed); 265 i2c->speed = max_speed; 266 } 267 268 i2c->adap.owner = THIS_MODULE; 269 i2c->adap.class = I2C_CLASS_HWMON; 270 i2c->adap.dev.parent = dev; 271 i2c->adap.algo = &usbio_i2c_algo; 272 273 if (i2c->quirks & USBIO_QUIRK_I2C_MAX_RW_LEN_52) 274 i2c->adap.quirks = &usbio_i2c_quirks_max_rw_len52; 275 else 276 i2c->adap.quirks = &usbio_i2c_quirks; 277 278 snprintf(i2c->adap.name, sizeof(i2c->adap.name), "%s.%d", 279 USBIO_I2C_CLIENT, i2c->adev->id); 280 281 device_set_node(&i2c->adap.dev, dev_fwnode(&adev->dev)); 282 283 auxiliary_set_drvdata(adev, i2c); 284 i2c_set_adapdata(&i2c->adap, i2c); 285 286 ret = i2c_add_adapter(&i2c->adap); 287 if (ret) 288 return ret; 289 290 if (has_acpi_companion(&i2c->adap.dev)) 291 acpi_dev_clear_dependencies(ACPI_COMPANION(&i2c->adap.dev)); 292 293 return 0; 294 } 295 296 static void usbio_i2c_remove(struct auxiliary_device *adev) 297 { 298 struct usbio_i2c *i2c = auxiliary_get_drvdata(adev); 299 300 i2c_del_adapter(&i2c->adap); 301 } 302 303 static const struct auxiliary_device_id usbio_i2c_id_table[] = { 304 { "usbio.usbio-i2c" }, 305 { } 306 }; 307 MODULE_DEVICE_TABLE(auxiliary, usbio_i2c_id_table); 308 309 static struct auxiliary_driver usbio_i2c_driver = { 310 .name = USBIO_I2C_CLIENT, 311 .probe = usbio_i2c_probe, 312 .remove = usbio_i2c_remove, 313 .id_table = usbio_i2c_id_table 314 }; 315 module_auxiliary_driver(usbio_i2c_driver); 316 317 MODULE_DESCRIPTION("Intel USBIO I2C driver"); 318 MODULE_AUTHOR("Israel Cepeda <israel.a.cepeda.lopez@intel.com>"); 319 MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>"); 320 MODULE_LICENSE("GPL"); 321 MODULE_IMPORT_NS("USBIO"); 322