Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.176 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796122968; b=ZRpcS8s+eXfwWUl1brDphcmsZBiPpXmNdmUOIr3ZhaxM3SMIq3Z0ziVWzl+NipXlD13b ALqkAUqCQs+oZXZT6ExgRDtB2Yek/NbZB/UoagvYUGFpz0FnBBufNHa1lBj8S7k3F3/jA rrh+C5TJRC59LOZzQDf9NbsKeXKybJHpFABL1R0mUfwH08oRjCrIXJ57OL5bUeSPf2Kh4 2sBJS2P9wloBUVULdMw5VdnkkqpWMhGii18rtQQjyRdcp8jX1i0g9IhTwQUEp+HOakoTS tSlBmfUIgGhLg9h2gOfWFZrCnhWYnbp8mYxtP+3ho9b2gAl2fpKyvyFo0XQNBXu4f9Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796122968; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=tbWgvfXuShd/VYOMnFNkKv0ZgScHXuabQHG/bTafrVQ=; b=aCRVB0VxB8xgfnrqGbFkKF343wRZYpleBlIMHgh0J1y9pAAFnCh5665j1Uzr1Y62Sm3C rgvbjREsawERfoTdyG4vfdTJ9J4U9nf0fOz7yXCtMX997vi2U5KN87z5N3UehfXr04E9x dubylcPWRS6LiIGZVwz9jbbJWfy2HXKLLGevtg2KVzJVpOgVeYQ6qIlkjWO0mEP1hAdHV PJeLy0QLwI5VhpxRAiI4r/W0a4vyUwQ8gWnHqY349vMbn4V68u+ERPCaBgxAgGEHhn2xx my2lC6uL55MihZkUWJ5Oj9LNE6aY73XzgW5l/+gAyFbam1LYoMhQzK7yRT5HGRnIIcg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=PnooBpPL; arc=none smtp.remote-ip=209.85.214.176 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5F08606A325 for ; Tue, 1 Dec 2026 11:02:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D5F08606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=PnooBpPL Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2f01f296937so86252015ad.3 for ; Tue, 01 Dec 2026 03:02:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796122967; x=1796727767; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tbWgvfXuShd/VYOMnFNkKv0ZgScHXuabQHG/bTafrVQ=; b=PnooBpPLznETxKHEK5ybk8O3KfglxaIycGJiFSJ09NofaNBGAl8XLIR9rd4hCDGlLD buZKgdvifYsv54HirrZIrQocOHOAH4BukVmdyVZZBSwfKWmSEb0ChBl1pCOjPUnpxHSi rHUeP5M/kgiVphohtSCb/zGv5O3POjdXbE/fLXA+4lRNEAUpPX76hFHCJjhTdg3BfGRI l+9KcG2wr9A92IQh4oqHIMK3HF7Nbcd4TJgW9zgjnCeYVISk998496URJstMaPKpGd9J s+/0GBIqbXWQ3rjAXNM84jdr2eIFMJkad+mHGA41X0tOoGCn3lCOiTdwUtDXzg2jLFgz 0lQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796122967; x=1796727767; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=tbWgvfXuShd/VYOMnFNkKv0ZgScHXuabQHG/bTafrVQ=; b=JgycxC3cVOiZlXSsrZhn+VthbiTbkSTHGJqWC30eEwSqJIFN2h68TWgT8PyFifUqxZ lGFnMQNOeHe0wC6t96QBrEQtt0oaTnPkg9mrGafzI5YACu/WCtqcdYXwcnPVgVCj9DEX h1uBnSqZwTJfqR+u6zFjxzQ3Eema3KZfKDr4ePsJYI6iuWI1TyGIS68RT+0medz9IKJQ ui33u8diTPtYPiUiM44iyr56m6ybhC4hZAwO2UMHQAxKjnuU37H3RmUkSZQ4J31iuHEn MdBsfkTSFodAlTTni/VEZznE4b/e31snUBbJBm/F7Gl2OCRVWbvROGy5x3LKac/MdtU8 sWNQ== X-Forwarded-Encrypted: i=1; AL+hErH+XMr3V1IitDl4i6pX2TE3nU57Y9L33DDi/lCkK0P89xXoQdIQUrzg5f5+4mSp99oWutO88i2MaSqv@vger.kernel.org X-Gm-Message-State: APuU4Z83T5MIDysHLnMTbhqhzUsPRpqrD6yI9kh9atP4+Nl5INa2qUKI LdzdV0JMrCNELyslorcl0DzGSPHJ0N5ASpiOV7jGANVA7dsRUihZ6wYO X-Gm-Gg: AZFdTZfv7s1T7TGtiBaWwIofiCmJb0e61sqBOmDt7Hk9N6ha2sIzkMIj+GwhkASIjvq IMQGl1pTjFEa50/gzumtrVqd4vLbPBAgkRyrNCvFXodins0Gp1ofcGkuDlEOSVlB7n2KkqP6Ilr EWpWmOM051V55y9WBeK+VBF8Wi3hgWyXmxVEScGtCJZ+onMwmZ6caDCHpoQhWkt4zsRN9Fd4ogO uTPPGC6BZMZ9FDnZYl542UgokVs73vZ4ijxAMpre7NkYBlZOEDgS2GTJDqH9jgoFOW5wApB9Y1c nIKQvBDAPBHagFPuCUMoGehJ0aQTh6mgeYWbkAC63qlYkh8cRBLt6eBVNZ3f4cBL9/OcECTGtOc g9+lgFBiwqieuiH+nnn8+uAfZ8SB0vP/AJ58RNlIo2pNO6RUMVOstkL725uCpwGW5UEeAd2qoQU yFJH5cVlZqCeW3Ks80QAPClM4dy981yhCyHf/bARcQg5CTjyo3sty/4A31UeIqUG4gLC+yUKppr /MFVTDBeOMGSssl6/GSKL3ClQnkfAbbB6X1IY1tWEYj/5vxP0VCpQsn2lelh5Si4noy8H6DGWGu /G8iLFvNpv02c+xmS9P5Sh55uY0I4WiBYSWisQDI0lrr5VE8ZEUREN73ADdxbhkycPhluqSqt1f iU4sgeRRBNJjgam8thKiuWCXkzcBA X-Received: by 2002:a17:903:3d05:b0:2f0:54f2:dbf2 with SMTP id d9443c01a7336-2f054f2e6d4mr338590075ad.60.1796122966611; Tue, 01 Dec 2026 03:02:46 -0800 (PST) Received: from default-vnic.defaultvcn.oraclevcn.com ([161.118.211.100]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f0ea7aca93sm94095575ad.44.2026.12.01.03.02.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 03:02:46 -0800 (PST) From: Tuan Kiet Huynh To: linux-media@vger.kernel.org, devicetree@vger.kernel.org Cc: sakari.ailus@linux.intel.com, mchehab@kernel.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, chernyav.a@gmail.com, hikmahgumelar@gmail.com, Tuan Kiet Huynh Subject: [PATCH v4 2/2] media: i2c: Add Samsung S5K5E8 camera sensor driver Date: Tue, 1 Dec 2026 18:02:35 +0700 Message-ID: <20261201110235.3327189-3-huynhtuankiet33@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201110235.3327189-1-huynhtuankiet33@gmail.com> References: <20261201110235.3327189-1-huynhtuankiet33@gmail.com> X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Andrii Cherniavskyi 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 Co-developed-by: Tuan Kiet Huynh Signed-off-by: Tuan Kiet Huynh --- 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 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 +// Copyright (c) 2026 Tuan Kiet Huynh + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 "); +MODULE_AUTHOR("Tuan Kiet Huynh "); +MODULE_DESCRIPTION("Samsung S5K5E8 image sensor driver"); +MODULE_LICENSE("GPL"); -- 2.43.0