media: i2c: ov7251: fixes and infrared strobe for Surface Book 2
Subsystems (3)
v2 cover media: i2c: ov7251: fixes and infrared strobe for Surface Book 2
The Microsoft Surface Book 2 has an OV7251 infrared camera (used by Windows Hello) next to its front camera, both on the Intel IPU3 camera interface. Its 850 nm emitter is wired to the sensor's STROBE output
pin: the ACPI tables describe no LED, GPIO line or regulator for it. With the mainline driver the camera streams, but always unlit.
Patches 1 to 4 fix bugs found while testing on this machine and on a Surface Pro 7+, 5 and 6 rework the driver's state handling and add the placement controls:
- 1: the 19.2 MHz PLL table for the 319.2 MHz link multiplies the clock before dividing it, which runs the PLL's VCO at 2553.6 MHz against the 300 to 800 MHz the datasheet allows. The Surface Pro 7+ drops most frames at that setting; the Book 2 copes. Dividing first keeps the link rate and brings the VCO to 638.4 MHz (Fixes, Cc stable). It goes before patch 2 because patch 2 is what makes the driver select this table.
- 2: the link frequency index is taken from the firmware list instead of the driver's table, so the sensor runs at 240 MHz while ipu-bridge asks for 319.2 MHz (Fixes, Cc stable).
- 3: V4L2_CID_PIXEL_RATE reports the CSI-2 bus rate instead of the pixel array clock, which is 48 MHz at both link frequencies (Fixes, Cc stable).
- 4: the 60 and 90 fps frame intervals are accepted but the sensor keeps streaming at 30 fps, the vertical blanking control not being updated (Fixes, Cc stable).
- 5: conversion to the sub-device active state, as Sakari asked on v1. The core then handles control events, which v1's patch 3 added by hand, and v4l2-compliance passes on the sensor.
- 6: orientation and rotation controls from firmware.
Dave Stevenson noted on v1 that set_frame_interval does not belong in a raw sensor driver that also exposes VBLANK. Patch 4 only fixes the current behaviour and does not stand in the way of removing it.
Patches 7 and 8 add the emitter. 7 moves the strobe registers, which the three mode tables write identically, out of the tables without functional change. 8 adds a V4L2_CID_FLASH_STROBE_OE control that enables the STROBE pin, pulses it on alternate frames and makes the pulse follow the exposure, from 52 lines up to 45% of the frame and never more than 776 lines. This matches the Windows driver installed on this machine (Intel ov7251.sys 30.18305.6.5127, read with a decompiler): all four of its streaming modes enable the STROBE pin and clamp the pulse to the exposure within 52 to 776 lines; the two modes Windows Hello uses light alternate frames (0xaa), the other two every frame (0xff). A register dump taken on a Surface Book 2 during a Windows Hello sign-in [1] matches the 30 fps alternate mode. V4L2 has no per-frame illumination flag, so user space tells lit frames from dark ones by brightness; the dark frames give the ambient-light reference a face liveness check uses. With every frame lit and filmed by a phone at locked exposure, each pulse is 1.1x (default exposure) to 1.2x (776-line pulse) as bright as during a Windows Hello sign-in on the same unit; with alternate frames the average emission is within the same ratio of Windows'.
A linux-surface pull request for the Surface Pro 7+ [2] instead turns the emitter on unconditionally while the sensor streams, with a module parameter to disable it. This series leaves the choice to user space:
the control is off by default, and the emitter is off whenever the sensor is not streaming.
Patch 9 makes ipu-bridge report a rotation of 90 degrees for the infrared sensor, whose frames show an upright scene turned 90 degrees clockwise while the firmware says 0. It adds a DMI quirk table next to the existing one for upside-down sensors, with entries for the Surface Book 2 and the Surface Pro 7+, the two machines checked. A user-space project for the Surface Book 3 rotates that model's infrared frames by 90 degrees as well [3].
Patch 8 needs V4L2_CID_FLASH_STROBE_OE, added in v7.0 by commit 5be4154f6255 ("media: v4l: ctrls: add a control for enabling strobe output"), and is not meant for stable. Only patches 1 to 4, the ones with a stable tag, apply to older trees on their own.
[1] https://github.com/linux-surface/linux-surface/issues/739
[2] https://github.com/linux-surface/linux-surface/pull/2252
[3] https://github.com/linux-surface/linux-surface/pull/2289
Changes since v1 [4]:
- New patch 1: in-range PLL dividers for the 319.2 MHz link. D. Manresa reported that v1's index fix made his Surface Pro 7+ (IPU6) drop most frames and stall; with the dividers swapped he measured 13 clean runs of 13 against 7 bad of 10. Dave Stevenson read the same values out of the datasheet and measured on a 24 MHz board that the out-of-range tables do not produce their nominal frequency. The 24 MHz tables have problems of their own and are left
alone: nobody in the thread can test a change to them.
- Patch 2: Reviewed-by from Daniel Scally.
- New patch 3: report the pixel array rate, 48 MHz, for both link frequencies. v1 reported 63.84 MHz at 319.2 MHz, the bus rate. Dave Stevenson saw the same on a Raspberry Pi, where libcamera asked for 100 fps and got 75.
- Patch 3 of v1 (control event support) dropped; patch 5 converts the driver to the sub-device active state instead, following ov5645, which gives the same result through the core (Sakari Ailus).
- Patch 8: use PERCENT from linux/units.h and plain clamp(), clamp the span to its limits before applying the duty limit (Andy Shevchenko). The CCI register helpers are left for a follow-up after this series.
- Patch 9: entry for the Surface Pro 7+, whose infrared frames D. Manresa found turned the same way as the Book 2's.
- Tested-by from D. Manresa on a Surface Pro 7+ (IPU6, kernel 6.19) on patches 4, 6 and 7. He tested v1's strobe control too, with the same result after Andy's changes, and asked to re-run the v2 patch rather than have the tag carried.
[4] https://lore.kernel.org/linux-media/[email protected]/
Tested on a Surface Book 2 (i7-8650U) with the series built out of tree against linux-surface 7.2.12 (test suite, logs and a v4l2loopback bridge for applications: https://github.com/antoine79/surface-ir-camera):
- link frequency 319200000, pixel rate 48000000, 30.0 fps measured; PLL registers read back as programmed while streaming
- 3 x 155 frames at 30 fps and 3 x 155 at 90 fps on the 319.2 MHz
link: 0 dropped buffers, 0 CSI-2 error lines
- v4l2-compliance -u on the sensor: 0 failures (was 1)
- --set-subdev-fps 60 / 90: 60.16 / 90.46 fps measured (was 30.01)
- orientation front, rotation 90 (0 before patch 9); the colour sensors still report 180
- dark frames and the sensor test pattern unchanged after patch 7
- strobe span read back while streaming: 504 lines at exposure 504, 776 at exposure 1704 (30 fps), 258 at exposure 552 (90 fps)
- mean pixel value of an indoor infrared frame at analogue gain 128:
18 to 19 unlit; with the strobe enabled the lit frames read 526 at exposure 504 and 609 at 1704, the dark frames in between 18 to 21
- strobe off and sensor suspended within 2 s of the stream stopping
- 10 unbind/unload/load cycles: no warning in the kernel log
- Howdy face login enrols and recognises through a v4l2loopback bridge that passes the lit frames
The remaining v4l2-compliance failures are in the ipu3-cio2 capture driver (buffer creation and mapping checks) and unchanged by this series.
I developed this series with an AI coding assistant (Claude): the design choices (alternate frames, the pulse limits, the exact SKU match), the reading of the Windows driver and the measurements above are mine, and I reviewed and tested every patch on the hardware before signing it off.
Antoine Pichot (9):
media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
media: i2c: ov7251: Fix link frequency index
media: i2c: ov7251: Report the pixel array rate for both link
frequencies
media: i2c: ov7251: Reset vblank when the frame interval changes
media: i2c: ov7251: Use sub-device active state
media: i2c: ov7251: Add orientation and rotation controls
media: i2c: ov7251: Move the strobe registers out of the mode tables
media: i2c: ov7251: Add strobe output enable control
media: ipu-bridge: Add a rotation quirk for the Surface Book 2 and Pro
7+
drivers/media/i2c/ov7251.c | 360 +++++++++++++-------------- drivers/media/pci/intel/ipu-bridge.c | 31 +++ 2 files changed, 209 insertions(+), 182 deletions(-)
Signature · 1 line
2.56.0
PATCH v2 1/9 media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
The 19.2 MHz PLL1 table for the 319.2 MHz link frequency puts a pre-divider of 1 before the multiplier of 133 and a divider of 4 after it, so the PLL1 multiplier output (the VCO) runs at 19.2 x 133 = 2553.6 MHz before being divided down to 638.4 MHz. The OV7750/OV7251 datasheet (rev 2.12, table 2-11) limits it to 300 to 800 MHz.
Use pre-divider 4 (register 0x30B4 = 0x04) and divider 1 (0x30B1 = 0x01). The multiplier output is then 4.8 x 133 = 638.4 MHz, inside the range, and the MIPI clock stays 638.4 Mbps, so the link frequency does not change. The Windows driver programs these values for the Surface Book 2's Windows Hello mode.
On ACPI systems this table has been unreachable since it was added, because ov7251_check_hwcfg() selects the wrong index. The next patch fixes the index, so the two must go in together. With the index fix alone, a Surface Pro 7+ (IPU6) had CSI-2 errors in 7 of 10 150-frame runs, 3 of them stalls; with this change 0 of 13. A Surface Book 2 (IPU3) streams clean with either table. Dave Stevenson measured on a 24 MHz Raspberry Pi board that the out-of-range tables miss their nominal frequency and proposed the same values. The 24 MHz tables are left alone because nobody in the thread can test a change to them.
--- drivers/media/i2c/ov7251.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/ov7251.c +2 -2
@@ -174,9 +174,9 @@ static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_240_mhz = { };static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_319_2_mhz = { - .pre_div =0x01 , + .pre_div =0x04 , .mult =0x85 , - .div =0x04 , + .div =0x01 , .pix_div =0x0a , .mipi_div =0x05 , };
Signature · 1 line
2.56.0
Re: [PATCH v2 1/9] media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
Quoted 22 lines · Antoine Pichot
The 19.2 MHz PLL1 table for the 319.2 MHz link frequency puts a pre-divider of 1 before the multiplier of 133 and a divider of 4 after it, so the PLL1 multiplier output (the VCO) runs at 19.2 x 133 = 2553.6 MHz before being divided down to 638.4 MHz. The OV7750/OV7251 datasheet (rev 2.12, table 2-11) limits it to 300 to 800 MHz.
Use pre-divider 4 (register
0x30B4=0x04) and divider 1 (0x30B1=0x01). The multiplier output is then 4.8 x 133 = 638.4 MHz, inside the range, and the MIPI clock stays 638.4 Mbps, so the link frequency does not change. The Windows driver programs these values for the Surface Book 2's Windows Hello mode.On ACPI systems this table has been unreachable since it was added, because
ov7251_check_hwcfg()selects the wrong index. The next patch fixes the index, so the two must go in together. With the index fix alone, a Surface Pro 7+ (IPU6) had CSI-2 errors in 7 of 10 150-frame runs, 3 of them stalls; with this change 0 of 13. A Surface Book 2 (IPU3) streams clean with either table. Dave Stevenson measured on a 24 MHz Raspberry Pi board that the out-of-range tables miss their nominal frequency and proposed the same values. The 24 MHz tables are left alone because nobody in the thread can test a change to them.
Quoted 4 lines
Fixes ed9566ce1946 ("media: i2c: Add support for new frequencies to ov7251")
These Links are not referred in the text above, make sure they are being referred, otherwise Linus T. becomes grumpy on this. On top, consider using Reported-by and Closes tags if applicable.
...
Code wise LGTM (I followed the discussion).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/9] media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
In reply to Antoine PichotHi Antoine
Quoted 13 lines · Antoine Pichot
The 19.2 MHz PLL1 table for the 319.2 MHz link frequency puts a pre-divider of 1 before the multiplier of 133 and a divider of 4 after it, so the PLL1 multiplier output (the VCO) runs at 19.2 x 133 = 2553.6 MHz before being divided down to 638.4 MHz. The OV7750/OV7251 datasheet (rev 2.12, table 2-11) limits it to 300 to 800 MHz.
Use pre-divider 4 (register
0x30B4=0x04) and divider 1 (0x30B1=0x01). The multiplier output is then 4.8 x 133 = 638.4 MHz, inside the range, and the MIPI clock stays 638.4 Mbps, so the link frequency does not change. The Windows driver programs these values for the Surface Book 2's Windows Hello mode.
Quoted 9 lines
On ACPI systems this table has been unreachable since it was added, because
ov7251_check_hwcfg()selects the wrong index. The next patch fixes the index, so the two must go in together. With the index fix alone, a Surface Pro 7+ (IPU6) had CSI-2 errors in 7 of 10 150-frame runs, 3 of them stalls; with this change 0 of 13. A Surface Book 2 (IPU3) streams clean with either table. Dave Stevenson measured on a 24 MHz Raspberry Pi board that the out-of-range tables miss their nominal frequency and proposed the same values. The 24 MHz tables are left alone because nobody in the thread can test a change to them.
I have a 24MHz module and had tested with it, which you'd acknowledged in the previous sentence.
You've got me sidetracked into doing the cleanups, and that has included validating the link frequencies on a 24MHz module. I've pushed where I am to
https://github.com/6by9/linux/tree/rpi-7.3.y-ov7251. Patch "media:
ov7251: Add support for a 400MHz link freq" fixes up all the 24MHz PLL settings. I've also done the conversion to use the V4L2 CCI library.
The cleanups do raise the question of what registers 0x3016-0x301c do, and which of the 2 variants is preferable (60 and 90fps both use the same values). The datasheet SC_CLKRSTx naming isn't exactly descriptive. I don't suppose you have any information?
It would be lovely to ditch those tables / merge them into global_init_settings, retaining ov7251_mode_info_data only to provide backwards compatibility with those clients that use set_frame_interval.
Feel free to pick up any or all of those patches, otherwise I'll come along behind you and submit them.
Dave
Quoted 28 lines
Fixes ed9566ce1946 ("media: i2c: Add support for new frequencies to ov7251")Link https://lore.kernel.org/linux-media/CAPY8ntBFJJPg3y46gC=bNq_55QifC7mKmeq8+pAEDVN42fGUXw@mail.gmail.com/ open hereSigned-off-by Antoine Pichot <[email protected]>--- drivers/media/i2c/ov7251.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/media/i2c/ov7251.c +2
-3@@ -174,9 +174,9 @@ static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_240_mhz = { };static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_319_2_mhz = { - .pre_div =0x01 , + .pre_div =0x04 , .mult =0x85 , - .div =0x04 , + .div =0x01 , .pix_div =0x0a , .mipi_div =0x05 , }; --2.56.0
Re: [PATCH v2 1/9] media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
Hi Dave,
Quoted 2 lines · Dave Stevenson
I have a 24MHz module and had tested with it, which you'd acknowledged in the previous sentence.
You are right, sorry. v3 replaces that sentence: the 24 MHz tables are left to a separate change, with your measurement cited.
Quoted 4 lines
I've pushed where I am to
https://github.com/6by9/linux/tree/rpi-7.3.y-ov7251. Patch "media:
ov7251: Add support for a 400MHz link freq" fixes up all the 24MHz PLL settings. I've also done the conversion to use the V4L2 CCI library.
Thanks. For v3 I am taking your "Move the common registers into the global init table" as patch 7, under your authorship, in place of my strobe-register patch: the strobe registers now sit in ov7251_global_init_setting, which answers your question on 5/7 of v1.
The move has two side effects on the bus, both in the commit message:
- The common registers are written at power-on instead of at every stream start, so a restart within the runtime PM autosuspend delay skips them. The datasheet allows it (table 2-8: software standby keeps the register values).
- Register 0x3820 holds 0x40 while the sensor streams, where it held 0x00 before. The flip control writes back the value probe cached, and probe now caches it after the init table has written 0x40, not right after the reset when it read 0x00. I poked the old value in during a lit stream: brightness, picture, orientation and frame rate did not change.
Tested on the Book 2 against v2: the clock, timing, PLL, flip and strobe registers read back while streaming at 30, 60 and 90 fps are identical apart from 0x3820. On a sensor kept powered, a restart and mode switches from 30 to 90, to 60 and back to 30 fps give a correct first frame every time, at 90.45, 60.15 and 30.0 fps.
The rest of your branch (400 MHz, CCI, HBLANK and MIN_HTS, the optional enable GPIO, the gain rename, the VBLANK minimum) I leave to you as you offered, to keep v3 to the fixes and the IR camera. I can test them on the Book 2 (19.2 MHz, 319.2 MHz link) when you post them. Two things for that:
- A data point for the VBLANK minimum. A sweep on the Book 2 in the 30 fps mode (exposure 400, gain 16, 20 frames per value) gives normal frames down to 41, white frames from 40 to 26 and frames rolled sideways from 25 to 16, both without CSI-2 errors, CRC errors from 13 and a stalled receiver at 10. The same boundary as on your Pi.
- At the tip of the branch (fde519da0503, "media: ov7251: Reduce OV7251_TIMING_MIN_HTS"), unless I misread it, ov7251_set_vflip() writes OV7251_TIMING_FORMAT2 (0x3821) rather than 0x3820, probe stores the 0x3821 read in timing_format1 rather than timing_format2, and the return value of devm_cci_regmap_init_i2c() is not checked. In case it helps before you post.
Thanks,
Antoine
Re: [PATCH v2 1/9] media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
Hi Antoine
Quoted 15 lines · Antoine Pichot
Hi Dave,
Quoted 2 lines · Dave Stevenson
I have a 24MHz module and had tested with it, which you'd acknowledged in the previous sentence.
You are right, sorry. v3 replaces that sentence: the 24 MHz tables are left to a separate change, with your measurement cited.
Quoted 4 lines
I've pushed where I am to
https://github.com/6by9/linux/tree/rpi-7.3.y-ov7251. Patch "media:
ov7251: Add support for a 400MHz link freq" fixes up all the 24MHz PLL settings. I've also done the conversion to use the V4L2 CCI library.
Quoted 4 lines
Thanks. For v3 I am taking your "Move the common registers into the global init table" as patch 7, under your authorship, in place of my strobe-register patch: the strobe registers now sit in
ov7251_global_init_setting, which answers your question on 5/7 of v1.
All good with me.
Quoted 13 lines
The move has two side effects on the bus, both in the commit message:
- The common registers are written at power-on instead of at every stream start, so a restart within the runtime PM autosuspend delay skips them. The datasheet allows it (table 2-8: software standby keeps the register values).
- Register0x3820holds0x40while the sensor streams, where it held0x00before. The flip control writes back the value probe cached, and probe now caches it after the init table has written0x40, not right after the reset when it read0x00. I poked the old value in during a lit stream: brightness, picture, orientation and frame rate did not change.
Quoted 5 lines
Tested on the Book 2 against v2: the clock, timing, PLL, flip and strobe registers read back while streaming at 30, 60 and 90 fps are identical apart from
0x3820. On a sensor kept powered, a restart and mode switches from 30 to 90, to 60 and back to 30 fps give a correct first frame every time, at 90.45, 60.15 and 30.0 fps.
That all sounds good.
I hadn't noticed that the common registers were written from power on, so thanks for checking it. It's generally when there are multiple resolutions supported that using the reset at the start of the common registers is of most use.
Quoted 4 lines
The rest of your branch (400 MHz, CCI, HBLANK and
MIN_HTS, the optional enable GPIO, the gain rename, the VBLANK minimum) I leave to you as you offered, to keep v3 to the fixes and the IR camera. I can test them on the Book 2 (19.2 MHz, 319.2 MHz link) when you post them.
That's fine with me. I'll leave doing any further work until your patches land, as the CCI change is tiresome to handle conflicts with.
Quoted 8 lines
Two things for that:
- A data point for the VBLANK minimum. A sweep on the Book 2 in the 30 fps mode (exposure 400, gain 16, 20 frames per value) gives normal frames down to 41, white frames from 40 to 26 and frames rolled sideways from 25 to 16, both without CSI-2 errors, CRC errors from 13 and a stalled receiver at 10. The same boundary as on your Pi.
Great. I was expecting it to be down to timing within the pixel array rather than the MIPI configuration, and too few blanking lines results in things going awry. I'm happy to hear that it does the same for you.
Quoted 6 lines
- At the tip of the branch (
fde519da0503, "media: ov7251: ReduceOV7251_TIMING_MIN_HTS"), unless I misread it,ov7251_set_vflip()writesOV7251_TIMING_FORMAT2(0x3821) rather than0x3820, probe stores the0x3821read intiming_format1rather thantiming_format2, and the return value ofdevm_cci_regmap_init_i2c()is not checked. In case it helps before you post.
Doh, too much copy/paste. Thanks for picking it up. That error is actually in "media: ov7251: Convert to use V4L2 CCI library". I've updated my local branch, but not repushed.
Dave
Quoted 2 lines
Thanks,
Antoine
Re: [PATCH v2 1/9] media: i2c: ov7251: Use in-range PLL1 dividers for the 319.2 MHz link
In reply to Dave StevensonHi Dave,
Quoted 4 lines · Dave Stevenson
The cleanups do raise the question of what registers 0x3016-0x301c do, and which of the 2 variants is preferable (60 and 90fps both use the same values). The datasheet
SC_CLKRSTxnaming isn't exactly descriptive. I don't suppose you have any information?
The datasheet calls them the system control clock/reset registers (SC_CLKRST0-6, table 7-1) and names every bit: per internal block, a clock gate in bits 7-4 and a reset in bits 3-0. I checked them on the Book 2 and believe they can safely move to ov7251_global_init_setting for all three rates with the 60 and 90 fps values, 0x3016 = 0x10 and 0x00 in the other five: timing control clocked, every other gate and every reset clear. Here is why:
a. Poked bit by bit mid-stream, any reset bit set holds its block
(frames stop or come out empty), and only one gate matters for streaming, the timing control's (0x3016 bit 4): cleared, frames stop at every rate. Two other gates matter only to blocks the driver does not use, group hold and the OTP. Every other gate and the debug bits changed nothing I could see. None of this depends on the frame rate.
b. Written as whole values at 30, 60 and 90 fps, mid-stream and with
the sensor restarted from software standby, this value, every gate set (the 30 fps table's 0xf0), the reset values and a set with only the documented gates on all streamed identically to the driver's
values: same frame rate, lit and dark frame means, black level at gain 16 and 1023, test pattern bars, no sequence gaps, errors or kernel messages. So did a rebuilt driver started from power-on with the six lines in the init table, this value or 0xf0, right after the reset or at its end, and with the lines simply removed. Every bit clear gave no frames at any rate.
c. It is what the Windows driver for this module (ov7251.sys
30.18305.6.5127) writes in all four of its modes, 30 fps included, what ov6211 and the other vendor tables I found use (Intel Aero, NVIDIA CyberDog, Allwinner, Unisoc, Sophgo, Ingenic, Cypress, at most one bit apart in 0x3018 and 0x301c), and what your rpi-5.15.y commit 63843c4bb331 [1] ran at all three rates. The 0xf0 set I only found in 30 fps tables derived from Qualcomm's auto-generated sensor library (ov7251_lib.h), which the 2018 driver's tables
match: its 30 fps mode carried 0xf0 and its 90 fps mode 0x10/0x00, so the two variants look inherited, not chosen.
Not writing them at all works too, then; I prefer the value above because it is the one everybody else runs. A driver that adds group hold one day sets 0x301c bit 5.
Details follow: block names, bit map, method and results.
[1] https://github.com/raspberrypi/linux/commit/63843c4bb331c07492adcceb7c693fd39c26d88b
Details
=======
Block names, as abbreviated in the datasheet; the expansions are OmniVision's usual ones, the confidence is mine:
name block confidence fc frame control (starts and ends each frame) high, tested tc timing control (line and pixel counters) high, tested aec automatic exposure control (unused here) high blc black level correction high isp pixel pipeline high vfifo output line buffer high, tested mipi CSI-2 transmitter high, tested otp one-time programmable memory high, read it grp group hold (applies writes at a frame edge) high, tested bist built-in self-test high srb serial register bus (I2C register block) medium asram asram_tst, internal RAM test logic medium sync snr_sync, frame sync to an external signal medium ac, src, mipisc (mipi_sc): datasheet names only low dbg "Debug control", undocumented n/a
Decoding, from table 7-1 of the datasheet (rev 2.12), which names every bit: each register holds four blocks, their clock gates in bits 7-4 (sclk_, * = pclk_) and their resets in bits 3-0 (rst_), at the same position except in 0x301a. The reset column is the value after the software reset (0x0103), from the same table.
reg reset b7 b6 b5 b4 b3 b2 b1 b0 0x3016 0xf0 fc dbg aec tc fc dbg aec tc 0x3017 0xf0 blc isp dbg vfifo blc isp dbg vfifo 0x3018 0xf0 dbg mipi ac otp dbg mipi ac otp 0x3019 0xf0 dbg dbg dbg dbg dbg dbg dbg dbg 0x301a 0xf0 dbg dbg vfifo* mipi* dbg mipisc dbg dbg 0x301b 0xf0 src dbg asram sync src dbg asram sync 0x301c 0xf2 bist srb grp dbg bist dbg grp dbg
Method: v3 driver streaming at 30 fps, where the seven registers read 0xf0 (every gate set, every reset clear), frames captured with v4l2-ctl, emitter pulsing alternate frames in a dark room or test pattern on. From user space over I2C, one bit rewritten mid-stream and the block's visible output compared before, during and after; a reset bit held for 2 s. Group hold and the OTP, which a stream never exercises, were driven directly: an exposure write under group hold then launched, an OTP load into its buffer, one attempt per stream, bit set against bit cleared. Nothing persists, the driver resets the sensor at every power-on.
Then whole values, at 30, 60 and 90 fps each: every bit clear; the timing control gate alone (0x3016 = 0x10, the rest 0x00); every gate set (0xf0 in all six); every documented gate set and the debug bits clear; and the reset values of the table above. The six registers were written mid-stream, and written with the sensor in software standby (0x0100) and restarted, the nearest thing to the init table without a driver rebuild. Each run checked the frame rate, the lit and dark frame means (strobe on alternate frames), the black level at gain 16 and 1023 with a 1-line exposure, the test pattern bars, the sequence numbers and the receiver's kernel messages against a run with the driver's own values. Finally from power-on: the driver rebuilt with the six lines moved to the init table (this value at its end and right after the reset, 0xf0 at its end) or removed, streamed at the three rates with the same checks.
Results (n/m = streams):
clock gate cleared effect
0x301c b5 grp group-hold write dropped 4/4 (applied 4/4
with the bit set), 30 and 60 fps
0x3018 b4 otp OTP load works 16/32 (16/16 with the bit
set), whatever the wait, rate or the other five registers
0x3017 b7 blc none: black level still tracks a gain change
0x3016 b6 dbg none: strobe still fires
0x3016 b4 tc frames stop, no error, resume when set
again (30 and 60 fps)reset held for 2 s effect 0x3016 b3 fc, b0 tc, frames stop, no error, resume on clear 0x3017 b0 vfifo, (mipi: one CRC error at release) 0x3018 b2 mipi 0x3017 b3 blc, b2 isp, empty frames: a frame sync error and a 0x3018 b1 ac, 0-byte payload per frame at the receiver, 0x301b b3 src, b0 sync sometimes a few CRC errors at release aec, otp, asram, bist, nothing visible
grp, mipisc, the debug
bits, 0x3019 at 0x00
and at 0xff
whole value (6 runs each) effect timing gate alone (0x10, 0x00) all four: identical to the driver's every gate set (6x 0xf0) own values in every run, same fps, documented gates set, debug lit/dark means, black level at gain clear (b0 d0 70 30 b0 e0) 16 and 1023, bars, no gaps, errors
reset values (0xf0, 0x301c 0xf2) or kernel messages
every bit clear no frames at any rate; 0x3016 bit 4
cleared alone does it, frames stop cleanly and resume when it is set
cold start, rebuilt driver effect this value at the end of the all identical to the runs above: init table or right after the registers read back as written, reset; 0xf0 at the end; lines same fps, lit/dark means, black removed (reset values) level, bars, no gaps, errors or
kernel messages
(The standby restart itself truncates one frame at the receiver at 60 and 90 fps, with or without new values.)
So a reset bit at 1 holds its block; a clock gate at 1 keeps its block clocked, and at 0 the chip clocks the image path anyway while streaming, except the timing control, whose gate stops the frames like its reset does. Every reset and the timing gate released cleanly, the receiver never needed a restart.
Thanks,
Antoine
PATCH v2 2/9 media: i2c: ov7251: Fix link frequency index
In reply to Antoine Pichotov7251_check_hwcfg() stores i, the index into the firmware's link_frequencies array, in link_freq_idx, which the driver then uses to index its own link_freq[], pixel_rates[] and pll1[] tables. The matching driver entry is j. Store j.
Observed on a Surface Book 2, where ipu-bridge lists one link frequency, 319.2 MHz, which is link_freq[1]: V4L2_CID_LINK_FREQ reads 240000000, and the pixel rate and PLL registers are off the same way.
The 319.2 MHz table this selects ran its PLL outside the datasheet range until the previous patch, so do not apply this fix without it.
--- drivers/media/i2c/ov7251.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/ov7251.c +1 -1
@@ -1523,7 +1523,7 @@ static int ov7251_check_hwcfg(struct ov7251 *ov7251) goto out_free_bus_cfg; } - ov7251->link_freq_idx = i; + ov7251->link_freq_idx = j; out_free_bus_cfg:v4l2_fwnode_endpoint_free (&bus_cfg);
Signature · 1 line
2.56.0
PATCH v2 3/9 media: i2c: ov7251: Report the pixel array rate for both link frequencies
In reply to Antoine Pichotpixel_rates[] gives V4L2_CID_PIXEL_RATE 48 MHz for the 240 MHz link and 63.84 MHz for the 319.2 MHz link, which is the CSI-2 bus rate of each link (one lane, 10 bits per pixel). The control is the pixel array rate, and Documentation/driver-api/media/tx-rx.rst says the bus rate "is not the same thing as the pixel rate on the camera sensor's pixel array which is indicated by the V4L2_CID_PIXEL_RATE control".
The pixel array runs from PLL2's 48 MHz system clock at both link
frequencies: the PLL2 registers are the same in both tables, and the measured frame rate is 48000000 / (928 x VTS) with either link. On a Surface Book 2 at 319.2 MHz, VTS 1724 gives 30.0 fps; 63.84 MHz would give 39.9 fps. A Surface Pro 7+ measures 30.01, 60.16 and 90.45 fps at VTS 1724, 860 and 572, the same 48 MHz. Dave Stevenson saw the same on a Raspberry Pi with a 24 MHz clock, where libcamera asked for 100 fps and got 75, the ratio of the two rates.
Report 48 MHz for both link frequencies and drop the table.
--- drivers/media/i2c/ov7251.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/media/i2c/ov7251.c +6 -9
@@ -22,6 +22,9 @@#include <media/v4l2-fwnode.h> #include <media/v4l2-subdev.h> +/* PLL2 sys_clk: the pixel array clock at both link frequencies */ +#define OV7251_PIXEL_CLOCK 48000000 +#define OV7251_SC_MODE_SELECT 0x0100#define OV7251_SC_MODE_SELECT_SW_STANDBY 0x0#define OV7251_SC_MODE_SELECT_STREAMING 0x1
@@ -633,11 +636,6 @@ static const s64 link_freq[] = { [OV7251_LINK_FREQ_319_2_MHZ] = 319200000, }; -static const s64 pixel_rates[] = { - [OV7251_LINK_FREQ_240_MHZ ] =48000000 , - [OV7251_LINK_FREQ_319_2_MHZ ] =63840000 , -}; -static const struct ov7251_mode_info ov7251_mode_info_data[] = { { .width =640 ,
@@ -1567,7 +1565,6 @@ static int ov7251_detect_chip(struct ov7251 *ov7251)static int ov7251_init_ctrls (struct ov7251 *ov7251) {int vblank_max, vblank_def; -s64 pixel_rate;int hblank;v4l2_ctrl_handler_init (&ov7251->ctrls,7 );
@@ -1586,12 +1583,12 @@ static int ov7251_init_ctrls(struct ov7251 *ov7251)ARRAY_SIZE (ov7251_test_pattern_menu) -1 , 0, 0, ov7251_test_pattern_menu); - pixel_rate = pixel_rates[ov7251->link_freq_idx]; ov7251->pixel_clock =v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops,V4L2_CID_PIXEL_RATE , - pixel_rate,INT_MAX , - pixel_rate, pixel_rate); +OV7251_PIXEL_CLOCK ,INT_MAX , +OV7251_PIXEL_CLOCK , +OV7251_PIXEL_CLOCK ); ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls, &ov7251_ctrl_ops, V4L2_CID_LINK_FREQ,
Signature · 1 line
2.56.0
Re: [PATCH v2 3/9] media: i2c: ov7251: Report the pixel array rate for both link frequencies
Quoted 17 lines · Antoine Pichot
pixel_rates[] gives
V4L2_CID_PIXEL_RATE48 MHz for the 240 MHz link and 63.84 MHz for the 319.2 MHz link, which is the CSI-2 bus rate of each link (one lane, 10 bits per pixel). The control is the pixel array rate, andDocumentation/driver-api/media/tx-rx.rstsays the bus rate "is not the same thing as the pixel rate on the camera sensor's pixel array which is indicated by theV4L2_CID_PIXEL_RATEcontrol".The pixel array runs from PLL2's 48 MHz system clock at both link
frequencies: the PLL2 registers are the same in both tables, and the measured frame rate is 48000000 / (928 x VTS) with either link. On a Surface Book 2 at 319.2 MHz, VTS 1724 gives 30.0 fps; 63.84 MHz would give 39.9 fps. A Surface Pro 7+ measures 30.01, 60.16 and 90.45 fps at VTS 1724, 860 and 572, the same 48 MHz. Dave Stevenson saw the same on a Raspberry Pi with a 24 MHz clock, where libcamera asked for 100 fps and got 75, the ratio of the two rates.Report 48 MHz for both link frequencies and drop the table.
...
Quoted 3 lines
Fixes ed9566ce1946 ("media: i2c: Add support for new frequencies to ov7251")
Same comment about Link tag.
...
drivers/media/i2c/ov7251.c line 26
Quoted 2 lines
drivers/media/i2c/ov7251.c +2
-0+/* PLL2 sys_clk: the pixel array clock at both link frequencies */ +#define OV7251_PIXEL_CLOCK 48000000
Perhaps
#define OV7251_PIXEL_CLOCK_Hz (48 * HZ_PER_MHZ)
(will require units.h to be included)?
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 4/9 media: i2c: ov7251: Reset vblank when the frame interval changes
In reply to Antoine PichotSetting a 60 or 90 fps frame interval is accepted and read back, but the sensor keeps streaming at 30 fps.
The three modes differ only in their frame length (VTS: 1724, 860 and 572 lines), which each mode's register table writes to 0x380e/0x380f. Since the V4L2_CID_VBLANK control was added, s_stream() applies the controls with __v4l2_ctrl_handler_setup() after the mode table, so the VBLANK control rewrites VTS. ov7251_set_frame_interval() switches current_mode but never updates the VBLANK control, which keeps the 30 fps value (1244, VTS 1724) and overrides the 60 or 90 fps VTS at every stream start.
ov7251_set_format() has the same issue in the other direction: it updates the VBLANK range and default for the new mode, but __v4l2_ctrl_modify_range() keeps the current value when it is still in range, so after a 60 fps frame interval a format change would go back to the 30 fps mode with a 60 fps VTS.
Update the VBLANK range and default in ov7251_set_frame_interval() too, and set VBLANK to the mode's default in both places, as is already done for exposure and gain. The VBLANK handler then also restores the exposure range for the new frame length.
Tested on a Microsoft Surface Book 2 (IPU3 CIO2): after v4l2-ctl --set-subdev-fps pad=0,fps=60 (90), v4l2-ctl measures 60.15 (90.45) fps instead of 30.01, with vblank 380 (92).
--- drivers/media/i2c/ov7251.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/media/i2c/ov7251.c +18 -0
@@ -1259,6 +1259,10 @@ static int ov7251_set_format(struct v4l2_subdev *sd,if (ret <0 ) goto exit; + ret =__v4l2_ctrl_s_ctrl (ov7251->vblank, vblank_def); +if (ret <0 ) + goto exit; + ov7251->current_mode = new_mode; }
@@ -1412,6 +1416,7 @@ static int ov7251_set_frame_interval(struct v4l2_subdev *subdev, {struct ov7251 *ov7251 =to_ov7251 (subdev);const struct ov7251_mode_info *new_mode; +int vblank_def;int ret =0 ;/*
@@ -1440,6 +1445,19 @@ static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,if (ret <0 ) goto exit; + vblank_def = new_mode->vts - new_mode->height; + ret =__v4l2_ctrl_modify_range (ov7251->vblank, +OV7251_TIMING_MIN_VTS , +OV7251_TIMING_MAX_VTS - + new_mode->height, +1 , vblank_def); +if (ret <0 ) + goto exit; + + ret =__v4l2_ctrl_s_ctrl (ov7251->vblank, vblank_def); +if (ret <0 ) + goto exit; + ov7251->current_mode = new_mode; }
Signature · 1 line
2.56.0
PATCH v2 5/9 media: i2c: ov7251: Use sub-device active state
In reply to Antoine PichotPort the ov7251 driver to the sub-device active state.
The driver kept its own copies of the active format and crop rectangle and selected between them and the try state with a pair of helpers switching on the format's "which" field. Move the format and the crop rectangle to the sub-device state for both the try and the active case, drop the helpers and the private copies, and use the v4l2_subdev_get_fmt helper for get_fmt. The init_state operation now fills whichever state it is given through set_fmt, so the explicit ov7251_init_state() call at the end of probe goes away; the exposure control gets the default mode's range at creation instead of being adjusted by that call.
The driver mutex went away with it. The control handler's own lock now serves as the state lock as well, so the control framework and the sub-device core serialise the pad operations, the control operations and the stream start between them; s_stream takes the active state lock itself. The mode pointer, which the frame interval operations still select by frame rate, stays and is protected by the same lock.
Behaviour is unchanged for applications. As a side effect the core now reports V4L2_SUBDEV_FL_HAS_EVENTS for the sub-device and handles control event subscription, so v4l2-compliance's VIDIOC_(UN)SUBSCRIBE_EVENT test passes without the driver having to declare the event operations.
--- drivers/media/i2c/ov7251.c | 154 +++++++++++-------------------------- 1 file changed, 43 insertions(+), 111 deletions(-)
drivers/media/i2c/ov7251.c +43 -111
@@ -130,8 +130,6 @@ struct ov7251 {struct v4l2_subdev sd;struct media_pad pad;struct v4l2_fwnode_endpoint ep; -struct v4l2_mbus_framefmt fmt; -struct v4l2_rect crop;struct clk *xclk;u32 xclk_freq;
@@ -157,7 +155,6 @@ struct ov7251 {u8 timing_format1;u8 timing_format2; -struct mutex lock;/* lock to protect power state, ctrls and mode */ bool power_on;struct gpio_desc *enable_gpio;
@@ -1041,8 +1038,6 @@ static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl) exposure_max)); } -/* v4l2_ctrl_lock() locks our mutex */ -if (!pm_runtime_get_if_in_use (ov7251->dev))return 0 ;
@@ -1130,52 +1125,6 @@ static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,return -EINVAL ; } -static struct v4l2_mbus_framefmt * -__ov7251_get_pad_format (struct ov7251 *ov7251, -struct v4l2_subdev_state *sd_state, -unsigned int pad, -enum v4l2_subdev_format_whence which) -{ -switch (which) { -case V4L2_SUBDEV_FORMAT_TRY : -return v4l2_subdev_state_get_format (sd_state, pad); -case V4L2_SUBDEV_FORMAT_ACTIVE : -return &ov7251->fmt; -default : -return NULL ; - } -} - -static int ov7251_get_format (struct v4l2_subdev *sd, -struct v4l2_subdev_state *sd_state, -struct v4l2_subdev_format *format) -{ -struct ov7251 *ov7251 =to_ov7251 (sd); - -mutex_lock (&ov7251->lock); - format->format = *__ov7251_get_pad_format (ov7251, sd_state, - format->pad, - format->which); -mutex_unlock (&ov7251->lock); - -return 0 ; -} - -static struct v4l2_rect * -__ov7251_get_pad_crop (struct ov7251 *ov7251, -struct v4l2_subdev_state *sd_state, -unsigned int pad,enum v4l2_subdev_format_whence which) -{ -switch (which) { -case V4L2_SUBDEV_FORMAT_TRY : -return v4l2_subdev_state_get_crop (sd_state, pad); -case V4L2_SUBDEV_FORMAT_ACTIVE : -return &ov7251->crop; -default : -return NULL ; - } -} -static inline u32 avg_fps (const struct v4l2_fract *t) {return (t->denominator + (t->numerator >>1 )) / t->numerator;
@@ -1220,12 +1169,9 @@ static int ov7251_set_format(struct v4l2_subdev *sd,int vblank_max, vblank_def;struct v4l2_rect *__crop;const struct ov7251_mode_info *new_mode; -int ret =0 ; - -mutex_lock (&ov7251->lock); +int ret; - __crop =__ov7251_get_pad_crop (ov7251, sd_state, format->pad, - format->which); + __crop =v4l2_subdev_state_get_crop (sd_state, format->pad); new_mode = v4l2_find_nearest_size(ov7251_mode_info_data, ARRAY_SIZE(ov7251_mode_info_data),
@@ -1240,16 +1186,16 @@ static int ov7251_set_format(struct v4l2_subdev *sd,1 , new_mode->exposure_max, 1, new_mode->exposure_def);if (ret <0 ) - goto exit; +return ret; ret =__v4l2_ctrl_s_ctrl (ov7251->exposure, new_mode->exposure_def);if (ret <0 ) - goto exit; +return ret; ret =__v4l2_ctrl_s_ctrl (ov7251->gain,16 );if (ret <0 ) - goto exit; +return ret; vblank_max =OV7251_TIMING_MAX_VTS - new_mode->height; vblank_def = new_mode->vts - new_mode->height;
@@ -1257,17 +1203,16 @@ static int ov7251_set_format(struct v4l2_subdev *sd,OV7251_TIMING_MIN_VTS , vblank_max, 1, vblank_def);if (ret <0 ) - goto exit; +return ret; ret =__v4l2_ctrl_s_ctrl (ov7251->vblank, vblank_def);if (ret <0 ) - goto exit; +return ret; ov7251->current_mode = new_mode; } - __format =__ov7251_get_pad_format (ov7251, sd_state, format->pad, - format->which); + __format =v4l2_subdev_state_get_format (sd_state, format->pad); __format->width = __crop->width; __format->height = __crop->height; __format->code =MEDIA_BUS_FMT_Y10_1X10 ;
@@ -1280,27 +1225,21 @@ static int ov7251_set_format(struct v4l2_subdev *sd, format->format = *__format; -exit: -mutex_unlock (&ov7251->lock); - -return ret; +return 0 ; }static int ov7251_init_state (struct v4l2_subdev *subdev,struct v4l2_subdev_state *sd_state) {struct v4l2_subdev_format fmt = { - .which = sd_state ?V4L2_SUBDEV_FORMAT_TRY - :V4L2_SUBDEV_FORMAT_ACTIVE , + .which =V4L2_SUBDEV_FORMAT_TRY , .format = { .width =640 , .height =480 } }; -ov7251_set_format (subdev,NULL , sd_state, &fmt); - -return 0 ; +return ov7251_set_format (subdev,NULL , sd_state, &fmt); } static int ov7251_get_selection(struct v4l2_subdev *sd,
@@ -1308,15 +1247,10 @@ static int ov7251_get_selection(struct v4l2_subdev *sd, struct v4l2_subdev_state *sd_state, struct v4l2_subdev_selection *sel) { - struct ov7251 *ov7251 = to_ov7251(sd); -switch (sel->target) {case V4L2_SEL_TGT_CROP_DEFAULT :case V4L2_SEL_TGT_CROP : -mutex_lock (&ov7251->lock); - sel->r = *__ov7251_get_pad_crop (ov7251, sd_state, sel->pad, - sel->which); -mutex_unlock (&ov7251->lock); + sel->r = *v4l2_subdev_state_get_crop (sd_state, sel->pad);break ;case V4L2_SEL_TGT_NATIVE_SIZE : sel->r.top =0 ;
@@ -1340,14 +1274,15 @@ static int ov7251_get_selection(struct v4l2_subdev *sd,static int ov7251_s_stream (struct v4l2_subdev *subdev,int enable) {struct ov7251 *ov7251 =to_ov7251 (subdev); +struct v4l2_subdev_state *state;int ret; -mutex_lock (&ov7251->lock); + state =v4l2_subdev_lock_and_get_active_state (subdev);if (enable) { ret =pm_runtime_resume_and_get (ov7251->dev);if (ret) { -mutex_unlock (&ov7251->lock); +v4l2_subdev_unlock_state (state);return ret; }
@@ -1381,12 +1316,12 @@ static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)pm_runtime_put (ov7251->dev); } -mutex_unlock (&ov7251->lock); +v4l2_subdev_unlock_state (state);return ret; err_power_down:pm_runtime_put (ov7251->dev); -mutex_unlock (&ov7251->lock); +v4l2_subdev_unlock_state (state);return ret; }
@@ -1403,9 +1338,7 @@ static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,if (fi->which !=V4L2_SUBDEV_FORMAT_ACTIVE )return -EINVAL ; -mutex_lock (&ov7251->lock); fi->interval = ov7251->current_mode->timeperframe; -mutex_unlock (&ov7251->lock);return 0 ; }
@@ -1417,7 +1350,7 @@ static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,struct ov7251 *ov7251 =to_ov7251 (subdev);const struct ov7251_mode_info *new_mode;int vblank_def; -int ret =0 ; +int ret;/* * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
@@ -1426,7 +1359,6 @@ static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,if (fi->which !=V4L2_SUBDEV_FORMAT_ACTIVE )return -EINVAL ; -mutex_lock (&ov7251->lock); new_mode =ov7251_find_mode_by_ival (ov7251, &fi->interval);if (new_mode != ov7251->current_mode) {
@@ -1434,16 +1366,16 @@ static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,1 , new_mode->exposure_max, 1, new_mode->exposure_def);if (ret <0 ) - goto exit; +return ret; ret =__v4l2_ctrl_s_ctrl (ov7251->exposure, new_mode->exposure_def);if (ret <0 ) - goto exit; +return ret; ret =__v4l2_ctrl_s_ctrl (ov7251->gain,16 );if (ret <0 ) - goto exit; +return ret; vblank_def = new_mode->vts - new_mode->height; ret = __v4l2_ctrl_modify_range(ov7251->vblank,
@@ -1452,21 +1384,18 @@ static int ov7251_set_frame_interval(struct v4l2_subdev *subdev, new_mode->height, 1, vblank_def);if (ret <0 ) - goto exit; +return ret; ret =__v4l2_ctrl_s_ctrl (ov7251->vblank, vblank_def);if (ret <0 ) - goto exit; +return ret; ov7251->current_mode = new_mode; } fi->interval = ov7251->current_mode->timeperframe; -exit: -mutex_unlock (&ov7251->lock); - -return ret; +return 0 ; }static const struct v4l2_subdev_video_ops ov7251_video_ops = {
@@ -1477,7 +1406,7 @@ static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = { .enum_mbus_code = ov7251_enum_mbus_code, .enum_frame_size = ov7251_enum_frame_size, .enum_frame_interval = ov7251_enum_frame_ival, - .get_fmt = ov7251_get_format, + .get_fmt = v4l2_subdev_get_fmt, .set_fmt = ov7251_set_format, .get_selection = ov7251_get_selection, .get_frame_interval = ov7251_get_frame_interval,
@@ -1582,18 +1511,20 @@ static int ov7251_detect_chip(struct ov7251 *ov7251)static int ov7251_init_ctrls (struct ov7251 *ov7251) { +const struct ov7251_mode_info *mode = ov7251->current_mode;int vblank_max, vblank_def;int hblank;v4l2_ctrl_handler_init (&ov7251->ctrls,7 ); - ov7251->ctrls.lock = &ov7251->lock;v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops,V4L2_CID_HFLIP ,0 ,1 ,1 ,0 );v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops,V4L2_CID_VFLIP ,0 ,1 ,1 ,0 ); ov7251->exposure =v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops, -V4L2_CID_EXPOSURE ,1 ,32 ,1 ,32 ); +V4L2_CID_EXPOSURE ,1 , + mode->exposure_max,1 , + mode->exposure_def); ov7251->gain =v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops,V4L2_CID_GAIN ,16 ,1023 ,1 ,16 ); v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
@@ -1703,14 +1634,10 @@ static int ov7251_probe(struct i2c_client *client)return PTR_ERR (ov7251->enable_gpio); } -mutex_init (&ov7251->lock); - ov7251->current_mode = &ov7251_mode_info_data[0 ]; ret =ov7251_init_ctrls (ov7251); -if (ret) { -dev_err_probe (dev, ret,"error during v4l2 ctrl init\n" ); - goto destroy_mutex; - } +if (ret) +return dev_err_probe (dev, ret,"error during v4l2 ctrl init\n" );v4l2_i2c_subdev_init (&ov7251->sd, client, &ov7251_subdev_ops); ov7251->sd.internal_ops = &ov7251_internal_ops;
@@ -1725,9 +1652,16 @@ static int ov7251_probe(struct i2c_client *client) goto free_ctrl; } + ov7251->sd.state_lock = ov7251->ctrls.lock; + ret =v4l2_subdev_init_finalize (&ov7251->sd); +if (ret <0 ) { +dev_err (dev,"could not initialize subdev state\n" ); + goto free_entity; + } + ret =ov7251_set_power_on (ov7251->dev);if (ret) - goto free_entity; + goto subdev_cleanup; ret =ov7251_detect_chip (ov7251);if (ret)
@@ -1768,11 +1702,9 @@ static int ov7251_probe(struct i2c_client *client) ret =v4l2_async_register_subdev (&ov7251->sd);if (ret <0 ) {dev_err (dev,"could not register v4l2 device\n" ); - goto free_entity; + goto subdev_cleanup; } -ov7251_init_state (&ov7251->sd,NULL ); -return 0 ; err_pm_runtime:
@@ -1780,12 +1712,12 @@ static int ov7251_probe(struct i2c_client *client)pm_runtime_put_noidle (ov7251->dev); power_down:ov7251_set_power_off (ov7251->dev); +subdev_cleanup: +v4l2_subdev_cleanup (&ov7251->sd); free_entity:media_entity_cleanup (&ov7251->sd.entity); free_ctrl:v4l2_ctrl_handler_free (&ov7251->ctrls); -destroy_mutex: -mutex_destroy (&ov7251->lock);return ret; }
@@ -1796,9 +1728,9 @@ static void ov7251_remove(struct i2c_client *client)struct ov7251 *ov7251 =to_ov7251 (sd);v4l2_async_unregister_subdev (&ov7251->sd); +v4l2_subdev_cleanup (sd);media_entity_cleanup (&ov7251->sd.entity);v4l2_ctrl_handler_free (&ov7251->ctrls); -mutex_destroy (&ov7251->lock);pm_runtime_disable (ov7251->dev);if (!pm_runtime_status_suspended (ov7251->dev))
Signature · 1 line
2.56.0
Re: [PATCH v2 5/9] media: i2c: ov7251: Use sub-device active state
Quoted 23 lines · Antoine Pichot
Port the ov7251 driver to the sub-device active state.
The driver kept its own copies of the active format and crop rectangle and selected between them and the try state with a pair of helpers switching on the format's "which" field. Move the format and the crop rectangle to the sub-device state for both the try and the active case, drop the helpers and the private copies, and use the
v4l2_subdev_get_fmthelper forget_fmt. Theinit_stateoperation now fills whichever state it is given throughset_fmt, so the explicitov7251_init_state()call at the end of probe goes away; the exposure control gets the default mode's range at creation instead of being adjusted by that call.The driver mutex went away with it. The control handler's own lock now serves as the state lock as well, so the control framework and the sub-device core serialise the pad operations, the control operations and the stream start between them;
s_streamtakes the active state lock itself. The mode pointer, which the frame interval operations still select by frame rate, stays and is protected by the same lock.Behaviour is unchanged for applications. As a side effect the core now reports
V4L2_SUBDEV_FL_HAS_EVENTSfor the sub-device and handles control event subscription, so v4l2-compliance's VIDIOC_(UN)SUBSCRIBE_EVENT test passes without the driver having to declare the event operations.
...
Quoted 13 lines
(quoted diff) +2
-2- mutex_lock(&ov7251->lock); + state = v4l2_subdev_lock_and_get_active_state(subdev); if (enable) { ret = pm_runtime_resume_and_get(ov7251->dev); if (ret) { - mutex_unlock(&ov7251->lock); + v4l2_subdev_unlock_state(state); return ret; } pm_runtime_put(ov7251->dev); }
Review
Quoted 10 lines
(quoted diff) +2
-2- mutex_unlock(&ov7251->lock); + v4l2_subdev_unlock_state(state); return ret; err_power_down: pm_runtime_put(ov7251->dev); - mutex_unlock(&ov7251->lock); + v4l2_subdev_unlock_state(state); return ret;
I'm wondering if v4l2 provides the set of conditional guards and hence some kind of ACQUIRE() macro.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 5/9] media: i2c: ov7251: Use sub-device active state
Hi Andy,
Quoted 2 lines · Andy Shevchenko
I'm wondering if v4l2 provides the set of conditional guards and hence some kind of
ACQUIRE()macro.
Not today. Nothing under include/media or drivers/media defines a guard class (no DEFINE_GUARD, DEFINE_CLASS, DEFINE_LOCK_GUARD or DEFINE_FREE), and the state lock is unconditional:v4l2_subdev_lock_state() is a plain mutex_lock(), so ACQUIRE_ERR() would have nothing to report.
A guard class could be added: Tomi proposed one in September 2024 [1]; Laurent preferred a __free() annotation and the series stalled. I would rather not add it in this series. The explicit pair is what imx290, imx415, ov2740 and ov8858 do in s_stream, and what Sakari asked to keep in the ov5647 conversion last December [2] until the driver moves to .enable_streams() and .disable_streams(), after which the core takes the state lock itself and the pairs go away.
[1] https://lore.kernel.org/linux-media/[email protected]/
[2] https://lore.kernel.org/linux-media/[email protected]/
Thanks,
Antoine
PATCH v2 6/9 media: i2c: ov7251: Add orientation and rotation controls
In reply to Antoine PichotParse the firmware device properties and register the V4L2_CID_CAMERA_ORIENTATION and V4L2_CID_CAMERA_SENSOR_ROTATION controls, as documented in Documentation/driver-api/media/camera-sensor.rst. libcamera uses them to place the camera and to correct the image orientation.
While at it, bump the control handler size hint to the number of controls the driver now creates.
On the Microsoft Surface Book 2 the ACPI _PLD (via ipu-bridge) reports the IR sensor as front facing with no rotation.
--- drivers/media/i2c/ov7251.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/media/i2c/ov7251.c +10 -1
@@ -1512,10 +1512,16 @@ static int ov7251_detect_chip(struct ov7251 *ov7251)static int ov7251_init_ctrls (struct ov7251 *ov7251) {const struct ov7251_mode_info *mode = ov7251->current_mode; +struct v4l2_fwnode_device_properties props;int vblank_max, vblank_def;int hblank; +int ret; -v4l2_ctrl_handler_init (&ov7251->ctrls,7 ); + ret =v4l2_fwnode_device_parse (ov7251->dev, &props); +if (ret) +return ret; + +v4l2_ctrl_handler_init (&ov7251->ctrls,11 );v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops,V4L2_CID_HFLIP ,0 ,1 ,1 ,0 );
@@ -1563,6 +1569,9 @@ static int ov7251_init_ctrls(struct ov7251 *ov7251)OV7251_TIMING_MIN_VTS , vblank_max,1 , vblank_def); +v4l2_ctrl_new_fwnode_properties (&ov7251->ctrls, &ov7251_ctrl_ops, + &props); + ov7251->sd.ctrl_handler = &ov7251->ctrls;if (ov7251->ctrls.error) {
Signature · 1 line
2.56.0
PATCH v2 7/9 media: i2c: ov7251: Move the strobe registers out of the mode tables
In reply to Antoine PichotThe three mode tables write the same values to the STROBE pad output enable bit (0x3005) and to the strobe control block (0x3b80-0x3b96). Move these writes to a separate register array written right after the mode table at stream start, and name the registers a following patch
needs: SC_REG5 (0x3005, whose bit 3 enables the STROBE pad output), the strobe frame pattern (0x3b81) and the 32-bit strobe frame span (0x3b8c).
The values written are unchanged, only the order of the writes within the stream start sequence changes. No functional change intended.
--- drivers/media/i2c/ov7251.c | 93 ++++++++++++++------------------------ 1 file changed, 33 insertions(+), 60 deletions(-)
drivers/media/i2c/ov7251.c +33 -60
@@ -55,6 +55,9 @@#define OV7251_PLL2_DIVIDER_REG 0x309d#define OV7251_PLL2_SYS_DIV_REG 0x309a#define OV7251_PLL2_ADC_DIV_REG 0x309b +#define OV7251_SC_REG5 0x3005 +#define OV7251_STROBE_FRAME_PATTERN 0x3b81 +#define OV7251_STROBE_FRAME_SPAN 0x3b8c#define OV7251_NATIVE_WIDTH 656#define OV7251_NATIVE_HEIGHT 496
@@ -239,8 +242,31 @@ static const struct reg_value ov7251_global_init_setting[] = { { 0x303b, 0x02 }, }; +/* Strobe output settings, the same for all modes */ +static const struct reg_value ov7251_strobe_regs[] = { + {OV7251_SC_REG5 ,0x00 }, + {0x3b80 ,0x00 }, + {OV7251_STROBE_FRAME_PATTERN ,0xa5 }, + {0x3b82 ,0x10 }, + {0x3b83 ,0x00 }, + {0x3b84 ,0x08 }, + {0x3b85 ,0x00 }, + {0x3b86 ,0x01 }, + {0x3b87 ,0x00 }, + {0x3b88 ,0x00 }, + {0x3b89 ,0x00 }, + {0x3b8a ,0x00 }, + {0x3b8b ,0x05 }, + {OV7251_STROBE_FRAME_SPAN ,0x00 }, + {0x3b8d ,0x00 }, + {0x3b8e ,0x00 }, + {0x3b8f ,0x1a }, + {0x3b94 ,0x05 }, + {0x3b95 ,0xf2 }, + {0x3b96 ,0x40 }, +}; +static const struct reg_value ov7251_setting_vga_30fps[] = { - {0x3005 ,0x00 }, {0x3012 ,0xc0 }, {0x3013 ,0xd2 }, { 0x3014, 0x04 },
@@ -313,25 +339,6 @@ static const struct reg_value ov7251_setting_vga_30fps[] = { { 0x3834, 0x00 }, { 0x3835, 0x0c }, { 0x3837, 0x00 }, - { 0x3b80, 0x00 }, - { 0x3b81, 0xa5 }, - { 0x3b82, 0x10 }, - { 0x3b83, 0x00 }, - { 0x3b84, 0x08 }, - { 0x3b85, 0x00 }, - { 0x3b86, 0x01 }, - { 0x3b87, 0x00 }, - { 0x3b88, 0x00 }, - { 0x3b89, 0x00 }, - { 0x3b8a, 0x00 }, - { 0x3b8b, 0x05 }, - { 0x3b8c, 0x00 }, - { 0x3b8d, 0x00 }, - { 0x3b8e, 0x00 }, - { 0x3b8f, 0x1a }, - { 0x3b94, 0x05 }, - { 0x3b95, 0xf2 }, - { 0x3b96, 0x40 }, { 0x3c00, 0x89 }, { 0x3c01, 0x63 }, { 0x3c02, 0x01 },
@@ -368,7 +375,6 @@ static const struct reg_value ov7251_setting_vga_30fps[] = { };static const struct reg_value ov7251_setting_vga_60fps[] = { - {0x3005 ,0x00 }, {0x3012 ,0xc0 }, {0x3013 ,0xd2 }, {0x3014 ,0x04 },
@@ -441,25 +447,6 @@ static const struct reg_value ov7251_setting_vga_60fps[] = { { 0x3834, 0x00 }, { 0x3835, 0x0c }, { 0x3837, 0x00 }, - { 0x3b80, 0x00 }, - { 0x3b81, 0xa5 }, - { 0x3b82, 0x10 }, - { 0x3b83, 0x00 }, - { 0x3b84, 0x08 }, - { 0x3b85, 0x00 }, - { 0x3b86, 0x01 }, - { 0x3b87, 0x00 }, - { 0x3b88, 0x00 }, - { 0x3b89, 0x00 }, - { 0x3b8a, 0x00 }, - { 0x3b8b, 0x05 }, - { 0x3b8c, 0x00 }, - { 0x3b8d, 0x00 }, - { 0x3b8e, 0x00 }, - { 0x3b8f, 0x1a }, - { 0x3b94, 0x05 }, - { 0x3b95, 0xf2 }, - { 0x3b96, 0x40 }, { 0x3c00, 0x89 }, { 0x3c01, 0x63 }, { 0x3c02, 0x01 },
@@ -496,7 +483,6 @@ static const struct reg_value ov7251_setting_vga_60fps[] = { };static const struct reg_value ov7251_setting_vga_90fps[] = { - {0x3005 ,0x00 }, {0x3012 ,0xc0 }, {0x3013 ,0xd2 }, {0x3014 ,0x04 },
@@ -569,25 +555,6 @@ static const struct reg_value ov7251_setting_vga_90fps[] = { { 0x3834, 0x00 }, { 0x3835, 0x0c }, { 0x3837, 0x00 }, - { 0x3b80, 0x00 }, - { 0x3b81, 0xa5 }, - { 0x3b82, 0x10 }, - { 0x3b83, 0x00 }, - { 0x3b84, 0x08 }, - { 0x3b85, 0x00 }, - { 0x3b86, 0x01 }, - { 0x3b87, 0x00 }, - { 0x3b88, 0x00 }, - { 0x3b89, 0x00 }, - { 0x3b8a, 0x00 }, - { 0x3b8b, 0x05 }, - { 0x3b8c, 0x00 }, - { 0x3b8d, 0x00 }, - { 0x3b8e, 0x00 }, - { 0x3b8f, 0x1a }, - { 0x3b94, 0x05 }, - { 0x3b95, 0xf2 }, - { 0x3b96, 0x40 }, { 0x3c00, 0x89 }, { 0x3c01, 0x63 }, { 0x3c02, 0x01 },
@@ -1301,6 +1268,12 @@ static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable) ov7251->current_mode->height); goto err_power_down; } + ret =ov7251_set_register_array (ov7251, ov7251_strobe_regs, +ARRAY_SIZE (ov7251_strobe_regs)); +if (ret <0 ) { +dev_err (ov7251->dev,"could not set strobe defaults\n" ); + goto err_power_down; + } ret =__v4l2_ctrl_handler_setup (&ov7251->ctrls);if (ret <0 ) {dev_err (ov7251->dev,"could not sync v4l2 controls\n" );
Signature · 1 line
2.56.0
PATCH v2 8/9 media: i2c: ov7251: Add strobe output enable control
In reply to Antoine PichotThe OV7251 has a STROBE output that pulses during the exposure of selected frames, meant to drive an illuminator. On the Microsoft Surface Book 2 the front OV7251 is an infrared camera (Windows Hello) and its 850 nm emitter is wired to that pin: ACPI describes no separate LED, GPIO line or regulator for it. The mode tables leave the pad disabled, so the camera only sees an unlit scene.
Add a V4L2_CID_FLASH_STROBE_OE control, off by default. When enabled it sets the STROBE pad output enable (SC_REG5 bit 3), strobes alternate frames (frame pattern 0xaa instead of the default 0xa5, which lights four frames in eight) and sets the
pulse length (strobe frame span, in lines) to the exposure time, clamped between 52 lines and 45% of the frame length, never more than 776 lines. When disabled it
restores the defaults the mode tables used to write. The span follows exposure and vertical blanking changes while the strobe is enabled.
The limits come from the Windows driver, which clamps the span to 52 to 776 lines: 776 lines is 45% of the 1724-line frame of the 30 fps mode. Expressing the upper limit as a duty cycle keeps the emitter within that envelope (eye safety) at 60 and 90 fps too, where a fixed 776 lines would let the pulse cover up to 96% of the frame (552 of 572 lines). Keeping the 776-line limit as well bounds the pulse when vertical blanking stretches the frame, up to 65535 lines.
Alternate frames is what the Windows driver uses for face recognition on this hardware: its 30 and 60 fps modes write 0xaa, and a register dump taken during a Windows Hello sign-in on a Surface Book 2 shows the same. Lighting every frame (0xff, which two of its other modes use) would double the average emitter power. User space tells lit frames from dark ones by their brightness; V4L2 has no per-frame illumination flag yet.
The control is written only while the sensor is powered, and from __v4l2_ctrl_handler_setup() at stream start, after the mode and strobe register tables. Setting it while the sensor is idle does not power it up, and the emitter goes off with the sensor when streaming stops.
On a Surface Book 2 at analogue gain 128 the mean pixel value of the infrared image of a room is 19 unlit; with the strobe enabled the lit frames read 496 at exposure 504 (span 504) and 587 at exposure 1704 (span 776), the dark frames in between 18 to 20.
The span read back while streaming is 258 at 90 fps, 45% of the 572-line frame.
--- drivers/media/i2c/ov7251.c | 85 ++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 9 deletions(-)
drivers/media/i2c/ov7251.c +76 -9
@@ -18,6 +18,8 @@#include <linux/regulator/consumer.h> #include <linux/slab.h> #include <linux/types.h> +#include <linux/unaligned.h> +#include <linux/units.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-fwnode.h> #include <media/v4l2-subdev.h>
@@ -56,8 +58,15 @@#define OV7251_PLL2_SYS_DIV_REG 0x309a#define OV7251_PLL2_ADC_DIV_REG 0x309b#define OV7251_SC_REG5 0x3005 +#define OV7251_SC_REG5_STROBE_OE BIT(3)#define OV7251_STROBE_FRAME_PATTERN 0x3b81 +#define OV7251_STROBE_FRAME_PATTERN_ALT 0xaa/* alternate frames */ +#define OV7251_STROBE_FRAME_PATTERN_DEF 0xa5#define OV7251_STROBE_FRAME_SPAN 0x3b8c +#define OV7251_STROBE_FRAME_SPAN_DEF 0x1a +#define OV7251_STROBE_SPAN_MIN 52 +#define OV7251_STROBE_SPAN_MAX 776 +#define OV7251_STROBE_MAX_DUTY_PERCENT 45#define OV7251_NATIVE_WIDTH 656#define OV7251_NATIVE_HEIGHT 496
@@ -151,6 +160,7 @@ struct ov7251 {struct v4l2_ctrl *gain;struct v4l2_ctrl *hblank;struct v4l2_ctrl *vblank; +struct v4l2_ctrl *strobe;/* Cached register values */ u8 aec_pk_manual;
@@ -242,11 +252,12 @@ static const struct reg_value ov7251_global_init_setting[] = { { 0x303b, 0x02 }, }; -/* Strobe output settings, the same for all modes */ +/* +* Strobe output settings, the same for all modes. The STROBE_OE control +* writes SC_REG5, the frame pattern and the frame span. +*/ static const struct reg_value ov7251_strobe_regs[] = { - {OV7251_SC_REG5 ,0x00 }, {0x3b80 ,0x00 }, - {OV7251_STROBE_FRAME_PATTERN ,0xa5 }, {0x3b82 ,0x10 }, {0x3b83 ,0x00 }, { 0x3b84, 0x08 },
@@ -257,10 +268,6 @@ static const struct reg_value ov7251_strobe_regs[] = { { 0x3b89, 0x00 }, { 0x3b8a, 0x00 }, { 0x3b8b, 0x05 }, - { OV7251_STROBE_FRAME_SPAN, 0x00 }, - { 0x3b8d, 0x00 }, - { 0x3b8e, 0x00 }, - { 0x3b8f, 0x1a }, { 0x3b94, 0x05 }, { 0x3b95, 0xf2 }, { 0x3b96, 0x40 },
@@ -718,7 +725,7 @@ static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val)static int ov7251_write_seq_regs (struct ov7251 *ov7251,u16 reg,u8 *val,u8 num) { -u8 regbuf[5 ]; +u8 regbuf[6 ];u8 nregbuf =sizeof (reg) + num *sizeof (*val);int ret =0 ;
@@ -970,6 +977,55 @@ static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)return ret; } +static int ov7251_set_strobe_span (struct ov7251 *ov7251) +{ +int span =OV7251_STROBE_FRAME_SPAN_DEF ; +int vts, max_span; +u8 val[4 ]; + +/* +* The pulse starts with the integration: make it cover the exposure, +* from 52 lines up to 776 lines and never more than 45% of the frame. +* The vendor driver caps it at 776 lines, 45% of the 1724 lines 30 fps +* frame; the duty cycle limit keeps the emitter within that envelope +* at higher frame rates, the absolute one with long vertical blanking. +*/ +if (ov7251->strobe->val) { + vts = ov7251->current_mode->height + ov7251->vblank->val; + max_span =DIV_ROUND_UP (vts *OV7251_STROBE_MAX_DUTY_PERCENT , +PERCENT ); + span =min (clamp (ov7251->exposure->val,OV7251_STROBE_SPAN_MIN , +OV7251_STROBE_SPAN_MAX ), max_span); + } + +put_unaligned_be32 (span, val); + +return ov7251_write_seq_regs (ov7251,OV7251_STROBE_FRAME_SPAN , val, +sizeof (val)); +} + +static int ov7251_set_strobe (struct ov7251 *ov7251,s32 value) +{ +int ret; + +/* +* The other SC_REG5 bits enable other pads, which the mode tables +* have always left disabled (0x00). +*/ + ret =ov7251_write_reg (ov7251,OV7251_SC_REG5 , + value ?OV7251_SC_REG5_STROBE_OE :0 ); +if (ret) +return ret; + + ret =ov7251_write_reg (ov7251,OV7251_STROBE_FRAME_PATTERN , + value ?OV7251_STROBE_FRAME_PATTERN_ALT : +OV7251_STROBE_FRAME_PATTERN_DEF ); +if (ret) +return ret; + +return ov7251_set_strobe_span (ov7251); +} +static const char *const ov7251_test_pattern_menu[] = {"Disabled" ,"Vertical Pattern Bars" ,
@@ -1011,6 +1067,8 @@ static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)switch (ctrl->id) {case V4L2_CID_EXPOSURE : ret =ov7251_set_exposure (ov7251, ctrl->val); +if (!ret && ov7251->strobe->val) + ret =ov7251_set_strobe_span (ov7251);break ;case V4L2_CID_GAIN : ret =ov7251_set_gain (ov7251, ctrl->val);
@@ -1026,6 +1084,11 @@ static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)break ;case V4L2_CID_VBLANK : ret =ov7251_vts_configure (ov7251, ctrl->val); +if (!ret && ov7251->strobe->val) + ret =ov7251_set_strobe_span (ov7251); +break ; +case V4L2_CID_FLASH_STROBE_OE : + ret =ov7251_set_strobe (ov7251, ctrl->val);break ;default : ret = -EINVAL ;
@@ -1494,7 +1557,7 @@ static int ov7251_init_ctrls(struct ov7251 *ov7251)if (ret)return ret; -v4l2_ctrl_handler_init (&ov7251->ctrls,11 ); +v4l2_ctrl_handler_init (&ov7251->ctrls,12 );v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops,V4L2_CID_HFLIP ,0 ,1 ,1 ,0 );
@@ -1542,6 +1605,10 @@ static int ov7251_init_ctrls(struct ov7251 *ov7251)OV7251_TIMING_MIN_VTS , vblank_max,1 , vblank_def); + ov7251->strobe =v4l2_ctrl_new_std (&ov7251->ctrls, &ov7251_ctrl_ops, +V4L2_CID_FLASH_STROBE_OE , +0 ,1 ,1 ,0 ); +v4l2_ctrl_new_fwnode_properties (&ov7251->ctrls, &ov7251_ctrl_ops, &props);
Signature · 1 line
2.56.0
Re: [PATCH v2 8/9] media: i2c: ov7251: Add strobe output enable control
Hi,
Quoted 7 lines · Antoine Pichot
Alternate frames is what the Windows driver uses for face recognition on this hardware: its 30 and 60 fps modes write
0xaa, and a register dump taken during a Windows Hello sign-in on a Surface Book 2 shows the same. Lighting every frame (0xff, which two of its other modes use) would double the average emitter power. User space tells lit frames from dark ones by their brightness; V4L2 has no per-frame illumination flag yet.
Before I send v3, could you please say which of these two you prefer?
V4L2_CID_FLASH_STROBE_OE is a boolean: it turns the strobe pin on or off. Two sensor settings then shape the signal: how long the pulse lasts within a frame (the span, which this driver ties to the exposure), and which frames get a pulse at all (the pattern, one bit per frame over eight frames). V4L2 has no control for the pattern. ov9282 writes 0xff in its common register table, a pulse on every frame; this patch writes 0xaa, a pulse on alternate frames. Both chips default to 0xa5, four frames in eight, so a driver has to pick one.
- when the control is on, pulse alternate frames, as in this patch;
- when the control is on, pulse every frame, like ov9282, and add a menu control "Strobe Frame Pattern" with "Every frame" and "Alternate frames", after the infrared torch mode of the Windows camera API (off, always on, alternating frame illumination).
Either way the pattern is one register write.
Thanks,
Antoine
PATCH v2 9/9 media: ipu-bridge: Add a rotation quirk for the Surface Book 2 and Pro 7+
In reply to Antoine PichotThe infrared camera of the Microsoft Surface Book 2, an OmniVision OV7251 with ACPI ID INT347E, is mounted rotated by 90 degrees. An upright scene appears rotated 90 degrees clockwise in its frames, with the top of the scene at the right edge of the image, while a frame taken at the same moment by the colour camera next to it shows the scene upright. The degree field of the sensor's SSDB buffer in the ACPI tables is 0, and so is the rotation field of its _PLD, so ipu_bridge_parse_rotation() sets the rotation property to 0 and userspace has no way to know that the image needs correcting.
The firmware is wrong here, and ipu-bridge is where the firmware data is turned into the rotation property: it already carries DMI (Desktop Management Interface) quirks for sensors mounted upside down without the firmware saying so. Add a second table for sensors rotated by 90 degrees, checked the same way, with entries for the Surface Book 2 and Surface Pro 7+ infrared sensors. Following V4L2_CID_CAMERA_SENSOR_ROTATION, 90 is the counter-clockwise rotation to apply to correct the image. The Book 2 entry matches on the SKU of the 13 inch model, the one checked; the 15 inch model has its own SKU and has not been checked. The colour sensors of this machine (INT33BE and INT347A) already report 180 degrees and are unchanged.
D. Manresa measured the same rotation on a Surface Pro 7+ (IPU6), whose infrared sensor is also an INT347E: an upright scene comes out rotated 90 degrees clockwise, with both flip controls at 0 and the firmware reporting a rotation of 0, so the Pro 7+ gets an entry too.
Tested on a 13 inch Surface Book 2 (SKU Surface_Book_1832) with a 7.2.12 kernel: V4L2_CID_CAMERA_SENSOR_ROTATION on the OV7251 reads 90 instead of 0, the colour sensors still read 180 and the orientation controls are unchanged.
--- drivers/media/pci/intel/ipu-bridge.c | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
drivers/media/pci/intel/ipu-bridge.c +31 -0
@@ -281,6 +281,32 @@ static const struct dmi_system_id upside_down_sensor_dmi_ids[] = { {}/* Terminating entry */ }; +/* +* DMI matches for laptops which have their sensor mounted rotated by 90 degrees +* without reporting it in the SSDB. +*/ +static const struct dmi_system_id rotated_90_sensor_dmi_ids[] = { + { +/* Microsoft Surface Book 2 13 inch, infrared sensor */ + .matches = { +DMI_EXACT_MATCH (DMI_SYS_VENDOR , +"Microsoft Corporation" ), +DMI_EXACT_MATCH (DMI_PRODUCT_SKU ,"Surface_Book_1832" ), + }, + .driver_data ="INT347E" , + }, + { +/* Microsoft Surface Pro 7+, infrared sensor */ + .matches = { +DMI_EXACT_MATCH (DMI_SYS_VENDOR , +"Microsoft Corporation" ), +DMI_EXACT_MATCH (DMI_PRODUCT_SKU ,"Surface_Pro_7+_1960" ), + }, + .driver_data ="INT347E" , + }, + {}/* Terminating entry */ +}; +static const struct ipu_property_names prop_names = { .clock_frequency ="clock-frequency" , .rotation ="rotation" ,
@@ -474,6 +500,11 @@ static u32 ipu_bridge_parse_rotation(struct acpi_device *adev,if (acpi_dev_hid_match (adev, dmi_id->driver_data))return 180 ; +for (dmi_id =dmi_first_match (rotated_90_sensor_dmi_ids); dmi_id; + dmi_id =dmi_first_match (dmi_id +1 )) +if (acpi_dev_hid_match (adev, dmi_id->driver_data)) +return 90 ; +switch (ssdb->degree) {case IPU_SENSOR_ROTATION_NORMAL :return 0 ;
Signature · 1 line
2.56.0
Re: [PATCH v2 9/9] media: ipu-bridge: Add a rotation quirk for the Surface Book 2 and Pro 7+
Quoted 27 lines · Antoine Pichot
The infrared camera of the Microsoft Surface Book 2, an OmniVision OV7251 with ACPI ID INT347E, is mounted rotated by 90 degrees. An upright scene appears rotated 90 degrees clockwise in its frames, with the top of the scene at the right edge of the image, while a frame taken at the same moment by the colour camera next to it shows the scene upright. The degree field of the sensor's SSDB buffer in the ACPI tables is 0, and so is the rotation field of its
_PLD, soipu_bridge_parse_rotation()sets the rotation property to 0 and userspace has no way to know that the image needs correcting.The firmware is wrong here, and ipu-bridge is where the firmware data is turned into the rotation property: it already carries DMI (Desktop Management Interface) quirks for sensors mounted upside down without the firmware saying so. Add a second table for sensors rotated by 90 degrees, checked the same way, with entries for the Surface Book 2 and Surface Pro 7+ infrared sensors. Following
V4L2_CID_CAMERA_SENSOR_ROTATION, 90 is the counter-clockwise rotation to apply to correct the image. The Book 2 entry matches on the SKU of the 13 inch model, the one checked; the 15 inch model has its own SKU and has not been checked. The colour sensors of this machine (INT33BE and INT347A) already report 180 degrees and are unchanged.D. Manresa measured the same rotation on a Surface Pro 7+ (IPU6), whose infrared sensor is also an INT347E: an upright scene comes out rotated 90 degrees clockwise, with both flip controls at 0 and the firmware reporting a rotation of 0, so the Pro 7+ gets an entry too.
Quoted 6 lines
Tested on a 13 inch Surface Book 2 (SKU
Surface_Book_1832) with a 7.2.12 kernel:V4L2_CID_CAMERA_SENSOR_ROTATIONon the OV7251 reads 90 instead of 0, the colour sensors still read 180 and the orientation controls are unchanged.
Add a reference here
and use [1] in the text above.
Signature · 2 lines
With Best Regards, Andy Shevchenko