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