Devicetree
 help / color / mirror / Atom feed
* [PATCH v4 0/2] media: i2c: Add Samsung S5K5E8 camera sensor driver
@ 2026-12-01 11:02 Tuan Kiet Huynh
  2026-12-01 11:02 ` [PATCH v4 1/2] dt-bindings: media: i2c: Add Samsung S5K5E8 image sensor Tuan Kiet Huynh
  2026-12-01 11:02 ` [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
  0 siblings, 2 replies; 4+ messages in thread
From: Tuan Kiet Huynh @ 2026-12-01 11:02 UTC (permalink / raw)
  To: linux-media, devicetree
  Cc: sakari.ailus, mchehab, robh, krzk+dt, conor+dt, chernyav.a,
	hikmahgumelar, Tuan Kiet Huynh

This is v4 of the Samsung S5K5E8 sensor driver, previously posted as
[PATCH v3 0/2] on 2026-11-22:
Link: https://lore.kernel.org/all/20261122034656.1093369-1-huynhtuankiet33@gmail.com/
v1 and v2 were posted on 2026-11-21:
Link: https://lore.kernel.org/all/20261121091907.1190346-1-huynhtuankiet33@gmail.com/
Link: https://lore.kernel.org/all/20261121120731.2028810-1-huynhtuankiet33@gmail.com/
(v1 had no cover letter; the first link is to its 1/2 patch.)

A note on the competing series first, because it matters more than
the changelog: Hikmah Gumelar posted a separate samsung,s5k5e8 series
for the Xiaomi Mi A1 (tissot) a day later, on 2026-11-23, and it is now
at v5 ([PATCH v5 0/7], 2026-11-27). Both series claim the same
compatible ("samsung,s5k5e8"), the same binding file, the same driver
file and the same Kconfig symbol, and neither series mentions the
other. I only became aware of the overlap when a reviewer pointed me
at it, and I would rather coordinate than race: I am happy to rebase
this series onto a single shared driver, or to carry Hikmah's driver
forward with the pieces below merged in -- whichever base the
maintainers prefer. For the record, where the two stand:

- This series is ahead on: HFLIP/VFLIP controls, a 1304x980 59.81 fps
  binned mode, a tested device-tree node for the Redmi Note 4X (mido)
  in the local downstream tree, an in-flight libcamera sensor
  properties and helper series on libcamera-devel (v2 sent
  2026-11-29, v3 to follow), hardware verification on mido, and
  test-pattern colour controls (registers 0x0602-0x0608, verified on
  mido). It also already uses the V4L2 CCI helpers, the active subdev
  state with the state lock as the control-handler lock,
  enable_streams/disable_streams with the s_stream helper, and
  v4l2_link_freq_to_bitmap() -- all things the Nov 27 reviews of
  Hikmah's v5 asked Hikmah to adopt (Sakari Ailus on the state lock,
  enable_streams and the link-frequency bitmap; Vladimir Zapolskiy on
  CCI).
- Hikmah's series is ahead on: human review (v5 was reviewed on
  Nov 27 by Sakari Ailus, Vladimir Zapolskiy and Bryan O'Donoghue,
  and Krzysztof Kozlowski reviewed the cover and DT binding on
  Dec 1), and a device-tree node plus rear-camera (OV12A10) and
  clock-fix support for tissot. Hikmah has also had the camera path
  running on 7.1.3, and on Nov 28 reported a physical Mi A1 booting
  7.4.0-rc1 and validating the camera pipeline (CAMSS, OV12A10,
  S5K5E8) on the device; no camera results have been reported yet.
  Hikmah is splitting the series and addressing the review feedback
  before v6.

One more incompatibility between the series, in the binding itself:
"the same binding file" above is true only as a filename. Hikmah's
binding (v5 2/7) requires avdd/dvdd/dovdd supplies and defines
reset-gpios as its only GPIO, while this series requires vdda/vddd/vddio
and adds optional powerdown-gpios, so no single device-tree node can
satisfy both bindings at once. This series follows the supply naming of
the upstream Samsung bindings (samsung,s5kjn1.yaml and
samsung,s5k3m5.yaml both use vdda/vddd/vddio), but that is a
precedent, not a ruling: if the two series are merged, the
maintainer will have to pick one naming, and I am happy to rename
to whichever is chosen.

One difference between the series I want to flag rather than silently
adopt: Hikmah's driver declares a 418 MHz link frequency. The mode
tables program 0x0820/0x0821 = 0x0344, which we read as 836 Mbps/lane,
i.e. 418 MHz -- but Hikmah never states that derivation, so it is our
inference, not his claim. I tried that value and measured the result on
mido hardware: with the PLL/timing tables unchanged, the sensor streams
at 29.89 fps at 2592x1944 and ~59.8 fps at 1304x980, which proves a
168.0 Mpix/s VT pixel clock -- but that is all it proves. The D-PHY
lane clock was not measured, and 168 Mpix/s does not by itself settle
the lane rate either way: the pixel data of a 2592-pixel RAW10 line is
transmitted only during the active 15.43 us of the 17.0 us line time, so
the peak lane rate is 12,960 bits/lane / 15.43 us = 840 Mbps/lane, i.e.
420 MHz -- exactly the driver's pixel-rate formula (420 MHz x 2 lanes
x 2 bits / 10). Spreading the same bits over the whole 17.0 us line
time would need only 762.4 Mbps/lane, i.e. 381.2 MHz, so that is a
lower bound on the lane rate, not the rate itself. Note
V4L2_CID_PIXEL_RATE is derived from LINK_FREQ, so the 0.48% figure
below is the error 418 MHz would introduce against the measured
168.0 Mpix/s VT clock, not an independent measurement. This version
keeps 420 MHz because it avoids a 0.48% error in V4L2_CID_PIXEL_RATE
(168.0 vs 167.2 Mpix/s at 418 MHz), not because the lane rate was
measured. The meaning of 0x0820/0x0821 = 0x0344 is evidently not
settled by that register alone; I would welcome a datasheet reference
either way.

On the driver name: v3 renamed s5k5e8yx to s5k5e8. The sensor reports
chip ID 0x5e80 and I have found no evidence of a distinct YX variant,
so the plain S5K5E8 name is used throughout.

Changes in v4 (all in 2/2; 1/2 is unchanged from v3):
- Add test pattern mode 4, PN9. Verified on the Redmi Note 4X: mode 4
  captures a deterministic pseudo-random 10-bit frame (mean 512.5,
  std 295.0, mid-row lag-1 autocorrelation 0.004 at 1304x980,
  whole-frame -0.006, two frames byte-identical), distinct from
  modes 0-3, which were re-checked and still work.
  v4l2-compliance still passes 46/46. (Hikmah's v5 already has PN9
  at index 4, so this is parity between the series, not an advantage.)
- Use the 16-bit test-pattern register 0x0600 instead of the
  8-bit 0x0601, and add test-pattern colour controls for registers
  0x0602-0x0608. Verified on mido: writing 0x03ff to 0x0602 with the
  others at 0x0000 turned the solid-colour frame red, and the inverse
  turned it green/blue; defaults are 0x200, the sensor reset value, so
  reset behaviour is unchanged.
- Fix the pm_runtime handling in set_ctrl: proceed unless
  pm_runtime_get_if_active() returns zero (device not active), and
  only drop the reference when one was actually taken.
- CROP_DEFAULT stays the constant modes[0] crop, (8,8)/2592x1944: it
  is the suggested crop covering the whole picture, not the crop of
  the currently selected mode.
- The extra stream-on sequence from the Hikmah driver (msleep 80 plus
  0x3c16/0x3c0d/0x0100/0x3c22 writes) was evaluated and is not adopted:
  the sensor streamed without it before, and hardware testing on mido
  after removing it (stream start/stop cycles at both modes, fps and
  test patterns re-checked) shows no regression, so v4 keeps the plain
  MODE_SELECT write.
- Two small cleanups: the s5k5e8_subdev_entity_ops struct (which only
  set link_validate) is removed, and S5K5E8_NUM_SUPPLIES is replaced
  by ARRAY_SIZE(s5k5e8_supply_names) at the use sites.
- Link frequency stays at 420 MHz, for the reason above.

Changes in v3:
- Renamed s5k5e8yx to s5k5e8 throughout (driver, binding, Kconfig
  symbol, compatible, MAINTAINERS); renamed standby-gpios to
  powerdown-gpios; sequential regulator enable (vddio -> vdda ->
  vddd) with powerdown-then-reset deassertion.
- Adopted full V4L2 subdev active state; RUNTIME_PM_OPS() with
  pm_ptr(); HFLIP/VFLIP handling with active-pad Bayer update;
  aligned the binned mode's MIPI link PLL registers (0x0306, 0x0308,
  0x0820/21 = 0x0344) with the Samsung OEM binned preview.
- Binding now requires vdda/vddd/vddio supplies and endpoint
  data-lanes; binding maintainer added.

Changes in v2:
- Rebased onto linux-media/media-committers next; updated .set_fmt
  and .get_selection for const struct v4l2_subdev_client_info *ci;
  converted PM ops to DEFINE_RUNTIME_DEV_PM_OPS() with pm_ptr();
  checkpatch --strict fixes; DT binding MAINTAINERS entry moved
  into 1/2.

One open question from downstream review, for the maintainers: a
reviewer of the downstream mido branch mentioned a work-in-progress
generic CCS driver covering "almost all s5kxxx sensors" and I asked
for a link to it, which was never supplied. I could not locate a CCS
driver covering the S5K5E8 (the mainline CCS driver lists no Samsung
sensor). Dedicated Samsung sensor drivers (s5kjn1, s5k3m5) have been
accepted upstream, so I proceeded with a dedicated driver, but please
say if CCS coverage is expected for this sensor instead.

Cc: Hikmah Gumelar <hikmahgumelar@gmail.com>

