From 5d166b2518459c20d42a8a0eecd45a63778b5d9e Mon Sep 17 00:00:00 2001 From: Fejer Elod Date: Thu, 29 Oct 2026 12:10:33 +0200 Subject: [PATCH] camera fixes --- CAMERA-GUIDE.md | 275 ++++++++ README.md | 40 +- fix-camera.sh | 56 ++ install.sh | 112 +++ ipu-bridge/Makefile | 1 + ipu-bridge/dell-xps16-da16250.patch | 16 + ipu-bridge/ipu-bridge.c | 1015 +++++++++++++++++++++++++++ 7 files changed, 1513 insertions(+), 2 deletions(-) create mode 100644 CAMERA-GUIDE.md create mode 100755 fix-camera.sh create mode 100755 install.sh create mode 100644 ipu-bridge/Makefile create mode 100644 ipu-bridge/dell-xps16-da16250.patch create mode 100644 ipu-bridge/ipu-bridge.c diff --git a/CAMERA-GUIDE.md b/CAMERA-GUIDE.md new file mode 100644 index 0000000..08082b9 --- /dev/null +++ b/CAMERA-GUIDE.md @@ -0,0 +1,275 @@ +# Built-in Camera Fix for Intel IPU6 Laptops on Fedora + +*As of 2026-10-29* + +The built-in camera on the Dell XPS 16 (DA16250) works on Fedora 44 after three fixes. Fix 1 moves libcamera's image processing off the NVIDIA GPU. Fix 2 corrects the upside-down sensor in the kernel. Fix 3 lets browsers use PipeWire cameras. Fixes 1 and 3 apply to most laptops with an Intel IPU6 camera; fix 2 depends on the laptop model. + +This guide sits in the `xps16-camera-fix-fedora` repository, next to the files the fixes need: + +``` +xps16-camera-fix-fedora/ +├── CAMERA-GUIDE.md this guide +├── README.md short summary +├── fix-camera.sh starts Claude Code with a prompt that follows this guide +├── install.sh builds and installs the patched module (Fix 2) +└── ipu-bridge/ + ├── ipu-bridge.c Fedora's 7.2 source plus one entry for the DA16250 + ├── Makefile + └── dell-xps16-da16250.patch the 7-line change, for reference +``` + +## At a glance + +Find your symptom, then go to the section in the last column. + +| Symptom | Cause | Where to look | +| --- | --- | --- | +| Camera is listed, but the picture is black or frozen | libcamera's GPU image processing uses the NVIDIA driver, which fails on every frame | Fix 1 | +| Picture is upside down in every app | Firmware says the sensor is upright, and your kernel doesn't know your model is an exception | Fix 2 | +| Brave, Chrome or Chromium finds no camera | The browser opens `/dev/video*` directly, and those nodes only carry raw sensor data | Fix 3 | +| Firefox finds no camera | Firefox's PipeWire camera setting is off | Fix 3 | +| Jagged, grainy picture in Kamoso | Kamoso always asks for 640×480, a 3× downscale with no smoothing | Fix 3 (use Snapshot) | +| Upside down only in COSMIC Camera | Bug in COSMIC Camera 1.3.7: it rotates a second time after libcamera already did | Fix 3 (use Snapshot) | +| A Flatpak camera app shows black, but Snapshot works | The app bundles its own libcamera, so the WirePlumber setting doesn't reach it | Fix 1 (Flatpak apps) | +| Grainy, muddy picture in a dim room | Exposure and gain are both at maximum | Image quality | +| `Device or resource busy` | Another app is using the camera; only one can at a time | Close the other app or browser tab | + +## Does this apply to you + +This guide is for laptops whose camera connects through an Intel IPU6 image processor, not over USB. It was tested on a Dell XPS 16 DA16250 (Core Ultra 9 285H, OmniVision OV02C10 sensor, RTX 5070) with Fedora 44, kernels 7.2.11 and 7.2.13, libcamera 0.7.2, PipeWire 1.6.9 and WirePlumber 0.5.18. + +Run these checks in a terminal, top to bottom. + +1. **Camera type.** A "Multimedia controller … IPU" line means a MIPI camera, so this guide applies. `8086:7d19` is the IPU6 in Meteor Lake and Arrow Lake. No line: you probably have a USB webcam, which works without this guide. + + ``` + lspci -nn | grep -i ipu + ``` + +2. **Kernel drivers.** Both `intel_ipu6` and `ipu_bridge` should be listed. + + ``` + lsmod | grep -E 'intel_ipu6|ipu_bridge' + ``` + +3. **Sensor found.** Look for a sensor name besides the "Intel IPU6 CSI2" lines (ours: `ov02c10 23-0036`). If there are only CSI2 lines, the sensor driver is missing, which this guide doesn't cover. + + ``` + grep . /sys/class/video4linux/v4l-subdev*/name + ``` + +4. **Userspace packages.** All three should be installed. + + ``` + rpm -q libcamera libcamera-ipa pipewire-plugin-libcamera + ``` + +5. **PipeWire sees it.** Under Video → Sources you should see "Built-in Front Camera". + + ``` + wpctl status + ``` + +6. **NVIDIA GPU.** Any output means you will probably need Fix 1. + + ``` + lspci | grep -i nvidia + ``` + +7. **Laptop model.** Fix 2 needs the exact string. + + ``` + cat /sys/class/dmi/id/product_name + ``` + +Lunar Lake laptops (Core Ultra 200V) use a newer IPU7 with a different kernel driver. This guide was not tested on them. + +## How it works + +The camera passes through five layers, and each fix targets one of them. + +| Layer | Component | What it does | Fix | +| --- | --- | --- | --- | +| Hardware | OV02C10 sensor | Mounted at 180°, but the firmware says 0° | | +| Hardware | IPU6 image unit | Receives raw Bayer frames over CSI-2 | | +| Kernel | `ipu-bridge` | Reads the firmware tables and reports the sensor rotation | **Fix 2:** add your model | +| Kernel | `ov02c10` driver | Exposure, gain, hardware horizontal/vertical flip | | +| Kernel | `intel_ipu6_isys` | Provides the `/dev/video*` nodes, with raw Bayer data only | | +| libcamera (inside WirePlumber) | Software ISP | Turns raw data into a color picture on the GPU | **Fix 1:** use Intel (Mesa), not NVIDIA | +| libcamera (inside WirePlumber) | Exposure, color, orientation | Sets sensor gain and exposure, and turns on the flips for 180° | | +| PipeWire | PipeWire + WirePlumber | Offers "Built-in Front Camera" to apps, with the camera portal for Flatpaks | | +| Apps | Snapshot, browsers, Kamoso | Ask PipeWire for the camera at some resolution | **Fix 3:** browser PipeWire flag | + +The kernel delivers only raw sensor data. libcamera turns it into a picture, and PipeWire hands that picture to apps. Apps that open `/dev/video*` directly skip libcamera and get only raw data, which is why browsers need the PipeWire flag. + +## Fix 1: black image on laptops with an NVIDIA GPU + +On Intel + NVIDIA laptops, tell WirePlumber to use the Intel GPU's driver (Mesa) for camera processing. This takes one file in your home folder and needs no sudo. + +**Why it breaks.** The IPU6 delivers raw Bayer data, and libcamera's software ISP turns it into a color picture on the GPU through EGL. The system hands out NVIDIA's EGL driver first, and with it every frame fails. Using the Intel GPU also keeps the NVIDIA GPU asleep while the camera is on. + +**Confirm the symptom.** Open the camera in any app first, because these lines only appear after a stream starts: + +``` +journalctl --user -b -u wireplumber | grep -E "EGL_VENDOR|GL_RENDERER|debayerGPU|FATAL" +``` + +The broken case looks like this: + +``` +EGL: EGL_VENDOR: NVIDIA +ERROR eGL egl.cpp:134 glFrameBufferTexture2D error 36054 +ERROR Debayer debayer_egl.cpp:639 debayerGPU failed +FATAL default pipeline_handler.cpp:398 assertion "data->queuedRequests_.empty()" failed in stop() +``` + +**Apply the fix.** Restarting WirePlumber can interrupt audio for a moment. + +``` +mkdir -p ~/.config/systemd/user/wireplumber.service.d +cat > ~/.config/systemd/user/wireplumber.service.d/camera-softisp-mesa.conf <<'EOF' +[Service] +Environment=__EGL_VENDOR_LIBRARY_FILENAMES=/usr/share/glvnd/egl_vendor.d/50_mesa.json +EOF +systemctl --user daemon-reload +systemctl --user restart wireplumber +``` + +**Check it worked.** Open the camera again and rerun the `journalctl` command. You should see `EGL_VENDOR: Mesa Project`, a `GL_RENDERER` line naming your Intel GPU, and no `debayerGPU failed`. On the test laptop the stream ran at about 28 frames per second. + +**If the Intel route fails.** Replace the `Environment=` line with `Environment=LIBCAMERA_SOFTISP_MODE=cpu` and restart WirePlumber. The CPU path works, but at small sizes it crops the middle of the image instead of scaling it: at 640×480 you get a zoomed-in view. + +**Flatpak apps that bundle libcamera.** Some camera apps, such as COSMIC Camera, ship their own libcamera and bypass WirePlumber, so they need the same setting. The Mesa file sits at a different path inside the sandbox: + +``` +flatpak override --user --env=__EGL_VENDOR_LIBRARY_FILENAMES=/usr/lib/x86_64-linux-gnu/GL/glvnd/egl_vendor.d/50_mesa.json +``` + +Undo it with `flatpak override --user --reset `. Snapshot and the browsers go through PipeWire and don't need this. + +## Fix 2: upside-down image + +Add your laptop to the kernel's list of upside-down sensors. Mainline Linux already lists the XPS 16 DA16250, but Fedora's 7.2 kernels don't yet. The fix is a patched `ipu-bridge` module that rebuilds itself on every kernel update. + +**Why it breaks.** The laptop firmware (ACPI) tells the kernel the sensor is mounted at 0°, but it is mounted at 180°. The `ipu-bridge` module keeps a list of models whose firmware gets this wrong and reports 180° for them. libcamera then flips the image in the sensor itself, at no cost. + +**Check 1: the rotation the system reports.** + +``` +ID=$(wpctl status | grep -m1 "Built-in Front Camera" | grep -oE "[0-9]+" | head -1) +pw-cli info $ID | grep api.libcamera.rotation +``` + +`"0"` on an upside-down camera means you need this fix. `"180"` means it is already handled. + +**Check 2: whether your kernel knows your model.** + +``` +M=$(modinfo -n ipu_bridge) +case $M in *.xz) xzcat "$M" ;; *) cat "$M" ;; esac | strings | grep -F "$(cat /sys/class/dmi/id/product_name)" +``` + +If your model name prints, the kernel already has the fix. If nothing prints, install the patched module. + +**Install it (XPS 16 DA16250, Fedora 7.2 kernels).** + +1. Install the build tools: `sudo dnf install kernel-devel-$(uname -r) gcc make`. +2. Check Secure Boot with `mokutil --sb-state` (see below if it is enabled). +3. In this folder, run `sudo ./install.sh`, then reboot. +4. Repeat Check 1. It should print `"180"`. + +**What install.sh does.** + +- Stops on any model other than the DA16250 (it compares the DMI product name). +- If the stock module already has the entry, removes any override for that kernel instead of building. +- Builds the module against the running kernel and checks that it was built for that kernel version. +- Compares every string in the stock module with the new one. Any difference besides the new entry stops the install, so a newer Fedora module is never replaced with an older copy. +- Installs to `/lib/modules//updates/ipu-bridge.ko`, which takes priority over the stock module, and runs `depmod`. +- Adds a kernel-install hook that repeats all this for each new kernel, logging to `/var/log/ipu-bridge-da16250.log`. The hook never fails a kernel install. On the test laptop it rebuilt the module by itself when kernel 7.2.13 arrived. + +The kernel will mark itself "tainted" because the module is unsigned and built outside the kernel tree. This is only a label. + +**Secure Boot.** With Secure Boot on, Fedora refuses unsigned modules, and the script does not sign. You would need to sign the module with a key enrolled in MOK, such as the akmods key if you use akmod-nvidia. This was not tested. + +**A different laptop model.** First check whether mainline Linux already lists your model (link in Sources). If not, change two places: + +1. Note your model string (`cat /sys/class/dmi/id/product_name`) and your sensor's ACPI ID from the command below. The test laptop prints `ov02c10 23-0036 OVTI02C1`. +2. In `ipu-bridge/ipu-bridge.c`, copy the DA16250 entry in `upside_down_sensor_dmi_ids[]`. Put your model string in `DMI_PRODUCT_NAME` and your sensor ID in `.driver_data`. +3. In `install.sh`, set `QUIRK` to your model string. + +``` +for s in /sys/class/video4linux/v4l-subdev*; do echo "$(cat $s/name) $(cat $s/device/firmware_node/hid 2>/dev/null)"; done +``` + +The bundled `ipu-bridge.c` matches Fedora's 7.2 kernels. On another kernel series the script stops at the string comparison, and the source needs refreshing from that kernel first. + +**Undo.** Run `sudo ./install.sh --uninstall`, then reboot. + +## Fix 3: apps and browsers + +Only apps that get the camera through PipeWire can use it. Browsers need one setting; for a camera app, use GNOME Snapshot. + +**Browsers.** + +- **Brave, Chrome, Chromium:** open `brave://flags/#enable-webrtc-pipewire-camera` (or `chrome://flags/…`), set "PipeWire Camera support" to Enabled, and relaunch. In Meet or Jitsi, choose "Built-in Front Camera". The desktop asks once whether the browser may use the camera. +- **Firefox:** in `about:config`, `media.webrtc.camera.allow-pipewire` must be `true`. + +With the flag on, USB webcams go through PipeWire too. PipeWire passes their frames through unchanged, and a USB webcam may show up twice in the list. + +**Camera apps.** The resolution an app asks for decides the quality, because the image is shrunk from the sensor's 1928×1092 without smoothing. + +| App | Asks for | Result on the test laptop | +| --- | --- | --- | +| GNOME Snapshot (`org.gnome.Snapshot`) | 1920×1080 | Works, upright, full quality. Recommended. | +| Brave with Google Meet | 1280×720 | Works, good quality. | +| Kamoso | 640×480, no setting to change it | Upright, but jagged and grainy. Photos are also 640×480. | +| COSMIC Camera 1.3.7 | 1920×1080 | Upside down: it rotates again after libcamera already did. It also bundles its own libcamera, so it needs the Flatpak setting from Fix 1. | + +Install Snapshot with `flatpak install --user flathub org.gnome.Snapshot`. To see what an app asked for, open it and run: + +``` +journalctl --user -b | grep "configuring streams" | tail -3 +``` + +Only one app can use the camera at a time. If an app shows nothing, close any other camera app or video-call tab. + +## Image quality + +Expect a usable but basic picture: grainy and muddy in a dim room. More light on your face helps more than any setting. + +- **Low light.** In the test room, auto-exposure hit both limits: the longest exposure possible at 30 fps, and 15.5× analogue gain. The image was still about half its target brightness. Maximum gain is where the grain comes from. +- **Basic processing.** Linux uses libcamera's software ISP, which has no noise reduction, no lens-shading correction and no calibrated color for this sensor. Windows uses Intel's hardware ISP with tuned processing. +- **Slow brightness changes.** libcamera 0.7.2 has no exposure model for the OV02C10, so auto-exposure moves in small steps. The picture takes 10 seconds or more to settle after the camera opens. +- **Resolution.** Use 1280×720 or higher. At 640×480 the image is shrunk about 3× without smoothing, which makes edges jagged. + +## Let Claude Code do it + +Claude Code can run the checks, read the logs and adapt the files for your model. You only run the sudo step yourself. + +1. Install Claude Code if you don't have it: `curl -fsSL https://claude.ai/install.sh | bash`. +2. Run `./fix-camera.sh` from this folder, or double-click it in your file manager. It opens a terminal if needed and starts Claude Code with a prompt that walks through this guide. The prompt is at the top of the script. +3. Claude Code asks before it runs commands or edits files. When it hands you a sudo command, run it in a separate terminal, then reboot. + +Some file managers open scripts in a text editor when you double-click them. In that case, right-click the file and choose the option to run or execute it, or start it from a terminal. + +## What gets installed and how to undo it + +Everything is reversible. Fix 1 and Fix 3 stay in your user account; only Fix 2 touches the system. + +| Change | Location | Root | Undo | +| --- | --- | --- | --- | +| WirePlumber setting (Fix 1) | `~/.config/systemd/user/wireplumber.service.d/camera-softisp-mesa.conf` | No | Delete the file, then `systemctl --user daemon-reload && systemctl --user restart wireplumber` | +| Flatpak app setting (Fix 1, only for apps that bundle libcamera) | The app's `flatpak override --user` entry | No | `flatpak override --user --reset ` | +| Patched module (Fix 2) | `/lib/modules//updates/ipu-bridge.ko`, one per kernel | Yes | `sudo ./install.sh --uninstall`, then reboot | +| Kernel-install hook (Fix 2) | `/etc/kernel/install.d/90-ipu-bridge-da16250.install` | Yes | Removed by `--uninstall` | +| Copy of the script and source the hook uses (Fix 2) | `/usr/local/src/ipu-bridge-da16250/` | Yes | Removed by `--uninstall` | +| Hook log (Fix 2) | `/var/log/ipu-bridge-da16250.log` | Yes | Not removed by `--uninstall`; delete it with `sudo rm` | +| Browser flag (Fix 3) | The browser's flags page | No | Set the flag back to Default | + +## Sources + +Checked on 29 October 2026. + +- [Mainline ipu-bridge.c](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/drivers/media/pci/intel/ipu-bridge.c): its upside-down list includes the XPS 16 DA16250. Search it for your own model. +- [Stable 7.2.y ipu-bridge.c](https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/plain/drivers/media/pci/intel/ipu-bridge.c?h=linux-7.2.y): the 7.2 series as of 7.2.13, without the DA16250 entry. +- [Fedora f44 kernel patch](https://src.fedoraproject.org/rpms/kernel/raw/f44/f/patch-7.2-redhat.patch): Fedora's own `ipu-bridge` changes (it adds the DA14250, not the DA16250). The bundled `ipu-bridge.c` is stable 7.2 plus these changes plus the DA16250 entry. diff --git a/README.md b/README.md index a79ec6f..547324e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,39 @@ -# xps16-camera-fix-fedora +# Dell XPS 16 (DA16250) webcam on Fedora -Fix the built-in camera on Intel IPU6 laptops (Dell XPS 16) on Fedora, with a one-click Claude Code helper \ No newline at end of file +Quick start: run `./fix-camera.sh`. It opens Claude Code with a prompt that +diagnoses and fixes the camera step by step. + +For the full step-by-step guide (diagnosis, all fixes, other laptop models), see +[CAMERA-GUIDE.md](CAMERA-GUIDE.md). + +Camera: OmniVision OV02C10 on the Intel IPU6 (Arrow Lake), driven by the +in-kernel `ov02c10` + `intel-ipu6-isys` drivers and libcamera's software ISP, +exposed to apps through PipeWire. + +## Fixes + +1. **Black image**: libcamera's GPU debayer failed on NVIDIA's EGL. Fixed by + `~/.config/systemd/user/wireplumber.service.d/camera-softisp-mesa.conf`, + which makes WirePlumber use Mesa (the Intel iGPU). +2. **Upside-down image**: the ACPI tables report 0° rotation for a sensor that + is mounted at 180°. `ipu-bridge` has a DMI quirk list for this. The + DA16250 entry is in mainline but not in Fedora's 7.2 kernels yet. + `ipu-bridge/` is Fedora's 7.2 source plus that one entry + (`ipu-bridge/dell-xps16-da16250.patch`). + +```sh +sudo ./install.sh # build + install for the running kernel, add kernel-install hook +sudo ./install.sh --uninstall +``` + +The hook (`/etc/kernel/install.d/90-ipu-bridge-da16250.install`) rebuilds the +module for each new kernel, logs to `/var/log/ipu-bridge-da16250.log`, and +removes the override once a kernel's stock module already has the quirk. It +refuses to install if the stock module differs from this source by anything +other than the quirk. + +## Apps + +Only PipeWire-aware apps see the camera. In Chromium or Brave, enable +`chrome://flags/#enable-webrtc-pipewire-camera` (`brave://flags/...`). In +Firefox, `media.webrtc.camera.allow-pipewire` must be `true`. diff --git a/fix-camera.sh b/fix-camera.sh new file mode 100755 index 0000000..5a06934 --- /dev/null +++ b/fix-camera.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# One-click camera fix: starts Claude Code in this folder with a prompt that +# diagnoses and fixes the built-in Intel IPU6 camera using CAMERA-GUIDE.md. +# Claude Code asks before it runs commands or edits files, and hands anything +# that needs root back to you. +# +# ./fix-camera.sh from a terminal, or double-click it in a file manager +set -euo pipefail + +SELF=$(readlink -f "$0") +cd "$(dirname "$SELF")" + +PROMPT='The built-in camera on this Fedora laptop does not work properly. Use CAMERA-GUIDE.md in this folder to diagnose and fix it: + +1. Run the checks in "Does this apply to you" and tell me what you find before changing anything. +2. Open the camera with a short PipeWire test capture and read the WirePlumber log. If libcamera picks the NVIDIA EGL driver and logs "debayerGPU failed", apply Fix 1 and confirm that frames arrive. +3. Check the rotation PipeWire reports. If the picture is upside down and this kernel'"'"'s ipu-bridge does not list my model, prepare Fix 2 from the files in this folder. Adapt install.sh and ipu-bridge/ipu-bridge.c if this is not a Dell XPS 16 DA16250. Build it, then give me the sudo command to run; do not install anything as root yourself. +4. Tell me which browser setting I need and which camera app to use. + +At the end, list every change you made and how to undo each one.' + +spawned=false +[ "${1:-}" = --spawned ] && spawned=true + +pause() { if $spawned; then read -rp "Press Enter to close this window. " _ || true; fi; } +die() { echo "fix-camera: $*" >&2; pause; exit 1; } + +# Double-clicked in a file manager: no terminal, so reopen in one. +if ! [ -t 0 ] || ! [ -t 1 ]; then + $spawned && exit 1 # the terminal we opened has no tty either; don't loop + if command -v xdg-terminal-exec >/dev/null; then exec xdg-terminal-exec "$SELF" --spawned + elif command -v konsole >/dev/null; then exec konsole -e "$SELF" --spawned + elif command -v ptyxis >/dev/null; then exec ptyxis -- "$SELF" --spawned + elif command -v gnome-terminal >/dev/null; then exec gnome-terminal -- "$SELF" --spawned + elif command -v xfce4-terminal >/dev/null; then exec xfce4-terminal -x "$SELF" --spawned + elif command -v xterm >/dev/null; then exec xterm -e "$SELF" --spawned + fi + command -v notify-send >/dev/null && notify-send "fix-camera" "No terminal app found. Run $SELF from a terminal." + exit 1 +fi + +[ -f CAMERA-GUIDE.md ] || die "CAMERA-GUIDE.md is missing; keep this script in the same folder as the guide" +command -v claude >/dev/null || die "Claude Code is not installed. Install it with: + + curl -fsSL https://claude.ai/install.sh | bash + +then open a new terminal and run this script again." + +cat <&2; exit 1; } + +# String literals of a module's .rodata, minus __FILE__ paths, so a signed +# distro build and our unsigned one compare equal when the source matches. +ro_strings() { + local ko sec + ko=$(mktemp -p "$WORK") # unique: callers run this in parallel via <(...) + case $1 in + *.xz) xz -dc "$1" >"$ko" ;; + *.zst) zstd -qdc "$1" >"$ko" ;; + *) cp "$1" "$ko" ;; + esac + for sec in $(readelf -SW "$ko" | grep -oE '\.rodata[^ ]*' | sort -u); do + objcopy -O binary --only-section="$sec" "$ko" /dev/stdout 2>/dev/null + done | strings -n4 | grep -vE '\.[ch]$' | sort -u +} + +remove_for() { + if [ -e "/lib/modules/$1/updates/ipu-bridge.ko" ]; then + rm -f "/lib/modules/$1/updates/ipu-bridge.ko" + depmod -a "$1" + echo "$1: removed patched ipu-bridge" + fi +} + +install_for() { + local kver=$1 stock ko=$WORK/build/ipu-bridge.ko + stock=$(ls "/lib/modules/$kver/kernel/drivers/media/pci/intel/"ipu-bridge.ko* 2>/dev/null | head -1) + [ -n "$stock" ] || die "$kver: no stock ipu-bridge module" + if ro_strings "$stock" | grep -xF "$QUIRK" >/dev/null; then + echo "$kver: stock ipu-bridge already has the quirk, no override needed" + remove_for "$kver" + return + fi + [ -e "/lib/modules/$kver/build/Makefile" ] || die "$kver: kernel-devel-$kver is not installed" + + rm -rf "$WORK/build" && mkdir "$WORK/build" + cp "$SRC/ipu-bridge.c" "$SRC/Makefile" "$WORK/build/" + make -s -C "/lib/modules/$kver/build" M="$WORK/build" modules >"$WORK/make.log" 2>&1 || + { cat "$WORK/make.log" >&2; die "$kver: build failed"; } + strip --strip-debug "$ko" + [ "$(modinfo -F vermagic "$ko" | cut -d' ' -f1)" = "$kver" ] || die "$kver: vermagic mismatch" + + # Never replace a newer distro module with our older copy of the source. + diff <(ro_strings "$stock") <(ro_strings "$ko" | grep -vxF "$QUIRK") >/dev/null || + die "$kver: stock ipu-bridge differs from ipu-bridge/ipu-bridge.c beyond the quirk; refresh the source" + + install -Dm644 "$ko" "/lib/modules/$kver/updates/ipu-bridge.ko" + depmod -a "$kver" + echo "$kver: installed patched ipu-bridge" +} + +[ "$(id -u)" = 0 ] || die "must run as root" +[ "$(cat /sys/class/dmi/id/product_name)" = "$QUIRK" ] || die "this is not a $QUIRK" + +case ${1:-} in +--uninstall) + for m in /lib/modules/*/updates/ipu-bridge.ko; do + [ -e "$m" ] && remove_for "$(basename "$(dirname "$(dirname "$m")")")" + done + rm -f "$HOOK" + rm -rf "$SHARE" + echo "Uninstalled. Reboot to go back to the stock module." + ;; +--kver) + install_for "${2:?--kver needs a kernel version}" + ;; +"") + install_for "$(uname -r)" + # Root-owned copy for the hook, so it never runs code from a user's home. + install -d "$SHARE/ipu-bridge" + install -m644 "$SRC/ipu-bridge.c" "$SRC/Makefile" "$SHARE/ipu-bridge/" + install -m755 "$SELF_DIR/install.sh" "$SHARE/install.sh" + cat >"$HOOK" <>/var/log/$NAME.log 2>&1 || + echo "$NAME: failed for \$2, see /var/log/$NAME.log" >&2 +exit 0 +EOF + chmod 755 "$HOOK" + echo "Hook installed: $HOOK" + echo "Reboot to load the patched module." + ;; +*) + die "unknown argument: $1" + ;; +esac diff --git a/ipu-bridge/Makefile b/ipu-bridge/Makefile new file mode 100644 index 0000000..c199503 --- /dev/null +++ b/ipu-bridge/Makefile @@ -0,0 +1 @@ +obj-m := ipu-bridge.o diff --git a/ipu-bridge/dell-xps16-da16250.patch b/ipu-bridge/dell-xps16-da16250.patch new file mode 100644 index 0000000..33b5fb8 --- /dev/null +++ b/ipu-bridge/dell-xps16-da16250.patch @@ -0,0 +1,16 @@ +--- a/drivers/media/pci/intel/ipu-bridge.c ++++ b/drivers/media/pci/intel/ipu-bridge.c +@@ -143,6 +143,13 @@ + }, + .driver_data = "OVTI02C1", + }, ++ { ++ .matches = { ++ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), ++ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 16 (Dell 16 Premium) DA16250"), ++ }, ++ .driver_data = "OVTI02C1", ++ }, + /* + * The first four characters of DMI_BOARD_NAME identify the Lenovo + * machine type/model. For example, a DMI_BOARD_NAME starting with diff --git a/ipu-bridge/ipu-bridge.c b/ipu-bridge/ipu-bridge.c new file mode 100644 index 0000000..373accf --- /dev/null +++ b/ipu-bridge/ipu-bridge.c @@ -0,0 +1,1015 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Author: Dan Scally */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define ADEV_DEV(adev) ACPI_PTR(&((adev)->dev)) + +/* + * 92335fcf-3203-4472-af93-7b4453ac29da + * + * Used to build MEI CSI device name to lookup MEI CSI device by + * device_find_child_by_name(). + */ +#define MEI_CSI_UUID \ + UUID_LE(0x92335FCF, 0x3203, 0x4472, \ + 0xAF, 0x93, 0x7B, 0x44, 0x53, 0xAC, 0x29, 0xDA) + +/* + * IVSC device name + * + * Used to match IVSC device by ipu_bridge_match_ivsc_dev() + */ +#define IVSC_DEV_NAME "intel_vsc" + +/* + * Extend this array with ACPI Hardware IDs of devices known to be working + * plus the number of link-frequencies expected by their drivers, along with + * the frequency values in hertz. This is somewhat opportunistic way of adding + * support for this for now in the hopes of a better source for the information + * (possibly some encoded value in the SSDB buffer that we're unaware of) + * becoming apparent in the future. + * + * Do not add an entry for a sensor that is not actually supported. + * + * Please keep the list sorted by ACPI HID. + */ +static const struct ipu_sensor_config ipu_supported_sensors[] = { + /* Himax HM11B1 */ + IPU_SENSOR_CONFIG("HIMX11B1", 1, 384000000), + /* Himax HM2170 */ + IPU_SENSOR_CONFIG("HIMX2170", 1, 384000000), + /* Himax HM2172 */ + IPU_SENSOR_CONFIG("HIMX2172", 1, 384000000), + /* GalaxyCore GC0310 */ + IPU_SENSOR_CONFIG("INT0310", 1, 55692000), + /* Omnivision OV5693 */ + IPU_SENSOR_CONFIG("INT33BE", 1, 419200000), + /* Onsemi MT9M114 */ + IPU_SENSOR_CONFIG("INT33F0", 1, 384000000), + /* Omnivision OV2740 */ + IPU_SENSOR_CONFIG("INT3474", 1, 180000000), + /* Omnivision OV5670 */ + IPU_SENSOR_CONFIG("INT3479", 1, 422400000), + /* Omnivision OV8865 */ + IPU_SENSOR_CONFIG("INT347A", 1, 360000000), + /* Omnivision OV7251 */ + IPU_SENSOR_CONFIG("INT347E", 1, 319200000), + /* Hynix Hi-556 */ + IPU_SENSOR_CONFIG("INT3537", 1, 437000000), + /* Lontium lt6911uxe */ + IPU_SENSOR_CONFIG("INTC10C5", 0), + /* Omnivision OV01A10 / OV01A1S */ + IPU_SENSOR_CONFIG("OVTI01A0", 1, 400000000), + IPU_SENSOR_CONFIG("OVTI01AS", 1, 400000000), + /* Omnivision OV02C10 */ + IPU_SENSOR_CONFIG("OVTI02C1", 1, 400000000), + /* Omnivision OV02E10 */ + IPU_SENSOR_CONFIG("OVTI02E1", 1, 360000000), + /* Omnivision ov05c10 */ + IPU_SENSOR_CONFIG("OVTI05C1", 1, 480000000), + /* Omnivision OV08A10 */ + IPU_SENSOR_CONFIG("OVTI08A1", 1, 500000000), + /* Omnivision OV08x40 */ + IPU_SENSOR_CONFIG("OVTI08F4", 3, 400000000, 749000000, 800000000), + /* Omnivision OV13B10 */ + IPU_SENSOR_CONFIG("OVTI13B1", 1, 560000000), + IPU_SENSOR_CONFIG("OVTIDB10", 1, 560000000), + /* Omnivision OV2680 */ + IPU_SENSOR_CONFIG("OVTI2680", 1, 331200000), + /* Omnivision OV5675 */ + IPU_SENSOR_CONFIG("OVTI5675", 1, 450000000), + /* Omnivision OV8856 */ + IPU_SENSOR_CONFIG("OVTI8856", 3, 180000000, 360000000, 720000000), + /* Sony IMX471 */ + IPU_SENSOR_CONFIG("SONY471A", 1, 200000000), + /* Sony IMX471 (found on Lenovo X1 Carbon G14) */ + IPU_SENSOR_CONFIG("TBE20A0", 1, 200000000), + /* Toshiba T4KA3 */ + IPU_SENSOR_CONFIG("XMCC0003", 1, 321468000), +}; + +/* + * DMI matches for laptops which have their sensor mounted upside-down + * without reporting a rotation of 180° in neither the SSDB nor the _PLD. + */ +static const struct dmi_system_id upside_down_sensor_dmi_ids[] = { + { + .matches = { + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 9340"), + }, + .driver_data = "OVTI02C1", + }, + { + .matches = { + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 9350"), + }, + .driver_data = "OVTI02C1", + }, + { + .matches = { + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 14 9440"), + }, + .driver_data = "OVTI02C1", + }, + { + .matches = { + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 14 (Dell 14 Premium) DA14250"), + }, + .driver_data = "OVTI02C1", + }, + { + .matches = { + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 16 9640"), + }, + .driver_data = "OVTI02C1", + }, + { + .matches = { + DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), + DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 16 (Dell 16 Premium) DA16250"), + }, + .driver_data = "OVTI02C1", + }, + /* + * The first four characters of DMI_BOARD_NAME identify the Lenovo + * machine type/model. For example, a DMI_BOARD_NAME starting with + * "21Q6" indicates a ThinkPad X9-15. + * + * Reference: https://psref.lenovo.com/ + */ + { + /* Lenovo X9-14 */ + .matches = { + DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), + DMI_MATCH(DMI_BOARD_NAME, "21QA"), + }, + .driver_data = "SONY471A", + }, + { + /* Lenovo X9-14 */ + .matches = { + DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), + DMI_MATCH(DMI_BOARD_NAME, "21QB"), + }, + .driver_data = "SONY471A", + }, + { + /* Lenovo X9-15 */ + .matches = { + DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), + DMI_MATCH(DMI_BOARD_NAME, "21Q6"), + }, + .driver_data = "SONY471A", + }, + { + /* Lenovo X9-15 */ + .matches = { + DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), + DMI_MATCH(DMI_BOARD_NAME, "21Q7"), + }, + .driver_data = "SONY471A", + }, + {} /* Terminating entry */ +}; + +static const struct ipu_property_names prop_names = { + .clock_frequency = "clock-frequency", + .rotation = "rotation", + .orientation = "orientation", + .bus_type = "bus-type", + .data_lanes = "data-lanes", + .remote_endpoint = "remote-endpoint", + .link_frequencies = "link-frequencies", +}; + +static const char * const ipu_vcm_types[] = { + "ad5823", + "dw9714", + "ad5816", + "dw9719", + "dw9718", + "dw9806b", + "wv517s", + "lc898122xa", + "lc898212axb", +}; + +/* + * Used to figure out IVSC acpi device by ipu_bridge_get_ivsc_acpi_dev() + * instead of device and driver match to probe IVSC device. + */ +static const struct acpi_device_id ivsc_acpi_ids[] = { + { "INTC1059" }, + { "INTC1095" }, + { "INTC100A" }, + { "INTC10CF" }, + { "INTC10DE" }, /* LNL */ + { "INTC10E0" }, /* ARL */ + { "INTC10E1" }, /* PTL */ +}; + +/* + * The subset of ivsc_acpi_ids[] which are IVSC, rather than CVS, devices. The + * CVS IDs are deliberately not listed here: new ones keep being added, whereas + * this list is complete. + */ +static const struct acpi_device_id ivsc_only_acpi_ids[] = { + { "INTC1059" }, + { "INTC1095" }, + { "INTC100A" }, + { "INTC10CF" }, + { } +}; + +static struct acpi_device *ipu_bridge_get_ivsc_acpi_dev(struct acpi_device *adev) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(ivsc_acpi_ids); i++) { + const struct acpi_device_id *acpi_id = &ivsc_acpi_ids[i]; + struct acpi_device *consumer, *ivsc_adev; + + acpi_handle handle = acpi_device_handle(ACPI_PTR(adev)); + for_each_acpi_dev_match(ivsc_adev, acpi_id->id, NULL, -1) + /* camera sensor depends on IVSC in DSDT if exist */ + for_each_acpi_consumer_dev(ivsc_adev, consumer) + if (ACPI_PTR(consumer->handle) == handle) { + acpi_dev_put(consumer); + return ivsc_adev; + } + } + + return NULL; +} + +static int ipu_bridge_match_ivsc_dev(struct device *dev, const void *adev) +{ + if (ACPI_COMPANION(dev) != adev) + return 0; + + if (!sysfs_streq(dev_name(dev), IVSC_DEV_NAME)) + return 0; + + return 1; +} + +static struct device *ipu_bridge_get_ivsc_csi_dev(struct acpi_device *adev) +{ + struct device *dev, *csi_dev; + uuid_le uuid = MEI_CSI_UUID; + char name[64]; + + /* IVSC device on platform bus */ + dev = bus_find_device(&platform_bus_type, NULL, adev, + ipu_bridge_match_ivsc_dev); + if (dev) { + snprintf(name, sizeof(name), "%s-%pUl", dev_name(dev), &uuid); + + csi_dev = device_find_child_by_name(dev, name); + + put_device(dev); + + return csi_dev; + } + + /* + * The lookups below match on the ACPI companion alone. That is fine for + * CVS, which binds a driver to that very device, but not for IVSC: there + * the ACPI device also has a driverless platform device, which would be + * returned instead of the mei-csi client. Return NULL for IVSC so that + * the caller fails and the probe is retried once the IVSC device shows + * up. + */ + if (!acpi_match_device_ids(adev, ivsc_only_acpi_ids)) + return NULL; + + /* Try to locate CVS device on the I2C bus */ + csi_dev = bus_find_device_by_acpi_dev(&i2c_bus_type, adev); + if (csi_dev) + return csi_dev; + + /* Fallback to platform bus for CVS device */ + return bus_find_device_by_acpi_dev(&platform_bus_type, adev); +} + +static int ipu_bridge_check_ivsc_dev(struct ipu_sensor *sensor, + struct acpi_device *sensor_adev) +{ + struct acpi_device *adev; + struct device *csi_dev; + + adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev); + if (adev) { + csi_dev = ipu_bridge_get_ivsc_csi_dev(adev); + if (!csi_dev) { + acpi_dev_put(adev); + dev_err(ADEV_DEV(adev), "Failed to find MEI or CVS CSI dev\n"); + return -ENODEV; + } + + sensor->csi_dev = csi_dev; + sensor->ivsc_adev = adev; + } + + return 0; +} + +static int ipu_bridge_read_acpi_buffer(struct acpi_device *adev, char *id, + void *data, u32 size) +{ + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; + union acpi_object *obj; + acpi_status status; + int ret = 0; + + status = acpi_evaluate_object(ACPI_PTR(adev->handle), + id, NULL, &buffer); + if (ACPI_FAILURE(status)) + return -ENODEV; + + obj = buffer.pointer; + if (!obj) { + dev_err(ADEV_DEV(adev), "Couldn't locate ACPI buffer\n"); + return -ENODEV; + } + + if (obj->type != ACPI_TYPE_BUFFER) { + dev_err(ADEV_DEV(adev), "Not an ACPI buffer\n"); + ret = -ENODEV; + goto out_free_buff; + } + + if (obj->buffer.length > size) { + dev_err(ADEV_DEV(adev), "Given buffer is too small\n"); + ret = -EINVAL; + goto out_free_buff; + } + + memcpy(data, obj->buffer.pointer, obj->buffer.length); + +out_free_buff: + kfree(buffer.pointer); + return ret; +} + +static u32 ipu_bridge_parse_rotation(struct acpi_device *adev, + struct ipu_sensor_ssdb *ssdb) +{ + const struct dmi_system_id *dmi_id; + + /* A machine may have one entry per sensor, so check all matches. */ + for (dmi_id = dmi_first_match(upside_down_sensor_dmi_ids); dmi_id; + dmi_id = dmi_first_match(dmi_id + 1)) + if (acpi_dev_hid_match(adev, dmi_id->driver_data)) + return 180; + + switch (ssdb->degree) { + case IPU_SENSOR_ROTATION_NORMAL: + return 0; + case IPU_SENSOR_ROTATION_INVERTED: + return 180; + default: + dev_warn(ADEV_DEV(adev), + "Unknown rotation %d. Assume 0 degree rotation\n", + ssdb->degree); + return 0; + } +} + +static enum v4l2_fwnode_orientation ipu_bridge_parse_orientation(struct acpi_device *adev) +{ + enum v4l2_fwnode_orientation orientation; + struct acpi_pld_info *pld = NULL; + + if (!acpi_get_physical_device_location(ACPI_PTR(adev->handle), &pld)) { + dev_warn(ADEV_DEV(adev), "_PLD call failed, using default orientation\n"); + return V4L2_FWNODE_ORIENTATION_EXTERNAL; + } + + switch (pld->panel) { + case ACPI_PLD_PANEL_FRONT: + orientation = V4L2_FWNODE_ORIENTATION_FRONT; + break; + case ACPI_PLD_PANEL_BACK: + orientation = V4L2_FWNODE_ORIENTATION_BACK; + break; + case ACPI_PLD_PANEL_TOP: + case ACPI_PLD_PANEL_LEFT: + case ACPI_PLD_PANEL_RIGHT: + case ACPI_PLD_PANEL_UNKNOWN: + orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL; + break; + default: + dev_warn(ADEV_DEV(adev), "Unknown _PLD panel val %d\n", + pld->panel); + orientation = V4L2_FWNODE_ORIENTATION_EXTERNAL; + break; + } + + ACPI_FREE(pld); + return orientation; +} + +int ipu_bridge_parse_ssdb(struct acpi_device *adev, struct ipu_sensor *sensor) +{ + struct ipu_sensor_ssdb ssdb = {}; + int ret; + + ret = ipu_bridge_read_acpi_buffer(adev, "SSDB", &ssdb, sizeof(ssdb)); + if (ret) + return ret; + + if (ssdb.vcmtype > ARRAY_SIZE(ipu_vcm_types)) { + dev_warn(ADEV_DEV(adev), "Unknown VCM type %d\n", ssdb.vcmtype); + ssdb.vcmtype = 0; + } + + if (ssdb.lanes > IPU_MAX_LANES) { + dev_err(ADEV_DEV(adev), "Number of lanes in SSDB is invalid\n"); + return -EINVAL; + } + + sensor->link = ssdb.link; + sensor->lanes = ssdb.lanes; + sensor->mclkspeed = ssdb.mclkspeed; + sensor->rotation = ipu_bridge_parse_rotation(adev, &ssdb); + sensor->orientation = ipu_bridge_parse_orientation(adev); + + if (ssdb.vcmtype) + sensor->vcm_type = ipu_vcm_types[ssdb.vcmtype - 1]; + + return 0; +} +EXPORT_SYMBOL_NS_GPL(ipu_bridge_parse_ssdb, "INTEL_IPU_BRIDGE"); + +static void ipu_bridge_create_fwnode_properties( + struct ipu_sensor *sensor, + struct ipu_bridge *bridge, + const struct ipu_sensor_config *cfg) +{ + struct ipu_property_names *names = &sensor->prop_names; + struct software_node *nodes = sensor->swnodes; + + sensor->prop_names = prop_names; + + if (sensor->csi_dev) { + sensor->local_ref[0] = + SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IVSC_SENSOR_ENDPOINT]); + sensor->remote_ref[0] = + SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IVSC_IPU_ENDPOINT]); + sensor->ivsc_sensor_ref[0] = + SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_SENSOR_ENDPOINT]); + sensor->ivsc_ipu_ref[0] = + SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IPU_ENDPOINT]); + + sensor->ivsc_sensor_ep_properties[0] = + PROPERTY_ENTRY_U32(names->bus_type, + V4L2_FWNODE_BUS_TYPE_CSI2_DPHY); + sensor->ivsc_sensor_ep_properties[1] = + PROPERTY_ENTRY_U32_ARRAY_LEN(names->data_lanes, + bridge->data_lanes, + sensor->lanes); + sensor->ivsc_sensor_ep_properties[2] = + PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint, + sensor->ivsc_sensor_ref); + + sensor->ivsc_ipu_ep_properties[0] = + PROPERTY_ENTRY_U32(names->bus_type, + V4L2_FWNODE_BUS_TYPE_CSI2_DPHY); + sensor->ivsc_ipu_ep_properties[1] = + PROPERTY_ENTRY_U32_ARRAY_LEN(names->data_lanes, + bridge->data_lanes, + sensor->lanes); + sensor->ivsc_ipu_ep_properties[2] = + PROPERTY_ENTRY_REF_ARRAY(names->remote_endpoint, + sensor->ivsc_ipu_ref); + } else { + sensor->local_ref[0] = + SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_IPU_ENDPOINT]); + sensor->remote_ref[0] = + SOFTWARE_NODE_REFERENCE(&nodes[SWNODE_SENSOR_ENDPOINT]); + } + + sensor->dev_properties[0] = PROPERTY_ENTRY_U32( + sensor->prop_names.clock_frequency, + sensor->mclkspeed); + sensor->dev_properties[1] = PROPERTY_ENTRY_U32( + sensor->prop_names.rotation, + sensor->rotation); + sensor->dev_properties[2] = PROPERTY_ENTRY_U32( + sensor->prop_names.orientation, + sensor->orientation); + if (sensor->vcm_type) { + sensor->vcm_ref[0] = + SOFTWARE_NODE_REFERENCE(&sensor->swnodes[SWNODE_VCM]); + sensor->dev_properties[3] = + PROPERTY_ENTRY_REF_ARRAY("lens-focus", sensor->vcm_ref); + } + + sensor->ep_properties[0] = PROPERTY_ENTRY_U32( + sensor->prop_names.bus_type, + V4L2_FWNODE_BUS_TYPE_CSI2_DPHY); + sensor->ep_properties[1] = PROPERTY_ENTRY_U32_ARRAY_LEN( + sensor->prop_names.data_lanes, + bridge->data_lanes, sensor->lanes); + sensor->ep_properties[2] = PROPERTY_ENTRY_REF_ARRAY( + sensor->prop_names.remote_endpoint, + sensor->local_ref); + + if (cfg->nr_link_freqs > 0) + sensor->ep_properties[3] = PROPERTY_ENTRY_U64_ARRAY_LEN( + sensor->prop_names.link_frequencies, + cfg->link_freqs, + cfg->nr_link_freqs); + + sensor->ipu_properties[0] = PROPERTY_ENTRY_U32_ARRAY_LEN( + sensor->prop_names.data_lanes, + bridge->data_lanes, sensor->lanes); + sensor->ipu_properties[1] = PROPERTY_ENTRY_REF_ARRAY( + sensor->prop_names.remote_endpoint, + sensor->remote_ref); +} + +static void ipu_bridge_init_swnode_names(struct ipu_sensor *sensor) +{ + snprintf(sensor->node_names.remote_port, + sizeof(sensor->node_names.remote_port), + SWNODE_GRAPH_PORT_NAME_FMT, sensor->link); + snprintf(sensor->node_names.port, + sizeof(sensor->node_names.port), + SWNODE_GRAPH_PORT_NAME_FMT, 0); /* Always port 0 */ + snprintf(sensor->node_names.endpoint, + sizeof(sensor->node_names.endpoint), + SWNODE_GRAPH_ENDPOINT_NAME_FMT, 0); /* And endpoint 0 */ + if (sensor->vcm_type) { + /* append link to distinguish nodes with same model VCM */ + snprintf(sensor->node_names.vcm, sizeof(sensor->node_names.vcm), + "%s-%u", sensor->vcm_type, sensor->link); + } + + if (sensor->csi_dev) { + snprintf(sensor->node_names.ivsc_sensor_port, + sizeof(sensor->node_names.ivsc_sensor_port), + SWNODE_GRAPH_PORT_NAME_FMT, 0); + snprintf(sensor->node_names.ivsc_ipu_port, + sizeof(sensor->node_names.ivsc_ipu_port), + SWNODE_GRAPH_PORT_NAME_FMT, 1); + } +} + +static void ipu_bridge_init_swnode_group(struct ipu_sensor *sensor) +{ + struct software_node *nodes = sensor->swnodes; + + sensor->group[SWNODE_SENSOR_HID] = &nodes[SWNODE_SENSOR_HID]; + sensor->group[SWNODE_SENSOR_PORT] = &nodes[SWNODE_SENSOR_PORT]; + sensor->group[SWNODE_SENSOR_ENDPOINT] = &nodes[SWNODE_SENSOR_ENDPOINT]; + sensor->group[SWNODE_IPU_PORT] = &nodes[SWNODE_IPU_PORT]; + sensor->group[SWNODE_IPU_ENDPOINT] = &nodes[SWNODE_IPU_ENDPOINT]; + if (sensor->vcm_type) + sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM]; + + if (sensor->csi_dev) { + sensor->group[SWNODE_IVSC_HID] = + &nodes[SWNODE_IVSC_HID]; + sensor->group[SWNODE_IVSC_SENSOR_PORT] = + &nodes[SWNODE_IVSC_SENSOR_PORT]; + sensor->group[SWNODE_IVSC_SENSOR_ENDPOINT] = + &nodes[SWNODE_IVSC_SENSOR_ENDPOINT]; + sensor->group[SWNODE_IVSC_IPU_PORT] = + &nodes[SWNODE_IVSC_IPU_PORT]; + sensor->group[SWNODE_IVSC_IPU_ENDPOINT] = + &nodes[SWNODE_IVSC_IPU_ENDPOINT]; + + if (sensor->vcm_type) + sensor->group[SWNODE_VCM] = &nodes[SWNODE_VCM]; + } else { + if (sensor->vcm_type) + sensor->group[SWNODE_IVSC_HID] = &nodes[SWNODE_VCM]; + } +} + +static void ipu_bridge_create_connection_swnodes(struct ipu_bridge *bridge, + struct ipu_sensor *sensor) +{ + struct ipu_node_names *names = &sensor->node_names; + struct software_node *nodes = sensor->swnodes; + + ipu_bridge_init_swnode_names(sensor); + + nodes[SWNODE_SENSOR_HID] = NODE_SENSOR(sensor->name, + sensor->dev_properties); + nodes[SWNODE_SENSOR_PORT] = NODE_PORT(sensor->node_names.port, + &nodes[SWNODE_SENSOR_HID]); + nodes[SWNODE_SENSOR_ENDPOINT] = NODE_ENDPOINT( + sensor->node_names.endpoint, + &nodes[SWNODE_SENSOR_PORT], + sensor->ep_properties); + nodes[SWNODE_IPU_PORT] = NODE_PORT(sensor->node_names.remote_port, + &bridge->ipu_hid_node); + nodes[SWNODE_IPU_ENDPOINT] = NODE_ENDPOINT( + sensor->node_names.endpoint, + &nodes[SWNODE_IPU_PORT], + sensor->ipu_properties); + + if (sensor->csi_dev) { + const char *device_hid = ""; + + device_hid = acpi_device_hid(sensor->ivsc_adev); + + snprintf(sensor->ivsc_name, sizeof(sensor->ivsc_name), "%s-%u", + device_hid, sensor->link); + + nodes[SWNODE_IVSC_HID] = NODE_SENSOR(sensor->ivsc_name, + sensor->ivsc_properties); + nodes[SWNODE_IVSC_SENSOR_PORT] = + NODE_PORT(names->ivsc_sensor_port, + &nodes[SWNODE_IVSC_HID]); + nodes[SWNODE_IVSC_SENSOR_ENDPOINT] = + NODE_ENDPOINT(names->endpoint, + &nodes[SWNODE_IVSC_SENSOR_PORT], + sensor->ivsc_sensor_ep_properties); + nodes[SWNODE_IVSC_IPU_PORT] = + NODE_PORT(names->ivsc_ipu_port, + &nodes[SWNODE_IVSC_HID]); + nodes[SWNODE_IVSC_IPU_ENDPOINT] = + NODE_ENDPOINT(names->endpoint, + &nodes[SWNODE_IVSC_IPU_PORT], + sensor->ivsc_ipu_ep_properties); + } + + nodes[SWNODE_VCM] = NODE_VCM(sensor->node_names.vcm); + + ipu_bridge_init_swnode_group(sensor); +} + +/* + * The actual instantiation must be done from a workqueue to avoid + * a deadlock on taking list_lock from v4l2-async twice. + */ +struct ipu_bridge_instantiate_vcm_work_data { + struct work_struct work; + struct device *sensor; + char name[16]; + struct i2c_board_info board_info; +}; + +static void ipu_bridge_instantiate_vcm_work(struct work_struct *work) +{ + struct ipu_bridge_instantiate_vcm_work_data *data = + container_of(work, struct ipu_bridge_instantiate_vcm_work_data, + work); + struct acpi_device *adev = ACPI_COMPANION(data->sensor); + struct i2c_client *vcm_client; + bool put_fwnode = true; + int ret; + + /* + * The client may get probed before the device_link gets added below + * make sure the sensor is powered-up during probe. + */ + ret = pm_runtime_get_sync(data->sensor); + if (ret < 0) { + dev_err(data->sensor, "Error %d runtime-resuming sensor, cannot instantiate VCM\n", + ret); + goto out_pm_put; + } + + /* + * Note the client is created only once and then kept around + * even after a rmmod, just like the software-nodes. + */ + vcm_client = i2c_acpi_new_device_by_fwnode(acpi_fwnode_handle(adev), + 1, &data->board_info); + if (IS_ERR(vcm_client)) { + dev_err(data->sensor, "Error instantiating VCM client: %pe\n", + vcm_client); + goto out_pm_put; + } + + device_link_add(&vcm_client->dev, data->sensor, DL_FLAG_PM_RUNTIME); + + dev_info(data->sensor, "Instantiated %s VCM\n", data->board_info.type); + put_fwnode = false; /* Ownership has passed to the i2c-client */ + +out_pm_put: + pm_runtime_put(data->sensor); + put_device(data->sensor); + if (put_fwnode) + fwnode_handle_put(data->board_info.fwnode); + kfree(data); +} + +int ipu_bridge_instantiate_vcm(struct device *sensor) +{ + struct ipu_bridge_instantiate_vcm_work_data *data; + struct fwnode_handle *vcm_fwnode; + struct i2c_client *vcm_client; + struct acpi_device *adev; + char *sep; + + adev = ACPI_COMPANION(sensor); + if (!adev) + return 0; + + vcm_fwnode = fwnode_find_reference(dev_fwnode(sensor), "lens-focus", 0); + if (IS_ERR(vcm_fwnode)) + return 0; + + /* When reloading modules the client will already exist */ + vcm_client = i2c_find_device_by_fwnode(vcm_fwnode); + if (vcm_client) { + fwnode_handle_put(vcm_fwnode); + put_device(&vcm_client->dev); + return 0; + } + + data = kzalloc_obj(*data); + if (!data) { + fwnode_handle_put(vcm_fwnode); + return -ENOMEM; + } + + INIT_WORK(&data->work, ipu_bridge_instantiate_vcm_work); + data->sensor = get_device(sensor); + snprintf(data->name, sizeof(data->name), "%s-VCM", + acpi_dev_name(adev)); + data->board_info.dev_name = data->name; + data->board_info.fwnode = vcm_fwnode; + snprintf(data->board_info.type, sizeof(data->board_info.type), + "%pfwP", vcm_fwnode); + /* Strip "-" postfix */ + sep = strchrnul(data->board_info.type, '-'); + *sep = 0; + + queue_work(system_long_wq, &data->work); + + return 0; +} +EXPORT_SYMBOL_NS_GPL(ipu_bridge_instantiate_vcm, "INTEL_IPU_BRIDGE"); + +static int ipu_bridge_instantiate_ivsc(struct ipu_sensor *sensor) +{ + struct fwnode_handle *fwnode; + + if (!sensor->csi_dev) + return 0; + + fwnode = software_node_fwnode(&sensor->swnodes[SWNODE_IVSC_HID]); + if (!fwnode) + return -ENODEV; + + set_secondary_fwnode(sensor->csi_dev, fwnode); + + return 0; +} + +static void ipu_bridge_unregister_sensors(struct ipu_bridge *bridge) +{ + struct ipu_sensor *sensor; + unsigned int i; + + for (i = 0; i < bridge->n_sensors; i++) { + sensor = &bridge->sensors[i]; + software_node_unregister_node_group(sensor->group); + acpi_dev_put(sensor->adev); + put_device(sensor->csi_dev); + acpi_dev_put(sensor->ivsc_adev); + } +} + +static int ipu_bridge_connect_sensor(const struct ipu_sensor_config *cfg, + struct ipu_bridge *bridge) +{ + struct fwnode_handle *fwnode, *primary; + struct ipu_sensor *sensor; + struct acpi_device *adev = NULL; + int ret; + + for_each_acpi_dev_match(adev, cfg->hid, NULL, -1) { + if (!ACPI_PTR(adev->status.enabled)) + continue; + + if (bridge->n_sensors >= IPU_MAX_PORTS) { + acpi_dev_put(adev); + dev_err(bridge->dev, "Exceeded available IPU ports\n"); + return -EINVAL; + } + + sensor = &bridge->sensors[bridge->n_sensors]; + + ret = bridge->parse_sensor_fwnode(adev, sensor); + if (ret) + goto err_put_adev; + + snprintf(sensor->name, sizeof(sensor->name), "%s-%u", + cfg->hid, sensor->link); + + ret = ipu_bridge_check_ivsc_dev(sensor, adev); + if (ret) + goto err_put_adev; + + ipu_bridge_create_fwnode_properties(sensor, bridge, cfg); + ipu_bridge_create_connection_swnodes(bridge, sensor); + + ret = software_node_register_node_group(sensor->group); + if (ret) + goto err_put_ivsc; + + fwnode = software_node_fwnode(&sensor->swnodes[ + SWNODE_SENSOR_HID]); + if (!fwnode) { + ret = -ENODEV; + goto err_free_swnodes; + } + + sensor->adev = ACPI_PTR(acpi_dev_get(adev)); + + primary = acpi_fwnode_handle(adev); + primary->secondary = fwnode; + + ret = ipu_bridge_instantiate_ivsc(sensor); + if (ret) + goto err_free_swnodes; + + dev_info(bridge->dev, "Found supported sensor %s\n", + acpi_dev_name(adev)); + + bridge->n_sensors++; + } + + return 0; + +err_free_swnodes: + software_node_unregister_node_group(sensor->group); +err_put_ivsc: + put_device(sensor->csi_dev); + acpi_dev_put(sensor->ivsc_adev); +err_put_adev: + acpi_dev_put(adev); + return ret; +} + +static int ipu_bridge_connect_sensors(struct ipu_bridge *bridge) +{ + unsigned int i; + int ret; + + for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) { + const struct ipu_sensor_config *cfg = + &ipu_supported_sensors[i]; + + ret = ipu_bridge_connect_sensor(cfg, bridge); + if (ret) + goto err_unregister_sensors; + } + + return 0; + +err_unregister_sensors: + ipu_bridge_unregister_sensors(bridge); + return ret; +} + +static int ipu_bridge_ivsc_is_ready(void) +{ + struct acpi_device *sensor_adev, *adev; + struct device *csi_dev; + bool ready = true; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(ipu_supported_sensors); i++) { + const struct ipu_sensor_config *cfg = + &ipu_supported_sensors[i]; + + for_each_acpi_dev_match(sensor_adev, cfg->hid, NULL, -1) { + if (!ACPI_PTR(sensor_adev->status.enabled)) + continue; + + adev = ipu_bridge_get_ivsc_acpi_dev(sensor_adev); + if (!adev) + continue; + + csi_dev = ipu_bridge_get_ivsc_csi_dev(adev); + if (!csi_dev) + ready = false; + + put_device(csi_dev); + acpi_dev_put(adev); + } + } + + return ready; +} + +static int ipu_bridge_check_fwnode_graph(struct fwnode_handle *fwnode) +{ + struct fwnode_handle *endpoint; + + if (IS_ERR_OR_NULL(fwnode)) + return -EINVAL; + + endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL); + if (endpoint) { + fwnode_handle_put(endpoint); + return 0; + } + + return ipu_bridge_check_fwnode_graph(fwnode->secondary); +} + +static DEFINE_MUTEX(ipu_bridge_mutex); + +int ipu_bridge_init(struct device *dev, + ipu_parse_sensor_fwnode_t parse_sensor_fwnode) +{ + struct fwnode_handle *fwnode; + struct ipu_bridge *bridge; + unsigned int i; + int ret; + + guard(mutex)(&ipu_bridge_mutex); + + if (!ipu_bridge_check_fwnode_graph(dev_fwnode(dev))) + return 0; + + if (!ipu_bridge_ivsc_is_ready()) + return dev_err_probe(dev, -EPROBE_DEFER, + "waiting for IVSC to become ready\n"); + + bridge = kzalloc_obj(*bridge); + if (!bridge) + return -ENOMEM; + + strscpy(bridge->ipu_node_name, IPU_HID, + sizeof(bridge->ipu_node_name)); + bridge->ipu_hid_node.name = bridge->ipu_node_name; + bridge->dev = dev; + bridge->parse_sensor_fwnode = parse_sensor_fwnode; + + ret = software_node_register(&bridge->ipu_hid_node); + if (ret < 0) { + dev_err(dev, "Failed to register the IPU HID node\n"); + goto err_free_bridge; + } + + /* + * Map the lane arrangement, which is fixed for the IPU3 (meaning we + * only need one, rather than one per sensor). We include it as a + * member of the struct ipu_bridge rather than a global variable so + * that it survives if the module is unloaded along with the rest of + * the struct. + */ + for (i = 0; i < IPU_MAX_LANES; i++) + bridge->data_lanes[i] = i + 1; + + ret = ipu_bridge_connect_sensors(bridge); + if (ret || bridge->n_sensors == 0) + goto err_unregister_ipu; + + dev_info(dev, "Connected %d cameras\n", bridge->n_sensors); + + fwnode = software_node_fwnode(&bridge->ipu_hid_node); + if (!fwnode) { + dev_err(dev, "Error getting fwnode from ipu software_node\n"); + ret = -ENODEV; + goto err_unregister_sensors; + } + + set_secondary_fwnode(dev, fwnode); + + return 0; + +err_unregister_sensors: + ipu_bridge_unregister_sensors(bridge); +err_unregister_ipu: + software_node_unregister(&bridge->ipu_hid_node); +err_free_bridge: + kfree(bridge); + + return ret; +} +EXPORT_SYMBOL_NS_GPL(ipu_bridge_init, "INTEL_IPU_BRIDGE"); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Intel IPU Sensors Bridge driver");