From ce38843fbd0028960601e60dd9f90d507d5e9312 Mon Sep 17 00:00:00 2001 From: robertijtsma <37256770+robertijtsma@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:49:50 +0100 Subject: [PATCH] media: i2c: ov7251: add SC8180X MIPI clock gate, autosuspend, and Surface Pro X IR strobe Enable reliable streaming and automatic 850 nm Windows Hello IR illumination for the front OV7251 IR camera on Microsoft Surface Pro X (SC8180X / SQ2): - Use pm_runtime_mark_last_busy() + pm_runtime_put_autosuspend() in ov7251_s_ctrl() and ov7251_s_stream() (matching the autosuspend timer already configured in probe) and add a 5 ms post-init settle delay to prevent reset glitches across back-to-back streams. - Program MIPI_CTRL00 (0x4800 = 0x24) on SC8180X for clean LP->HS transitions on CSIPHY3 without affecting non-SC8180X platforms. - Drive the sensor's hardware STROBE pad (0x3b81, 0x3b8e..0x3b8f, 0x3027, 0x3005) while streaming on microsoft,surface-pro-x (or when enabled via ovti,ir-strobe / ir_strobe=1), and turn it off on stream stop so the 850 nm IR illuminator activates only while streaming. Tested on Microsoft Surface Pro X SQ2 (SC8180X, Linux 7.3.1-1-surface) with OpenCV and Howdy IR face authentication. Signed-off-by: robertijtsma <37256770+robertijtsma@users.noreply.github.com> --- drivers/media/i2c/ov7251.c | 77 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/drivers/media/i2c/ov7251.c b/drivers/media/i2c/ov7251.c index 311c61d9e25d9..2ce62d9efe15a 100644 --- a/drivers/media/i2c/ov7251.c +++ b/drivers/media/i2c/ov7251.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,40 @@ #define OV7251_SC_MODE_SELECT_SW_STANDBY 0x0 #define OV7251_SC_MODE_SELECT_STREAMING 0x1 +#define OV7251_SC_IO_CTRL1 0x3005 +#define OV7251_IO_CTRL1_STROBE_EN BIT(3) +#define OV7251_SC_GP_IO_OUT1 0x3009 +#define OV7251_SC_GP_IO_SEL1 0x3027 +#define OV7251_STROBE_PATTERN 0x3b81 +#define OV7251_STROBE_SPAN_H 0x3b8e +#define OV7251_STROBE_SPAN_L 0x3b8f +#define OV7251_MIPI_CTRL00 0x4800 +#define OV7251_MIPI_CTRL00_LP11_GATE 0x24 + +static int ir_strobe = -1; +module_param(ir_strobe, int, 0644); +MODULE_PARM_DESC(ir_strobe, + "Drive hardware IR strobe pin during streaming (-1 auto, 0 off, 1 on)"); + +static int strobe_pattern = 0xff; +module_param(strobe_pattern, int, 0644); +MODULE_PARM_DESC(strobe_pattern, + "OV7251 reg 0x3b81 frame pattern mask (default 0xff)"); + +static int strobe_span = 0x0200; +module_param(strobe_span, int, 0644); +MODULE_PARM_DESC(strobe_span, + "OV7251 reg 0x3b8e..0x3b8f strobe frame span (default 0x0200)"); + +static bool ov7251_use_ir_strobe(struct device *dev) +{ + if (ir_strobe >= 0) + return ir_strobe > 0; + + return device_property_read_bool(dev, "ovti,ir-strobe") || + of_machine_is_compatible("microsoft,surface-pro-x"); +} + #define OV7251_CHIP_ID_HIGH 0x300a #define OV7251_CHIP_ID_HIGH_BYTE 0x77 #define OV7251_CHIP_ID_LOW 0x300b @@ -941,6 +976,8 @@ static int ov7251_set_power_on(struct device *dev) return ret; } + usleep_range(5000, 10000); + return ret; } @@ -1072,7 +1109,8 @@ static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl) break; } - pm_runtime_put(ov7251->dev); + pm_runtime_mark_last_busy(ov7251->dev); + pm_runtime_put_autosuspend(ov7251->dev); return ret; } @@ -1367,21 +1405,54 @@ static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable) dev_err(ov7251->dev, "could not sync v4l2 controls\n"); goto err_power_down; } + + if (of_machine_is_compatible("qcom,sc8180x")) { + ret = ov7251_write_reg(ov7251, OV7251_MIPI_CTRL00, + OV7251_MIPI_CTRL00_LP11_GATE); + if (ret) { + dev_err(ov7251->dev, + "could not set MIPI_CTRL00: %d\n", ret); + goto err_power_down; + } + } + + if (ov7251_use_ir_strobe(ov7251->dev)) { + if (strobe_pattern >= 0) + ov7251_write_reg(ov7251, OV7251_STROBE_PATTERN, + strobe_pattern & 0xff); + if (strobe_span >= 0) { + ov7251_write_reg(ov7251, OV7251_STROBE_SPAN_H, + (strobe_span >> 8) & 0xff); + ov7251_write_reg(ov7251, OV7251_STROBE_SPAN_L, + strobe_span & 0xff); + } + ov7251_write_reg(ov7251, OV7251_SC_GP_IO_SEL1, 0x00); + ov7251_write_reg(ov7251, OV7251_SC_IO_CTRL1, + OV7251_IO_CTRL1_STROBE_EN); + } + ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT, OV7251_SC_MODE_SELECT_STREAMING); if (ret) goto err_power_down; } else { + if (ov7251_use_ir_strobe(ov7251->dev)) { + ov7251_write_reg(ov7251, OV7251_SC_IO_CTRL1, 0x00); + ov7251_write_reg(ov7251, OV7251_SC_GP_IO_SEL1, 0x00); + ov7251_write_reg(ov7251, OV7251_SC_GP_IO_OUT1, 0x00); + } ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT, OV7251_SC_MODE_SELECT_SW_STANDBY); - pm_runtime_put(ov7251->dev); + pm_runtime_mark_last_busy(ov7251->dev); + pm_runtime_put_autosuspend(ov7251->dev); } mutex_unlock(&ov7251->lock); return ret; err_power_down: - pm_runtime_put(ov7251->dev); + pm_runtime_mark_last_busy(ov7251->dev); + pm_runtime_put_autosuspend(ov7251->dev); mutex_unlock(&ov7251->lock); return ret; }