Andrii Cherniavskyi (2):
  dt-bindings: media: i2c: Add Samsung S5K5E8 image sensor
  media: i2c: Add Samsung S5K5E8 camera sensor driver

 .../bindings/media/i2c/samsung,s5k5e8.yaml    |  109 ++
 MAINTAINERS                                   |    9 +
 drivers/media/i2c/Kconfig                     |   10 +
 drivers/media/i2c/Makefile                    |    1 +
 drivers/media/i2c/s5k5e8.c                    | 1108 ++++++++++++++++++++
 5 files changed, 1237 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml
 create mode 100644 drivers/media/i2c/s5k5e8.c


base-commit: ccf78740d
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/2] dt-bindings: media: i2c: Add Samsung S5K5E8 image sensor
  2026-12-01 11:02 [PATCH v4 0/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
@ 2026-12-01 11:02 ` Tuan Kiet Huynh
  2026-12-01 11:02 ` [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
  1 sibling, 0 replies; 4+ messages in thread
From: Tuan Kiet Huynh @ 2026-12-01 11:02 UTC (permalink / raw)
  To: linux-media, devicetree
  Cc: sakari.ailus, mchehab, robh, krzk+dt, conor+dt, chernyav.a,
	hikmahgumelar, Tuan Kiet Huynh

From: Andrii Cherniavskyi <chernyav.a@gmail.com>

Add Device Tree bindings for the Samsung S5K5E8 5MP RAW10 CMOS image
sensor, including required regulators, optional reset/powerdown GPIOs,
and 2-lane MIPI CSI-2 endpoint properties.

Signed-off-by: Andrii Cherniavskyi <chernyav.a@gmail.com>
Co-developed-by: Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
Signed-off-by: Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
---
 .../bindings/media/i2c/samsung,s5k5e8.yaml    | 109 ++++++++++++++++++
 MAINTAINERS                                   |   8 ++
 2 files changed, 117 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml

diff --git a/Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml b/Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml
new file mode 100644
index 000000000..3aa50bd58
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml
@@ -0,0 +1,109 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/i2c/samsung,s5k5e8.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Samsung S5K5E8 Image Sensor
+
+description:
+  Samsung S5K5E8 is a 5MP CMOS image sensor controlled over an I2C-compatible
+  serial interface, outputting RAW10 image data over a 2-lane MIPI D-PHY
+  interface.
+
+maintainers:
+  - Andrii Cherniavskyi <chernyav.a@gmail.com>
+  - Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
+
+allOf:
+  - $ref: /schemas/media/video-interface-devices.yaml#
+
+properties:
+  compatible:
+    const: samsung,s5k5e8
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    description: MCLK supply clock.
+    maxItems: 1
+
+  reset-gpios:
+    description: Active low GPIO connected to RESET pad of the sensor.
+    maxItems: 1
+
+  powerdown-gpios:
+    description:
+      Active low GPIO connected to the XSHUTDN (powerdown) pad of the sensor.
+    maxItems: 1
+
+  vdda-supply:
+    description: Analogue voltage supply, 2.8 volts.
+
+  vddd-supply:
+    description: Digital core voltage supply, 1.2 volts.
+
+  vddio-supply:
+    description: Digital I/O voltage supply, 1.8 or 2.8 volts.
+
+  port:
+    $ref: /schemas/graph.yaml#/$defs/port-base
+    additionalProperties: false
+
+    properties:
+      endpoint:
+        $ref: /schemas/media/video-interfaces.yaml#
+        unevaluatedProperties: false
+
+        properties:
+          data-lanes:
+            items:
+              - const: 1
+              - const: 2
+
+        required:
+          - data-lanes
+          - link-frequencies
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - vdda-supply
+  - vddd-supply
+  - vddio-supply
+  - port
+
+unevaluatedProperties: false
+
+examples:
+  - |
+      #include <dt-bindings/gpio/gpio.h>
+
+      i2c {
+          #address-cells = <1>;
+          #size-cells = <0>;
+
+          camera@2d {
+              compatible = "samsung,s5k5e8";
+              reg = <0x2d>;
+              clocks = <&camera_mclk 0>;
+              assigned-clocks = <&camera_mclk 0>;
+              assigned-clock-rates = <24000000>;
+              reset-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
+              powerdown-gpios = <&gpio1 11 GPIO_ACTIVE_LOW>;
+              vdda-supply = <&vreg_2p8>;
+              vddd-supply = <&vreg_1p2>;
+              vddio-supply = <&vreg_1p8>;
+
+              port {
+                  endpoint {
+                      data-lanes = <1 2>;
+                      link-frequencies = /bits/ 64 <420000000>;
+                      remote-endpoint = <&mipi_csi2_ep>;
+                  };
+              };
+          };
+      };
+...
diff --git a/MAINTAINERS b/MAINTAINERS
index f72ce07a4..10b7bf57a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25013,6 +25013,14 @@ M:	Andrzej Hajda <andrzej.hajda@intel.com>
 S:	Supported
 F:	drivers/media/i2c/s5k5baf.c
 
+SAMSUNG S5K5E8 CAMERA DRIVER
+M:	Andrii Cherniavskyi <chernyav.a@gmail.com>
+M:	Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
+L:	linux-media@vger.kernel.org
+S:	Maintained
+T:	git git://linuxtv.org/media_tree.git
+F:	Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml
+
 SAMSUNG S5KJN1 CAMERA DRIVER
 M:	Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
 S:	Maintained

base-commit: ccf78740d63813c452c035b57d17a650492562fa
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver
  2026-12-01 11:02 [PATCH v4 0/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
  2026-12-01 11:02 ` [PATCH v4 1/2] dt-bindings: media: i2c: Add Samsung S5K5E8 image sensor Tuan Kiet Huynh
@ 2026-12-01 11:02 ` Tuan Kiet Huynh
  2026-12-01 11:16   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Tuan Kiet Huynh @ 2026-12-01 11:02 UTC (permalink / raw)
  To: linux-media, devicetree
  Cc: sakari.ailus, mchehab, robh, krzk+dt, conor+dt, chernyav.a,
	hikmahgumelar, Tuan Kiet Huynh

From: Andrii Cherniavskyi <chernyav.a@gmail.com>

Add a driver for the Samsung S5K5E8 5-megapixel image sensor, as
found in the front camera of the Xiaomi Redmi Note 4X (mido).

Two modes are supported: 2592x1944 at 29.89 fps and 2x2-binned
1304x980 at 59.81 fps, both verified on mido hardware. The driver
uses the CCI regmap, runtime PM with a 1000 ms autosuspend delay,
and the standard V4L2 control set (exposure, analogue gain, vblank,
hflip/vflip, test pattern).

Test patterns cover modes 0-4. Mode 4 is the sensor's PN9 pattern,
programmed through the 16-bit test-pattern register 0x0600. Verified on
mido: mode 4 captures a deterministic pseudo-random 10-bit frame (mean
512.5, std 295.0, mid-row lag-1 autocorrelation 0.004 and whole-frame
-0.006, both at 1304x980, two frames byte-identical), distinct from
modes 0-3, which were re-checked and still work. It is not claimed to be
a bit-exact textbook PN9 m-sequence: analysis of the captured bitstream
finds a degree-360 LFSR (40 interleaved primitive degree-9 sequences),
not the degree-9 x^9+x^4+1 sequence, so the pattern is described only as
the sensor's PN9 mode. The solid-colour test-pattern value is
programmable per Bayer channel through registers 0x0602 (red), 0x0604
(greenR), 0x0606 (blue) and 0x0608 (greenB), 10 bits each, exposed as
V4L2_CID_TEST_PATTERN_RED / GREENR / BLUE / GREENB defaulting to 0x200,
the sensor reset value. Verified on mido: writing 0x03ff to 0x0602 with
the others at 0x0000 turned the solid-colour frame red, and the inverse
turned it green/blue.

s_ctrl only writes the sensor when it is powered: it proceeds unless
pm_runtime_get_if_active() returns zero (device not active), and
drops the reference with pm_runtime_put_autosuspend() only when one
was actually taken, following the ov64a40 pattern. This keeps the
autosuspend delay honoured and avoids returning -EINVAL when
runtime PM is disabled, proceeding to access the device as ov64a40
does.

V4L2_SEL_TGT_CROP_DEFAULT returns the constant active pixel array,
(8,8)/2592x1944, in both modes -- the active array, while CROP_BOUNDS is
the native (0,0)/2608x1960 array, which it must be because the binned
mode's crop already covers the full array; the per-mode crop is reported
through V4L2_SEL_TGT_CROP instead. (imx219, imx258 and ov64a40 return
CROP_DEFAULT equal to CROP_BOUNDS, and s5kjn5 returns a single constant
rect for all selection targets; none of them returns a default rect
distinct from CROP_BOUNDS.)

Link frequency is 420 MHz. Only the frame rate, and therefore the
168 Mpix/s VT pixel clock (2856 * 1968 @ 29.89 fps, 2784 * 1009 @
59.81 fps), was measured on hardware; the D-PHY lane clock was not.
Keeping 420 MHz avoids a 0.48% error in V4L2_CID_PIXEL_RATE
(168.0 vs 167.2 Mpix/s at 418 MHz), and the 0x0820/0x0821 value of
0x0344 alone does not settle the lane rate either way.

Fixes from blind review of the v4 submission: drop the !ep -EINVAL
check in s5k5e8_check_hwcfg() so a missing endpoint defers probe
(v4l2_fwnode_endpoint_alloc_parse() already returns -EPROBE_DEFER);
name the standard SMIA/CCS registers in the mode tables and drop the
redundant VTS/exposure writes there; update the mbus code only after
the orientation write succeeds; pass NULL ops for the read-only
fwnode properties; configure autosuspend before registering the
sub-device; and roll back the pad format and control ranges if a
mode-switch control update fails.

Signed-off-by: Andrii Cherniavskyi <chernyav.a@gmail.com>
Co-developed-by: Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
Signed-off-by: Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
---
 MAINTAINERS                |    1 +
 drivers/media/i2c/Kconfig  |   10 +
 drivers/media/i2c/Makefile |    1 +
 drivers/media/i2c/s5k5e8.c | 1108 ++++++++++++++++++++++++++++++++++++
 4 files changed, 1120 insertions(+)
 create mode 100644 drivers/media/i2c/s5k5e8.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 10b7bf57a..d55b9b1bc 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25020,6 +25020,7 @@ L:	linux-media@vger.kernel.org
 S:	Maintained
 T:	git git://linuxtv.org/media_tree.git
 F:	Documentation/devicetree/bindings/media/i2c/samsung,s5k5e8.yaml
+F:	drivers/media/i2c/s5k5e8.c
 
 SAMSUNG S5KJN1 CAMERA DRIVER
 M:	Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>
diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 162b8ea02..e36e50d8d 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -885,6 +885,16 @@ config VIDEO_S5K5BAF
 	  This is a V4L2 sensor driver for Samsung S5K5BAF 2M
 	  camera sensor with an embedded SoC image signal processor.
 
+config VIDEO_S5K5E8
+	tristate "Samsung S5K5E8 sensor support"
+	select V4L2_CCI_I2C
+	help
+	  This is a V4L2 sensor driver for Samsung S5K5E8 5MP raw
+	  camera sensor.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called s5k5e8.
+
 config VIDEO_S5K6A3
 	tristate "Samsung S5K6A3 sensor support"
 	help
diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile
index a750942e9..decc5035f 100644
--- a/drivers/media/i2c/Makefile
+++ b/drivers/media/i2c/Makefile
@@ -137,6 +137,7 @@ obj-$(CONFIG_VIDEO_RJ54N1) += rj54n1cb0c.o
 obj-$(CONFIG_VIDEO_S5C73M3) += s5c73m3/
 obj-$(CONFIG_VIDEO_S5K3M5) += s5k3m5.o
 obj-$(CONFIG_VIDEO_S5K5BAF) += s5k5baf.o
+obj-$(CONFIG_VIDEO_S5K5E8) += s5k5e8.o
 obj-$(CONFIG_VIDEO_S5K6A3) += s5k6a3.o
 obj-$(CONFIG_VIDEO_S5KJN1) += s5kjn1.o
 obj-$(CONFIG_VIDEO_S5KJN5) += s5kjn5.o
diff --git a/drivers/media/i2c/s5k5e8.c b/drivers/media/i2c/s5k5e8.c
new file mode 100644
index 000000000..8a3d03424
--- /dev/null
+++ b/drivers/media/i2c/s5k5e8.c
@@ -0,0 +1,1108 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2026 Andrii Cherniavskyi <chernyav.a@gmail.com>
+// Copyright (c) 2026 Tuan Kiet Huynh <huynhtuankiet33@gmail.com>
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
+#include <linux/units.h>
+#include <media/v4l2-cci.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-fwnode.h>
+
+#define S5K5E8_LINK_FREQ_420MHZ		(420ULL * HZ_PER_MHZ)
+#define S5K5E8_MCLK_FREQ_24MHZ		(24 * HZ_PER_MHZ)
+#define S5K5E8_DATA_LANES		2
+#define S5K5E8_NATIVE_WIDTH		2608
+#define S5K5E8_NATIVE_HEIGHT		1960
+
+#define S5K5E8_REG_CHIP_ID		CCI_REG16(0x0000)
+#define S5K5E8_CHIP_ID			0x5e80
+
+#define S5K5E8_REG_CTRL_MODE		CCI_REG8(0x0100)
+#define S5K5E8_MODE_STREAMING		BIT(0)
+
+#define S5K5E8_REG_ORIENTATION		CCI_REG8(0x0101)
+#define S5K5E8_VFLIP			BIT(1)
+#define S5K5E8_HFLIP			BIT(0)
+
+#define S5K5E8_REG_EXPOSURE		CCI_REG16(0x0202)
+#define S5K5E8_EXPOSURE_MIN		8
+#define S5K5E8_EXPOSURE_STEP		1
+#define S5K5E8_EXPOSURE_MARGIN		4
+
+#define S5K5E8_REG_AGAIN		CCI_REG16(0x0204)
+#define S5K5E8_AGAIN_MIN		0x020
+#define S5K5E8_AGAIN_MAX		0x200
+#define S5K5E8_AGAIN_STEP		1
+#define S5K5E8_AGAIN_DEFAULT		0x020
+
+#define S5K5E8_REG_VTS			CCI_REG16(0x0340)
+#define S5K5E8_VTS_MAX			0xffff
+#define S5K5E8_REG_HTS			CCI_REG16(0x0342)
+#define S5K5E8_REG_X_ADDR_START		CCI_REG16(0x0344)
+#define S5K5E8_REG_Y_ADDR_START		CCI_REG16(0x0346)
+#define S5K5E8_REG_X_ADDR_END		CCI_REG16(0x0348)
+#define S5K5E8_REG_Y_ADDR_END		CCI_REG16(0x034a)
+#define S5K5E8_REG_X_OUTPUT_SIZE	CCI_REG16(0x034c)
+#define S5K5E8_REG_Y_OUTPUT_SIZE	CCI_REG16(0x034e)
+
+#define S5K5E8_REG_CSI_LANE_MODE		CCI_REG8(0x0114)
+#define S5K5E8_REG_EXTCLK_FREQ		CCI_REG16(0x0136)
+#define S5K5E8_REG_FINE_INTEGRATION_TIME	CCI_REG16(0x0200)
+#define S5K5E8_REG_PRE_PLL_CLK_DIV		CCI_REG16(0x0304)
+#define S5K5E8_REG_PLL_MULTIPLIER		CCI_REG16(0x0306)
+#define S5K5E8_REG_X_EVEN_INC		CCI_REG16(0x0380)
+#define S5K5E8_REG_X_ODD_INC		CCI_REG16(0x0382)
+#define S5K5E8_REG_Y_EVEN_INC		CCI_REG16(0x0384)
+#define S5K5E8_REG_Y_ODD_INC		CCI_REG16(0x0386)
+#define S5K5E8_REG_LINK_BIT_RATE		CCI_REG16(0x0820)
+#define S5K5E8_REG_BINNING_MODE		CCI_REG8(0x0900)
+#define S5K5E8_REG_BINNING_TYPE		CCI_REG8(0x0901)
+
+#define S5K5E8_REG_TEST_PATTERN		CCI_REG16(0x0600)
+#define S5K5E8_REG_TEST_PATTERN_RED		CCI_REG16(0x0602)
+#define S5K5E8_REG_TEST_PATTERN_GREENR	CCI_REG16(0x0604)
+#define S5K5E8_REG_TEST_PATTERN_BLUE		CCI_REG16(0x0606)
+#define S5K5E8_REG_TEST_PATTERN_GREENB	CCI_REG16(0x0608)
+#define S5K5E8_TEST_PATTERN_COLOUR_MIN		0
+#define S5K5E8_TEST_PATTERN_COLOUR_MAX		0x3ff
+#define S5K5E8_TEST_PATTERN_COLOUR_DEFAULT	0x200
+
+#define to_s5k5e8(_sd)			container_of(_sd, struct s5k5e8, sd)
+
+static const s64 s5k5e8_link_freq_menu[] = {
+	S5K5E8_LINK_FREQ_420MHZ,
+};
+
+/* List of supported formats to cover horizontal and vertical flip controls */
+static const u32 s5k5e8_mbus_formats[] = {
+	MEDIA_BUS_FMT_SGRBG10_1X10,	MEDIA_BUS_FMT_SRGGB10_1X10,
+	MEDIA_BUS_FMT_SBGGR10_1X10,	MEDIA_BUS_FMT_SGBRG10_1X10,
+};
+
+struct s5k5e8_reg_list {
+	const struct cci_reg_sequence *regs;
+	unsigned int num_regs;
+};
+
+struct s5k5e8_mode {
+	u32 width;				/* Frame width in pixels */
+	u32 height;				/* Frame height in pixels */
+	u32 hts;				/* Horizontal timing size */
+	u32 vts;				/* Default vertical timing size */
+	u32 exposure;				/* Default exposure value */
+	struct v4l2_rect crop;			/* Sensor array crop rect */
+	struct s5k5e8_reg_list reg_list;	/* Sensor register setting */
+};
+
+static const char *const s5k5e8_test_pattern_menu[] = {
+	"Disabled",
+	"Solid colour",
+	"Colour bars",
+	"Fade to grey colour bars",
+	"PN9",
+};
+
+static const char *const s5k5e8_supply_names[] = {
+	"vddio",	/* Digital I/O power */
+	"vdda",		/* Analog power */
+	"vddd",		/* Digital core power */
+};
+
+struct s5k5e8 {
+	struct device *dev;
+	struct regmap *regmap;
+	struct clk *mclk;
+	struct gpio_desc *reset_gpio;
+	struct gpio_desc *powerdown_gpio;
+	struct regulator_bulk_data supplies[ARRAY_SIZE(s5k5e8_supply_names)];
+
+	struct v4l2_subdev sd;
+	struct media_pad pad;
+
+	struct v4l2_ctrl_handler ctrl_handler;
+	struct v4l2_ctrl *hblank;
+	struct v4l2_ctrl *vblank;
+	struct v4l2_ctrl *exposure;
+	struct {
+		/* hflip/vflip cluster */
+		struct v4l2_ctrl *hflip;
+		struct v4l2_ctrl *vflip;
+	};
+};
+
+/*
+ * Vendor analog and timing initialization sequence (including repeated
+ * internal analog/timing writes required by the vendor power-up flow).
+ */
+static const struct cci_reg_sequence init_array_setting[] = {
+	{ CCI_REG8(0x0100), 0x00 },
+	{ CCI_REG8(0x3906), 0x7e },
+	{ CCI_REG8(0x3c01), 0x0f },
+	{ CCI_REG8(0x3c14), 0x00 },
+	{ CCI_REG8(0x3235), 0x08 },
+	{ CCI_REG8(0x3063), 0x2e },
+	{ CCI_REG8(0x307a), 0x10 },
+	{ CCI_REG8(0x307b), 0x0e },
+	{ CCI_REG8(0x3079), 0x20 },
+	{ CCI_REG8(0x3070), 0x05 },
+	{ CCI_REG8(0x3067), 0x06 },
+	{ CCI_REG8(0x3071), 0x62 },
+	{ CCI_REG8(0x3072), 0x16 },
+	{ CCI_REG8(0x3203), 0x43 },
+	{ CCI_REG8(0x3205), 0x43 },
+	{ CCI_REG8(0x320b), 0x42 },
+	{ CCI_REG8(0x3007), 0x00 },
+	{ CCI_REG8(0x3008), 0x14 },
+	{ CCI_REG8(0x3020), 0x58 },
+	{ CCI_REG8(0x300d), 0x34 },
+	{ CCI_REG8(0x300e), 0x17 },
+	{ CCI_REG8(0x3021), 0x02 },
+	{ CCI_REG8(0x3010), 0x59 },
+	{ CCI_REG8(0x3002), 0x01 },
+	{ CCI_REG8(0x3005), 0x01 },
+	{ CCI_REG8(0x3008), 0x04 },
+	{ CCI_REG8(0x300f), 0x70 },
+	{ CCI_REG8(0x3010), 0x69 },
+	{ CCI_REG8(0x3017), 0x10 },
+	{ CCI_REG8(0x3019), 0x19 },
+	{ CCI_REG8(0x300c), 0x62 },
+	{ CCI_REG8(0x3064), 0x10 },
+	{ CCI_REG8(0x3c08), 0x0e },
+	{ CCI_REG8(0x3c09), 0x10 },
+	{ CCI_REG8(0x3c31), 0x0d },
+	{ CCI_REG8(0x3c32), 0xac },
+};
+
+static const struct cci_reg_sequence s5k5e8_2592x1944_30fps_mode[] = {
+	{ S5K5E8_REG_EXTCLK_FREQ, 0x1800 },
+	{ S5K5E8_REG_PRE_PLL_CLK_DIV, 0x0006 },
+	{ S5K5E8_REG_PLL_MULTIPLIER, 0x18a8 },
+	{ CCI_REG8(0x0308), 0x34 },
+	{ CCI_REG8(0x0309), 0x42 },
+	{ CCI_REG8(0x3c1f), 0x00 },
+	{ CCI_REG8(0x3c17), 0x00 },
+	{ CCI_REG8(0x3c0b), 0x04 },
+	{ CCI_REG8(0x3c1c), 0x47 },
+	{ CCI_REG8(0x3c1d), 0x15 },
+	{ CCI_REG8(0x3c14), 0x04 },
+	{ CCI_REG8(0x3c16), 0x00 },
+	{ S5K5E8_REG_LINK_BIT_RATE, 0x0344 },
+	{ S5K5E8_REG_CSI_LANE_MODE, 0x01 },
+	{ S5K5E8_REG_X_ADDR_START,  0x0008 },
+	{ S5K5E8_REG_Y_ADDR_START,  0x0008 },
+	{ S5K5E8_REG_X_ADDR_END,    0x0a27 },
+	{ S5K5E8_REG_Y_ADDR_END,    0x079f },
+	{ S5K5E8_REG_X_OUTPUT_SIZE, 0x0a20 },
+	{ S5K5E8_REG_Y_OUTPUT_SIZE, 0x0798 },
+	{ S5K5E8_REG_BINNING_MODE, 0x00 },
+	{ S5K5E8_REG_BINNING_TYPE, 0x00 },
+	{ S5K5E8_REG_X_EVEN_INC, 0x0001 },
+	{ S5K5E8_REG_X_ODD_INC, 0x0001 },
+	{ S5K5E8_REG_Y_EVEN_INC, 0x0001 },
+	{ S5K5E8_REG_Y_ODD_INC, 0x0001 },
+	{ S5K5E8_REG_HTS, 0x0b28 },
+	{ S5K5E8_REG_FINE_INTEGRATION_TIME, 0x0000 },
+	{ CCI_REG8(0x3303), 0x02 },
+	{ CCI_REG8(0x3400), 0x00 },
+	{ CCI_REG8(0x323b), 0x02 },
+	{ CCI_REG8(0x3301), 0x00 },
+	{ CCI_REG8(0x3321), 0x04 },
+	{ CCI_REG8(0x3306), 0x00 },
+	{ CCI_REG8(0x3307), 0x08 },
+	{ CCI_REG8(0x3308), 0x0a },
+	{ CCI_REG8(0x3309), 0x27 },
+	{ CCI_REG8(0x330a), 0x01 },
+	{ CCI_REG8(0x330b), 0x01 },
+	{ CCI_REG8(0x330e), 0x00 },
+	{ CCI_REG8(0x330f), 0x08 },
+	{ CCI_REG8(0x3310), 0x07 },
+	{ CCI_REG8(0x3311), 0x9f },
+	{ CCI_REG8(0x3312), 0x01 },
+	{ CCI_REG8(0x3313), 0x01 },
+};
+
+static const struct cci_reg_sequence s5k5e8_1304x980_60fps_mode[] = {
+	{ S5K5E8_REG_EXTCLK_FREQ, 0x1800 },
+	{ S5K5E8_REG_PRE_PLL_CLK_DIV, 0x0006 },
+	{ S5K5E8_REG_PLL_MULTIPLIER, 0x18a8 },
+	{ CCI_REG8(0x0308), 0x34 },
+	{ CCI_REG8(0x0309), 0x42 },
+	{ CCI_REG8(0x3c1f), 0x00 },
+	{ CCI_REG8(0x3c17), 0x00 },
+	{ CCI_REG8(0x3c0b), 0x04 },
+	{ CCI_REG8(0x3c1c), 0x47 },
+	{ CCI_REG8(0x3c1d), 0x15 },
+	{ CCI_REG8(0x3c14), 0x04 },
+	{ CCI_REG8(0x3c16), 0x00 },
+	{ S5K5E8_REG_LINK_BIT_RATE, 0x0344 },
+	{ S5K5E8_REG_CSI_LANE_MODE, 0x01 },
+	{ S5K5E8_REG_X_ADDR_START,  0x0000 },
+	{ S5K5E8_REG_Y_ADDR_START,  0x0000 },
+	{ S5K5E8_REG_X_ADDR_END,    0x0a2f },
+	{ S5K5E8_REG_Y_ADDR_END,    0x07a7 },
+	{ S5K5E8_REG_X_OUTPUT_SIZE, 0x0518 },
+	{ S5K5E8_REG_Y_OUTPUT_SIZE, 0x03d4 },
+	{ S5K5E8_REG_BINNING_MODE, 0x01 },
+	{ S5K5E8_REG_BINNING_TYPE, 0x22 },
+	{ S5K5E8_REG_X_EVEN_INC, 0x0001 },
+	{ S5K5E8_REG_X_ODD_INC, 0x0001 },
+	{ S5K5E8_REG_Y_EVEN_INC, 0x0001 },
+	{ S5K5E8_REG_Y_ODD_INC, 0x0003 },
+	{ S5K5E8_REG_HTS, 0x0ae0 },
+	{ S5K5E8_REG_FINE_INTEGRATION_TIME, 0x0000 },
+	{ CCI_REG8(0x3303), 0x02 },
+	{ CCI_REG8(0x3400), 0x00 },
+	{ CCI_REG8(0x323b), 0x02 },
+	{ CCI_REG8(0x3301), 0x00 },
+	{ CCI_REG8(0x3321), 0x04 },
+	{ CCI_REG8(0x3306), 0x00 },
+	{ CCI_REG8(0x3307), 0x00 },
+	{ CCI_REG8(0x3308), 0x0a },
+	{ CCI_REG8(0x3309), 0x2f },
+	{ CCI_REG8(0x330a), 0x01 },
+	{ CCI_REG8(0x330b), 0x01 },
+	{ CCI_REG8(0x330e), 0x00 },
+	{ CCI_REG8(0x330f), 0x00 },
+	{ CCI_REG8(0x3310), 0x07 },
+	{ CCI_REG8(0x3311), 0xa7 },
+	{ CCI_REG8(0x3312), 0x03 },
+	{ CCI_REG8(0x3313), 0x01 },
+};
+
+/*
+ * Both modes share the same PLL setup (the binned mode uses the full-mode
+ * link setup matching Samsung OEM binned preview): 168 Mpix/s VT pixel array
+ * rate (2856 * 1968 @ 29.89 fps in full mode, 2784 * 1009 @ 59.81 fps in 2x2
+ * binned mode), measured on hardware, and 2-lane CSI-2 output at a 420 MHz
+ * link frequency. Only the frame rate, and therefore the VT pixel clock,
+ * was measured; the D-PHY lane clock was not. Keeping 420 MHz avoids a
+ * 0.48% error in V4L2_CID_PIXEL_RATE (168.0 vs 167.2 Mpix/s at 418 MHz),
+ * and the 0x0820/0x0821 value of 0x0344 alone does not settle the lane
+ * rate either way.
+ */
+static const struct s5k5e8_mode s5k5e8_supported_modes[] = {
+	{
+		.width = 2592,
+		.height = 1944,
+		.vts = 1968,
+		.hts = 2856,
+		.exposure = 990,
+		.crop = {
+			.left = 8,
+			.top = 8,
+			.width = 2592,
+			.height = 1944,
+		},
+		.reg_list = {
+			.regs = s5k5e8_2592x1944_30fps_mode,
+			.num_regs = ARRAY_SIZE(s5k5e8_2592x1944_30fps_mode),
+		},
+	},
+	{
+		.width = 1304,
+		.height = 980,
+		.vts = 1009,
+		.hts = 2784,
+		.exposure = 990,
+		.crop = {
+			.left = 0,
+			.top = 0,
+			.width = 2608,
+			.height = 1960,
+		},
+		.reg_list = {
+			.regs = s5k5e8_1304x980_60fps_mode,
+			.num_regs = ARRAY_SIZE(s5k5e8_1304x980_60fps_mode),
+		},
+	},
+};
+
+static u32 s5k5e8_get_format_code(bool hflip, bool vflip)
+{
+	return s5k5e8_mbus_formats[(vflip ? 2 : 0) | (hflip ? 1 : 0)];
+}
+
+static const struct s5k5e8_mode *
+s5k5e8_get_mode_from_state(struct v4l2_subdev_state *state)
+{
+	const struct v4l2_mbus_framefmt *fmt;
+
+	fmt = v4l2_subdev_state_get_format(state, 0);
+
+	return v4l2_find_nearest_size(s5k5e8_supported_modes,
+				      ARRAY_SIZE(s5k5e8_supported_modes),
+				      width, height, fmt->width, fmt->height);
+}
+
+static int s5k5e8_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct s5k5e8 *s5k5e8 =
+		container_of(ctrl->handler, struct s5k5e8, ctrl_handler);
+	struct v4l2_subdev_state *state;
+	const struct s5k5e8_mode *mode;
+	s64 exposure_max, exposure_def;
+	int pm_status;
+	int ret;
+
+	state = v4l2_subdev_get_locked_active_state(&s5k5e8->sd);
+	if (WARN_ON_ONCE(!state))
+		return -EINVAL;
+
+	mode = s5k5e8_get_mode_from_state(state);
+
+	/* Propagate change of current control to all related controls */
+	switch (ctrl->id) {
+	case V4L2_CID_VBLANK:
+		/* Update max exposure while meeting expected vblanking */
+		exposure_max =
+			mode->height + ctrl->val - S5K5E8_EXPOSURE_MARGIN;
+		exposure_def = min_t(s64, mode->exposure, exposure_max);
+		ret = __v4l2_ctrl_modify_range(s5k5e8->exposure,
+					       s5k5e8->exposure->minimum,
+					       exposure_max,
+					       s5k5e8->exposure->step,
+					       exposure_def);
+		if (ret)
+			return ret;
+		break;
+	}
+
+	/* V4L2 controls are applied when sensor is powered up for streaming */
+	pm_status = pm_runtime_get_if_active(s5k5e8->dev);
+	if (!pm_status)
+		return 0;
+
+	switch (ctrl->id) {
+	case V4L2_CID_ANALOGUE_GAIN:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_AGAIN, ctrl->val,
+				NULL);
+		break;
+	case V4L2_CID_EXPOSURE:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_EXPOSURE,
+				ctrl->val, NULL);
+		break;
+	case V4L2_CID_VBLANK:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_VTS,
+				ctrl->val + mode->height, NULL);
+		break;
+	case V4L2_CID_VFLIP:
+	case V4L2_CID_HFLIP:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_ORIENTATION,
+				(s5k5e8->vflip->val ? S5K5E8_VFLIP : 0) |
+				(s5k5e8->hflip->val ? S5K5E8_HFLIP : 0),
+				NULL);
+		/* Only report the flipped Bayer order once the sensor has it. */
+		if (!ret)
+			v4l2_subdev_state_get_format(state, 0)->code =
+				s5k5e8_get_format_code(s5k5e8->hflip->val,
+						       s5k5e8->vflip->val);
+		break;
+	case V4L2_CID_TEST_PATTERN:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_TEST_PATTERN,
+				ctrl->val, NULL);
+		break;
+	case V4L2_CID_TEST_PATTERN_RED:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_TEST_PATTERN_RED,
+				ctrl->val, NULL);
+		break;
+	case V4L2_CID_TEST_PATTERN_GREENR:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_TEST_PATTERN_GREENR,
+				ctrl->val, NULL);
+		break;
+	case V4L2_CID_TEST_PATTERN_BLUE:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_TEST_PATTERN_BLUE,
+				ctrl->val, NULL);
+		break;
+	case V4L2_CID_TEST_PATTERN_GREENB:
+		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_TEST_PATTERN_GREENB,
+				ctrl->val, NULL);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	if (pm_status > 0)
+		pm_runtime_put_autosuspend(s5k5e8->dev);
+
+	return ret;
+}
+
+static const struct v4l2_ctrl_ops s5k5e8_ctrl_ops = {
+	.s_ctrl = s5k5e8_set_ctrl,
+};
+
+static inline u64 s5k5e8_freq_to_pixel_rate(const u64 freq)
+{
+	return div_u64(freq * 2 * S5K5E8_DATA_LANES, 10);
+}
+
+static int s5k5e8_init_controls(struct s5k5e8 *s5k5e8)
+{
+	struct v4l2_ctrl_handler *ctrl_hdlr = &s5k5e8->ctrl_handler;
+	const struct s5k5e8_mode *mode = &s5k5e8_supported_modes[0];
+	s64 pixel_rate, hblank, vblank, exposure_max;
+	struct v4l2_fwnode_device_properties props;
+	struct v4l2_ctrl *ctrl;
+	unsigned int i;
+	int ret;
+
+	v4l2_ctrl_handler_init(ctrl_hdlr, 15);
+
+	ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, NULL, V4L2_CID_LINK_FREQ,
+				      ARRAY_SIZE(s5k5e8_link_freq_menu) - 1,
+				      0, s5k5e8_link_freq_menu);
+	if (ctrl)
+		ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+
+	pixel_rate = s5k5e8_freq_to_pixel_rate(s5k5e8_link_freq_menu[0]);
+	v4l2_ctrl_new_std(ctrl_hdlr, NULL, V4L2_CID_PIXEL_RATE,
+			  pixel_rate, pixel_rate, 1, pixel_rate);
+
+	hblank = mode->hts - mode->width;
+	s5k5e8->hblank = v4l2_ctrl_new_std(ctrl_hdlr, NULL, V4L2_CID_HBLANK,
+					   hblank, hblank, 1, hblank);
+	if (s5k5e8->hblank)
+		s5k5e8->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
+
+	vblank = mode->vts - mode->height;
+	s5k5e8->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &s5k5e8_ctrl_ops,
+					   V4L2_CID_VBLANK, vblank,
+					   S5K5E8_VTS_MAX - mode->height, 1,
+					   vblank);
+
+	v4l2_ctrl_new_std(ctrl_hdlr, &s5k5e8_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
+			  S5K5E8_AGAIN_MIN, S5K5E8_AGAIN_MAX,
+			  S5K5E8_AGAIN_STEP, S5K5E8_AGAIN_DEFAULT);
+
+	exposure_max = mode->vts - S5K5E8_EXPOSURE_MARGIN;
+	s5k5e8->exposure =
+		v4l2_ctrl_new_std(ctrl_hdlr, &s5k5e8_ctrl_ops,
+				  V4L2_CID_EXPOSURE, S5K5E8_EXPOSURE_MIN,
+				  exposure_max, S5K5E8_EXPOSURE_STEP,
+				  mode->exposure);
+
+	v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &s5k5e8_ctrl_ops,
+				     V4L2_CID_TEST_PATTERN,
+				     ARRAY_SIZE(s5k5e8_test_pattern_menu) - 1,
+				     0, 0, s5k5e8_test_pattern_menu);
+
+	for (i = 0; i < 4; i++) {
+		/*
+		 * The assumption is that
+		 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
+		 * V4L2_CID_TEST_PATTERN_BLUE   == V4L2_CID_TEST_PATTERN_RED + 2
+		 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
+		 *
+		 * The solid colour is grey by default: the sensor reset value
+		 * of all four colour registers is 0x200, verified on the
+		 * Redmi Note 4X (mido).
+		 */
+		v4l2_ctrl_new_std(ctrl_hdlr, &s5k5e8_ctrl_ops,
+				  V4L2_CID_TEST_PATTERN_RED + i,
+				  S5K5E8_TEST_PATTERN_COLOUR_MIN,
+				  S5K5E8_TEST_PATTERN_COLOUR_MAX,
+				  1, S5K5E8_TEST_PATTERN_COLOUR_DEFAULT);
+	}
+
+	s5k5e8->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &s5k5e8_ctrl_ops,
+					  V4L2_CID_HFLIP, 0, 1, 1, 0);
+	if (s5k5e8->hflip)
+		s5k5e8->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
+
+	s5k5e8->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &s5k5e8_ctrl_ops,
+					  V4L2_CID_VFLIP, 0, 1, 1, 0);
+	if (s5k5e8->vflip)
+		s5k5e8->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
+
+	if (ctrl_hdlr->error) {
+		ret = ctrl_hdlr->error;
+		goto error_free_hdlr;
+	}
+
+	v4l2_ctrl_cluster(2, &s5k5e8->hflip);
+
+	ret = v4l2_fwnode_device_parse(s5k5e8->dev, &props);
+	if (ret)
+		goto error_free_hdlr;
+
+	ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, NULL,
+					      &props);
+	if (ret)
+		goto error_free_hdlr;
+
+	s5k5e8->sd.ctrl_handler = ctrl_hdlr;
+
+	return 0;
+
+error_free_hdlr:
+	v4l2_ctrl_handler_free(ctrl_hdlr);
+
+	return ret;
+}
+
+static int s5k5e8_enable_streams(struct v4l2_subdev *sd,
+				 struct v4l2_subdev_state *state, u32 pad,
+				 u64 streams_mask)
+{
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+	const struct s5k5e8_mode *mode = s5k5e8_get_mode_from_state(state);
+	const struct s5k5e8_reg_list *reg_list = &mode->reg_list;
+	int ret;
+
+	ret = pm_runtime_resume_and_get(s5k5e8->dev);
+	if (ret)
+		return ret;
+
+	cci_multi_reg_write(s5k5e8->regmap, init_array_setting,
+			    ARRAY_SIZE(init_array_setting), &ret);
+	cci_multi_reg_write(s5k5e8->regmap, reg_list->regs,
+			    reg_list->num_regs, &ret);
+	if (ret)
+		goto error;
+
+	ret = __v4l2_ctrl_handler_setup(s5k5e8->sd.ctrl_handler);
+	if (ret)
+		goto error;
+
+	ret = cci_write(s5k5e8->regmap, S5K5E8_REG_CTRL_MODE,
+			S5K5E8_MODE_STREAMING, NULL);
+	if (ret)
+		goto error;
+
+	__v4l2_ctrl_grab(s5k5e8->hflip, true);
+	__v4l2_ctrl_grab(s5k5e8->vflip, true);
+
+	return 0;
+
+error:
+	dev_err(s5k5e8->dev, "failed to start streaming: %d\n", ret);
+	pm_runtime_put_autosuspend(s5k5e8->dev);
+
+	return ret;
+}
+
+static int s5k5e8_disable_streams(struct v4l2_subdev *sd,
+				  struct v4l2_subdev_state *state, u32 pad,
+				  u64 streams_mask)
+{
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+	int ret;
+
+	ret = cci_write(s5k5e8->regmap, S5K5E8_REG_CTRL_MODE, 0, NULL);
+	if (ret)
+		dev_err(s5k5e8->dev, "failed to stop streaming: %d\n", ret);
+
+	__v4l2_ctrl_grab(s5k5e8->hflip, false);
+	__v4l2_ctrl_grab(s5k5e8->vflip, false);
+
+	pm_runtime_put_autosuspend(s5k5e8->dev);
+
+	return ret;
+}
+
+static void s5k5e8_update_pad_format(struct s5k5e8 *s5k5e8,
+				     const struct s5k5e8_mode *mode,
+				     struct v4l2_mbus_framefmt *fmt)
+{
+	fmt->code = s5k5e8_get_format_code(s5k5e8->hflip->cur.val,
+					   s5k5e8->vflip->cur.val);
+	fmt->width = mode->width;
+	fmt->height = mode->height;
+	fmt->field = V4L2_FIELD_NONE;
+	fmt->colorspace = V4L2_COLORSPACE_RAW;
+	fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
+	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
+	fmt->xfer_func = V4L2_XFER_FUNC_NONE;
+}
+
+static int s5k5e8_set_pad_format(struct v4l2_subdev *sd,
+				 const struct v4l2_subdev_client_info *ci,
+				 struct v4l2_subdev_state *state,
+				 struct v4l2_subdev_format *fmt)
+{
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+	const struct s5k5e8_mode *mode;
+	const struct s5k5e8_mode *old_mode;
+	struct v4l2_mbus_framefmt *format;
+	struct v4l2_rect *crop;
+	struct v4l2_mbus_framefmt old_format;
+	struct v4l2_rect old_crop;
+	s64 hblank, vblank;
+	bool mode_changed;
+	int ret;
+
+	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE &&
+	    v4l2_subdev_is_streaming(sd))
+		return -EBUSY;
+
+	mode = v4l2_find_nearest_size(s5k5e8_supported_modes,
+				      ARRAY_SIZE(s5k5e8_supported_modes),
+				      width, height, fmt->format.width,
+				      fmt->format.height);
+
+	s5k5e8_update_pad_format(s5k5e8, mode, &fmt->format);
+
+	format = v4l2_subdev_state_get_format(state, 0);
+	crop = v4l2_subdev_state_get_crop(state, 0);
+	mode_changed = format->width != mode->width ||
+		       format->height != mode->height;
+
+	/*
+	 * Commit the new format first: s5k5e8_set_ctrl() derives the mode
+	 * from the state format, so the control updates below must see the
+	 * new mode. Save the old format, crop and mode to roll back to if
+	 * any of those updates fails.
+	 */
+	old_format = *format;
+	old_crop = *crop;
+	old_mode = s5k5e8_get_mode_from_state(state);
+
+	*format = fmt->format;
+	*crop = mode->crop;
+
+	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE && mode_changed) {
+		hblank = mode->hts - mode->width;
+		ret = __v4l2_ctrl_modify_range(s5k5e8->hblank, hblank, hblank,
+					       1, hblank);
+		if (ret)
+			goto restore;
+
+		vblank = mode->vts - mode->height;
+		ret = __v4l2_ctrl_modify_range(s5k5e8->vblank, vblank,
+					       S5K5E8_VTS_MAX - mode->height,
+					       1, vblank);
+		if (ret)
+			goto restore;
+
+		ret = __v4l2_ctrl_s_ctrl(s5k5e8->vblank, vblank);
+		if (ret)
+			goto restore;
+
+		/*
+		 * No explicit exposure range update here: setting VBLANK
+		 * above already updated the exposure range through
+		 * s5k5e8_set_ctrl().
+		 */
+		ret = __v4l2_ctrl_s_ctrl(s5k5e8->exposure, mode->exposure);
+		if (ret)
+			goto restore;
+	}
+
+	return 0;
+
+restore:
+	*format = old_format;
+	*crop = old_crop;
+
+	/* Best effort: restore the control ranges of the old mode. */
+	hblank = old_mode->hts - old_mode->width;
+	__v4l2_ctrl_modify_range(s5k5e8->hblank, hblank, hblank, 1, hblank);
+	vblank = old_mode->vts - old_mode->height;
+	__v4l2_ctrl_modify_range(s5k5e8->vblank, vblank,
+				 S5K5E8_VTS_MAX - old_mode->height, 1, vblank);
+	__v4l2_ctrl_s_ctrl(s5k5e8->vblank, vblank);
+	__v4l2_ctrl_s_ctrl(s5k5e8->exposure, old_mode->exposure);
+
+	return ret;
+}
+
+static int s5k5e8_enum_mbus_code(struct v4l2_subdev *sd,
+				 struct v4l2_subdev_state *sd_state,
+				 struct v4l2_subdev_mbus_code_enum *code)
+{
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+
+	/* Media bus code index is constant, but code formats are not */
+	if (code->index > 0)
+		return -EINVAL;
+
+	code->code = s5k5e8_get_format_code(s5k5e8->hflip->cur.val,
+					    s5k5e8->vflip->cur.val);
+
+	return 0;
+}
+
+static int s5k5e8_enum_frame_size(struct v4l2_subdev *sd,
+				  struct v4l2_subdev_state *sd_state,
+				  struct v4l2_subdev_frame_size_enum *fse)
+{
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+
+	if (fse->index >= ARRAY_SIZE(s5k5e8_supported_modes))
+		return -EINVAL;
+
+	if (fse->code != s5k5e8_get_format_code(s5k5e8->hflip->cur.val,
+						s5k5e8->vflip->cur.val))
+		return -EINVAL;
+
+	fse->min_width = s5k5e8_supported_modes[fse->index].width;
+	fse->max_width = fse->min_width;
+	fse->min_height = s5k5e8_supported_modes[fse->index].height;
+	fse->max_height = fse->min_height;
+
+	return 0;
+}
+
+static int s5k5e8_get_selection(struct v4l2_subdev *sd,
+				const struct v4l2_subdev_client_info *ci,
+				struct v4l2_subdev_state *state,
+				struct v4l2_subdev_selection *sel)
+{
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+		sel->r = (struct v4l2_rect){
+			.left = 0,
+			.top = 0,
+			.width = S5K5E8_NATIVE_WIDTH,
+			.height = S5K5E8_NATIVE_HEIGHT,
+		};
+		return 0;
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r = s5k5e8_supported_modes[0].crop;
+		return 0;
+	case V4L2_SEL_TGT_CROP:
+		sel->r = *v4l2_subdev_state_get_crop(state, 0);
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int s5k5e8_init_state(struct v4l2_subdev *sd,
+			     struct v4l2_subdev_state *state)
+{
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+	const struct s5k5e8_mode *mode = &s5k5e8_supported_modes[0];
+
+	s5k5e8_update_pad_format(s5k5e8, mode,
+				 v4l2_subdev_state_get_format(state, 0));
+	*v4l2_subdev_state_get_crop(state, 0) = mode->crop;
+
+	return 0;
+}
+
+static const struct v4l2_subdev_video_ops s5k5e8_video_ops = {
+	.s_stream = v4l2_subdev_s_stream_helper,
+};
+
+static const struct v4l2_subdev_pad_ops s5k5e8_pad_ops = {
+	.set_fmt = s5k5e8_set_pad_format,
+	.get_fmt = v4l2_subdev_get_fmt,
+	.get_selection = s5k5e8_get_selection,
+	.enum_mbus_code = s5k5e8_enum_mbus_code,
+	.enum_frame_size = s5k5e8_enum_frame_size,
+	.enable_streams = s5k5e8_enable_streams,
+	.disable_streams = s5k5e8_disable_streams,
+};
+
+static const struct v4l2_subdev_ops s5k5e8_subdev_ops = {
+	.video = &s5k5e8_video_ops,
+	.pad = &s5k5e8_pad_ops,
+};
+
+static const struct v4l2_subdev_internal_ops s5k5e8_internal_ops = {
+	.init_state = s5k5e8_init_state,
+};
+
+static int s5k5e8_identify_sensor(struct s5k5e8 *s5k5e8)
+{
+	u64 val;
+	int ret;
+
+	ret = cci_read(s5k5e8->regmap, S5K5E8_REG_CHIP_ID, &val, NULL);
+	if (ret) {
+		dev_err(s5k5e8->dev, "failed to read chip id: %d\n", ret);
+		return ret;
+	}
+
+	if (val != S5K5E8_CHIP_ID) {
+		dev_err(s5k5e8->dev,
+			"chip id mismatch: expected 0x%04x, got 0x%04llx\n",
+			S5K5E8_CHIP_ID, val);
+		return -ENODEV;
+	}
+
+	dev_dbg(s5k5e8->dev, "identified Samsung S5K5E8 sensor (chip id 0x%04llx)\n",
+		val);
+
+	return 0;
+}
+
+static int s5k5e8_check_hwcfg(struct s5k5e8 *s5k5e8)
+{
+	struct fwnode_handle *fwnode = dev_fwnode(s5k5e8->dev), *ep;
+	struct v4l2_fwnode_endpoint bus_cfg = {
+		.bus_type = V4L2_MBUS_CSI2_DPHY,
+	};
+	unsigned long freq_bitmap;
+	int ret;
+
+	if (!fwnode)
+		return -ENODEV;
+
+	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
+
+	ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
+	fwnode_handle_put(ep);
+	if (ret)
+		return ret;
+
+	if (bus_cfg.bus.mipi_csi2.num_data_lanes != S5K5E8_DATA_LANES) {
+		dev_err(s5k5e8->dev, "Invalid number of data lanes: %u\n",
+			bus_cfg.bus.mipi_csi2.num_data_lanes);
+		ret = -EINVAL;
+		goto endpoint_free;
+	}
+
+	ret = v4l2_link_freq_to_bitmap(s5k5e8->dev, bus_cfg.link_frequencies,
+				       bus_cfg.nr_of_link_frequencies,
+				       s5k5e8_link_freq_menu,
+				       ARRAY_SIZE(s5k5e8_link_freq_menu),
+				       &freq_bitmap);
+
+endpoint_free:
+	v4l2_fwnode_endpoint_free(&bus_cfg);
+
+	return ret;
+}
+
+static int s5k5e8_power_on(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+	unsigned int i;
+	int ret;
+
+	/* Enable supplies sequentially: vddio -> vdda -> vddd */
+	for (i = 0; i < ARRAY_SIZE(s5k5e8_supply_names); i++) {
+		ret = regulator_enable(s5k5e8->supplies[i].consumer);
+		if (ret)
+			goto disable_regulators;
+
+		usleep_range(1000, 1500);
+	}
+
+	ret = clk_prepare_enable(s5k5e8->mclk);
+	if (ret)
+		goto disable_regulators;
+
+	usleep_range(1000, 2000);
+
+	gpiod_set_value_cansleep(s5k5e8->powerdown_gpio, 0);
+	usleep_range(1000, 2000);
+	gpiod_set_value_cansleep(s5k5e8->reset_gpio, 0);
+
+	usleep_range(10000, 11000);
+
+	return 0;
+
+disable_regulators:
+	while (i > 0)
+		regulator_disable(s5k5e8->supplies[--i].consumer);
+
+	return ret;
+}
+
+static int s5k5e8_power_off(struct device *dev)
+{
+	struct v4l2_subdev *sd = dev_get_drvdata(dev);
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+	unsigned int i;
+
+	gpiod_set_value_cansleep(s5k5e8->reset_gpio, 1);
+	gpiod_set_value_cansleep(s5k5e8->powerdown_gpio, 1);
+
+	clk_disable_unprepare(s5k5e8->mclk);
+
+	for (i = ARRAY_SIZE(s5k5e8_supply_names); i > 0; i--)
+		regulator_disable(s5k5e8->supplies[i - 1].consumer);
+
+	return 0;
+}
+
+static int s5k5e8_probe(struct i2c_client *client)
+{
+	struct s5k5e8 *s5k5e8;
+	unsigned long freq;
+	unsigned int i;
+	int ret;
+
+	s5k5e8 = devm_kzalloc(&client->dev, sizeof(*s5k5e8), GFP_KERNEL);
+	if (!s5k5e8)
+		return -ENOMEM;
+
+	s5k5e8->dev = &client->dev;
+	v4l2_i2c_subdev_init(&s5k5e8->sd, client, &s5k5e8_subdev_ops);
+
+	s5k5e8->regmap = devm_cci_regmap_init_i2c(client, 16);
+	if (IS_ERR(s5k5e8->regmap))
+		return dev_err_probe(s5k5e8->dev, PTR_ERR(s5k5e8->regmap),
+				     "failed to init CCI\n");
+
+	s5k5e8->mclk = devm_v4l2_sensor_clk_get(s5k5e8->dev, NULL);
+	if (IS_ERR(s5k5e8->mclk))
+		return dev_err_probe(s5k5e8->dev, PTR_ERR(s5k5e8->mclk),
+				     "failed to get MCLK clock\n");
+
+	freq = clk_get_rate(s5k5e8->mclk);
+	if (freq != S5K5E8_MCLK_FREQ_24MHZ)
+		return dev_err_probe(s5k5e8->dev, -EINVAL,
+				     "MCLK clock frequency %lu is not supported\n",
+				     freq);
+
+	ret = s5k5e8_check_hwcfg(s5k5e8);
+	if (ret)
+		return dev_err_probe(s5k5e8->dev, ret,
+				     "failed to check HW configuration\n");
+
+	s5k5e8->reset_gpio =
+		devm_gpiod_get_optional(s5k5e8->dev, "reset", GPIOD_OUT_HIGH);
+	if (IS_ERR(s5k5e8->reset_gpio))
+		return dev_err_probe(s5k5e8->dev,
+				     PTR_ERR(s5k5e8->reset_gpio),
+				     "failed to get reset GPIO\n");
+
+	s5k5e8->powerdown_gpio =
+		devm_gpiod_get_optional(s5k5e8->dev, "powerdown",
+					GPIOD_OUT_HIGH);
+	if (IS_ERR(s5k5e8->powerdown_gpio))
+		return dev_err_probe(s5k5e8->dev,
+				     PTR_ERR(s5k5e8->powerdown_gpio),
+				     "failed to get powerdown GPIO\n");
+
+	for (i = 0; i < ARRAY_SIZE(s5k5e8_supply_names); i++)
+		s5k5e8->supplies[i].supply = s5k5e8_supply_names[i];
+
+	ret = devm_regulator_bulk_get(s5k5e8->dev,
+				      ARRAY_SIZE(s5k5e8_supply_names),
+				      s5k5e8->supplies);
+	if (ret)
+		return dev_err_probe(s5k5e8->dev, ret,
+				     "failed to get supply regulators\n");
+
+	ret = s5k5e8_power_on(s5k5e8->dev);
+	if (ret)
+		return dev_err_probe(s5k5e8->dev, ret,
+				     "failed to power on sensor\n");
+
+	ret = s5k5e8_identify_sensor(s5k5e8);
+	if (ret)
+		goto power_off;
+
+	ret = s5k5e8_init_controls(s5k5e8);
+	if (ret) {
+		ret = dev_err_probe(s5k5e8->dev, ret,
+				    "failed to init controls\n");
+		goto power_off;
+	}
+
+	s5k5e8->sd.state_lock = s5k5e8->ctrl_handler.lock;
+	s5k5e8->sd.internal_ops = &s5k5e8_internal_ops;
+	s5k5e8->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
+			    V4L2_SUBDEV_FL_HAS_EVENTS;
+	s5k5e8->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+	s5k5e8->pad.flags = MEDIA_PAD_FL_SOURCE;
+
+	ret = media_entity_pads_init(&s5k5e8->sd.entity, 1, &s5k5e8->pad);
+	if (ret) {
+		ret = dev_err_probe(s5k5e8->dev, ret,
+				    "failed to init media entity pads\n");
+		goto v4l2_ctrl_handler_free;
+	}
+
+	ret = v4l2_subdev_init_finalize(&s5k5e8->sd);
+	if (ret < 0) {
+		ret = dev_err_probe(s5k5e8->dev, ret,
+				    "failed to finalize v4l2 subdev\n");
+		goto media_entity_cleanup;
+	}
+
+	pm_runtime_set_active(s5k5e8->dev);
+	pm_runtime_set_autosuspend_delay(s5k5e8->dev, 1000);
+	pm_runtime_use_autosuspend(s5k5e8->dev);
+	pm_runtime_enable(s5k5e8->dev);
+
+	ret = v4l2_async_register_subdev_sensor(&s5k5e8->sd);
+	if (ret < 0) {
+		ret = dev_err_probe(s5k5e8->dev, ret,
+				    "failed to register V4L2 subdev\n");
+		goto subdev_cleanup;
+	}
+
+	pm_runtime_idle(s5k5e8->dev);
+
+	return 0;
+
+subdev_cleanup:
+	pm_runtime_dont_use_autosuspend(s5k5e8->dev);
+	pm_runtime_disable(s5k5e8->dev);
+	pm_runtime_set_suspended(s5k5e8->dev);
+	v4l2_subdev_cleanup(&s5k5e8->sd);
+
+media_entity_cleanup:
+	media_entity_cleanup(&s5k5e8->sd.entity);
+
+v4l2_ctrl_handler_free:
+	v4l2_ctrl_handler_free(s5k5e8->sd.ctrl_handler);
+
+power_off:
+	s5k5e8_power_off(s5k5e8->dev);
+
+	return ret;
+}
+
+static void s5k5e8_remove(struct i2c_client *client)
+{
+	struct v4l2_subdev *sd = i2c_get_clientdata(client);
+	struct s5k5e8 *s5k5e8 = to_s5k5e8(sd);
+
+	v4l2_async_unregister_subdev(sd);
+	v4l2_subdev_cleanup(sd);
+	media_entity_cleanup(&sd->entity);
+	v4l2_ctrl_handler_free(sd->ctrl_handler);
+	pm_runtime_dont_use_autosuspend(s5k5e8->dev);
+	pm_runtime_disable(s5k5e8->dev);
+
+	if (!pm_runtime_status_suspended(s5k5e8->dev)) {
+		s5k5e8_power_off(s5k5e8->dev);
+		pm_runtime_set_suspended(s5k5e8->dev);
+	}
+}
+
+static const struct dev_pm_ops s5k5e8_pm_ops = {
+	RUNTIME_PM_OPS(s5k5e8_power_off, s5k5e8_power_on, NULL)
+};
+
+static const struct of_device_id s5k5e8_of_match[] = {
+	{ .compatible = "samsung,s5k5e8" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, s5k5e8_of_match);
+
+static struct i2c_driver s5k5e8_i2c_driver = {
+	.driver = {
+		.name = "s5k5e8",
+		.pm = pm_ptr(&s5k5e8_pm_ops),
+		.of_match_table = s5k5e8_of_match,
+	},
+	.probe = s5k5e8_probe,
+	.remove = s5k5e8_remove,
+};
+
+module_i2c_driver(s5k5e8_i2c_driver);
+
+MODULE_AUTHOR("Andrii Cherniavskyi <chernyav.a@gmail.com>");
+MODULE_AUTHOR("Tuan Kiet Huynh <huynhtuankiet33@gmail.com>");
+MODULE_DESCRIPTION("Samsung S5K5E8 image sensor driver");
+MODULE_LICENSE("GPL");
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver
  2026-12-01 11:02 ` [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
@ 2026-12-01 11:16   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-01 11:16 UTC (permalink / raw)
  To: Tuan Kiet Huynh, devicetree; +Cc: robh, conor+dt, media-ci

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] media: i2c: s5k5e8: stale pad format code on flip in
  s5k5e8_set_ctrl()
- [Medium] media: i2c: s5k5e8: skipped exposure range update in
  s5k5e8_set_pad_format()
- [Medium] media: i2c: s5k5e8: double power-off on error in
  s5k5e8_probe()
--

commit 80073db7588e5bb612eb448a1a6e71dcce3b2a95
Author: Andrii Cherniavskyi <chernyav.a@gmail.com>

media: i2c: Add Samsung S5K5E8 camera sensor driver

This commit adds a V4L2 subdevice driver for the Samsung S5K5E8 5-megapixel
MIPI CSI-2 image sensor.

> diff --git a/drivers/media/i2c/s5k5e8.c b/drivers/media/i2c/s5k5e8.c
> new file mode 100644
> index 0000000000000..8a3d034243514
> --- /dev/null
> +++ b/drivers/media/i2c/s5k5e8.c
> @@ -0,0 +1,1108 @@

[ ... ]

> +static int s5k5e8_set_ctrl(struct v4l2_ctrl *ctrl)
> +{
> +	struct s5k5e8 *s5k5e8 =
> +		container_of(ctrl->handler, struct s5k5e8, ctrl_handler);
> +	struct v4l2_subdev_state *state;
> +	const struct s5k5e8_mode *mode;
> +	s64 exposure_max, exposure_def;
> +	int pm_status;
> +	int ret;
> +
> +	state = v4l2_subdev_get_locked_active_state(&s5k5e8->sd);
> +	if (WARN_ON_ONCE(!state))
> +		return -EINVAL;
> +
> +	mode = s5k5e8_get_mode_from_state(state);
> +
> +	/* Propagate change of current control to all related controls */
> +	switch (ctrl->id) {
> +	case V4L2_CID_VBLANK:
> +		/* Update max exposure while meeting expected vblanking */
> +		exposure_max =
> +			mode->height + ctrl->val - S5K5E8_EXPOSURE_MARGIN;
> +		exposure_def = min_t(s64, mode->exposure, exposure_max);
> +		ret = __v4l2_ctrl_modify_range(s5k5e8->exposure,
> +					       s5k5e8->exposure->minimum,
> +					       exposure_max,
> +					       s5k5e8->exposure->step,
> +					       exposure_def);
> +		if (ret)
> +			return ret;
> +		break;
> +	}
> +
> +	/* V4L2 controls are applied when sensor is powered up for streaming */
> +	pm_status = pm_runtime_get_if_active(s5k5e8->dev);
> +	if (!pm_status)
> +		return 0;
> +
> +	switch (ctrl->id) {
> +	case V4L2_CID_ANALOGUE_GAIN:
> +		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_AGAIN, ctrl->val,
> +				NULL);
> +		break;
> +	case V4L2_CID_EXPOSURE:
> +		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_EXPOSURE,
> +				ctrl->val, NULL);
> +		break;
> +	case V4L2_CID_VBLANK:
> +		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_VTS,
> +				ctrl->val + mode->height, NULL);
> +		break;
> +	case V4L2_CID_VFLIP:
> +	case V4L2_CID_HFLIP:
> +		ret = cci_write(s5k5e8->regmap, S5K5E8_REG_ORIENTATION,
> +				(s5k5e8->vflip->val ? S5K5E8_VFLIP : 0) |
> +				(s5k5e8->hflip->val ? S5K5E8_HFLIP : 0),
> +				NULL);
> +		/* Only report the flipped Bayer order once the sensor has it. */
> +		if (!ret)
> +			v4l2_subdev_state_get_format(state, 0)->code =
> +				s5k5e8_get_format_code(s5k5e8->hflip->val,
> +						       s5k5e8->vflip->val);
> +		break;

[Severity: Medium]
Does changing V4L2_CID_HFLIP or V4L2_CID_VFLIP while the sensor is
runtime-suspended leave the active pad format's media bus code stale?

Because s5k5e8_enable_streams() grabs hflip and vflip while streaming,
userspace normally modifies them when the device is idle and suspended.
In that state pm_runtime_get_if_active() returns 0, so the V4L2 core
commits the new flip values to cur.val while
v4l2_subdev_state_get_format(state, 0)->code stays unflipped.
s5k5e8_enum_mbus_code() and s5k5e8_enum_frame_size() then report the new
flipped Bayer code from cur.val, whereas VIDIOC_SUBDEV_G_FMT and pipeline
link validation before streaming see the old code.

[ ... ]

> +static int s5k5e8_set_pad_format(struct v4l2_subdev *sd,
> +				 const struct v4l2_subdev_client_info *ci,
> +				 struct v4l2_subdev_state *state,
> +				 struct v4l2_subdev_format *fmt)
> +{

[ ... ]

> +	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE && mode_changed) {
> +		hblank = mode->hts - mode->width;
> +		ret = __v4l2_ctrl_modify_range(s5k5e8->hblank, hblank, hblank,
> +					       1, hblank);
> +		if (ret)
> +			goto restore;
> +
> +		vblank = mode->vts - mode->height;
> +		ret = __v4l2_ctrl_modify_range(s5k5e8->vblank, vblank,
> +					       S5K5E8_VTS_MAX - mode->height,
> +					       1, vblank);
> +		if (ret)
> +			goto restore;
> +
> +		ret = __v4l2_ctrl_s_ctrl(s5k5e8->vblank, vblank);
> +		if (ret)
> +			goto restore;
> +
> +		/*
> +		 * No explicit exposure range update here: setting VBLANK
> +		 * above already updated the exposure range through
> +		 * s5k5e8_set_ctrl().
> +		 */
> +		ret = __v4l2_ctrl_s_ctrl(s5k5e8->exposure, mode->exposure);
> +		if (ret)
> +			goto restore;
> +	}
> +
> +	return 0;
> +
> +restore:
> +	*format = old_format;
> +	*crop = old_crop;
> +
> +	/* Best effort: restore the control ranges of the old mode. */
> +	hblank = old_mode->hts - old_mode->width;
> +	__v4l2_ctrl_modify_range(s5k5e8->hblank, hblank, hblank, 1, hblank);
> +	vblank = old_mode->vts - old_mode->height;
> +	__v4l2_ctrl_modify_range(s5k5e8->vblank, vblank,
> +				 S5K5E8_VTS_MAX - old_mode->height, 1, vblank);
> +	__v4l2_ctrl_s_ctrl(s5k5e8->vblank, vblank);
> +	__v4l2_ctrl_s_ctrl(s5k5e8->exposure, old_mode->exposure);
> +
> +	return ret;
> +}

[Severity: Medium]
Can relying on __v4l2_ctrl_s_ctrl(s5k5e8->vblank, vblank) skip the
s5k5e8->exposure range update when switching modes?

Both __v4l2_ctrl_modify_range() and __v4l2_ctrl_s_ctrl() only invoke
s5k5e8_set_ctrl() when the control value actually changes from cur.val.
If userspace sets V4L2_CID_VBLANK to 29 in the 2592x1944 mode and then
switches to the 1304x980 mode (where the default vblank is also 1009 - 980
= 29), s5k5e8_set_ctrl() is not called, leaving s5k5e8->exposure->maximum
at 1969 instead of 1005 (and skipping the S5K5E8_REG_VTS write if the
sensor is runtime-active).  For the same reason, if
__v4l2_ctrl_s_ctrl(s5k5e8->vblank, vblank) fails during cci_write() after
modifying the exposure range, vblank->cur.val is still the old mode's
vblank, so the restore path will not roll back the exposure range either.

[ ... ]

> +	pm_runtime_set_active(s5k5e8->dev);
> +	pm_runtime_set_autosuspend_delay(s5k5e8->dev, 1000);
> +	pm_runtime_use_autosuspend(s5k5e8->dev);
> +	pm_runtime_enable(s5k5e8->dev);
> +
> +	ret = v4l2_async_register_subdev_sensor(&s5k5e8->sd);
> +	if (ret < 0) {
> +		ret = dev_err_probe(s5k5e8->dev, ret,
> +				    "failed to register V4L2 subdev\n");
> +		goto subdev_cleanup;
> +	}
> +
> +	pm_runtime_idle(s5k5e8->dev);
> +
> +	return 0;
> +
> +subdev_cleanup:
> +	pm_runtime_dont_use_autosuspend(s5k5e8->dev);
> +	pm_runtime_disable(s5k5e8->dev);
> +	pm_runtime_set_suspended(s5k5e8->dev);
> +	v4l2_subdev_cleanup(&s5k5e8->sd);
> +
> +media_entity_cleanup:
> +	media_entity_cleanup(&s5k5e8->sd.entity);
> +
> +v4l2_ctrl_handler_free:
> +	v4l2_ctrl_handler_free(s5k5e8->sd.ctrl_handler);
> +
> +power_off:
> +	s5k5e8_power_off(s5k5e8->dev);
> +
> +	return ret;
> +}

[Severity: Medium]
Does the subdev_cleanup error path call s5k5e8_power_off() twice if
v4l2_async_register_subdev_sensor() fails?

Runtime PM is enabled with the device active and usage_count == 0, so
calling pm_runtime_dont_use_autosuspend() before pm_runtime_disable()
triggers an immediate rpm_idle() -> rpm_suspend() that runs
s5k5e8_power_off().  Falling through to the power_off label then calls
s5k5e8_power_off() a second time, causing unbalanced clk_disable_unprepare()
and regulator_disable() calls.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201110235.3327189-1-huynhtuankiet33@gmail.com?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 11:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 11:02 [PATCH v4 0/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
2026-12-01 11:02 ` [PATCH v4 1/2] dt-bindings: media: i2c: Add Samsung S5K5E8 image sensor Tuan Kiet Huynh
2026-12-01 11:02 ` [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Tuan Kiet Huynh
2026-12-01 11:16   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox