1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2026 Intel Corporation 4 */ 5 6 #include <linux/acpi.h> 7 #include <linux/cleanup.h> 8 #include <linux/delay.h> 9 #include <linux/device.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/i2c.h> 12 #include <linux/init.h> 13 #include <linux/interrupt.h> 14 #include <linux/jiffies.h> 15 #include <linux/mod_devicetable.h> 16 #include <linux/module.h> 17 #include <linux/pci.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/slab.h> 21 #include <linux/time64.h> 22 #include <linux/workqueue.h> 23 24 #include <media/ipu-bridge.h> 25 #include <media/ipu6-pci-table.h> 26 27 #include "icvs.h" 28 29 /* Command timeouts determined experimentally */ 30 #define CMD_TIMEOUT (5 * HZ) 31 #define FW_READY_DELAY_MS 100 32 33 #define PCI_DEVICE_ID_INTEL_IPU7 0x645d /* MTL / LNL */ 34 #define PCI_DEVICE_ID_INTEL_IPU7P5 0xb05d /* ARL / PTL */ 35 36 /* 37 * IPU7 PCI device IDs not covered by ipu6_pci_tbl in ipu6-pci-table.h. 38 * Once the IPU6 driver gains support for IPU7, this table can be dropped. 39 */ 40 static const struct pci_device_id icvs_ipu7_tbl[] = { 41 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IPU7) }, 42 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IPU7P5) }, 43 { } 44 }; 45 46 static const struct acpi_gpio_params gpio_wake = { 0, 0, false }; 47 static const struct acpi_gpio_params gpio_rst = { 1, 0, false }; 48 static const struct acpi_gpio_params gpio_req = { 2, 0, false }; 49 static const struct acpi_gpio_params gpio_resp = { 3, 0, false }; 50 static const struct acpi_gpio_mapping icvs_acpi_gpios[] = { 51 { "wake-gpio", &gpio_wake, 1 }, 52 { "rst-gpio", &gpio_rst, 1 }, 53 { "req-gpio", &gpio_req, 1 }, 54 { "resp-gpio", &gpio_resp, 1 }, 55 { } 56 }; 57 58 static const struct acpi_gpio_params lgpio_req = { 0, 0, false }; 59 static const struct acpi_gpio_params lgpio_resp = { 1, 0, false }; 60 static const struct acpi_gpio_mapping icvs_acpi_lgpios[] = { 61 { "req-gpio", &lgpio_req, 1 }, 62 { "resp-gpio", &lgpio_resp, 1 }, 63 { } 64 }; 65 66 /* Device quirk table */ 67 static const struct icvs_device_quirk cvs_quirk_table[] = { 68 { 0x2ac1, 0x20d0, ICVS_NO_MIPI_CONFIG | 69 ICVS_NO_CAPS | 70 ICVS_NO_FW_UPDATE 71 }, /* Lattice NX33 */ 72 { 0x06CB, 0x0701, ICVS_SKIP_FW_RESET | 73 ICVS_HOST_SENSOR_PWR_CTRL | 74 ICVS_HOST_PRIV_CTRL | 75 ICVS_FW_BUF_SIZE_256 | 76 ICVS_FW_HEADER_SIZE_256 77 }, /* Synaptics SVP7xxx */ 78 { } 79 }; 80 81 /** 82 * cvs_set_quirks - Match device VID/PID and set quirks 83 * @ctx: CVS device context 84 * @vid: Vendor ID 85 * @pid: Product ID 86 * 87 * Searches the quirk table for a matching VID/PID and populates ctx->quirks 88 * with the corresponding quirk flags. 89 * If no match is found, quirks is set to 0. 90 */ 91 static void cvs_set_quirks(struct icvs *ctx, u16 vid, u16 pid) 92 { 93 ctx->quirks = 0; 94 95 for (unsigned int i = 0; i < ARRAY_SIZE(cvs_quirk_table); i++) { 96 if (cvs_quirk_table[i].vid == vid && 97 cvs_quirk_table[i].pid == pid) { 98 ctx->quirks = cvs_quirk_table[i].quirks; 99 dev_info(cvs_dev(ctx), 100 "Quirks: 0x%lx (VID:0x%04x PID:0x%04x)\n", 101 ctx->quirks, vid, pid); 102 return; 103 } 104 } 105 106 dev_info(cvs_dev(ctx), 107 "No quirks for device (VID:0x%04x PID:0x%04x)\n", vid, pid); 108 } 109 110 /* I2C transport helpers */ 111 112 /** 113 * cvs_read_i2c - Issue a read-type command and fetch device response 114 * @ctx: CVS device context 115 * @cmd_id: Command identifier (big endian) 116 * @resp: Destination buffer for response payload 117 * @size: Size of payload to read into @resp (without prefix) 118 * 119 * Sends @cmd_id and reads back the response in a single I2C transaction. 120 * When the device prepends a 4-byte protocol prefix, the combined 121 * prefix+payload is read into a temporary buffer and only the payload is 122 * copied to @resp, avoiding any dependency on the layout of the caller's 123 * buffer. 124 * 125 * Return: 0 on success or negative errno. 126 */ 127 static int cvs_read_i2c(struct icvs *ctx, __be16 cmd_id, void *resp, 128 size_t size) 129 { 130 size_t prefix_size = ctx->prefix ? sizeof(u32) : 0; 131 size_t read_size = size + prefix_size; 132 struct i2c_client *i2c = ctx->i2c_client; 133 u8 *buf __free(kfree) = NULL; 134 int cnt; 135 136 if (!resp || !size) 137 return -EINVAL; 138 139 cnt = i2c_master_send(i2c, (const char *)&cmd_id, sizeof(cmd_id)); 140 if (cnt != sizeof(cmd_id)) 141 return cnt < 0 ? cnt : -EIO; 142 143 buf = kmalloc(read_size, GFP_KERNEL); 144 if (!buf) 145 return -ENOMEM; 146 147 cnt = i2c_master_recv(i2c, buf, read_size); 148 if (cnt != read_size) { 149 dev_dbg(cvs_dev(ctx), "recv cmd 0x%04x short read (%d/%zu)\n", 150 be16_to_cpu(cmd_id), cnt, read_size); 151 return cnt < 0 ? cnt : -EIO; 152 } 153 154 memcpy(resp, buf + prefix_size, size); 155 156 return 0; 157 } 158 159 /** 160 * cvs_write_i2c - Write a raw command buffer to the device over I2C 161 * @ctx: CVS device context 162 * @data: Buffer containing command + payload 163 * @size: Total bytes to write 164 * 165 * Return: 0 on success or negative errno. 166 */ 167 static int cvs_write_i2c(struct icvs *ctx, const void *data, int size) 168 { 169 struct i2c_client *i2c = ctx->i2c_client; 170 int cnt; 171 172 if (size < 0 || !data) 173 return -EINVAL; 174 175 cnt = i2c_master_send(i2c, data, size); 176 if (cnt != size) { 177 dev_dbg(cvs_dev(ctx), "send short (%d/%d)\n", cnt, size); 178 return cnt < 0 ? cnt : -EIO; 179 } 180 181 return 0; 182 } 183 184 /** 185 * cvs_checksum - Simple additive checksum helper 186 * @data: 32-bit aligned data buffer 187 * @len: Length in bytes (multiple of 4) 188 * 189 * Return: 32-bit additive checksum of the dwords in @data. 190 */ 191 static u32 cvs_checksum(const void *data, size_t len) 192 { 193 const u32 *words = data; 194 u32 csum = 0; 195 196 if (WARN_ON_ONCE(len % sizeof(u32))) 197 return 0; 198 199 for (unsigned int i = 0; i < len / sizeof(u32); i++) 200 csum += words[i]; 201 202 return csum; 203 } 204 205 /** 206 * cvs_schedule_and_wait - Schedule polling work then wait for completion 207 * @ctx: CVS device context 208 * @work_delay_ms: Delay in milliseconds before polling work executes 209 * @wait_jiffies: Timeout (jiffies) to wait for cmd completion 210 * 211 * Queues ctx->work to run after @work_delay_ms and then waits up to 212 * @wait_jiffies for ctx->cmd_completion. 213 * 214 * Return: 0 on success, -ETIMEDOUT on timeout, negative errno on error. 215 */ 216 static int cvs_schedule_and_wait(struct icvs *ctx, unsigned int work_delay_ms, 217 unsigned long wait_jiffies) 218 { 219 int ret; 220 221 schedule_delayed_work(&ctx->work, msecs_to_jiffies(work_delay_ms)); 222 ret = wait_for_completion_killable_timeout(&ctx->cmd_completion, 223 wait_jiffies); 224 if (ret < 0) 225 return ret; 226 if (!ret) 227 return -ETIMEDOUT; 228 229 return 0; 230 } 231 232 /** 233 * cvs_wait_wake_or_sleep - Wait for wake IRQ or sleep fallback 234 * @ctx: CVS device context 235 * @timeout_jiffies: Timeout (jiffies) for wake event on full-cap devices 236 * @sleep_ms: Milliseconds to sleep on light-cap devices 237 * 238 * For full capability devices (ICVS_FULLCAP) waits interruptibly on 239 * ctx->hostwake_event until ctx->hostwake_event_arg becomes true or 240 * @timeout_jiffies elapses. The flag is cleared after the wait. 241 * For light capability devices performs a blocking msleep(@sleep_ms). 242 * 243 * Return codes normalized for caller switch handling: 244 * <0 : error / interrupted 245 * -ETIMEDOUT : timeout (wake event not observed) 246 * 0 : success (wake observed OR light-cap sleep elapsed) 247 * 248 * Return: negative errno, -ETIMEDOUT on timeout, 0 on success. 249 */ 250 static int cvs_wait_wake_or_sleep(struct icvs *ctx, 251 unsigned long timeout_jiffies, 252 unsigned int sleep_ms) 253 { 254 int ret; 255 256 if (ctx->res == ICVS_FULLCAP) { 257 ret = wait_event_interruptible_timeout(ctx->hostwake_event, 258 ctx->hostwake_event_arg, 259 timeout_jiffies); 260 ctx->hostwake_event_arg = false; 261 if (ret < 0) 262 return ret; 263 if (!ret) 264 return -ETIMEDOUT; 265 return 0; 266 } 267 268 msleep(sleep_ms); 269 270 return 0; /* treat sleep path as success */ 271 } 272 273 /** 274 * cvs_config_mipi - Send a HOST_SET_MIPI_CONFIG command 275 * @ctx: CVS device context 276 * @c: Command container with conf field populated 277 * @len: Length of original command structure (unused except for symmetry) 278 * 279 * Packages @c->param.conf into a cvs_mipi_data_packet including size and 280 * checksum, then writes it to the device. 281 * 282 * Firmware note: the CVS firmware expects the MIPI configuration to be 283 * sent as a single transaction, including all relevant parameters and checksum. 284 * 285 * Return: 0 on success, NO_MIPI_CONFIG or negative errno from I2C write. 286 */ 287 static int cvs_config_mipi(struct icvs *ctx, struct icvs_cmd *c, size_t len) 288 { 289 struct icvs_mipi_data_packet pkt = { 290 .cmd_id = c->cmd_id, 291 .size = sizeof(c->param.conf), 292 .crc = cvs_checksum(&c->param.conf, sizeof(c->param.conf)), 293 .conf = c->param.conf, 294 }; 295 296 if (ctx->quirks & ICVS_NO_MIPI_CONFIG) 297 return 0; 298 299 return cvs_write_i2c(ctx, &pkt, sizeof(pkt)); 300 } 301 302 /** 303 * cvs_get_device_state - Query current device state bitfield 304 * @ctx: CVS device context 305 * @state: Returned state value 306 * 307 * Issues GET_DEV_STATE and fills @state. 308 * 309 * Return: 0 on success or negative errno. 310 */ 311 static int cvs_get_device_state(struct icvs *ctx, u8 *state) 312 { 313 struct icvs_resp n = { 314 .cmd_id = cpu_to_be16(ICVS_GET_DEV_STATE), 315 }; 316 int ret; 317 318 ret = cvs_read_i2c(ctx, n.cmd_id, &n.resp.state, sizeof(n.resp.state)); 319 if (ret) 320 return ret; 321 322 *state = n.resp.state; 323 324 return 0; 325 } 326 327 /** 328 * cvs_get_device_caps - Read protocol capabilities 329 * @ctx: CVS device context 330 * @caps: Capability structure to populate 331 * 332 * Return: 0 on success or negative errno. 333 */ 334 static int cvs_get_device_caps(struct icvs *ctx, 335 struct icvs_dev_capabilities *caps) 336 { 337 struct icvs_resp n = { 338 .cmd_id = cpu_to_be16(ICVS_GET_DEV_CAPABILITY), 339 }; 340 int ret; 341 342 if (ctx->quirks & ICVS_NO_CAPS) 343 return 0; 344 345 ret = cvs_read_i2c(ctx, n.cmd_id, &n.resp.cap, sizeof(n.resp.cap)); 346 if (ret) 347 return ret; 348 349 *caps = n.resp.cap; 350 351 return 0; 352 } 353 354 /** 355 * cvs_hw_init - Probe device for prefix support and apply quirks 356 * @ctx: CVS device context 357 * 358 * Sends GET_DEV_VID_PID and probes for a 32-bit prefix. 359 * If it matches ICVS_PREFIX_VAL, sets ctx->prefix for subsequent reads. 360 * Then reads VID/PID and applies matching quirks. 361 * GET_DEV_VID_PID is supported by all protocol versions. 362 * 363 * Return: 0 on success or negative errno. 364 */ 365 static int cvs_hw_init(struct icvs *ctx) 366 { 367 struct icvs_resp n = { }; 368 __be16 cmd = cpu_to_be16(ICVS_GET_DEV_VID_PID); 369 u32 resp; 370 int ret; 371 372 /* 373 * Clear prefix so cvs_read_i2c always reads exactly sizeof(u32) bytes 374 * here, regardless of any value left over from a previous call (e.g. 375 * on resume). 376 */ 377 ctx->prefix = false; 378 ret = cvs_read_i2c(ctx, cmd, &resp, sizeof(resp)); 379 if (ret) 380 return ret; 381 382 ctx->prefix = resp == ICVS_PREFIX_VAL; 383 384 /* Now read VID/PID to apply quirks */ 385 ret = cvs_read_i2c(ctx, cmd, 386 &n.resp.vid_pid, sizeof(n.resp.vid_pid)); 387 if (ret) 388 return ret; 389 390 cvs_set_quirks(ctx, n.resp.vid_pid.v_id, n.resp.vid_pid.p_id); 391 392 return 0; 393 } 394 395 /** 396 * cvs_irq_handler - Wake IRQ handler (full capability devices) 397 * @irq: IRQ number 398 * @dev_id: Device context pointer 399 * 400 * Sets a waitqueue flag and wakes up sleeping waiters. 401 * 402 * Return: IRQ_HANDLED always. 403 */ 404 static irqreturn_t cvs_irq_handler(int irq, void *dev_id) 405 { 406 struct icvs *ctx = dev_id; 407 408 ctx->hostwake_event_arg = true; 409 wake_up_interruptible(&ctx->hostwake_event); 410 411 return IRQ_HANDLED; 412 } 413 414 /** 415 * cvs_reset - Toggle reset GPIO for full capability devices 416 * @ctx: CVS device context 417 * 418 * Drives reset low briefly then high if device resources indicate full 419 * capability. Light devices have no reset line. 420 */ 421 static void cvs_reset(struct icvs *ctx) 422 { 423 if (ctx->quirks & ICVS_SKIP_FW_RESET) 424 return; 425 426 if (ctx->res == ICVS_FULLCAP) { 427 gpiod_set_value(ctx->rst, 0); 428 fsleep(2000); 429 gpiod_set_value(ctx->rst, 1); 430 } 431 } 432 433 /** 434 * cvs_recv - Delayed work handler polling for command completion 435 * @work: Embedded delayed_work member 436 * 437 * Re-reads device state; if device_busy remains set, re-schedules itself. 438 * Otherwise stores state into wq_resp and completes the command. 439 */ 440 static void cvs_recv(struct work_struct *work) 441 { 442 struct icvs *ctx = container_of(work, struct icvs, work.work); 443 u8 state = 0; 444 int ret; 445 446 ret = cvs_get_device_state(ctx, &state); 447 if (ret < 0) { 448 dev_dbg(cvs_dev(ctx), "state read failed: %d\n", ret); 449 return; 450 } 451 452 if (state & ICVS_DEV_STATE_BUSY) { 453 dev_dbg(cvs_dev(ctx), "device busy, reschedule\n"); 454 schedule_delayed_work(&ctx->work, 455 msecs_to_jiffies(FW_READY_DELAY_MS)); 456 return; 457 } 458 459 ctx->wq_resp.resp.state = state; 460 complete(&ctx->cmd_completion); 461 } 462 463 /** 464 * cvs_send - Common command submission path 465 * @ctx: CVS device context 466 * @cmd: Command buffer (icvs_cmd) with cmd_id and param populated 467 * @len: Buffer length 468 * 469 * Dispatches a set of supported commands: 470 * - ICVS_SET_DEV_HOST_ID, 471 * - ICVS_HOST_SENSOR_OWNER, 472 * - ICVS_HOST_SET_MIPI_CONFIG 473 * - ICVS_FW_LOADER_* 474 * 475 * For I2C based commands it sets big-endian cmd ids, writes to the device 476 * and waits (via delayed work) for completion or timeout. 477 * GPIO based ownership toggles are handled locally. 478 * 479 * Caller must hold ctx->lock when invoking this function and check for i2c 480 * bus availability. 481 * 482 * Return: 0 on success, negative errno, -EINVAL for unsupported command 483 * or status from device in ctx->wq_resp. 484 */ 485 int cvs_send(struct icvs *ctx, struct icvs_cmd *cmd, size_t len) 486 { 487 int ret, status = 0; 488 489 lockdep_assert_held(&ctx->lock); 490 491 dev_dbg(cvs_dev(ctx), "send cmd = 0x%04x", be16_to_cpu(cmd->cmd_id)); 492 493 reinit_completion(&ctx->cmd_completion); 494 495 switch (be16_to_cpu(cmd->cmd_id)) { 496 case ICVS_SET_DEV_HOST_ID: 497 cmd->cmd_id = cpu_to_be16(ICVS_SET_DEV_HOST_ID); 498 ret = cvs_write_i2c(ctx, cmd, len); 499 if (ret < 0) 500 break; 501 502 ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS, 503 CMD_TIMEOUT); 504 if (ret < 0) 505 break; 506 507 status = ctx->wq_resp.resp.state & 508 ICVS_DEV_STATE_ERROR ? -EINVAL : 0; 509 break; 510 case ICVS_HOST_SENSOR_OWNER: 511 gpiod_set_value_cansleep(ctx->req, cmd->param.param); 512 fsleep(FW_READY_DELAY_MS * USEC_PER_MSEC); 513 ret = gpiod_get_value_cansleep(ctx->resp); 514 status = cmd->param.param == ret ? 0 : -EINVAL; 515 ret = 0; /* success */ 516 break; 517 case ICVS_HOST_SET_MIPI_CONFIG: 518 cmd->cmd_id = cpu_to_be16(ICVS_HOST_SET_MIPI_CONFIG); 519 ret = cvs_config_mipi(ctx, cmd, len); 520 if (ret < 0) 521 break; 522 523 ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS, 524 CMD_TIMEOUT); 525 status = (ctx->wq_resp.resp.state & 526 ICVS_DEV_STATE_ERROR) ? -EINVAL : 0; 527 break; 528 case ICVS_FW_LOADER_START: 529 cmd->cmd_id = cpu_to_be16(ICVS_FW_LOADER_START); 530 ret = cvs_write_i2c(ctx, cmd, len); 531 if (ret < 0) 532 break; 533 534 ret = cvs_wait_wake_or_sleep(ctx, CMD_TIMEOUT, 535 FW_READY_DELAY_MS); 536 if (ret) 537 break; 538 539 ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS, 540 CMD_TIMEOUT); 541 status = (ctx->wq_resp.resp.state & 542 ICVS_DEV_STATE_DOWNLOAD) ? 0 : -EINVAL; 543 break; 544 case ICVS_FW_LOADER_DATA: 545 /* Quirk for older protocols */ 546 if (ctx->caps.protocol_version_major >= 2 && 547 ctx->caps.protocol_version_minor >= 2) { 548 cmd->cmd_id = cpu_to_be16(ICVS_FW_LOADER_DATA); 549 ret = cvs_write_i2c(ctx, cmd, len); 550 } else { 551 ret = cvs_write_i2c(ctx, &cmd->param, 552 len - sizeof(cmd->cmd_id)); 553 } 554 555 if (ret < 0) 556 return ret; 557 558 ret = cvs_wait_wake_or_sleep(ctx, FW_READY_DELAY_MS, 559 FW_READY_DELAY_MS); 560 if (ret) 561 break; 562 563 ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS, 564 CMD_TIMEOUT); 565 status = ctx->wq_resp.resp.state & 566 ICVS_DEV_STATE_ERROR ? -EINVAL : 0; 567 break; 568 case ICVS_FW_LOADER_END: 569 cmd->cmd_id = cpu_to_be16(ICVS_FW_LOADER_END); 570 ret = cvs_write_i2c(ctx, cmd, len); 571 if (ret < 0) 572 break; 573 574 ret = cvs_wait_wake_or_sleep(ctx, CMD_TIMEOUT, 575 FW_READY_DELAY_MS); 576 if (ret) 577 break; 578 579 ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS, 580 CMD_TIMEOUT); 581 status = !(ctx->wq_resp.resp.state & 582 ICVS_DEV_STATE_DOWNLOAD) ? 0 : -EINVAL; 583 break; 584 default: 585 ret = -EINVAL; 586 break; 587 } 588 589 if (ret < 0) 590 return ret; 591 592 return ctx->wq_resp.status = status; 593 } 594 595 /** 596 * cvs_set_link_owner - Switch CSI-2 link ownership between host and device 597 * @ctx: CVS device context 598 * @owner: Desired owner (ICVS_CSI_LINK_HOST or ICVS_CSI_LINK_CVS) 599 * 600 * Called from runtime PM callbacks to claim or release the CSI-2 link. 601 * Also callable directly for error recovery paths. 602 * 603 * Return: 0 on success or negative errno. 604 */ 605 int cvs_set_link_owner(struct icvs *ctx, enum icvs_csi_link_owner owner) 606 { 607 struct icvs_cmd cmd = { 608 .cmd_id = cpu_to_be16(ICVS_HOST_SENSOR_OWNER), 609 .param.param = owner, 610 }; 611 size_t cmd_size = sizeof(cmd.cmd_id) + sizeof(cmd.param.param); 612 613 guard(mutex)(&ctx->lock); 614 return cvs_send(ctx, &cmd, cmd_size); 615 } 616 617 /** 618 * cvs_configure_dev_caps - Configure device capability ownership bits 619 * @ctx: CVS device context 620 * 621 * Tells the CVS device which of its features (privacy LED, RGB camera 622 * power-up, vision sensing) are controlled by the host, then sends 623 * SET_DEV_HOST_ID. 624 * 625 * Return: 0 on success or negative errno. 626 */ 627 static int cvs_configure_dev_caps(struct icvs *ctx) 628 { 629 struct icvs_cmd cmd = { .cmd_id = cpu_to_be16(ICVS_SET_DEV_HOST_ID) }; 630 size_t sz = sizeof(cmd.cmd_id) + sizeof(cmd.param.host_id); 631 632 if (ctx->quirks & ICVS_NO_CAPS) 633 return 0; 634 635 if (ctx->quirks & ICVS_HOST_VISION_SENSING) 636 cmd.param.host_id |= ICVS_HOST_ID_VISION_SENSING; 637 if (ctx->quirks & ICVS_HOST_PRIV_CTRL) 638 cmd.param.host_id |= ICVS_HOST_ID_PRIVACY_LED; 639 if (ctx->quirks & ICVS_HOST_SENSOR_PWR_CTRL) 640 cmd.param.host_id |= ICVS_HOST_ID_RGBCAMERA_PWRUP; 641 642 guard(mutex)(&ctx->lock); 643 return cvs_send(ctx, &cmd, sz); 644 } 645 646 /** 647 * cvs_core_probe - Shared probe path for I2C & platform instantiation 648 * @dev: Parent device 649 * @i2c: I2C client (NULL for platform devices) 650 * 651 * Discovers IPU, parses ACPI resources, sets up GPIOs/IRQs, initializes 652 * sub-device (CSI) and host identifier, and exposes sysfs firmware interface. 653 * 654 * Return: 0 on success or negative errno. 655 */ 656 static int cvs_core_probe(struct device *dev, struct i2c_client *i2c) 657 { 658 struct pci_dev *ipu = NULL; 659 struct icvs *ctx; 660 int ret; 661 662 /* Locate IPU device */ 663 for (unsigned int i = 0; !ipu && ipu6_pci_tbl[i].vendor; i++) 664 ipu = pci_get_device(ipu6_pci_tbl[i].vendor, 665 ipu6_pci_tbl[i].device, NULL); 666 for (unsigned int i = 0; !ipu && icvs_ipu7_tbl[i].vendor; i++) 667 ipu = pci_get_device(icvs_ipu7_tbl[i].vendor, 668 icvs_ipu7_tbl[i].device, NULL); 669 if (!ipu) 670 return -ENODEV; 671 672 ret = ipu_bridge_init(&ipu->dev, ipu_bridge_parse_ssdb); 673 if (ret < 0) 674 goto err_put_ipu; 675 676 if (!dev_fwnode(dev)) { 677 ret = -ENXIO; 678 goto err_put_ipu; 679 } 680 681 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 682 if (!ctx) { 683 ret = -ENOMEM; 684 goto err_put_ipu; 685 } 686 687 ctx->i2c_client = i2c; 688 689 ret = gpiod_count(dev, NULL); 690 switch (ret) { 691 case ICVS_GPIO_SYNC: 692 ctx->res = ICVS_LIGHTCAP; 693 break; 694 case ICVS_GPIO_ASYNC: 695 ctx->res = ICVS_FULLCAP; 696 break; 697 default: 698 dev_err(dev, "unexpected GPIO count %d\n", ret); 699 ret = -EINVAL; 700 goto err_put_ipu; 701 } 702 703 ret = devm_acpi_dev_add_driver_gpios(dev, 704 ctx->res == ICVS_FULLCAP ? 705 icvs_acpi_gpios : 706 icvs_acpi_lgpios); 707 if (ret) { 708 dev_err_probe(dev, ret, "failed to add ACPI GPIOs\n"); 709 goto err_put_ipu; 710 } 711 712 ctx->req = devm_gpiod_get(dev, "req", GPIOD_OUT_HIGH); 713 if (IS_ERR(ctx->req)) { 714 ret = dev_err_probe(dev, PTR_ERR(ctx->req), 715 "failed to get req GPIO\n"); 716 goto err_put_ipu; 717 } 718 719 ctx->resp = devm_gpiod_get(dev, "resp", GPIOD_IN); 720 if (IS_ERR(ctx->resp)) { 721 ret = dev_err_probe(dev, PTR_ERR(ctx->resp), 722 "failed to get resp GPIO\n"); 723 goto err_put_ipu; 724 } 725 726 if (ctx->res == ICVS_FULLCAP) { 727 struct gpio_desc *wake; 728 729 ctx->rst = devm_gpiod_get(dev, "rst", GPIOD_OUT_HIGH); 730 if (IS_ERR(ctx->rst)) { 731 ret = dev_err_probe(dev, PTR_ERR(ctx->rst), 732 "failed to get rst GPIO\n"); 733 goto err_put_ipu; 734 } 735 736 wake = devm_gpiod_get(dev, "wake", GPIOD_IN); 737 if (IS_ERR(wake)) { 738 ret = dev_err_probe(dev, PTR_ERR(wake), 739 "failed to get wake GPIO\n"); 740 goto err_put_ipu; 741 } 742 743 ctx->irq = gpiod_to_irq(wake); 744 if (ctx->irq < 0) { 745 ret = dev_err_probe(dev, ctx->irq, 746 "failed to get wake IRQ\n"); 747 goto err_put_ipu; 748 } 749 750 ret = devm_request_threaded_irq(dev, ctx->irq, NULL, 751 cvs_irq_handler, 752 IRQF_ONESHOT | IRQF_NO_SUSPEND, 753 "cvs_wake", ctx); 754 if (ret) { 755 dev_err_probe(dev, ret, "failed to request IRQ\n"); 756 goto err_put_ipu; 757 } 758 } 759 760 ret = devm_mutex_init(dev, &ctx->lock); 761 if (ret) 762 goto err_put_ipu; 763 764 init_completion(&ctx->cmd_completion); 765 init_waitqueue_head(&ctx->hostwake_event); 766 INIT_DELAYED_WORK(&ctx->work, cvs_recv); 767 768 if (i2c) { 769 ret = cvs_hw_init(ctx); 770 if (ret) { 771 dev_err(dev, "HW init failed (%d)\n", ret); 772 /* 773 * Fallback to GPIO-only mode. 774 * Some BIOS show the device on the I2C bus, however, 775 * the device is not accessible via I2C. 776 */ 777 ctx->i2c_client = NULL; 778 goto fail_i2c; 779 } 780 781 ret = cvs_get_device_caps(ctx, &ctx->caps); 782 if (ret) { 783 dev_err_probe(dev, ret, "get caps failed\n"); 784 goto err_put_ipu; 785 } 786 787 ret = cvs_configure_dev_caps(ctx); 788 if (ret) { 789 dev_err_probe(dev, ret, 790 "configure dev caps failed\n"); 791 goto err_put_ipu; 792 } 793 } 794 795 fail_i2c: 796 ret = cvs_csi_init(ctx, dev, i2c); 797 if (ret) { 798 dev_err_probe(dev, ret, "CSI init failed\n"); 799 goto err_put_ipu; 800 } 801 802 dev_set_drvdata(dev, ctx); 803 pm_runtime_set_autosuspend_delay(dev, 1000); 804 pm_runtime_use_autosuspend(dev); 805 pm_runtime_enable(dev); 806 pm_runtime_idle(dev); 807 808 /* 809 * Create a PM runtime device link with IPU as consumer and CVS as 810 * supplier. When the IPU runtime-resumes to start streaming, the PM 811 * framework automatically resumes CVS first, triggering 812 * cvs_runtime_resume() which hands CSI-2 link ownership to the host. 813 */ 814 ctx->ipu_link = device_link_add(&ipu->dev, dev, 815 DL_FLAG_PM_RUNTIME | 816 DL_FLAG_RPM_ACTIVE | 817 DL_FLAG_STATELESS); 818 if (!ctx->ipu_link) { 819 dev_err(dev, "IPU device link failed\n"); 820 ret = -ENODEV; 821 goto err_csi_remove; 822 } 823 824 if (has_acpi_companion(dev)) 825 acpi_dev_clear_dependencies(ACPI_COMPANION(dev)); 826 827 put_device(&ipu->dev); 828 829 return 0; 830 831 err_csi_remove: 832 if (ctx->ipu_link) 833 device_link_del(ctx->ipu_link); 834 cvs_csi_remove(ctx); 835 pm_runtime_dont_use_autosuspend(dev); 836 pm_runtime_disable(dev); 837 pm_runtime_set_suspended(dev); 838 839 err_put_ipu: 840 put_device(&ipu->dev); 841 842 return ret; 843 } 844 845 /** 846 * cvs_probe - I2C driver probe entry 847 * @i2c: I2C client 848 * 849 * Return: 0 on success or negative errno. 850 */ 851 static int cvs_probe(struct i2c_client *i2c) 852 { 853 return cvs_core_probe(&i2c->dev, i2c); 854 } 855 856 /** 857 * cvs_core_remove - Shared remove logic 858 * @dev: Device 859 */ 860 static void cvs_core_remove(struct device *dev) 861 { 862 struct icvs *ctx = dev_get_drvdata(dev); 863 864 cancel_delayed_work_sync(&ctx->work); 865 cvs_csi_remove(ctx); 866 867 if (ctx->ipu_link) 868 device_link_del(ctx->ipu_link); 869 870 pm_runtime_put_noidle(dev); 871 pm_runtime_disable(dev); 872 pm_runtime_set_suspended(dev); 873 874 cvs_reset(ctx); 875 } 876 877 /** 878 * cvs_remove - I2C driver remove 879 * @client: I2C client 880 */ 881 static void cvs_remove(struct i2c_client *client) 882 { 883 cvs_core_remove(&client->dev); 884 } 885 886 /** 887 * cvs_suspend - System suspend callback 888 * @dev: Device 889 * 890 * Return: 0. 891 */ 892 static int __maybe_unused cvs_suspend(struct device *dev) 893 { 894 struct icvs *ctx = dev_get_drvdata(dev); 895 896 cancel_delayed_work_sync(&ctx->work); 897 898 return 0; 899 } 900 901 /** 902 * cvs_resume - System resume callback 903 * @dev: Device 904 * 905 * Re-validates I2C link prefix and re-sends host id if transport available. 906 * 907 * Return: 0 on success or negative errno if I2C check fails. 908 */ 909 static int __maybe_unused cvs_resume(struct device *dev) 910 { 911 struct icvs *ctx = dev_get_drvdata(dev); 912 int ret; 913 914 if (ctx->i2c_client) { 915 ret = cvs_hw_init(ctx); 916 if (ret) 917 return ret; 918 return cvs_configure_dev_caps(ctx); 919 } 920 921 return 0; 922 } 923 924 /** 925 * cvs_runtime_resume - Runtime PM resume: claim CSI-2 link ownership 926 * @dev: Device 927 * 928 * Triggered automatically when the IPU (consumer) runtime-resumes, because 929 * a DL_FLAG_PM_RUNTIME device link makes CVS the supplier. Transfers CSI-2 930 * link ownership to the host so the IPU can start receiving sensor frames. 931 * 932 * Return: 0 on success or negative errno. 933 */ 934 static int __maybe_unused cvs_runtime_resume(struct device *dev) 935 { 936 struct icvs *ctx = dev_get_drvdata(dev); 937 938 return cvs_set_link_owner(ctx, ICVS_CSI_LINK_HOST); 939 } 940 941 /** 942 * cvs_runtime_suspend - Runtime PM suspend: release CSI-2 link ownership 943 * @dev: Device 944 * 945 * Called when the streaming reference is dropped by cvs_csi_disable_streams 946 * via pm_runtime_put_autosuspend. Returns CSI-2 link ownership to CVS firmware. 947 * 948 * Return: 0 on success or negative errno. 949 */ 950 static int __maybe_unused cvs_runtime_suspend(struct device *dev) 951 { 952 struct icvs *ctx = dev_get_drvdata(dev); 953 954 return cvs_set_link_owner(ctx, ICVS_CSI_LINK_CVS); 955 } 956 957 static const struct dev_pm_ops __maybe_unused cvs_pm_ops = { 958 SET_SYSTEM_SLEEP_PM_OPS(cvs_suspend, cvs_resume) 959 SET_RUNTIME_PM_OPS(cvs_runtime_suspend, cvs_runtime_resume, NULL) 960 }; 961 962 static const struct acpi_device_id intel_cvs_acpi_match[] = { 963 { "INTC10DE" }, /* LNL */ 964 { "INTC10E0" }, /* ARL */ 965 { "INTC10E1" }, /* PTL */ 966 { } 967 }; 968 MODULE_DEVICE_TABLE(acpi, intel_cvs_acpi_match); 969 970 static struct i2c_driver cvs_driver = { 971 .driver = { 972 .name = "intel_cvs", 973 .acpi_match_table = intel_cvs_acpi_match, 974 .pm = pm_ptr(&cvs_pm_ops), 975 }, 976 .probe = cvs_probe, 977 .remove = cvs_remove, 978 }; 979 980 static int cvs_platform_probe(struct platform_device *pdev) 981 { 982 return cvs_core_probe(&pdev->dev, NULL); 983 } 984 985 static void cvs_platform_remove(struct platform_device *pdev) 986 { 987 cvs_core_remove(&pdev->dev); 988 } 989 990 /* 991 * Platform driver structure. 992 * 993 * Some platforms may instantiate the CVS device as a platform device 994 * without I2C support. This driver binding allows such platforms to use the 995 * CVS core functionality (GPIOs, CSI sub-device) without I2C. 996 */ 997 static struct platform_driver cvs_platform_driver = { 998 .driver = { 999 .name = "cvs_platform", 1000 .acpi_match_table = intel_cvs_acpi_match, 1001 .pm = pm_ptr(&cvs_pm_ops), 1002 }, 1003 .probe = cvs_platform_probe, 1004 .remove = cvs_platform_remove, 1005 }; 1006 1007 /** 1008 * cvs_init - Module init registering I2C and platform drivers 1009 * 1010 * Return: 0 on success or negative errno. 1011 */ 1012 static int __init cvs_init(void) 1013 { 1014 int ret; 1015 1016 ret = i2c_add_driver(&cvs_driver); 1017 if (ret) 1018 return ret; 1019 1020 ret = platform_driver_register(&cvs_platform_driver); 1021 if (ret) { 1022 i2c_del_driver(&cvs_driver); 1023 return ret; 1024 } 1025 1026 return 0; 1027 } 1028 1029 /** 1030 * cvs_exit - Module exit unregistering drivers 1031 */ 1032 static void __exit cvs_exit(void) 1033 { 1034 platform_driver_unregister(&cvs_platform_driver); 1035 i2c_del_driver(&cvs_driver); 1036 } 1037 module_init(cvs_init); 1038 module_exit(cvs_exit); 1039 1040 MODULE_IMPORT_NS("INTEL_IPU_BRIDGE"); 1041 MODULE_AUTHOR("Miguel Vadillo <miguel.vadillo@intel.com>"); 1042 MODULE_DESCRIPTION("Intel Vision Sensing Controller driver"); 1043 MODULE_LICENSE("GPL"); 1044