From 8c59207d1d5c0242a787b936ee188fc5db31d72a Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 01/78] dt-bindings: vendor-prefixes: Add Jooan, OpenIPC and SmartSens Register the vendor prefixes used by the T23 camera board and sensor bindings. Signed-off-by: Difan Zhang --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index af9a8d43b2..fcc1127cc2 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -762,6 +762,8 @@ patternProperties: description: Jiandangjing Technology Co., Ltd. "^jide,.*": description: Jide Tech + "^jooan,.*": + description: Shenzhen Jooan Technology Co., Ltd. "^joz,.*": description: JOZ BV "^kam,.*": @@ -1100,6 +1102,8 @@ patternProperties: description: OpenCores.org "^openembed,.*": description: OpenEmbed + "^openipc,.*": + description: OpenIPC Project "^openpandora,.*": description: OpenPandora GmbH "^openrisc,.*": @@ -1372,6 +1376,8 @@ patternProperties: description: SmartLabs LLC "^smartrg,.*": description: SmartRG, Inc. + "^smartsens,.*": + description: SmartSens Technology (Shanghai) Co., Ltd. "^smi,.*": description: Silicon Motion Technology Corporation "^smsc,.*": From e807c5c3fa545b317d483194a61c9769ed6db4b4 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 02/78] dt-bindings: mips: ingenic: Add T23 camera boards Describe the Ingenic T23 SoC and the Jooan JA-A12, JA-A10 and OpenIPC QEMU board compatibles. Signed-off-by: Difan Zhang --- .../bindings/mips/ingenic/devices.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Documentation/devicetree/bindings/mips/ingenic/devices.yaml b/Documentation/devicetree/bindings/mips/ingenic/devices.yaml index f2e822afe7..2f6f0c6c33 100644 --- a/Documentation/devicetree/bindings/mips/ingenic/devices.yaml +++ b/Documentation/devicetree/bindings/mips/ingenic/devices.yaml @@ -53,6 +53,23 @@ properties: - const: yna,cu2000-neo - const: ingenic,x2000e + - description: Jooan JA-A12 / JA-A10 and generic Ingenic T23 boards + items: + - enum: + - jooan,ja-a12 + - jooan,ja-a10 + - openipc,t23-qemu + - const: ingenic,t23 + + - description: OpenIPC T23 QEMU Virtual IPC + items: + - const: openipc,t23-qemu + - const: jooan,ja-a12 + - const: ingenic,t23 + + - description: Generic Ingenic T23 SoC + const: ingenic,t23 + additionalProperties: true ... From 3932b530002fbcb1e005146aa7ef4bc6065b35eb Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:34:42 -0800 Subject: [PATCH 03/78] MIPS: ingenic: Add T23 SoC support and watchdog restart Register the T23 machine and board selection, and use the CPM scratch signature and TCU watchdog for system restart. Memory size and reusable camera CMA are supplied by Device Tree using the standard kernel command line. Signed-off-by: Difan Zhang --- arch/mips/generic/board-ingenic.c | 71 +++++++++++++++++++++++++++++++ arch/mips/include/asm/bootinfo.h | 1 + arch/mips/ingenic/Kconfig | 12 ++++++ 3 files changed, 84 insertions(+) diff --git a/arch/mips/generic/board-ingenic.c b/arch/mips/generic/board-ingenic.c index 1f4906875e..2a9da0865b 100644 --- a/arch/mips/generic/board-ingenic.c +++ b/arch/mips/generic/board-ingenic.c @@ -5,17 +5,23 @@ * Copyright (C) 2009-2010, Lars-Peter Clausen * Copyright (C) 2011, Maarten ter Huurne * Copyright (C) 2020 Paul Cercueil + * Copyright (C) 2026 Difan Zhang */ +#include #include +#include #include #include #include #include +#include #include +#include #include #include +#include #include #include #include @@ -32,6 +38,8 @@ static __init char *ingenic_get_system_type(unsigned long machtype) return "X2000E"; case MACH_INGENIC_X2000: return "X2000"; + case MACH_INGENIC_T23: + return "T23"; case MACH_INGENIC_X1830: return "X1830"; case MACH_INGENIC_X1000E: @@ -147,6 +155,7 @@ static const struct of_device_id ingenic_of_match[] __initconst = { { .compatible = "ingenic,x1000", .data = (void *)MACH_INGENIC_X1000 }, { .compatible = "ingenic,x1000e", .data = (void *)MACH_INGENIC_X1000E }, { .compatible = "ingenic,x1830", .data = (void *)MACH_INGENIC_X1830 }, + { .compatible = "ingenic,t23", .data = (void *)MACH_INGENIC_T23 }, { .compatible = "ingenic,x2000", .data = (void *)MACH_INGENIC_X2000 }, { .compatible = "ingenic,x2000e", .data = (void *)MACH_INGENIC_X2000E }, { .compatible = "ingenic,x2000h", .data = (void *)MACH_INGENIC_X2000H }, @@ -159,6 +168,64 @@ MIPS_MACHINE(ingenic) = { .fixup_fdt = ingenic_fixup_fdt, }; +#define T23_TCU_BASE_PHYS 0x10002000 +#define T23_CPM_CPPSR 0x34 +#define T23_CPM_CPSPPR 0x38 +#define T23_TCU_TSCR 0x3c +#define T23_WDT_TDR 0x00 +#define T23_WDT_TCER 0x04 +#define T23_WDT_TCNT 0x08 +#define T23_WDT_TCSR 0x0c +#define T23_RECOVERY_SIG 0x001a1a +#define T23_REBOOT_SIG 0x003535 + +static void ingenic_t23_write_cppsr(const char *cmd) +{ + void __iomem *cpm = (void __iomem *)CKSEG1ADDR(INGENIC_CGU_BASE); + u32 sig = (cmd && !strcmp(cmd, "recovery")) ? + T23_RECOVERY_SIG : T23_REBOOT_SIG; + int retries = 1000; + + do { + writel(0x5a5a, cpm + T23_CPM_CPSPPR); + writel(sig, cpm + T23_CPM_CPPSR); + writel(0x0, cpm + T23_CPM_CPSPPR); + if (readl(cpm + T23_CPM_CPPSR) == sig) + break; + udelay(100); + } while (--retries); +} + +static int ingenic_t23_reboot_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + if (action == SYS_RESTART) + ingenic_t23_write_cppsr(data); + return NOTIFY_DONE; +} + +static struct notifier_block ingenic_t23_reboot_nb = { + .notifier_call = ingenic_t23_reboot_notify, +}; + +static void ingenic_t23_restart(char *command) +{ + void __iomem *tcu = (void __iomem *)CKSEG1ADDR(T23_TCU_BASE_PHYS); + + local_irq_disable(); + ingenic_t23_write_cppsr(command); + + writel(BIT(16), tcu + T23_TCU_TSCR); + writel(0, tcu + T23_WDT_TCNT); + writel(4, tcu + T23_WDT_TDR); + writel((3 << 3) | BIT(1), tcu + T23_WDT_TCSR); + writel(0, tcu + T23_WDT_TCER); + writel(1, tcu + T23_WDT_TCER); + mdelay(200); + for (;;) + __asm__(".set push;\n.set mips3;\nwait;\n.set pop;\n"); +} + static void ingenic_wait_instr(void) { __asm__(".set push;\n" @@ -192,6 +259,10 @@ static int __init ingenic_pm_init(void) if (IS_ENABLED(CONFIG_PM_SLEEP)) suspend_set_ops(&ingenic_pm_ops); _machine_halt = ingenic_halt; + if (mips_machtype == MACH_INGENIC_T23) { + register_reboot_notifier(&ingenic_t23_reboot_nb); + _machine_restart = ingenic_t23_restart; + } } return 0; diff --git a/arch/mips/include/asm/bootinfo.h b/arch/mips/include/asm/bootinfo.h index 2128ba9033..85bb51070a 100644 --- a/arch/mips/include/asm/bootinfo.h +++ b/arch/mips/include/asm/bootinfo.h @@ -86,6 +86,7 @@ enum ingenic_machine_type { MACH_INGENIC_X2000E, MACH_INGENIC_X2000H, MACH_INGENIC_X2100, + MACH_INGENIC_T23, }; extern char *system_type; diff --git a/arch/mips/ingenic/Kconfig b/arch/mips/ingenic/Kconfig index f595b339a4..6c115a3c62 100644 --- a/arch/mips/ingenic/Kconfig +++ b/arch/mips/ingenic/Kconfig @@ -9,6 +9,7 @@ config MACH_INGENIC_GENERIC select MACH_JZ4780 select MACH_X1000 select MACH_X1830 + select MACH_T23 choice prompt "Machine type" @@ -43,6 +44,10 @@ config X1830_CU1830_NEO bool "YSH & ATIL CU1830 Module with Neo backplane" select MACH_X1830 +config JZ_T23_JA_A12 + bool "Ingenic T23 IPC board" + select MACH_T23 + endchoice config MACH_JZ4725B @@ -76,3 +81,10 @@ config MACH_X1830 select MIPS_CPU_SCACHE select SYS_HAS_CPU_MIPS32_R2 select SYS_SUPPORTS_HIGHMEM + +config MACH_T23 + bool + select ARCH_HAS_RESET_CONTROLLER + select MIPS_CPU_SCACHE + select SYS_HAS_CPU_MIPS32_R2 + select SYS_SUPPORTS_HIGHMEM From fb45ec1f163dbd8bf27d1ad75b10d9689290de2b Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:34:42 -0800 Subject: [PATCH 04/78] MIPS: XBurst: Invalidate the T23 BTB after instruction cache maintenance Name CP0 Config7 bit 1 (MIPS_CONF7_XBURST_BTB_INV) and restrict the BTB invalidation after I-cache range maintenance to T23 XBurst1 cores so older JZ47xx and XBurst2 parts are unaffected. Signed-off-by: Difan Zhang --- arch/mips/include/asm/mipsregs.h | 2 ++ arch/mips/mm/c-r4k.c | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/arch/mips/include/asm/mipsregs.h b/arch/mips/include/asm/mipsregs.h index db8e02493e..3500c37690 100644 --- a/arch/mips/include/asm/mipsregs.h +++ b/arch/mips/include/asm/mipsregs.h @@ -871,6 +871,8 @@ #define XBURST_PAGECTRL_HPTLB_DIS 0xa9000000 /* Ingenic Config7 bits */ +/* Invalidate Branch Target Buffer on Ingenic XBurst1 cores */ +#define MIPS_CONF7_XBURST_BTB_INV (_ULCAST_(1) << 1) #define MIPS_CONF7_BTB_LOOP_EN (_ULCAST_(1) << 4) /* Config7 Bits specific to MIPS Technologies. */ diff --git a/arch/mips/mm/c-r4k.c b/arch/mips/mm/c-r4k.c index 10413b6f66..64c0cab3c7 100644 --- a/arch/mips/mm/c-r4k.c +++ b/arch/mips/mm/c-r4k.c @@ -673,6 +673,17 @@ static inline void __local_r4k_flush_icache_range(unsigned long start, break; } } + + /* + * Ingenic T23 XBurst1 (MXU2.0, PRID_IMP_XBURST_REV2) requires an + * explicit CP0 Config7 BTB invalidation after I-cache maintenance so + * execution does not branch using stale target predictions. + */ + if (boot_cpu_type() == CPU_XBURST && + (current_cpu_data.processor_id & PRID_IMP_MASK) == + PRID_IMP_XBURST_REV2 && + mips_machtype == MACH_INGENIC_T23) + set_c0_config7(MIPS_CONF7_XBURST_BTB_INV); } static inline void local_r4k_flush_icache_range(unsigned long start, From 841e2c415bde4f5131a6908ec490aad395428314 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:34:42 -0800 Subject: [PATCH 05/78] MIPS: scache: Order T23 DMA cache maintenance and handle large ranges Order secondary-cache maintenance against device accesses and use indexed cache blasts for ranges covering the cache on XBurst T23, leaving other MIPS platforms untouched. Clear the T23 CP0 ErrCtl state and record scache_size only during XBurst MACH_INGENIC_T23 setup. Signed-off-by: Difan Zhang --- arch/mips/mm/sc-mips.c | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/arch/mips/mm/sc-mips.c b/arch/mips/mm/sc-mips.c index 06ec304ad4..4d6f1ba2b3 100644 --- a/arch/mips/mm/sc-mips.c +++ b/arch/mips/mm/sc-mips.c @@ -2,11 +2,13 @@ /* * Copyright (C) 2006 Chris Dearman (chris@mips.com), */ +#include #include #include #include #include +#include #include #include #include @@ -20,12 +22,48 @@ /* * MIPS32/MIPS64 L2 cache handling */ +static unsigned long scache_size __read_mostly; + +static inline bool mips_sc_is_t23(void) +{ + return current_cpu_type() == CPU_XBURST && + mips_machtype == MACH_INGENIC_T23; +} + +static inline void mips_sc_blast(void) +{ + switch (cpu_scache_line_size()) { + case 16: + blast_scache16(); + break; + case 32: + blast_scache32(); + break; + case 64: + blast_scache64(); + break; + case 128: + blast_scache128(); + break; + } +} /* * Writeback and invalidate the secondary cache before DMA. */ static void mips_sc_wback_inv(unsigned long addr, unsigned long size) { + if (mips_sc_is_t23()) { + __sync(); + if (num_online_cpus() == 1 && scache_size && + size >= scache_size) + mips_sc_blast(); + else + blast_scache_range(addr, addr + size); + __fast_iob(); + return; + } + blast_scache_range(addr, addr + size); } @@ -37,6 +75,21 @@ static void mips_sc_inv(unsigned long addr, unsigned long size) unsigned long lsize = cpu_scache_line_size(); unsigned long almask = ~(lsize - 1); + if (mips_sc_is_t23()) { + if (num_online_cpus() == 1 && scache_size && + size >= scache_size) { + __sync(); + mips_sc_blast(); + } else { + cache_op(Hit_Writeback_Inv_SD, addr & almask); + cache_op(Hit_Writeback_Inv_SD, + (addr + size - 1) & almask); + blast_inv_scache_range(addr, addr + size); + } + __fast_iob(); + return; + } + cache_op(Hit_Writeback_Inv_SD, addr & almask); cache_op(Hit_Writeback_Inv_SD, (addr + size - 1) & almask); blast_inv_scache_range(addr, addr + size); @@ -241,11 +294,21 @@ static inline int mips_sc_probe(void) c->scache.sets = 256; c->scache.ways = 4; break; + + /* + * Clear CP0 ErrCtl ($26,0) on T23 so secondary-cache ops + * address normal Tag/Data RAM rather than test mode. + */ + case MACH_INGENIC_T23: + write_c0_ecc(0x0); + break; } } c->scache.waysize = c->scache.sets * c->scache.linesz; c->scache.waybit = __ffs(c->scache.waysize); + if (mips_sc_is_t23()) + scache_size = c->scache.ways * c->scache.waysize; c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT; From 7ff23d5f4b5d53b10e81d246b1ccec3009ad2064 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:45:01 -0800 Subject: [PATCH 06/78] clk: ingenic: Handle T23 divider handshakes and forward clock parents Allow CGU dividers opting into cgu->stop_div_on_disable (T23) to handshake CE/STOP bits around divider enable/disable and rate updates, skip empty sparse clock slots, and resolve forward-referenced parents without altering existing Ingenic CGU SoCs. Signed-off-by: Difan Zhang --- drivers/clk/ingenic/cgu.c | 141 +++++++++++++++++++++++++++++++++++++- drivers/clk/ingenic/cgu.h | 2 + 2 files changed, 141 insertions(+), 2 deletions(-) diff --git a/drivers/clk/ingenic/cgu.c b/drivers/clk/ingenic/cgu.c index 0c9c8344ad..d32e4e3693 100644 --- a/drivers/clk/ingenic/cgu.c +++ b/drivers/clk/ingenic/cgu.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -533,6 +534,8 @@ ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate, int ret = 0; if (clk_info->type & CGU_CLK_DIV) { + bool keep_stopped; + div = ingenic_clk_calc_div(hw, clk_info, parent_rate, req_rate); rate = DIV_ROUND_UP(parent_rate, div); @@ -547,11 +550,21 @@ ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate, spin_lock_irqsave(&cgu->lock, flags); reg = readl(cgu->base + clk_info->div.reg); + keep_stopped = cgu->stop_div_on_disable && + clk_info->div.stop_bit != -1 && + (reg & BIT(clk_info->div.stop_bit)); + /* update the divide */ mask = GENMASK(clk_info->div.bits - 1, 0); reg &= ~(mask << clk_info->div.shift); reg |= hw_div << clk_info->div.shift; + if (keep_stopped) { + writel(reg, cgu->base + clk_info->div.reg); + spin_unlock_irqrestore(&cgu->lock, flags); + return 0; + } + /* clear the stop bit */ if (clk_info->div.stop_bit != -1) reg &= ~BIT(clk_info->div.stop_bit); @@ -567,6 +580,12 @@ ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate, if (clk_info->div.busy_bit != -1) ret = ingenic_clk_check_stable(cgu, clk_info); + if (cgu->stop_div_on_disable && clk_info->div.ce_bit != -1) { + reg = readl(cgu->base + clk_info->div.reg); + reg &= ~BIT(clk_info->div.ce_bit); + writel(reg, cgu->base + clk_info->div.reg); + } + spin_unlock_irqrestore(&cgu->lock, flags); return ret; } @@ -574,6 +593,59 @@ ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate, return -EINVAL; } +/* The caller holds cgu->lock; clocks may start before timekeeping. */ +static int ingenic_cgu_div_wait(struct ingenic_cgu *cgu, + const struct ingenic_cgu_div_info *info) +{ + unsigned int i; + + if (info->busy_bit < 0) + return 0; + for (i = 0; i < 10000; i++) { + if (!(readl(cgu->base + info->reg) & BIT(info->busy_bit))) + return 0; + udelay(1); + } + return -ETIMEDOUT; +} + +static int ingenic_cgu_div_set_enabled(struct ingenic_cgu *cgu, + const struct ingenic_cgu_div_info *info, + bool enable) +{ + u32 reg; + int ret; + + if (info->stop_bit < 0) + return 0; + ret = ingenic_cgu_div_wait(cgu, info); + if (ret) + return ret; + reg = readl(cgu->base + info->reg); + if (!!(reg & BIT(info->stop_bit)) == !enable) + return 0; + if (enable) + reg &= ~BIT(info->stop_bit); + else + reg |= BIT(info->stop_bit); + if (info->ce_bit >= 0) + reg |= BIT(info->ce_bit); + writel(reg, cgu->base + info->reg); + + ret = enable ? ingenic_cgu_div_wait(cgu, info) : 0; + if (ret) { + reg = readl(cgu->base + info->reg); + reg |= BIT(info->stop_bit); + writel(reg, cgu->base + info->reg); + } + if (info->ce_bit >= 0) { + reg = readl(cgu->base + info->reg); + reg &= ~BIT(info->ce_bit); + writel(reg, cgu->base + info->reg); + } + return ret; +} + static int ingenic_clk_enable(struct clk_hw *hw) { struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); @@ -581,6 +653,27 @@ static int ingenic_clk_enable(struct clk_hw *hw) struct ingenic_cgu *cgu = ingenic_clk->cgu; unsigned long flags; + if (cgu->stop_div_on_disable) { + int ret = 0; + + spin_lock_irqsave(&cgu->lock, flags); + if (clk_info->type & CGU_CLK_DIV) { + ret = ingenic_cgu_div_set_enabled(cgu, &clk_info->div, + true); + if (ret) + goto unlock; + } + if (clk_info->type & CGU_CLK_GATE) + ingenic_cgu_gate_set(cgu, &clk_info->gate, false); +unlock: + spin_unlock_irqrestore(&cgu->lock, flags); + if (!ret && (clk_info->type & CGU_CLK_GATE) && + clk_info->gate.delay_us) + udelay(clk_info->gate.delay_us); + + return ret; + } + if (clk_info->type & CGU_CLK_GATE) { /* ungate the clock */ spin_lock_irqsave(&cgu->lock, flags); @@ -601,6 +694,22 @@ static void ingenic_clk_disable(struct clk_hw *hw) struct ingenic_cgu *cgu = ingenic_clk->cgu; unsigned long flags; + if (cgu->stop_div_on_disable) { + int ret = 0; + + spin_lock_irqsave(&cgu->lock, flags); + if (clk_info->type & CGU_CLK_GATE) + ingenic_cgu_gate_set(cgu, &clk_info->gate, true); + if (clk_info->type & CGU_CLK_DIV) + ret = ingenic_cgu_div_set_enabled(cgu, &clk_info->div, + false); + spin_unlock_irqrestore(&cgu->lock, flags); + if (ret) + pr_warn_ratelimited("%s: divider stop timed out\n", + clk_hw_get_name(hw)); + return; + } + if (clk_info->type & CGU_CLK_GATE) { /* gate the clock */ spin_lock_irqsave(&cgu->lock, flags); @@ -616,6 +725,19 @@ static int ingenic_clk_is_enabled(struct clk_hw *hw) struct ingenic_cgu *cgu = ingenic_clk->cgu; int enabled = 1; + if (cgu->stop_div_on_disable) { + unsigned long flags; + + spin_lock_irqsave(&cgu->lock, flags); + if ((clk_info->type & CGU_CLK_DIV) && clk_info->div.stop_bit >= 0) + enabled = !(readl(cgu->base + clk_info->div.reg) & + BIT(clk_info->div.stop_bit)); + if (clk_info->type & CGU_CLK_GATE) + enabled &= !ingenic_cgu_gate_get(cgu, &clk_info->gate); + spin_unlock_irqrestore(&cgu->lock, flags); + return enabled; + } + if (clk_info->type & CGU_CLK_GATE) enabled = !ingenic_cgu_gate_get(cgu, &clk_info->gate); @@ -669,6 +791,8 @@ static int ingenic_register_clock(struct ingenic_cgu *cgu, unsigned idx) } if (!clk_info->type) { + if (cgu->stop_div_on_disable && !clk_info->name) + return 0; pr_err("%s: no clock type specified for '%s'\n", __func__, clk_info->name); goto out; @@ -706,17 +830,30 @@ static int ingenic_register_clock(struct ingenic_cgu *cgu, unsigned idx) num_possible = ARRAY_SIZE(clk_info->parents); for (i = 0; i < num_possible; i++) { - if (clk_info->parents[i] == -1) + int parent_idx = clk_info->parents[i]; + + if (parent_idx == -1) continue; - parent = cgu->clocks.clks[clk_info->parents[i]]; + parent = cgu->clocks.clks[parent_idx]; parent_names[clk_init.num_parents] = + (cgu->stop_div_on_disable && !parent) ? + cgu->clock_info[parent_idx].name : __clk_get_name(parent); clk_init.num_parents++; } BUG_ON(!clk_init.num_parents); BUG_ON(clk_init.num_parents > ARRAY_SIZE(parent_names)); + } else if (cgu->stop_div_on_disable) { + int parent_idx = clk_info->parents[0]; + + if (parent_idx == -1) + goto out; + clk_init.num_parents = 1; + parent = cgu->clocks.clks[parent_idx]; + parent_names[0] = parent ? __clk_get_name(parent) : + cgu->clock_info[parent_idx].name; } else { BUG_ON(clk_info->parents[0] == -1); clk_init.num_parents = 1; diff --git a/drivers/clk/ingenic/cgu.h b/drivers/clk/ingenic/cgu.h index 99da9bd86e..d6d3a2fc55 100644 --- a/drivers/clk/ingenic/cgu.h +++ b/drivers/clk/ingenic/cgu.h @@ -194,6 +194,7 @@ struct ingenic_cgu_clk_info { * @clock_info: an array containing information about implemented clocks * @clocks: used to provide clocks to DT, allows lookup of struct clk* * @lock: lock to be held whilst manipulating CGU registers + * @stop_div_on_disable: stop/start dividers via stop_bit on disable/enable */ struct ingenic_cgu { struct device_node *np; @@ -203,6 +204,7 @@ struct ingenic_cgu { struct clk_onecell_data clocks; spinlock_t lock; + bool stop_div_on_disable; }; /** From 860d4f09e5aa6fc43ce88e2cfcf014b9a3959a4d Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 07/78] dt-bindings: clock: ingenic: Add T23 CGU clocks Describe the T23 clock generator and publish clock identifiers for its CPU, bus, peripheral and audio clocks. Signed-off-by: Difan Zhang --- .../bindings/clock/ingenic,cgu.yaml | 55 +++++++++--- include/dt-bindings/clock/ingenic,t23-cgu.h | 85 +++++++++++++++++++ 2 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 include/dt-bindings/clock/ingenic,t23-cgu.h diff --git a/Documentation/devicetree/bindings/clock/ingenic,cgu.yaml b/Documentation/devicetree/bindings/clock/ingenic,cgu.yaml index 509df06b9c..6e9b98000b 100644 --- a/Documentation/devicetree/bindings/clock/ingenic,cgu.yaml +++ b/Documentation/devicetree/bindings/clock/ingenic,cgu.yaml @@ -29,6 +29,7 @@ select: - ingenic,jz4780-cgu - ingenic,x1000-cgu - ingenic,x1830-cgu + - ingenic,t23-cgu required: - compatible @@ -48,19 +49,25 @@ properties: ranges: true compatible: - items: - - enum: - - ingenic,jz4740-cgu - - ingenic,jz4725b-cgu - - ingenic,jz4755-cgu - - ingenic,jz4760-cgu - - ingenic,jz4760b-cgu - - ingenic,jz4770-cgu - - ingenic,jz4780-cgu - - ingenic,x1000-cgu - - ingenic,x1830-cgu - - const: simple-mfd - minItems: 1 + oneOf: + - items: + - enum: + - ingenic,jz4740-cgu + - ingenic,jz4725b-cgu + - ingenic,jz4755-cgu + - ingenic,jz4760-cgu + - ingenic,jz4760b-cgu + - ingenic,jz4770-cgu + - ingenic,jz4780-cgu + - ingenic,x1000-cgu + - ingenic,x1830-cgu + - ingenic,t23-cgu + - const: simple-mfd + minItems: 1 + - items: + - const: ingenic,t23-cgu + - const: syscon + - const: simple-mfd reg: maxItems: 1 @@ -89,6 +96,9 @@ properties: minItems: 1 maxItems: 64 + reset-controller: + $ref: /schemas/reset/ingenic,t23-reset.yaml# + required: - "#clock-cells" - compatible @@ -100,7 +110,24 @@ patternProperties: "^usb-phy@[a-f0-9]+$": $ref: /schemas/phy/ingenic,phy-usb.yaml# "^mac-phy-ctrl@[a-f0-9]+$": - $ref: /schemas/net/ingenic,mac.yaml# + type: object + +allOf: + - if: + properties: + compatible: + contains: + const: ingenic,t23-cgu + then: + patternProperties: + "^mac-phy-ctrl@[a-f0-9]+$": + $ref: /schemas/mfd/syscon.yaml# + else: + properties: + reset-controller: false + patternProperties: + "^mac-phy-ctrl@[a-f0-9]+$": + $ref: /schemas/net/ingenic,mac.yaml# additionalProperties: false diff --git a/include/dt-bindings/clock/ingenic,t23-cgu.h b/include/dt-bindings/clock/ingenic,t23-cgu.h new file mode 100644 index 0000000000..47a8fe1582 --- /dev/null +++ b/include/dt-bindings/clock/ingenic,t23-cgu.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ +/* + * Device Tree clock bindings for Ingenic T23 SoC + * Copyright (C) 2026 Difan Zhang + */ + +#ifndef __DT_BINDINGS_CLOCK_INGENIC_T23_CGU_H__ +#define __DT_BINDINGS_CLOCK_INGENIC_T23_CGU_H__ + +/* Fixed / Root Clocks */ +#define CLK_EXT 0 +#define CLK_RTC 1 +#define CLK_APLL 2 +#define CLK_MPLL 3 + +/* CPCCR Core Dividers */ +#define CLK_SCLKA 4 +#define CLK_CCLK 5 +#define CLK_L2CLK 6 +#define CLK_H0CLK 7 +#define CLK_H2CLK 8 +#define CLK_PCLK 9 + +/* Peripheral Gates (CLKGR - Bank 0) */ +#define CLK_EFUSE 10 +#define CLK_OTG 11 +#define CLK_MSC0 12 +#define CLK_MSC1 13 +#define CLK_SSI0 14 +#define CLK_I2C0 15 +#define CLK_I2C1 16 +#define CLK_I2C2 17 +#define CLK_AIC 18 +#define CLK_SADC 19 +#define CLK_UART0 20 +#define CLK_UART1 21 +#define CLK_UART2 22 +#define CLK_HASH 23 +#define CLK_SFC 24 +#define CLK_PDMA 25 +#define CLK_ISP 26 +#define CLK_LCD 27 +#define CLK_CSI 28 +#define CLK_RISCV 29 +#define CLK_RSA 30 +#define CLK_DES 31 +#define CLK_TCU 32 +#define CLK_DDR 33 + +/* Peripheral Gates (CLKGR1 - Bank 1) */ +#define CLK_AVPU 34 +#define CLK_DTRNG 35 +#define CLK_IPU 36 +#define CLK_IVDC 37 +#define CLK_GMAC 38 +#define CLK_AES 39 +#define CLK_AHB0 40 +#define CLK_SYS_OST 41 +#define CLK_APB0 42 +#define CLK_CPU 43 +#define CLK_MSC 44 + +/* Composite Peripheral Clocks (CDRs) */ +#define CLK_CGU_MSC_MUX 45 +#define CLK_CGU_ISP 46 +#define CLK_CGU_CIM 47 +#define CLK_CGU_SSI 48 +#define CLK_CGU_SFC 49 +#define CLK_CGU_I2S_SPK 50 +#define CLK_CGU_I2S_MIC 51 +#define CLK_CGU_MSC1 52 +#define CLK_CGU_MSC0 53 +#define CLK_CGU_LPC 54 +#define CLK_CGU_VPU 55 +#define CLK_CGU_DDR 56 +#define CLK_CGU_MACPHY 57 +#define CLK_CGU_RSA 58 + +/* Core Muxes (internal to CGU) */ +#define CLK_CPUMUX 59 +#define CLK_AHB2PMUX 60 + +#define CLK_NR_CLKS 61 + +#endif /* __DT_BINDINGS_CLOCK_INGENIC_T23_CGU_H__ */ From b1c10d5fc70f1efa19cffd10dddc84b20d820e44 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:45:01 -0800 Subject: [PATCH 08/78] clk: ingenic: Add the T23 clock generator Add the T23 APLL and MPLL, clock muxes, dividers and peripheral gates. Provide fractional I2S dividers for the microphone and speaker clocks. Signed-off-by: Difan Zhang --- drivers/clk/ingenic/Kconfig | 10 + drivers/clk/ingenic/Makefile | 1 + drivers/clk/ingenic/t23-cgu.c | 622 ++++++++++++++++++++++++++++++++++ 3 files changed, 633 insertions(+) create mode 100644 drivers/clk/ingenic/t23-cgu.c diff --git a/drivers/clk/ingenic/Kconfig b/drivers/clk/ingenic/Kconfig index f80ac4f299..90b52ebcf8 100644 --- a/drivers/clk/ingenic/Kconfig +++ b/drivers/clk/ingenic/Kconfig @@ -85,6 +85,16 @@ config INGENIC_CGU_X1830 If building for a X1830 SoC, you want to say Y here. +config INGENIC_CGU_T23 + bool "Ingenic T23 CGU driver" + default MACH_T23 + select INGENIC_CGU_COMMON + help + Support the clocks provided by the CGU hardware on Ingenic T23 + and compatible SoCs. + + If building for a T23 SoC, you want to say Y here. + config INGENIC_TCU_CLK bool "Ingenic JZ47xx TCU clocks driver" default MACH_INGENIC diff --git a/drivers/clk/ingenic/Makefile b/drivers/clk/ingenic/Makefile index 81d8e23c26..df38eecb12 100644 --- a/drivers/clk/ingenic/Makefile +++ b/drivers/clk/ingenic/Makefile @@ -8,4 +8,5 @@ obj-$(CONFIG_INGENIC_CGU_JZ4770) += jz4770-cgu.o obj-$(CONFIG_INGENIC_CGU_JZ4780) += jz4780-cgu.o obj-$(CONFIG_INGENIC_CGU_X1000) += x1000-cgu.o obj-$(CONFIG_INGENIC_CGU_X1830) += x1830-cgu.o +obj-$(CONFIG_INGENIC_CGU_T23) += t23-cgu.o obj-$(CONFIG_INGENIC_TCU_CLK) += tcu.o diff --git a/drivers/clk/ingenic/t23-cgu.c b/drivers/clk/ingenic/t23-cgu.c new file mode 100644 index 0000000000..67ff6a8b13 --- /dev/null +++ b/drivers/clk/ingenic/t23-cgu.c @@ -0,0 +1,622 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 SoC CGU driver + * + * Copyright (C) 2019 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include + +#include + +#include "cgu.h" +#include "pm.h" + +/* CGU CPM register offsets */ +#define CPM_CPCCR 0x00 +#define CPM_CPAPCR 0x10 +#define CPM_CPMPCR 0x14 +#define CPM_CLKGR 0x20 +#define CPM_CLKGR1 0x28 +#define CPM_DDRCDR 0x2c +#define CPM_VPUCDR 0x30 +#define CPM_RSACDR 0x4c +#define CPM_MACCDR 0x54 +#define CPM_SSICDR 0x60 +#define CPM_LPCDR 0x64 +#define CPM_MSC0CDR 0x68 +#define CPM_I2SSPKCDR 0x70 +#define CPM_SFCCDR 0x74 +#define CPM_CIMCDR 0x7c +#define CPM_ISPCDR 0x80 +#define CPM_I2SMICCDR 0x84 +#define CPM_MSC1CDR 0xa4 +#define CPM_CPCSR 0xd4 + +static struct ingenic_cgu *cgu; + +static u32 t23_frac_to_value(u32 frac) +{ + u64 t = (u64)(frac & 0xffffff) * 125; + + return (u32)((t + 0x100000) >> 21); +} + +static unsigned long +t23_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = ingenic_clk->idx == CLK_APLL ? + CPM_CPAPCR : CPM_CPMPCR; + u32 cpxpcr = readl(cgu_ptr->base + reg); + unsigned long m, n, od1, od0, frac, frac_value; + unsigned long rate; + + if (!(cpxpcr & BIT(0))) + return 0; + + m = (cpxpcr >> 20) & 0xfff; + n = (cpxpcr >> 14) & 0x3f; + od1 = (cpxpcr >> 11) & 0x07; + od0 = (cpxpcr >> 8) & 0x07; + + if (!parent_rate || !n || !od1 || !od0) + return 0; + + frac = readl(cgu_ptr->base + reg + 8); + frac_value = t23_frac_to_value(frac); + + rate = ((parent_rate / 4000) * m / n / od1 / od0) * 4000 + + ((parent_rate / 4000) * frac_value / n / od1 / od0) * 4; + + return rate; +} + +static int t23_pll_is_enabled(struct clk_hw *hw) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = ingenic_clk->idx == CLK_APLL ? + CPM_CPAPCR : CPM_CPMPCR; + + return !!(readl(cgu_ptr->base + reg) & BIT(0)); +} + +static int t23_pll_enable(struct clk_hw *hw) +{ + return 0; +} + +static void t23_pll_disable(struct clk_hw *hw) +{ +} + +static const struct clk_ops t23_pll_ops = { + .recalc_rate = t23_pll_recalc_rate, + .is_enabled = t23_pll_is_enabled, + .enable = t23_pll_enable, + .disable = t23_pll_disable, +}; + +static unsigned long t23_i2s_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + u32 val = readl(cgu_ptr->base + reg); + u32 m = (val >> 20) & 0x1ff; + u32 n = val & 0xfffff; + + if (!n || !parent_rate) + return 0; + + return div_u64((u64)parent_rate * m, n); +} + +static u8 t23_i2s_get_parent(struct clk_hw *hw) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + u32 val = readl(cgu_ptr->base + reg); + + return (val >> 30) & 1; +} + +static int t23_i2s_set_parent(struct clk_hw *hw, u8 index) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + unsigned long flags; + u32 val; + + if (index > 1) + return -EINVAL; + + spin_lock_irqsave(&cgu_ptr->lock, flags); + val = readl(cgu_ptr->base + reg) & ~GENMASK(31, 30); + val |= (u32)index << 30; + writel(val, cgu_ptr->base + reg); + spin_unlock_irqrestore(&cgu_ptr->lock, flags); + + return 0; +} + +static int t23_i2s_enable(struct clk_hw *hw) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + unsigned long flags; + u32 val; + + spin_lock_irqsave(&cgu_ptr->lock, flags); + val = readl(cgu_ptr->base + reg); + val |= BIT(29); + writel(val, cgu_ptr->base + reg); + spin_unlock_irqrestore(&cgu_ptr->lock, flags); + + return 0; +} + +static void t23_i2s_disable(struct clk_hw *hw) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + unsigned long flags; + u32 val; + + spin_lock_irqsave(&cgu_ptr->lock, flags); + val = readl(cgu_ptr->base + reg); + val &= ~BIT(29); + writel(val, cgu_ptr->base + reg); + spin_unlock_irqrestore(&cgu_ptr->lock, flags); +} + +static int t23_i2s_is_enabled(struct clk_hw *hw) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + + return !!(readl(cgu_ptr->base + reg) & BIT(29)); +} + +static int t23_i2s_determine_rate(struct clk_hw *hw, + struct clk_rate_request *req) +{ + if (!req->best_parent_rate || !req->rate) + req->rate = 0; + return 0; +} + +static int t23_i2s_set_rate(struct clk_hw *hw, unsigned long req_rate, + unsigned long parent_rate) +{ + struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw); + struct ingenic_cgu *cgu_ptr = ingenic_clk->cgu; + unsigned int reg = (ingenic_clk->idx == CLK_CGU_I2S_SPK) ? + CPM_I2SSPKCDR : CPM_I2SMICCDR; + unsigned long flags; + u64 m, n, m_mul, tmp_val, diff, min_diff = ~0ULL; + u32 best_m = 1, best_n = 1; + u32 val; + + if (!parent_rate || !req_rate) + return -EINVAL; + + for (m = 1; m <= 0x1ff; m++) { + m_mul = (u64)parent_rate * m; + n = div64_u64(m_mul + (req_rate / 2), req_rate); + if (!n) + n = 1; + if (n > 0xfffff) + continue; + tmp_val = div64_u64(m_mul, n); + if (tmp_val == req_rate) { + best_m = m; + best_n = n; + break; + } + diff = tmp_val > req_rate ? tmp_val - req_rate : + req_rate - tmp_val; + if (diff < min_diff) { + min_diff = diff; + best_m = m; + best_n = n; + } + } + + spin_lock_irqsave(&cgu_ptr->lock, flags); + val = readl(cgu_ptr->base + reg) & 0xe0000000; + val |= (best_m << 20) | (best_n & 0xfffff); + writel(val, cgu_ptr->base + reg); + spin_unlock_irqrestore(&cgu_ptr->lock, flags); + + return 0; +} + +static const struct clk_ops t23_i2s_ops = { + .get_parent = t23_i2s_get_parent, + .set_parent = t23_i2s_set_parent, + .recalc_rate = t23_i2s_recalc_rate, + .determine_rate = t23_i2s_determine_rate, + .set_rate = t23_i2s_set_rate, + .enable = t23_i2s_enable, + .disable = t23_i2s_disable, + .is_enabled = t23_i2s_is_enabled, +}; + +static const struct ingenic_cgu_clk_info t23_cgu_clocks[] = { + /* External root clocks */ + [CLK_EXT] = { "ext", CGU_CLK_EXT }, + [CLK_RTC] = { "rtc", CGU_CLK_EXT }, + + /* PLLs (read-only from bootloader) */ + [CLK_APLL] = { + "apll", CGU_CLK_CUSTOM, + .parents = { CLK_EXT, -1, -1, -1 }, + .custom = { &t23_pll_ops }, + }, + [CLK_MPLL] = { + "mpll", CGU_CLK_CUSTOM, + .parents = { CLK_EXT, -1, -1, -1 }, + .custom = { &t23_pll_ops }, + }, + + /* Core Muxes */ + [CLK_SCLKA] = { + "sclka", CGU_CLK_MUX, + .parents = { -1, CLK_EXT, CLK_APLL, -1 }, + .mux = { CPM_CPCCR, 30, 2 }, + }, + [CLK_CPUMUX] = { + "cpu_mux", CGU_CLK_MUX, + .parents = { -1, CLK_SCLKA, CLK_MPLL, -1 }, + .mux = { CPM_CPCCR, 28, 2 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_AHB2PMUX] = { + "ahb2_apb_mux", CGU_CLK_MUX, + .parents = { -1, CLK_SCLKA, CLK_MPLL, -1 }, + .mux = { CPM_CPCCR, 24, 2 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + + /* Core Dividers (CPCCR) */ + [CLK_CCLK] = { + "cclk", CGU_CLK_DIV, + .parents = { CLK_CPUMUX, -1, -1, -1 }, + .div = { CPM_CPCCR, 0, 1, 4, 22, -1, -1 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_L2CLK] = { + "l2clk", CGU_CLK_DIV, + .parents = { CLK_CPUMUX, -1, -1, -1 }, + .div = { CPM_CPCCR, 4, 1, 4, 22, -1, -1 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_H0CLK] = { + "h0clk", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { -1, CLK_SCLKA, CLK_MPLL, -1 }, + .mux = { CPM_CPCCR, 26, 2 }, + .div = { CPM_CPCCR, 8, 1, 4, 21, -1, -1 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_H2CLK] = { + "h2clk", CGU_CLK_DIV, + .parents = { CLK_AHB2PMUX, -1, -1, -1 }, + .div = { CPM_CPCCR, 12, 1, 4, 20, -1, -1 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_PCLK] = { + "pclk", CGU_CLK_DIV, + .parents = { CLK_AHB2PMUX, -1, -1, -1 }, + .div = { CPM_CPCCR, 16, 1, 4, 20, -1, -1 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + + /* Peripheral Gates (CLKGR - Bank 0) */ + [CLK_EFUSE] = { + "efuse", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 1 }, + }, + [CLK_OTG] = { + "otg", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 3 }, + }, + [CLK_MSC0] = { + "msc0", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 4 }, + }, + [CLK_MSC1] = { + "msc1", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 5 }, + }, + [CLK_SSI0] = { + "ssi0", CGU_CLK_GATE, + .parents = { CLK_CGU_SSI, -1, -1, -1 }, + .gate = { CPM_CLKGR, 6 }, + }, + [CLK_I2C0] = { + "i2c0", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 7 }, + }, + [CLK_I2C1] = { + "i2c1", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 8 }, + }, + [CLK_I2C2] = { + "i2c2", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 9 }, + }, + [CLK_AIC] = { + "aic", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 11 }, + }, + [CLK_SADC] = { + "sadc", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 13 }, + }, + [CLK_UART0] = { + "uart0", CGU_CLK_GATE, + .parents = { CLK_EXT, -1, -1, -1 }, + .gate = { CPM_CLKGR, 14 }, + }, + [CLK_UART1] = { + "uart1", CGU_CLK_GATE, + .parents = { CLK_EXT, -1, -1, -1 }, + .gate = { CPM_CLKGR, 15 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_UART2] = { + "uart2", CGU_CLK_GATE, + .parents = { CLK_EXT, -1, -1, -1 }, + .gate = { CPM_CLKGR, 16 }, + }, + [CLK_HASH] = { + "hash", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 18 }, + }, + [CLK_SFC] = { + "sfc", CGU_CLK_GATE, + .parents = { CLK_CGU_SFC, -1, -1, -1 }, + .gate = { CPM_CLKGR, 20 }, + }, + [CLK_PDMA] = { + "pdma", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 21 }, + }, + [CLK_ISP] = { + "isp", CGU_CLK_GATE, + .parents = { CLK_CGU_ISP, -1, -1, -1 }, + .gate = { CPM_CLKGR, 23 }, + }, + [CLK_LCD] = { + "lcd", CGU_CLK_GATE, + .parents = { CLK_H0CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 24 }, + }, + [CLK_CSI] = { + "csi", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 25 }, + }, + [CLK_RISCV] = { + "riscv", CGU_CLK_GATE, + .parents = { CLK_L2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 26 }, + }, + [CLK_RSA] = { + "rsa", CGU_CLK_GATE, + .parents = { CLK_CGU_RSA, -1, -1, -1 }, + .gate = { CPM_CLKGR, 27 }, + }, + [CLK_DES] = { + "des", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 28 }, + }, + [CLK_TCU] = { + "tcu", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR, 30 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_DDR] = { + "ddr", CGU_CLK_GATE, + .parents = { CLK_CGU_DDR, -1, -1, -1 }, + .gate = { CPM_CLKGR, 31 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + + /* Peripheral Gates (CLKGR1 - Bank 1) */ + [CLK_AVPU] = { + "avpu", CGU_CLK_GATE, + .parents = { CLK_CGU_VPU, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 0 }, + }, + [CLK_DTRNG] = { + "dtrng", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 1 }, + }, + [CLK_IPU] = { + "ipu", CGU_CLK_GATE, + .parents = { CLK_H0CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 2 }, + }, + [CLK_IVDC] = { + "ivdc", CGU_CLK_GATE, + .parents = { CLK_CGU_VPU, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 3 }, + }, + [CLK_GMAC] = { + "gmac", CGU_CLK_GATE, + .parents = { CLK_CGU_MACPHY, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 4 }, + }, + [CLK_AES] = { + "aes", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 5 }, + }, + [CLK_AHB0] = { + "ahb0_gate", CGU_CLK_GATE, + .parents = { CLK_H0CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 10 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_SYS_OST] = { + "sys_ost", CGU_CLK_GATE, + .parents = { CLK_EXT, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 11 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_APB0] = { + "apb0_gate", CGU_CLK_GATE, + .parents = { CLK_PCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 14 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_CPU] = { + "cpu_gate", CGU_CLK_GATE, + .parents = { CLK_CCLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 15 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_MSC] = { + "msc", CGU_CLK_GATE, + .parents = { CLK_H2CLK, -1, -1, -1 }, + .gate = { CPM_CLKGR1, 16 }, + }, + + /* Composite Peripheral Clocks (CDRs) */ + [CLK_CGU_ISP] = { + "cgu_isp", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_ISPCDR, 30, 2 }, + .div = { CPM_ISPCDR, 0, 1, 4, 29, 28, 27 }, + }, + [CLK_CGU_CIM] = { + "cgu_cim", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_CIMCDR, 30, 2 }, + .div = { CPM_CIMCDR, 0, 1, 8, 29, 28, 27 }, + }, + [CLK_CGU_SSI] = { + "cgu_ssi", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_SSICDR, 30, 2 }, + .div = { CPM_SSICDR, 0, 1, 8, 28, 27, 26 }, + }, + [CLK_CGU_SFC] = { + "cgu_sfc", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_SFCCDR, 30, 2 }, + .div = { CPM_SFCCDR, 0, 1, 8, 28, 27, 26 }, + }, + [CLK_CGU_I2S_SPK] = { + "cgu_i2s_spk", CGU_CLK_CUSTOM, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .custom = { &t23_i2s_ops }, + }, + [CLK_CGU_I2S_MIC] = { + "cgu_i2s_mic", CGU_CLK_CUSTOM, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .custom = { &t23_i2s_ops }, + }, + [CLK_CGU_MSC1] = { + "cgu_msc1", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_MSC1CDR, 30, 2 }, + .div = { CPM_MSC1CDR, 0, 2, 8, 29, 28, 27 }, + }, + [CLK_CGU_MSC0] = { + "cgu_msc0", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_MSC0CDR, 30, 2 }, + .div = { CPM_MSC0CDR, 0, 2, 8, 29, 28, 27 }, + }, + [CLK_CGU_LPC] = { + "cgu_lpc", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_LPCDR, 30, 2 }, + .div = { CPM_LPCDR, 0, 1, 8, 28, 27, 26 }, + }, + [CLK_CGU_VPU] = { + "cgu_vpu", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_VPUCDR, 30, 2 }, + .div = { CPM_VPUCDR, 0, 1, 4, 29, 28, 27 }, + }, + [CLK_CGU_DDR] = { + "cgu_ddr", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { -1, CLK_SCLKA, CLK_MPLL, -1 }, + .mux = { CPM_DDRCDR, 30, 2 }, + .div = { CPM_DDRCDR, 0, 1, 4, 29, 28, 27 }, + .flags = CLK_IS_CRITICAL | CLK_IGNORE_UNUSED, + }, + [CLK_CGU_MACPHY] = { + "cgu_macphy", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_MACCDR, 30, 2 }, + .div = { CPM_MACCDR, 0, 1, 8, 29, 28, 27 }, + }, + [CLK_CGU_RSA] = { + "cgu_rsa", CGU_CLK_MUX | CGU_CLK_DIV, + .parents = { CLK_SCLKA, CLK_MPLL, -1, -1 }, + .mux = { CPM_RSACDR, 30, 2 }, + .div = { CPM_RSACDR, 0, 1, 4, 29, 28, 27 }, + }, +}; + +static void __init t23_cgu_init(struct device_node *np) +{ + int retval; + + cgu = ingenic_cgu_new(t23_cgu_clocks, + ARRAY_SIZE(t23_cgu_clocks), np); + if (!cgu) { + pr_err("%s: failed to initialise CGU\n", __func__); + return; + } + + cgu->stop_div_on_disable = true; + + retval = ingenic_cgu_register_clocks(cgu); + if (retval) { + pr_err("%s: failed to register CGU clocks\n", __func__); + return; + } + + ingenic_cgu_register_syscore(cgu); +} + +CLK_OF_DECLARE_DRIVER(t23_cgu, "ingenic,t23-cgu", t23_cgu_init); From 572aa30222eed9ce4f57abef9fd4bea3da114553 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 09/78] dt-bindings: reset: Add the Ingenic T23 bus-stop controller Describe the CPM bus-stop reset controller and define reset identifiers for the ISP and Helix VPU. Signed-off-by: Difan Zhang --- .../bindings/reset/ingenic,t23-reset.yaml | 27 +++++++++++++++++++ include/dt-bindings/reset/ingenic,t23-reset.h | 9 +++++++ 2 files changed, 36 insertions(+) create mode 100644 Documentation/devicetree/bindings/reset/ingenic,t23-reset.yaml create mode 100644 include/dt-bindings/reset/ingenic,t23-reset.h diff --git a/Documentation/devicetree/bindings/reset/ingenic,t23-reset.yaml b/Documentation/devicetree/bindings/reset/ingenic,t23-reset.yaml new file mode 100644 index 0000000000..b88c9f84ec --- /dev/null +++ b/Documentation/devicetree/bindings/reset/ingenic,t23-reset.yaml @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/reset/ingenic,t23-reset.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 CPM Bus-Stop Reset Controller + +maintainers: + - Paul Cercueil + +description: + Child reset controller of the Ingenic T23 CGU/CPM syscon driving SRBC + bus-stop handshakes and soft resets for ISP and Helix VPU blocks. + +properties: + compatible: + const: ingenic,t23-reset + + "#reset-cells": + const: 1 + +required: + - compatible + - "#reset-cells" + +additionalProperties: false diff --git a/include/dt-bindings/reset/ingenic,t23-reset.h b/include/dt-bindings/reset/ingenic,t23-reset.h new file mode 100644 index 0000000000..01e80c55af --- /dev/null +++ b/include/dt-bindings/reset/ingenic,t23-reset.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __DT_BINDINGS_RESET_INGENIC_T23_H__ +#define __DT_BINDINGS_RESET_INGENIC_T23_H__ + +#define T23_RESET_ISP 0 +#define T23_RESET_HELIX 1 + +#endif From 3309d91c45f114fbd6cd0d122a29d2840893b5b4 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:45:03 -0800 Subject: [PATCH 10/78] reset: Add Ingenic T23 bus-stop handshakes Stop bus traffic before asserting ISP or VPU reset and wait for the CPM acknowledgement before releasing the block. Signed-off-by: Difan Zhang --- drivers/reset/Kconfig | 10 +++ drivers/reset/Makefile | 1 + drivers/reset/reset-ingenic-t23.c | 143 ++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 drivers/reset/reset-ingenic-t23.c diff --git a/drivers/reset/Kconfig b/drivers/reset/Kconfig index 380b7e87fc..28ec5b88ac 100644 --- a/drivers/reset/Kconfig +++ b/drivers/reset/Kconfig @@ -113,6 +113,16 @@ config RESET_IMX8MP_AUDIOMIX help This enables the reset controller driver for i.MX8MP AudioMix +config RESET_INGENIC_T23 + bool "Ingenic T23 CPM reset controller" + depends on OF && HAS_IOMEM + depends on MACH_T23 || COMPILE_TEST + default MACH_T23 + select MFD_SYSCON + help + Reset the Ingenic T23 ISP and Helix VPU through the CPM SRBC + bus-stop handshake. + config RESET_INTEL_GW bool "Intel Reset Controller Driver" depends on X86 || COMPILE_TEST diff --git a/drivers/reset/Makefile b/drivers/reset/Makefile index a757da46c1..1f426b87af 100644 --- a/drivers/reset/Makefile +++ b/drivers/reset/Makefile @@ -16,6 +16,7 @@ obj-$(CONFIG_RESET_GPIO) += reset-gpio.o obj-$(CONFIG_RESET_HSDK) += reset-hsdk.o obj-$(CONFIG_RESET_IMX7) += reset-imx7.o obj-$(CONFIG_RESET_IMX8MP_AUDIOMIX) += reset-imx8mp-audiomix.o +obj-$(CONFIG_RESET_INGENIC_T23) += reset-ingenic-t23.o obj-$(CONFIG_RESET_INTEL_GW) += reset-intel-gw.o obj-$(CONFIG_RESET_K210) += reset-k210.o obj-$(CONFIG_RESET_LANTIQ) += reset-lantiq.o diff --git a/drivers/reset/reset-ingenic-t23.c b/drivers/reset/reset-ingenic-t23.c new file mode 100644 index 0000000000..fa70480c3d --- /dev/null +++ b/drivers/reset/reset-ingenic-t23.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 CPM reset controller + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define T23_SRBC 0xc4 +#define T23_ISP_ACK BIT(20) +#define T23_ISP_STOP BIT(21) +#define T23_ISP_RESET BIT(22) +#define T23_HELIX_ACK BIT(29) +#define T23_HELIX_STOP BIT(30) +#define T23_HELIX_RESET BIT(31) + +struct t23_reset { + struct reset_controller_dev rcdev; + struct regmap *map; + struct mutex lock; +}; + +static int t23_helix_reset(struct t23_reset *reset) +{ + unsigned int saved, value; + unsigned int mask = T23_HELIX_STOP | T23_HELIX_RESET; + int ret, restore_ret; + + ret = regmap_read(reset->map, T23_SRBC, &saved); + if (ret) + return ret; + ret = regmap_write_bits(reset->map, T23_SRBC, + T23_HELIX_STOP, T23_HELIX_STOP); + if (ret) + goto restore; + ret = regmap_read_poll_timeout(reset->map, T23_SRBC, value, + value & T23_HELIX_ACK, 20, 655340); + if (ret) + goto restore; + ret = regmap_write_bits(reset->map, T23_SRBC, mask, + (saved & T23_HELIX_STOP) | T23_HELIX_RESET); +restore: + restore_ret = regmap_write_bits(reset->map, T23_SRBC, mask, saved & mask); + return ret ? ret : restore_ret; +} + +static int t23_reset_reset(struct reset_controller_dev *rcdev, + unsigned long id) +{ + struct t23_reset *reset = container_of(rcdev, struct t23_reset, rcdev); + unsigned int value, i; + int ret; + + if (id != T23_RESET_ISP && id != T23_RESET_HELIX) + return -EINVAL; + mutex_lock(&reset->lock); + if (id == T23_RESET_HELIX) { + ret = t23_helix_reset(reset); + goto unlock; + } + ret = regmap_write_bits(reset->map, T23_SRBC, T23_ISP_STOP, + T23_ISP_STOP); + if (ret) + goto unlock; + /* Stop new bus requests before pulsing the ISP reset. */ + for (i = 0; i < 500; i++) { + ret = regmap_read(reset->map, T23_SRBC, &value); + if (ret) + goto release_stop; + if (value & T23_ISP_ACK) + break; + msleep(2); + } + if (i == 500) { + ret = -ETIMEDOUT; + goto release_stop; + } + ret = regmap_write_bits(reset->map, T23_SRBC, + T23_ISP_STOP | T23_ISP_RESET, T23_ISP_RESET); + if (ret) + goto release_stop; + ret = regmap_write_bits(reset->map, T23_SRBC, T23_ISP_RESET, 0); + goto unlock; + +release_stop: + regmap_write_bits(reset->map, T23_SRBC, T23_ISP_STOP, 0); +unlock: + mutex_unlock(&reset->lock); + return ret; +} + +static const struct reset_control_ops t23_reset_ops = { + .reset = t23_reset_reset, +}; + +static int t23_reset_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *parent; + struct t23_reset *reset; + + reset = devm_kzalloc(dev, sizeof(*reset), GFP_KERNEL); + if (!reset) + return -ENOMEM; + parent = of_get_parent(dev->of_node); + reset->map = syscon_node_to_regmap(parent); + of_node_put(parent); + if (IS_ERR(reset->map)) + return PTR_ERR(reset->map); + mutex_init(&reset->lock); + reset->rcdev.owner = THIS_MODULE; + reset->rcdev.ops = &t23_reset_ops; + reset->rcdev.of_node = dev->of_node; + reset->rcdev.nr_resets = 2; + return devm_reset_controller_register(dev, &reset->rcdev); +} + +static const struct of_device_id t23_reset_of_match[] = { + { .compatible = "ingenic,t23-reset" }, + { } +}; +MODULE_DEVICE_TABLE(of, t23_reset_of_match); + +static struct platform_driver t23_reset_driver = { + .probe = t23_reset_probe, + .driver = { + .name = "ingenic-t23-reset", + .of_match_table = t23_reset_of_match, + }, +}; +module_platform_driver(t23_reset_driver); + +MODULE_DESCRIPTION("Ingenic T23 CPM reset controller"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); From d7a377ba4a08e83db49a86999c6c46375c98931f Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 11/78] dt-bindings: pinctrl: ingenic: Add T23 GPIO banks Describe the T23 pin controller and its GPIO-bank compatibles. Signed-off-by: Difan Zhang --- Documentation/devicetree/bindings/pinctrl/ingenic,pinctrl.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/pinctrl/ingenic,pinctrl.yaml b/Documentation/devicetree/bindings/pinctrl/ingenic,pinctrl.yaml index 890961826c..98316b7f1e 100644 --- a/Documentation/devicetree/bindings/pinctrl/ingenic,pinctrl.yaml +++ b/Documentation/devicetree/bindings/pinctrl/ingenic,pinctrl.yaml @@ -43,6 +43,7 @@ properties: - ingenic,x1000-pinctrl - ingenic,x1500-pinctrl - ingenic,x1830-pinctrl + - ingenic,t23-pinctrl - ingenic,x2000-pinctrl - ingenic,x2100-pinctrl - items: @@ -82,6 +83,7 @@ patternProperties: - ingenic,x1000-gpio - ingenic,x1500-gpio - ingenic,x1830-gpio + - ingenic,t23-gpio - ingenic,x2000-gpio - ingenic,x2100-gpio From 8f74a2c98cb0725487600f60cc5484283c82f0b9 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:58:27 -0800 Subject: [PATCH 12/78] pinctrl: ingenic: Add T23 pinmux and pull bias support Add the T23 PA-PD pin groups and functions, 0x1000 bank stride, shadow register access, and per-pin pull-up and pull-down configuration. Signed-off-by: Difan Zhang --- drivers/pinctrl/pinctrl-ingenic.c | 168 ++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/drivers/pinctrl/pinctrl-ingenic.c b/drivers/pinctrl/pinctrl-ingenic.c index 3170373773..e4d7014a9a 100644 --- a/drivers/pinctrl/pinctrl-ingenic.c +++ b/drivers/pinctrl/pinctrl-ingenic.c @@ -63,6 +63,9 @@ #define X1830_GPIO_SR 0x150 #define X1830_GPIO_SMT 0x160 +#define T23_GPIO_PUEN 0x110 +#define T23_GPIO_PDEN 0x120 + #define X2000_GPIO_EDG 0x70 #define X2000_GPIO_PEPU 0x80 #define X2000_GPIO_PEPD 0x90 @@ -113,6 +116,7 @@ enum jz_version { ID_X1000, ID_X1500, ID_X1830, + ID_T23, ID_X2000, ID_X2100, }; @@ -163,6 +167,7 @@ static const unsigned long enabled_socs = IS_ENABLED(CONFIG_MACH_X1000) << ID_X1000 | IS_ENABLED(CONFIG_MACH_X1500) << ID_X1500 | IS_ENABLED(CONFIG_MACH_X1830) << ID_X1830 | + IS_ENABLED(CONFIG_MACH_T23) << ID_T23 | IS_ENABLED(CONFIG_MACH_X2000) << ID_X2000 | IS_ENABLED(CONFIG_MACH_X2100) << ID_X2100; @@ -2578,6 +2583,137 @@ static const struct ingenic_chip_info x1830_chip_info = { .access_table = &x1830_access_table, }; +static const u32 t23_pull_ups[4] = { + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, +}; + +static const u32 t23_pull_downs[4] = { + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, +}; + +static int t23_uart0_data_pins[] = { 0x33, 0x36, }; +static int t23_uart0_hwflow_pins[] = { 0x34, 0x35, }; +static int t23_uart1_data_a_pins[] = { 0x06, 0x07, }; +static int t23_uart1_data_b_pins[] = { 0x37, 0x38, }; +static int t23_uart2_data_b_pins[] = { 0x31, 0x32, }; +static int t23_uart2_data_c_pins[] = { 0x4d, 0x4e, }; +static int t23_sfc_data_pins[] = { 0x17, 0x18, 0x1b, 0x1c, }; +static int t23_sfc_quad_pins[] = { 0x2f, 0x30, }; +static int t23_mmc0_1bit_pins[] = { 0x24, 0x25, 0x20, }; +static int t23_mmc0_4bit_pins[] = { 0x21, 0x22, 0x23, }; +static int t23_mmc1_1bit_c_pins[] = { 0x42, 0x43, 0x44, }; +static int t23_mmc1_4bit_c_pins[] = { 0x45, 0x46, 0x47, }; +static int t23_i2c0_data_pins[] = { 0x0c, 0x0d, }; +static int t23_cim_mclk_pins[] = { 0x0f, }; +static int t23_i2c1_data_a_pins[] = { 0x10, 0x11, }; +static int t23_i2c1_data_b_pins[] = { 0x39, 0x3a, }; +static int t23_i2c1_data_c_pins[] = { 0x48, 0x49, }; +static int t23_i2c2_data_b_pins[] = { 0x34, 0x35, }; +static int t23_ssi0_data_b_pins[] = { 0x2f, 0x30, 0x31, 0x32, }; +static int t23_i2s_pins[] = { 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, }; +static int t23_dmic_pins[] = { 0x3c, 0x3d, 0x3e, }; +static int t23_pwm0_b_pins[] = { 0x31, }; +static int t23_pwm1_b_pins[] = { 0x32, }; +static int t23_pwm2_c_pins[] = { 0x48, }; +static int t23_pwm3_c_pins[] = { 0x49, }; +static int t23_mac_rmii_pins[] = { + 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2d, 0x2e, 0x2f, 0x30, +}; +static int t23_vic_switch_pins[] = { 0x07, }; + +static const struct group_desc t23_groups[] = { + INGENIC_PIN_GROUP("uart0-data", t23_uart0_data, 0), + INGENIC_PIN_GROUP("uart0-hwflow", t23_uart0_hwflow, 0), + INGENIC_PIN_GROUP("uart1-data-a", t23_uart1_data_a, 2), + INGENIC_PIN_GROUP("uart1-data-b", t23_uart1_data_b, 0), + INGENIC_PIN_GROUP("uart1-data", t23_uart1_data_b, 0), + INGENIC_PIN_GROUP("uart2-data-b", t23_uart2_data_b, 2), + INGENIC_PIN_GROUP("uart2-data-c", t23_uart2_data_c, 2), + INGENIC_PIN_GROUP("sfc-data", t23_sfc_data, 1), + INGENIC_PIN_GROUP("sfc-quad", t23_sfc_quad, 2), + INGENIC_PIN_GROUP("mmc0-1bit", t23_mmc0_1bit, 0), + INGENIC_PIN_GROUP("mmc0-4bit", t23_mmc0_4bit, 0), + INGENIC_PIN_GROUP("mmc1-1bit-c", t23_mmc1_1bit_c, 0), + INGENIC_PIN_GROUP("mmc1-4bit-c", t23_mmc1_4bit_c, 0), + INGENIC_PIN_GROUP("i2c0-data", t23_i2c0_data, 1), + INGENIC_PIN_GROUP("cim-mclk", t23_cim_mclk, 1), + INGENIC_PIN_GROUP("i2c1-data-a", t23_i2c1_data_a, 2), + INGENIC_PIN_GROUP("i2c1-data-b", t23_i2c1_data_b, 0), + INGENIC_PIN_GROUP("i2c1-data", t23_i2c1_data_b, 0), + INGENIC_PIN_GROUP("i2c1-data-c", t23_i2c1_data_c, 3), + INGENIC_PIN_GROUP("i2c2-data-b", t23_i2c2_data_b, 2), + INGENIC_PIN_GROUP("i2c2-data", t23_i2c2_data_b, 2), + INGENIC_PIN_GROUP("ssi0-data-b", t23_ssi0_data_b, 1), + INGENIC_PIN_GROUP("i2s-data", t23_i2s, 0), + INGENIC_PIN_GROUP("dmic-data", t23_dmic, 3), + INGENIC_PIN_GROUP("pwm0-b", t23_pwm0_b, 0), + INGENIC_PIN_GROUP("pwm1-b", t23_pwm1_b, 0), + INGENIC_PIN_GROUP("pwm2-c", t23_pwm2_c, 0), + INGENIC_PIN_GROUP("pwm3-c", t23_pwm3_c, 0), + INGENIC_PIN_GROUP("mac-rmii", t23_mac_rmii, 0), + INGENIC_PIN_GROUP("mac", t23_mac_rmii, 0), + INGENIC_PIN_GROUP("vic-switch", t23_vic_switch, 0), +}; + +static const char * const t23_uart0_groups[] = { "uart0-data", "uart0-hwflow", }; +static const char * const t23_uart1_groups[] = { + "uart1-data-a", "uart1-data-b", "uart1-data", +}; +static const char * const t23_uart2_groups[] = { "uart2-data-b", "uart2-data-c", }; +static const char * const t23_sfc_groups[] = { "sfc-data", "sfc-quad", }; +static const char * const t23_mmc0_groups[] = { "mmc0-1bit", "mmc0-4bit", }; +static const char * const t23_mmc1_groups[] = { "mmc1-1bit-c", "mmc1-4bit-c", }; +static const char * const t23_i2c0_groups[] = { "i2c0-data", }; +static const char * const t23_cim_groups[] = { "cim-mclk", }; +static const char * const t23_i2c1_groups[] = { + "i2c1-data-a", "i2c1-data-b", "i2c1-data", "i2c1-data-c", +}; +static const char * const t23_i2c2_groups[] = { "i2c2-data-b", "i2c2-data", }; +static const char * const t23_ssi0_groups[] = { "ssi0-data-b", }; +static const char * const t23_i2s_groups[] = { "i2s-data", }; +static const char * const t23_dmic_groups[] = { "dmic-data", }; +static const char * const t23_pwm0_groups[] = { "pwm0-b", }; +static const char * const t23_pwm1_groups[] = { "pwm1-b", }; +static const char * const t23_pwm2_groups[] = { "pwm2-c", }; +static const char * const t23_pwm3_groups[] = { "pwm3-c", }; +static const char * const t23_mac_groups[] = { "mac-rmii", "mac", }; +static const char * const t23_vic_groups[] = { "vic-switch", }; + +static const struct function_desc t23_functions[] = { + INGENIC_PIN_FUNCTION("uart0", t23_uart0), + INGENIC_PIN_FUNCTION("uart1", t23_uart1), + INGENIC_PIN_FUNCTION("uart2", t23_uart2), + INGENIC_PIN_FUNCTION("sfc", t23_sfc), + INGENIC_PIN_FUNCTION("mmc0", t23_mmc0), + INGENIC_PIN_FUNCTION("mmc1", t23_mmc1), + INGENIC_PIN_FUNCTION("i2c0", t23_i2c0), + INGENIC_PIN_FUNCTION("cim", t23_cim), + INGENIC_PIN_FUNCTION("i2c1", t23_i2c1), + INGENIC_PIN_FUNCTION("i2c2", t23_i2c2), + INGENIC_PIN_FUNCTION("ssi0", t23_ssi0), + INGENIC_PIN_FUNCTION("i2s", t23_i2s), + INGENIC_PIN_FUNCTION("dmic", t23_dmic), + INGENIC_PIN_FUNCTION("pwm0", t23_pwm0), + INGENIC_PIN_FUNCTION("pwm1", t23_pwm1), + INGENIC_PIN_FUNCTION("pwm2", t23_pwm2), + INGENIC_PIN_FUNCTION("pwm3", t23_pwm3), + INGENIC_PIN_FUNCTION("mac", t23_mac), + INGENIC_PIN_FUNCTION("vic", t23_vic), +}; + +static const struct ingenic_chip_info t23_chip_info = { + .num_chips = 4, + .reg_offset = 0x1000, + .version = ID_T23, + .groups = t23_groups, + .num_groups = ARRAY_SIZE(t23_groups), + .functions = t23_functions, + .num_functions = ARRAY_SIZE(t23_functions), + .pull_ups = t23_pull_ups, + .pull_downs = t23_pull_downs, + .access_table = &x1830_access_table, +}; + static const u32 x2000_pull_ups[5] = { 0x0003ffff, 0xffffffff, 0x1ff0ffff, 0xc7fe3f3f, 0x8fff003f, }; @@ -3843,6 +3979,14 @@ static int ingenic_pinconf_get(struct pinctrl_dev *pctldev, !ingenic_get_pin_config(jzpc, pin, X2000_GPIO_PEPU) && (jzpc->info->pull_downs[offt] & BIT(idx)); + } else if (jzpc->info->version == ID_T23) { + pullup = ingenic_get_pin_config(jzpc, pin, T23_GPIO_PUEN) && + !ingenic_get_pin_config(jzpc, pin, T23_GPIO_PDEN) && + (jzpc->info->pull_ups[offt] & BIT(idx)); + pulldown = ingenic_get_pin_config(jzpc, pin, T23_GPIO_PDEN) && + !ingenic_get_pin_config(jzpc, pin, T23_GPIO_PUEN) && + (jzpc->info->pull_downs[offt] & BIT(idx)); + } else if (is_soc_or_above(jzpc, ID_X1830)) { unsigned int half = PINS_PER_GPIO_CHIP / 2; unsigned int idxh = (pin % half) * 2; @@ -3941,6 +4085,23 @@ static void ingenic_set_bias(struct ingenic_pinctrl *jzpc, ingenic_config_pin(jzpc, pin, X2000_GPIO_PEPD, false); } + } else if (jzpc->info->version == ID_T23) { + switch (bias) { + case GPIO_PULL_UP: + ingenic_config_pin(jzpc, pin, T23_GPIO_PDEN, false); + ingenic_config_pin(jzpc, pin, T23_GPIO_PUEN, true); + break; + case GPIO_PULL_DOWN: + ingenic_config_pin(jzpc, pin, T23_GPIO_PUEN, false); + ingenic_config_pin(jzpc, pin, T23_GPIO_PDEN, true); + break; + case GPIO_PULL_DIS: + default: + ingenic_config_pin(jzpc, pin, T23_GPIO_PUEN, false); + ingenic_config_pin(jzpc, pin, T23_GPIO_PDEN, false); + break; + } + } else if (is_soc_or_above(jzpc, ID_X1830)) { unsigned int idx = pin % PINS_PER_GPIO_CHIP; unsigned int half = PINS_PER_GPIO_CHIP / 2; @@ -4151,6 +4312,7 @@ static const struct of_device_id ingenic_gpio_of_matches[] __initconst = { { .compatible = "ingenic,jz4780-gpio" }, { .compatible = "ingenic,x1000-gpio" }, { .compatible = "ingenic,x1830-gpio" }, + { .compatible = "ingenic,t23-gpio" }, { .compatible = "ingenic,x2000-gpio" }, { .compatible = "ingenic,x2100-gpio" }, {}, @@ -4260,6 +4422,8 @@ static int __init ingenic_pinctrl_probe(struct platform_device *pdev) if (chip_info->access_table) { regmap_config.rd_table = chip_info->access_table; regmap_config.wr_table = chip_info->access_table; + if (chip_info->version == ID_T23) + regmap_config.max_register = 0x8000 - 4; } else { regmap_config.max_register = chip_info->num_chips * chip_info->reg_offset - 4; } @@ -4401,6 +4565,10 @@ static const struct of_device_id ingenic_pinctrl_of_matches[] = { .compatible = "ingenic,x1830-pinctrl", .data = IF_ENABLED(CONFIG_MACH_X1830, &x1830_chip_info) }, + { + .compatible = "ingenic,t23-pinctrl", + .data = IF_ENABLED(CONFIG_MACH_T23, &t23_chip_info) + }, { .compatible = "ingenic,x2000-pinctrl", .data = IF_ENABLED(CONFIG_MACH_X2000, &x2000_chip_info) From b1c1e1b4be87ca3c62a057f10a9c7503fecf3c05 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 13/78] dt-bindings: interrupt-controller: ingenic: Add T23 INTC Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../bindings/interrupt-controller/ingenic,intc.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/interrupt-controller/ingenic,intc.yaml b/Documentation/devicetree/bindings/interrupt-controller/ingenic,intc.yaml index 609308a5f9..df7413eb89 100644 --- a/Documentation/devicetree/bindings/interrupt-controller/ingenic,intc.yaml +++ b/Documentation/devicetree/bindings/interrupt-controller/ingenic,intc.yaml @@ -26,7 +26,9 @@ properties: - ingenic,jz4760b-intc - const: ingenic,jz4760-intc - items: - - const: ingenic,x1000-intc + - enum: + - ingenic,x1000-intc + - ingenic,t23-intc - const: ingenic,jz4780-intc - items: - const: ingenic,jz4725b-intc From 9085761d21422561d019f6c4d28a37c6c1b6e824 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 14/78] irqchip/ingenic: Add the T23 interrupt controller Match the T23 two-bank interrupt controller to the existing Ingenic INTC implementation. Signed-off-by: Difan Zhang --- drivers/irqchip/irq-ingenic.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/irqchip/irq-ingenic.c b/drivers/irqchip/irq-ingenic.c index cee839ca62..ec15a83d38 100644 --- a/drivers/irqchip/irq-ingenic.c +++ b/drivers/irqchip/irq-ingenic.c @@ -158,3 +158,4 @@ IRQCHIP_DECLARE(jz4760_intc, "ingenic,jz4760-intc", intc_2chip_of_init); IRQCHIP_DECLARE(jz4770_intc, "ingenic,jz4770-intc", intc_2chip_of_init); IRQCHIP_DECLARE(jz4775_intc, "ingenic,jz4775-intc", intc_2chip_of_init); IRQCHIP_DECLARE(jz4780_intc, "ingenic,jz4780-intc", intc_2chip_of_init); +IRQCHIP_DECLARE(t23_intc, "ingenic,t23-intc", intc_2chip_of_init); From 8f92baae85160c65ca0e6073e09a7943020e5346 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 15/78] dt-bindings: timer: ingenic: Add T23 system OST Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../devicetree/bindings/timer/ingenic,sysost.yaml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/timer/ingenic,sysost.yaml b/Documentation/devicetree/bindings/timer/ingenic,sysost.yaml index bdc82d8bce..efd51c7ba3 100644 --- a/Documentation/devicetree/bindings/timer/ingenic,sysost.yaml +++ b/Documentation/devicetree/bindings/timer/ingenic,sysost.yaml @@ -18,9 +18,14 @@ properties: const: 1 compatible: - enum: - - ingenic,x1000-ost - - ingenic,x2000-ost + oneOf: + - enum: + - ingenic,x1000-ost + - ingenic,t23-ost + - ingenic,x2000-ost + - items: + - const: ingenic,t23-ost + - const: ingenic,x1000-ost reg: maxItems: 1 From d941d27f928aa0fcce4ac5dc50d5067235a2951b Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 16/78] clocksource/drivers/ingenic: Add T23 system OST Register the ingenic,t23-ost compatible in ingenic-sysost and clear OST_REG_OSTFR in the event IRQ handler only on T23. Signed-off-by: Difan Zhang --- drivers/clocksource/ingenic-sysost.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/clocksource/ingenic-sysost.c b/drivers/clocksource/ingenic-sysost.c index cb6fc2f152..73702026c6 100644 --- a/drivers/clocksource/ingenic-sysost.c +++ b/drivers/clocksource/ingenic-sysost.c @@ -56,6 +56,7 @@ struct ingenic_soc_info { unsigned int num_channels; + bool clear_fflag; }; struct ingenic_ost_clk_info { @@ -260,6 +261,8 @@ static irqreturn_t ingenic_ost_cevt_cb(int irq, void *dev_id) struct ingenic_ost *ost = to_ingenic_ost(evt); writel(OSTECR_OST1ENC, ost->base + OST_REG_OSTECR); + if (ost->soc_info->clear_fflag) + writel((u32)~OSTFR_FFLAG, ost->base + OST_REG_OSTFR); if (evt->event_handler) evt->event_handler(evt); @@ -415,8 +418,14 @@ static const struct ingenic_soc_info x1000_soc_info = { .num_channels = 2, }; +static const struct ingenic_soc_info t23_soc_info = { + .num_channels = 2, + .clear_fflag = true, +}; + static const struct of_device_id __maybe_unused ingenic_ost_of_matches[] __initconst = { { .compatible = "ingenic,x1000-ost", .data = &x1000_soc_info }, + { .compatible = "ingenic,t23-ost", .data = &t23_soc_info }, { /* sentinel */ } }; @@ -538,3 +547,4 @@ static int __init ingenic_ost_init(struct device_node *np) } TIMER_OF_DECLARE(x1000_ost, "ingenic,x1000-ost", ingenic_ost_init); +TIMER_OF_DECLARE(t23_ost, "ingenic,t23-ost", ingenic_ost_init); From 93a8d9171039bfa38d6e69beefa4fb7c461a1567 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 17/78] dt-bindings: timer: ingenic: Add the T23 TCU Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../bindings/timer/ingenic,tcu.yaml | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/timer/ingenic,tcu.yaml b/Documentation/devicetree/bindings/timer/ingenic,tcu.yaml index 585b5f5217..7cb604ac34 100644 --- a/Documentation/devicetree/bindings/timer/ingenic,tcu.yaml +++ b/Documentation/devicetree/bindings/timer/ingenic,tcu.yaml @@ -25,6 +25,7 @@ select: - ingenic,jz4770-tcu - ingenic,jz4780-tcu - ingenic,x1000-tcu + - ingenic,t23-tcu required: - compatible @@ -56,6 +57,12 @@ properties: - ingenic,jz4725b-tcu - ingenic,jz4760-tcu - ingenic,x1000-tcu + - ingenic,t23-tcu + - const: simple-mfd + - items: + - const: ingenic,t23-tcu + - const: ingenic,x1000-tcu + - const: syscon - const: simple-mfd - items: - enum: @@ -122,12 +129,14 @@ patternProperties: - enum: - ingenic,jz4740-watchdog - ingenic,jz4780-watchdog + - ingenic,t23-watchdog - items: - enum: - ingenic,jz4770-watchdog - ingenic,jz4760b-watchdog - ingenic,jz4760-watchdog - ingenic,jz4725b-watchdog + - ingenic,t23-watchdog - const: ingenic,jz4740-watchdog reg: @@ -157,6 +166,10 @@ patternProperties: - ingenic,jz4740-pwm - ingenic,jz4725b-pwm - ingenic,x1000-pwm + - ingenic,t23-pwm + - items: + - const: ingenic,t23-pwm + - const: ingenic,x1000-pwm - items: - enum: - ingenic,jz4760-pwm @@ -169,7 +182,7 @@ patternProperties: maxItems: 1 clocks: - minItems: 6 + minItems: 4 maxItems: 8 clock-names: @@ -182,7 +195,7 @@ patternProperties: - const: timer5 - const: timer6 - const: timer7 - minItems: 6 + minItems: 4 required: - compatible @@ -190,6 +203,20 @@ patternProperties: - clocks - clock-names + allOf: + - if: + not: + properties: + compatible: + contains: + const: ingenic,t23-pwm + then: + properties: + clocks: + minItems: 6 + clock-names: + minItems: 6 + "^timer@[a-f0-9]+$": type: object properties: From d11dfec21d7c30022cb94bf38fcb7dc871fbbd31 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 18/78] watchdog: jz4740: Add the T23 watchdog compatible Match the T23 TCU watchdog to the existing Ingenic watchdog operations. Signed-off-by: Difan Zhang --- drivers/watchdog/jz4740_wdt.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/watchdog/jz4740_wdt.c b/drivers/watchdog/jz4740_wdt.c index 395bde79e2..d8386bb74f 100644 --- a/drivers/watchdog/jz4740_wdt.c +++ b/drivers/watchdog/jz4740_wdt.c @@ -129,6 +129,7 @@ static const struct watchdog_ops jz4740_wdt_ops = { static const struct of_device_id jz4740_wdt_of_matches[] = { { .compatible = "ingenic,jz4740-watchdog", }, { .compatible = "ingenic,jz4780-watchdog", }, + { .compatible = "ingenic,t23-watchdog", }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches); From db35552e578969993df75daed3fb1cc26bba0c6e Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 19/78] pwm: jz4740: Add the four-channel T23 controller Describe the four PWM channels available on T23 and match its TCU PWM compatible. Signed-off-by: Difan Zhang --- drivers/pwm/pwm-jz4740.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/pwm/pwm-jz4740.c b/drivers/pwm/pwm-jz4740.c index 6bdb016193..b18ade53b4 100644 --- a/drivers/pwm/pwm-jz4740.c +++ b/drivers/pwm/pwm-jz4740.c @@ -258,10 +258,15 @@ static const struct soc_info x1000_soc_info = { .num_pwms = 5, }; +static const struct soc_info t23_soc_info = { + .num_pwms = 4, +}; + static const struct of_device_id jz4740_pwm_dt_ids[] = { { .compatible = "ingenic,jz4740-pwm", .data = &jz4740_soc_info }, { .compatible = "ingenic,jz4725b-pwm", .data = &jz4725b_soc_info }, { .compatible = "ingenic,x1000-pwm", .data = &x1000_soc_info }, + { .compatible = "ingenic,t23-pwm", .data = &t23_soc_info }, {}, }; MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids); From 8ad0d140bfb975563eed955774a9ed85a77a203a Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 20/78] dt-bindings: serial: ingenic: Add the T23 UART Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../devicetree/bindings/serial/ingenic,uart.yaml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/serial/ingenic,uart.yaml b/Documentation/devicetree/bindings/serial/ingenic,uart.yaml index d5f153bdeb..a4e71dd35a 100644 --- a/Documentation/devicetree/bindings/serial/ingenic,uart.yaml +++ b/Documentation/devicetree/bindings/serial/ingenic,uart.yaml @@ -11,6 +11,16 @@ maintainers: allOf: - $ref: serial.yaml# + - if: + not: + properties: + compatible: + contains: + const: ingenic,t23-uart + then: + required: + - dmas + - dma-names properties: $nodename: @@ -24,6 +34,10 @@ properties: - ingenic,jz4760-uart - ingenic,jz4780-uart - ingenic,x1000-uart + - ingenic,t23-uart + - items: + - const: ingenic,t23-uart + - const: ingenic,x1000-uart - items: - enum: - ingenic,jz4770-uart @@ -68,8 +82,6 @@ required: - interrupts - clocks - clock-names - - dmas - - dma-names unevaluatedProperties: false From f994bc87e99ec1677261ba83df5fe8fb8ab7b5cd Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 21/78] serial: 8250_ingenic: Add T23 UART and early console support Register the ingenic,t23-uart compatible and a T23-specific earlycon handler falling back to a 24 MHz EXT clock when /ext is absent or left at BASE_BAUD * 16. Signed-off-by: Difan Zhang --- drivers/tty/serial/8250/8250_ingenic.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/tty/serial/8250/8250_ingenic.c b/drivers/tty/serial/8250/8250_ingenic.c index a2783e38a2..39451a208e 100644 --- a/drivers/tty/serial/8250/8250_ingenic.c +++ b/drivers/tty/serial/8250/8250_ingenic.c @@ -18,6 +18,8 @@ #include #include +#include + #include "8250.h" /** ingenic_uart_config: SOC specific config data. */ @@ -165,6 +167,20 @@ OF_EARLYCON_DECLARE(jz4775_uart, "ingenic,jz4775-uart", OF_EARLYCON_DECLARE(jz4780_uart, "ingenic,jz4780-uart", ingenic_early_console_setup); +static int __init t23_early_console_setup(struct earlycon_device *dev, + const char *opt) +{ + if (initial_boot_params) + ingenic_early_console_setup_clock(dev); + if (!dev->port.uartclk || dev->port.uartclk == BASE_BAUD * 16) + dev->port.uartclk = 24000000; + + return ingenic_earlycon_setup_tail(dev, opt); +} + +OF_EARLYCON_DECLARE(t23_uart, "ingenic,t23-uart", + t23_early_console_setup); + OF_EARLYCON_DECLARE(x1000_uart, "ingenic,x1000-uart", ingenic_early_console_setup); @@ -351,6 +367,7 @@ static const struct of_device_id of_match[] = { { .compatible = "ingenic,jz4775-uart", .data = &jz4760_uart_config }, { .compatible = "ingenic,jz4780-uart", .data = &jz4780_uart_config }, { .compatible = "ingenic,x1000-uart", .data = &x1000_uart_config }, + { .compatible = "ingenic,t23-uart", .data = &x1000_uart_config }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, of_match); From 32b78743546c8f13a63ac4a130dfb31cac9afc8d Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 22/78] dt-bindings: dma: ingenic: Add the T23 PDMA controller Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- Documentation/devicetree/bindings/dma/ingenic,dma.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/devicetree/bindings/dma/ingenic,dma.yaml b/Documentation/devicetree/bindings/dma/ingenic,dma.yaml index d9cca3006e..4f92f10ae3 100644 --- a/Documentation/devicetree/bindings/dma/ingenic,dma.yaml +++ b/Documentation/devicetree/bindings/dma/ingenic,dma.yaml @@ -29,6 +29,10 @@ properties: - ingenic,jz4780-dma - ingenic,x1000-dma - ingenic,x1830-dma + - ingenic,t23-dma + - items: + - const: ingenic,t23-dma + - const: ingenic,x1830-dma - items: - const: ingenic,jz4770-bdma - const: ingenic,jz4760b-bdma From 8d109acacd1d63f6b4c1cacbc7e570bfebc65a47 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 23/78] dmaengine: jz4780: Add T23 PDMA support Register the ingenic,t23-dma compatible with T23-specific descriptor cleanup and channel-lock handling, leaving existing Ingenic DMA controllers untouched. Signed-off-by: Difan Zhang --- drivers/dma/dma-jz4780.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/dma/dma-jz4780.c b/drivers/dma/dma-jz4780.c index c9cfa341db..ae3f4d4c70 100644 --- a/drivers/dma/dma-jz4780.c +++ b/drivers/dma/dma-jz4780.c @@ -93,6 +93,7 @@ #define JZ_SOC_DATA_PER_CHAN_PM BIT(2) #define JZ_SOC_DATA_NO_DCKES_DCKEC BIT(3) #define JZ_SOC_DATA_BREAK_LINKS BIT(4) +#define JZ_SOC_DATA_T23 BIT(5) /** * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller. @@ -379,7 +380,10 @@ static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg( sg_dma_len(&sgl[i]), direction); if (err < 0) { - jz4780_dma_desc_free(&jzchan->desc->vdesc); + if (jzdma->soc_data->flags & JZ_SOC_DATA_T23) + jz4780_dma_desc_free(&desc->vdesc); + else + jz4780_dma_desc_free(&jzchan->desc->vdesc); return NULL; } @@ -409,6 +413,7 @@ static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic( unsigned long flags) { struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan); + struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan); struct jz4780_dma_desc *desc; unsigned int periods, i; int err; @@ -426,7 +431,10 @@ static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic( err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr, period_len, direction); if (err < 0) { - jz4780_dma_desc_free(&jzchan->desc->vdesc); + if (jzdma->soc_data->flags & JZ_SOC_DATA_T23) + jz4780_dma_desc_free(&desc->vdesc); + else + jz4780_dma_desc_free(&jzchan->desc->vdesc); return NULL; } @@ -642,6 +650,7 @@ static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie, struct dma_tx_state *txstate) { struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan); + struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan); struct virt_dma_desc *vdesc; enum dma_status status; unsigned long flags; @@ -658,7 +667,9 @@ static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan, /* On the issued list, so hasn't been processed yet */ residue = jz4780_dma_desc_residue(jzchan, to_jz4780_dma_desc(vdesc), 0); - } else if (cookie == jzchan->desc->vdesc.tx.cookie) { + } else if ((!(jzdma->soc_data->flags & JZ_SOC_DATA_T23) || + jzchan->desc) && + cookie == jzchan->desc->vdesc.tx.cookie) { residue = jz4780_dma_desc_residue(jzchan, jzchan->desc, jzchan->curr_hwdesc + 1); } @@ -682,6 +693,8 @@ static bool jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma, bool ack = true; spin_lock(&jzchan->vchan.lock); + if (soc_flags & JZ_SOC_DATA_T23) + desc = jzchan->desc; dcs = jz4780_dma_chn_readl(jzdma, jzchan->id, JZ_DMA_REG_DCS); jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0); @@ -1102,6 +1115,12 @@ static const struct jz4780_dma_soc_data x1830_dma_soc_data = { .flags = JZ_SOC_DATA_PROGRAMMABLE_DMA, }; +static const struct jz4780_dma_soc_data t23_dma_soc_data = { + .nb_channels = 32, + .transfer_ord_max = 7, + .flags = JZ_SOC_DATA_PROGRAMMABLE_DMA | JZ_SOC_DATA_T23, +}; + static const struct of_device_id jz4780_dma_dt_match[] = { { .compatible = "ingenic,jz4740-dma", .data = &jz4740_dma_soc_data }, { .compatible = "ingenic,jz4725b-dma", .data = &jz4725b_dma_soc_data }, @@ -1116,6 +1135,7 @@ static const struct of_device_id jz4780_dma_dt_match[] = { { .compatible = "ingenic,jz4780-dma", .data = &jz4780_dma_soc_data }, { .compatible = "ingenic,x1000-dma", .data = &x1000_dma_soc_data }, { .compatible = "ingenic,x1830-dma", .data = &x1830_dma_soc_data }, + { .compatible = "ingenic,t23-dma", .data = &t23_dma_soc_data }, {}, }; MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match); From 1d78852b9978f19408583c2d866a5668197d8ebb Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 24/78] dt-bindings: i2c: ingenic: Add the T23 I2C controller Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../devicetree/bindings/i2c/ingenic,i2c.yaml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/i2c/ingenic,i2c.yaml b/Documentation/devicetree/bindings/i2c/ingenic,i2c.yaml index b61fdc9548..ab52d868d2 100644 --- a/Documentation/devicetree/bindings/i2c/ingenic,i2c.yaml +++ b/Documentation/devicetree/bindings/i2c/ingenic,i2c.yaml @@ -11,6 +11,16 @@ maintainers: allOf: - $ref: /schemas/i2c/i2c-controller.yaml# + - if: + not: + properties: + compatible: + contains: + const: ingenic,t23-i2c + then: + required: + - dmas + - dma-names properties: $nodename: @@ -21,6 +31,10 @@ properties: - enum: - ingenic,jz4770-i2c - ingenic,x1000-i2c + - ingenic,t23-i2c + - items: + - const: ingenic,t23-i2c + - const: ingenic,x1000-i2c - items: - const: ingenic,jz4780-i2c - const: ingenic,jz4770-i2c @@ -34,6 +48,9 @@ properties: clocks: maxItems: 1 + clock-names: + const: i2c + clock-frequency: enum: [ 100000, 400000 ] @@ -53,8 +70,6 @@ required: - interrupts - clocks - clock-frequency - - dmas - - dma-names unevaluatedProperties: false From 15a135a81f3a0f06c48b99c166160340bb393ecf Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 25/78] i2c: jz4780: Add T23 controller support Register the ingenic,t23-i2c compatible and scope read command count guards and repeated-start stop_hold handling to ID_T23 so earlier Ingenic I2C controllers run unchanged. Signed-off-by: Difan Zhang --- drivers/i2c/busses/i2c-jz4780.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/busses/i2c-jz4780.c b/drivers/i2c/busses/i2c-jz4780.c index 1bf25f575f..aef3b14409 100644 --- a/drivers/i2c/busses/i2c-jz4780.c +++ b/drivers/i2c/busses/i2c-jz4780.c @@ -126,6 +126,7 @@ enum ingenic_i2c_version { ID_JZ4780, ID_X1000, + ID_T23, }; /* ingenic_i2c_config: SoC specific config data. */ @@ -416,10 +417,14 @@ static void jz4780_i2c_send_rcmd(struct jz4780_i2c *i2c, { int i; + if (i2c->cdata->version >= ID_T23 && cmd_count <= 0) + return; + for (i = 0; i < cmd_count - 1; i++) jz4780_i2c_writew(i2c, JZ4780_I2C_DC, JZ4780_I2C_DC_READ); - if ((cmd_left == 0) && (i2c->cdata->version >= ID_X1000)) + if ((cmd_left == 0) && (i2c->cdata->version >= ID_X1000) && + (i2c->cdata->version < ID_T23 || !i2c->stop_hold)) jz4780_i2c_writew(i2c, JZ4780_I2C_DC, JZ4780_I2C_DC_READ | X1000_I2C_DC_STOP); else @@ -493,7 +498,8 @@ static irqreturn_t jz4780_i2c_irq(int irqno, void *dev_id) i2c->cdata->fifosize - i2c->cdata->tx_level - 1); - if (cmd_to_send) { + if (i2c->cdata->version >= ID_T23 ? + cmd_to_send > 0 : cmd_to_send) { i2c->rd_cmd_xfered += cmd_to_send; cmd_left = i2c->rd_total_len - i2c->rd_cmd_xfered; @@ -751,10 +757,19 @@ static const struct ingenic_i2c_config x1000_i2c_config = { .rx_level = X1000_I2C_FIFO_LEN / 2 - 1, }; +static const struct ingenic_i2c_config t23_i2c_config = { + .version = ID_T23, + + .fifosize = X1000_I2C_FIFO_LEN, + .tx_level = X1000_I2C_FIFO_LEN / 2, + .rx_level = X1000_I2C_FIFO_LEN / 2 - 1, +}; + static const struct of_device_id jz4780_i2c_of_matches[] = { { .compatible = "ingenic,jz4770-i2c", .data = &jz4780_i2c_config }, { .compatible = "ingenic,jz4780-i2c", .data = &jz4780_i2c_config }, { .compatible = "ingenic,x1000-i2c", .data = &x1000_i2c_config }, + { .compatible = "ingenic,t23-i2c", .data = &t23_i2c_config }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, jz4780_i2c_of_matches); From 1330074db20ef1b7282dd52c503fbaf5860d7f3b Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 26/78] dt-bindings: spi: ingenic: Add the T23 SSI controller Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- Documentation/devicetree/bindings/spi/ingenic,spi.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/devicetree/bindings/spi/ingenic,spi.yaml b/Documentation/devicetree/bindings/spi/ingenic,spi.yaml index c08d55b900..2039283270 100644 --- a/Documentation/devicetree/bindings/spi/ingenic,spi.yaml +++ b/Documentation/devicetree/bindings/spi/ingenic,spi.yaml @@ -21,7 +21,11 @@ properties: - ingenic,jz4775-spi - ingenic,jz4780-spi - ingenic,x1000-spi + - ingenic,t23-spi - ingenic,x2000-spi + - items: + - const: ingenic,t23-spi + - const: ingenic,x1000-spi - items: - enum: - ingenic,jz4760-spi From 30b8cdc099aade065f6759befe9e77ea6e395fa7 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 27/78] spi: ingenic: Add the T23 SSI compatible Bind the T23 SSI controller to the existing X1000 SPI controller operations. Signed-off-by: Difan Zhang --- drivers/spi/spi-ingenic.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/spi/spi-ingenic.c b/drivers/spi/spi-ingenic.c index 318b076870..67bce75cee 100644 --- a/drivers/spi/spi-ingenic.c +++ b/drivers/spi/spi-ingenic.c @@ -501,6 +501,7 @@ static const struct of_device_id spi_ingenic_of_match[] = { { .compatible = "ingenic,jz4775-spi", .data = &jz4780_soc_info }, { .compatible = "ingenic,jz4780-spi", .data = &jz4780_soc_info }, { .compatible = "ingenic,x1000-spi", .data = &x1000_soc_info }, + { .compatible = "ingenic,t23-spi", .data = &x1000_soc_info }, { .compatible = "ingenic,x2000-spi", .data = &x2000_soc_info }, {} }; From ac97f3c4fbcf02b314969dcac38932b734f1af36 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 28/78] dt-bindings: rtc: ingenic: Add the T23 RTC Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../devicetree/bindings/rtc/ingenic,rtc.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Documentation/devicetree/bindings/rtc/ingenic,rtc.yaml b/Documentation/devicetree/bindings/rtc/ingenic,rtc.yaml index de9879bdb3..a177158737 100644 --- a/Documentation/devicetree/bindings/rtc/ingenic,rtc.yaml +++ b/Documentation/devicetree/bindings/rtc/ingenic,rtc.yaml @@ -22,6 +22,14 @@ allOf: then: properties: "#clock-cells": false + - if: + properties: + compatible: + contains: + const: ingenic,t23-rtc + then: + properties: + system-power-controller: false properties: compatible: @@ -29,6 +37,10 @@ properties: - enum: - ingenic,jz4740-rtc - ingenic,jz4760-rtc + - ingenic,t23-rtc + - items: + - const: ingenic,t23-rtc + - const: ingenic,jz4780-rtc - items: - const: ingenic,jz4725b-rtc - const: ingenic,jz4740-rtc From 7dc21b561c9fadc2bd1553aa1266a419ee748cd6 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 29/78] rtc: jz4740: Add T23 RTC support Enable the T23 RTC counter without programming the older SoC wakeup parameters, and skip registering the legacy power-off handler on T23. Signed-off-by: Difan Zhang --- drivers/rtc/rtc-jz4740.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c index bafa7d1b9b..f9af02d14b 100644 --- a/drivers/rtc/rtc-jz4740.c +++ b/drivers/rtc/rtc-jz4740.c @@ -55,6 +55,7 @@ enum jz4740_rtc_type { ID_JZ4740, ID_JZ4760, ID_JZ4780, + ID_T23, }; struct jz4740_rtc { @@ -264,6 +265,7 @@ static const struct of_device_id jz4740_rtc_of_match[] = { { .compatible = "ingenic,jz4760-rtc", .data = (void *)ID_JZ4760 }, { .compatible = "ingenic,jz4770-rtc", .data = (void *)ID_JZ4780 }, { .compatible = "ingenic,jz4780-rtc", .data = (void *)ID_JZ4780 }, + { .compatible = "ingenic,t23-rtc", .data = (void *)ID_T23 }, {}, }; MODULE_DEVICE_TABLE(of, jz4740_rtc_of_match); @@ -382,7 +384,10 @@ static int jz4740_rtc_probe(struct platform_device *pdev) rtc->rtc->range_max = U32_MAX; rate = clk_get_rate(clk); - jz4740_rtc_set_wakeup_params(rtc, np, rate); + if (rtc->type != ID_T23) + jz4740_rtc_set_wakeup_params(rtc, np, rate); + else if (!(jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL) & JZ_RTC_CTRL_ENABLE)) + jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_ENABLE, true); /* Each 1 Hz pulse should happen after (rate) ticks */ jz4740_rtc_reg_write(rtc, JZ_REG_RTC_REGULATOR, rate - 1); @@ -396,7 +401,7 @@ static int jz4740_rtc_probe(struct platform_device *pdev) if (ret) return dev_err_probe(dev, ret, "Failed to request rtc irq\n"); - if (of_device_is_system_power_controller(np)) { + if (rtc->type != ID_T23 && of_device_is_system_power_controller(np)) { dev_for_power_off = dev; if (!pm_power_off) From 2307f1249df5c35059588f2a6264331d351ccf05 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 30/78] dt-bindings: rng: ingenic: Add the T23 DTRNG Describe the T23 hardware compatible and its resources using the existing Ingenic controller schema. Signed-off-by: Difan Zhang --- .../devicetree/bindings/rng/ingenic,trng.yaml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/rng/ingenic,trng.yaml b/Documentation/devicetree/bindings/rng/ingenic,trng.yaml index acaeb63caf..dc3a613a99 100644 --- a/Documentation/devicetree/bindings/rng/ingenic,trng.yaml +++ b/Documentation/devicetree/bindings/rng/ingenic,trng.yaml @@ -14,12 +14,20 @@ description: properties: compatible: - enum: - - ingenic,x1830-dtrng + oneOf: + - enum: + - ingenic,x1830-dtrng + - ingenic,t23-dtrng + - items: + - const: ingenic,t23-dtrng + - const: ingenic,x1830-dtrng reg: maxItems: 1 + interrupts: + maxItems: 1 + clocks: maxItems: 1 From c661a05561cdadb47f8c41d40b4bb51c0ef1cb6f Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:01:27 -0800 Subject: [PATCH 31/78] hwrng: ingenic: Add the T23 DTRNG compatible Expose the T23 random number generator through the existing Ingenic hwrng driver. Signed-off-by: Difan Zhang --- drivers/char/hw_random/ingenic-trng.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/char/hw_random/ingenic-trng.c b/drivers/char/hw_random/ingenic-trng.c index 1672320e7d..ab2e1eee63 100644 --- a/drivers/char/hw_random/ingenic-trng.c +++ b/drivers/char/hw_random/ingenic-trng.c @@ -110,6 +110,7 @@ static int ingenic_trng_probe(struct platform_device *pdev) static const struct of_device_id ingenic_trng_of_match[] = { { .compatible = "ingenic,x1830-dtrng" }, + { .compatible = "ingenic,t23-dtrng" }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, ingenic_trng_of_match); From fb04d5e6163e62fa6a3f3ad51e56a9b62c4feedb Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 32/78] dt-bindings: phy: ingenic: Add the T23 USB PHY Describe the T23 USB PHY, its CPM syscon and optional tuning register window. Signed-off-by: Difan Zhang --- .../bindings/phy/ingenic,phy-usb.yaml | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/phy/ingenic,phy-usb.yaml b/Documentation/devicetree/bindings/phy/ingenic,phy-usb.yaml index 30b42008db..312dbc0f4f 100644 --- a/Documentation/devicetree/bindings/phy/ingenic,phy-usb.yaml +++ b/Documentation/devicetree/bindings/phy/ingenic,phy-usb.yaml @@ -21,14 +21,29 @@ properties: - ingenic,jz4780-phy - ingenic,x1000-phy - ingenic,x1830-phy + - ingenic,t23-phy - ingenic,x2000-phy reg: - maxItems: 1 + minItems: 1 + maxItems: 2 + + reg-names: + minItems: 1 + items: + - const: cpm + - const: tuning clocks: maxItems: 1 + clock-names: + const: otg + + ingenic,cpm: + $ref: /schemas/types.yaml#/definitions/phandle + description: Phandle to the CPM syscon node. + vcc-supply: description: VCC power supply @@ -39,9 +54,19 @@ required: - compatible - reg - clocks - - vcc-supply - '#phy-cells' +allOf: + - if: + not: + properties: + compatible: + contains: + const: ingenic,t23-phy + then: + required: + - vcc-supply + additionalProperties: false examples: From 70c2384f5b58115e0ff809b2cc63d4368bf96402 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:06:01 -0800 Subject: [PATCH 33/78] phy: ingenic: Add T23 USB PHY initialization Sequence the T23 PHY clocks, CPM reset and suspend controls, and optional analog tuning registers when starting or stopping the USB PHY. Signed-off-by: Difan Zhang --- drivers/phy/ingenic/Kconfig | 3 +- drivers/phy/ingenic/phy-ingenic-usb.c | 127 ++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/drivers/phy/ingenic/Kconfig b/drivers/phy/ingenic/Kconfig index f23cc10932..340f63e609 100644 --- a/drivers/phy/ingenic/Kconfig +++ b/drivers/phy/ingenic/Kconfig @@ -8,6 +8,7 @@ config PHY_INGENIC_USB depends on USB_SUPPORT depends on HAS_IOMEM select GENERIC_PHY + select MFD_SYSCON if MACH_T23 help This driver provides USB PHY support for the USB controller found - on the JZ-series and X-series SoCs from Ingenic. + on the JZ-series, X-series, and T-series SoCs from Ingenic. diff --git a/drivers/phy/ingenic/phy-ingenic-usb.c b/drivers/phy/ingenic/phy-ingenic-usb.c index eb2721f72a..5001f55c85 100644 --- a/drivers/phy/ingenic/phy-ingenic-usb.c +++ b/drivers/phy/ingenic/phy-ingenic-usb.c @@ -10,10 +10,12 @@ #include #include #include +#include #include #include #include #include +#include #include /* OTGPHY register offsets */ @@ -22,6 +24,10 @@ #define REG_USBVBFIL_OFFSET 0x08 #define REG_USBPCR1_OFFSET 0x0c +/* CPM registers outside the 0x10 USBPCR block */ +#define CPM_OPCR_OFFSET 0x24 +#define CPM_SRBC_OFFSET 0xc4 + /* bits within the USBPCR register */ #define USBPCR_USB_MODE BIT(31) #define USBPCR_AVLD_REG BIT(30) @@ -82,9 +88,15 @@ #define USBPCR1_USB_SEL BIT(28) #define USBPCR1_PORT_RST BIT(21) #define USBPCR1_WORD_IF_16BIT BIT(19) +#define USBPCR1_T23_WORD_IF_8BIT BIT(19) + +#define OPCR_SPENDN0 BIT(7) +#define SRBC_USB_SR BIT(12) struct ingenic_soc_info { void (*usb_phy_init)(struct phy *phy); + void (*usb_phy_exit)(struct phy *phy); + bool needs_cpm; }; struct ingenic_usb_phy { @@ -92,10 +104,19 @@ struct ingenic_usb_phy { struct phy *phy; void __iomem *base; + void __iomem *tuning_base; + struct regmap *cpm_regmap; struct clk *clk; struct regulator *vcc_supply; }; +static void ingenic_usb_phy_cpm_set_bit(struct ingenic_usb_phy *priv, + unsigned int reg, u32 mask, bool set) +{ + if (priv->cpm_regmap) + regmap_update_bits(priv->cpm_regmap, reg, mask, set ? mask : 0); +} + static int ingenic_usb_phy_init(struct phy *phy) { struct ingenic_usb_phy *priv = phy_get_drvdata(phy); @@ -123,6 +144,9 @@ static int ingenic_usb_phy_exit(struct phy *phy) { struct ingenic_usb_phy *priv = phy_get_drvdata(phy); + if (priv->soc_info->usb_phy_exit) + priv->soc_info->usb_phy_exit(phy); + clk_disable_unprepare(priv->clk); regulator_disable(priv->vcc_supply); @@ -289,6 +313,83 @@ static void x2000_usb_phy_init(struct phy *phy) writel(reg, priv->base + REG_USBPCR_OFFSET); } +static void t23_usb_phy_init(struct phy *phy) +{ + struct ingenic_usb_phy *priv = phy_get_drvdata(phy); + u32 reg, usbpcr1; + + /* + * Select DWC OTG and 16-bit/30MHz UTMI data bus width. Unlike older + * JZ47xx SoCs, T23's WORD_IF0 (bit 19) is 0 for 16-bit/30MHz and 1 for + * 8-bit/60MHz. + */ + usbpcr1 = readl(priv->base + REG_USBPCR1_OFFSET); + usbpcr1 |= BIT(8) | BIT(9) | BIT(28) | BIT(29) | BIT(30); + usbpcr1 &= ~USBPCR1_T23_WORD_IF_8BIT; + usbpcr1 &= ~(0x7 << 23); + usbpcr1 |= (5 << 23); + writel(usbpcr1, priv->base + REG_USBPCR1_OFFSET); + + writel(0, priv->base + REG_USBVBFIL_OFFSET); + writel(USBRDT_VBFIL_LD_EN | 0x96, priv->base + REG_USBRDT_OFFSET); + + reg = 0x8380385a; + reg |= USBPCR_USB_MODE | USBPCR_COMMONONN; + reg &= ~(USBPCR_OTG_DISABLE | USBPCR_SIDDQ | + USBPCR_IDPULLUP_MASK | USBPCR_VBUSVLDEXT | + USBPCR_VBUSVLDEXTSEL); + writel(reg, priv->base + REG_USBPCR_OFFSET); + writel(reg | USBPCR_POR, priv->base + REG_USBPCR_OFFSET); + + reg = readl(priv->base + REG_USBRDT_OFFSET); + writel(reg & ~USBRDT_UTMI_RST, priv->base + REG_USBRDT_OFFSET); + + ingenic_usb_phy_cpm_set_bit(priv, CPM_SRBC_OFFSET, SRBC_USB_SR, true); + udelay(5); + + reg = readl(priv->base + REG_USBPCR_OFFSET); + writel(reg & ~USBPCR_POR, priv->base + REG_USBPCR_OFFSET); + udelay(10); + + ingenic_usb_phy_cpm_set_bit(priv, CPM_OPCR_OFFSET, OPCR_SPENDN0, true); + udelay(550); + + reg = readl(priv->base + REG_USBRDT_OFFSET); + writel(reg | USBRDT_UTMI_RST, priv->base + REG_USBRDT_OFFSET); + udelay(10); + + ingenic_usb_phy_cpm_set_bit(priv, CPM_SRBC_OFFSET, SRBC_USB_SR, false); + + if (priv->tuning_base) { + reg = readl(priv->tuning_base + 0x78); + writel(reg | 0x20, priv->tuning_base + 0x78); + + reg = readl(priv->tuning_base + 0x70); + writel(reg & ~0x8, priv->tuning_base + 0x70); + + writel(0x09, priv->tuning_base + 0x30); + writel(0x49, priv->tuning_base + 0x40); + writel(0x52, priv->tuning_base + 0x120); + writel(0x07, priv->tuning_base + 0x124); + writel(0x00, priv->tuning_base + 0x60); + writel(0x80, priv->tuning_base + 0x64); + writel(0xf8, priv->tuning_base + 0x68); + } +} + +static void t23_usb_phy_exit(struct phy *phy) +{ + struct ingenic_usb_phy *priv = phy_get_drvdata(phy); + u32 reg; + + ingenic_usb_phy_cpm_set_bit(priv, CPM_OPCR_OFFSET, OPCR_SPENDN0, false); + udelay(5); + + reg = readl(priv->base + REG_USBPCR_OFFSET); + reg |= USBPCR_OTG_DISABLE | USBPCR_SIDDQ; + writel(reg, priv->base + REG_USBPCR_OFFSET); +} + static const struct ingenic_soc_info jz4770_soc_info = { .usb_phy_init = jz4770_usb_phy_init, }; @@ -313,6 +414,12 @@ static const struct ingenic_soc_info x2000_soc_info = { .usb_phy_init = x2000_usb_phy_init, }; +static const struct ingenic_soc_info t23_soc_info = { + .usb_phy_init = t23_usb_phy_init, + .usb_phy_exit = t23_usb_phy_exit, + .needs_cpm = true, +}; + static int ingenic_usb_phy_probe(struct platform_device *pdev) { struct ingenic_usb_phy *priv; @@ -336,6 +443,25 @@ static int ingenic_usb_phy_probe(struct platform_device *pdev) return PTR_ERR(priv->base); } + if (priv->soc_info->needs_cpm) { + struct resource *res; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 1); + if (res) { + priv->tuning_base = devm_ioremap_resource(dev, res); + if (IS_ERR(priv->tuning_base)) + return PTR_ERR(priv->tuning_base); + } + + priv->cpm_regmap = syscon_regmap_lookup_by_phandle(dev->of_node, + "ingenic,cpm"); + if (IS_ERR(priv->cpm_regmap) && dev->parent) + priv->cpm_regmap = syscon_node_to_regmap(dev->parent->of_node); + if (IS_ERR(priv->cpm_regmap)) + return dev_err_probe(dev, PTR_ERR(priv->cpm_regmap), + "Failed to get CPM syscon\n"); + } + priv->clk = devm_clk_get(dev, NULL); if (IS_ERR(priv->clk)) { err = PTR_ERR(priv->clk); @@ -369,6 +495,7 @@ static const struct of_device_id ingenic_usb_phy_of_matches[] = { { .compatible = "ingenic,jz4780-phy", .data = &jz4780_soc_info }, { .compatible = "ingenic,x1000-phy", .data = &x1000_soc_info }, { .compatible = "ingenic,x1830-phy", .data = &x1830_soc_info }, + { .compatible = "ingenic,t23-phy", .data = &t23_soc_info }, { .compatible = "ingenic,x2000-phy", .data = &x2000_soc_info }, { /* sentinel */ } }; From ad4c3525ee4eff179493ff5e8fd2346369e62263 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 34/78] dt-bindings: usb: dwc2: Add Ingenic T23 OTG Describe the Ingenic T23 DWC2 OTG controller compatible. Signed-off-by: Difan Zhang --- Documentation/devicetree/bindings/usb/dwc2.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Documentation/devicetree/bindings/usb/dwc2.yaml b/Documentation/devicetree/bindings/usb/dwc2.yaml index a5f2e3442a..16ff8111b9 100644 --- a/Documentation/devicetree/bindings/usb/dwc2.yaml +++ b/Documentation/devicetree/bindings/usb/dwc2.yaml @@ -24,7 +24,11 @@ properties: - const: ingenic,x1600-otg - const: ingenic,x1700-otg - const: ingenic,x1830-otg + - const: ingenic,t23-otg - const: ingenic,x2000-otg + - items: + - const: ingenic,t23-otg + - const: snps,dwc2 - items: - const: rockchip,rk3066-usb - const: snps,dwc2 From 4cae26d59da8fb545f4a44edd022039a5e82d0fe Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:06:01 -0800 Subject: [PATCH 35/78] usb: dwc2: Add Ingenic T23 controller parameters Select high-speed UTMI operation with a 16-bit PHY interface and disable unsupported OTG and low-power capabilities on T23. Signed-off-by: Difan Zhang --- drivers/usb/dwc2/params.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/usb/dwc2/params.c b/drivers/usb/dwc2/params.c index 4d73fae80b..ab4cfd4a89 100644 --- a/drivers/usb/dwc2/params.c +++ b/drivers/usb/dwc2/params.c @@ -81,6 +81,24 @@ static void dwc2_set_x1600_params(struct dwc2_hsotg *hsotg) !device_property_read_bool(hsotg->dev, "disable-over-current"); } +static void dwc2_set_t23_params(struct dwc2_hsotg *hsotg) +{ + struct dwc2_core_params *p = &hsotg->params; + + p->otg_caps.hnp_support = false; + p->otg_caps.srp_support = false; + p->speed = DWC2_SPEED_PARAM_HIGH; + p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI; + p->phy_utmi_width = 16; + p->power_down = DWC2_POWER_DOWN_PARAM_NONE; + p->lpm = false; + p->lpm_clock_gating = false; + p->besl = false; + p->hird_threshold_en = false; + p->activate_ingenic_overcurrent_detection = + !device_property_read_bool(hsotg->dev, "disable-over-current"); +} + static void dwc2_set_x2000_params(struct dwc2_hsotg *hsotg) { struct dwc2_core_params *p = &hsotg->params; @@ -312,6 +330,7 @@ const struct of_device_id dwc2_of_match_table[] = { { .compatible = "ingenic,x1600-otg", .data = dwc2_set_x1600_params }, { .compatible = "ingenic,x1700-otg", .data = dwc2_set_x1600_params }, { .compatible = "ingenic,x1830-otg", .data = dwc2_set_x1600_params }, + { .compatible = "ingenic,t23-otg", .data = dwc2_set_t23_params }, { .compatible = "ingenic,x2000-otg", .data = dwc2_set_x2000_params }, { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params }, { .compatible = "lantiq,danube-usb", .data = &dwc2_set_ltq_danube_params }, From d253779b5588d95a8eb5c15c5bd7bfd344279953 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 36/78] dt-bindings: mmc: ingenic: Add the T23 controller Describe the T23 MMC compatible and its optional controller gate clock. Signed-off-by: Difan Zhang --- .../devicetree/bindings/mmc/ingenic,mmc.yaml | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/mmc/ingenic,mmc.yaml b/Documentation/devicetree/bindings/mmc/ingenic,mmc.yaml index bb4e0be0c8..e5829d0c6f 100644 --- a/Documentation/devicetree/bindings/mmc/ingenic,mmc.yaml +++ b/Documentation/devicetree/bindings/mmc/ingenic,mmc.yaml @@ -11,6 +11,18 @@ maintainers: allOf: - $ref: mmc-controller.yaml# + - if: + not: + properties: + compatible: + contains: + const: ingenic,t23-mmc + then: + properties: + clocks: + maxItems: 1 + clock-names: + const: mmc properties: compatible: @@ -22,6 +34,10 @@ properties: - ingenic,jz4775-mmc - ingenic,jz4780-mmc - ingenic,x1000-mmc + - ingenic,t23-mmc + - items: + - const: ingenic,t23-mmc + - const: ingenic,x1000-mmc - items: - const: ingenic,jz4770-mmc - const: ingenic,jz4760-mmc @@ -33,10 +49,16 @@ properties: maxItems: 1 clocks: - maxItems: 1 + minItems: 1 + items: + - description: MMC controller clock + - description: MMC gate clock clock-names: - const: mmc + minItems: 1 + items: + - const: mmc + - const: gate dmas: minItems: 1 @@ -47,6 +69,9 @@ properties: - items: - const: rx - const: tx + - items: + - const: tx + - const: rx - const: tx-rx required: From cda3fdf98566a5bbae0e6d0827fc4ddfc8b5c78e Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:06:01 -0800 Subject: [PATCH 37/78] mmc: jz4740: Add T23 gate clock and command register support Enable the optional controller gate clock, unmask TIMEOUT_RES alongside END_CMD_RES for commands without a response, and use a 16-bit command register write for the T23 MMC interface. Signed-off-by: Difan Zhang --- drivers/mmc/host/jz4740_mmc.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/mmc/host/jz4740_mmc.c b/drivers/mmc/host/jz4740_mmc.c index 6a45991ca0..9aa5eba706 100644 --- a/drivers/mmc/host/jz4740_mmc.c +++ b/drivers/mmc/host/jz4740_mmc.c @@ -93,6 +93,7 @@ #define JZ_MMC_CMDAT_RSP_R2 2 #define JZ_MMC_CMDAT_RSP_R3 3 +#define JZ_MMC_IRQ_TIMEOUT_RES BIT(9) #define JZ_MMC_IRQ_SDIO BIT(7) #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6) #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5) @@ -118,6 +119,7 @@ enum jz4740_mmc_version { JZ_MMC_JZ4760, JZ_MMC_JZ4780, JZ_MMC_X1000, + JZ_MMC_T23, }; enum jz4740_mmc_state { @@ -150,6 +152,7 @@ struct jz4740_mmc_host { struct mmc_host *mmc; struct platform_device *pdev; struct clk *clk; + struct clk *clk_gate; enum jz4740_mmc_version version; @@ -167,6 +170,7 @@ struct jz4740_mmc_host { uint32_t cmdat; uint32_t irq_mask; + u32 cmd_irqs; spinlock_t lock; @@ -646,7 +650,7 @@ static void jz4740_mmc_timeout(struct timer_list *t) if (!test_and_clear_bit(0, &host->waiting)) return; - jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false); + jz4740_mmc_set_irq_enabled(host, host->cmd_irqs, false); host->req->cmd->error = -ETIMEDOUT; jz4740_mmc_request_done(host); @@ -731,7 +735,10 @@ static void jz4740_mmc_send_command(struct jz4740_mmc_host *host, writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB); } - writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD); + if (host->version >= JZ_MMC_T23) + writew(cmd->opcode, host->base + JZ_REG_MMC_CMD); + else + writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD); writel(cmd->arg, host->base + JZ_REG_MMC_ARG); writel(cmdat, host->base + JZ_REG_MMC_CMDAT); @@ -875,7 +882,10 @@ static irqreturn_t jz_mmc_irq(int irq, void *devid) cmd->error = -EIO; } - jz4740_mmc_set_irq_enabled(host, irq_reg, false); + tmp = irq_reg; + if (tmp & host->cmd_irqs) + tmp |= host->cmd_irqs; + jz4740_mmc_set_irq_enabled(host, tmp, false); jz4740_mmc_write_irq_reg(host, irq_reg); return IRQ_WAKE_THREAD; @@ -927,7 +937,7 @@ static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req) host->req = req; jz4740_mmc_write_irq_reg(host, ~0); - jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true); + jz4740_mmc_set_irq_enabled(host, host->cmd_irqs, true); host->state = JZ4740_MMC_STATE_READ_RESPONSE; set_bit(0, &host->waiting); @@ -1032,6 +1042,7 @@ static const struct of_device_id jz4740_mmc_of_match[] = { { .compatible = "ingenic,jz4775-mmc", .data = (void *) JZ_MMC_JZ4780 }, { .compatible = "ingenic,jz4780-mmc", .data = (void *) JZ_MMC_JZ4780 }, { .compatible = "ingenic,x1000-mmc", .data = (void *) JZ_MMC_X1000 }, + { .compatible = "ingenic,t23-mmc", .data = (void *) JZ_MMC_T23 }, {}, }; MODULE_DEVICE_TABLE(of, jz4740_mmc_of_match); @@ -1052,6 +1063,9 @@ static int jz4740_mmc_probe(struct platform_device* pdev) /* Default if no match is JZ4740 */ host->version = (enum jz4740_mmc_version)device_get_match_data(&pdev->dev); + host->cmd_irqs = JZ_MMC_IRQ_END_CMD_RES; + if (host->version == JZ_MMC_T23) + host->cmd_irqs |= JZ_MMC_IRQ_TIMEOUT_RES; ret = mmc_of_parse(mmc); if (ret) { @@ -1074,6 +1088,14 @@ static int jz4740_mmc_probe(struct platform_device* pdev) goto err_free_host; } + if (host->version >= JZ_MMC_T23) { + host->clk_gate = devm_clk_get_optional_enabled(&pdev->dev, "gate"); + if (IS_ERR(host->clk_gate)) { + ret = PTR_ERR(host->clk_gate); + goto err_free_host; + } + } + host->base = devm_platform_get_and_ioremap_resource(pdev, 0, &host->mem_res); if (IS_ERR(host->base)) { ret = PTR_ERR(host->base); From 8b3081d760140d158297d7ca272e1acc22a033e6 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 38/78] dt-bindings: net: ingenic: Add T23 MAC and PHY control Describe the T23 DWMAC compatible, MAC PHY control syscon and associated clock and reset resources. Signed-off-by: Difan Zhang --- .../devicetree/bindings/mfd/syscon.yaml | 2 + .../devicetree/bindings/net/ingenic,mac.yaml | 59 ++++++++++++++++--- .../devicetree/bindings/net/snps,dwmac.yaml | 1 + 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Documentation/devicetree/bindings/mfd/syscon.yaml b/Documentation/devicetree/bindings/mfd/syscon.yaml index cc9b17ad69..9bf6f151e0 100644 --- a/Documentation/devicetree/bindings/mfd/syscon.yaml +++ b/Documentation/devicetree/bindings/mfd/syscon.yaml @@ -72,6 +72,7 @@ select: - hisilicon,pcie-sas-subctrl - hisilicon,peri-subctrl - hpe,gxp-sysreg + - ingenic,t23-mac-phy-ctrl - loongson,ls1b-syscon - loongson,ls1c-syscon - lsi,axxia-syscon @@ -169,6 +170,7 @@ properties: - hisilicon,pcie-sas-subctrl - hisilicon,peri-subctrl - hpe,gxp-sysreg + - ingenic,t23-mac-phy-ctrl - loongson,ls1b-syscon - loongson,ls1c-syscon - lsi,axxia-syscon diff --git a/Documentation/devicetree/bindings/net/ingenic,mac.yaml b/Documentation/devicetree/bindings/net/ingenic,mac.yaml index bdea101c2f..3056dae6fd 100644 --- a/Documentation/devicetree/bindings/net/ingenic,mac.yaml +++ b/Documentation/devicetree/bindings/net/ingenic,mac.yaml @@ -12,14 +12,48 @@ maintainers: description: The Ethernet Media Access Controller in Ingenic SoCs. +select: + properties: + compatible: + contains: + enum: + - ingenic,jz4775-mac + - ingenic,x1000-mac + - ingenic,x1600-mac + - ingenic,x1830-mac + - ingenic,t23-mac + - ingenic,x2000-mac + required: + - compatible + +allOf: + - $ref: snps,dwmac.yaml# + - if: + not: + properties: + compatible: + contains: + const: ingenic,t23-mac + then: + properties: + clocks: + maxItems: 1 + clock-names: + const: stmmaceth + properties: compatible: - enum: - - ingenic,jz4775-mac - - ingenic,x1000-mac - - ingenic,x1600-mac - - ingenic,x1830-mac - - ingenic,x2000-mac + oneOf: + - enum: + - ingenic,jz4775-mac + - ingenic,x1000-mac + - ingenic,x1600-mac + - ingenic,x1830-mac + - ingenic,t23-mac + - ingenic,x2000-mac + - items: + - const: ingenic,t23-mac + - const: snps,dwmac reg: maxItems: 1 @@ -31,10 +65,16 @@ properties: const: macirq clocks: - maxItems: 1 + minItems: 1 + items: + - description: GMAC main clock + - description: MAC PHY clock clock-names: - const: stmmaceth + minItems: 1 + items: + - const: stmmaceth + - const: macphy mode-reg: $ref: /schemas/types.yaml#/definitions/phandle @@ -55,7 +95,7 @@ required: - clock-names - mode-reg -additionalProperties: false +unevaluatedProperties: false examples: - | @@ -73,5 +113,6 @@ examples: clock-names = "stmmaceth"; mode-reg = <&mac_phy_ctrl>; + phy-mode = "rmii"; }; ... diff --git a/Documentation/devicetree/bindings/net/snps,dwmac.yaml b/Documentation/devicetree/bindings/net/snps,dwmac.yaml index 0deb5d43f8..da20e64ff0 100644 --- a/Documentation/devicetree/bindings/net/snps,dwmac.yaml +++ b/Documentation/devicetree/bindings/net/snps,dwmac.yaml @@ -64,6 +64,7 @@ properties: - ingenic,x1000-mac - ingenic,x1600-mac - ingenic,x1830-mac + - ingenic,t23-mac - ingenic,x2000-mac - loongson,ls2k-dwmac - loongson,ls7a-dwmac From 9dc3b830f8c14e9488ea63699387fc240c660ee1 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:06:01 -0800 Subject: [PATCH 39/78] net: stmmac: ingenic: Add T23 MAC support Configure the T23 MAC PHY interface through its syscon register and enable the optional macphy clock. Signed-off-by: Difan Zhang --- .../ethernet/stmicro/stmmac/dwmac-ingenic.c | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-ingenic.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-ingenic.c index 19c93b998f..50003a8b46 100644 --- a/drivers/net/ethernet/stmicro/stmmac/dwmac-ingenic.c +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-ingenic.c @@ -52,12 +52,14 @@ enum ingenic_mac_version { ID_X1600, ID_X1830, ID_X2000, + ID_T23, }; struct ingenic_mac { const struct ingenic_soc_info *soc_info; struct device *dev; struct regmap *regmap; + struct clk *macphy_clk; int rx_delay; int tx_delay; @@ -185,6 +187,40 @@ static int x1830_mac_set_mode(struct plat_stmmacenet_data *plat_dat) return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, val); } +static int t23_mac_set_mode(struct plat_stmmacenet_data *plat_dat) +{ + struct ingenic_mac *mac = plat_dat->bsp_priv; + unsigned int val; + + switch (plat_dat->mac_interface) { + case PHY_INTERFACE_MODE_MII: + val = FIELD_PREP(MACPHYC_TXCLK_SEL_MASK, MACPHYC_TXCLK_SEL_INPUT) | + FIELD_PREP(MACPHYC_PHY_INFT_MASK, MACPHYC_PHY_INFT_MII); + break; + case PHY_INTERFACE_MODE_GMII: + val = FIELD_PREP(MACPHYC_TXCLK_SEL_MASK, MACPHYC_TXCLK_SEL_OUTPUT) | + FIELD_PREP(MACPHYC_PHY_INFT_MASK, MACPHYC_PHY_INFT_GMII); + break; + case PHY_INTERFACE_MODE_RMII: + val = FIELD_PREP(MACPHYC_TXCLK_SEL_MASK, MACPHYC_TXCLK_SEL_INPUT) | + FIELD_PREP(MACPHYC_PHY_INFT_MASK, MACPHYC_PHY_INFT_RMII); + break; + case PHY_INTERFACE_MODE_RGMII: + case PHY_INTERFACE_MODE_RGMII_ID: + case PHY_INTERFACE_MODE_RGMII_TXID: + case PHY_INTERFACE_MODE_RGMII_RXID: + val = FIELD_PREP(MACPHYC_TXCLK_SEL_MASK, MACPHYC_TXCLK_SEL_OUTPUT) | + FIELD_PREP(MACPHYC_PHY_INFT_MASK, MACPHYC_PHY_INFT_RGMII); + break; + default: + dev_err(mac->dev, "Unsupported interface %d\n", + plat_dat->mac_interface); + return -EINVAL; + } + + return regmap_update_bits(mac->regmap, 0, mac->soc_info->mask, val); +} + static int x2000_mac_set_mode(struct plat_stmmacenet_data *plat_dat) { struct ingenic_mac *mac = plat_dat->bsp_priv; @@ -262,6 +298,15 @@ static int ingenic_mac_probe(struct platform_device *pdev) return PTR_ERR(mac->regmap); } + if (data->version == ID_T23) { + mac->macphy_clk = devm_clk_get_optional_enabled(&pdev->dev, + "macphy"); + if (IS_ERR(mac->macphy_clk)) + return dev_err_probe(&pdev->dev, + PTR_ERR(mac->macphy_clk), + "Failed to get macphy clock\n"); + } + if (!of_property_read_u32(pdev->dev.of_node, "tx-clk-delay-ps", &tx_delay_ps)) { if (tx_delay_ps >= MACPHYC_TX_DELAY_PS_MIN && tx_delay_ps <= MACPHYC_TX_DELAY_PS_MAX) { @@ -350,6 +395,13 @@ static struct ingenic_soc_info x1830_soc_info = { .set_mode = x1830_mac_set_mode, }; +static struct ingenic_soc_info t23_soc_info = { + .version = ID_T23, + .mask = MACPHYC_TXCLK_SEL_MASK | MACPHYC_SOFT_RST_MASK | MACPHYC_PHY_INFT_MASK, + + .set_mode = t23_mac_set_mode, +}; + static struct ingenic_soc_info x2000_soc_info = { .version = ID_X2000, .mask = MACPHYC_TX_SEL_MASK | MACPHYC_TX_DELAY_MASK | MACPHYC_RX_SEL_MASK | @@ -363,6 +415,7 @@ static const struct of_device_id ingenic_mac_of_matches[] = { { .compatible = "ingenic,x1000-mac", .data = &x1000_soc_info }, { .compatible = "ingenic,x1600-mac", .data = &x1600_soc_info }, { .compatible = "ingenic,x1830-mac", .data = &x1830_soc_info }, + { .compatible = "ingenic,t23-mac", .data = &t23_soc_info }, { .compatible = "ingenic,x2000-mac", .data = &x2000_soc_info }, { } }; From 8f9cd779b3cdf97cf3ec8133b2b033e78888d02f Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:14:01 -0800 Subject: [PATCH 40/78] mtd: spi-nor: Add Zbit ZB25VQ64 and ZB25VQ128 Recognize the Zbit 8 MiB and 16 MiB SPI NOR parts used by T23 camera boards, including 4 KiB erases, dual and quad reads, and the SR2 quad-enable bit. Signed-off-by: Difan Zhang --- drivers/mtd/spi-nor/Makefile | 1 + drivers/mtd/spi-nor/core.c | 1 + drivers/mtd/spi-nor/core.h | 1 + drivers/mtd/spi-nor/zbit.c | 42 ++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 drivers/mtd/spi-nor/zbit.c diff --git a/drivers/mtd/spi-nor/Makefile b/drivers/mtd/spi-nor/Makefile index 5dd9c35f6b..821b14ec15 100644 --- a/drivers/mtd/spi-nor/Makefile +++ b/drivers/mtd/spi-nor/Makefile @@ -14,6 +14,7 @@ spi-nor-objs += spansion.o spi-nor-objs += sst.o spi-nor-objs += winbond.o spi-nor-objs += xmc.o +spi-nor-objs += zbit.o spi-nor-$(CONFIG_DEBUG_FS) += debugfs.o obj-$(CONFIG_MTD_SPI_NOR) += spi-nor.o diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c index d666e044b0..1bb2e8a02a 100644 --- a/drivers/mtd/spi-nor/core.c +++ b/drivers/mtd/spi-nor/core.c @@ -1977,6 +1977,7 @@ static const struct spi_nor_manufacturer *manufacturers[] = { &spi_nor_sst, &spi_nor_winbond, &spi_nor_xmc, + &spi_nor_zbit, }; static const struct flash_info spi_nor_generic_flash = { diff --git a/drivers/mtd/spi-nor/core.h b/drivers/mtd/spi-nor/core.h index df07ded382..c2c8fbc951 100644 --- a/drivers/mtd/spi-nor/core.h +++ b/drivers/mtd/spi-nor/core.h @@ -599,6 +599,7 @@ extern const struct spi_nor_manufacturer spi_nor_spansion; extern const struct spi_nor_manufacturer spi_nor_sst; extern const struct spi_nor_manufacturer spi_nor_winbond; extern const struct spi_nor_manufacturer spi_nor_xmc; +extern const struct spi_nor_manufacturer spi_nor_zbit; extern const struct attribute_group *spi_nor_sysfs_groups[]; diff --git a/drivers/mtd/spi-nor/zbit.c b/drivers/mtd/spi-nor/zbit.c new file mode 100644 index 0000000000..95e413a260 --- /dev/null +++ b/drivers/mtd/spi-nor/zbit.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Zbit Semiconductor SPI NOR flash parts + * + * Copyright (C) 2026 Difan Zhang + */ + +#include + +#include "core.h" + +static void zbit_nor_default_init(struct spi_nor *nor) +{ + nor->params->quad_enable = spi_nor_sr2_bit1_quad_enable; +} + +static const struct spi_nor_fixups zbit_nor_fixups = { + .default_init = zbit_nor_default_init, +}; + +static const struct flash_info zbit_nor_parts[] = { + { + .id = SNOR_ID(0x5e, 0x40, 0x17), + .name = "zb25vq64", + .size = SZ_8M, + .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | + SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK, + }, { + .id = SNOR_ID(0x5e, 0x40, 0x18), + .name = "zb25vq128", + .size = SZ_16M, + .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | + SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK, + }, +}; + +const struct spi_nor_manufacturer spi_nor_zbit = { + .name = "zbit", + .parts = zbit_nor_parts, + .nparts = ARRAY_SIZE(zbit_nor_parts), + .fixups = &zbit_nor_fixups, +}; From 0ddbb1fe958167878b2063ad074c17b6dca943c2 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 41/78] dt-bindings: spi: Add the Ingenic SFC v1 controller Describe the register windows, clocks, interrupts and other resources used by the T23 device. Signed-off-by: Difan Zhang --- .../bindings/spi/ingenic,t23-sfc.yaml | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Documentation/devicetree/bindings/spi/ingenic,t23-sfc.yaml diff --git a/Documentation/devicetree/bindings/spi/ingenic,t23-sfc.yaml b/Documentation/devicetree/bindings/spi/ingenic,t23-sfc.yaml new file mode 100644 index 0000000000..52f9390ec9 --- /dev/null +++ b/Documentation/devicetree/bindings/spi/ingenic,t23-sfc.yaml @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/spi/ingenic,t23-sfc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T-Series SFC v1 SPI Flash Controller + +maintainers: + - Paul Cercueil + +allOf: + - $ref: spi-controller.yaml# + +properties: + compatible: + enum: + - ingenic,t23-sfc + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + items: + - description: SFC divider clock + - description: SFC AHB gate clock + + clock-names: + items: + - const: sfc + - const: gate + +required: + - compatible + - reg + - interrupts + - clocks + - clock-names + +unevaluatedProperties: false + +examples: + - | + #include + spi@13440000 { + compatible = "ingenic,t23-sfc"; + reg = <0x13440000 0x10000>; + interrupt-parent = <&intc>; + interrupts = <7>; + clocks = <&cgu CLK_CGU_SFC>, <&cgu CLK_SFC>; + clock-names = "sfc", "gate"; + #address-cells = <1>; + #size-cells = <0>; + + flash@0 { + compatible = "jedec,spi-nor"; + reg = <0>; + label = "jz_sfc"; + spi-max-frequency = <50000000>; + }; + }; From 44a924ae4b2a0641fe1eb828fb0178aa27a044cc Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 13:50:05 -0800 Subject: [PATCH 42/78] spi: Add an Ingenic SFC v1 spi-mem driver Implement spi-mem operations with 32-bit MMIO, PIO for short commands, and a coherent bounce buffer for transfers within the SFC DMA address window. Clear SR_TREQ and fill up to SFC_FIFO_THRESHOLD words per PIO write burst, propagating FIFO underruns on completion. Signed-off-by: Difan Zhang --- drivers/spi/Kconfig | 11 + drivers/spi/Makefile | 1 + drivers/spi/spi-ingenic-sfc.c | 465 ++++++++++++++++++++++++++++++++++ 3 files changed, 477 insertions(+) create mode 100644 drivers/spi/spi-ingenic-sfc.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 9ea2f3ebc0..e0f8621d2f 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -484,6 +484,17 @@ config SPI_INGENIC To compile this driver as a module, choose M here: the module will be called spi-ingenic. +config SPI_INGENIC_SFC + tristate "Ingenic T-Series SFC v1 SPI Flash Controller" + depends on MACH_INGENIC || COMPILE_TEST + depends on HAS_IOMEM + help + This enables spi-mem support for the Special Flash Controller (SFC v1) + on Ingenic T-Series SoCs such as the T23. + + To compile this driver as a module, choose M here: the module + will be called spi-ingenic-sfc. + config SPI_INTEL tristate diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 969e982376..663f5b0180 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -68,6 +68,7 @@ obj-$(CONFIG_SPI_HISI_SFC_V3XX) += spi-hisi-sfc-v3xx.o obj-$(CONFIG_SPI_IMG_SPFI) += spi-img-spfi.o obj-$(CONFIG_SPI_IMX) += spi-imx.o obj-$(CONFIG_SPI_INGENIC) += spi-ingenic.o +obj-$(CONFIG_SPI_INGENIC_SFC) += spi-ingenic-sfc.o obj-$(CONFIG_SPI_INTEL) += spi-intel.o obj-$(CONFIG_SPI_INTEL_PCI) += spi-intel-pci.o obj-$(CONFIG_SPI_INTEL_PLATFORM) += spi-intel-platform.o diff --git a/drivers/spi/spi-ingenic-sfc.c b/drivers/spi/spi-ingenic-sfc.c new file mode 100644 index 0000000000..d3c4565ece --- /dev/null +++ b/drivers/spi/spi-ingenic-sfc.c @@ -0,0 +1,465 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T-Series SFC v1 SPI Flash Controller (spi-mem) driver + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SFC_GLB 0x0000 +#define SFC_DEV_CONF 0x0004 +#define SFC_DEV_STA_EXP 0x0008 +#define SFC_DEV_STA_RT 0x000c +#define SFC_DEV_STA_MSK 0x0010 +#define SFC_TRAN_CONF(n) (0x0014 + ((n) * 4)) +#define SFC_TRAN_LEN 0x002c +#define SFC_DEV_ADDR(n) (0x0030 + ((n) * 4)) +#define SFC_DEV_ADDR_PLUS(n) (0x0048 + ((n) * 4)) +#define SFC_MEM_ADDR 0x0060 +#define SFC_TRIG 0x0064 +#define SFC_SR 0x0068 +#define SFC_SCR 0x006c +#define SFC_INTC 0x0070 +#define SFC_CGE 0x0078 +#define SFC_RM_DR 0x1000 + +#define GLB_TRAN_DIR BIT(13) +#define GLB_THRESHOLD_MASK GENMASK(12, 7) +#define GLB_OP_DMA BIT(6) +#define GLB_PHASE_NUM_MASK GENMASK(5, 3) +#define GLB_WP_EN BIT(2) +#define GLB_BURST_MD_MASK GENMASK(1, 0) + +#define DEV_CONF_SMP_DELAY_MASK GENMASK(17, 16) +#define DEV_CONF_HALF_CYCLE_DELAY 1 +#define DEV_CONF_THOLD_MASK GENMASK(12, 11) +#define DEV_CONF_TSETUP_MASK GENMASK(10, 9) +#define DEV_CONF_TSH_MASK GENMASK(8, 5) +#define DEV_CONF_CPHA BIT(4) +#define DEV_CONF_CPOL BIT(3) +#define DEV_CONF_CEDL BIT(2) +#define DEV_CONF_HOLDDL BIT(1) +#define DEV_CONF_WPDL BIT(0) + +#define TRAN_CONF_MODE_MASK GENMASK(31, 29) +#define TRAN_SPI_STANDARD 0x0 +#define TRAN_SPI_DUAL 0x1 +#define TRAN_SPI_IO_DUAL 0x2 +#define TRAN_SPI_QUAD 0x5 +#define TRAN_SPI_IO_QUAD 0x6 +#define TRAN_CONF_ADDR_WIDTH_MASK GENMASK(28, 26) +#define TRAN_CONF_POLL_EN BIT(25) +#define TRAN_CONF_CMD_EN BIT(24) +#define TRAN_CONF_DMYBITS_MASK GENMASK(22, 17) +#define TRAN_CONF_DATA_EN BIT(16) +#define TRAN_CONF_CMD_MASK GENMASK(15, 0) + +#define TRIG_FLUSH BIT(2) +#define TRIG_STOP BIT(1) +#define TRIG_START BIT(0) + +#define SR_FIFO_NUM_MASK GENMASK(22, 16) +#define SR_END BIT(4) +#define SR_TREQ BIT(3) +#define SR_RREQ BIT(2) +#define SR_OVER BIT(1) +#define SR_UNDER BIT(0) +#define SR_IRQ_MASK GENMASK(4, 0) + +#define SFC_FIFO_THRESHOLD 31 +#define SFC_PIO_MAX_BYTES 64 +#define SFC_BOUNCE_SIZE PAGE_SIZE +#define SFC_DMA_ADDR_MASK 0x1fffffffU + +struct ingenic_sfc { + void __iomem *base; + struct device *dev; + struct clk *clk; + struct clk *clk_gate; + int irq; + struct completion done; + struct mutex lock; + void *bounce; + dma_addr_t dma_phys; + u32 cur_speed_hz; + u32 status_err; +}; + +static void ingenic_sfc_quiesce(struct ingenic_sfc *sfc) +{ + writel(SR_IRQ_MASK, sfc->base + SFC_INTC); + writel(TRIG_STOP | TRIG_FLUSH, sfc->base + SFC_TRIG); + writel(SR_IRQ_MASK, sfc->base + SFC_SCR); +} + +static irqreturn_t ingenic_sfc_irq(int irq, void *data) +{ + struct ingenic_sfc *sfc = data; + u32 sr, mask; + + sr = readl(sfc->base + SFC_SR); + mask = ~readl(sfc->base + SFC_INTC) & SR_IRQ_MASK; + if (!(sr & mask)) + return IRQ_NONE; + + if (sr & (SR_OVER | SR_UNDER)) + sfc->status_err = sr & (SR_OVER | SR_UNDER); + + writel(SR_IRQ_MASK, sfc->base + SFC_INTC); + writel(sr & SR_IRQ_MASK, sfc->base + SFC_SCR); + complete(&sfc->done); + + return IRQ_HANDLED; +} + +static int ingenic_sfc_op_mode(const struct spi_mem_op *op) +{ + if (op->cmd.buswidth != 1) + return -EOPNOTSUPP; + + if (op->addr.nbytes && op->addr.buswidth != 1 && + op->addr.buswidth != 2 && op->addr.buswidth != 4) + return -EOPNOTSUPP; + + if (!op->data.nbytes) + return (op->addr.buswidth <= 1) ? TRAN_SPI_STANDARD : -EOPNOTSUPP; + + switch (op->data.buswidth) { + case 1: + return (op->addr.buswidth <= 1) ? TRAN_SPI_STANDARD : -EOPNOTSUPP; + case 2: + if (op->addr.buswidth <= 1) + return TRAN_SPI_DUAL; + if (op->addr.buswidth == 2) + return TRAN_SPI_IO_DUAL; + return -EOPNOTSUPP; + case 4: + if (op->addr.buswidth <= 1) + return TRAN_SPI_QUAD; + if (op->addr.buswidth == 4) + return TRAN_SPI_IO_QUAD; + return -EOPNOTSUPP; + default: + return -EOPNOTSUPP; + } +} + +static bool ingenic_sfc_supports_op(struct spi_mem *mem, + const struct spi_mem_op *op) +{ + unsigned int dummy_cycles = 0; + + if (!spi_mem_default_supports_op(mem, op)) + return false; + + if (op->cmd.nbytes != 1 || op->addr.nbytes > 6) + return false; + + if (op->dummy.nbytes) { + if (!op->dummy.buswidth) + return false; + dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth; + if (dummy_cycles > 63) + return false; + } + + return ingenic_sfc_op_mode(op) >= 0; +} + +static int ingenic_sfc_adjust_op_size(struct spi_mem *mem, + struct spi_mem_op *op) +{ + if (op->data.nbytes > SFC_BOUNCE_SIZE) + op->data.nbytes = SFC_BOUNCE_SIZE; + + return 0; +} + +static void ingenic_sfc_config_bus(struct ingenic_sfc *sfc, + struct spi_device *spi) +{ + u32 speed = spi->max_speed_hz ? spi->max_speed_hz : 50000000U; + u32 dev_conf; + unsigned long actual; + + if (speed != sfc->cur_speed_hz) { + clk_set_rate(sfc->clk, speed); + sfc->cur_speed_hz = speed; + } + + actual = clk_get_rate(sfc->clk); + dev_conf = DEV_CONF_CEDL | DEV_CONF_HOLDDL | DEV_CONF_WPDL; + if (spi->mode & SPI_CPHA) + dev_conf |= DEV_CONF_CPHA; + if (spi->mode & SPI_CPOL) + dev_conf |= DEV_CONF_CPOL; + if (actual >= 100000000UL) + dev_conf |= FIELD_PREP(DEV_CONF_SMP_DELAY_MASK, + DEV_CONF_HALF_CYCLE_DELAY); + + writel(dev_conf, sfc->base + SFC_DEV_CONF); +} + +static int ingenic_sfc_pio_read(struct ingenic_sfc *sfc, u8 *buf, size_t len) +{ + size_t off = 0; + u32 sr, words, word; + int ret; + + while (off < len) { + ret = readl_poll_timeout_atomic(sfc->base + SFC_SR, sr, + FIELD_GET(SR_FIFO_NUM_MASK, sr) || + (sr & (SR_RREQ | SR_END | SR_OVER)), + 1, 200000); + if (ret) + return ret; + if (sr & SR_OVER) + return -EIO; + + words = FIELD_GET(SR_FIFO_NUM_MASK, sr); + if (!words && (sr & (SR_RREQ | SR_END))) + words = 1; + + while (words-- && off < len) { + size_t chunk = min_t(size_t, 4, len - off); + + word = readl(sfc->base + SFC_RM_DR); + memcpy(buf + off, &word, chunk); + off += chunk; + } + writel(SR_RREQ, sfc->base + SFC_SCR); + } + + return readl_poll_timeout_atomic(sfc->base + SFC_SR, sr, + sr & SR_END, 1, 200000); +} + +static int ingenic_sfc_pio_write(struct ingenic_sfc *sfc, const u8 *buf, + size_t len) +{ + u32 sr; + int ret; + + while (len) { + unsigned int words = SFC_FIFO_THRESHOLD; + + ret = readl_poll_timeout_atomic(sfc->base + SFC_SR, sr, + sr & (SR_TREQ | SR_UNDER), + 1, 200000); + if (ret) + return ret; + if (sr & SR_UNDER) + return -EIO; + + /* One request admits a threshold-sized burst of FIFO words. */ + writel(SR_TREQ, sfc->base + SFC_SCR); + while (words-- && len) { + u32 word = 0; + size_t chunk = min(len, sizeof(word)); + + memcpy(&word, buf, chunk); + writel(word, sfc->base + SFC_RM_DR); + buf += chunk; + len -= chunk; + } + } + + ret = readl_poll_timeout_atomic(sfc->base + SFC_SR, sr, + sr & (SR_END | SR_UNDER), 1, 200000); + return ret ?: (sr & SR_UNDER ? -EIO : 0); +} + +static int ingenic_sfc_exec_op(struct spi_mem *mem, + const struct spi_mem_op *op) +{ + struct ingenic_sfc *sfc = spi_controller_get_devdata(mem->spi->controller); + int mode = ingenic_sfc_op_mode(op); + unsigned int dummy_cycles = 0; + size_t nbytes = op->data.nbytes; + bool is_write = (op->data.dir == SPI_MEM_DATA_OUT); + bool use_dma; + u32 glb, tran_conf, sr; + int ret = 0; + + if (mode < 0) + return mode; + if (nbytes > SFC_BOUNCE_SIZE) + return -EINVAL; + + if (op->dummy.nbytes) + dummy_cycles = (op->dummy.nbytes * 8) / op->dummy.buswidth; + + use_dma = nbytes > SFC_PIO_MAX_BYTES; + + mutex_lock(&sfc->lock); + + ingenic_sfc_quiesce(sfc); + ingenic_sfc_config_bus(sfc, mem->spi); + + glb = FIELD_PREP(GLB_THRESHOLD_MASK, SFC_FIFO_THRESHOLD) | + FIELD_PREP(GLB_PHASE_NUM_MASK, 1) | + GLB_WP_EN | + FIELD_PREP(GLB_BURST_MD_MASK, 3); + if (is_write) + glb |= GLB_TRAN_DIR; + if (use_dma) + glb |= GLB_OP_DMA; + + tran_conf = FIELD_PREP(TRAN_CONF_MODE_MASK, (u32)mode) | + FIELD_PREP(TRAN_CONF_ADDR_WIDTH_MASK, op->addr.nbytes) | + TRAN_CONF_CMD_EN | + FIELD_PREP(TRAN_CONF_DMYBITS_MASK, dummy_cycles) | + FIELD_PREP(TRAN_CONF_CMD_MASK, op->cmd.opcode); + if (nbytes) + tran_conf |= TRAN_CONF_DATA_EN; + + writel(glb, sfc->base + SFC_GLB); + writel(tran_conf, sfc->base + SFC_TRAN_CONF(0)); + writel(nbytes, sfc->base + SFC_TRAN_LEN); + writel((u32)op->addr.val, sfc->base + SFC_DEV_ADDR(0)); + writel((u32)(op->addr.val >> 32), sfc->base + SFC_DEV_ADDR_PLUS(0)); + + if (use_dma) { + if (is_write) { + memcpy(sfc->bounce, op->data.buf.out, nbytes); + wmb(); /* flush bounce buffer writes before SFC DMA */ + } + sfc->status_err = 0; + reinit_completion(&sfc->done); + writel((u32)sfc->dma_phys & SFC_DMA_ADDR_MASK, + sfc->base + SFC_MEM_ADDR); + writel(~(SR_END | SR_OVER | SR_UNDER) & SR_IRQ_MASK, + sfc->base + SFC_INTC); + writel(TRIG_START, sfc->base + SFC_TRIG); + if (!wait_for_completion_timeout(&sfc->done, + msecs_to_jiffies(1000))) + ret = -ETIMEDOUT; + else if (sfc->status_err) + ret = -EIO; + + if (!ret && !is_write) { + rmb(); /* order DMA completion before bounce copy */ + memcpy(op->data.buf.in, sfc->bounce, nbytes); + } + } else { + writel(TRIG_START, sfc->base + SFC_TRIG); + if (!nbytes) + ret = readl_poll_timeout_atomic(sfc->base + SFC_SR, sr, + sr & SR_END, 1, 200000); + else if (is_write) + ret = ingenic_sfc_pio_write(sfc, op->data.buf.out, nbytes); + else + ret = ingenic_sfc_pio_read(sfc, op->data.buf.in, nbytes); + } + + ingenic_sfc_quiesce(sfc); + mutex_unlock(&sfc->lock); + + return ret; +} + +static const struct spi_controller_mem_ops ingenic_sfc_mem_ops = { + .supports_op = ingenic_sfc_supports_op, + .adjust_op_size = ingenic_sfc_adjust_op_size, + .exec_op = ingenic_sfc_exec_op, +}; + +static int ingenic_sfc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct spi_controller *ctlr; + struct ingenic_sfc *sfc; + int ret; + + ctlr = devm_spi_alloc_host(dev, sizeof(*sfc)); + if (!ctlr) + return -ENOMEM; + + sfc = spi_controller_get_devdata(ctlr); + sfc->dev = dev; + mutex_init(&sfc->lock); + init_completion(&sfc->done); + + sfc->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(sfc->base)) + return PTR_ERR(sfc->base); + + sfc->irq = platform_get_irq(pdev, 0); + if (sfc->irq < 0) + return sfc->irq; + + sfc->clk = devm_clk_get_enabled(dev, "sfc"); + if (IS_ERR(sfc->clk)) + return dev_err_probe(dev, PTR_ERR(sfc->clk), + "Failed to enable sfc clock\n"); + + sfc->clk_gate = devm_clk_get_optional_enabled(dev, "gate"); + if (IS_ERR(sfc->clk_gate)) + return dev_err_probe(dev, PTR_ERR(sfc->clk_gate), + "Failed to enable sfc gate clock\n"); + + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(29)); + if (ret) + return ret; + + sfc->bounce = dmam_alloc_coherent(dev, SFC_BOUNCE_SIZE, &sfc->dma_phys, + GFP_KERNEL); + if (!sfc->bounce) + return -ENOMEM; + + if (sfc->dma_phys + SFC_BOUNCE_SIZE > (SFC_DMA_ADDR_MASK + 1ULL)) + return dev_err_probe(dev, -ERANGE, + "DMA bounce buffer outside 29-bit window\n"); + + ingenic_sfc_quiesce(sfc); + + ret = devm_request_irq(dev, sfc->irq, ingenic_sfc_irq, 0, + dev_name(dev), sfc); + if (ret) + return dev_err_probe(dev, ret, "Failed to request IRQ\n"); + + ctlr->bus_num = -1; + ctlr->num_chipselect = 1; + ctlr->mode_bits = SPI_CPHA | SPI_CPOL | + SPI_RX_DUAL | SPI_RX_QUAD | + SPI_TX_DUAL | SPI_TX_QUAD; + ctlr->mem_ops = &ingenic_sfc_mem_ops; + ctlr->dev.of_node = dev->of_node; + + platform_set_drvdata(pdev, ctlr); + + return devm_spi_register_controller(dev, ctlr); +} + +static const struct of_device_id ingenic_sfc_of_match[] = { + { .compatible = "ingenic,t23-sfc" }, + { } +}; +MODULE_DEVICE_TABLE(of, ingenic_sfc_of_match); + +static struct platform_driver ingenic_sfc_driver = { + .probe = ingenic_sfc_probe, + .driver = { + .name = "spi-ingenic-sfc", + .of_match_table = ingenic_sfc_of_match, + }, +}; +module_platform_driver(ingenic_sfc_driver); + +MODULE_DESCRIPTION("Ingenic T-Series SFC v1 spi-mem controller driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); From 213e6a83431ebfa342800e375d67655878681d38 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 43/78] dt-bindings: nvmem: ingenic: Add T23 eFuse Document the direct clock-gated memory-mapped eFuse window on Ingenic T23 SoCs in its own binding schema. Signed-off-by: Difan Zhang --- .../bindings/nvmem/ingenic,t23-efuse.yaml | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Documentation/devicetree/bindings/nvmem/ingenic,t23-efuse.yaml diff --git a/Documentation/devicetree/bindings/nvmem/ingenic,t23-efuse.yaml b/Documentation/devicetree/bindings/nvmem/ingenic,t23-efuse.yaml new file mode 100644 index 0000000000..c54a382361 --- /dev/null +++ b/Documentation/devicetree/bindings/nvmem/ingenic,t23-efuse.yaml @@ -0,0 +1,50 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/nvmem/ingenic,t23-efuse.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 SoC eFuse NVMEM + +maintainers: + - Difan Zhang + +description: + Direct memory-mapped read-only eFuse OTP window on Ingenic T23 SoCs, + gated by the CPM eFuse clock. + +allOf: + - $ref: nvmem.yaml# + +properties: + compatible: + const: ingenic,t23-efuse + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: efuse + +required: + - compatible + - reg + - clocks + - clock-names + +unevaluatedProperties: false + +examples: + - | + #include + + efuse@13540200 { + compatible = "ingenic,t23-efuse"; + reg = <0x13540200 0x40>; + clocks = <&cgu CLK_EFUSE>; + clock-names = "efuse"; + }; +... From c30f552ab927a4b8323b300143a2f57359baeb23 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:21:37 -0800 Subject: [PATCH 44/78] nvmem: Add the Ingenic T23 eFuse provider Provide clock-gated, little-endian word reads from the T23 eFuse bank through the nvmem subsystem. Signed-off-by: Difan Zhang --- drivers/nvmem/Kconfig | 12 +++++ drivers/nvmem/Makefile | 2 + drivers/nvmem/ingenic-efuse.c | 98 +++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 drivers/nvmem/ingenic-efuse.c diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index d2c384f580..c5f2d91bcc 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -101,6 +101,18 @@ config NVMEM_IMX_OCOTP_SCU This is a driver for the SCU On-Chip OTP Controller (OCOTP) available on i.MX8 SoCs. +config NVMEM_INGENIC_EFUSE + tristate "Ingenic T23 EFUSE support" + depends on MACH_INGENIC || COMPILE_TEST + depends on HAS_IOMEM + help + Read-only nvmem access to the T23 EFUSE data window. Bytes come + from little-endian word loads. The EFUSE clock is enabled only + for the duration of a read. + + This driver can also be built as a module. If so, the module + will be called nvmem-ingenic-efuse. + config NVMEM_JZ4780_EFUSE tristate "JZ4780 EFUSE Memory Support" depends on MACH_INGENIC || COMPILE_TEST diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index cdd01fbf13..eec7d78fe7 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -24,6 +24,8 @@ obj-$(CONFIG_NVMEM_IMX_OCOTP_ELE) += nvmem-imx-ocotp-ele.o nvmem-imx-ocotp-ele-y := imx-ocotp-ele.o obj-$(CONFIG_NVMEM_IMX_OCOTP_SCU) += nvmem-imx-ocotp-scu.o nvmem-imx-ocotp-scu-y := imx-ocotp-scu.o +obj-$(CONFIG_NVMEM_INGENIC_EFUSE) += nvmem-ingenic-efuse.o +nvmem-ingenic-efuse-y := ingenic-efuse.o obj-$(CONFIG_NVMEM_JZ4780_EFUSE) += nvmem_jz4780_efuse.o nvmem_jz4780_efuse-y := jz4780-efuse.o obj-$(CONFIG_NVMEM_LAN9662_OTPC) += nvmem-lan9662-otpc.o diff --git a/drivers/nvmem/ingenic-efuse.c b/drivers/nvmem/ingenic-efuse.c new file mode 100644 index 0000000000..c411fbed82 --- /dev/null +++ b/drivers/nvmem/ingenic-efuse.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 EFUSE nvmem. + * + * CLKGR bit 1 gates this block. A read while gated can hang the SoC, so + * the clock is enabled only around the access. The registers are words; + * each requested byte is taken from the little-endian word. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include + +struct ingenic_efuse { + void __iomem *base; + struct clk *clk; +}; + +static int ingenic_efuse_read(void *context, unsigned int offset, void *val, + size_t bytes) +{ + struct ingenic_efuse *priv = context; + u8 *out = val; + int ret; + + ret = clk_prepare_enable(priv->clk); + if (ret) + return ret; + + while (bytes--) { + u32 word = readl(priv->base + (offset & ~3U)); + + *out++ = word >> ((offset & 3) * 8); + offset++; + } + + clk_disable_unprepare(priv->clk); + return 0; +} + +static int ingenic_efuse_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct nvmem_config config = { }; + struct ingenic_efuse *priv; + struct nvmem_device *nvmem; + struct resource *res; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + priv->base = devm_ioremap_resource(dev, res); + if (IS_ERR(priv->base)) + return PTR_ERR(priv->base); + + priv->clk = devm_clk_get(dev, "efuse"); + if (IS_ERR(priv->clk)) + return PTR_ERR(priv->clk); + + config.dev = dev; + config.priv = priv; + config.size = resource_size(res); + config.stride = 1; + config.word_size = 1; + config.read_only = true; + config.root_only = true; + config.reg_read = ingenic_efuse_read; + config.owner = THIS_MODULE; + + nvmem = devm_nvmem_register(dev, &config); + return PTR_ERR_OR_ZERO(nvmem); +} + +static const struct of_device_id ingenic_efuse_of_match[] = { + { .compatible = "ingenic,t23-efuse" }, + { } +}; +MODULE_DEVICE_TABLE(of, ingenic_efuse_of_match); + +static struct platform_driver ingenic_efuse_driver = { + .probe = ingenic_efuse_probe, + .driver = { + .name = "ingenic-efuse", + .of_match_table = ingenic_efuse_of_match, + }, +}; +module_platform_driver(ingenic_efuse_driver); + +MODULE_AUTHOR("Difan Zhang"); +MODULE_DESCRIPTION("Ingenic T23 EFUSE nvmem"); +MODULE_LICENSE("GPL"); From 9b109a7ea2eff1553f5936fe82aea0be124205dd Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 45/78] dt-bindings: memory-controllers: Add Ingenic T23 DDR windows Describe the register windows, clocks, interrupts and other resources used by the T23 device. Signed-off-by: Difan Zhang --- .../memory-controllers/ingenic,t23-ddr.yaml | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Documentation/devicetree/bindings/memory-controllers/ingenic,t23-ddr.yaml diff --git a/Documentation/devicetree/bindings/memory-controllers/ingenic,t23-ddr.yaml b/Documentation/devicetree/bindings/memory-controllers/ingenic,t23-ddr.yaml new file mode 100644 index 0000000000..747ffb0c2f --- /dev/null +++ b/Documentation/devicetree/bindings/memory-controllers/ingenic,t23-ddr.yaml @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/memory-controllers/ingenic,t23-ddr.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 DDR Memory Controller + +maintainers: + - Paul Cercueil + +properties: + compatible: + const: ingenic,t23-ddr + + reg: + maxItems: 3 + + reg-names: + items: + - const: controller + - const: phy + - const: apb + +required: + - compatible + - reg + - reg-names + +additionalProperties: false + +examples: + - | + memory-controller@134f0000 { + compatible = "ingenic,t23-ddr"; + reg = <0x134f0000 0x400>, + <0x13011000 0x100>, + <0x13012000 0x100>; + reg-names = "controller", "phy", "apb"; + }; From e9c05bb90831972b6cb3e7b2b3f6d7fc48d2bad0 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:21:37 -0800 Subject: [PATCH 46/78] memory: Add Ingenic T23 DDR register window mapping Claim and map the DDR controller, PHY and APB register windows initialized by the bootloader. Signed-off-by: Difan Zhang --- drivers/memory/Kconfig | 8 ++++ drivers/memory/Makefile | 1 + drivers/memory/ingenic-t23-ddr.c | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 drivers/memory/ingenic-t23-ddr.c diff --git a/drivers/memory/Kconfig b/drivers/memory/Kconfig index c82d8d8a16..871bde7859 100644 --- a/drivers/memory/Kconfig +++ b/drivers/memory/Kconfig @@ -180,6 +180,14 @@ config JZ4780_NEMC the Ingenic JZ4780. This controller is used to handle external memory devices such as NAND and SRAM. +config INGENIC_T23_DDR + tristate "Ingenic T23 DDR controller windows" + depends on (MACH_INGENIC || COMPILE_TEST) && HAS_IOMEM + help + Map and claim the T23 DDR controller, PHY and APB register windows + initialized by early bootloader firmware. This driver does not + reprogram DRAM timing at runtime. + config MTK_SMI tristate "MediaTek SoC Memory Controller driver" if COMPILE_TEST depends on ARCH_MEDIATEK || COMPILE_TEST diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile index d2e6ca9abb..fefeaaa680 100644 --- a/drivers/memory/Makefile +++ b/drivers/memory/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_FSL_CORENET_CF) += fsl-corenet-cf.o obj-$(CONFIG_FSL_IFC) += fsl_ifc.o obj-$(CONFIG_MVEBU_DEVBUS) += mvebu-devbus.o obj-$(CONFIG_JZ4780_NEMC) += jz4780-nemc.o +obj-$(CONFIG_INGENIC_T23_DDR) += ingenic-t23-ddr.o obj-$(CONFIG_MTK_SMI) += mtk-smi.o obj-$(CONFIG_DA8XX_DDRCTL) += da8xx-ddrctl.o obj-$(CONFIG_PL353_SMC) += pl353-smc.o diff --git a/drivers/memory/ingenic-t23-ddr.c b/drivers/memory/ingenic-t23-ddr.c new file mode 100644 index 0000000000..6239c5a651 --- /dev/null +++ b/drivers/memory/ingenic-t23-ddr.c @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 DDR windows. + * + * Boot has already programmed timing. This driver only maps the + * controller, PHY, and APB ranges from the device tree. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include + +static const char * const ingenic_t23_ddr_windows[] = { + "controller", + "phy", + "apb", +}; + +struct ingenic_t23_ddr { + void __iomem *base[ARRAY_SIZE(ingenic_t23_ddr_windows)]; +}; + +static int ingenic_t23_ddr_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct ingenic_t23_ddr *ddr; + int i; + + ddr = devm_kzalloc(dev, sizeof(*ddr), GFP_KERNEL); + if (!ddr) + return -ENOMEM; + + for (i = 0; i < ARRAY_SIZE(ingenic_t23_ddr_windows); i++) { + struct resource *res; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + ingenic_t23_ddr_windows[i]); + ddr->base[i] = devm_ioremap_resource(dev, res); + if (IS_ERR(ddr->base[i])) + return PTR_ERR(ddr->base[i]); + } + + platform_set_drvdata(pdev, ddr); + dev_info(dev, "mapped controller, phy and apb\n"); + return 0; +} + +static const struct of_device_id ingenic_t23_ddr_of_match[] = { + { .compatible = "ingenic,t23-ddr" }, + { } +}; +MODULE_DEVICE_TABLE(of, ingenic_t23_ddr_of_match); + +static struct platform_driver ingenic_t23_ddr_driver = { + .probe = ingenic_t23_ddr_probe, + .driver = { + .name = "ingenic-t23-ddr", + .of_match_table = ingenic_t23_ddr_of_match, + }, +}; +module_platform_driver(ingenic_t23_ddr_driver); + +MODULE_AUTHOR("Difan Zhang"); +MODULE_DESCRIPTION("Ingenic T23 DDR window map"); +MODULE_LICENSE("GPL"); From a3d3b3f859598a634885de8e4638786279808b1a Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 47/78] dt-bindings: crypto: Add the Ingenic T23 AES engine Describe the register windows, clocks, interrupts and other resources used by the T23 device. Signed-off-by: Difan Zhang --- .../bindings/crypto/ingenic,t23-aes.yaml | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Documentation/devicetree/bindings/crypto/ingenic,t23-aes.yaml diff --git a/Documentation/devicetree/bindings/crypto/ingenic,t23-aes.yaml b/Documentation/devicetree/bindings/crypto/ingenic,t23-aes.yaml new file mode 100644 index 0000000000..b4fdf3a556 --- /dev/null +++ b/Documentation/devicetree/bindings/crypto/ingenic,t23-aes.yaml @@ -0,0 +1,44 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/crypto/ingenic,t23-aes.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 Hardware AES Engine + +maintainers: + - Paul Cercueil + +properties: + compatible: + const: ingenic,t23-aes + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: aes + +required: + - compatible + - reg + - interrupts + - clocks + +additionalProperties: false + +examples: + - | + crypto@13430000 { + compatible = "ingenic,t23-aes"; + reg = <0x13430000 0x1000>; + interrupts = <23>; + clocks = <&cgu 38>; + clock-names = "aes"; + }; From 717a95a595ae8934da0983cf7c49b0a666a7589f Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:21:38 -0800 Subject: [PATCH 48/78] crypto: ingenic: Add the T23 AES skcipher driver Expose ECB, CBC and CTR AES through skcipher using double-buffered DMA, clock gating and a software fallback for atomic contexts. Signed-off-by: Difan Zhang --- drivers/crypto/Kconfig | 16 + drivers/crypto/Makefile | 2 + drivers/crypto/ingenic-aes.c | 859 +++++++++++++++++++++++++++++++++++ 3 files changed, 877 insertions(+) create mode 100644 drivers/crypto/ingenic-aes.c diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig index 481be45ab7..6b47a0c364 100644 --- a/drivers/crypto/Kconfig +++ b/drivers/crypto/Kconfig @@ -675,6 +675,22 @@ config CRYPTO_DEV_IMGTEC_HASH hardware hash accelerator. Supporting MD5/SHA1/SHA224/SHA256 hashing algorithms. +config CRYPTO_DEV_INGENIC_AES + tristate "Ingenic T23 AES hardware accelerator" + depends on MACH_INGENIC || COMPILE_TEST + depends on OF && HAS_DMA + select CRYPTO_SKCIPHER + select CRYPTO_AES + select CRYPTO_ECB + select CRYPTO_CBC + select CRYPTO_CTR + help + Support for the AES hardware accelerator on Ingenic T23 SoCs, + implementing ECB, CBC, and CTR skcipher modes with DMA. + + To compile this driver as a module, choose M here: the module + will be called ingenic-crypto. + config CRYPTO_DEV_ROCKCHIP tristate "Rockchip's Cryptographic Engine driver" depends on OF && ARCH_ROCKCHIP diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile index ad4ccef67d..8cfabbfd5f 100644 --- a/drivers/crypto/Makefile +++ b/drivers/crypto/Makefile @@ -19,6 +19,8 @@ obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_COMMON) += caam/ obj-$(CONFIG_CRYPTO_DEV_GEODE) += geode-aes.o obj-$(CONFIG_CRYPTO_DEV_HIFN_795X) += hifn_795x.o obj-$(CONFIG_CRYPTO_DEV_IMGTEC_HASH) += img-hash.o +ingenic-crypto-objs := ingenic-aes.o +obj-$(CONFIG_CRYPTO_DEV_INGENIC_AES) += ingenic-crypto.o obj-$(CONFIG_CRYPTO_DEV_MARVELL) += marvell/ obj-$(CONFIG_CRYPTO_DEV_MXS_DCP) += mxs-dcp.o obj-$(CONFIG_CRYPTO_DEV_NIAGARA2) += n2_crypto.o diff --git a/drivers/crypto/ingenic-aes.c b/drivers/crypto/ingenic-aes.c new file mode 100644 index 0000000000..7df003ec01 --- /dev/null +++ b/drivers/crypto/ingenic-aes.c @@ -0,0 +1,859 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Ingenic T23 SoC Hardware AES Cryptographic Accelerator Driver + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* AES Register Offsets (matching vendor jz-aes.h) */ +#define AES_ASCR 0x00 /* AES Control Register */ +#define AES_ASSR 0x04 /* AES Status Register */ +#define AES_ASINTM 0x08 /* AES Interrupt Mask Register */ +#define AES_ASSA 0x0C /* AES DMA Source Address */ +#define AES_ASDA 0x10 /* AES DMA Destination Address */ +#define AES_ASTC 0x14 /* AES DMA Transfer Count (blocks) */ +#define AES_ASDI 0x18 /* AES Data Input */ +#define AES_ASDO 0x1C /* AES Data Output */ +#define AES_ASKY 0x20 /* AES Key Register */ +#define AES_ASIV 0x24 /* AES IV Register */ + +/* AES_ASCR Bitfields */ +#define AES_ASCR_CLR BIT(10) +#define AES_ASCR_DMAS BIT(9) +#define AES_ASCR_DMAE BIT(8) +#define AES_ASCR_KEYL_128B (0 << 6) +#define AES_ASCR_KEYL_192B (1 << 6) +#define AES_ASCR_KEYL_256B (2 << 6) +#define AES_ASCR_KEYL_MASK GENMASK(7, 6) +#define AES_ASCR_ECB_MODE (0 << 5) +#define AES_ASCR_CBC_MODE (1 << 5) +#define AES_ASCR_MODE_MASK BIT(5) +#define AES_ASCR_INEN_DAT (0 << 4) +#define AES_ASCR_INDE_DAT (1 << 4) +#define AES_ASCR_AESS BIT(3) +#define AES_ASCR_KEYS BIT(2) +#define AES_ASCR_INIT_IV BIT(1) +#define AES_ASCR_AES_EN BIT(0) + +/* AES_ASSR Bitfields; writing 1 toggles a bit, so clear by writing back what is set */ +#define AES_ASSR_DMAD BIT(2) +#define AES_ASSR_AESD BIT(1) +#define AES_ASSR_KEYD BIT(0) + +/* AES_ASINTM Bitfields (matching vendor jz-aes.h) */ +#define AES_ASINTM_DMA_INIT_M BIT(2) /* DMA interrupt mask */ +#define AES_ASINTM_AES_INIT_M BIT(1) /* AES done interrupt mask */ +#define AES_ASINTM_KEY_INIT_M BIT(0) /* Key done interrupt mask */ + +#define INGENIC_AES_SLOT_SIZE (16 * 1024) + +static unsigned int chunk_size = 8192; +module_param(chunk_size, uint, 0644); +MODULE_PARM_DESC(chunk_size, "bytes per DMA transfer (16..16384, multiple of 16)"); + +/* One DMA transfer: big-endian word images of its input and output. */ +struct ingenic_aes_slot { + u8 *in; + u8 *out; + dma_addr_t src; + dma_addr_t dst; + size_t len; +}; + +struct ingenic_aes_dev { + struct device *dev; + void __iomem *base; + struct clk *clk; + struct completion dma_done; + void *bounce_buf; + struct ingenic_aes_slot slot[2]; + size_t used; /* bytes of each slot buffer to scrub */ + bool dead; /* DMA timed out; the engine may still own the buffer */ +}; + +struct ingenic_aes_ctx { + u32 key[8]; + unsigned int keylen; + struct crypto_sync_skcipher *fallback_tfm; + bool fallback_is_ecb; +}; + +static struct ingenic_aes_dev *g_aes_dev; +static DEFINE_MUTEX(ingenic_aes_lock); + +static void ingenic_aes_clear_status(struct ingenic_aes_dev *dev, u32 mask) +{ + u32 status = readl(dev->base + AES_ASSR) & mask; + + if (status) + writel(status, dev->base + AES_ASSR); +} + +static int ingenic_aes_set_key_hw(struct ingenic_aes_dev *dev, u32 ascr, + const u32 *key, u32 keylen) +{ + u32 status; + int i, ret; + + ingenic_aes_clear_status(dev, AES_ASSR_KEYD); + for (i = 0; i < (keylen / 4); i++) + writel(get_unaligned_be32((const u8 *)key + 4 * i), + dev->base + AES_ASKY); + writel(ascr | AES_ASCR_KEYS, dev->base + AES_ASCR); + ret = readl_poll_timeout(dev->base + AES_ASSR, status, + status & AES_ASSR_KEYD, 2, 50000); + ingenic_aes_clear_status(dev, AES_ASSR_KEYD); + return ret; +} + +static void ingenic_aes_set_iv_hw(struct ingenic_aes_dev *dev, u32 ascr, + const u8 *iv) +{ + unsigned int i; + + for (i = 0; i < AES_BLOCK_SIZE; i += 4) + writel(get_unaligned_be32(iv + i), dev->base + AES_ASIV); + writel(ascr | AES_ASCR_INIT_IV, dev->base + AES_ASCR); +} + +static void ingenic_aes_reset(struct ingenic_aes_dev *dev) +{ + writel(0, dev->base + AES_ASINTM); + ingenic_aes_clear_status(dev, AES_ASSR_DMAD | AES_ASSR_AESD | + AES_ASSR_KEYD); + writel(AES_ASCR_CLR, dev->base + AES_ASCR); + writel(0, dev->base + AES_ASCR); + readl(dev->base + AES_ASCR); +} + +/* + * DMA moves big-endian 32-bit words, so byte i of a stream sits at i ^ 3 + * in a slot buffer. The copy below swaps while copying. + */ +static void ingenic_aes_copy_swab(u8 *dst, const u8 *src, size_t pos, + size_t n, bool to_slot) +{ + size_t i = 0; + + if (!((pos | (uintptr_t)(to_slot ? src : dst)) & 3)) { + u32 *d = (u32 *)(to_slot ? dst + pos : dst); + const u32 *s = (const u32 *)(to_slot ? src : src + pos); + + for (; i + 4 <= n; i += 4) + *d++ = swab32(*s++); + } + for (; i < n; i++) { + if (to_slot) + dst[(pos + i) ^ 3] = src[i]; + else + dst[i] = src[(pos + i) ^ 3]; + } +} + +static void ingenic_aes_load(u8 *slot, struct scatterlist *sg, size_t skip, + size_t len) +{ + struct sg_mapping_iter m; + size_t pos = 0; + + sg_miter_start(&m, sg, sg_nents(sg), SG_MITER_FROM_SG); + sg_miter_skip(&m, skip); + while (pos < len && sg_miter_next(&m)) { + size_t n = min(m.length, len - pos); + + ingenic_aes_copy_swab(slot, m.addr, pos, n, true); + m.consumed = n; + pos += n; + } + sg_miter_stop(&m); +} + +static void ingenic_aes_store(struct scatterlist *sg, size_t skip, + const u8 *slot, size_t len) +{ + struct sg_mapping_iter m; + size_t pos = 0; + + sg_miter_start(&m, sg, sg_nents(sg), SG_MITER_TO_SG); + sg_miter_skip(&m, skip); + while (pos < len && sg_miter_next(&m)) { + size_t n = min(m.length, len - pos); + + ingenic_aes_copy_swab(m.addr, slot, pos, n, false); + m.consumed = n; + pos += n; + } + sg_miter_stop(&m); +} + +/* dst = src ^ keystream in one pass; src and dst may be the same list. */ +static void ingenic_aes_xor_store(struct scatterlist *dsg, + struct scatterlist *ssg, size_t skip, + const u8 *ks, size_t len) +{ + struct sg_mapping_iter d, m; + size_t pos = 0; + + sg_miter_start(&m, ssg, sg_nents(ssg), SG_MITER_FROM_SG); + sg_miter_start(&d, dsg, sg_nents(dsg), SG_MITER_TO_SG); + sg_miter_skip(&m, skip); + sg_miter_skip(&d, skip); + while (pos < len && sg_miter_next(&m) && sg_miter_next(&d)) { + size_t n = min3(m.length, d.length, len - pos); + const u8 *in = m.addr; + u8 *out = d.addr; + size_t i = 0; + + if (!((pos | (uintptr_t)in | (uintptr_t)out) & 3)) { + const u32 *k = (const u32 *)(ks + pos); + + for (; i + 4 <= n; i += 4) + *(u32 *)(out + i) = *(const u32 *)(in + i) ^ + swab32(*k++); + } + for (; i < n; i++) + out[i] = in[i] ^ ks[(pos + i) ^ 3]; + m.consumed = n; + d.consumed = n; + pos += n; + } + sg_miter_stop(&d); + sg_miter_stop(&m); +} + +/* Counter blocks for one CTR transfer, written as big-endian words. */ +static void ingenic_aes_ctr_fill(u8 *slot, u8 *ctr, size_t len) +{ + size_t pos; + + for (pos = 0; pos < len; pos += AES_BLOCK_SIZE) { + ingenic_aes_copy_swab(slot, ctr, pos, AES_BLOCK_SIZE, true); + crypto_inc(ctr, AES_BLOCK_SIZE); + } +} + +/* Program and start one transfer; ingenic_aes_finish() waits for it. */ +static int ingenic_aes_start(struct ingenic_aes_dev *dev, + struct ingenic_aes_ctx *ctx, + struct ingenic_aes_slot *sl, size_t len, + const u8 *iv, bool is_cbc, bool encrypt) +{ + u32 ascr, status; + int ret; + + sl->len = len; + sl->src = dma_map_single(dev->dev, sl->in, len, DMA_TO_DEVICE); + if (dma_mapping_error(dev->dev, sl->src)) + return -ENOMEM; + sl->dst = dma_map_single(dev->dev, sl->out, len, DMA_FROM_DEVICE); + if (dma_mapping_error(dev->dev, sl->dst)) { + dma_unmap_single(dev->dev, sl->src, len, DMA_TO_DEVICE); + return -ENOMEM; + } + + ascr = AES_ASCR_AES_EN; + if (is_cbc) + ascr |= AES_ASCR_CBC_MODE; + if (!encrypt) + ascr |= AES_ASCR_INDE_DAT; + if (ctx->keylen == AES_KEYSIZE_192) + ascr |= AES_ASCR_KEYL_192B; + else if (ctx->keylen == AES_KEYSIZE_256) + ascr |= AES_ASCR_KEYL_256B; + + ingenic_aes_reset(dev); + writel(ascr, dev->base + AES_ASCR); + writel(ascr | AES_ASCR_CLR, dev->base + AES_ASCR); + ret = readl_poll_timeout(dev->base + AES_ASCR, status, + !!(status & AES_ASCR_CBC_MODE) == is_cbc, + 2, 50000); + if (ret) + goto fail; + ascr |= AES_ASCR_DMAE; + writel(ascr, dev->base + AES_ASCR); + + ret = ingenic_aes_set_key_hw(dev, ascr, ctx->key, ctx->keylen); + if (ret) + goto fail; + if (is_cbc) + ingenic_aes_set_iv_hw(dev, ascr, iv); + + writel(len / AES_BLOCK_SIZE, dev->base + AES_ASTC); + writel(sl->src, dev->base + AES_ASSA); + writel(sl->dst, dev->base + AES_ASDA); + ingenic_aes_clear_status(dev, AES_ASSR_DMAD | AES_ASSR_AESD); + reinit_completion(&dev->dma_done); + writel(AES_ASINTM_DMA_INIT_M, dev->base + AES_ASINTM); + writel(ascr | AES_ASCR_DMAS, dev->base + AES_ASCR); + return 0; + +fail: + ingenic_aes_reset(dev); + dma_unmap_single(dev->dev, sl->dst, len, DMA_FROM_DEVICE); + dma_unmap_single(dev->dev, sl->src, len, DMA_TO_DEVICE); + return ret; +} + +static int ingenic_aes_finish(struct ingenic_aes_dev *dev, + struct ingenic_aes_slot *sl) +{ + int ret = 0; + + if (!wait_for_completion_timeout(&dev->dma_done, msecs_to_jiffies(500))) { + dev_err(dev->dev, "DMA timeout: ASCR %08x ASSR %08x\n", + readl(dev->base + AES_ASCR), readl(dev->base + AES_ASSR)); + /* An engine that did not finish might still write; never reuse it. */ + dev->dead = true; + ret = -ETIMEDOUT; + } + ingenic_aes_reset(dev); + dma_unmap_single(dev->dev, sl->dst, sl->len, DMA_FROM_DEVICE); + dma_unmap_single(dev->dev, sl->src, sl->len, DMA_TO_DEVICE); + return ret; +} + +static irqreturn_t ingenic_aes_irq(int irq, void *data) +{ + struct ingenic_aes_dev *dev = data; + + if (!(readl(dev->base + AES_ASSR) & AES_ASSR_DMAD)) + return IRQ_NONE; + writel(0, dev->base + AES_ASINTM); + ingenic_aes_clear_status(dev, AES_ASSR_DMAD); + complete(&dev->dma_done); + return IRQ_HANDLED; +} + +static inline size_t ingenic_aes_len_at(size_t off, size_t total, size_t step) +{ + return min(step, total - off); +} + +static inline size_t ingenic_aes_dma_len(size_t off, size_t total, size_t step, + bool is_ctr) +{ + size_t len = ingenic_aes_len_at(off, total, step); + + return is_ctr ? round_up(len, AES_BLOCK_SIZE) : len; +} + +/* + * Transfers alternate between two slots: while the engine runs one, the + * CPU loads the next and stores the previous. CBC chaining values come + * from the slot buffers, so in-place requests are safe. + */ +static int ingenic_aes_crypt_dma(struct ingenic_aes_dev *dev, + struct ingenic_aes_ctx *ctx, + struct skcipher_request *req, + bool is_cbc, bool is_ctr, bool encrypt) +{ + size_t total = req->cryptlen, step, off, next; + u8 iv[AES_BLOCK_SIZE], ctr[AES_BLOCK_SIZE]; + unsigned int k = 0; + int ret; + + step = clamp_t(size_t, round_down(READ_ONCE(chunk_size), AES_BLOCK_SIZE), + AES_BLOCK_SIZE, INGENIC_AES_SLOT_SIZE); + dev->used = round_up(min(total, step), AES_BLOCK_SIZE); + if (is_ctr) + memcpy(ctr, req->iv, AES_BLOCK_SIZE); + else if (is_cbc) + memcpy(iv, req->iv, AES_BLOCK_SIZE); + + if (is_ctr) + ingenic_aes_ctr_fill(dev->slot[0].in, ctr, + ingenic_aes_dma_len(0, total, step, is_ctr)); + else + ingenic_aes_load(dev->slot[0].in, req->src, 0, + ingenic_aes_len_at(0, total, step)); + ret = ingenic_aes_start(dev, ctx, &dev->slot[0], + ingenic_aes_dma_len(0, total, step, is_ctr), + iv, is_cbc, encrypt); + if (ret) + return ret; + + for (off = 0; off < total; off = next, k ^= 1) { + struct ingenic_aes_slot *cur = &dev->slot[k]; + struct ingenic_aes_slot *nxt = &dev->slot[k ^ 1]; + size_t len = ingenic_aes_len_at(off, total, step); + + next = off + len; + if (next < total) { + if (is_ctr) + ingenic_aes_ctr_fill(nxt->in, ctr, + ingenic_aes_dma_len(next, total, step, is_ctr)); + else + ingenic_aes_load(nxt->in, req->src, next, + ingenic_aes_len_at(next, total, step)); + } + ret = ingenic_aes_finish(dev, cur); + if (ret) + return ret; + /* The next CBC transfer chains from this one's last ciphertext block. */ + if (is_cbc) + ingenic_aes_copy_swab(iv, encrypt ? cur->out : cur->in, + len - AES_BLOCK_SIZE, AES_BLOCK_SIZE, false); + if (next < total) { + ret = ingenic_aes_start(dev, ctx, nxt, + ingenic_aes_dma_len(next, total, step, is_ctr), + iv, is_cbc, encrypt); + if (ret) + return ret; + } + if (is_ctr) + ingenic_aes_xor_store(req->dst, req->src, off, cur->out, len); + else + ingenic_aes_store(req->dst, off, cur->out, len); + } + + if (is_ctr) + memcpy(req->iv, ctr, AES_BLOCK_SIZE); + else if (is_cbc) + memcpy(req->iv, iv, AES_BLOCK_SIZE); + return 0; +} + +static int ingenic_aes_fallback(struct ingenic_aes_ctx *ctx, + struct skcipher_request *req, bool encrypt) +{ + SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback_tfm); + int ret; + + skcipher_request_set_sync_tfm(subreq, ctx->fallback_tfm); + skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL); + skcipher_request_set_crypt(subreq, req->src, req->dst, + req->cryptlen, req->iv); + if (encrypt) + ret = crypto_skcipher_encrypt(subreq); + else + ret = crypto_skcipher_decrypt(subreq); + skcipher_request_zero(subreq); + return ret; +} + +/* Request data does not outlive the request in the shared slot buffers. */ +static void ingenic_aes_scrub(struct ingenic_aes_dev *dev) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(dev->slot); i++) { + memzero_explicit(dev->slot[i].in, dev->used); + memzero_explicit(dev->slot[i].out, dev->used); + } + dev->used = 0; +} + +static int ingenic_aes_crypt(struct skcipher_request *req, bool is_cbc, bool encrypt) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct ingenic_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + struct ingenic_aes_dev *dev; + int ret; + + if (!req->cryptlen) + return 0; + + if (req->cryptlen % AES_BLOCK_SIZE) + return -EINVAL; + + if (sg_nents_for_len(req->src, req->cryptlen) < 0 || + sg_nents_for_len(req->dst, req->cryptlen) < 0) + return -EINVAL; + + /* Atomic callers cannot wait for hardware completion. */ + if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)) + return ingenic_aes_fallback(ctx, req, encrypt); + + mutex_lock(&ingenic_aes_lock); + dev = g_aes_dev; + if (dev && !dev->dead) { + ret = ingenic_aes_crypt_dma(dev, ctx, req, is_cbc, false, encrypt); + ingenic_aes_scrub(dev); + } else if (dev) { + ret = ingenic_aes_fallback(ctx, req, encrypt); + } else { + ret = -ENODEV; + } + mutex_unlock(&ingenic_aes_lock); + + return ret; +} + +static int ingenic_aes_ctr_fallback(struct ingenic_aes_ctx *ctx, + struct skcipher_request *req) +{ + if (!ctx->fallback_is_ecb) { + SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback_tfm); + int ret; + + skcipher_request_set_sync_tfm(subreq, ctx->fallback_tfm); + skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL); + skcipher_request_set_crypt(subreq, req->src, req->dst, + req->cryptlen, req->iv); + ret = crypto_skcipher_encrypt(subreq); + skcipher_request_zero(subreq); + return ret; + } else { + size_t remaining = req->cryptlen; + size_t offset = 0; + u8 iv[AES_BLOCK_SIZE]; + u8 ks[AES_BLOCK_SIZE]; + u8 in_buf[AES_BLOCK_SIZE]; + struct scatterlist sg_in, sg_out; + SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback_tfm); + int ret = 0; + + memcpy(iv, req->iv, AES_BLOCK_SIZE); + + skcipher_request_set_sync_tfm(subreq, ctx->fallback_tfm); + skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL); + + while (remaining > 0) { + size_t chunk = min_t(size_t, remaining, AES_BLOCK_SIZE); + + memcpy(in_buf, iv, AES_BLOCK_SIZE); + crypto_inc(iv, AES_BLOCK_SIZE); + + sg_init_one(&sg_in, in_buf, AES_BLOCK_SIZE); + sg_init_one(&sg_out, ks, AES_BLOCK_SIZE); + skcipher_request_set_crypt(subreq, &sg_in, &sg_out, + AES_BLOCK_SIZE, NULL); + ret = crypto_skcipher_encrypt(subreq); + if (ret) + break; + + scatterwalk_map_and_copy(in_buf, req->src, offset, chunk, 0); + crypto_xor(in_buf, ks, chunk); + scatterwalk_map_and_copy(in_buf, req->dst, offset, chunk, 1); + + offset += chunk; + remaining -= chunk; + } + + skcipher_request_zero(subreq); + if (!ret) + memcpy(req->iv, iv, AES_BLOCK_SIZE); + return ret; + } +} + +static int ingenic_aes_ctr_crypt(struct skcipher_request *req) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct ingenic_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + struct ingenic_aes_dev *dev; + int ret; + + if (!req->cryptlen) + return 0; + + if (sg_nents_for_len(req->src, req->cryptlen) < 0 || + sg_nents_for_len(req->dst, req->cryptlen) < 0) + return -EINVAL; + + /* Atomic callers cannot wait for hardware completion. */ + if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)) + return ingenic_aes_ctr_fallback(ctx, req); + + mutex_lock(&ingenic_aes_lock); + dev = g_aes_dev; + if (dev && !dev->dead) { + ret = ingenic_aes_crypt_dma(dev, ctx, req, false, true, true); + ingenic_aes_scrub(dev); + } else if (dev) { + ret = ingenic_aes_ctr_fallback(ctx, req); + } else { + ret = -ENODEV; + } + mutex_unlock(&ingenic_aes_lock); + + return ret; +} + +static int ingenic_aes_ecb_encrypt(struct skcipher_request *req) +{ + return ingenic_aes_crypt(req, false, true); +} + +static int ingenic_aes_ecb_decrypt(struct skcipher_request *req) +{ + return ingenic_aes_crypt(req, false, false); +} + +static int ingenic_aes_cbc_encrypt(struct skcipher_request *req) +{ + return ingenic_aes_crypt(req, true, true); +} + +static int ingenic_aes_cbc_decrypt(struct skcipher_request *req) +{ + return ingenic_aes_crypt(req, true, false); +} + +static int ingenic_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, + unsigned int keylen) +{ + struct ingenic_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + int ret; + + if (keylen != AES_KEYSIZE_128 && + keylen != AES_KEYSIZE_192 && + keylen != AES_KEYSIZE_256) + return -EINVAL; + + crypto_sync_skcipher_clear_flags(ctx->fallback_tfm, CRYPTO_TFM_REQ_MASK); + crypto_sync_skcipher_set_flags(ctx->fallback_tfm, + crypto_skcipher_get_flags(tfm) & + CRYPTO_TFM_REQ_MASK); + ret = crypto_sync_skcipher_setkey(ctx->fallback_tfm, key, keylen); + if (ret) + return ret; + memzero_explicit(ctx->key, sizeof(ctx->key)); + memcpy(ctx->key, key, keylen); + ctx->keylen = keylen; + return 0; +} + +static int ingenic_aes_init_tfm(struct crypto_skcipher *tfm) +{ + struct ingenic_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + const char *alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm)); + + ctx->fallback_tfm = crypto_alloc_sync_skcipher(alg_name, 0, + CRYPTO_ALG_NEED_FALLBACK); + if (IS_ERR(ctx->fallback_tfm) && !strcmp(alg_name, "ctr(aes)")) { + ctx->fallback_tfm = crypto_alloc_sync_skcipher("ecb(aes)", 0, + CRYPTO_ALG_NEED_FALLBACK); + if (!IS_ERR(ctx->fallback_tfm)) + ctx->fallback_is_ecb = true; + } + if (IS_ERR(ctx->fallback_tfm)) { + if (g_aes_dev && g_aes_dev->dev) + dev_err(g_aes_dev->dev, "failed to allocate fallback for %s: %ld\n", + alg_name, PTR_ERR(ctx->fallback_tfm)); + else + pr_err("ingenic-aes: failed to allocate fallback for %s: %ld\n", + alg_name, PTR_ERR(ctx->fallback_tfm)); + return PTR_ERR(ctx->fallback_tfm); + } + + return 0; +} + +static void ingenic_aes_exit_tfm(struct crypto_skcipher *tfm) +{ + struct ingenic_aes_ctx *ctx = crypto_skcipher_ctx(tfm); + + if (ctx->fallback_tfm) + crypto_free_sync_skcipher(ctx->fallback_tfm); + memzero_explicit(ctx->key, sizeof(ctx->key)); +} + +static struct skcipher_alg ingenic_aes_algs[] = { + { + .base.cra_name = "ecb(aes)", + .base.cra_driver_name = "ecb-aes-ingenic", + .base.cra_priority = 300, + .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | + CRYPTO_ALG_KERN_DRIVER_ONLY | + CRYPTO_ALG_NEED_FALLBACK, + .base.cra_blocksize = AES_BLOCK_SIZE, + .base.cra_ctxsize = sizeof(struct ingenic_aes_ctx), + .base.cra_alignmask = 0, + .base.cra_module = THIS_MODULE, + .init = ingenic_aes_init_tfm, + .exit = ingenic_aes_exit_tfm, + .setkey = ingenic_aes_setkey, + .encrypt = ingenic_aes_ecb_encrypt, + .decrypt = ingenic_aes_ecb_decrypt, + .min_keysize = AES_MIN_KEY_SIZE, + .max_keysize = AES_MAX_KEY_SIZE, + .chunksize = AES_BLOCK_SIZE, + .walksize = AES_BLOCK_SIZE, + }, + { + .base.cra_name = "cbc(aes)", + .base.cra_driver_name = "cbc-aes-ingenic", + .base.cra_priority = 300, + .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | + CRYPTO_ALG_KERN_DRIVER_ONLY | + CRYPTO_ALG_NEED_FALLBACK, + .base.cra_blocksize = AES_BLOCK_SIZE, + .base.cra_ctxsize = sizeof(struct ingenic_aes_ctx), + .base.cra_alignmask = 0, + .base.cra_module = THIS_MODULE, + .init = ingenic_aes_init_tfm, + .exit = ingenic_aes_exit_tfm, + .setkey = ingenic_aes_setkey, + .encrypt = ingenic_aes_cbc_encrypt, + .decrypt = ingenic_aes_cbc_decrypt, + .min_keysize = AES_MIN_KEY_SIZE, + .max_keysize = AES_MAX_KEY_SIZE, + .ivsize = AES_BLOCK_SIZE, + .chunksize = AES_BLOCK_SIZE, + .walksize = AES_BLOCK_SIZE, + }, + { + .base.cra_name = "ctr(aes)", + .base.cra_driver_name = "ctr-aes-ingenic", + .base.cra_priority = 300, + .base.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER | + CRYPTO_ALG_KERN_DRIVER_ONLY | + CRYPTO_ALG_NEED_FALLBACK, + .base.cra_blocksize = 1, + .base.cra_ctxsize = sizeof(struct ingenic_aes_ctx), + .base.cra_alignmask = 0, + .base.cra_module = THIS_MODULE, + .init = ingenic_aes_init_tfm, + .exit = ingenic_aes_exit_tfm, + .setkey = ingenic_aes_setkey, + .encrypt = ingenic_aes_ctr_crypt, + .decrypt = ingenic_aes_ctr_crypt, + .min_keysize = AES_MIN_KEY_SIZE, + .max_keysize = AES_MAX_KEY_SIZE, + .ivsize = AES_BLOCK_SIZE, + .chunksize = AES_BLOCK_SIZE, + .walksize = AES_BLOCK_SIZE, + }, +}; + +static int ingenic_aes_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct ingenic_aes_dev *aes; + struct resource *res; + int ret, irq, i; + + aes = devm_kzalloc(dev, sizeof(*aes), GFP_KERNEL); + if (!aes) + return -ENOMEM; + + aes->dev = dev; + init_completion(&aes->dma_done); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + aes->base = devm_ioremap_resource(dev, res); + if (IS_ERR(aes->base)) + return PTR_ERR(aes->base); + + aes->clk = devm_clk_get(dev, "aes"); + if (IS_ERR(aes->clk)) { + dev_err(dev, "failed to get aes clock: %ld\n", PTR_ERR(aes->clk)); + return PTR_ERR(aes->clk); + } + + ret = clk_prepare_enable(aes->clk); + if (ret) { + dev_err(dev, "failed to enable aes clock: %d\n", ret); + return ret; + } + + irq = platform_get_irq(pdev, 0); + if (irq < 0) { + ret = irq; + goto err_clk; + } + ingenic_aes_reset(aes); + ret = devm_request_irq(dev, irq, ingenic_aes_irq, 0, dev_name(dev), aes); + if (ret) + goto err_clk; + + aes->bounce_buf = kmalloc(4 * INGENIC_AES_SLOT_SIZE, GFP_KERNEL); + if (!aes->bounce_buf) { + ret = -ENOMEM; + goto err_clk; + } + for (i = 0; i < ARRAY_SIZE(aes->slot); i++) { + aes->slot[i].in = aes->bounce_buf + 2 * i * INGENIC_AES_SLOT_SIZE; + aes->slot[i].out = aes->slot[i].in + INGENIC_AES_SLOT_SIZE; + } + + ingenic_aes_reset(aes); + mutex_lock(&ingenic_aes_lock); + if (g_aes_dev) { + mutex_unlock(&ingenic_aes_lock); + ret = -EBUSY; + goto err_free; + } + g_aes_dev = aes; + mutex_unlock(&ingenic_aes_lock); + + platform_set_drvdata(pdev, aes); + + ret = crypto_register_skciphers(ingenic_aes_algs, ARRAY_SIZE(ingenic_aes_algs)); + if (ret) { + dev_err(dev, "failed to register skciphers: %d\n", ret); + goto err_bounce; + } + + dev_dbg(dev, "AES accelerator registered (DMA)\n"); + return 0; + +err_bounce: + mutex_lock(&ingenic_aes_lock); + g_aes_dev = NULL; + mutex_unlock(&ingenic_aes_lock); +err_free: + kfree(aes->bounce_buf); +err_clk: + clk_disable_unprepare(aes->clk); + return ret; +} + +static void ingenic_aes_remove(struct platform_device *pdev) +{ + struct ingenic_aes_dev *aes = platform_get_drvdata(pdev); + + crypto_unregister_skciphers(ingenic_aes_algs, ARRAY_SIZE(ingenic_aes_algs)); + + mutex_lock(&ingenic_aes_lock); + g_aes_dev = NULL; + mutex_unlock(&ingenic_aes_lock); + + ingenic_aes_reset(aes); + clk_disable_unprepare(aes->clk); + /* After a DMA timeout the buffer is leaked rather than handed back. */ + if (!aes->dead) + kfree(aes->bounce_buf); +} + +static const struct of_device_id ingenic_aes_of_match[] = { + { .compatible = "ingenic,t23-aes" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, ingenic_aes_of_match); + +static struct platform_driver ingenic_aes_driver = { + .probe = ingenic_aes_probe, + .remove = ingenic_aes_remove, + .driver = { + .name = "ingenic-aes", + .of_match_table = ingenic_aes_of_match, + }, +}; +module_platform_driver(ingenic_aes_driver); + +MODULE_DESCRIPTION("Ingenic T23 Hardware AES Accelerator Driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); From c73581d97f660f721005b3928db9affb78850d64 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 49/78] dt-bindings: iio: adc: ingenic: Add the T23 SAR ADC Describe the register windows, clocks, interrupts and other resources used by the T23 device. Signed-off-by: Difan Zhang --- .../bindings/iio/adc/ingenic,adc.yaml | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/Documentation/devicetree/bindings/iio/adc/ingenic,adc.yaml b/Documentation/devicetree/bindings/iio/adc/ingenic,adc.yaml index 9cd0fd5397..b1da55c095 100644 --- a/Documentation/devicetree/bindings/iio/adc/ingenic,adc.yaml +++ b/Documentation/devicetree/bindings/iio/adc/ingenic,adc.yaml @@ -26,6 +26,7 @@ properties: - ingenic,jz4760-adc - ingenic,jz4760b-adc - ingenic,jz4770-adc + - ingenic,t23-sadc '#io-channel-cells': const: 1 @@ -39,8 +40,9 @@ properties: maxItems: 1 clock-names: - items: - - const: adc + enum: + - adc + - sadc interrupts: maxItems: 1 @@ -52,15 +54,29 @@ properties: which does not have such a divider. type: boolean -if: - not: - properties: - compatible: - contains: - const: ingenic,jz4760b-adc -then: - properties: - ingenic,use-internal-divider: false +allOf: + - if: + not: + properties: + compatible: + contains: + const: ingenic,jz4760b-adc + then: + properties: + ingenic,use-internal-divider: false + - if: + properties: + compatible: + contains: + const: ingenic,t23-sadc + then: + properties: + clock-names: + const: sadc + else: + properties: + clock-names: + const: adc required: - compatible From 064195d30463135fe8cd943c52934a773088cf12 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:21:38 -0800 Subject: [PATCH 50/78] iio: adc: Add the Ingenic T23 SAR ADC Expose the AUX0 voltage channel and scale through IIO with interrupt-driven conversion completion. Signed-off-by: Difan Zhang --- drivers/iio/adc/Kconfig | 11 ++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ingenic-sadc.c | 268 +++++++++++++++++++++++++++++++++ 3 files changed, 280 insertions(+) create mode 100644 drivers/iio/adc/ingenic-sadc.c diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 5b80bb3d92..ec81c42354 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -631,6 +631,17 @@ config INGENIC_ADC This driver can also be built as a module. If so, the module will be called ingenic_adc. +config INGENIC_T23_SADC + tristate "Ingenic T23 SAR ADC driver" + depends on MACH_INGENIC || COMPILE_TEST + depends on OF && HAS_IOMEM + help + Say yes here to build support for the Successive Approximation + Register (SAR) ADC unit on Ingenic T23 SoCs. + + To compile this driver as a module, choose M here: the module + will be called ingenic-sadc. + config INTEL_MRFLD_ADC tristate "Intel Merrifield Basin Cove ADC driver" depends on INTEL_SOC_PMIC_MRFLD diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index c64cc574d4..9ed5f685a1 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -61,6 +61,7 @@ obj-$(CONFIG_IMX8QXP_ADC) += imx8qxp-adc.o obj-$(CONFIG_IMX93_ADC) += imx93_adc.o obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o obj-$(CONFIG_INGENIC_ADC) += ingenic-adc.o +obj-$(CONFIG_INGENIC_T23_SADC) += ingenic-sadc.o obj-$(CONFIG_INTEL_MRFLD_ADC) += intel_mrfld_adc.o obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o diff --git a/drivers/iio/adc/ingenic-sadc.c b/drivers/iio/adc/ingenic-sadc.c new file mode 100644 index 0000000000..5f0b12bc3c --- /dev/null +++ b/drivers/iio/adc/ingenic-sadc.c @@ -0,0 +1,268 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 auxiliary ADC + * Copyright (C) 2012 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Ingenic SADC Register Offsets (matching vendor jz_adc_v13.c / jz_adc.h) + */ +#define SADC_ENABLE 0x00 /* JZ_REG_ADC_ENABLE / SADC_ADENA_OFFSET */ +#define SADC_CTRL 0x08 /* JZ_REG_ADC_CTRL / SADC_ADCTRL_OFFSET */ +#define SADC_STATUS 0x0c /* JZ_REG_ADC_STATUS / SADC_ADSTATE_OFFSET */ +#define SADC_AUX_DATA 0x10 /* JZ_REG_ADC_AUX_BASE */ +#define SADC_CLKDIV 0x20 /* JZ_REG_ADC_CLKDIV */ + +/* SADC_ENABLE bits */ +#define SADC_SLEEP BIT(15) /* Sleep mode control (16-bit access) */ +#define SADC_AUXEN BIT(0) /* ADENA_AUXEN: AUX channel enable */ + +/* SADC_STATUS bits */ +#define SADC_STATUS_ARDY BIT(0) /* ADSTATE_ARDY: AUX data ready flag */ + +/* SADC_CTRL interrupt mask bits */ +#define SADC_CTRL_ARDYM BIT(0) /* ADCTRL_ARDYM: AUX ready interrupt mask */ +#define SADC_CTRL_MASK_ALL GENMASK(7, 0) /* 0xff: mask all 8-bit IRQs */ +#define SADC_CTRL_MASK_ALL_16 GENMASK(15, 0) /* 0xffff: mask all 16-bit IRQs */ + +/* SADC_AUX_DATA conversion output mask */ +#define SADC_DATA_MASK GENMASK(11, 0) /* ADADAT_ADATA_MASK: 12-bit ADC data */ + +/* SADC_CLKDIV clock divider configuration */ +#define SADC_CLKDIV_DIV 12 +#define SADC_CLKDIV_US 2 +#define SADC_CLKDIV_MS 200 +#define SADC_CLKDIV_VAL (((SADC_CLKDIV_MS - 1) << 16) | \ + ((SADC_CLKDIV_US - 1) << 8) | \ + (SADC_CLKDIV_DIV - 1)) + +struct ingenic_sadc { + void __iomem *base; + struct clk *clk; + struct mutex lock; + struct completion completion; + int irq; +}; + +static const struct iio_chan_spec ingenic_sadc_channels[] = { + { + .type = IIO_VOLTAGE, + .indexed = 1, + .channel = 0, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .datasheet_name = "AUX0", + }, +}; + +static irqreturn_t ingenic_sadc_irq(int irq, void *dev_id) +{ + struct ingenic_sadc *sadc = dev_id; + u8 status = readb(sadc->base + SADC_STATUS); + + if (status & SADC_STATUS_ARDY) { + writeb(SADC_CTRL_MASK_ALL, sadc->base + SADC_CTRL); + writeb(SADC_STATUS_ARDY, sadc->base + SADC_STATUS); + complete(&sadc->completion); + return IRQ_HANDLED; + } + + return IRQ_NONE; +} + +static void ingenic_sadc_mask_and_drain(struct ingenic_sadc *sadc) +{ + writeb(SADC_CTRL_MASK_ALL, sadc->base + SADC_CTRL); + synchronize_irq(sadc->irq); + writew(0, sadc->base + SADC_ENABLE); + writeb(SADC_STATUS_ARDY, sadc->base + SADC_STATUS); +} + +static int ingenic_sadc_read_channel(struct ingenic_sadc *sadc, int channel, int *val) +{ + long ret; + + if (channel != 0) + return -EINVAL; + + ret = mutex_lock_interruptible(&sadc->lock); + if (ret) + return ret; + + /* CTRL bits mask interrupts; drain a previous conversion first. */ + ingenic_sadc_mask_and_drain(sadc); + reinit_completion(&sadc->completion); + + writeb(SADC_CTRL_MASK_ALL & ~SADC_CTRL_ARDYM, sadc->base + SADC_CTRL); + writew(SADC_AUXEN, sadc->base + SADC_ENABLE); + + ret = wait_for_completion_interruptible_timeout(&sadc->completion, + msecs_to_jiffies(500)); + if (ret > 0) { + *val = readl(sadc->base + SADC_AUX_DATA) & SADC_DATA_MASK; + ret = 0; + } else if (!ret) { + ret = -ETIMEDOUT; + } + + ingenic_sadc_mask_and_drain(sadc); + mutex_unlock(&sadc->lock); + return ret; +} + +static int ingenic_sadc_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct ingenic_sadc *sadc = iio_priv(indio_dev); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + ret = ingenic_sadc_read_channel(sadc, chan->channel, val); + if (ret) + return ret; + return IIO_VAL_INT; + + case IIO_CHAN_INFO_SCALE: + *val = 1800; + *val2 = 12; + return IIO_VAL_FRACTIONAL_LOG2; + + default: + return -EINVAL; + } +} + +static const struct iio_info ingenic_sadc_info = { + .read_raw = ingenic_sadc_read_raw, +}; + +static void ingenic_sadc_hw_init(struct ingenic_sadc *sadc) +{ + writew(SADC_SLEEP, sadc->base + SADC_ENABLE); + writew(SADC_CTRL_MASK_ALL_16, sadc->base + SADC_CTRL); + msleep(5); + writew(0, sadc->base + SADC_ENABLE); + writel(SADC_CLKDIV_VAL, sadc->base + SADC_CLKDIV); + writeb(SADC_STATUS_ARDY, sadc->base + SADC_STATUS); +} + +static int ingenic_sadc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct iio_dev *indio_dev; + struct ingenic_sadc *sadc; + struct resource *res; + int ret; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*sadc)); + if (!indio_dev) + return -ENOMEM; + + sadc = iio_priv(indio_dev); + mutex_init(&sadc->lock); + init_completion(&sadc->completion); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + sadc->base = devm_ioremap_resource(dev, res); + if (IS_ERR(sadc->base)) + return PTR_ERR(sadc->base); + + sadc->clk = devm_clk_get(dev, "sadc"); + if (IS_ERR(sadc->clk)) { + dev_err(dev, "failed to get sadc clock: %ld\n", PTR_ERR(sadc->clk)); + return PTR_ERR(sadc->clk); + } + + ret = clk_prepare_enable(sadc->clk); + if (ret) { + dev_err(dev, "failed to enable sadc clock: %d\n", ret); + return ret; + } + + ingenic_sadc_hw_init(sadc); + + sadc->irq = platform_get_irq(pdev, 0); + if (sadc->irq < 0) { + dev_err(dev, "failed to get IRQ: %d\n", sadc->irq); + ret = sadc->irq; + goto err_clk; + } + + ret = devm_request_irq(dev, sadc->irq, ingenic_sadc_irq, 0, + dev_name(dev), sadc); + if (ret) { + dev_err(dev, "failed to request IRQ %d: %d\n", sadc->irq, ret); + goto err_clk; + } + + indio_dev->name = "ingenic-sadc"; + indio_dev->info = &ingenic_sadc_info; + indio_dev->modes = INDIO_DIRECT_MODE; + indio_dev->channels = ingenic_sadc_channels; + indio_dev->num_channels = ARRAY_SIZE(ingenic_sadc_channels); + + platform_set_drvdata(pdev, indio_dev); + + ret = iio_device_register(indio_dev); + if (ret) { + dev_err(dev, "failed to register IIO device: %d\n", ret); + goto err_clk; + } + + return 0; + +err_clk: + writew(SADC_SLEEP, sadc->base + SADC_ENABLE); + clk_disable_unprepare(sadc->clk); + return ret; +} + +static void ingenic_sadc_remove(struct platform_device *pdev) +{ + struct iio_dev *indio_dev = platform_get_drvdata(pdev); + struct ingenic_sadc *sadc = iio_priv(indio_dev); + + iio_device_unregister(indio_dev); + + writeb(SADC_CTRL_MASK_ALL, sadc->base + SADC_CTRL); + writew(SADC_SLEEP, sadc->base + SADC_ENABLE); + writeb(SADC_STATUS_ARDY, sadc->base + SADC_STATUS); + clk_disable_unprepare(sadc->clk); +} + +static const struct of_device_id ingenic_sadc_of_match[] = { + { .compatible = "ingenic,t23-sadc" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, ingenic_sadc_of_match); + +static struct platform_driver ingenic_sadc_driver = { + .probe = ingenic_sadc_probe, + .remove = ingenic_sadc_remove, + .driver = { + .name = "ingenic-sadc", + .of_match_table = ingenic_sadc_of_match, + }, +}; +module_platform_driver(ingenic_sadc_driver); + +MODULE_AUTHOR("Ingenic Semiconductor Co., Ltd."); +MODULE_AUTHOR("Difan Zhang"); +MODULE_DESCRIPTION("Ingenic T23 auxiliary ADC"); +MODULE_LICENSE("GPL"); From 6d18cc4f8b0044abfc2505fe213300466e9ef8de Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 51/78] dt-bindings: remoteproc: ingenic: Describe the T23 coprocessor Describe the register windows, clocks, interrupts and other resources used by the T23 device. Signed-off-by: Difan Zhang --- .../bindings/remoteproc/ingenic,vpu.yaml | 92 +++++++++++++++---- 1 file changed, 75 insertions(+), 17 deletions(-) diff --git a/Documentation/devicetree/bindings/remoteproc/ingenic,vpu.yaml b/Documentation/devicetree/bindings/remoteproc/ingenic,vpu.yaml index 8b55dbd909..5ccd57d8e0 100644 --- a/Documentation/devicetree/bindings/remoteproc/ingenic,vpu.yaml +++ b/Documentation/devicetree/bindings/remoteproc/ingenic,vpu.yaml @@ -17,35 +17,45 @@ maintainers: properties: compatible: - const: ingenic,jz4770-vpu-rproc + oneOf: + - enum: + - ingenic,jz4770-vpu-rproc + - ingenic,t23-coprocessor + - ingenic,t23-riscv + - items: + - const: ingenic,t23-coprocessor + - const: ingenic,t23-riscv reg: - items: - - description: aux registers - - description: tcsm0 registers - - description: tcsm1 registers - - description: sram registers + minItems: 2 + maxItems: 4 reg-names: - items: - - const: aux - - const: tcsm0 - - const: tcsm1 - - const: sram + minItems: 2 + maxItems: 4 clocks: - items: - - description: aux clock - - description: vpu clock + minItems: 1 + maxItems: 2 clock-names: - items: - - const: aux - - const: vpu + minItems: 1 + maxItems: 2 + + ingenic,cpm: + $ref: /schemas/types.yaml#/definitions/phandle + description: Phandle to the CPM syscon node. interrupts: maxItems: 1 + interrupt-names: + const: doorbell + + firmware-name: + $ref: /schemas/types.yaml#/definitions/string + description: Default firmware image name. + required: - compatible - reg @@ -54,6 +64,54 @@ required: - clock-names - interrupts +allOf: + - if: + properties: + compatible: + contains: + enum: + - ingenic,t23-coprocessor + - ingenic,t23-riscv + then: + properties: + reg: + items: + - description: aux registers + - description: tcsm shared memory + reg-names: + items: + - const: aux + - const: tcsm + clocks: + maxItems: 1 + clock-names: + const: coproc + else: + properties: + reg: + items: + - description: aux registers + - description: tcsm0 registers + - description: tcsm1 registers + - description: sram registers + reg-names: + items: + - const: aux + - const: tcsm0 + - const: tcsm1 + - const: sram + clocks: + items: + - description: aux clock + - description: vpu clock + clock-names: + items: + - const: aux + - const: vpu + ingenic,cpm: false + interrupt-names: false + firmware-name: false + additionalProperties: false examples: From d4baa6887996072c2a1d3e3a148fe53fad15e35a Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:21:39 -0800 Subject: [PATCH 52/78] remoteproc: ingenic: Add a T23 coprocessor skeleton Add the experimental T23 auxiliary coprocessor framework behind a disabled-by-default configuration option. Firmware execution and mailbox operation remain incomplete. Signed-off-by: Difan Zhang --- drivers/remoteproc/Kconfig | 11 ++ drivers/remoteproc/Makefile | 2 + drivers/remoteproc/t23_coproc.h | 121 ++++++++++++++++++++ drivers/remoteproc/t23_coproc_core.c | 156 ++++++++++++++++++++++++++ drivers/remoteproc/t23_coproc_rproc.c | 117 +++++++++++++++++++ 5 files changed, 407 insertions(+) create mode 100644 drivers/remoteproc/t23_coproc.h create mode 100644 drivers/remoteproc/t23_coproc_core.c create mode 100644 drivers/remoteproc/t23_coproc_rproc.c diff --git a/drivers/remoteproc/Kconfig b/drivers/remoteproc/Kconfig index 85bb276b80..d03c86f220 100644 --- a/drivers/remoteproc/Kconfig +++ b/drivers/remoteproc/Kconfig @@ -54,6 +54,17 @@ config INGENIC_VPU_RPROC This can be either built-in or a loadable module. If unsure say N. +config INGENIC_T23_REMOTEPROC + tristate "Ingenic T23 RISC-V coprocessor support" + depends on MACH_INGENIC || COMPILE_TEST + depends on OF && HAS_IOMEM + select MFD_SYSCON + help + Say y or m here to build the remoteproc skeleton driver for the + auxiliary RISC-V coprocessor on Ingenic T23 SoCs. + + This is an experimental placeholder and is disabled by default. + config MTK_SCP tristate "Mediatek SCP support" depends on ARCH_MEDIATEK || COMPILE_TEST diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile index 5ff4e2fee4..7459925be5 100644 --- a/drivers/remoteproc/Makefile +++ b/drivers/remoteproc/Makefile @@ -14,6 +14,8 @@ obj-$(CONFIG_REMOTEPROC_CDEV) += remoteproc_cdev.o obj-$(CONFIG_IMX_REMOTEPROC) += imx_rproc.o obj-$(CONFIG_IMX_DSP_REMOTEPROC) += imx_dsp_rproc.o obj-$(CONFIG_INGENIC_VPU_RPROC) += ingenic_rproc.o +obj-$(CONFIG_INGENIC_T23_REMOTEPROC) += ingenic-t23-coproc.o +ingenic-t23-coproc-y := t23_coproc_core.o t23_coproc_rproc.o obj-$(CONFIG_MTK_SCP) += mtk_scp.o mtk_scp_ipi.o obj-$(CONFIG_OMAP_REMOTEPROC) += omap_remoteproc.o obj-$(CONFIG_WKUP_M3_RPROC) += wkup_m3_rproc.o diff --git a/drivers/remoteproc/t23_coproc.h b/drivers/remoteproc/t23_coproc.h new file mode 100644 index 0000000000..8529a3608e --- /dev/null +++ b/drivers/remoteproc/t23_coproc.h @@ -0,0 +1,121 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Ingenic T23 Low-Power RISC-V Coprocessor Platform Driver Header + * + * Copyright (C) 2026 Difan Zhang + */ + +#ifndef _T23_COPROC_H_ +#define _T23_COPROC_H_ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define T23_PKT_MAGIC 0x504B5431 /* "PKT1" */ +#define T23_MAX_PAYLOAD_LEN 48 + +/** + * struct t23_ipc_packet - Standard 64-byte IPC Packet Descriptor + */ +struct t23_ipc_packet { + u32 magic; + u16 seq_id; + u16 cmd; + u32 status; + u16 data_len; + u16 checksum; + u8 data[T23_MAX_PAYLOAD_LEN]; +} __attribute__((packed)); + +/* + * MMIO Apertures: + * AUX Control Aperture: 0x132a0000, 4 KiB + * TCSM SRAM Aperture: 0x132c0000, 16 KiB + * 0x12000000 must not be touched. + */ +#define AUX_IOBASE 0x132a0000 +#define AUX_IOSIZE 0x1000 +#define TCSM_IOBASE 0x132c0000 +#define TCSM_IOSIZE 0x4000 + +/* + * Register offsets in AUX MMIO aperture. + * Note: The coprocessor register map is documented as placeholder; + * these auxiliary MMIO registers and bitfields remain undocumented. + */ +#define REG_COPROC_CTRL 0x00 /* placeholder; undocumented */ +#define REG_COPROC_STATUS 0x04 /* placeholder; undocumented */ +#define REG_HOST_DOORBELL 0x08 /* placeholder; undocumented */ +#define REG_COPROC_DOORBELL 0x0c /* placeholder; undocumented */ +#define REG_HOST_IRQ_STATUS 0x10 /* placeholder; undocumented */ +#define REG_HOST_IRQ_CLEAR 0x14 /* placeholder; undocumented */ +#define REG_FAST_MSG_TX 0x20 /* placeholder; undocumented */ +#define REG_FAST_MSG_RX 0x24 /* placeholder; undocumented */ + +/* Control and status bits (placeholder; undocumented) */ +#define COPROC_CTRL_SW_RESET BIT(0) +#define COPROC_CTRL_MSG_IRQ_EN BIT(3) +#define COPROC_STATUS_RUNNING BIT(0) +#define COPROC_DOORBELL_TRIGGER BIT(0) +#define HOST_IRQ_RX_PENDING BIT(2) + +/* CPM reset register and bit */ +#define CPM_SRBC_OFFSET 0xc4 +#define SRBC_RISCV_SR BIT(26) + +/* TCSM shared memory partitioning */ +#define T23_TCSM_MBOX_OFFSET 0x0000 +#define T23_TCSM_TX_OFFSET 0x0200 +#define T23_TCSM_RX_OFFSET 0x0900 +#define T23_TCSM_FW_OFFSET 0x1000 + +#define T23_RING_NUM_SLOTS 28 +#define T23_MBOX_MAGIC 0x5432334D /* "T23M" */ +#define T23_MBOX_VERSION 0x0100 + +/** + * struct t23_mbox_header - TCSM mailbox header at offset 0x0000 (64 bytes) + */ +struct t23_mbox_header { + u32 magic; /* offset 0x00: 0x5432334D ("T23M") */ + u16 version; /* offset 0x04: 0x0100 */ + u16 flags; /* offset 0x06 */ + u32 tx_buf_offset; /* offset 0x08: 0x0200 */ + u32 tx_buf_size; /* offset 0x0c: 1792 bytes */ + u32 rx_buf_offset; /* offset 0x10: 0x0900 */ + u32 rx_buf_size; /* offset 0x14: 1792 bytes */ + u32 config_offset; /* offset 0x18 */ + u32 reserved0; /* offset 0x1c: 32-bit alignment padding */ + u32 heartbeat_counter; /* offset 0x20: updated by RISC-V loop */ + u32 tx_head; /* offset 0x24: updated by Host MIPS */ + u32 tx_tail; /* offset 0x28: updated by RISC-V */ + u32 rx_head; /* offset 0x2c: updated by RISC-V */ + u32 rx_tail; /* offset 0x30: updated by Host MIPS */ + u32 reserved[3]; /* offset 0x34..0x3f: padding to 64 bytes */ +} __attribute__((packed)); + +/** + * struct t23_coproc_priv - Driver private state context + */ +struct t23_coproc_priv { + struct device *dev; + struct rproc *rproc; + struct mutex lock; + void __iomem *aux_base; + void __iomem *tcsm_base; + struct regmap *cpm_map; + struct clk *clk; + int irq; + bool running; +}; + +/* Remoteproc operations */ +extern const struct rproc_ops t23_coproc_rproc_ops; + +#endif /* _T23_COPROC_H_ */ diff --git a/drivers/remoteproc/t23_coproc_core.c b/drivers/remoteproc/t23_coproc_core.c new file mode 100644 index 0000000000..63c9060e7b --- /dev/null +++ b/drivers/remoteproc/t23_coproc_core.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 Low-Power RISC-V Coprocessor Platform Core Driver + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include "t23_coproc.h" +#include "remoteproc_internal.h" + +static irqreturn_t t23_coproc_isr(int irq, void *dev_id) +{ + struct t23_coproc_priv *priv = dev_id; + u32 status; + + status = readl(priv->aux_base + REG_HOST_IRQ_STATUS); + if (!status && !readl(priv->aux_base + REG_HOST_DOORBELL)) + return IRQ_NONE; + + writel(status, priv->aux_base + REG_HOST_IRQ_CLEAR); + writel(0, priv->aux_base + REG_HOST_DOORBELL); + + return IRQ_HANDLED; +} + +static int t23_coproc_probe(struct platform_device *pdev) +{ + const char *fw_name = "ingenic/t23_riscv.elf"; + struct device *dev = &pdev->dev; + struct t23_coproc_priv *priv; + struct rproc *rproc; + struct resource *res; + int ret; + + ret = rproc_of_parse_firmware(dev, 0, &fw_name); + if (ret < 0 && ret != -EINVAL) + return ret; + + rproc = rproc_alloc(dev, dev_name(dev), &t23_coproc_rproc_ops, + fw_name, sizeof(*priv)); + if (!rproc) { + dev_err(dev, "failed to allocate remoteproc context\n"); + return -ENOMEM; + } + + priv = rproc->priv; + priv->dev = dev; + priv->rproc = rproc; + rproc->auto_boot = false; + mutex_init(&priv->lock); + + /* AUX control aperture */ + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aux"); + if (!res) + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + priv->aux_base = devm_ioremap_resource(dev, res); + if (IS_ERR(priv->aux_base)) { + ret = PTR_ERR(priv->aux_base); + goto err_free_rproc; + } + + /* TCSM shared memory aperture */ + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcsm"); + if (!res) + res = platform_get_resource(pdev, IORESOURCE_MEM, 1); + priv->tcsm_base = devm_ioremap_resource(dev, res); + if (IS_ERR(priv->tcsm_base)) { + ret = PTR_ERR(priv->tcsm_base); + goto err_free_rproc; + } + + priv->clk = devm_clk_get(dev, "coproc"); + if (IS_ERR(priv->clk)) + priv->clk = devm_clk_get(dev, NULL); + if (IS_ERR(priv->clk)) { + ret = PTR_ERR(priv->clk); + dev_err(dev, "failed to acquire CLK_RISCV: %d\n", ret); + goto err_free_rproc; + } + + priv->cpm_map = syscon_regmap_lookup_by_phandle(dev->of_node, "ingenic,cpm"); + if (IS_ERR(priv->cpm_map)) { + dev_warn(dev, "CPM syscon regmap not found, relying on direct register access\n"); + priv->cpm_map = NULL; + } + + priv->irq = platform_get_irq(pdev, 0); + if (priv->irq < 0) { + ret = priv->irq; + dev_err(dev, "failed to get IRQ: %d\n", ret); + goto err_free_rproc; + } + + ret = devm_request_irq(dev, priv->irq, t23_coproc_isr, 0, dev_name(dev), priv); + if (ret) { + dev_err(dev, "failed to request IRQ %d: %d\n", priv->irq, ret); + goto err_free_rproc; + } + disable_irq(priv->irq); + + platform_set_drvdata(pdev, priv); + + ret = rproc_add(rproc); + if (ret) { + dev_err(dev, "failed to add remoteproc: %d\n", ret); + goto err_free_rproc; + } + + dev_dbg(dev, "Ingenic T23 Low-Power Coprocessor driver initialized\n"); + return 0; + +err_free_rproc: + rproc_free(rproc); + return ret; +} + +static void t23_coproc_remove(struct platform_device *pdev) +{ + struct t23_coproc_priv *priv = platform_get_drvdata(pdev); + + if (!priv) + return; + + if (priv->running) + rproc_shutdown(priv->rproc); + rproc_del(priv->rproc); + rproc_free(priv->rproc); + dev_dbg(&pdev->dev, "Ingenic T23 Coprocessor unregistered\n"); +} + +static const struct of_device_id t23_coproc_of_match[] = { + { .compatible = "ingenic,t23-coprocessor" }, + { .compatible = "ingenic,t23-riscv" }, + { } +}; +MODULE_DEVICE_TABLE(of, t23_coproc_of_match); + +static struct platform_driver t23_coproc_driver = { + .probe = t23_coproc_probe, + .remove = t23_coproc_remove, + .driver = { + .name = "ingenic-t23-coproc", + .of_match_table = t23_coproc_of_match, + }, +}; +module_platform_driver(t23_coproc_driver); + +MODULE_AUTHOR("Ingenic Semiconductor Co., Ltd."); +MODULE_AUTHOR("Difan Zhang"); +MODULE_DESCRIPTION("Ingenic T23 low-power coprocessor remoteproc platform driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/remoteproc/t23_coproc_rproc.c b/drivers/remoteproc/t23_coproc_rproc.c new file mode 100644 index 0000000000..cb1d76bf6a --- /dev/null +++ b/drivers/remoteproc/t23_coproc_rproc.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 Low-Power RISC-V Coprocessor Remoteproc Operations + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include "t23_coproc.h" + +static int t23_coproc_rproc_start(struct rproc *rproc) +{ + struct t23_coproc_priv *priv = rproc->priv; + struct t23_mbox_header hdr; + int ret; + + ret = clk_prepare_enable(priv->clk); + if (ret) { + dev_err(priv->dev, "failed to enable CLK_RISCV: %d\n", ret); + return ret; + } + + /* Initialize TCSM Mailbox Header */ + memset(&hdr, 0, sizeof(hdr)); + hdr.magic = T23_MBOX_MAGIC; + hdr.version = T23_MBOX_VERSION; + hdr.tx_buf_offset = T23_TCSM_TX_OFFSET; + hdr.tx_buf_size = sizeof(struct t23_ipc_packet) * T23_RING_NUM_SLOTS; + hdr.rx_buf_offset = T23_TCSM_RX_OFFSET; + hdr.rx_buf_size = sizeof(struct t23_ipc_packet) * T23_RING_NUM_SLOTS; + + memcpy_toio(priv->tcsm_base + T23_TCSM_MBOX_OFFSET, &hdr, sizeof(hdr)); + wmb(); + + /* Deassert CPM global reset (bit 26 in CPM_SRBC) */ + if (priv->cpm_map) + regmap_update_bits(priv->cpm_map, CPM_SRBC_OFFSET, + SRBC_RISCV_SR, 0); + + /* Deassert coprocessor local soft reset (bit 0 in REG_COPROC_CTRL) */ + writel(readl(priv->aux_base + REG_COPROC_CTRL) & ~COPROC_CTRL_SW_RESET, + priv->aux_base + REG_COPROC_CTRL); + + /* Enable host doorbell message interrupt (bit 3 in REG_COPROC_CTRL) */ + writel(readl(priv->aux_base + REG_COPROC_CTRL) | COPROC_CTRL_MSG_IRQ_EN, + priv->aux_base + REG_COPROC_CTRL); + + enable_irq(priv->irq); + priv->running = true; + + /* + * Reset is released after the ELF load. The dump has no coprocessor + * firmware and no handshake, so this does not mean the core answered. + */ + dev_dbg(priv->dev, "coprocessor reset released at 0x%llx\n", + (unsigned long long)rproc->bootaddr); + return 0; +} + +static int t23_coproc_rproc_stop(struct rproc *rproc) +{ + struct t23_coproc_priv *priv = rproc->priv; + + priv->running = false; + disable_irq(priv->irq); + + /* Mask host doorbell interrupt and assert local soft reset */ + writel((readl(priv->aux_base + REG_COPROC_CTRL) & ~COPROC_CTRL_MSG_IRQ_EN) | + COPROC_CTRL_SW_RESET, priv->aux_base + REG_COPROC_CTRL); + + /* Assert CPM global reset */ + if (priv->cpm_map) + regmap_update_bits(priv->cpm_map, CPM_SRBC_OFFSET, + SRBC_RISCV_SR, SRBC_RISCV_SR); + + clk_disable_unprepare(priv->clk); + dev_dbg(priv->dev, "Coprocessor stopped\n"); + return 0; +} + +static void t23_coproc_rproc_kick(struct rproc *rproc, int vqid) +{ + struct t23_coproc_priv *priv = rproc->priv; + + /* Pulse coprocessor doorbell to generate interrupt to RISC-V core */ + writel(COPROC_DOORBELL_TRIGGER, priv->aux_base + REG_COPROC_DOORBELL); +} + +static void *t23_coproc_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, + bool *is_iomem) +{ + struct t23_coproc_priv *priv = rproc->priv; + + if (!len) + return NULL; + if (da >= TCSM_IOBASE) + da -= TCSM_IOBASE; + if (da < TCSM_IOSIZE && len <= TCSM_IOSIZE - da) { + if (is_iomem) + *is_iomem = true; + return (void __force *)(priv->tcsm_base + da); + } + + return NULL; +} + +const struct rproc_ops t23_coproc_rproc_ops = { + .start = t23_coproc_rproc_start, + .stop = t23_coproc_rproc_stop, + .kick = t23_coproc_rproc_kick, + .da_to_va = t23_coproc_rproc_da_to_va, +}; From 0ded55740622ddb5e4459da3574fbbf34b35cfed Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 53/78] dt-bindings: sound: ingenic: Add T23 codec resources Describe the T23 internal codec register window, clocks and audio interfaces. Signed-off-by: Difan Zhang --- .../bindings/sound/ingenic,codec.yaml | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/sound/ingenic,codec.yaml b/Documentation/devicetree/bindings/sound/ingenic,codec.yaml index b58b90850e..a5f77f26d6 100644 --- a/Documentation/devicetree/bindings/sound/ingenic,codec.yaml +++ b/Documentation/devicetree/bindings/sound/ingenic,codec.yaml @@ -11,6 +11,24 @@ maintainers: allOf: - $ref: dai-common.yaml# + - if: + properties: + compatible: + contains: + const: ingenic,t23-codec + then: + properties: + '#sound-dai-cells': + const: 1 + clocks: false + clock-names: false + else: + properties: + '#sound-dai-cells': + const: 0 + required: + - clocks + - clock-names properties: $nodename: @@ -23,6 +41,7 @@ properties: - ingenic,jz4760-codec - ingenic,jz4725b-codec - ingenic,jz4740-codec + - ingenic,t23-codec - items: - const: ingenic,jz4760b-codec - const: ingenic,jz4760-codec @@ -38,15 +57,13 @@ properties: - const: aic '#sound-dai-cells': - const: 0 + enum: [0, 1] unevaluatedProperties: false required: - compatible - reg - - clocks - - clock-names - '#sound-dai-cells' examples: From 1ead4dcd89eb0d30c18fb6f4f52e07dccd3c476e Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:39:03 -0800 Subject: [PATCH 54/78] ASoC: codecs: Add the Ingenic T23 internal codec Provide the T23 microphone and speaker routes, gain and mute controls, and playback reference interface through ASoC. Signed-off-by: Difan Zhang --- sound/soc/codecs/Kconfig | 13 + sound/soc/codecs/Makefile | 2 + sound/soc/codecs/ingenic-t23-codec.c | 716 +++++++++++++++++++++++++++ 3 files changed, 731 insertions(+) create mode 100644 sound/soc/codecs/ingenic-t23-codec.c diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig index 399dcf3d1c..4eb93258d6 100644 --- a/sound/soc/codecs/Kconfig +++ b/sound/soc/codecs/Kconfig @@ -129,6 +129,7 @@ config SND_SOC_ALL_CODECS imply SND_SOC_JZ4725B_CODEC imply SND_SOC_JZ4760_CODEC imply SND_SOC_JZ4770_CODEC + imply SND_SOC_INGENIC_T23_CODEC imply SND_SOC_LM4857 imply SND_SOC_LM49453 imply SND_SOC_LOCHNAGAR_SC @@ -1089,6 +1090,18 @@ config SND_SOC_JZ4770_CODEC This driver can also be built as a module. If so, the module will be called snd-soc-jz4770-codec. +config SND_SOC_INGENIC_T23_CODEC + depends on MACH_INGENIC || COMPILE_TEST + depends on OF + select REGMAP_MMIO + tristate "Ingenic T23 internal CODEC" + help + Enable support for the internal audio CODEC found in the Ingenic + T23 SoC. + + This driver can also be built as a module. If so, the module + will be called snd-soc-ingenic-t23-codec. + config SND_SOC_DA7210 tristate depends on SND_SOC_I2C_AND_SPI diff --git a/sound/soc/codecs/Makefile b/sound/soc/codecs/Makefile index 47c621b3a0..57aea2dc0e 100644 --- a/sound/soc/codecs/Makefile +++ b/sound/soc/codecs/Makefile @@ -142,6 +142,7 @@ snd-soc-jz4740-codec-y := jz4740.o snd-soc-jz4725b-codec-y := jz4725b.o snd-soc-jz4760-codec-y := jz4760.o snd-soc-jz4770-codec-y := jz4770.o +snd-soc-ingenic-t23-codec-y := ingenic-t23-codec.o snd-soc-lm4857-y := lm4857.o snd-soc-lm49453-y := lm49453.o snd-soc-lochnagar-sc-y := lochnagar-sc.o @@ -550,6 +551,7 @@ obj-$(CONFIG_SND_SOC_JZ4740_CODEC) += snd-soc-jz4740-codec.o obj-$(CONFIG_SND_SOC_JZ4725B_CODEC) += snd-soc-jz4725b-codec.o obj-$(CONFIG_SND_SOC_JZ4760_CODEC) += snd-soc-jz4760-codec.o obj-$(CONFIG_SND_SOC_JZ4770_CODEC) += snd-soc-jz4770-codec.o +obj-$(CONFIG_SND_SOC_INGENIC_T23_CODEC) += snd-soc-ingenic-t23-codec.o obj-$(CONFIG_SND_SOC_LM4857) += snd-soc-lm4857.o obj-$(CONFIG_SND_SOC_LM49453) += snd-soc-lm49453.o obj-$(CONFIG_SND_SOC_LOCHNAGAR_SC) += snd-soc-lochnagar-sc.o diff --git a/sound/soc/codecs/ingenic-t23-codec.c b/sound/soc/codecs/ingenic-t23-codec.c new file mode 100644 index 0000000000..0e3a9f8a4d --- /dev/null +++ b/sound/soc/codecs/ingenic-t23-codec.c @@ -0,0 +1,716 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 SoC Internal Audio Codec Driver + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define T23_CODEC_REG_00 0x00 +#define T23_CODEC_REG_08 0x08 +#define T23_CODEC_REG_0C 0x0c +#define T23_CODEC_REG_10 0x10 +#define T23_CODEC_REG_14 0x14 +#define T23_CODEC_REG_20 0x20 +#define T23_CODEC_REG_24 0x24 +#define T23_CODEC_REG_80 0x80 +#define T23_CODEC_REG_84 0x84 +#define T23_CODEC_REG_88 0x88 +#define T23_CODEC_REG_8C 0x8c +#define T23_CODEC_REG_90 0x90 +#define T23_CODEC_REG_94 0x94 +#define T23_CODEC_REG_98 0x98 +#define T23_CODEC_REG_9C 0x9c +#define T23_CODEC_REG_A0 0xa0 +#define T23_CODEC_REG_110 0x110 +#define T23_CODEC_REG_124 0x124 + +struct t23_codec { + struct device *dev; + struct regmap *regmap; + struct mutex lock; + unsigned int playback_volume; + unsigned int capture_volume; + bool playback_switch; + bool capture_switch; + bool hpf_switch; + bool muted; + bool initialized; +}; + +#define T23_CODEC_RATES (SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_96000 | \ + SNDRV_PCM_RATE_KNOT) + +static const unsigned int t23_sample_rates[8] = { + 8000, 12000, 16000, 24000, 32000, 44100, 48000, 96000 +}; + +static const unsigned int t23_sample_rate_codes[8] = { + 7, 6, 5, 4, 3, 2, 1, 0 +}; + +static const struct snd_pcm_hw_constraint_list t23_rate_constraints = { + .count = ARRAY_SIZE(t23_sample_rates), + .list = t23_sample_rates, +}; + +static int t23_codec_hw_init(struct t23_codec *codec) +{ + int i, ret; + unsigned int capture_volume = 16; + unsigned int shift_val = 0; + int cur_bit = 0; + + if (codec->initialized) { + ret = regmap_read(codec->regmap, T23_CODEC_REG_94, + &capture_volume); + if (ret) + return ret; + capture_volume &= 0x1f; + } + + /* Reset sequence: 0 -> 10ms -> 3 -> 10ms -> 2 -> 10ms -> 3 */ + regmap_update_bits(codec->regmap, T23_CODEC_REG_00, 0x3, 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_00, 0x3, 3); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_00, 0x3, 2); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_00, 0x3, 3); + msleep(10); + + regmap_write(codec->regmap, T23_CODEC_REG_0C, 0x3e); + regmap_write(codec->regmap, T23_CODEC_REG_10, 0x10); + regmap_write(codec->regmap, T23_CODEC_REG_14, 0x3e); + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, 0x3, 1); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_84, 0x7f, 1); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_88, BIT(5), BIT(5)); + msleep(10); + + /* Anti-pop 7-step DAC ramp */ + for (i = 7; i > 0; i--) { + shift_val = ((cur_bit | 1) & 0xff) | shift_val; + regmap_update_bits(codec->regmap, T23_CODEC_REG_84, 0x7f, shift_val); + msleep(30); + cur_bit = shift_val << 1; + } + + msleep(20); + regmap_update_bits(codec->regmap, T23_CODEC_REG_00, BIT(1), 0); + regmap_update_bits(codec->regmap, T23_CODEC_REG_00, BIT(1), BIT(1)); + + /* Clear bit 6 of 0x124 before updating 0x94. */ + regmap_update_bits(codec->regmap, T23_CODEC_REG_124, BIT(6), 0); + msleep(10); + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_94, 0x1f, + capture_volume & 0x1f); + msleep(10); + if (!ret) + codec->initialized = true; + return ret; +} + +static int t23_codec_set_sample_rate(struct t23_codec *codec, unsigned int rate) +{ + int i; + + for (i = 0; i < 8; i++) { + if (rate == t23_sample_rates[i]) + return regmap_update_bits(codec->regmap, + T23_CODEC_REG_110, 0x7, + t23_sample_rate_codes[i]); + } + + return -EINVAL; +} + +static int t23_codec_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *dai) +{ + struct snd_soc_component *component = dai->component; + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + unsigned int rate = params_rate(params); + int format_code, ret; + + switch (params_format(params)) { + case SNDRV_PCM_FORMAT_S16_LE: + format_code = 0; + break; + case SNDRV_PCM_FORMAT_S20_3LE: + format_code = 1; + break; + case SNDRV_PCM_FORMAT_S24_LE: + format_code = 2; + break; + default: + return -EINVAL; + } + + ret = t23_codec_set_sample_rate(codec, rate); + if (ret) + return ret; + + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { + return regmap_update_bits(codec->regmap, T23_CODEC_REG_10, + 0x3 << 5, format_code << 5); + } else { + unsigned int mono = params_channels(params) == 1 ? BIT(0) : 0; + + /* + * T23_CODEC_REG_08: + * bit 0: 1 for right mono record + * bits [4:3]: 2 for I2S format + * bits [6:5]: format code (0: 16-bit, 1: 20-bit, 2: 24-bit) + * bit 7: ADC interface enable + */ + return regmap_update_bits(codec->regmap, T23_CODEC_REG_08, + (0x3 << 5) | (0x3 << 3) | BIT(0) | BIT(7), + (format_code << 5) | (0x2 << 3) | mono | + (codec->capture_switch ? BIT(7) : 0)); + } +} + +static int t23_codec_mute_stream(struct snd_soc_dai *dai, int mute, int stream) +{ + struct snd_soc_component *component = dai->component; + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + int ret = 0; + + if (stream != SNDRV_PCM_STREAM_PLAYBACK) + return 0; + + mutex_lock(&codec->lock); + if (codec->playback_switch) { + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, 0x1f, + mute ? 0 : codec->playback_volume); + } + if (!ret) + codec->muted = !!mute; + mutex_unlock(&codec->lock); + return ret; +} + +static int t23_codec_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) +{ + if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S || + (fmt & SND_SOC_DAIFMT_INV_MASK) != SND_SOC_DAIFMT_NB_NF) + return -EINVAL; + + switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { + case SND_SOC_DAIFMT_CBP_CFP: + return 0; + default: + return -EINVAL; + } +} + +static int t23_codec_dai_startup(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + return snd_pcm_hw_constraint_list(substream->runtime, 0, + SNDRV_PCM_HW_PARAM_RATE, + &t23_rate_constraints); +} + +static const struct snd_soc_dai_ops t23_codec_dai_ops = { + .startup = t23_codec_dai_startup, + .hw_params = t23_codec_hw_params, + .mute_stream = t23_codec_mute_stream, + .set_fmt = t23_codec_set_fmt, + .no_capture_mute = 1, +}; + +static struct snd_soc_dai_driver t23_codec_dai[] = { + { + .name = "t23-hifi", + .playback = { + .stream_name = "Playback", + .channels_min = 1, + .channels_max = 2, + .rates = T23_CODEC_RATES, + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, + }, + .capture = { + .stream_name = "Capture", + .channels_min = 1, + .channels_max = 2, + .rates = T23_CODEC_RATES, + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, + }, + .ops = &t23_codec_dai_ops, + .symmetric_rate = 1, + }, + /* Terminates the AIC playback reference link; no codec path. */ + { + .name = "t23-loop", + .capture = { + .stream_name = "Loop Capture", + .channels_min = 1, + .channels_max = 1, + .rates = T23_CODEC_RATES, + .formats = SNDRV_PCM_FMTBIT_S16_LE, + }, + }, +}; + +static int t23_codec_dac_event(struct snd_soc_dapm_widget *w, + struct snd_kcontrol *kcontrol, int event) +{ + struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + if (SND_SOC_DAPM_EVENT_ON(event)) { + regmap_update_bits(codec->regmap, T23_CODEC_REG_80, 0x7, 0x7); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, 0xf, 0xf); + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, BIT(3) | BIT(2) | 0x3, + BIT(3) | BIT(2) | 0x2); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, BIT(5) | BIT(6), + BIT(5) | BIT(6)); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_9C, 0xff, 0xf8); + msleep(10); + mutex_lock(&codec->lock); + regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, BIT(7) | 0x1f, + (codec->playback_switch ? BIT(7) : 0) | + ((codec->playback_switch && !codec->muted) ? codec->playback_volume : 0)); + mutex_unlock(&codec->lock); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_84, 0x7f, 1); + msleep(10); + } else if (SND_SOC_DAPM_EVENT_OFF(event)) { + regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, + 0x1f | BIT(7) | BIT(6) | BIT(5), 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_9C, 0xf0, 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, BIT(3), 0); + msleep(10); + } + + return 0; +} + +static int t23_codec_adc_event(struct snd_soc_dapm_widget *w, + struct snd_kcontrol *kcontrol, int event) +{ + struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + if (SND_SOC_DAPM_EVENT_ON(event)) { + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, 0x3, 0x2); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_24, 0x1f, 0x8); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_08, 0x18 | BIT(7), + 0x10 | (codec->capture_switch ? BIT(7) : 0)); + msleep(10); + regmap_write(codec->regmap, T23_CODEC_REG_0C, 0x3e); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(7), BIT(7)); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_88, 0x1f, 0x1f); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(5), BIT(5)); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_90, BIT(4) | BIT(5), + BIT(4) | BIT(5)); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, 0xf0, 0xf0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(6), BIT(6)); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_90, 0xc0, 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_84, 0x7f, 1); + msleep(11); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(4), BIT(4)); + msleep(10); + regmap_write(codec->regmap, T23_CODEC_REG_20, + 0x92 | (codec->hpf_switch ? BIT(6) : 0)); + msleep(10); + } else if (SND_SOC_DAPM_EVENT_OFF(event)) { + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(4), 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, 0x70, 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_90, BIT(4) | BIT(5), 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(5), 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_88, BIT(4), 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_98, 0x90, 0); + msleep(10); + regmap_update_bits(codec->regmap, T23_CODEC_REG_8C, BIT(6), 0); + msleep(10); + } + + return 0; +} + +static const DECLARE_TLV_DB_SCALE(dac_tlv, -4650, 150, 0); +static const DECLARE_TLV_DB_SCALE(adc_tlv, 0, 150, 0); + +static int t23_playback_volume_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + mutex_lock(&codec->lock); + ucontrol->value.integer.value[0] = codec->playback_volume; + mutex_unlock(&codec->lock); + return 0; +} + +static int t23_playback_volume_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + long volume = ucontrol->value.integer.value[0]; + int ret = 0; + + if (volume < 0 || volume > 31) + return -EINVAL; + + mutex_lock(&codec->lock); + if (codec->playback_volume != volume) { + if (codec->playback_switch && !codec->muted) + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, + 0x1f, volume); + if (!ret) { + codec->playback_volume = volume; + ret = 1; + } + } + mutex_unlock(&codec->lock); + return ret; +} + +static int t23_playback_switch_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + mutex_lock(&codec->lock); + ucontrol->value.integer.value[0] = codec->playback_switch; + mutex_unlock(&codec->lock); + return 0; +} + +static int t23_playback_switch_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + unsigned int val = !!ucontrol->value.integer.value[0]; + int ret = 0; + + mutex_lock(&codec->lock); + if (codec->playback_switch != val) { + if (val) { + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, + BIT(7) | 0x1f, + BIT(7) | (codec->muted ? 0 : codec->playback_volume)); + } else { + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_A0, + BIT(7) | 0x1f, 0); + } + if (!ret) { + codec->playback_switch = val; + ret = 1; + } + } + mutex_unlock(&codec->lock); + return ret; +} + +static int t23_capture_volume_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + mutex_lock(&codec->lock); + ucontrol->value.integer.value[0] = codec->capture_volume; + mutex_unlock(&codec->lock); + return 0; +} + +static int t23_capture_volume_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + long volume = ucontrol->value.integer.value[0]; + int ret = 0; + + if (volume < 0 || volume > 31) + return -EINVAL; + + mutex_lock(&codec->lock); + if (codec->capture_volume != volume) { + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_124, BIT(6), 0); + if (ret) + goto unlock; + msleep(10); + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_94, 0x1f, volume); + if (ret) + goto unlock; + msleep(10); + codec->capture_volume = volume; + ret = 1; + } +unlock: + mutex_unlock(&codec->lock); + return ret; +} + +static int t23_capture_switch_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + mutex_lock(&codec->lock); + ucontrol->value.integer.value[0] = codec->capture_switch; + mutex_unlock(&codec->lock); + return 0; +} + +static int t23_capture_switch_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + unsigned int val = !!ucontrol->value.integer.value[0]; + int ret = 0; + + mutex_lock(&codec->lock); + if (codec->capture_switch != val) { + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_08, + BIT(7), val ? BIT(7) : 0); + if (!ret) { + codec->capture_switch = val; + ret = 1; + } + } + mutex_unlock(&codec->lock); + return ret; +} + +static int t23_hpf_switch_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + mutex_lock(&codec->lock); + ucontrol->value.integer.value[0] = codec->hpf_switch; + mutex_unlock(&codec->lock); + return 0; +} + +static int t23_hpf_switch_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = + snd_soc_kcontrol_component(kcontrol); + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + unsigned int val = !!ucontrol->value.integer.value[0]; + int ret = 0; + + mutex_lock(&codec->lock); + if (codec->hpf_switch != val) { + ret = regmap_update_bits(codec->regmap, T23_CODEC_REG_20, + BIT(6), val ? BIT(6) : 0); + if (!ret) { + codec->hpf_switch = val; + ret = 1; + } + } + mutex_unlock(&codec->lock); + return ret; +} + +static const struct snd_kcontrol_new t23_codec_controls[] = { + SOC_SINGLE_EXT_TLV("Master Playback Volume", T23_CODEC_REG_A0, 0, 31, 0, + t23_playback_volume_get, t23_playback_volume_put, + dac_tlv), + SOC_SINGLE_BOOL_EXT("Master Playback Switch", 0, + t23_playback_switch_get, t23_playback_switch_put), + SOC_SINGLE_EXT_TLV("Mic Capture Volume", T23_CODEC_REG_94, 0, 31, 0, + t23_capture_volume_get, t23_capture_volume_put, + adc_tlv), + SOC_SINGLE_BOOL_EXT("Mic Capture Switch", 0, + t23_capture_switch_get, t23_capture_switch_put), + SOC_SINGLE_BOOL_EXT("High-Pass Filter Capture Switch", 0, + t23_hpf_switch_get, t23_hpf_switch_put), +}; + +static const struct snd_soc_dapm_widget t23_codec_dapm_widgets[] = { + SND_SOC_DAPM_DAC_E("DAC", "Playback", SND_SOC_NOPM, 0, 0, + t23_codec_dac_event, + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), + SND_SOC_DAPM_ADC_E("ADC", "Capture", SND_SOC_NOPM, 0, 0, + t23_codec_adc_event, + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), + + SND_SOC_DAPM_OUTPUT("LOUT"), + SND_SOC_DAPM_OUTPUT("ROUT"), + SND_SOC_DAPM_INPUT("MICIN"), +}; + +static const struct snd_soc_dapm_route t23_codec_dapm_routes[] = { + { "LOUT", NULL, "DAC" }, + { "ROUT", NULL, "DAC" }, + { "ADC", NULL, "MICIN" }, +}; + +static int t23_codec_set_bias_level(struct snd_soc_component *component, + enum snd_soc_bias_level level) +{ + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + switch (level) { + case SND_SOC_BIAS_ON: + break; + case SND_SOC_BIAS_PREPARE: + break; + case SND_SOC_BIAS_STANDBY: + if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) + return t23_codec_hw_init(codec); + break; + case SND_SOC_BIAS_OFF: + break; + } + + return 0; +} + +static void t23_codec_component_remove(struct snd_soc_component *component) +{ + struct t23_codec *codec = snd_soc_component_get_drvdata(component); + + if (codec && codec->regmap) { + regmap_write(codec->regmap, T23_CODEC_REG_A0, 0); + regmap_update_bits(codec->regmap, T23_CODEC_REG_08, BIT(7), 0); + regmap_write(codec->regmap, T23_CODEC_REG_00, 0); + } +} + +static const struct snd_soc_component_driver t23_codec_component = { + .remove = t23_codec_component_remove, + .set_bias_level = t23_codec_set_bias_level, + .controls = t23_codec_controls, + .num_controls = ARRAY_SIZE(t23_codec_controls), + .dapm_widgets = t23_codec_dapm_widgets, + .num_dapm_widgets = ARRAY_SIZE(t23_codec_dapm_widgets), + .dapm_routes = t23_codec_dapm_routes, + .num_dapm_routes = ARRAY_SIZE(t23_codec_dapm_routes), + .suspend_bias_off = 1, + .idle_bias_on = 1, + /* + * No pmdown_time: an idle DAC emits a tone at fs/6 that the speaker + * amplifier would play until the delayed power-down. + */ + .endianness = 1, +}; + +static const struct regmap_config t23_codec_regmap_config = { + .reg_bits = 32, + .reg_stride = 4, + .val_bits = 32, + .max_register = 0x134, + .cache_type = REGCACHE_NONE, +}; + +static const struct of_device_id t23_codec_of_matches[] = { + { .compatible = "ingenic,t23-codec" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, t23_codec_of_matches); + +static int t23_codec_probe(struct platform_device *pdev) +{ + struct t23_codec *codec; + void __iomem *base; + int ret; + + codec = devm_kzalloc(&pdev->dev, sizeof(*codec), GFP_KERNEL); + if (!codec) + return -ENOMEM; + + codec->dev = &pdev->dev; + mutex_init(&codec->lock); + codec->playback_volume = 24; /* 0x18 */ + codec->capture_volume = 16; /* 0x10 */ + codec->playback_switch = true; + codec->capture_switch = true; + codec->hpf_switch = true; + codec->muted = true; + + base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(base)) + return PTR_ERR(base); + + codec->regmap = devm_regmap_init_mmio(&pdev->dev, base, + &t23_codec_regmap_config); + if (IS_ERR(codec->regmap)) + return PTR_ERR(codec->regmap); + platform_set_drvdata(pdev, codec); + + ret = devm_snd_soc_register_component(&pdev->dev, + &t23_codec_component, + t23_codec_dai, + ARRAY_SIZE(t23_codec_dai)); + if (ret) { + dev_err(&pdev->dev, "failed to register codec component: %d\n", ret); + return ret; + } + + return 0; +} + +static struct platform_driver t23_codec_driver = { + .probe = t23_codec_probe, + .driver = { + .name = "ingenic-t23-codec", + .of_match_table = t23_codec_of_matches, + }, +}; + +module_platform_driver(t23_codec_driver); + +MODULE_DESCRIPTION("Ingenic T23 SoC internal audio codec driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); From 1bd3c8cae1f22a50d812039bdbb18b4bec4638d3 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 04:29:30 -0800 Subject: [PATCH 55/78] dt-bindings: sound: ingenic: Add T23 AIC and reference capture Describe the T23 AIC and playback-reference DMA interface used for audio capture and playback. Signed-off-by: Difan Zhang --- .../bindings/sound/ingenic,aic.yaml | 42 +++++++++++++--- .../bindings/sound/ingenic,t23-aic-loop.yaml | 49 +++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 Documentation/devicetree/bindings/sound/ingenic,t23-aic-loop.yaml diff --git a/Documentation/devicetree/bindings/sound/ingenic,aic.yaml b/Documentation/devicetree/bindings/sound/ingenic,aic.yaml index d15c000f14..0c405985f5 100644 --- a/Documentation/devicetree/bindings/sound/ingenic,aic.yaml +++ b/Documentation/devicetree/bindings/sound/ingenic,aic.yaml @@ -11,6 +11,33 @@ maintainers: allOf: - $ref: dai-common.yaml# + - if: + properties: + compatible: + contains: + const: ingenic,t23-aic + then: + properties: + clocks: + minItems: 2 + maxItems: 3 + clock-names: + oneOf: + - items: + - const: aic + - const: i2s + - items: + - const: cgu_i2s_spk + - const: cgu_i2s_mic + - const: aic + else: + properties: + clocks: + maxItems: 2 + clock-names: + items: + - const: aic + - const: i2s properties: $nodename: @@ -24,9 +51,14 @@ properties: - ingenic,jz4770-i2s - ingenic,jz4780-i2s - ingenic,x1000-i2s + - ingenic,t23-i2s + - ingenic,t23-aic - items: - const: ingenic,jz4725b-i2s - const: ingenic,jz4740-i2s + - items: + - const: ingenic,t23-aic + - const: ingenic,jz4780-i2s '#sound-dai-cells': const: 0 @@ -38,14 +70,12 @@ properties: maxItems: 1 clocks: - items: - - description: AIC clock - - description: I2S clock + minItems: 2 + maxItems: 3 clock-names: - items: - - const: aic - - const: i2s + minItems: 2 + maxItems: 3 dmas: items: diff --git a/Documentation/devicetree/bindings/sound/ingenic,t23-aic-loop.yaml b/Documentation/devicetree/bindings/sound/ingenic,t23-aic-loop.yaml new file mode 100644 index 0000000000..ef92649840 --- /dev/null +++ b/Documentation/devicetree/bindings/sound/ingenic,t23-aic-loop.yaml @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/sound/ingenic,t23-aic-loop.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 AIC Loopback Capture DAI + +maintainers: + - Paul Cercueil + +allOf: + - $ref: dai-common.yaml# + +properties: + compatible: + const: ingenic,t23-aic-loop + + '#sound-dai-cells': + const: 0 + + ingenic,aic: + $ref: /schemas/types.yaml#/definitions/phandle + description: Phandle to the companion Ingenic T23 AIC node. + + dmas: + maxItems: 1 + + dma-names: + const: rx + +required: + - compatible + - ingenic,aic + - '#sound-dai-cells' + - dmas + - dma-names + +unevaluatedProperties: false + +examples: + - | + aic-loop { + compatible = "ingenic,t23-aic-loop"; + ingenic,aic = <&aic>; + #sound-dai-cells = <0>; + dmas = <&pdma 5 0xffffffff>; + dma-names = "rx"; + }; From 43a905ecdf22a2f8ebd13d81a6b28d309ae7eac5 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:39:03 -0800 Subject: [PATCH 56/78] ASoC: jz4740: Add T23 AIC clocking and reference capture Handle the T23 speaker and microphone clocks, mono and stereo channel routing, FIFO resets, and playback-reference FIFO DMA capture. Signed-off-by: Difan Zhang --- sound/soc/jz4740/jz4740-i2s.c | 631 ++++++++++++++++++++++++++++++++-- 1 file changed, 593 insertions(+), 38 deletions(-) diff --git a/sound/soc/jz4740/jz4740-i2s.c b/sound/soc/jz4740/jz4740-i2s.c index 5176195316..50a89089ce 100644 --- a/sound/soc/jz4740/jz4740-i2s.c +++ b/sound/soc/jz4740/jz4740-i2s.c @@ -1,20 +1,26 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (C) 2010, Lars-Peter Clausen + * Copyright (C) 2026 Difan Zhang */ #include #include #include +#include #include #include #include +#include #include #include #include +#include +#include #include #include #include +#include #include #include @@ -30,7 +36,9 @@ #define JZ_REG_AIC_I2S_STATUS 0x1c #define JZ_REG_AIC_CLK_DIV 0x30 #define JZ_REG_AIC_FIFO 0x34 +#define T23_REG_AIC_LOOP_FIFO 0x38 +#define T23_AIC_CONF_FIFO_RST GENMASK(14, 13) #define JZ_AIC_CONF_OVERFLOW_PLAY_LAST BIT(6) #define JZ_AIC_CONF_INTERNAL_CODEC BIT(5) #define JZ_AIC_CONF_I2S BIT(4) @@ -39,10 +47,15 @@ #define JZ_AIC_CONF_SYNC_CLK_MASTER BIT(1) #define JZ_AIC_CONF_ENABLE BIT(0) +#define JZ_AIC_CTRL_CHANNEL_MASK GENMASK(26, 24) +/* Copy transmitted samples into the loop FIFO on T23 */ +#define T23_AIC_CTRL_LOOP (BIT(23) | BIT(22)) #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE GENMASK(21, 19) #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE GENMASK(18, 16) #define JZ_AIC_CTRL_ENABLE_RX_DMA BIT(15) #define JZ_AIC_CTRL_ENABLE_TX_DMA BIT(14) +#define T23_AIC_CTRL_MONO_MASK GENMASK(13, 12) +#define T23_AIC_CTRL_MONO_RIGHT BIT(12) #define JZ_AIC_CTRL_MONO_TO_STEREO BIT(11) #define JZ_AIC_CTRL_SWITCH_ENDIANNESS BIT(10) #define JZ_AIC_CTRL_SIGNED_TO_UNSIGNED BIT(9) @@ -56,6 +69,7 @@ #define JZ_AIC_CTRL_ENABLE_PLAYBACK BIT(1) #define JZ_AIC_CTRL_ENABLE_CAPTURE BIT(0) +#define T23_AIC_I2S_FMT_INTERNAL_CLOCK BIT(17) #define JZ_AIC_I2S_FMT_DISABLE_BIT_CLK BIT(12) #define JZ_AIC_I2S_FMT_DISABLE_BIT_ICLK BIT(13) #define JZ_AIC_I2S_FMT_ENABLE_SYS_CLK BIT(4) @@ -72,10 +86,12 @@ struct i2s_soc_info { struct reg_field field_i2sdiv_playback; bool shared_fifo_flush; + bool is_t23; }; struct jz4740_i2s { struct regmap *regmap; + phys_addr_t phys_base; struct regmap_field *field_rx_fifo_thresh; struct regmap_field *field_tx_fifo_thresh; @@ -84,11 +100,36 @@ struct jz4740_i2s { struct clk *clk_aic; struct clk *clk_i2s; + struct clk *clk_tx; + struct clk *clk_rx; struct snd_dmaengine_dai_dma_data playback_dma_data; struct snd_dmaengine_dai_dma_data capture_dma_data; const struct i2s_soc_info *soc_info; + + spinlock_t lock; + unsigned int play_rate; + unsigned int loop_rate; + bool tx_clk_on; + bool rx_clk_on; + bool capture_running; +}; + +/* T23 playback reference: the loop FIFO, read through its own DMA channel */ +struct t23_aic_loop { + struct jz4740_i2s *i2s; + struct device *aic; + struct snd_dmaengine_dai_dma_data dma_data; +}; + +static const unsigned int t23_i2s_rates[8] = { + 8000, 12000, 16000, 24000, 32000, 44100, 48000, 96000 +}; + +static const struct snd_pcm_hw_constraint_list t23_i2s_rate_constraints = { + .count = ARRAY_SIZE(t23_i2s_rates), + .list = t23_i2s_rates, }; static int jz4740_i2s_startup(struct snd_pcm_substream *substream, @@ -97,6 +138,45 @@ static int jz4740_i2s_startup(struct snd_pcm_substream *substream, struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai); int ret; + if (i2s->soc_info->is_t23) { + bool is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; + struct clk *clk = is_play ? i2s->clk_tx : i2s->clk_rx; + unsigned int loop_rate = READ_ONCE(i2s->loop_rate); + + ret = snd_pcm_hw_constraint_list(substream->runtime, 0, + SNDRV_PCM_HW_PARAM_RATE, + &t23_i2s_rate_constraints); + if (ret < 0) + return ret; + + if (is_play && loop_rate) { + ret = snd_pcm_hw_constraint_single(substream->runtime, + SNDRV_PCM_HW_PARAM_RATE, + loop_rate); + if (ret < 0) + return ret; + } + + if (clk) { + bool other_on = is_play ? i2s->rx_clk_on : + i2s->tx_clk_on; + + if (!(other_on && i2s->clk_tx == i2s->clk_rx) && + !clk_get_rate(clk)) { + ret = clk_set_rate(clk, 8000 * 256); + if (ret) + return ret; + } + ret = clk_prepare_enable(clk); + if (ret) + return ret; + if (is_play) + i2s->tx_clk_on = true; + else + i2s->rx_clk_on = true; + } + } + /* * When we can flush FIFOs independently, only flush the FIFO * that is starting up. We can do this when the DAI is active @@ -121,9 +201,11 @@ static int jz4740_i2s_startup(struct snd_pcm_substream *substream, if (i2s->soc_info->shared_fifo_flush) regmap_set_bits(i2s->regmap, JZ_REG_AIC_CTRL, JZ_AIC_CTRL_TFLUSH); - ret = clk_prepare_enable(i2s->clk_i2s); - if (ret) - return ret; + if (!i2s->soc_info->is_t23) { + ret = clk_prepare_enable(i2s->clk_i2s); + if (ret) + return ret; + } regmap_set_bits(i2s->regmap, JZ_REG_AIC_CONF, JZ_AIC_CONF_ENABLE); return 0; @@ -133,41 +215,94 @@ static void jz4740_i2s_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) { struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai); + unsigned long flags; + + if (i2s->soc_info->is_t23) { + bool is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; + struct clk *clk = is_play ? i2s->clk_tx : i2s->clk_rx; + + if (clk) { + if (is_play && i2s->tx_clk_on) { + clk_disable_unprepare(clk); + i2s->tx_clk_on = false; + } else if (!is_play && i2s->rx_clk_on) { + clk_disable_unprepare(clk); + i2s->rx_clk_on = false; + } + } + + if (is_play) { + spin_lock_irqsave(&i2s->lock, flags); + i2s->play_rate = 0; + spin_unlock_irqrestore(&i2s->lock, flags); + } + } if (snd_soc_dai_active(dai)) return; regmap_clear_bits(i2s->regmap, JZ_REG_AIC_CONF, JZ_AIC_CONF_ENABLE); - clk_disable_unprepare(i2s->clk_i2s); + if (!i2s->soc_info->is_t23) + clk_disable_unprepare(i2s->clk_i2s); } static int jz4740_i2s_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai) { struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai); - uint32_t mask; + unsigned long flags; + uint32_t mask, val; if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) mask = JZ_AIC_CTRL_ENABLE_PLAYBACK | JZ_AIC_CTRL_ENABLE_TX_DMA; else mask = JZ_AIC_CTRL_ENABLE_CAPTURE | JZ_AIC_CTRL_ENABLE_RX_DMA; + if (!i2s->soc_info->is_t23) { + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + case SNDRV_PCM_TRIGGER_RESUME: + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: + regmap_set_bits(i2s->regmap, JZ_REG_AIC_CTRL, mask); + break; + case SNDRV_PCM_TRIGGER_STOP: + case SNDRV_PCM_TRIGGER_SUSPEND: + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: + regmap_clear_bits(i2s->regmap, JZ_REG_AIC_CTRL, mask); + break; + default: + return -EINVAL; + } + + return 0; + } + switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: - regmap_set_bits(i2s->regmap, JZ_REG_AIC_CTRL, mask); + val = mask; break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_PAUSE_PUSH: - regmap_clear_bits(i2s->regmap, JZ_REG_AIC_CTRL, mask); + val = 0; break; default: return -EINVAL; } + spin_lock_irqsave(&i2s->lock, flags); + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { + i2s->capture_running = !!val; + if (!val) + mask |= T23_AIC_CTRL_LOOP; + } + regmap_update_bits(i2s->regmap, JZ_REG_AIC_CTRL, + mask | JZ_AIC_CTRL_TFLUSH | JZ_AIC_CTRL_RFLUSH, val); + spin_unlock_irqrestore(&i2s->lock, flags); + return 0; } @@ -178,6 +313,9 @@ static int jz4740_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) JZ_AIC_CONF_SYNC_CLK_MASTER; unsigned int conf = 0, format = 0; + if (i2s->soc_info->is_t23) + format |= T23_AIC_I2S_FMT_INTERNAL_CLOCK; + switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { case SND_SOC_DAIFMT_BP_FP: conf |= JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER; @@ -253,13 +391,11 @@ static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream, { struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai); struct regmap_field *div_field; - unsigned long i2sdiv_max; + struct clk *clk; + unsigned long i2sdiv_max, flags; unsigned int sample_size; - uint32_t ctrl, conf; - int div = 1; - - regmap_read(i2s->regmap, JZ_REG_AIC_CTRL, &ctrl); - regmap_read(i2s->regmap, JZ_REG_AIC_CONF, &conf); + uint32_t ctrl, conf, mask; + int div = 1, ret; switch (params_format(params)) { case SNDRV_PCM_FORMAT_S8: @@ -278,41 +414,104 @@ static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream, return -EINVAL; } - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { - ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE; - ctrl |= FIELD_PREP(JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE, sample_size); + if (!i2s->soc_info->is_t23) { + regmap_read(i2s->regmap, JZ_REG_AIC_CTRL, &ctrl); + regmap_read(i2s->regmap, JZ_REG_AIC_CONF, &conf); + + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { + ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE; + ctrl |= FIELD_PREP(JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE, + sample_size); + + if (params_channels(params) == 1) + ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO; + else + ctrl &= ~JZ_AIC_CTRL_MONO_TO_STEREO; + + div_field = i2s->field_i2sdiv_playback; + i2sdiv_max = GENMASK(i2s->soc_info->field_i2sdiv_playback.msb, + i2s->soc_info->field_i2sdiv_playback.lsb); + } else { + ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE; + ctrl |= FIELD_PREP(JZ_AIC_CTRL_INPUT_SAMPLE_SIZE, + sample_size); + + div_field = i2s->field_i2sdiv_capture; + i2sdiv_max = GENMASK(i2s->soc_info->field_i2sdiv_capture.msb, + i2s->soc_info->field_i2sdiv_capture.lsb); + } + + if (conf & (JZ_AIC_CONF_BIT_CLK_MASTER | + JZ_AIC_CONF_SYNC_CLK_MASTER)) { + div = jz4740_i2s_get_i2sdiv(clk_get_rate(i2s->clk_i2s), + params_rate(params), + i2sdiv_max); + if (div < 0) + return div; + } + + regmap_write(i2s->regmap, JZ_REG_AIC_CTRL, ctrl); + regmap_field_write(div_field, div - 1); + + return 0; + } + + clk = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? + i2s->clk_tx : i2s->clk_rx; + if (clk) { + ret = clk_set_rate(clk, params_rate(params) * 256); + if (ret) + return ret; + } + regmap_read(i2s->regmap, JZ_REG_AIC_CONF, &conf); + + ctrl = 0; + mask = JZ_AIC_CTRL_TFLUSH | JZ_AIC_CTRL_RFLUSH; + + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { + mask |= JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE | + JZ_AIC_CTRL_MONO_TO_STEREO | + JZ_AIC_CTRL_CHANNEL_MASK; + ctrl |= FIELD_PREP(JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE, sample_size) | + FIELD_PREP(JZ_AIC_CTRL_CHANNEL_MASK, + params_channels(params) - 1); if (params_channels(params) == 1) ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO; - else - ctrl &= ~JZ_AIC_CTRL_MONO_TO_STEREO; div_field = i2s->field_i2sdiv_playback; i2sdiv_max = GENMASK(i2s->soc_info->field_i2sdiv_playback.msb, i2s->soc_info->field_i2sdiv_playback.lsb); } else { - ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE; + mask |= JZ_AIC_CTRL_INPUT_SAMPLE_SIZE | T23_AIC_CTRL_MONO_MASK; ctrl |= FIELD_PREP(JZ_AIC_CTRL_INPUT_SAMPLE_SIZE, sample_size); + if (params_channels(params) == 1) + ctrl |= T23_AIC_CTRL_MONO_RIGHT; div_field = i2s->field_i2sdiv_capture; i2sdiv_max = GENMASK(i2s->soc_info->field_i2sdiv_capture.msb, i2s->soc_info->field_i2sdiv_capture.lsb); } - /* - * Only calculate I2SDIV if we're supplying the bit or frame clock. - * If the codec is supplying both clocks then the divider output is - * unused, and we don't want it to limit the allowed sample rates. - */ - if (conf & (JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER)) { - div = jz4740_i2s_get_i2sdiv(clk_get_rate(i2s->clk_i2s), + if ((conf & (JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER)) && + clk) { + div = jz4740_i2s_get_i2sdiv(clk_get_rate(clk), params_rate(params), i2sdiv_max); if (div < 0) return div; } - regmap_write(i2s->regmap, JZ_REG_AIC_CTRL, ctrl); + spin_lock_irqsave(&i2s->lock, flags); + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { + if (i2s->loop_rate && i2s->loop_rate != params_rate(params)) { + spin_unlock_irqrestore(&i2s->lock, flags); + return -EBUSY; + } + i2s->play_rate = params_rate(params); + } + regmap_update_bits(i2s->regmap, JZ_REG_AIC_CTRL, mask, ctrl); regmap_field_write(div_field, div - 1); + spin_unlock_irqrestore(&i2s->lock, flags); return 0; } @@ -415,13 +614,229 @@ static const struct i2s_soc_info jz4780_i2s_soc_info = { .field_i2sdiv_playback = REG_FIELD(JZ_REG_AIC_CLK_DIV, 0, 3), }; +static struct snd_soc_dai_driver t23_i2s_dai = { + .playback = { + .channels_min = 1, + .channels_max = 2, + .rates = SNDRV_PCM_RATE_8000_96000 | SNDRV_PCM_RATE_KNOT, + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, + }, + .capture = { + .channels_min = 1, + .channels_max = 2, + .rates = SNDRV_PCM_RATE_8000_96000 | SNDRV_PCM_RATE_KNOT, + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, + }, + .symmetric_rate = 1, + .ops = &jz4740_i2s_dai_ops, +}; + +static const struct i2s_soc_info t23_i2s_soc_info = { + .dai = &t23_i2s_dai, + .field_rx_fifo_thresh = REG_FIELD(JZ_REG_AIC_CONF, 24, 27), + .field_tx_fifo_thresh = REG_FIELD(JZ_REG_AIC_CONF, 16, 20), + .field_i2sdiv_capture = REG_FIELD(JZ_REG_AIC_CLK_DIV, 8, 11), + .field_i2sdiv_playback = REG_FIELD(JZ_REG_AIC_CLK_DIV, 0, 3), + .is_t23 = true, +}; + +static int t23_aic_loop_dai_probe(struct snd_soc_dai *dai) +{ + struct t23_aic_loop *loop = snd_soc_dai_get_drvdata(dai); + + snd_soc_dai_init_dma_data(dai, NULL, &loop->dma_data); + return 0; +} + +static int t23_aic_loop_startup(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + struct t23_aic_loop *loop = snd_soc_dai_get_drvdata(dai); + unsigned int rate = READ_ONCE(loop->i2s->play_rate); + int ret; + + ret = snd_pcm_hw_constraint_list(substream->runtime, 0, + SNDRV_PCM_HW_PARAM_RATE, + &t23_i2s_rate_constraints); + if (ret < 0 || !rate) + return ret < 0 ? ret : 0; + + ret = snd_pcm_hw_constraint_single(substream->runtime, + SNDRV_PCM_HW_PARAM_RATE, rate); + return ret < 0 ? ret : 0; +} + +static int t23_aic_loop_hw_params(struct snd_pcm_substream *substream, + struct snd_pcm_hw_params *params, + struct snd_soc_dai *dai) +{ + struct t23_aic_loop *loop = snd_soc_dai_get_drvdata(dai); + struct jz4740_i2s *i2s = loop->i2s; + unsigned long flags; + int ret = 0; + + spin_lock_irqsave(&i2s->lock, flags); + if (i2s->play_rate && i2s->play_rate != params_rate(params)) + ret = -EBUSY; + else + i2s->loop_rate = params_rate(params); + spin_unlock_irqrestore(&i2s->lock, flags); + return ret; +} + +static int t23_aic_loop_set(struct jz4740_i2s *i2s, bool on) +{ + unsigned long flags; + + spin_lock_irqsave(&i2s->lock, flags); + if (on && !i2s->capture_running) { + spin_unlock_irqrestore(&i2s->lock, flags); + return -EBUSY; + } + regmap_update_bits(i2s->regmap, JZ_REG_AIC_CTRL, + JZ_AIC_CTRL_TFLUSH | JZ_AIC_CTRL_RFLUSH | + T23_AIC_CTRL_LOOP, + on ? T23_AIC_CTRL_LOOP : 0); + spin_unlock_irqrestore(&i2s->lock, flags); + return 0; +} + +static int t23_aic_loop_trigger(struct snd_pcm_substream *substream, int cmd, + struct snd_soc_dai *dai) +{ + struct t23_aic_loop *loop = snd_soc_dai_get_drvdata(dai); + + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + case SNDRV_PCM_TRIGGER_RESUME: + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: + return t23_aic_loop_set(loop->i2s, true); + case SNDRV_PCM_TRIGGER_STOP: + case SNDRV_PCM_TRIGGER_SUSPEND: + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: + return t23_aic_loop_set(loop->i2s, false); + default: + return -EINVAL; + } +} + +static void t23_aic_loop_shutdown(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + struct t23_aic_loop *loop = snd_soc_dai_get_drvdata(dai); + unsigned long flags; + + t23_aic_loop_set(loop->i2s, false); + spin_lock_irqsave(&loop->i2s->lock, flags); + loop->i2s->loop_rate = 0; + spin_unlock_irqrestore(&loop->i2s->lock, flags); +} + +static const struct snd_soc_dai_ops t23_aic_loop_dai_ops = { + .probe = t23_aic_loop_dai_probe, + .startup = t23_aic_loop_startup, + .shutdown = t23_aic_loop_shutdown, + .trigger = t23_aic_loop_trigger, + .hw_params = t23_aic_loop_hw_params, +}; + +static struct snd_soc_dai_driver t23_aic_loop_dai = { + .name = "t23-aic-loop-dai", + .capture = { + .stream_name = "Loop Capture", + .channels_min = 1, + .channels_max = 1, + .rates = SNDRV_PCM_RATE_8000_96000 | SNDRV_PCM_RATE_KNOT, + .formats = SNDRV_PCM_FMTBIT_S16_LE, + }, + .ops = &t23_aic_loop_dai_ops, +}; + +static const struct snd_soc_component_driver t23_aic_loop_component = { + .name = "t23-aic-loop", +}; + +static int t23_aic_loop_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct platform_device *aic; + struct device_node *np; + struct t23_aic_loop *loop; + int ret; + + loop = devm_kzalloc(dev, sizeof(*loop), GFP_KERNEL); + if (!loop) + return -ENOMEM; + + np = of_parse_phandle(dev->of_node, "ingenic,aic", 0); + if (!np) + return -EINVAL; + aic = of_find_device_by_node(np); + of_node_put(np); + if (!aic) + return -EPROBE_DEFER; + + loop->aic = &aic->dev; + loop->i2s = platform_get_drvdata(aic); + if (!loop->i2s) { + put_device(loop->aic); + return -EPROBE_DEFER; + } + + if (!device_link_add(dev, loop->aic, DL_FLAG_AUTOREMOVE_CONSUMER)) { + put_device(loop->aic); + return -EINVAL; + } + + loop->dma_data.addr = loop->i2s->phys_base + T23_REG_AIC_LOOP_FIFO; + loop->dma_data.maxburst = 8; + platform_set_drvdata(pdev, loop); + + ret = devm_snd_soc_register_component(dev, &t23_aic_loop_component, + &t23_aic_loop_dai, 1); + if (!ret) + ret = devm_snd_dmaengine_pcm_register(dev, NULL, 0); + if (ret) + put_device(loop->aic); + return ret; +} + +static void t23_aic_loop_remove(struct platform_device *pdev) +{ + struct t23_aic_loop *loop = platform_get_drvdata(pdev); + + put_device(loop->aic); +} + +static const struct of_device_id t23_aic_loop_of_matches[] = { + { .compatible = "ingenic,t23-aic-loop" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, t23_aic_loop_of_matches); + +static struct platform_driver t23_aic_loop_driver = { + .probe = t23_aic_loop_probe, + .remove = t23_aic_loop_remove, + .driver = { + .name = "t23-aic-loop", + .of_match_table = t23_aic_loop_of_matches, + }, +}; + static int jz4740_i2s_suspend(struct snd_soc_component *component) { struct jz4740_i2s *i2s = snd_soc_component_get_drvdata(component); if (snd_soc_component_active(component)) { regmap_clear_bits(i2s->regmap, JZ_REG_AIC_CONF, JZ_AIC_CONF_ENABLE); - clk_disable_unprepare(i2s->clk_i2s); + if (i2s->soc_info->is_t23) { + if (i2s->tx_clk_on && i2s->clk_tx) + clk_disable_unprepare(i2s->clk_tx); + if (i2s->rx_clk_on && i2s->clk_rx) + clk_disable_unprepare(i2s->clk_rx); + } else { + clk_disable_unprepare(i2s->clk_i2s); + } } clk_disable_unprepare(i2s->clk_aic); @@ -435,20 +850,48 @@ static int jz4740_i2s_resume(struct snd_soc_component *component) int ret; ret = clk_prepare_enable(i2s->clk_aic); - if (ret) + if (ret) { + if (i2s->soc_info->is_t23) { + i2s->tx_clk_on = false; + i2s->rx_clk_on = false; + } return ret; + } if (snd_soc_component_active(component)) { - ret = clk_prepare_enable(i2s->clk_i2s); - if (ret) { - clk_disable_unprepare(i2s->clk_aic); - return ret; + if (i2s->soc_info->is_t23) { + if (i2s->tx_clk_on && i2s->clk_tx) { + ret = clk_prepare_enable(i2s->clk_tx); + if (ret) { + i2s->tx_clk_on = false; + i2s->rx_clk_on = false; + goto err_disable_aic; + } + } + if (i2s->rx_clk_on && i2s->clk_rx) { + ret = clk_prepare_enable(i2s->clk_rx); + if (ret) { + if (i2s->tx_clk_on && i2s->clk_tx) + clk_disable_unprepare(i2s->clk_tx); + i2s->tx_clk_on = false; + i2s->rx_clk_on = false; + goto err_disable_aic; + } + } + } else { + ret = clk_prepare_enable(i2s->clk_i2s); + if (ret) + goto err_disable_aic; } regmap_set_bits(i2s->regmap, JZ_REG_AIC_CONF, JZ_AIC_CONF_ENABLE); } return 0; + +err_disable_aic: + clk_disable_unprepare(i2s->clk_aic); + return ret; } static int jz4740_i2s_probe(struct snd_soc_component *component) @@ -460,6 +903,58 @@ static int jz4740_i2s_probe(struct snd_soc_component *component) if (ret) return ret; + if (i2s->soc_info->is_t23) { + u32 val; + + if (i2s->clk_tx) { + ret = clk_set_rate(i2s->clk_tx, 8000 * 256); + if (ret) + goto err_disable_aic; + ret = clk_prepare_enable(i2s->clk_tx); + if (ret) + goto err_disable_aic; + } + if (i2s->clk_rx) { + ret = clk_set_rate(i2s->clk_rx, 8000 * 256); + if (ret) + goto err_disable_tx; + ret = clk_prepare_enable(i2s->clk_rx); + if (ret) + goto err_disable_tx; + } + + regmap_write(i2s->regmap, JZ_REG_AIC_CONF, + JZ_AIC_CONF_RESET | + JZ_AIC_CONF_BIT_CLK_MASTER | + JZ_AIC_CONF_SYNC_CLK_MASTER); + ret = regmap_read_poll_timeout(i2s->regmap, JZ_REG_AIC_CONF, + val, !(val & T23_AIC_CONF_FIFO_RST), + 1000, 100000); + if (i2s->clk_rx) + clk_disable_unprepare(i2s->clk_rx); + if (i2s->clk_tx) + clk_disable_unprepare(i2s->clk_tx); + if (ret) + goto err_disable_aic; + + regmap_write(i2s->regmap, JZ_REG_AIC_CONF, + JZ_AIC_CONF_OVERFLOW_PLAY_LAST | + JZ_AIC_CONF_I2S | JZ_AIC_CONF_INTERNAL_CODEC); + regmap_field_write(i2s->field_rx_fifo_thresh, 8); + regmap_field_write(i2s->field_tx_fifo_thresh, 16); + regmap_write(i2s->regmap, JZ_REG_AIC_CTRL, 0); + regmap_write(i2s->regmap, JZ_REG_AIC_I2S_FMT, + T23_AIC_I2S_FMT_INTERNAL_CLOCK); + return 0; + +err_disable_tx: + if (i2s->clk_tx) + clk_disable_unprepare(i2s->clk_tx); +err_disable_aic: + clk_disable_unprepare(i2s->clk_aic); + return ret; + } + regmap_write(i2s->regmap, JZ_REG_AIC_CONF, JZ_AIC_CONF_RESET); regmap_write(i2s->regmap, JZ_REG_AIC_CONF, @@ -494,6 +989,8 @@ static const struct of_device_id jz4740_of_matches[] = { { .compatible = "ingenic,jz4770-i2s", .data = &jz4770_i2s_soc_info }, { .compatible = "ingenic,jz4780-i2s", .data = &jz4780_i2s_soc_info }, { .compatible = "ingenic,x1000-i2s", .data = &x1000_i2s_soc_info }, + { .compatible = "ingenic,t23-aic", .data = &t23_i2s_soc_info }, + { .compatible = "ingenic,t23-i2s", .data = &t23_i2s_soc_info }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, jz4740_of_matches); @@ -535,6 +1032,13 @@ static const struct regmap_config jz4740_i2s_regmap_config = { .max_register = JZ_REG_AIC_FIFO, }; +static const struct regmap_config t23_i2s_regmap_config = { + .reg_bits = 32, + .reg_stride = 4, + .val_bits = 32, + .max_register = T23_REG_AIC_LOOP_FIFO, +}; + static int jz4740_i2s_dev_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -547,13 +1051,15 @@ static int jz4740_i2s_dev_probe(struct platform_device *pdev) if (!i2s) return -ENOMEM; + spin_lock_init(&i2s->lock); i2s->soc_info = device_get_match_data(dev); regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem); if (IS_ERR(regs)) return PTR_ERR(regs); - i2s->playback_dma_data.maxburst = 16; + i2s->phys_base = mem->start; + i2s->playback_dma_data.maxburst = i2s->soc_info->is_t23 ? 32 : 16; i2s->playback_dma_data.addr = mem->start + JZ_REG_AIC_FIFO; i2s->capture_dma_data.maxburst = 16; @@ -563,11 +1069,32 @@ static int jz4740_i2s_dev_probe(struct platform_device *pdev) if (IS_ERR(i2s->clk_aic)) return PTR_ERR(i2s->clk_aic); - i2s->clk_i2s = devm_clk_get(dev, "i2s"); - if (IS_ERR(i2s->clk_i2s)) - return PTR_ERR(i2s->clk_i2s); + if (i2s->soc_info->is_t23) { + i2s->clk_tx = devm_clk_get_optional(dev, "cgu_i2s_spk"); + if (IS_ERR(i2s->clk_tx)) + return PTR_ERR(i2s->clk_tx); + + i2s->clk_rx = devm_clk_get_optional(dev, "cgu_i2s_mic"); + if (IS_ERR(i2s->clk_rx)) + return PTR_ERR(i2s->clk_rx); + + if (!i2s->clk_tx) { + i2s->clk_tx = devm_clk_get(dev, "i2s"); + if (IS_ERR(i2s->clk_tx)) + return PTR_ERR(i2s->clk_tx); + if (!i2s->clk_rx) + i2s->clk_rx = i2s->clk_tx; + } + i2s->clk_i2s = i2s->clk_tx; + } else { + i2s->clk_i2s = devm_clk_get(dev, "i2s"); + if (IS_ERR(i2s->clk_i2s)) + return PTR_ERR(i2s->clk_i2s); + } i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs, + i2s->soc_info->is_t23 ? + &t23_i2s_regmap_config : &jz4740_i2s_regmap_config); if (IS_ERR(i2s->regmap)) return PTR_ERR(i2s->regmap); @@ -587,15 +1114,43 @@ static int jz4740_i2s_dev_probe(struct platform_device *pdev) SND_DMAENGINE_PCM_FLAG_COMPAT); } +static void jz4740_i2s_dev_remove(struct platform_device *pdev) +{ + struct jz4740_i2s *i2s = platform_get_drvdata(pdev); + + if (i2s && i2s->soc_info->is_t23 && i2s->regmap) { + regmap_write(i2s->regmap, JZ_REG_AIC_CTRL, 0); + regmap_write(i2s->regmap, JZ_REG_AIC_CONF, 0); + } +} + static struct platform_driver jz4740_i2s_driver = { .probe = jz4740_i2s_dev_probe, + .remove = jz4740_i2s_dev_remove, .driver = { .name = "jz4740-i2s", .of_match_table = jz4740_of_matches, }, }; -module_platform_driver(jz4740_i2s_driver); +static struct platform_driver * const jz4740_i2s_drivers[] = { + &jz4740_i2s_driver, + &t23_aic_loop_driver, +}; + +static int __init jz4740_i2s_init(void) +{ + return platform_register_drivers(jz4740_i2s_drivers, + ARRAY_SIZE(jz4740_i2s_drivers)); +} +module_init(jz4740_i2s_init); + +static void __exit jz4740_i2s_exit(void) +{ + platform_unregister_drivers(jz4740_i2s_drivers, + ARRAY_SIZE(jz4740_i2s_drivers)); +} +module_exit(jz4740_i2s_exit); MODULE_AUTHOR("Lars-Peter Clausen, "); MODULE_DESCRIPTION("Ingenic JZ4740 SoC I2S driver"); From 46915c7c2d2b2c19efd5a5c00dcab5e6d83dc38b Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:44:04 -0800 Subject: [PATCH 57/78] media: uapi: Add Ingenic T23 ISP metadata formats and controls Define the T23 ISP statistics and extensible parameter metadata layouts, pixel-format identifiers and user control range. Document the buffer interfaces for userspace 3A services. Signed-off-by: Difan Zhang --- .../userspace-api/media/v4l/meta-formats.rst | 1 + .../media/v4l/metafmt-t23-isp.rst | 82 ++++ drivers/media/v4l2-core/v4l2-ioctl.c | 6 + .../uapi/linux/media/ingenic/t23-isp-config.h | 370 ++++++++++++++++++ include/uapi/linux/v4l2-controls.h | 6 + include/uapi/linux/videodev2.h | 4 + 6 files changed, 469 insertions(+) create mode 100644 Documentation/userspace-api/media/v4l/metafmt-t23-isp.rst create mode 100644 include/uapi/linux/media/ingenic/t23-isp-config.h diff --git a/Documentation/userspace-api/media/v4l/meta-formats.rst b/Documentation/userspace-api/media/v4l/meta-formats.rst index c6e56b5888..cfee7c818d 100644 --- a/Documentation/userspace-api/media/v4l/meta-formats.rst +++ b/Documentation/userspace-api/media/v4l/meta-formats.rst @@ -17,6 +17,7 @@ These formats are used for the :ref:`metadata` interface only. metafmt-intel-ipu3 metafmt-pisp-be metafmt-rkisp1 + metafmt-t23-isp metafmt-uvc metafmt-vivid metafmt-vsp1-hgo diff --git a/Documentation/userspace-api/media/v4l/metafmt-t23-isp.rst b/Documentation/userspace-api/media/v4l/metafmt-t23-isp.rst new file mode 100644 index 0000000000..8d085323e6 --- /dev/null +++ b/Documentation/userspace-api/media/v4l/metafmt-t23-isp.rst @@ -0,0 +1,82 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _v4l2-meta-fmt-t23-isp-stats: +.. _v4l2-meta-fmt-t23-isp-ext-params: + +******************************************************************************* +V4L2_META_FMT_T23_ISP_EXT_PARAMS ('T23E'), V4L2_META_FMT_T23_ISP_STATS ('T23S') +******************************************************************************* + +======================== +Configuration parameters +======================== + +Userspace configures the Ingenic T23 ISP per input through the metadata output +node (``T23 params0`` or ``T23 params1``) using the extensible block format +:c:type:`t23_isp_ext_params_cfg` (``V4L2_META_FMT_T23_ISP_EXT_PARAMS``) +defined in ````. + +Each block begins with a :c:type:`t23_isp_ext_params_block_header` containing +the block ``type``, ``flags`` (``T23_ISP_EXT_PARAMS_FL_BLOCK_ENABLE`` or +``T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE``), and 8-byte aligned ``size``. All +``reserved`` fields in block headers and payloads must be zero. Valid parameter +value ranges are enforced on ``VIDIOC_QBUF``: + +- **BLC** (:c:type:`t23_isp_blc_config`): ``r``, ``gr``, ``gb``, ``b`` in + ``0..4095``. +- **WB** (:c:type:`t23_isp_wb_config`): ``gain_r``, ``gain_b`` in ``0..0x3fff`` + (U2.10 format, ``1024 == 1.0x``). +- **CCM** (:c:type:`t23_isp_ccm_config`): signed Q10 coefficients in + ``-8192..8191``. +- **Gamma** (:c:type:`t23_isp_gamma_config`): 129 entries in ``0..4095``. +- **LSC** (:c:type:`t23_isp_lsc_config`): ``words <= 2046`` and a multiple of 3, + ``scale <= 3``, ``mean_en <= 1``. +- **MDNS** (:c:type:`t23_isp_mdns_config`): ``block_size`` a multiple of 4 in + ``16..255``; ``top[0]`` limited to bits ``0xd1111111``, ``top[1]`` limited to + bits ``0x00f1310f`` (preventing activation of unallocated history planes), and + ``top[2] == 0``. + +At most one queued parameter buffer is dequeued per active frame immediately +before arming the ISP hardware for that input, and its blocks are merged into +that input's persistent parameter state (unchanged blocks retain their previous +settings until disabled via ``T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE`` or the +parameter queue stops streaming). + +=========================== +3A and histogram statistics +=========================== + +Per-frame AE, histogram, and pre-WB AWB statistics are captured from the +metadata capture node (``T23 stats0`` or ``T23 stats1``) as +:c:type:`t23_isp_stat_buffer` (``V4L2_META_FMT_T23_ISP_STATS``). + +- ``sequence`` matches the ``v4l2_buffer.sequence`` of the corresponding video + capture frame on that input. +- ``ae`` contains a 15x15 grid of 21-bit luma sums and 13-bit pixel class + counts. +- ``hist[0]`` contains the 256-bin full-frame pre-gamma luma histogram + (21-bit bin counts, ``0..0x1fffff``); ``hist[1]`` exposes the second 256-bin + hardware histogram bank collected in the same DMA pass. +- ``awb`` contains a 15x15 grid of 21-bit RGB sums and 13-bit sample counts. + +======================== +Privacy masks and lines +======================== + +Each input's ISP subdevice (``t23-isp-core0`` / ``t23-isp-core1``) exposes four +custom controls defined in ```` under +``V4L2_CID_USER_T23_BASE``: + +- ``V4L2_CID_T23_PRIVACY_MASK``: ``4x4`` ``__u16`` matrix of + ``{left, top, width, height}`` rectangles in sensor coordinates (``0..4095``; + zero width or height disables the mask). Applied to all scaled outputs. +- ``V4L2_CID_T23_PRIVACY_MASK_COLOR``: ``0xRRGGBB`` integer fill colour. +- ``V4L2_CID_T23_DRAW_LINE``: ``6x5`` ``__u16`` matrix of + ``{x0, y0, x1, y1, width}`` vectors in sensor coordinates (``0..4095``; + zero width or equal endpoints disables the line). Drawn on MSCA channel 0. +- ``V4L2_CID_T23_DRAW_LINE_COLOR``: ``0xRRGGBB`` integer stroke colour. + +t23-isp uAPI data types +======================= + +.. kernel-doc:: include/uapi/linux/media/ingenic/t23-isp-config.h diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c index 0f5c3e286a..67e900f704 100644 --- a/drivers/media/v4l2-core/v4l2-ioctl.c +++ b/drivers/media/v4l2-core/v4l2-ioctl.c @@ -1467,6 +1467,12 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) case V4L2_PIX_FMT_Y212: descr = "12-bit YUYV Packed"; break; case V4L2_PIX_FMT_Y216: descr = "16-bit YUYV Packed"; break; case V4L2_META_FMT_RPI_BE_CFG: descr = "RPi PiSP BE Config format"; break; + case V4L2_META_FMT_T23_ISP_EXT_PARAMS: + descr = "Ingenic T23 ISP Ext 3A Params"; + break; + case V4L2_META_FMT_T23_ISP_STATS: + descr = "Ingenic T23 ISP 3A Statistics"; + break; case V4L2_META_FMT_GENERIC_8: descr = "8-bit Generic Metadata"; break; case V4L2_META_FMT_GENERIC_CSI2_10: descr = "8-bit Generic Meta, 10b CSI-2"; break; case V4L2_META_FMT_GENERIC_CSI2_12: descr = "8-bit Generic Meta, 12b CSI-2"; break; diff --git a/include/uapi/linux/media/ingenic/t23-isp-config.h b/include/uapi/linux/media/ingenic/t23-isp-config.h new file mode 100644 index 0000000000..d53f3c4285 --- /dev/null +++ b/include/uapi/linux/media/ingenic/t23-isp-config.h @@ -0,0 +1,370 @@ +/* SPDX-License-Identifier: ((GPL-2.0+ WITH Linux-syscall-note) OR MIT) */ +/* + * Ingenic T23 ISP statistics and parameter buffer formats, and controls + * + * Statistics are captured on a V4L2_BUF_TYPE_META_CAPTURE node using + * V4L2_META_FMT_T23_ISP_STATS. Parameters are queued on a + * V4L2_BUF_TYPE_META_OUTPUT node using V4L2_META_FMT_T23_ISP_EXT_PARAMS. + * + * Copyright (C) 2026 Difan Zhang + */ +#ifndef _UAPI_MEDIA_INGENIC_T23_ISP_CONFIG_H +#define _UAPI_MEDIA_INGENIC_T23_ISP_CONFIG_H + +#include +#include + +#define T23_ISP_GRID_ROWS 15 +#define T23_ISP_GRID_COLS 15 +#define T23_ISP_ZONES (T23_ISP_GRID_ROWS * T23_ISP_GRID_COLS) +#define T23_ISP_HIST_BINS 256 + +#define T23_ISP_STATS_VERSION 1 + +/* Valid statistics in struct t23_isp_stat_buffer::flags */ +#define T23_ISP_STATS_AE (1U << 0) +#define T23_ISP_STATS_HIST (1U << 1) +#define T23_ISP_STATS_AWB (1U << 2) + +/** + * struct t23_isp_ae_zone - luma statistics for one zone + * @sum_low: sum of 8-bit luma over pixels at or below @ae_low + * @sum_mid: sum of luma over pixels between the two thresholds + * @sum_high: sum of luma over pixels at or above @ae_high + * @count_low: number of samples in the low class + * @count_high: number of samples in the high class + * + * The mid-class count is the zone sample count minus both class counts. + * Samples reflect sensor integration time, analogue gain and ISP digital gain. + */ +struct t23_isp_ae_zone { + __u32 sum_low; + __u32 sum_mid; + __u32 sum_high; + __u16 count_low; + __u16 count_high; +}; + +/** + * struct t23_isp_awb_zone - colour statistics for one zone + * @sum_r: red sum over samples inside the AWB colour and luma windows + * @sum_g: green sum over the same samples + * @sum_b: blue sum over the same samples + * @count: number of samples inside the windows + * + * Values are sampled in the linear pre-WB domain before white-balance gains. + */ +struct t23_isp_awb_zone { + __u32 sum_r; + __u32 sum_g; + __u32 sum_b; + __u32 count; +}; + +/** + * struct t23_isp_stat_buffer - statistics for one processed frame + * @version: T23_ISP_STATS_VERSION + * @size: sizeof(struct t23_isp_stat_buffer) + * @flags: T23_ISP_STATS_* bits marking valid sections + * @sequence: sequence number of the matching capture buffer + * @zone_width: zone width in Bayer quads + * @zone_height: zone height in Bayer quads + * @ae_low: upper luma bound of the AE low class + * @ae_high: lower luma bound of the AE high class + * @reserved0: must be zero + * @reserved: must be zero + * @ae: AE zones, row-major + * @hist: 21-bit luma histograms (0..0x1fffff); set 0 is the full-frame + * pre-gamma luma histogram, set 1 is the secondary hardware bank + * @awb: AWB zones, row-major + */ +struct t23_isp_stat_buffer { + __u32 version; + __u32 size; + __u32 flags; + __u32 sequence; + __u16 zone_width; + __u16 zone_height; + __u8 ae_low; + __u8 ae_high; + __u16 reserved0; + __u32 reserved[4]; + struct t23_isp_ae_zone ae[T23_ISP_ZONES]; + __u32 hist[2][T23_ISP_HIST_BINS]; + struct t23_isp_awb_zone awb[T23_ISP_ZONES]; +}; + +#define T23_ISP_GAMMA_POINTS 129 +#define T23_ISP_LSC_MAX_WORDS 2046 +#define T23_ISP_MDNS_WORDS 135 +#define T23_ISP_SDNS_WORDS 104 +#define T23_ISP_YDNS_WORDS 4 +#define T23_ISP_YSP_WORDS 32 +#define T23_ISP_DMSC_WORDS 128 + +/** + * struct t23_isp_blc_config - black-level pedestals per Bayer channel + * @r: red channel black level (12-bit scale, 0..4095) + * @gr: green-red channel black level (12-bit scale, 0..4095) + * @gb: green-blue channel black level (12-bit scale, 0..4095) + * @b: blue channel black level (12-bit scale, 0..4095) + */ +struct t23_isp_blc_config { + __u16 r; + __u16 gr; + __u16 gb; + __u16 b; +}; + +/** + * struct t23_isp_wb_config - white-balance gains + * @gain_r: red gain, 1.0 = 1024, range 0..0x3fff + * @gain_b: blue gain, 1.0 = 1024, range 0..0x3fff + */ +struct t23_isp_wb_config { + __u16 gain_r; + __u16 gain_b; +}; + +/** + * struct t23_isp_ccm_config - colour correction matrix + * @coeff: row-major 3x3 matrix applied to linear RGB, 1.0 = 1024, range + * -8192..8191 + * @reserved: must be zero + */ +struct t23_isp_ccm_config { + __s16 coeff[9]; + __u16 reserved; +}; + +/** + * struct t23_isp_gamma_config - luma-independent RGB gamma curve + * @curve: 129 equally spaced points over the 12-bit input, values 0..4095 + * @reserved: must be zero + */ +struct t23_isp_gamma_config { + __u16 curve[T23_ISP_GAMMA_POINTS]; + __u16 reserved; +}; + +/** + * struct t23_isp_lsc_config - lens shading correction mesh + * @mesh_width: mesh nodes per row + * @mesh_height: mesh rows + * @stride: mesh stride field of the LSC control register + * @scale: gain scale, 0 = 1.0 at 2048, 1 = 1024, 2 = 512, 3 = 256 + * @mean_en: enable the mean mode of the LSC block (0 or 1) + * @words: used entries of @lut, a multiple of three, at most + * T23_ISP_LSC_MAX_WORDS + * @lut: mesh gains, two 12-bit gains per word (high gain in bits 23:12) + */ +struct t23_isp_lsc_config { + __u16 mesh_width; + __u16 mesh_height; + __u16 stride; + __u8 scale; + __u8 mean_en; + __u32 words; + __u32 lut[T23_ISP_LSC_MAX_WORDS]; +}; + +/** + * struct t23_isp_mdns_config - temporal (3D) noise reduction + * @top: function enables, statistics groups and debug control, in register + * order 0x7810, 0x7814, 0x7808; top[0] bits outside 0xd1111111, + * top[1] bits outside 0x00f1310f (which would enable unallocated history + * planes), and any non-zero top[2] are rejected with -EINVAL + * @block_size: motion statistics block size in pixels, a multiple of 4, + * 16..255; the driver derives the block grid from the frame size + * @sta_cfg: motion statistics control + * @sta_thres: motion statistics thresholds + * @val: Y and C filter registers 0x78c8..0x7aec in ascending order, without + * the grid and chroma window registers + */ +struct t23_isp_mdns_config { + __u32 top[3]; + __u32 block_size; + __u32 sta_cfg; + __u32 sta_thres; + __u32 val[T23_ISP_MDNS_WORDS]; +}; + +/* + * Extensible parameter block format (V4L2_META_FMT_T23_ISP_EXT_PARAMS) + */ +#define T23_ISP_EXT_PARAMS_V1 1 +#define T23_ISP_EXT_PARAMS_MAX_SIZE 12288 + +#define T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE (1U << 0) +#define T23_ISP_EXT_PARAMS_FL_BLOCK_ENABLE (1U << 1) + +enum t23_isp_ext_params_block_type { + T23_ISP_EXT_PARAMS_BLOCK_TYPE_BLC = 0, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_WB, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_CCM, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_GAMMA, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_LSC, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_MDNS, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_SDNS, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_YDNS, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_YSP, + T23_ISP_EXT_PARAMS_BLOCK_TYPE_DMSC, +}; + +/** + * struct t23_isp_ext_params_block_header - extensible parameter block header + * @type: enum t23_isp_ext_params_block_type + * @flags: bitmask of T23_ISP_EXT_PARAMS_FL_BLOCK_* + * @size: total size in bytes of the block including this header (8-byte aligned) + */ +struct t23_isp_ext_params_block_header { + __u16 type; + __u16 flags; + __u32 size; +}; + +/** + * struct t23_isp_ext_params_blc_config - extensible BLC parameter block + * @header: block header + * @config: black-level configuration + */ +struct t23_isp_ext_params_blc_config { + struct t23_isp_ext_params_block_header header; + struct t23_isp_blc_config config; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_wb_config - extensible WB parameter block + * @header: block header + * @config: white-balance gain configuration + * @reserved: must be zero + */ +struct t23_isp_ext_params_wb_config { + struct t23_isp_ext_params_block_header header; + struct t23_isp_wb_config config; + __u32 reserved; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_ccm_config - extensible CCM parameter block + * @header: block header + * @config: colour correction matrix configuration + * @reserved: must be zero + */ +struct t23_isp_ext_params_ccm_config { + struct t23_isp_ext_params_block_header header; + struct t23_isp_ccm_config config; + __u32 reserved; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_gamma_config - extensible gamma parameter block + * @header: block header + * @config: gamma curve configuration + * @reserved: must be zero + */ +struct t23_isp_ext_params_gamma_config { + struct t23_isp_ext_params_block_header header; + struct t23_isp_gamma_config config; + __u32 reserved; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_lsc_config - extensible LSC parameter block + * @header: block header + * @config: lens shading correction mesh configuration + * @reserved: must be zero + */ +struct t23_isp_ext_params_lsc_config { + struct t23_isp_ext_params_block_header header; + struct t23_isp_lsc_config config; + __u32 reserved; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_mdns_config - extensible MDNS parameter block + * @header: block header + * @config: temporal noise reduction configuration + * @reserved: must be zero + */ +struct t23_isp_ext_params_mdns_config { + struct t23_isp_ext_params_block_header header; + struct t23_isp_mdns_config config; + __u32 reserved; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_sdns_config - spatial noise reduction registers + * @header: block header + * @words: register values 0x8800..0x88c0 and 0x8a60..0x8b38 in ascending order + */ +struct t23_isp_ext_params_sdns_config { + struct t23_isp_ext_params_block_header header; + __u32 words[T23_ISP_SDNS_WORDS]; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_ydns_config - luma noise reduction registers + * @header: block header + * @words: register values 0x7af0..0x7afc in ascending order + */ +struct t23_isp_ext_params_ydns_config { + struct t23_isp_ext_params_block_header header; + __u32 words[T23_ISP_YDNS_WORDS]; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_ysp_config - luma sharpening registers + * @header: block header + * @words: register values 0x7000..0x707c in ascending order + */ +struct t23_isp_ext_params_ysp_config { + struct t23_isp_ext_params_block_header header; + __u32 words[T23_ISP_YSP_WORDS]; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_dmsc_config - demosaic registers + * @header: block header + * @words: register values 0x4800..0x4a5c in ascending order, excluding the + * commit register 0x499c and unused addresses + */ +struct t23_isp_ext_params_dmsc_config { + struct t23_isp_ext_params_block_header header; + __u32 words[T23_ISP_DMSC_WORDS]; +} __attribute__((aligned(8))); + +/** + * struct t23_isp_ext_params_cfg - extensible ISP parameter buffer + * @version: T23_ISP_EXT_PARAMS_V1 + * @data_size: effective byte length of concatenated blocks in @data + * @data: concatenated struct t23_isp_ext_params_*_config blocks + */ +struct t23_isp_ext_params_cfg { + __u32 version; + __u32 data_size; + __u8 data[T23_ISP_EXT_PARAMS_MAX_SIZE]; +} __attribute__((aligned(8))); + +/* + * Privacy masks of an input, set on the ISP subdev: a __u16 array of + * T23_ISP_PRIVACY_MASKS rows of left, top, width and height in sensor + * pixels. A row with zero width or height is off. Every output of the input + * is masked. + */ +#define T23_ISP_PRIVACY_MASKS 4 +#define V4L2_CID_T23_PRIVACY_MASK (V4L2_CID_USER_T23_BASE + 0) +/* Fill colour of the privacy masks, 0xRRGGBB */ +#define V4L2_CID_T23_PRIVACY_MASK_COLOR (V4L2_CID_USER_T23_BASE + 1) + +/* + * Vector lines of an input, set on the ISP subdev: a __u16 array of + * T23_ISP_DRAW_LINES rows of x0, y0, x1, y1 and width in sensor pixels. + * A row with zero width or equal endpoints is off. Drawn on MSCA channel 0. + */ +#define T23_ISP_DRAW_LINES 6 +#define V4L2_CID_T23_DRAW_LINE (V4L2_CID_USER_T23_BASE + 2) +/* Stroke colour of the vector lines, 0xRRGGBB */ +#define V4L2_CID_T23_DRAW_LINE_COLOR (V4L2_CID_USER_T23_BASE + 3) + +#endif /* _UAPI_MEDIA_INGENIC_T23_ISP_CONFIG_H */ diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h index 974fd254e5..6e40b86953 100644 --- a/include/uapi/linux/v4l2-controls.h +++ b/include/uapi/linux/v4l2-controls.h @@ -215,6 +215,12 @@ enum v4l2_colorfx { */ #define V4L2_CID_USER_THP7312_BASE (V4L2_CID_USER_BASE + 0x11c0) +/* + * The base for the Ingenic T23 ISP driver controls. + * We reserve 16 controls for this driver. + */ +#define V4L2_CID_USER_T23_BASE (V4L2_CID_USER_BASE + 0x1250) + /* MPEG-class control IDs */ /* The MPEG controls are applicable to all codec controls * and the 'MPEG' part of the define is historical */ diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h index 27239cb640..d544dc425d 100644 --- a/include/uapi/linux/videodev2.h +++ b/include/uapi/linux/videodev2.h @@ -860,6 +860,10 @@ struct v4l2_pix_format { /* Vendor specific - used for RaspberryPi PiSP */ #define V4L2_META_FMT_RPI_BE_CFG v4l2_fourcc('R', 'P', 'B', 'C') /* PiSP BE configuration */ +/* Vendor specific - used for Ingenic T23 ISP */ +#define V4L2_META_FMT_T23_ISP_EXT_PARAMS v4l2_fourcc('T', '2', '3', 'E') /* Ingenic T23 ISP extensible parameters */ +#define V4L2_META_FMT_T23_ISP_STATS v4l2_fourcc('T', '2', '3', 'S') /* Ingenic T23 ISP statistics */ + #ifdef __KERNEL__ /* * Line-based metadata formats. Remember to update v4l_fill_fmtdesc() when From 6d1d2490fdd16e51383aca72e6ed5b63818d8091 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:25:01 -0800 Subject: [PATCH 58/78] dt-bindings: media: Add the Ingenic T23 PTZ motor Describe the GPIO phase outputs, travel limits and reset steps of the T23 camera PTZ mechanism. Signed-off-by: Difan Zhang --- .../bindings/media/ingenic,t23-motor.yaml | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/ingenic,t23-motor.yaml diff --git a/Documentation/devicetree/bindings/media/ingenic,t23-motor.yaml b/Documentation/devicetree/bindings/media/ingenic,t23-motor.yaml new file mode 100644 index 0000000000..dbaa8d4d2b --- /dev/null +++ b/Documentation/devicetree/bindings/media/ingenic,t23-motor.yaml @@ -0,0 +1,86 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/ingenic,t23-motor.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 GPIO Unipolar Stepper Pan/Tilt Camera Motor + +maintainers: + - Paul Cercueil + +properties: + compatible: + const: ingenic,t23-motor + + pan-gpios: + minItems: 4 + maxItems: 4 + description: Four unipolar phase GPIOs driving the pan stepper motor. + + tilt-gpios: + minItems: 4 + maxItems: 4 + description: Four unipolar phase GPIOs driving the tilt stepper motor. + + switch-gpios: + maxItems: 1 + description: + Optional output multiplexer GPIO that switches a shared 4-wire coil + driver between the pan and tilt motor windings. + + ingenic,pan-max-steps: + description: Maximum travel range of the pan axis in motor steps. + $ref: /schemas/types.yaml#/definitions/uint32 + + ingenic,tilt-max-steps: + description: Maximum travel range of the tilt axis in motor steps. + $ref: /schemas/types.yaml#/definitions/uint32 + + ingenic,pan-max-degrees: + description: Angular range of the pan axis corresponding to pan-max-steps. + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 360 + default: 355 + + ingenic,tilt-max-degrees: + description: Angular range of the tilt axis corresponding to tilt-max-steps. + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 360 + default: 90 + + ingenic,pan-reset-steps: + description: Homing step count driven along the pan axis during reset. + $ref: /schemas/types.yaml#/definitions/uint32 + + ingenic,tilt-reset-steps: + description: Homing step count driven along the tilt axis during reset. + $ref: /schemas/types.yaml#/definitions/uint32 + + ingenic,tilt-step-div: + description: Timer divider applied to tilt step pulses relative to pan. + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 64 + default: 1 + +required: + - compatible + - pan-gpios + +additionalProperties: false + +examples: + - | + #include + + motor { + compatible = "ingenic,t23-motor"; + pan-gpios = <&gpb 22 GPIO_ACTIVE_HIGH>, + <&gpb 21 GPIO_ACTIVE_HIGH>, + <&gpb 20 GPIO_ACTIVE_HIGH>, + <&gpb 19 GPIO_ACTIVE_HIGH>; + switch-gpios = <&gpa 17 GPIO_ACTIVE_HIGH>; + }; From 8d12defa97ccae1d4c72d189692add4ef5edc5bd Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 05:52:22 -0800 Subject: [PATCH 59/78] media: platform: ingenic: Add the T23 PTZ motor driver Drive the camera pan and tilt stepper phases using GPIO descriptors and an hrtimer pulse generator, exposed through V4L2 motor controls. Signed-off-by: Difan Zhang --- drivers/media/platform/Kconfig | 1 + drivers/media/platform/Makefile | 1 + drivers/media/platform/ingenic/Kconfig | 20 + drivers/media/platform/ingenic/Makefile | 2 + drivers/media/platform/ingenic/ptz-motor.c | 884 +++++++++++++++++++++ 5 files changed, 908 insertions(+) create mode 100644 drivers/media/platform/ingenic/Kconfig create mode 100644 drivers/media/platform/ingenic/Makefile create mode 100644 drivers/media/platform/ingenic/ptz-motor.c diff --git a/drivers/media/platform/Kconfig b/drivers/media/platform/Kconfig index 85d2627776..fda786fbab 100644 --- a/drivers/media/platform/Kconfig +++ b/drivers/media/platform/Kconfig @@ -71,6 +71,7 @@ source "drivers/media/platform/broadcom/Kconfig" source "drivers/media/platform/cadence/Kconfig" source "drivers/media/platform/chips-media/Kconfig" source "drivers/media/platform/imagination/Kconfig" +source "drivers/media/platform/ingenic/Kconfig" source "drivers/media/platform/intel/Kconfig" source "drivers/media/platform/marvell/Kconfig" source "drivers/media/platform/mediatek/Kconfig" diff --git a/drivers/media/platform/Makefile b/drivers/media/platform/Makefile index ace4e34483..29813fdf29 100644 --- a/drivers/media/platform/Makefile +++ b/drivers/media/platform/Makefile @@ -14,6 +14,7 @@ obj-y += broadcom/ obj-y += cadence/ obj-y += chips-media/ obj-y += imagination/ +obj-y += ingenic/ obj-y += intel/ obj-y += marvell/ obj-y += mediatek/ diff --git a/drivers/media/platform/ingenic/Kconfig b/drivers/media/platform/ingenic/Kconfig new file mode 100644 index 0000000000..15b63a3592 --- /dev/null +++ b/drivers/media/platform/ingenic/Kconfig @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Ingenic media platform drivers +# + +comment "Ingenic media platform drivers" + +config VIDEO_INGENIC_PTZ_MOTOR + tristate "Ingenic T23 GPIO PTZ stepper motor" + depends on V4L_PLATFORM_DRIVERS + depends on VIDEO_DEV && GPIOLIB && HIGH_RES_TIMERS + depends on MACH_INGENIC || COMPILE_TEST + select MEDIA_CONTROLLER + select VIDEO_V4L2_SUBDEV_API + help + V4L2 pan/tilt stepper motor driver for Ingenic T23 camera boards + using an hrtimer pulse generator. + + To compile this driver as a module, choose M here: the module + will be called ptz-motor. diff --git a/drivers/media/platform/ingenic/Makefile b/drivers/media/platform/ingenic/Makefile new file mode 100644 index 0000000000..a6d9d0a54f --- /dev/null +++ b/drivers/media/platform/ingenic/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_VIDEO_INGENIC_PTZ_MOTOR) += ptz-motor.o diff --git a/drivers/media/platform/ingenic/ptz-motor.c b/drivers/media/platform/ingenic/ptz-motor.c new file mode 100644 index 0000000000..c947d2ffe8 --- /dev/null +++ b/drivers/media/platform/ingenic/ptz-motor.c @@ -0,0 +1,884 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 PTZ stepper motor driver using hrtimer pulse generator + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MOTOR_STOP_DECEL_THRESH_X 31 +#define MOTOR_STOP_DECEL_TARGET_X 29 +#define MOTOR_STOP_DECEL_THRESH_Y 8 +#define MOTOR_STOP_DECEL_TARGET_Y 6 + +#define MOTOR_PHASE_ST1 BIT(3) +#define MOTOR_PHASE_ST2 BIT(2) +#define MOTOR_PHASE_ST3 BIT(1) +#define MOTOR_PHASE_ST4 BIT(0) + +static const unsigned char step_8[8] = { + 0x08, 0x0c, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09 +}; + +static const unsigned char pan_ramp_div[16] = { + 0x02, 0x00, 0x00, 0x00, 0x03, 0x02, 0x00, 0x00, + 0x04, 0x03, 0x02, 0x00, 0x04, 0x03, 0x02, 0x01 +}; + +enum motor_channel_state { + MOTOR_STATE_MOVE = 0, + MOTOR_STATE_CRUISE = 1, + MOTOR_STATE_RESET = 2, + MOTOR_STATE_STOP = 3, +}; + +struct motor_coils { + struct gpio_desc *coil[4]; +}; + +struct motor_channel { + struct motor_coils *coils; + int max_steps; + int cur_steps; + int reset_steps; + int direction; + enum motor_channel_state state; + wait_queue_head_t wait; +}; + +struct motor_device { + struct kref ref; + struct device *dev; + struct media_device mdev; + struct v4l2_device v4l2_dev; + struct v4l2_subdev sd; + atomic_t subdev_kref; + struct v4l2_ctrl_handler ctrls; + struct v4l2_ctrl *pan_rel; + struct v4l2_ctrl *tilt_rel; + struct v4l2_ctrl *pan_abs; + struct v4l2_ctrl *tilt_abs; + struct v4l2_ctrl *pan_reset; + struct v4l2_ctrl *tilt_reset; + struct v4l2_ctrl *pan_speed_ctrl; + struct v4l2_ctrl *tilt_speed_ctrl; + struct motor_coils coils[2]; + struct gpio_desc *switch_gpiod; + struct motor_channel motors[2]; + int pan_max_steps; + int tilt_max_steps; + u32 pan_max_degrees; + u32 tilt_max_degrees; + int pan_reset_steps; + int tilt_reset_steps; + int tilt_step_div; + const unsigned char *ramp_div; + unsigned int irq_count; + struct completion completion; + bool wait_flag; + struct hrtimer timer; + ktime_t step_interval; + int pan_speed; + int tilt_speed; + struct mutex dev_mutex; + spinlock_t lock; + enum motor_channel_state status; + int switch_flag; + int target_step_x; + int target_step_y; + int steps_x; + int steps_y; +}; + +static s32 motor_steps_to_arcsec(int steps, int max_steps, u32 max_degrees) +{ + u32 total_arcsec = max_degrees * 3600U; + u64 num; + + if (max_steps <= 0) + return 0; + if (steps >= 0) { + num = (u64)steps * total_arcsec; + return (s32)DIV_ROUND_CLOSEST_ULL(num, (u32)max_steps); + } + num = (u64)(-(s64)steps) * total_arcsec; + return -(s32)DIV_ROUND_CLOSEST_ULL(num, (u32)max_steps); +} + +static int motor_arcsec_to_steps(s32 arcsec, int max_steps, u32 max_degrees) +{ + u32 total_arcsec = max_degrees * 3600U; + u64 num; + + if (!total_arcsec) + return 0; + if (arcsec >= 0) { + num = (u64)arcsec * (u32)max_steps; + return (int)DIV_ROUND_CLOSEST_ULL(num, total_arcsec); + } + num = (u64)(-(s64)arcsec) * (u32)max_steps; + return -(int)DIV_ROUND_CLOSEST_ULL(num, total_arcsec); +} + +static void motor_timer_start(struct motor_device *mdev, int speed_val) +{ + unsigned int speed = clamp_t(int, speed_val, 100, 900); + + mdev->step_interval = ktime_set(0, div_u64(1000000000ULL, speed)); + hrtimer_start(&mdev->timer, mdev->step_interval, HRTIMER_MODE_REL); +} + +static void motor_move_step(struct motor_device *mdev, int motor_idx) +{ + struct motor_channel *chan = &mdev->motors[motor_idx]; + struct motor_coils *coils = chan->coils; + unsigned char phase = 0; + int step; + + if (chan->state != MOTOR_STATE_STOP) { + step = chan->cur_steps % 8; + if (step < 0) + step += 8; + phase = step_8[step]; + } + + if (coils->coil[0]) + gpiod_set_value(coils->coil[0], !!(phase & MOTOR_PHASE_ST1)); + if (coils->coil[1]) + gpiod_set_value(coils->coil[1], !!(phase & MOTOR_PHASE_ST2)); + if (coils->coil[2]) + gpiod_set_value(coils->coil[2], !!(phase & MOTOR_PHASE_ST3)); + if (coils->coil[3]) + gpiod_set_value(coils->coil[3], !!(phase & MOTOR_PHASE_ST4)); + + if (chan->state == MOTOR_STATE_RESET) + chan->reset_steps++; +} + +static bool motor_step_tick(struct motor_device *mdev) +{ + struct motor_channel *m0 = &mdev->motors[0]; + struct motor_channel *m1 = &mdev->motors[1]; + unsigned int count, skip; + + if (m0->state == MOTOR_STATE_STOP && m1->state == MOTOR_STATE_STOP) { + mdev->status = MOTOR_STATE_STOP; + motor_move_step(mdev, 0); + motor_move_step(mdev, 1); + if (mdev->wait_flag) { + mdev->wait_flag = false; + complete(&mdev->completion); + } + return false; + } + + if (m0->cur_steps < 1) { + if (m0->state == MOTOR_STATE_MOVE && m0->direction == -1) + m0->state = MOTOR_STATE_STOP; + else if (m0->state == MOTOR_STATE_CRUISE) + m0->direction = 1; + m0->cur_steps = 0; + } + + if (m0->cur_steps >= m0->max_steps) { + if (m0->state == MOTOR_STATE_RESET) { + m0->state = MOTOR_STATE_STOP; + m0->max_steps = mdev->pan_max_steps; + wake_up(&m0->wait); + m0->direction = -1; + } else if (m0->state == MOTOR_STATE_MOVE) { + if (m0->direction == 1) + m0->state = MOTOR_STATE_STOP; + } else { + m0->direction = -1; + } + m0->cur_steps = m0->max_steps; + } + + if (m1->cur_steps < 1) { + if (m1->state == MOTOR_STATE_MOVE && m1->direction == -1) + m1->state = MOTOR_STATE_STOP; + else if (m1->state == MOTOR_STATE_CRUISE) + m1->direction = 1; + m1->cur_steps = 0; + } + + if (m1->cur_steps >= m1->max_steps) { + if (m1->state == MOTOR_STATE_RESET) { + m1->state = MOTOR_STATE_STOP; + m1->max_steps = mdev->tilt_max_steps; + wake_up(&m1->wait); + m1->direction = -1; + } else if (m1->state == MOTOR_STATE_MOVE) { + if (m1->direction == 1) + m1->state = MOTOR_STATE_STOP; + } else { + m1->direction = -1; + } + m1->cur_steps = m1->max_steps; + } + + if (mdev->status == MOTOR_STATE_CRUISE) { + m0->cur_steps += m0->direction; + count = ++mdev->irq_count; + if (!(count % mdev->tilt_step_div)) + m1->cur_steps += m1->direction; + motor_move_step(mdev, 0); + motor_move_step(mdev, 1); + return true; + } + + if (mdev->status == MOTOR_STATE_RESET) { + if (m0->state != MOTOR_STATE_STOP) { + m0->cur_steps += m0->direction; + motor_move_step(mdev, 0); + mdev->steps_x++; + } + if (m1->state != MOTOR_STATE_STOP) { + m1->cur_steps += m1->direction; + motor_move_step(mdev, 1); + mdev->steps_y++; + } + return true; + } + + count = ++mdev->irq_count; + if (mdev->steps_x < mdev->target_step_x && m0->state != MOTOR_STATE_STOP) { + skip = min_t(unsigned int, + (mdev->target_step_x - mdev->steps_x) / 10, 3); + skip = mdev->ramp_div[skip]; + if (skip && !(count % skip)) { + m0->cur_steps += m0->direction; + motor_move_step(mdev, 0); + mdev->steps_x++; + } + } else { + m0->state = MOTOR_STATE_STOP; + motor_move_step(mdev, 0); + } + + if (mdev->steps_y < mdev->target_step_y && m1->state != MOTOR_STATE_STOP) { + if (count % mdev->tilt_step_div) + return true; + m1->cur_steps += m1->direction; + mdev->steps_y++; + } else { + m1->state = MOTOR_STATE_STOP; + } + motor_move_step(mdev, 1); + return true; +} + +static enum hrtimer_restart motor_timer_callback(struct hrtimer *timer) +{ + struct motor_device *mdev = container_of(timer, struct motor_device, timer); + unsigned long flags; + ktime_t interval; + bool running; + + spin_lock_irqsave(&mdev->lock, flags); + running = motor_step_tick(mdev); + interval = mdev->step_interval; + spin_unlock_irqrestore(&mdev->lock, flags); + + if (running) { + hrtimer_forward_now(timer, interval); + return HRTIMER_RESTART; + } + + return HRTIMER_NORESTART; +} + +static void motor_set_default(struct motor_device *mdev) +{ + int i; + + mdev->status = MOTOR_STATE_STOP; + for (i = 0; i < 2; i++) { + mdev->motors[i].state = MOTOR_STATE_STOP; + motor_move_step(mdev, i); + } +} + +static void motor_get_message(struct motor_device *mdev, unsigned int *msg) +{ + unsigned long flags; + + mutex_lock(&mdev->dev_mutex); + spin_lock_irqsave(&mdev->lock, flags); + msg[0] = mdev->motors[0].cur_steps; + msg[1] = mdev->motors[1].cur_steps; + + if (mdev->switch_flag == 1 && + mdev->motors[1].max_steps != mdev->tilt_max_steps + 30) { + msg[0] = mdev->motors[1].cur_steps; + msg[1] = mdev->motors[0].cur_steps; + } + + msg[2] = mdev->status == MOTOR_STATE_STOP ? 0 : 1; + spin_unlock_irqrestore(&mdev->lock, flags); + mutex_unlock(&mdev->dev_mutex); +} + +static int motor_ops_move(struct motor_device *mdev, int hstep, int vstep) +{ + unsigned long flags; + int step_val; + + if (hstep == INT_MIN || vstep == INT_MIN) + return -ERANGE; + if (!hstep && !vstep) + return 0; + + mutex_lock(&mdev->dev_mutex); + spin_lock_irqsave(&mdev->lock, flags); + + if (!mdev->switch_gpiod) { + if ((hstep > 0 && + mdev->motors[0].cur_steps >= mdev->motors[0].max_steps) || + (hstep < 0 && mdev->motors[0].cur_steps <= 0)) + hstep = 0; + if ((vstep > 0 && + mdev->motors[1].cur_steps >= mdev->motors[1].max_steps) || + (vstep < 0 && mdev->motors[1].cur_steps <= 0)) + vstep = 0; + if (!hstep && !vstep) + goto unlock; + + mdev->target_step_x = abs(hstep); + mdev->target_step_y = abs(vstep); + mdev->ramp_div = &pan_ramp_div[min_t(unsigned int, + mdev->target_step_x / 10, 3) * 4]; + mdev->irq_count = 0; + mdev->status = MOTOR_STATE_MOVE; + mdev->steps_x = 0; + mdev->steps_y = 0; + mdev->motors[0].state = MOTOR_STATE_MOVE; + mdev->motors[0].direction = (hstep > 0) ? 1 : -1; + mdev->motors[1].state = MOTOR_STATE_MOVE; + mdev->motors[1].direction = (vstep > 0) ? 1 : -1; + motor_timer_start(mdev, + (hstep && vstep) ? max(mdev->pan_speed, mdev->tilt_speed) : + (hstep ? mdev->pan_speed : mdev->tilt_speed)); + goto unlock; + } + + if (hstep == 0) { + if (mdev->switch_flag == 1) { + if (mdev->motors[1].max_steps != mdev->tilt_max_steps + 30) { + int tmp = mdev->motors[1].cur_steps; + + mdev->motors[1].max_steps = mdev->tilt_max_steps + 30; + mdev->motors[1].cur_steps = mdev->motors[0].cur_steps; + mdev->motors[0].cur_steps = tmp; + } + mdev->switch_flag = 0; + } + gpiod_set_value(mdev->switch_gpiod, 1); + step_val = vstep; + } else { + gpiod_set_value(mdev->switch_gpiod, 0); + if (mdev->motors[1].max_steps != mdev->pan_max_steps + 100) { + int tmp = mdev->motors[0].cur_steps; + + mdev->motors[1].max_steps = mdev->pan_max_steps + 100; + mdev->motors[0].cur_steps = mdev->motors[1].cur_steps; + mdev->motors[1].cur_steps = tmp; + } + mdev->switch_flag = 1; + step_val = hstep; + } + + if ((step_val > 0 && + mdev->motors[1].cur_steps >= mdev->motors[1].max_steps) || + (step_val < 0 && mdev->motors[1].cur_steps <= 0)) + goto unlock; + + mdev->ramp_div = pan_ramp_div; + mdev->irq_count = 0; + mdev->status = MOTOR_STATE_MOVE; + mdev->target_step_x = 0; + mdev->target_step_y = abs(step_val); + mdev->steps_x = 0; + mdev->steps_y = 0; + mdev->motors[0].state = MOTOR_STATE_MOVE; + mdev->motors[0].direction = -1; + mdev->motors[1].state = MOTOR_STATE_MOVE; + mdev->motors[1].direction = (step_val > 0) ? 1 : -1; + + motor_timer_start(mdev, hstep ? mdev->pan_speed : mdev->tilt_speed); + +unlock: + spin_unlock_irqrestore(&mdev->lock, flags); + mutex_unlock(&mdev->dev_mutex); + return 0; +} + +static int motor_ops_stop(struct motor_device *mdev) +{ + unsigned long flags; + long ret = 1; + + mutex_lock(&mdev->dev_mutex); + spin_lock_irqsave(&mdev->lock, flags); + if (mdev->status != MOTOR_STATE_STOP) { + if (mdev->status == MOTOR_STATE_MOVE) { + if ((u32)mdev->target_step_x - + (u32)mdev->steps_x >= MOTOR_STOP_DECEL_THRESH_X) { + mdev->target_step_x = MOTOR_STOP_DECEL_TARGET_X; + mdev->steps_x = 0; + mdev->ramp_div = &pan_ramp_div[min_t(unsigned int, + mdev->target_step_x / 10, 3) * 4]; + } + if ((u32)mdev->target_step_y - + (u32)mdev->steps_y > MOTOR_STOP_DECEL_THRESH_Y) { + mdev->target_step_y = MOTOR_STOP_DECEL_TARGET_Y; + mdev->steps_y = 0; + } + } else if (mdev->status == MOTOR_STATE_CRUISE) { + mdev->status = MOTOR_STATE_MOVE; + mdev->motors[0].state = MOTOR_STATE_MOVE; + mdev->motors[1].state = MOTOR_STATE_MOVE; + mdev->target_step_x = 0; + mdev->target_step_y = 0; + mdev->steps_x = 0; + mdev->steps_y = 0; + } + mdev->irq_count = 0; + reinit_completion(&mdev->completion); + mdev->wait_flag = true; + spin_unlock_irqrestore(&mdev->lock, flags); + + ret = wait_for_completion_interruptible_timeout(&mdev->completion, + msecs_to_jiffies(15000)); + + spin_lock_irqsave(&mdev->lock, flags); + } + mdev->status = MOTOR_STATE_STOP; + mdev->motors[0].state = MOTOR_STATE_STOP; + mdev->motors[1].state = MOTOR_STATE_STOP; + spin_unlock_irqrestore(&mdev->lock, flags); + + hrtimer_cancel(&mdev->timer); + + spin_lock_irqsave(&mdev->lock, flags); + if (mdev->switch_flag == 1) { + if (mdev->motors[1].max_steps != mdev->tilt_max_steps + 30) { + int tmp = mdev->motors[1].cur_steps; + + mdev->motors[1].max_steps = mdev->tilt_max_steps + 30; + mdev->motors[1].cur_steps = mdev->motors[0].cur_steps; + mdev->motors[0].cur_steps = tmp; + } + mdev->switch_flag = 0; + } + mdev->wait_flag = false; + motor_set_default(mdev); + spin_unlock_irqrestore(&mdev->lock, flags); + mutex_unlock(&mdev->dev_mutex); + + if (ret < 0) + return ret; + if (!ret) + return -ETIMEDOUT; + return 0; +} + +static int motor_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct motor_device *mdev = container_of(ctrl->handler, struct motor_device, ctrls); + unsigned int msg[3]; + + switch (ctrl->id) { + case V4L2_CID_PAN_RELATIVE: + return motor_ops_move(mdev, + motor_arcsec_to_steps(ctrl->val, mdev->pan_max_steps, + mdev->pan_max_degrees), + 0); + case V4L2_CID_TILT_RELATIVE: + return motor_ops_move(mdev, 0, + motor_arcsec_to_steps(ctrl->val, mdev->tilt_max_steps, + mdev->tilt_max_degrees)); + case V4L2_CID_PAN_ABSOLUTE: { + int target = motor_arcsec_to_steps(ctrl->val, mdev->pan_max_steps, + mdev->pan_max_degrees); + + motor_get_message(mdev, msg); + return motor_ops_move(mdev, target - (int)msg[0], 0); + } + case V4L2_CID_TILT_ABSOLUTE: { + int target = motor_arcsec_to_steps(ctrl->val, mdev->tilt_max_steps, + mdev->tilt_max_degrees); + + motor_get_message(mdev, msg); + return motor_ops_move(mdev, 0, target - (int)msg[1]); + } + case V4L2_CID_PAN_RESET: + motor_get_message(mdev, msg); + return motor_ops_move(mdev, mdev->pan_reset_steps - (int)msg[0], 0); + case V4L2_CID_TILT_RESET: + motor_get_message(mdev, msg); + return motor_ops_move(mdev, 0, mdev->tilt_reset_steps - (int)msg[1]); + case V4L2_CID_PAN_SPEED: + case V4L2_CID_TILT_SPEED: { + unsigned long flags; + + if (ctrl->val < 0 || ctrl->val > 900) + return -EINVAL; + mutex_lock(&mdev->dev_mutex); + spin_lock_irqsave(&mdev->lock, flags); + if (ctrl->id == V4L2_CID_PAN_SPEED) + mdev->pan_speed = ctrl->val; + else + mdev->tilt_speed = ctrl->val; + spin_unlock_irqrestore(&mdev->lock, flags); + mutex_unlock(&mdev->dev_mutex); + if (!ctrl->val) + return motor_ops_stop(mdev); + return 0; + } + default: + return -EINVAL; + } +} + +static int motor_g_volatile_ctrl(struct v4l2_ctrl *ctrl) +{ + struct motor_device *mdev = container_of(ctrl->handler, struct motor_device, ctrls); + unsigned int msg[3]; + + motor_get_message(mdev, msg); + switch (ctrl->id) { + case V4L2_CID_PAN_ABSOLUTE: + ctrl->val = motor_steps_to_arcsec(msg[0], mdev->pan_max_steps, + mdev->pan_max_degrees); + return 0; + case V4L2_CID_TILT_ABSOLUTE: + ctrl->val = motor_steps_to_arcsec(msg[1], mdev->tilt_max_steps, + mdev->tilt_max_degrees); + return 0; + default: + return -EINVAL; + } +} + +static const struct v4l2_ctrl_ops motor_ctrl_ops = { + .s_ctrl = motor_s_ctrl, + .g_volatile_ctrl = motor_g_volatile_ctrl, +}; + +static const struct v4l2_subdev_core_ops motor_subdev_core_ops = { + .subscribe_event = v4l2_ctrl_subdev_subscribe_event, + .unsubscribe_event = v4l2_event_subdev_unsubscribe, +}; + +static const struct v4l2_subdev_ops motor_subdev_ops = { + .core = &motor_subdev_core_ops, +}; + +static void motor_free(struct kref *ref) +{ + struct motor_device *mdev = container_of(ref, struct motor_device, ref); + + v4l2_ctrl_handler_free(&mdev->ctrls); + kfree(mdev); +} + +static void motor_put(void *data) +{ + struct motor_device *mdev = data; + + kref_put(&mdev->ref, motor_free); +} + +static void motor_subdev_release(struct v4l2_subdev *sd) +{ + struct motor_device *mdev = v4l2_get_subdevdata(sd); + + if (mdev && atomic_xchg(&mdev->subdev_kref, 0)) + motor_put(mdev); +} + +static const struct v4l2_subdev_internal_ops motor_subdev_internal_ops = { + .release = motor_subdev_release, +}; + +static int motor_get_coils(struct device *dev, const char *con_id, + struct motor_coils *out) +{ + int i; + + for (i = 0; i < 4; i++) { + struct gpio_desc *desc; + + desc = devm_gpiod_get_index_optional(dev, con_id, i, + GPIOD_OUT_LOW); + if (IS_ERR(desc)) + return PTR_ERR(desc); + if (!desc) { + if (i == 0) + return 1; + return -EINVAL; + } + if (gpiod_cansleep(desc)) + return -EINVAL; + out->coil[i] = desc; + } + return 0; +} + +static int motor_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; + struct motor_device *mdev; + s32 pan_max_arcsec, tilt_max_arcsec, pan_def_arcsec, tilt_def_arcsec; + u32 val; + int ret, i; + + mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); + if (!mdev) + return -ENOMEM; + + kref_init(&mdev->ref); + v4l2_ctrl_handler_init(&mdev->ctrls, 8); + ret = devm_add_action_or_reset(dev, motor_put, mdev); + if (ret) + return ret; + + mdev->dev = dev; + mdev->pan_max_steps = 3800; + mdev->tilt_max_steps = 1200; + mdev->pan_max_degrees = 355; + mdev->tilt_max_degrees = 90; + mdev->pan_reset_steps = mdev->pan_max_steps / 2; + mdev->tilt_reset_steps = 600; + mdev->tilt_step_div = 1; + mdev->pan_speed = 900; + mdev->tilt_speed = 900; + + if (np) { + if (!of_property_read_u32(np, "ingenic,pan-max-steps", &val)) { + if (!val || val > INT_MAX - 100) + return -EINVAL; + mdev->pan_max_steps = val; + mdev->pan_reset_steps = val / 2; + } + if (!of_property_read_u32(np, "ingenic,tilt-max-steps", &val)) { + if (!val || val > INT_MAX - 30) + return -EINVAL; + mdev->tilt_max_steps = val; + mdev->tilt_reset_steps = min(mdev->tilt_reset_steps, + (int)val); + } + if (!of_property_read_u32(np, "ingenic,pan-max-degrees", &val)) { + if (!val || val > 360) + return -EINVAL; + mdev->pan_max_degrees = val; + } + if (!of_property_read_u32(np, "ingenic,tilt-max-degrees", &val)) { + if (!val || val > 360) + return -EINVAL; + mdev->tilt_max_degrees = val; + } + if (!of_property_read_u32(np, "ingenic,pan-reset-steps", &val)) { + if (val > mdev->pan_max_steps) + return -EINVAL; + mdev->pan_reset_steps = val; + } + if (!of_property_read_u32(np, "ingenic,tilt-reset-steps", &val)) { + if (val > mdev->tilt_max_steps) + return -EINVAL; + mdev->tilt_reset_steps = val; + } + if (!of_property_read_u32(np, "ingenic,tilt-step-div", &val)) { + if (!val || val > 64) + return -EINVAL; + mdev->tilt_step_div = val; + } + } + + mdev->switch_gpiod = devm_gpiod_get_optional(dev, "switch", + GPIOD_OUT_HIGH); + if (IS_ERR(mdev->switch_gpiod)) + return PTR_ERR(mdev->switch_gpiod); + if (mdev->switch_gpiod && gpiod_cansleep(mdev->switch_gpiod)) + return -EINVAL; + + ret = motor_get_coils(dev, "pan", &mdev->coils[0]); + if (ret < 0) + return ret; + ret = motor_get_coils(dev, "tilt", &mdev->coils[1]); + if (ret < 0) + return ret; + if (ret == 1) + mdev->coils[1] = mdev->coils[0]; + + mutex_init(&mdev->dev_mutex); + spin_lock_init(&mdev->lock); + init_completion(&mdev->completion); + + hrtimer_init(&mdev->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + mdev->timer.function = motor_timer_callback; + mdev->step_interval = ktime_set(0, div_u64(1000000000ULL, mdev->pan_speed)); + + for (i = 0; i < 2; i++) { + mdev->motors[i].coils = &mdev->coils[i]; + init_waitqueue_head(&mdev->motors[i].wait); + } + mdev->motors[0].max_steps = mdev->pan_max_steps + 100; + mdev->motors[1].max_steps = mdev->tilt_max_steps + 30; + + motor_set_default(mdev); + + mdev->mdev.dev = dev; + strscpy(mdev->mdev.model, "Ingenic T23 PTZ Motor", sizeof(mdev->mdev.model)); + media_device_init(&mdev->mdev); + + mdev->v4l2_dev.mdev = &mdev->mdev; + ret = v4l2_device_register(dev, &mdev->v4l2_dev); + if (ret) + goto err_media; + + pan_max_arcsec = mdev->pan_max_degrees * 3600U; + tilt_max_arcsec = mdev->tilt_max_degrees * 3600U; + pan_def_arcsec = motor_steps_to_arcsec(mdev->pan_reset_steps, + mdev->pan_max_steps, + mdev->pan_max_degrees); + tilt_def_arcsec = motor_steps_to_arcsec(mdev->tilt_reset_steps, + mdev->tilt_max_steps, + mdev->tilt_max_degrees); + + mdev->pan_rel = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_PAN_RELATIVE, + -pan_max_arcsec, pan_max_arcsec, + 1, 0); + mdev->tilt_rel = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_TILT_RELATIVE, + -tilt_max_arcsec, tilt_max_arcsec, + 1, 0); + mdev->pan_abs = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_PAN_ABSOLUTE, 0, + pan_max_arcsec, 1, pan_def_arcsec); + if (mdev->pan_abs) + mdev->pan_abs->flags |= V4L2_CTRL_FLAG_VOLATILE | + V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; + mdev->tilt_abs = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_TILT_ABSOLUTE, 0, + tilt_max_arcsec, 1, tilt_def_arcsec); + if (mdev->tilt_abs) + mdev->tilt_abs->flags |= V4L2_CTRL_FLAG_VOLATILE | + V4L2_CTRL_FLAG_EXECUTE_ON_WRITE; + mdev->pan_reset = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_PAN_RESET, 0, 0, 0, 0); + mdev->tilt_reset = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_TILT_RESET, 0, 0, 0, 0); + mdev->pan_speed_ctrl = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_PAN_SPEED, + 0, 900, 1, 900); + mdev->tilt_speed_ctrl = v4l2_ctrl_new_std(&mdev->ctrls, &motor_ctrl_ops, + V4L2_CID_TILT_SPEED, + 0, 900, 1, 900); + if (mdev->ctrls.error) { + ret = mdev->ctrls.error; + goto err_v4l2; + } + mdev->v4l2_dev.ctrl_handler = &mdev->ctrls; + + v4l2_subdev_init(&mdev->sd, &motor_subdev_ops); + mdev->sd.owner = THIS_MODULE; + mdev->sd.dev = dev; + mdev->sd.internal_ops = &motor_subdev_internal_ops; + mdev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; + mdev->sd.entity.function = MEDIA_ENT_F_LENS; + mdev->sd.ctrl_handler = &mdev->ctrls; + strscpy(mdev->sd.name, "t23-motor", sizeof(mdev->sd.name)); + v4l2_set_subdevdata(&mdev->sd, mdev); + + ret = v4l2_device_register_subdev(&mdev->v4l2_dev, &mdev->sd); + if (ret) + goto err_v4l2; + + kref_get(&mdev->ref); + atomic_set(&mdev->subdev_kref, 1); + ret = v4l2_device_register_subdev_nodes(&mdev->v4l2_dev); + if (ret) { + if (atomic_xchg(&mdev->subdev_kref, 0)) + motor_put(mdev); + goto err_v4l2; + } + + ret = media_device_register(&mdev->mdev); + if (ret) + goto err_v4l2; + + platform_set_drvdata(pdev, mdev); + return 0; + +err_v4l2: + v4l2_device_unregister(&mdev->v4l2_dev); +err_media: + media_device_cleanup(&mdev->mdev); + return ret; +} + +static void motor_remove(struct platform_device *pdev) +{ + struct motor_device *mdev = platform_get_drvdata(pdev); + + if (!mdev) + return; + + v4l2_device_unregister(&mdev->v4l2_dev); + media_device_unregister(&mdev->mdev); + media_device_cleanup(&mdev->mdev); + motor_ops_stop(mdev); + hrtimer_cancel(&mdev->timer); + motor_set_default(mdev); + if (mdev->switch_gpiod) + gpiod_set_value(mdev->switch_gpiod, 0); +} + +static const struct of_device_id motor_of_match[] = { + { .compatible = "ingenic,t23-motor" }, + { } +}; +MODULE_DEVICE_TABLE(of, motor_of_match); + +static struct platform_driver motor_driver = { + .probe = motor_probe, + .remove = motor_remove, + .driver = { + .name = "ptz-motor", + .of_match_table = motor_of_match, + }, +}; +module_platform_driver(motor_driver); + +MODULE_AUTHOR("Difan Zhang"); +MODULE_DESCRIPTION("Ingenic T23 PTZ Stepper Motor Driver"); +MODULE_LICENSE("GPL"); From a3c45138eaa67cd247d20ca3bd3f3659db916d8c Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:25:01 -0800 Subject: [PATCH 60/78] dt-bindings: media: i2c: Add GalaxyCore GC2053 sensors Describe the GC2053 CSI-2 sensor clocks, reset and power-down GPIOs, and media endpoint. Signed-off-by: Difan Zhang --- .../bindings/media/i2c/galaxycore,gc2053.yaml | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/galaxycore,gc2053.yaml diff --git a/Documentation/devicetree/bindings/media/i2c/galaxycore,gc2053.yaml b/Documentation/devicetree/bindings/media/i2c/galaxycore,gc2053.yaml new file mode 100644 index 0000000000..09d305bb74 --- /dev/null +++ b/Documentation/devicetree/bindings/media/i2c/galaxycore,gc2053.yaml @@ -0,0 +1,95 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/i2c/galaxycore,gc2053.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: GalaxyCore GC2053 2MP 1080p MIPI CSI-2 Raw Bayer Image Sensor + +maintainers: + - Paul Cercueil + +properties: + compatible: + const: galaxycore,gc2053 + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: xvclk + + avdd-supply: + description: Analog 2.8V power supply regulator. + + dovdd-supply: + description: Digital I/O 1.8V power supply regulator. + + dvdd-supply: + description: Digital core 1.2V power supply regulator. + + reset-gpios: + maxItems: 1 + + pwdn-gpios: + maxItems: 1 + + 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 + + link-frequencies: + maxItems: 1 + items: + const: 297000000 + + required: + - data-lanes + - link-frequencies + + required: + - endpoint + +required: + - compatible + - reg + - clocks + - port + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + sensor@37 { + compatible = "galaxycore,gc2053"; + reg = <0x37>; + clocks = <&sensor_clk>; + clock-names = "xvclk"; + + port { + gc2053_out: endpoint { + data-lanes = <1 2>; + link-frequencies = /bits/ 64 <297000000>; + remote-endpoint = <&csi_in>; + }; + }; + }; + }; From 0760cc80c4fbbb86f028fe267a5763fab853bfb4 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 06:09:37 -0800 Subject: [PATCH 61/78] media: i2c: Add the GalaxyCore GC2053 sensor driver Add active-state subdevice formats, exposure and analogue gain controls, writable VBLANK with exposure-range updates, and frame interval configuration for the GC2053. Signed-off-by: Difan Zhang --- drivers/media/i2c/Kconfig | 9 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/gc2053.c | 945 +++++++++++++++++++++++++++++++++++++ 3 files changed, 955 insertions(+) create mode 100644 drivers/media/i2c/gc2053.c diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index 4703071352..dfcb4e1c3d 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -90,6 +90,15 @@ config VIDEO_GC08A3 To compile this driver as a module, choose M here: the module will be called gc08a3. +config VIDEO_GC2053 + tristate "GalaxyCore GC2053 sensor support" + help + This is a Video4Linux2 sensor driver for the GalaxyCore GC2053 + 1080p MIPI CSI-2 CMOS image sensor. + + To compile this driver as a module, choose M here: the + module will be called gc2053. + config VIDEO_GC2145 select V4L2_CCI_I2C tristate "GalaxyCore GC2145 sensor support" diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index fbb988bd06..7d13d59e05 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -40,6 +40,7 @@ obj-$(CONFIG_VIDEO_ET8EK8) += et8ek8/ obj-$(CONFIG_VIDEO_GC0308) += gc0308.o obj-$(CONFIG_VIDEO_GC05A2) += gc05a2.o obj-$(CONFIG_VIDEO_GC08A3) += gc08a3.o +obj-$(CONFIG_VIDEO_GC2053) += gc2053.o obj-$(CONFIG_VIDEO_GC2145) += gc2145.o obj-$(CONFIG_VIDEO_HI556) += hi556.o obj-$(CONFIG_VIDEO_HI846) += hi846.o diff --git a/drivers/media/i2c/gc2053.c b/drivers/media/i2c/gc2053.c new file mode 100644 index 0000000000..940b208678 --- /dev/null +++ b/drivers/media/i2c/gc2053.c @@ -0,0 +1,945 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * GalaxyCore GC2053 MIPI CSI-2 1080p CMOS Image Sensor Driver + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GC2053_CHIP_ID 0x2053 +#define GC2053_REG_PAGE 0xfe +#define GC2053_REG_CHIP_ID_H 0xf0 +#define GC2053_REG_CHIP_ID_L 0xf1 +#define GC2053_REG_EXP_H 0x03 +#define GC2053_REG_EXP_L 0x04 +#define GC2053_REG_MIRROR 0x17 +#define GC2053_REG_STREAM 0x3e +#define GC2053_REG_VTS_H 0x41 +#define GC2053_REG_VTS_L 0x42 +#define GC2053_REG_TEST_PATTERN 0x8c +#define GC2053_REG_AGAIN_B3 0xb3 +#define GC2053_REG_AGAIN_B4 0xb4 +#define GC2053_REG_AGAIN_B8 0xb8 +#define GC2053_REG_AGAIN_B9 0xb9 + +#define GC2053_XVCLK_FREQ 24000000 +#define GC2053_PIXEL_RATE 74250000LL +#define GC2053_LINK_FREQ_297M 297000000LL + +#define GC2053_WIDTH 1920 +#define GC2053_HEIGHT 1080 +#define GC2053_HTS_DEF 2200 +#define GC2053_HBLANK_DEF (GC2053_HTS_DEF - GC2053_WIDTH) +#define GC2053_VTS_MIN 1125 +#define GC2053_VTS_DEF 1125 +#define GC2053_VTS_MAX 16383 +#define GC2053_EXP_MIN 1 +#define GC2053_EXP_MARGIN 8 + +struct gc2053_regval { + u8 reg; + u8 val; +}; + +struct gc2053_gain_entry { + u8 b4; + u8 b3; + u8 b8; + u8 b9; +}; + +/* + * Standard 26-step GC2053 analog + auto-pregain table (1.00x .. 63.98x). + * Effective linear multiplier is (b8 * 64 + (b9 & 0x3f)) / 64.0. + */ +static const struct gc2053_gain_entry gc2053_gain_lut[] = { + { 0x00, 0x00, 0x01, 0x00 }, /* 1.000x */ + { 0x00, 0x10, 0x01, 0x0c }, /* 1.188x */ + { 0x00, 0x20, 0x01, 0x1b }, /* 1.422x */ + { 0x00, 0x30, 0x01, 0x2c }, /* 1.688x */ + { 0x00, 0x40, 0x01, 0x3f }, /* 1.984x */ + { 0x00, 0x50, 0x02, 0x16 }, /* 2.344x */ + { 0x00, 0x60, 0x02, 0x35 }, /* 2.828x */ + { 0x00, 0x70, 0x03, 0x16 }, /* 3.344x */ + { 0x00, 0x80, 0x04, 0x02 }, /* 4.031x */ + { 0x00, 0x90, 0x04, 0x31 }, /* 4.766x */ + { 0x00, 0xa0, 0x05, 0x32 }, /* 5.781x */ + { 0x00, 0xb0, 0x06, 0x35 }, /* 6.828x */ + { 0x00, 0xc0, 0x08, 0x04 }, /* 8.062x */ + { 0x00, 0x5a, 0x09, 0x19 }, /* 9.391x */ + { 0x00, 0x83, 0x0b, 0x0f }, /* 11.234x */ + { 0x00, 0x93, 0x0d, 0x12 }, /* 13.281x */ + { 0x00, 0x84, 0x10, 0x00 }, /* 16.000x */ + { 0x00, 0x94, 0x12, 0x3a }, /* 18.906x */ + { 0x01, 0x2c, 0x1a, 0x02 }, /* 26.031x */ + { 0x01, 0x3c, 0x1b, 0x20 }, /* 27.500x */ + { 0x00, 0x8c, 0x20, 0x0f }, /* 32.234x */ + { 0x00, 0x9c, 0x26, 0x07 }, /* 38.109x */ + { 0x02, 0x64, 0x36, 0x21 }, /* 54.516x */ + { 0x02, 0x74, 0x37, 0x3a }, /* 55.906x */ + { 0x00, 0xc6, 0x3d, 0x02 }, /* 61.031x */ + { 0x00, 0xdc, 0x3f, 0x3f }, /* 63.984x */ +}; + +/* 1920x1080 RAW10 30fps 24MHz xvclk MIPI CSI-2 init sequence */ +static const struct gc2053_regval gc2053_init_1080p_regs[] = { + { 0xfe, 0x80 }, + { 0xfe, 0x80 }, + { 0xfe, 0x80 }, + { 0xfe, 0x00 }, + { 0xf2, 0x00 }, + { 0xf3, 0x00 }, + { 0xf4, 0x36 }, + { 0xf5, 0xc0 }, + { 0xf6, 0x44 }, + { 0xf7, 0x01 }, + { 0xf8, 0x2c }, + { 0xf9, 0x42 }, + { 0xfc, 0x8e }, + { 0xfe, 0x00 }, + { 0x87, 0x18 }, + { 0xee, 0x30 }, + { 0xd0, 0xb7 }, + { 0x03, 0x04 }, + { 0x04, 0x5d }, + { 0x05, 0x04 }, + { 0x06, 0x4c }, + { 0x07, 0x00 }, + { 0x08, 0x11 }, + { 0x09, 0x00 }, + { 0x0a, 0x02 }, + { 0x0b, 0x00 }, + { 0x0c, 0x02 }, + { 0x0d, 0x04 }, + { 0x0e, 0x40 }, + { 0x12, 0xe2 }, + { 0x13, 0x16 }, + { 0x19, 0x0a }, + { 0x21, 0x1c }, + { 0x28, 0x0a }, + { 0x29, 0x24 }, + { 0x2b, 0x04 }, + { 0x32, 0xf8 }, + { 0x37, 0x03 }, + { 0x39, 0x15 }, + { 0x41, 0x04 }, + { 0x42, 0x65 }, + { 0x43, 0x07 }, + { 0x44, 0x40 }, + { 0x46, 0x0b }, + { 0x4b, 0x20 }, + { 0x4e, 0x08 }, + { 0x55, 0x20 }, + { 0x66, 0x05 }, + { 0x67, 0x05 }, + { 0x77, 0x01 }, + { 0x78, 0x00 }, + { 0x7c, 0x93 }, + { 0x8c, 0x12 }, + { 0x8d, 0x92 }, + { 0x90, 0x00 }, + { 0x9d, 0x10 }, + { 0xce, 0x7c }, + { 0xd2, 0x41 }, + { 0xd3, 0xdc }, + { 0xe6, 0x50 }, + { 0xb6, 0xc0 }, + { 0xb0, 0x70 }, + { 0xb1, 0x01 }, + { 0xb2, 0x00 }, + { 0xb3, 0x00 }, + { 0xb4, 0x00 }, + { 0xb8, 0x01 }, + { 0xb9, 0x00 }, + { 0xfe, 0x01 }, + { 0x55, 0x00 }, + { 0xfe, 0x04 }, + { 0x14, 0x78 }, + { 0x15, 0x78 }, + { 0x16, 0x78 }, + { 0x17, 0x78 }, + { 0xfe, 0x01 }, + { 0x04, 0x00 }, + { 0x94, 0x03 }, + { 0x97, 0x07 }, + { 0x98, 0x80 }, + { 0x9a, 0x06 }, + { 0xfe, 0x00 }, + { 0x7b, 0x2a }, + { 0x23, 0x2d }, + { 0xfe, 0x03 }, + { 0x01, 0x27 }, + { 0x02, 0x56 }, + { 0x03, 0xb6 }, + { 0x12, 0x80 }, + { 0x13, 0x07 }, + { 0x15, 0x12 }, + { 0xfe, 0x00 }, + { 0x3e, 0x00 }, +}; + +static const s64 gc2053_link_freqs[] = { + GC2053_LINK_FREQ_297M, +}; + +static const char * const gc2053_test_pattern_menu[] = { + "Disabled", + "Color Bars", +}; + +static const char * const gc2053_supply_names[] = { + "avdd", + "dovdd", + "dvdd", +}; + +struct gc2053 { + struct i2c_client *client; + struct clk *xvclk; + struct regulator_bulk_data supplies[ARRAY_SIZE(gc2053_supply_names)]; + struct gpio_desc *reset_gpio; + struct gpio_desc *pwdn_gpio; + + struct v4l2_subdev sd; + struct media_pad pad; + struct v4l2_mbus_config bus; + + struct mutex lock; /* serializes subdev active state & controls */ + struct v4l2_ctrl_handler ctrls; + struct v4l2_ctrl *exposure; + struct v4l2_ctrl *again; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *pixel_rate; + struct v4l2_ctrl *hflip; + struct v4l2_ctrl *vflip; + + u32 vts; + bool streaming; +}; + +static inline struct gc2053 *to_gc2053(struct v4l2_subdev *sd) +{ + return container_of(sd, struct gc2053, sd); +} + +static int gc2053_write_reg(struct gc2053 *gc, u8 reg, u8 val) +{ + u8 buf[2] = { reg, val }; + struct i2c_msg msg = { + .addr = gc->client->addr, + .flags = 0, + .len = 2, + .buf = buf, + }; + int ret = i2c_transfer(gc->client->adapter, &msg, 1); + + return ret == 1 ? 0 : (ret < 0 ? ret : -EIO); +} + +static int gc2053_read_reg(struct gc2053 *gc, u8 reg, u8 *val) +{ + struct i2c_msg msgs[2] = { + { + .addr = gc->client->addr, + .flags = 0, + .len = 1, + .buf = ®, + }, + { + .addr = gc->client->addr, + .flags = I2C_M_RD, + .len = 1, + .buf = val, + }, + }; + int ret = i2c_transfer(gc->client->adapter, msgs, 2); + + return ret == 2 ? 0 : (ret < 0 ? ret : -EIO); +} + +static int gc2053_write_array(struct gc2053 *gc, + const struct gc2053_regval *regs, size_t count) +{ + size_t i; + int ret; + + for (i = 0; i < count; i++) { + ret = gc2053_write_reg(gc, regs[i].reg, regs[i].val); + if (ret) + return ret; + } + return 0; +} + +static int gc2053_set_exposure(struct gc2053 *gc, u32 lines) +{ + int ret; + + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + return ret; + ret = gc2053_write_reg(gc, GC2053_REG_EXP_H, (lines >> 8) & 0x3f); + if (ret) + return ret; + return gc2053_write_reg(gc, GC2053_REG_EXP_L, lines & 0xff); +} + +static int gc2053_set_vts(struct gc2053 *gc, u32 vts) +{ + int ret; + + gc->vts = vts; + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + return ret; + ret = gc2053_write_reg(gc, GC2053_REG_VTS_H, (vts >> 8) & 0x3f); + if (ret) + return ret; + return gc2053_write_reg(gc, GC2053_REG_VTS_L, vts & 0xff); +} + +static int gc2053_set_again(struct gc2053 *gc, s32 index) +{ + const struct gc2053_gain_entry *g; + int ret; + + if (index < 0 || index >= ARRAY_SIZE(gc2053_gain_lut)) + return -EINVAL; + g = &gc2053_gain_lut[index]; + + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + return ret; + ret = gc2053_write_reg(gc, GC2053_REG_AGAIN_B4, g->b4); + if (ret) + return ret; + ret = gc2053_write_reg(gc, GC2053_REG_AGAIN_B3, g->b3); + if (ret) + return ret; + ret = gc2053_write_reg(gc, GC2053_REG_AGAIN_B8, g->b8); + if (ret) + return ret; + return gc2053_write_reg(gc, GC2053_REG_AGAIN_B9, g->b9); +} + +static int gc2053_set_flip(struct gc2053 *gc, bool hflip, bool vflip) +{ + u8 val = 0x80 | (vflip ? 0x02 : 0x00) | (hflip ? 0x01 : 0x00); + int ret; + + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + return ret; + return gc2053_write_reg(gc, GC2053_REG_MIRROR, val); +} + +static int gc2053_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct gc2053 *gc = container_of(ctrl->handler, struct gc2053, ctrls); + s32 old_exp = gc->exposure ? gc->exposure->val : 0; + int ret; + + if (ctrl->id == V4L2_CID_VBLANK && gc->exposure) { + u32 vts = GC2053_HEIGHT + ctrl->val; + s32 max_exp = vts - GC2053_EXP_MARGIN; + + __v4l2_ctrl_modify_range(gc->exposure, GC2053_EXP_MIN, max_exp, + 1, min_t(s32, gc->exposure->default_value, + max_exp)); + if (!gc->streaming) { + gc->vts = vts; + return 0; + } + } else if (!gc->streaming) { + return 0; + } + + switch (ctrl->id) { + case V4L2_CID_EXPOSURE: + return gc2053_set_exposure(gc, ctrl->val); + case V4L2_CID_ANALOGUE_GAIN: + return gc2053_set_again(gc, ctrl->val); + case V4L2_CID_VBLANK: + if (gc->exposure && gc->exposure->val < old_exp) { + ret = gc2053_set_exposure(gc, gc->exposure->val); + if (ret) + return ret; + } + return gc2053_set_vts(gc, GC2053_HEIGHT + ctrl->val); + case V4L2_CID_HFLIP: + case V4L2_CID_VFLIP: + return gc2053_set_flip(gc, + ctrl->id == V4L2_CID_HFLIP ? ctrl->val : + (gc->hflip ? gc->hflip->val : 0), + ctrl->id == V4L2_CID_VFLIP ? ctrl->val : + (gc->vflip ? gc->vflip->val : 0)); + case V4L2_CID_TEST_PATTERN: + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + return ret; + return gc2053_write_reg(gc, GC2053_REG_TEST_PATTERN, + ctrl->val ? 0x13 : 0x12); + default: + return -EINVAL; + } +} + +static const struct v4l2_ctrl_ops gc2053_ctrl_ops = { + .s_ctrl = gc2053_s_ctrl, +}; + +static void gc2053_fill_fmt(struct v4l2_mbus_framefmt *fmt) +{ + fmt->width = GC2053_WIDTH; + fmt->height = GC2053_HEIGHT; + fmt->code = MEDIA_BUS_FMT_SRGGB10_1X10; + fmt->field = V4L2_FIELD_NONE; + fmt->colorspace = V4L2_COLORSPACE_SRGB; +} + +static int gc2053_init_state(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state) +{ + struct v4l2_mbus_framefmt *fmt = + v4l2_subdev_state_get_format(sd_state, 0); + + if (fmt) + gc2053_fill_fmt(fmt); + return 0; +} + +static int gc2053_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_mbus_code_enum *code) +{ + if (code->pad || code->index) + return -EINVAL; + code->code = MEDIA_BUS_FMT_SRGGB10_1X10; + return 0; +} + +static int gc2053_enum_frame_size(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_size_enum *fse) +{ + if (fse->pad || fse->index || fse->code != MEDIA_BUS_FMT_SRGGB10_1X10) + return -EINVAL; + fse->min_width = GC2053_WIDTH; + fse->max_width = GC2053_WIDTH; + fse->min_height = GC2053_HEIGHT; + fse->max_height = GC2053_HEIGHT; + return 0; +} + +static int gc2053_enum_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_interval_enum *fie) +{ + if (fie->pad || fie->index || fie->code != MEDIA_BUS_FMT_SRGGB10_1X10 || + fie->width != GC2053_WIDTH || fie->height != GC2053_HEIGHT) + return -EINVAL; + fie->interval.numerator = 1; + fie->interval.denominator = 30; + return 0; +} + +static int gc2053_set_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_format *format) +{ + struct v4l2_mbus_framefmt *fmt; + + if (format->pad) + return -EINVAL; + fmt = v4l2_subdev_state_get_format(sd_state, format->pad); + if (!fmt) + return -EINVAL; + gc2053_fill_fmt(fmt); + format->format = *fmt; + return 0; +} + +static int gc2053_get_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_interval *fi) +{ + struct gc2053 *gc = to_gc2053(sd); + u64 num = (u64)GC2053_HTS_DEF * gc->vts; + u64 den = GC2053_PIXEL_RATE; + unsigned long div; + + if (fi->pad) + return -EINVAL; + div = gcd(num, den); + if (div) { + num = div64_ul(num, div); + den = div64_ul(den, div); + } + fi->interval.numerator = num; + fi->interval.denominator = den; + return 0; +} + +static int gc2053_set_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_interval *fi) +{ + struct gc2053 *gc = to_gc2053(sd); + + if (fi->pad) + return -EINVAL; + + if (fi->which == V4L2_SUBDEV_FORMAT_ACTIVE && gc->vblank && + fi->interval.numerator && fi->interval.denominator) { + u64 vts = div_u64(GC2053_PIXEL_RATE * fi->interval.numerator, + (u64)GC2053_HTS_DEF * fi->interval.denominator); + s32 vblank = (s32)vts - GC2053_HEIGHT; + + vblank = clamp(vblank, gc->vblank->minimum, + gc->vblank->maximum); + __v4l2_ctrl_s_ctrl(gc->vblank, vblank); + } + return gc2053_get_frame_interval(sd, sd_state, fi); +} + +static int gc2053_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad, + struct v4l2_mbus_config *config) +{ + if (pad) + return -EINVAL; + *config = to_gc2053(sd)->bus; + return 0; +} + +static int gc2053_s_stream(struct v4l2_subdev *sd, int enable) +{ + struct gc2053 *gc = to_gc2053(sd); + u8 lanes = gc->bus.bus.mipi_csi2.num_data_lanes; + u8 stream_val = lanes == 1 ? 0x90 : 0x91; + int ret = 0; + + mutex_lock(&gc->lock); + if (gc->streaming == !!enable) + goto unlock; + + if (!enable) { + gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + ret = gc2053_write_reg(gc, GC2053_REG_STREAM, 0x00); + gc->streaming = false; + __v4l2_ctrl_grab(gc->hflip, false); + __v4l2_ctrl_grab(gc->vflip, false); + pm_runtime_put(&gc->client->dev); + goto unlock; + } + + ret = pm_runtime_resume_and_get(&gc->client->dev); + if (ret < 0) + goto unlock; + + ret = gc2053_write_array(gc, gc2053_init_1080p_regs, + ARRAY_SIZE(gc2053_init_1080p_regs)); + if (ret) + goto err_rpm; + + gc->streaming = true; + ret = __v4l2_ctrl_handler_setup(&gc->ctrls); + if (ret) + goto err_stop; + + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + goto err_stop; + ret = gc2053_write_reg(gc, GC2053_REG_STREAM, stream_val); + if (ret) + goto err_stop; + + __v4l2_ctrl_grab(gc->hflip, true); + __v4l2_ctrl_grab(gc->vflip, true); + goto unlock; + +err_stop: + gc->streaming = false; + gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + gc2053_write_reg(gc, GC2053_REG_STREAM, 0x00); +err_rpm: + pm_runtime_put(&gc->client->dev); +unlock: + mutex_unlock(&gc->lock); + return ret; +} + +static const struct v4l2_subdev_core_ops gc2053_core_ops = { + .subscribe_event = v4l2_ctrl_subdev_subscribe_event, + .unsubscribe_event = v4l2_event_subdev_unsubscribe, +}; + +static const struct v4l2_subdev_video_ops gc2053_video_ops = { + .s_stream = gc2053_s_stream, +}; + +static const struct v4l2_subdev_pad_ops gc2053_pad_ops = { + .enum_mbus_code = gc2053_enum_mbus_code, + .enum_frame_size = gc2053_enum_frame_size, + .enum_frame_interval = gc2053_enum_frame_interval, + .get_fmt = v4l2_subdev_get_fmt, + .set_fmt = gc2053_set_fmt, + .get_frame_interval = gc2053_get_frame_interval, + .set_frame_interval = gc2053_set_frame_interval, + .get_mbus_config = gc2053_get_mbus_config, +}; + +static const struct v4l2_subdev_ops gc2053_subdev_ops = { + .core = &gc2053_core_ops, + .video = &gc2053_video_ops, + .pad = &gc2053_pad_ops, +}; + +static const struct v4l2_subdev_internal_ops gc2053_internal_ops = { + .init_state = gc2053_init_state, +}; + +static int gc2053_power_on(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct gc2053 *gc = to_gc2053(sd); + int ret; + + ret = regulator_bulk_enable(ARRAY_SIZE(gc2053_supply_names), + gc->supplies); + if (ret) + return ret; + + ret = clk_prepare_enable(gc->xvclk); + if (ret) { + regulator_bulk_disable(ARRAY_SIZE(gc2053_supply_names), + gc->supplies); + return ret; + } + + if (gc->pwdn_gpio) { + gpiod_set_value_cansleep(gc->pwdn_gpio, 0); + usleep_range(1000, 2000); + } + if (gc->reset_gpio) { + gpiod_set_value_cansleep(gc->reset_gpio, 1); + usleep_range(1000, 2000); + gpiod_set_value_cansleep(gc->reset_gpio, 0); + usleep_range(5000, 8000); + } + return 0; +} + +static int gc2053_power_off(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct gc2053 *gc = to_gc2053(sd); + + if (gc->reset_gpio) + gpiod_set_value_cansleep(gc->reset_gpio, 1); + if (gc->pwdn_gpio) + gpiod_set_value_cansleep(gc->pwdn_gpio, 1); + clk_disable_unprepare(gc->xvclk); + regulator_bulk_disable(ARRAY_SIZE(gc2053_supply_names), gc->supplies); + return 0; +} + +static int gc2053_identify(struct gc2053 *gc) +{ + u8 id_h = 0, id_l = 0; + u16 id; + int ret; + + ret = gc2053_write_reg(gc, GC2053_REG_PAGE, 0x00); + if (ret) + return ret; + ret = gc2053_read_reg(gc, GC2053_REG_CHIP_ID_H, &id_h); + if (ret) + return ret; + ret = gc2053_read_reg(gc, GC2053_REG_CHIP_ID_L, &id_l); + if (ret) + return ret; + + id = ((u16)id_h << 8) | id_l; + if (id != GC2053_CHIP_ID) { + dev_err(&gc->client->dev, + "unexpected chip ID %#04x (expected %#04x)\n", + id, GC2053_CHIP_ID); + return -ENODEV; + } + return 0; +} + +static int gc2053_parse_fwnode(struct gc2053 *gc) +{ + struct v4l2_fwnode_endpoint bus_cfg = { + .bus_type = V4L2_MBUS_CSI2_DPHY, + }; + struct fwnode_handle *ep; + unsigned long link_freq_bitmap; + int ret; + + ep = fwnode_graph_get_next_endpoint(dev_fwnode(&gc->client->dev), NULL); + if (!ep) + return -ENOTCONN; + + 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 != 2) { + ret = -EINVAL; + goto out_free; + } + + ret = v4l2_link_freq_to_bitmap(&gc->client->dev, + bus_cfg.link_frequencies, + bus_cfg.nr_of_link_frequencies, + gc2053_link_freqs, + ARRAY_SIZE(gc2053_link_freqs), + &link_freq_bitmap); + if (ret) + goto out_free; + + gc->bus.type = V4L2_MBUS_CSI2_DPHY; + gc->bus.bus.mipi_csi2 = bus_cfg.bus.mipi_csi2; + +out_free: + v4l2_fwnode_endpoint_free(&bus_cfg); + return ret; +} + +static int gc2053_init_controls(struct gc2053 *gc) +{ + struct v4l2_fwnode_device_properties props; + struct v4l2_ctrl *link_freq; + int ret; + + ret = v4l2_ctrl_handler_init(&gc->ctrls, 10); + if (ret) + return ret; + + gc->ctrls.lock = &gc->lock; + + link_freq = v4l2_ctrl_new_int_menu(&gc->ctrls, NULL, + V4L2_CID_LINK_FREQ, + ARRAY_SIZE(gc2053_link_freqs) - 1, + 0, gc2053_link_freqs); + if (link_freq) + link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + gc->pixel_rate = v4l2_ctrl_new_std(&gc->ctrls, NULL, + V4L2_CID_PIXEL_RATE, + GC2053_PIXEL_RATE, GC2053_PIXEL_RATE, + 1, GC2053_PIXEL_RATE); + if (gc->pixel_rate) + gc->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + gc->hblank = v4l2_ctrl_new_std(&gc->ctrls, NULL, V4L2_CID_HBLANK, + GC2053_HBLANK_DEF, GC2053_HBLANK_DEF, + 1, GC2053_HBLANK_DEF); + if (gc->hblank) + gc->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + gc->vblank = v4l2_ctrl_new_std(&gc->ctrls, &gc2053_ctrl_ops, + V4L2_CID_VBLANK, + GC2053_VTS_MIN - GC2053_HEIGHT, + GC2053_VTS_MAX - GC2053_HEIGHT, + 1, GC2053_VTS_DEF - GC2053_HEIGHT); + + gc->exposure = v4l2_ctrl_new_std(&gc->ctrls, &gc2053_ctrl_ops, + V4L2_CID_EXPOSURE, GC2053_EXP_MIN, + GC2053_VTS_DEF - GC2053_EXP_MARGIN, + 1, GC2053_VTS_DEF - GC2053_EXP_MARGIN); + + gc->again = v4l2_ctrl_new_std(&gc->ctrls, &gc2053_ctrl_ops, + V4L2_CID_ANALOGUE_GAIN, 0, + ARRAY_SIZE(gc2053_gain_lut) - 1, 1, 0); + + gc->hflip = v4l2_ctrl_new_std(&gc->ctrls, &gc2053_ctrl_ops, + V4L2_CID_HFLIP, 0, 1, 1, 0); + gc->vflip = v4l2_ctrl_new_std(&gc->ctrls, &gc2053_ctrl_ops, + V4L2_CID_VFLIP, 0, 1, 1, 0); + + v4l2_ctrl_new_std_menu_items(&gc->ctrls, &gc2053_ctrl_ops, + V4L2_CID_TEST_PATTERN, + ARRAY_SIZE(gc2053_test_pattern_menu) - 1, + 0, 0, gc2053_test_pattern_menu); + + ret = v4l2_fwnode_device_parse(&gc->client->dev, &props); + if (!ret) + v4l2_ctrl_new_fwnode_properties(&gc->ctrls, &gc2053_ctrl_ops, + &props); + + if (gc->ctrls.error) { + ret = gc->ctrls.error; + v4l2_ctrl_handler_free(&gc->ctrls); + return ret; + } + + gc->sd.ctrl_handler = &gc->ctrls; + return 0; +} + +static int gc2053_probe(struct i2c_client *client) +{ + struct device *dev = &client->dev; + struct gc2053 *gc; + unsigned int i; + int ret; + + gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL); + if (!gc) + return -ENOMEM; + + gc->client = client; + gc->vts = GC2053_VTS_DEF; + mutex_init(&gc->lock); + + ret = gc2053_parse_fwnode(gc); + if (ret) + return ret; + + for (i = 0; i < ARRAY_SIZE(gc2053_supply_names); i++) + gc->supplies[i].supply = gc2053_supply_names[i]; + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(gc2053_supply_names), + gc->supplies); + if (ret) + return dev_err_probe(dev, ret, "failed to get regulators\n"); + + gc->xvclk = devm_clk_get_optional(dev, "xvclk"); + if (IS_ERR(gc->xvclk)) + return PTR_ERR(gc->xvclk); + if (!gc->xvclk) { + gc->xvclk = devm_clk_get(dev, NULL); + if (IS_ERR(gc->xvclk)) + return PTR_ERR(gc->xvclk); + } + + ret = clk_set_rate(gc->xvclk, GC2053_XVCLK_FREQ); + if (ret) + return ret; + + gc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); + if (IS_ERR(gc->reset_gpio)) + return PTR_ERR(gc->reset_gpio); + + gc->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH); + if (IS_ERR(gc->pwdn_gpio)) + return PTR_ERR(gc->pwdn_gpio); + + v4l2_i2c_subdev_init(&gc->sd, client, &gc2053_subdev_ops); + gc->sd.internal_ops = &gc2053_internal_ops; + + ret = gc2053_power_on(dev); + if (ret) + return ret; + + ret = gc2053_identify(gc); + if (ret) + goto err_power_off; + + ret = gc2053_init_controls(gc); + if (ret) + goto err_power_off; + + gc->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; + gc->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; + gc->pad.flags = MEDIA_PAD_FL_SOURCE; + ret = media_entity_pads_init(&gc->sd.entity, 1, &gc->pad); + if (ret) + goto err_ctrls; + + gc->sd.state_lock = &gc->lock; + ret = v4l2_subdev_init_finalize(&gc->sd); + if (ret) + goto err_entity; + + ret = v4l2_async_register_subdev_sensor(&gc->sd); + if (ret) + goto err_subdev; + + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + pm_runtime_idle(dev); + + return 0; + +err_subdev: + v4l2_subdev_cleanup(&gc->sd); +err_entity: + media_entity_cleanup(&gc->sd.entity); +err_ctrls: + v4l2_ctrl_handler_free(&gc->ctrls); +err_power_off: + gc2053_power_off(dev); + return ret; +} + +static void gc2053_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct gc2053 *gc = to_gc2053(sd); + + v4l2_async_unregister_subdev(&gc->sd); + v4l2_subdev_cleanup(&gc->sd); + media_entity_cleanup(&gc->sd.entity); + v4l2_ctrl_handler_free(&gc->ctrls); + + pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) + gc2053_power_off(&client->dev); + pm_runtime_set_suspended(&client->dev); +} + +static const struct dev_pm_ops gc2053_pm_ops = { + SET_RUNTIME_PM_OPS(gc2053_power_off, gc2053_power_on, NULL) +}; + +static const struct i2c_device_id gc2053_id[] = { + { "gc2053", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, gc2053_id); + +static const struct of_device_id gc2053_of_match[] = { + { .compatible = "galaxycore,gc2053" }, + { } +}; +MODULE_DEVICE_TABLE(of, gc2053_of_match); + +static struct i2c_driver gc2053_driver = { + .driver = { + .name = "gc2053", + .of_match_table = gc2053_of_match, + .pm = &gc2053_pm_ops, + }, + .probe = gc2053_probe, + .remove = gc2053_remove, + .id_table = gc2053_id, +}; +module_i2c_driver(gc2053_driver); + +MODULE_DESCRIPTION("GalaxyCore GC2053 MIPI CSI-2 sensor driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); From 46742c786cce4e454b54f99d4907592384800d50 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:25:01 -0800 Subject: [PATCH 62/78] dt-bindings: media: i2c: Add SmartSens T23 camera sensors Describe the SC1A4T, SC2336 and SC2337P sensor variants and their CSI-2 endpoint and board resources. Signed-off-by: Difan Zhang --- .../bindings/media/i2c/smartsens,sc2336.yaml | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/i2c/smartsens,sc2336.yaml diff --git a/Documentation/devicetree/bindings/media/i2c/smartsens,sc2336.yaml b/Documentation/devicetree/bindings/media/i2c/smartsens,sc2336.yaml new file mode 100644 index 0000000000..03d6f54cf9 --- /dev/null +++ b/Documentation/devicetree/bindings/media/i2c/smartsens,sc2336.yaml @@ -0,0 +1,146 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/i2c/smartsens,sc2336.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: SmartSens SC1A4T, SC2336, SC2336P and SC2337P 2MP MIPI CSI-2 Sensors + +maintainers: + - Paul Cercueil + +properties: + compatible: + enum: + - smartsens,sc1a4t + - smartsens,sc2336 + - smartsens,sc2336p + - smartsens,sc2337p + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: xvclk + + avdd-supply: + description: Analog 2.8V power supply regulator. + + dovdd-supply: + description: Digital I/O 1.8V power supply regulator. + + dvdd-supply: + description: Digital core 1.2V power supply regulator. + + reset-gpios: + maxItems: 1 + + pwdn-gpios: + maxItems: 1 + + smartsens,frame-sync-slave: + type: boolean + description: + Configure the sensor in external frame-synchronization slave mode for + dual-sensor lockstep capture. + + smartsens,secondary-i2c-addr: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 0x08 + maximum: 0x77 + description: + Remap a paired secondary sensor on the shared reset line to this 7-bit + I2C address during primary sensor probe before releasing pwdn-gpios. + + port: + $ref: /schemas/graph.yaml#/$defs/port-base + additionalProperties: false + + properties: + endpoint: + $ref: /schemas/media/video-interfaces.yaml# + unevaluatedProperties: false + + properties: + data-lanes: + minItems: 1 + maxItems: 2 + items: + - const: 1 + - const: 2 + + link-frequencies: + maxItems: 1 + items: + enum: + - 198000000 + - 360000000 + + required: + - data-lanes + - link-frequencies + + required: + - endpoint + +allOf: + - if: + properties: + compatible: + contains: + const: smartsens,sc1a4t + then: + properties: + port: + properties: + endpoint: + properties: + data-lanes: + maxItems: 1 + link-frequencies: + items: + const: 360000000 + else: + properties: + port: + properties: + endpoint: + properties: + data-lanes: + minItems: 2 + link-frequencies: + items: + const: 198000000 + +required: + - compatible + - reg + - clocks + - port + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + sensor@30 { + compatible = "smartsens,sc2336"; + reg = <0x30>; + clocks = <&sensor_clk>; + clock-names = "xvclk"; + + port { + sc2336_out: endpoint { + data-lanes = <1 2>; + link-frequencies = /bits/ 64 <198000000>; + remote-endpoint = <&csi_in>; + }; + }; + }; + }; From 05e3c5bce241e3d91e7c84e3d5016729c9939e42 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 06:09:37 -0800 Subject: [PATCH 63/78] media: i2c: Add SmartSens SC1A4T, SC2336 and SC2337P sensors Provide the T23 SmartSens sensor variants with common register, power and V4L2 control handling. Support active subdevice state, live VBLANK and exposure limits, and stream-time Bayer flip locking. Signed-off-by: Difan Zhang --- drivers/media/i2c/Kconfig | 1 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/smartsens/Kconfig | 47 + drivers/media/i2c/smartsens/Makefile | 7 + drivers/media/i2c/smartsens/sensor_sc1a4t.c | 493 +++++++++ drivers/media/i2c/smartsens/sensor_sc2336p.c | 476 +++++++++ drivers/media/i2c/smartsens/sensor_sc2337p.c | 473 +++++++++ .../media/i2c/smartsens/smartsens-common.c | 981 ++++++++++++++++++ .../media/i2c/smartsens/smartsens-common.h | 113 ++ 9 files changed, 2592 insertions(+) create mode 100644 drivers/media/i2c/smartsens/Kconfig create mode 100644 drivers/media/i2c/smartsens/Makefile create mode 100644 drivers/media/i2c/smartsens/sensor_sc1a4t.c create mode 100644 drivers/media/i2c/smartsens/sensor_sc2336p.c create mode 100644 drivers/media/i2c/smartsens/sensor_sc2337p.c create mode 100644 drivers/media/i2c/smartsens/smartsens-common.c create mode 100644 drivers/media/i2c/smartsens/smartsens-common.h diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index dfcb4e1c3d..fafd969879 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -710,6 +710,7 @@ config VIDEO_VGXY61 source "drivers/media/i2c/ccs/Kconfig" source "drivers/media/i2c/et8ek8/Kconfig" +source "drivers/media/i2c/smartsens/Kconfig" endif diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index 7d13d59e05..6b60cc8564 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -128,6 +128,7 @@ obj-$(CONFIG_VIDEO_SAA711X) += saa7115.o obj-$(CONFIG_VIDEO_SAA7127) += saa7127.o obj-$(CONFIG_VIDEO_SAA717X) += saa717x.o obj-$(CONFIG_VIDEO_SAA7185) += saa7185.o +obj-y += smartsens/ obj-$(CONFIG_VIDEO_SONY_BTF_MPX) += sony-btf-mpx.o obj-$(CONFIG_VIDEO_ST_MIPID02) += st-mipid02.o obj-$(CONFIG_VIDEO_TC358743) += tc358743.o diff --git a/drivers/media/i2c/smartsens/Kconfig b/drivers/media/i2c/smartsens/Kconfig new file mode 100644 index 0000000000..ea421cff14 --- /dev/null +++ b/drivers/media/i2c/smartsens/Kconfig @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +config VIDEO_SMARTSENS_COMMON + tristate + +config VIDEO_SC1A4T + tristate "SmartSens SC1A4T sensor support" + depends on I2C && VIDEO_DEV && OF + select MEDIA_CONTROLLER + select VIDEO_V4L2_SUBDEV_API + select V4L2_FWNODE + select VIDEO_SMARTSENS_COMMON + help + This is a Video4Linux2 sensor driver for the SmartSens SC1A4T + CMOS image sensor in primary or frame-sync slave configuration. + + To compile this driver as a module, choose M here: the module + will be called sensor_sc1a4t. + +config VIDEO_SC2336P + tristate "SmartSens SC2336 / SC2336P sensor support" + depends on I2C && VIDEO_DEV && OF + select MEDIA_CONTROLLER + select VIDEO_V4L2_SUBDEV_API + select V4L2_FWNODE + select VIDEO_SMARTSENS_COMMON + help + This is a Video4Linux2 sensor driver for the SmartSens SC2336 + and SC2336P CMOS image sensors. + + To compile this driver as a module, choose M here: the module + will be called sensor_sc2336p. + +config VIDEO_SC2337P + tristate "SmartSens SC2337P sensor support" + depends on I2C && VIDEO_DEV && OF + select MEDIA_CONTROLLER + select VIDEO_V4L2_SUBDEV_API + select V4L2_FWNODE + select VIDEO_SMARTSENS_COMMON + help + This is a Video4Linux2 sensor driver for the SmartSens SC2337P + CMOS image sensor. + + To compile this driver as a module, choose M here: the module + will be called sensor_sc2337p. diff --git a/drivers/media/i2c/smartsens/Makefile b/drivers/media/i2c/smartsens/Makefile new file mode 100644 index 0000000000..c85e27741a --- /dev/null +++ b/drivers/media/i2c/smartsens/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +obj-$(CONFIG_VIDEO_SMARTSENS_COMMON) += smartsens-common.o +obj-$(CONFIG_VIDEO_SC1A4T) += sensor_sc1a4t.o +obj-$(CONFIG_VIDEO_SC2336P) += sensor_sc2336p.o +obj-$(CONFIG_VIDEO_SC2337P) += sensor_sc2337p.o diff --git a/drivers/media/i2c/smartsens/sensor_sc1a4t.c b/drivers/media/i2c/smartsens/sensor_sc1a4t.c new file mode 100644 index 0000000000..52d61bb5b1 --- /dev/null +++ b/drivers/media/i2c/smartsens/sensor_sc1a4t.c @@ -0,0 +1,493 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Ingenic T23N sc1a4t sensor driver + */ + +#include +#include +#include + +#include "smartsens-common.h" + +static int fsync_mode = 3; +module_param(fsync_mode, int, 0444); +MODULE_PARM_DESC(fsync_mode, "Frame synchronization mode"); + +static int shvflip; +module_param(shvflip, int, 0644); +MODULE_PARM_DESC(shvflip, "Horizontal and vertical flip (0: normal, 1: flipped)"); + +static const s64 sc1a4t_link_freqs[] = { 360000000 }; + +static const struct smartsens_again_lut sc1a4t_again_lut[] = { + { 0x00000080, 0x00000000 }, + { 0x00000084, 0x00000b46 }, + { 0x00000088, 0x00001690 }, + { 0x0000008c, 0x0000212e }, + { 0x00000090, 0x00002b80 }, + { 0x00000094, 0x0000358a }, + { 0x00000098, 0x00003f9f }, + { 0x0000009c, 0x00004923 }, + { 0x000000a0, 0x00005269 }, + { 0x000000a4, 0x00005b75 }, + { 0x000000a8, 0x00006492 }, + { 0x000000ac, 0x00006d30 }, + { 0x000000b0, 0x0000759c }, + { 0x000000b4, 0x00007dd8 }, + { 0x000000b8, 0x00008628 }, + { 0x000000bc, 0x00008e09 }, + { 0x000000c0, 0x000095bf }, + { 0x000000c4, 0x00009d4d }, + { 0x000000c8, 0x0000a4f1 }, + { 0x000000cc, 0x0000ac32 }, + { 0x000000d0, 0x0000b34f }, + { 0x000000d4, 0x0000ba49 }, + { 0x000000d8, 0x0000c15b }, + { 0x000000dc, 0x0000c814 }, + { 0x000000e0, 0x0000ceae }, + { 0x000000e4, 0x0000d52a }, + { 0x000000e8, 0x0000dbbd }, + { 0x000000ec, 0x0000e200 }, + { 0x000000f0, 0x0000e829 }, + { 0x000000f4, 0x0000ee37 }, + { 0x000000f8, 0x0000f45d }, + { 0x000000fc, 0x0000fa3a }, + { 0x00000880, 0x0000ffff }, + { 0x00000884, 0x00010b45 }, + { 0x00000888, 0x0001168f }, + { 0x0000088c, 0x0001212d }, + { 0x00000890, 0x00012b7f }, + { 0x00000894, 0x00013589 }, + { 0x00000898, 0x00013f9e }, + { 0x0000089c, 0x00014922 }, + { 0x000008a0, 0x00015268 }, + { 0x000008a4, 0x00015b74 }, + { 0x000008a8, 0x00016491 }, + { 0x000008ac, 0x00016d2f }, + { 0x000008b0, 0x0001759b }, + { 0x000008b4, 0x00017dd7 }, + { 0x000008b8, 0x00018627 }, + { 0x000008bc, 0x00018e08 }, + { 0x000008c0, 0x000195be }, + { 0x000008c4, 0x00019d4c }, + { 0x000008c8, 0x0001a4f0 }, + { 0x000008cc, 0x0001ac31 }, + { 0x000008d0, 0x0001b34e }, + { 0x000008d4, 0x0001ba48 }, + { 0x000008d8, 0x0001c15a }, + { 0x000008dc, 0x0001c813 }, + { 0x000008e0, 0x0001cead }, + { 0x000008e4, 0x0001d529 }, + { 0x000008e8, 0x0001dbbc }, + { 0x000008ec, 0x0001e1ff }, + { 0x000008f0, 0x0001e828 }, + { 0x000008f4, 0x0001ee36 }, + { 0x000008f8, 0x0001f45c }, + { 0x000008fc, 0x0001fa39 }, + { 0x00000980, 0x0001fffe }, + { 0x00000984, 0x00020b44 }, + { 0x00000988, 0x0002168e }, + { 0x0000098c, 0x0002212c }, + { 0x00000990, 0x00022b7e }, + { 0x00000994, 0x00023588 }, + { 0x00000998, 0x00023f9d }, + { 0x0000099c, 0x00024921 }, + { 0x000009a0, 0x00025267 }, + { 0x000009a4, 0x00025b73 }, + { 0x000009a8, 0x00026490 }, + { 0x000009ac, 0x00026d2e }, + { 0x000009b0, 0x0002759a }, + { 0x000009b4, 0x00027dd6 }, + { 0x000009b8, 0x00028626 }, + { 0x000009bc, 0x00028e07 }, + { 0x000009c0, 0x000295bd }, + { 0x000009c4, 0x00029d4b }, + { 0x000009c8, 0x0002a4ef }, + { 0x000009cc, 0x0002ac30 }, + { 0x000009d0, 0x0002b34d }, + { 0x000009d4, 0x0002ba47 }, + { 0x000009d8, 0x0002c159 }, + { 0x000009dc, 0x0002c812 }, + { 0x000009e0, 0x0002ceac }, + { 0x000009e4, 0x0002d528 }, + { 0x000009e8, 0x0002dbbb }, + { 0x000009ec, 0x0002e1fe }, + { 0x000009f0, 0x0002e827 }, + { 0x000009f4, 0x0002ee35 }, + { 0x000009f8, 0x0002f45b }, + { 0x000009fc, 0x0002fa38 }, + { 0x00000b80, 0x0002fffd }, + { 0x00000b84, 0x00030b43 }, + { 0x00000b88, 0x0003168d }, + { 0x00000b8c, 0x0003212b }, + { 0x00000b90, 0x00032b7d }, + { 0x00000b94, 0x00033587 }, + { 0x00000b98, 0x00033f9c }, + { 0x00000b9c, 0x00034920 }, + { 0x00000ba0, 0x00035266 }, + { 0x00000ba4, 0x00035b72 }, + { 0x00000ba8, 0x0003648f }, + { 0x00000bac, 0x00036d2d }, + { 0x00000bb0, 0x00037599 }, + { 0x00000bb4, 0x00037dd5 }, + { 0x00000bb8, 0x00038625 }, + { 0x00000bbc, 0x00038e06 }, + { 0x00000bc0, 0x000395bc }, + { 0x00000bc4, 0x00039d4a }, + { 0x00000bc8, 0x0003a4ee }, + { 0x00000bcc, 0x0003ac2f }, + { 0x00000bd0, 0x0003b34c }, + { 0x00000bd4, 0x0003ba46 }, + { 0x00000bd8, 0x0003c158 }, + { 0x00000bdc, 0x0003c811 }, + { 0x00000be0, 0x0003ceab }, + { 0x00000be4, 0x0003d527 }, + { 0x00000be8, 0x0003dbba }, + { 0x00000bec, 0x0003e1fd }, + { 0x00000bf0, 0x0003e826 }, + { 0x00000bf4, 0x0003ee34 }, + { 0x00000bf8, 0x0003f45a }, + { 0x00000bfc, 0x0003fa37 }, + { 0x00000f80, 0x0003fffc }, + { 0x00000f84, 0x00040b42 }, + { 0x00000f8c, 0x0004212a }, + { 0x00000f90, 0x00042b7c }, + { 0x00000f94, 0x00043586 }, + { 0x00000f98, 0x00043f9b }, + { 0x00000f9c, 0x0004491f }, + { 0x00000fa0, 0x00045265 }, + { 0x00000fa4, 0x00045b71 }, + { 0x00000fa8, 0x0004648e }, + { 0x00000fac, 0x00046d2c }, + { 0x00000fb0, 0x00047598 }, + { 0x00000fb4, 0x00047dd4 }, + { 0x00000fb8, 0x00048624 }, + { 0x00000fbc, 0x00048e05 }, + { 0x00000fc0, 0x000495bb }, + { 0x00000fc4, 0x00049d49 }, + { 0x00000fc8, 0x0004a4ed }, + { 0x00000fcc, 0x0004ac2e }, + { 0x00000fd0, 0x0004b34b }, + { 0x00000fd4, 0x0004ba45 }, + { 0x00000fd8, 0x0004c157 }, + { 0x00000fdc, 0x0004c810 }, + { 0x00000fe0, 0x0004ceaa }, + { 0x00000fe4, 0x0004d526 }, + { 0x00000fe8, 0x0004dbb9 }, + { 0x00000fec, 0x0004e1fc }, + { 0x00000ff0, 0x0004e825 }, + { 0x00000ff4, 0x0004ee33 }, + { 0x00000ff8, 0x0004f459 }, + { 0x00000ffc, 0x0004fa36 }, + { 0x00001f80, 0x0004fffb }, + { 0x00001f84, 0x00050b41 }, + { 0x00001f88, 0x0005168b }, + { 0x00001f8c, 0x00052129 }, + { 0x00001f90, 0x00052b7b }, + { 0x00001f94, 0x00053585 }, + { 0x00001f98, 0x00053f9a }, + { 0x00001f9c, 0x0005491e }, + { 0x00001fa0, 0x00055264 }, + { 0x00001fa4, 0x00055b70 }, + { 0x00001fa8, 0x0005648d }, + { 0x00001fac, 0x00056d2b }, + { 0x00001fb0, 0x00057597 }, + { 0x00001fb4, 0x00057dd3 }, + { 0x00001fb8, 0x00058623 }, + { 0x00001fbc, 0x00058e04 }, + { 0x00001fc0, 0x000595ba }, + { 0x00001fc4, 0x00059d48 }, + { 0x00001fc8, 0x0005a4ec }, + { 0x00001fcc, 0x0005ac2d }, + { 0x00001fd0, 0x0005b34a }, + { 0x00001fd4, 0x0005ba44 }, + { 0x00001fd8, 0x0005c156 }, + { 0x00001fdc, 0x0005c80f }, + { 0x00001fe0, 0x0005cea9 }, + { 0x00001fe4, 0x0005d525 }, + { 0x00001fe8, 0x0005dbb8 }, + { 0x00001fec, 0x0005e1fb }, + { 0x00001ff0, 0x0005e824 }, + { 0x00001ff4, 0x0005ee32 }, + { 0x00001ff8, 0x0005f458 }, + { 0x00001ffc, 0x0005fa35 }, +}; + +static const struct regval_list sc1a4t_stream_on_mipi[] = { + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc1a4t_stream_off_mipi[] = { + { 0x0100, 0x00 }, + { 0xffff, 0x00 }, +}; +static const struct regval_list sc1a4t_init_primary_head[] = { + { 0x0100, 0x00 }, + { 0x36e9, 0x80 }, + { 0x37f9, 0x80 }, + { 0x300a, 0x24 }, + { 0x301f, 0x32 }, + { 0x3032, 0xa0 }, + { 0x3106, 0x05 }, + { 0x320c, 0x06 }, + { 0x320d, 0x40 }, + { 0x320e, 0x0b }, + { 0x320f, 0xb8 }, + { 0x322e, 0x0b }, + { 0x322f, 0xb4 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc1a4t_init_slave_head[] = { + { 0x0100, 0x00 }, + { 0x36e9, 0x80 }, + { 0x37f9, 0x80 }, + { 0x300a, 0x22 }, + { 0x301f, 0x31 }, + { 0x3106, 0x05 }, + { 0x320c, 0x06 }, + { 0x320d, 0x40 }, + { 0x320e, 0x0b }, + { 0x320f, 0xb8 }, + { 0x3222, 0x02 }, + { 0x3224, 0x92 }, + { 0x3225, 0x10 }, + { 0x322e, 0x0b }, + { 0x322f, 0xb4 }, + { 0x3230, 0x00 }, + { 0x3231, 0x04 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc1a4t_init_body_720p[] = { + { 0x3301, 0x0b }, + { 0x3303, 0x10 }, + { 0x3306, 0x50 }, + { 0x3308, 0x0a }, + { 0x330a, 0x00 }, + { 0x330b, 0xda }, + { 0x330e, 0x0a }, + { 0x331e, 0x61 }, + { 0x331f, 0xa1 }, + { 0x3320, 0x04 }, + { 0x3327, 0x08 }, + { 0x3329, 0x09 }, + { 0x3364, 0x1f }, + { 0x3390, 0x09 }, + { 0x3391, 0x0f }, + { 0x3392, 0x1f }, + { 0x3393, 0x30 }, + { 0x3394, 0xff }, + { 0x3395, 0xff }, + { 0x33ad, 0x10 }, + { 0x33b3, 0x40 }, + { 0x33f9, 0x50 }, + { 0x33fb, 0x80 }, + { 0x33fc, 0x09 }, + { 0x33fd, 0x0f }, + { 0x349f, 0x03 }, + { 0x34a6, 0x09 }, + { 0x34a7, 0x0f }, + { 0x34a8, 0x40 }, + { 0x34a9, 0x30 }, + { 0x34aa, 0x00 }, + { 0x34ab, 0xe8 }, + { 0x34ac, 0x01 }, + { 0x34ad, 0x0c }, + { 0x3630, 0xe2 }, + { 0x3632, 0x76 }, + { 0x3633, 0x33 }, + { 0x3639, 0xf4 }, + { 0x3641, 0x28 }, + { 0x3670, 0x09 }, + { 0x3674, 0xe2 }, + { 0x3675, 0xea }, + { 0x3676, 0xea }, + { 0x367c, 0x09 }, + { 0x367d, 0x0f }, + { 0x3690, 0x22 }, + { 0x3691, 0x22 }, + { 0x3692, 0x32 }, + { 0x3698, 0x88 }, + { 0x3699, 0x8f }, + { 0x369a, 0xa0 }, + { 0x369b, 0xd1 }, + { 0x369c, 0x09 }, + { 0x369d, 0x0f }, + { 0x36a2, 0x09 }, + { 0x36a3, 0x0b }, + { 0x36a4, 0x0f }, + { 0x36d0, 0x01 }, + { 0x36ea, 0x06 }, + { 0x36eb, 0x05 }, + { 0x36ec, 0x05 }, + { 0x36ed, 0x28 }, + { 0x370f, 0x01 }, + { 0x3722, 0x41 }, + { 0x3724, 0x41 }, + { 0x3725, 0xc1 }, + { 0x3727, 0x14 }, + { 0x3728, 0x00 }, + { 0x37b0, 0x21 }, + { 0x37b1, 0x21 }, + { 0x37b2, 0x37 }, + { 0x37b3, 0x09 }, + { 0x37b4, 0x0f }, + { 0x37fa, 0x0c }, + { 0x37fb, 0x31 }, + { 0x37fc, 0x01 }, + { 0x37fd, 0x17 }, + { 0x3903, 0x40 }, + { 0x3904, 0x04 }, + { 0x3905, 0x8d }, + { 0x3907, 0x00 }, + { 0x3908, 0x41 }, + { 0x391f, 0x41 }, + { 0x3933, 0x80 }, + { 0x3934, 0x0a }, + { 0x3935, 0x01 }, + { 0x3936, 0x55 }, + { 0x3937, 0x63 }, + { 0x3938, 0x63 }, + { 0x3939, 0x0f }, + { 0x393a, 0xef }, + { 0x393b, 0x0f }, + { 0x393c, 0xcd }, + { 0x3e00, 0x00 }, + { 0x3e01, 0xbb }, + { 0x3e02, 0x20 }, + { 0x440e, 0x02 }, + { 0x4509, 0x25 }, + { 0x450d, 0x28 }, + { 0x4819, 0x09 }, + { 0x481b, 0x05 }, + { 0x481d, 0x13 }, + { 0x481f, 0x04 }, + { 0x4821, 0x0a }, + { 0x4823, 0x05 }, + { 0x4825, 0x04 }, + { 0x4827, 0x05 }, + { 0x4829, 0x08 }, + { 0x5780, 0x66 }, + { 0x578d, 0x40 }, + { 0x36e9, 0x24 }, + { 0x37f9, 0x20 }, + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list * const sc1a4t_primary_seqs[] = { + sc1a4t_init_primary_head, + sc1a4t_init_body_720p, +}; + +static const struct regval_list * const sc1a4t_slave_seqs[] = { + sc1a4t_init_slave_head, + sc1a4t_init_body_720p, +}; + +/* Secondary sensor frame-sync half-frame offset. */ +#define SC1A4TS1_SYNC_ROWS 1629 + +static int sc1a4t_set_vblank(struct v4l2_subdev *sd, s32 val) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + u32 vts = sensor->height + val; + s32 delay; + int ret; + + ret = smartsens_set_vblank(sd, val); + if (ret || !sensor->frame_sync_slave) + return ret; + + delay = ((s32)(vts / 2) - SC1A4TS1_SYNC_ROWS) % (s32)vts; + if (delay < 0) + delay += vts; + ret = smartsens_write_reg(sd, 0x3230, delay >> 8); + if (ret) + return ret; + return smartsens_write_reg(sd, 0x3231, delay & 0xff); +} + +static int sc1a4t_fsync(struct v4l2_subdev *sd, bool slave) +{ + int ret; + + if (fsync_mode != 3) + return 0; + if (slave) + return smartsens_write_reg(sd, 0x3224, 0x97); + ret = smartsens_write_reg(sd, 0x322e, 6); + if (ret) + return ret; + return smartsens_write_reg(sd, 0x322f, 0x60); +} + +static const struct smartsens_desc sc1a4t_desc = { + .name = "sc1a4t", + .chip_id = 0xda4d, + .expected_lanes = 1, + .link_freqs = sc1a4t_link_freqs, + .num_link_freqs = ARRAY_SIZE(sc1a4t_link_freqs), + .width = 1280, + .height = 720, + .fps = 15, + .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, + .colorspace = V4L2_COLORSPACE_RAW, + .exposure_min = 2, + .exposure_max = 2994, + .exposure_def = 1000, + .vblank_min = 280, + .vblank_max = 8191 - 720, + .vblank_def = 2280, + .hblank = 320, + .pixel_rate = 72000000, + .vts_def = 3000, + .again_lut = sc1a4t_again_lut, + .again_lut_size = ARRAY_SIZE(sc1a4t_again_lut), + .max_again = 0x5fa35, + .sc1a4t_again_layout = true, + .stream_on = sc1a4t_stream_on_mipi, + .stream_off = sc1a4t_stream_off_mipi, + .primary_seqs = sc1a4t_primary_seqs, + .num_primary_seqs = ARRAY_SIZE(sc1a4t_primary_seqs), + .slave_seqs = sc1a4t_slave_seqs, + .num_slave_seqs = ARRAY_SIZE(sc1a4t_slave_seqs), + .set_vblank = sc1a4t_set_vblank, + .fsync = sc1a4t_fsync, +}; + +static int sc1a4t_probe(struct i2c_client *client) +{ + return smartsens_probe(client, &sc1a4t_desc, !!shvflip); +} + +static const struct i2c_device_id sc1a4t_id[] = { + { "sc1a4t", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, sc1a4t_id); + +static const struct of_device_id sc1a4t_of_match[] = { + { .compatible = "smartsens,sc1a4t" }, + { } +}; +MODULE_DEVICE_TABLE(of, sc1a4t_of_match); + +static struct i2c_driver sc1a4t_driver = { + .driver = { + .name = "sc1a4t", + .of_match_table = sc1a4t_of_match, + .pm = pm_ptr(&smartsens_pm_ops), + }, + .probe = sc1a4t_probe, + .remove = smartsens_remove, + .id_table = sc1a4t_id, +}; +module_i2c_driver(sc1a4t_driver); + +MODULE_DESCRIPTION("SmartSens SC1A4T MIPI CSI-2 sensor driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/i2c/smartsens/sensor_sc2336p.c b/drivers/media/i2c/smartsens/sensor_sc2336p.c new file mode 100644 index 0000000000..e7b1d099b4 --- /dev/null +++ b/drivers/media/i2c/smartsens/sensor_sc2336p.c @@ -0,0 +1,476 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Ingenic T23N sc2336p sensor driver + */ + +#include +#include +#include + +#include "smartsens-common.h" + +static int fsync_mode = 3; +module_param(fsync_mode, int, 0444); +MODULE_PARM_DESC(fsync_mode, "Frame synchronization mode"); + +static const s64 sc2336p_link_freqs[] = { 198000000 }; + +static const struct smartsens_again_lut sc2336p_again_lut[] = { + { 0x00000080, 0x00000000 }, + { 0x00000084, 0x00000b46 }, + { 0x00000088, 0x00001690 }, + { 0x0000008c, 0x0000212e }, + { 0x00000090, 0x00002b80 }, + { 0x00000094, 0x0000358a }, + { 0x00000098, 0x00003f9f }, + { 0x0000009c, 0x00004923 }, + { 0x000000a0, 0x00005269 }, + { 0x000000a4, 0x00005b76 }, + { 0x000000a8, 0x00006492 }, + { 0x000000ac, 0x00006d31 }, + { 0x000000b0, 0x0000759d }, + { 0x000000b4, 0x00007dd9 }, + { 0x000000b8, 0x00008629 }, + { 0x000000bc, 0x00008e09 }, + { 0x000000c0, 0x000095c0 }, + { 0x000000c4, 0x00009d4e }, + { 0x000000c8, 0x0000a4f2 }, + { 0x000000cc, 0x0000ac32 }, + { 0x000000d0, 0x0000b350 }, + { 0x000000d4, 0x0000ba4a }, + { 0x000000d8, 0x0000c15c }, + { 0x000000dc, 0x0000c814 }, + { 0x000000e0, 0x0000ceae }, + { 0x000000e4, 0x0000d52b }, + { 0x000000e8, 0x0000dbbe }, + { 0x000000ec, 0x0000e201 }, + { 0x000000f0, 0x0000e829 }, + { 0x000000f4, 0x0000ee38 }, + { 0x000000f8, 0x0000f45e }, + { 0x000000fc, 0x0000fa3b }, + { 0x00000880, 0x00010000 }, + { 0x00000884, 0x00010b46 }, + { 0x00000888, 0x00011690 }, + { 0x0000088c, 0x0001212e }, + { 0x00000890, 0x00012b80 }, + { 0x00000894, 0x0001358a }, + { 0x00000898, 0x00013f9f }, + { 0x0000089c, 0x00014923 }, + { 0x000008a0, 0x00015269 }, + { 0x000008a4, 0x00015b76 }, + { 0x000008a8, 0x00016492 }, + { 0x000008ac, 0x00016d31 }, + { 0x000008b0, 0x0001759d }, + { 0x000008b4, 0x00017dd9 }, + { 0x000008b8, 0x00018629 }, + { 0x000008bc, 0x00018e09 }, + { 0x000008c0, 0x000195c0 }, + { 0x000008c4, 0x00019d4e }, + { 0x000008c8, 0x0001a4f2 }, + { 0x000008cc, 0x0001ac32 }, + { 0x000008d0, 0x0001b350 }, + { 0x000008d4, 0x0001ba4a }, + { 0x000008d8, 0x0001c15c }, + { 0x000008dc, 0x0001c814 }, + { 0x000008e0, 0x0001ceae }, + { 0x000008e4, 0x0001d52b }, + { 0x000008e8, 0x0001dbbe }, + { 0x000008ec, 0x0001e201 }, + { 0x000008f0, 0x0001e829 }, + { 0x000008f4, 0x0001ee38 }, + { 0x000008f8, 0x0001f45e }, + { 0x000008fc, 0x0001fa3b }, + { 0x00000980, 0x00020000 }, + { 0x00000984, 0x00020b46 }, + { 0x00000988, 0x00021690 }, + { 0x0000098c, 0x0002212e }, + { 0x00000990, 0x00022b80 }, + { 0x00000994, 0x0002358a }, + { 0x00000998, 0x00023f9f }, + { 0x0000099c, 0x00024923 }, + { 0x000009a0, 0x00025269 }, + { 0x000009a4, 0x00025b76 }, + { 0x000009a8, 0x00026492 }, + { 0x000009ac, 0x00026d31 }, + { 0x000009b0, 0x0002759d }, + { 0x000009b4, 0x00027dd9 }, + { 0x000009b8, 0x00028629 }, + { 0x000009bc, 0x00028e09 }, + { 0x000009c0, 0x000295c0 }, + { 0x000009c4, 0x00029d4e }, + { 0x000009c8, 0x0002a4f2 }, + { 0x000009cc, 0x0002ac32 }, + { 0x000009d0, 0x0002b350 }, + { 0x000009d4, 0x0002ba4a }, + { 0x000009d8, 0x0002c15c }, + { 0x000009dc, 0x0002c814 }, + { 0x000009e0, 0x0002ceae }, + { 0x000009e4, 0x0002d52b }, + { 0x000009e8, 0x0002dbbe }, + { 0x000009ec, 0x0002e201 }, + { 0x000009f0, 0x0002e829 }, + { 0x000009f4, 0x0002ee38 }, + { 0x000009f8, 0x0002f45e }, + { 0x000009fc, 0x0002fa3b }, + { 0x00000b80, 0x00030000 }, + { 0x00000b84, 0x00030b46 }, + { 0x00000b88, 0x00031690 }, + { 0x00000b8c, 0x0003212e }, + { 0x00000b90, 0x00032b80 }, + { 0x00000b94, 0x0003358a }, + { 0x00000b98, 0x00033f9f }, + { 0x00000b9c, 0x00034923 }, + { 0x00000ba0, 0x00035269 }, + { 0x00000ba4, 0x00035b76 }, + { 0x00000ba8, 0x00036492 }, + { 0x00000bac, 0x00036d31 }, + { 0x00000bb0, 0x0003759d }, + { 0x00000bb4, 0x00037dd9 }, + { 0x00000bb8, 0x00038629 }, + { 0x00000bbc, 0x00038e09 }, + { 0x00000bc0, 0x000395c0 }, + { 0x00000bc4, 0x00039d4e }, + { 0x00000bc8, 0x0003a4f2 }, + { 0x00000bcc, 0x0003ac32 }, + { 0x00000bd0, 0x0003b350 }, + { 0x00000bd4, 0x0003ba4a }, + { 0x00000bd8, 0x0003c15c }, + { 0x00000bdc, 0x0003c814 }, + { 0x00000be0, 0x0003ceae }, + { 0x00000be4, 0x0003d52b }, + { 0x00000be8, 0x0003dbbe }, + { 0x00000bec, 0x0003e201 }, + { 0x00000bf0, 0x0003e829 }, + { 0x00000bf4, 0x0003ee38 }, + { 0x00000bf8, 0x0003f45e }, + { 0x00000bfc, 0x0003fa3b }, + { 0x00000f80, 0x00040000 }, + { 0x00000f84, 0x00040b46 }, + { 0x00000f88, 0x00041690 }, + { 0x00000f8c, 0x0004212e }, + { 0x00000f90, 0x00042b80 }, + { 0x00000f94, 0x0004358a }, + { 0x00000f98, 0x00043f9f }, + { 0x00000f9c, 0x00044923 }, + { 0x00000fa0, 0x00045269 }, + { 0x00000fa4, 0x00045b76 }, + { 0x00000fa8, 0x00046492 }, + { 0x00000fac, 0x00046d31 }, + { 0x00000fb0, 0x0004759d }, + { 0x00000fb4, 0x00047dd9 }, + { 0x00000fb8, 0x00048629 }, + { 0x00000fbc, 0x00048e09 }, + { 0x00000fc0, 0x000495c0 }, + { 0x00000fc4, 0x00049d4e }, + { 0x00000fc8, 0x0004a4f2 }, + { 0x00000fcc, 0x0004ac32 }, + { 0x00000fd0, 0x0004b350 }, + { 0x00000fd4, 0x0004ba4a }, + { 0x00000fd8, 0x0004c15c }, + { 0x00000fdc, 0x0004c814 }, + { 0x00000fe0, 0x0004ceae }, + { 0x00000fe4, 0x0004d52b }, + { 0x00000fe8, 0x0004dbbe }, + { 0x00000fec, 0x0004e201 }, + { 0x00000ff0, 0x0004e829 }, + { 0x00000ff4, 0x0004ee38 }, + { 0x00000ff8, 0x0004f45e }, + { 0x00000ffc, 0x0004fa3b }, + { 0x00001f80, 0x00050000 }, +}; + +static const struct regval_list sc2336p_stream_on_mipi[] = { + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2336p_stream_off_mipi[] = { + { 0x0100, 0x00 }, + { 0xffff, 0x00 }, +}; +static const struct regval_list sc2336p_init_primary_head[] = { + { 0x0103, 0x01 }, + { 0x0100, 0x00 }, + { 0x36e9, 0x80 }, + { 0x37f9, 0x80 }, + { 0x300a, 0x24 }, + { 0x301f, 0x1b }, + { 0x3032, 0xa0 }, + { 0x3106, 0x05 }, + { 0x320e, 0x09 }, + { 0x320f, 0xdb }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2336p_init_slave_head[] = { + { 0x0103, 0x01 }, + { 0x0100, 0x00 }, + { 0x36e9, 0x80 }, + { 0x37f9, 0x80 }, + { 0x300a, 0x24 }, + { 0x301f, 0x1b }, + { 0x3106, 0x05 }, + { 0x320e, 0x09 }, + { 0x320f, 0x60 }, + { 0x3222, 0x02 }, + { 0x3224, 0x82 }, + { 0x322e, 0x09 }, + { 0x322f, 0x5c }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2336p_init_body_1080p[] = { + { 0x3248, 0x04 }, + { 0x3249, 0x0b }, + { 0x3253, 0x08 }, + { 0x3301, 0x09 }, + { 0x3302, 0xff }, + { 0x3303, 0x10 }, + { 0x3306, 0x80 }, + { 0x3307, 0x02 }, + { 0x3309, 0xc8 }, + { 0x330a, 0x01 }, + { 0x330b, 0x30 }, + { 0x330c, 0x16 }, + { 0x330d, 0xff }, + { 0x3318, 0x02 }, + { 0x331f, 0xb9 }, + { 0x3321, 0x0a }, + { 0x3327, 0x0e }, + { 0x332b, 0x12 }, + { 0x3333, 0x10 }, + { 0x3334, 0x40 }, + { 0x335e, 0x06 }, + { 0x335f, 0x0a }, + { 0x3364, 0x1f }, + { 0x337c, 0x02 }, + { 0x337d, 0x0e }, + { 0x3390, 0x09 }, + { 0x3391, 0x0f }, + { 0x3392, 0x1f }, + { 0x3393, 0x20 }, + { 0x3394, 0x20 }, + { 0x3395, 0xe0 }, + { 0x33a2, 0x04 }, + { 0x33b1, 0x80 }, + { 0x33b2, 0x68 }, + { 0x33b3, 0x42 }, + { 0x33f9, 0x90 }, + { 0x33fb, 0xd0 }, + { 0x33fc, 0x0f }, + { 0x33fd, 0x1f }, + { 0x349f, 0x03 }, + { 0x34a6, 0x0f }, + { 0x34a7, 0x1f }, + { 0x34a8, 0x42 }, + { 0x34a9, 0x18 }, + { 0x34aa, 0x01 }, + { 0x34ab, 0x43 }, + { 0x34ac, 0x01 }, + { 0x34ad, 0x80 }, + { 0x3630, 0xf4 }, + { 0x3632, 0x44 }, + { 0x3633, 0x22 }, + { 0x3639, 0xf4 }, + { 0x363c, 0x47 }, + { 0x3670, 0x09 }, + { 0x3674, 0xf4 }, + { 0x3675, 0xfb }, + { 0x3676, 0xed }, + { 0x367c, 0x09 }, + { 0x367d, 0x0f }, + { 0x3690, 0x22 }, + { 0x3691, 0x22 }, + { 0x3692, 0x22 }, + { 0x3698, 0x89 }, + { 0x3699, 0x96 }, + { 0x369a, 0xd0 }, + { 0x369b, 0xd0 }, + { 0x369c, 0x09 }, + { 0x369d, 0x0f }, + { 0x36a2, 0x09 }, + { 0x36a3, 0x0f }, + { 0x36a4, 0x1f }, + { 0x36d0, 0x01 }, + { 0x36ea, 0x0b }, + { 0x36eb, 0x0c }, + { 0x36ec, 0x1c }, + { 0x36ed, 0x18 }, + { 0x3722, 0xc1 }, + { 0x3724, 0x41 }, + { 0x3725, 0xc1 }, + { 0x3728, 0x20 }, + { 0x37fa, 0xcb }, + { 0x37fb, 0x32 }, + { 0x37fc, 0x11 }, + { 0x37fd, 0x07 }, + { 0x3900, 0x0d }, + { 0x3905, 0x98 }, + { 0x3919, 0x04 }, + { 0x391b, 0x81 }, + { 0x391c, 0x10 }, + { 0x3933, 0x81 }, + { 0x3934, 0xd0 }, + { 0x3940, 0x75 }, + { 0x3941, 0x00 }, + { 0x3942, 0x01 }, + { 0x3943, 0xd1 }, + { 0x3952, 0x02 }, + { 0x3953, 0x0f }, + { 0x3e01, 0x95 }, + { 0x3e02, 0xa0 }, + { 0x3e08, 0x1f }, + { 0x3e1b, 0x14 }, + { 0x4509, 0x38 }, + { 0x4819, 0x06 }, + { 0x481b, 0x03 }, + { 0x481d, 0x0b }, + { 0x481f, 0x02 }, + { 0x4821, 0x08 }, + { 0x4823, 0x03 }, + { 0x4825, 0x02 }, + { 0x4827, 0x03 }, + { 0x4829, 0x04 }, + { 0x484c, 0x0a }, + { 0x5799, 0x06 }, + { 0x5ae0, 0xfe }, + { 0x5ae1, 0x40 }, + { 0x5ae2, 0x30 }, + { 0x5ae3, 0x28 }, + { 0x5ae4, 0x20 }, + { 0x5ae5, 0x30 }, + { 0x5ae6, 0x28 }, + { 0x5ae7, 0x20 }, + { 0x5ae8, 0x3c }, + { 0x5ae9, 0x30 }, + { 0x5aea, 0x28 }, + { 0x5aeb, 0x3c }, + { 0x5aec, 0x30 }, + { 0x5aed, 0x28 }, + { 0x5aee, 0xfe }, + { 0x5aef, 0x40 }, + { 0x5af4, 0x30 }, + { 0x5af5, 0x28 }, + { 0x5af6, 0x20 }, + { 0x5af7, 0x30 }, + { 0x5af8, 0x28 }, + { 0x5af9, 0x20 }, + { 0x5afa, 0x3c }, + { 0x5afb, 0x30 }, + { 0x5afc, 0x28 }, + { 0x5afd, 0x3c }, + { 0x5afe, 0x30 }, + { 0x5aff, 0x28 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2336p_init_primary_tail[] = { + { 0x36e9, 0x53 }, + { 0x37f9, 0x33 }, + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2336p_init_slave_tail[] = { + { 0x320e, 0x09 }, + { 0x320f, 0xda }, + { 0x322e, 0x09 }, + { 0x322f, 0xd6 }, + { 0x3e01, 0x95 }, + { 0x3e02, 0x70 }, + { 0x36e9, 0x53 }, + { 0x37f9, 0x33 }, + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list * const sc2336p_primary_seqs[] = { + sc2336p_init_primary_head, + sc2336p_init_body_1080p, + sc2336p_init_primary_tail, +}; + +static const struct regval_list * const sc2336p_slave_seqs[] = { + sc2336p_init_slave_head, + sc2336p_init_body_1080p, + sc2336p_init_slave_tail, +}; + +static int sc2336p_fsync(struct v4l2_subdev *sd, bool slave) +{ + if (fsync_mode != 3) + return 0; + return smartsens_write_reg(sd, slave ? 0x3224 : 0x322f, + slave ? 0x83 : 0xa0); +} + +static const struct smartsens_desc sc2336p_desc = { + .name = "sc2336p", + .chip_id = 0x9b3a, + .alt_chip_id = 0xcb3a, + .id_needs_stream = true, + .expected_lanes = 2, + .link_freqs = sc2336p_link_freqs, + .num_link_freqs = ARRAY_SIZE(sc2336p_link_freqs), + .width = 1920, + .height = 1080, + .fps = 15, + .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, + .colorspace = V4L2_COLORSPACE_RAW, + .exposure_min = 2, + .exposure_max = 2397, + .exposure_def = 1000, + .vblank_min = 120, + .vblank_slave_min = 170, + .vblank_max = 8191 - 1080, + .vblank_def = 1443, + .hblank = 280, + .pixel_rate = 79200000, + .vts_def = 2523, + .again_lut = sc2336p_again_lut, + .again_lut_size = ARRAY_SIZE(sc2336p_again_lut), + .max_again = 262144, + .stream_on = sc2336p_stream_on_mipi, + .stream_off = sc2336p_stream_off_mipi, + .primary_seqs = sc2336p_primary_seqs, + .num_primary_seqs = ARRAY_SIZE(sc2336p_primary_seqs), + .slave_seqs = sc2336p_slave_seqs, + .num_slave_seqs = ARRAY_SIZE(sc2336p_slave_seqs), + .fsync = sc2336p_fsync, +}; + +static int sc2336p_probe(struct i2c_client *client) +{ + return smartsens_probe(client, &sc2336p_desc, false); +} + +static const struct i2c_device_id sc2336p_id[] = { + { "sc2336", 0 }, + { "sc2336p", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, sc2336p_id); + +static const struct of_device_id sc2336p_of_match[] = { + { .compatible = "smartsens,sc2336" }, + { .compatible = "smartsens,sc2336p" }, + { } +}; +MODULE_DEVICE_TABLE(of, sc2336p_of_match); + +static struct i2c_driver sc2336p_driver = { + .driver = { + .name = "sc2336p", + .of_match_table = sc2336p_of_match, + .pm = pm_ptr(&smartsens_pm_ops), + }, + .probe = sc2336p_probe, + .remove = smartsens_remove, + .id_table = sc2336p_id, +}; +module_i2c_driver(sc2336p_driver); + +MODULE_DESCRIPTION("SmartSens SC2336P MIPI CSI-2 sensor driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/i2c/smartsens/sensor_sc2337p.c b/drivers/media/i2c/smartsens/sensor_sc2337p.c new file mode 100644 index 0000000000..e3a5e85a5a --- /dev/null +++ b/drivers/media/i2c/smartsens/sensor_sc2337p.c @@ -0,0 +1,473 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Ingenic T23N sc2337p sensor driver + */ + +#include +#include +#include + +#include "smartsens-common.h" + +static int fsync_mode = 3; +module_param(fsync_mode, int, 0444); +MODULE_PARM_DESC(fsync_mode, "Frame synchronization mode"); + +static const s64 sc2337p_link_freqs[] = { 198000000 }; + +static const struct smartsens_again_lut sc2337p_again_lut[] = { + { 0x00000080, 0x00000000 }, + { 0x00000084, 0x00000b46 }, + { 0x00000088, 0x00001690 }, + { 0x0000008c, 0x0000212e }, + { 0x00000090, 0x00002b80 }, + { 0x00000094, 0x0000358a }, + { 0x00000098, 0x00003f9f }, + { 0x0000009c, 0x00004923 }, + { 0x000000a0, 0x00005269 }, + { 0x000000a4, 0x00005b76 }, + { 0x000000a8, 0x00006492 }, + { 0x000000ac, 0x00006d31 }, + { 0x000000b0, 0x0000759d }, + { 0x000000b4, 0x00007dd9 }, + { 0x000000b8, 0x00008629 }, + { 0x000000bc, 0x00008e09 }, + { 0x000000c0, 0x000095c0 }, + { 0x000000c4, 0x00009d4e }, + { 0x000000c8, 0x0000a4f2 }, + { 0x000000cc, 0x0000ac32 }, + { 0x000000d0, 0x0000b350 }, + { 0x000000d4, 0x0000ba4a }, + { 0x000000d8, 0x0000c15c }, + { 0x000000dc, 0x0000c814 }, + { 0x000000e0, 0x0000ceae }, + { 0x000000e4, 0x0000d52b }, + { 0x000000e8, 0x0000dbbe }, + { 0x000000ec, 0x0000e201 }, + { 0x000000f0, 0x0000e829 }, + { 0x000000f4, 0x0000ee38 }, + { 0x000000f8, 0x0000f45e }, + { 0x000000fc, 0x0000fa3b }, + { 0x00000880, 0x00010000 }, + { 0x00000884, 0x00010b46 }, + { 0x00000888, 0x00011690 }, + { 0x0000088c, 0x0001212e }, + { 0x00000890, 0x00012b80 }, + { 0x00000894, 0x0001358a }, + { 0x00000898, 0x00013f9f }, + { 0x0000089c, 0x00014923 }, + { 0x000008a0, 0x00015269 }, + { 0x000008a4, 0x00015b76 }, + { 0x000008a8, 0x00016492 }, + { 0x000008ac, 0x00016d31 }, + { 0x000008b0, 0x0001759d }, + { 0x000008b4, 0x00017dd9 }, + { 0x000008b8, 0x00018629 }, + { 0x000008bc, 0x00018e09 }, + { 0x000008c0, 0x000195c0 }, + { 0x000008c4, 0x00019d4e }, + { 0x000008c8, 0x0001a4f2 }, + { 0x000008cc, 0x0001ac32 }, + { 0x000008d0, 0x0001b350 }, + { 0x000008d4, 0x0001ba4a }, + { 0x000008d8, 0x0001c15c }, + { 0x000008dc, 0x0001c814 }, + { 0x000008e0, 0x0001ceae }, + { 0x000008e4, 0x0001d52b }, + { 0x000008e8, 0x0001dbbe }, + { 0x000008ec, 0x0001e201 }, + { 0x000008f0, 0x0001e829 }, + { 0x000008f4, 0x0001ee38 }, + { 0x000008f8, 0x0001f45e }, + { 0x000008fc, 0x0001fa3b }, + { 0x00000980, 0x00020000 }, + { 0x00000984, 0x00020b46 }, + { 0x00000988, 0x00021690 }, + { 0x0000098c, 0x0002212e }, + { 0x00000990, 0x00022b80 }, + { 0x00000994, 0x0002358a }, + { 0x00000998, 0x00023f9f }, + { 0x0000099c, 0x00024923 }, + { 0x000009a0, 0x00025269 }, + { 0x000009a4, 0x00025b76 }, + { 0x000009a8, 0x00026492 }, + { 0x000009ac, 0x00026d31 }, + { 0x000009b0, 0x0002759d }, + { 0x000009b4, 0x00027dd9 }, + { 0x000009b8, 0x00028629 }, + { 0x000009bc, 0x00028e09 }, + { 0x000009c0, 0x000295c0 }, + { 0x000009c4, 0x00029d4e }, + { 0x000009c8, 0x0002a4f2 }, + { 0x000009cc, 0x0002ac32 }, + { 0x000009d0, 0x0002b350 }, + { 0x000009d4, 0x0002ba4a }, + { 0x000009d8, 0x0002c15c }, + { 0x000009dc, 0x0002c814 }, + { 0x000009e0, 0x0002ceae }, + { 0x000009e4, 0x0002d52b }, + { 0x000009e8, 0x0002dbbe }, + { 0x000009ec, 0x0002e201 }, + { 0x000009f0, 0x0002e829 }, + { 0x000009f4, 0x0002ee38 }, + { 0x000009f8, 0x0002f45e }, + { 0x000009fc, 0x0002fa3b }, + { 0x00000b80, 0x00030000 }, + { 0x00000b84, 0x00030b46 }, + { 0x00000b88, 0x00031690 }, + { 0x00000b8c, 0x0003212e }, + { 0x00000b90, 0x00032b80 }, + { 0x00000b94, 0x0003358a }, + { 0x00000b98, 0x00033f9f }, + { 0x00000b9c, 0x00034923 }, + { 0x00000ba0, 0x00035269 }, + { 0x00000ba4, 0x00035b76 }, + { 0x00000ba8, 0x00036492 }, + { 0x00000bac, 0x00036d31 }, + { 0x00000bb0, 0x0003759d }, + { 0x00000bb4, 0x00037dd9 }, + { 0x00000bb8, 0x00038629 }, + { 0x00000bbc, 0x00038e09 }, + { 0x00000bc0, 0x000395c0 }, + { 0x00000bc4, 0x00039d4e }, + { 0x00000bc8, 0x0003a4f2 }, + { 0x00000bcc, 0x0003ac32 }, + { 0x00000bd0, 0x0003b350 }, + { 0x00000bd4, 0x0003ba4a }, + { 0x00000bd8, 0x0003c15c }, + { 0x00000bdc, 0x0003c814 }, + { 0x00000be0, 0x0003ceae }, + { 0x00000be4, 0x0003d52b }, + { 0x00000be8, 0x0003dbbe }, + { 0x00000bec, 0x0003e201 }, + { 0x00000bf0, 0x0003e829 }, + { 0x00000bf4, 0x0003ee38 }, + { 0x00000bf8, 0x0003f45e }, + { 0x00000bfc, 0x0003fa3b }, + { 0x00000f80, 0x00040000 }, + { 0x00000f84, 0x00040b46 }, + { 0x00000f88, 0x00041690 }, + { 0x00000f8c, 0x0004212e }, + { 0x00000f90, 0x00042b80 }, + { 0x00000f94, 0x0004358a }, + { 0x00000f98, 0x00043f9f }, + { 0x00000f9c, 0x00044923 }, + { 0x00000fa0, 0x00045269 }, + { 0x00000fa4, 0x00045b76 }, + { 0x00000fa8, 0x00046492 }, + { 0x00000fac, 0x00046d31 }, + { 0x00000fb0, 0x0004759d }, + { 0x00000fb4, 0x00047dd9 }, + { 0x00000fb8, 0x00048629 }, + { 0x00000fbc, 0x00048e09 }, + { 0x00000fc0, 0x000495c0 }, + { 0x00000fc4, 0x00049d4e }, + { 0x00000fc8, 0x0004a4f2 }, + { 0x00000fcc, 0x0004ac32 }, + { 0x00000fd0, 0x0004b350 }, + { 0x00000fd4, 0x0004ba4a }, + { 0x00000fd8, 0x0004c15c }, + { 0x00000fdc, 0x0004c814 }, + { 0x00000fe0, 0x0004ceae }, + { 0x00000fe4, 0x0004d52b }, + { 0x00000fe8, 0x0004dbbe }, + { 0x00000fec, 0x0004e201 }, + { 0x00000ff0, 0x0004e829 }, + { 0x00000ff4, 0x0004ee38 }, + { 0x00000ff8, 0x0004f45e }, + { 0x00000ffc, 0x0004fa3b }, + { 0x00001f80, 0x00050000 }, +}; + +static const struct regval_list sc2337p_stream_on_mipi[] = { + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2337p_stream_off_mipi[] = { + { 0x0100, 0x00 }, + { 0xffff, 0x00 }, +}; +static const struct regval_list sc2337p_init_primary_head[] = { + { 0x0103, 0x01 }, + { 0x0100, 0x00 }, + { 0x36e9, 0x80 }, + { 0x37f9, 0x80 }, + { 0x300a, 0x24 }, + { 0x301f, 0x1b }, + { 0x3032, 0xa0 }, + { 0x3106, 0x05 }, + { 0x320e, 0x09 }, + { 0x320f, 0xdb }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2337p_init_slave_head[] = { + { 0x0103, 0x01 }, + { 0x0100, 0x00 }, + { 0x36e9, 0x80 }, + { 0x37f9, 0x80 }, + { 0x300a, 0x24 }, + { 0x301f, 0x1b }, + { 0x3106, 0x05 }, + { 0x320e, 0x09 }, + { 0x320f, 0x60 }, + { 0x3222, 0x02 }, + { 0x3224, 0x82 }, + { 0x322e, 0x09 }, + { 0x322f, 0x5c }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2337p_init_body_1080p[] = { + { 0x3248, 0x04 }, + { 0x3249, 0x0b }, + { 0x3253, 0x08 }, + { 0x3301, 0x09 }, + { 0x3302, 0xff }, + { 0x3303, 0x10 }, + { 0x3306, 0x80 }, + { 0x3307, 0x02 }, + { 0x3309, 0xc8 }, + { 0x330a, 0x01 }, + { 0x330b, 0x30 }, + { 0x330c, 0x16 }, + { 0x330d, 0xff }, + { 0x3318, 0x02 }, + { 0x331f, 0xb9 }, + { 0x3321, 0x0a }, + { 0x3327, 0x0e }, + { 0x332b, 0x12 }, + { 0x3333, 0x10 }, + { 0x3334, 0x40 }, + { 0x335e, 0x06 }, + { 0x335f, 0x0a }, + { 0x3364, 0x1f }, + { 0x337c, 0x02 }, + { 0x337d, 0x0e }, + { 0x3390, 0x09 }, + { 0x3391, 0x0f }, + { 0x3392, 0x1f }, + { 0x3393, 0x20 }, + { 0x3394, 0x20 }, + { 0x3395, 0xe0 }, + { 0x33a2, 0x04 }, + { 0x33b1, 0x80 }, + { 0x33b2, 0x68 }, + { 0x33b3, 0x42 }, + { 0x33f9, 0x90 }, + { 0x33fb, 0xd0 }, + { 0x33fc, 0x0f }, + { 0x33fd, 0x1f }, + { 0x349f, 0x03 }, + { 0x34a6, 0x0f }, + { 0x34a7, 0x1f }, + { 0x34a8, 0x42 }, + { 0x34a9, 0x18 }, + { 0x34aa, 0x01 }, + { 0x34ab, 0x43 }, + { 0x34ac, 0x01 }, + { 0x34ad, 0x80 }, + { 0x3630, 0xf4 }, + { 0x3632, 0x44 }, + { 0x3633, 0x22 }, + { 0x3639, 0xf4 }, + { 0x363c, 0x47 }, + { 0x3670, 0x09 }, + { 0x3674, 0xf4 }, + { 0x3675, 0xfb }, + { 0x3676, 0xed }, + { 0x367c, 0x09 }, + { 0x367d, 0x0f }, + { 0x3690, 0x22 }, + { 0x3691, 0x22 }, + { 0x3692, 0x22 }, + { 0x3698, 0x89 }, + { 0x3699, 0x96 }, + { 0x369a, 0xd0 }, + { 0x369b, 0xd0 }, + { 0x369c, 0x09 }, + { 0x369d, 0x0f }, + { 0x36a2, 0x09 }, + { 0x36a3, 0x0f }, + { 0x36a4, 0x1f }, + { 0x36d0, 0x01 }, + { 0x36ea, 0x0b }, + { 0x36eb, 0x0c }, + { 0x36ec, 0x1c }, + { 0x36ed, 0x18 }, + { 0x3722, 0xc1 }, + { 0x3724, 0x41 }, + { 0x3725, 0xc1 }, + { 0x3728, 0x20 }, + { 0x37fa, 0xcb }, + { 0x37fb, 0x32 }, + { 0x37fc, 0x11 }, + { 0x37fd, 0x07 }, + { 0x3900, 0x0d }, + { 0x3905, 0x98 }, + { 0x3919, 0x04 }, + { 0x391b, 0x81 }, + { 0x391c, 0x10 }, + { 0x3933, 0x81 }, + { 0x3934, 0xd0 }, + { 0x3940, 0x75 }, + { 0x3941, 0x00 }, + { 0x3942, 0x01 }, + { 0x3943, 0xd1 }, + { 0x3952, 0x02 }, + { 0x3953, 0x0f }, + { 0x3e01, 0x95 }, + { 0x3e02, 0xa0 }, + { 0x3e08, 0x1f }, + { 0x3e1b, 0x14 }, + { 0x4509, 0x38 }, + { 0x4819, 0x06 }, + { 0x481b, 0x03 }, + { 0x481d, 0x0b }, + { 0x481f, 0x02 }, + { 0x4821, 0x08 }, + { 0x4823, 0x03 }, + { 0x4825, 0x02 }, + { 0x4827, 0x03 }, + { 0x4829, 0x04 }, + { 0x484c, 0x0a }, + { 0x5799, 0x06 }, + { 0x5ae0, 0xfe }, + { 0x5ae1, 0x40 }, + { 0x5ae2, 0x30 }, + { 0x5ae3, 0x28 }, + { 0x5ae4, 0x20 }, + { 0x5ae5, 0x30 }, + { 0x5ae6, 0x28 }, + { 0x5ae7, 0x20 }, + { 0x5ae8, 0x3c }, + { 0x5ae9, 0x30 }, + { 0x5aea, 0x28 }, + { 0x5aeb, 0x3c }, + { 0x5aec, 0x30 }, + { 0x5aed, 0x28 }, + { 0x5aee, 0xfe }, + { 0x5aef, 0x40 }, + { 0x5af4, 0x30 }, + { 0x5af5, 0x28 }, + { 0x5af6, 0x20 }, + { 0x5af7, 0x30 }, + { 0x5af8, 0x28 }, + { 0x5af9, 0x20 }, + { 0x5afa, 0x3c }, + { 0x5afb, 0x30 }, + { 0x5afc, 0x28 }, + { 0x5afd, 0x3c }, + { 0x5afe, 0x30 }, + { 0x5aff, 0x28 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2337p_init_primary_tail[] = { + { 0x36e9, 0x53 }, + { 0x37f9, 0x33 }, + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list sc2337p_init_slave_tail[] = { + { 0x320e, 0x09 }, + { 0x320f, 0xda }, + { 0x322e, 0x09 }, + { 0x322f, 0xd6 }, + { 0x3e01, 0x95 }, + { 0x3e02, 0x70 }, + { 0x36e9, 0x53 }, + { 0x37f9, 0x33 }, + { 0x0100, 0x01 }, + { 0xffff, 0x00 }, +}; + +static const struct regval_list * const sc2337p_primary_seqs[] = { + sc2337p_init_primary_head, + sc2337p_init_body_1080p, + sc2337p_init_primary_tail, +}; + +static const struct regval_list * const sc2337p_slave_seqs[] = { + sc2337p_init_slave_head, + sc2337p_init_body_1080p, + sc2337p_init_slave_tail, +}; + +static int sc2337p_fsync(struct v4l2_subdev *sd, bool slave) +{ + if (fsync_mode != 3) + return 0; + return smartsens_write_reg(sd, slave ? 0x3224 : 0x322f, + slave ? 0x83 : 0xa0); +} + +static const struct smartsens_desc sc2337p_desc = { + .name = "sc2337p", + .chip_id = 0x9b3a, + .alt_chip_id = 0xcb3a, + .id_needs_stream = true, + .expected_lanes = 2, + .link_freqs = sc2337p_link_freqs, + .num_link_freqs = ARRAY_SIZE(sc2337p_link_freqs), + .width = 1920, + .height = 1080, + .fps = 15, + .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, + .colorspace = V4L2_COLORSPACE_RAW, + .exposure_min = 2, + .exposure_max = 2397, + .exposure_def = 1000, + .vblank_min = 170, + .vblank_max = 8191 - 1080, + .vblank_def = 1443, + .hblank = 280, + .pixel_rate = 79200000, + .vts_def = 2523, + .again_lut = sc2337p_again_lut, + .again_lut_size = ARRAY_SIZE(sc2337p_again_lut), + .max_again = 262144, + .stream_on = sc2337p_stream_on_mipi, + .stream_off = sc2337p_stream_off_mipi, + .primary_seqs = sc2337p_primary_seqs, + .num_primary_seqs = ARRAY_SIZE(sc2337p_primary_seqs), + .slave_seqs = sc2337p_slave_seqs, + .num_slave_seqs = ARRAY_SIZE(sc2337p_slave_seqs), + .fsync = sc2337p_fsync, +}; + +static int sc2337p_probe(struct i2c_client *client) +{ + return smartsens_probe(client, &sc2337p_desc, false); +} + +static const struct i2c_device_id sc2337p_id[] = { + { "sc2337p", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, sc2337p_id); + +static const struct of_device_id sc2337p_of_match[] = { + { .compatible = "smartsens,sc2337p" }, + { } +}; +MODULE_DEVICE_TABLE(of, sc2337p_of_match); + +static struct i2c_driver sc2337p_driver = { + .driver = { + .name = "sc2337p", + .of_match_table = sc2337p_of_match, + .pm = pm_ptr(&smartsens_pm_ops), + }, + .probe = sc2337p_probe, + .remove = smartsens_remove, + .id_table = sc2337p_id, +}; +module_i2c_driver(sc2337p_driver); + +MODULE_DESCRIPTION("SmartSens SC2337P MIPI CSI-2 sensor driver"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/i2c/smartsens/smartsens-common.c b/drivers/media/i2c/smartsens/smartsens-common.c new file mode 100644 index 0000000000..3cb1a4ba1f --- /dev/null +++ b/drivers/media/i2c/smartsens/smartsens-common.c @@ -0,0 +1,981 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Shared helpers for SmartSens MIPI CSI-2 image sensors + * + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "smartsens-common.h" + +#define SMARTSENS_XVCLK_RATE 24000000 +#define SMARTSENS_REG_STREAM 0x0100 +#define SMARTSENS_REG_SEC_I2C_ADDR 0x3004 +#define SMARTSENS_REG_SYS_RESET 0x301a +#define SMARTSENS_REG_CHIP_ID_H 0x3107 +#define SMARTSENS_REG_CHIP_ID_L 0x3108 +#define SMARTSENS_REG_VTS_H 0x320e +#define SMARTSENS_REG_VTS_L 0x320f +#define SMARTSENS_REG_MIRROR_FLIP 0x3221 +#define SMARTSENS_REG_EXP_H 0x3e00 +#define SMARTSENS_REG_EXP_M 0x3e01 +#define SMARTSENS_REG_EXP_L 0x3e02 +#define SMARTSENS_REG_AGAIN_FINE 0x3e07 +#define SMARTSENS_REG_AGAIN_CTRL 0x3e08 +#define SMARTSENS_REG_AGAIN_COARSE 0x3e09 +#define SMARTSENS_REG_TEST_PATTERN 0x4501 + +static const char * const smartsens_supply_names[SMARTSENS_NUM_SUPPLIES] = { + "avdd", + "dovdd", + "dvdd", +}; + +static const char * const smartsens_test_pattern_menu[] = { + "Disabled", + "Vertical Color Bars", + "Pseudo-Random Data", + "Solid Square", + "Black Image", +}; + +int smartsens_read_reg(struct v4l2_subdev *sd, u16 reg, u8 *val) +{ + struct i2c_client *client = v4l2_get_subdevdata(sd); + u8 buf[2] = { reg >> 8, reg & 0xff }; + struct i2c_msg msgs[2] = { + { + .addr = client->addr, + .flags = 0, + .len = sizeof(buf), + .buf = buf, + }, + { + .addr = client->addr, + .flags = I2C_M_RD, + .len = 1, + .buf = val, + }, + }; + int ret; + + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); + return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO); +} +EXPORT_SYMBOL_GPL(smartsens_read_reg); + +int smartsens_write_reg(struct v4l2_subdev *sd, u16 reg, u8 val) +{ + struct i2c_client *client = v4l2_get_subdevdata(sd); + u8 buf[3] = { reg >> 8, reg & 0xff, val }; + struct i2c_msg msg = { + .addr = client->addr, + .flags = 0, + .len = sizeof(buf), + .buf = buf, + }; + int ret; + + ret = i2c_transfer(client->adapter, &msg, 1); + return ret == 1 ? 0 : (ret < 0 ? ret : -EIO); +} +EXPORT_SYMBOL_GPL(smartsens_write_reg); + +static int smartsens_write_array(struct v4l2_subdev *sd, + const struct regval_list *vals, + bool skip_stream_on) +{ + int ret; + + while (vals->reg_num != 0xffff) { + if (vals->reg_num == 0xfffe) { + msleep(vals->value); + } else if (!(skip_stream_on && + vals->reg_num == SMARTSENS_REG_STREAM && + vals->value == 1)) { + ret = smartsens_write_reg(sd, vals->reg_num, + vals->value); + if (ret < 0) + return ret; + } + vals++; + } + return 0; +} + +static unsigned int smartsens_max_gain(const struct smartsens_desc *desc) +{ + unsigned int max_gain = desc->again_lut_size ? + desc->again_lut_size - 1 : 0; + + while (max_gain && desc->again_lut[max_gain].gain_val > desc->max_again) + max_gain--; + return max_gain; +} + +static int smartsens_set_flip(struct v4l2_subdev *sd, int hflip, int vflip) +{ + u8 val = 0; + + if (hflip) + val |= 0x06; + if (vflip) + val |= 0x60; + return smartsens_write_reg(sd, SMARTSENS_REG_MIRROR_FLIP, val); +} + +static int smartsens_set_test_pattern(struct v4l2_subdev *sd, s32 val) +{ + u8 regval = val ? (0x08 | ((val - 1) & 0x03)) : 0x00; + + return smartsens_write_reg(sd, SMARTSENS_REG_TEST_PATTERN, regval); +} + +static int smartsens_set_exposure(struct v4l2_subdev *sd, s32 lines) +{ + int ret; + + ret = smartsens_write_reg(sd, SMARTSENS_REG_EXP_H, (lines >> 12) & 0x0f); + if (ret) + return ret; + ret = smartsens_write_reg(sd, SMARTSENS_REG_EXP_M, (lines >> 4) & 0xff); + if (ret) + return ret; + return smartsens_write_reg(sd, SMARTSENS_REG_EXP_L, (lines << 4) & 0xf0); +} + +int smartsens_set_vblank(struct v4l2_subdev *sd, s32 val) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + u32 vts = sensor->height + val; + int ret; + + sensor->vts = vts; + ret = smartsens_write_reg(sd, SMARTSENS_REG_VTS_H, (vts >> 8) & 0xff); + if (ret) + return ret; + return smartsens_write_reg(sd, SMARTSENS_REG_VTS_L, vts & 0xff); +} +EXPORT_SYMBOL_GPL(smartsens_set_vblank); + +static int smartsens_set_again(struct v4l2_subdev *sd, s32 index) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + const struct smartsens_desc *desc = sensor->desc; + u32 reg_code; + int ret; + + if (index < 0 || index >= desc->again_lut_size) + return -EINVAL; + reg_code = desc->again_lut[index].reg_code; + if (desc->sc1a4t_again_layout) { + ret = smartsens_write_reg(sd, SMARTSENS_REG_AGAIN_FINE, + reg_code & 0xff); + if (ret) + return ret; + return smartsens_write_reg(sd, SMARTSENS_REG_AGAIN_COARSE, + (reg_code >> 8) & 0xff); + } + ret = smartsens_write_reg(sd, SMARTSENS_REG_AGAIN_COARSE, + (reg_code >> 8) & 0xff); + if (ret) + return ret; + ret = smartsens_write_reg(sd, SMARTSENS_REG_AGAIN_CTRL, 0x1f); + if (ret) + return ret; + return smartsens_write_reg(sd, SMARTSENS_REG_AGAIN_FINE, + reg_code & 0xff); +} + +static int smartsens_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct smartsens_sensor *sensor = + container_of(ctrl->handler, struct smartsens_sensor, ctrls); + struct v4l2_subdev *sd = &sensor->sd; + s32 old_exp = sensor->exposure ? sensor->exposure->val : 0; + + if (ctrl->id == V4L2_CID_VBLANK && sensor->exposure) { + u32 margin = sensor->exposure_margin ? sensor->exposure_margin : 6; + u32 maximum = sensor->height + ctrl->val - margin; + + __v4l2_ctrl_modify_range(sensor->exposure, + sensor->exposure->minimum, + maximum, + sensor->exposure->step, + min_t(u32, sensor->exposure->default_value, + maximum)); + } + + if (!sensor->streaming) { + if (ctrl->id == V4L2_CID_VBLANK) + sensor->vts = sensor->height + ctrl->val; + return 0; + } + + switch (ctrl->id) { + case V4L2_CID_EXPOSURE: + return smartsens_set_exposure(sd, ctrl->val); + case V4L2_CID_ANALOGUE_GAIN: + return smartsens_set_again(sd, ctrl->val); + case V4L2_CID_HFLIP: + case V4L2_CID_VFLIP: { + int hflip = (ctrl->id == V4L2_CID_HFLIP) ? ctrl->val : + (sensor->hflip ? sensor->hflip->val : 0); + int vflip = (ctrl->id == V4L2_CID_VFLIP) ? ctrl->val : + (sensor->vflip ? sensor->vflip->val : 0); + return smartsens_set_flip(sd, hflip, vflip); + } + case V4L2_CID_VBLANK: + if (sensor->exposure && sensor->exposure->val < old_exp) { + int ret = smartsens_set_exposure(sd, + sensor->exposure->val); + if (ret) + return ret; + } + return sensor->desc->set_vblank ? + sensor->desc->set_vblank(sd, ctrl->val) : + smartsens_set_vblank(sd, ctrl->val); + case V4L2_CID_TEST_PATTERN: + return smartsens_set_test_pattern(sd, ctrl->val); + default: + return -EINVAL; + } +} + +static const struct v4l2_ctrl_ops smartsens_ctrl_ops = { + .s_ctrl = smartsens_s_ctrl, +}; + +static int smartsens_enable_power_supplies(struct smartsens_sensor *sensor) +{ + int ret; + + ret = regulator_bulk_enable(SMARTSENS_NUM_SUPPLIES, sensor->supplies); + if (ret) + return ret; + + ret = clk_prepare_enable(sensor->mclk); + if (ret) { + regulator_bulk_disable(SMARTSENS_NUM_SUPPLIES, + sensor->supplies); + return ret; + } + return 0; +} + +static int smartsens_power_on(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + int ret; + + ret = smartsens_enable_power_supplies(sensor); + if (ret) + return ret; + + if (sensor->pwdn_gpio) { + gpiod_set_value_cansleep(sensor->pwdn_gpio, 0); + msleep(10); + } + /* + * Do not assert a shared reset line when a secondary sensor has been + * remapped to secondary_i2c_addr, or its remapped I2C address is lost. + */ + if (sensor->reset_gpio && !sensor->secondary_i2c_addr) { + gpiod_set_value_cansleep(sensor->reset_gpio, 1); + msleep(10); + gpiod_set_value_cansleep(sensor->reset_gpio, 0); + msleep(10); + } + return 0; +} + +static int smartsens_power_off(struct device *dev) +{ + struct v4l2_subdev *sd = dev_get_drvdata(dev); + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + + if (sensor->reset_gpio && !sensor->secondary_i2c_addr) + gpiod_set_value_cansleep(sensor->reset_gpio, 1); + if (sensor->pwdn_gpio) + gpiod_set_value_cansleep(sensor->pwdn_gpio, 1); + clk_disable_unprepare(sensor->mclk); + regulator_bulk_disable(SMARTSENS_NUM_SUPPLIES, sensor->supplies); + return 0; +} + +static int smartsens_probe_power_on(struct smartsens_sensor *sensor) +{ + int ret; + + if (!sensor->secondary_i2c_addr) + return smartsens_power_on(sensor->sd.dev); + + ret = smartsens_enable_power_supplies(sensor); + if (ret) + return ret; + + /* + * Hold the primary sensor in power-down while releasing the shared + * reset line so the secondary sensor responds at 0x30 and can be + * reassigned to secondary_i2c_addr before the primary wakes. + */ + if (sensor->pwdn_gpio) { + gpiod_set_value_cansleep(sensor->pwdn_gpio, 1); + msleep(10); + } + if (sensor->reset_gpio) { + gpiod_set_value_cansleep(sensor->reset_gpio, 1); + msleep(10); + gpiod_set_value_cansleep(sensor->reset_gpio, 0); + msleep(10); + } + ret = smartsens_write_reg(&sensor->sd, SMARTSENS_REG_SEC_I2C_ADDR, + sensor->secondary_i2c_addr << 1); + if (ret) + goto err_off; + if (sensor->pwdn_gpio) { + gpiod_set_value_cansleep(sensor->pwdn_gpio, 0); + msleep(10); + } + return 0; + +err_off: + if (sensor->pwdn_gpio) + gpiod_set_value_cansleep(sensor->pwdn_gpio, 1); + clk_disable_unprepare(sensor->mclk); + regulator_bulk_disable(SMARTSENS_NUM_SUPPLIES, sensor->supplies); + return ret; +} + +static int smartsens_identify(struct smartsens_sensor *sensor) +{ + const struct smartsens_desc *desc = sensor->desc; + struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd); + u8 id_h = 0, id_l = 0; + u16 id; + int ret, stop_ret = 0; + + if (desc->id_needs_stream) { + ret = smartsens_write_reg(&sensor->sd, SMARTSENS_REG_SYS_RESET, + 0xf8); + if (ret) + return ret; + ret = smartsens_write_reg(&sensor->sd, SMARTSENS_REG_STREAM, + 0x01); + if (ret) + goto stop; + msleep(10); + } + + ret = smartsens_read_reg(&sensor->sd, SMARTSENS_REG_CHIP_ID_H, &id_h); + if (ret) + goto stop; + ret = smartsens_read_reg(&sensor->sd, SMARTSENS_REG_CHIP_ID_L, &id_l); + if (ret) + goto stop; + + id = ((u16)id_h << 8) | id_l; + if (id != desc->chip_id && + (!desc->alt_chip_id || id != desc->alt_chip_id)) { + dev_err(&client->dev, + "unexpected chip ID %#04x (expected %#04x)\n", + id, desc->chip_id); + ret = -ENODEV; + goto stop; + } + dev_dbg(&client->dev, "detected chip %#04x\n", id); + +stop: + if (desc->id_needs_stream) + stop_ret = smartsens_write_reg(&sensor->sd, + SMARTSENS_REG_STREAM, 0x00); + return ret ? ret : stop_ret; +} + +static int smartsens_s_stream(struct v4l2_subdev *sd, int enable) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + struct i2c_client *client = v4l2_get_subdevdata(sd); + const struct smartsens_desc *desc = sensor->desc; + const struct regval_list * const *seqs; + size_t num_seqs, i; + int ret = 0; + + mutex_lock(&sensor->lock); + if (sensor->streaming == !!enable) + goto out; + + if (!enable) { + ret = smartsens_write_array(sd, desc->stream_off, false); + sensor->streaming = false; + if (sensor->hflip) + __v4l2_ctrl_grab(sensor->hflip, false); + if (sensor->vflip) + __v4l2_ctrl_grab(sensor->vflip, false); + pm_runtime_put(&client->dev); + goto out; + } + + ret = pm_runtime_resume_and_get(&client->dev); + if (ret < 0) + goto out; + + seqs = sensor->frame_sync_slave ? desc->slave_seqs : desc->primary_seqs; + num_seqs = sensor->frame_sync_slave ? desc->num_slave_seqs : + desc->num_primary_seqs; + for (i = 0; i < num_seqs; i++) { + ret = smartsens_write_array(sd, seqs[i], true); + if (ret) + goto stop; + } + if (sensor->shvflip) { + ret = smartsens_write_reg(sd, SMARTSENS_REG_MIRROR_FLIP, 0x66); + if (ret) + goto stop; + } + + sensor->streaming = true; + ret = __v4l2_ctrl_handler_setup(&sensor->ctrls); + if (ret) + goto stop; + ret = smartsens_write_array(sd, desc->stream_on, false); + if (ret) + goto stop; + if (desc->fsync) { + ret = desc->fsync(sd, sensor->frame_sync_slave); + if (ret) + goto stop; + } + if (sensor->hflip) + __v4l2_ctrl_grab(sensor->hflip, true); + if (sensor->vflip) + __v4l2_ctrl_grab(sensor->vflip, true); + goto out; + +stop: + smartsens_write_array(sd, desc->stream_off, false); + sensor->streaming = false; + pm_runtime_put(&client->dev); +out: + mutex_unlock(&sensor->lock); + return ret; +} + +static int smartsens_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_mbus_code_enum *code) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + + if (code->pad || code->index) + return -EINVAL; + code->code = sensor->fmt.code; + return 0; +} + +static int smartsens_set_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_format *format) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + struct v4l2_mbus_framefmt *fmt; + + if (format->pad) + return -EINVAL; + fmt = v4l2_subdev_state_get_format(sd_state, format->pad); + if (!fmt) + return -EINVAL; + *fmt = sensor->fmt; + format->format = *fmt; + return 0; +} + +static int smartsens_enum_frame_size(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_size_enum *fse) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + + if (fse->pad || fse->index || fse->code != sensor->fmt.code) + return -EINVAL; + fse->min_width = fse->max_width = sensor->fmt.width; + fse->min_height = fse->max_height = sensor->fmt.height; + return 0; +} + +static int +smartsens_enum_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_interval_enum *fie) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + + if (fie->pad || fie->index || fie->code != sensor->fmt.code || + fie->width != sensor->fmt.width || + fie->height != sensor->fmt.height) + return -EINVAL; + fie->interval.numerator = 1; + fie->interval.denominator = sensor->fps; + return 0; +} + +/* Called with sd_state->lock (&sensor->lock) already held by v4l2-subdev. */ +static int smartsens_get_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_interval *fi) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + u64 pclk = sensor->pixel_rate ? sensor->pixel_rate->val : 0; + u32 hts = sensor->fmt.width + (sensor->hblank ? sensor->hblank->val : 0); + u64 divisor; + + if (fi->pad) + return -EINVAL; + + if (pclk && hts && sensor->vts) { + u64 num = (u64)hts * sensor->vts; + u64 den = pclk; + + divisor = gcd(num, den); + if (divisor) { + num = div64_u64(num, divisor); + den = div64_u64(den, divisor); + } + fi->interval.numerator = num; + fi->interval.denominator = den; + return 0; + } + + fi->interval.numerator = 1; + fi->interval.denominator = sensor->fps; + return 0; +} + +static int smartsens_set_frame_interval(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_frame_interval *fi) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + u64 pclk = sensor->pixel_rate ? sensor->pixel_rate->val : 0; + u32 hts = sensor->fmt.width + (sensor->hblank ? sensor->hblank->val : 0); + + if (fi->pad) + return -EINVAL; + + if (fi->which == V4L2_SUBDEV_FORMAT_ACTIVE && sensor->vblank && + pclk && hts && fi->interval.numerator && fi->interval.denominator) { + u64 vts = div64_u64(pclk * fi->interval.numerator, + (u64)hts * fi->interval.denominator); + s32 vblank = (s32)vts - (s32)sensor->height; + + vblank = clamp(vblank, sensor->vblank->minimum, + sensor->vblank->maximum); + __v4l2_ctrl_s_ctrl(sensor->vblank, vblank); + } + + return smartsens_get_frame_interval(sd, sd_state, fi); +} + +static int smartsens_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad, + struct v4l2_mbus_config *config) +{ + if (pad) + return -EINVAL; + *config = to_smartsens_sensor(sd)->bus; + return 0; +} + +static int smartsens_init_state(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state) +{ + struct v4l2_mbus_framefmt *fmt = + v4l2_subdev_state_get_format(sd_state, 0); + + if (fmt) + *fmt = to_smartsens_sensor(sd)->fmt; + return 0; +} + +static const struct v4l2_subdev_pad_ops smartsens_pad_ops = { + .enum_mbus_code = smartsens_enum_mbus_code, + .enum_frame_size = smartsens_enum_frame_size, + .enum_frame_interval = smartsens_enum_frame_interval, + .get_fmt = v4l2_subdev_get_fmt, + .set_fmt = smartsens_set_fmt, + .get_frame_interval = smartsens_get_frame_interval, + .set_frame_interval = smartsens_set_frame_interval, + .get_mbus_config = smartsens_get_mbus_config, +}; + +static const struct v4l2_subdev_internal_ops smartsens_internal_ops = { + .init_state = smartsens_init_state, +}; + +#ifdef CONFIG_VIDEO_ADV_DEBUG +static int smartsens_g_register(struct v4l2_subdev *sd, + struct v4l2_dbg_register *reg) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + struct i2c_client *client = v4l2_get_subdevdata(sd); + u8 value = 0; + int ret; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + if (reg->reg > 0xffff) + return -EINVAL; + ret = pm_runtime_resume_and_get(&client->dev); + if (ret < 0) + return ret; + mutex_lock(&sensor->lock); + ret = smartsens_read_reg(sd, reg->reg, &value); + mutex_unlock(&sensor->lock); + pm_runtime_put(&client->dev); + if (ret) + return ret; + reg->size = 1; + reg->val = value; + return 0; +} + +static int smartsens_s_register(struct v4l2_subdev *sd, + const struct v4l2_dbg_register *reg) +{ + struct smartsens_sensor *sensor = to_smartsens_sensor(sd); + struct i2c_client *client = v4l2_get_subdevdata(sd); + int ret; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + if (reg->reg > 0xffff || reg->val > 0xff) + return -EINVAL; + ret = pm_runtime_resume_and_get(&client->dev); + if (ret < 0) + return ret; + mutex_lock(&sensor->lock); + ret = smartsens_write_reg(sd, reg->reg, reg->val); + mutex_unlock(&sensor->lock); + pm_runtime_put(&client->dev); + return ret; +} +#endif + +static const struct v4l2_subdev_core_ops smartsens_core_ops = { +#ifdef CONFIG_VIDEO_ADV_DEBUG + .g_register = smartsens_g_register, + .s_register = smartsens_s_register, +#endif + .subscribe_event = v4l2_ctrl_subdev_subscribe_event, + .unsubscribe_event = v4l2_event_subdev_unsubscribe, +}; + +static const struct v4l2_subdev_video_ops smartsens_video_ops = { + .s_stream = smartsens_s_stream, +}; + +static const struct v4l2_subdev_ops smartsens_subdev_ops = { + .core = &smartsens_core_ops, + .video = &smartsens_video_ops, + .pad = &smartsens_pad_ops, +}; + +static int smartsens_parse_fwnode(struct device *dev, + struct smartsens_sensor *sensor) +{ + const struct smartsens_desc *desc = sensor->desc; + struct v4l2_fwnode_endpoint bus_cfg = { + .bus_type = V4L2_MBUS_CSI2_DPHY, + }; + struct fwnode_handle *endpoint; + unsigned long link_freq_bitmap; + unsigned int lanes, i; + u32 sec_addr; + int ret; + + sensor->frame_sync_slave = + device_property_read_bool(dev, "smartsens,frame-sync-slave"); + if (device_property_present(dev, "smartsens,secondary-i2c-addr")) { + ret = device_property_read_u32(dev, + "smartsens,secondary-i2c-addr", + &sec_addr); + if (ret) + return ret; + if (sec_addr < 0x08 || sec_addr > 0x77) + return -EINVAL; + sensor->secondary_i2c_addr = sec_addr; + } + + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); + if (!endpoint) + return -ENOTCONN; + + ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg); + fwnode_handle_put(endpoint); + if (ret) + return ret; + + lanes = bus_cfg.bus.mipi_csi2.num_data_lanes; + if (lanes != desc->expected_lanes || + bus_cfg.bus.mipi_csi2.clock_lane != 0) { + ret = -EINVAL; + goto out_free; + } + for (i = 0; i < lanes; i++) { + if (bus_cfg.bus.mipi_csi2.data_lanes[i] != i + 1) { + ret = -EINVAL; + goto out_free; + } + } + for (i = 0; i <= lanes; i++) { + if (bus_cfg.bus.mipi_csi2.lane_polarities[i]) { + ret = -EINVAL; + goto out_free; + } + } + + ret = v4l2_link_freq_to_bitmap(dev, bus_cfg.link_frequencies, + bus_cfg.nr_of_link_frequencies, + desc->link_freqs, desc->num_link_freqs, + &link_freq_bitmap); + if (ret) + goto out_free; + + sensor->bus.type = V4L2_MBUS_CSI2_DPHY; + sensor->bus.bus.mipi_csi2 = bus_cfg.bus.mipi_csi2; + +out_free: + v4l2_fwnode_endpoint_free(&bus_cfg); + return ret; +} + +static int smartsens_init_controls(struct smartsens_sensor *sensor) +{ + const struct smartsens_desc *desc = sensor->desc; + struct v4l2_ctrl *link_freq; + s32 vblank_min = sensor->frame_sync_slave && desc->vblank_slave_min ? + desc->vblank_slave_min : desc->vblank_min; + u32 again_max = smartsens_max_gain(desc); + int ret; + + v4l2_ctrl_handler_init(&sensor->ctrls, 9); + sensor->ctrls.lock = &sensor->lock; + + link_freq = v4l2_ctrl_new_int_menu(&sensor->ctrls, NULL, + V4L2_CID_LINK_FREQ, + desc->num_link_freqs - 1, 0, + desc->link_freqs); + if (link_freq) + link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; + + sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_EXPOSURE, + desc->exposure_min, + desc->exposure_max, + 1, desc->exposure_def); + sensor->again = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_ANALOGUE_GAIN, + 0, again_max, 1, 0); + sensor->hflip = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_HFLIP, 0, 1, 1, + sensor->shvflip ? 1 : 0); + sensor->vflip = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_VFLIP, 0, 1, 1, + sensor->shvflip ? 1 : 0); + sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_VBLANK, vblank_min, + desc->vblank_max, 1, + desc->vblank_def); + sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_HBLANK, desc->hblank, + desc->hblank, 1, desc->hblank); + if (sensor->hblank) + sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; + sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, + &smartsens_ctrl_ops, + V4L2_CID_PIXEL_RATE, + desc->pixel_rate, + desc->pixel_rate, 1, + desc->pixel_rate); + if (sensor->pixel_rate) + sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; + sensor->test_pattern = v4l2_ctrl_new_std_menu_items( + &sensor->ctrls, &smartsens_ctrl_ops, V4L2_CID_TEST_PATTERN, + ARRAY_SIZE(smartsens_test_pattern_menu) - 1, 0, 0, + smartsens_test_pattern_menu); + + if (sensor->ctrls.error) { + ret = sensor->ctrls.error; + v4l2_ctrl_handler_free(&sensor->ctrls); + return ret; + } + sensor->sd.ctrl_handler = &sensor->ctrls; + return 0; +} + +int smartsens_probe(struct i2c_client *client, + const struct smartsens_desc *desc, bool shvflip) +{ + struct device *dev = &client->dev; + struct smartsens_sensor *sensor; + unsigned int i; + int ret; + + sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); + if (!sensor) + return -ENOMEM; + + sensor->desc = desc; + sensor->shvflip = shvflip; + sensor->height = desc->height; + sensor->fps = desc->fps; + sensor->vts = desc->vts_def; + sensor->frame_length_default = desc->vts_def; + sensor->exposure_margin = desc->exposure_margin ? + desc->exposure_margin : 6; + sensor->fmt.width = desc->width; + sensor->fmt.height = desc->height; + sensor->fmt.code = desc->mbus_code; + sensor->fmt.field = V4L2_FIELD_NONE; + sensor->fmt.colorspace = desc->colorspace; + mutex_init(&sensor->lock); + + ret = smartsens_parse_fwnode(dev, sensor); + if (ret) + return ret; + + for (i = 0; i < SMARTSENS_NUM_SUPPLIES; i++) + sensor->supplies[i].supply = smartsens_supply_names[i]; + ret = devm_regulator_bulk_get(dev, SMARTSENS_NUM_SUPPLIES, + sensor->supplies); + if (ret) + return dev_err_probe(dev, ret, "failed to get regulators\n"); + + sensor->mclk = devm_clk_get(dev, "xvclk"); + if (IS_ERR(sensor->mclk)) + return dev_err_probe(dev, PTR_ERR(sensor->mclk), + "failed to get xvclk\n"); + ret = clk_set_rate(sensor->mclk, SMARTSENS_XVCLK_RATE); + if (!ret && clk_get_rate(sensor->mclk) != SMARTSENS_XVCLK_RATE) + ret = -ERANGE; + if (ret) + return dev_err_probe(dev, ret, + "failed to set xvclk to 24MHz\n"); + + sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", + GPIOD_OUT_HIGH); + if (IS_ERR(sensor->reset_gpio)) + return PTR_ERR(sensor->reset_gpio); + + sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", + GPIOD_OUT_HIGH); + if (IS_ERR(sensor->pwdn_gpio)) + return PTR_ERR(sensor->pwdn_gpio); + + v4l2_i2c_subdev_init(&sensor->sd, client, &smartsens_subdev_ops); + if (client->name[0]) + snprintf(sensor->sd.name, sizeof(sensor->sd.name), "%s %d-%04x", + client->name, i2c_adapter_id(client->adapter), + client->addr); + sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | + V4L2_SUBDEV_FL_HAS_EVENTS; + sensor->sd.internal_ops = &smartsens_internal_ops; + sensor->sd.state_lock = &sensor->lock; + + ret = smartsens_probe_power_on(sensor); + if (ret) + return ret; + + ret = smartsens_identify(sensor); + if (ret) + goto err_power_off; + + ret = smartsens_init_controls(sensor); + if (ret) + goto err_power_off; + + sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; + sensor->pad.flags = MEDIA_PAD_FL_SOURCE; + ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); + if (ret) + goto err_ctrls; + + ret = v4l2_subdev_init_finalize(&sensor->sd); + if (ret) + goto err_entity; + + ret = v4l2_async_register_subdev_sensor(&sensor->sd); + if (ret) + goto err_subdev; + + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + pm_runtime_idle(dev); + return 0; + +err_subdev: + v4l2_subdev_cleanup(&sensor->sd); +err_entity: + media_entity_cleanup(&sensor->sd.entity); +err_ctrls: + v4l2_ctrl_handler_free(&sensor->ctrls); +err_power_off: + smartsens_power_off(dev); + return ret; +} +EXPORT_SYMBOL_GPL(smartsens_probe); + +void smartsens_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd = i2c_get_clientdata(client); + struct smartsens_sensor *sensor = sd ? to_smartsens_sensor(sd) : NULL; + + if (!sensor) + return; + + v4l2_async_unregister_subdev(sd); + v4l2_subdev_cleanup(sd); + media_entity_cleanup(&sd->entity); + if (sd->ctrl_handler) { + v4l2_ctrl_handler_free(sd->ctrl_handler); + sd->ctrl_handler = NULL; + } + pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) + smartsens_power_off(&client->dev); + pm_runtime_set_suspended(&client->dev); +} +EXPORT_SYMBOL_GPL(smartsens_remove); + +const struct dev_pm_ops smartsens_pm_ops = { + RUNTIME_PM_OPS(smartsens_power_off, smartsens_power_on, NULL) +}; +EXPORT_SYMBOL_GPL(smartsens_pm_ops); + +MODULE_DESCRIPTION("SmartSens MIPI CSI-2 image sensor common helpers"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/i2c/smartsens/smartsens-common.h b/drivers/media/i2c/smartsens/smartsens-common.h new file mode 100644 index 0000000000..9b4b55f70c --- /dev/null +++ b/drivers/media/i2c/smartsens/smartsens-common.h @@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __SMARTSENS_COMMON_H__ +#define __SMARTSENS_COMMON_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SMARTSENS_NUM_SUPPLIES 3 + +struct regval_list { + u16 reg_num; + u8 value; +}; + +struct smartsens_again_lut { + u32 reg_code; + u32 gain_val; +}; + +struct smartsens_sensor; + +struct smartsens_desc { + const char *name; + u16 chip_id; + u16 alt_chip_id; + bool id_needs_stream; + unsigned int expected_lanes; + const s64 *link_freqs; + unsigned int num_link_freqs; + u32 width; + u32 height; + u32 fps; + u32 mbus_code; + enum v4l2_colorspace colorspace; + s32 exposure_min; + s32 exposure_max; + s32 exposure_def; + u32 exposure_margin; + s32 vblank_min; + s32 vblank_slave_min; + s32 vblank_max; + s32 vblank_def; + s32 hblank; + s64 pixel_rate; + u32 vts_def; + const struct smartsens_again_lut *again_lut; + size_t again_lut_size; + u32 max_again; + bool sc1a4t_again_layout; + const struct regval_list *stream_on; + const struct regval_list *stream_off; + const struct regval_list * const *primary_seqs; + size_t num_primary_seqs; + const struct regval_list * const *slave_seqs; + size_t num_slave_seqs; + int (*set_vblank)(struct v4l2_subdev *sd, s32 val); + int (*fsync)(struct v4l2_subdev *sd, bool slave); +}; + +struct smartsens_sensor { + struct v4l2_subdev sd; + struct media_pad pad; + struct v4l2_mbus_framefmt fmt; + const struct smartsens_desc *desc; + struct clk *mclk; + struct regulator_bulk_data supplies[SMARTSENS_NUM_SUPPLIES]; + struct gpio_desc *reset_gpio; + struct gpio_desc *pwdn_gpio; + unsigned int height; + unsigned int fps; + unsigned int vts; + unsigned int frame_length_default; + unsigned int exposure_margin; + struct mutex lock; /* serializes subdev state, streaming & controls */ + bool streaming; + bool shvflip; + bool frame_sync_slave; + u32 secondary_i2c_addr; + struct v4l2_mbus_config bus; + struct v4l2_ctrl_handler ctrls; + struct v4l2_ctrl *exposure; + struct v4l2_ctrl *again; + struct v4l2_ctrl *hflip; + struct v4l2_ctrl *vflip; + struct v4l2_ctrl *vblank; + struct v4l2_ctrl *hblank; + struct v4l2_ctrl *pixel_rate; + struct v4l2_ctrl *test_pattern; +}; + +static inline struct smartsens_sensor *to_smartsens_sensor(struct v4l2_subdev *sd) +{ + return container_of(sd, struct smartsens_sensor, sd); +} + +int smartsens_read_reg(struct v4l2_subdev *sd, u16 reg, u8 *val); +int smartsens_write_reg(struct v4l2_subdev *sd, u16 reg, u8 val); +int smartsens_set_vblank(struct v4l2_subdev *sd, s32 val); +int smartsens_probe(struct i2c_client *client, + const struct smartsens_desc *desc, bool shvflip); +void smartsens_remove(struct i2c_client *client); + +extern const struct dev_pm_ops smartsens_pm_ops; + +#endif /* __SMARTSENS_COMMON_H__ */ From 7b8de867106138adff6e5e55fe81478a26bcb8a6 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:25:01 -0800 Subject: [PATCH 64/78] dt-bindings: media: Add the Ingenic T23 ISP Describe the T23 ISP register windows, clocks, interrupts and reset resources. Signed-off-by: Difan Zhang --- .../bindings/media/ingenic,t23-isp.yaml | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/ingenic,t23-isp.yaml diff --git a/Documentation/devicetree/bindings/media/ingenic,t23-isp.yaml b/Documentation/devicetree/bindings/media/ingenic,t23-isp.yaml new file mode 100644 index 0000000000..dc351d5590 --- /dev/null +++ b/Documentation/devicetree/bindings/media/ingenic,t23-isp.yaml @@ -0,0 +1,142 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/ingenic,t23-isp.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 MIPI CSI-2, VIC, ISP and MSCA Video Pipeline + +maintainers: + - Paul Cercueil + +description: + Ingenic T23 integrated image signal processing pipeline comprising the + MIPI CSI-2 receiver and D-PHY, Video Input Controller (VIC), Tiziano ISP + core, and 3-channel MSCA scaler/DMA output engine. + +properties: + compatible: + const: ingenic,t23-isp + + reg: + items: + - description: ISP core and MSCA registers + - description: Video Input Controller (VIC) registers + - description: MIPI CSI-2 controller registers + - description: MIPI D-PHY registers + - description: IVDC frame-sharing registers + + reg-names: + items: + - const: core + - const: vic + - const: csi + - const: phy + - const: ivdc + + interrupts: + items: + - description: ISP core and MSCA interrupt + - description: VIC capture interrupt + - description: IVDC interrupt + minItems: 2 + + interrupt-names: + items: + - const: core + - const: vic + - const: ivdc + minItems: 2 + + clocks: + items: + - description: ISP divider clock + - description: ISP bus gate clock + - description: CSI gate clock + - description: VPU/IVDC divider clock + - description: IVDC gate clock + + clock-names: + items: + - const: cgu_isp + - const: isp + - const: csi + - const: cgu_vpu + - const: ivdc + + resets: + maxItems: 1 + + reset-names: + items: + - const: isp + + memory-region: + maxItems: 1 + + ingenic,frame-sync-source-port: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + description: + Sensor port index driving dual-sensor TDM frame synchronization. + + ports: + $ref: /schemas/graph.yaml#/properties/ports + + properties: + port@0: + $ref: /schemas/graph.yaml#/$defs/port-base + unevaluatedProperties: false + description: Primary MIPI CSI-2 / DVP sensor input port. + + properties: + endpoint: + $ref: /schemas/media/video-interfaces.yaml# + unevaluatedProperties: false + + port@1: + $ref: /schemas/graph.yaml#/$defs/port-base + unevaluatedProperties: false + description: Secondary TDM MIPI CSI-2 sensor input port. + + properties: + endpoint: + $ref: /schemas/media/video-interfaces.yaml# + unevaluatedProperties: false + +required: + - compatible + - reg + - reg-names + - interrupts + - interrupt-names + - clocks + - clock-names + - resets + - reset-names + +additionalProperties: false + +examples: + - | + #include + #include + + isp@13300000 { + compatible = "ingenic,t23-isp"; + reg = <0x13300000 0x90000>, + <0x133e0000 0x10000>, + <0x10023000 0x1000>, + <0x10022000 0x1000>, + <0x13200000 0x10000>; + reg-names = "core", "vic", "csi", "phy", "ivdc"; + interrupt-parent = <&intc>; + interrupts = <29>, <30>, <38>; + interrupt-names = "core", "vic", "ivdc"; + clocks = <&cgu CLK_CGU_ISP>, <&cgu CLK_ISP>, + <&cgu CLK_CSI>, <&cgu CLK_CGU_VPU>, + <&cgu CLK_IVDC>; + clock-names = "cgu_isp", "isp", "csi", "cgu_vpu", "ivdc"; + resets = <&cpm_reset T23_RESET_ISP>; + reset-names = "isp"; + }; From 89c83c006802c4ef243529c8c85570de5e3f5e5e Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 06:45:10 -0800 Subject: [PATCH 65/78] media: platform: ingenic: Add T23 CSI, VIC, ISP and MSCA capture Add the T23 CSI, VIC, ISP, MDNS and multi-scaler capture pipeline with Media Controller topology, NV12/NV21 outputs, per-frame statistics and parameter metadata, scratch-buffer parking and dual-input frame synchronization. Signed-off-by: Difan Zhang --- drivers/media/platform/ingenic/Kconfig | 2 + drivers/media/platform/ingenic/Makefile | 1 + drivers/media/platform/ingenic/tx-isp/Kconfig | 20 + .../media/platform/ingenic/tx-isp/Makefile | 9 + .../platform/ingenic/tx-isp/tx-isp-common.h | 25 + .../platform/ingenic/tx-isp/tx-isp-core.c | 744 +++++ .../platform/ingenic/tx-isp/tx-isp-csi.c | 168 + .../platform/ingenic/tx-isp/tx-isp-device.h | 207 ++ .../platform/ingenic/tx-isp/tx-isp-driver.c | 403 +++ .../ingenic/tx-isp/tx-isp-interrupt.c | 103 + .../platform/ingenic/tx-isp/tx-isp-mdns.c | 732 +++++ .../platform/ingenic/tx-isp/tx-isp-mdns.h | 62 + .../platform/ingenic/tx-isp/tx-isp-meta.c | 482 +++ .../platform/ingenic/tx-isp/tx-isp-meta.h | 68 + .../platform/ingenic/tx-isp/tx-isp-motion.c | 255 ++ .../platform/ingenic/tx-isp/tx-isp-motion.h | 39 + .../platform/ingenic/tx-isp/tx-isp-output.c | 742 +++++ .../platform/ingenic/tx-isp/tx-isp-overlay.c | 678 ++++ .../platform/ingenic/tx-isp/tx-isp-overlay.h | 82 + .../platform/ingenic/tx-isp/tx-isp-params.c | 465 +++ .../platform/ingenic/tx-isp/tx-isp-params.h | 51 + .../platform/ingenic/tx-isp/tx-isp-profile.c | 2069 ++++++++++++ .../platform/ingenic/tx-isp/tx-isp-profile.h | 13 + .../platform/ingenic/tx-isp/tx-isp-regs.h | 699 +++++ .../platform/ingenic/tx-isp/tx-isp-stats.c | 174 ++ .../platform/ingenic/tx-isp/tx-isp-stats.h | 24 + .../platform/ingenic/tx-isp/tx-isp-vic.c | 271 ++ .../platform/ingenic/tx-isp/tx-isp-video.c | 2773 +++++++++++++++++ 28 files changed, 11361 insertions(+) create mode 100644 drivers/media/platform/ingenic/tx-isp/Kconfig create mode 100644 drivers/media/platform/ingenic/tx-isp/Makefile create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-common.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-core.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-csi.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-device.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-driver.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-interrupt.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-meta.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-meta.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-motion.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-motion.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-output.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-params.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-params.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-profile.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-profile.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-regs.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-stats.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-stats.h create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-vic.c create mode 100644 drivers/media/platform/ingenic/tx-isp/tx-isp-video.c diff --git a/drivers/media/platform/ingenic/Kconfig b/drivers/media/platform/ingenic/Kconfig index 15b63a3592..5e856e84bb 100644 --- a/drivers/media/platform/ingenic/Kconfig +++ b/drivers/media/platform/ingenic/Kconfig @@ -18,3 +18,5 @@ config VIDEO_INGENIC_PTZ_MOTOR To compile this driver as a module, choose M here: the module will be called ptz-motor. + +source "drivers/media/platform/ingenic/tx-isp/Kconfig" diff --git a/drivers/media/platform/ingenic/Makefile b/drivers/media/platform/ingenic/Makefile index a6d9d0a54f..5345fb21af 100644 --- a/drivers/media/platform/ingenic/Makefile +++ b/drivers/media/platform/ingenic/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_VIDEO_INGENIC_PTZ_MOTOR) += ptz-motor.o +obj-$(CONFIG_VIDEO_INGENIC_ISP) += tx-isp/ diff --git a/drivers/media/platform/ingenic/tx-isp/Kconfig b/drivers/media/platform/ingenic/tx-isp/Kconfig new file mode 100644 index 0000000000..a8c55bf7fa --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/Kconfig @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +config VIDEO_INGENIC_ISP + tristate "Ingenic T23 Image Signal Processor (ISP) driver" + depends on V4L_PLATFORM_DRIVERS + depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API && MEDIA_CONTROLLER + depends on MACH_INGENIC || COMPILE_TEST + depends on OF && HAS_DMA + select VIDEOBUF2_V4L2 + select VIDEOBUF2_DMA_CONTIG + select VIDEOBUF2_VMALLOC + select V4L2_FWNODE + help + Say yes or m here to build V4L2 support for the Ingenic T23 VIC, + CSI-2 receiver, ISP core, MSCA scaler, motion detector, and OSD + overlay pipeline. + + To compile this driver as a module, choose M here: the module + will be called tx-isp. diff --git a/drivers/media/platform/ingenic/tx-isp/Makefile b/drivers/media/platform/ingenic/tx-isp/Makefile new file mode 100644 index 0000000000..aa2cf6f8aa --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +obj-$(CONFIG_VIDEO_INGENIC_ISP) += tx-isp.o +tx-isp-y := tx-isp-interrupt.o tx-isp-vic.o tx-isp-csi.o \ + tx-isp-core.o tx-isp-output.o tx-isp-stats.o \ + tx-isp-mdns.o tx-isp-profile.o tx-isp-params.o \ + tx-isp-meta.o tx-isp-motion.o tx-isp-overlay.o \ + tx-isp-video.o tx-isp-driver.o diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-common.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-common.h new file mode 100644 index 0000000000..e9c2a726c0 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-common.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_COMMON_H__ +#define __TX_ISP_COMMON_H__ + +#include +#include +struct tx_isp_subdev; + +struct tx_isp_subdev_ops { + irqreturn_t (*interrupt_service_routine)(struct tx_isp_subdev *sd, + bool *handled); +}; + +struct tx_isp_subdev { + struct resource *res; + void __iomem *base; + struct tx_isp_subdev_ops *ops; + void *dev_priv; +}; + +int tx_isp_subdev_init(struct platform_device *pdev, struct tx_isp_subdev *sd, + struct tx_isp_subdev_ops *ops); +void tx_isp_subdev_deinit(struct tx_isp_subdev *sd); +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-core.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-core.c new file mode 100644 index 0000000000..ee963ef54b --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-core.c @@ -0,0 +1,744 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 image signal processor + * + * Copyright (C) 2017-2023 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tx-isp-device.h" +#include "tx-isp-mdns.h" +#include "tx-isp-profile.h" + +static int mdns = -1; +module_param(mdns, int, 0644); +MODULE_PARM_DESC(mdns, "Temporal noise reduction: -1=auto (<=720p), 0=bypass, 1=enable"); + +static struct tx_isp_core_device *g_core_dev; +static u32 core_irq_seen; + +static int t23_mbus_to_bayer_order(u32 code, u32 *order) +{ + switch (code) { + case MEDIA_BUS_FMT_SRGGB10_1X10: + *order = ISP_TOP_BAYER_RGGB; + return 0; + case MEDIA_BUS_FMT_SBGGR10_1X10: + *order = ISP_TOP_BAYER_BGGR; + return 0; + case MEDIA_BUS_FMT_SGBRG10_1X10: + *order = ISP_TOP_BAYER_GBRG; + return 0; + case MEDIA_BUS_FMT_SGRBG10_1X10: + *order = ISP_TOP_BAYER_GRBG; + return 0; + default: + return -EINVAL; + } +} + +static void core_mdns_release(struct tx_isp_core_device *core) +{ + unsigned int i; + + /* The caller has completed the bus-stop/reset handshake. */ + for (i = 0; i < ARRAY_SIZE(core->history); i++) { + tx_isp_mdns_frame_done(&core->history[i], true); + tx_isp_mdns_free(&core->history[i]); + } +} + +int tx_isp_core_mdns_start(struct tx_isp_subdev *sd, unsigned int input) +{ + struct tx_isp_core_device *core = sd->dev_priv; + unsigned int i; + int ret; + + if (!core->mdns_enabled) + return 0; + if (input >= ARRAY_SIZE(core->history)) + return -EINVAL; + if (!core->history[input].cpu) + return 0; + for (i = 0; i < ARRAY_SIZE(core->history); i++) + if (core->history[i].active) + return -EBUSY; + ret = tx_isp_mdns_start(&core->history[input], sd->base, + TX_ISP_MDNS_SC1A4T_LINEAR_DAY, 0, + core->params[input].valid & TX_ISP_PARAM_MDNS ? + &core->params[input].cfg.mdns : NULL); + if (!ret) + core->mdns_input = input; + return ret; +} + +void tx_isp_core_queue_ext_params(struct tx_isp_subdev *sd, unsigned int input, + const struct t23_isp_ext_params_cfg *ext, + size_t payload) +{ + struct tx_isp_core_device *core = sd->dev_priv; + + if (input < ARRAY_SIZE(core->history)) + tx_isp_ext_params_merge(&core->params[input], ext, payload); +} + +void tx_isp_core_reset_params(struct tx_isp_subdev *sd, unsigned int input) +{ + struct tx_isp_core_device *core = sd->dev_priv; + + if (input < ARRAY_SIZE(core->history)) { + core->params[input].valid = 0; + core->params[input].dirty = 0; + core->params[input].blc_valid = false; + core->params[input].blc_dirty = false; + } +} + +/* The two inputs share the core, so switching input flushes all active blocks. */ +void tx_isp_core_apply_params(struct tx_isp_subdev *sd, unsigned int input) +{ + struct tx_isp_core_device *core = sd->dev_priv; + u32 valid, dropped; + + if (input >= ARRAY_SIZE(core->history)) + return; + + valid = core->params[input].valid; + dropped = core->hw_custom_mask & ~valid; + if (dropped) { + if (dropped & TX_ISP_PARAM_DMSC) { + tx_isp_core_restore_dmsc(sd); + writel(ISP_DMSC_REFRESH_COMMIT, + sd->base + ISP_DMSC_TRIGGER); + } + tx_isp_profile_restore_blocks(sd->base, + sd->res ? resource_size(sd->res) : 0, + dropped); + core->hw_custom_mask &= ~dropped; + } + if (core->last_params_input != (int)input) { + core->params[input].dirty = valid; + core->params[input].blc_dirty = core->params[input].blc_valid; + } + tx_isp_params_write(sd, &core->params[input]); + core->hw_custom_mask |= valid; + core->last_params_input = input; +} + +void tx_isp_core_mdns_frame_done(struct tx_isp_subdev *sd) +{ + struct tx_isp_core_device *core = sd->dev_priv; + + if (core->mdns_enabled) + tx_isp_mdns_frame_done(&core->history[core->mdns_input], false); +} + +static int core_stream_on(struct tx_isp_subdev *sd) +{ + struct tx_isp_core_device *core = sd ? sd->dev_priv : NULL; + u32 width, height, inputs; + unsigned int i; + bool want_mdns; + int ret; + + if (!core) + return -EINVAL; + if (core->running) + return 0; + core_irq_seen = 0; + width = core->geometry >> 16; + height = core->geometry & 0xffff; + inputs = core->active_inputs ? core->active_inputs : BIT(0); + if (g_tx_isp_dev->processed && + (!sd->res || resource_size(sd->res) < 0x80180)) + return -EINVAL; + if (mdns == 0) + want_mdns = false; + else if (mdns > 0) + want_mdns = true; + else + want_mdns = (width * height <= 1280U * 720U); + core->mdns_enabled = false; + if (g_tx_isp_dev->processed && want_mdns) { + core->mdns_enabled = true; + for (i = 0; i < ARRAY_SIZE(core->history); i++) { + if (!(inputs & BIT(i))) + continue; + ret = tx_isp_mdns_alloc(&g_tx_isp_dev->pdev->dev, + &core->history[i], width, height); + if (ret == -ENOMEM) { + dev_warn(&g_tx_isp_dev->pdev->dev, + "CMA tight; bypassing MDNS history\n"); + core_mdns_release(core); + core->mdns_enabled = false; + break; + } + if (ret) { + core_mdns_release(core); + core->mdns_enabled = false; + return ret; + } + } + } + core->input_control = readl(sd->base + ISP_IPC_MODE); + /* Temporal filtering requires history buffers; the VIC path has none. */ + writel(ISP_MDNS_FUNC_CTRL_BYPASS, sd->base + ISP_MDNS_FUNC_CONTROL); + writel(ISP_MDNS_FUNC_GROUP_DEFAULT, sd->base + ISP_MDNS_FUNC_GROUP); + writel(0, sd->base + ISP_MDNS_DEBUG_CONTROL); + writel(ISP_MDNS_REFRESH_VAL, sd->base + ISP_MDNS_FUNC_REFRESH); + writel(ISP_MDNS_TRIGGER_START, sd->base + ISP_MDNS_REG_TRIGGER); + writel(core->geometry, sd->base + ISP_TOP_IMAGE_SIZE); + writel(core->bayer_order, sd->base + ISP_TOP_BAYER_ORDER); + writel((readl(sd->base + ISP_TOP_CTRL) & ~BIT(23)) | ISP_TOP_CTRL_ENABLE, + sd->base + ISP_TOP_CTRL); + if (g_tx_isp_dev->processed) { + writel(ISP_TOP_REG_1C_DEFAULT, sd->base + ISP_TOP_REG_1C); + writel(ISP_TOP_REG_2C_DEFAULT, sd->base + ISP_TOP_REG_2C); + writel(ISP_TOP_REG_10_DEFAULT, sd->base + ISP_TOP_REG_10); + writel(ISP_TOP_REG_30_DEFAULT, sd->base + ISP_TOP_REG_30); + writel(ISP_TOP_CTRL_DEFAULT, sd->base + ISP_TOP_CTRL); + writel(ISP_TOP_REG_33C_DEFAULT, sd->base + ISP_TOP_REG_33C); + /* Post output shadow before ISP trigger runs. */ + ret = tx_isp_core_output_init(sd); + if (ret) + return ret; + writel(ISP_IPC_MODE_DEFAULT, sd->base + ISP_IPC_MODE); + writel(ISP_IPC_TRIGGER_START, sd->base + ISP_IPC_TRIGGER); + ret = tx_isp_profile_init(sd->base, resource_size(sd->res), + core->geometry >> 16, + core->geometry & 0xffff); + if (ret) + return ret; + core->hw_custom_mask = 0; + core->last_params_input = -1; + ret = tx_isp_mdns_configure_bypassed(sd->base, + core->geometry >> 16, core->geometry & 0xffff, + TX_ISP_MDNS_SC1A4T_LINEAR_DAY, 0); + if (ret) + return ret; + ret = t23_isp_stats_configure(&core->stats, sd->base, 0); + if (ret) + return ret; + writel(U32_MAX, sd->base + ISP_INT_CLEAR); + writel(ISP_INT_MASK_ALL, sd->base + ISP_INT_MASK); + writel(ISP_INT1_BIT15, sd->base + ISP_INT1_MASK); + } + if (!g_tx_isp_dev->processed) { + writel(ISP_IPC_MODE_DEFAULT, sd->base + ISP_IPC_MODE); + writel(ISP_IPC_TRIGGER_START, sd->base + ISP_IPC_TRIGGER); + writel(ISP_IPC_MODE_DEFAULT | ISP_IPC_MODE_PORT1, + sd->base + ISP_IPC_MODE); + } + core->running = true; + return 0; +} + +static int core_stream_off(struct tx_isp_subdev *sd) +{ + struct tx_isp_core_device *core = sd ? sd->dev_priv : NULL; + bool was_running; + int ret; + + if (!core) + return -EINVAL; + was_running = core->running; + if (core->running) { + writel(ISP_IPC_TRIGGER_STOP, sd->base + ISP_IPC_TRIGGER); + writel(0, sd->base + ISP_INT_MASK); + writel(0, sd->base + ISP_INT1_MASK); + writel(1, sd->base + MSCA_Y_RESET(0)); + writel(1, sd->base + MSCA_UV_RESET(0)); + writel(0, sd->base + MSCA_CH_EN_MASK); + writel(1, sd->base + MSCA_SHD_TRIGGER); + writel(core->input_control, sd->base + ISP_IPC_MODE); + } + core->running = false; + if (was_running || core->stats.active || core->history[0].cpu || + core->history[1].cpu || + g_tx_isp_dev->dma_quiescence_failed) { + ret = reset_control_reset(g_tx_isp_dev->reset); + if (ret) { + g_tx_isp_dev->dma_quiescence_failed = true; + dev_err(&g_tx_isp_dev->pdev->dev, + "ISP bus stop failed: %d\n", ret); + return ret; + } + core->stats.active = false; + core_mdns_release(core); + g_tx_isp_dev->dma_quiescence_failed = false; + } + return 0; +} + +static int core_s_stream(struct tx_isp_subdev *sd, int enable) +{ + if (!sd) + return -EINVAL; + return enable ? core_stream_on(sd) : core_stream_off(sd); +} + +int tx_isp_core_s_stream(struct tx_isp_subdev *sd, int enable) +{ + return core_s_stream(sd, enable); +} + +static const u16 gamma_t70[129] = { + 0, 175, 275, 358, 431, 499, 561, 620, + 677, 730, 782, 832, 880, 927, 973, 1018, + 1061, 1104, 1146, 1187, 1227, 1266, 1305, 1343, + 1381, 1418, 1455, 1491, 1526, 1562, 1596, 1631, + 1665, 1698, 1731, 1764, 1797, 1829, 1861, 1893, + 1924, 1955, 1986, 2017, 2047, 2077, 2107, 2137, + 2166, 2195, 2224, 2253, 2282, 2310, 2338, 2366, + 2394, 2422, 2449, 2477, 2504, 2531, 2558, 2584, + 2611, 2637, 2664, 2690, 2716, 2742, 2767, 2793, + 2818, 2844, 2869, 2894, 2919, 2944, 2969, 2993, + 3018, 3042, 3067, 3091, 3115, 3139, 3163, 3187, + 3211, 3234, 3258, 3281, 3305, 3328, 3351, 3374, + 3397, 3420, 3443, 3466, 3488, 3511, 3534, 3556, + 3578, 3601, 3623, 3645, 3667, 3689, 3711, 3733, + 3755, 3777, 3798, 3820, 3841, 3863, 3884, 3906, + 3927, 3948, 3969, 3990, 4011, 4032, 4053, 4074, + 4095, +}; + +static const u16 gamma_t100[129] = { + 0, 451, 618, 744, 847, 938, 1019, 1093, + 1161, 1225, 1285, 1342, 1396, 1448, 1498, 1545, + 1591, 1636, 1679, 1721, 1761, 1801, 1839, 1877, + 1913, 1949, 1984, 2019, 2052, 2085, 2118, 2149, + 2181, 2211, 2242, 2271, 2301, 2329, 2358, 2386, + 2413, 2441, 2468, 2494, 2520, 2546, 2572, 2597, + 2622, 2647, 2671, 2695, 2719, 2743, 2766, 2789, + 2812, 2835, 2858, 2880, 2902, 2924, 2945, 2967, + 2988, 3009, 3030, 3051, 3072, 3092, 3113, 3133, + 3153, 3172, 3192, 3212, 3231, 3250, 3269, 3288, + 3307, 3326, 3345, 3363, 3381, 3400, 3418, 3436, + 3454, 3472, 3489, 3507, 3524, 3542, 3559, 3576, + 3593, 3610, 3627, 3644, 3660, 3677, 3693, 3710, + 3726, 3742, 3759, 3775, 3791, 3807, 3822, 3838, + 3854, 3869, 3885, 3900, 3916, 3931, 3946, 3962, + 3977, 3992, 4007, 4021, 4036, 4051, 4066, 4080, + 4095, +}; + +static const u16 gamma_t180[129] = { + 0, 1203, 1433, 1587, 1707, 1806, 1891, 1966, + 2033, 2095, 2151, 2203, 2252, 2298, 2342, 2383, + 2422, 2460, 2495, 2530, 2563, 2594, 2625, 2655, + 2683, 2711, 2738, 2764, 2790, 2815, 2839, 2862, + 2885, 2908, 2930, 2952, 2973, 2993, 3013, 3033, + 3053, 3072, 3091, 3109, 3127, 3145, 3162, 3180, + 3197, 3213, 3230, 3246, 3262, 3278, 3293, 3308, + 3323, 3338, 3353, 3368, 3382, 3396, 3410, 3424, + 3437, 3451, 3464, 3477, 3490, 3503, 3516, 3529, + 3541, 3554, 3566, 3578, 3590, 3602, 3614, 3625, + 3637, 3648, 3659, 3671, 3682, 3693, 3704, 3715, + 3725, 3736, 3747, 3757, 3767, 3778, 3788, 3798, + 3808, 3818, 3828, 3838, 3848, 3857, 3867, 3876, + 3886, 3895, 3905, 3914, 3923, 3932, 3941, 3950, + 3959, 3968, 3977, 3986, 3994, 4003, 4012, 4020, + 4029, 4037, 4046, 4054, 4062, 4071, 4079, 4087, + 4095, +}; + +const s16 tx_isp_default_ccm[9] = { + 2104, -748, -329, + -238, 1583, -320, + 76, -1041, 1988 +}; + +void tx_isp_core_set_blc_default(struct tx_isp_subdev *sd) +{ + if (!sd || !sd->base) + return; + + writel(ISP_DGAIN_TRIGGER_COMMIT, sd->base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_BLC_ONE, sd->base + ISP_DGAIN_BLC0); + writel(ISP_DGAIN_TRIGGER_COMMIT, sd->base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_BLC_PAIR_ONE, sd->base + ISP_DGAIN_BLC1); + writel(ISP_DGAIN_TRIGGER_COMMIT, sd->base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_BLC_ONE, sd->base + ISP_DGAIN_BLC2); +} + +void tx_isp_core_set_dgain(struct tx_isp_subdev *sd, u32 dgain_q10) +{ + u32 g = clamp_val(dgain_q10, 256U, 16383U); + u32 pair = g | (g << 16); + + if (!sd || !sd->base) + return; + + writel(ISP_DGAIN_TRIGGER_COMMIT, sd->base + ISP_DGAIN_TRIGGER); + writel(pair, sd->base + ISP_DGAIN_GAIN01); + writel(ISP_DGAIN_TRIGGER_COMMIT, sd->base + ISP_DGAIN_TRIGGER); + writel(pair, sd->base + ISP_DGAIN_GAIN23); +} + +void tx_isp_core_set_wb(struct tx_isp_subdev *sd, u32 r_gain_q8, u32 b_gain_q8) +{ + u32 r_gain_q10 = ISP_WB_GAIN_TAG | min_t(u64, (u64)r_gain_q8 * 4, 0x3fff); + u32 b_gain_q10 = ISP_WB_GAIN_TAG | min_t(u64, (u64)b_gain_q8 * 4, 0x3fff); + + if (!sd || !sd->base) + return; + + writel(ISP_WB_TRIGGER_COMMIT, sd->base + ISP_WB_TRIGGER); + writel(r_gain_q10, sd->base + ISP_WB_GAIN_R); + writel(ISP_WB_TRIGGER_COMMIT, sd->base + ISP_WB_TRIGGER); + writel(b_gain_q10, sd->base + ISP_WB_GAIN_B); + writel(ISP_WB_TRIGGER_COMMIT, sd->base + ISP_WB_TRIGGER); + writel(r_gain_q10, sd->base + ISP_WB_GAIN_GR); + writel(ISP_WB_TRIGGER_COMMIT, sd->base + ISP_WB_TRIGGER); + writel(b_gain_q10, sd->base + ISP_WB_GAIN_GB); +} + +u16 tx_isp_calc_gamma_entry(int j, u32 g) +{ + if (j <= 0) + return 0; + if (j >= 128) + return 4095; + if (g <= 100) { + if (g <= 70) + return gamma_t70[j]; + return gamma_t70[j] + ((gamma_t100[j] - gamma_t70[j]) * (g - 70)) / 30; + } else { + if (g >= 180) + return gamma_t180[j]; + return gamma_t100[j] + ((gamma_t180[j] - gamma_t100[j]) * (g - 100)) / 80; + } +} + +int tx_isp_core_set_gamma(struct tx_isp_subdev *sd, u32 gamma_val) +{ + u32 value; + unsigned int j; + + if (!sd || !sd->base) + return -EINVAL; + if (!sd->res || resource_size(sd->res) < 0x50200) + return -EOPNOTSUPP; + + for (j = 0; j < 128; j++) { + value = tx_isp_calc_gamma_entry(j, gamma_val) | + (u32)tx_isp_calc_gamma_entry(j + 1, gamma_val) << 12; + writel(value, sd->base + ISP_GAMMA_LUT_R + j * 4); + writel(value, sd->base + ISP_GAMMA_LUT_G + j * 4); + writel(value, sd->base + ISP_GAMMA_LUT_B + j * 4); + } + return 0; +} + +void tx_isp_core_set_ccm(struct tx_isp_subdev *sd, const s16 ccm[9]) +{ + u32 value; + unsigned int i; + + if (!sd || !sd->base) + return; + if (!ccm) + ccm = tx_isp_default_ccm; + + for (i = 0; i < 9; i += 2) { + value = ccm[i] & 0x3fff; + if (i + 1 < 9) + value |= (ccm[i + 1] & 0x3fff) << 16; + writel(ISP_CCM_TRIGGER_STROBE, sd->base + ISP_CCM_TRIGGER); + writel(value, sd->base + ISP_CCM_COEFF_BASE + i * 2); + } +} + +static u32 dmsc_sharpness_scale(u32 base, u32 strength, u32 maximum) +{ + base = min(base, maximum); + if (strength <= 128) + return base * strength >> 7; + return base + ((maximum - base) * (strength - 128) >> 7); +} + +void tx_isp_core_set_sharpness(struct tx_isp_subdev *sd, u32 sharpness) +{ + u32 defaults[3], value; + + if (!sd || !sd->base) + return; + sharpness = min(sharpness, 255U); + tx_isp_core_dmsc_defaults(defaults); + value = (defaults[0] & 0xffff0000) | + dmsc_sharpness_scale(defaults[0] & 0xffff, sharpness, 0xfff); + writel(value, sd->base + ISP_DMSC_REG_08); + value = dmsc_sharpness_scale(defaults[1] & 0xffff, sharpness, 0x3ff) | + dmsc_sharpness_scale(defaults[1] >> 16, sharpness, 0x3ff) << 16; + writel(value, sd->base + ISP_DMSC_REG_48); + value = dmsc_sharpness_scale(defaults[2] & 0xffff, sharpness, 0xfff) | + dmsc_sharpness_scale(defaults[2] >> 16, sharpness, 0xfff) << 16; + writel(value, sd->base + ISP_DMSC_REG_60); +} + +int tx_isp_core_init_hardware(struct tx_isp_subdev *sd) +{ + if (!sd || !sd->base) + return -EINVAL; + + tx_isp_core_set_wb(sd, 256, 256); + tx_isp_core_set_ccm(sd, tx_isp_default_ccm); + return 0; +} + +static irqreturn_t core_interrupt_service_routine(struct tx_isp_subdev *sd, + bool *handled) +{ + u32 stat, all_done; + bool dma_done = false, error = false; + + if (!sd || !sd->base) + return IRQ_NONE; + + all_done = readl(sd->base + ISP_INT1_STATUS); + stat = readl(sd->base + ISP_INT_STATUS); + dev_dbg(&g_tx_isp_dev->pdev->dev, + "t23 ISP interrupt: %#x all_done=%#x\n", stat, all_done); + if ((stat & ISP_INT_ERR_MASK) && g_tx_isp_dev->processed) { + if (!(core_irq_seen & ISP_INT_ERR_MASK)) { + void __iomem *vic = + g_tx_isp_dev->subdevs[TX_ISP_SUBDEV_VIC]->base; + struct device *dev = &g_tx_isp_dev->pdev->dev; + + dev_dbg(dev, "t23 BCSH: %08x %08x %08x %08x %08x %08x\n", + readl(sd->base + ISP_BCSH_REG_00), readl(sd->base + ISP_BCSH_REG_18), + readl(sd->base + ISP_BCSH_REG_38), readl(sd->base + ISP_BCSH_REG_54), + readl(sd->base + ISP_BCSH_REG_64), readl(sd->base + ISP_BCSH_REG_70)); + dev_dbg(dev, "t23 MDNS: %08x %08x %08x %08x\n", + readl(sd->base + ISP_MDNS_FUNC_CONTROL), readl(sd->base + ISP_MDNS_FUNC_GROUP), + readl(sd->base + ISP_MDNS_DEBUG_CONTROL), readl(sd->base + ISP_MDNS_FUNC_REFRESH)); + + dev_dbg(dev, "t23 ISP first fault: prior=%08x stat=%08x done=%08x id=%08x geom=%08x top=%08x gate=%08x/%08x\n", + core_irq_seen, stat, all_done, + readl(sd->base + ISP_TOP_REG_00), readl(sd->base + ISP_TOP_IMAGE_SIZE), + readl(sd->base + ISP_TOP_CTRL), readl(sd->base + ISP_TOP_REG_10), + readl(sd->base + ISP_TOP_REG_30)); + dev_dbg(dev, "t23 ISP first fault: run=%08x mode=%08x input=%08x msca_en=%08x msca_mode=%08x geom=%08x/%08x/%08x\n", + readl(sd->base + ISP_IPC_TRIGGER), readl(sd->base + ISP_IPC_MODE), + readl(sd->base + ISP_IPC_INPUT_STAT), readl(sd->base + MSCA_CH_EN_MASK), + readl(sd->base + MSCA_REG_060), readl(sd->base + MSCA_FCROP_SIZE), + readl(sd->base + MSCA_CH_SCALE_SIZE(0)), readl(sd->base + MSCA_CH_CROP_SIZE(0))); + dev_dbg(dev, "t23 ISP first fault: queue=%08x/%08x fifo=%08x/%08x vic=%08x/%08x/%08x ivdc=%08x\n", + readl(sd->base + MSCA_CH_REG_34(0)), readl(sd->base + MSCA_CH_REG_54(0)), + readl(sd->base + MSCA_Y_DONE_EMPTY(0)), readl(sd->base + MSCA_UV_DONE_EMPTY(0)), + readl(vic + VIC_STATUS_REG_0), readl(vic + VIC_STATUS_REG_1), + readl(vic + VIC_PORT_STATUS), + readl(g_tx_isp_dev->ivdc_base + IVDC_TOP_INFO1)); + dev_dbg(dev, "t23 ISP first fault: ivdc_status=%08x lines=%08x info=%08x/%08x stride=%08x/%08x reg50=%08x ctl=%08x\n", + readl(g_tx_isp_dev->ivdc_base + IVDC_INTC_STATE), + readl(g_tx_isp_dev->ivdc_base + IVDC_MAX_DDR_LINE), + readl(g_tx_isp_dev->ivdc_base + IVDC_TOP_INFO), + readl(g_tx_isp_dev->ivdc_base + IVDC_ISP_INFO), + readl(sd->base + MSCA_Y_STRIDE(0)), readl(sd->base + MSCA_UV_STRIDE(0)), + readl(sd->base + MSCA_REG_050), readl(sd->base + MSCA_SCALE_CTL_MODE)); + } + { + u32 input_reg = readl(sd->base + ISP_IPC_INPUT_STAT); + + dev_err_ratelimited(&g_tx_isp_dev->pdev->dev, + "ISP pipeline error: %#x, input %#x\n", + stat, input_reg); + } + error = true; + } else if ((stat & (ISP_INT_MSCA_CH0_DONE | ISP_INT_MSCA_CH1_DONE | + ISP_INT_MSCA_CH2_DONE)) && + g_tx_isp_dev->processed) { + struct tx_isp_core_device *core = sd->dev_priv; + unsigned int ch; + u32 y, uv; + + /* Status bit n: MSCA channel n finished a frame. */ + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + if (!(stat & BIT(ch)) || !(core->armed_mask & BIT(ch))) + continue; + if ((readl(sd->base + MSCA_Y_DONE_EMPTY(ch)) & MSCA_DONE_EMPTY) || + (readl(sd->base + MSCA_UV_DONE_EMPTY(ch)) & MSCA_DONE_EMPTY)) { + error = true; + break; + } + y = readl(sd->base + MSCA_Y_DONE(ch)) & MSCA_DONE_ADDR; + uv = readl(sd->base + MSCA_UV_DONE(ch)) & MSCA_DONE_ADDR; + if (y != core->armed[ch].y || uv != core->armed[ch].uv) { + error = true; + break; + } + core->done_mask |= BIT(ch); + } + dma_done = core->armed_mask && + core->done_mask == core->armed_mask; + } + core_irq_seen |= stat; + if (stat & ISP_CORE_STATS_IRQS) { + struct tx_isp_core_device *core = sd->dev_priv; + + core->stats_irq |= stat & ISP_CORE_STATS_IRQS; + } + if (all_done) + writel(all_done, sd->base + ISP_INT1_CLEAR); + if (stat) + writel(stat, sd->base + ISP_INT_CLEAR); + + if (g_tx_isp_dev->processed) + t23_isp_video_processed_event(g_tx_isp_dev, dma_done, + all_done & ISP_INT1_BIT15, error); + + if (!stat && !all_done) + return IRQ_NONE; + + + if (handled) + *handled = true; + return IRQ_HANDLED; +} + +static struct tx_isp_subdev_ops core_subdev_ops = { + .interrupt_service_routine = core_interrupt_service_routine, +}; + +int t23_isp_configure_input(struct tx_isp_device *isp, + struct v4l2_subdev *sensor, unsigned int port, + u32 active_inputs) +{ + struct v4l2_subdev_format fmt = { .which = V4L2_SUBDEV_FORMAT_ACTIVE }; + struct v4l2_mbus_config bus = { }; + struct v4l2_ctrl *link_freq; + struct tx_isp_subdev *core_sd; + struct tx_isp_core_device *core; + u32 bayer_order; + s64 frequency; + unsigned int lanes, pad = 0; + int ret; + + if (!isp || !sensor) + return -EINVAL; + core_sd = isp->subdevs[TX_ISP_SUBDEV_CORE]; + core = core_sd ? core_sd->dev_priv : NULL; + if (!core) + return -ENODEV; + for (pad = 0; pad < sensor->entity.num_pads; pad++) + if (sensor->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE) + break; + if (pad == sensor->entity.num_pads) + pad = 0; + fmt.pad = pad; + ret = v4l2_subdev_call_state_active(sensor, pad, get_fmt, &fmt); + if (ret) + return ret; + if (t23_mbus_to_bayer_order(fmt.format.code, &bayer_order) || + !fmt.format.width || fmt.format.width > 1920 || + !fmt.format.height || fmt.format.height > 1080 || + ((fmt.format.width | fmt.format.height) & 1)) + return -EINVAL; + ret = v4l2_subdev_call(sensor, pad, get_mbus_config, pad, &bus); + if (ret) + return ret; + if (bus.type != V4L2_MBUS_CSI2_DPHY) + return -EINVAL; + lanes = bus.bus.mipi_csi2.num_data_lanes; + if (lanes < 1 || lanes > 2) + return -EINVAL; + link_freq = v4l2_ctrl_find(sensor->ctrl_handler, V4L2_CID_LINK_FREQ); + if (!link_freq || link_freq->type != V4L2_CTRL_TYPE_INTEGER_MENU) + return -ENOLINK; + ret = v4l2_ctrl_g_ctrl(link_freq); + if (ret < 0 || ret > link_freq->maximum) + return -EINVAL; + frequency = link_freq->qmenu_int[ret]; + if (frequency < 40000000 || frequency >= 750000000) + return -EINVAL; + if (isp->streaming) + return -EBUSY; + ret = reset_control_reset(isp->reset); + if (ret) { + dev_err(&isp->pdev->dev, "ISP reset failed: %d\n", ret); + return ret; + } + core->stats.active = false; + core_mdns_release(core); + isp->dma_quiescence_failed = false; + ret = tx_isp_csi_configure(isp->subdevs[TX_ISP_SUBDEV_CSI], lanes, + div_s64(frequency * 2, 1000000)); + if (ret) + return ret; + ret = tx_isp_vic_configure(isp->subdevs[TX_ISP_SUBDEV_VIC], + fmt.format.width, fmt.format.height, port); + if (!ret) { + core->geometry = (fmt.format.width << 16) | fmt.format.height; + core->bayer_order = bayer_order; + core->active_inputs = active_inputs ? active_inputs : BIT(0); + } + return ret; +} + +int tx_isp_core_probe(struct platform_device *pdev) +{ + struct tx_isp_core_device *core; + int ret; + + core = kzalloc(sizeof(*core), GFP_KERNEL); + if (!core) + return -ENOMEM; + + ret = tx_isp_subdev_init(pdev, &core->sd, &core_subdev_ops); + if (ret) { + kfree(core); + return ret; + } + + core->sd.dev_priv = core; + core->last_params_input = -1; + /* MDNS stays in the processed chain and needs history buffers. */ + core->mdns_enabled = true; + core->params = vzalloc(array_size(ARRAY_SIZE(core->history), + sizeof(*core->params))); + if (!core->params) { + tx_isp_subdev_deinit(&core->sd); + kfree(core); + return -ENOMEM; + } + ret = t23_isp_stats_alloc(&core->stats, &g_tx_isp_dev->pdev->dev); + if (ret) { + vfree(core->params); + tx_isp_subdev_deinit(&core->sd); + kfree(core); + return ret; + } + g_core_dev = core; + + tx_isp_core_init_hardware(&core->sd); + + return 0; +} + +int tx_isp_core_remove(struct platform_device *pdev) +{ + struct tx_isp_core_device *core = g_core_dev; + + if (core) { + core_stream_off(&core->sd); + if (t23_isp_stats_free(&core->stats)) + dev_err(&pdev->dev, + "ISP statistics memory retained: DMA is not quiescent\n"); + tx_isp_subdev_deinit(&core->sd); + vfree(core->params); + kfree(core); + g_core_dev = NULL; + } + return 0; +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-csi.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-csi.c new file mode 100644 index 0000000000..ccd6584bcf --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-csi.c @@ -0,0 +1,168 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2017-2023 Ingenic Semiconductor Co., Ltd. */ +/* Copyright (C) 2026 Difan Zhang */ + +#include +#include +#include +#include + +#include "tx-isp-device.h" + +struct tx_isp_csi_device { + struct tx_isp_subdev sd; + void __iomem *phy; + unsigned int lanes; + unsigned int clock_mhz; + bool configured; + bool running; +}; + +static struct tx_isp_csi_device *g_csi_dev; + +int tx_isp_csi_configure(struct tx_isp_subdev *sd, unsigned int lanes, + unsigned int clock_mhz) +{ + struct tx_isp_csi_device *csi = sd ? sd->dev_priv : NULL; + + if (!csi) + return -EINVAL; + if (csi->running) + return -EBUSY; + csi->configured = false; + if (lanes < 1 || lanes > 2 || clock_mhz < 80 || clock_mhz >= 1500) + return -EINVAL; + csi->lanes = lanes; + csi->clock_mhz = clock_mhz; + csi->configured = true; + return 0; +} + +static u32 csi_settle_range(unsigned int clock) +{ + static const unsigned int upper[] = { + 110, 150, 200, 250, 300, 400, 500, 600, 700, 800, 1000, + }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(upper); i++) + if (clock < upper[i]) + return i; + return 11; +} + +void tx_isp_csi_select_rate(struct tx_isp_subdev *sd, unsigned int clock_mhz) +{ + struct tx_isp_csi_device *csi = sd->dev_priv; + u32 value; + + if (csi->clock_mhz == clock_mhz) + return; + value = (readl(csi->phy + DPHY_CLK_SETTLE) & ~0xfU) | + csi_settle_range(clock_mhz); + writel(value, csi->phy + DPHY_CLK_SETTLE); + writel(value, csi->phy + DPHY_DATA0_SETTLE); + writel(value, csi->phy + DPHY_DATA1_SETTLE); + readl(csi->phy + DPHY_DATA1_SETTLE); + csi->clock_mhz = clock_mhz; +} + +int csi_phy_start(struct tx_isp_subdev *sd) +{ + struct tx_isp_csi_device *csi = sd ? sd->dev_priv : NULL; + u32 value; + + if (!csi || !sd->base || !csi->phy) + return -EINVAL; + if (!csi->configured) + return -ENOLINK; + if (csi->running) + return 0; + writel(csi->lanes - 1, sd->base + CSI2_N_LANES); + writel(readl(sd->base + CSI2_PHY_SHUTDOWNZ) & ~CSI2_PHY_SHUTDOWNZ_BIT, + sd->base + CSI2_PHY_SHUTDOWNZ); + writel(0, sd->base + CSI2_DPHY_RSTZ); + msleep(1); + writel(readl(sd->base + CSI2_RESETN) & ~CSI2_RESETN_BIT, + sd->base + CSI2_RESETN); + msleep(1); + writel(CSI2_DPHY_RSTZ_BIT, sd->base + CSI2_DPHY_RSTZ); + msleep(1); + writel(CSI2_TIMEOUT_DEFAULT, sd->base + CSI2_TIMEOUT_REG); + value = (readl(csi->phy + DPHY_CLK_SETTLE) & ~DPHY_SETTLE_MASK) | + csi_settle_range(csi->clock_mhz); + writel(value, csi->phy + DPHY_CLK_SETTLE); + writel(value, csi->phy + DPHY_DATA0_SETTLE); + writel(value, csi->phy + DPHY_DATA1_SETTLE); + writel(DPHY_PWR_ON, csi->phy + DPHY_PWR_REG); + writel(DPHY_RST_RELEASE, csi->phy + DPHY_RST_REG); + writel(CSI2_RESETN_BIT, sd->base + CSI2_RESETN); + msleep(10); + csi->running = true; + return 0; +} + +int csi_phy_stop(struct tx_isp_subdev *sd) +{ + struct tx_isp_csi_device *csi = sd ? sd->dev_priv : NULL; + + if (!csi || !sd->base) + return -EINVAL; + if (!csi->running) + return 0; + writel(readl(sd->base + CSI2_PHY_SHUTDOWNZ) & ~CSI2_PHY_SHUTDOWNZ_BIT, + sd->base + CSI2_PHY_SHUTDOWNZ); + writel(readl(sd->base + CSI2_DPHY_RSTZ) & ~CSI2_DPHY_RSTZ_BIT, + sd->base + CSI2_DPHY_RSTZ); + writel(readl(sd->base + CSI2_RESETN) & ~CSI2_RESETN_BIT, + sd->base + CSI2_RESETN); + csi->running = false; + return 0; +} + +static struct tx_isp_subdev_ops csi_subdev_ops = {}; + +int tx_isp_csi_probe(struct platform_device *pdev) +{ + struct tx_isp_csi_device *csi; + struct resource *res; + int ret; + + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy"); + if (!res || resource_size(res) < 0x264) { + dev_err(&pdev->dev, "missing or invalid phy resource\n"); + return -EINVAL; + } + csi = kzalloc(sizeof(*csi), GFP_KERNEL); + if (!csi) + return -ENOMEM; + csi->phy = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(csi->phy)) { + ret = PTR_ERR(csi->phy); + dev_err(&pdev->dev, "failed to ioremap phy resource: %d\n", ret); + kfree(csi); + return ret; + } + ret = tx_isp_subdev_init(pdev, &csi->sd, &csi_subdev_ops); + if (ret) { + dev_err(&pdev->dev, "failed to initialize csi subdev: %d\n", ret); + kfree(csi); + return ret; + } + csi->sd.dev_priv = csi; + g_csi_dev = csi; + return 0; +} + +int tx_isp_csi_remove(struct platform_device *pdev) +{ + struct tx_isp_csi_device *csi = g_csi_dev; + + if (csi) { + csi_phy_stop(&csi->sd); + tx_isp_subdev_deinit(&csi->sd); + kfree(csi); + g_csi_dev = NULL; + } + return 0; +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-device.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-device.h new file mode 100644 index 0000000000..812ee1fed5 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-device.h @@ -0,0 +1,207 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_DEVICE_H__ +#define __TX_ISP_DEVICE_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tx-isp-common.h" +#include "tx-isp-regs.h" +#include "tx-isp-mdns.h" +#include "tx-isp-params.h" +#include "tx-isp-stats.h" + +enum { + TX_ISP_SUBDEV_VIC = 0, + TX_ISP_SUBDEV_CSI = 1, + TX_ISP_SUBDEV_CORE = 2, + TX_ISP_SUBDEV_MAX = 3, +}; + +struct isp_irq_binding { + struct tx_isp_device *owner; + struct tx_isp_subdev *sd; + int irq; + bool requested; + bool enabled; +}; + +struct tx_isp_device { + struct platform_device *pdev; + struct tx_isp_subdev *subdevs[TX_ISP_SUBDEV_MAX]; + unsigned int subdev_cnt; + struct isp_irq_binding bindings[TX_ISP_SUBDEV_MAX]; + bool streaming; + bool processed; + bool dma_quiescence_failed; + struct clk *cgu_isp; + struct clk *isp_gate; + struct clk *csi_gate; + struct clk *cgu_vpu; + struct clk *ivdc_gate; + void __iomem *ivdc_base; + struct reset_control *reset; + void *dma_buffer; + dma_addr_t dma_address; + size_t dma_size; + void *scratch_buf; + dma_addr_t scratch_dma; + size_t scratch_size; + void *video_priv; +}; + +extern struct tx_isp_device *g_tx_isp_dev; + +int tx_isp_request_irq(struct platform_device *pdev, struct tx_isp_device *isp); +void tx_isp_free_irq(struct tx_isp_device *isp); +void tx_isp_enable_irq(struct tx_isp_device *isp); +void tx_isp_disable_irq(struct tx_isp_device *isp); + +int tx_isp_vic_probe(struct platform_device *pdev); +int tx_isp_vic_remove(struct platform_device *pdev); +int tx_isp_vic_configure(struct tx_isp_subdev *sd, unsigned int width, + unsigned int height, unsigned int port); +void tx_isp_vic_dma_arm(struct tx_isp_subdev *sd, dma_addr_t address); +void tx_isp_vic_dma_resume(struct tx_isp_subdev *sd); +void tx_isp_vic_dma_stop(struct tx_isp_subdev *sd); +void tx_isp_vic_select_port(struct tx_isp_subdev *sd, unsigned int port); +int tx_isp_vic_start(struct tx_isp_subdev *sd); +void tx_isp_vic_capture_trigger(struct tx_isp_subdev *sd, bool enable); +int tx_isp_vic_stop(struct tx_isp_subdev *sd); +irqreturn_t isp_vic_interrupt_service_routine(struct tx_isp_subdev *sd, + bool *handled); + +int tx_isp_csi_probe(struct platform_device *pdev); +int tx_isp_csi_remove(struct platform_device *pdev); +int csi_phy_start(struct tx_isp_subdev *sd); +int csi_phy_stop(struct tx_isp_subdev *sd); +void tx_isp_csi_select_rate(struct tx_isp_subdev *sd, unsigned int clock_mhz); +int tx_isp_csi_configure(struct tx_isp_subdev *sd, unsigned int lanes, + unsigned int clock_mhz); + + +/* MSCA channel 0 writes the full frame and channels 1-2 scaled copies. */ +#define T23_MSCA_OUTPUTS 3 +#define T23_SCALED_OUTPUTS (T23_MSCA_OUTPUTS - 1) + +struct tx_isp_msca_target { + u16 width; /* 0 leaves the channel off */ + u16 height; + dma_addr_t y; + dma_addr_t uv; + bool hflip; + bool vflip; +}; + +#define T23_MSCA_MASKS 4 +#define T23_MSCA_LINES 6 +/* OSD blocks per input; the scaler has eight, programmed for each frame */ +#define T23_MSCA_OSD_REGIONS 8 + +struct tx_isp_rect { + u16 left; + u16 top; + u16 width; + u16 height; +}; + +struct tx_isp_line { + u16 x0; + u16 y0; + u16 x1; + u16 y1; + u16 width; +}; + +struct tx_isp_msca_osd { + dma_addr_t addr; /* 0: off */ + struct tx_isp_rect rect; + u32 stride; +}; + +/* + * Overlays drawn by MSCA, in input pixels. Lines and OSD cover channel 0 + * only, and one pixel format applies to every OSD block of a frame. + */ +struct tx_isp_msca_overlay { + struct tx_isp_rect mask[T23_MSCA_MASKS]; /* zero size: off */ + u32 mask_yuv; /* Y << 16 | U << 8 | V */ + struct tx_isp_line line[T23_MSCA_LINES]; /* zero width: off */ + u32 line_yuv; /* Y << 16 | U << 8 | V */ + u32 osd_format; /* V4L2_PIX_FMT_* */ + struct tx_isp_msca_osd osd[T23_MSCA_OSD_REGIONS]; +}; + +struct tx_isp_core_device { + struct tx_isp_subdev sd; + struct t23_isp_stats stats; + u32 stats_irq; /* statistics done bits of the current frame */ + struct tx_isp_params_state *params; /* one per input */ + int last_params_input; /* input currently loaded in HW */ + u32 hw_custom_mask; /* params blocks active in HW */ + struct tx_isp_mdns_history history[2]; + bool mdns_enabled; + unsigned int mdns_input; + bool running; + u32 input_control; + u32 geometry; + struct tx_isp_msca_target armed[T23_MSCA_OUTPUTS]; + u32 armed_mask; /* MSCA channels writing the current frame */ + u32 done_mask; + u32 bayer_order; + u32 active_inputs; +}; + +int tx_isp_core_probe(struct platform_device *pdev); +int tx_isp_core_remove(struct platform_device *pdev); +int tx_isp_core_s_stream(struct tx_isp_subdev *sd, int enable); +int tx_isp_core_mdns_start(struct tx_isp_subdev *sd, unsigned int input); +void tx_isp_core_queue_ext_params(struct tx_isp_subdev *sd, unsigned int input, + const struct t23_isp_ext_params_cfg *ext, + size_t payload); +void tx_isp_core_apply_params(struct tx_isp_subdev *sd, unsigned int input); +void tx_isp_core_reset_params(struct tx_isp_subdev *sd, unsigned int input); +void tx_isp_core_mdns_frame_done(struct tx_isp_subdev *sd); +extern const s16 tx_isp_default_ccm[9]; +int tx_isp_core_dma_arm(struct tx_isp_subdev *sd, + const struct tx_isp_msca_target out[T23_MSCA_OUTPUTS], + const struct tx_isp_msca_overlay *overlay, + unsigned int input); +void tx_isp_core_dma_stop(struct tx_isp_subdev *sd, bool abort); +void tx_isp_core_park_channel(struct tx_isp_subdev *sd, unsigned int ch, + dma_addr_t scratch_dma); +int tx_isp_core_output_init(struct tx_isp_subdev *sd); +void tx_isp_core_restore_dmsc(struct tx_isp_subdev *sd); +void tx_isp_core_dmsc_defaults(u32 values[3]); +int t23_isp_configure_input(struct tx_isp_device *isp, + struct v4l2_subdev *sensor, unsigned int port, + u32 active_inputs); +void t23_isp_video_dma_done(struct tx_isp_device *isp, bool error); +void t23_isp_video_processed_event(struct tx_isp_device *isp, bool dma_done, + bool all_done, bool error); +void t23_isp_video_frame_event(struct tx_isp_device *isp); +u16 tx_isp_calc_gamma_entry(int j, u32 g); +int tx_isp_core_init_hardware(struct tx_isp_subdev *sd); +void tx_isp_core_set_blc_default(struct tx_isp_subdev *sd); +void tx_isp_core_set_dgain(struct tx_isp_subdev *sd, u32 dgain_q10); +void tx_isp_core_set_wb(struct tx_isp_subdev *sd, u32 r_gain_q8, u32 b_gain_q8); +int tx_isp_core_set_gamma(struct tx_isp_subdev *sd, u32 gamma_val); +void tx_isp_core_set_ccm(struct tx_isp_subdev *sd, const s16 ccm[9]); +void tx_isp_core_set_sharpness(struct tx_isp_subdev *sd, u32 sharpness); + +int t23_isp_video_register(struct tx_isp_device *isp); +void t23_isp_video_unregister(void); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-driver.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-driver.c new file mode 100644 index 0000000000..21cd4d0ec0 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-driver.c @@ -0,0 +1,403 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 Video Processor Subsystem - Platform Driver + * + * Copyright (C) 2017-2023 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tx-isp-device.h" + +struct tx_isp_device *g_tx_isp_dev; + +int tx_isp_subdev_init(struct platform_device *pdev, struct tx_isp_subdev *sd, + struct tx_isp_subdev_ops *ops) +{ + struct resource *res; + + if (!sd || !pdev || !g_tx_isp_dev) + return -EINVAL; + if (pdev->id < 0 || pdev->id >= TX_ISP_SUBDEV_MAX) + return -EINVAL; + if (g_tx_isp_dev->subdevs[pdev->id]) + return -EBUSY; + + sd->ops = ops; + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (res) { + sd->res = request_mem_region(res->start, resource_size(res), + dev_name(&pdev->dev)); + if (!sd->res) + return -EBUSY; + sd->base = ioremap(res->start, resource_size(res)); + if (!sd->base) { + release_mem_region(res->start, resource_size(res)); + sd->res = NULL; + return -ENOMEM; + } + } + + g_tx_isp_dev->subdevs[pdev->id] = sd; + g_tx_isp_dev->subdev_cnt = + max_t(unsigned int, g_tx_isp_dev->subdev_cnt, pdev->id + 1); + + return 0; +} + +void tx_isp_subdev_deinit(struct tx_isp_subdev *sd) +{ + int i; + + if (!sd || !g_tx_isp_dev) + return; + if (sd->base) { + iounmap(sd->base); + sd->base = NULL; + } + if (sd->res) { + release_mem_region(sd->res->start, resource_size(sd->res)); + sd->res = NULL; + } + + for (i = 0; i < g_tx_isp_dev->subdev_cnt; i++) { + if (g_tx_isp_dev->subdevs[i] == sd) { + g_tx_isp_dev->subdevs[i] = NULL; + break; + } + } + while (g_tx_isp_dev->subdev_cnt && + !g_tx_isp_dev->subdevs[g_tx_isp_dev->subdev_cnt - 1]) + g_tx_isp_dev->subdev_cnt--; +} + +static struct { + const char *name; + const char *resource_name; + bool has_irq; + struct resource resources[3]; + unsigned int count; + int (*probe)(struct platform_device *pdev); + int (*remove)(struct platform_device *pdev); + struct platform_device *pdev; + bool probed; +} children[] = { + [TX_ISP_SUBDEV_VIC] = { .name = "tx-isp-vic", + .resource_name = "vic", + .has_irq = true, + .probe = tx_isp_vic_probe, + .remove = tx_isp_vic_remove }, + [TX_ISP_SUBDEV_CSI] = { .name = "tx-isp-csi", + .resource_name = "csi", + .probe = tx_isp_csi_probe, + .remove = tx_isp_csi_remove }, + [TX_ISP_SUBDEV_CORE] = { .name = "tx-isp-core", + .resource_name = "core", + .has_irq = true, + .probe = tx_isp_core_probe, + .remove = tx_isp_core_remove }, +}; + +static int tx_isp_get_resources(struct platform_device *pdev) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(children); i++) { + struct resource *res; + int irq; + + children[i].count = 0; + if (!children[i].resource_name) + continue; + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + children[i].resource_name); + if (!res) + return -EINVAL; + children[i].resources[children[i].count++] = *res; + if (i == TX_ISP_SUBDEV_CSI) { + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + "phy"); + if (!res) + return -EINVAL; + children[i].resources[children[i].count++] = *res; + } + if (!children[i].has_irq) + continue; + irq = platform_get_irq_byname(pdev, children[i].resource_name); + if (irq < 0) + return irq; + children[i].resources[children[i].count++] = (struct resource){ + .start = irq, + .end = irq, + .flags = IORESOURCE_IRQ, + }; + } + return 0; +} + +static void tx_isp_remove_children(void) +{ + int i; + /* IRQ callbacks must finish before any subdevice is freed. */ + tx_isp_free_irq(g_tx_isp_dev); + for (i = ARRAY_SIZE(children) - 1; i >= 0; i--) { + if (children[i].probed) { + children[i].remove(children[i].pdev); + children[i].probed = false; + } + if (children[i].pdev) { + platform_device_unregister(children[i].pdev); + children[i].pdev = NULL; + } + } +} + +struct tx_isp_clk_desc { + const char *name; + size_t offset; + unsigned long rate; +}; + +static const struct tx_isp_clk_desc isp_clk_descs[] = { + { "cgu_isp", offsetof(struct tx_isp_device, cgu_isp), 0 }, + { "isp", offsetof(struct tx_isp_device, isp_gate), 0 }, + { "csi", offsetof(struct tx_isp_device, csi_gate), 0 }, + { "cgu_vpu", offsetof(struct tx_isp_device, cgu_vpu), 416000000 }, + { "ivdc", offsetof(struct tx_isp_device, ivdc_gate), 0 }, +}; + +static void tx_isp_clocks_disable(struct tx_isp_device *ispdev) +{ + int i; + + for (i = ARRAY_SIZE(isp_clk_descs) - 1; i >= 0; i--) { + struct clk **clk_p = (void *)ispdev + isp_clk_descs[i].offset; + + if (*clk_p) { + clk_disable_unprepare(*clk_p); + clk_put(*clk_p); + *clk_p = NULL; + } + } +} + +static int tx_isp_clocks_enable(struct tx_isp_device *ispdev) +{ + struct device *dev = &ispdev->pdev->dev; + int i, ret; + + for (i = 0; i < ARRAY_SIZE(isp_clk_descs); i++) { + const struct tx_isp_clk_desc *desc = &isp_clk_descs[i]; + struct clk **clk_p = (void *)ispdev + desc->offset; + struct clk *clk; + + clk = clk_get(dev, desc->name); + if (IS_ERR(clk)) { + ret = PTR_ERR(clk); + goto err; + } + if (desc->rate) { + ret = clk_set_rate(clk, desc->rate); + if (ret) { + clk_put(clk); + goto err; + } + } + ret = clk_prepare_enable(clk); + if (ret) { + clk_put(clk); + goto err; + } + *clk_p = clk; + } + return 0; + +err: + tx_isp_clocks_disable(ispdev); + return ret; +} + +static int tx_isp_probe(struct platform_device *pdev) +{ + struct tx_isp_device *ispdev; + struct resource *res; + int ret, i; + + if (g_tx_isp_dev) + return -EBUSY; + ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); + if (ret) + return ret; + vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32)); + + ret = tx_isp_get_resources(pdev); + if (ret) + return ret; + ispdev = kzalloc(sizeof(*ispdev), GFP_KERNEL); + if (!ispdev) + return -ENOMEM; + + ispdev->pdev = pdev; + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ivdc"); + ispdev->ivdc_base = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(ispdev->ivdc_base)) { + ret = PTR_ERR(ispdev->ivdc_base); + kfree(ispdev); + return ret; + } + ispdev->reset = devm_reset_control_get_exclusive(&pdev->dev, "isp"); + if (IS_ERR(ispdev->reset)) { + ret = PTR_ERR(ispdev->reset); + kfree(ispdev); + return ret; + } + platform_set_drvdata(pdev, ispdev); + g_tx_isp_dev = ispdev; + ret = of_reserved_mem_device_init(&pdev->dev); + if (ret && ret != -ENODEV) + goto fail_clocks; + + ispdev->scratch_size = PAGE_SIZE; + ispdev->scratch_buf = dma_alloc_coherent(&pdev->dev, + ispdev->scratch_size, + &ispdev->scratch_dma, + GFP_KERNEL); + if (!ispdev->scratch_buf) { + ret = -ENOMEM; + goto fail_clocks; + } + + ret = tx_isp_clocks_enable(ispdev); + if (ret) { + dev_err(&pdev->dev, "failed to enable ISP clocks: %d\n", ret); + goto fail_clocks; + } + + for (i = 0; i < ARRAY_SIZE(children); i++) { + struct platform_device *child; + + if (!children[i].name) + continue; + child = platform_device_register_resndata( + &pdev->dev, children[i].name, i, children[i].resources, + children[i].count, NULL, 0); + if (IS_ERR(child)) { + ret = PTR_ERR(child); + goto fail; + } + children[i].pdev = child; + ret = children[i].probe(child); + if (ret) + goto fail; + children[i].probed = true; + } + for (i = 0; i < ARRAY_SIZE(children); i++) { + struct tx_isp_subdev *sd = ispdev->subdevs[i]; + + if (!children[i].pdev) + continue; + if (platform_get_resource(children[i].pdev, IORESOURCE_IRQ, + 0) && + (!sd || !sd->ops || + !sd->ops->interrupt_service_routine)) { + ret = -ENXIO; + dev_err(&pdev->dev, "%s has no interrupt handler\n", + children[i].name); + goto fail; + } + } + for (i = 0; i < ARRAY_SIZE(children); i++) { + if (!children[i].pdev) + continue; + if (!platform_get_resource(children[i].pdev, IORESOURCE_IRQ, 0)) + continue; + ret = tx_isp_request_irq(children[i].pdev, ispdev); + if (ret) + goto fail; + } + + ret = t23_isp_video_register(ispdev); + if (ret) { + dev_err(&pdev->dev, "failed to register V4L2 device: %d\n", + ret); + goto fail; + } + + return 0; +fail: + t23_isp_video_unregister(); + tx_isp_remove_children(); + tx_isp_clocks_disable(ispdev); +fail_clocks: + if (ispdev->scratch_buf && !ispdev->dma_quiescence_failed) { + dma_free_coherent(&pdev->dev, ispdev->scratch_size, + ispdev->scratch_buf, ispdev->scratch_dma); + } + if (ispdev->dma_buffer && !ispdev->dma_quiescence_failed) { + dma_free_coherent(&pdev->dev, ispdev->dma_size, + ispdev->dma_buffer, ispdev->dma_address); + } + if (!ispdev->dma_quiescence_failed) + of_reserved_mem_device_release(&pdev->dev); + platform_set_drvdata(pdev, NULL); + g_tx_isp_dev = NULL; + kfree(ispdev); + return ret; +} + +static void tx_isp_remove(struct platform_device *pdev) +{ + struct tx_isp_device *ispdev = platform_get_drvdata(pdev); + + if (ispdev) { + t23_isp_video_unregister(); + tx_isp_remove_children(); + tx_isp_clocks_disable(ispdev); + if (ispdev->scratch_buf && !ispdev->dma_quiescence_failed) { + dma_free_coherent(&pdev->dev, ispdev->scratch_size, + ispdev->scratch_buf, + ispdev->scratch_dma); + } + if (ispdev->dma_buffer && !ispdev->dma_quiescence_failed) { + dma_free_coherent(&pdev->dev, ispdev->dma_size, + ispdev->dma_buffer, ispdev->dma_address); + } + if (!ispdev->dma_quiescence_failed) + of_reserved_mem_device_release(&pdev->dev); + platform_set_drvdata(pdev, NULL); + kfree(ispdev); + g_tx_isp_dev = NULL; + } +} + +static const struct of_device_id tx_isp_of_match[] = { + { .compatible = "ingenic,t23-isp" }, + {} +}; +MODULE_DEVICE_TABLE(of, tx_isp_of_match); + +static struct platform_driver tx_isp_driver = { + .probe = tx_isp_probe, + .remove = tx_isp_remove, + .driver = { + .name = "tx-isp", + .of_match_table = tx_isp_of_match, + }, +}; + +module_platform_driver(tx_isp_driver); + +MODULE_AUTHOR("Ingenic Semiconductor"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_DESCRIPTION("Ingenic T23 Video Processor Subsystem Driver"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS(DMA_BUF); diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-interrupt.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-interrupt.c new file mode 100644 index 0000000000..9284a6f097 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-interrupt.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 Video Processor Subsystem - Interrupt Management + * + * Copyright (C) 2017-2023 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include + +#include "tx-isp-device.h" + +static irqreturn_t isp_irq_handle(int irq, void *data) +{ + struct isp_irq_binding *binding = data; + struct tx_isp_subdev *sd = binding->sd; + bool handled = false; + irqreturn_t ret; + + if (!sd || !sd->ops || !sd->ops->interrupt_service_routine) + return IRQ_NONE; + ret = sd->ops->interrupt_service_routine(sd, &handled); + if (handled) + return IRQ_HANDLED; + return ret; +} + +int tx_isp_request_irq(struct platform_device *pdev, + struct tx_isp_device *ispdev) +{ + int irq; + int ret; + struct isp_irq_binding *binding; + struct tx_isp_subdev *sd; + + if (!pdev || !ispdev) + return -EINVAL; + if (pdev->id < 0 || pdev->id >= TX_ISP_SUBDEV_MAX) + return -EINVAL; + binding = &ispdev->bindings[pdev->id]; + if (binding->requested) + return -EBUSY; + sd = ispdev->subdevs[pdev->id]; + if (!sd || !sd->ops || !sd->ops->interrupt_service_routine) + return 0; + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return irq; + binding->owner = ispdev; + binding->sd = sd; + binding->irq = irq; + + ret = request_threaded_irq(irq, NULL, isp_irq_handle, IRQF_ONESHOT, + dev_name(&pdev->dev), binding); + if (ret) { + dev_err(&pdev->dev, "failed to request IRQ %d: %d\n", irq, ret); + return ret; + } + + binding->requested = true; + disable_irq(irq); + return 0; +} + +void tx_isp_free_irq(struct tx_isp_device *ispdev) +{ + int i; + + for (i = ARRAY_SIZE(ispdev->bindings) - 1; i >= 0; i--) + if (ispdev->bindings[i].owner == ispdev) { + if (ispdev->bindings[i].requested) + free_irq(ispdev->bindings[i].irq, &ispdev->bindings[i]); + memset(&ispdev->bindings[i], 0, sizeof(ispdev->bindings[i])); + } +} + +void tx_isp_enable_irq(struct tx_isp_device *ispdev) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(ispdev->bindings); i++) + if (ispdev->bindings[i].owner == ispdev && ispdev->bindings[i].requested && + !ispdev->bindings[i].enabled) { + enable_irq(ispdev->bindings[i].irq); + ispdev->bindings[i].enabled = true; + } +} + +void tx_isp_disable_irq(struct tx_isp_device *ispdev) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(ispdev->bindings); i++) + if (ispdev->bindings[i].owner == ispdev && ispdev->bindings[i].requested && + ispdev->bindings[i].enabled) { + disable_irq(ispdev->bindings[i].irq); + ispdev->bindings[i].enabled = false; + } +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.c new file mode 100644 index 0000000000..7334d64662 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.c @@ -0,0 +1,732 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#include +#include +#include +#include + +#include "tx-isp-mdns.h" +#include "tx-isp-regs.h" + +struct tx_isp_mdns_reg { + u16 offset; + u32 value; +}; + +/* SC1A4T linear day profile at gain-table index 1. */ +static const struct tx_isp_mdns_reg mdns_sc1a4t_base[] = { + { 0x7870, 0x00000000 }, + { 0x7888, 0x00000002 }, + { 0x78a0, 0x00000001 }, + { 0x78b8, 0x00000003 }, + { 0x7874, 0x502d1010 }, + { 0x788c, 0x502d1010 }, + { 0x78a4, 0x502d1010 }, + { 0x78bc, 0x502d1010 }, + { 0x7880, 0x00080000 }, + { 0x7898, 0x00000008 }, + { 0x78b0, 0x00080008 }, + { 0x7884, 0x02c80500 }, + { 0x789c, 0x02d004f8 }, + { 0x78b4, 0x02c804f8 }, + { 0x7878, 0x000014f1 }, + { 0x7890, 0x000014f1 }, + { 0x78a8, 0x000014f1 }, + { 0x78c0, 0x000014f1 }, + { 0x787c, 0xff190114 }, + { 0x7894, 0xff190114 }, + { 0x78ac, 0xff190114 }, + { 0x78c4, 0xff190114 }, + { 0x7944, 0xff0c3064 }, + { 0x7948, 0xc8a07850 }, + { 0x7958, 0xc8a07850 }, + { 0x7968, 0xc8a07850 }, + { 0x7978, 0xc8a07850 }, + { 0x794c, 0xd7d5d4d3 }, + { 0x795c, 0xd7d5d4d3 }, + { 0x796c, 0xd7d5d4d3 }, + { 0x797c, 0xd7d5d4d3 }, + { 0x7950, 0xfaf0e6dc }, + { 0x7960, 0xfaf0e6dc }, + { 0x7970, 0xfaf0e6dc }, + { 0x7980, 0xfaf0e6dc }, + { 0x7954, 0xfafafafa }, + { 0x7964, 0xfafafafa }, + { 0x7974, 0xfafafafa }, + { 0x7984, 0xfafafafa }, + { 0x7988, 0xc8a07850 }, + { 0x798c, 0xd7d5d4d3 }, + { 0x7990, 0xfaf0e6dc }, + { 0x7994, 0xfafafafa }, + { 0x7998, 0x00141402 }, + { 0x799c, 0x00dc14ff }, + { 0x79a0, 0x000000ff }, + { 0x78c8, 0x00000000 }, + { 0x78d4, 0x00070006 }, + { 0x78d8, 0x00090008 }, + { 0x78cc, 0x001c0018 }, + { 0x78d0, 0x00240020 }, + { 0x78e0, 0x0a0a0a0a }, + { 0x78e4, 0x00008080 }, + { 0x78e8, 0x00000100 }, + { 0x78ec, 0x80808080 }, + { 0x78f0, 0x80808080 }, + { 0x78f4, 0x80808080 }, + { 0x78f8, 0x80808080 }, + { 0x78fc, 0x00000104 }, + { 0x7900, 0x80808080 }, + { 0x7904, 0x80808080 }, + { 0x7908, 0x80808080 }, + { 0x790c, 0x80808080 }, + { 0x7910, 0x00000106 }, + { 0x7914, 0x80808080 }, + { 0x7918, 0x80808080 }, + { 0x791c, 0x80808080 }, + { 0x7920, 0x80808080 }, + { 0x7924, 0x00000140 }, + { 0x7928, 0x80808080 }, + { 0x792c, 0x80808080 }, + { 0x7930, 0x80808080 }, + { 0x7934, 0x80808080 }, + { 0x7938, 0x00000001 }, + { 0x793c, 0x80808080 }, + { 0x7940, 0x80808080 }, + { 0x79a4, 0x0000000a }, + { 0x79a8, 0x00000000 }, + { 0x79ac, 0x0000000a }, + { 0x79b0, 0x00000000 }, + { 0x79b4, 0x00000000 }, + { 0x79b8, 0x64646464 }, + { 0x79bc, 0x64646464 }, + { 0x79c0, 0x78646464 }, + { 0x79c4, 0x000a1e46 }, + { 0x79c8, 0x64646464 }, + { 0x79cc, 0x64646464 }, + { 0x79d0, 0x78646464 }, + { 0x79d4, 0x000a1e46 }, + { 0x79d8, 0x0064320a }, + { 0x79dc, 0x000000dc }, + { 0x79e0, 0x00000000 }, + { 0x79e4, 0x0064320a }, + { 0x79e8, 0x000000dc }, + { 0x79ec, 0x00000000 }, + { 0x79f0, 0x82828282 }, + { 0x79f4, 0x82828282 }, + { 0x79f8, 0x28324646 }, + { 0x79fc, 0x00000a14 }, + { 0x7a00, 0x00004000 }, + { 0x7a64, 0xffff3141 }, + { 0x7a68, 0x00141400 }, + { 0x7a6c, 0x00c80aff }, + { 0x7a70, 0x000000ff }, + { 0x7a04, 0x00008080 }, + { 0x7a08, 0x00000000 }, + { 0x7a0c, 0x80808080 }, + { 0x7a10, 0x80808080 }, + { 0x7a14, 0x80808080 }, + { 0x7a18, 0x80808080 }, + { 0x7a1c, 0x00000000 }, + { 0x7a20, 0x80808080 }, + { 0x7a24, 0x80808080 }, + { 0x7a28, 0x80808080 }, + { 0x7a2c, 0x80808080 }, + { 0x7a30, 0x00000000 }, + { 0x7a34, 0x80808080 }, + { 0x7a38, 0x80808080 }, + { 0x7a3c, 0x80808080 }, + { 0x7a40, 0x80808080 }, + { 0x7a44, 0x00000000 }, + { 0x7a48, 0x80808080 }, + { 0x7a4c, 0x80808080 }, + { 0x7a50, 0x80808080 }, + { 0x7a54, 0x80808080 }, + { 0x7a58, 0x00000001 }, + { 0x7a5c, 0x00808080 }, + { 0x7a60, 0x00000000 }, + { 0x7a74, 0x09091480 }, + { 0x7a78, 0x00000000 }, + { 0x7a7c, 0x00000000 }, + { 0x7a80, 0x0017ffa1 }, + { 0x7a84, 0x00040000 }, + { 0x7a88, 0x01600280 }, + { 0x7a8c, 0x00000000 }, + { 0x7a90, 0xffffffff }, + { 0x7a94, 0xffffffff }, + { 0x7a98, 0xffffffff }, + { 0x7a9c, 0xffffffff }, + { 0x7aa0, 0xffffffff }, + { 0x7aa4, 0xffffffff }, + { 0x7aa8, 0xffffffff }, + { 0x7aac, 0xffffffff }, + { 0x7ab0, 0x001e140a }, + { 0x7ab4, 0x000000c8 }, + { 0x7ab8, 0x00008080 }, + { 0x7abc, 0x001e140a }, + { 0x7ac0, 0x000000c8 }, + { 0x7ac4, 0x00000001 }, + { 0x7ac8, 0xffffffff }, + { 0x7acc, 0xffffffff }, + { 0x7ad0, 0xffffffff }, + { 0x7ad4, 0x64ffffff }, + { 0x7ad8, 0x1e140a80 }, + { 0x7adc, 0x00000000 }, + { 0x7ae0, 0x00000000 }, + { 0x7ae4, 0x00000000 }, + { 0x7ae8, 0x00000000 }, + { 0x7aec, 0x00200040 }, +}; + +enum tx_isp_mdns_fields { + MDNS_BYTES, + MDNS_MOTION, + MDNS_STATIC, + MDNS_SAD, + MDNS_HISTOGRAM, +}; + +struct tx_isp_mdns_gain_reg { + u16 offset; + u8 fields; + u32 value[9]; +}; + +/* Entries correspond to log2(total gain) = 0 through 8. */ +static const struct tx_isp_mdns_gain_reg mdns_sc1a4t_gain[] = { + { 0x7878, MDNS_MOTION, { + 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, + 0x000028f1, 0x000028f1, 0x000028f1, 0x000028f1 } }, + { 0x7890, MDNS_MOTION, { + 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, + 0x000028f1, 0x000028f1, 0x000028f1, 0x000028f1 } }, + { 0x78a8, MDNS_MOTION, { + 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, + 0x000028f1, 0x000028f1, 0x000028f1, 0x000028f1 } }, + { 0x78c0, MDNS_MOTION, { + 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, 0x000014f1, + 0x000028f1, 0x000028f1, 0x000028f1, 0x000028f1 } }, + { 0x787c, MDNS_STATIC, { + 0xff1900f4, 0xff190114, 0xff190134, 0xff190154, 0xff190174, + 0xff190224, 0xff190244, 0xff190244, 0xff190244 } }, + { 0x7894, MDNS_STATIC, { + 0xff1900f4, 0xff190114, 0xff190134, 0xff190154, 0xff190174, + 0xff190224, 0xff190244, 0xff190244, 0xff190244 } }, + { 0x78ac, MDNS_STATIC, { + 0xff1900f4, 0xff190114, 0xff190134, 0xff190154, 0xff190174, + 0xff190224, 0xff190244, 0xff190244, 0xff190244 } }, + { 0x78c4, MDNS_STATIC, { + 0xff1900f4, 0xff190114, 0xff190134, 0xff190154, 0xff190174, + 0xff190224, 0xff190244, 0xff190244, 0xff190244 } }, + { 0x7944, MDNS_SAD, { + 0xff0a3054, 0xff0c3064, 0xff0e3074, 0xff103084, 0xff123094, + 0xff143124, 0xff143124, 0xff143124, 0xff143124 } }, + { 0x799c, MDNS_BYTES, { + 0x00dc14ff, 0x00dc14ff, 0x00dc14ff, 0x00e614ff, 0x00e614ff, + 0x00e614ff, 0x00e614ff, 0x00f014ff, 0x00f014ff } }, + { 0x78c8, MDNS_HISTOGRAM, { + 0x0000001f, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000 } }, + { 0x78e4, MDNS_BYTES, { + 0x00008080, 0x00008080, 0x00008c8c, 0x00008c8c, 0x00009696, + 0x00009696, 0x0000a0a0, 0x0000a0a0, 0x0000aaaa } }, + { 0x7940, MDNS_BYTES, { + 0x80808080, 0x80808080, 0x80808080, 0x80808080, 0x80808080, + 0x80808080, 0x80808080, 0x64646464, 0x32323232 } }, + { 0x79a4, MDNS_BYTES, { + 0x00000008, 0x0000000a, 0x0000000e, 0x00000010, 0x00000012, + 0x00000014, 0x00000016, 0x00000018, 0x0000001a } }, + { 0x79ac, MDNS_BYTES, { + 0x00000008, 0x0000000a, 0x0000000e, 0x00000010, 0x00000012, + 0x00000014, 0x00000016, 0x00000018, 0x0000001a } }, + { 0x79b4, MDNS_BYTES, { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000001, 0x00000001 } }, + { 0x79b8, MDNS_BYTES, { + 0x64646464, 0x64646464, 0xb4b4b4b4, 0xb4b4b4b4, 0xc8c8c8c8, + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff } }, + { 0x79bc, MDNS_BYTES, { + 0x64646464, 0x64646464, 0xb4b4b4b4, 0xb4b4b4b4, 0xc8c8c8c8, + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff } }, + { 0x79c0, MDNS_BYTES, { + 0x78646464, 0x78646464, 0x78b4b4b4, 0x78b4b4b4, 0x96c8c8c8, + 0xaaffffff, 0xb4ffffff, 0xc8ffffff, 0xf0ffffff } }, + { 0x79c4, MDNS_BYTES, { + 0x000a1e46, 0x000a1e46, 0x000a1e46, 0x000a1e46, 0x000a3264, + 0x00285078, 0x00285082, 0x00326496, 0x003264e6 } }, + { 0x79c8, MDNS_BYTES, { + 0x64646464, 0x64646464, 0xb4b4b4b4, 0xb4b4b4b4, 0xc8c8c8c8, + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff } }, + { 0x79cc, MDNS_BYTES, { + 0x64646464, 0x64646464, 0xb4b4b4b4, 0xb4b4b4b4, 0xc8c8c8c8, + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff } }, + { 0x79d0, MDNS_BYTES, { + 0x78646464, 0x78646464, 0x78b4b4b4, 0x78b4b4b4, 0x96c8c8c8, + 0xaaffffff, 0xb4ffffff, 0xc8ffffff, 0xf0ffffff } }, + { 0x79d4, MDNS_BYTES, { + 0x000a1e46, 0x000a1e46, 0x000a1e46, 0x000a1e46, 0x000a3264, + 0x00285078, 0x00285082, 0x00326496, 0x323264e6 } }, + { 0x79d8, MDNS_BYTES, { + 0x0064320a, 0x0064320a, 0x0064320a, 0x0064320a, 0x0064320a, + 0x0064320a, 0x0064320a, 0x0064320a, 0x00643200 } }, + { 0x79e4, MDNS_BYTES, { + 0x0064320a, 0x0064320a, 0x0064320a, 0x0064320a, 0x0064320a, + 0x0064320a, 0x0064320a, 0x0064320a, 0x00643200 } }, + { 0x79f0, MDNS_BYTES, { + 0x82828282, 0x82828282, 0x82828282, 0x82828282, 0x82828282, + 0x82828282, 0xaaaaaaaa, 0xc8c8c8c8, 0xf0f0f0f0 } }, + { 0x79f4, MDNS_BYTES, { + 0x82828282, 0x82828282, 0x82828282, 0x82828282, 0x82828282, + 0x82828282, 0xaaaaaaaa, 0xc8c8c8c8, 0xf0f0f0f0 } }, + { 0x79f8, MDNS_BYTES, { + 0x28324646, 0x28324646, 0x28324646, 0x32324646, 0x46464646, + 0x64828c82, 0x78aaaaaa, 0x96c8c8c8, 0xb4dcebe6 } }, + { 0x79fc, MDNS_BYTES, { + 0x00000a14, 0x00000a14, 0x00000a14, 0x00000a32, 0x00002846, + 0x0000283c, 0x0a0a285a, 0x0a0a3c78, 0x0a0a6496 } }, + { 0x7a6c, MDNS_BYTES, { + 0x00c80aff, 0x00c80aff, 0x00c80aff, 0x00d20aff, 0x00d20aff, + 0x00d20aff, 0x00dc0aff, 0x00dc0aff, 0x00dc0aff } }, + { 0x7ab0, MDNS_BYTES, { + 0x001e140a, 0x001e140a, 0x001e140a, 0x001e140a, 0x001e140a, + 0x001e140a, 0x00966450, 0x00966450, 0x00966450 } }, + { 0x7ab8, MDNS_BYTES, { + 0x00008080, 0x00008080, 0x00008080, 0x00008080, 0x00008080, + 0x00008080, 0x00000404, 0x00000404, 0x00000404 } }, + { 0x7abc, MDNS_BYTES, { + 0x001e140a, 0x001e140a, 0x001e140a, 0x001e140a, 0x001e140a, + 0x001e140a, 0x00966450, 0x00966450, 0x00966450 } }, + { 0x7ad4, MDNS_BYTES, { + 0x64ffffff, 0x64ffffff, 0x64ffffff, 0x64ffffff, 0x64ffffff, + 0x64ffffff, 0xffffffff, 0xffffffff, 0xffffffff } }, + { 0x7ad8, MDNS_BYTES, { + 0x1e140a80, 0x1e140a80, 0x1e140a80, 0x1e140a80, 0x1e140a80, + 0x1e140a80, 0x96645080, 0x96645080, 0x96645080 } }, +}; + +const u16 tx_isp_mdns_address_regs[TX_ISP_MDNS_PLANES] = { + 0x7820, 0x7828, 0x7830, 0x7840, 0x7848, + 0x7850, 0x7858, 0x7860, 0x7868, +}; + +int tx_isp_mdns_layout(u32 width, u32 height, + struct tx_isp_mdns_layout *layout) +{ + struct tx_isp_mdns_layout result = { }; + u32 stride, motion_stride; + u64 bytes[3], total = 0; + unsigned int i; + + if (!layout || width < 32 || height < 16 || + width > U16_MAX || height > U16_MAX || ((width | height) & 1)) + return -EINVAL; + + stride = ALIGN(width, 8); + motion_stride = ALIGN(DIV_ROUND_UP(width, 32), 8); + + bytes[0] = (u64)stride * height; + bytes[1] = bytes[0] / 2; + bytes[2] = (u64)(DIV_ROUND_UP(height, 16) + 1) * motion_stride; + for (i = 0; i < ARRAY_SIZE(bytes); i++) { + if (bytes[i] > U32_MAX - total) + return -EOVERFLOW; + result.plane[i].offset = total; + result.plane[i].size = bytes[i]; + total += bytes[i]; + } + result.plane[0].stride = stride; + result.plane[1].stride = stride; + result.plane[2].stride = motion_stride; + /* The SC1A4T profile disables these history planes with zero strides. */ + for (i = 3; i < 6; i++) { + result.plane[i] = result.plane[2]; + result.plane[i].stride = 0; + } + for (i = 6; i < TX_ISP_MDNS_PLANES; i++) { + result.plane[i] = result.plane[0]; + result.plane[i].stride = 0; + } + if (total > U32_MAX - (PAGE_SIZE - 1)) + return -EOVERFLOW; + result.size = ALIGN(total, PAGE_SIZE); + *layout = result; + return 0; +} + +int tx_isp_mdns_alloc(struct device *dev, struct tx_isp_mdns_history *history, + u32 width, u32 height) +{ + struct tx_isp_mdns_layout layout; + dma_addr_t dma; + void *cpu; + int ret; + + if (!dev || !history) + return -EINVAL; + if (history->cpu || history->active) + return -EBUSY; + ret = tx_isp_mdns_layout(width, height, &layout); + if (ret) + return ret; + cpu = dma_alloc_coherent(dev, layout.size, &dma, GFP_KERNEL); + if (!cpu) + return -ENOMEM; + if ((u64)dma + layout.size - 1 > U32_MAX || (dma & 7)) { + dma_free_coherent(dev, layout.size, cpu, dma); + return -ERANGE; + } + memset(cpu, 0, layout.size); + history->layout = layout; + history->dev = dev; + history->dma = dma; + history->cpu = cpu; + history->width = width; + history->height = height; + /* Cleared storage is not a completed reference frame. */ + history->valid = false; + return 0; +} + +int tx_isp_mdns_free(struct tx_isp_mdns_history *history) +{ + if (!history) + return -EINVAL; + if (history->active) + return -EBUSY; + if (history->cpu) + dma_free_coherent(history->dev, history->layout.size, + history->cpu, history->dma); + memset(history, 0, sizeof(*history)); + return 0; +} + +int tx_isp_mdns_invalidate(struct tx_isp_mdns_history *history) +{ + if (!history || !history->cpu) + return -EINVAL; + if (history->active) + return -EBUSY; + history->valid = false; + memset(history->cpu, 0, history->layout.size); + return 0; +} + +static u32 tx_isp_mdns_interpolate(u32 a, u32 b, u32 fraction) +{ + u32 delta = (a > b ? a - b : b - a) * fraction; + + delta = (delta >> 16) + !!(delta & BIT(15)); + return a > b ? a - delta : a + delta; +} + +static u32 tx_isp_mdns_field(u32 a, u32 b, u32 fraction, u32 mask, u32 shift) +{ + return tx_isp_mdns_interpolate((a >> shift) & mask, + (b >> shift) & mask, fraction) << shift; +} + +static u32 tx_isp_mdns_gain(u16 reg, u32 value, u32 gain_log2_q16) +{ + const struct tx_isp_mdns_gain_reg *entry; + u32 index = min(gain_log2_q16 >> 16, 8U); + u32 fraction = gain_log2_q16 & 0xffff; + u32 a, b, mask, result; + unsigned int i, j, shift, fields; + + for (i = 0; i < ARRAY_SIZE(mdns_sc1a4t_gain); i++) { + entry = &mdns_sc1a4t_gain[i]; + if (entry->offset != reg) + continue; + a = entry->value[index]; + if (index == 8) + return a; + b = entry->value[index + 1]; + if (entry->fields == MDNS_MOTION) + return (a & ~0xff00) | + tx_isp_mdns_field(a, b, fraction, 0xff, 8); + if (entry->fields == MDNS_STATIC) + return (a & ~0xfff0) | + tx_isp_mdns_field(a, b, fraction, 0xfff, 4); + if (entry->fields == MDNS_SAD) + return (a & ~0x00ff0ff8) | + tx_isp_mdns_field(a, b, fraction, 0x1ff, 3) | + tx_isp_mdns_field(a, b, fraction, 0xff, 16); + fields = entry->fields == MDNS_HISTOGRAM ? 5 : 4; + result = 0; + for (j = 0; j < fields; j++) { + shift = entry->fields == MDNS_HISTOGRAM ? j : j * 8; + mask = entry->fields == MDNS_HISTOGRAM ? 1 : 0xff; + result |= tx_isp_mdns_field(a, b, fraction, + mask, shift); + } + return result; + } + return value; +} + +static u32 tx_isp_mdns_block_size(u32 gain_log2_q16) +{ + static const u8 block_size[] = { 16, 16, 16, 16, 30, 30, 30, 30, 30 }; + u32 index = min(gain_log2_q16 >> 16, 8U); + u32 block = block_size[index]; + + if (index < 8) + block = tx_isp_mdns_interpolate(block, block_size[index + 1], + gain_log2_q16 & 0xffff); + return ALIGN(block, 4); +} + +static u32 tx_isp_mdns_geometry(u16 reg, u32 value, u32 width, u32 height, + u32 block) +{ + u32 x = 0, y = 0; + u32 cw = width / 2, ch = height / 2; + u32 half = block / 2; + + switch (reg) { + case 0x7870: + case 0x7888: + case 0x78a0: + case 0x78b8: + if (reg == 0x78a0 || reg == 0x78b8) + x = half; + if (reg == 0x7888 || reg == 0x78b8) + y = half; + return (!!((width - x) % block)) | + (!!((height - y) % block) << 1); + case 0x7874: + case 0x788c: + case 0x78a4: + case 0x78bc: + if (reg == 0x78a4 || reg == 0x78bc) + x = half; + if (reg == 0x788c || reg == 0x78bc) + y = half; + return (DIV_ROUND_UP(width - x, block) << 24) | + (DIV_ROUND_UP(height - y, block) << 16) | + (block << 8) | block; + case 0x7880: + return half << 16; + case 0x7898: + return half; + case 0x78b0: + return (half << 16) | half; + case 0x7884: + return ((height - half) << 16) | width; + case 0x789c: + return (height << 16) | (width - half); + case 0x78b4: + return ((height - half) << 16) | (width - half); + case 0x7a84: + return ((ch % 16 / 2) << 16) | (cw % 16 / 2); + case 0x7a88: + return ((ch - ch % 16 / 2 * 2) << 16) | + (cw - cw % 16 / 2 * 2); + default: + return value; + } +} + +static void tx_isp_mdns_write_params(void __iomem *base, u32 width, u32 height, + u32 gain_log2_q16) +{ + const struct tx_isp_mdns_reg *reg; + u32 block = tx_isp_mdns_block_size(gain_log2_q16); + u32 value; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(mdns_sc1a4t_base); i++) { + reg = &mdns_sc1a4t_base[i]; + value = tx_isp_mdns_gain(reg->offset, reg->value, + gain_log2_q16); + value = tx_isp_mdns_geometry(reg->offset, value, width, + height, block); + writel(value, base + reg->offset); + } +} + +/* Registers carried in struct t23_isp_mdns_config::val, ascending. */ +static const u16 mdns_config_regs[T23_ISP_MDNS_WORDS] = { + 0x78c8, 0x78cc, 0x78d0, 0x78d4, 0x78d8, 0x78e0, 0x78e4, 0x78e8, + 0x78ec, 0x78f0, 0x78f4, 0x78f8, 0x78fc, 0x7900, 0x7904, 0x7908, + 0x790c, 0x7910, 0x7914, 0x7918, 0x791c, 0x7920, 0x7924, 0x7928, + 0x792c, 0x7930, 0x7934, 0x7938, 0x793c, 0x7940, 0x7944, 0x7948, + 0x794c, 0x7950, 0x7954, 0x7958, 0x795c, 0x7960, 0x7964, 0x7968, + 0x796c, 0x7970, 0x7974, 0x7978, 0x797c, 0x7980, 0x7984, 0x7988, + 0x798c, 0x7990, 0x7994, 0x7998, 0x799c, 0x79a0, 0x79a4, 0x79a8, + 0x79ac, 0x79b0, 0x79b4, 0x79b8, 0x79bc, 0x79c0, 0x79c4, 0x79c8, + 0x79cc, 0x79d0, 0x79d4, 0x79d8, 0x79dc, 0x79e0, 0x79e4, 0x79e8, + 0x79ec, 0x79f0, 0x79f4, 0x79f8, 0x79fc, 0x7a00, 0x7a04, 0x7a08, + 0x7a0c, 0x7a10, 0x7a14, 0x7a18, 0x7a1c, 0x7a20, 0x7a24, 0x7a28, + 0x7a2c, 0x7a30, 0x7a34, 0x7a38, 0x7a3c, 0x7a40, 0x7a44, 0x7a48, + 0x7a4c, 0x7a50, 0x7a54, 0x7a58, 0x7a5c, 0x7a60, 0x7a64, 0x7a68, + 0x7a6c, 0x7a70, 0x7a74, 0x7a78, 0x7a7c, 0x7a80, 0x7a8c, 0x7a90, + 0x7a94, 0x7a98, 0x7a9c, 0x7aa0, 0x7aa4, 0x7aa8, 0x7aac, 0x7ab0, + 0x7ab4, 0x7ab8, 0x7abc, 0x7ac0, 0x7ac4, 0x7ac8, 0x7acc, 0x7ad0, + 0x7ad4, 0x7ad8, 0x7adc, 0x7ae0, 0x7ae4, 0x7ae8, 0x7aec, +}; + +static int tx_isp_mdns_config_index(u16 offset) +{ + unsigned int lo = 0, hi = ARRAY_SIZE(mdns_config_regs), mid; + + while (lo < hi) { + mid = (lo + hi) / 2; + if (mdns_config_regs[mid] == offset) + return mid; + if (mdns_config_regs[mid] < offset) + lo = mid + 1; + else + hi = mid; + } + return -1; +} + +/* + * The statistics control and threshold words are replicated in each of the + * four grid phases; the grid itself follows the frame size. + */ +static void tx_isp_mdns_write_config(void __iomem *base, u32 width, u32 height, + const struct t23_isp_mdns_config *cfg) +{ + unsigned int i; + u16 offset; + u32 value; + int n; + + for (i = 0; i < ARRAY_SIZE(mdns_sc1a4t_base); i++) { + offset = mdns_sc1a4t_base[i].offset; + n = tx_isp_mdns_config_index(offset); + if (n >= 0) + value = cfg->val[n]; + else if (offset < 0x78c8 && (offset - 0x7878) % 0x18 == 0) + value = cfg->sta_cfg; + else if (offset < 0x78c8 && (offset - 0x787c) % 0x18 == 0) + value = cfg->sta_thres; + else + value = tx_isp_mdns_geometry(offset, 0, width, height, + cfg->block_size); + writel(value, base + offset); + } +} + +int tx_isp_mdns_check_config(const struct t23_isp_mdns_config *cfg) +{ + if ((cfg->top[0] & ~ISP_MDNS_FUNC_CTRL_MASK) || + (cfg->top[1] & ~ISP_MDNS_FUNC_GROUP_MASK) || cfg->top[2]) + return -EINVAL; + if (cfg->block_size < 16 || cfg->block_size > 255 || + cfg->block_size % 4) + return -EINVAL; + return 0; +} + +static int tx_isp_mdns_validate(u32 width, u32 height, + enum tx_isp_mdns_profile profile, + u32 gain_log2_q16) +{ + if (width < 32 || height < 16 || ((width | height) & 1)) + return -EINVAL; + if (profile != TX_ISP_MDNS_SC1A4T_LINEAR_DAY) + return -EOPNOTSUPP; + if (gain_log2_q16 & BIT(31)) + return -EINVAL; + /* Motion-grid row and column counts occupy eight bits each. */ + if (width > 4080 || height > 4080) + return -EOPNOTSUPP; + return 0; +} + +int tx_isp_mdns_configure_bypassed(void __iomem *base, u32 width, u32 height, + enum tx_isp_mdns_profile profile, + u32 gain_log2_q16) +{ + int ret; + + if (!base) + return -EINVAL; + ret = tx_isp_mdns_validate(width, height, profile, gain_log2_q16); + if (ret) + return ret; + writel(ISP_MDNS_FUNC_CTRL_BYPASS, base + ISP_MDNS_FUNC_CONTROL); + writel(ISP_MDNS_FUNC_GROUP_DEFAULT, base + ISP_MDNS_FUNC_GROUP); + writel(0, base + ISP_MDNS_DEBUG_CONTROL); + writel(ISP_MDNS_TRIGGER_STOP, base + ISP_MDNS_REG_TRIGGER); + tx_isp_mdns_write_params(base, width, height, gain_log2_q16); + writel(ISP_MDNS_REFRESH_VAL, base + ISP_MDNS_FUNC_REFRESH); + writel(ISP_MDNS_TRIGGER_START, base + ISP_MDNS_REG_TRIGGER); + return 0; +} + +int tx_isp_mdns_start(struct tx_isp_mdns_history *history, void __iomem *base, + enum tx_isp_mdns_profile profile, u32 gain_log2_q16, + const struct t23_isp_mdns_config *cfg) +{ + const struct tx_isp_mdns_plane *plane; + unsigned int i; + int ret; + + if (!history || !history->cpu || !base) + return -EINVAL; + if (history->active) + return -EBUSY; + ret = tx_isp_mdns_validate(history->width, history->height, profile, + gain_log2_q16); + if (ret) + return ret; + + for (i = 0; i < TX_ISP_MDNS_PLANES; i++) { + plane = &history->layout.plane[i]; + writel((u32)history->dma + plane->offset, + base + tx_isp_mdns_address_regs[i]); + writel(plane->stride, base + tx_isp_mdns_address_regs[i] + 4); + } + writel(ISP_MDNS_DMA_CTRL0_DEFAULT, base + ISP_MDNS_DMA_CTRL0); + writel(ISP_MDNS_DMA_CTRL1_DEFAULT, base + ISP_MDNS_DMA_CTRL1); + writel(ISP_MDNS_TRIGGER_STOP, base + ISP_MDNS_REG_TRIGGER); + if (cfg) { + writel(cfg->top[0] & ISP_MDNS_FUNC_CTRL_MASK, + base + ISP_MDNS_FUNC_CONTROL); + writel(cfg->top[1] & ISP_MDNS_FUNC_GROUP_MASK, + base + ISP_MDNS_FUNC_GROUP); + writel(0, base + ISP_MDNS_DEBUG_CONTROL); + tx_isp_mdns_write_config(base, history->width, history->height, + cfg); + } else { + writel(ISP_MDNS_FUNC_CTRL_ENABLE, base + ISP_MDNS_FUNC_CONTROL); + writel(ISP_MDNS_FUNC_GROUP_DEFAULT, base + ISP_MDNS_FUNC_GROUP); + writel(0, base + ISP_MDNS_DEBUG_CONTROL); + tx_isp_mdns_write_params(base, history->width, history->height, + gain_log2_q16); + } + if (!history->valid) + writel(ISP_MDNS_REFRESH_VAL, base + ISP_MDNS_FUNC_REFRESH); + /* Keep the allocation pinned until the whole ISP pipeline is idle. */ + history->active = true; + /* Publish initialized history before starting DMA. */ + wmb(); + writel(ISP_MDNS_TRIGGER_START, base + ISP_MDNS_REG_TRIGGER); + return 0; +} + +/* Called only after ISP all-done, or after a completed hardware reset. */ +void tx_isp_mdns_frame_done(struct tx_isp_mdns_history *history, bool error) +{ + if (!history || !history->active) + return; + history->active = false; + history->valid = !error; +} + +int tx_isp_mdns_bypass(struct tx_isp_mdns_history *history, void __iomem *base) +{ + if (!history || !base) + return -EINVAL; + if (history->active) + return -EBUSY; + writel(ISP_MDNS_FUNC_CTRL_BYPASS, base + ISP_MDNS_FUNC_CONTROL); + writel(ISP_MDNS_FUNC_GROUP_DEFAULT, base + ISP_MDNS_FUNC_GROUP); + writel(0, base + ISP_MDNS_DEBUG_CONTROL); + writel(ISP_MDNS_REFRESH_VAL, base + ISP_MDNS_FUNC_REFRESH); + writel(ISP_MDNS_TRIGGER_START, base + ISP_MDNS_REG_TRIGGER); + history->valid = false; + return 0; +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.h new file mode 100644 index 0000000000..1b75aae5fb --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-mdns.h @@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_MDNS_H__ +#define __TX_ISP_MDNS_H__ + +#include +#include +#include +#include + +#define TX_ISP_MDNS_PLANES 9 + +enum tx_isp_mdns_profile { + TX_ISP_MDNS_SC1A4T_LINEAR_DAY, +}; + +struct tx_isp_mdns_plane { + u32 offset; + u32 stride; + u32 size; +}; + +struct tx_isp_mdns_layout { + struct tx_isp_mdns_plane plane[TX_ISP_MDNS_PLANES]; + u32 size; +}; + +/* One allocation per sensor; callers serialize changes with the stream lock. */ +struct tx_isp_mdns_history { + struct tx_isp_mdns_layout layout; + struct device *dev; + void *cpu; + dma_addr_t dma; + u32 width; + u32 height; + bool active; + bool valid; +}; + +extern const u16 tx_isp_mdns_address_regs[TX_ISP_MDNS_PLANES]; + +/* Compact history layout for TX_ISP_MDNS_SC1A4T_LINEAR_DAY. */ +int tx_isp_mdns_layout(u32 width, u32 height, + struct tx_isp_mdns_layout *layout); +int tx_isp_mdns_alloc(struct device *dev, struct tx_isp_mdns_history *history, + u32 width, u32 height); +int tx_isp_mdns_free(struct tx_isp_mdns_history *history); +int tx_isp_mdns_invalidate(struct tx_isp_mdns_history *history); +/* Pipeline must be idle. Gain is log2(total gain) in Q16; zero means 1x. */ +int tx_isp_mdns_configure_bypassed(void __iomem *base, u32 width, u32 height, + enum tx_isp_mdns_profile profile, + u32 gain_log2_q16); +/* Pipeline must be idle. Gain is log2(total gain) in Q16; zero means 1x. */ +/* A non-NULL @cfg replaces the profile; the grid still follows the frame. */ +int tx_isp_mdns_start(struct tx_isp_mdns_history *history, void __iomem *base, + enum tx_isp_mdns_profile profile, u32 gain_log2_q16, + const struct t23_isp_mdns_config *cfg); +int tx_isp_mdns_check_config(const struct t23_isp_mdns_config *cfg); +void tx_isp_mdns_frame_done(struct tx_isp_mdns_history *history, bool error); +int tx_isp_mdns_bypass(struct tx_isp_mdns_history *history, void __iomem *base); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-meta.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-meta.c new file mode 100644 index 0000000000..f05a300d28 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-meta.c @@ -0,0 +1,482 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Statistics capture and parameter output nodes of the T23 ISP. + */ +#include +#include +#include +#include +#include +#include + +#include "tx-isp-device.h" +#include "tx-isp-meta.h" + +struct t23_meta_buffer { + struct vb2_v4l2_buffer vb; + struct list_head list; + struct t23_isp_ext_params_cfg *cfg; + size_t payload; +}; + +static inline struct t23_meta_buffer *to_t23_meta_buffer(struct vb2_buffer *vb) +{ + return container_of(to_vb2_v4l2_buffer(vb), struct t23_meta_buffer, vb); +} + +static size_t t23_meta_size_fmt(u32 fourcc) +{ + switch (fourcc) { + case V4L2_META_FMT_T23_ISP_EXT_PARAMS: + return sizeof(struct t23_isp_ext_params_cfg); + default: + return sizeof(struct t23_isp_stat_buffer); + } +} + +static size_t t23_meta_size(const struct t23_meta_node *node) +{ + return t23_meta_size_fmt(node->dataformat); +} + +static u32 t23_meta_fourcc(const struct t23_meta_node *node) +{ + return node->dataformat; +} + +static int t23_meta_queue_setup(struct vb2_queue *q, unsigned int *nbuffers, + unsigned int *nplanes, unsigned int sizes[], + struct device *alloc_devs[]) +{ + struct t23_meta_node *node = vb2_get_drv_priv(q); + + if (*nplanes) + return sizes[0] < t23_meta_size(node) ? -EINVAL : 0; + *nplanes = 1; + sizes[0] = t23_meta_size(node); + return 0; +} + +static int t23_meta_buf_init(struct vb2_buffer *vb) +{ + struct t23_meta_node *node = vb2_get_drv_priv(vb->vb2_queue); + struct t23_meta_buffer *buf = to_t23_meta_buffer(vb); + + if (!node->output) + return 0; + buf->cfg = kvzalloc(sizeof(*buf->cfg), GFP_KERNEL); + return buf->cfg ? 0 : -ENOMEM; +} + +static void t23_meta_buf_cleanup(struct vb2_buffer *vb) +{ + struct t23_meta_buffer *buf = to_t23_meta_buffer(vb); + + kvfree(buf->cfg); + buf->cfg = NULL; + buf->payload = 0; +} + +static int t23_meta_buf_prepare(struct vb2_buffer *vb) +{ + struct t23_meta_node *node = vb2_get_drv_priv(vb->vb2_queue); + struct t23_meta_buffer *buf = to_t23_meta_buffer(vb); + size_t hdr_len = offsetof(struct t23_isp_ext_params_cfg, data); + size_t payload = vb2_get_plane_payload(vb, 0); + const void *vaddr; + + if (!node->output) { + if (vb2_plane_size(vb, 0) < t23_meta_size(node)) + return -EINVAL; + vb2_set_plane_payload(vb, 0, t23_meta_size(node)); + return 0; + } + vaddr = vb2_plane_vaddr(vb, 0); + if (!buf->cfg || !vaddr || payload < hdr_len || + payload > sizeof(*buf->cfg) || payload > vb2_plane_size(vb, 0)) + return -EINVAL; + /* + * Copy the user-mmapped payload into a kernel shadow once so + * userspace cannot mutate block headers or fields after validation. + */ + memcpy(buf->cfg, vaddr, payload); + buf->payload = payload; + return tx_isp_ext_params_check(buf->cfg, payload); +} + +static void t23_meta_buf_queue(struct vb2_buffer *vb) +{ + struct t23_meta_node *node = vb2_get_drv_priv(vb->vb2_queue); + struct t23_meta_buffer *buf = to_t23_meta_buffer(vb); + unsigned long flags; + + spin_lock_irqsave(&node->meta->lock, flags); + list_add_tail(&buf->list, &node->bufs); + spin_unlock_irqrestore(&node->meta->lock, flags); +} + +static int t23_meta_start_streaming(struct vb2_queue *q, unsigned int count) +{ + struct t23_meta_node *node = vb2_get_drv_priv(q); + unsigned long flags; + + spin_lock_irqsave(&node->meta->lock, flags); + node->streaming = true; + spin_unlock_irqrestore(&node->meta->lock, flags); + return 0; +} + +static void t23_meta_stop_streaming(struct vb2_queue *q) +{ + struct t23_meta_node *node = vb2_get_drv_priv(q); + struct t23_meta_buffer *buf, *tmp; + unsigned long flags; + LIST_HEAD(done); + + spin_lock_irqsave(&node->meta->lock, flags); + node->streaming = false; + /* Without a parameter stream the input returns to driver defaults. */ + if (node->output) + node->meta->reset[node->input] = true; + list_splice_init(&node->bufs, &done); + spin_unlock_irqrestore(&node->meta->lock, flags); + + list_for_each_entry_safe(buf, tmp, &done, list) { + list_del(&buf->list); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); + } +} + +static const struct vb2_ops t23_meta_vb2_ops = { + .queue_setup = t23_meta_queue_setup, + .buf_init = t23_meta_buf_init, + .buf_cleanup = t23_meta_buf_cleanup, + .buf_prepare = t23_meta_buf_prepare, + .buf_queue = t23_meta_buf_queue, + .start_streaming = t23_meta_start_streaming, + .stop_streaming = t23_meta_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int t23_meta_querycap(struct file *file, void *fh, + struct v4l2_capability *cap) +{ + struct t23_meta_node *node = video_drvdata(file); + + strscpy(cap->driver, "tx-isp", sizeof(cap->driver)); + strscpy(cap->card, node->vdev.name, sizeof(cap->card)); + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", + dev_name(node->meta->dev)); + return 0; +} + +static int t23_meta_enum_fmt(struct file *file, void *fh, + struct v4l2_fmtdesc *f) +{ + struct t23_meta_node *node = video_drvdata(file); + + if (f->index) + return -EINVAL; + f->pixelformat = node->output ? V4L2_META_FMT_T23_ISP_EXT_PARAMS : + V4L2_META_FMT_T23_ISP_STATS; + return 0; +} + +static int t23_meta_g_fmt(struct file *file, void *fh, struct v4l2_format *f) +{ + struct t23_meta_node *node = video_drvdata(file); + + memset(&f->fmt.meta, 0, sizeof(f->fmt.meta)); + f->fmt.meta.dataformat = t23_meta_fourcc(node); + f->fmt.meta.buffersize = t23_meta_size(node); + return 0; +} + +static int t23_meta_try_fmt_out(struct file *file, void *fh, + struct v4l2_format *f) +{ + memset(&f->fmt.meta, 0, sizeof(f->fmt.meta)); + f->fmt.meta.dataformat = V4L2_META_FMT_T23_ISP_EXT_PARAMS; + f->fmt.meta.buffersize = t23_meta_size_fmt(V4L2_META_FMT_T23_ISP_EXT_PARAMS); + return 0; +} + +static int t23_meta_s_fmt_out(struct file *file, void *fh, + struct v4l2_format *f) +{ + struct t23_meta_node *node = video_drvdata(file); + + if (vb2_is_busy(&node->queue)) + return -EBUSY; + t23_meta_try_fmt_out(file, fh, f); + node->dataformat = f->fmt.meta.dataformat; + return 0; +} + +static const struct v4l2_ioctl_ops t23_meta_ioctl_ops = { + .vidioc_querycap = t23_meta_querycap, + .vidioc_enum_fmt_meta_cap = t23_meta_enum_fmt, + .vidioc_g_fmt_meta_cap = t23_meta_g_fmt, + .vidioc_s_fmt_meta_cap = t23_meta_g_fmt, + .vidioc_try_fmt_meta_cap = t23_meta_g_fmt, + .vidioc_enum_fmt_meta_out = t23_meta_enum_fmt, + .vidioc_g_fmt_meta_out = t23_meta_g_fmt, + .vidioc_s_fmt_meta_out = t23_meta_s_fmt_out, + .vidioc_try_fmt_meta_out = t23_meta_try_fmt_out, + .vidioc_reqbufs = vb2_ioctl_reqbufs, + .vidioc_querybuf = vb2_ioctl_querybuf, + .vidioc_create_bufs = vb2_ioctl_create_bufs, + .vidioc_qbuf = vb2_ioctl_qbuf, + .vidioc_dqbuf = vb2_ioctl_dqbuf, + .vidioc_expbuf = vb2_ioctl_expbuf, + .vidioc_streamon = vb2_ioctl_streamon, + .vidioc_streamoff = vb2_ioctl_streamoff, +}; + +static const struct v4l2_file_operations t23_meta_fops = { + .owner = THIS_MODULE, + .open = v4l2_fh_open, + .release = vb2_fop_release, + .poll = vb2_fop_poll, + .mmap = vb2_fop_mmap, + .unlocked_ioctl = video_ioctl2, +}; + +static int t23_meta_node_init(struct t23_meta *meta, struct t23_meta_node *node, + struct v4l2_device *v4l2_dev, struct mutex *lock, + unsigned int input, bool output) +{ + struct vb2_queue *q = &node->queue; + int ret; + + node->meta = meta; + node->input = input; + node->output = output; + node->dataformat = output ? V4L2_META_FMT_T23_ISP_EXT_PARAMS : + V4L2_META_FMT_T23_ISP_STATS; + INIT_LIST_HEAD(&node->bufs); + + q->type = output ? V4L2_BUF_TYPE_META_OUTPUT : V4L2_BUF_TYPE_META_CAPTURE; + q->io_modes = VB2_MMAP | VB2_DMABUF; + q->drv_priv = node; + q->ops = &t23_meta_vb2_ops; + q->mem_ops = &vb2_vmalloc_memops; + q->buf_struct_size = sizeof(struct t23_meta_buffer); + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + q->lock = lock; + q->dev = meta->dev; + ret = vb2_queue_init(q); + if (ret) + return ret; + node->queue_initialized = true; + + node->vdev = (struct video_device){ + .fops = &t23_meta_fops, + .ioctl_ops = &t23_meta_ioctl_ops, + .release = video_device_release_empty, + .v4l2_dev = v4l2_dev, + .queue = q, + .lock = lock, + .vfl_dir = output ? VFL_DIR_TX : VFL_DIR_RX, + .device_caps = (output ? V4L2_CAP_META_OUTPUT : + V4L2_CAP_META_CAPTURE) | + V4L2_CAP_STREAMING, + }; + snprintf(node->vdev.name, sizeof(node->vdev.name), "T23 %s%u", + output ? "params" : "stats", input); + node->vdev.entity.function = MEDIA_ENT_F_IO_V4L; + node->pad.flags = output ? MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK; + ret = media_entity_pads_init(&node->vdev.entity, 1, &node->pad); + if (ret) + return ret; + node->entity_initialized = true; + video_set_drvdata(&node->vdev, node); + + ret = video_register_device(&node->vdev, VFL_TYPE_VIDEO, -1); + if (ret) + return ret; + node->registered = true; + return 0; +} + +int t23_meta_register(struct t23_meta *meta, struct v4l2_device *v4l2_dev, + struct mutex *lock, struct media_entity *isp[], + unsigned int params_pad, unsigned int stats_pad) +{ + unsigned int i; + int ret; + + spin_lock_init(&meta->lock); + for (i = 0; i < T23_META_INPUTS; i++) { + ret = t23_meta_node_init(meta, &meta->params[i], v4l2_dev, lock, + i, true); + if (ret) + goto fail; + ret = media_create_pad_link(&meta->params[i].vdev.entity, 0, + isp[i], params_pad, + MEDIA_LNK_FL_ENABLED | + MEDIA_LNK_FL_IMMUTABLE); + if (ret) + goto fail; + ret = t23_meta_node_init(meta, &meta->stats[i], v4l2_dev, lock, + i, false); + if (ret) + goto fail; + ret = media_create_pad_link(isp[i], stats_pad, + &meta->stats[i].vdev.entity, 0, + MEDIA_LNK_FL_ENABLED | + MEDIA_LNK_FL_IMMUTABLE); + if (ret) + goto fail; + } + return 0; + +fail: + t23_meta_unregister(meta); + return ret; +} + +static void t23_meta_node_unregister(struct t23_meta_node *node) +{ + if (!node->registered) + return; + video_unregister_device(&node->vdev); + node->registered = false; +} + +void t23_meta_unregister(struct t23_meta *meta) +{ + unsigned int i; + + for (i = 0; i < T23_META_INPUTS; i++) { + t23_meta_node_unregister(&meta->params[i]); + t23_meta_node_unregister(&meta->stats[i]); + } +} + +static void t23_meta_node_release(struct t23_meta_node *node) +{ + if (node->queue_initialized) + vb2_queue_release(&node->queue); + if (node->entity_initialized) + media_entity_cleanup(&node->vdev.entity); + node->queue_initialized = false; + node->entity_initialized = false; +} + +void t23_meta_release(struct t23_meta *meta) +{ + unsigned int i; + + for (i = 0; i < T23_META_INPUTS; i++) { + t23_meta_node_release(&meta->params[i]); + t23_meta_node_release(&meta->stats[i]); + } +} + +static struct t23_meta_buffer *t23_meta_next(struct t23_meta_node *node) +{ + struct t23_meta_buffer *buf; + unsigned long flags; + + spin_lock_irqsave(&node->meta->lock, flags); + buf = node->streaming ? + list_first_entry_or_null(&node->bufs, struct t23_meta_buffer, + list) : NULL; + if (buf) + list_del(&buf->list); + spin_unlock_irqrestore(&node->meta->lock, flags); + return buf; +} + +void t23_meta_params_apply(struct t23_meta *meta, unsigned int input, + struct tx_isp_subdev *core) +{ + struct t23_meta_buffer *buf; + unsigned long flags; + bool reset; + + if (input >= T23_META_INPUTS) + return; + spin_lock_irqsave(&meta->lock, flags); + reset = meta->reset[input]; + meta->reset[input] = false; + spin_unlock_irqrestore(&meta->lock, flags); + if (reset) + tx_isp_core_reset_params(core, input); + buf = t23_meta_next(&meta->params[input]); + if (buf) { + tx_isp_core_queue_ext_params(core, input, buf->cfg, + buf->payload); + buf->vb.vb2_buf.timestamp = ktime_get_ns(); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); + } + tx_isp_core_apply_params(core, input); +} + +/* AE and AWB records pack 21-bit sums and 13-bit counts into 16 bytes. */ +static void t23_meta_decode_ae(struct t23_isp_ae_zone *zone, const u32 *w) +{ + zone->sum_low = w[0] & GENMASK(20, 0); + zone->sum_mid = (FIELD_GET(GENMASK(9, 0), w[1]) << 11) | + FIELD_GET(GENMASK(31, 21), w[0]); + zone->sum_high = FIELD_GET(GENMASK(30, 10), w[1]); + zone->count_low = (FIELD_GET(GENMASK(11, 0), w[2]) << 1) | + FIELD_GET(BIT(31), w[1]); + zone->count_high = FIELD_GET(GENMASK(24, 12), w[2]); +} + +static void t23_meta_decode_awb(struct t23_isp_awb_zone *zone, const u32 *w) +{ + zone->sum_r = w[0] & GENMASK(20, 0); + zone->sum_g = (FIELD_GET(GENMASK(9, 0), w[1]) << 11) | + FIELD_GET(GENMASK(31, 21), w[0]); + zone->sum_b = FIELD_GET(GENMASK(30, 10), w[1]); + zone->count = (FIELD_GET(BIT(0), w[3]) << 12) | + FIELD_GET(GENMASK(31, 20), w[2]); +} + +void t23_meta_stats_done(struct t23_meta *meta, unsigned int input, + const struct t23_meta_frame *frame) +{ + struct t23_isp_stat_buffer *out; + struct t23_meta_buffer *buf; + const u32 *hw = frame->hw; + unsigned int i; + + if (input >= T23_META_INPUTS) + return; + buf = t23_meta_next(&meta->stats[input]); + if (!buf) + return; + + out = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); + memset(out, 0, sizeof(*out)); + out->version = T23_ISP_STATS_VERSION; + out->size = sizeof(*out); + out->flags = frame->valid; + out->sequence = frame->sequence; + out->zone_width = frame->zone_width; + out->zone_height = frame->zone_height; + out->ae_low = frame->ae_low; + out->ae_high = frame->ae_high; + dma_rmb(); + if (frame->valid & T23_ISP_STATS_AE) + for (i = 0; i < T23_ISP_ZONES; i++) + t23_meta_decode_ae(&out->ae[i], hw + i * 4); + if (frame->valid & T23_ISP_STATS_HIST) + for (i = 0; i < 2 * T23_ISP_HIST_BINS; i++) + out->hist[i / T23_ISP_HIST_BINS][i % T23_ISP_HIST_BINS] = + hw[0x1000 / 4 + i] & GENMASK(20, 0); + if (frame->valid & T23_ISP_STATS_AWB) + for (i = 0; i < T23_ISP_ZONES; i++) + t23_meta_decode_awb(&out->awb[i], hw + 0x2000 / 4 + i * 4); + + buf->vb.sequence = frame->sequence; + buf->vb.field = V4L2_FIELD_NONE; + buf->vb.vb2_buf.timestamp = frame->timestamp; + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, sizeof(*out)); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-meta.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-meta.h new file mode 100644 index 0000000000..3a7517c37b --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-meta.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_META_H__ +#define __TX_ISP_META_H__ + +#include +#include +#include +#include +#include +#include +#include + +#define T23_META_INPUTS 2 + +struct t23_meta; +struct tx_isp_subdev; + +struct t23_meta_node { + struct t23_meta *meta; + unsigned int input; + bool output; + u32 dataformat; + struct video_device vdev; + struct media_pad pad; + struct vb2_queue queue; + struct list_head bufs; + bool streaming; + bool queue_initialized; + bool entity_initialized; + bool registered; +}; + +struct t23_meta { + struct device *dev; + spinlock_t lock; + struct t23_meta_node stats[T23_META_INPUTS]; + struct t23_meta_node params[T23_META_INPUTS]; + bool reset[T23_META_INPUTS]; /* drop the input's parameters */ +}; + +struct t23_meta_frame { + u32 sequence; + u64 timestamp; + const void *hw; /* AE at +0, histogram at +0x1000, AWB at +0x2000 */ + u32 valid; /* T23_ISP_STATS_* */ + u16 zone_width; + u16 zone_height; + u8 ae_low; + u8 ae_high; +}; + +/* + * The ISP entity of each input must have a sink pad @params_pad and a source + * pad @stats_pad. + */ +int t23_meta_register(struct t23_meta *meta, struct v4l2_device *v4l2_dev, + struct mutex *lock, struct media_entity *isp[], + unsigned int params_pad, unsigned int stats_pad); +void t23_meta_unregister(struct t23_meta *meta); +void t23_meta_release(struct t23_meta *meta); +/* Both run in atomic context: the capture path holds a spinlock. */ +void t23_meta_params_apply(struct t23_meta *meta, unsigned int input, + struct tx_isp_subdev *core); +void t23_meta_stats_done(struct t23_meta *meta, unsigned int input, + const struct t23_meta_frame *frame); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-motion.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-motion.c new file mode 100644 index 0000000000..98100900a4 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-motion.c @@ -0,0 +1,255 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Motion detection on the 15x15 AE zone statistics, reported through the + * V4L2 detection controls and V4L2_EVENT_MOTION_DET. + * + * Each zone keeps a background luma. The median ratio of zone luma to + * background is the frame's gain, so exposure changes and global light + * changes cancel while a moving subject in fewer than half the zones does + * not. Zones are compared on a square-root scale with the frame mean at + * 128, close to the output gamma: a 1% flicker in a highlight five times + * the mean is two levels there, against eight on a linear scale. A zone + * moves when it differs from its scaled background by more than its + * threshold in those levels on two frames running, which drops the + * single frames where the sensor applies exposure and gain apart. A change + * in most zones at once is a lamp switching: it is reported, and the + * background is learned again while auto exposure settles. + */ +#include +#include + +#include "tx-isp-motion.h" + +#define MD_DEFAULT_THRESHOLD 8 +#define MD_HOLD_FRAMES 12 /* about half a second */ +#define MD_TRACK_SHIFT 4 /* background follows a still zone */ +#define MD_TRACK_MOVING_SHIFT 5 /* and absorbs a stopped subject */ +#define MD_LEARN_FRAMES 24 /* about a second of fast tracking */ +#define MD_GAIN_ONE (1U << 12) + +static const struct v4l2_ctrl_ops t23_motion_ops; + +static const struct v4l2_ctrl_config t23_motion_threshold_grid = { + .ops = &t23_motion_ops, + .id = V4L2_CID_DETECT_MD_THRESHOLD_GRID, + .dims = { T23_ISP_GRID_ROWS, T23_ISP_GRID_COLS }, + .max = 255, + .step = 1, + .def = MD_DEFAULT_THRESHOLD, +}; + +static const struct v4l2_ctrl_config t23_motion_region_grid = { + .ops = &t23_motion_ops, + .id = V4L2_CID_DETECT_MD_REGION_GRID, + .dims = { T23_ISP_GRID_ROWS, T23_ISP_GRID_COLS }, + .max = T23_MOTION_REGIONS - 1, + .step = 1, +}; + +static void t23_motion_queue(struct t23_motion *md, u32 mask, u32 sequence) +{ + struct v4l2_event ev = { + .type = V4L2_EVENT_MOTION_DET, + .u.motion_det = { + .flags = V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ, + .frame_sequence = sequence, + .region_mask = mask, + }, + }; + unsigned int i; + + md->mask = mask; + if (md->sd && md->sd->devnode) + v4l2_subdev_notify_event(md->sd, &ev); + for (i = 0; i < ARRAY_SIZE(md->vdev); i++) + if (md->vdev[i] && video_is_registered(md->vdev[i])) + v4l2_event_queue(md->vdev[i], &ev); +} + +void t23_motion_reset(struct t23_motion *md) +{ + md->valid = false; + memset(md->hold, 0, sizeof(md->hold)); + if (md->mask) + t23_motion_queue(md, 0, 0); +} + +static int t23_motion_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct t23_motion *md = ctrl->priv; + + switch (ctrl->id) { + case V4L2_CID_DETECT_MD_MODE: + md->mode = ctrl->val; + t23_motion_reset(md); + break; + case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD: + md->threshold = ctrl->val; + break; + case V4L2_CID_DETECT_MD_THRESHOLD_GRID: + memcpy(md->cell_threshold, ctrl->p_new.p_u16, + sizeof(md->cell_threshold)); + break; + case V4L2_CID_DETECT_MD_REGION_GRID: + memcpy(md->cell_region, ctrl->p_new.p_u8, + sizeof(md->cell_region)); + break; + default: + return -EINVAL; + } + return 0; +} + +static const struct v4l2_ctrl_ops t23_motion_ops = { + .s_ctrl = t23_motion_s_ctrl, +}; + +void t23_motion_init(struct t23_motion *md, struct v4l2_ctrl_handler *hdl, + struct v4l2_subdev *sd, + struct video_device *main, struct video_device *scaled0, + struct video_device *scaled1) +{ + struct v4l2_ctrl *ctrls[4]; + unsigned int i; + + md->sd = sd; + md->vdev[0] = main; + md->vdev[1] = scaled0; + md->vdev[2] = scaled1; + md->mode = V4L2_DETECT_MD_MODE_DISABLED; + md->threshold = MD_DEFAULT_THRESHOLD; + for (i = 0; i < T23_ISP_ZONES; i++) + md->cell_threshold[i] = MD_DEFAULT_THRESHOLD; + ctrls[0] = v4l2_ctrl_new_std_menu(hdl, &t23_motion_ops, + V4L2_CID_DETECT_MD_MODE, + V4L2_DETECT_MD_MODE_REGION_GRID, 0, + V4L2_DETECT_MD_MODE_DISABLED); + ctrls[1] = v4l2_ctrl_new_std(hdl, &t23_motion_ops, + V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD, + 1, 255, 1, MD_DEFAULT_THRESHOLD); + ctrls[2] = v4l2_ctrl_new_custom(hdl, &t23_motion_threshold_grid, NULL); + ctrls[3] = v4l2_ctrl_new_custom(hdl, &t23_motion_region_grid, NULL); + for (i = 0; i < ARRAY_SIZE(ctrls); i++) + if (ctrls[i]) + ctrls[i]->priv = md; +} + +/* Median of @n values, reordering them */ +static u32 t23_motion_median(u32 *v, int n) +{ + int lo = 0, hi = n - 1, k = n / 2; + + while (lo < hi) { + u32 pivot = v[(lo + hi) / 2]; + int i = lo, j = hi; + + while (i <= j) { + while (v[i] < pivot) + i++; + while (v[j] > pivot) + j--; + if (i <= j) + swap(v[i++], v[j--]); + } + if (k <= j) + hi = j; + else if (k >= i) + lo = i; + else + break; + } + return v[k]; +} + +/* Luma sum of zone @i: the low, middle and high band sums. */ +static u32 t23_motion_luma(const u32 *ae, unsigned int i) +{ + const u32 *w = ae + i * 4; + + return (w[0] & 0x1fffff) + ((w[1] & 0x3ff) << 11 | w[0] >> 21) + + ((w[1] & 0x7ffffc00) >> 10); +} + +/* 16 * 128 * sqrt(@sum / mean) for @scale = 2^48 / mean, mean in Q8 */ +static u32 t23_motion_level(u32 sum, u32 scale) +{ + u32 ratio = min_t(u64, (u64)sum * scale >> 28, 1U << 20); /* Q12 */ + + return int_sqrt(ratio << 10); +} + +void t23_motion_frame(struct t23_motion *md, const u32 *ae, u32 sequence) +{ + u32 *luma = md->luma, gain, scale, regions = 0, mask = 0, moved = 0; + u64 mean = 0; + unsigned int i; + s32 mode = READ_ONCE(md->mode); + + if (mode == V4L2_DETECT_MD_MODE_DISABLED) + return; + for (i = 0; i < T23_ISP_ZONES; i++) + luma[i] = t23_motion_luma(ae, i); + if (!md->valid) { + for (i = 0; i < T23_ISP_ZONES; i++) + md->background[i] = luma[i] << 8; + bitmap_zero(md->over, T23_ISP_ZONES); + md->learning = MD_LEARN_FRAMES; + md->valid = true; + return; + } + + for (i = 0; i < T23_ISP_ZONES; i++) + md->ratio[i] = (luma[i] << 8) / max(md->background[i] >> 12, 1U); + gain = t23_motion_median(md->ratio, T23_ISP_ZONES); + /* A day/night switch or a lamp halves or doubles the whole frame. */ + if (gain > 2 * MD_GAIN_ONE || gain < MD_GAIN_ONE / 2) { + md->valid = false; + return; + } + for (i = 0; i < T23_ISP_ZONES; i++) { + md->background[i] = min_t(u64, (u64)md->background[i] * gain >> 12, + U32_MAX); + mean += md->background[i]; + } + mean = div_u64(mean, T23_ISP_ZONES); + /* Below a zone mean of one level the frame is black. */ + if (mean <= 1U << 16) + return; + scale = div64_u64(1ULL << 48, mean); + + for (i = 0; i < T23_ISP_ZONES; i++) { + s64 delta = ((s64)luma[i] << 8) - md->background[i]; + u32 threshold = mode == V4L2_DETECT_MD_MODE_GLOBAL ? + md->threshold : md->cell_threshold[i]; + u32 now = t23_motion_level(luma[i], scale); + u32 then = t23_motion_level(md->background[i] >> 8, scale); + u32 level = abs((s32)(now - then)); + bool over = threshold && level > threshold * 16 && !md->learning; + bool moving = over && test_bit(i, md->over); + unsigned int shift = md->learning ? 1 : moving ? + MD_TRACK_MOVING_SHIFT : MD_TRACK_SHIFT; + + if (moving) { + regions |= BIT(mode == V4L2_DETECT_MD_MODE_REGION_GRID ? + md->cell_region[i] : 0); + moved++; + } + __assign_bit(i, md->over, over); + md->background[i] += delta >> shift; + } + if (md->learning) + md->learning--; + if (moved > T23_ISP_ZONES / 2) + md->valid = false; + for (i = 0; i < T23_MOTION_REGIONS; i++) { + if (regions & BIT(i)) + md->hold[i] = MD_HOLD_FRAMES; + else if (md->hold[i]) + md->hold[i]--; + if (md->hold[i]) + mask |= BIT(i); + } + if (mask != md->mask) + t23_motion_queue(md, mask, sequence); +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-motion.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-motion.h new file mode 100644 index 0000000000..6aaf8ce5be --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-motion.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_MOTION_H__ +#define __TX_ISP_MOTION_H__ + +#include +#include +#include +#include +#include + +#define T23_MOTION_REGIONS 32 + +struct t23_motion { + struct v4l2_subdev *sd; /* ISP subdev receiving events */ + struct video_device *vdev[3]; /* nodes that receive the events */ + s32 mode; /* enum v4l2_detect_md_mode */ + u16 threshold; + u16 cell_threshold[T23_ISP_ZONES]; + u8 cell_region[T23_ISP_ZONES]; + u32 background[T23_ISP_ZONES]; /* zone luma sum, Q8 */ + u32 luma[T23_ISP_ZONES]; /* this frame's zone luma sums */ + u32 ratio[T23_ISP_ZONES]; /* scratch for the frame gain */ + DECLARE_BITMAP(over, T23_ISP_ZONES); /* zones over threshold last frame */ + u8 hold[T23_MOTION_REGIONS]; /* frames left to report a region */ + u8 learning; /* frames left before reporting */ + u32 mask; /* regions last reported */ + bool valid; +}; + +void t23_motion_init(struct t23_motion *md, struct v4l2_ctrl_handler *hdl, + struct v4l2_subdev *sd, + struct video_device *main, struct video_device *scaled0, + struct video_device *scaled1); +void t23_motion_reset(struct t23_motion *md); +/* @ae: the input's AE statistics as the ISP wrote them */ +void t23_motion_frame(struct t23_motion *md, const u32 *ae, u32 sequence); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-output.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-output.c new file mode 100644 index 0000000000..b8edc13489 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-output.c @@ -0,0 +1,742 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#include +#include + +#include "tx-isp-device.h" + +struct t23_isp_reg { + u32 offset; + u32 value; +}; + +/* SC1A4T linear-mode demosaic coefficients. */ +static const struct t23_isp_reg dmsc_linear[] = { + { 0x4800, 0x00000000 }, + { 0x4854, 0x00001fff }, + { 0x4858, 0x00001fff }, + { 0x486c, 0x00001fff }, + { 0x4870, 0x00001fff }, + { 0x4890, 0x00200820 }, + { 0x48b0, 0x08080601 }, + { 0x48b4, 0x08000233 }, + { 0x4944, 0x00000000 }, + { 0x4950, 0x00000000 }, + { 0x4954, 0x00000000 }, + { 0x4958, 0x00000000 }, + { 0x495c, 0x00000000 }, + { 0x4960, 0x00000000 }, + { 0x4964, 0x00000000 }, + { 0x4968, 0x00000000 }, + { 0x496c, 0x00000000 }, + { 0x4970, 0x00000000 }, + { 0x4974, 0x00000000 }, + { 0x4978, 0x00000000 }, + { 0x497c, 0x00000000 }, + { 0x4984, 0x04000400 }, + { 0x49a8, 0x00000000 }, + { 0x49c4, 0x0fff00c8 }, + { 0x49d0, 0x0fff00c8 }, + { 0x49dc, 0x0fff00c8 }, + { 0x49e0, 0x0000001f }, + { 0x49f8, 0x00010010 }, + { 0x4804, 0x000078c8 }, + { 0x4808, 0x00c80078 }, + { 0x480c, 0x00640064 }, + { 0x4810, 0x00000058 }, + { 0x4814, 0x00000000 }, + { 0x4820, 0x7f6404b0 }, + { 0x4824, 0x00000000 }, + { 0x4828, 0x7f6404b0 }, + { 0x482c, 0x00000000 }, + { 0x4830, 0x7f6404b0 }, + { 0x4834, 0x07d3feb8 }, + { 0x4838, 0x0fa0001e }, + { 0x483c, 0x000000fe }, + { 0x4840, 0x00040100 }, + { 0x4844, 0x00100600 }, + { 0x4848, 0x00640078 }, + { 0x484c, 0x00c800c8 }, + { 0x4850, 0x0fff0000 }, + { 0x485c, 0x00080080 }, + { 0x4860, 0x005a005a }, + { 0x4864, 0x00200020 }, + { 0x4868, 0x0fff0000 }, + { 0x4874, 0x00fe0000 }, + { 0x4878, 0x00fe0064 }, + { 0x487c, 0x000003df }, + { 0x4880, 0x00100000 }, + { 0x4884, 0x00fe00c8 }, + { 0x4888, 0x07d000c0 }, + { 0x488c, 0x033d0f9f }, + { 0x489c, 0x00640f3c }, + { 0x48a4, 0x0fff003c }, + { 0x48ac, 0x0000003c }, + { 0x48b8, 0x00c82420 }, + { 0x48c4, 0x00002003 }, + { 0x4948, 0x0f9f0000 }, + { 0x494c, 0x0f9f0000 }, + { 0x4980, 0x00000000 }, + { 0x4988, 0x0320000a }, + { 0x49ac, 0x00060005 }, + { 0x49b0, 0x00280090 }, + { 0x49b4, 0x000a0014 }, + { 0x49b8, 0x00000fff }, + { 0x49bc, 0x00000014 }, + { 0x49c0, 0x0008003c }, + { 0x49c8, 0x00000078 }, + { 0x49cc, 0x0008003c }, + { 0x49d4, 0x000000f0 }, + { 0x49d8, 0x0008003c }, + { 0x49e4, 0x0fff012c }, + { 0x49e8, 0x002b0113 }, + { 0x49ec, 0x00000000 }, + { 0x49f0, 0x0fff0000 }, + { 0x49f4, 0x0000012c }, + { 0x49fc, 0x000f0000 }, + { 0x4a00, 0x00000000 }, + { 0x4a04, 0x000ff020 }, + { 0x4a08, 0x00000030 }, + { 0x48c8, 0x00000000 }, + { 0x48cc, 0x00000000 }, + { 0x48d0, 0x00000000 }, + { 0x48d4, 0x00000000 }, + { 0x48d8, 0x00000000 }, + { 0x48dc, 0x00000000 }, + { 0x48e0, 0x00000000 }, + { 0x48e4, 0x00000000 }, + { 0x48f4, 0x1d65540c }, + { 0x48f8, 0x208207de }, + { 0x48fc, 0x20820820 }, + { 0x4900, 0x20820820 }, + { 0x4904, 0x20820820 }, + { 0x4908, 0x20820820 }, + { 0x490c, 0x00000820 }, + { 0x491c, 0x10820820 }, + { 0x4920, 0x02208410 }, + { 0x4924, 0x00000042 }, + { 0x4928, 0x00000000 }, + { 0x492c, 0x00000000 }, + { 0x4930, 0x00000000 }, + { 0x4934, 0x00000000 }, + { 0x4a0c, 0x01000100 }, + { 0x4a10, 0x01000100 }, + { 0x4a14, 0x01000100 }, + { 0x4a18, 0x01000100 }, + { 0x4a1c, 0x01000100 }, + { 0x4a20, 0x01000100 }, + { 0x4a24, 0x01000100 }, + { 0x4a28, 0x01000100 }, + { 0x4a2c, 0x01000100 }, + { 0x4a30, 0x01000100 }, + { 0x4a34, 0x01000100 }, + { 0x4a38, 0x01000100 }, + { 0x4a3c, 0x01000100 }, + { 0x4a40, 0x01000100 }, + { 0x4a44, 0x01000100 }, + { 0x4a48, 0x01000100 }, + { 0x4a4c, 0x01000100 }, + { 0x4a50, 0x01000100 }, + { 0x4a54, 0x01000100 }, + { 0x4a58, 0x01000100 }, + { 0x4a5c, 0x01000100 }, + { 0x499c, 0x00000001 }, +}; + +/* + * Scaler filter kernel: 257 samples from the tap centre to two taps out, + * 1536 at the centre. + */ +static const s16 msca_kernel[257] = { + 1535, 1534, 1532, 1528, 1523, 1516, 1507, 1497, + 1486, 1473, 1459, 1444, 1427, 1409, 1389, 1369, + 1347, 1324, 1300, 1274, 1248, 1221, 1194, 1165, + 1135, 1105, 1074, 1043, 1011, 979, 946, 913, + 880, 847, 813, 779, 745, 711, 678, 645, + 612, 579, 546, 514, 483, 452, 421, 391, + 361, 332, 304, 277, 251, 225, 200, 175, + 152, 130, 109, 89, 69, 50, 32, 16, + 0, -15, -29, -41, -53, -64, -75, -84, + -92, -99, -106, -112, -117, -121, -125, -128, + -130, -131, -132, -132, -132, -131, -130, -128, + -126, -124, -121, -118, -114, -110, -106, -102, + -98, -94, -89, -84, -79, -75, -70, -65, + -60, -55, -51, -47, -43, -39, -35, -31, + -27, -24, -21, -18, -15, -12, -10, -8, + -7, -5, -4, -3, -2, -1, 0, 0, + 0, 0, 0, 0, -1, -2, -3, -4, + -5, -7, -8, -9, -10, -12, -14, -16, + -17, -19, -20, -22, -23, -25, -26, -27, + -28, -30, -31, -32, -33, -34, -34, -35, + -35, -36, -36, -36, -36, -36, -36, -36, + -35, -34, -33, -33, -32, -31, -29, -28, + -27, -26, -24, -23, -21, -20, -18, -16, + -14, -13, -11, -9, -7, -5, -3, -1, + 0, 2, 3, 5, 6, 8, 9, 11, + 12, 13, 14, 15, 16, 17, 18, 19, + 19, 20, 20, 21, 21, 21, 21, 21, + 21, 21, 21, 21, 20, 20, 19, 19, + 18, 18, 17, 16, 15, 15, 14, 13, + 12, 12, 11, 10, 9, 8, 7, 6, + 6, 6, 5, 4, 3, 2, 2, 2, + 2, 2, 1, 0, 0, 0, 0, 0, + 0, +}; + +#define MSCA_PHASES 8 +#define MSCA_COEFFS (2 * MSCA_PHASES + 1) +#define MSCA_COEFF_WORDS ((MSCA_COEFFS + 1) / 2) +#define MSCA_STEP_ONE 0x4000 /* output/input ratio of 1 */ +#define MSCA_RATIO_MAX 0x6aaa + +/* + * Sample the kernel for an output/input @ratio. Downscaling stretches it + * over more input pixels, which band-limits the output. + */ +static void msca_kernel_sample(u32 ratio, s16 coeff[MSCA_COEFFS]) +{ + u32 step = ratio / 2, start = 0, end = 0x400, pos, frac; + unsigned int i = 0, seg = 0; + s32 v; + + while (i < MSCA_COEFFS) { + pos = i * step; + if (pos >= end) { + start = end; + end += 0x400; + seg++; + continue; + } + frac = pos - start; + v = msca_kernel[seg + 1] * (s32)frac + + msca_kernel[seg] * (s32)(0x400 - frac); + coeff[i++] = (v >> 10) + ((v >> 9) & 1); + } +} + +/* Scale the four taps of each phase to a gain of exactly 2048. */ +static void msca_kernel_normalize(s16 coeff[MSCA_COEFFS]) +{ + static const int offset[4] = { -MSCA_PHASES, 0, MSCA_PHASES, 2 * MSCA_PHASES }; + static const u8 raise[4] = { 1, 2, 0, 3 }, lower[4] = { 3, 0, 2, 1 }; + s16 out[MSCA_COEFFS] = { 0 }; + unsigned int phase, k; + + for (phase = 0; phase < MSCA_PHASES; phase++) { + int tap[4], v[4], sum = 0, error = -2048; + + for (k = 0; k < 4; k++) { + tap[k] = abs(offset[k] - (int)phase); + sum += coeff[tap[k]]; + } + for (k = 0; k < 4; k++) { + int half = coeff[tap[k]] < 0 ? -(sum / 2) : sum / 2; + + v[k] = (coeff[tap[k]] * 2048 + half) / sum; + error += v[k]; + } + /* Rounding leaves at most two units over or under. */ + for (k = 0; error < 0; k++, error++) + v[raise[k]]++; + for (k = 0; error > 0; k++, error--) + v[lower[k]]--; + for (k = 0; k < 4; k++) + out[tap[k]] = v[k]; + } + memcpy(coeff, out, sizeof(out)); +} + +/* Queue one direction's filter on the indirect port at @address. */ +static void msca_write_filter(void __iomem *base, u32 address, u32 ratio) +{ + s16 coeff[MSCA_COEFFS + 1]; + unsigned int i; + + msca_kernel_sample(min_t(u32, ratio, MSCA_RATIO_MAX), coeff); + msca_kernel_normalize(coeff); + coeff[MSCA_COEFFS] = 0; + for (i = 0; i < MSCA_COEFF_WORDS; i++) { + writel((coeff[2 * i] & (u16)MSCA_COEF_LO) | + (u32)(coeff[2 * i + 1] & (u16)MSCA_COEF_LO) << 16, + base + MSCA_SHD_FIFO); + writel(address + i * 4, base + MSCA_SHD_FIFO); + } +} + +static const struct t23_isp_reg csc_bt601_full[] = { + { 0x6000, 0x0000001f }, + { 0x6004, 0x00000000 }, + { 0x6010, 0x07596532 }, + { 0x6014, 0x20054cad }, + { 0x6018, 0x0536b600 }, + { 0x6020, 0x00008000 }, + { 0x6030, 0xff00ff00 }, +}; + +void tx_isp_core_dmsc_defaults(u32 values[3]) +{ + values[0] = ISP_DMSC_REG_08_DEFAULT; + values[1] = ISP_DMSC_REG_48_DEFAULT; + values[2] = ISP_DMSC_REG_60_DEFAULT; +} + +static void t23_write_regs(void __iomem *base, + const struct t23_isp_reg *regs, unsigned int count) +{ + unsigned int i; + + for (i = 0; i < count; i++) + writel(regs[i].value, base + regs[i].offset); +} + +static void t23_msca_aux_init(void __iomem *base) +{ + unsigned int ch, mask, offset; + + /* No mask, line or OSD plane owns a buffer at stream start. */ + for (ch = 0; ch < 3; ch++) { + for (mask = 0; mask < 4; mask++) { + offset = MSCA_MASK(ch, mask); + writel(0, base + offset); + writel(0, base + offset + 8); + writel(0, base + offset + 4); + } + writel(0, base + MSCA_MASK_EN); + } + for (offset = MSCA_LINE(0); offset <= MSCA_LINE(MSCA_LINES - 1) + 0xc; offset += 4) + writel(0, base + offset); + writel(0, base + MSCA_OSD_ORDER); + for (offset = MSCA_OSD_BLOCK(0); offset <= MSCA_OSD_BLOCK(MSCA_OSD_BLOCKS - 1) + 0xc; offset += 4) + writel(0, base + offset); + writel(0, base + MSCA_OSD_EN); + writel(MSCA_OSD_CTRL_DEFAULT, base + MSCA_OSD_CTRL); +} + +/* Channel disable: hold the line flush, drop the channel bit, trigger. */ +static void t23_msca_channel_disable(void __iomem *base, unsigned int ch) +{ + writel(1, base + MSCA_Y_RESET(ch)); + writel(1, base + MSCA_UV_RESET(ch)); + writel(readl(base + MSCA_CH_EN_MASK) & ~MSCA_CH_EN(ch), + base + MSCA_CH_EN_MASK); + writel(1, base + MSCA_SHD_TRIGGER); + writel(1, base + MSCA_CH_REG_4C(ch)); + writel(1, base + MSCA_CH_REG_6C(ch)); +} + +static int t23_msca_channel_configure(struct tx_isp_subdev *sd) +{ + struct tx_isp_core_device *core = sd->dev_priv; + void __iomem *base = sd->base; + u32 width = core->geometry >> 16; + u32 height = core->geometry & 0xffff; + u32 posted = ((height & 0x1fff) << 16) | (width & 0xfff); + unsigned int ch; + + /* + * Channel 0 stays disabled. Its size registers keep the 1920x1080 + * reset value until a VIC frame commits the shadow. + */ + writel(0, base + MSCA_FCROP_POS); + writel(posted, base + MSCA_FCROP_SIZE); + writel(MSCA_GEOM(1280, 720), base + MSCA_CH_SCALE_SIZE(1)); + writel(MSCA_GEOM(1280, 720), base + MSCA_CH_CROP_SIZE(1)); + writel(MSCA_GEOM(1280, 720), base + MSCA_CH_SCALE_SIZE(2)); + writel(MSCA_GEOM(1280, 720), base + MSCA_CH_CROP_SIZE(2)); + for (ch = 0; ch < 3; ch++) + t23_msca_channel_disable(base, ch); + writel(ISP_TOP_REG_33C_DEFAULT, base + ISP_TOP_REG_33C); + dev_dbg_once(&g_tx_isp_dev->pdev->dev, + "t23 MSCA post: run=%08x wrote=%08x geom=%08x ch1=%08x mode=%08x trig=%08x en=%08x\n", + readl(base + ISP_IPC_TRIGGER), posted, readl(base + MSCA_FCROP_SIZE), + readl(base + MSCA_CH_SCALE_SIZE(1)), readl(base + MSCA_REG_060), + readl(base + MSCA_SHD_TRIGGER), + readl(base + MSCA_CH_EN_MASK)); + t23_msca_aux_init(base); + return 0; +} + +int tx_isp_core_output_init(struct tx_isp_subdev *sd) +{ + void __iomem *base = sd->base; + unsigned int ch; + + writel(0, base + MSCA_CH_EN_MASK); + for (ch = 0; ch < 3; ch++) { + writel(1, base + MSCA_Y_RESET(ch)); + writel(1, base + MSCA_UV_RESET(ch)); + } + writel(ISP_DGAIN_LUT_PARAM0_DEFAULT, base + ISP_DGAIN_LUT_PARAM0); + writel(ISP_DGAIN_LUT_PARAM1_DEFAULT, base + ISP_DGAIN_LUT_PARAM1); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(0, base + ISP_DGAIN_BLC3); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_GAIN_ONE, base + ISP_DGAIN_GAIN01); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_GAIN_ONE, base + ISP_DGAIN_GAIN23); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_BLC_ONE, base + ISP_DGAIN_BLC0); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_BLC_PAIR_ONE, base + ISP_DGAIN_BLC1); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(ISP_DGAIN_BLC_ONE, base + ISP_DGAIN_BLC2); + t23_write_regs(base, dmsc_linear, ARRAY_SIZE(dmsc_linear)); + t23_write_regs(base, csc_bt601_full, ARRAY_SIZE(csc_bt601_full)); + return t23_msca_channel_configure(sd); +} + +void tx_isp_core_restore_dmsc(struct tx_isp_subdev *sd) +{ + if (!sd || !sd->base) + return; + t23_write_regs(sd->base, dmsc_linear, ARRAY_SIZE(dmsc_linear)); +} + +static void t23_msca_snapshot(struct tx_isp_subdev *sd, const char *stage) +{ + void __iomem *channel = sd->base; + + pr_debug("t23 MSCA %s: run=%08x mode=%08x geom=%08x/%08x/%08x shd_trig=%08x shd_dma=%08x/%08x en=%08x\n", + stage, readl(sd->base + ISP_IPC_TRIGGER), readl(sd->base + MSCA_REG_060), + readl(sd->base + MSCA_FCROP_SIZE), readl(channel + MSCA_CH_SCALE_SIZE(0)), + readl(channel + MSCA_CH_CROP_SIZE(0)), readl(sd->base + MSCA_SHD_TRIGGER), + readl(sd->base + MSCA_SHD_DMA), readl(sd->base + MSCA_SHD_STAT), + readl(sd->base + MSCA_CH_EN_MASK)); + pr_debug("t23 ISP blocks %s: gib=%08x dmsc=%08x csc=%08x/%08x awb=%08x key=%08x\n", + stage, readl(sd->base + ISP_DGAIN_GAIN01), readl(sd->base + ISP_DMSC_REG_04), + readl(sd->base + ISP_CSC_CONTROL), readl(sd->base + ISP_CSC_COEFF0), + readl(sd->base + ISP_AWB_LUM_TH_FREQ), readl(sd->base + ISP_TOP_REG_33C)); + pr_debug("t23 MSCA config %s: crop=%08x/%08x step=%08x/%08x out=%08x stride=%08x/%08x reg50=%08x\n", + stage, readl(channel + MSCA_CH_FCROP_POS(0)), readl(channel + MSCA_CH_CROP_POS(0)), + readl(channel + MSCA_CH_VSTEP(0)), readl(channel + MSCA_CH_HSTEP(0)), + readl(channel + MSCA_CH_OUT(0)), readl(channel + MSCA_Y_STRIDE(0)), + readl(channel + MSCA_UV_STRIDE(0)), readl(sd->base + MSCA_REG_050)); +} + +/* Filters of the enabled channels, loaded by the shadow DMA. */ +static void msca_write_filters(struct tx_isp_subdev *sd, + const struct tx_isp_msca_target out[]) +{ + struct tx_isp_core_device *core = sd->dev_priv; + u32 width = core->geometry >> 16, height = core->geometry & 0xffff; + unsigned int ch, entries = 0; + + writel(MSCA_SHD_CTRL_DEFAULT_BEGIN, sd->base + MSCA_SHD_CTRL); + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + if (!out[ch].width) + continue; + msca_write_filter(sd->base, MSCA_SHD_VCOEF(ch), + (out[ch].height << 14) / height); + msca_write_filter(sd->base, MSCA_SHD_HCOEF(ch), + (out[ch].width << 14) / width); + entries += 2 * MSCA_COEFF_WORDS; + } + writel(MSCA_SHD_CTRL_DEFAULT_END, sd->base + MSCA_SHD_CTRL); + writel(((entries - 1) << 4) | MSCA_SHD_DMA_START, sd->base + MSCA_SHD_DMA); +} + +/* OSD pixel formats: type in bits 0-1 of MSCA_OSD_CTRL, and colour order */ +static const struct { + u32 fourcc; + u32 ctrl; /* bit 8 ignores the pixel alpha */ + u32 order; +} msca_osd_formats[] = { + { V4L2_PIX_FMT_ABGR32, 0, 0 }, /* bytes B, G, R, A */ + { V4L2_PIX_FMT_XBGR32, BIT(8), 0 }, + { V4L2_PIX_FMT_ARGB32, 0, 13 }, /* bytes A, R, G, B */ + { V4L2_PIX_FMT_XRGB32, BIT(8), 13 }, + { V4L2_PIX_FMT_ARGB555, 1, 0 }, + { V4L2_PIX_FMT_XRGB555, 1 | BIT(8), 0 }, +}; + +static int msca_osd_format(u32 fourcc) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(msca_osd_formats); i++) + if (msca_osd_formats[i].fourcc == fourcc) + return i; + return -EINVAL; +} + +static int msca_check_overlay(const struct tx_isp_msca_overlay *ov, + u32 width, u32 height) +{ + unsigned int n; + + for (n = 0; n < T23_MSCA_OSD_REGIONS; n++) { + const struct tx_isp_msca_osd *osd = &ov->osd[n]; + const struct tx_isp_rect *r = &osd->rect; + + if (!osd->addr) + continue; + if (msca_osd_format(ov->osd_format) < 0 || osd->addr & 7 || + !r->width || !r->height || r->left + r->width > width || + r->top + r->height > height) + return -EINVAL; + } + return 0; +} + +/* Scale input span [@start, @end) to an output of @out pixels from @in. */ +static void msca_scale_span(u32 start, u32 end, u32 in, u32 out, + u32 *first, u32 *last) +{ + *first = min(start, in) * out / in; + *last = DIV_ROUND_UP(min(end, in) * out, in); +} + +static void msca_write_line(void __iomem *base, unsigned int n, + const struct tx_isp_line *ln, u32 color, + u32 width, u32 height) +{ + u32 x0, y0, x1, y1, lw, dx, ady, octant, slope; + s32 dy; + + if (!ln->width || !width || !height) { + writel(0, base + MSCA_LINE_CTRL(n)); + return; + } + x0 = min_t(u32, ln->x0, width - 1); + y0 = min_t(u32, ln->y0, height - 1); + x1 = min_t(u32, ln->x1, width - 1); + y1 = min_t(u32, ln->y1, height - 1); + if (x0 == x1 && y0 == y1) { + writel(0, base + MSCA_LINE_CTRL(n)); + return; + } + if (x1 < x0 || (x0 == x1 && y1 < y0)) { + swap(x0, x1); + swap(y0, y1); + } + lw = min_t(u32, ln->width, 255); + dx = x1 - x0; + dy = (s32)y1 - (s32)y0; + ady = abs(dy); + + writel(x0 << 16 | y0, base + MSCA_LINE_START(n)); + writel(FIELD_PREP(MSCA_LINE_COLOR_WIDTH, lw) | color, + base + MSCA_LINE_COLOR(n)); + if (!dx || !ady) { + writel((dx ?: lw) << 16 | (ady ?: lw), + base + MSCA_LINE_END(n)); + writel(1, base + MSCA_LINE_CTRL(n)); + return; + } + octant = (dy < 0 ? 1 : 0) | (dx < ady ? 2 : 0); + slope = dx >= ady ? (ady << 8) / dx : (dx << 8) / ady; + writel(dx << 16 | ady, base + MSCA_LINE_END(n)); + writel(FIELD_PREP(MSCA_LINE_CTRL_OCTANT, octant) | + FIELD_PREP(MSCA_LINE_CTRL_SLOPE, slope) | + MSCA_LINE_CTRL_DDA_EN | 1, + base + MSCA_LINE_CTRL(n)); +} + +static void msca_write_overlay(struct tx_isp_subdev *sd, + const struct tx_isp_msca_target out[], + const struct tx_isp_msca_overlay *ov) +{ + struct tx_isp_core_device *core = sd->dev_priv; + u32 width = core->geometry >> 16, height = core->geometry & 0xffff; + const struct tx_isp_rect *r; + unsigned int ch, n; + u32 masks = 0, blocks = 0; + int format; + + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + if (!out[ch].width) + continue; + for (n = 0; n < T23_MSCA_MASKS; n++) { + void __iomem *reg = sd->base + MSCA_MASK(ch, n); + u32 x0, x1, y0, y1; + + r = &ov->mask[n]; + msca_scale_span(r->left, r->left + r->width, width, + out[ch].width, &x0, &x1); + msca_scale_span(r->top, r->top + r->height, height, + out[ch].height, &y0, &y1); + if (x1 <= x0 || y1 <= y0) { + writel(0, reg + 4); + continue; + } + writel(x0 << 16 | y0, reg); + writel((x1 - x0) << 16 | (y1 - y0), reg + 4); + writel(ov->mask_yuv, reg + 8); + masks |= MSCA_MASK_EN_CH(ch); + } + } + writel(masks, sd->base + MSCA_MASK_EN); + + for (n = 0; n < T23_MSCA_LINES; n++) + msca_write_line(sd->base, n, &ov->line[n], ov->line_yuv, + out[0].width, out[0].height); + + for (n = 0; n < T23_MSCA_OSD_REGIONS; n++) { + const struct tx_isp_msca_osd *osd = &ov->osd[n]; + + if (!osd->addr) + continue; + r = &osd->rect; + writel(r->left << 16 | r->top, sd->base + MSCA_OSD_POS(n)); + writel(r->width << 16 | r->height, sd->base + MSCA_OSD_SIZE(n)); + writel(osd->addr, sd->base + MSCA_OSD_ADDR(n)); + writel(osd->stride, sd->base + MSCA_OSD_STRIDE(n)); + blocks |= MSCA_OSD_EN_BLOCK(n); + } + if (blocks) { + format = msca_osd_format(ov->osd_format); + writel(msca_osd_formats[format].ctrl | MSCA_OSD_CTRL_DEFAULT, + sd->base + MSCA_OSD_CTRL); + writel(msca_osd_formats[format].order, sd->base + MSCA_OSD_ORDER); + } + writel(blocks, sd->base + MSCA_OSD_EN); +} + +int tx_isp_core_dma_arm(struct tx_isp_subdev *sd, + const struct tx_isp_msca_target out[T23_MSCA_OUTPUTS], + const struct tx_isp_msca_overlay *overlay, + unsigned int input) +{ + struct tx_isp_core_device *core = sd->dev_priv; + u32 width = core->geometry >> 16, height = core->geometry & 0xffff; + u32 reg50 = MSCA_REG_050_DEFAULT; + void __iomem *channel; + unsigned int ch; + u32 mask = 0; + int ret; + + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + if (!out[ch].width) + continue; + /* The FIFOs drop the low three address bits. */ + if ((out[ch].y | out[ch].uv) & 7 || out[ch].width > width || + out[ch].height > height || !out[ch].height) + return -EINVAL; + mask |= BIT(ch); + } + if (!mask) + return -EINVAL; + ret = msca_check_overlay(overlay, width, height); + if (ret) + return ret; + + ret = t23_isp_stats_configure(&core->stats, sd->base, input); + if (ret) + return ret; + WRITE_ONCE(core->stats_irq, 0); + ret = tx_isp_core_mdns_start(sd, input); + if (ret) + return ret; + t23_msca_snapshot(sd, "before-start"); + if ((readl(sd->base + MSCA_SHD_DMA) & MSCA_SHD_DMA_BUSY) || + (readl(sd->base + MSCA_SHD_STAT) >> 1)) + return -EBUSY; + msca_write_filters(sd, out); + /* + * Once a frame has committed the init shadow, width goes in the + * high half. Strides follow that trigger. + */ + writel(0, sd->base + MSCA_FCROP_POS); + writel(core->geometry, sd->base + MSCA_FCROP_SIZE); + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + u32 size = out[ch].width << 16 | out[ch].height; + + if (!out[ch].width) + continue; + channel = sd->base + MSCA_CH_BASE(ch); + writel(0, channel); + writel(size, channel + MSCA_CH_OFF_SCALE_SIZE); + writel(0, channel + MSCA_CH_OFF_CROP_POS); + writel(size, channel + MSCA_CH_OFF_CROP_SIZE); + writel(MSCA_SCALE_PARA0_DEFAULT, channel + MSCA_CH_OFF_SCALE_PARA0); + writel(MSCA_SCALE_PARA1_DEFAULT, channel + MSCA_CH_OFF_SCALE_PARA1); + writel((height << 14) / out[ch].height, + channel + MSCA_CH_OFF_VSTEP); + writel((width << 14) / out[ch].width, + channel + MSCA_CH_OFF_HSTEP); + if (out[ch].hflip) + reg50 |= MSCA_REG_050_MIRROR(ch); + if (out[ch].vflip) + reg50 |= MSCA_REG_050_FLIP(ch); + } + msca_write_overlay(sd, out, overlay); + writel(reg50, sd->base + MSCA_REG_050); + writel(0, sd->base + MSCA_SCALE_CTL_MODE); + writel(1, sd->base + MSCA_SHD_TRIGGER); + core->done_mask = 0; + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + core->armed[ch] = out[ch]; + channel = sd->base + MSCA_CH_BASE(ch); + if (!out[ch].width) { + writel(0, channel + MSCA_CH_OFF_Y_STRIDE); + writel(0, channel + MSCA_CH_OFF_UV_STRIDE); + continue; + } + writel(out[ch].width, channel + MSCA_CH_OFF_Y_STRIDE); + writel(out[ch].width, channel + MSCA_CH_OFF_UV_STRIDE); + writel(0, sd->base + MSCA_Y_RESET(ch)); + writel(0, sd->base + MSCA_UV_RESET(ch)); + writel(out[ch].y, sd->base + MSCA_Y_FIFO(ch)); + writel(out[ch].uv, sd->base + MSCA_UV_FIFO(ch)); + } + core->armed_mask = mask; + /* A channel left enabled would keep writing to its last buffer. */ + writel((readl(sd->base + MSCA_CH_EN_MASK) & + ~GENMASK(T23_MSCA_OUTPUTS - 1, 0)) | mask, + sd->base + MSCA_CH_EN_MASK); + writel(1, sd->base + MSCA_SHD_TRIGGER); + dev_dbg_once(&g_tx_isp_dev->pdev->dev, + "t23 MSCA arm: run=%08x mode=%08x geom=%08x/%08x/%08x en=%08x stride=%08x\n", + readl(sd->base + ISP_IPC_TRIGGER), readl(sd->base + MSCA_REG_060), + readl(sd->base + MSCA_FCROP_SIZE), readl(sd->base + MSCA_CH_SCALE_SIZE(0)), + readl(sd->base + MSCA_CH_CROP_SIZE(0)), readl(sd->base + MSCA_CH_EN_MASK), + readl(sd->base + MSCA_Y_STRIDE(0))); + t23_msca_snapshot(sd, "after-queue"); + return 0; +} + +void tx_isp_core_dma_stop(struct tx_isp_subdev *sd, bool abort) +{ + unsigned int ch, n; + + if (abort) { + writel(ISP_IPC_TRIGGER_STOP, sd->base + ISP_IPC_TRIGGER); + writel(0, sd->base + ISP_INT_MASK); + writel(0, sd->base + ISP_INT1_MASK); + } + for (ch = 0; ch < T23_MSCA_OUTPUTS; ch++) { + writel(1, sd->base + MSCA_Y_RESET(ch)); + writel(1, sd->base + MSCA_UV_RESET(ch)); + } + for (n = 0; n < MSCA_LINES; n++) + writel(0, sd->base + MSCA_LINE_CTRL(n)); + writel(0, sd->base + MSCA_CH_EN_MASK); + writel(0, sd->base + MSCA_MASK_EN); + writel(0, sd->base + MSCA_OSD_EN); + writel(1, sd->base + MSCA_SHD_TRIGGER); +} + +void tx_isp_core_park_channel(struct tx_isp_subdev *sd, unsigned int ch, + dma_addr_t scratch_dma) +{ + struct tx_isp_core_device *core = sd ? sd->dev_priv : NULL; + void __iomem *channel; + + if (!core || !sd->base || ch >= T23_MSCA_OUTPUTS) + return; + channel = sd->base + MSCA_CH_BASE(ch); + writel(0, channel + MSCA_CH_OFF_Y_STRIDE); + writel(0, channel + MSCA_CH_OFF_UV_STRIDE); + if (scratch_dma && !(scratch_dma & 7)) { + writel(scratch_dma, sd->base + MSCA_Y_FIFO(ch)); + writel(scratch_dma + 2048, sd->base + MSCA_UV_FIFO(ch)); + } + writel(1, sd->base + MSCA_Y_RESET(ch)); + writel(1, sd->base + MSCA_UV_RESET(ch)); + writel(readl(sd->base + MSCA_CH_EN_MASK) & ~MSCA_CH_EN(ch), + sd->base + MSCA_CH_EN_MASK); + writel(1, sd->base + MSCA_SHD_TRIGGER); + memset(&core->armed[ch], 0, sizeof(core->armed[ch])); + core->armed_mask &= ~BIT(ch); + core->done_mask &= ~BIT(ch); +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.c new file mode 100644 index 0000000000..8ab2098489 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.c @@ -0,0 +1,678 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Privacy masks, vector lines and on-screen display drawn by the T23 ISP + * output scaler. + * + * Each input has four mask rectangles and six vector lines, set through + * array controls on its capture nodes in sensor pixels; masks scale to + * every output while lines are drawn on MSCA channel 0. Each input also has + * eight OSD regions, each a video output node: the last queued bitmap is + * blended into the main output at the compose rectangle on every frame and + * is returned once a newer buffer replaces it. The hardware draws the OSD + * on MSCA channel 0 only, so it does not reach the scaled outputs, and + * takes one pixel format for all regions of a frame. Overlapping blocks + * corrupt the overlap and every later block, so a region that overlaps a + * lower-numbered one is not drawn. + */ +#include +#include +#include +#include + +#include "tx-isp-overlay.h" + +/* Largest frame the MSCA geometry registers describe */ +#define T23_OSD_MAX_SIZE 4095 + +struct t23_osd_buffer { + struct vb2_v4l2_buffer vb; + struct list_head list; +}; + +static inline struct t23_osd_buffer *to_t23_osd_buffer(struct vb2_buffer *vb) +{ + return container_of(to_vb2_v4l2_buffer(vb), struct t23_osd_buffer, vb); +} + +static const struct { + u32 fourcc; + u8 bpp; +} t23_osd_formats[] = { + { V4L2_PIX_FMT_ABGR32, 4 }, + { V4L2_PIX_FMT_XBGR32, 4 }, + { V4L2_PIX_FMT_ARGB32, 4 }, + { V4L2_PIX_FMT_XRGB32, 4 }, + { V4L2_PIX_FMT_ARGB555, 2 }, + { V4L2_PIX_FMT_XRGB555, 2 }, +}; + +/* Full-range BT.601, as the capture nodes report */ +static u32 t23_mask_yuv(u32 rgb) +{ + int r = rgb >> 16 & 0xff, g = rgb >> 8 & 0xff, b = rgb & 0xff; + int y = (77 * r + 150 * g + 29 * b + 128) >> 8; + int u = ((-43 * r - 85 * g + 128 * b + 128) >> 8) + 128; + int v = ((128 * r - 107 * g - 21 * b + 128) >> 8) + 128; + + return FIELD_PREP(MSCA_MASK_COLOR_Y, clamp(y, 0, 255)) | + FIELD_PREP(MSCA_MASK_COLOR_U, clamp(u, 0, 255)) | + FIELD_PREP(MSCA_MASK_COLOR_V, clamp(v, 0, 255)); +} + +static int t23_mask_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct t23_overlay_input *in = ctrl->priv; + struct t23_overlay *overlay = in->overlay; + const u16 *rect = ctrl->p_new.p_u16; + unsigned long flags; + unsigned int i; + + spin_lock_irqsave(&overlay->lock, flags); + switch (ctrl->id) { + case V4L2_CID_T23_PRIVACY_MASK: + for (i = 0; i < T23_MSCA_MASKS; i++, rect += 4) + in->mask[i] = (struct tx_isp_rect){ + rect[0], rect[1], rect[2], rect[3] + }; + break; + case V4L2_CID_T23_PRIVACY_MASK_COLOR: + in->mask_yuv = t23_mask_yuv(ctrl->val); + break; + case V4L2_CID_T23_DRAW_LINE: + for (i = 0; i < T23_MSCA_LINES; i++, rect += 5) + in->line[i] = (struct tx_isp_line){ + rect[0], rect[1], rect[2], rect[3], rect[4] + }; + break; + case V4L2_CID_T23_DRAW_LINE_COLOR: + in->line_yuv = t23_mask_yuv(ctrl->val); + break; + } + spin_unlock_irqrestore(&overlay->lock, flags); + return 0; +} + +static const struct v4l2_ctrl_ops t23_mask_ops = { + .s_ctrl = t23_mask_s_ctrl, +}; + +static const struct v4l2_ctrl_config t23_mask_rects = { + .ops = &t23_mask_ops, + .id = V4L2_CID_T23_PRIVACY_MASK, + .name = "Privacy Mask", + .type = V4L2_CTRL_TYPE_U16, + .dims = { T23_ISP_PRIVACY_MASKS, 4 }, + .max = T23_OSD_MAX_SIZE, + .step = 1, +}; + +static const struct v4l2_ctrl_config t23_mask_color = { + .ops = &t23_mask_ops, + .id = V4L2_CID_T23_PRIVACY_MASK_COLOR, + .name = "Privacy Mask Color", + .type = V4L2_CTRL_TYPE_INTEGER, + .max = 0xffffff, + .step = 1, +}; + +static const struct v4l2_ctrl_config t23_draw_lines = { + .ops = &t23_mask_ops, + .id = V4L2_CID_T23_DRAW_LINE, + .name = "Draw Line", + .type = V4L2_CTRL_TYPE_U16, + .dims = { T23_ISP_DRAW_LINES, 5 }, + .max = T23_OSD_MAX_SIZE, + .step = 1, +}; + +static const struct v4l2_ctrl_config t23_draw_line_color = { + .ops = &t23_mask_ops, + .id = V4L2_CID_T23_DRAW_LINE_COLOR, + .name = "Draw Line Color", + .type = V4L2_CTRL_TYPE_INTEGER, + .max = 0xffffff, + .step = 1, +}; + +void t23_overlay_init(struct t23_overlay *overlay, struct device *dev, + bool (*sync)(struct t23_overlay *, unsigned int)) +{ + unsigned int i, j; + + BUILD_BUG_ON(T23_ISP_PRIVACY_MASKS != T23_MSCA_MASKS); + BUILD_BUG_ON(T23_ISP_DRAW_LINES != T23_MSCA_LINES); + BUILD_BUG_ON(T23_MSCA_OSD_REGIONS != MSCA_OSD_BLOCKS); + overlay->dev = dev; + overlay->sync = sync; + spin_lock_init(&overlay->lock); + for (i = 0; i < T23_OVERLAY_INPUTS; i++) { + struct t23_overlay_input *in = &overlay->inputs[i]; + + in->overlay = overlay; + in->index = i; + in->mask_yuv = t23_mask_yuv(0); + in->line_yuv = t23_mask_yuv(0); + for (j = 0; j < T23_MSCA_OSD_REGIONS; j++) { + in->osd[j].input = in; + in->osd[j].index = j; + INIT_LIST_HEAD(&in->osd[j].bufs); + } + } +} + +void t23_overlay_init_controls(struct t23_overlay *overlay, unsigned int input, + struct v4l2_ctrl_handler *hdl) +{ + static const struct v4l2_ctrl_config * const cfgs[] = { + &t23_mask_rects, + &t23_mask_color, + &t23_draw_lines, + &t23_draw_line_color, + }; + struct v4l2_ctrl *ctrl; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(cfgs); i++) { + ctrl = v4l2_ctrl_new_custom(hdl, cfgs[i], NULL); + if (ctrl) + ctrl->priv = &overlay->inputs[input]; + } +} + +static int t23_osd_bpp(u32 fourcc) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(t23_osd_formats); i++) + if (t23_osd_formats[i].fourcc == fourcc) + return t23_osd_formats[i].bpp; + return 0; +} + +/* + * Even sizes and positions keep the bitmap on whole NV12 chroma samples. + * Lines start on 8-byte boundaries. + */ +static void t23_osd_try_format(const struct t23_overlay_input *in, + struct v4l2_pix_format *pix) +{ + u32 max_width = in->width ?: T23_OSD_MAX_SIZE; + u32 max_height = in->height ?: T23_OSD_MAX_SIZE; + int bpp = t23_osd_bpp(pix->pixelformat); + + if (!bpp) { + pix->pixelformat = t23_osd_formats[0].fourcc; + bpp = t23_osd_formats[0].bpp; + } + pix->width = clamp_t(u32, pix->width, 2, max_width) & ~1U; + pix->height = clamp_t(u32, pix->height, 2, max_height) & ~1U; + pix->field = V4L2_FIELD_NONE; + pix->bytesperline = ALIGN(pix->width * bpp, 8); + pix->sizeimage = pix->bytesperline * pix->height; + pix->colorspace = V4L2_COLORSPACE_SRGB; + pix->xfer_func = V4L2_XFER_FUNC_DEFAULT; + pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; + pix->quantization = V4L2_QUANTIZATION_DEFAULT; + pix->flags = 0; + pix->priv = 0; +} + +/* Keep the bitmap inside the frame; called with the lock held. */ +static void t23_osd_clamp_position(struct t23_osd_region *rg) +{ + u32 width = rg->input->width ?: T23_OSD_MAX_SIZE; + u32 height = rg->input->height ?: T23_OSD_MAX_SIZE; + + rg->left = min_t(u32, rg->left, (width - min(width, rg->pix.width)) & ~1U); + rg->top = min_t(u32, rg->top, (height - min(height, rg->pix.height)) & ~1U); +} + +void t23_overlay_set_frame(struct t23_overlay *overlay, unsigned int input, + u32 width, u32 height) +{ + struct t23_overlay_input *in = &overlay->inputs[input]; + unsigned long flags; + unsigned int i; + + spin_lock_irqsave(&overlay->lock, flags); + in->width = width; + in->height = height; + for (i = 0; i < T23_MSCA_OSD_REGIONS; i++) + t23_osd_clamp_position(&in->osd[i]); + spin_unlock_irqrestore(&overlay->lock, flags); +} + +static bool t23_osd_overlaps(const struct tx_isp_rect *a, + const struct tx_isp_rect *b) +{ + return a->left < b->left + b->width && b->left < a->left + a->width && + a->top < b->top + b->height && b->top < a->top + a->height; +} + +void t23_overlay_frame(struct t23_overlay *overlay, unsigned int input, + struct tx_isp_msca_overlay *out) +{ + struct t23_overlay_input *in = &overlay->inputs[input]; + struct t23_osd_buffer *buf; + unsigned long flags; + unsigned int i, j; + + memset(out, 0, sizeof(*out)); + spin_lock_irqsave(&overlay->lock, flags); + memcpy(out->mask, in->mask, sizeof(out->mask)); + out->mask_yuv = in->mask_yuv; + memcpy(out->line, in->line, sizeof(out->line)); + out->line_yuv = in->line_yuv; + for (i = 0; i < T23_MSCA_OSD_REGIONS; i++) { + struct t23_osd_region *rg = &in->osd[i]; + + if (!rg->streaming) + continue; + buf = list_first_entry_or_null(&rg->bufs, struct t23_osd_buffer, + list); + if (buf) { + list_del(&buf->list); + if (rg->shown) + vb2_buffer_done(&rg->shown->vb.vb2_buf, + VB2_BUF_STATE_DONE); + rg->shown = buf; + } + if (!rg->shown || rg->left + rg->pix.width > in->width || + rg->top + rg->pix.height > in->height) + continue; + out->osd[i].rect = (struct tx_isp_rect){ + rg->left, rg->top, rg->pix.width, rg->pix.height + }; + for (j = 0; j < i; j++) + if (out->osd[j].addr && + t23_osd_overlaps(&out->osd[i].rect, &out->osd[j].rect)) + break; + if (j < i) + continue; + out->osd[i].addr = + vb2_dma_contig_plane_dma_addr(&rg->shown->vb.vb2_buf, 0); + out->osd[i].stride = rg->pix.bytesperline; + /* Streaming regions of an input share one format. */ + out->osd_format = rg->pix.pixelformat; + } + spin_unlock_irqrestore(&overlay->lock, flags); +} + +static int t23_osd_queue_setup(struct vb2_queue *q, unsigned int *nbuffers, + unsigned int *nplanes, unsigned int sizes[], + struct device *alloc_devs[]) +{ + struct t23_osd_region *rg = vb2_get_drv_priv(q); + + if (*nplanes) + return *nplanes == 1 && sizes[0] >= rg->pix.sizeimage ? + 0 : -EINVAL; + *nplanes = 1; + sizes[0] = rg->pix.sizeimage; + return 0; +} + +static int t23_osd_buf_prepare(struct vb2_buffer *vb) +{ + struct t23_osd_region *rg = vb2_get_drv_priv(vb->vb2_queue); + dma_addr_t addr = vb2_dma_contig_plane_dma_addr(vb, 0); + u32 size = rg->pix.sizeimage; + + if (vb2_plane_size(vb, 0) < size || + vb2_get_plane_payload(vb, 0) < size || addr & 7) + return -EINVAL; + return 0; +} + +static void t23_osd_buf_queue(struct vb2_buffer *vb) +{ + struct t23_osd_region *rg = vb2_get_drv_priv(vb->vb2_queue); + unsigned long flags; + + spin_lock_irqsave(&rg->input->overlay->lock, flags); + list_add_tail(&to_t23_osd_buffer(vb)->list, &rg->bufs); + spin_unlock_irqrestore(&rg->input->overlay->lock, flags); +} + +static void t23_osd_return_buffers(struct t23_osd_region *rg, + enum vb2_buffer_state state) +{ + struct t23_overlay *overlay = rg->input->overlay; + struct t23_osd_buffer *buf, *tmp; + unsigned long flags; + LIST_HEAD(done); + + spin_lock_irqsave(&overlay->lock, flags); + if (rg->shown) + list_add(&rg->shown->list, &done); + rg->shown = NULL; + list_splice_tail_init(&rg->bufs, &done); + spin_unlock_irqrestore(&overlay->lock, flags); + list_for_each_entry_safe(buf, tmp, &done, list) { + list_del(&buf->list); + vb2_buffer_done(&buf->vb.vb2_buf, state); + } +} + +/* The regions of an input stream with one pixel format at a time. */ +static int t23_osd_start_streaming(struct vb2_queue *q, unsigned int count) +{ + struct t23_osd_region *rg = vb2_get_drv_priv(q); + struct t23_overlay_input *in = rg->input; + unsigned long flags; + unsigned int i; + + spin_lock_irqsave(&in->overlay->lock, flags); + for (i = 0; i < T23_MSCA_OSD_REGIONS; i++) + if (in->osd[i].streaming && + in->osd[i].pix.pixelformat != rg->pix.pixelformat) + break; + if (i == T23_MSCA_OSD_REGIONS) + rg->streaming = true; + spin_unlock_irqrestore(&in->overlay->lock, flags); + if (!rg->streaming) { + t23_osd_return_buffers(rg, VB2_BUF_STATE_QUEUED); + return -EBUSY; + } + return 0; +} + +static bool t23_osd_pin_buffer(struct vb2_buffer *vb) +{ + struct dma_buf *dbuf; + + if (vb->memory == VB2_MEMORY_DMABUF) { + get_dma_buf(vb->planes[0].dbuf); + return true; + } + if (!vb->vb2_queue->mem_ops->get_dmabuf) + return false; + dbuf = vb->vb2_queue->mem_ops->get_dmabuf(vb, vb->planes[0].mem_priv, + O_RDWR); + return !IS_ERR_OR_NULL(dbuf); +} + +static void t23_osd_stop_streaming(struct vb2_queue *q) +{ + struct t23_osd_region *rg = vb2_get_drv_priv(q); + struct t23_overlay *overlay = rg->input->overlay; + unsigned long flags; + + spin_lock_irqsave(&overlay->lock, flags); + rg->streaming = false; + spin_unlock_irqrestore(&overlay->lock, flags); + /* A frame armed before the stop may still read the shown bitmap. */ + if (!overlay->sync(overlay, rg->input->index)) { + struct t23_osd_buffer *shown; + bool pinned = false; + + spin_lock_irqsave(&overlay->lock, flags); + shown = rg->shown; + spin_unlock_irqrestore(&overlay->lock, flags); + /* get_dmabuf() may sleep, so pin in process context. */ + if (shown) + pinned = t23_osd_pin_buffer(&shown->vb.vb2_buf); + dev_err_once(overlay->dev, + "OSD DMA stop timed out; %s active buffer\n", + pinned ? "pinned" : "could not pin"); + } + t23_osd_return_buffers(rg, VB2_BUF_STATE_ERROR); +} + +static const struct vb2_ops t23_osd_vb2_ops = { + .queue_setup = t23_osd_queue_setup, + .buf_prepare = t23_osd_buf_prepare, + .buf_queue = t23_osd_buf_queue, + .start_streaming = t23_osd_start_streaming, + .stop_streaming = t23_osd_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int t23_osd_querycap(struct file *file, void *fh, + struct v4l2_capability *cap) +{ + struct t23_osd_region *rg = video_drvdata(file); + + strscpy(cap->driver, "tx-isp", sizeof(cap->driver)); + strscpy(cap->card, rg->vdev.name, sizeof(cap->card)); + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", + dev_name(rg->input->overlay->dev)); + return 0; +} + +static int t23_osd_enum_fmt(struct file *file, void *fh, + struct v4l2_fmtdesc *f) +{ + if (f->index >= ARRAY_SIZE(t23_osd_formats)) + return -EINVAL; + f->pixelformat = t23_osd_formats[f->index].fourcc; + return 0; +} + +static int t23_osd_g_fmt(struct file *file, void *fh, struct v4l2_format *f) +{ + struct t23_osd_region *rg = video_drvdata(file); + + f->fmt.pix = rg->pix; + return 0; +} + +static int t23_osd_try_fmt(struct file *file, void *fh, struct v4l2_format *f) +{ + struct t23_osd_region *rg = video_drvdata(file); + + t23_osd_try_format(rg->input, &f->fmt.pix); + return 0; +} + +static int t23_osd_s_fmt(struct file *file, void *fh, struct v4l2_format *f) +{ + struct t23_osd_region *rg = video_drvdata(file); + unsigned long flags; + + if (vb2_is_busy(&rg->queue)) + return -EBUSY; + t23_osd_try_format(rg->input, &f->fmt.pix); + spin_lock_irqsave(&rg->input->overlay->lock, flags); + rg->pix = f->fmt.pix; + t23_osd_clamp_position(rg); + spin_unlock_irqrestore(&rg->input->overlay->lock, flags); + return 0; +} + +static int t23_osd_g_selection(struct file *file, void *fh, + struct v4l2_selection *s) +{ + struct t23_osd_region *rg = video_drvdata(file); + struct t23_overlay_input *in = rg->input; + + if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) + return -EINVAL; + switch (s->target) { + case V4L2_SEL_TGT_COMPOSE: + s->r.left = rg->left; + s->r.top = rg->top; + break; + case V4L2_SEL_TGT_COMPOSE_DEFAULT: + s->r.left = 0; + s->r.top = 0; + break; + case V4L2_SEL_TGT_COMPOSE_BOUNDS: + if (!in->width) + return -ENOLINK; + s->r = (struct v4l2_rect){ 0, 0, in->width, in->height }; + return 0; + default: + return -EINVAL; + } + s->r.width = rg->pix.width; + s->r.height = rg->pix.height; + return 0; +} + +/* The bitmap is not scaled: only the compose position can change. */ +static int t23_osd_s_selection(struct file *file, void *fh, + struct v4l2_selection *s) +{ + struct t23_osd_region *rg = video_drvdata(file); + unsigned long flags; + + if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || + s->target != V4L2_SEL_TGT_COMPOSE) + return -EINVAL; + spin_lock_irqsave(&rg->input->overlay->lock, flags); + rg->left = clamp(s->r.left, 0, (s32)T23_OSD_MAX_SIZE) & ~1; + rg->top = clamp(s->r.top, 0, (s32)T23_OSD_MAX_SIZE) & ~1; + t23_osd_clamp_position(rg); + s->r = (struct v4l2_rect){ + rg->left, rg->top, rg->pix.width, rg->pix.height + }; + spin_unlock_irqrestore(&rg->input->overlay->lock, flags); + return 0; +} + +static const struct v4l2_ioctl_ops t23_osd_ioctl_ops = { + .vidioc_querycap = t23_osd_querycap, + .vidioc_enum_fmt_vid_out = t23_osd_enum_fmt, + .vidioc_g_fmt_vid_out = t23_osd_g_fmt, + .vidioc_s_fmt_vid_out = t23_osd_s_fmt, + .vidioc_try_fmt_vid_out = t23_osd_try_fmt, + .vidioc_g_selection = t23_osd_g_selection, + .vidioc_s_selection = t23_osd_s_selection, + .vidioc_reqbufs = vb2_ioctl_reqbufs, + .vidioc_querybuf = vb2_ioctl_querybuf, + .vidioc_create_bufs = vb2_ioctl_create_bufs, + .vidioc_prepare_buf = vb2_ioctl_prepare_buf, + .vidioc_qbuf = vb2_ioctl_qbuf, + .vidioc_dqbuf = vb2_ioctl_dqbuf, + .vidioc_expbuf = vb2_ioctl_expbuf, + .vidioc_streamon = vb2_ioctl_streamon, + .vidioc_streamoff = vb2_ioctl_streamoff, +}; + +static const struct v4l2_file_operations t23_osd_fops = { + .owner = THIS_MODULE, + .open = v4l2_fh_open, + .release = vb2_fop_release, + .poll = vb2_fop_poll, + .mmap = vb2_fop_mmap, + .unlocked_ioctl = video_ioctl2, +}; + +static int t23_osd_node_init(struct t23_osd_region *rg, + struct v4l2_device *v4l2_dev, struct mutex *lock) +{ + struct vb2_queue *q = &rg->queue; + int ret; + + rg->pix = (struct v4l2_pix_format){ + .width = 64, + .height = 32, + .pixelformat = t23_osd_formats[0].fourcc, + }; + t23_osd_try_format(rg->input, &rg->pix); + + q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; + q->io_modes = VB2_MMAP | VB2_DMABUF; + q->drv_priv = rg; + q->ops = &t23_osd_vb2_ops; + q->mem_ops = &vb2_dma_contig_memops; + q->buf_struct_size = sizeof(struct t23_osd_buffer); + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + q->lock = lock; + q->dev = rg->input->overlay->dev; + ret = vb2_queue_init(q); + if (ret) + return ret; + rg->queue_initialized = true; + + rg->vdev = (struct video_device){ + .fops = &t23_osd_fops, + .ioctl_ops = &t23_osd_ioctl_ops, + .release = video_device_release_empty, + .v4l2_dev = v4l2_dev, + .queue = q, + .lock = lock, + .vfl_dir = VFL_DIR_TX, + .device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING, + }; + snprintf(rg->vdev.name, sizeof(rg->vdev.name), "T23 osd%u.%u", + rg->input->index, rg->index); + rg->vdev.entity.function = MEDIA_ENT_F_IO_V4L; + rg->pad.flags = MEDIA_PAD_FL_SOURCE; + ret = media_entity_pads_init(&rg->vdev.entity, 1, &rg->pad); + if (ret) + return ret; + rg->entity_initialized = true; + video_set_drvdata(&rg->vdev, rg); + + ret = video_register_device(&rg->vdev, VFL_TYPE_VIDEO, -1); + if (ret) + return ret; + rg->registered = true; + return 0; +} + +int t23_overlay_register(struct t23_overlay *overlay, + struct v4l2_device *v4l2_dev, struct mutex *lock, + struct media_entity *isp[], unsigned int osd_pad) +{ + unsigned int i, j; + int ret; + + for (i = 0; i < T23_OVERLAY_INPUTS; i++) { + for (j = 0; j < T23_MSCA_OSD_REGIONS; j++) { + struct t23_osd_region *rg = &overlay->inputs[i].osd[j]; + + ret = t23_osd_node_init(rg, v4l2_dev, lock); + if (ret) + goto fail; + ret = media_create_pad_link(&rg->vdev.entity, 0, isp[i], + osd_pad + j, + MEDIA_LNK_FL_ENABLED); + if (ret) + goto fail; + } + } + return 0; + +fail: + t23_overlay_unregister(overlay); + return ret; +} + +void t23_overlay_unregister(struct t23_overlay *overlay) +{ + unsigned int i, j; + + for (i = 0; i < T23_OVERLAY_INPUTS; i++) { + for (j = 0; j < T23_MSCA_OSD_REGIONS; j++) { + struct t23_osd_region *rg = &overlay->inputs[i].osd[j]; + + if (!rg->registered) + continue; + video_unregister_device(&rg->vdev); + rg->registered = false; + } + } +} + +void t23_overlay_release(struct t23_overlay *overlay) +{ + unsigned int i, j; + + for (i = 0; i < T23_OVERLAY_INPUTS; i++) { + for (j = 0; j < T23_MSCA_OSD_REGIONS; j++) { + struct t23_osd_region *rg = &overlay->inputs[i].osd[j]; + + if (rg->queue_initialized) + vb2_queue_release(&rg->queue); + if (rg->entity_initialized) + media_entity_cleanup(&rg->vdev.entity); + rg->queue_initialized = false; + rg->entity_initialized = false; + } + } +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.h new file mode 100644 index 0000000000..96ec78a259 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-overlay.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_OVERLAY_H__ +#define __TX_ISP_OVERLAY_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tx-isp-device.h" + +#define T23_OVERLAY_INPUTS 2 + +struct t23_overlay; +struct t23_overlay_input; +struct t23_osd_buffer; + +/* One OSD bitmap output node */ +struct t23_osd_region { + struct t23_overlay_input *input; + unsigned int index; + struct video_device vdev; + struct media_pad pad; + struct vb2_queue queue; + struct list_head bufs; /* queued, not yet shown */ + struct t23_osd_buffer *shown; /* read by the ISP on each frame */ + struct v4l2_pix_format pix; + u16 left; + u16 top; + bool streaming; + bool queue_initialized; + bool entity_initialized; + bool registered; +}; + +/* Privacy masks, vector lines and OSD regions of one input */ +struct t23_overlay_input { + struct t23_overlay *overlay; + unsigned int index; + u16 width; /* input frame, 0 until a sensor binds */ + u16 height; + struct tx_isp_rect mask[T23_MSCA_MASKS]; + u32 mask_yuv; + struct tx_isp_line line[T23_MSCA_LINES]; + u32 line_yuv; + struct t23_osd_region osd[T23_MSCA_OSD_REGIONS]; +}; + +struct t23_overlay { + struct device *dev; + spinlock_t lock; + struct t23_overlay_input inputs[T23_OVERLAY_INPUTS]; + /* Wait until no frame of @input is in flight; false on timeout. */ + bool (*sync)(struct t23_overlay *overlay, unsigned int input); +}; + +void t23_overlay_init(struct t23_overlay *overlay, struct device *dev, + bool (*sync)(struct t23_overlay *, unsigned int)); +void t23_overlay_init_controls(struct t23_overlay *overlay, unsigned int input, + struct v4l2_ctrl_handler *hdl); +/* The ISP entity of each input has a sink pad per OSD region from @osd_pad. */ +int t23_overlay_register(struct t23_overlay *overlay, + struct v4l2_device *v4l2_dev, struct mutex *lock, + struct media_entity *isp[], unsigned int osd_pad); +void t23_overlay_unregister(struct t23_overlay *overlay); +void t23_overlay_release(struct t23_overlay *overlay); +void t23_overlay_set_frame(struct t23_overlay *overlay, unsigned int input, + u32 width, u32 height); +/* + * Fill @out for the frame of @input being armed. Runs in atomic context once + * the previous frame has completed, so a replaced OSD buffer is returned. + */ +void t23_overlay_frame(struct t23_overlay *overlay, unsigned int input, + struct tx_isp_msca_overlay *out); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-params.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-params.c new file mode 100644 index 0000000000..dfd9b2f995 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-params.c @@ -0,0 +1,465 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Per-input ISP parameters queued from userspace. + */ +#include +#include +#include + +#include "tx-isp-mdns.h" + +#include "tx-isp-device.h" +#include "tx-isp-params.h" + +/* SDNS tuning registers; 0x88c4..0x8a5c hold fixed filter kernels. */ +static const u16 sdns_regs[T23_ISP_SDNS_WORDS] = { + 0x8800, 0x8804, 0x8808, 0x880c, 0x8810, 0x8814, 0x8818, 0x881c, + 0x8820, 0x8824, 0x8828, 0x882c, 0x8830, 0x8834, 0x8838, 0x883c, + 0x8840, 0x8844, 0x8848, 0x884c, 0x8850, 0x8854, 0x8858, 0x885c, + 0x8860, 0x8864, 0x8868, 0x886c, 0x8870, 0x8874, 0x8878, 0x887c, + 0x8880, 0x8884, 0x8888, 0x888c, 0x8890, 0x8894, 0x8898, 0x889c, + 0x88a0, 0x88a4, 0x88a8, 0x88ac, 0x88b0, 0x88b4, 0x88b8, 0x88bc, + 0x88c0, 0x8a60, 0x8a64, 0x8a68, 0x8a6c, 0x8a70, 0x8a74, 0x8a78, + 0x8a7c, 0x8a80, 0x8a84, 0x8a88, 0x8a8c, 0x8a90, 0x8a94, 0x8a98, + 0x8a9c, 0x8aa0, 0x8aa4, 0x8aa8, 0x8aac, 0x8ab0, 0x8ab4, 0x8ab8, + 0x8abc, 0x8ac0, 0x8ac4, 0x8ac8, 0x8acc, 0x8ad0, 0x8ad4, 0x8ad8, + 0x8adc, 0x8ae0, 0x8ae4, 0x8ae8, 0x8aec, 0x8af0, 0x8af4, 0x8af8, + 0x8afc, 0x8b00, 0x8b04, 0x8b08, 0x8b0c, 0x8b10, 0x8b14, 0x8b18, + 0x8b1c, 0x8b20, 0x8b24, 0x8b28, 0x8b2c, 0x8b30, 0x8b34, 0x8b38, +}; + +static const u16 dmsc_regs[T23_ISP_DMSC_WORDS] = { + 0x4800, 0x4804, 0x4808, 0x480c, 0x4810, 0x4814, 0x4820, 0x4824, + 0x4828, 0x482c, 0x4830, 0x4834, 0x4838, 0x483c, 0x4840, 0x4844, + 0x4848, 0x484c, 0x4850, 0x4854, 0x4858, 0x485c, 0x4860, 0x4864, + 0x4868, 0x486c, 0x4870, 0x4874, 0x4878, 0x487c, 0x4880, 0x4884, + 0x4888, 0x488c, 0x4890, 0x489c, 0x48a4, 0x48ac, 0x48b0, 0x48b4, + 0x48b8, 0x48c4, 0x48c8, 0x48cc, 0x48d0, 0x48d4, 0x48d8, 0x48dc, + 0x48e0, 0x48e4, 0x48f4, 0x48f8, 0x48fc, 0x4900, 0x4904, 0x4908, + 0x490c, 0x491c, 0x4920, 0x4924, 0x4928, 0x492c, 0x4930, 0x4934, + 0x4944, 0x4948, 0x494c, 0x4950, 0x4954, 0x4958, 0x495c, 0x4960, + 0x4964, 0x4968, 0x496c, 0x4970, 0x4974, 0x4978, 0x497c, 0x4980, + 0x4984, 0x4988, 0x49a8, 0x49ac, 0x49b0, 0x49b4, 0x49b8, 0x49bc, + 0x49c0, 0x49c4, 0x49c8, 0x49cc, 0x49d0, 0x49d4, 0x49d8, 0x49dc, + 0x49e0, 0x49e4, 0x49e8, 0x49ec, 0x49f0, 0x49f4, 0x49f8, 0x49fc, + 0x4a00, 0x4a04, 0x4a08, 0x4a0c, 0x4a10, 0x4a14, 0x4a18, 0x4a1c, + 0x4a20, 0x4a24, 0x4a28, 0x4a2c, 0x4a30, 0x4a34, 0x4a38, 0x4a3c, + 0x4a40, 0x4a44, 0x4a48, 0x4a4c, 0x4a50, 0x4a54, 0x4a58, 0x4a5c, +}; + +static size_t tx_isp_ext_block_size(u16 type) +{ + switch (type) { + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_BLC: + return sizeof(struct t23_isp_ext_params_blc_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_WB: + return sizeof(struct t23_isp_ext_params_wb_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_CCM: + return sizeof(struct t23_isp_ext_params_ccm_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_GAMMA: + return sizeof(struct t23_isp_ext_params_gamma_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_LSC: + return sizeof(struct t23_isp_ext_params_lsc_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_MDNS: + return sizeof(struct t23_isp_ext_params_mdns_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_SDNS: + return sizeof(struct t23_isp_ext_params_sdns_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_YDNS: + return sizeof(struct t23_isp_ext_params_ydns_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_YSP: + return sizeof(struct t23_isp_ext_params_ysp_config); + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_DMSC: + return sizeof(struct t23_isp_ext_params_dmsc_config); + default: + return 0; + } +} + +static int tx_isp_ext_check_reserved(const struct t23_isp_ext_params_block_header *hdr) +{ + switch (hdr->type) { + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_WB: + return ((const struct t23_isp_ext_params_wb_config *)hdr)->reserved ? + -EINVAL : 0; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_CCM: { + const struct t23_isp_ext_params_ccm_config *b = (const void *)hdr; + + return (b->reserved || b->config.reserved) ? -EINVAL : 0; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_GAMMA: { + const struct t23_isp_ext_params_gamma_config *b = (const void *)hdr; + + return (b->reserved || b->config.reserved) ? -EINVAL : 0; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_LSC: + return ((const struct t23_isp_ext_params_lsc_config *)hdr)->reserved ? + -EINVAL : 0; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_MDNS: + return ((const struct t23_isp_ext_params_mdns_config *)hdr)->reserved ? + -EINVAL : 0; + default: + return 0; + } +} + +int tx_isp_ext_params_check(const struct t23_isp_ext_params_cfg *ext, + size_t payload) +{ + size_t hdr_len = offsetof(struct t23_isp_ext_params_cfg, data); + size_t pos = 0; + unsigned int i; + + if (payload < hdr_len || ext->version != T23_ISP_EXT_PARAMS_V1 || + ext->data_size > T23_ISP_EXT_PARAMS_MAX_SIZE || + ext->data_size > payload - hdr_len || (ext->data_size & 7)) + return -EINVAL; + + while (pos < ext->data_size) { + const struct t23_isp_ext_params_block_header *hdr; + size_t expected; + + if (ext->data_size - pos < sizeof(*hdr)) + return -EINVAL; + hdr = (const void *)(ext->data + pos); + expected = tx_isp_ext_block_size(hdr->type); + if (!expected || hdr->size != expected || + hdr->size > ext->data_size - pos) + return -EINVAL; + if ((hdr->flags & ~(T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE | + T23_ISP_EXT_PARAMS_FL_BLOCK_ENABLE)) || + (hdr->flags == (T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE | + T23_ISP_EXT_PARAMS_FL_BLOCK_ENABLE))) + return -EINVAL; + if (tx_isp_ext_check_reserved(hdr)) + return -EINVAL; + if (!(hdr->flags & T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE)) { + switch (hdr->type) { + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_BLC: { + const struct t23_isp_ext_params_blc_config *b = + (const void *)hdr; + if ((b->config.r | b->config.gr | + b->config.gb | b->config.b) > 4095) + return -EINVAL; + break; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_WB: { + const struct t23_isp_ext_params_wb_config *b = + (const void *)hdr; + if (b->config.gain_r > 0x3fff || + b->config.gain_b > 0x3fff) + return -EINVAL; + break; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_CCM: { + const struct t23_isp_ext_params_ccm_config *b = + (const void *)hdr; + for (i = 0; i < ARRAY_SIZE(b->config.coeff); i++) + if (b->config.coeff[i] < -8192 || + b->config.coeff[i] > 8191) + return -EINVAL; + break; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_GAMMA: { + const struct t23_isp_ext_params_gamma_config *b = + (const void *)hdr; + for (i = 0; i < T23_ISP_GAMMA_POINTS; i++) + if (b->config.curve[i] > 4095) + return -EINVAL; + break; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_LSC: { + const struct t23_isp_ext_params_lsc_config *b = + (const void *)hdr; + if (b->config.words > T23_ISP_LSC_MAX_WORDS || + b->config.words % 3 || + b->config.scale > 3 || + b->config.mean_en > 1) + return -EINVAL; + break; + } + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_MDNS: { + const struct t23_isp_ext_params_mdns_config *b = + (const void *)hdr; + if (tx_isp_mdns_check_config(&b->config)) + return -EINVAL; + break; + } + default: + break; + } + } + pos += hdr->size; + } + return 0; +} + +static void tx_isp_merge_block(struct tx_isp_params_state *state, u32 bit, + void *dst, const void *src, size_t len) +{ + if (!(state->valid & bit) || memcmp(dst, src, len)) { + memcpy(dst, src, len); + state->valid |= bit; + state->dirty |= bit; + } +} + +static void tx_isp_merge_lsc(struct tx_isp_params_state *state, + const struct t23_isp_lsc_config *src) +{ + struct t23_isp_lsc_config *dst = &state->cfg.lsc; + size_t hdr_bytes = offsetof(struct t23_isp_lsc_config, lut); + u32 words = min_t(u32, src->words, T23_ISP_LSC_MAX_WORDS) / 3 * 3; + size_t lut_bytes = words * sizeof(dst->lut[0]); + + if (!(state->valid & TX_ISP_PARAM_LSC) || + memcmp(dst, src, hdr_bytes) || + dst->words != words || + memcmp(dst->lut, src->lut, lut_bytes)) { + memcpy(dst, src, hdr_bytes); + dst->words = words; + memcpy(dst->lut, src->lut, lut_bytes); + state->valid |= TX_ISP_PARAM_LSC; + state->dirty |= TX_ISP_PARAM_LSC; + } +} + +/* @ext is the validated kernel shadow copy owned by tx-isp-meta. */ +void tx_isp_ext_params_merge(struct tx_isp_params_state *state, + const struct t23_isp_ext_params_cfg *ext, + size_t payload) +{ + struct tx_isp_params_values *dst = &state->cfg; + size_t hdr_len = offsetof(struct t23_isp_ext_params_cfg, data); + size_t pos = 0; + + if (!ext || payload < hdr_len || ext->data_size > payload - hdr_len) + return; + + while (pos + sizeof(struct t23_isp_ext_params_block_header) <= ext->data_size) { + const struct t23_isp_ext_params_block_header *hdr = + (const void *)(ext->data + pos); + bool dis = hdr->flags & T23_ISP_EXT_PARAMS_FL_BLOCK_DISABLE; + + if (hdr->size < sizeof(*hdr) || hdr->size > ext->data_size - pos) + break; + + switch (hdr->type) { + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_BLC: + if (dis) { + state->blc_valid = false; + state->blc_dirty = false; + } else { + const struct t23_isp_blc_config *src = + &((const struct t23_isp_ext_params_blc_config *)hdr)->config; + + if (!state->blc_valid || + memcmp(&state->blc, src, sizeof(state->blc))) { + state->blc = *src; + state->blc_valid = true; + state->blc_dirty = true; + } + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_WB: + if (dis) { + state->valid &= ~TX_ISP_PARAM_WB; + state->dirty &= ~TX_ISP_PARAM_WB; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_WB, + &dst->wb, + &((const struct t23_isp_ext_params_wb_config *)hdr)->config, + sizeof(dst->wb)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_CCM: + if (dis) { + state->valid &= ~TX_ISP_PARAM_CCM; + state->dirty &= ~TX_ISP_PARAM_CCM; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_CCM, + &dst->ccm, + &((const struct t23_isp_ext_params_ccm_config *)hdr)->config, + sizeof(dst->ccm)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_GAMMA: + if (dis) { + state->valid &= ~TX_ISP_PARAM_GAMMA; + state->dirty &= ~TX_ISP_PARAM_GAMMA; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_GAMMA, + &dst->gamma, + &((const struct t23_isp_ext_params_gamma_config *)hdr)->config, + sizeof(dst->gamma)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_LSC: + if (dis) { + state->valid &= ~TX_ISP_PARAM_LSC; + state->dirty &= ~TX_ISP_PARAM_LSC; + } else { + tx_isp_merge_lsc(state, + &((const struct t23_isp_ext_params_lsc_config *)hdr)->config); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_MDNS: + if (dis) { + state->valid &= ~TX_ISP_PARAM_MDNS; + state->dirty &= ~TX_ISP_PARAM_MDNS; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_MDNS, + &dst->mdns, + &((const struct t23_isp_ext_params_mdns_config *)hdr)->config, + sizeof(dst->mdns)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_SDNS: + if (dis) { + state->valid &= ~TX_ISP_PARAM_SDNS; + state->dirty &= ~TX_ISP_PARAM_SDNS; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_SDNS, + dst->sdns, + ((const struct t23_isp_ext_params_sdns_config *)hdr)->words, + sizeof(dst->sdns)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_YDNS: + if (dis) { + state->valid &= ~TX_ISP_PARAM_YDNS; + state->dirty &= ~TX_ISP_PARAM_YDNS; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_YDNS, + dst->ydns, + ((const struct t23_isp_ext_params_ydns_config *)hdr)->words, + sizeof(dst->ydns)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_YSP: + if (dis) { + state->valid &= ~TX_ISP_PARAM_YSP; + state->dirty &= ~TX_ISP_PARAM_YSP; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_YSP, + dst->ysp, + ((const struct t23_isp_ext_params_ysp_config *)hdr)->words, + sizeof(dst->ysp)); + } + break; + case T23_ISP_EXT_PARAMS_BLOCK_TYPE_DMSC: + if (dis) { + state->valid &= ~TX_ISP_PARAM_DMSC; + state->dirty &= ~TX_ISP_PARAM_DMSC; + } else { + tx_isp_merge_block(state, TX_ISP_PARAM_DMSC, + dst->dmsc, + ((const struct t23_isp_ext_params_dmsc_config *)hdr)->words, + sizeof(dst->dmsc)); + } + break; + default: + break; + } + pos += hdr->size; + } +} + +static void tx_isp_params_write_ccm(void __iomem *base, const s16 *c) +{ + unsigned int i; + u32 value; + + /* Each coefficient word latches through the 0x5000 strobe. */ + for (i = 0; i < 9; i += 2) { + value = (u16)c[i] & 0x3fff; + if (i < 8) + value |= ((u16)c[i + 1] & 0x3fff) << 16; + writel(ISP_CCM_TRIGGER_STROBE, base + ISP_CCM_TRIGGER); + writel(value, base + ISP_CCM_COEFF_BASE + i * 2); + } +} + +static void tx_isp_params_write_lsc(void __iomem *base, + const struct t23_isp_lsc_config *lsc) +{ + unsigned int i; + + writel(lsc->mesh_height << 16 | lsc->mesh_width, base + ISP_LSC_GRID_SIZE); + writel(lsc->stride << 16 | lsc->mean_en << 15 | lsc->scale, + base + ISP_LSC_CONTROL); + /* Each mesh cell occupies three words of a four-word slot. */ + for (i = 0; i < lsc->words; i++) + writel(lsc->lut[i], base + ISP_LSC_LUT_BASE + (i / 3) * 16 + (i % 3) * 4); + writel(ISP_LSC_LUT_TRIGGER_COMMIT, base + ISP_LSC_LUT_TRIGGER); +} + +/* MDNS is written by tx_isp_mdns_start() inside its hold window. */ +void tx_isp_params_write(struct tx_isp_subdev *sd, + struct tx_isp_params_state *state) +{ + const struct tx_isp_params_values *cfg = &state->cfg; + void __iomem *base = sd->base; + u32 dirty = state->valid & state->dirty; + unsigned int i; + u32 value; + + if (state->blc_valid && state->blc_dirty) { + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(state->blc.r & 0xfff, base + ISP_DGAIN_BLC0); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(((u32)(state->blc.gb & 0xfff) << 16) | + (state->blc.gr & 0xfff), base + ISP_DGAIN_BLC1); + writel(ISP_DGAIN_TRIGGER_COMMIT, base + ISP_DGAIN_TRIGGER); + writel(state->blc.b & 0xfff, base + ISP_DGAIN_BLC2); + } + if (dirty & TX_ISP_PARAM_WB) { + u32 r = ISP_WB_GAIN_TAG | cfg->wb.gain_r; + u32 b = ISP_WB_GAIN_TAG | cfg->wb.gain_b; + + writel(ISP_WB_TRIGGER_COMMIT, base + ISP_WB_TRIGGER); + writel(r, base + ISP_WB_GAIN_R); + writel(ISP_WB_TRIGGER_COMMIT, base + ISP_WB_TRIGGER); + writel(b, base + ISP_WB_GAIN_B); + writel(ISP_WB_TRIGGER_COMMIT, base + ISP_WB_TRIGGER); + writel(r, base + ISP_WB_GAIN_GR); + writel(ISP_WB_TRIGGER_COMMIT, base + ISP_WB_TRIGGER); + writel(b, base + ISP_WB_GAIN_GB); + } + if ((dirty & TX_ISP_PARAM_GAMMA) && sd->res && + resource_size(sd->res) >= 0x50200) { + for (i = 0; i < T23_ISP_GAMMA_POINTS - 1; i++) { + value = cfg->gamma.curve[i] | + (u32)cfg->gamma.curve[i + 1] << 12; + writel(value, base + ISP_GAMMA_LUT_R + i * 4); + writel(value, base + ISP_GAMMA_LUT_G + i * 4); + writel(value, base + ISP_GAMMA_LUT_B + i * 4); + } + } + if (dirty & TX_ISP_PARAM_YSP) { + for (i = 0; i < T23_ISP_YSP_WORDS; i++) + writel(cfg->ysp[i], base + ISP_YSP_REG_BASE + i * 4); + writel(ISP_YSP_REFRESH_COMMIT, base + ISP_YSP_REFRESH); + } + if (dirty & TX_ISP_PARAM_DMSC) { + for (i = 0; i < T23_ISP_DMSC_WORDS; i++) + writel(cfg->dmsc[i], base + dmsc_regs[i]); + writel(ISP_DMSC_REFRESH_COMMIT, base + ISP_DMSC_TRIGGER); + } + if (dirty & TX_ISP_PARAM_YDNS) + for (i = 0; i < T23_ISP_YDNS_WORDS; i++) + writel(cfg->ydns[i], base + ISP_YDNS_REG_BASE + i * 4); + if (dirty & TX_ISP_PARAM_SDNS) { + for (i = 0; i < T23_ISP_SDNS_WORDS; i++) + writel(cfg->sdns[i], base + sdns_regs[i]); + writel(ISP_SDNS_REFRESH_COMMIT, base + ISP_SDNS_REFRESH); + } + if ((dirty & TX_ISP_PARAM_LSC) && sd->res && + resource_size(sd->res) >= 0x2c000) + tx_isp_params_write_lsc(base, &cfg->lsc); + if (dirty & TX_ISP_PARAM_CCM) + tx_isp_params_write_ccm(base, cfg->ccm.coeff); + + state->dirty = 0; + state->blc_dirty = false; +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-params.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-params.h new file mode 100644 index 0000000000..255ac21ddd --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-params.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_PARAMS_H__ +#define __TX_ISP_PARAMS_H__ + +#include +#include +#include + +#define TX_ISP_PARAM_WB BIT(0) +#define TX_ISP_PARAM_CCM BIT(1) +#define TX_ISP_PARAM_GAMMA BIT(2) +#define TX_ISP_PARAM_LSC BIT(3) +#define TX_ISP_PARAM_MDNS BIT(4) +#define TX_ISP_PARAM_SDNS BIT(5) +#define TX_ISP_PARAM_YDNS BIT(6) +#define TX_ISP_PARAM_YSP BIT(7) +#define TX_ISP_PARAM_DMSC BIT(8) + +struct tx_isp_params_values { + struct t23_isp_wb_config wb; + struct t23_isp_ccm_config ccm; + struct t23_isp_gamma_config gamma; + struct t23_isp_mdns_config mdns; + u32 sdns[T23_ISP_SDNS_WORDS]; + u32 ydns[T23_ISP_YDNS_WORDS]; + u32 ysp[T23_ISP_YSP_WORDS]; + u32 dmsc[T23_ISP_DMSC_WORDS]; + struct t23_isp_lsc_config lsc; +}; + +struct tx_isp_subdev; + +struct tx_isp_params_state { + struct tx_isp_params_values cfg; + struct t23_isp_blc_config blc; + u32 valid; /* TX_ISP_PARAM_* blocks held in @cfg */ + u32 dirty; /* TX_ISP_PARAM_* blocks needing HW write */ + bool blc_valid; + bool blc_dirty; +}; + +int tx_isp_ext_params_check(const struct t23_isp_ext_params_cfg *ext, + size_t payload); +void tx_isp_ext_params_merge(struct tx_isp_params_state *state, + const struct t23_isp_ext_params_cfg *ext, + size_t payload); +void tx_isp_params_write(struct tx_isp_subdev *sd, + struct tx_isp_params_state *state); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-profile.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-profile.c new file mode 100644 index 0000000000..f1deabf359 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-profile.c @@ -0,0 +1,2069 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#include +#include +#include +#include "tx-isp-params.h" +#include "tx-isp-profile.h" +#include "tx-isp-regs.h" + +struct t23_profile_reg { + u32 offset; + u32 value; +}; + +static const struct t23_profile_reg lsc_config[] = { + { 0x03800, 0x001c001c }, + { 0x03804, 0x00180000 }, +}; + +static const u32 lsc_lut[] = { + 0x0071572d, 0x0070671c, 0x0070571c, 0x006d46f7, 0x006c96ea, 0x006c76e8, + 0x006886ad, 0x006816a5, 0x0067c6a1, 0x00642664, 0x0063e65f, 0x0063965a, + 0x00608623, 0x00605620, 0x0060061b, 0x005d95ef, 0x005d85ed, 0x005d45e9, + 0x005b65c6, 0x005b65c5, 0x005b05c1, 0x0059c5a8, 0x0059d5a8, 0x005985a3, + 0x0058d593, 0x0058f595, 0x0058a590, 0x0058958a, 0x0058a58c, 0x00586587, + 0x0058e58a, 0x0059058c, 0x0058b587, 0x0059d594, 0x0059f596, 0x0059a592, + 0x005b75a9, 0x005b85ab, 0x005b45a5, 0x005db5c7, 0x005dc5c9, 0x005d75c4, + 0x0060a5f1, 0x0060a5f2, 0x006055ed, 0x00645626, 0x00642624, 0x0063f621, + 0x0068e668, 0x00688663, 0x00686661, 0x006e66b8, 0x006db6af, 0x006dc6af, + 0x0074a717, 0x00738709, 0x0073e70c, 0x007a577b, 0x0078a763, 0x0079976e, + 0x007df7c7, 0x007bc7a8, 0x007d27bb, 0x00709722, 0x006fb711, 0x006fb711, + 0x006c86eb, 0x006be6de, 0x006bc6dd, 0x0067c6a2, 0x0067669a, 0x00672697, + 0x00637659, 0x00634654, 0x0062f64f, 0x005fe619, 0x005fb616, 0x005f7612, + 0x005cf5e5, 0x005ce5e4, 0x005ca5df, 0x005ac5bc, 0x005ac5bb, 0x005a75b7, + 0x0059259e, 0x0059459e, 0x0058f59a, 0x0058358a, 0x0058458b, 0x00581587, + 0x0057f57f, 0x00580582, 0x0057c57d, 0x00584580, 0x00585582, 0x0058157d, + 0x0059358a, 0x0059558c, 0x00591587, 0x005ad59f, 0x005ae5a0, 0x005ab59c, + 0x005d15be, 0x005d25be, 0x005ce5bb, 0x006005e7, 0x006005e7, 0x005fc5e4, + 0x0063b61b, 0x0063861a, 0x00636618, 0x0068365d, 0x0067d659, 0x0067c657, + 0x006da6ac, 0x006d06a4, 0x006d26a5, 0x0073d70b, 0x0072b6fd, 0x00733701, + 0x0079876e, 0x0077c756, 0x0078d763, 0x007d17ba, 0x007ad799, 0x007c67ae, + 0x006fb713, 0x006ec702, 0x006ed704, 0x006ba6dd, 0x006b06d0, 0x006af6d0, + 0x0066f695, 0x0066968c, 0x0066568a, 0x0062b64c, 0x00627647, 0x00623643, + 0x005f260d, 0x005ef60a, 0x005eb605, 0x005c35d9, 0x005c25d7, 0x005be5d4, + 0x005a05b0, 0x005a05b0, 0x0059c5ac, 0x00587592, 0x00587592, 0x0058458f, + 0x0057757d, 0x0057857e, 0x0057457b, 0x00571573, 0x00573574, 0x00570571, + 0x00577573, 0x00578574, 0x00575571, 0x0058657d, 0x0058857f, 0x0058457c, + 0x005a0592, 0x005a1593, 0x0059e590, 0x005c45b0, 0x005c55b2, 0x005c15af, + 0x005f35da, 0x005f25da, 0x005f05d7, 0x0062e60f, 0x0062b60d, 0x0062960b, + 0x0067564f, 0x0067064c, 0x0067064b, 0x006cb69e, 0x006c2697, 0x006c5698, + 0x0072e6fb, 0x0071c6ee, 0x007256f3, 0x0078875e, 0x0076a746, 0x0077e754, + 0x007c17aa, 0x0079a787, 0x007b679f, 0x006eb704, 0x006dd6f3, 0x006dd6f4, + 0x006ab6cd, 0x006a16c1, 0x006a06c1, 0x00660686, 0x0065a67d, 0x0065767b, + 0x0061c63c, 0x00619639, 0x00615635, 0x005e35fe, 0x005e25fc, 0x005de5f7, + 0x005b65cb, 0x005b55ca, 0x005b15c6, 0x005925a3, 0x005925a2, 0x0058e59e, + 0x00578583, 0x00579584, 0x00576581, 0x0056856f, 0x00569570, 0x0056656d, + 0x00563564, 0x00564565, 0x00561563, 0x00568564, 0x00569566, 0x00567563, + 0x0057756f, 0x0057a570, 0x0057756d, 0x00592584, 0x00593584, 0x00590582, + 0x005b65a3, 0x005b65a3, 0x005b45a1, 0x005e45cc, 0x005e45cc, 0x005e25c9, + 0x0061f600, 0x0061c5ff, 0x0061b5fd, 0x00666641, 0x0066163d, 0x0066163c, + 0x006bb68e, 0x006b2688, 0x006b5689, 0x0071c6eb, 0x0070a6de, 0x007146e3, + 0x0077674c, 0x00757734, 0x0076d744, 0x007ad796, 0x00786773, 0x007a478d, + 0x006db6f3, 0x006cd6e3, 0x006cd6e4, 0x0069b6be, 0x006916b2, 0x006906b2, + 0x00651675, 0x0064b66e, 0x0064966c, 0x0060d62e, 0x0060b629, 0x00606626, + 0x005d45ef, 0x005d45ed, 0x005cf5ea, 0x005a75bc, 0x005a65bb, 0x005a35b8, + 0x00583594, 0x00583594, 0x00580590, 0x00569574, 0x0056a575, 0x00567572, + 0x0055955f, 0x0055a561, 0x0055755e, 0x00553555, 0x00554556, 0x00552554, + 0x00559554, 0x00559556, 0x00558553, 0x0056855f, 0x0056a561, 0x0056855e, + 0x00582574, 0x00584576, 0x00582574, 0x005a7593, 0x005a7594, 0x005a5592, + 0x005d55bc, 0x005d55bd, 0x005d45bb, 0x0060f5f0, 0x0060d5ef, 0x0060d5ef, + 0x00655631, 0x0065162d, 0x0065262e, 0x006aa67e, 0x006a1678, 0x006a5679, + 0x0070a6da, 0x006f96cd, 0x007046d3, 0x00763739, 0x00744722, 0x0075b732, + 0x0079a783, 0x0077175e, 0x0079277b, 0x006cd6e5, 0x006bf6d4, 0x006bf6d5, + 0x0068c6af, 0x006836a3, 0x006826a3, 0x00643668, 0x0063d65f, 0x0063b65e, + 0x00600620, 0x005fd61b, 0x005f9618, 0x005c75e2, 0x005c65e0, 0x005c25dc, + 0x005995af, 0x005995ae, 0x005955ab, 0x00575586, 0x00575586, 0x00572582, + 0x0055a566, 0x0055b567, 0x00559564, 0x00549550, 0x0054b552, 0x00548550, + 0x00543546, 0x00545546, 0x00543545, 0x00549545, 0x0054b546, 0x00549545, + 0x00559550, 0x0055b551, 0x00559550, 0x00574566, 0x00575566, 0x00574565, + 0x00598584, 0x00599586, 0x00598584, 0x005c75ae, 0x005c65ae, 0x005c65ae, + 0x006005e2, 0x005ff5e2, 0x005ff5e1, 0x00647622, 0x0064261f, 0x00644620, + 0x0069a66f, 0x00692669, 0x0069666a, 0x006f96ca, 0x006e86bd, 0x006f36c5, + 0x00750728, 0x00732711, 0x0074a722, 0x00787771, 0x0075f74d, 0x0078076a, + 0x006c06d8, 0x006b26c8, 0x006b26c8, 0x006806a3, 0x00677697, 0x00675696, + 0x0063765c, 0x00631654, 0x0062e652, 0x005f3613, 0x005f1610, 0x005ed60d, + 0x005bb5d6, 0x005ba5d4, 0x005b65d1, 0x0058d5a3, 0x0058d5a3, 0x0058a59f, + 0x0056957a, 0x0056957a, 0x00566577, 0x0054d559, 0x0054e55b, 0x0054c558, + 0x0053b543, 0x0053d545, 0x0053b543, 0x00535538, 0x00536538, 0x00535537, + 0x0053b537, 0x0053c538, 0x0053b537, 0x0054b542, 0x0054d543, 0x0054c542, + 0x00567559, 0x0056955a, 0x00567559, 0x0058c578, 0x0058d579, 0x0058c578, + 0x005ba5a2, 0x005bb5a2, 0x005ba5a2, 0x005f45d5, 0x005f25d5, 0x005f35d4, + 0x00639615, 0x00635612, 0x00637613, 0x0068c662, 0x0068565c, 0x0068965e, + 0x006eb6bb, 0x006db6af, 0x006e66b7, 0x0074171a, 0x00724702, 0x0073c714, + 0x00777761, 0x0074f73d, 0x0077175b, 0x006b66cd, 0x006a96bf, 0x006a86be, + 0x00677699, 0x0066d68e, 0x0066c68d, 0x0062e652, 0x0062764a, 0x00626649, + 0x005ea60b, 0x005e7606, 0x005e4603, 0x005b25cc, 0x005b05cb, 0x005ae5c7, + 0x00583599, 0x00584598, 0x00580595, 0x0055e56f, 0x0055f570, 0x0055c56d, + 0x0054254f, 0x00543550, 0x0054154e, 0x00530538, 0x00531539, 0x0052f537, + 0x0052952b, 0x0052a52c, 0x0052952a, 0x0052f52a, 0x0053052b, 0x0052f52a, + 0x00540536, 0x00541538, 0x00541537, 0x0055c54e, 0x0055e54e, 0x0055d54e, + 0x0058256e, 0x0058256f, 0x0058256f, 0x005b0598, 0x005b0598, 0x005b0598, + 0x005e95cb, 0x005e75ca, 0x005e95cb, 0x0062e60b, 0x0062b608, 0x0062d609, + 0x00682657, 0x0067a652, 0x0067f654, 0x006e06b0, 0x006cf6a4, 0x006db6ac, + 0x0073570d, 0x007186f6, 0x00730708, 0x0076a754, 0x00743731, 0x0076574f, + 0x006af6c6, 0x006a36b8, 0x006a16b7, 0x00670692, 0x00667687, 0x00665686, + 0x0062764b, 0x00620644, 0x0061e641, 0x005e3603, 0x005df5ff, 0x005dd5fd, + 0x005ab5c6, 0x005a95c3, 0x005a65c1, 0x0057c592, 0x0057c591, 0x0057958e, + 0x00556567, 0x00557568, 0x00554565, 0x00539546, 0x0053b548, 0x00538545, + 0x0052552e, 0x00527530, 0x0052552d, 0x0051f521, 0x0051f522, 0x0051f521, + 0x00525520, 0x00526521, 0x00525520, 0x0053752d, 0x0053852d, 0x0053852d, + 0x00553544, 0x00555546, 0x00555545, 0x00579566, 0x0057a566, 0x0057a566, + 0x005a858f, 0x005a858f, 0x005a8590, 0x005e15c3, 0x005e05c3, 0x005e25c3, + 0x00627602, 0x00623600, 0x00626602, 0x0067a64e, 0x00672649, 0x0067764c, + 0x006d76a7, 0x006c669c, 0x006d26a4, 0x0072c704, 0x0070f6ed, 0x007276ff, + 0x0076074b, 0x0073a727, 0x0075c746, 0x006a96c1, 0x0069e6b4, 0x0069c6b2, + 0x0066b68d, 0x00662683, 0x00660681, 0x00622647, 0x0061b63f, 0x0061a63d, + 0x005df5ff, 0x005db5fa, 0x005d95f8, 0x005a65c1, 0x005a45be, 0x005a25bc, + 0x0057758d, 0x0057758d, 0x0057458a, 0x00550563, 0x00551563, 0x0054f561, + 0x00532540, 0x00534541, 0x0053253f, 0x0051f527, 0x0051f528, 0x0051e527, + 0x0051751a, 0x0051751a, 0x00517519, 0x0051d518, 0x0051e519, 0x0051e519, + 0x00530525, 0x00531526, 0x00531526, 0x0054e53d, 0x0054f53f, 0x0054f53f, + 0x00574560, 0x00574561, 0x00575561, 0x005a358a, 0x005a358a, 0x005a358b, + 0x005dc5be, 0x005da5bd, 0x005dc5be, 0x006215fd, 0x0061d5fa, 0x006205fd, + 0x00674649, 0x0066c643, 0x00671647, 0x006d16a2, 0x006c0696, 0x006cd69e, + 0x007266fe, 0x007086e7, 0x007226fa, 0x0075a745, 0x00732721, 0x00756740, + 0x006a86bf, 0x0069c6b2, 0x0069a6b0, 0x0066968b, 0x00661681, 0x0065e67f, + 0x00620644, 0x0061a63e, 0x0061863b, 0x005dd5fd, 0x005d95f8, 0x005d75f6, + 0x005a45bf, 0x005a25bc, 0x005a05b9, 0x0057458b, 0x00574589, 0x00571587, + 0x0054e561, 0x0054e560, 0x0054c55e, 0x0053053d, 0x0053053e, 0x0052e53c, + 0x0051a524, 0x0051a524, 0x0051a522, 0x00512514, 0x00513515, 0x00512514, + 0x00519514, 0x00519514, 0x0051a514, 0x0052c521, 0x0052d522, 0x0052d522, + 0x0054b53a, 0x0054b53b, 0x0054b53b, 0x0057155c, 0x0057155e, 0x0057255e, + 0x005a0587, 0x005a0587, 0x005a1588, 0x005d95bb, 0x005d75ba, 0x005d95bc, + 0x0061e5fa, 0x0061a5f7, 0x0061e5fa, 0x00671647, 0x00668640, 0x0066f644, + 0x006ce69f, 0x006bd693, 0x006ca69c, 0x007236fb, 0x007046e3, 0x0071e6f7, + 0x00756741, 0x0072e71d, 0x0075373d, 0x006a86bf, 0x0069d6b2, 0x0069b6b1, + 0x0066a68b, 0x00661682, 0x0065f680, 0x00620645, 0x0061b63e, 0x0061863c, + 0x005dd5fd, 0x005d95f9, 0x005d75f7, 0x005a45c0, 0x005a25bc, 0x005a05ba, + 0x0057458c, 0x0057358a, 0x00571587, 0x0054e561, 0x0054e55f, 0x0054b55e, + 0x0052f53d, 0x0052f53d, 0x0052e53c, 0x0051a523, 0x0051a523, 0x00519522, + 0x00512514, 0x00512514, 0x00512513, 0x00517512, 0x00518512, 0x00518513, + 0x0052b520, 0x0052c520, 0x0052d521, 0x0054a539, 0x0054a53a, 0x0054b53b, + 0x0057055c, 0x0057155d, 0x0057255e, 0x005a0587, 0x0059f587, 0x005a0588, + 0x005d95bb, 0x005d75ba, 0x005d95bc, 0x0061e5fa, 0x006195f7, 0x0061e5fa, + 0x00671646, 0x0066863f, 0x0066f645, 0x006ce69f, 0x006bc692, 0x006ca69c, + 0x007226fb, 0x007036e3, 0x0071e6f7, 0x00756741, 0x0072d71c, 0x0075373d, + 0x006ac6c3, 0x006a06b6, 0x0069e6b4, 0x0066d68e, 0x00664685, 0x00662683, + 0x00624648, 0x0061e641, 0x0061b63f, 0x005e1601, 0x005dc5fc, 0x005da5fa, + 0x005a85c2, 0x005a45bf, 0x005a35bd, 0x0057758e, 0x0057558c, 0x0057458a, + 0x00550564, 0x00550561, 0x0054e560, 0x00532540, 0x0053153f, 0x0053053e, + 0x0051c525, 0x0051c525, 0x0051c525, 0x00514516, 0x00514516, 0x00514516, + 0x0051a515, 0x0051a515, 0x0051b515, 0x0052d522, 0x0052e522, 0x0052f523, + 0x0054c53b, 0x0054c53c, 0x0054e53d, 0x0057355e, 0x0057355e, 0x00574560, + 0x005a2589, 0x005a1589, 0x005a358a, 0x005dc5be, 0x005d95bb, 0x005dc5be, + 0x006215fd, 0x0061c5f9, 0x006205fd, 0x00673649, 0x0066a641, 0x00672647, + 0x006d16a1, 0x006bf694, 0x006cd69f, 0x007256fe, 0x007056e5, 0x007226fa, + 0x00759744, 0x0072f71e, 0x00756740, 0x006b26ca, 0x006a66bb, 0x006a46ba, + 0x00673696, 0x0066a68b, 0x00668688, 0x0062964f, 0x00623647, 0x00620644, + 0x005e6607, 0x005e2601, 0x005df5ff, 0x005ad5c8, 0x005aa5c4, 0x005a85c2, + 0x0057d594, 0x0057a591, 0x0057a58f, 0x00556569, 0x00554566, 0x00553565, + 0x00537546, 0x00537545, 0x00536544, 0x0052252c, 0x0052252a, 0x0052252a, + 0x0051a51c, 0x0051a51c, 0x0051a51c, 0x0052051b, 0x0052051b, 0x0052151c, + 0x00533528, 0x00534528, 0x00535529, 0x00551541, 0x00551541, 0x00553543, + 0x00577564, 0x00577564, 0x0057a565, 0x005a758f, 0x005a558d, 0x005a858f, + 0x005e15c3, 0x005dd5c1, 0x005e25c3, 0x00626602, 0x006205fe, 0x00626602, + 0x0067964e, 0x0066f647, 0x0067764c, 0x006d76a7, 0x006c4699, 0x006d36a4, + 0x0072b704, 0x0070b6ea, 0x00727700, 0x0075f749, 0x00735723, 0x0075b745, + 0x006bc6d3, 0x006ae6c3, 0x006ac6c2, 0x0067c69e, 0x00672693, 0x00670691, + 0x00632657, 0x0062b64f, 0x0062964c, 0x005ee60f, 0x005e9609, 0x005e7607, + 0x005b45d0, 0x005b15cc, 0x005af5c9, 0x0058459c, 0x00582598, 0x00581597, + 0x0055e571, 0x0055c56e, 0x0055b56d, 0x0054054e, 0x0053f54d, 0x0053f54c, + 0x0052c535, 0x0052b534, 0x0052b534, 0x00523526, 0x00524525, 0x00524525, + 0x0052a525, 0x0052a525, 0x0052a525, 0x0053c531, 0x0053c531, 0x0053d532, + 0x00559549, 0x00559549, 0x0055b54b, 0x0057f56b, 0x0057f56b, 0x0058156c, + 0x005af596, 0x005ad595, 0x005b0598, 0x005e85ca, 0x005e55c8, 0x005e95cb, + 0x0062e60a, 0x00628605, 0x0062e60a, 0x00682656, 0x0067764e, 0x0067f654, + 0x006e06b0, 0x006cb6a1, 0x006db6ac, 0x0073570d, 0x007126f2, 0x0072f709, + 0x00768753, 0x0073d72c, 0x0076474e, 0x006c86e0, 0x006b86ce, 0x006b86ce, + 0x006896aa, 0x0067d69d, 0x0067b69c, 0x0063d662, 0x0063665a, 0x00633657, + 0x005f861a, 0x005f3613, 0x005f1611, 0x005bf5db, 0x005bb5d7, 0x005b85d4, + 0x0058f5a6, 0x0058c5a3, 0x0058b5a1, 0x0056957b, 0x00566578, 0x00566577, + 0x0054b559, 0x0054a557, 0x0054a556, 0x00538540, 0x00537540, 0x0053753f, + 0x00530532, 0x00530532, 0x00530532, 0x00535531, 0x00535531, 0x00536532, + 0x0054853d, 0x0054853d, 0x0054853e, 0x00564554, 0x00564554, 0x00565556, + 0x0058a575, 0x00589574, 0x0058b577, 0x005b85a0, 0x005b659e, 0x005ba5a1, + 0x005f25d4, 0x005ef5d1, 0x005f25d5, 0x00639614, 0x0063260e, 0x00638613, + 0x0068c660, 0x00681658, 0x0068965f, 0x006eb6bb, 0x006d56ac, 0x006e66b7, + 0x00740718, 0x0071d6fd, 0x0073a714, 0x0077475f, 0x00748736, 0x0077075a, + 0x006d76ef, 0x006c66db, 0x006c66dd, 0x006966ba, 0x006896aa, 0x006896a9, + 0x0064b670, 0x00642666, 0x00640665, 0x00605627, 0x00600620, 0x005fe61e, + 0x005cc5e7, 0x005c75e2, 0x005c65e1, 0x0059c5b3, 0x005985af, 0x005985ae, + 0x00577588, 0x00573584, 0x00573584, 0x00559566, 0x00558564, 0x00558564, + 0x0054754e, 0x0054654d, 0x0054654e, 0x0053f541, 0x0053e540, 0x0053f541, + 0x00545540, 0x00544540, 0x00546540, 0x0055654b, 0x0055554b, 0x0055654c, + 0x00571562, 0x00570561, 0x00572564, 0x00596582, 0x00595581, 0x00598584, + 0x005c65ad, 0x005c35ab, 0x005c65ae, 0x006005e1, 0x005fa5dd, 0x005ff5e2, + 0x00645620, 0x0063e61a, 0x00644620, 0x0069a66e, 0x0068e664, 0x0069666c, + 0x006f96c9, 0x006e36b8, 0x006f46c5, 0x0074f727, 0x0072b70a, 0x00749722, + 0x0078476e, 0x00756744, 0x0077f769, 0x006e9701, 0x006d56eb, 0x006d76ed, + 0x006a76ca, 0x006986b9, 0x006996ba, 0x0065b681, 0x00651674, 0x0064f674, + 0x00615636, 0x0060d62e, 0x0060d62d, 0x005db5f7, 0x005d65f0, 0x005d45ef, + 0x005ab5c1, 0x005a75bd, 0x005a65bc, 0x00585597, 0x00582593, 0x00582593, + 0x00569576, 0x00566573, 0x00567574, 0x0055755f, 0x0055655d, 0x0055655e, + 0x00550552, 0x0054f550, 0x00550552, 0x00555551, 0x00555550, 0x00556551, + 0x0056655c, 0x0056455b, 0x0056655d, 0x00581571, 0x0057f571, 0x00582573, + 0x005a5592, 0x005a358f, 0x005a6592, 0x005d45bb, 0x005d05b8, 0x005d55bc, + 0x0060f5f0, 0x006085ea, 0x0060e5f0, 0x00655630, 0x0064c628, 0x0065462f, + 0x006a967e, 0x0069c673, 0x006a767b, 0x0070a6d9, 0x006f16c6, 0x007046d5, + 0x00761738, 0x0073a719, 0x0075b732, 0x00797781, 0x00766754, 0x0079177a, + 0x006fe716, 0x006e66fd, 0x006ea701, 0x006ba6de, 0x006a86ca, 0x006ab6cd, + 0x0066d693, 0x0065f684, 0x00662686, 0x00626649, 0x0061d63d, 0x0061e63e, + 0x005ec608, 0x005e45ff, 0x005e5600, 0x005bd5d3, 0x005b65cc, 0x005b75cd, + 0x005975a8, 0x005925a3, 0x005935a4, 0x0057c588, 0x00577583, 0x0057a585, + 0x00569571, 0x0056656d, 0x0056956f, 0x00563565, 0x00561562, 0x00563564, + 0x00567564, 0x00566561, 0x00568564, 0x0057856e, 0x0057556c, 0x0057956f, + 0x00592584, 0x0058e580, 0x00593584, 0x005b65a3, 0x005b259e, 0x005b85a4, + 0x005e65cd, 0x005df5c7, 0x005e75cd, 0x00620601, 0x006175fa, 0x00620602, + 0x00667642, 0x0065a637, 0x00665641, 0x006bd690, 0x006ab681, 0x006b868e, + 0x0071e6ec, 0x007016d6, 0x007176e7, 0x0077774d, 0x0074b72a, 0x0076f746, + 0x007ad796, 0x00778766, 0x007a678f, 0x0071372c, 0x006f870f, 0x006ff717, + 0x006d06f4, 0x006b96db, 0x006bf6e2, 0x006816a8, 0x0066f694, 0x0067569a, + 0x0063a65c, 0x0062c64c, 0x00631652, 0x005ff61b, 0x005f460f, 0x005f8613, + 0x005ce5e5, 0x005c65db, 0x005c95df, 0x005a95bb, 0x005a25b3, 0x005a65b7, + 0x0058e59a, 0x00587593, 0x0058c598, 0x0057d584, 0x0057757e, 0x0057c583, + 0x00577578, 0x00571573, 0x00577578, 0x0057b577, 0x00576573, 0x0057c577, + 0x0058b582, 0x0058557c, 0x0058c582, 0x005a5597, 0x0059e590, 0x005a5598, + 0x005c95b6, 0x005c15ae, 0x005ca5b6, 0x005f85df, 0x005ee5d6, 0x005f95e0, + 0x00632614, 0x00626609, 0x00633614, 0x0067b654, 0x0066a647, 0x00678654, + 0x006d06a4, 0x006ba691, 0x006cd6a1, 0x00733701, 0x007126e6, 0x0072d6fb, + 0x0078d764, 0x0075e73b, 0x0078675c, 0x007c57ae, 0x0078b778, 0x007be7a7, + 0x00729742, 0x0070971f, 0x0071472c, 0x006e4709, 0x006c86eb, 0x006d46f6, + 0x006956bd, 0x0067e6a4, 0x006896ae, 0x0064d66f, 0x0063b65b, 0x00644665, + 0x0061162e, 0x0060261d, 0x0060b626, 0x005e25f8, 0x005d45ea, 0x005dc5f2, + 0x005bc5ce, 0x005b05c1, 0x005b85c9, 0x005a15ae, 0x005975a3, 0x0059f5ab, + 0x00590598, 0x0058658d, 0x0058f595, 0x0058a58b, 0x00581582, 0x0058a58b, + 0x0058e58a, 0x00586582, 0x0058f58b, 0x0059d595, 0x0059558c, 0x0059e595, + 0x005b75a9, 0x005ad5a0, 0x005b85ab, 0x005dc5c9, 0x005cf5bd, 0x005dc5c9, + 0x0060b5f2, 0x005fd5e4, 0x0060b5f2, 0x00645626, 0x00634617, 0x00645627, + 0x0068e668, 0x00678654, 0x0068b667, 0x006e56b7, 0x006c969f, 0x006e06b4, + 0x00749717, 0x007226f6, 0x00743711, 0x007a577a, 0x0076f74c, 0x0079d773, + 0x007de7c6, 0x0079d78a, 0x007d67be, 0x0073c756, 0x0071672e, 0x0072773f, + 0x006f671c, 0x006d56f9, 0x006e6709, 0x006a76cf, 0x0068b6b0, 0x0069a6c0, + 0x0065e681, 0x00647668, 0x00655676, 0x0062263f, 0x0060e629, 0x0061b636, + 0x005f2608, 0x005e15f6, 0x005ed603, 0x005cd5de, 0x005bd5cd, 0x005c95d9, + 0x005b25be, 0x005a35af, 0x005b05bb, 0x005a15a8, 0x0059359a, 0x005a05a7, + 0x0059a59d, 0x0058e58f, 0x0059b59c, 0x005a059c, 0x0059258f, 0x005a059c, + 0x005ae5a5, 0x005a0598, 0x005af5a6, 0x005c85ba, 0x005b95ab, 0x005c95bb, + 0x005ec5d9, 0x005dc5c9, 0x005ed5da, 0x0061b602, 0x006085f1, 0x0061c603, + 0x00656637, 0x00641623, 0x00657638, 0x0069e678, 0x00684661, 0x0069e678, + 0x006f76c9, 0x006d56ab, 0x006f36c6, 0x0075d729, 0x0072f702, 0x00756724, + 0x007bb78f, 0x0077f75b, 0x007b3787, 0x007f47dc, 0x007ae79a, 0x007ec7d5, + 0x0074b765, 0x00721739, 0x0073774e, 0x0070572b, 0x006e0703, 0x006f5718, + 0x006b46dd, 0x006946ba, 0x006a96cf, 0x0066c68e, 0x00650671, 0x00663685, + 0x0062f64c, 0x00618632, 0x00629644, 0x005ff616, 0x005ea600, 0x005fa610, + 0x005d95ea, 0x005c65d7, 0x005d75e7, 0x005be5cc, 0x005ae5b9, 0x005be5c9, + 0x005ae5b6, 0x0059d5a4, 0x005ae5b4, 0x005a85aa, 0x0059859a, 0x005a85aa, + 0x005ad5a9, 0x0059d599, 0x005ae5ab, 0x005bb5b3, 0x005ab5a2, 0x005bd5b4, + 0x005d55c7, 0x005c35b5, 0x005d75c9, 0x005fa5e6, 0x005e55d3, 0x005fb5e7, + 0x00629610, 0x006125fa, 0x0062a611, 0x00664645, 0x0064a62c, 0x00665646, + 0x006ac686, 0x0068e66a, 0x006ac686, 0x007066d7, 0x006df6b4, 0x007026d5, + 0x0076c738, 0x0073a70c, 0x00766733, 0x007cb7a0, 0x0078a765, 0x007c4798, + 0x008057ed, 0x007bc7a8, 0x008007e7, 0x00756770, 0x00729740, 0x00742759, + 0x00710735, 0x006e770a, 0x006ff724, 0x006bf6e8, 0x0069b6c1, 0x006b36da, + 0x00675699, 0x00657678, 0x0066d68f, 0x00639656, 0x0061e639, 0x0063364f, + 0x0060861f, 0x005f1606, 0x0060561b, 0x005e35f5, 0x005ce5de, 0x005e15f1, + 0x005c95d4, 0x005b55c0, 0x005c75d3, 0x005b85bf, 0x005a55ab, 0x005b85be, + 0x005b25b3, 0x0059f5a0, 0x005b35b4, 0x005b65b3, 0x005a35a0, 0x005b85b4, + 0x005c55bc, 0x005b15a9, 0x005c75be, 0x005df5d1, 0x005c95bc, 0x005e15d2, + 0x006025ef, 0x005ec5d9, 0x006055f2, 0x00632619, 0x00618601, 0x0063461b, + 0x0066d64f, 0x00650633, 0x0066f64f, 0x006b7691, 0x00694670, 0x006b6691, + 0x007106e1, 0x006e56bb, 0x0070d6df, 0x00777743, 0x00741712, 0x0077273e, + 0x007d77ab, 0x0079376d, 0x007d17a4, 0x008137fa, 0x007c67b1, 0x0080d7f4, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, + 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, 0x00224224, +}; + +static const u32 gamma_lut[] = { + 0x000f5000, 0x0019d0f5, 0x0022119d, 0x00294221, 0x002f7294, 0x003502f7, + 0x00398350, 0x003eb398, 0x004503eb, 0x004e0450, 0x005314e0, 0x0056a531, + 0x005a156a, 0x005d55a1, 0x006075d5, 0x00637607, 0x00666637, 0x00693666, + 0x006be693, 0x006e86be, 0x007116e8, 0x00739711, 0x00760739, 0x00786760, + 0x007aa786, 0x007ce7aa, 0x007f27ce, 0x008147f2, 0x00836814, 0x00857836, + 0x00878857, 0x00897878, 0x008b7897, 0x008d58b7, 0x008f48d5, 0x009118f4, + 0x0092f911, 0x0094b92f, 0x0096894b, 0x00984968, 0x0099f984, 0x009ba99f, + 0x009d59ba, 0x009ef9d5, 0x00a0a9ef, 0x00a23a0a, 0x00a3da23, 0x00a56a3d, + 0x00a6ea56, 0x00a87a6e, 0x00a9fa87, 0x00ab7a9f, 0x00acfab7, 0x00ae6acf, + 0x00afdae6, 0x00b14afd, 0x00b2bb14, 0x00b41b2b, 0x00b57b41, 0x00b6db57, + 0x00b83b6d, 0x00b99b83, 0x00baeb99, 0x00bc3bae, 0x00bd8bc3, 0x00bedbd8, + 0x00c02bed, 0x00c16c02, 0x00c2ac16, 0x00c3ec2a, 0x00c52c3e, 0x00c66c52, + 0x00c7ac66, 0x00c8dc7a, 0x00ca0c8d, 0x00cb4ca0, 0x00cc7cb4, 0x00cd9cc7, + 0x00ceccd9, 0x00cffcec, 0x00d11cff, 0x00d23d11, 0x00d36d23, 0x00d48d36, + 0x00d5ad48, 0x00d6bd5a, 0x00d7dd6b, 0x00d8fd7d, 0x00da0d8f, 0x00db1da0, + 0x00dc3db1, 0x00dd4dc3, 0x00de5dd4, 0x00df6de5, 0x00e06df6, 0x00e17e06, + 0x00e28e17, 0x00e38e28, 0x00e48e38, 0x00e59e48, 0x00e69e59, 0x00e79e69, + 0x00e89e79, 0x00e99e89, 0x00ea9e99, 0x00eb9ea9, 0x00ec8eb9, 0x00ed8ec8, + 0x00ee7ed8, 0x00ef7ee7, 0x00f06ef7, 0x00f15f06, 0x00f24f15, 0x00f33f24, + 0x00f42f33, 0x00f51f42, 0x00f60f51, 0x00f6ff60, 0x00f7ef6f, 0x00f8cf7e, + 0x00f9bf8c, 0x00fa9f9b, 0x00fb8fa9, 0x00fc6fb8, 0x00fd5fc6, 0x00fe3fd5, + 0x00ff1fe3, 0x00fffff1, +}; + +static const struct t23_profile_reg ysp_regs_0[] = { + { 0x07090, 0x00000001 }, + { 0x07000, 0x00030000 }, + { 0x07004, 0x00050000 }, + { 0x07008, 0x010a0001 }, + { 0x0700c, 0x10ff0c00 }, + { 0x07010, 0x0100963f }, + { 0x07014, 0x00000a02 }, + { 0x07018, 0x0a08003f }, + { 0x0701c, 0x0a140000 }, + { 0x07020, 0x003fff08 }, + { 0x07024, 0x00133200 }, + { 0x07028, 0x00010610 }, + { 0x0702c, 0xff00ff00 }, + { 0x07030, 0x0003ff00 }, + { 0x07034, 0x003f003f }, + { 0x07038, 0x00000000 }, + { 0x0703c, 0x00000000 }, + { 0x07040, 0x00000000 }, + { 0x07044, 0x00000000 }, + { 0x07048, 0x20202020 }, + { 0x0704c, 0x20202020 }, + { 0x07050, 0x20202020 }, + { 0x07054, 0x20202020 }, + { 0x07058, 0x20202020 }, + { 0x0705c, 0x20202020 }, + { 0x07060, 0x20202020 }, + { 0x07064, 0x20202020 }, + { 0x07068, 0x00190019 }, + { 0x0706c, 0x00190019 }, + { 0x07070, 0x00200020 }, + { 0x07074, 0x00200020 }, + { 0x07078, 0x00010103 }, + { 0x0707c, 0x00010103 }, + { 0x07090, 0x00000001 }, +}; + +static const struct t23_profile_reg ydns_regs_0[] = { + { 0x07af0, 0x00c89650 }, + { 0x07af4, 0x00006464 }, + { 0x07af8, 0x00022222 }, + { 0x07afc, 0x00001410 }, + { 0x07af0, 0x00c89650 }, + { 0x07af4, 0x00006464 }, + { 0x07af8, 0x00022222 }, + { 0x07afc, 0x00001410 }, +}; + +static const struct t23_profile_reg sdns_regs_0[] = { + { 0x08800, 0x00000010 }, + { 0x08804, 0x00020002 }, + { 0x08808, 0x0bb89c41 }, + { 0x0880c, 0x00008888 }, + { 0x08810, 0x00000100 }, + { 0x08814, 0x281e140a }, + { 0x08818, 0x00080000 }, + { 0x0881c, 0x00070005 }, + { 0x08820, 0x000f000a }, + { 0x08824, 0x00190014 }, + { 0x08828, 0x0028001e }, + { 0x0882c, 0x003c0032 }, + { 0x08830, 0x00500046 }, + { 0x08834, 0x0064005a }, + { 0x08838, 0x0000006e }, + { 0x0883c, 0x00070005 }, + { 0x08840, 0x000f000a }, + { 0x08844, 0x00190014 }, + { 0x08848, 0x0028001e }, + { 0x0884c, 0x003c0032 }, + { 0x08850, 0x00500046 }, + { 0x08854, 0x0064005a }, + { 0x08858, 0x0000006e }, + { 0x0885c, 0x03030303 }, + { 0x08860, 0x02020202 }, + { 0x08864, 0x01010102 }, + { 0x08868, 0x01010101 }, + { 0x0886c, 0x06060808 }, + { 0x08870, 0x03030404 }, + { 0x08874, 0x02020202 }, + { 0x08878, 0x01010102 }, + { 0x0887c, 0x0410ff0a }, + { 0x08880, 0x00200010 }, + { 0x08884, 0x0050003c }, + { 0x08888, 0x00a00078 }, + { 0x0888c, 0x00dc00c8 }, + { 0x08890, 0x012c0104 }, + { 0x08894, 0x017c0154 }, + { 0x08898, 0x01cc01a4 }, + { 0x0889c, 0x00000258 }, + { 0x088a0, 0x00002020 }, + { 0x088a4, 0x00002020 }, + { 0x088a8, 0x00002020 }, + { 0x088ac, 0x00002020 }, + { 0x088b0, 0x00002020 }, + { 0x088b4, 0x00002020 }, + { 0x088b8, 0x00002020 }, + { 0x088bc, 0x00000000 }, + { 0x088c0, 0x07d00000 }, + { 0x088c4, 0x0002999a }, + { 0x088c8, 0x0001999a }, + { 0x088cc, 0x0000999a }, + { 0x088d0, 0x000f0007 }, + { 0x088d4, 0x0033001e }, + { 0x088d8, 0x003b001c }, + { 0x088dc, 0x00ce0079 }, + { 0x088e0, 0x0084003e }, + { 0x088e4, 0x01d00111 }, + { 0x088e8, 0x00eb006e }, + { 0x088ec, 0x033801e5 }, + { 0x088f0, 0x016f00ad }, + { 0x088f4, 0x050802f7 }, + { 0x088f8, 0x021000f8 }, + { 0x088fc, 0x073e0445 }, + { 0x08900, 0x02ce0152 }, + { 0x08904, 0x09db05d0 }, + { 0x08908, 0x03aa01b9 }, + { 0x0890c, 0x0ce00797 }, + { 0x08910, 0x04a3022e }, + { 0x08914, 0x104c099b }, + { 0x08918, 0x05ba02b1 }, + { 0x0891c, 0x141e0bdb }, + { 0x08920, 0x04960342 }, + { 0x08924, 0x18580e59 }, + { 0x08928, 0x083f03e1 }, + { 0x0892c, 0x1cf91113 }, + { 0x08930, 0x09ae048d }, + { 0x08934, 0x22001409 }, + { 0x08938, 0x0b3b0547 }, + { 0x0893c, 0x276f173e }, + { 0x08940, 0x0ce3047f }, + { 0x08944, 0x2d441aad }, + { 0x08948, 0x0ea906e5 }, + { 0x0894c, 0x33801e5b }, + { 0x08950, 0x00003333 }, + { 0x08954, 0x00002000 }, + { 0x08958, 0x00001111 }, + { 0x0895c, 0x0000055d }, + { 0x08960, 0x00000ccd }, + { 0x08964, 0x00000842 }, + { 0x08968, 0x00000421 }, + { 0x0896c, 0x00000153 }, + { 0x08970, 0x000005c8 }, + { 0x08974, 0x000003a8 }, + { 0x08978, 0x000001d1 }, + { 0x0897c, 0x00000097 }, + { 0x08980, 0x00000342 }, + { 0x08984, 0x0000020c }, + { 0x08988, 0x00000106 }, + { 0x0898c, 0x00000055 }, + { 0x08990, 0x00000212 }, + { 0x08994, 0x00000152 }, + { 0x08998, 0x000000a7 }, + { 0x0899c, 0x00000037 }, + { 0x089a0, 0x00000172 }, + { 0x089a4, 0x000000ea }, + { 0x089a8, 0x00000074 }, + { 0x089ac, 0x00000026 }, + { 0x089b0, 0x0000010f }, + { 0x089b4, 0x000000ac }, + { 0x089b8, 0x00000055 }, + { 0x089bc, 0x0000001c }, + { 0x089c0, 0x000000d0 }, + { 0x089c4, 0x00000084 }, + { 0x089c8, 0x00000041 }, + { 0x089cc, 0x00000015 }, + { 0x089d0, 0x000000a4 }, + { 0x089d4, 0x00000068 }, + { 0x089d8, 0x00000034 }, + { 0x089dc, 0x00000011 }, + { 0x089e0, 0x00000085 }, + { 0x089e4, 0x00000054 }, + { 0x089e8, 0x0000002a }, + { 0x089ec, 0x0000000e }, + { 0x089f0, 0x0000006e }, + { 0x089f4, 0x00000046 }, + { 0x089f8, 0x00000023 }, + { 0x089fc, 0x0000000b }, + { 0x08a00, 0x0000005c }, + { 0x08a04, 0x0000003b }, + { 0x08a08, 0x0000001d }, + { 0x08a0c, 0x00000009 }, + { 0x08a10, 0x0000004f }, + { 0x08a14, 0x00000032 }, + { 0x08a18, 0x00000019 }, + { 0x08a1c, 0x00000008 }, + { 0x08a20, 0x00000044 }, + { 0x08a24, 0x0000002b }, + { 0x08a28, 0x00000015 }, + { 0x08a2c, 0x00000007 }, + { 0x08a30, 0x0000003b }, + { 0x08a34, 0x00000025 }, + { 0x08a38, 0x00000013 }, + { 0x08a3c, 0x00000006 }, + { 0x08a40, 0x00000034 }, + { 0x08a44, 0x00000021 }, + { 0x08a48, 0x00000010 }, + { 0x08a4c, 0x00000005 }, + { 0x08a50, 0x04030201 }, + { 0x08a54, 0x08070605 }, + { 0x08a58, 0x0c0b0a09 }, + { 0x08a5c, 0x100f0e0d }, + { 0x08a60, 0x00000011 }, + { 0x08a64, 0x00000a00 }, + { 0x08a68, 0x00ff000a }, + { 0x08a6c, 0x00000a0a }, + { 0x08a70, 0x00002468 }, + { 0x08a74, 0x003f1408 }, + { 0x08a78, 0x000a000a }, + { 0x08a7c, 0x000a000a }, + { 0x08a80, 0x00001fff }, + { 0x08a84, 0x000a000a }, + { 0x08a88, 0x000a000a }, + { 0x08a8c, 0x00001fff }, + { 0x08a90, 0x00ff00c8 }, + { 0x08a94, 0x000000c8 }, + { 0x08a98, 0x000001ff }, + { 0x08a9c, 0x00033200 }, + { 0x08aa0, 0x00001610 }, + { 0x08aa4, 0x00000000 }, + { 0x08aa8, 0x00000000 }, + { 0x08aac, 0x00001fff }, + { 0x08ab0, 0x00000000 }, + { 0x08ab4, 0x00000000 }, + { 0x08ab8, 0x00001fff }, + { 0x08abc, 0x00c8003f }, + { 0x08ac0, 0x0000c8ff }, + { 0x08ac4, 0x00000000 }, + { 0x08ac8, 0x00000003 }, + { 0x08acc, 0x00000000 }, + { 0x08ad0, 0x00000000 }, + { 0x08ad4, 0x00000000 }, + { 0x08ad8, 0x00000000 }, + { 0x08adc, 0x1c202020 }, + { 0x08ae0, 0x181a1a1c }, + { 0x08ae4, 0x10121416 }, + { 0x08ae8, 0x080a000e }, + { 0x08aec, 0x00000006 }, + { 0x08af0, 0x00000000 }, + { 0x08af4, 0x1c202020 }, + { 0x08af8, 0x181a1a1c }, + { 0x08afc, 0x10121416 }, + { 0x08b00, 0x080a0c0e }, + { 0x08b04, 0x00000006 }, + { 0x08b08, 0x00000000 }, + { 0x08b0c, 0x1c202020 }, + { 0x08b10, 0x181a1a1c }, + { 0x08b14, 0x10121416 }, + { 0x08b18, 0x080a0c0e }, + { 0x08b1c, 0x00000006 }, + { 0x08b20, 0x00000000 }, + { 0x08b24, 0x1c202020 }, + { 0x08b28, 0x181a1a1c }, + { 0x08b2c, 0x10121416 }, + { 0x08b30, 0x080a0c0e }, + { 0x08b34, 0x00000006 }, + { 0x08b38, 0x00000000 }, + { 0x08b4c, 0x00000001 }, + { 0x08b4c, 0x00000001 }, + { 0x08800, 0x00000010 }, + { 0x08804, 0x00020002 }, + { 0x08808, 0x0bb89c41 }, + { 0x08810, 0x00000100 }, + { 0x08814, 0x281e140a }, + { 0x0885c, 0x03030303 }, + { 0x08860, 0x02020202 }, + { 0x08864, 0x01010102 }, + { 0x08868, 0x01010101 }, + { 0x0886c, 0x06060808 }, + { 0x08870, 0x03030404 }, + { 0x08874, 0x02020202 }, + { 0x08878, 0x01010102 }, + { 0x0887c, 0x0410ff0a }, + { 0x08a64, 0x00000a00 }, + { 0x08a68, 0x00ff000a }, + { 0x08a6c, 0x00000a0a }, + { 0x08a70, 0x00002468 }, + { 0x08a74, 0x003f1408 }, + { 0x08a78, 0x000a000a }, + { 0x08a7c, 0x000a000a }, + { 0x08a80, 0x00001fff }, + { 0x08a84, 0x000a000a }, + { 0x08a88, 0x000a000a }, + { 0x08a8c, 0x00001fff }, + { 0x08a90, 0x00ff00c8 }, + { 0x08a94, 0x000000c8 }, + { 0x08a98, 0x000001ff }, + { 0x08aa4, 0x00000000 }, + { 0x08aa8, 0x00000000 }, + { 0x08ab0, 0x00000000 }, + { 0x08ab4, 0x00000000 }, + { 0x08ab8, 0x00001fff }, + { 0x08abc, 0x00c8003f }, + { 0x08ac0, 0x0000c8ff }, + { 0x08ac4, 0x00000000 }, +}; + +static const struct t23_profile_reg hldc_regs_0[] = { + { 0x09000, 0x00010000 }, + { 0x09004, 0x019900cc }, + { 0x09008, 0x019900cc }, + { 0x0900c, 0x000006f1 }, + { 0x09010, 0x000006f1 }, + { 0x09014, 0x00000375 }, + { 0x09018, 0x00000375 }, + { 0x0901c, 0x00000000 }, + { 0x09020, 0x00000000 }, + { 0x09024, 0x00000020 }, + { 0x09028, 0x00000020 }, + { 0x09044, 0x00000003 }, + { 0x09000, 0x00010000 }, + { 0x09004, 0x019900cc }, + { 0x09008, 0x019900cc }, + { 0x0900c, 0x000006f1 }, + { 0x09010, 0x000006f1 }, + { 0x09014, 0x00000375 }, + { 0x09018, 0x00000375 }, + { 0x0901c, 0x00000000 }, + { 0x09020, 0x00000000 }, + { 0x09024, 0x00000020 }, + { 0x09028, 0x00000020 }, + { 0x09044, 0x00000003 }, +}; + +static const struct t23_profile_reg clm_regs_0[] = { + { 0x06800, 0x00000001 }, + { 0x06804, 0x00000000 }, +}; + +static const u32 clm_lut_1[] = { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, + 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, + 0x9f3e7cf9, 0x00000007, 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, + 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, + 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, +}; + +static const u32 clm_lut_2[] = { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, + 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, 0x9f3e7cf9, 0x00000007, + 0x9f3e7cf9, 0x00000007, 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, + 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, + 0x4e9d3a74, 0x00000007, 0x4e9d3a74, 0x00000007, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, +}; + +static const u32 clm_lut_3[] = { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xa7d3e9f4, 0x00001f4f, + 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, + 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, + 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, + 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, + 0x773b9dce, 0x00001cee, 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, + 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, + 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, 0xbcde6f37, 0x00001379, + 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, + 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, + 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, + 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, + 0xa4d26934, 0x00001349, 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, + 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, + 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, 0x66b359ac, 0x00001acd, + 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, + 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, + 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, + 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, + 0xe773b9dc, 0x00001dce, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, +}; + +static const u32 clm_lut_4[] = { + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xa7d3e9f4, 0x00001f4f, + 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, + 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, 0xa7d3e9f4, 0x00001f4f, + 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, + 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, 0x773b9dce, 0x00001cee, + 0x773b9dce, 0x00001cee, 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, + 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, + 0xedf6fb7d, 0x000017db, 0xedf6fb7d, 0x000017db, 0xbcde6f37, 0x00001379, + 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, + 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, 0xbcde6f37, 0x00001379, + 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, + 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, 0xa4d26934, 0x00001349, + 0xa4d26934, 0x00001349, 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, + 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, + 0xcd66b359, 0x0000159a, 0xcd66b359, 0x0000159a, 0x66b359ac, 0x00001acd, + 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, + 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, 0x66b359ac, 0x00001acd, + 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, + 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, 0xe773b9dc, 0x00001dce, + 0xe773b9dc, 0x00001dce, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, +}; + +static const struct t23_profile_reg dpc_regs_0[] = { + { 0x02864, 0x00000000 }, + { 0x02868, 0x00000000 }, + { 0x0286c, 0x00000000 }, + { 0x02870, 0x00000000 }, + { 0x02874, 0x00000000 }, + { 0x02878, 0x00000000 }, + { 0x0287c, 0x00000000 }, + { 0x02880, 0x00000000 }, + { 0x02800, 0x00000000 }, + { 0x02838, 0x0118005a }, + { 0x0281c, 0x0118005a }, + { 0x0280c, 0x00010001 }, + { 0x0283c, 0x00640064 }, + { 0x02820, 0x00640064 }, + { 0x02840, 0x01cc01a4 }, + { 0x02824, 0x01cc01a4 }, + { 0x02844, 0x025801f4 }, + { 0x02828, 0x025801f4 }, + { 0x02848, 0x01900168 }, + { 0x0282c, 0x01900168 }, + { 0x0284c, 0x01f401cc }, + { 0x02830, 0x01f401cc }, + { 0x02808, 0x00010101 }, + { 0x02818, 0x003f00ff }, + { 0x02850, 0x012c0028 }, + { 0x02834, 0x012c0028 }, + { 0x02814, 0x00ff00ff }, + { 0x02810, 0x00000010 }, + { 0x02804, 0x00001010 }, + { 0x02884, 0x00000000 }, + { 0x02854, 0x00000000 }, + { 0x02858, 0x00000000 }, + { 0x0285c, 0x00000000 }, + { 0x02860, 0x0fff0000 }, + { 0x02898, 0x00000001 }, + { 0x02898, 0x00000001 }, + { 0x02838, 0x0118005a }, + { 0x0281c, 0x0118005a }, + { 0x0280c, 0x00010001 }, + { 0x0283c, 0x00640064 }, + { 0x02820, 0x00640064 }, + { 0x02840, 0x01cc01a4 }, + { 0x02824, 0x01cc01a4 }, + { 0x02844, 0x025801f4 }, + { 0x02828, 0x025801f4 }, + { 0x02848, 0x01900168 }, + { 0x0282c, 0x01900168 }, + { 0x0284c, 0x01f401cc }, + { 0x02830, 0x01f401cc }, + { 0x02808, 0x00010101 }, + { 0x02818, 0x003f00ff }, + { 0x02850, 0x012c0028 }, + { 0x02834, 0x012c0028 }, + { 0x02814, 0x00ff00ff }, + { 0x02854, 0x00000000 }, + { 0x02858, 0x00000000 }, + { 0x0285c, 0x00000000 }, + { 0x02860, 0x0fff0000 }, +}; + +static const struct t23_profile_reg bcsh_regs_0[] = { + { 0x08000, 0x000003ff }, + { 0x08004, 0x0000036c }, + { 0x08008, 0x004003ac }, + { 0x0800c, 0x000003ff }, + { 0x08010, 0x004003bc }, + { 0x08014, 0x004003bc }, + { 0x08018, 0x044003c0 }, + { 0x0801c, 0x04000400 }, + { 0x08020, 0x04000400 }, + { 0x08024, 0x000003ff }, + { 0x08028, 0x00000000 }, + { 0x0802c, 0x03ff0000 }, + { 0x08030, 0x00000000 }, + { 0x08034, 0x00000000 }, + { 0x08038, 0x000003ff }, + { 0x0803c, 0x00100001 }, + { 0x08040, 0x00580018 }, + { 0x08044, 0x00100001 }, + { 0x08048, 0x03600320 }, + { 0x0804c, 0x00400001 }, + { 0x08050, 0x00180008 }, + { 0x08054, 0x02000001 }, + { 0x08058, 0x03330413 }, + { 0x0805c, 0x03440010 }, + { 0x08060, 0x034c0008 }, + { 0x08064, 0x00040021 }, + { 0x08068, 0x002a000a }, + { 0x0806c, 0x041a04b0 }, + { 0x08070, 0x041a04b0 }, +}; + +static const struct t23_profile_reg adr_720_regs_0[] = { + { 0x04490, 0x00000000 }, + { 0x04000, 0x00b400d4 }, + { 0x04010, 0x00b40000 }, + { 0x04014, 0x021c0168 }, + { 0x04018, 0x000002d0 }, + { 0x0401c, 0x00d40000 }, + { 0x04020, 0x027c01a8 }, + { 0x04024, 0x04240350 }, + { 0x04028, 0x00000500 }, + { 0x04454, 0x02a4002c }, + { 0x04458, 0x04cc0034 }, + { 0x04390, 0x00400020 }, + { 0x04394, 0x00c00080 }, + { 0x04398, 0x01800100 }, + { 0x0439c, 0x03000200 }, + { 0x043a0, 0x06000400 }, + { 0x043a4, 0x00000800 }, + { 0x04354, 0x04000400 }, + { 0x04358, 0x04000400 }, + { 0x0435c, 0x04000400 }, + { 0x04360, 0x04000400 }, + { 0x04364, 0x00000400 }, + { 0x04084, 0x00400020 }, + { 0x04088, 0x00c00080 }, + { 0x0408c, 0x01800100 }, + { 0x04090, 0x03000200 }, + { 0x04094, 0x06000400 }, + { 0x04098, 0x00200800 }, + { 0x0409c, 0x00800040 }, + { 0x040a0, 0x010000c0 }, + { 0x040a4, 0x02000180 }, + { 0x040a8, 0x04000300 }, + { 0x040ac, 0x08000600 }, + { 0x040b0, 0x00400020 }, + { 0x040b4, 0x00c00080 }, + { 0x040b8, 0x01800100 }, + { 0x040bc, 0x03000200 }, + { 0x040c0, 0x06000400 }, + { 0x040c4, 0x00200800 }, + { 0x040c8, 0x00800040 }, + { 0x040cc, 0x010000c0 }, + { 0x040d0, 0x02000180 }, + { 0x040d4, 0x04000300 }, + { 0x040d8, 0x08000600 }, + { 0x040dc, 0x00400020 }, + { 0x040e0, 0x00c00080 }, + { 0x040e4, 0x01800100 }, + { 0x040e8, 0x03000200 }, + { 0x040ec, 0x06000400 }, + { 0x040f0, 0x00200800 }, + { 0x040f4, 0x00800040 }, + { 0x040f8, 0x010000c0 }, + { 0x040fc, 0x02000180 }, + { 0x04100, 0x04000300 }, + { 0x04104, 0x08000600 }, + { 0x04108, 0x00400020 }, + { 0x0410c, 0x00c00080 }, + { 0x04110, 0x01800100 }, + { 0x04114, 0x03000200 }, + { 0x04118, 0x06000400 }, + { 0x0411c, 0x00200800 }, + { 0x04120, 0x00800040 }, + { 0x04124, 0x010000c0 }, + { 0x04128, 0x02000180 }, + { 0x0412c, 0x04000300 }, + { 0x04130, 0x08000600 }, + { 0x04134, 0x00400020 }, + { 0x04138, 0x00c00080 }, + { 0x0413c, 0x01800100 }, + { 0x04140, 0x03000200 }, + { 0x04144, 0x06000400 }, + { 0x04148, 0x00200800 }, + { 0x0414c, 0x00800040 }, + { 0x04150, 0x010000c0 }, + { 0x04154, 0x02000180 }, + { 0x04158, 0x04000300 }, + { 0x0415c, 0x08000600 }, + { 0x04160, 0x00400020 }, + { 0x04164, 0x00c00080 }, + { 0x04168, 0x01800100 }, + { 0x0416c, 0x03000200 }, + { 0x04170, 0x06000400 }, + { 0x04174, 0x00200800 }, + { 0x04178, 0x00800040 }, + { 0x0417c, 0x010000c0 }, + { 0x04180, 0x02000180 }, + { 0x04184, 0x04000300 }, + { 0x04188, 0x08000600 }, + { 0x0418c, 0x00400020 }, + { 0x04190, 0x00c00080 }, + { 0x04194, 0x01800100 }, + { 0x04198, 0x03000200 }, + { 0x0419c, 0x06000400 }, + { 0x041a0, 0x00200800 }, + { 0x041a4, 0x00800040 }, + { 0x041a8, 0x010000c0 }, + { 0x041ac, 0x02000180 }, + { 0x041b0, 0x04000300 }, + { 0x041b4, 0x08000600 }, + { 0x041b8, 0x00400020 }, + { 0x041bc, 0x00c00080 }, + { 0x041c0, 0x01800100 }, + { 0x041c4, 0x03000200 }, + { 0x041c8, 0x06000400 }, + { 0x041cc, 0x00200800 }, + { 0x041d0, 0x00800040 }, + { 0x041d4, 0x010000c0 }, + { 0x041d8, 0x02000180 }, + { 0x041dc, 0x04000300 }, + { 0x041e0, 0x08000600 }, + { 0x041e4, 0x00400020 }, + { 0x041e8, 0x00c00080 }, + { 0x041ec, 0x01800100 }, + { 0x041f0, 0x03000200 }, + { 0x041f4, 0x06000400 }, + { 0x041f8, 0x00200800 }, + { 0x041fc, 0x00800040 }, + { 0x04200, 0x010000c0 }, + { 0x04204, 0x02000180 }, + { 0x04208, 0x04000300 }, + { 0x0420c, 0x08000600 }, + { 0x04210, 0x00400020 }, + { 0x04214, 0x00c00080 }, + { 0x04218, 0x01800100 }, + { 0x0421c, 0x03000200 }, + { 0x04220, 0x06000400 }, + { 0x04224, 0x00200800 }, + { 0x04228, 0x00800040 }, + { 0x0422c, 0x010000c0 }, + { 0x04230, 0x02000180 }, + { 0x04234, 0x04000300 }, + { 0x04238, 0x08000600 }, + { 0x0423c, 0x00400020 }, + { 0x04240, 0x00c00080 }, + { 0x04244, 0x01800100 }, + { 0x04248, 0x03000200 }, + { 0x0424c, 0x06000400 }, + { 0x04250, 0x00200800 }, + { 0x04254, 0x00800040 }, + { 0x04258, 0x010000c0 }, + { 0x0425c, 0x02000180 }, + { 0x04260, 0x04000300 }, + { 0x04264, 0x08000600 }, + { 0x04268, 0x00400020 }, + { 0x0426c, 0x00c00080 }, + { 0x04270, 0x01800100 }, + { 0x04274, 0x03000200 }, + { 0x04278, 0x06000400 }, + { 0x0427c, 0x00200800 }, + { 0x04280, 0x00800040 }, + { 0x04284, 0x010000c0 }, + { 0x04288, 0x02000180 }, + { 0x0428c, 0x04000300 }, + { 0x04290, 0x08000600 }, + { 0x04004, 0x014000a0 }, + { 0x04448, 0x03330266 }, + { 0x0444c, 0x04cd0400 }, + { 0x04450, 0x0000059a }, + { 0x0402c, 0x072509bc }, + { 0x04030, 0x052505f0 }, + { 0x04034, 0x0414048d }, + { 0x04038, 0x035903af }, + { 0x0403c, 0x02ca030d }, + { 0x04040, 0x0257028e }, + { 0x04044, 0x01f60224 }, + { 0x04048, 0x01a301cb }, + { 0x0404c, 0x0159017d }, + { 0x04050, 0x01180138 }, + { 0x04054, 0x00dd00fa }, + { 0x04058, 0x00a700c2 }, + { 0x0405c, 0x0076008e }, + { 0x04060, 0x0048005f }, + { 0x04064, 0x001e0033 }, + { 0x04068, 0x0000000a }, + { 0x04340, 0x00800000 }, + { 0x04344, 0x02000100 }, + { 0x04348, 0x06000400 }, + { 0x0434c, 0x0e000a00 }, + { 0x04350, 0x00000f00 }, + { 0x04368, 0x00070007 }, + { 0x0436c, 0x00090008 }, + { 0x04370, 0x000a0009 }, + { 0x04374, 0x0008000a }, + { 0x0406c, 0x00400020 }, + { 0x04070, 0x00c00080 }, + { 0x04074, 0x01800100 }, + { 0x04078, 0x03000200 }, + { 0x0407c, 0x06000400 }, + { 0x04080, 0x00000800 }, + { 0x04334, 0x06060505 }, + { 0x04338, 0x08070706 }, + { 0x0433c, 0x0b090908 }, + { 0x04294, 0x00000000 }, + { 0x04298, 0x00000000 }, + { 0x0429c, 0x01010100 }, + { 0x042a0, 0x01010101 }, + { 0x042a4, 0x03020202 }, + { 0x042a8, 0x04040303 }, + { 0x042ac, 0x05050504 }, + { 0x042b0, 0x05050505 }, + { 0x042b4, 0x00000000 }, + { 0x042b8, 0x00000000 }, + { 0x042bc, 0x00000000 }, + { 0x042c0, 0x01010100 }, + { 0x042c4, 0x01010101 }, + { 0x042c8, 0x02020202 }, + { 0x042cc, 0x02020202 }, + { 0x042d0, 0x02020202 }, + { 0x042d4, 0x00000000 }, + { 0x042d8, 0x00000000 }, + { 0x042dc, 0x01010000 }, + { 0x042e0, 0x01010101 }, + { 0x042e4, 0x02020201 }, + { 0x042e8, 0x02020202 }, + { 0x042ec, 0x02020202 }, + { 0x042f0, 0x02020202 }, + { 0x04314, 0x00000000 }, + { 0x04318, 0x00000000 }, + { 0x0431c, 0x00000000 }, + { 0x04320, 0x00000000 }, + { 0x04324, 0x00000000 }, + { 0x04328, 0x00000000 }, + { 0x0432c, 0x00000000 }, + { 0x04330, 0x00000000 }, + { 0x042f4, 0x00000000 }, + { 0x042f8, 0x01010000 }, + { 0x042fc, 0x01010101 }, + { 0x04300, 0x02020202 }, + { 0x04304, 0x03030303 }, + { 0x04308, 0x03030303 }, + { 0x0430c, 0x03030303 }, + { 0x04310, 0x03030303 }, + { 0x04378, 0x00400020 }, + { 0x0437c, 0x00c00080 }, + { 0x04380, 0x01800100 }, + { 0x04384, 0x03000200 }, + { 0x04388, 0x06000400 }, + { 0x0438c, 0x00000800 }, + { 0x043a8, 0x06060505 }, + { 0x043ac, 0x08070706 }, + { 0x043b0, 0x0b090908 }, + { 0x043b4, 0x004a0000 }, + { 0x043b8, 0x008e006f }, + { 0x043bc, 0x00c100a9 }, + { 0x043c0, 0x00ec00d7 }, + { 0x043c4, 0x01840100 }, + { 0x043c8, 0x037b024c }, + { 0x043cc, 0x00310000 }, + { 0x043d0, 0x00750054 }, + { 0x043d4, 0x00b00093 }, + { 0x043d8, 0x00e600cb }, + { 0x043dc, 0x01be0100 }, + { 0x043e0, 0x05470308 }, + { 0x043e4, 0x00200000 }, + { 0x043e8, 0x00600040 }, + { 0x043ec, 0x00a00080 }, + { 0x043f0, 0x00e000c0 }, + { 0x043f4, 0x02000100 }, + { 0x043f8, 0x08000400 }, + { 0x043fc, 0x00150000 }, + { 0x04400, 0x004f0031 }, + { 0x04404, 0x0092006f }, + { 0x04408, 0x00da00b5 }, + { 0x0440c, 0x024c0100 }, + { 0x04410, 0x0c200547 }, + { 0x04414, 0x000e0000 }, + { 0x04418, 0x00410025 }, + { 0x0441c, 0x00850061 }, + { 0x04420, 0x00d400ab }, + { 0x04424, 0x02a40100 }, + { 0x04428, 0x126106f7 }, + { 0x0442c, 0x04000400 }, + { 0x04430, 0x08000600 }, + { 0x04434, 0x00000800 }, + { 0x04438, 0x04000400 }, + { 0x0443c, 0x04000400 }, + { 0x04440, 0x00000400 }, + { 0x04444, 0x00090900 }, + { 0x04484, 0x015e009d }, + { 0x04488, 0x03f90260 }, +}; + +static const struct t23_profile_reg defog_720_regs_0[] = { + { 0x05b80, 0x00000000 }, + { 0x05b04, 0x00000000 }, + { 0x05b0c, 0xffffffff }, + { 0x05b00, 0x00000000 }, + { 0x05800, 0x00480000 }, + { 0x05804, 0x00d80090 }, + { 0x05808, 0x01680120 }, + { 0x0580c, 0x01f801b0 }, + { 0x05810, 0x02880240 }, + { 0x05814, 0x000002d0 }, + { 0x05820, 0x00480000 }, + { 0x05824, 0x00d70090 }, + { 0x05828, 0x0165011e }, + { 0x0582c, 0x01f301ac }, + { 0x05830, 0x0281023a }, + { 0x05834, 0x030f02c8 }, + { 0x05838, 0x039d0356 }, + { 0x0583c, 0x042b03e4 }, + { 0x05840, 0x04b90472 }, + { 0x05844, 0x00000500 }, +}; + +static const u32 defog_720_lut_1[] = { + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, +}; + +static const struct t23_profile_reg defog_720_regs_2[] = { + { 0x05850, 0x000e0007 }, + { 0x05854, 0x001e0016 }, + { 0x05858, 0x00300027 }, + { 0x0585c, 0x00430039 }, + { 0x05860, 0x0059004e }, + { 0x05864, 0x00720065 }, + { 0x05868, 0x00900080 }, + { 0x0586c, 0x00b400a1 }, + { 0x05870, 0x00e100c9 }, + { 0x05874, 0x012000fe }, + { 0x05878, 0x0183014a }, + { 0x0587c, 0x029501da }, + { 0x05880, 0x001e000f }, + { 0x05884, 0x003f002e }, + { 0x05888, 0x00620050 }, + { 0x0588c, 0x00880074 }, + { 0x05890, 0x00b1009c }, + { 0x05894, 0x00de00c7 }, + { 0x05898, 0x011000f6 }, + { 0x0589c, 0x0148012b }, + { 0x058a0, 0x01880167 }, + { 0x058a4, 0x01d201ac }, + { 0x058a8, 0x022b01fc }, + { 0x058ac, 0x0298025e }, + { 0x058b0, 0x032802da }, + { 0x058b4, 0x03ef0385 }, + { 0x058b8, 0x05810495 }, + { 0x058bc, 0x0000077e }, + { 0x058c0, 0x00000000 }, + { 0x058c4, 0x01010000 }, + { 0x058c8, 0x03020202 }, + { 0x058cc, 0x04040303 }, + { 0x058d0, 0x07060505 }, + { 0x058d4, 0x0b0a0908 }, + { 0x058d8, 0x0d0d0c0b }, + { 0x058dc, 0x0d0d0d0d }, + { 0x058e0, 0x00000000 }, + { 0x058e4, 0x01010000 }, + { 0x058e8, 0x03020202 }, + { 0x058ec, 0x04040303 }, + { 0x058f0, 0x07060505 }, + { 0x058f4, 0x0a090807 }, + { 0x058f8, 0x0d0d0c0b }, + { 0x058fc, 0x0d0d0d0d }, + { 0x05900, 0x00000000 }, + { 0x05904, 0x01010000 }, + { 0x05908, 0x02020201 }, + { 0x0590c, 0x04030303 }, + { 0x05910, 0x07060504 }, + { 0x05914, 0x0a0a0908 }, + { 0x05918, 0x0a0a0a0a }, + { 0x0591c, 0x0a0a0a0a }, + { 0x05920, 0x00000000 }, + { 0x05924, 0x01010000 }, + { 0x05928, 0x02020201 }, + { 0x0592c, 0x04030303 }, + { 0x05930, 0x07060505 }, + { 0x05934, 0x0b0b0908 }, + { 0x05938, 0x0b0b0b0b }, + { 0x0593c, 0x0b0b0b0b }, + { 0x05940, 0x00000000 }, + { 0x05944, 0x00000000 }, + { 0x05948, 0x01000000 }, + { 0x0594c, 0x02010101 }, + { 0x05950, 0x02020202 }, + { 0x05954, 0x03030303 }, + { 0x05958, 0x03030303 }, + { 0x0595c, 0x03030303 }, + { 0x05a00, 0x0000f241 }, + { 0x05a04, 0x1010100d }, + { 0x05a08, 0x10100810 }, + { 0x05a10, 0x28201808 }, + { 0x05a14, 0x3c3c3c3c }, + { 0x05a18, 0x003c0038 }, + { 0x05a1c, 0x04030304 }, + { 0x05a20, 0x03030303 }, + { 0x05a24, 0x00181008 }, + { 0x05a28, 0x00201c0a }, + { 0x05a30, 0x07060505 }, + { 0x05a34, 0x00804020 }, + { 0x05a38, 0x00201a08 }, + { 0x05a40, 0x00000203 }, + { 0x05a44, 0x00100c04 }, + { 0x05a48, 0x0000041f }, + { 0x05a50, 0x00050506 }, + { 0x05a54, 0x00806040 }, + { 0x05a58, 0x00201c05 }, + { 0x05a60, 0x00060505 }, + { 0x05a64, 0x00804020 }, + { 0x05a68, 0x20201810 }, + { 0x05a70, 0x00060505 }, + { 0x05a74, 0x00804020 }, + { 0x05a78, 0x20201812 }, + { 0x05b10, 0x00ab0054 }, +}; + +static const struct t23_profile_reg adr_1080_regs_0[] = { + { 0x04490, 0x00000000 }, + { 0x04000, 0x010e0140 }, + { 0x04010, 0x010e0000 }, + { 0x04014, 0x032a021c }, + { 0x04018, 0x00000438 }, + { 0x0401c, 0x01400000 }, + { 0x04020, 0x03c00280 }, + { 0x04024, 0x06400500 }, + { 0x04028, 0x00000780 }, + { 0x04454, 0x03f60042 }, + { 0x04458, 0x07300050 }, + { 0x04390, 0x00400020 }, + { 0x04394, 0x00c00080 }, + { 0x04398, 0x01800100 }, + { 0x0439c, 0x03000200 }, + { 0x043a0, 0x06000400 }, + { 0x043a4, 0x00000800 }, + { 0x04354, 0x04000400 }, + { 0x04358, 0x04000400 }, + { 0x0435c, 0x04000400 }, + { 0x04360, 0x04000400 }, + { 0x04364, 0x00000400 }, + { 0x04084, 0x00400020 }, + { 0x04088, 0x00c00080 }, + { 0x0408c, 0x01800100 }, + { 0x04090, 0x03000200 }, + { 0x04094, 0x06000400 }, + { 0x04098, 0x00200800 }, + { 0x0409c, 0x00800040 }, + { 0x040a0, 0x010000c0 }, + { 0x040a4, 0x02000180 }, + { 0x040a8, 0x04000300 }, + { 0x040ac, 0x08000600 }, + { 0x040b0, 0x00400020 }, + { 0x040b4, 0x00c00080 }, + { 0x040b8, 0x01800100 }, + { 0x040bc, 0x03000200 }, + { 0x040c0, 0x06000400 }, + { 0x040c4, 0x00200800 }, + { 0x040c8, 0x00800040 }, + { 0x040cc, 0x010000c0 }, + { 0x040d0, 0x02000180 }, + { 0x040d4, 0x04000300 }, + { 0x040d8, 0x08000600 }, + { 0x040dc, 0x00400020 }, + { 0x040e0, 0x00c00080 }, + { 0x040e4, 0x01800100 }, + { 0x040e8, 0x03000200 }, + { 0x040ec, 0x06000400 }, + { 0x040f0, 0x00200800 }, + { 0x040f4, 0x00800040 }, + { 0x040f8, 0x010000c0 }, + { 0x040fc, 0x02000180 }, + { 0x04100, 0x04000300 }, + { 0x04104, 0x08000600 }, + { 0x04108, 0x00400020 }, + { 0x0410c, 0x00c00080 }, + { 0x04110, 0x01800100 }, + { 0x04114, 0x03000200 }, + { 0x04118, 0x06000400 }, + { 0x0411c, 0x00200800 }, + { 0x04120, 0x00800040 }, + { 0x04124, 0x010000c0 }, + { 0x04128, 0x02000180 }, + { 0x0412c, 0x04000300 }, + { 0x04130, 0x08000600 }, + { 0x04134, 0x00400020 }, + { 0x04138, 0x00c00080 }, + { 0x0413c, 0x01800100 }, + { 0x04140, 0x03000200 }, + { 0x04144, 0x06000400 }, + { 0x04148, 0x00200800 }, + { 0x0414c, 0x00800040 }, + { 0x04150, 0x010000c0 }, + { 0x04154, 0x02000180 }, + { 0x04158, 0x04000300 }, + { 0x0415c, 0x08000600 }, + { 0x04160, 0x00400020 }, + { 0x04164, 0x00c00080 }, + { 0x04168, 0x01800100 }, + { 0x0416c, 0x03000200 }, + { 0x04170, 0x06000400 }, + { 0x04174, 0x00200800 }, + { 0x04178, 0x00800040 }, + { 0x0417c, 0x010000c0 }, + { 0x04180, 0x02000180 }, + { 0x04184, 0x04000300 }, + { 0x04188, 0x08000600 }, + { 0x0418c, 0x00400020 }, + { 0x04190, 0x00c00080 }, + { 0x04194, 0x01800100 }, + { 0x04198, 0x03000200 }, + { 0x0419c, 0x06000400 }, + { 0x041a0, 0x00200800 }, + { 0x041a4, 0x00800040 }, + { 0x041a8, 0x010000c0 }, + { 0x041ac, 0x02000180 }, + { 0x041b0, 0x04000300 }, + { 0x041b4, 0x08000600 }, + { 0x041b8, 0x00400020 }, + { 0x041bc, 0x00c00080 }, + { 0x041c0, 0x01800100 }, + { 0x041c4, 0x03000200 }, + { 0x041c8, 0x06000400 }, + { 0x041cc, 0x00200800 }, + { 0x041d0, 0x00800040 }, + { 0x041d4, 0x010000c0 }, + { 0x041d8, 0x02000180 }, + { 0x041dc, 0x04000300 }, + { 0x041e0, 0x08000600 }, + { 0x041e4, 0x00400020 }, + { 0x041e8, 0x00c00080 }, + { 0x041ec, 0x01800100 }, + { 0x041f0, 0x03000200 }, + { 0x041f4, 0x06000400 }, + { 0x041f8, 0x00200800 }, + { 0x041fc, 0x00800040 }, + { 0x04200, 0x010000c0 }, + { 0x04204, 0x02000180 }, + { 0x04208, 0x04000300 }, + { 0x0420c, 0x08000600 }, + { 0x04210, 0x00400020 }, + { 0x04214, 0x00c00080 }, + { 0x04218, 0x01800100 }, + { 0x0421c, 0x03000200 }, + { 0x04220, 0x06000400 }, + { 0x04224, 0x00200800 }, + { 0x04228, 0x00800040 }, + { 0x0422c, 0x010000c0 }, + { 0x04230, 0x02000180 }, + { 0x04234, 0x04000300 }, + { 0x04238, 0x08000600 }, + { 0x0423c, 0x00400020 }, + { 0x04240, 0x00c00080 }, + { 0x04244, 0x01800100 }, + { 0x04248, 0x03000200 }, + { 0x0424c, 0x06000400 }, + { 0x04250, 0x00200800 }, + { 0x04254, 0x00800040 }, + { 0x04258, 0x010000c0 }, + { 0x0425c, 0x02000180 }, + { 0x04260, 0x04000300 }, + { 0x04264, 0x08000600 }, + { 0x04268, 0x00400020 }, + { 0x0426c, 0x00c00080 }, + { 0x04270, 0x01800100 }, + { 0x04274, 0x03000200 }, + { 0x04278, 0x06000400 }, + { 0x0427c, 0x00200800 }, + { 0x04280, 0x00800040 }, + { 0x04284, 0x010000c0 }, + { 0x04288, 0x02000180 }, + { 0x0428c, 0x04000300 }, + { 0x04290, 0x08000600 }, + { 0x04004, 0x014000a0 }, + { 0x04448, 0x03330266 }, + { 0x0444c, 0x04cd0400 }, + { 0x04450, 0x0000059a }, + { 0x0402c, 0x0f3c14c3 }, + { 0x04030, 0x0af90caa }, + { 0x04034, 0x08b309b5 }, + { 0x04038, 0x072307dc }, + { 0x0403c, 0x05f30682 }, + { 0x04040, 0x04fd0572 }, + { 0x04044, 0x042f0492 }, + { 0x04048, 0x037d03d3 }, + { 0x0404c, 0x02e0032c }, + { 0x04050, 0x02550299 }, + { 0x04054, 0x01d70215 }, + { 0x04058, 0x0165019d }, + { 0x0405c, 0x00fc012f }, + { 0x04060, 0x009a00ca }, + { 0x04064, 0x0040006c }, + { 0x04068, 0x00000015 }, + { 0x04340, 0x00800000 }, + { 0x04344, 0x02000100 }, + { 0x04348, 0x06000400 }, + { 0x0434c, 0x0e000a00 }, + { 0x04350, 0x00000f00 }, + { 0x04368, 0x00070007 }, + { 0x0436c, 0x00090008 }, + { 0x04370, 0x000a0009 }, + { 0x04374, 0x0008000a }, + { 0x0406c, 0x00400020 }, + { 0x04070, 0x00c00080 }, + { 0x04074, 0x01800100 }, + { 0x04078, 0x03000200 }, + { 0x0407c, 0x06000400 }, + { 0x04080, 0x00000800 }, + { 0x04334, 0x06060505 }, + { 0x04338, 0x08070706 }, + { 0x0433c, 0x0b090908 }, + { 0x04294, 0x00000000 }, + { 0x04298, 0x00000000 }, + { 0x0429c, 0x01010000 }, + { 0x042a0, 0x01010101 }, + { 0x042a4, 0x02020202 }, + { 0x042a8, 0x04030303 }, + { 0x042ac, 0x04040404 }, + { 0x042b0, 0x04040404 }, + { 0x042b4, 0x00000000 }, + { 0x042b8, 0x00000000 }, + { 0x042bc, 0x00000000 }, + { 0x042c0, 0x01010000 }, + { 0x042c4, 0x01010101 }, + { 0x042c8, 0x02020201 }, + { 0x042cc, 0x02020202 }, + { 0x042d0, 0x02020202 }, + { 0x042d4, 0x00000000 }, + { 0x042d8, 0x00000000 }, + { 0x042dc, 0x01010000 }, + { 0x042e0, 0x01010101 }, + { 0x042e4, 0x02010101 }, + { 0x042e8, 0x02020202 }, + { 0x042ec, 0x02020202 }, + { 0x042f0, 0x02020202 }, + { 0x04314, 0x00000000 }, + { 0x04318, 0x00000000 }, + { 0x0431c, 0x00000000 }, + { 0x04320, 0x00000000 }, + { 0x04324, 0x00000000 }, + { 0x04328, 0x00000000 }, + { 0x0432c, 0x00000000 }, + { 0x04330, 0x00000000 }, + { 0x042f4, 0x00000000 }, + { 0x042f8, 0x01000000 }, + { 0x042fc, 0x01010101 }, + { 0x04300, 0x02020201 }, + { 0x04304, 0x03030302 }, + { 0x04308, 0x03030303 }, + { 0x0430c, 0x03030303 }, + { 0x04310, 0x03030303 }, + { 0x04378, 0x00400020 }, + { 0x0437c, 0x00c00080 }, + { 0x04380, 0x01800100 }, + { 0x04384, 0x03000200 }, + { 0x04388, 0x06000400 }, + { 0x0438c, 0x00000800 }, + { 0x043a8, 0x06060505 }, + { 0x043ac, 0x08070706 }, + { 0x043b0, 0x0b090908 }, + { 0x043b4, 0x004a0000 }, + { 0x043b8, 0x008e006f }, + { 0x043bc, 0x00c100a9 }, + { 0x043c0, 0x00ec00d7 }, + { 0x043c4, 0x01840100 }, + { 0x043c8, 0x037b024c }, + { 0x043cc, 0x00310000 }, + { 0x043d0, 0x00750054 }, + { 0x043d4, 0x00b00093 }, + { 0x043d8, 0x00e600cb }, + { 0x043dc, 0x01be0100 }, + { 0x043e0, 0x05470308 }, + { 0x043e4, 0x00200000 }, + { 0x043e8, 0x00600040 }, + { 0x043ec, 0x00a00080 }, + { 0x043f0, 0x00e000c0 }, + { 0x043f4, 0x02000100 }, + { 0x043f8, 0x08000400 }, + { 0x043fc, 0x00150000 }, + { 0x04400, 0x004f0031 }, + { 0x04404, 0x0092006f }, + { 0x04408, 0x00da00b5 }, + { 0x0440c, 0x024c0100 }, + { 0x04410, 0x0c200547 }, + { 0x04414, 0x000e0000 }, + { 0x04418, 0x00410025 }, + { 0x0441c, 0x00850061 }, + { 0x04420, 0x00d400ab }, + { 0x04424, 0x02a40100 }, + { 0x04428, 0x126106f7 }, + { 0x0442c, 0x04000400 }, + { 0x04430, 0x08000600 }, + { 0x04434, 0x00000800 }, + { 0x04438, 0x04000400 }, + { 0x0443c, 0x04000400 }, + { 0x04440, 0x00000400 }, + { 0x04444, 0x00090900 }, + { 0x04484, 0x015e009d }, + { 0x04488, 0x03f90260 }, +}; + +static const struct t23_profile_reg defog_1080_regs_0[] = { + { 0x05b80, 0x00000000 }, + { 0x05b04, 0x00000000 }, + { 0x05b0c, 0xffffffff }, + { 0x05b00, 0x00000000 }, + { 0x05800, 0x006c0000 }, + { 0x05804, 0x014400d8 }, + { 0x05808, 0x021c01b0 }, + { 0x0580c, 0x02f40288 }, + { 0x05810, 0x03cc0360 }, + { 0x05814, 0x00000438 }, + { 0x05820, 0x006b0000 }, + { 0x05824, 0x014100d6 }, + { 0x05828, 0x021701ac }, + { 0x0582c, 0x02ed0282 }, + { 0x05830, 0x03c30358 }, + { 0x05834, 0x0499042e }, + { 0x05838, 0x056e0504 }, + { 0x0583c, 0x064205d8 }, + { 0x05840, 0x071606ac }, + { 0x05844, 0x00000780 }, +}; + +static const u32 defog_1080_lut_1[] = { + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, + 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, 0xc8c8c8e6, +}; + +static const struct t23_profile_reg defog_1080_regs_2[] = { + { 0x05850, 0x00210010 }, + { 0x05854, 0x00440032 }, + { 0x05858, 0x006b0057 }, + { 0x0585c, 0x00970081 }, + { 0x05860, 0x00c800af }, + { 0x05864, 0x010100e4 }, + { 0x05868, 0x01440121 }, + { 0x0586c, 0x0194016a }, + { 0x05870, 0x01fb01c4 }, + { 0x05874, 0x0287023b }, + { 0x05878, 0x036802e8 }, + { 0x0587c, 0x05cf042b }, + { 0x05880, 0x00440022 }, + { 0x05884, 0x008e0068 }, + { 0x05888, 0x00dc00b4 }, + { 0x0588c, 0x01310106 }, + { 0x05890, 0x018e015f }, + { 0x05894, 0x01f401c0 }, + { 0x05898, 0x0264022a }, + { 0x0589c, 0x02e302a2 }, + { 0x058a0, 0x03730328 }, + { 0x058a4, 0x041a03c3 }, + { 0x058a8, 0x04e10478 }, + { 0x058ac, 0x05d70555 }, + { 0x058b0, 0x071a066c }, + { 0x058b4, 0x08da07eb }, + { 0x058b8, 0x0c630a4f }, + { 0x058bc, 0x000010dc }, + { 0x058c0, 0x00000000 }, + { 0x058c4, 0x01010000 }, + { 0x058c8, 0x03020202 }, + { 0x058cc, 0x04040303 }, + { 0x058d0, 0x07060505 }, + { 0x058d4, 0x0b0a0908 }, + { 0x058d8, 0x0d0d0c0b }, + { 0x058dc, 0x0d0d0d0d }, + { 0x058e0, 0x00000000 }, + { 0x058e4, 0x01010000 }, + { 0x058e8, 0x03020202 }, + { 0x058ec, 0x04040303 }, + { 0x058f0, 0x07060505 }, + { 0x058f4, 0x0a090807 }, + { 0x058f8, 0x0d0d0c0b }, + { 0x058fc, 0x0d0d0d0d }, + { 0x05900, 0x00000000 }, + { 0x05904, 0x01010000 }, + { 0x05908, 0x02020201 }, + { 0x0590c, 0x04030303 }, + { 0x05910, 0x07060504 }, + { 0x05914, 0x0a0a0908 }, + { 0x05918, 0x0a0a0a0a }, + { 0x0591c, 0x0a0a0a0a }, + { 0x05920, 0x00000000 }, + { 0x05924, 0x01010000 }, + { 0x05928, 0x02020201 }, + { 0x0592c, 0x04030303 }, + { 0x05930, 0x07060505 }, + { 0x05934, 0x0b0b0908 }, + { 0x05938, 0x0b0b0b0b }, + { 0x0593c, 0x0b0b0b0b }, + { 0x05940, 0x00000000 }, + { 0x05944, 0x00000000 }, + { 0x05948, 0x01000000 }, + { 0x0594c, 0x02010101 }, + { 0x05950, 0x02020202 }, + { 0x05954, 0x03030303 }, + { 0x05958, 0x03030303 }, + { 0x0595c, 0x03030303 }, + { 0x05a00, 0x0000f241 }, + { 0x05a04, 0x1010100d }, + { 0x05a08, 0x10100810 }, + { 0x05a10, 0x28201808 }, + { 0x05a14, 0x3c3c3c3c }, + { 0x05a18, 0x003c0038 }, + { 0x05a1c, 0x04030304 }, + { 0x05a20, 0x03030303 }, + { 0x05a24, 0x00181008 }, + { 0x05a28, 0x00201c0a }, + { 0x05a30, 0x07060505 }, + { 0x05a34, 0x00804020 }, + { 0x05a38, 0x00201a08 }, + { 0x05a40, 0x00000203 }, + { 0x05a44, 0x00100c04 }, + { 0x05a48, 0x0000041f }, + { 0x05a50, 0x00050506 }, + { 0x05a54, 0x00806040 }, + { 0x05a58, 0x00201c05 }, + { 0x05a60, 0x00060505 }, + { 0x05a64, 0x00804020 }, + { 0x05a68, 0x20201810 }, + { 0x05a70, 0x00060505 }, + { 0x05a74, 0x00804020 }, + { 0x05a78, 0x20201812 }, + { 0x05b10, 0x00ab0054 }, +}; + +static const struct t23_profile_reg csccr_config[] = { + { 0x0c040, 0x00000400 }, + { 0x0c044, 0x0000ff00 }, + { 0x0c050, 0x00000400 }, + { 0x0c054, 0x007f0180 }, + { 0x0c010, 0xffffffff }, +}; + +static void write_regs(void __iomem *base, + const struct t23_profile_reg *regs, unsigned int count) +{ + unsigned int i; + + for (i = 0; i < count; i++) + writel(regs[i].value, base + regs[i].offset); +} + +static void write_lut(void __iomem *base, u32 offset, + const u32 *values, unsigned int count) +{ + unsigned int i; + + for (i = 0; i < count; i++) + writel(values[i], base + offset + i * 4); +} + +int tx_isp_profile_init(void __iomem *base, size_t size, + unsigned int width, unsigned int height) +{ + unsigned int i; + + if (!base || size < 0x80180) + return -EINVAL; + if (!((width == 1280 && height == 720) || + (width == 1920 && height == 1080))) + return -EOPNOTSUPP; + + /* DEIR has three 32-word tables in linear mode. */ + for (i = 0; i < 32; i++) { + writel(0, base + ISP_DGAIN_DEIR_LUT0 + i * 4); + writel(0, base + ISP_DGAIN_DEIR_LUT1 + i * 4); + writel(0, base + ISP_DGAIN_DEIR_LUT2 + i * 4); + } + + write_regs(base, lsc_config, ARRAY_SIZE(lsc_config)); + /* Each LSC cell occupies three words in a four-word slot. */ + for (i = 0; i < ARRAY_SIZE(lsc_lut); i++) + writel(lsc_lut[i], base + ISP_LSC_LUT_BASE + (i / 3) * 16 + (i % 3) * 4); + writel(ISP_LSC_LUT_TRIGGER_COMMIT, base + ISP_LSC_LUT_TRIGGER); + for (i = 0; i < ARRAY_SIZE(gamma_lut); i++) { + writel(gamma_lut[i], base + ISP_GAMMA_LUT_R + i * 4); + writel(gamma_lut[i], base + ISP_GAMMA_LUT_G + i * 4); + writel(gamma_lut[i], base + ISP_GAMMA_LUT_B + i * 4); + } + + write_regs(base, ysp_regs_0, ARRAY_SIZE(ysp_regs_0)); + write_regs(base, ydns_regs_0, ARRAY_SIZE(ydns_regs_0)); + write_regs(base, sdns_regs_0, ARRAY_SIZE(sdns_regs_0)); + write_regs(base, hldc_regs_0, ARRAY_SIZE(hldc_regs_0)); + write_regs(base, clm_regs_0, ARRAY_SIZE(clm_regs_0)); + write_lut(base, 0x60000, clm_lut_1, ARRAY_SIZE(clm_lut_1)); + write_lut(base, 0x68000, clm_lut_2, ARRAY_SIZE(clm_lut_2)); + write_lut(base, 0x70000, clm_lut_3, ARRAY_SIZE(clm_lut_3)); + write_lut(base, 0x78000, clm_lut_4, ARRAY_SIZE(clm_lut_4)); + write_regs(base, dpc_regs_0, ARRAY_SIZE(dpc_regs_0)); + write_regs(base, bcsh_regs_0, ARRAY_SIZE(bcsh_regs_0)); + if (height == 720) { + write_regs(base, adr_720_regs_0, ARRAY_SIZE(adr_720_regs_0)); + write_regs(base, defog_720_regs_0, ARRAY_SIZE(defog_720_regs_0)); + write_lut(base, 0x58000, defog_720_lut_1, ARRAY_SIZE(defog_720_lut_1)); + write_regs(base, defog_720_regs_2, ARRAY_SIZE(defog_720_regs_2)); + } else { + write_regs(base, adr_1080_regs_0, ARRAY_SIZE(adr_1080_regs_0)); + write_regs(base, defog_1080_regs_0, ARRAY_SIZE(defog_1080_regs_0)); + write_lut(base, 0x58000, defog_1080_lut_1, ARRAY_SIZE(defog_1080_lut_1)); + write_regs(base, defog_1080_regs_2, ARRAY_SIZE(defog_1080_regs_2)); + } + write_regs(base, csccr_config, ARRAY_SIZE(csccr_config)); + return 0; +} + +void tx_isp_profile_restore_blocks(void __iomem *base, size_t size, u32 mask) +{ + unsigned int i; + + if (!base) + return; + + if ((mask & TX_ISP_PARAM_LSC) && size >= 0x2c000) { + write_regs(base, lsc_config, ARRAY_SIZE(lsc_config)); + for (i = 0; i < ARRAY_SIZE(lsc_lut); i++) + writel(lsc_lut[i], + base + ISP_LSC_LUT_BASE + (i / 3) * 16 + (i % 3) * 4); + writel(ISP_LSC_LUT_TRIGGER_COMMIT, base + ISP_LSC_LUT_TRIGGER); + } + if ((mask & TX_ISP_PARAM_GAMMA) && size >= 0x50200) { + for (i = 0; i < ARRAY_SIZE(gamma_lut); i++) { + writel(gamma_lut[i], base + ISP_GAMMA_LUT_R + i * 4); + writel(gamma_lut[i], base + ISP_GAMMA_LUT_G + i * 4); + writel(gamma_lut[i], base + ISP_GAMMA_LUT_B + i * 4); + } + } + if (mask & TX_ISP_PARAM_YSP) { + write_regs(base, ysp_regs_0, ARRAY_SIZE(ysp_regs_0)); + writel(ISP_YSP_REFRESH_COMMIT, base + ISP_YSP_REFRESH); + } + if (mask & TX_ISP_PARAM_YDNS) + write_regs(base, ydns_regs_0, ARRAY_SIZE(ydns_regs_0)); + if (mask & TX_ISP_PARAM_SDNS) { + write_regs(base, sdns_regs_0, ARRAY_SIZE(sdns_regs_0)); + writel(ISP_SDNS_REFRESH_COMMIT, base + ISP_SDNS_REFRESH); + } +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-profile.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-profile.h new file mode 100644 index 0000000000..9242e787a9 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-profile.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef TX_ISP_PROFILE_H +#define TX_ISP_PROFILE_H + +#include +#include + +int tx_isp_profile_init(void __iomem *base, size_t size, + unsigned int width, unsigned int height); +void tx_isp_profile_restore_blocks(void __iomem *base, size_t size, u32 mask); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-regs.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-regs.h new file mode 100644 index 0000000000..466c60022d --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-regs.h @@ -0,0 +1,699 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_REGS_H__ +#define __TX_ISP_REGS_H__ + +#include +#include + +/* ========================================================================= + * 1. ISP Top / Global Control (sd->base) + * ========================================================================= */ +#define ISP_TOP_REG_00 0x0000 /* undocumented */ +#define ISP_TOP_IMAGE_SIZE 0x0004 /* Input image resolution */ +#define ISP_TOP_IMAGE_WIDTH_MASK GENMASK(31, 16) +#define ISP_TOP_IMAGE_HEIGHT_MASK GENMASK(15, 0) +#define ISP_TOP_IMAGE_SIZE_VAL(w, h) (((w) << 16) | ((h) & 0xffff)) + +#define ISP_TOP_BAYER_ORDER 0x0008 /* Sensor Bayer pattern order */ +#define ISP_TOP_BAYER_RGGB 0 +#define ISP_TOP_BAYER_BGGR 1 +#define ISP_TOP_BAYER_GBRG 2 +#define ISP_TOP_BAYER_GRBG 3 + +#define ISP_TOP_CTRL 0x000c /* ISP global top control */ +#define ISP_TOP_CTRL_ENABLE BIT(0) +#define ISP_TOP_CTRL_DEFAULT 0xb4742069 /* undocumented */ + +#define ISP_TOP_REG_10 0x0010 /* undocumented */ +#define ISP_TOP_REG_10_DEFAULT 0x000000f3 /* undocumented */ + +#define ISP_TOP_REG_1C 0x001c /* undocumented */ +#define ISP_TOP_REG_1C_DEFAULT 0x00000000 /* undocumented */ + +#define ISP_TOP_RESET_CTRL 0x0020 /* Soft reset control */ +#define ISP_TOP_RESET_RELEASE 0x00000000 + +#define ISP_TOP_RESET_TRIG 0x0024 /* Soft reset trigger */ +#define ISP_TOP_RESET_TRIG_REQ BIT(0) + +#define ISP_TOP_RESET_STAT 0x0028 /* Soft reset status */ +#define ISP_TOP_RESET_STAT_DONE BIT(0) +#define ISP_TOP_RESET_STAT_CLEAR BIT(2) + +#define ISP_TOP_REG_2C 0x002c /* undocumented */ +#define ISP_TOP_REG_2C_DEFAULT 0x00000000 /* undocumented */ + +#define ISP_TOP_REG_30 0x0030 /* undocumented */ +#define ISP_TOP_REG_30_DEFAULT 0x000000f3 /* undocumented */ + +#define ISP_TOP_REG_33C 0x033c /* undocumented */ +#define ISP_TOP_REG_33C_DEFAULT 0x20230219 /* undocumented */ + +/* ========================================================================= + * 2. ISP Core Interrupt Controller (sd->base) + * ========================================================================= */ +#define ISP_INT_MASK 0x00b0 /* Core interrupt enable mask */ +#define ISP_INT_MASK_ALL 0xffffffff + +#define ISP_INT_STATUS 0x00b4 /* Core interrupt status */ +#define ISP_INT_MSCA_CH0_DONE BIT(0) +#define ISP_INT_MSCA_CH1_DONE BIT(1) +#define ISP_INT_MSCA_CH2_DONE BIT(2) +#define ISP_INT_ERR_MASK GENMASK(9, 3) +#define ISP_INT_ERR_BIT3 BIT(3) +#define ISP_INT_ERR_BIT4 BIT(4) +#define ISP_INT_ERR_BIT5 BIT(5) +#define ISP_INT_ERR_BIT6 BIT(6) +#define ISP_INT_ERR_BIT7 BIT(7) +#define ISP_INT_ERR_BIT8 BIT(8) +#define ISP_INT_ERR_BIT9 BIT(9) +#define ISP_INT_FRAME_EVENT BIT(12) +#define ISP_INT_IPC_FRAME_DONE BIT(13) +#define ISP_INT_ADR BIT(18) +#define ISP_INT_DEFOG BIT(20) +#define ISP_INT_AE BIT(26) +#define ISP_INT_HIST BIT(27) +#define ISP_INT_AWB BIT(30) +#define ISP_INT_AF BIT(31) + +#define ISP_CORE_STATS_IRQS (ISP_INT_AE | ISP_INT_HIST | ISP_INT_AWB) + +#define ISP_INT_CLEAR 0x00b8 /* Core interrupt clear (W1C) */ + +#define ISP_INT1_MASK 0x00c0 /* Interrupt bank 1 mask */ +#define ISP_INT1_BIT15 BIT(15) /* pipeline done interrupt */ + +#define ISP_INT1_CLEAR 0x00c4 /* Interrupt bank 1 clear (W1C) */ + +#define ISP_INT1_STATUS 0x00c8 /* Interrupt bank 1 status */ +#define ISP_INT1_STATUS_BIT15 BIT(15) /* pipeline done flag */ + +/* ========================================================================= + * 3. ISP IPC / Run Control (sd->base) + * ========================================================================= */ +#define ISP_IPC_TRIGGER 0x0800 /* Frame processing trigger */ +#define ISP_IPC_TRIGGER_START BIT(0) +#define ISP_IPC_TRIGGER_STOP 0x00000000 + +#define ISP_IPC_MODE 0x0804 /* Ingest mode and sensor port select */ +#define ISP_IPC_MODE_PORT1 BIT(6) +#define ISP_IPC_MODE_DEFAULT 0x0000001c /* undocumented */ + +#define ISP_IPC_INPUT_STAT 0x084c /* Input pipeline status */ + +/* ========================================================================= + * 4. ISP Digital Gain & Black Level Correction (GIB) (sd->base) + * ========================================================================= */ +#define ISP_DGAIN_GAIN01 0x1030 /* Channels 0 and 1 digital gain */ +#define ISP_DGAIN_GAIN23 0x1034 /* Channels 2 and 3 digital gain */ +#define ISP_DGAIN_LUT_PARAM0 0x1038 /* GIB interpolation config word 0 */ +#define ISP_DGAIN_LUT_PARAM1 0x103c /* GIB interpolation config word 1 */ +#define ISP_DGAIN_BLC0 0x1060 /* Black level correction channel 0 */ +#define ISP_DGAIN_BLC1 0x1064 /* Black level correction channel 1 */ +#define ISP_DGAIN_BLC2 0x1068 /* Black level correction channel 2 */ +#define ISP_DGAIN_BLC3 0x106c /* Black level correction channel 3 */ +#define ISP_DGAIN_TRIGGER 0x1070 /* Digital gain parameter commit */ + +#define ISP_DGAIN_GAIN_ONE 0x04000400 /* 1.0x gain for channel pair (Q10) */ +#define ISP_DGAIN_LUT_PARAM0_DEFAULT 0x0fff0fff /* undocumented */ +#define ISP_DGAIN_LUT_PARAM1_DEFAULT 0x00005430 /* undocumented */ +#define ISP_DGAIN_BLC_ONE 0x00000103 /* undocumented */ +#define ISP_DGAIN_BLC_PAIR_ONE 0x01030103 /* undocumented */ +#define ISP_DGAIN_TRIGGER_COMMIT 0x1 + +#define ISP_DGAIN_DEIR_LUT0 0x80000 /* GIB DEIR table 0 */ +#define ISP_DGAIN_DEIR_LUT1 0x80080 /* GIB DEIR table 1 */ +#define ISP_DGAIN_DEIR_LUT2 0x80100 /* GIB DEIR table 2 */ + +/* ========================================================================= + * 5. ISP White Balance Gain (sd->base) + * ========================================================================= */ +#define ISP_WB_TRIGGER 0x1800 /* White balance latch trigger strobe */ +#define ISP_WB_TRIGGER_COMMIT 0x1 +#define ISP_WB_GAIN_R 0x1804 /* Red channel gain */ +#define ISP_WB_GAIN_B 0x1808 /* Blue channel gain */ +#define ISP_WB_GAIN_GR 0x180c /* Green-red channel gain */ +#define ISP_WB_GAIN_GB 0x1810 /* Green-blue channel gain */ +#define ISP_WB_GAIN_TAG 0x04000000 /* undocumented gain tag */ + +/* ========================================================================= + * 6. ISP Lens Shading Correction (LSC) (sd->base) + * ========================================================================= */ +#define ISP_LSC_GRID_SIZE 0x3800 /* Mesh grid dimensions (height:width) */ +#define ISP_LSC_CONTROL 0x3804 /* LSC control */ +#define ISP_LSC_LUT_BASE 0x28000 /* Mesh gain correction lookup table */ +#define ISP_LSC_LUT_TRIGGER 0x2800c /* LSC LUT commit strobe */ +#define ISP_LSC_LUT_TRIGGER_COMMIT 0x0 + +/* ========================================================================= + * 7. ISP Adaptive Dynamic Range (ADR) (sd->base) + * ========================================================================= */ +#define ISP_ADR_GRID_STEP 0x4000 /* Grid block step (y_step:x_step) */ +#define ISP_ADR_GRID_Y0 0x4010 /* Vertical grid row boundary 0 */ +#define ISP_ADR_GRID_Y1 0x4014 /* Vertical grid row boundary 1 */ +#define ISP_ADR_GRID_HEIGHT 0x4018 /* Frame total height for grid */ +#define ISP_ADR_GRID_X0 0x401c /* Horizontal grid column boundary 0 */ +#define ISP_ADR_GRID_X1 0x4020 /* Horizontal grid column boundary 1 */ +#define ISP_ADR_GRID_X2 0x4024 /* Horizontal grid column boundary 2 */ +#define ISP_ADR_GRID_WIDTH 0x4028 /* Frame total width for grid */ +#define ISP_ADR_MARGIN_Y 0x4454 /* Vertical statistical margin */ +#define ISP_ADR_MARGIN_X 0x4458 /* Horizontal statistical margin */ +#define ISP_ADR_DMA_CTRL 0x4490 /* ADR statistics DMA control */ +#define ISP_ADR_DMA_ENABLE 0x3 +#define ISP_ADR_DMA_ADDR_BASE 0x4494 /* ADR statistics DMA buffer banks */ +#define ISP_ADR_DMA_ADDR(bank) (0x4494 + ((bank) * 4)) + +/* ========================================================================= + * 8. ISP Demosaic (DMSC) (sd->base) + * ========================================================================= */ +#define ISP_DMSC_REG_BASE 0x4800 /* DMSC register block start */ +#define ISP_DMSC_REG_04 0x4804 /* DMSC status / configuration register */ +#define ISP_DMSC_REG_08 0x4808 /* undocumented */ +#define ISP_DMSC_REG_08_DEFAULT 0x00c80078 /* undocumented */ +#define ISP_DMSC_REG_48 0x4848 /* undocumented */ +#define ISP_DMSC_REG_48_DEFAULT 0x00640078 /* undocumented */ +#define ISP_DMSC_REG_60 0x4860 /* undocumented */ +#define ISP_DMSC_REG_60_DEFAULT 0x005a005a /* undocumented */ +#define ISP_DMSC_TRIGGER 0x499c /* DMSC parameter refresh commit */ +#define ISP_DMSC_REFRESH_COMMIT 0x1 + +/* ========================================================================= + * 9. ISP Colour Correction Matrix (CCM) (sd->base) + * ========================================================================= */ +#define ISP_CCM_TRIGGER 0x5000 /* CCM coefficient latch strobe */ +#define ISP_CCM_TRIGGER_STROBE 0x1 +#define ISP_CCM_COEFF_BASE 0x5004 /* CCM packed coefficient register base */ +#define ISP_CCM_COEFF(n) (0x5004 + ((n) * 4)) + +/* ========================================================================= + * 10. ISP Defog / Dehaze (sd->base) + * ========================================================================= */ +#define ISP_DEFOG_BOUND_V_BASE 0x5800 /* Defog vertical boundaries base */ +#define ISP_DEFOG_BOUND_H_BASE 0x5820 /* Defog horizontal boundaries base */ +#define ISP_DEFOG_CONTROL 0x5b00 /* Defog module control */ +#define ISP_DEFOG_MIN_THRES 0x5b04 /* Defog minimum threshold */ +#define ISP_DEFOG_MAX_THRES 0x5b0c /* Defog maximum threshold */ +#define ISP_DEFOG_DMA_CTRL 0x5b80 /* Defog statistics DMA control */ +#define ISP_DEFOG_DMA_ENABLE 0x3 +#define ISP_DEFOG_DMA_ADDR_BASE 0x5b84 /* Defog statistics DMA buffer banks */ +#define ISP_DEFOG_DMA_ADDR(bank) (0x5b84 + ((bank) * 4)) + +/* ========================================================================= + * 11. ISP Colour Space Conversion (CSC) (sd->base) + * ========================================================================= */ +#define ISP_CSC_CONTROL 0x6000 /* CSC function enable and routing */ +#define ISP_CSC_CTRL_BT601 0x0000001f +#define ISP_CSC_OFFSET0 0x6004 /* CSC input luma offset */ +#define ISP_CSC_COEFF0 0x6010 /* CSC coefficient word 0 (Y coeffs) */ +#define ISP_CSC_COEFF1 0x6014 /* CSC coefficient word 1 (Cb coeffs) */ +#define ISP_CSC_COEFF2 0x6018 /* CSC coefficient word 2 (Cr coeffs) */ +#define ISP_CSC_OFFSET1 0x6020 /* CSC chroma offset */ +#define ISP_CSC_CLIP 0x6030 /* CSC output dynamic range clamping */ + +/* ========================================================================= + * 12. ISP Luma Sharpening / Peaking (YSP) (sd->base) + * ========================================================================= */ +#define ISP_YSP_REG_BASE 0x7000 /* YSP register block start */ +#define ISP_YSP_REFRESH 0x7090 /* YSP parameter refresh commit */ +#define ISP_YSP_REFRESH_COMMIT 0x1 + +/* ========================================================================= + * 13. ISP Temporal Noise Reduction (MDNS) (sd->base) + * ========================================================================= */ +#define ISP_MDNS_REG_TRIGGER 0x7804 /* MDNS hardware run/stop trigger */ +#define ISP_MDNS_TRIGGER_START 0x111 +#define ISP_MDNS_TRIGGER_STOP 0x110 +#define ISP_MDNS_DEBUG_CONTROL 0x7808 /* MDNS debug / bypass control */ +#define ISP_MDNS_FUNC_CONTROL 0x7810 /* MDNS functional enables */ +#define ISP_MDNS_FUNC_CTRL_BYPASS 0x101 +#define ISP_MDNS_FUNC_CTRL_ENABLE 0xd1111111 +#define ISP_MDNS_FUNC_CTRL_MASK 0xd1111111U +#define ISP_MDNS_FUNC_GROUP 0x7814 /* MDNS parameter group control */ +#define ISP_MDNS_FUNC_GROUP_DEFAULT 0x00f01100 +#define ISP_MDNS_FUNC_GROUP_MASK 0x00f1310fU +#define ISP_MDNS_FUNC_REFRESH 0x7818 /* MDNS parameter shadow latch trigger */ +#define ISP_MDNS_REFRESH_VAL 0x21 +#define ISP_MDNS_PLANE_ADDR_BASE 0x7820 /* MDNS history DMA buffer base */ +#define ISP_MDNS_DMA_CTRL0 0x7838 /* MDNS DMA allocation control 0 */ +#define ISP_MDNS_DMA_CTRL0_DEFAULT 0x0 +#define ISP_MDNS_DMA_CTRL1 0x783c /* MDNS DMA allocation control 1 */ +#define ISP_MDNS_DMA_CTRL1_DEFAULT 0x1 + +/* ========================================================================= + * 14. ISP Luma Noise Reduction (YDNS) (sd->base) + * ========================================================================= */ +#define ISP_YDNS_REG_BASE 0x7af0 /* YDNS register block start */ + +/* ========================================================================= + * 15. ISP Brightness, Contrast, Saturation, Hue (BCSH) (sd->base) + * ========================================================================= */ +#define ISP_BCSH_REG_00 0x8000 /* BCSH configuration register 0 */ +#define ISP_BCSH_REG_18 0x8018 /* BCSH configuration register 6 */ +#define ISP_BCSH_REG_38 0x8038 /* BCSH configuration register 14 */ +#define ISP_BCSH_REG_54 0x8054 /* BCSH configuration register 21 */ +#define ISP_BCSH_REG_64 0x8064 /* BCSH configuration register 25 */ +#define ISP_BCSH_REG_70 0x8070 /* BCSH configuration register 28 */ + +/* ========================================================================= + * 16. ISP Spatial Noise Reduction (SDNS) (sd->base) + * ========================================================================= */ +#define ISP_SDNS_REG_BASE 0x8800 /* SDNS register block start */ +#define ISP_SDNS_REFRESH 0x8b4c /* SDNS parameter refresh commit */ +#define ISP_SDNS_REFRESH_COMMIT 0x1 + +/* ========================================================================= + * 17. ISP Gamma Lookup Tables (sd->base) + * ========================================================================= */ +#define ISP_GAMMA_LUT_BASE(c) (0x40000 + ((c) * 0x8000)) +#define ISP_GAMMA_LUT_R 0x40000 /* Red channel gamma curve LUT */ +#define ISP_GAMMA_LUT_G 0x48000 /* Green channel gamma curve LUT */ +#define ISP_GAMMA_LUT_B 0x50000 /* Blue channel gamma curve LUT */ + +/* ========================================================================= + * 18. ISP Auto Exposure Statistics (AE) (sd->base) + * ========================================================================= */ +#define ISP_AE_REG_BASE 0xa000 /* AE register block start */ +#define ISP_AE_CONTROL 0xa004 /* AE statistics enable and mode */ +#define ISP_AE_CTRL_DEFAULT 0xf001f001 /* undocumented */ +#define ISP_AE_ZONE_W_BASE 0xa008 /* AE zone horizontal dimensions base */ +#define ISP_AE_ZONE_H_BASE 0xa018 /* AE zone vertical dimensions base */ +#define ISP_AE_LUM_TH_FREQ 0xa028 /* AE luminance threshold and sampling frequency */ +#define ISP_AE_LUM_TH_FREQ_DEFAULT 0xaa1e /* undocumented */ +#define ISP_AE_DMA_ADDR_BASE 0xa02c /* AE zone statistics DMA buffer banks */ +#define ISP_AE_DMA_ZONE_ADDR(bank) (0xa02c + ((bank) * 4)) +#define ISP_AE_DMA_HIST_ADDR_BASE 0xa03c /* AE histogram statistics DMA banks */ +#define ISP_AE_DMA_HIST_ADDR(bank) (0xa03c + ((bank) * 4)) +#define ISP_AE_DMA_CTRL 0xa04c /* AE statistics DMA control */ +#define ISP_AE_DMA_CTRL_DEFAULT 0x10010000 /* undocumented */ + +/* ========================================================================= + * 19. ISP Auto White Balance Statistics (AWB) (sd->base) + * ========================================================================= */ +#define ISP_AWB_REG_BASE 0xb000 /* AWB register block start */ +#define ISP_AWB_TRIGGER 0xb000 /* AWB statistics trigger strobe */ +#define ISP_AWB_TRIGGER_COMMIT 0x1 +#define ISP_AWB_CONTROL 0xb004 /* AWB statistics enable and mode */ +#define ISP_AWB_CTRL_DEFAULT 0xf001f001 /* undocumented */ +#define ISP_AWB_ZONE_W_BASE 0xb008 /* AWB zone horizontal dimensions base */ +#define ISP_AWB_ZONE_H_BASE 0xb018 /* AWB zone vertical dimensions base */ +#define ISP_AWB_COLOR_PARAM0 0xb028 /* AWB color parameter 0 */ +#define ISP_AWB_COLOR_PARAM0_DEFAULT 0x0122004b /* undocumented */ +#define ISP_AWB_COLOR_PARAM1 0xb02c /* AWB color parameter 1 */ +#define ISP_AWB_COLOR_PARAM1_DEFAULT 0x00dc0023 /* undocumented */ +#define ISP_AWB_COLOR_PARAM2 0xb030 /* AWB color parameter 2 */ +#define ISP_AWB_COLOR_PARAM2_DEFAULT 0x00c30100 /* undocumented */ +#define ISP_AWB_COLOR_PARAM3 0xb034 /* AWB color parameter 3 */ +#define ISP_AWB_COLOR_PARAM3_DEFAULT 0x017c0100 /* undocumented */ +#define ISP_AWB_LUM_TH_FREQ 0xb038 /* AWB luminance threshold and sampling */ +#define ISP_AWB_LUM_TH_FREQ_DEFAULT 0x0001f00a /* undocumented */ +#define ISP_AWB_DMA_ADDR_BASE 0xb03c /* AWB statistics DMA buffer banks */ +#define ISP_AWB_DMA_ADDR(bank) (0xb03c + ((bank) * 4)) +#define ISP_AWB_DMA_CTRL 0xb04c /* AWB statistics DMA control */ +#define ISP_AWB_DMA_CTRL_DEFAULT 0x0 /* undocumented */ + +/* ========================================================================= + * 20. ISP Auto Focus Statistics (AF) (sd->base) + * ========================================================================= */ +#define ISP_AF_TRIGGER 0xb800 /* AF statistics trigger strobe */ +#define ISP_AF_TRIGGER_COMMIT 0x1 +#define ISP_AF_CONTROL 0xb804 /* AF statistics configuration */ +#define ISP_AF_CTRL_DEFAULT 0x80018003 /* undocumented */ +#define ISP_AF_ZONE_W_BASE 0xb808 /* AF zone horizontal dimensions base */ +#define ISP_AF_ZONE_H_BASE 0xb818 /* AF zone vertical dimensions base */ +#define ISP_AF_THRES0 0xb828 /* AF metric threshold 0 */ +#define ISP_AF_THRES0_DEFAULT 0x0000c8f0 /* undocumented */ +#define ISP_AF_THRES1 0xb82c /* AF metric threshold 1 */ +#define ISP_AF_FILTER_BASE 0xb830 /* AF FIR/IIR filter coefficient base */ +#define ISP_AF_DMA_ADDR_BASE 0xb8a8 /* AF statistics DMA buffer banks */ +#define ISP_AF_DMA_ADDR(bank) (0xb8a8 + ((bank) * 4)) +#define ISP_AF_DMA_CTRL 0xb8b8 /* AF statistics DMA control */ +#define ISP_AF_DMA_CTRL_DEFAULT 0x000100dc /* undocumented */ +#define ISP_AF_STATUS 0xb8bc /* AF statistics interrupt status */ + +/* ========================================================================= + * 21. MSCA Scaler and Output (sd->base) + * ========================================================================= */ +#define MSCA_SHD_TRIGGER 0xd010 /* Commit shadowed MSCA registers */ +#define MSCA_CH_EN_MASK 0xd040 /* Channel enable bitmask */ +#define MSCA_CH_EN(ch) BIT(ch) +#define MSCA_MASK_EN 0xd044 /* Mask enable register */ +#define MSCA_MASK_EN_CH(ch) BIT(ch) +#define MSCA_OSD_EN 0xd048 /* OSD block enable register */ +#define MSCA_OSD_EN_BLOCK(n) BIT(n) +#define MSCA_SCALE_CTL_MODE 0xd04c /* Scaler tap filter mode */ +#define MSCA_SCALE_CTL_MODE_V(ch) BIT(ch) +#define MSCA_SCALE_CTL_MODE_H(ch) BIT(4 + (ch)) +#define MSCA_REG_050 0xd050 /* Per-channel active mask, UV order, and mirror/flip */ +#define MSCA_REG_050_DEFAULT GENMASK(2, 0) /* Per-channel active/clock enable (Ch0-Ch2) */ +#define MSCA_REG_050_UV_TYPE(ch) BIT(12 + (ch)) +#define MSCA_REG_050_Y_MIRROR(ch) BIT(16 + (ch)) +#define MSCA_REG_050_Y_FLIP(ch) BIT(19 + (ch)) +#define MSCA_REG_050_UV_MIRROR(ch) BIT(24 + (ch)) +#define MSCA_REG_050_UV_FLIP(ch) BIT(27 + (ch)) +#define MSCA_REG_050_MIRROR(ch) (MSCA_REG_050_Y_MIRROR(ch) | MSCA_REG_050_UV_MIRROR(ch)) +#define MSCA_REG_050_FLIP(ch) (MSCA_REG_050_Y_FLIP(ch) | MSCA_REG_050_UV_FLIP(ch)) +#define MSCA_REG_060 0xd060 /* IVDC inline link enable (shadowed; latches on frame commit) */ +#define MSCA_IVDC_SYNC MSCA_REG_060 +#define MSCA_FCROP_POS 0xd090 /* Front crop origin */ +#define MSCA_FCROP_SIZE 0xd094 /* Front crop dimensions */ + +#define MSCA_GEOM_HI GENMASK(28, 16) +#define MSCA_GEOM_LO GENMASK(11, 0) +#define MSCA_GEOM(hi, lo) (FIELD_PREP(MSCA_GEOM_HI, hi) | FIELD_PREP(MSCA_GEOM_LO, lo)) + +#define MSCA_CH_BASE(ch) (0xd100 + (ch) * 0x100) +#define MSCA_CH_OFF_FCROP_POS 0x00 +#define MSCA_CH_OFF_SCALE_SIZE 0x04 +#define MSCA_CH_OFF_CROP_POS 0x08 +#define MSCA_CH_OFF_CROP_SIZE 0x0c +#define MSCA_CH_OFF_VSTEP 0x10 +#define MSCA_CH_OFF_HSTEP 0x14 +#define MSCA_CH_OFF_OUT 0x28 +#define MSCA_CH_OFF_Y_FIFO 0x30 +#define MSCA_CH_OFF_REG_34 0x34 +#define MSCA_CH_OFF_Y_DONE 0x38 +#define MSCA_CH_OFF_Y_DONE_EMPTY 0x3c +#define MSCA_CH_OFF_Y_STRIDE 0x40 +#define MSCA_CH_OFF_Y_RESET 0x44 +#define MSCA_CH_OFF_REG_4C 0x4c +#define MSCA_CH_OFF_UV_FIFO 0x50 +#define MSCA_CH_OFF_REG_54 0x54 +#define MSCA_CH_OFF_UV_DONE 0x58 +#define MSCA_CH_OFF_UV_DONE_EMPTY 0x5c +#define MSCA_CH_OFF_UV_STRIDE 0x60 +#define MSCA_CH_OFF_UV_RESET 0x64 +#define MSCA_CH_OFF_REG_6C 0x6c +#define MSCA_CH_OFF_SCALE_PARA0 0x70 +#define MSCA_CH_OFF_SCALE_PARA1 0x74 + +#define MSCA_CH_FCROP_POS(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_FCROP_POS) +#define MSCA_CH_SCALE_SIZE(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_SCALE_SIZE) +#define MSCA_CH_CROP_POS(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_CROP_POS) +#define MSCA_CH_CROP_SIZE(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_CROP_SIZE) +#define MSCA_CH_VSTEP(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_VSTEP) +#define MSCA_CH_HSTEP(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_HSTEP) +#define MSCA_STEP GENMASK(18, 0) +#define MSCA_CH_OUT(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_OUT) +#define MSCA_CH_OUT_FMT GENMASK(1, 0) +#define MSCA_CH_OUT_STROBE BIT(6) +#define MSCA_Y_FIFO(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_Y_FIFO) +#define MSCA_CH_REG_34(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_REG_34) +#define MSCA_Y_DONE(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_Y_DONE) +#define MSCA_Y_DONE_EMPTY(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_Y_DONE_EMPTY) +#define MSCA_Y_STRIDE(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_Y_STRIDE) +#define MSCA_Y_RESET(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_Y_RESET) +#define MSCA_CH_REG_4C(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_REG_4C) +#define MSCA_UV_FIFO(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_UV_FIFO) +#define MSCA_CH_REG_54(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_REG_54) +#define MSCA_UV_DONE(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_UV_DONE) +#define MSCA_UV_DONE_EMPTY(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_UV_DONE_EMPTY) +#define MSCA_UV_STRIDE(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_UV_STRIDE) +#define MSCA_UV_RESET(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_UV_RESET) +#define MSCA_CH_REG_6C(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_REG_6C) +#define MSCA_CH_SCALE_PARA0(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_SCALE_PARA0) +#define MSCA_CH_SCALE_PARA1(ch) (MSCA_CH_BASE(ch) + MSCA_CH_OFF_SCALE_PARA1) +#define MSCA_DONE_ADDR GENMASK(31, 3) +#define MSCA_DONE_TAG GENMASK(2, 0) +#define MSCA_DONE_EMPTY BIT(0) + +#define MSCA_SCALE_PARA0_X0 GENMASK(9, 0) +#define MSCA_SCALE_PARA0_X1 GENMASK(25, 16) +#define MSCA_SCALE_PARA1_Y0 GENMASK(7, 0) +#define MSCA_SCALE_PARA1_Y1 GENMASK(15, 8) +#define MSCA_SCALE_PARA1_SLOPE GENMASK(24, 16) +#define MSCA_SCALE_PARA1_SHIFT GENMASK(31, 28) +#define MSCA_SCALE_PARA0_DEFAULT (FIELD_PREP(MSCA_SCALE_PARA0_X0, 150) | \ + FIELD_PREP(MSCA_SCALE_PARA0_X1, 450)) +#define MSCA_SCALE_PARA1_DEFAULT (FIELD_PREP(MSCA_SCALE_PARA1_Y0, 0x80) | \ + FIELD_PREP(MSCA_SCALE_PARA1_Y1, 0x80) | \ + FIELD_PREP(MSCA_SCALE_PARA1_SHIFT, 8)) + +#define MSCA_MASK(ch, n) (0xd400 + (ch) * 0x40 + (n) * 0x10) +#define MSCA_MASK_POS(ch, n) (MSCA_MASK(ch, n) + 0x00) +#define MSCA_MASK_SIZE(ch, n) (MSCA_MASK(ch, n) + 0x04) +#define MSCA_MASK_COLOR(ch, n) (MSCA_MASK(ch, n) + 0x08) +#define MSCA_MASK_COLOR_Y GENMASK(23, 16) +#define MSCA_MASK_COLOR_U GENMASK(15, 8) +#define MSCA_MASK_COLOR_V GENMASK(7, 0) + +#define MSCA_LINE(n) (0xd4c0 + (n) * 0x10) +#define MSCA_LINES 6 +#define MSCA_LINE_START(n) (MSCA_LINE(n) + 0x00) +#define MSCA_LINE_END(n) (MSCA_LINE(n) + 0x04) +#define MSCA_LINE_COLOR(n) (MSCA_LINE(n) + 0x08) +#define MSCA_LINE_COLOR_YUV GENMASK(23, 0) +#define MSCA_LINE_COLOR_WIDTH GENMASK(31, 24) +#define MSCA_LINE_CTRL(n) (MSCA_LINE(n) + 0x0c) +#define MSCA_LINE_CTRL_CH_MASK GENMASK(2, 0) +#define MSCA_LINE_CTRL_DDA_EN BIT(5) +#define MSCA_LINE_CTRL_ALPHA GENMASK(15, 8) +#define MSCA_LINE_CTRL_SLOPE GENMASK(24, 16) +#define MSCA_LINE_CTRL_OCTANT GENMASK(27, 25) + +#define MSCA_OSD_CTRL 0xd750 +#define MSCA_OSD_CTRL_TYPE GENMASK(1, 0) +#define MSCA_OSD_CTRL_TYPE_32BIT 0 +#define MSCA_OSD_CTRL_TYPE_1555 1 +#define MSCA_OSD_CTRL_DEFAULT GENMASK(6, 4) /* undocumented */ +#define MSCA_OSD_CTRL_NO_ALPHA BIT(8) +#define MSCA_OSD_CTRL_ALPHA_MASK GENMASK(15, 8) +#define MSCA_OSD_ORDER 0xd754 +#define MSCA_OSD_ORDER_BGRA 0 +#define MSCA_OSD_ORDER_ARGB 13 +#define MSCA_OSD_BLOCK(n) (0xd760 + (n) * 0x10) +#define MSCA_OSD_BLOCKS 8 +#define MSCA_OSD_POS(n) (MSCA_OSD_BLOCK(n) + 0x00) +#define MSCA_OSD_SIZE(n) (MSCA_OSD_BLOCK(n) + 0x04) +#define MSCA_OSD_ADDR(n) (MSCA_OSD_BLOCK(n) + 0x08) +#define MSCA_OSD_STRIDE(n) (MSCA_OSD_BLOCK(n) + 0x0c) + +#define MSCA_SHD_CTRL 0xe000 +#define MSCA_SHD_CTRL_DEFAULT_BEGIN 0x00000101 /* undocumented */ +#define MSCA_SHD_CTRL_DEFAULT_END 0x01010102 /* undocumented */ +#define MSCA_SHD_FIFO 0xe004 +#define MSCA_SHD_STAT 0xe00c +#define MSCA_SHD_STAT_BUSY GENMASK(31, 1) +#define MSCA_SHD_DMA 0xe100 +#define MSCA_SHD_DMA_START BIT(0) +#define MSCA_SHD_DMA_BUSY BIT(1) +#define MSCA_SHD_DMA_COUNT GENMASK(19, 4) +#define MSCA_SHD_VCOEF(ch) (0x800 + (ch) * 0x60) +#define MSCA_SHD_HCOEF(ch) (0x830 + (ch) * 0x60) +#define MSCA_COEF_LO GENMASK(12, 0) +#define MSCA_COEF_HI GENMASK(28, 16) + +/* ========================================================================= + * 22. IVDC (ivdc_base) + * ========================================================================= */ +#define IVDC_RESET 0x0000 /* Software reset */ +#define IVDC_ISP_CTRL 0x0004 /* ISP interface control */ +#define IVDC_VPU_CTRL 0x0008 /* VPU interface control */ +#define IVDC_ISP_SIZE 0x0010 /* ISP input resolution */ +#define IVDC_ISP_ALN 0x0014 /* ISP alignment configuration */ +#define IVDC_VPU0_SIZE 0x0018 /* VPU channel 0 size */ +#define IVDC_VPU1_SIZE 0x001c /* VPU channel 1 size */ +#define IVDC_STRIDE 0x0020 /* Line stride */ +#define IVDC_VPU_FRAME_ID 0x0024 /* VPU frame identifier */ +#define IVDC_RAM_MAX_LINE 0x0028 /* RAM maximum line threshold */ +#define IVDC_RAM_BASE_ADDR 0x002c /* RAM base buffer address */ +#define IVDC_DDR_MAX_LINE 0x0030 /* DDR maximum line threshold */ +#define IVDC_DDR_Y_BASE_ADDR 0x0034 /* DDR Y component base address */ +#define IVDC_DDR_UV_BASE_ADDR 0x0038 /* DDR UV component base address */ +#define IVDC_FIFO_BASE_ADDR 0x003c /* FIFO base buffer address */ +#define IVDC_INTC_ENABLE 0x0040 /* Interrupt controller enable */ +#define IVDC_INTC_STATE 0x0044 /* Interrupt controller status */ +#define IVDC_DDR_HALF_FULL_THRD 0x0048 /* DDR half-full threshold */ +#define IVDC_ISP_HALF_DONE_THRD 0x004c /* ISP half-done threshold */ +#define IVDC_VPU_HALF_DONE_THRD 0x0050 /* VPU half-done threshold */ +#define IVDC_TOP_INFO1 0x005c /* Top stream trigger control */ +#define IVDC_TOP_INFO 0x0060 /* Top status / FIFO state */ +#define IVDC_ISP_INFO 0x0064 /* ISP pipeline status */ +#define IVDC_VPU_INFO0 0x0068 /* VPU channel 0 status */ +#define IVDC_VPU_INFO1 0x006c /* VPU channel 1 status */ +#define IVDC_VPU0_START 0x0070 /* VPU channel 0 DMA start */ +#define IVDC_VPU0_CLR_FIFO 0x0074 /* VPU channel 0 FIFO clear */ +#define IVDC_VPU0_RUN_FIFO 0x0078 /* VPU channel 0 FIFO run */ +#define IVDC_VPU0_FIFO_STATE 0x0080 /* VPU channel 0 FIFO status */ +#define IVDC_VPU1_START 0x0088 /* VPU channel 1 DMA start */ +#define IVDC_VPU1_CLR_FIFO 0x008c /* VPU channel 1 FIFO clear */ +#define IVDC_VPU1_RUN_FIFO 0x0090 /* VPU channel 1 FIFO run */ +#define IVDC_VPU1_FIFO_STATE 0x0098 /* VPU channel 1 FIFO status */ +#define IVDC_ISP_STOP_THRD 0x00a4 /* ISP stop threshold */ +#define IVDC_ISP_DISCARD_THRD 0x00a8 /* ISP discard threshold */ +#define IVDC_IDMA_ENABLE 0x00b0 /* Internal DMA enable */ +#define IVDC_IDMA_Y_ADDR 0x00b4 /* Internal DMA Y plane address */ +#define IVDC_IDMA_UV_ADDR 0x00b8 /* Internal DMA UV plane address */ +#define IVDC_IDMA_STRIDE 0x00bc /* Internal DMA line stride */ +#define IVDC_VPU0_END_CFG0 0x00c0 /* VPU channel 0 end config 0 */ +#define IVDC_VPU1_END_CFG0 0x00c4 /* VPU channel 1 end config 0 */ +#define IVDC_VPU0_END_CFG1 0x00c8 /* VPU channel 0 end config 1 */ +#define IVDC_VPU1_END_CFG1 0x00cc /* VPU channel 1 end config 1 */ +#define IVDC_TIMEOUT_ENABLE 0x00d0 /* Timeout monitor enable */ +#define IVDC_TIMEOUT_WAIT_THRD 0x00d4 /* Timeout wait threshold */ +#define IVDC_TIMEOUT_WORK_THRD 0x00d8 /* Timeout work threshold */ +#define IVDC_MAX_DDR_LINE 0x00ec /* DDR maximum line reached status */ +#define IVDC_MAX_DDR_LINE_Y_MASK GENMASK(14, 0) +#define IVDC_MAX_DDR_LINE_UV_MASK GENMASK(30, 16) + +/* ========================================================================= + * 23. VIC (Video Ingest Controller) (vic_base) + * ========================================================================= */ +#define VIC_CONTROL_REG 0x0000 /* VIC capture control */ +#define VIC_CTRL_ENABLE BIT(0) +#define VIC_CTRL_RESET_BIT1 BIT(1) +#define VIC_CTRL_RESET_BIT2 BIT(2) + +#define VIC_IMAGE_SIZE 0x0004 /* Ingest image resolution */ +#define VIC_IMAGE_WIDTH_MASK GENMASK(31, 16) +#define VIC_IMAGE_HEIGHT_MASK GENMASK(15, 0) + +#define VIC_ENABLE_REG 0x0008 /* Sub-module enable flags */ +#define VIC_ENABLE_DEFAULT 0x00000004 /* undocumented */ + +#define VIC_CONF_REG 0x000c /* Ingest configuration mode */ +#define VIC_CONF_DEFAULT 0x00000002 /* undocumented */ + +#define VIC_INPUT_REG 0x0014 /* Ingest input interface selection */ +#define VIC_INPUT_DEFAULT 0x00000001 /* undocumented */ + +#define VIC_LINE_STRIDE 0x0100 /* Ingest line stride */ +#define VIC_DVP_TIMING0 0x0104 /* DVP interface timing parameter 0 */ +#define VIC_DVP_TIMING1 0x0108 /* DVP interface timing parameter 1 */ +#define VIC_CSI_CONF 0x010c /* CSI interface configuration */ +#define VIC_CSI_CONF_DEFAULT (0x2b << 12) /* undocumented */ +#define VIC_CSI_SIZE 0x0110 /* CSI virtual channel 0 size */ +#define VIC_CSI_SIZE1 0x0114 /* CSI virtual channel 1 size */ +#define VIC_CSI_SIZE2 0x0118 /* CSI virtual channel 2 size */ +#define VIC_CSI_SIZE3 0x011c /* CSI virtual channel 3 size */ +#define VIC_FRM_CTRL 0x01a0 /* Frame control configuration */ +#define VIC_FRM_CYCLE 0x01a4 /* Frame blanking cycle timing */ +#define VIC_FRM_CYCLE_DEFAULT 0x000a000a /* undocumented */ +#define VIC_FIFO_TH_0 0x01a8 /* FIFO threshold 0 */ +#define VIC_FIFO_TH_0_DEFAULT 0x4440 /* undocumented */ +#define VIC_FIFO_TH_1 0x01ac /* FIFO threshold 1 */ +#define VIC_FIFO_TH_1_DEFAULT 0x4440 /* undocumented */ +#define VIC_FIFO_TH_2 0x01b0 /* FIFO threshold 2 */ +#define VIC_FIFO_TH_2_DEFAULT 0x10 /* undocumented */ + +#define VIC_STATUS_REG_0 0x01e0 /* VIC interrupt status bank 0 */ +#define VIC_INT0_FRAME_DONE BIT(0) +#define VIC_INT0_FRAME_START BIT(4) +#define VIC_INT0_FRAME_ASFIFO_OVF BIT(9) +#define VIC_INT0_HOR_ERR_CH0 BIT(10) +#define VIC_INT0_HOR_ERR_CH1 BIT(11) +#define VIC_INT0_HOR_ERR_CH2 BIT(12) +#define VIC_INT0_HOR_ERR_CH3 BIT(13) +#define VIC_INT0_VER_ERR_CH0 BIT(14) +#define VIC_INT0_VER_ERR_CH1 BIT(15) +#define VIC_INT0_VER_ERR_CH2 BIT(16) +#define VIC_INT0_VER_ERR_CH3 BIT(17) +#define VIC_INT0_HVF_ERR BIT(18) +#define VIC_INT0_DVP_HCOMP_ERR BIT(19) +#define VIC_INT0_DMA_SYFIFO_OVF BIT(20) +#define VIC_INT0_CONTROL_LIMIT_ERR BIT(21) +#define VIC_INT0_IMAGE_SYFIFO_OVF BIT(22) +#define VIC_INT0_MIPI_FID_ASFIFO_OVF BIT(23) +#define VIC_INT0_MIPI_CH0_HCOMP_ERR BIT(24) +#define VIC_INT0_MIPI_CH1_HCOMP_ERR BIT(25) +#define VIC_INT0_MIPI_CH2_HCOMP_ERR BIT(26) +#define VIC_INT0_MIPI_CH3_HCOMP_ERR BIT(27) +#define VIC_INT0_MIPI_CH0_VCOMP_ERR BIT(28) +#define VIC_INT0_MIPI_CH1_VCOMP_ERR BIT(29) +#define VIC_INT0_MIPI_CH2_VCOMP_ERR BIT(30) +#define VIC_INT0_MIPI_CH3_VCOMP_ERR BIT(31) + +#define VIC_STATUS_REG_1 0x01e4 /* VIC interrupt status bank 1 */ +#define VIC_INT1_DMA_CH0_FRAME_DONE BIT(0) +#define VIC_INT1_DMA_CH1_FRAME_DONE BIT(1) +#define VIC_INT1_DMA_ARB_TRANS_DONE_OVF BIT(2) +#define VIC_INT1_DMA_CHID_OVF BIT(3) + +#define VIC_MASK_REG_0 0x01e8 /* VIC interrupt mask bank 0 */ +#define VIC_MASK_REG_1 0x01ec /* VIC interrupt mask bank 1 */ +#define VIC_CLEAR_REG_0 0x01f0 /* VIC interrupt clear bank 0 (W1C) */ +#define VIC_CLEAR_REG_1 0x01f4 /* VIC interrupt clear bank 1 (W1C) */ + +#define VIC_AUTO_SWITCH_CTRL 0x0200 /* Sensor ping-pong switch control */ +#define VIC_AUTO_SWITCH_EN BIT(31) +#define VIC_AUTO_SWITCH_ACTIVE BIT(16) +#define VIC_AUTO_SWITCH_PORT0 BIT(0) + +#define VIC_PORT_SEL 0x0204 /* Active sensor input port select */ +#define VIC_PORT_SEL_PORT0 0x00000000 +#define VIC_PORT_SEL_PORT1 0xffffffff + +#define VIC_AUTO_SWITCH_TIME 0x0208 /* Auto switch cycle duration */ +#define VIC_PORT_STATUS 0x020c /* Active sensor port status */ + +#define VIC_DMA_CONTROL 0x0300 /* VIC DMA control */ +#define VIC_DMA_CTRL_ENABLE BIT(31) +#define VIC_DMA_CTRL_AUTO BIT(16) +#define VIC_DMA_CTRL_FMT_RAW10 BIT(5) + +#define VIC_DMA_RESOLUTION 0x0304 /* VIC DMA buffer dimensions */ +#define VIC_DMA_RES_WIDTH_MASK GENMASK(31, 16) +#define VIC_DMA_RES_HEIGHT_MASK GENMASK(15, 0) + +#define VIC_DMA_RESET 0x0308 /* VIC DMA reset trigger */ +#define VIC_DMA_RESET_TRIG BIT(0) + +#define VIC_DMA_STRIDE 0x0310 /* VIC DMA Y/ch0 plane line stride */ +#define VIC_DMA_STRIDE1 0x0314 /* VIC DMA UV/ch1 plane line stride */ +#define VIC_DMA_BANK(n) (0x0318 + 4 * (n)) +#define VIC_DMA_CURRENT 0x0380 /* Active DMA buffer address pointer */ + +/* ========================================================================= + * 24. CSI-2 Host Controller (sd->base) + * ========================================================================= */ +#define CSI2_VERSION 0x0000 /* Controller version */ +#define CSI2_N_LANES 0x0004 /* Number of active data lanes */ +#define CSI2_N_LANES_MASK GENMASK(1, 0) + +#define CSI2_PHY_SHUTDOWNZ 0x0008 /* D-PHY shutdown control (active low) */ +#define CSI2_PHY_SHUTDOWNZ_BIT BIT(0) + +#define CSI2_DPHY_RSTZ 0x000c /* D-PHY reset control (active low) */ +#define CSI2_DPHY_RSTZ_BIT BIT(0) + +#define CSI2_RESETN 0x0010 /* CSI-2 core controller reset (active low) */ +#define CSI2_RESETN_BIT BIT(0) + +#define CSI2_PHY_STATE 0x0014 /* D-PHY line state status */ +#define CSI2_PHY_STOPSTATEDATA(n) BIT(4 + (n)) +#define CSI2_PHY_RXCLKACTIVEHS BIT(8) +#define CSI2_PHY_RXULPSCLKNOT BIT(9) +#define CSI2_PHY_STOPSTATECLK BIT(10) + +#define CSI2_DATA_IDS_1 0x0018 /* Data ID configuration 1 */ +#define CSI2_DATA_IDS_2 0x001c /* Data ID configuration 2 */ +#define CSI2_ERR1 0x0020 /* Packet / ECC error status register 1 */ +#define CSI2_ERR2 0x0024 /* CRC / sync error status register 2 */ +#define CSI2_MSK1 0x0028 /* Error mask register 1 */ +#define CSI2_MSK2 0x002c /* Error mask register 2 */ +#define CSI2_PHY_TST_CTRL0 0x0030 /* D-PHY test interface control 0 */ +#define CSI2_PHY_TESTCLR BIT(0) +#define CSI2_PHY_TESTCLK BIT(1) + +#define CSI2_PHY_TST_CTRL1 0x0034 /* D-PHY test interface control 1 */ +#define CSI2_PHY_TESTEN BIT(16) + +#define CSI2_TIMEOUT_REG 0x0100 /* Reception timeout threshold */ +#define CSI2_TIMEOUT_DEFAULT 0x00000003 /* undocumented */ + +/* ========================================================================= + * 25. Ingenic T23 D-PHY Receiver (csi->phy) + * ========================================================================= */ +#define DPHY_PWR_REG 0x0000 /* D-PHY power control */ +#define DPHY_PWR_ON 0x0000007d /* undocumented */ + +#define DPHY_RST_REG 0x0128 /* D-PHY reset control */ +#define DPHY_RST_RELEASE 0x0000003f /* undocumented */ + +#define DPHY_CLK_SETTLE 0x0160 /* Clock lane settle time */ +#define DPHY_DATA0_SETTLE 0x01e0 /* Data lane 0 settle time */ +#define DPHY_DATA1_SETTLE 0x0260 /* Data lane 1 settle time */ +#define DPHY_LANE_SETTLE(lane) (0x0160 + (lane) * 0x80) +#define DPHY_SETTLE_MASK GENMASK(3, 0) + +#endif /* __TX_ISP_REGS_H__ */ diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-stats.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-stats.c new file mode 100644 index 0000000000..075ac6568a --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-stats.c @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#include "tx-isp-stats.h" +#include "tx-isp-regs.h" + +#define T23_STATS_INPUTS 2 +#define T23_STATS_SIZE (T23_STATS_INPUTS * T23_STATS_INPUT_SIZE) + +static const u32 sc1a4t_af_filters[] = { + 0x008007ea, 0x0080012c, 0x000007ea, 0x078a07eb, + 0x078a013d, 0x000007eb, 0x02000026, 0x00d60100, + 0x01000793, 0x00560053, 0x060000ee, 0x01da0100, + 0x07000722, 0x00ef00de, 0xff000800, 0x23b0ffeb, + 0xff002400, 0x23a0f0dc, 0xff001e00, 0x0ff0f5e6, + 0xff001900, 0x0f80fadc, 0x01fe00ff, 0x00ff0010, + 0x01fe00ff, 0x00ff0010, 0x01fe00ff, 0x00ff0010, + 0x01fe00ff, 0x00ff0010, +}; + +int t23_isp_stats_alloc(struct t23_isp_stats *stats, struct device *dev) +{ + if (stats->buffer) + return stats->dev == dev ? 0 : -EINVAL; + stats->buffer = dma_alloc_coherent(dev, T23_STATS_SIZE, &stats->dma, + GFP_KERNEL); + if (!stats->buffer) + return -ENOMEM; + if ((u64)stats->dma + T23_STATS_SIZE - 1 > U32_MAX) { + dma_free_coherent(dev, T23_STATS_SIZE, stats->buffer, stats->dma); + stats->buffer = NULL; + stats->dma = 0; + return -ERANGE; + } + stats->dev = dev; + stats->active = false; + memset(stats->buffer, 0, T23_STATS_SIZE); + return 0; +} + +static void t23_stats_banks(void __iomem *base, unsigned int offset, + dma_addr_t address, unsigned int stride) +{ + unsigned int i; + + for (i = 0; i < 4; i++) + writel(address + i * stride, base + offset + i * 4); +} + +static void t23_stats_zone_sizes(void __iomem *base, unsigned int offset, + unsigned int count, unsigned int size) +{ + unsigned int i, j; + u32 value; + + for (i = 0; i < 15; i += 4) { + value = 0; + for (j = 0; j < 4 && i + j < count; j++) + value |= size << (j * 8); + writel(value, base + offset + i); + } +} + +static void t23_stats_boundaries(void __iomem *base, unsigned int offset, + unsigned int count, unsigned int size) +{ + unsigned int i, low, high; + + for (i = 0; i <= count; i += 2) { + low = i * (size / count) + min(i, size % count); + high = i == count ? 0 : + (i + 1) * (size / count) + min(i + 1, size % count); + writel(low | high << 16, base + offset + i * 2); + } +} + +static int t23_stats_geometry(void __iomem *base) +{ + u32 geometry = readl(base + ISP_TOP_IMAGE_SIZE); + unsigned int width = geometry >> 16, height = geometry & 0xffff; + unsigned int x, y, margin_x, margin_y, offset; + + if (width < 32 || height < 32 || width > 1920 || height > 1080) + return -EINVAL; + + /* AE and AWB collect 15 by 15 zones in the Bayer half-resolution grid. */ + for (offset = ISP_AE_REG_BASE; offset <= ISP_AWB_REG_BASE; offset += 0x1000) { + writel(ISP_AE_CTRL_DEFAULT, base + offset + 4); + t23_stats_zone_sizes(base, offset + 8, 15, width / 30); + t23_stats_zone_sizes(base, offset + 0x18, 15, height / 30); + } + writel(ISP_AE_LUM_TH_FREQ_DEFAULT, base + ISP_AE_LUM_TH_FREQ); + writel(ISP_AWB_TRIGGER_COMMIT, base + ISP_AWB_TRIGGER); + writel(ISP_AWB_COLOR_PARAM0_DEFAULT, base + ISP_AWB_COLOR_PARAM0); + writel(ISP_AWB_TRIGGER_COMMIT, base + ISP_AWB_TRIGGER); + writel(ISP_AWB_COLOR_PARAM1_DEFAULT, base + ISP_AWB_COLOR_PARAM1); + writel(ISP_AWB_TRIGGER_COMMIT, base + ISP_AWB_TRIGGER); + writel(ISP_AWB_COLOR_PARAM2_DEFAULT, base + ISP_AWB_COLOR_PARAM2); + writel(ISP_AWB_TRIGGER_COMMIT, base + ISP_AWB_TRIGGER); + writel(ISP_AWB_COLOR_PARAM3_DEFAULT, base + ISP_AWB_COLOR_PARAM3); + /* Initial luminance threshold and sampling frequency for AWB statistics. */ + writel(ISP_AWB_TRIGGER_COMMIT, base + ISP_AWB_TRIGGER); + writel(ISP_AWB_LUM_TH_FREQ_DEFAULT, base + ISP_AWB_LUM_TH_FREQ); + + writel(ISP_AF_CTRL_DEFAULT, base + ISP_AF_CONTROL); + t23_stats_zone_sizes(base, ISP_AF_ZONE_W_BASE, 8, ((width - 15) / 8) & ~1U); + t23_stats_zone_sizes(base, ISP_AF_ZONE_H_BASE, 8, ((height - 3) / 8) & ~1U); + writel(ISP_AF_TRIGGER_COMMIT, base + ISP_AF_TRIGGER); + writel(ISP_AF_THRES0_DEFAULT, base + ISP_AF_THRES0); + writel(0, base + ISP_AF_THRES1); + for (offset = 0; offset < ARRAY_SIZE(sc1a4t_af_filters); offset++) + writel(sc1a4t_af_filters[offset], base + ISP_AF_FILTER_BASE + offset * 4); + + x = (width / 6) & ~1U; + y = (height / 4) & ~1U; + margin_x = max((x / 4) & ~1U, 20U); + margin_y = max((y / 4) & ~1U, 20U); + writel(x | y << 16, base + ISP_ADR_GRID_STEP); + writel(y << 16, base + ISP_ADR_GRID_Y0); + writel(2 * y | 3 * y << 16, base + ISP_ADR_GRID_Y1); + writel(height, base + ISP_ADR_GRID_HEIGHT); + writel(x << 16, base + ISP_ADR_GRID_X0); + writel(2 * x | 3 * x << 16, base + ISP_ADR_GRID_X1); + writel(4 * x | 5 * x << 16, base + ISP_ADR_GRID_X2); + writel(width, base + ISP_ADR_GRID_WIDTH); + writel(margin_y | (height - margin_y) << 16, base + ISP_ADR_MARGIN_Y); + writel(margin_x | (width - margin_x) << 16, base + ISP_ADR_MARGIN_X); + + t23_stats_boundaries(base, ISP_DEFOG_BOUND_V_BASE, 10, height); + t23_stats_boundaries(base, ISP_DEFOG_BOUND_H_BASE, 18, width); + writel(0, base + ISP_DEFOG_MIN_THRES); + writel(U32_MAX, base + ISP_DEFOG_MAX_THRES); + writel(0, base + ISP_DEFOG_CONTROL); + return 0; +} + +int t23_isp_stats_configure(struct t23_isp_stats *stats, void __iomem *base, + unsigned int input) +{ + dma_addr_t address; + int ret; + + if (!stats->buffer || input >= T23_STATS_INPUTS) + return -EINVAL; + address = stats->dma + input * T23_STATS_INPUT_SIZE; + dma_wmb(); + /* AE writes 4 KiB of zone data and 2 KiB of histogram data. */ + t23_stats_banks(base, ISP_AE_DMA_ADDR_BASE, address, 0); + t23_stats_banks(base, ISP_AE_DMA_HIST_ADDR_BASE, address + 0x1000, 0); + t23_stats_banks(base, ISP_AWB_DMA_ADDR_BASE, address + 0x2000, 0); + t23_stats_banks(base, ISP_AF_DMA_ADDR_BASE, address + 0x3000, 0); + t23_stats_banks(base, ISP_ADR_DMA_ADDR_BASE, address + 0x4000, 0x1000); + t23_stats_banks(base, ISP_DEFOG_DMA_ADDR_BASE, address + 0x8000, 0x1000); + ret = t23_stats_geometry(base); + if (ret) + return ret; + writel(ISP_AE_DMA_CTRL_DEFAULT, base + ISP_AE_DMA_CTRL); + writel(ISP_AWB_DMA_CTRL_DEFAULT, base + ISP_AWB_DMA_CTRL); + writel(ISP_AF_DMA_CTRL_DEFAULT, base + ISP_AF_DMA_CTRL); + writel(ISP_ADR_DMA_ENABLE, base + ISP_ADR_DMA_CTRL); + writel(ISP_DEFOG_DMA_ENABLE, base + ISP_DEFOG_DMA_CTRL); + stats->active = true; + return 0; +} + +int t23_isp_stats_free(struct t23_isp_stats *stats) +{ + if (stats->active) + return -EBUSY; + if (!stats->buffer) + return 0; + dma_free_coherent(stats->dev, T23_STATS_SIZE, stats->buffer, stats->dma); + memset(stats, 0, sizeof(*stats)); + return 0; +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-stats.h b/drivers/media/platform/ingenic/tx-isp/tx-isp-stats.h new file mode 100644 index 0000000000..380cb2dab4 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-stats.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ +#ifndef __TX_ISP_STATS_H__ +#define __TX_ISP_STATS_H__ + +#include +#include + +/* Per input: AE +0, histogram +0x1000, AWB +0x2000, AF +0x3000 */ +#define T23_STATS_INPUT_SIZE 0xc000 + +struct t23_isp_stats { + struct device *dev; + void *buffer; + dma_addr_t dma; + bool active; +}; + +int t23_isp_stats_alloc(struct t23_isp_stats *stats, struct device *dev); +int t23_isp_stats_configure(struct t23_isp_stats *stats, void __iomem *base, + unsigned int input); +int t23_isp_stats_free(struct t23_isp_stats *stats); + +#endif diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-vic.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-vic.c new file mode 100644 index 0000000000..de99f1c0a5 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-vic.c @@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 Video Ingest Controller + * Copyright (C) 2017-2023 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include + +#include "tx-isp-device.h" + +struct tx_isp_vic_device { + struct tx_isp_subdev sd; + struct platform_device *pdev; + struct mutex lock; + unsigned int width; + unsigned int height; + unsigned int port; + bool configured; + bool running; +}; + +static struct tx_isp_vic_device *g_vic_dev; + +int tx_isp_vic_configure(struct tx_isp_subdev *sd, unsigned int width, + unsigned int height, unsigned int port) +{ + struct tx_isp_vic_device *vic = sd ? sd->dev_priv : NULL; + int ret = 0; + + if (!vic || !width || width > 1920 || !height || height > 1080 || port > 1) + return -EINVAL; + mutex_lock(&vic->lock); + if (vic->running) + ret = -EBUSY; + else { + vic->width = width; + vic->height = height; + vic->port = port; + vic->configured = true; + } + mutex_unlock(&vic->lock); + return ret; +} + +int tx_isp_vic_start(struct tx_isp_subdev *sd) +{ + struct tx_isp_vic_device *vic = sd ? sd->dev_priv : NULL; + u32 value; + int ret = 0; + + if (!vic || !sd->base) + return -EINVAL; + mutex_lock(&vic->lock); + if (!vic->configured) { + ret = -EINVAL; + goto unlock; + } + if (vic->running) + goto unlock; + + /* The selected sensor supplies the input clock during reset. */ + writel(vic->port ? VIC_PORT_SEL_PORT1 : VIC_PORT_SEL_PORT0, + sd->base + VIC_PORT_SEL); + writel(0, sd->base + VIC_AUTO_SWITCH_CTRL); + writel(VIC_FRM_CYCLE_DEFAULT, sd->base + VIC_FRM_CYCLE); + writel(DIV_ROUND_UP(vic->width * 10, 32), sd->base + VIC_LINE_STRIDE); + writel(VIC_CONF_DEFAULT, sd->base + VIC_CONF_REG); + writel(VIC_INPUT_DEFAULT, sd->base + VIC_INPUT_REG); + writel((vic->width << 16) | vic->height, sd->base + VIC_IMAGE_SIZE); + writel(VIC_CSI_CONF_DEFAULT, sd->base + VIC_CSI_CONF); + writel(vic->width << 16, sd->base + VIC_CSI_SIZE); + writel(0, sd->base + VIC_CSI_SIZE1); + writel(0, sd->base + VIC_CSI_SIZE2); + writel(0, sd->base + VIC_CSI_SIZE3); + writel(VIC_FIFO_TH_1_DEFAULT, sd->base + VIC_FIFO_TH_1); + writel(VIC_FIFO_TH_0_DEFAULT, sd->base + VIC_FIFO_TH_0); + writel(VIC_FIFO_TH_2_DEFAULT, sd->base + VIC_FIFO_TH_2); + writel(VIC_CTRL_RESET_BIT1, sd->base + VIC_CONTROL_REG); + writel(VIC_CTRL_RESET_BIT2, sd->base + VIC_CONTROL_REG); + writel(0, sd->base + VIC_FRM_CTRL); + /* + * A frame-sync slave's clock starts after the master's first exposure + * and the slave's delay, up to a frame each: 109 ms at a 3000-line + * frame with a 2994-line master exposure. + */ + ret = readl_poll_timeout(sd->base + VIC_CONTROL_REG, value, !value, + 100, 500000); + if (ret) { + dev_err(&vic->pdev->dev, "VIC reset timed out: %#x\n", value); + goto unlock; + } + writel(0, sd->base + VIC_DVP_TIMING0); + writel(0, sd->base + VIC_DVP_TIMING1); + if (g_tx_isp_dev->processed) { + unsigned long rate = clk_get_rate(g_tx_isp_dev->cgu_isp); + + /* SC1A4T uses a 100 us hardware camera-switch delay. */ + writel(VIC_AUTO_SWITCH_EN | VIC_AUTO_SWITCH_ACTIVE | VIC_AUTO_SWITCH_PORT0, + sd->base + VIC_AUTO_SWITCH_CTRL); + writel((rate / 1000000) * 100, + sd->base + VIC_AUTO_SWITCH_TIME); + } + writel(VIC_ENABLE_DEFAULT, sd->base + VIC_ENABLE_REG); + writel(U32_MAX, sd->base + VIC_CLEAR_REG_0); + writel(U32_MAX, sd->base + VIC_CLEAR_REG_1); + writel(~(VIC_INT0_FRAME_START | VIC_INT0_VER_ERR_CH2 | + VIC_INT0_DMA_SYFIFO_OVF | VIC_INT0_CONTROL_LIMIT_ERR | + VIC_INT0_MIPI_CH0_HCOMP_ERR | VIC_INT0_MIPI_CH2_HCOMP_ERR), + sd->base + VIC_MASK_REG_0); + writel(~(VIC_INT1_DMA_CH0_FRAME_DONE | + VIC_INT1_DMA_ARB_TRANS_DONE_OVF | + VIC_INT1_DMA_CHID_OVF), + sd->base + VIC_MASK_REG_1); + if (!g_tx_isp_dev->processed) + writel(VIC_CTRL_ENABLE, sd->base + VIC_CONTROL_REG); + vic->running = true; +unlock: + mutex_unlock(&vic->lock); + return ret; +} + +void tx_isp_vic_capture_trigger(struct tx_isp_subdev *sd, bool enable) +{ + writel(enable ? VIC_CTRL_ENABLE : 0, sd->base + VIC_CONTROL_REG); + readl(sd->base + VIC_CONTROL_REG); +} + +int tx_isp_vic_stop(struct tx_isp_subdev *sd) +{ + struct tx_isp_vic_device *vic = sd ? sd->dev_priv : NULL; + + if (!vic || !sd->base) + return -EINVAL; + mutex_lock(&vic->lock); + if (vic->running) { + tx_isp_vic_dma_stop(sd); + writel(U32_MAX, sd->base + VIC_MASK_REG_0); + writel(U32_MAX, sd->base + VIC_MASK_REG_1); + writel(0, sd->base + VIC_CONTROL_REG); + vic->running = false; + } + mutex_unlock(&vic->lock); + return 0; +} + +void tx_isp_vic_dma_arm(struct tx_isp_subdev *sd, dma_addr_t address) +{ + struct tx_isp_vic_device *vic = sd->dev_priv; + unsigned int bank; + + writel(VIC_DMA_RESET_TRIG, sd->base + VIC_DMA_RESET); + writel((vic->width << 16) | vic->height, + sd->base + VIC_DMA_RESOLUTION); + writel(vic->width * 2, sd->base + VIC_DMA_STRIDE); + writel(vic->width * 2, sd->base + VIC_DMA_STRIDE1); + for (bank = 0; bank < 5; bank++) + writel(address, sd->base + VIC_DMA_BANK(bank)); + /* One frame per arm; unqueued userspace buffers are never DMA targets. */ + writel(VIC_DMA_CTRL_ENABLE | VIC_DMA_CTRL_AUTO | VIC_DMA_CTRL_FMT_RAW10, + sd->base + VIC_DMA_CONTROL); + writel(VIC_CTRL_ENABLE, sd->base + VIC_CONTROL_REG); +} + +void tx_isp_vic_select_port(struct tx_isp_subdev *sd, unsigned int port) +{ + struct tx_isp_vic_device *vic = sd->dev_priv; + + /* Change the mux only while DMA is stopped. */ + vic->port = port; + writel(port ? VIC_PORT_SEL_PORT1 : VIC_PORT_SEL_PORT0, + sd->base + VIC_PORT_SEL); + readl(sd->base + VIC_PORT_SEL); +} + +void tx_isp_vic_dma_stop(struct tx_isp_subdev *sd) +{ + writel(0, sd->base + VIC_DMA_CONTROL); + readl(sd->base + VIC_DMA_CONTROL); +} + +void tx_isp_vic_dma_resume(struct tx_isp_subdev *sd) +{ + writel(readl(sd->base + VIC_DMA_CONTROL) | VIC_DMA_CTRL_ENABLE, + sd->base + VIC_DMA_CONTROL); +} + +irqreturn_t isp_vic_interrupt_service_routine(struct tx_isp_subdev *sd, + bool *handled) +{ + struct tx_isp_vic_device *vic = sd ? sd->dev_priv : NULL; + u32 mask, mask1, pending, pending1, errors, errors1; + + if (!vic || !sd->base) + return IRQ_NONE; + mask = readl(sd->base + VIC_MASK_REG_0); + mask1 = readl(sd->base + VIC_MASK_REG_1); + pending = readl(sd->base + VIC_STATUS_REG_0) & ~mask; + pending1 = readl(sd->base + VIC_STATUS_REG_1) & ~mask1; + if (!pending && !pending1) + return IRQ_NONE; + writel(pending, sd->base + VIC_CLEAR_REG_0); + writel(pending1, sd->base + VIC_CLEAR_REG_1); + + errors = pending & GENMASK(31, 9); + errors1 = pending1 & GENMASK(3, 2); + if (errors || errors1) + dev_err_ratelimited(&vic->pdev->dev, + "VIC error status: %#x/%#x\n", errors, + errors1); + if ((pending & VIC_INT0_FRAME_START) && + !(pending1 & VIC_INT1_DMA_CH0_FRAME_DONE) && !errors && !errors1 && + (readl(sd->base + VIC_PORT_STATUS) & 1) == vic->port) + t23_isp_video_frame_event(dev_get_drvdata(vic->pdev->dev.parent)); + if ((pending1 & VIC_INT1_DMA_CH0_FRAME_DONE) || errors || errors1) { + struct tx_isp_device *isp = dev_get_drvdata(vic->pdev->dev.parent); + bool bad_address = (pending1 & VIC_INT1_DMA_CH0_FRAME_DONE) && + readl(sd->base + VIC_DMA_CURRENT) != isp->dma_address; + bool bad_port = (pending1 & VIC_INT1_DMA_CH0_FRAME_DONE) && + (readl(sd->base + VIC_PORT_STATUS) & 1) != vic->port; + + tx_isp_vic_dma_stop(sd); + if (isp->processed) { + if (errors || errors1) + t23_isp_video_processed_event(isp, false, false, true); + } else { + t23_isp_video_dma_done(isp, errors || errors1 || + bad_address || bad_port); + } + } + if (handled) + *handled = true; + return IRQ_HANDLED; +} + +static struct tx_isp_subdev_ops vic_subdev_ops = { + .interrupt_service_routine = isp_vic_interrupt_service_routine, +}; + +int tx_isp_vic_probe(struct platform_device *pdev) +{ + struct tx_isp_vic_device *vic; + int ret; + + vic = kzalloc(sizeof(*vic), GFP_KERNEL); + if (!vic) + return -ENOMEM; + vic->pdev = pdev; + mutex_init(&vic->lock); + ret = tx_isp_subdev_init(pdev, &vic->sd, &vic_subdev_ops); + if (ret) { + kfree(vic); + return ret; + } + vic->sd.dev_priv = vic; + g_vic_dev = vic; + return 0; +} + +int tx_isp_vic_remove(struct platform_device *pdev) +{ + struct tx_isp_vic_device *vic = g_vic_dev; + + if (vic) { + tx_isp_vic_stop(&vic->sd); + tx_isp_subdev_deinit(&vic->sd); + kfree(vic); + g_vic_dev = NULL; + } + return 0; +} diff --git a/drivers/media/platform/ingenic/tx-isp/tx-isp-video.c b/drivers/media/platform/ingenic/tx-isp/tx-isp-video.c new file mode 100644 index 0000000000..1a0bc01878 --- /dev/null +++ b/drivers/media/platform/ingenic/tx-isp/tx-isp-video.c @@ -0,0 +1,2773 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ingenic T23 Video Processor Subsystem - VIC capture + * + * Copyright (C) 2017-2023 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tx-isp-device.h" +#include "tx-isp-meta.h" +#include "tx-isp-motion.h" +#include "tx-isp-overlay.h" + +#define T23_MAX_INPUTS 2 +#define T23_CAPTURE_CHANNELS 2 + +struct t23_isp_buffer { + struct vb2_v4l2_buffer vb; + struct list_head list; +}; + +struct t23_async_subdev { + struct v4l2_async_connection asc; + unsigned int port; + unsigned int index; +}; + +struct t23_video_input { + struct v4l2_subdev *sd; + unsigned int pad; + u32 mbus_code; + bool initialized; +}; + +struct t23_isp_video; + +/* Downscaled NV12 copy of an input's processed frames, from MSCA channels 1-2 */ +struct t23_sub_node { + unsigned int index; + struct video_device vdev; + struct media_pad pad; + struct vb2_queue queue; + struct list_head bufs; + struct v4l2_pix_format pix; + bool streaming; + bool stopping; + bool queue_initialized; + bool entity_initialized; + bool vdev_registered; + unsigned int skip_frames; +}; + +struct t23_video_node { + struct t23_isp_video *video; + unsigned int channel_idx; + struct video_device vdev; + struct media_pad pad; + struct v4l2_subdev isp_sd; + /* + * 0: sensor, 1: video output, 2: parameters, 3: statistics, + * 4..5: scaled outputs, 6 onwards: OSD regions + */ + struct media_pad isp_pads[4 + T23_SCALED_OUTPUTS + T23_MSCA_OSD_REGIONS]; + struct vb2_queue queue; + struct media_pipeline pipe; /* shared by the main and scaled nodes */ + struct mutex isp_sd_lock; + struct v4l2_ctrl_handler controls; + struct t23_sub_node sub[T23_SCALED_OUTPUTS]; + struct t23_motion motion; + + /* Per-channel ISP controls (exposed on t23-isp-coreN subdev) */ + struct v4l2_ctrl *hflip; + struct v4l2_ctrl *vflip; + struct v4l2_ctrl *dgain; + struct v4l2_ctrl *red_balance; + struct v4l2_ctrl *blue_balance; + struct v4l2_ctrl *gamma_ctrl; + struct v4l2_ctrl *brightness; + struct v4l2_ctrl *contrast; + struct v4l2_ctrl *saturation; + struct v4l2_ctrl *sharpness; + + bool hflip_val; + bool vflip_val; + u32 dgain_val; + u32 red_val; + u32 blue_val; + u32 gamma_val; + s32 brightness_val; + u32 contrast_val; + u32 saturation_val; + u32 sharpness_val; + u8 gamma_lut[1024]; + + int input; + struct list_head bufs; + struct v4l2_pix_format pix; + bool streaming; + bool stopping; + bool queue_initialized; + bool entity_initialized; + bool vdev_registered; + bool isp_sd_finalized; + bool isp_sd_registered; + u32 sequence; /* frames delivered on any output node */ + unsigned int csi_clock_mhz; + unsigned int skip_frames; +}; + +struct t23_isp_video { + struct v4l2_device v4l2_dev; + struct media_device mdev; + struct v4l2_async_notifier notifier; + struct t23_async_subdev *ep_asd[T23_MAX_INPUTS]; + unsigned int num_endpoints; + struct t23_video_input inputs[T23_MAX_INPUTS]; + struct mutex lock; + spinlock_t qlock; + wait_queue_head_t dma_wait; + struct tx_isp_device *isp; + struct device *dev; + void (*media_release)(struct media_devnode *node); + int active_channel; + u32 active_outputs; /* BIT(0) main, BIT(1..2) scaled */ + unsigned int next_channel; + int sync_source; + struct mutex sync_lock; /* frame length of the synchronized inputs */ + struct work_struct vblank_sync_work; + atomic_t vblank_syncing; + unsigned int stream_order[T23_MAX_INPUTS]; + unsigned int num_streaming; + bool disconnected; + bool streaming; + bool dma_active; + bool processed_dma_done; + bool processed_all_done; + bool processed_completing; + bool dma_failed; + bool irq_enabled; + bool media_registered; + bool registered; + struct t23_video_node channels[T23_CAPTURE_CHANNELS]; + struct t23_meta meta; + struct t23_overlay overlay; +}; + +static struct t23_isp_video *t23_video; +static DEFINE_MUTEX(t23_video_lock); +static void t23_apply_processed_controls(struct t23_video_node *chan); + +static bool t23_chan_streaming(const struct t23_video_node *chan) +{ + unsigned int s; + + if (chan->streaming) + return true; + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (chan->sub[s].streaming) + return true; + return false; +} + +/* + * Run one VIC frame with MSCA output disabled. It commits the MSCA shadow + * geometry and seeds the MDNS history before the first DMA frame. + */ +static int t23_processed_prime(struct t23_isp_video *video, unsigned int input) +{ + struct tx_isp_device *isp = video->isp; + struct tx_isp_subdev *sd = isp->subdevs[TX_ISP_SUBDEV_CORE]; + struct tx_isp_core_device *core = sd->dev_priv; + void __iomem *vic = isp->subdevs[TX_ISP_SUBDEV_VIC]->base; + unsigned int elapsed, latched = 0; + u32 status, done, pending; + int ret; + + ret = t23_isp_stats_configure(&core->stats, sd->base, input); + if (ret) + return ret; + ret = tx_isp_core_mdns_start(sd, input); + if (ret) + return ret; + writel(0, sd->base + MSCA_CH_EN_MASK); + writel(1, sd->base + MSCA_SHD_TRIGGER); + writel(1, sd->base + MSCA_REG_060); + writel(1, isp->ivdc_base + IVDC_TOP_INFO1); + writel(ISP_IPC_MODE_DEFAULT, sd->base + ISP_IPC_MODE); + writel(ISP_IPC_TRIGGER_START, sd->base + ISP_IPC_TRIGGER); + dev_dbg(video->dev, + "prime before: run=%08x mode=%08x irq=%08x done=%08x msca=%08x geom=%08x\n", + readl(sd->base + ISP_IPC_TRIGGER), readl(sd->base + ISP_IPC_MODE), + readl(sd->base + ISP_INT_STATUS), readl(sd->base + ISP_INT1_STATUS), + readl(sd->base + MSCA_REG_060), readl(sd->base + MSCA_FCROP_SIZE)); + tx_isp_vic_capture_trigger(isp->subdevs[TX_ISP_SUBDEV_VIC], true); + for (elapsed = 10; elapsed <= 150; elapsed += 10) { + msleep(10); + if (!latched && readl(sd->base + MSCA_REG_060) == 1) + latched = elapsed; + } + tx_isp_vic_capture_trigger(isp->subdevs[TX_ISP_SUBDEV_VIC], false); + status = readl(sd->base + ISP_INT_STATUS); + done = readl(sd->base + ISP_INT1_STATUS); + dev_dbg(video->dev, + "prime after: latch_ms=%u run=%08x mode=%08x irq=%08x done=%08x msca=%08x geom=%08x\n", + latched, readl(sd->base + ISP_IPC_TRIGGER), readl(sd->base + ISP_IPC_MODE), + readl(sd->base + ISP_INT_STATUS), readl(sd->base + ISP_INT1_STATUS), + readl(sd->base + MSCA_REG_060), readl(sd->base + MSCA_FCROP_SIZE)); + if (status & ISP_INT_ERR_MASK) { + dev_err(video->dev, "prime frame error: %#x\n", status); + return -EIO; + } + if (!latched || !(done & ISP_INT1_BIT15)) { + dev_err(video->dev, "prime frame timeout: done=%#x\n", done); + return -ETIMEDOUT; + } + tx_isp_core_mdns_frame_done(sd); + writel(done, sd->base + ISP_INT1_CLEAR); + writel(status, sd->base + ISP_INT_CLEAR); + pending = readl(vic + VIC_STATUS_REG_0); + writel(pending, vic + VIC_CLEAR_REG_0); + pending = readl(vic + VIC_STATUS_REG_1); + writel(pending, vic + VIC_CLEAR_REG_1); + return 0; +} + +static bool t23_main_ready(struct t23_video_node *chan) +{ + return chan->streaming && !chan->stopping && !list_empty(&chan->bufs); +} + +static bool t23_sub_ready(struct t23_video_node *chan, unsigned int s) +{ + struct t23_sub_node *sub = &chan->sub[s]; + + return chan->video->isp->processed && sub->streaming && + !sub->stopping && !list_empty(&sub->bufs); +} + +static void t23_msca_target(struct tx_isp_msca_target *out, + const struct t23_video_node *chan, + struct list_head *bufs, + const struct v4l2_pix_format *pix) +{ + struct t23_isp_buffer *buf = list_first_entry(bufs, + struct t23_isp_buffer, list); + dma_addr_t y = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0); + + out->width = pix->width; + out->height = pix->height; + out->y = y; + out->uv = y + (size_t)pix->bytesperline * pix->height; + out->hflip = chan->hflip_val; + out->vflip = chan->vflip_val; +} + +static void t23_capture_next(struct t23_isp_video *video) +{ + unsigned int n, i, s; + int ret; + + if (!video->streaming || video->dma_active || video->dma_failed) + return; + for (n = 0; n < T23_CAPTURE_CHANNELS; n++) { + unsigned int index = (video->next_channel + n) % T23_CAPTURE_CHANNELS; + struct t23_video_node *chan = &video->channels[index]; + bool main = t23_main_ready(chan); + u32 outputs = main ? BIT(0) : 0; + + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (t23_sub_ready(chan, s)) + outputs |= BIT(s + 1); + if (!outputs) + continue; + video->active_channel = index; + video->active_outputs = outputs; + video->next_channel = (index + 1) % T23_CAPTURE_CHANNELS; + video->dma_active = true; + dma_wmb(); + tx_isp_csi_select_rate(video->isp->subdevs[TX_ISP_SUBDEV_CSI], + chan->csi_clock_mhz); + tx_isp_vic_select_port(video->isp->subdevs[TX_ISP_SUBDEV_VIC], index); + if (video->isp->processed) { + struct tx_isp_msca_target out[T23_MSCA_OUTPUTS] = { }; + struct tx_isp_msca_overlay overlay; + + /* MSCA writes straight into the head buffers. */ + if (main) + t23_msca_target(&out[0], chan, &chan->bufs, + &chan->pix); + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (outputs & BIT(s + 1)) + t23_msca_target(&out[s + 1], chan, + &chan->sub[s].bufs, + &chan->sub[s].pix); + video->processed_dma_done = false; + video->processed_all_done = false; + video->processed_completing = false; + t23_meta_params_apply(&video->meta, index, + video->isp->subdevs[TX_ISP_SUBDEV_CORE]); + t23_apply_processed_controls(chan); + t23_overlay_frame(&video->overlay, index, &overlay); + ret = tx_isp_core_dma_arm( + video->isp->subdevs[TX_ISP_SUBDEV_CORE], out, + &overlay, index); + if (ret) { + video->dma_failed = true; + video->dma_active = false; + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + if (video->channels[i].streaming) + vb2_queue_error(&video->channels[i].queue); + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (video->channels[i].sub[s].streaming) + vb2_queue_error(&video->channels[i].sub[s].queue); + } + wake_up_all(&video->dma_wait); + dev_err(video->dev, "Cannot arm ISP DMA: %d\n", ret); + return; + } + tx_isp_vic_capture_trigger( + video->isp->subdevs[TX_ISP_SUBDEV_VIC], true); + } else + tx_isp_vic_dma_arm(video->isp->subdevs[TX_ISP_SUBDEV_VIC], + video->isp->dma_address); + break; + } +} + +void t23_isp_video_processed_event(struct tx_isp_device *isp, bool dma_done, + bool all_done, bool error) +{ + struct t23_isp_video *video = isp->video_priv; + unsigned long flags; + bool complete; + + if (!video || !isp->processed) + return; + spin_lock_irqsave(&video->qlock, flags); + if (!video->streaming || !video->dma_active || video->dma_failed || + video->processed_completing) { + spin_unlock_irqrestore(&video->qlock, flags); + return; + } + video->processed_dma_done |= dma_done; + video->processed_all_done |= all_done; + /* Output completion alone does not release the shared ISP input. */ + complete = error || + (video->processed_dma_done && video->processed_all_done); + if (complete) { + video->processed_completing = true; + tx_isp_vic_capture_trigger(isp->subdevs[TX_ISP_SUBDEV_VIC], false); + tx_isp_core_dma_stop(isp->subdevs[TX_ISP_SUBDEV_CORE], error); + if (!error) + tx_isp_core_mdns_frame_done(isp->subdevs[TX_ISP_SUBDEV_CORE]); + video->processed_dma_done = false; + video->processed_all_done = false; + } + spin_unlock_irqrestore(&video->qlock, flags); + if (complete) + t23_isp_video_dma_done(isp, error); +} + +void t23_isp_video_frame_event(struct tx_isp_device *isp) +{ + struct t23_isp_video *video = isp->video_priv; + struct t23_video_node *chan; + unsigned long flags; + + if (!video || isp->processed) + return; + + spin_lock_irqsave(&video->qlock, flags); + chan = video->active_channel < 0 ? NULL : + &video->channels[video->active_channel]; + if (chan && chan->streaming && video->dma_active && !video->dma_failed) + tx_isp_vic_dma_resume(isp->subdevs[TX_ISP_SUBDEV_VIC]); + spin_unlock_irqrestore(&video->qlock, flags); +} + +static void t23_apply_processed_controls(struct t23_video_node *chan) +{ + struct tx_isp_subdev *core_sd = + chan->video->isp->subdevs[TX_ISP_SUBDEV_CORE]; + struct tx_isp_core_device *core = core_sd ? core_sd->dev_priv : NULL; + const struct tx_isp_params_state *params = + core && core->params && + chan->channel_idx < ARRAY_SIZE(core->history) ? + &core->params[chan->channel_idx] : NULL; + + if (!params || !params->blc_valid) + tx_isp_core_set_blc_default(core_sd); + tx_isp_core_set_dgain(core_sd, chan->dgain_val); + if (!params || !(params->valid & TX_ISP_PARAM_WB)) + tx_isp_core_set_wb(core_sd, chan->red_val, chan->blue_val); + if (!params || !(params->valid & TX_ISP_PARAM_CCM)) + tx_isp_core_set_ccm(core_sd, tx_isp_default_ccm); + if (!params || !(params->valid & TX_ISP_PARAM_DMSC)) + tx_isp_core_set_sharpness(core_sd, chan->sharpness_val); +} + +static void t23_update_gamma_lut(struct t23_video_node *chan) +{ + int i; + u32 g = chan->gamma_val; + + g = clamp(g, 70U, 180U); + + for (i = 0; i < 1024; i++) { + int j = i / 8; + int r = i % 8; + u16 v0 = tx_isp_calc_gamma_entry(j, g); + u16 v1 = tx_isp_calc_gamma_entry(j + 1, g); + u32 v = v0 + ((v1 - v0) * r) / 8; + u32 out = v >> 4; + chan->gamma_lut[i] = (out > 255) ? 255 : (u8)out; + } +} + +static void t23_process_raw10(void *destination, const void *source, + unsigned int width, unsigned int height, + u32 r_gain_q8, u32 b_gain_q8) +{ + __le16 *dst = destination; + const __le16 *src = destination; + unsigned int x, y; + + /* Copy uncached DMA memory in bulk before per-pixel processing. */ + memcpy(destination, source, (size_t)width * height * sizeof(*dst)); + for (y = 0; y < height; y++) { + const __le16 *s_row = src + y * width; + __le16 *d_row = dst + y * width; + bool y_odd = (y & 1); + + for (x = 0; x < width; x++) { + u32 raw = (le16_to_cpu(s_row[x]) >> 6) & 0x03ff; + bool x_odd = (x & 1); + + if (!y_odd && !x_odd) { + /* Blue site (even row, even col in BGGR) */ + int val = max(0, (int)raw - 64); + val = (val * (int)b_gain_q8) >> 8; + val += 64; + raw = clamp_val(val, 0, 1023); + } else if (y_odd && x_odd) { + /* Red site (odd row, odd col in BGGR) */ + int val = max(0, (int)raw - 64); + val = (val * (int)r_gain_q8) >> 8; + val += 64; + raw = clamp_val(val, 0, 1023); + } + d_row[x] = cpu_to_le16(raw); + } + } +} + +static void t23_process_isp_frame(void *destination, const void *source, + unsigned int width, unsigned int height, + u32 pixelformat, struct t23_video_node *chan) +{ + const __le16 *src = source; + u8 *dst = destination; + u8 *y_plane = dst; + u8 *uv_plane = dst + (size_t)width * height; + u8 *yuyv_dst = dst; + u8 *rgb_dst = dst; + u32 r_gain = chan->red_val; + u32 b_gain = chan->blue_val; + const u8 *lut = chan->gamma_lut; + s32 bright = chan->brightness_val; + int contrast = (int)chan->contrast_val; + int sat = (int)chan->saturation_val; + unsigned int x, y; + + for (y = 0; y < height; y += 2) { + const __le16 *row0 = src + y * width; + const __le16 *row1 = src + (y + 1) * width; + unsigned int uv_y = y / 2; + + for (x = 0; x < width; x += 2) { + u32 raw_b, raw_gr, raw_gb, raw_r; + int b0, gr0, gb0, r0; + int b_wb, r_wb, g_top, g_bot; + const s16 *ccm; + int r_ccm0, g_ccm0, b_ccm0; + int r_ccm1, g_ccm1, b_ccm1; + u8 r_g0, g_g0, b_g0; + u8 r_g1, g_g1, b_g1; + + /* 2x2 BGGR raw cell */ + raw_b = (le16_to_cpu(row0[x]) >> 6) & 0x3ff; + raw_gr = (le16_to_cpu(row0[x + 1]) >> 6) & 0x3ff; + raw_gb = (le16_to_cpu(row1[x]) >> 6) & 0x3ff; + raw_r = (le16_to_cpu(row1[x + 1]) >> 6) & 0x3ff; + + /* Black level subtraction (pedestal 64) */ + b0 = max(0, (int)raw_b - 64); + gr0 = max(0, (int)raw_gr - 64); + gb0 = max(0, (int)raw_gb - 64); + r0 = max(0, (int)raw_r - 64); + + /* Digital white balance */ + b_wb = min(1023, (b0 * (int)b_gain) >> 8); + r_wb = min(1023, (r0 * (int)r_gain) >> 8); + g_top = gr0; + g_bot = gb0; + + /* CCM for the top (gr) and bottom (gb) rows */ + ccm = tx_isp_default_ccm; + r_ccm0 = clamp_val((ccm[0] * r_wb + ccm[1] * g_top + ccm[2] * b_wb + 512) >> 10, 0, 1023); + g_ccm0 = clamp_val((ccm[3] * r_wb + ccm[4] * g_top + ccm[5] * b_wb + 512) >> 10, 0, 1023); + b_ccm0 = clamp_val((ccm[6] * r_wb + ccm[7] * g_top + ccm[8] * b_wb + 512) >> 10, 0, 1023); + + r_ccm1 = clamp_val((ccm[0] * r_wb + ccm[1] * g_bot + ccm[2] * b_wb + 512) >> 10, 0, 1023); + g_ccm1 = clamp_val((ccm[3] * r_wb + ccm[4] * g_bot + ccm[5] * b_wb + 512) >> 10, 0, 1023); + b_ccm1 = clamp_val((ccm[6] * r_wb + ccm[7] * g_bot + ccm[8] * b_wb + 512) >> 10, 0, 1023); + + /* Gamma LUT lookup (10-bit -> 8-bit display) */ + r_g0 = lut[r_ccm0]; + g_g0 = lut[g_ccm0]; + b_g0 = lut[b_ccm0]; + + r_g1 = lut[r_ccm1]; + g_g1 = lut[g_ccm1]; + b_g1 = lut[b_ccm1]; + + if (pixelformat == V4L2_PIX_FMT_RGB24) { + int r_p0 = clamp_val((((r_g0 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + int g_p0 = clamp_val((((g_g0 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + int b_p0 = clamp_val((((b_g0 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + + int r_p1 = clamp_val((((r_g1 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + int g_p1 = clamp_val((((g_g1 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + int b_p1 = clamp_val((((b_g1 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + + size_t idx00 = ((size_t)y * width + x) * 3; + size_t idx01 = ((size_t)y * width + x + 1) * 3; + size_t idx10 = (((size_t)y + 1) * width + x) * 3; + size_t idx11 = (((size_t)y + 1) * width + x + 1) * 3; + + rgb_dst[idx00] = r_p0; rgb_dst[idx00 + 1] = g_p0; rgb_dst[idx00 + 2] = b_p0; + rgb_dst[idx01] = r_p0; rgb_dst[idx01 + 1] = g_p0; rgb_dst[idx01 + 2] = b_p0; + rgb_dst[idx10] = r_p1; rgb_dst[idx10 + 1] = g_p1; rgb_dst[idx10 + 2] = b_p1; + rgb_dst[idx11] = r_p1; rgb_dst[idx11 + 1] = g_p1; rgb_dst[idx11 + 2] = b_p1; + } else { + /* BT.601 RGB to YUV */ + int y0 = (77 * r_g0 + 150 * g_g0 + 29 * b_g0 + 128) >> 8; + int y1 = (77 * r_g1 + 150 * g_g1 + 29 * b_g1 + 128) >> 8; + + int r_avg = (r_g0 + r_g1) >> 1; + int g_avg = (g_g0 + g_g1) >> 1; + int b_avg = (b_g0 + b_g1) >> 1; + + int u = ((-43 * r_avg - 85 * g_avg + 128 * b_avg + 128) >> 8) + 128; + int v = ((128 * r_avg - 107 * g_avg - 21 * b_avg + 128) >> 8) + 128; + + u8 y_out0 = clamp_val((((y0 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + u8 y_out1 = clamp_val((((y1 - 128) * contrast) >> 7) + 128 + bright, 0, 255); + u8 u_out = clamp_val((((u - 128) * sat) >> 7) + 128, 0, 255); + u8 v_out = clamp_val((((v - 128) * sat) >> 7) + 128, 0, 255); + + if (pixelformat == V4L2_PIX_FMT_NV12) { + y_plane[(size_t)y * width + x] = y_out0; + y_plane[(size_t)y * width + x + 1] = y_out0; + y_plane[((size_t)y + 1) * width + x] = y_out1; + y_plane[((size_t)y + 1) * width + x + 1] = y_out1; + + uv_plane[(size_t)uv_y * width + x] = u_out; + uv_plane[(size_t)uv_y * width + x + 1] = v_out; + } else if (pixelformat == V4L2_PIX_FMT_YUYV) { + size_t o0 = ((size_t)y * width + x) * 2; + size_t o1 = (((size_t)y + 1) * width + x) * 2; + + yuyv_dst[o0] = y_out0; + yuyv_dst[o0 + 1] = u_out; + yuyv_dst[o0 + 2] = y_out0; + yuyv_dst[o0 + 3] = v_out; + + yuyv_dst[o1] = y_out1; + yuyv_dst[o1 + 1] = u_out; + yuyv_dst[o1 + 2] = y_out1; + yuyv_dst[o1 + 3] = v_out; + } + + } + } + } +} + +static void t23_stats_done(struct t23_isp_video *video, + struct t23_video_node *chan, u32 sequence, + u64 timestamp) +{ + struct tx_isp_subdev *sd = video->isp->subdevs[TX_ISP_SUBDEV_CORE]; + struct tx_isp_core_device *core = sd->dev_priv; + u32 irq = xchg(&core->stats_irq, 0); + u32 thresholds = readl(sd->base + ISP_AE_LUM_TH_FREQ); + struct t23_meta_frame frame = { + .sequence = sequence, + .timestamp = timestamp, + .hw = core->stats.buffer + chan->channel_idx * T23_STATS_INPUT_SIZE, + .zone_width = chan->pix.width / 30, + .zone_height = chan->pix.height / 30, + .ae_low = thresholds & 0xff, + .ae_high = thresholds >> 8, + }; + + if (irq & ISP_INT_AE) + frame.valid |= T23_ISP_STATS_AE; + if (irq & ISP_INT_HIST) + frame.valid |= T23_ISP_STATS_HIST; + if (irq & ISP_INT_AWB) + frame.valid |= T23_ISP_STATS_AWB; + t23_meta_stats_done(&video->meta, chan->channel_idx, &frame); + if (frame.valid & T23_ISP_STATS_AE) + t23_motion_frame(&chan->motion, frame.hw, sequence); +} + +/* The buffer a finished frame fills, or NULL while start-up frames drop. */ +static struct t23_isp_buffer *t23_frame_buffer(struct list_head *bufs, + unsigned int *skip_frames) +{ + struct t23_isp_buffer *buf; + + if (*skip_frames) { + (*skip_frames)--; + return NULL; + } + if (list_empty(bufs)) + return NULL; + buf = list_first_entry(bufs, struct t23_isp_buffer, list); + list_del_init(&buf->list); + return buf; +} + +static void t23_fail_queue(struct vb2_queue *queue, struct list_head *bufs) +{ + struct t23_isp_buffer *buf; + + vb2_queue_error(queue); + while (!list_empty(bufs)) { + buf = list_first_entry(bufs, struct t23_isp_buffer, list); + list_del_init(&buf->list); + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, 0); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); + } +} + +static void t23_buffer_done(struct t23_isp_buffer *buf, u32 sequence, + u64 timestamp, unsigned long size) +{ + buf->vb.sequence = sequence; + buf->vb.field = V4L2_FIELD_NONE; + buf->vb.vb2_buf.timestamp = timestamp; + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); +} + +void t23_isp_video_dma_done(struct tx_isp_device *isp, bool error) +{ + struct t23_isp_video *video = isp->video_priv; + struct t23_isp_buffer *buf = NULL, *sub_buf[T23_SCALED_OUTPUTS] = { }; + struct t23_video_node *chan; + unsigned long flags; + unsigned int s; + bool any_sub = false; + u32 sequence; + u64 timestamp; + + if (!video) + return; + + spin_lock_irqsave(&video->qlock, flags); + if (video->active_channel < 0) { + spin_unlock_irqrestore(&video->qlock, flags); + return; + } + chan = &video->channels[video->active_channel]; + if (!video->dma_active) { + spin_unlock_irqrestore(&video->qlock, flags); + return; + } + if (!error && !t23_chan_streaming(chan)) { + video->dma_active = false; + t23_capture_next(video); + wake_up_all(&video->dma_wait); + spin_unlock_irqrestore(&video->qlock, flags); + return; + } + if (error) { + unsigned int i; + + video->dma_failed = true; + video->dma_active = false; + wake_up_all(&video->dma_wait); + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + struct t23_video_node *failed = &video->channels[i]; + + if (failed->streaming) + t23_fail_queue(&failed->queue, &failed->bufs); + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (failed->sub[s].streaming) + t23_fail_queue(&failed->sub[s].queue, + &failed->sub[s].bufs); + } + spin_unlock_irqrestore(&video->qlock, flags); + dev_err(&isp->pdev->dev, "DMA transfer failed on input %u\n", + chan->channel_idx); + return; + } + + video->dma_failed = false; + if (video->active_outputs & BIT(0)) + buf = t23_frame_buffer(&chan->bufs, &chan->skip_frames); + for (s = 0; s < T23_SCALED_OUTPUTS; s++) { + if (video->active_outputs & BIT(s + 1)) + sub_buf[s] = t23_frame_buffer(&chan->sub[s].bufs, + &chan->sub[s].skip_frames); + any_sub |= !!sub_buf[s]; + } + if (!buf && !any_sub) { + video->dma_active = false; + t23_capture_next(video); + wake_up_all(&video->dma_wait); + spin_unlock_irqrestore(&video->qlock, flags); + return; + } + sequence = chan->sequence++; + timestamp = ktime_get_ns(); + spin_unlock_irqrestore(&video->qlock, flags); + + dma_rmb(); + + if (isp->processed) { + /* The frame is already in the buffers. */ + t23_stats_done(video, chan, sequence, timestamp); + } else if (chan->pix.pixelformat == V4L2_PIX_FMT_SBGGR10 || + chan->pix.pixelformat == V4L2_PIX_FMT_SRGGB10 || + chan->pix.pixelformat == V4L2_PIX_FMT_SGBRG10 || + chan->pix.pixelformat == V4L2_PIX_FMT_SGRBG10) { + t23_process_raw10(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), + isp->dma_buffer, chan->pix.width, chan->pix.height, + chan->red_val, chan->blue_val); + } else { + t23_process_isp_frame(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), + isp->dma_buffer, chan->pix.width, chan->pix.height, + chan->pix.pixelformat, chan); + } + + if (buf) + t23_buffer_done(buf, sequence, timestamp, chan->pix.sizeimage); + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (sub_buf[s]) + t23_buffer_done(sub_buf[s], sequence, timestamp, + chan->sub[s].pix.sizeimage); + spin_lock_irqsave(&video->qlock, flags); + video->dma_active = false; + t23_capture_next(video); + wake_up_all(&video->dma_wait); + spin_unlock_irqrestore(&video->qlock, flags); +} + +static bool is_raw10_pixfmt(u32 pixelformat) +{ + return pixelformat == V4L2_PIX_FMT_SBGGR10 || + pixelformat == V4L2_PIX_FMT_SRGGB10 || + pixelformat == V4L2_PIX_FMT_SGBRG10 || + pixelformat == V4L2_PIX_FMT_SGRBG10; +} + +static struct v4l2_subdev *t23_sensor(struct t23_video_node *chan) +{ + struct t23_isp_video *video = chan->video; + + if (video->disconnected || chan->input < 0) + return NULL; + return video->inputs[chan->input].sd; +} + +static int t23_sensor_vblank(struct v4l2_subdev *sd, s32 *val, bool set) +{ + struct v4l2_control c = { .id = V4L2_CID_VBLANK, .value = *val }; + int ret; + + if (!sd || !sd->ctrl_handler) + return -ENOLINK; + if (set) + return v4l2_s_ctrl(NULL, sd->ctrl_handler, &c); + ret = v4l2_g_ctrl(sd->ctrl_handler, &c); + *val = c.value; + return ret; +} + +/* + * A frame-sync slave must run its source's frame length: a longer slave + * frame is cut short by the next sync pulse and fails the VIC transfer, + * and the slave's half-frame offset is derived from its own length. Set + * @length on @sensor and copy it to the slaves. Setting a slave keeps the + * source's length and returns it in @length. A shorter frame reaches the + * slaves first. + */ +static int t23_set_frame_length(struct t23_isp_video *video, + struct v4l2_subdev *sensor, s32 *length) +{ + s32 val = *length; + struct v4l2_subdev *source; + s32 current_val; + unsigned int i; + bool shorter; + int ret; + + if (video->sync_source < 0) + return t23_sensor_vblank(sensor, length, true); + source = video->inputs[video->sync_source].sd; + mutex_lock(&video->sync_lock); + ret = t23_sensor_vblank(source, ¤t_val, false); + if (ret) + goto out; + if (sensor != source) + val = current_val; + shorter = val < current_val; + if (!shorter) { + ret = t23_sensor_vblank(source, &val, true); + if (ret) + goto out; + } + for (i = 0; i < video->num_endpoints; i++) { + struct v4l2_subdev *slave = video->inputs[i].sd; + + if (i == video->sync_source || !slave) + continue; + ret = t23_sensor_vblank(slave, &val, true); + if (ret) + goto out; + } + if (shorter) + ret = t23_sensor_vblank(source, &val, true); + if (!ret) + *length = val; +out: + mutex_unlock(&video->sync_lock); + return ret; +} + +static int t23_sync_frame_length(struct t23_isp_video *video) +{ + struct v4l2_subdev *source; + unsigned int i; + s32 src_val; + int ret; + + if (video->sync_source < 0) + return 0; + source = video->inputs[video->sync_source].sd; + if (!source) + return 0; + + mutex_lock(&video->sync_lock); + ret = t23_sensor_vblank(source, &src_val, false); + if (ret) + goto out; + for (i = 0; i < video->num_endpoints; i++) { + struct v4l2_subdev *slave = video->inputs[i].sd; + s32 slave_val; + + if (i == video->sync_source || !slave) + continue; + if (!t23_sensor_vblank(slave, &slave_val, false) && + slave_val == src_val) + continue; + atomic_inc(&video->vblank_syncing); + ret = t23_sensor_vblank(slave, &src_val, true); + atomic_dec(&video->vblank_syncing); + if (ret) + goto out; + } +out: + mutex_unlock(&video->sync_lock); + return ret; +} + +static void t23_vblank_notify(struct v4l2_ctrl *ctrl, void *priv) +{ + struct t23_isp_video *video = priv; + + if (ctrl->id == V4L2_CID_VBLANK && !atomic_read(&video->vblank_syncing)) + queue_work(system_highpri_wq, &video->vblank_sync_work); +} + +static void t23_vblank_sync_work(struct work_struct *work) +{ + struct t23_isp_video *video = + container_of(work, struct t23_isp_video, vblank_sync_work); + + mutex_lock(&video->lock); + if (!video->disconnected && video->registered) + t23_sync_frame_length(video); + mutex_unlock(&video->lock); +} + +static void t23_sensor_vblank_watch(struct t23_isp_video *video, + struct v4l2_subdev *sd, bool enable) +{ + struct v4l2_ctrl *ctrl; + + if (video->sync_source < 0 || !sd || !sd->ctrl_handler) + return; + ctrl = v4l2_ctrl_find(sd->ctrl_handler, V4L2_CID_VBLANK); + if (!ctrl) + return; + mutex_lock(sd->ctrl_handler->lock); + if (enable) { + if (!ctrl->handler->notify) + v4l2_ctrl_notify(ctrl, t23_vblank_notify, video); + } else if (ctrl->handler->notify == t23_vblank_notify) { + v4l2_ctrl_notify(ctrl, NULL, NULL); + } + mutex_unlock(sd->ctrl_handler->lock); +} + +static int t23_control_set(struct v4l2_ctrl *ctrl) +{ + struct t23_video_node *chan = container_of(ctrl->handler, + struct t23_video_node, controls); + struct v4l2_subdev *sensor = t23_sensor(chan); + struct v4l2_control value = { .id = ctrl->id, .value = ctrl->val }; + + switch (ctrl->id) { + case V4L2_CID_HFLIP: + chan->hflip_val = ctrl->val; + if (chan->video->streaming && !chan->video->isp->processed && + sensor && sensor->ctrl_handler) + return v4l2_s_ctrl(NULL, sensor->ctrl_handler, &value); + break; + + case V4L2_CID_VFLIP: + chan->vflip_val = ctrl->val; + if (chan->video->streaming && !chan->video->isp->processed && + sensor && sensor->ctrl_handler) + return v4l2_s_ctrl(NULL, sensor->ctrl_handler, &value); + break; + + case V4L2_CID_DIGITAL_GAIN: + chan->dgain_val = ctrl->val; + break; + + case V4L2_CID_RED_BALANCE: + chan->red_val = ctrl->val; + break; + + case V4L2_CID_BLUE_BALANCE: + chan->blue_val = ctrl->val; + break; + + case V4L2_CID_BRIGHTNESS: + chan->brightness_val = ctrl->val; + break; + + case V4L2_CID_CONTRAST: + chan->contrast_val = ctrl->val; + break; + + case V4L2_CID_SATURATION: + chan->saturation_val = ctrl->val; + break; + + case V4L2_CID_SHARPNESS: + chan->sharpness_val = ctrl->val; + break; + + case V4L2_CID_GAMMA: + chan->gamma_val = ctrl->val; + t23_update_gamma_lut(chan); + break; + + default: + return -EINVAL; + } + return 0; +} + +static const struct v4l2_ctrl_ops t23_control_ops = { + .s_ctrl = t23_control_set, +}; + +static const struct { + u32 pixelformat; + const char *description; +} t23_formats[] = { + { V4L2_PIX_FMT_NV12, "Y/CbCr 4:2:0" }, + { V4L2_PIX_FMT_SBGGR10, "10-bit Bayer BGBG/GRGR" }, + { V4L2_PIX_FMT_SRGGB10, "10-bit Bayer RGRG/GBGB" }, + { V4L2_PIX_FMT_SGBRG10, "10-bit Bayer GBGB/RGRG" }, + { V4L2_PIX_FMT_SGRBG10, "10-bit Bayer GRGR/BGBG" }, + { V4L2_PIX_FMT_YUYV, "YUYV 4:2:2" }, + { V4L2_PIX_FMT_RGB24, "24-bit RGB 8-8-8" }, +}; + +static bool is_supported_format(u32 pixelformat) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(t23_formats); i++) { + if (t23_formats[i].pixelformat == pixelformat) + return true; + } + return false; +} + +static int t23_fill_pix_format(struct v4l2_pix_format *pix, u32 pixelformat, + u32 width, u32 height) +{ + memset(pix, 0, sizeof(*pix)); + pix->width = width; + pix->height = height; + pix->pixelformat = pixelformat; + pix->field = V4L2_FIELD_NONE; + + switch (pixelformat) { + case V4L2_PIX_FMT_SBGGR10: + case V4L2_PIX_FMT_SRGGB10: + case V4L2_PIX_FMT_SGBRG10: + case V4L2_PIX_FMT_SGRBG10: + if (check_mul_overflow(width, 2U, &pix->bytesperline) || + check_mul_overflow(pix->bytesperline, height, &pix->sizeimage)) + return -EOVERFLOW; + pix->colorspace = V4L2_COLORSPACE_RAW; + break; + case V4L2_PIX_FMT_NV12: + pix->bytesperline = width; + /* + * Pad allocation size to 16-row macroblock height so Helix + * zero-copy DMABUF reads never cross the end of the buffer. + */ + pix->sizeimage = (width * ALIGN(height, 16) * 3) / 2; + pix->colorspace = V4L2_COLORSPACE_SMPTE170M; + pix->quantization = V4L2_QUANTIZATION_FULL_RANGE; + break; + case V4L2_PIX_FMT_YUYV: + if (check_mul_overflow(width, 2U, &pix->bytesperline) || + check_mul_overflow(pix->bytesperline, height, &pix->sizeimage)) + return -EOVERFLOW; + pix->colorspace = V4L2_COLORSPACE_SMPTE170M; + pix->quantization = V4L2_QUANTIZATION_FULL_RANGE; + break; + case V4L2_PIX_FMT_RGB24: + if (check_mul_overflow(width, 3U, &pix->bytesperline) || + check_mul_overflow(pix->bytesperline, height, &pix->sizeimage)) + return -EOVERFLOW; + pix->colorspace = V4L2_COLORSPACE_SRGB; + break; + default: + return -EINVAL; + } + return 0; +} + +static u32 t23_chan_sink_code(const struct t23_video_node *chan) +{ + if (chan->input >= 0 && chan->video->inputs[chan->input].mbus_code) + return chan->video->inputs[chan->input].mbus_code; + return MEDIA_BUS_FMT_SBGGR10_1X10; +} + +static int t23_isp_subdev_get_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *fmt) +{ + struct t23_video_node *chan = v4l2_get_subdevdata(sd); + + if (fmt->pad >= 4 && fmt->pad < 4 + T23_SCALED_OUTPUTS) { + const struct t23_sub_node *sub = &chan->sub[fmt->pad - 4]; + + fmt->format.width = sub->pix.width; + fmt->format.height = sub->pix.height; + fmt->format.code = MEDIA_BUS_FMT_YUYV8_1X16; + fmt->format.colorspace = V4L2_COLORSPACE_SMPTE170M; + fmt->format.field = V4L2_FIELD_NONE; + return 0; + } + if (fmt->pad >= 2) + return -ENOTTY; + fmt->format.width = chan->pix.width; + fmt->format.height = chan->pix.height; + if (fmt->pad == 0) { + fmt->format.code = t23_chan_sink_code(chan); + fmt->format.colorspace = V4L2_COLORSPACE_RAW; + } else { + if (is_raw10_pixfmt(chan->pix.pixelformat)) { + fmt->format.code = t23_chan_sink_code(chan); + fmt->format.colorspace = V4L2_COLORSPACE_RAW; + } else if (chan->pix.pixelformat == V4L2_PIX_FMT_RGB24) { + fmt->format.code = MEDIA_BUS_FMT_RGB888_1X24; + fmt->format.colorspace = V4L2_COLORSPACE_SRGB; + } else { + fmt->format.code = MEDIA_BUS_FMT_YUYV8_1X16; + fmt->format.colorspace = V4L2_COLORSPACE_SMPTE170M; + } + } + fmt->format.field = V4L2_FIELD_NONE; + return 0; +} + +static int t23_isp_subdev_set_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *fmt) +{ + return t23_isp_subdev_get_fmt(sd, state, fmt); +} + +static int t23_isp_subdev_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state, + struct v4l2_subdev_mbus_code_enum *code) +{ + static const u32 sink_codes[] = { + MEDIA_BUS_FMT_SBGGR10_1X10, + MEDIA_BUS_FMT_SRGGB10_1X10, + MEDIA_BUS_FMT_SGBRG10_1X10, + MEDIA_BUS_FMT_SGRBG10_1X10, + }; + + if (code->pad == 0) { + if (code->index >= ARRAY_SIZE(sink_codes)) + return -EINVAL; + code->code = sink_codes[code->index]; + return 0; + } + if (code->pad == 1) { + static const u32 src_codes[] = { + MEDIA_BUS_FMT_YUYV8_1X16, + MEDIA_BUS_FMT_SBGGR10_1X10, + MEDIA_BUS_FMT_SRGGB10_1X10, + MEDIA_BUS_FMT_SGBRG10_1X10, + MEDIA_BUS_FMT_SGRBG10_1X10, + MEDIA_BUS_FMT_RGB888_1X24, + }; + if (code->index >= ARRAY_SIZE(src_codes)) + return -EINVAL; + code->code = src_codes[code->index]; + return 0; + } + if (code->pad >= 4 && code->pad < 4 + T23_SCALED_OUTPUTS) { + if (code->index > 0) + return -EINVAL; + code->code = MEDIA_BUS_FMT_YUYV8_1X16; + return 0; + } + return -ENOTTY; +} + +static int t23_isp_sd_subscribe_event(struct v4l2_subdev *sd, + struct v4l2_fh *fh, + struct v4l2_event_subscription *sub) +{ + if (sub->type == V4L2_EVENT_MOTION_DET) + return v4l2_event_subscribe(fh, sub, 8, NULL); + return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub); +} + +static const struct v4l2_subdev_core_ops t23_isp_sd_core_ops = { + .subscribe_event = t23_isp_sd_subscribe_event, + .unsubscribe_event = v4l2_event_subdev_unsubscribe, +}; + +static const struct v4l2_subdev_pad_ops t23_isp_sd_pad_ops = { + .get_fmt = t23_isp_subdev_get_fmt, + .set_fmt = t23_isp_subdev_set_fmt, + .enum_mbus_code = t23_isp_subdev_enum_mbus_code, +}; + +static const struct v4l2_subdev_ops t23_isp_sd_ops = { + .core = &t23_isp_sd_core_ops, + .pad = &t23_isp_sd_pad_ops, +}; + +static int t23_mbus_to_pix(const struct v4l2_mbus_framefmt *fmt, + struct v4l2_pix_format *pix) +{ + u32 fourcc; + + switch (fmt->code) { + case MEDIA_BUS_FMT_SBGGR10_1X10: + fourcc = V4L2_PIX_FMT_SBGGR10; + break; + case MEDIA_BUS_FMT_SRGGB10_1X10: + fourcc = V4L2_PIX_FMT_SRGGB10; + break; + case MEDIA_BUS_FMT_SGBRG10_1X10: + fourcc = V4L2_PIX_FMT_SGBRG10; + break; + case MEDIA_BUS_FMT_SGRBG10_1X10: + fourcc = V4L2_PIX_FMT_SGRBG10; + break; + default: + return -EINVAL; + } + if (!fmt->width || !fmt->height || fmt->field != V4L2_FIELD_NONE) + return -EINVAL; + return t23_fill_pix_format(pix, fourcc, fmt->width, fmt->height); +} + +static int t23_sensor_format(struct t23_isp_video *video, unsigned int input, + struct v4l2_pix_format *pix) +{ + struct v4l2_subdev_format fmt = { .which = V4L2_SUBDEV_FORMAT_ACTIVE }; + struct v4l2_subdev *sensor; + int ret; + + if (video->disconnected) + return -ENODEV; + if (input >= video->num_endpoints || !video->inputs[input].sd) + return -ENOLINK; + sensor = video->inputs[input].sd; + fmt.pad = video->inputs[input].pad; + ret = v4l2_subdev_call_state_active(sensor, pad, get_fmt, &fmt); + if (ret) + return ret; + video->inputs[input].mbus_code = fmt.format.code; + return t23_mbus_to_pix(&fmt.format, pix); +} + +/* + * MSCA channel 1 downscales by up to 8 in each direction. Widths are whole + * 16-pixel units and heights even, which keeps both planes 8-byte aligned. + */ +#define T23_SUB_MAX_DOWNSCALE 8 + +static void t23_sub_limits(u32 width, u32 height, struct v4l2_frmsize_stepwise *s) +{ + s->max_width = width & ~15U; + s->max_height = height & ~1U; + s->min_width = min(ALIGN(DIV_ROUND_UP(width, T23_SUB_MAX_DOWNSCALE), 16), + s->max_width); + s->min_height = min(ALIGN(DIV_ROUND_UP(height, T23_SUB_MAX_DOWNSCALE), 2), + s->max_height); + s->step_width = 16; + s->step_height = 2; +} + +static int t23_sub_format(struct t23_video_node *chan, u32 width, u32 height, + struct v4l2_pix_format *pix) +{ + struct v4l2_frmsize_stepwise limits; + struct v4l2_pix_format sensor; + int ret; + + ret = t23_sensor_format(chan->video, chan->input, &sensor); + if (ret) + return ret; + t23_sub_limits(sensor.width, sensor.height, &limits); + width = clamp(width, limits.min_width, limits.max_width) & ~15U; + height = clamp(height, limits.min_height, limits.max_height) & ~1U; + return t23_fill_pix_format(pix, V4L2_PIX_FMT_NV12, width, height); +} + +/* Per-node state for the main (MSCA channel 0) or scaled (channels 1-2) outputs */ +struct t23_output { + struct vb2_queue *queue; + struct list_head *bufs; + struct media_pad *pad; + bool *streaming; + bool *stopping; + unsigned int *skip_frames; + u32 bit; +}; + +static unsigned int t23_queue_output_idx(struct t23_video_node *chan, + struct vb2_queue *q) +{ + unsigned int s; + + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (q == &chan->sub[s].queue) + return s + 1; + return 0; +} + +static struct t23_output t23_output(struct t23_video_node *chan, + unsigned int idx) +{ + if (idx) { + struct t23_sub_node *sub = &chan->sub[idx - 1]; + + return (struct t23_output) { + &sub->queue, &sub->bufs, &sub->pad, + &sub->streaming, &sub->stopping, &sub->skip_frames, + BIT(idx), + }; + } + return (struct t23_output) { + &chan->queue, &chan->bufs, &chan->pad, + &chan->streaming, &chan->stopping, &chan->skip_frames, BIT(0), + }; +} + +/* Whether the frame in flight writes @out of @chan. */ +static bool t23_output_busy(struct t23_video_node *chan, + const struct t23_output *out) +{ + struct t23_isp_video *video = chan->video; + + return READ_ONCE(video->dma_active) && + READ_ONCE(video->active_channel) == chan->channel_idx && + (READ_ONCE(video->active_outputs) & out->bit); +} + +static void t23_return_output(struct t23_video_node *chan, + const struct t23_output *out, + enum vb2_buffer_state state) +{ + struct t23_isp_buffer *buf, *tmp; + unsigned long flags; + + spin_lock_irqsave(&chan->video->qlock, flags); + list_for_each_entry_safe(buf, tmp, out->bufs, list) { + list_del_init(&buf->list); + vb2_buffer_done(&buf->vb.vb2_buf, state); + } + spin_unlock_irqrestore(&chan->video->qlock, flags); +} + +static void t23_sensors_stop(struct t23_isp_video *video) +{ + while (video->num_streaming) { + unsigned int i = video->stream_order[--video->num_streaming]; + struct t23_video_input *input = &video->inputs[i]; + int ret; + + ret = v4l2_subdev_call(input->sd, video, s_stream, 0); + if (ret && ret != -ENOIOCTLCMD) + dev_err(video->dev, "Sensor %s failed to stop: %d\n", + input->sd->name, ret); + input->initialized = false; + } +} + +static int t23_sensor_start(struct t23_isp_video *video, unsigned int index) +{ + struct v4l2_subdev *sd = video->inputs[index].sd; + + if (!sd) + return -ENOLINK; + /* Include a failed start in the reverse-order shutdown. */ + video->stream_order[video->num_streaming++] = index; + video->inputs[index].initialized = true; + return v4l2_subdev_call(sd, video, s_stream, 1); +} + +static int t23_sensors_stream(struct t23_isp_video *video) +{ + unsigned int i; + int ret; + + if (video->sync_source >= 0) { + struct v4l2_subdev *source = video->inputs[video->sync_source].sd; + s32 val; + + /* Sensor sub-device nodes can set a slave's length directly. */ + ret = t23_sensor_vblank(source, &val, false); + if (!ret) + ret = t23_set_frame_length(video, source, &val); + if (ret) + return ret; + ret = t23_sensor_start(video, video->sync_source); + if (ret) + return ret; + } + for (i = 0; i < video->num_endpoints; i++) { + if (i == video->sync_source || !video->inputs[i].sd) + continue; + ret = t23_sensor_start(video, i); + if (ret) + return ret; + } + return 0; +} + +static void t23_pipeline_stop(struct t23_isp_video *video) +{ + struct tx_isp_device *isp = video->isp; + + if (!video->streaming) + return; + video->streaming = false; + if (video->irq_enabled) { + tx_isp_disable_irq(isp); + video->irq_enabled = false; + } + t23_sensors_stop(video); + if (isp->subdevs[TX_ISP_SUBDEV_VIC]) + tx_isp_vic_stop(isp->subdevs[TX_ISP_SUBDEV_VIC]); + video->dma_active = false; + if (isp->subdevs[TX_ISP_SUBDEV_CORE]) + tx_isp_core_s_stream(isp->subdevs[TX_ISP_SUBDEV_CORE], 0); + if (isp->subdevs[TX_ISP_SUBDEV_CSI]) + csi_phy_stop(isp->subdevs[TX_ISP_SUBDEV_CSI]); + isp->streaming = false; + video->active_channel = -1; +} + +static int t23_sensor_bus(struct v4l2_subdev *sd, unsigned int pad, + struct v4l2_mbus_config *bus, s64 *frequency) +{ + struct v4l2_ctrl *ctrl; + int index, ret; + + memset(bus, 0, sizeof(*bus)); + ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad, bus); + if (ret) + return ret; + if (bus->type != V4L2_MBUS_CSI2_DPHY || + bus->bus.mipi_csi2.num_data_lanes < 1 || + bus->bus.mipi_csi2.num_data_lanes > 2) + return -EINVAL; + ctrl = v4l2_ctrl_find(sd->ctrl_handler, V4L2_CID_LINK_FREQ); + if (!ctrl || ctrl->type != V4L2_CTRL_TYPE_INTEGER_MENU) + return -ENOLINK; + index = v4l2_ctrl_g_ctrl(ctrl); + if (index < ctrl->minimum || index > ctrl->maximum) + return -EINVAL; + *frequency = ctrl->qmenu_int[index]; + if (*frequency < 40000000 || *frequency >= 750000000) + return -EINVAL; + return 0; +} + +/* Start the shared pipeline; @processed selects ISP output over raw VIC. */ +static int t23_pipeline_start(struct t23_video_node *chan, bool processed) +{ + struct t23_isp_video *video = chan->video; + struct tx_isp_device *isp = video->isp; + struct v4l2_subdev *sensor = t23_sensor(chan); + struct v4l2_pix_format pix; + struct v4l2_mbus_config bus; + u32 active_inputs = 0; + s64 frequency; + size_t dma_size; + unsigned long flags; + unsigned int i; + int ret; + + if (video->disconnected || !video->registered || !isp) + return -ENODEV; + if (!sensor) + return -ENOLINK; + + ret = t23_sensor_format(video, chan->input, &pix); + if (ret) + return ret; + + if (!is_raw10_pixfmt(pix.pixelformat) || + (is_raw10_pixfmt(chan->pix.pixelformat) && + chan->pix.pixelformat != pix.pixelformat) || + pix.width != chan->pix.width || + pix.height != chan->pix.height) + return -EPIPE; + + ret = t23_sensor_bus(sensor, video->inputs[chan->input].pad, &bus, + &frequency); + if (ret) + return ret; + for (i = 0; i < video->num_endpoints; i++) { + struct v4l2_pix_format other; + struct v4l2_mbus_config other_bus; + s64 other_frequency; + + if (!video->inputs[i].sd) + continue; + active_inputs |= BIT(video->ep_asd[i]->port); + ret = t23_sensor_format(video, i, &other); + if (ret) + return ret; + if (other.width != pix.width || other.height != pix.height || + other.pixelformat != pix.pixelformat) + return -EPIPE; + ret = t23_sensor_bus(video->inputs[i].sd, + video->inputs[i].pad, &other_bus, + &other_frequency); + if (ret) + return ret; + if (other_bus.type != bus.type || + other_bus.bus.mipi_csi2.num_data_lanes != + bus.bus.mipi_csi2.num_data_lanes) + return -EPIPE; + } + chan->csi_clock_mhz = div_s64(frequency * 2, 1000000); + if (video->streaming) { + if (isp->processed != processed) + return -EBUSY; + if (video->dma_failed) + return -EIO; + return t23_sync_frame_length(video); + } + isp->processed = processed; + + /* Only VIC capture bounces through a driver buffer. */ + dma_size = (size_t)pix.width * pix.height * 2; + if (!isp->processed && (!isp->dma_buffer || dma_size > isp->dma_size)) { + void *new_buf; + dma_addr_t new_dma; + + new_buf = dma_alloc_coherent(video->dev, dma_size, &new_dma, + GFP_KERNEL); + if (!new_buf) + return -ENOMEM; + if (isp->dma_buffer) + dma_free_coherent(video->dev, isp->dma_size, + isp->dma_buffer, isp->dma_address); + isp->dma_buffer = new_buf; + isp->dma_address = new_dma; + isp->dma_size = dma_size; + } + + ret = t23_isp_configure_input( + isp, sensor, video->ep_asd[chan->input]->port, active_inputs); + if (ret) + goto pipeline_off; + + ret = csi_phy_start(isp->subdevs[TX_ISP_SUBDEV_CSI]); + if (ret) + goto pipeline_off; + + ret = tx_isp_core_s_stream(isp->subdevs[TX_ISP_SUBDEV_CORE], 1); + if (ret) + goto core_off; + + /* + * In processed mode MSCA 0xd050 mirrors and flips the YUV outputs, + * so keep the sensor Bayer pattern unflipped; in raw mode forward + * each channel's flip state to its sensor. + */ + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + struct v4l2_subdev *s = t23_sensor(&video->channels[i]); + struct v4l2_control hflip = { + .id = V4L2_CID_HFLIP, + .value = processed ? 0 : video->channels[i].hflip_val, + }; + struct v4l2_control vflip = { + .id = V4L2_CID_VFLIP, + .value = processed ? 0 : video->channels[i].vflip_val, + }; + + if (!s || !s->ctrl_handler) + continue; + v4l2_s_ctrl(NULL, s->ctrl_handler, &hflip); + v4l2_s_ctrl(NULL, s->ctrl_handler, &vflip); + } + + /* VIC reset needs the selected sensor's input clock. */ + ret = t23_sensors_stream(video); + if (ret) + goto core_off; + + ret = tx_isp_vic_start(isp->subdevs[TX_ISP_SUBDEV_VIC]); + if (ret) + goto core_off; + if (isp->processed) { + ret = t23_processed_prime(video, chan->channel_idx); + if (ret) + goto core_off; + } + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) + t23_motion_reset(&video->channels[i].motion); + + video->dma_failed = false; + video->streaming = true; + isp->streaming = true; + spin_lock_irqsave(&video->qlock, flags); + t23_capture_next(video); + spin_unlock_irqrestore(&video->qlock, flags); + + tx_isp_enable_irq(isp); + video->irq_enabled = true; + + return 0; + +core_off: + t23_sensors_stop(video); + tx_isp_vic_capture_trigger(isp->subdevs[TX_ISP_SUBDEV_VIC], false); + tx_isp_vic_stop(isp->subdevs[TX_ISP_SUBDEV_VIC]); + tx_isp_core_s_stream(isp->subdevs[TX_ISP_SUBDEV_CORE], 0); + csi_phy_stop(isp->subdevs[TX_ISP_SUBDEV_CSI]); +pipeline_off: + t23_sensors_stop(video); + return ret; +} + +static void t23_disconnect_outputs(struct t23_video_node *chan) +{ + unsigned int i; + + for (i = 0; i < T23_MSCA_OUTPUTS; i++) { + struct t23_output out = t23_output(chan, i); + + if (*out.streaming) + media_pipeline_stop(out.pad); + *out.streaming = false; + vb2_queue_error(out.queue); + t23_return_output(chan, &out, VB2_BUF_STATE_ERROR); + } +} + +static int t23_check_endpoint(const struct v4l2_fwnode_endpoint *ep) +{ + unsigned int lanes = ep->bus.mipi_csi2.num_data_lanes; + unsigned int i; + + if (ep->base.port >= T23_MAX_INPUTS || ep->base.id || + ep->bus_type != V4L2_MBUS_CSI2_DPHY || !lanes || lanes > 2 || + ep->bus.mipi_csi2.clock_lane) + return -EINVAL; + for (i = 0; i < lanes; i++) + if (ep->bus.mipi_csi2.data_lanes[i] != i + 1) + return -EINVAL; + for (i = 0; i <= lanes; i++) + if (ep->bus.mipi_csi2.lane_polarities[i]) + return -EINVAL; + return 0; +} + +static int t23_bound(struct v4l2_async_notifier *notifier, + struct v4l2_subdev *sd, struct v4l2_async_connection *asc) +{ + struct t23_isp_video *video = + container_of(notifier, struct t23_isp_video, notifier); + struct t23_async_subdev *endpoint = + container_of(asc, struct t23_async_subdev, asc); + struct t23_video_node *chan = &video->channels[endpoint->port]; + struct v4l2_pix_format sensor_pix; + unsigned int input = endpoint->index; + unsigned int pad, s; + int ret; + + if (input >= video->num_endpoints) + return -EINVAL; + for (pad = 0; pad < sd->entity.num_pads; pad++) + if (sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE) + break; + if (pad == sd->entity.num_pads || + sd->entity.function != MEDIA_ENT_F_CAM_SENSOR) + return -EINVAL; + mutex_lock(&video->lock); + video->inputs[input].sd = sd; + video->inputs[input].pad = pad; + chan->input = input; + ret = t23_sensor_format(video, input, &sensor_pix); + if (ret) + goto clear; + /* Default capture format on MC capture nodes is hardware-ISP NV12. */ + ret = t23_fill_pix_format(&chan->pix, V4L2_PIX_FMT_NV12, + sensor_pix.width, sensor_pix.height); + if (ret) + goto clear; + for (s = 0; s < T23_SCALED_OUTPUTS; s++) { + ret = t23_sub_format(chan, chan->pix.width / 2, + chan->pix.height / 2, &chan->sub[s].pix); + if (ret) + goto clear; + } + t23_overlay_set_frame(&video->overlay, chan->channel_idx, + chan->pix.width, chan->pix.height); + mutex_lock(&video->mdev.graph_mutex); + ret = media_create_pad_link(&sd->entity, pad, &chan->isp_sd.entity, 0, + MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE); + mutex_unlock(&video->mdev.graph_mutex); + if (!ret) { + t23_sensor_vblank_watch(video, sd, true); + t23_sync_frame_length(video); + mutex_unlock(&video->lock); + return 0; + } +clear: + chan->input = -1; + memset(&video->inputs[input], 0, sizeof(video->inputs[input])); + mutex_unlock(&video->lock); + return ret; +} + +static void t23_unbind(struct v4l2_async_notifier *notifier, + struct v4l2_subdev *sd, struct v4l2_async_connection *asc) +{ + struct t23_isp_video *video = + container_of(notifier, struct t23_isp_video, notifier); + unsigned int i; + + t23_sensor_vblank_watch(video, sd, false); + cancel_work_sync(&video->vblank_sync_work); + mutex_lock(&video->lock); + t23_pipeline_stop(video); + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + struct t23_video_node *chan = &video->channels[i]; + + t23_disconnect_outputs(chan); + if (t23_sensor(chan) == sd) { + memset(&video->inputs[chan->input], 0, + sizeof(video->inputs[chan->input])); + chan->input = -1; + memset(&chan->pix, 0, sizeof(chan->pix)); + } + } + mutex_unlock(&video->lock); +} + +static int t23_complete(struct v4l2_async_notifier *notifier) +{ + struct t23_isp_video *video = + container_of(notifier, struct t23_isp_video, notifier); + + return v4l2_device_register_subdev_nodes(&video->v4l2_dev); +} + +static const struct v4l2_async_notifier_operations t23_async_ops = { + .bound = t23_bound, + .complete = t23_complete, + .unbind = t23_unbind, +}; + +static int t23_queue_setup(struct vb2_queue *q, unsigned int *count, + unsigned int *planes, unsigned int sizes[], + struct device *devs[]) +{ + struct t23_video_node *chan = vb2_get_drv_priv(q); + struct t23_isp_video *video = chan->video; + unsigned int idx = t23_queue_output_idx(chan, q); + unsigned int size = idx ? chan->sub[idx - 1].pix.sizeimage : + chan->pix.sizeimage; + + if (!t23_sensor(chan)) + return video->disconnected ? -ENODEV : -ENOLINK; + if (!size) + return -EINVAL; + if (*planes) + return *planes == 1 && sizes[0] >= size ? 0 : -EINVAL; + *planes = 1; + sizes[0] = size; + *count = max_t(unsigned int, *count, 2); + return 0; +} + +static int t23_buf_prepare(struct vb2_buffer *vb) +{ + struct t23_video_node *chan = vb2_get_drv_priv(vb->vb2_queue); + struct t23_isp_video *video = chan->video; + unsigned int idx = t23_queue_output_idx(chan, vb->vb2_queue); + unsigned int size = idx ? chan->sub[idx - 1].pix.sizeimage : + chan->pix.sizeimage; + + if (video->disconnected) + return -ENODEV; + if (vb2_plane_size(vb, 0) < size) + return -EINVAL; + /* Only the main node CPU-converted formats are written through a mapping. */ + if (!idx && chan->pix.pixelformat != V4L2_PIX_FMT_NV12 && + !vb2_plane_vaddr(vb, 0)) + return -EFAULT; + vb2_set_plane_payload(vb, 0, 0); + return 0; +} + +static void t23_buf_queue(struct vb2_buffer *vb) +{ + struct t23_video_node *chan = vb2_get_drv_priv(vb->vb2_queue); + struct t23_isp_video *video = chan->video; + struct t23_isp_buffer *buf = + container_of(to_vb2_v4l2_buffer(vb), struct t23_isp_buffer, vb); + unsigned int idx = t23_queue_output_idx(chan, vb->vb2_queue); + struct list_head *bufs = idx ? &chan->sub[idx - 1].bufs : &chan->bufs; + unsigned long flags; + + spin_lock_irqsave(&video->qlock, flags); + list_add_tail(&buf->list, bufs); + t23_capture_next(video); + spin_unlock_irqrestore(&video->qlock, flags); +} + +static int t23_output_start(struct t23_video_node *chan, unsigned int idx, + bool processed) +{ + struct t23_output out = t23_output(chan, idx); + struct t23_isp_video *video = chan->video; + unsigned long flags; + int ret; + + if (IS_ERR(media_entity_remote_source_pad_unique(out.pad->entity))) { + ret = -ENOLINK; + goto buffers; + } + ret = media_pipeline_start(out.pad, &chan->pipe); + if (ret) + goto buffers; + if (video->irq_enabled) { + tx_isp_disable_irq(video->isp); + video->irq_enabled = false; + } + spin_lock_irqsave(&video->qlock, flags); + if (!t23_chan_streaming(chan)) + chan->sequence = 0; + *out.skip_frames = 2; + *out.stopping = false; + *out.streaming = true; + spin_unlock_irqrestore(&video->qlock, flags); + ret = t23_pipeline_start(chan, processed); + if (ret) { + spin_lock_irqsave(&video->qlock, flags); + *out.streaming = false; + spin_unlock_irqrestore(&video->qlock, flags); + media_pipeline_stop(out.pad); + if (video->streaming) { + tx_isp_enable_irq(video->isp); + video->irq_enabled = true; + } + goto buffers; + } + spin_lock_irqsave(&video->qlock, flags); + t23_capture_next(video); + spin_unlock_irqrestore(&video->qlock, flags); + if (!video->irq_enabled) { + tx_isp_enable_irq(video->isp); + video->irq_enabled = true; + } + return 0; +buffers: + t23_return_output(chan, &out, VB2_BUF_STATE_QUEUED); + return ret; +} + +static void t23_output_stop(struct t23_video_node *chan, unsigned int idx) +{ + struct t23_output out = t23_output(chan, idx); + struct t23_isp_video *video = chan->video; + unsigned long flags; + unsigned int i, s; + bool running = false, other_running = false, complete_parked = false; + + spin_lock_irqsave(&video->qlock, flags); + *out.stopping = true; + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + const struct t23_video_node *c = &video->channels[i]; + + if (c->streaming && !(i == chan->channel_idx && idx == 0)) + other_running = true; + for (s = 0; s < T23_SCALED_OUTPUTS; s++) + if (c->sub[s].streaming && + !(i == chan->channel_idx && idx == s + 1)) + other_running = true; + } + spin_unlock_irqrestore(&video->qlock, flags); + + /* Bounded 2-frame drain (~200 ms) so an in-flight frame finishes cleanly. */ + wait_event_timeout(video->dma_wait, !t23_output_busy(chan, &out), + msecs_to_jiffies(200)); + + /* Drain the threaded completion before returning its destination buffer. */ + if (video->irq_enabled) { + tx_isp_disable_irq(video->isp); + video->irq_enabled = false; + } + if (video->isp && video->isp->processed && + t23_output_busy(chan, &out) && !other_running) + t23_isp_video_processed_event(video->isp, false, false, true); + spin_lock_irqsave(&video->qlock, flags); + if (video->isp && t23_output_busy(chan, &out)) { + if (video->isp->processed && other_running) { + struct tx_isp_subdev *core_sd = + video->isp->subdevs[TX_ISP_SUBDEV_CORE]; + struct tx_isp_core_device *core = + core_sd ? core_sd->dev_priv : NULL; + + /* + * Park this single MSCA channel onto the 4 KiB scratch + * page (stride=0) without disturbing active streams. + */ + tx_isp_core_park_channel(core_sd, idx, + video->isp->scratch_dma); + video->active_outputs &= ~out.bit; + if (!video->active_outputs || + (core && core->armed_mask && + core->done_mask == core->armed_mask)) { + video->processed_dma_done = true; + if (video->processed_all_done) + complete_parked = true; + } + } else { + tx_isp_vic_dma_stop(video->isp->subdevs[TX_ISP_SUBDEV_VIC]); + video->dma_active = false; + video->active_channel = -1; + } + } + if (*out.streaming) { + *out.streaming = false; + spin_unlock_irqrestore(&video->qlock, flags); + media_pipeline_stop(out.pad); + spin_lock_irqsave(&video->qlock, flags); + } + if (complete_parked) { + spin_unlock_irqrestore(&video->qlock, flags); + t23_isp_video_processed_event(video->isp, true, true, false); + spin_lock_irqsave(&video->qlock, flags); + } + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) + running |= t23_chan_streaming(&video->channels[i]); + if (running) + t23_capture_next(video); + spin_unlock_irqrestore(&video->qlock, flags); + if (running) { + tx_isp_enable_irq(video->isp); + video->irq_enabled = true; + } else if (video->isp) { + t23_pipeline_stop(video); + } + t23_return_output(chan, &out, VB2_BUF_STATE_ERROR); +} + +static int t23_start_streaming(struct vb2_queue *q, unsigned int count) +{ + struct t23_video_node *chan = vb2_get_drv_priv(q); + unsigned int idx = t23_queue_output_idx(chan, q); + bool processed = idx || (chan->pix.pixelformat == V4L2_PIX_FMT_NV12); + + return t23_output_start(chan, idx, processed); +} + +static void t23_stop_streaming(struct vb2_queue *q) +{ + struct t23_video_node *chan = vb2_get_drv_priv(q); + unsigned int idx = t23_queue_output_idx(chan, q); + + t23_output_stop(chan, idx); +} + +static const struct vb2_ops t23_vb2_ops = { + .queue_setup = t23_queue_setup, + .buf_prepare = t23_buf_prepare, + .buf_queue = t23_buf_queue, + .start_streaming = t23_start_streaming, + .stop_streaming = t23_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int t23_sub_querycap(struct file *file, void *priv, + struct v4l2_capability *cap) +{ + struct t23_sub_node *sub = + container_of(video_devdata(file), struct t23_sub_node, vdev); + struct t23_video_node *chan = video_drvdata(file); + + strscpy(cap->driver, "tx-isp", sizeof(cap->driver)); + if (sub->index) + snprintf(cap->card, sizeof(cap->card), "T23 scaled ch%u.%u", + chan->channel_idx, sub->index); + else + snprintf(cap->card, sizeof(cap->card), "T23 scaled ch%u", + chan->channel_idx); + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", + dev_name(chan->video->dev)); + return 0; +} + +static int t23_sub_enum_fmt(struct file *file, void *priv, + struct v4l2_fmtdesc *f) +{ + if (f->index) + return -EINVAL; + f->pixelformat = V4L2_PIX_FMT_NV12; + strscpy(f->description, "Y/CbCr 4:2:0", sizeof(f->description)); + return 0; +} + +static int t23_sub_g_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct t23_sub_node *sub = + container_of(video_devdata(file), struct t23_sub_node, vdev); + + f->fmt.pix = sub->pix; + return 0; +} + +static int t23_sub_try_fmt(struct file *file, void *priv, + struct v4l2_format *f) +{ + struct t23_video_node *chan = video_drvdata(file); + + return t23_sub_format(chan, f->fmt.pix.width, f->fmt.pix.height, + &f->fmt.pix); +} + +static int t23_sub_s_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct t23_sub_node *sub = + container_of(video_devdata(file), struct t23_sub_node, vdev); + int ret; + + if (vb2_is_busy(&sub->queue)) + return -EBUSY; + ret = t23_sub_try_fmt(file, priv, f); + if (!ret) + sub->pix = f->fmt.pix; + return ret; +} + +static int t23_sub_enum_framesizes(struct file *file, void *priv, + struct v4l2_frmsizeenum *f) +{ + struct t23_video_node *chan = video_drvdata(file); + struct v4l2_pix_format sensor; + + if (f->index || f->pixel_format != V4L2_PIX_FMT_NV12 || + t23_sensor_format(chan->video, chan->input, &sensor)) + return -EINVAL; + f->type = V4L2_FRMSIZE_TYPE_STEPWISE; + t23_sub_limits(sensor.width, sensor.height, &f->stepwise); + return 0; +} + +static int t23_frame_interval(struct t23_video_node *chan, + struct v4l2_frmivalenum *f) +{ + struct v4l2_subdev_frame_interval interval = { + .which = V4L2_SUBDEV_FORMAT_ACTIVE, + }; + struct v4l2_subdev *sensor = t23_sensor(chan); + int ret; + + if (!sensor) + return -EINVAL; + interval.pad = chan->video->inputs[chan->input].pad; + ret = v4l2_subdev_call_state_active(sensor, pad, get_frame_interval, + &interval); + if (ret) + return ret; + f->type = V4L2_FRMIVAL_TYPE_DISCRETE; + f->discrete = interval.interval; + return 0; +} + +static int t23_sub_enum_frameintervals(struct file *file, void *priv, + struct v4l2_frmivalenum *f) +{ + struct t23_video_node *chan = video_drvdata(file); + struct v4l2_pix_format pix; + + if (f->index || f->pixel_format != V4L2_PIX_FMT_NV12 || + t23_sub_format(chan, f->width, f->height, &pix) || + pix.width != f->width || pix.height != f->height) + return -EINVAL; + return t23_frame_interval(chan, f); +} + +static int t23_subscribe_event(struct v4l2_fh *fh, + const struct v4l2_event_subscription *sub) +{ + if (sub->type == V4L2_EVENT_MOTION_DET) + return v4l2_event_subscribe(fh, sub, 8, NULL); + return v4l2_ctrl_subscribe_event(fh, sub); +} + +static int t23_querycap(struct file *file, void *priv, + struct v4l2_capability *cap) +{ + struct video_device *vdev = video_devdata(file); + struct t23_video_node *chan = video_get_drvdata(vdev); + struct t23_isp_video *video = chan->video; + + strscpy(cap->driver, "tx-isp", sizeof(cap->driver)); + snprintf(cap->card, sizeof(cap->card), "T23 capture ch%u", chan->channel_idx); + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", + dev_name(video->dev)); + return 0; +} + +static int t23_enum_fmt(struct file *file, void *priv, struct v4l2_fmtdesc *f) +{ + if (f->index >= ARRAY_SIZE(t23_formats)) + return -EINVAL; + f->pixelformat = t23_formats[f->index].pixelformat; + strscpy(f->description, t23_formats[f->index].description, sizeof(f->description)); + return 0; +} + +static int t23_g_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct video_device *vdev = video_devdata(file); + struct t23_video_node *chan = video_get_drvdata(vdev); + + f->fmt.pix = chan->pix; + return 0; +} + +static int t23_try_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct t23_video_node *chan = video_get_drvdata(video_devdata(file)); + struct t23_isp_video *video = chan->video; + struct v4l2_pix_format sensor_pix; + u32 pixelformat; + int ret; + + if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + ret = t23_sensor_format(video, chan->input, &sensor_pix); + if (ret) + return ret; + + pixelformat = f->fmt.pix.pixelformat; + if (!is_supported_format(pixelformat)) + pixelformat = V4L2_PIX_FMT_NV12; + else if (is_raw10_pixfmt(pixelformat)) + pixelformat = sensor_pix.pixelformat; + + return t23_fill_pix_format(&f->fmt.pix, pixelformat, sensor_pix.width, sensor_pix.height); +} + +static int t23_s_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct t23_video_node *chan = video_get_drvdata(video_devdata(file)); + int ret; + + if (vb2_is_busy(&chan->queue)) + return -EBUSY; + ret = t23_try_fmt(file, priv, f); + if (ret) + return ret; + chan->pix = f->fmt.pix; + return 0; +} + +static int t23_enum_framesizes(struct file *file, void *priv, + struct v4l2_frmsizeenum *f) +{ + struct video_device *vdev = video_devdata(file); + struct t23_video_node *chan = video_get_drvdata(vdev); + + if (f->index || !t23_sensor(chan) || + !is_supported_format(f->pixel_format)) + return -EINVAL; + f->type = V4L2_FRMSIZE_TYPE_DISCRETE; + f->discrete.width = chan->pix.width; + f->discrete.height = chan->pix.height; + return 0; +} + +static int t23_enum_frameintervals(struct file *file, void *priv, + struct v4l2_frmivalenum *f) +{ + struct t23_video_node *chan = video_drvdata(file); + + if (f->index || !is_supported_format(f->pixel_format) || + f->width != chan->pix.width || f->height != chan->pix.height) + return -EINVAL; + return t23_frame_interval(chan, f); +} + +static int t23_g_parm(struct file *file, void *priv, struct v4l2_streamparm *a) +{ + struct t23_video_node *chan = video_drvdata(file); + struct v4l2_subdev_frame_interval interval = { + .which = V4L2_SUBDEV_FORMAT_ACTIVE, + }; + struct v4l2_subdev *sensor = t23_sensor(chan); + struct v4l2_fract tpf = { 1, 15 }; + + if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + + if (sensor) { + interval.pad = chan->video->inputs[chan->input].pad; + if (!v4l2_subdev_call_state_active(sensor, pad, + get_frame_interval, + &interval) && + interval.interval.numerator && + interval.interval.denominator) + tpf = interval.interval; + } + + memset(&a->parm.capture, 0, sizeof(a->parm.capture)); + a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; + a->parm.capture.timeperframe = tpf; + return 0; +} + +static int t23_s_parm(struct file *file, void *priv, struct v4l2_streamparm *a) +{ + struct t23_video_node *chan = video_drvdata(file); + struct v4l2_subdev *sensor = t23_sensor(chan); + + if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + + if (sensor && a->parm.capture.timeperframe.numerator && + a->parm.capture.timeperframe.denominator) { + struct v4l2_subdev_frame_interval interval = { + .which = V4L2_SUBDEV_FORMAT_ACTIVE, + .pad = chan->video->inputs[chan->input].pad, + .interval = a->parm.capture.timeperframe, + }; + int ret; + + ret = v4l2_subdev_call_state_active(sensor, pad, + set_frame_interval, + &interval); + if (ret && ret != -ENOIOCTLCMD) + return ret; + } + + return t23_g_parm(file, priv, a); +} + +static const struct v4l2_ioctl_ops t23_ioctl_ops = { + .vidioc_querycap = t23_querycap, + .vidioc_enum_fmt_vid_cap = t23_enum_fmt, + .vidioc_g_fmt_vid_cap = t23_g_fmt, + .vidioc_s_fmt_vid_cap = t23_s_fmt, + .vidioc_try_fmt_vid_cap = t23_try_fmt, + .vidioc_enum_framesizes = t23_enum_framesizes, + .vidioc_enum_frameintervals = t23_enum_frameintervals, + .vidioc_g_parm = t23_g_parm, + .vidioc_s_parm = t23_s_parm, + .vidioc_reqbufs = vb2_ioctl_reqbufs, + .vidioc_create_bufs = vb2_ioctl_create_bufs, + .vidioc_prepare_buf = vb2_ioctl_prepare_buf, + .vidioc_querybuf = vb2_ioctl_querybuf, + .vidioc_qbuf = vb2_ioctl_qbuf, + .vidioc_dqbuf = vb2_ioctl_dqbuf, + .vidioc_expbuf = vb2_ioctl_expbuf, + .vidioc_streamon = vb2_ioctl_streamon, + .vidioc_streamoff = vb2_ioctl_streamoff, + .vidioc_subscribe_event = t23_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, +}; + +static const struct v4l2_ioctl_ops t23_sub_ioctl_ops = { + .vidioc_querycap = t23_sub_querycap, + .vidioc_enum_fmt_vid_cap = t23_sub_enum_fmt, + .vidioc_g_fmt_vid_cap = t23_sub_g_fmt, + .vidioc_s_fmt_vid_cap = t23_sub_s_fmt, + .vidioc_try_fmt_vid_cap = t23_sub_try_fmt, + .vidioc_enum_framesizes = t23_sub_enum_framesizes, + .vidioc_enum_frameintervals = t23_sub_enum_frameintervals, + .vidioc_g_parm = t23_g_parm, + .vidioc_s_parm = t23_s_parm, + .vidioc_reqbufs = vb2_ioctl_reqbufs, + .vidioc_create_bufs = vb2_ioctl_create_bufs, + .vidioc_prepare_buf = vb2_ioctl_prepare_buf, + .vidioc_querybuf = vb2_ioctl_querybuf, + .vidioc_qbuf = vb2_ioctl_qbuf, + .vidioc_dqbuf = vb2_ioctl_dqbuf, + .vidioc_expbuf = vb2_ioctl_expbuf, + .vidioc_streamon = vb2_ioctl_streamon, + .vidioc_streamoff = vb2_ioctl_streamoff, + .vidioc_subscribe_event = t23_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, +}; + +static const struct v4l2_file_operations t23_fops = { + .owner = THIS_MODULE, + .open = v4l2_fh_open, + .release = vb2_fop_release, + .poll = vb2_fop_poll, + .mmap = vb2_fop_mmap, + .unlocked_ioctl = video_ioctl2, +}; + +static void t23_video_release(struct v4l2_device *v4l2_dev) +{ + struct t23_isp_video *video = + container_of(v4l2_dev, struct t23_isp_video, v4l2_dev); + unsigned int ch, s; + + for (ch = 0; ch < T23_CAPTURE_CHANNELS; ch++) { + struct t23_video_node *chan = &video->channels[ch]; + + for (s = 0; s < T23_SCALED_OUTPUTS; s++) { + if (chan->sub[s].queue_initialized) + vb2_queue_release(&chan->sub[s].queue); + if (chan->sub[s].entity_initialized) + media_entity_cleanup(&chan->sub[s].vdev.entity); + } + if (chan->queue_initialized) + vb2_queue_release(&chan->queue); + if (chan->entity_initialized) + media_entity_cleanup(&chan->vdev.entity); + if (chan->isp_sd_registered) { + v4l2_device_unregister_subdev(&chan->isp_sd); + chan->isp_sd_registered = false; + } + if (chan->isp_sd_finalized) { + v4l2_subdev_cleanup(&chan->isp_sd); + media_entity_cleanup(&chan->isp_sd.entity); + chan->isp_sd_finalized = false; + } + v4l2_ctrl_handler_free(&chan->controls); + } + t23_meta_release(&video->meta); + t23_overlay_release(&video->overlay); + media_device_cleanup(&video->mdev); + put_device(video->dev); + kfree(video); +} + +static void t23_media_release(struct media_devnode *node) +{ + struct t23_isp_video *video = dev_get_drvdata(&node->dev); + + if (video->media_release) + video->media_release(node); + v4l2_device_put(&video->v4l2_dev); +} + +/* The scaled nodes follow the main node's pipeline. */ +static int t23_sub_register(struct t23_video_node *chan, unsigned int idx) +{ + struct t23_isp_video *video = chan->video; + struct t23_sub_node *sub = &chan->sub[idx]; + struct vb2_queue *q = &sub->queue; + int ret; + + sub->index = idx; + INIT_LIST_HEAD(&sub->bufs); + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + q->io_modes = VB2_MMAP | VB2_DMABUF; + q->drv_priv = chan; + q->ops = &t23_vb2_ops; + q->mem_ops = &vb2_dma_contig_memops; + q->buf_struct_size = sizeof(struct t23_isp_buffer); + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + q->min_queued_buffers = 2; + q->lock = &video->lock; + q->dev = video->dev; + ret = vb2_queue_init(q); + if (ret) + return ret; + sub->queue_initialized = true; + + sub->vdev = (struct video_device){ + .fops = &t23_fops, + .ioctl_ops = &t23_sub_ioctl_ops, + .release = video_device_release_empty, + .v4l2_dev = &video->v4l2_dev, + .queue = q, + .lock = &video->lock, + .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | + V4L2_CAP_IO_MC, + }; + if (idx) + snprintf(sub->vdev.name, sizeof(sub->vdev.name), + "T23 scaled%u.%u", chan->channel_idx, idx); + else + snprintf(sub->vdev.name, sizeof(sub->vdev.name), + "T23 scaled%u", chan->channel_idx); + sub->vdev.entity.function = MEDIA_ENT_F_IO_V4L; + sub->pad.flags = MEDIA_PAD_FL_SINK; + ret = media_entity_pads_init(&sub->vdev.entity, 1, &sub->pad); + if (ret) + return ret; + sub->entity_initialized = true; + video_set_drvdata(&sub->vdev, chan); + ret = video_register_device(&sub->vdev, VFL_TYPE_VIDEO, -1); + if (ret) + return ret; + sub->vdev_registered = true; + return media_create_pad_link(&chan->isp_sd.entity, 4 + idx, + &sub->vdev.entity, 0, + MEDIA_LNK_FL_ENABLED); +} + +static bool t23_overlay_sync(struct t23_overlay *overlay, unsigned int input) +{ + struct t23_isp_video *video = + container_of(overlay, struct t23_isp_video, overlay); + + if (!wait_event_timeout(video->dma_wait, + !READ_ONCE(video->dma_active) || + READ_ONCE(video->active_channel) != input, + msecs_to_jiffies(500))) { + WRITE_ONCE(video->dma_failed, true); + return false; + } + return true; +} + +int t23_isp_video_register(struct tx_isp_device *isp) +{ + struct t23_isp_video *video; + struct media_entity *isp_entities[T23_CAPTURE_CHANNELS]; + struct fwnode_handle *ep_node; + unsigned int i, j; + u32 sync_port; + int ret; + + mutex_lock(&t23_video_lock); + if (t23_video) { + ret = -EBUSY; + goto unlock; + } + video = kzalloc(sizeof(*video), GFP_KERNEL); + if (!video) { + ret = -ENOMEM; + goto unlock; + } + video->dev = get_device(&isp->pdev->dev); + video->isp = isp; + video->active_channel = -1; + spin_lock_init(&video->qlock); + init_waitqueue_head(&video->dma_wait); + video->sync_source = -1; + mutex_init(&video->lock); + mutex_init(&video->sync_lock); + INIT_WORK(&video->vblank_sync_work, t23_vblank_sync_work); + atomic_set(&video->vblank_syncing, 0); + t23_overlay_init(&video->overlay, video->dev, t23_overlay_sync); + + video->mdev.dev = video->dev; + strscpy(video->mdev.model, "T23 capture", sizeof(video->mdev.model)); + media_device_init(&video->mdev); + ret = v4l2_device_register(video->dev, &video->v4l2_dev); + if (ret) { + media_device_cleanup(&video->mdev); + put_device(video->dev); + kfree(video); + goto unlock; + } + video->v4l2_dev.release = t23_video_release; + video->v4l2_dev.mdev = &video->mdev; + + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + struct t23_video_node *chan = &video->channels[i]; + struct vb2_queue *q = &chan->queue; + + chan->video = video; + chan->channel_idx = i; + chan->input = -1; + mutex_init(&chan->isp_sd_lock); + + v4l2_ctrl_handler_init(&chan->controls, 24); + chan->controls.lock = &chan->isp_sd_lock; + if (chan->controls.error) { + ret = chan->controls.error; + goto fail_nodes; + } + + /* ISP native controls (exposed exclusively on t23-isp-coreN) */ + chan->hflip = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_HFLIP, 0, 1, 1, 0); + chan->vflip = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_VFLIP, 0, 1, 1, 0); + chan->dgain = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_DIGITAL_GAIN, 256, 16383, 1, 1024); + chan->red_balance = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_RED_BALANCE, 0, 1024, 1, 256); + chan->blue_balance = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_BLUE_BALANCE, 0, 1024, 1, 256); + chan->brightness = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_BRIGHTNESS, -128, 127, 1, 0); + chan->contrast = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_CONTRAST, 0, 255, 1, 128); + chan->saturation = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_SATURATION, 0, 255, 1, 128); + chan->sharpness = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_SHARPNESS, 0, 255, 1, 128); + chan->gamma_ctrl = v4l2_ctrl_new_std(&chan->controls, &t23_control_ops, + V4L2_CID_GAMMA, 70, 180, 1, 100); + t23_motion_init(&chan->motion, &chan->controls, &chan->isp_sd, + &chan->vdev, &chan->sub[0].vdev, + &chan->sub[1].vdev); + t23_overlay_init_controls(&video->overlay, i, &chan->controls); + + if (chan->controls.error) { + ret = chan->controls.error; + goto fail_nodes; + } + + chan->dgain_val = 1024; + chan->red_val = 256; + chan->blue_val = 256; + chan->brightness_val = 0; + chan->contrast_val = 128; + chan->saturation_val = 128; + chan->sharpness_val = 128; + chan->gamma_val = 100; + t23_update_gamma_lut(chan); + + INIT_LIST_HEAD(&chan->bufs); + + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + q->io_modes = VB2_MMAP | VB2_DMABUF; + q->drv_priv = chan; + q->ops = &t23_vb2_ops; + q->mem_ops = &vb2_dma_contig_memops; + q->buf_struct_size = sizeof(struct t23_isp_buffer); + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + q->min_queued_buffers = 2; + q->lock = &video->lock; + q->dev = video->dev; + ret = vb2_queue_init(q); + if (ret) + goto fail_nodes; + chan->queue_initialized = true; + + chan->vdev = (struct video_device){ + .fops = &t23_fops, + .ioctl_ops = &t23_ioctl_ops, + .release = video_device_release_empty, + .v4l2_dev = &video->v4l2_dev, + .queue = q, + .lock = &video->lock, + .device_caps = V4L2_CAP_VIDEO_CAPTURE | + V4L2_CAP_STREAMING | V4L2_CAP_IO_MC, + }; + snprintf(chan->vdev.name, sizeof(chan->vdev.name), "T23 video%u", i); + chan->vdev.entity.function = MEDIA_ENT_F_IO_V4L; + chan->pad.flags = MEDIA_PAD_FL_SINK; + ret = media_entity_pads_init(&chan->vdev.entity, 1, &chan->pad); + if (ret) + goto fail_nodes; + chan->entity_initialized = true; + video_set_drvdata(&chan->vdev, chan); + + ret = video_register_device(&chan->vdev, VFL_TYPE_VIDEO, -1); + if (ret) + goto fail_nodes; + chan->vdev_registered = true; + + /* Initialize per-channel ISP subdev */ + v4l2_subdev_init(&chan->isp_sd, &t23_isp_sd_ops); + snprintf(chan->isp_sd.name, sizeof(chan->isp_sd.name), "t23-isp-core%u", i); + chan->isp_sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | + V4L2_SUBDEV_FL_HAS_EVENTS; + chan->isp_sd.state_lock = &chan->isp_sd_lock; + chan->isp_sd.ctrl_handler = &chan->controls; + chan->isp_sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; + chan->isp_pads[0].flags = MEDIA_PAD_FL_SINK; + chan->isp_pads[1].flags = MEDIA_PAD_FL_SOURCE; + chan->isp_pads[2].flags = MEDIA_PAD_FL_SINK; + chan->isp_pads[3].flags = MEDIA_PAD_FL_SOURCE; + for (j = 0; j < T23_SCALED_OUTPUTS; j++) + chan->isp_pads[4 + j].flags = MEDIA_PAD_FL_SOURCE; + for (j = 4 + T23_SCALED_OUTPUTS; j < ARRAY_SIZE(chan->isp_pads); j++) + chan->isp_pads[j].flags = MEDIA_PAD_FL_SINK; + ret = media_entity_pads_init(&chan->isp_sd.entity, + ARRAY_SIZE(chan->isp_pads), + chan->isp_pads); + if (ret) + goto fail_nodes; + v4l2_set_subdevdata(&chan->isp_sd, chan); + ret = v4l2_subdev_init_finalize(&chan->isp_sd); + if (ret) { + media_entity_cleanup(&chan->isp_sd.entity); + goto fail_nodes; + } + chan->isp_sd_finalized = true; + ret = v4l2_device_register_subdev(&video->v4l2_dev, &chan->isp_sd); + if (ret) + goto fail_nodes; + chan->isp_sd_registered = true; + + /* Link chan->isp_sd pad 1 (SOURCE) to chan->vdev.entity pad 0 (SINK) */ + ret = media_create_pad_link(&chan->isp_sd.entity, 1, + &chan->vdev.entity, 0, + MEDIA_LNK_FL_ENABLED); + if (ret) + goto fail_nodes; + isp_entities[i] = &chan->isp_sd.entity; + } + BUILD_BUG_ON(T23_META_INPUTS != T23_CAPTURE_CHANNELS); + video->meta.dev = video->dev; + ret = t23_meta_register(&video->meta, &video->v4l2_dev, &video->lock, + isp_entities, 2, 3); + if (ret) + goto fail_nodes; + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + for (j = 0; j < T23_SCALED_OUTPUTS; j++) { + ret = t23_sub_register(&video->channels[i], j); + if (ret) + goto fail_nodes; + } + } + BUILD_BUG_ON(T23_OVERLAY_INPUTS != T23_CAPTURE_CHANNELS); + ret = t23_overlay_register(&video->overlay, &video->v4l2_dev, + &video->lock, isp_entities, + 4 + T23_SCALED_OUTPUTS); + if (ret) + goto fail_nodes; + + v4l2_async_nf_init(&video->notifier, &video->v4l2_dev); + video->notifier.ops = &t23_async_ops; + fwnode_graph_for_each_endpoint(dev_fwnode(video->dev), ep_node) { + struct v4l2_fwnode_endpoint ep = { + .bus_type = V4L2_MBUS_CSI2_DPHY, + }; + struct t23_async_subdev *t23_asd; + + ret = v4l2_fwnode_endpoint_parse(ep_node, &ep); + if (ret) { + fwnode_handle_put(ep_node); + goto notifier; + } + ret = t23_check_endpoint(&ep); + if (ret) { + fwnode_handle_put(ep_node); + goto notifier; + } + for (j = 0; j < video->num_endpoints; j++) { + if (video->ep_asd[j]->port == ep.base.port) { + fwnode_handle_put(ep_node); + ret = -EINVAL; + goto notifier; + } + } + if (video->num_endpoints >= T23_MAX_INPUTS) { + fwnode_handle_put(ep_node); + ret = -EINVAL; + goto notifier; + } + t23_asd = v4l2_async_nf_add_fwnode_remote(&video->notifier, + ep_node, + struct t23_async_subdev); + if (IS_ERR(t23_asd)) { + fwnode_handle_put(ep_node); + ret = PTR_ERR(t23_asd); + goto notifier; + } + t23_asd->port = ep.base.port; + t23_asd->index = video->num_endpoints; + video->ep_asd[video->num_endpoints++] = t23_asd; + } + if (device_property_present(video->dev, "ingenic,frame-sync-source-port")) { + ret = device_property_read_u32(video->dev, + "ingenic,frame-sync-source-port", &sync_port); + if (ret) + goto notifier; + for (i = 0; i < video->num_endpoints; i++) { + if (video->ep_asd[i]->port == sync_port) { + video->sync_source = i; + break; + } + } + if (video->sync_source < 0) { + ret = -EINVAL; + goto notifier; + } + } + ret = v4l2_async_nf_register(&video->notifier); + if (ret) + goto notifier; + ret = media_device_register(&video->mdev); + if (ret) + goto async; + video->media_registered = true; + v4l2_device_get(&video->v4l2_dev); + dev_set_drvdata(&video->mdev.devnode->dev, video); + video->media_release = video->mdev.devnode->release; + video->mdev.devnode->release = t23_media_release; + mutex_lock(&video->lock); + isp->video_priv = video; + video->registered = true; + mutex_unlock(&video->lock); + t23_video = video; + mutex_unlock(&t23_video_lock); + return 0; + +async: + v4l2_async_nf_unregister(&video->notifier); + cancel_work_sync(&video->vblank_sync_work); +notifier: + v4l2_async_nf_cleanup(&video->notifier); +fail_nodes: + t23_overlay_unregister(&video->overlay); + t23_meta_unregister(&video->meta); + for (i = 0; i < T23_CAPTURE_CHANNELS; i++) { + struct t23_video_node *chan = &video->channels[i]; + + for (j = 0; j < T23_SCALED_OUTPUTS; j++) + if (chan->sub[j].vdev_registered) + video_unregister_device(&chan->sub[j].vdev); + if (chan->vdev_registered) + video_unregister_device(&chan->vdev); + if (chan->isp_sd_registered) { + v4l2_device_unregister_subdev(&chan->isp_sd); + chan->isp_sd_registered = false; + } + if (chan->isp_sd_finalized) { + v4l2_subdev_cleanup(&chan->isp_sd); + media_entity_cleanup(&chan->isp_sd.entity); + chan->isp_sd_finalized = false; + } + } + v4l2_device_unregister(&video->v4l2_dev); + v4l2_device_put(&video->v4l2_dev); +unlock: + mutex_unlock(&t23_video_lock); + return ret; +} + +void t23_isp_video_unregister(void) +{ + struct t23_isp_video *video; + unsigned int ch, s; + + mutex_lock(&t23_video_lock); + video = t23_video; + t23_video = NULL; + mutex_unlock(&t23_video_lock); + if (!video) + return; + + mutex_lock(&video->lock); + video->registered = false; + t23_pipeline_stop(video); + for (ch = 0; ch < T23_CAPTURE_CHANNELS; ch++) { + struct t23_video_node *chan = &video->channels[ch]; + + t23_disconnect_outputs(chan); + } + video->disconnected = true; + video->isp = NULL; + mutex_unlock(&video->lock); + + v4l2_async_nf_unregister(&video->notifier); + for (ch = 0; ch < video->num_endpoints; ch++) + if (video->inputs[ch].sd) + t23_sensor_vblank_watch(video, video->inputs[ch].sd, false); + cancel_work_sync(&video->vblank_sync_work); + v4l2_async_nf_cleanup(&video->notifier); + + t23_overlay_unregister(&video->overlay); + t23_meta_unregister(&video->meta); + for (ch = 0; ch < T23_CAPTURE_CHANNELS; ch++) { + struct t23_video_node *chan = &video->channels[ch]; + + for (s = 0; s < T23_SCALED_OUTPUTS; s++) { + if (chan->sub[s].vdev_registered) { + video_unregister_device(&chan->sub[s].vdev); + chan->sub[s].vdev_registered = false; + } + } + if (chan->vdev_registered) { + video_unregister_device(&chan->vdev); + chan->vdev_registered = false; + } + if (chan->isp_sd_registered) { + v4l2_device_unregister_subdev(&chan->isp_sd); + chan->isp_sd_registered = false; + } + if (chan->isp_sd_finalized) { + v4l2_subdev_cleanup(&chan->isp_sd); + media_entity_cleanup(&chan->isp_sd.entity); + chan->isp_sd_finalized = false; + } + } + + if (video->media_registered) + media_device_unregister(&video->mdev); + v4l2_device_unregister(&video->v4l2_dev); + v4l2_device_put(&video->v4l2_dev); +} + + From deedd59f5932891c1f849c7e4f71453b13bb3238 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:25:01 -0800 Subject: [PATCH 66/78] dt-bindings: media: Add the Ingenic T23 Helix encoder Describe the T23 Helix encoder register windows, clocks, interrupts and reset resources. Signed-off-by: Difan Zhang --- .../bindings/media/ingenic,t23-helix.yaml | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/ingenic,t23-helix.yaml diff --git a/Documentation/devicetree/bindings/media/ingenic,t23-helix.yaml b/Documentation/devicetree/bindings/media/ingenic,t23-helix.yaml new file mode 100644 index 0000000000..c0c81eca30 --- /dev/null +++ b/Documentation/devicetree/bindings/media/ingenic,t23-helix.yaml @@ -0,0 +1,66 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/ingenic,t23-helix.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 Helix H.264 and JPEG Video Encoder + +maintainers: + - Paul Cercueil + +description: + Hardware H.264 (Baseline/Main/High Profile) and baseline JPEG video + encoder block in Ingenic T23 SoCs driven via VDMA descriptor chains. + +properties: + compatible: + const: ingenic,t23-helix + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + items: + - description: VPU core divider clock + - description: AVPU/Helix bus gate clock + - description: IVDC bus gate clock + + clock-names: + items: + - const: vpu + - const: gate + - const: ivdc + + resets: + maxItems: 1 + + memory-region: + maxItems: 1 + +required: + - compatible + - reg + - interrupts + - clocks + - clock-names + +additionalProperties: false + +examples: + - | + #include + #include + + video-codec@13100000 { + compatible = "ingenic,t23-helix"; + reg = <0x13100000 0x100000>; + interrupt-parent = <&intc>; + interrupts = <40>; + clocks = <&cgu CLK_CGU_VPU>, <&cgu CLK_AVPU>, <&cgu CLK_IVDC>; + clock-names = "vpu", "gate", "ivdc"; + resets = <&cpm_reset T23_RESET_HELIX>; + }; From 18e5e389ed4099bec4e69e61f2f14f6683134bff Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:01:44 -0800 Subject: [PATCH 67/78] media: platform: ingenic: Add the T23 Helix V4L2 encoder Add the T23 V4L2 memory-to-memory H.264 and JPEG encoder with per-context reference buffers, DMA-BUF input, rate control, aligned DMA staging and encoder STOP/EOS draining. Signed-off-by: Difan Zhang --- drivers/media/platform/ingenic/Kconfig | 1 + drivers/media/platform/ingenic/Makefile | 1 + drivers/media/platform/ingenic/helix/Kconfig | 17 + drivers/media/platform/ingenic/helix/Makefile | 6 + .../media/platform/ingenic/helix/helix_drv.c | 352 ++++ .../media/platform/ingenic/helix/helix_drv.h | 203 +++ .../media/platform/ingenic/helix/helix_h264.c | 549 ++++++ .../media/platform/ingenic/helix/helix_h264.h | 23 + .../ingenic/helix/helix_h264_tables.h | 216 +++ .../media/platform/ingenic/helix/helix_hw.h | 263 +++ .../media/platform/ingenic/helix/helix_jpeg.c | 327 ++++ .../media/platform/ingenic/helix/helix_rc.c | 152 ++ .../media/platform/ingenic/helix/helix_rc.h | 39 + .../platform/ingenic/helix/helix_tables.h | 122 ++ .../media/platform/ingenic/helix/helix_v4l2.c | 1501 +++++++++++++++++ .../media/platform/ingenic/helix/helix_vdma.c | 134 ++ .../media/platform/ingenic/helix/helix_vdma.h | 45 + 17 files changed, 3951 insertions(+) create mode 100644 drivers/media/platform/ingenic/helix/Kconfig create mode 100644 drivers/media/platform/ingenic/helix/Makefile create mode 100644 drivers/media/platform/ingenic/helix/helix_drv.c create mode 100644 drivers/media/platform/ingenic/helix/helix_drv.h create mode 100644 drivers/media/platform/ingenic/helix/helix_h264.c create mode 100644 drivers/media/platform/ingenic/helix/helix_h264.h create mode 100644 drivers/media/platform/ingenic/helix/helix_h264_tables.h create mode 100644 drivers/media/platform/ingenic/helix/helix_hw.h create mode 100644 drivers/media/platform/ingenic/helix/helix_jpeg.c create mode 100644 drivers/media/platform/ingenic/helix/helix_rc.c create mode 100644 drivers/media/platform/ingenic/helix/helix_rc.h create mode 100644 drivers/media/platform/ingenic/helix/helix_tables.h create mode 100644 drivers/media/platform/ingenic/helix/helix_v4l2.c create mode 100644 drivers/media/platform/ingenic/helix/helix_vdma.c create mode 100644 drivers/media/platform/ingenic/helix/helix_vdma.h diff --git a/drivers/media/platform/ingenic/Kconfig b/drivers/media/platform/ingenic/Kconfig index 5e856e84bb..c13234ad64 100644 --- a/drivers/media/platform/ingenic/Kconfig +++ b/drivers/media/platform/ingenic/Kconfig @@ -20,3 +20,4 @@ config VIDEO_INGENIC_PTZ_MOTOR will be called ptz-motor. source "drivers/media/platform/ingenic/tx-isp/Kconfig" +source "drivers/media/platform/ingenic/helix/Kconfig" diff --git a/drivers/media/platform/ingenic/Makefile b/drivers/media/platform/ingenic/Makefile index 5345fb21af..a0f3023d6f 100644 --- a/drivers/media/platform/ingenic/Makefile +++ b/drivers/media/platform/ingenic/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_VIDEO_INGENIC_PTZ_MOTOR) += ptz-motor.o obj-$(CONFIG_VIDEO_INGENIC_ISP) += tx-isp/ +obj-$(CONFIG_VIDEO_INGENIC_HELIX) += helix/ diff --git a/drivers/media/platform/ingenic/helix/Kconfig b/drivers/media/platform/ingenic/helix/Kconfig new file mode 100644 index 0000000000..b408584dcb --- /dev/null +++ b/drivers/media/platform/ingenic/helix/Kconfig @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +config VIDEO_INGENIC_HELIX + tristate "Ingenic T23 Helix V4L2 M2M video encoder" + depends on V4L_MEM2MEM_DRIVERS + depends on VIDEO_DEV + depends on MACH_INGENIC || COMPILE_TEST + depends on OF && HAS_DMA + select VIDEOBUF2_DMA_CONTIG + select V4L2_MEM2MEM_DEV + help + V4L2 memory-to-memory H.264 and JPEG hardware encoder driver + for Ingenic T23 SoCs. + + To compile this driver as a module, choose M here: the module + will be called ingenic-helix-v4l2. diff --git a/drivers/media/platform/ingenic/helix/Makefile b/drivers/media/platform/ingenic/helix/Makefile new file mode 100644 index 0000000000..6385a90dde --- /dev/null +++ b/drivers/media/platform/ingenic/helix/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +obj-$(CONFIG_VIDEO_INGENIC_HELIX) += ingenic-helix-v4l2.o +ingenic-helix-v4l2-objs := helix_drv.o helix_v4l2.o \ + helix_jpeg.o helix_vdma.o helix_h264.o helix_rc.o diff --git a/drivers/media/platform/ingenic/helix/helix_drv.c b/drivers/media/platform/ingenic/helix/helix_drv.c new file mode 100644 index 0000000000..237c190256 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_drv.c @@ -0,0 +1,352 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#include "helix_drv.h" + +int helix_hw_reset(struct helix_dev *hdev) +{ + return reset_control_reset(hdev->reset); +} + +static void helix_free_dma(struct helix_dev *hdev); + +int helix_hw_quiesce(struct helix_dev *hdev) +{ + int ret; + + lockdep_assert_held(&hdev->hw_lock); + ret = helix_hw_reset(hdev); + if (ret) { + dev_err(hdev->dev, "helix reset failed: %d\n", ret); + WRITE_ONCE(hdev->fatal, true); + return ret; + } + + WRITE_ONCE(hdev->fatal, false); + WRITE_ONCE(hdev->hw_running, false); + hdev->dma_owner = NULL; + if (hdev->h264_hold_valid) { + helix_free_h264_buffers(hdev, &hdev->h264_hold); + memset(&hdev->h264_hold, 0, sizeof(hdev->h264_hold)); + hdev->h264_hold_valid = false; + } + if (!hdev->dma_users) + helix_free_dma(hdev); + return 0; +} + +static void helix_free_dma(struct helix_dev *hdev) +{ + if (hdev->task_vaddr) + dma_free_coherent(hdev->dev, HELIX_TASK_SIZE, hdev->task_vaddr, + hdev->task_paddr); + if (hdev->input_vaddr) + dma_free_coherent(hdev->dev, hdev->input_size, + hdev->input_vaddr, hdev->input_paddr); + if (hdev->scratch_vaddr) + dma_free_coherent(hdev->dev, hdev->scratch_size, + hdev->scratch_vaddr, hdev->scratch_paddr); + if (hdev->bounce_vaddr) + dma_free_coherent(hdev->dev, hdev->bounce_size, + hdev->bounce_vaddr, hdev->bounce_paddr); + hdev->task_vaddr = NULL; + hdev->input_vaddr = NULL; + hdev->scratch_vaddr = NULL; + hdev->bounce_vaddr = NULL; + hdev->input_size = 0; + hdev->scratch_size = 0; +} + +/* + * hw_lock held. Allocate CPU fallback input buffer lazily only when an input + * frame requires format conversion or width/address alignment padding. + */ +int helix_ensure_input_dma(struct helix_dev *hdev, size_t input_size) +{ + struct device *dev = hdev->dev; + dma_addr_t paddr; + void *vaddr; + + lockdep_assert_held(&hdev->hw_lock); + input_size = PAGE_ALIGN(input_size); + if (input_size <= hdev->input_size && hdev->input_vaddr) + return 0; + if (READ_ONCE(hdev->hw_running)) + return -EBUSY; + vaddr = dma_alloc_coherent(dev, input_size, &paddr, GFP_KERNEL); + if (!vaddr) + return -ENOMEM; + if (hdev->input_vaddr) + dma_free_coherent(dev, hdev->input_size, + hdev->input_vaddr, hdev->input_paddr); + hdev->input_vaddr = vaddr; + hdev->input_paddr = paddr; + hdev->input_size = input_size; + return 0; +} + +/* + * hw_lock held. Shared task, bitstream bounce, and H.264 intra-frame scratch + * buffers allocated from CMA pool while any context is actively streaming. + */ +int helix_get_dma(struct helix_dev *hdev, bool need_scratch) +{ + struct device *dev = hdev->dev; + + lockdep_assert_held(&hdev->hw_lock); + if (need_scratch && !hdev->scratch_vaddr) { + hdev->scratch_vaddr = dma_alloc_coherent(dev, + HELIX_SCRATCH_BUF_SIZE, + &hdev->scratch_paddr, + GFP_KERNEL); + if (!hdev->scratch_vaddr) + goto err; + hdev->scratch_size = HELIX_SCRATCH_BUF_SIZE; + } + if (!hdev->bounce_vaddr) { + hdev->bounce_vaddr = dma_alloc_coherent(dev, HELIX_BOUNCE_BUF_SIZE, + &hdev->bounce_paddr, + GFP_KERNEL); + if (!hdev->bounce_vaddr) + goto err; + hdev->bounce_size = HELIX_BOUNCE_BUF_SIZE; + } + if (!hdev->task_vaddr) { + hdev->task_vaddr = dma_alloc_coherent(dev, HELIX_TASK_SIZE, + &hdev->task_paddr, + GFP_KERNEL); + if (!hdev->task_vaddr) + goto err; + } + hdev->dma_users++; + return 0; + +err: + if (!hdev->dma_users && !READ_ONCE(hdev->hw_running)) + helix_free_dma(hdev); + return -ENOMEM; +} + +/* hw_lock held. A VPU that did not stop may still write these buffers. */ +void helix_put_dma(struct helix_dev *hdev) +{ + lockdep_assert_held(&hdev->hw_lock); + if (!--hdev->dma_users && !READ_ONCE(hdev->hw_running) && + !READ_ONCE(hdev->fatal)) + helix_free_dma(hdev); +} + +static irqreturn_t helix_irq_handler(int irq, void *data) +{ + struct helix_dev *hdev = data; + u32 status = readl(hdev->base + REG_SCH_STAT); + u32 glbc; + + if (!status) + return IRQ_NONE; + + /* Mask all scheduler interrupt enables in REG_SCH_GLBC */ + glbc = readl(hdev->base + REG_SCH_GLBC); + writel(glbc & ~SCH_INTE_MASK, hdev->base + REG_SCH_GLBC); + + if (status & SCH_STAT_ENDFLAG) { + if (status & SCH_STAT_JPGEND) { + hdev->bslen = readl(hdev->base + REG_JPGC_STAT) & JPGC_STAT_LEN_MASK; + writel(readl(hdev->base + REG_JPGC_STAT) & ~JPGC_STAT_ENDF, + hdev->base + REG_JPGC_STAT); + } else { + hdev->bslen = readl(hdev->base + REG_SDE_CFG9); + writel(readl(hdev->base + REG_SDE_STAT) & ~SDE_STAT_BSEND, + hdev->base + REG_SDE_STAT); + writel(readl(hdev->base + REG_DBLK_GSTA) & ~DBLK_STAT_DOEND, + hdev->base + REG_DBLK_GSTA); + } + } else { + hdev->bslen = 0; + } + + WRITE_ONCE(hdev->irq_status, status); + complete(&hdev->completion); + return IRQ_HANDLED; +} + +static void helix_free(struct kref *ref) +{ + struct helix_dev *hdev = container_of(ref, struct helix_dev, ref); + + if (hdev->workqueue) + destroy_workqueue(hdev->workqueue); + v4l2_m2m_release(hdev->m2m_dev); + v4l2_device_unregister(&hdev->v4l2_dev); + put_device(hdev->dev); + kfree(hdev); +} + +static void helix_video_release(struct video_device *vfd) +{ + struct helix_dev *hdev = video_get_drvdata(vfd); + + kref_put(&hdev->ref, helix_free); +} + +static int helix_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct helix_dev *hdev; + int ret; + + hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); + if (!hdev) + return -ENOMEM; + hdev->dev = get_device(dev); + kref_init(&hdev->ref); + mutex_init(&hdev->dev_mutex); + mutex_init(&hdev->hw_lock); + init_completion(&hdev->completion); + INIT_WORK(&hdev->work, helix_job_work); + INIT_LIST_HEAD(&hdev->contexts); + hdev->workqueue = alloc_ordered_workqueue(HELIX_NAME, WQ_MEM_RECLAIM); + if (!hdev->workqueue) { + ret = -ENOMEM; + goto err_free; + } + platform_set_drvdata(pdev, hdev); + hdev->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(hdev->base)) { + ret = PTR_ERR(hdev->base); + goto err_free; + } + hdev->reset = devm_reset_control_get_exclusive(dev, NULL); + if (IS_ERR(hdev->reset)) { + ret = PTR_ERR(hdev->reset); + goto err_free; + } + hdev->clocks[0].id = "vpu"; + hdev->clocks[1].id = "gate"; + hdev->clocks[2].id = "ivdc"; + ret = devm_clk_bulk_get(dev, ARRAY_SIZE(hdev->clocks), hdev->clocks); + if (ret) + goto err_free; + ret = clk_bulk_prepare_enable(ARRAY_SIZE(hdev->clocks), hdev->clocks); + if (ret) + goto err_free; + ret = helix_hw_reset(hdev); + if (ret) + goto err_clk; + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); + if (ret) + goto err_clk; + vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32)); + if (!dev->dma_mem) { + ret = of_reserved_mem_device_init(dev); + if (ret && ret != -EBUSY && ret != -ENODEV) + goto err_clk; + } + hdev->irq = platform_get_irq(pdev, 0); + if (hdev->irq < 0) { + ret = hdev->irq; + goto err_pool; + } + writel(0, hdev->base + REG_SCH_GLBC); + ret = devm_request_irq(dev, hdev->irq, helix_irq_handler, 0, + dev_name(dev), hdev); + if (ret) + goto err_pool; + ret = v4l2_device_register(dev, &hdev->v4l2_dev); + if (ret) + goto err_pool; + hdev->m2m_dev = v4l2_m2m_init(&helix_m2m_ops); + if (IS_ERR(hdev->m2m_dev)) { + ret = PTR_ERR(hdev->m2m_dev); + goto err_v4l2; + } + strscpy(hdev->vfd.name, HELIX_NAME, sizeof(hdev->vfd.name)); + hdev->vfd.fops = &helix_v4l2_fops; + hdev->vfd.ioctl_ops = &helix_v4l2_ioctl_ops; + hdev->vfd.release = helix_video_release; + hdev->vfd.lock = &hdev->dev_mutex; + hdev->vfd.v4l2_dev = &hdev->v4l2_dev; + hdev->vfd.vfl_dir = VFL_DIR_M2M; + hdev->vfd.device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; + video_set_drvdata(&hdev->vfd, hdev); + kref_get(&hdev->ref); + ret = video_register_device(&hdev->vfd, VFL_TYPE_VIDEO, -1); + if (ret) { + kref_put(&hdev->ref, helix_free); + goto err_m2m; + } + return 0; + +err_m2m: + v4l2_m2m_release(hdev->m2m_dev); +err_v4l2: + v4l2_device_unregister(&hdev->v4l2_dev); +err_pool: + of_reserved_mem_device_release(dev); +err_clk: + clk_bulk_disable_unprepare(ARRAY_SIZE(hdev->clocks), hdev->clocks); +err_free: + if (hdev->workqueue) + destroy_workqueue(hdev->workqueue); + put_device(dev); + kfree(hdev); + return ret; +} + +static void helix_remove(struct platform_device *pdev) +{ + struct helix_dev *hdev = platform_get_drvdata(pdev); + struct helix_ctx *ctx; + int ret; + + mutex_lock(&hdev->dev_mutex); + WRITE_ONCE(hdev->disconnected, true); + WRITE_ONCE(hdev->aborting, true); + list_for_each_entry(ctx, &hdev->contexts, list) { + WRITE_ONCE(ctx->stopping, true); + vb2_queue_error(v4l2_m2m_get_src_vq(ctx->m2m_ctx)); + vb2_queue_error(v4l2_m2m_get_dst_vq(ctx->m2m_ctx)); + } + complete(&hdev->completion); + flush_workqueue(hdev->workqueue); + mutex_lock(&hdev->hw_lock); + writel(0, hdev->base + REG_SCH_GLBC); + ret = helix_hw_quiesce(hdev); + mutex_unlock(&hdev->hw_lock); + list_for_each_entry(ctx, &hdev->contexts, list) { + v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); + v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); + } + clk_bulk_disable_unprepare(ARRAY_SIZE(hdev->clocks), hdev->clocks); + if (ret || READ_ONCE(hdev->hw_running) || hdev->h264_hold_valid) { + /* Keep DMA targets reserved when stop was not acknowledged. */ + dev_err(hdev->dev, "helix DMA not quiescent; retaining DMA buffers\n"); + goto unregister; + } + helix_free_dma(hdev); + of_reserved_mem_device_release(hdev->dev); +unregister: + mutex_unlock(&hdev->dev_mutex); + video_unregister_device(&hdev->vfd); + kref_put(&hdev->ref, helix_free); +} + +static const struct of_device_id helix_of_match[] = { + { .compatible = "ingenic,t23-helix" }, + { } +}; +MODULE_DEVICE_TABLE(of, helix_of_match); + +static struct platform_driver helix_driver = { + .probe = helix_probe, + .remove = helix_remove, + .driver = { + .name = HELIX_NAME, + .of_match_table = helix_of_match, + }, +}; +module_platform_driver(helix_driver); + +MODULE_DESCRIPTION("Ingenic T23 Helix Video Encoder"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS(DMA_BUF); diff --git a/drivers/media/platform/ingenic/helix/helix_drv.h b/drivers/media/platform/ingenic/helix/helix_drv.h new file mode 100644 index 0000000000..a80cac29f8 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_drv.h @@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ + +#ifndef _HELIX_DRV_H_ +#define _HELIX_DRV_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "helix_hw.h" +#include "helix_rc.h" +#include "helix_vdma.h" + +#define HELIX_NAME "ingenic-helix-v4l2" + +#define HELIX_MIN_W 64 +#define HELIX_MIN_H 64 +#define HELIX_MAX_W 1920 +#define HELIX_MAX_H 1088 +#define HELIX_DEF_W 1920 +#define HELIX_DEF_H 1088 + +#define HELIX_BOUNCE_BUF_SIZE (1536 * 1024) +#define HELIX_SCRATCH_BUF_SIZE (1536 * 1024) + +struct helix_dev; +struct helix_ctx; + +struct helix_fmt { + u32 fourcc; + u32 types; + u32 num_planes; + u8 depth[VIDEO_MAX_PLANES]; +}; + +struct helix_dev { + struct device *dev; + void __iomem *base; + struct reset_control *reset; + struct kref ref; + struct clk_bulk_data clocks[3]; + int irq; + + struct v4l2_device v4l2_dev; + struct video_device vfd; + struct v4l2_m2m_dev *m2m_dev; + + struct mutex dev_mutex; + struct mutex hw_lock; + + struct helix_ctx *curr_ctx; + bool hw_busy; + /* True from a kicked task until the IRQ completes or a reset succeeds. */ + bool hw_running; + struct helix_ctx *dma_owner; + struct helix_h264_buffers h264_hold; + bool h264_hold_valid; + bool disconnected; + bool aborting; + bool fatal; + struct work_struct work; + struct workqueue_struct *workqueue; + struct list_head contexts; + struct completion completion; + u32 irq_status; + u32 bslen; + /* Shared DMA buffers, allocated while any context is streaming. */ + unsigned int dma_users; + void *input_vaddr; + dma_addr_t input_paddr; + size_t input_size; + + void *scratch_vaddr; + dma_addr_t scratch_paddr; + size_t scratch_size; + + void *bounce_vaddr; + dma_addr_t bounce_paddr; + size_t bounce_size; + __le32 *task_vaddr; + dma_addr_t task_paddr; +}; + +struct helix_ctx { + struct v4l2_fh fh; + struct v4l2_ctrl_handler hdl; + struct list_head list; + bool stopping; + struct helix_dev *hdev; + struct v4l2_m2m_ctx *m2m_ctx; + + struct v4l2_format src_fmt; + struct v4l2_format dst_fmt; + struct v4l2_rect crop; + + u32 jpeg_quality; + u32 qmem[256]; + u8 qtable[128]; + + struct helix_h264_buffers h264_bufs; + bool h264_bufs_allocated; + bool dma_ref; + u32 i_frame_qp; + u32 p_frame_qp; + u32 min_qp; + u32 max_qp; + u32 gop_size; + u32 h264_profile; + u32 h264_level; + u32 header_mode; + bool force_key_frame; + bool repeat_seq_header; + struct helix_rc rc; + + u32 frame_num; + u32 sequence; + u32 idr_pic_id; + u32 gop_count; +}; + +/* CP0 Config7 VPU enable/disable hardware controls */ +static inline void helix_cp0_config7_enable(void) +{ +#ifdef CONFIG_MIPS + u32 val; + + __asm__ __volatile__( + ".set push\n\t" + ".set mips32r2\n\t" + "mfc0 %0, $16, 7\n\t" + "ori %0, %0, 0x340\n\t" + "andi %0, %0, 0x3ff\n\t" + "mtc0 %0, $16, 7\n\t" + "ehb\n\t" + ".set pop\n\t" + : "=r"(val)); +#endif +} + +static inline void helix_cp0_config7_disable(void) +{ +#ifdef CONFIG_MIPS + u32 val; + + __asm__ __volatile__( + ".set push\n\t" + ".set mips32r2\n\t" + "mfc0 %0, $16, 7\n\t" + "andi %0, %0, 0xbf\n\t" + "mtc0 %0, $16, 7\n\t" + "ehb\n\t" + ".set pop\n\t" + : "=r"(val)); +#endif +} + +const struct helix_fmt *helix_find_fmt(u32 fourcc, u32 type); + +void helix_calc_qtable(u32 quality, u8 *qtable, u32 *qmem); +size_t helix_jpeg_write_headers(u8 *buf, u32 width, u32 height, + const u8 *qtable, u32 visible_width, u32 visible_height); +void helix_jpeg_write_eoi(u8 *buf, size_t offset); +int helix_jpeg_hw_start(struct helix_dev *hdev, + dma_addr_t src_y_dma, dma_addr_t src_uv_dma, + dma_addr_t dst_dma, u32 width, u32 height, + u32 y_stride, u32 uv_stride, u32 fourcc, + const u32 *qmem); + +extern const struct v4l2_ioctl_ops helix_v4l2_ioctl_ops; +extern const struct v4l2_file_operations helix_v4l2_fops; +extern const struct v4l2_m2m_ops helix_m2m_ops; + +int helix_v4l2_init_ctx(struct helix_ctx *ctx); +void helix_v4l2_cleanup_ctx(struct helix_ctx *ctx); + +int helix_get_dma(struct helix_dev *hdev, bool need_scratch); +int helix_ensure_input_dma(struct helix_dev *hdev, size_t input_size); +void helix_put_dma(struct helix_dev *hdev); +int helix_hw_reset(struct helix_dev *hdev); +int helix_hw_quiesce(struct helix_dev *hdev); +void helix_job_work(struct work_struct *work); + +#endif diff --git a/drivers/media/platform/ingenic/helix/helix_h264.c b/drivers/media/platform/ingenic/helix/helix_h264.c new file mode 100644 index 0000000000..022a852d83 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_h264.c @@ -0,0 +1,549 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include +#include +#include + +#include "helix_drv.h" +#include "helix_h264.h" +#include "helix_h264_tables.h" + +struct h264_bit_writer { + u8 *buf; + size_t pos; + size_t max_size; + u32 bit_buf; + int bit_count; + int zero_count; + bool overflow; +}; + +static inline void h264_bw_init(struct h264_bit_writer *bw, u8 *buf, size_t max_size) +{ + bw->buf = buf; + bw->pos = 0; + bw->max_size = max_size; + bw->bit_buf = 0; + bw->bit_count = 0; + bw->zero_count = 0; + bw->overflow = false; +} + +static inline void h264_bw_put_byte(struct h264_bit_writer *bw, u8 b) +{ + if (bw->pos >= bw->max_size) { + bw->overflow = true; + return; + } + + /* Emulation prevention byte insertion */ + if (bw->zero_count >= 2 && b <= 3) { + if (bw->pos >= bw->max_size) { + bw->overflow = true; + return; + } + bw->buf[bw->pos++] = 0x03; + bw->zero_count = 0; + } + + if (bw->pos >= bw->max_size) { + bw->overflow = true; + return; + } + bw->buf[bw->pos++] = b; + + if (b == 0) + bw->zero_count++; + else + bw->zero_count = 0; +} + +static inline void h264_bw_put_bits(struct h264_bit_writer *bw, u32 val, int n) +{ + int i; + + for (i = n - 1; i >= 0; i--) { + bw->bit_buf = (bw->bit_buf << 1) | ((val >> i) & 1); + bw->bit_count++; + if (bw->bit_count == 8) { + h264_bw_put_byte(bw, (u8)bw->bit_buf); + bw->bit_buf = 0; + bw->bit_count = 0; + } + } +} + +static inline void h264_bw_put_ue(struct h264_bit_writer *bw, u32 val) +{ + u32 val_plus1 = val + 1; + int len = fls(val_plus1) - 1; + + h264_bw_put_bits(bw, 0, len); + h264_bw_put_bits(bw, val_plus1, len + 1); +} + +static inline void h264_bw_put_se(struct h264_bit_writer *bw, int val) +{ + if (val <= 0) + h264_bw_put_ue(bw, (u32)(-2 * val)); + else + h264_bw_put_ue(bw, (u32)(2 * val - 1)); +} + +static inline void h264_bw_rbsp_trailing(struct h264_bit_writer *bw) +{ + h264_bw_put_bits(bw, 1, 1); + if (bw->bit_count > 0) + h264_bw_put_bits(bw, 0, 8 - bw->bit_count); +} + +static inline void h264_bw_start_code(struct h264_bit_writer *bw) +{ + if (bw->pos + 4 > bw->max_size) { + bw->overflow = true; + return; + } + bw->buf[bw->pos++] = 0x00; + bw->buf[bw->pos++] = 0x00; + bw->buf[bw->pos++] = 0x00; + bw->buf[bw->pos++] = 0x01; + bw->zero_count = 0; +} + +ssize_t helix_h264_write_sps(struct helix_ctx *ctx, u8 *buf, size_t max_size, + u32 vis_w, u32 vis_h, u32 coded_w, u32 coded_h) +{ + struct h264_bit_writer bw; + u8 level_idc; + bool crop = (vis_w < coded_w) || (vis_h < coded_h); + + switch (ctx->h264_level) { + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0: + level_idc = 30; + break; + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1: + level_idc = 31; + break; + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0: + level_idc = 40; + break; + default: + level_idc = (coded_w > 1280 || coded_h > 720) ? 40 : 31; + break; + } + + h264_bw_init(&bw, buf, max_size); + h264_bw_start_code(&bw); + h264_bw_put_byte(&bw, 0x67); + + /* High profile. PPS CABAC and transform_8x8 match this idc. */ + h264_bw_put_bits(&bw, 100, 8); + h264_bw_put_bits(&bw, 0x00, 8); + h264_bw_put_bits(&bw, level_idc, 8); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 1); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 0); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 2); + h264_bw_put_ue(&bw, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_ue(&bw, (coded_w / 16) - 1); + h264_bw_put_ue(&bw, (coded_h / 16) - 1); + h264_bw_put_bits(&bw, 1, 1); + h264_bw_put_bits(&bw, 1, 1); + + if (crop) { + h264_bw_put_bits(&bw, 1, 1); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, (coded_w - vis_w) / 2); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, (coded_h - vis_h) / 2); + } else { + h264_bw_put_bits(&bw, 0, 1); + } + + h264_bw_put_bits(&bw, 0, 1); + h264_bw_rbsp_trailing(&bw); + + return bw.overflow ? -ENOSPC : (ssize_t)bw.pos; +} + +ssize_t helix_h264_write_pps(struct helix_ctx *ctx, u8 *buf, size_t max_size, u32 qp) +{ + struct h264_bit_writer bw; + + h264_bw_init(&bw, buf, max_size); + h264_bw_start_code(&bw); + h264_bw_put_byte(&bw, 0x68); + + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 0); + h264_bw_put_bits(&bw, 1, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 0); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_bits(&bw, 0, 2); + h264_bw_put_se(&bw, 0); + h264_bw_put_se(&bw, 0); + h264_bw_put_se(&bw, 0); + h264_bw_put_bits(&bw, 1, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_bits(&bw, 1, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_se(&bw, 0); + h264_bw_rbsp_trailing(&bw); + + return bw.overflow ? -ENOSPC : (ssize_t)bw.pos; +} + +ssize_t helix_h264_write_slice(struct helix_ctx *ctx, u8 *buf, size_t max_size, + bool is_idr, u32 qp, const u8 *hw_bs, size_t hw_len) +{ + struct h264_bit_writer bw; + u32 qp_clamp = clamp_val(qp, ctx->min_qp, ctx->max_qp); + size_t i; + + h264_bw_init(&bw, buf, max_size); + h264_bw_start_code(&bw); + + if (is_idr) { + h264_bw_put_byte(&bw, 0x65); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 7); + h264_bw_put_ue(&bw, 0); + h264_bw_put_bits(&bw, 0, 4); + h264_bw_put_ue(&bw, ctx->idr_pic_id++ & 0x7); + h264_bw_put_bits(&bw, 0, 2); + h264_bw_put_se(&bw, (int)qp_clamp - 26); + h264_bw_put_ue(&bw, 0); + h264_bw_put_se(&bw, 0); + h264_bw_put_se(&bw, 0); + } else { + h264_bw_put_byte(&bw, 0x41); + h264_bw_put_ue(&bw, 0); + h264_bw_put_ue(&bw, 5); + h264_bw_put_ue(&bw, 0); + h264_bw_put_bits(&bw, ctx->frame_num & 0xf, 4); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_bits(&bw, 0, 1); + h264_bw_put_ue(&bw, 0); + h264_bw_put_se(&bw, (int)qp_clamp - 26); + h264_bw_put_ue(&bw, 0); + h264_bw_put_se(&bw, 0); + h264_bw_put_se(&bw, 0); + } + + while (bw.bit_count != 0) + h264_bw_put_bits(&bw, 1, 1); + + for (i = 0; i < hw_len; i++) + h264_bw_put_byte(&bw, hw_bs[i]); + + return bw.overflow ? -ENOSPC : (ssize_t)bw.pos; +} + +int helix_h264_hw_start(struct helix_dev *hdev, struct helix_ctx *ctx, + dma_addr_t src_y_dma, dma_addr_t src_uv_dma, + dma_addr_t dst_dma, u32 width, u32 height, + u32 y_stride, u32 uv_stride, u32 fourcc, + bool is_idr, u32 qp) +{ + struct helix_vdma_task task; + u32 mb_w = width / 16; + u32 mb_h = height / 16; + int cur_slot = ctx->h264_bufs.cur_slot; + int ref_slot = 1 - cur_slot; + dma_addr_t recon_y_paddr = ctx->h264_bufs.recon_y_paddr[cur_slot]; + dma_addr_t recon_c_paddr = ctx->h264_bufs.recon_c_paddr[cur_slot]; + dma_addr_t ref_y_paddr = ctx->h264_bufs.recon_y_paddr[ref_slot]; + dma_addr_t ref_c_paddr = ctx->h264_bufs.recon_c_paddr[ref_slot]; + dma_addr_t scratch_paddr = hdev->scratch_paddr; + u32 qp_clamp = clamp_val(qp, ctx->min_qp, ctx->max_qp); + bool is_p = !is_idr; + /* Pre-scaled motion-estimation lambda thresholds. Above QP 33, TCSM filters ramp from these bases. */ + u32 dqp = (qp_clamp > 0x21) ? qp_clamp - 0x21 : 0; + u32 me_lambda_y0 = 0x180; + u32 me_lambda_c0 = 0x060; + u32 me_lambda_c1 = 0x060; + u32 me_lambda_step_y = 0x030; + u32 me_lambda_step_c0 = 0x00c; + u32 me_lambda_step_c1 = 0x00c; + u32 tcsm_sad_weight = (me_lambda_y0 + me_lambda_step_y * dqp) & 0xffff; + u32 tcsm_sad_thresh = (me_lambda_c0 + me_lambda_step_c0 * dqp) & 0xffff; + const s8 *cabac_mn = is_idr ? h264_cabac_context_init_I : h264_cabac_context_init_P0; + u8 cabac_state[460]; + u32 efe_kick; + unsigned int i; + int ret; + + if ((fourcc != V4L2_PIX_FMT_NV12 && fourcc != V4L2_PIX_FMT_NV21) || + width & 15 || height & 15 || + y_stride != width || uv_stride != width || + (src_y_dma | src_uv_dma | dst_dma) & 127 || + !ctx->h264_bufs_allocated || !scratch_paddr) + return -EINVAL; + + for (i = 0; i < 460; i++) { + int m = cabac_mn[2 * i]; + int n = cabac_mn[2 * i + 1]; + int pre = ((m * (int)qp_clamp) >> 4) + n; + + cabac_state[i] = (u8)clamp_val(pre, 1, 126); + } + + helix_vdma_task_init(&task, hdev->task_vaddr, hdev->task_paddr, HELIX_TASK_SIZE); + + /* EFE geometry, raw input pointers and TCSM base addresses */ + helix_vdma_append(&task, REG_EFE_GEOM, + (((mb_h - 1) & 0xff) << 8) | ((mb_w - 1) & 0xff)); + helix_vdma_append(&task, REG_EFE_COEF_BA, 0x131c0000); + helix_vdma_append(&task, REG_EFE_RAWY_SBA, src_y_dma); + helix_vdma_append(&task, REG_EFE_RAWC_SBA, src_uv_dma); + helix_vdma_append(&task, REG_EFE_RAWV_SBA, 0); + helix_vdma_append(&task, REG_EFE_RAW_STRD, (y_stride << 16) | uv_stride); + helix_vdma_append(&task, REG_EFE_TOPMV_BA, 0x131c4800); + helix_vdma_append(&task, REG_EFE_TOPPA_BA, 0x131c4000); + helix_vdma_append(&task, REG_EFE_MECHN_BA, 0x131c5400); + helix_vdma_append(&task, REG_EFE_MAUCHN_BA, 0x131c4c00); + helix_vdma_append(&task, REG_EFE_DBLKCHN_BA, 0x131c5000); + helix_vdma_append(&task, REG_EFE_SDECHN_BA, 0x131c5600); + helix_vdma_append(&task, REG_EFE_RAW_DBA, HELIX_INTERNAL_RAM); + + /* EFE ROI, quantization table and spatial analysis parameters */ + helix_vdma_append(&task, REG_EFE_ROI_MAX_QP, min(qp_clamp + 13, 51U)); + helix_vdma_append(&task, REG_EFE_ROI_BASE_INFO0, 0); + helix_vdma_append(&task, REG_EFE_ROI_BASE_INFO1, 0); + for (i = 0; i < 8; i++) + helix_vdma_append(&task, REG_EFE_ROI_POS_INFO0 + i * 4, 0); + helix_vdma_append(&task, REG_EFE_QP_GEN_TAB, 0x000c5800); + helix_vdma_append(&task, REG_EFE_CQP_OFST, 0); + helix_vdma_append(&task, REG_EFE_SSAD, 0); + helix_vdma_append(&task, REG_EFE_DCS, 0x00400000); + helix_vdma_append(&task, REG_EFE_QPG_CTRL, + 0x10 | ((qp_clamp & 0xff) << 8) | + ((max_t(int, (int)qp_clamp - 12, 1) & 0xff) << 16) | + ((min(qp_clamp + 13, 51U) & 0xff) << 24)); + helix_vdma_append(&task, REG_EFE_CRP_FILT, 0); + helix_vdma_append(&task, REG_EFE_CRP_THR0, 0); + helix_vdma_append(&task, REG_EFE_CRP_THR1, 0); + helix_vdma_append(&task, REG_EFE_CRP_THR2, 0); + helix_vdma_append(&task, REG_EFE_CRP_THR3, 0); + helix_vdma_append(&task, REG_EFE_CRP_MB_OFST0, 0); + helix_vdma_append(&task, REG_EFE_CRP_MB_OFST1, 0); + helix_vdma_append(&task, REG_EFE_SAS_MCNT1, is_p ? 0x030484c1 : 0x060404c1); + helix_vdma_append(&task, REG_EFE_QP_SUM, is_p ? 0x61615c21 : 0x61615921); + helix_vdma_append(&task, REG_EFE_SAS_FLT_THR, 0x12492fc0); + helix_vdma_append(&task, REG_EFE_ME_CFG0, 0x12492492); + helix_vdma_append(&task, REG_EFE_ME_CFG1, 0x0a000db3); + helix_vdma_append(&task, REG_EFE_ME_CFG2, 0x0f0a0505); + helix_vdma_append(&task, REG_EFE_ME_CFG3, 0); + helix_vdma_append(&task, REG_EFE_ME_LAMBDA0, (me_lambda_step_y << 16) | me_lambda_y0); + helix_vdma_append(&task, REG_EFE_ME_LAMBDA1, (me_lambda_step_c0 << 16) | me_lambda_c0); + helix_vdma_append(&task, REG_EFE_ME_LAMBDA2, (me_lambda_step_c1 << 16) | me_lambda_c1); + helix_vdma_append(&task, REG_EFE_SAS_THDG2, 0x00001400); + helix_vdma_append(&task, REG_EFE_SAS_MB_OFSTA, 0xaf8c7864); + helix_vdma_append(&task, REG_EFE_SAS_MB_OFSTB, 0x8c7d6432); + helix_vdma_append(&task, REG_EFE_SAS_CNT, 0xe1af9678); + helix_vdma_append(&task, REG_EFE_SAS_MCNT0, 0x003e3c3a); + helix_vdma_append(&task, REG_EFE_ME_CFG4, 0x0c25bd33); + helix_vdma_append(&task, REG_EFE_ME_CFG5, 0x10f54009); + helix_vdma_append(&task, REG_EFE_ME_CFG6, 0x37464605); + helix_vdma_append(&task, REG_EFE_INTRA_CFG0, 0x00000330); + helix_vdma_append(&task, REG_EFE_INTRA_CFG1, 0x80000330); + + /* VDMA block sizing and reference buffer configuration */ + helix_vdma_append(&task, REG_VDMA_UNLK, (height << 14) | width | 0x20000000); + helix_vdma_append(&task, REG_VDMA_TASKRG, 0); + helix_vdma_append(&task, REG_VDMA_TASKST, 0); + helix_vdma_append(&task, REG_VDMA_CH0_CNT, + ((((width + 63) >> 6) * ((height + 15) >> 4)) << 16) | + ((((width + 31) >> 5) * ((height + 15) >> 4)) & 0xffff)); + helix_vdma_append(&task, REG_VDMA_REF_Y_BA, is_p ? ref_y_paddr : 0); + helix_vdma_append(&task, REG_VDMA_REF_C_BA, is_p ? ref_c_paddr : 0); + helix_vdma_append(&task, REG_VDMA_STRIDE, (mb_w << 20) | (mb_w << 3)); + helix_vdma_append(&task, REG_VDMA_CFG0, 0); + helix_vdma_append(&task, REG_VDMA_CFG1, 0); + helix_vdma_append(&task, REG_VDMA_CFG2, 0); + helix_vdma_append(&task, REG_VDMA_CFG3, 0); + helix_vdma_append(&task, REG_VDMA_LOCK, 0x00000020); + + /* Motion compensation and estimation (P-frames only) */ + if (is_p) { + helix_vdma_append(&task, REG_MCE_CHN_BA, 0x131c5400); + helix_vdma_append(&task, REG_MCE_TMV_BA, 0x131c4800); + helix_vdma_append(&task, REG_MCE_CHN_SYNC, 0x13100070); + helix_vdma_append(&task, REG_MCE_FRM_SIZE, + ((height - 1) << 16) | ((width - 1) & 0xffff)); + helix_vdma_append(&task, REG_MCE_FRM_STRD, (mb_w << 20) | (mb_w << 4)); + helix_vdma_append(&task, REG_MCE_COMP_CTRL, 0x800a8021); + helix_vdma_append(&task, REG_MCE_ESTI_CTRL, 0x871f5008); + helix_vdma_append(&task, REG_MCE_MRGI, 0); + helix_vdma_append(&task, REG_MCE_MVR, 0x02000200); + helix_vdma_append(&task, REG_MCE_REF_Y_BA, ref_y_paddr); + helix_vdma_append(&task, REG_MCE_REF_C_BA, ref_c_paddr); + helix_vdma_append(&task, REG_MCE_SRC_Y_BA, src_y_dma); + helix_vdma_append(&task, REG_MCE_SRC_C_BA, src_uv_dma); + helix_vdma_append(&task, REG_MCE_CFG0, 0); + helix_vdma_append(&task, REG_MCE_CFG1, 0); + helix_vdma_append(&task, REG_MCE_SLC_SPOS, 0); + helix_vdma_append(&task, REG_MCE_FSC, + (mb_w >= 0x33 && mb_h > 0x26) ? 0x08080303 : 0); + helix_vdma_append(&task, REG_MCE_CFG2, 0); + helix_vdma_append(&task, REG_MCE_GLB_CTRL, 0x544fd9b1); + } + + /* Video Macroblock Analysis Unit (VMAU) configuration */ + helix_vdma_append(&task, REG_VMAU_GBL_RUN, 4); + helix_vdma_append(&task, REG_VMAU_VIDEO_TYPE, is_idr ? 0x80001b00 : 0x80000b00); + helix_vdma_append(&task, REG_VMAU_NCCHN_ADDR, 0x131c4c00); + helix_vdma_append(&task, REG_VMAU_ENC_DONE, 0x131ffffc); + helix_vdma_append(&task, REG_VMAU_DEC_DONE, 0x13100074); + helix_vdma_append(&task, REG_VMAU_Y_GS, (height << 16) | width); + helix_vdma_append(&task, REG_VMAU_GBL_CTR, 0x01000001); + helix_vdma_append(&task, REG_VMAU_DEADZONE, 0x0b150b15); + helix_vdma_append(&task, REG_VMAU_FIFO_CFG, 0x0b5552d5); + helix_vdma_append(&task, REG_VMAU_TOP_BASE, 0x131c4000); + helix_vdma_append(&task, REG_VMAU_MD_CFG0, + ((mb_h - 1) << 24) | ((mb_w - 1) << 16) | 0x8000 | (is_idr ? 1 : 2)); + helix_vdma_append(&task, REG_VMAU_MD_CFG1, 0x0000c200); + helix_vdma_append(&task, REG_VMAU_MD_CFG2, 0xcc000000); + helix_vdma_append(&task, REG_VMAU_MD_CFG3, 0); + helix_vdma_append(&task, REG_VMAU_MD_CFG4, 0); + helix_vdma_append(&task, REG_VMAU_MD_CFG5, 0); + helix_vdma_append(&task, REG_VMAU_MD_CFG6, 0x55004304); + helix_vdma_append(&task, REG_VMAU_MD_CFG7, 0x11 | ((qp_clamp & 0x3f) << 17)); + helix_vdma_append(&task, REG_VMAU_MD_CFG8, 0); + helix_vdma_append(&task, REG_VMAU_MD_CFG9, 0); + helix_vdma_append(&task, REG_VMAU_FIFO_CTRL, 0x23000000); + helix_vdma_append(&task, REG_VMAU_CFG7, 0x00400000); + helix_vdma_append(&task, REG_VMAU_CFG8, 0x00400000); + helix_vdma_append(&task, REG_VMAU_ACMASK, 0xffff8002); + + /* VMAU FIFO initial configuration */ + for (i = 0; i < 40; i++) + helix_vdma_append(&task, REG_VMAU_FIFO_DATA, helix_h264_vmau_fifo_words[i]); + + /* CABAC intra/inter slice initialization contexts */ + for (i = 0; i < 42; i++) { + u32 idx; + + if (i < 10) + idx = is_p ? (i + 0x0e) : (i + 3); + else if (i < 0x16) + idx = i + 0x3f; + else if (i <= 0x1b) + idx = i + 0x2a; + else + idx = i + 0x0c; + + helix_vdma_append(&task, REG_VMAU_CTX, cabac_state[idx]); + } + + /* CABAC syntax element context initialization */ + for (i = 0; i < 225; i++) + helix_vdma_append(&task, REG_VMAU_CABAC_RUN1, + cabac_state[helix_h264_cabac_fifo_map[i]]); + + /* VMAU slice and syntax engine parameters */ + helix_vdma_append(&task, REG_VMAU_SDE_CFG0, 0x30000006); + helix_vdma_append(&task, REG_VMAU_SDE_CFG1, 0x44444444); + helix_vdma_append(&task, REG_VMAU_SDE_CFG2, 0); + helix_vdma_append(&task, REG_VMAU_SDE_CFG3, 0x00430000); + helix_vdma_append(&task, REG_VMAU_SDE_CFG4, 0); + helix_vdma_append(&task, REG_VMAU_SDE_CFG5, 0); + + /* Deadzone and inverse quant matrices */ + for (i = 0; i < 64; i++) + helix_vdma_append(&task, REG_VMAU_RC_MAD_RAM + i * 4, + helix_h264_deadzone_matrix[i]); + + for (i = 0; i < 32; i++) + helix_vdma_append(&task, REG_VMAU_IQUANT_BASE + i * 4, + helix_h264_iquant_matrix[i]); + + helix_vdma_append(&task, REG_VMAU_GBL_RUN, 1); + + /* Deblocking filter and syntax decoder/encoder */ + helix_vdma_append(&task, REG_DBLK_CFG, 0x00000c0c); + helix_vdma_append(&task, REG_DBLK_TRIG, 8); + helix_vdma_append(&task, REG_SDE_STAT, 0); + helix_vdma_append(&task, REG_SDE_GL_CTRL, 0x00000011); + helix_vdma_append(&task, REG_SDE_SL_GEOM, (mb_h << 24) | (mb_w << 16)); + helix_vdma_append(&task, REG_SDE_CODEC_ID, 2); + helix_vdma_append(&task, REG_SDE_CFG0, 3); + helix_vdma_append(&task, REG_SDE_CFG1, (qp_clamp << 8) | 0x30 | (is_idr ? 1 : 2)); + helix_vdma_append(&task, REG_SDE_CFG2, 0x131c5600); + helix_vdma_append(&task, REG_SDE_CFG3, 0x1310007c); + helix_vdma_append(&task, REG_SDE_CFG4, dst_dma); + helix_vdma_append(&task, REG_SDE_CFG5, HELIX_BOUNCE_BUF_SIZE); + for (i = 0; i < 460; i++) { + u32 s = cabac_state[i] & 0x7f; + u32 idx = (s <= 0x3f) ? (0x3f - s) : (s - 0x40); + u32 val = (s >> 6) | helix_h264_cavlc_table[idx]; + + helix_vdma_append(&task, REG_SDE_CTX_TBL + i * 4, val); + } + helix_vdma_append(&task, REG_SDE_SL_CTRL, 2); + + /* Output DMA and pipeline scheduler configuration */ + helix_vdma_append(&task, REG_ODMA_GLB_TRIG, ((width < 0x211) << 18) | ((mb_h - 1) << 8) | (mb_w - 1)); + helix_vdma_append(&task, REG_ODMA_GLB_CTRL, dst_dma & 0xffffff80); + helix_vdma_append(&task, REG_ODMA_TLC_BA, (scratch_paddr + 0x08000) & 0xffffff80); + helix_vdma_append(&task, REG_ODMA_RAW_Y_BA, (scratch_paddr + 0x38000) & 0xffffff80); + helix_vdma_append(&task, REG_ODMA_RAW_C_BA, (scratch_paddr + 0xb8000) & 0xffffff80); + helix_vdma_append(&task, REG_ODMA_BUF3_BA, (scratch_paddr + 0x158000) & 0xffffff80); + helix_vdma_append(&task, REG_ODMA_BUF2_BA, (scratch_paddr + 0xd8000) & 0xffffff80); + helix_vdma_append(&task, REG_ODMA_AXI_CTRL, 0x400); + helix_vdma_append(&task, REG_ODMA_CFG0, 1); + for (i = 0; i < 8; i++) + helix_vdma_append(&task, REG_SCH_SCHC + i * 4, 0); + helix_vdma_append(&task, REG_SCH_SCHC, ((is_p & 1) << 2) | 0x08080400); + helix_vdma_append(&task, REG_SCH_BND, (is_p & 1) | 0x97850f02); + + /* + * JRFD reference reconstruction geometry and reconstruction buffers. + * Bit 31 enables reconstruct output; bit 29 flags narrow macroblock widths (1 or 3). + */ + helix_vdma_append(&task, REG_JRFD_GEOM, + (1U << 31) | ((mb_w == 1 || mb_w == 3) ? (1U << 29) : 0) | + (height << 14) | width); + helix_vdma_append(&task, REG_JRFD_Y_BA, recon_y_paddr); + helix_vdma_append(&task, REG_JRFD_C_BA, recon_c_paddr); + helix_vdma_append(&task, REG_JRFD_STRD, (mb_w << 20) | (mb_w << 3)); + for (i = 0; i < 4; i++) + helix_vdma_append(&task, REG_JRFD_CFG0 + i * 4, 0); + helix_vdma_append(&task, REG_JRFD_GLB_CTRL, 0x20); + + /* TCSM source and reference DMA setup */ + helix_vdma_append(&task, REG_TCSM_GEOM, ((height - 1) << 16) | (width - 1)); + helix_vdma_append(&task, REG_TCSM_Y_BA, src_y_dma); + helix_vdma_append(&task, REG_TCSM_C_BA, src_uv_dma); + helix_vdma_append(&task, REG_TCSM_REF_Y_BA, is_p ? ref_y_paddr : 0); + helix_vdma_append(&task, REG_TCSM_REF_C_BA, is_p ? ref_c_paddr : 0); + helix_vdma_append(&task, REG_TCSM_SRC_Y_BA, src_y_dma); + helix_vdma_append(&task, REG_TCSM_SRC_C_BA, src_uv_dma); + helix_vdma_append(&task, REG_TCSM_STRD, (uv_stride << 16) | (y_stride & 0xffff)); + helix_vdma_append(&task, REG_TCSM_CFG0, tcsm_sad_weight); + helix_vdma_append(&task, REG_TCSM_CFG1, + ((tcsm_sad_thresh & 0x3fff) << 16) | (tcsm_sad_thresh & 0x3fff)); + helix_vdma_append(&task, REG_TCSM_CTRL, is_p ? 0x0002ff63 : 0x0002ff21); + + /* Pipeline termination kick */ + efe_kick = (1U << 31) | (1U << 30) | ((qp_clamp & 0x3f) << 8) | + (((height & 0xf) != 0) << 7) | ((is_p & 1) << 4) | 0x20 | + ((fourcc == V4L2_PIX_FMT_NV21) ? EFE_PLANE_NV21 : EFE_PLANE_NV12) | + EFE_EN | EFE_RUN; + helix_vdma_append(&task, REG_EFE_CTRL, efe_kick); + helix_vdma_terminate(&task); + + ret = helix_vdma_kick(hdev, &task); + return ret; +} diff --git a/drivers/media/platform/ingenic/helix/helix_h264.h b/drivers/media/platform/ingenic/helix/helix_h264.h new file mode 100644 index 0000000000..7e7b903aeb --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_h264.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ + +#ifndef _HELIX_H264_H_ +#define _HELIX_H264_H_ + +#include + +struct helix_dev; +struct helix_ctx; + +ssize_t helix_h264_write_sps(struct helix_ctx *ctx, u8 *buf, size_t max_size, + u32 vis_w, u32 vis_h, u32 coded_w, u32 coded_h); +ssize_t helix_h264_write_pps(struct helix_ctx *ctx, u8 *buf, size_t max_size, u32 qp); +ssize_t helix_h264_write_slice(struct helix_ctx *ctx, u8 *buf, size_t max_size, + bool is_idr, u32 qp, const u8 *hw_bs, size_t hw_len); +int helix_h264_hw_start(struct helix_dev *hdev, struct helix_ctx *ctx, + dma_addr_t src_y_dma, dma_addr_t src_uv_dma, + dma_addr_t dst_dma, u32 width, u32 height, + u32 y_stride, u32 uv_stride, u32 fourcc, + bool is_idr, u32 qp); + +#endif diff --git a/drivers/media/platform/ingenic/helix/helix_h264_tables.h b/drivers/media/platform/ingenic/helix/helix_h264_tables.h new file mode 100644 index 0000000000..6aabd6d59e --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_h264_tables.h @@ -0,0 +1,216 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ + +#ifndef _HELIX_H264_TABLES_H_ +#define _HELIX_H264_TABLES_H_ + +#include + +static const u32 helix_h264_vmau_fifo_words[40] = { + 0x8888801f, 0x40011008, 0x00044444, 0x88888007, + 0x40011001, 0x00044444, 0x8888803b, 0x40011008, + 0x00044444, 0x88888037, 0x40011008, 0x00044444, + 0x8888802f, 0x40011008, 0x00044444, 0x88888800, + 0x40011002, 0x00044444, 0x88888800, 0x4001100f, + 0x00044444, 0x88888000, 0x40011008, 0x00044444, + 0x41111100, 0x00000004, 0x41111100, 0x00000004, + 0x41111100, 0x00000004, 0x41111100, 0x00000004, + 0x41111100, 0x00000004, 0x41111100, 0x00000004, + 0x41111100, 0x00000004, 0x41111100, 0x00000004, +}; + +static const u32 helix_h264_deadzone_matrix[64] = { + 0x000cc100, 0x0009d0b2, 0x000c30cc, 0x0009209d, + 0x0009d0b2, 0x0007c092, 0x0009209d, 0x0007507c, + 0x0007c092, 0x0006e075, 0x0007507c, 0x0006906e, + 0x0006e075, 0x00063069, 0x0006906e, 0x0005f063, + 0x0007c092, 0x0006e075, 0x0007507c, 0x0006906e, + 0x0006e075, 0x00063069, 0x0006906e, 0x0005f063, + 0x00063069, 0x0005905f, 0x0005f063, 0x00055059, + 0x0005905f, 0x00051055, 0x00055059, 0x0004e051, + 0x000b20d7, 0x000970a3, 0x000b20b2, 0x0008d097, + 0x000970a3, 0x0008408d, 0x0008d097, 0x00080084, + 0x0008408d, 0x00078080, 0x00080084, 0x00075078, + 0x00078080, 0x0006e075, 0x00075078, 0x0006b06e, + 0x0008408d, 0x00078080, 0x00080084, 0x00075078, + 0x00078080, 0x0006e075, 0x00075078, 0x0006b06e, + 0x0006e075, 0x0006606b, 0x0006b06e, 0x00061066, + 0x0006606b, 0x0005f061, 0x00061066, 0x0005b05f, +}; + +/* Inverse quant at 0x80900. Four 6-bit coefficients per word, same byte order as above. */ +static const u32 helix_h264_iquant_matrix[32] = { + 0x00697510, 0x0071a554, 0x0085c697, 0x008e171a, + 0x0096385c, 0x009e58e1, 0x00a67963, 0x00ae99e5, + 0x0096385c, 0x009e58e1, 0x00a67963, 0x00ae99e5, + 0x00baba67, 0x00c2eae9, 0x00cb0bab, 0x00d32c2e, + 0x006d95d3, 0x0075b5d7, 0x007dd6d9, 0x0081f75b, + 0x008a07dd, 0x008e281f, 0x009638a0, 0x009a58e2, + 0x008a07dd, 0x008e281f, 0x009638a0, 0x009a58e2, + 0x00a26963, 0x00aa89a5, 0x00aeaa26, 0x00b6baa8, +}; + +static const u32 helix_h264_cavlc_table[64] = { + 0xeeceaefc, 0xe1c3a5fc, 0xd6b99cfc, 0xcbb094f2, + 0xc1a78ce4, 0xb79e85da, 0xad967ece, 0xa48e78c4, + 0x9c8772ba, 0x94806cb0, 0x8c7966a6, 0x8573619e, + 0x7e6d5c96, 0x7867578e, 0x72625386, 0x6c5d4e80, + 0x66584a78, 0x61544672, 0x5c4f436c, 0x574b3f66, + 0x53473c62, 0x4e43395c, 0x4a403658, 0x463d3352, + 0x4339304e, 0x3f362e4a, 0x3c342b46, 0x39312942, + 0x362e273e, 0x332c253c, 0x30292338, 0x2e272136, + 0x2b251f32, 0x29231d30, 0x27211c2c, 0x251f1a2a, + 0x231e1928, 0x211c1826, 0x1f1b1624, 0x1d191522, + 0x1c181420, 0x1a17131e, 0x1915121c, 0x1714111a, + 0x16131018, 0x15120f18, 0x14110e16, 0x13100d14, + 0x120f0c14, 0x110e0c12, 0x100d0b12, 0x0f0d0a10, + 0x0e0c0a10, 0x0d0b090e, 0x0c0a090e, 0x0c0a080c, + 0x0b09070c, 0x0a09070a, 0x0a08070a, 0x0908060a, + 0x09070608, 0x08070508, 0x07060508, 0x00000000, +}; + +static const u16 helix_h264_cabac_fifo_map[225] = { + 0x055, 0x056, 0x057, 0x058, 0x069, 0x06a, 0x06b, 0x06c, 0x06d, 0x06e, 0x06f, 0x070, + 0x071, 0x072, 0x073, 0x074, 0x075, 0x076, 0x077, 0x0a6, 0x0a7, 0x0a8, 0x0a9, 0x0aa, + 0x0ab, 0x0ac, 0x0ad, 0x0ae, 0x0af, 0x0b0, 0x0b1, 0x0b2, 0x0b3, 0x0b4, 0x0e3, 0x0e4, + 0x0e5, 0x0e6, 0x0e7, 0x0e8, 0x0e9, 0x0ea, 0x0eb, 0x0ec, 0x059, 0x05a, 0x05b, 0x05c, + 0x078, 0x079, 0x07a, 0x07b, 0x07c, 0x07d, 0x07e, 0x07f, 0x080, 0x081, 0x082, 0x083, + 0x084, 0x085, 0x0b5, 0x0b6, 0x0b7, 0x0b8, 0x0b9, 0x0ba, 0x0bb, 0x0bc, 0x0bd, 0x0be, + 0x0bf, 0x0c0, 0x0c1, 0x0c2, 0x0ed, 0x0ee, 0x0ef, 0x0f0, 0x0f1, 0x0f2, 0x0f3, 0x0f4, + 0x0f5, 0x0f6, 0x05d, 0x05e, 0x05f, 0x060, 0x086, 0x087, 0x088, 0x089, 0x08a, 0x08b, + 0x08c, 0x08d, 0x08e, 0x08f, 0x090, 0x091, 0x092, 0x093, 0x094, 0x0c3, 0x0c4, 0x0c5, + 0x0c6, 0x0c7, 0x0c8, 0x0c9, 0x0ca, 0x0cb, 0x0cc, 0x0cd, 0x0ce, 0x0cf, 0x0d0, 0x0d1, + 0x0f7, 0x0f8, 0x0f9, 0x0fa, 0x0fb, 0x0fc, 0x0fd, 0x0fe, 0x0ff, 0x100, 0x061, 0x062, + 0x063, 0x064, 0x095, 0x096, 0x097, 0x0d2, 0x0d3, 0x0d4, 0x101, 0x102, 0x103, 0x104, + 0x105, 0x106, 0x107, 0x108, 0x109, 0x065, 0x066, 0x067, 0x068, 0x098, 0x099, 0x09a, + 0x09b, 0x09c, 0x09d, 0x09e, 0x09f, 0x0a0, 0x0a1, 0x0a2, 0x0a3, 0x0a4, 0x0a5, 0x0d5, + 0x0d6, 0x0d7, 0x0d8, 0x0d9, 0x0da, 0x0db, 0x0dc, 0x0dd, 0x0de, 0x0df, 0x0e0, 0x0e1, + 0x0e2, 0x10a, 0x10b, 0x10c, 0x10d, 0x10e, 0x10f, 0x110, 0x111, 0x112, 0x113, 0x192, + 0x193, 0x194, 0x195, 0x196, 0x197, 0x198, 0x199, 0x19a, 0x19b, 0x19c, 0x19d, 0x19e, + 0x19f, 0x1a0, 0x1a1, 0x1a2, 0x1a3, 0x1a4, 0x1a5, 0x1a6, 0x1a7, 0x1a8, 0x1a9, 0x1aa, + 0x1ab, 0x1ac, 0x1ad, 0x1ae, 0x1af, 0x1b0, 0x1b1, 0x1b2, 0x1b3, +}; + +static const s8 h264_cabac_context_init_I[920] = { + 0x14, -0x0f, 0x02, 0x36, 0x03, 0x4a, 0x14, -0x0f, 0x02, 0x36, 0x03, 0x4a, -0x1c, 0x7f, -0x17, 0x68, + -0x06, 0x35, -0x01, 0x36, 0x07, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x3f, + -0x09, 0x53, 0x04, 0x56, 0x00, 0x61, -0x07, 0x48, 0x0d, 0x29, 0x03, 0x3e, 0x00, 0x0b, 0x01, 0x37, + 0x00, 0x45, -0x11, 0x7f, -0x0d, 0x66, 0x00, 0x52, -0x07, 0x4a, -0x15, 0x6b, -0x1b, 0x7f, -0x1f, 0x7f, + -0x18, 0x7f, -0x12, 0x5f, -0x1b, 0x7f, -0x15, 0x72, -0x1e, 0x7f, -0x11, 0x7b, -0x0c, 0x73, -0x10, 0x7a, + -0x0b, 0x73, -0x0c, 0x3f, -0x02, 0x44, -0x0f, 0x54, -0x0d, 0x68, -0x03, 0x46, -0x08, 0x5d, -0x0a, 0x5a, + -0x1e, 0x7f, -0x01, 0x4a, -0x06, 0x61, -0x07, 0x5b, -0x14, 0x7f, -0x04, 0x38, -0x05, 0x52, -0x07, 0x4c, + -0x16, 0x7d, -0x07, 0x5d, -0x0b, 0x57, -0x03, 0x4d, -0x05, 0x47, -0x04, 0x3f, -0x04, 0x44, -0x0c, 0x54, + -0x07, 0x3e, -0x07, 0x41, 0x08, 0x3d, 0x05, 0x38, -0x02, 0x42, 0x01, 0x40, 0x00, 0x3d, -0x02, 0x4e, + 0x01, 0x32, 0x07, 0x34, 0x0a, 0x23, 0x00, 0x2c, 0x0b, 0x26, 0x01, 0x2d, 0x00, 0x2e, 0x05, 0x2c, + 0x1f, 0x11, 0x01, 0x33, 0x07, 0x32, 0x1c, 0x13, 0x10, 0x21, 0x0e, 0x3e, -0x0d, 0x6c, -0x0f, 0x64, + -0x0d, 0x65, -0x0d, 0x5b, -0x0c, 0x5e, -0x0a, 0x58, -0x10, 0x54, -0x0a, 0x56, -0x07, 0x53, -0x0d, 0x57, + -0x13, 0x5e, 0x01, 0x46, 0x00, 0x48, -0x05, 0x4a, 0x12, 0x3b, -0x08, 0x66, -0x0f, 0x64, 0x00, 0x5f, + -0x04, 0x4b, 0x02, 0x48, -0x0b, 0x4b, -0x03, 0x47, 0x0f, 0x2e, -0x0d, 0x45, 0x00, 0x3e, 0x00, 0x41, + 0x15, 0x25, -0x0f, 0x48, 0x09, 0x39, 0x10, 0x36, 0x00, 0x3e, 0x0c, 0x48, 0x18, 0x00, 0x0f, 0x09, + 0x08, 0x19, 0x0d, 0x12, 0x0f, 0x09, 0x0d, 0x13, 0x0a, 0x25, 0x0c, 0x12, 0x06, 0x1d, 0x14, 0x21, + 0x0f, 0x1e, 0x04, 0x2d, 0x01, 0x3a, 0x00, 0x3e, 0x07, 0x3d, 0x0c, 0x26, 0x0b, 0x2d, 0x0f, 0x27, + 0x0b, 0x2a, 0x0d, 0x2c, 0x10, 0x2d, 0x0c, 0x29, 0x0a, 0x31, 0x1e, 0x22, 0x12, 0x2a, 0x0a, 0x37, + 0x11, 0x33, 0x11, 0x2e, 0x00, 0x59, 0x1a, -0x13, 0x16, -0x11, 0x1a, -0x11, 0x1e, -0x19, 0x1c, -0x14, + 0x21, -0x17, 0x25, -0x1b, 0x21, -0x17, 0x28, -0x1c, 0x26, -0x11, 0x21, -0x0b, 0x28, -0x0f, 0x29, -0x06, + 0x26, 0x01, 0x29, 0x11, 0x1e, -0x06, 0x1b, 0x03, 0x1a, 0x16, 0x25, -0x10, 0x23, -0x04, 0x26, -0x08, + 0x26, -0x03, 0x25, 0x03, 0x26, 0x05, 0x2a, 0x00, 0x23, 0x10, 0x27, 0x16, 0x0e, 0x30, 0x1b, 0x25, + 0x15, 0x3c, 0x0c, 0x44, 0x02, 0x61, -0x03, 0x47, -0x06, 0x2a, -0x05, 0x32, -0x03, 0x36, -0x02, 0x3e, + 0x00, 0x3a, 0x01, 0x3f, -0x02, 0x48, -0x01, 0x4a, -0x09, 0x5b, -0x05, 0x43, -0x05, 0x1b, -0x03, 0x27, + -0x02, 0x2c, 0x00, 0x2e, -0x10, 0x40, -0x08, 0x44, -0x0a, 0x4e, -0x06, 0x4d, -0x0a, 0x56, -0x0c, 0x5c, + -0x0f, 0x37, -0x0a, 0x3c, -0x06, 0x3e, -0x04, 0x41, -0x0c, 0x49, -0x08, 0x4c, -0x07, 0x50, -0x09, 0x58, + -0x11, 0x6e, -0x0b, 0x61, -0x14, 0x54, -0x0b, 0x4f, -0x06, 0x49, -0x04, 0x4a, -0x0d, 0x56, -0x0d, 0x60, + -0x0b, 0x61, -0x13, 0x75, -0x08, 0x4e, -0x05, 0x21, -0x04, 0x30, -0x02, 0x35, -0x03, 0x3e, -0x0d, 0x47, + -0x0a, 0x4f, -0x0c, 0x56, -0x0d, 0x5a, -0x0e, 0x61, 0x00, 0x00, -0x06, 0x5d, -0x06, 0x54, -0x08, 0x4f, + 0x00, 0x42, -0x01, 0x47, 0x00, 0x3e, -0x02, 0x3c, -0x02, 0x3b, -0x05, 0x4b, -0x03, 0x3e, -0x04, 0x3a, + -0x09, 0x42, -0x01, 0x4f, 0x00, 0x47, 0x03, 0x44, 0x0a, 0x2c, -0x07, 0x3e, 0x0f, 0x24, 0x0e, 0x28, + 0x10, 0x1b, 0x0c, 0x1d, 0x01, 0x2c, 0x14, 0x24, 0x12, 0x20, 0x05, 0x2a, 0x01, 0x30, 0x0a, 0x3e, + 0x11, 0x2e, 0x09, 0x40, -0x0c, 0x68, -0x0b, 0x61, -0x10, 0x60, -0x07, 0x58, -0x08, 0x55, -0x07, 0x55, + -0x09, 0x55, -0x0d, 0x58, 0x04, 0x42, -0x03, 0x4d, -0x03, 0x4c, -0x06, 0x4c, 0x0a, 0x3a, -0x01, 0x4c, + -0x01, 0x53, -0x07, 0x63, -0x0e, 0x5f, 0x02, 0x5f, 0x00, 0x4c, -0x05, 0x4a, 0x00, 0x46, -0x0b, 0x4b, + 0x01, 0x44, 0x00, 0x41, -0x0e, 0x49, 0x03, 0x3e, 0x04, 0x3e, -0x01, 0x44, -0x0d, 0x4b, 0x0b, 0x37, + 0x05, 0x40, 0x0c, 0x46, 0x0f, 0x06, 0x06, 0x13, 0x07, 0x10, 0x0c, 0x0e, 0x12, 0x0d, 0x0d, 0x0b, + 0x0d, 0x0f, 0x0f, 0x10, 0x0c, 0x17, 0x0d, 0x17, 0x0f, 0x14, 0x0e, 0x1a, 0x0e, 0x2c, 0x11, 0x28, + 0x11, 0x2f, 0x18, 0x11, 0x15, 0x15, 0x19, 0x16, 0x1f, 0x1b, 0x16, 0x1d, 0x13, 0x23, 0x0e, 0x32, + 0x0a, 0x39, 0x07, 0x3f, -0x02, 0x4d, -0x04, 0x52, -0x03, 0x5e, 0x09, 0x45, -0x0c, 0x6d, 0x24, -0x23, + 0x24, -0x22, 0x20, -0x1a, 0x25, -0x1e, 0x2c, -0x20, 0x22, -0x12, 0x22, -0x0f, 0x28, -0x0f, 0x21, -0x07, + 0x23, -0x05, 0x21, 0x00, 0x26, 0x02, 0x21, 0x0d, 0x17, 0x23, 0x0d, 0x3a, 0x1d, -0x03, 0x1a, 0x00, + 0x16, 0x1e, 0x1f, -0x07, 0x23, -0x0f, 0x22, -0x03, 0x22, 0x03, 0x24, -0x01, 0x22, 0x05, 0x20, 0x0b, + 0x23, 0x05, 0x22, 0x0c, 0x27, 0x0b, 0x1e, 0x1d, 0x22, 0x1a, 0x1d, 0x27, 0x13, 0x42, 0x1f, 0x15, + 0x1f, 0x1f, 0x19, 0x32, -0x11, 0x78, -0x14, 0x70, -0x12, 0x72, -0x0b, 0x55, -0x0f, 0x5c, -0x0e, 0x59, + -0x1a, 0x47, -0x0f, 0x51, -0x0e, 0x50, 0x00, 0x44, -0x0e, 0x46, -0x18, 0x38, -0x17, 0x44, -0x18, 0x32, + -0x0b, 0x4a, 0x17, -0x0d, 0x1a, -0x0d, 0x28, -0x0f, 0x31, -0x0e, 0x2c, 0x03, 0x2d, 0x06, 0x2c, 0x22, + 0x21, 0x36, 0x13, 0x52, -0x03, 0x4b, -0x01, 0x17, 0x01, 0x22, 0x01, 0x2b, 0x00, 0x36, -0x02, 0x37, + 0x00, 0x3d, 0x01, 0x40, 0x00, 0x44, -0x09, 0x5c, -0x0e, 0x6a, -0x0d, 0x61, -0x0f, 0x5a, -0x0c, 0x5a, + -0x12, 0x58, -0x0a, 0x49, -0x09, 0x4f, -0x0e, 0x56, -0x0a, 0x49, -0x0a, 0x46, -0x0a, 0x45, -0x05, 0x42, + -0x09, 0x40, -0x05, 0x3a, 0x02, 0x3b, 0x15, -0x0a, 0x18, -0x0b, 0x1c, -0x08, 0x1c, -0x01, 0x1d, 0x03, + 0x1d, 0x09, 0x23, 0x14, 0x1d, 0x24, 0x0e, 0x43 +}; + +static const s8 h264_cabac_context_init_P0[920] = { + 0x14, -0x0f, 0x02, 0x36, 0x03, 0x4a, 0x14, -0x0f, 0x02, 0x36, 0x03, 0x4a, -0x1c, 0x7f, -0x17, 0x68, + -0x06, 0x35, -0x01, 0x36, 0x07, 0x33, 0x17, 0x21, 0x17, 0x02, 0x15, 0x00, 0x01, 0x09, 0x00, 0x31, + -0x25, 0x76, 0x05, 0x39, -0x0d, 0x4e, -0x0b, 0x41, 0x01, 0x3e, 0x0c, 0x31, -0x04, 0x49, 0x11, 0x32, + 0x12, 0x40, 0x09, 0x2b, 0x1d, 0x00, 0x1a, 0x43, 0x10, 0x5a, 0x09, 0x68, -0x2e, 0x7f, -0x14, 0x68, + 0x01, 0x43, -0x0d, 0x4e, -0x0b, 0x41, 0x01, 0x3e, -0x06, 0x56, -0x11, 0x5f, -0x06, 0x3d, 0x09, 0x2d, + -0x03, 0x45, -0x06, 0x51, -0x0b, 0x60, 0x06, 0x37, 0x07, 0x43, -0x05, 0x56, 0x02, 0x58, 0x00, 0x3a, + -0x03, 0x4c, -0x0a, 0x5e, 0x05, 0x36, 0x04, 0x45, -0x03, 0x51, 0x00, 0x58, -0x07, 0x43, -0x05, 0x4a, + -0x04, 0x4a, -0x05, 0x50, -0x07, 0x48, 0x01, 0x3a, 0x00, 0x29, 0x00, 0x3f, 0x00, 0x3f, 0x00, 0x3f, + -0x09, 0x53, 0x04, 0x56, 0x00, 0x61, -0x07, 0x48, 0x0d, 0x29, 0x03, 0x3e, 0x00, 0x2d, -0x04, 0x4e, + -0x03, 0x60, -0x1b, 0x7e, -0x1c, 0x62, -0x19, 0x65, -0x17, 0x43, -0x1c, 0x52, -0x14, 0x5e, -0x10, 0x53, + -0x16, 0x6e, -0x15, 0x5b, -0x12, 0x66, -0x0d, 0x5d, -0x1d, 0x7f, -0x07, 0x5c, -0x05, 0x59, -0x07, 0x60, + -0x0d, 0x6c, -0x03, 0x2e, -0x01, 0x41, -0x01, 0x39, -0x09, 0x5d, -0x03, 0x4a, -0x09, 0x5c, -0x08, 0x57, + -0x17, 0x7e, 0x05, 0x36, 0x06, 0x3c, 0x06, 0x3b, 0x06, 0x45, -0x01, 0x30, 0x00, 0x44, -0x04, 0x45, + -0x08, 0x58, -0x02, 0x55, -0x06, 0x4e, -0x01, 0x4b, -0x07, 0x4d, 0x02, 0x36, 0x05, 0x32, -0x03, 0x44, + 0x01, 0x32, 0x06, 0x2a, -0x04, 0x51, 0x01, 0x3f, -0x04, 0x46, 0x00, 0x43, 0x02, 0x39, -0x02, 0x4c, + 0x0b, 0x23, 0x04, 0x40, 0x01, 0x3d, 0x0b, 0x23, 0x12, 0x19, 0x0c, 0x18, 0x0d, 0x1d, 0x0d, 0x24, + -0x0a, 0x5d, -0x07, 0x49, -0x02, 0x49, 0x0d, 0x2e, 0x09, 0x31, -0x07, 0x64, 0x09, 0x35, 0x02, 0x35, + 0x05, 0x35, -0x02, 0x3d, 0x00, 0x38, 0x00, 0x38, -0x0d, 0x3f, -0x05, 0x3c, -0x01, 0x3e, 0x04, 0x39, + -0x06, 0x45, 0x04, 0x39, 0x0e, 0x27, 0x04, 0x33, 0x0d, 0x44, 0x03, 0x40, 0x01, 0x3d, 0x09, 0x3f, + 0x07, 0x32, 0x10, 0x27, 0x05, 0x2c, 0x04, 0x34, 0x0b, 0x30, -0x05, 0x3c, -0x01, 0x3b, 0x00, 0x3b, + 0x16, 0x21, 0x05, 0x2c, 0x0e, 0x2b, -0x01, 0x4e, 0x00, 0x3c, 0x09, 0x45, 0x0b, 0x1c, 0x02, 0x28, + 0x03, 0x2c, 0x00, 0x31, 0x00, 0x2e, 0x02, 0x2c, 0x02, 0x33, 0x00, 0x2f, 0x04, 0x27, 0x02, 0x3e, + 0x06, 0x2e, 0x00, 0x36, 0x03, 0x36, 0x02, 0x3a, 0x04, 0x3f, 0x06, 0x33, 0x06, 0x39, 0x07, 0x35, + 0x06, 0x34, 0x06, 0x37, 0x0b, 0x2d, 0x0e, 0x24, 0x08, 0x35, -0x01, 0x52, 0x07, 0x37, -0x03, 0x4e, + 0x0f, 0x2e, 0x16, 0x1f, -0x01, 0x54, 0x19, 0x07, 0x1e, -0x07, 0x1c, 0x03, 0x1c, 0x04, 0x20, 0x00, + 0x22, -0x01, 0x1e, 0x06, 0x1e, 0x06, 0x20, 0x09, 0x1f, 0x13, 0x1a, 0x1b, 0x1a, 0x1e, 0x25, 0x14, + 0x1c, 0x22, 0x11, 0x46, 0x01, 0x43, 0x05, 0x3b, 0x09, 0x43, 0x10, 0x1e, 0x12, 0x20, 0x12, 0x23, + 0x16, 0x1d, 0x18, 0x1f, 0x17, 0x26, 0x12, 0x2b, 0x14, 0x29, 0x0b, 0x3f, 0x09, 0x3b, 0x09, 0x40, + -0x01, 0x5e, -0x02, 0x59, -0x09, 0x6c, -0x06, 0x4c, -0x02, 0x2c, 0x00, 0x2d, 0x00, 0x34, -0x03, 0x40, + -0x02, 0x3b, -0x04, 0x46, -0x04, 0x4b, -0x08, 0x52, -0x11, 0x66, -0x09, 0x4d, 0x03, 0x18, 0x00, 0x2a, + 0x00, 0x30, 0x00, 0x37, -0x06, 0x3b, -0x07, 0x47, -0x0c, 0x53, -0x0b, 0x57, -0x1e, 0x77, 0x01, 0x3a, + -0x03, 0x1d, -0x01, 0x24, 0x01, 0x26, 0x02, 0x2b, -0x06, 0x37, 0x00, 0x3a, 0x00, 0x40, -0x03, 0x4a, + -0x0a, 0x5a, 0x00, 0x46, -0x04, 0x1d, 0x05, 0x1f, 0x07, 0x2a, 0x01, 0x3b, -0x02, 0x3a, -0x03, 0x48, + -0x03, 0x51, -0x0b, 0x61, 0x00, 0x3a, 0x08, 0x05, 0x0a, 0x0e, 0x0e, 0x12, 0x0d, 0x1b, 0x02, 0x28, + 0x00, 0x3a, -0x03, 0x46, -0x06, 0x4f, -0x08, 0x55, 0x00, 0x00, -0x0d, 0x6a, -0x10, 0x6a, -0x0a, 0x57, + -0x15, 0x72, -0x12, 0x6e, -0x0e, 0x62, -0x16, 0x6e, -0x15, 0x6a, -0x12, 0x67, -0x15, 0x6b, -0x17, 0x6c, + -0x1a, 0x70, -0x0a, 0x60, -0x0c, 0x5f, -0x05, 0x5b, -0x09, 0x5d, -0x16, 0x5e, -0x05, 0x56, 0x09, 0x43, + -0x04, 0x50, -0x0a, 0x55, -0x01, 0x46, 0x07, 0x3c, 0x09, 0x3a, 0x05, 0x3d, 0x0c, 0x32, 0x0f, 0x32, + 0x12, 0x31, 0x11, 0x36, 0x0a, 0x29, 0x07, 0x2e, -0x01, 0x33, 0x07, 0x31, 0x08, 0x34, 0x09, 0x29, + 0x06, 0x2f, 0x02, 0x37, 0x0d, 0x29, 0x0a, 0x2c, 0x06, 0x32, 0x05, 0x35, 0x0d, 0x31, 0x04, 0x3f, + 0x06, 0x40, -0x02, 0x45, -0x02, 0x3b, 0x06, 0x46, 0x0a, 0x2c, 0x09, 0x1f, 0x0c, 0x2b, 0x03, 0x35, + 0x0e, 0x22, 0x0a, 0x26, -0x03, 0x34, 0x0d, 0x28, 0x11, 0x20, 0x07, 0x2c, 0x07, 0x26, 0x0d, 0x32, + 0x0a, 0x39, 0x1a, 0x2b, 0x0e, 0x0b, 0x0b, 0x0e, 0x09, 0x0b, 0x12, 0x0b, 0x15, 0x09, 0x17, -0x02, + 0x20, -0x0f, 0x20, -0x0f, 0x22, -0x15, 0x27, -0x17, 0x2a, -0x21, 0x29, -0x1f, 0x2e, -0x1c, 0x26, -0x0c, + 0x15, 0x1d, 0x2d, -0x18, 0x35, -0x2d, 0x30, -0x1a, 0x41, -0x2b, 0x2b, -0x13, 0x27, -0x0a, 0x1e, 0x09, + 0x12, 0x1a, 0x14, 0x1b, 0x00, 0x39, -0x0e, 0x52, -0x05, 0x4b, -0x13, 0x61, -0x23, 0x7d, 0x1b, 0x00, + 0x1c, 0x00, 0x1f, -0x04, 0x1b, 0x06, 0x22, 0x08, 0x1e, 0x0a, 0x18, 0x16, 0x21, 0x13, 0x16, 0x20, + 0x1a, 0x1f, 0x15, 0x29, 0x1a, 0x2c, 0x17, 0x2f, 0x10, 0x41, 0x0e, 0x47, 0x08, 0x3c, 0x06, 0x3f, + 0x11, 0x41, 0x15, 0x18, 0x17, 0x14, 0x1a, 0x17, 0x1b, 0x20, 0x1c, 0x17, 0x1c, 0x18, 0x17, 0x28, + 0x18, 0x20, 0x1c, 0x1d, 0x17, 0x2a, 0x13, 0x39, 0x16, 0x35, 0x16, 0x3d, 0x0b, 0x56, 0x0c, 0x28, + 0x0b, 0x33, 0x0e, 0x3b, -0x04, 0x4f, -0x07, 0x47, -0x05, 0x45, -0x09, 0x46, -0x08, 0x42, -0x0a, 0x44, + -0x13, 0x49, -0x0c, 0x45, -0x10, 0x46, -0x0f, 0x43, -0x14, 0x3e, -0x13, 0x46, -0x10, 0x42, -0x16, 0x41, + -0x14, 0x3f, 0x09, -0x02, 0x1a, -0x09, 0x21, -0x09, 0x27, -0x07, 0x29, -0x02, 0x2d, 0x03, 0x31, 0x09, + 0x2d, 0x1b, 0x24, 0x3b, -0x06, 0x42, -0x07, 0x23, -0x07, 0x2a, -0x08, 0x2d, -0x05, 0x30, -0x0c, 0x38, + -0x06, 0x3c, -0x05, 0x3e, -0x08, 0x42, -0x08, 0x4c, -0x05, 0x55, -0x06, 0x51, -0x0a, 0x4d, -0x07, 0x51, + -0x11, 0x50, -0x12, 0x49, -0x04, 0x4a, -0x0a, 0x53, -0x09, 0x47, -0x09, 0x43, -0x01, 0x3d, -0x08, 0x42, + -0x0e, 0x42, 0x00, 0x3b, 0x02, 0x3b, 0x15, -0x0d, 0x21, -0x0e, 0x27, -0x07, 0x2e, -0x02, 0x33, 0x02, + 0x3c, 0x06, 0x3d, 0x11, 0x37, 0x22, 0x2a, 0x3e +}; + +#endif diff --git a/drivers/media/platform/ingenic/helix/helix_hw.h b/drivers/media/platform/ingenic/helix/helix_hw.h new file mode 100644 index 0000000000..208dcade1f --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_hw.h @@ -0,0 +1,263 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ + +#ifndef _HELIX_HW_H_ +#define _HELIX_HW_H_ + +#include +#include + +#define HELIX_INTERNAL_RAM 0x131f0000 +#define HELIX_TASK_SIZE 16384 + +/* Pipeline Scheduler Registers (0x00000) */ +#define REG_SCH_GLBC 0x00000 +#define REG_SCH_STAT 0x00034 +#define REG_SCH_SCHC 0x00060 +#define REG_SCH_BND 0x00064 +#define REG_VDMA_TASKRG_T21 0x00084 + +#define SCH_STAT_ENDFLAG BIT(0) +#define SCH_STAT_ACFGERR BIT(2) +#define SCH_STAT_JPGEND BIT(4) +#define SCH_STAT_BSERR BIT(7) +#define SCH_STAT_ORESERR BIT(10) + +#define SCH_GLBC_HIAXI BIT(9) +#define SCH_INTE_ENDF BIT(16) +#define SCH_INTE_BSERR BIT(17) +#define SCH_INTE_TLBERR BIT(18) +#define SCH_INTE_ACFGERR BIT(20) +#define SCH_INTE_RESERR BIT(29) +#define SCH_INTE_MASK (SCH_INTE_RESERR | (0x1f << 16)) + +/* VDMA Control & Descriptors (0x10000) */ +#define REG_VDMA_LOCK 0x10000 +#define REG_VDMA_UNLK 0x10004 +#define REG_VDMA_TASKRG 0x10008 +#define REG_VDMA_TASKST 0x1000C +#define REG_VDMA_CH0_CNT 0x10010 +#define REG_VDMA_REF_Y_BA 0x10014 +#define REG_VDMA_REF_C_BA 0x10018 +#define REG_VDMA_STRIDE 0x1001c +#define REG_VDMA_CFG0 0x10020 +#define REG_VDMA_CFG1 0x10024 +#define REG_VDMA_CFG2 0x10028 +#define REG_VDMA_CFG3 0x1002c + +#define VDMA_ACFG_VLD BIT(31) +#define VDMA_ACFG_TERM BIT(30) +#define VDMA_ACFG_RUN 0x1 +#define VDMA_ACFG_CLR 0x8 +#define VDMA_ACFG_SAFE 0x4 +#define VDMA_ACFG_ERR BIT(3) +#define VDMA_ACFG_END BIT(2) +#define VDMA_ACFG_DHA(a) (((u32)(a)) & ~0x7fU) +#define VDMA_ACFG_IDX(a) (((u32)(a)) & 0xffffc) + +/* Output DMA (ODMA) Registers (0x30000) */ +#define REG_ODMA_GLB_TRIG 0x30000 +#define REG_ODMA_GLB_CTRL 0x30004 +#define REG_ODMA_TLC_BA 0x30018 +#define REG_ODMA_CFG0 0x30024 +#define REG_ODMA_AXI_CTRL 0x30040 +#define REG_ODMA_RAW_Y_BA 0x3004c +#define REG_ODMA_RAW_C_BA 0x30050 +#define REG_ODMA_BUF2_BA 0x30054 +#define REG_ODMA_BUF3_BA 0x30058 + +/* Encoding Frontend (EFE) Registers (0x40000) */ +#define REG_EFE_CTRL 0x40000 +#define REG_EFE_GEOM 0x40004 +#define REG_EFE_COEF_BA 0x4000C +#define REG_EFE_RAWY_SBA 0x40010 +#define REG_EFE_RAWC_SBA 0x40014 +#define REG_EFE_TOPMV_BA 0x40018 +#define REG_EFE_TOPPA_BA 0x4001C +#define REG_EFE_MECHN_BA 0x40020 +#define REG_EFE_MAUCHN_BA 0x40024 +#define REG_EFE_DBLKCHN_BA 0x40028 +#define REG_EFE_SDECHN_BA 0x4002C +#define REG_EFE_RAW_DBA 0x40030 +#define REG_EFE_RAWV_SBA 0x40034 +#define REG_EFE_RAW_STRD 0x40038 +#define REG_EFE_ROI_MAX_QP 0x40040 +#define REG_EFE_ROI_BASE_INFO0 0x40044 +#define REG_EFE_ROI_BASE_INFO1 0x40048 +#define REG_EFE_ROI_POS_INFO0 0x4004C +#define REG_EFE_QP_GEN_TAB 0x4006C +#define REG_EFE_QPG_CTRL 0x40074 +#define REG_EFE_CRP_FILT 0x40078 +#define REG_EFE_CRP_THR0 0x4007C +#define REG_EFE_CRP_THR1 0x40080 +#define REG_EFE_CRP_THR2 0x40084 +#define REG_EFE_CRP_THR3 0x40088 +#define REG_EFE_CRP_MB_OFST0 0x4008C +#define REG_EFE_CRP_MB_OFST1 0x40090 +#define REG_EFE_SAS_THDG2 0x400AC +#define REG_EFE_SAS_MB_OFSTA 0x400B0 +#define REG_EFE_SAS_MB_OFSTB 0x400B4 +#define REG_EFE_SAS_CNT 0x400B8 +#define REG_EFE_SAS_MCNT0 0x400BC +#define REG_EFE_SAS_MCNT1 0x400C0 +#define REG_EFE_QP_SUM 0x400C4 +#define REG_EFE_SAS_FLT_THR 0x400C8 +#define REG_EFE_ME_CFG0 0x400cc +#define REG_EFE_ME_CFG1 0x400d0 +#define REG_EFE_ME_CFG2 0x400d4 +#define REG_EFE_ME_CFG3 0x400d8 +#define REG_EFE_ME_LAMBDA0 0x400dc +#define REG_EFE_ME_LAMBDA1 0x400e0 +#define REG_EFE_ME_LAMBDA2 0x400e4 +#define REG_EFE_ME_CFG4 0x400e8 +#define REG_EFE_ME_CFG5 0x400ec +#define REG_EFE_ME_CFG6 0x400f0 +#define REG_EFE_SSAD 0x40108 +#define REG_EFE_DCS 0x4010C +#define REG_EFE_CQP_OFST 0x40120 +#define REG_EFE_INTRA_CFG0 0x40170 +#define REG_EFE_INTRA_CFG1 0x40174 + +#define EFE_RUN BIT(0) +#define EFE_EN BIT(1) +#define EFE_PLANE_NV12 (0x2 << 2) +#define EFE_PLANE_NV21 (0x3 << 2) +#define EFE_ID_JPEG (0x1 << 14) +#define EFE_CTRL_NV12 (EFE_ID_JPEG | EFE_PLANE_NV12 | EFE_EN | EFE_RUN) /* 0x400b */ +#define EFE_CTRL_NV21 (EFE_ID_JPEG | EFE_PLANE_NV21 | EFE_EN | EFE_RUN) /* 0x400f */ + +/* Motion Compensation & Estimation (MCE) Registers (0x50000) */ +#define REG_MCE_GLB_CTRL 0x50000 +#define REG_MCE_COMP_CTRL 0x50010 +#define REG_MCE_ESTI_CTRL 0x50040 +#define REG_MCE_MRGI 0x50044 +#define REG_MCE_MVR 0x50048 +#define REG_MCE_FSC 0x5004C +#define REG_MCE_FRM_SIZE 0x50060 +#define REG_MCE_FRM_STRD 0x50064 +#define REG_MCE_SLC_SPOS 0x50068 +#define REG_MCE_REF_Y_BA 0x5006c +#define REG_MCE_REF_C_BA 0x50070 +#define REG_MCE_CFG0 0x50074 +#define REG_MCE_CFG1 0x50078 +#define REG_MCE_CFG2 0x5007c +#define REG_MCE_TMV_BA 0x50104 +#define REG_MCE_CHN_SYNC 0x50108 +#define REG_MCE_CHN_BA 0x5010C +#define REG_MCE_SRC_Y_BA 0x50110 +#define REG_MCE_SRC_C_BA 0x50114 + +/* Reconstruction Frame Buffer (JRFD) Registers (0x60000) */ +#define REG_JRFD_GLB_CTRL 0x60000 +#define REG_JRFD_GEOM 0x60004 +#define REG_JRFD_Y_BA 0x60008 +#define REG_JRFD_C_BA 0x6000C +#define REG_JRFD_STRD 0x60010 +#define REG_JRFD_CFG0 0x60014 + +/* Deblocking Filter (DBLK) Registers (0x70000) */ +#define REG_DBLK_CFG 0x7005c /* undocumented; programmed with stock value */ +#define REG_DBLK_TRIG 0x70060 +#define REG_DBLK_GSTA 0x70070 +#define DBLK_STAT_DOEND BIT(0) + +/* Video Macroblock Analysis Unit (VMAU) Registers (0x80000) */ +#define REG_VMAU_NCCHN_ADDR 0x8000C +#define REG_VMAU_TOP_BASE 0x80028 +#define REG_VMAU_CTX 0x8002c +#define REG_VMAU_MD_CFG0 0x80030 +#define REG_VMAU_MD_CFG1 0x80034 +#define REG_VMAU_MD_CFG2 0x80038 +#define REG_VMAU_MD_CFG3 0x8003c +#define REG_VMAU_GBL_RUN 0x80040 +#define REG_VMAU_GBL_CTR 0x80044 +#define REG_VMAU_VIDEO_TYPE 0x80050 +#define REG_VMAU_Y_GS 0x80054 +#define REG_VMAU_DEC_DONE 0x80058 +#define REG_VMAU_ENC_DONE 0x8005C +#define REG_VMAU_DEADZONE 0x80078 +#define REG_VMAU_ACMASK 0x8007C +#define REG_VMAU_MD_CFG4 0x800d8 +#define REG_VMAU_MD_CFG5 0x800dc +#define REG_VMAU_MD_CFG6 0x800fc +#define REG_VMAU_MD_CFG7 0x80114 +#define REG_VMAU_MD_CFG8 0x80118 +#define REG_VMAU_MD_CFG9 0x8011c +#define REG_VMAU_CABAC_RUN1 0x80174 +#define REG_VMAU_FIFO_CTRL 0x80194 +#define REG_VMAU_FIFO_DATA 0x80198 +#define REG_VMAU_FIFO_CFG 0x8019c +#define REG_VMAU_SDE_CFG0 0x801c0 +#define REG_VMAU_SDE_CFG1 0x801c4 +#define REG_VMAU_SDE_CFG2 0x801c8 +#define REG_VMAU_SDE_CFG3 0x801cc +#define REG_VMAU_SDE_CFG4 0x801d0 +#define REG_VMAU_SDE_CFG5 0x801d4 +#define REG_VMAU_CFG7 0x801e0 +#define REG_VMAU_CFG8 0x801e4 +#define REG_VMAU_RC_MAD_RAM 0x80800 +#define REG_VMAU_IQUANT_BASE 0x80900 +#define REG_VMAU_QT 0x80980 + +/* Syntax Decoder/Encoder (SDE) Registers (0x90000) */ +#define REG_SDE_STAT 0x90000 +#define REG_SDE_SL_CTRL 0x90004 +#define REG_SDE_SL_GEOM 0x90008 +#define REG_SDE_GL_CTRL 0x9000C +#define REG_SDE_CODEC_ID 0x90010 +#define REG_SDE_CFG0 0x90014 +#define REG_SDE_CFG1 0x90018 +#define REG_SDE_CFG2 0x9001C +#define REG_SDE_CFG3 0x90020 +#define REG_SDE_CFG4 0x90024 +#define REG_SDE_CFG5 0x90028 +#define REG_SDE_CFG9 0x90038 +#define REG_SDE_CTX_TBL 0x92000 +#define SDE_STAT_BSEND BIT(1) + +/* TCSM Registers (0xB0000) */ +#define REG_TCSM_CTRL 0xB0000 +#define REG_TCSM_GEOM 0xB0004 +#define REG_TCSM_Y_BA 0xB0008 +#define REG_TCSM_C_BA 0xB000C +#define REG_TCSM_STRD 0xB0010 +#define REG_TCSM_REF_Y_BA 0xB0014 +#define REG_TCSM_REF_C_BA 0xB0018 +#define REG_TCSM_CFG0 0xB001c +#define REG_TCSM_CFG1 0xB0020 +#define REG_TCSM_SRC_Y_BA 0xB0030 +#define REG_TCSM_SRC_C_BA 0xB0034 +#define REG_TCSM_FLUSH 0xc0000 + +/* JPEG Codec (JPGC) Registers (0xE0000) */ +#define REG_JPGC_TRIG 0xE0000 +#define REG_JPGC_GLBI 0xE0004 +#define REG_JPGC_STAT 0xE0008 +#define REG_JPGC_BSA 0xE000C +#define REG_JPGC_P0A 0xE0010 +#define REG_JPGC_P1A 0xE0014 +#define REG_JPGC_P2A 0xE0018 +#define REG_JPGC_NMCU 0xE0028 +#define REG_JPGC_NRSM 0xE002C +#define REG_JPGC_P0C 0xE0030 +#define REG_JPGC_P1C 0xE0034 +#define REG_JPGC_P2C 0xE0038 +#define REG_JPGC_MAX_BS 0xE0068 +#define REG_JPGC_QMEM 0xE1400 +#define REG_JPGC_HUFE 0xE1800 + +#define JPGC_STAT_ENDF BIT(31) +#define JPGC_STAT_LEN_MASK 0x00FFFFFF + +#define JPGC_CORE_OPEN BIT(0) +#define JPGC_BS_TRIG BIT(1) +#define JPGC_PP_TRIG BIT(2) +#define JPGC_TRIG_ENCODE (JPGC_CORE_OPEN | JPGC_BS_TRIG | JPGC_PP_TRIG) /* 0x07 */ + +#define JPGC_P0C_DEFAULT 0x30 +#define JPGC_P1C_DEFAULT 7 +#define JPGC_P2C_DEFAULT 7 +#define JPGC_GLBI_INIT 1 +#define JPGC_GLBI_RUN 0x000a0121 + +#endif /* _HELIX_HW_H_ */ diff --git a/drivers/media/platform/ingenic/helix/helix_jpeg.c b/drivers/media/platform/ingenic/helix/helix_jpeg.c new file mode 100644 index 0000000000..a7ca9a77f1 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_jpeg.c @@ -0,0 +1,327 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include +#include +#include +#include + +#include "helix_drv.h" +#include "helix_tables.h" + +static const u8 dc_lum_bits[16] = { + 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 +}; +static const u8 dc_lum_vals[12] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 +}; + +static const u8 dc_chr_bits[16] = { + 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 +}; +static const u8 dc_chr_vals[12] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 +}; + +static const u8 ac_lum_bits[16] = { + 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d +}; +static const u8 ac_lum_vals[162] = { + 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, + 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, + 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, + 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, + 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, + 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, + 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, + 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, + 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, + 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, + 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, + 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, + 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, + 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, + 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, + 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa +}; + +static const u8 ac_chr_bits[16] = { + 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 +}; +static const u8 ac_chr_vals[162] = { + 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, + 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, + 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, + 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, + 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, + 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, + 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, + 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, + 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, + 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, + 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, + 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, + 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, + 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, + 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, + 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa +}; + +static const u32 helix_hufe_table[384] = { + 0x00000100, 0x00000101, 0x00000204, 0x0000030b, 0x0000041a, 0x00000678, 0x000007f8, 0x000009f6, + 0x00000f82, 0x00000f83, 0x0000030c, 0x0000041b, 0x00000679, 0x000008f6, 0x00000af6, 0x00000f84, + 0x00000f85, 0x00000f86, 0x00000f87, 0x00000f88, 0x0000041c, 0x000007f9, 0x000009f7, 0x00000bf4, + 0x00000f89, 0x00000f8a, 0x00000f8b, 0x00000f8c, 0x00000f8d, 0x00000f8e, 0x0000053a, 0x000008f7, + 0x00000bf5, 0x00000f8f, 0x00000f90, 0x00000f91, 0x00000f92, 0x00000f93, 0x00000f94, 0x00000f95, + 0x0000053b, 0x000009f8, 0x00000f96, 0x00000f97, 0x00000f98, 0x00000f99, 0x00000f9a, 0x00000f9b, + 0x00000f9c, 0x00000f9d, 0x0000067a, 0x00000af7, 0x00000f9e, 0x00000f9f, 0x00000fa0, 0x00000fa1, + 0x00000fa2, 0x00000fa3, 0x00000fa4, 0x00000fa5, 0x0000067b, 0x00000bf6, 0x00000fa6, 0x00000fa7, + 0x00000fa8, 0x00000fa9, 0x00000faa, 0x00000fab, 0x00000fac, 0x00000fad, 0x000007fa, 0x00000bf7, + 0x00000fae, 0x00000faf, 0x00000fb0, 0x00000fb1, 0x00000fb2, 0x00000fb3, 0x00000fb4, 0x00000fb5, + 0x000008f8, 0x00000ec0, 0x00000fb6, 0x00000fb7, 0x00000fb8, 0x00000fb9, 0x00000fba, 0x00000fbb, + 0x00000fbc, 0x00000fbd, 0x000008f9, 0x00000fbe, 0x00000fbf, 0x00000fc0, 0x00000fc1, 0x00000fc2, + 0x00000fc3, 0x00000fc4, 0x00000fc5, 0x00000fc6, 0x000008fa, 0x00000fc7, 0x00000fc8, 0x00000fc9, + 0x00000fca, 0x00000fcb, 0x00000fcc, 0x00000fcd, 0x00000fce, 0x00000fcf, 0x000009f9, 0x00000fd0, + 0x00000fd1, 0x00000fd2, 0x00000fd3, 0x00000fd4, 0x00000fd5, 0x00000fd6, 0x00000fd7, 0x00000fd8, + 0x000009fa, 0x00000fd9, 0x00000fda, 0x00000fdb, 0x00000fdc, 0x00000fdd, 0x00000fde, 0x00000fdf, + 0x00000fe0, 0x00000fe1, 0x00000af8, 0x00000fe2, 0x00000fe3, 0x00000fe4, 0x00000fe5, 0x00000fe6, + 0x00000fe7, 0x00000fe8, 0x00000fe9, 0x00000fea, 0x00000feb, 0x00000fec, 0x00000fed, 0x00000fee, + 0x00000fef, 0x00000ff0, 0x00000ff1, 0x00000ff2, 0x00000ff3, 0x00000ff4, 0x00000ff5, 0x00000ff6, + 0x00000ff7, 0x00000ff8, 0x00000ff9, 0x00000ffa, 0x00000ffb, 0x00000ffc, 0x00000ffd, 0x00000ffe, + 0x0000030a, 0x00000af9, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, + 0x00000fd0, 0x00000fd1, 0x00000fd2, 0x00000fd3, 0x00000fd4, 0x00000fd5, 0x00000fd6, 0x00000fd7, + 0x00000101, 0x00000204, 0x0000030a, 0x00000418, 0x00000419, 0x00000538, 0x00000678, 0x000008f4, + 0x000009f6, 0x00000bf4, 0x0000030b, 0x00000539, 0x000007f6, 0x000008f5, 0x00000af6, 0x00000bf5, + 0x00000f88, 0x00000f89, 0x00000f8a, 0x00000f8b, 0x0000041a, 0x000007f7, 0x000009f7, 0x00000bf6, + 0x00000ec2, 0x00000f8c, 0x00000f8d, 0x00000f8e, 0x00000f8f, 0x00000f90, 0x0000041b, 0x000007f8, + 0x000009f8, 0x00000bf7, 0x00000f91, 0x00000f92, 0x00000f93, 0x00000f94, 0x00000f95, 0x00000f96, + 0x0000053a, 0x000008f6, 0x00000f97, 0x00000f98, 0x00000f99, 0x00000f9a, 0x00000f9b, 0x00000f9c, + 0x00000f9d, 0x00000f9e, 0x0000053b, 0x000009f9, 0x00000f9f, 0x00000fa0, 0x00000fa1, 0x00000fa2, + 0x00000fa3, 0x00000fa4, 0x00000fa5, 0x00000fa6, 0x00000679, 0x00000af7, 0x00000fa7, 0x00000fa8, + 0x00000fa9, 0x00000faa, 0x00000fab, 0x00000fac, 0x00000fad, 0x00000fae, 0x0000067a, 0x00000af8, + 0x00000faf, 0x00000fb0, 0x00000fb1, 0x00000fb2, 0x00000fb3, 0x00000fb4, 0x00000fb5, 0x00000fb6, + 0x000007f9, 0x00000fb7, 0x00000fb8, 0x00000fb9, 0x00000fba, 0x00000fbb, 0x00000fbc, 0x00000fbd, + 0x00000fbe, 0x00000fbf, 0x000008f7, 0x00000fc0, 0x00000fc1, 0x00000fc2, 0x00000fc3, 0x00000fc4, + 0x00000fc5, 0x00000fc6, 0x00000fc7, 0x00000fc8, 0x000008f8, 0x00000fc9, 0x00000fca, 0x00000fcb, + 0x00000fcc, 0x00000fcd, 0x00000fce, 0x00000fcf, 0x00000fd0, 0x00000fd1, 0x000008f9, 0x00000fd2, + 0x00000fd3, 0x00000fd4, 0x00000fd5, 0x00000fd6, 0x00000fd7, 0x00000fd8, 0x00000fd9, 0x00000fda, + 0x000008fa, 0x00000fdb, 0x00000fdc, 0x00000fdd, 0x00000fde, 0x00000fdf, 0x00000fe0, 0x00000fe1, + 0x00000fe2, 0x00000fe3, 0x00000af9, 0x00000fe4, 0x00000fe5, 0x00000fe6, 0x00000fe7, 0x00000fe8, + 0x00000fe9, 0x00000fea, 0x00000feb, 0x00000fec, 0x00000de0, 0x00000fed, 0x00000fee, 0x00000fef, + 0x00000ff0, 0x00000ff1, 0x00000ff2, 0x00000ff3, 0x00000ff4, 0x00000ff5, 0x00000ec3, 0x00000ff6, + 0x00000ff7, 0x00000ff8, 0x00000ff9, 0x00000ffa, 0x00000ffb, 0x00000ffc, 0x00000ffd, 0x00000ffe, + 0x00000100, 0x000009fa, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, + 0x00000fd0, 0x00000fd1, 0x00000fd2, 0x00000fd3, 0x00000fd4, 0x00000fd5, 0x00000fd6, 0x00000fd7, + 0x00000100, 0x00000202, 0x00000203, 0x00000204, 0x00000205, 0x00000206, 0x0000030e, 0x0000041e, + 0x0000053e, 0x0000067e, 0x000007fe, 0x000008fe, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, + 0x00000100, 0x00000101, 0x00000102, 0x00000206, 0x0000030e, 0x0000041e, 0x0000053e, 0x0000067e, + 0x000007fe, 0x000008fe, 0x000009fe, 0x00000afe, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, +}; + +void helix_calc_qtable(u32 quality, u8 *qtable, u32 *qmem) +{ + u32 scale; + unsigned int i; + + quality = clamp_t(u32, quality, 1, 100); + scale = (quality < 50) ? (5000 / quality) : (200 - 2 * quality); + + for (i = 0; i < 64; i++) { + u8 idx = helix_zigzag[i]; + u32 val = (helix_base_lum_qtable[idx] * scale + 50) / 100; + qtable[i] = clamp_t(u32, val, 1, 255); + } + for (i = 0; i < 64; i++) { + u8 idx = helix_zigzag[i]; + u32 val = (helix_base_chr_qtable[idx] * scale + 50) / 100; + qtable[64 + i] = clamp_t(u32, val, 1, 255); + } + + for (i = 0; i < 128; i++) + qmem[i] = stock_qmem_lut[qtable[i]]; + for (i = 128; i < 256; i++) + qmem[i] = 0; +} + +static const u8 jpeg_header_jfif[20] = { + 0xFF, 0xD8, /* SOI */ + 0xFF, 0xE0, 0x00, 0x10, /* APP0, len 16 */ + 'J', 'F', 'I', 'F', 0x00, /* identifier */ + 0x01, 0x01, /* version 1.1 */ + 0x00, /* density units */ + 0x00, 0x01, 0x00, 0x01, /* X/Y density (1x1) */ + 0x00, 0x00, /* thumbnail (0x0) */ +}; + +static const u8 jpeg_sos_header[14] = { + 0xFF, 0xDA, 0x00, 0x0C, /* SOS, len 12 */ + 0x03, /* 3 components */ + 0x01, 0x00, /* Y: DC 0, AC 0 */ + 0x02, 0x11, /* Cb: DC 1, AC 1 */ + 0x03, 0x11, /* Cr: DC 1, AC 1 */ + 0x00, 0x3F, 0x00, /* spectral selection: 0..63, AH/AL 0 */ +}; + +static void jpeg_write_dqt(u8 **dst, u8 table_id, const u8 *qtable) +{ + u8 *p = *dst; + + *p++ = 0xFF; + *p++ = 0xDB; + put_unaligned_be16(67, p); + p += 2; + *p++ = table_id; + memcpy(p, qtable, 64); + p += 64; + *dst = p; +} + +static void jpeg_write_sof0(u8 **dst, u16 height, u16 width) +{ + static const u8 sof0_components[9] = { + 0x01, 0x22, 0x00, /* Y: 2x2 sampling, DQT 0 */ + 0x02, 0x11, 0x01, /* Cb: 1x1 sampling, DQT 1 */ + 0x03, 0x11, 0x01, /* Cr: 1x1 sampling, DQT 1 */ + }; + u8 *p = *dst; + + *p++ = 0xFF; + *p++ = 0xC0; + put_unaligned_be16(2 + 1 + 2 + 2 + 1 + sizeof(sof0_components), p); + p += 2; + *p++ = 8; + put_unaligned_be16(height, p); + p += 2; + put_unaligned_be16(width, p); + p += 2; + *p++ = 3; + memcpy(p, sof0_components, sizeof(sof0_components)); + p += sizeof(sof0_components); + *dst = p; +} + +static void jpeg_write_dht(u8 **dst, u8 class_id, const u8 *bits, size_t nbits, + const u8 *vals, size_t nvals) +{ + u8 *p = *dst; + + *p++ = 0xFF; + *p++ = 0xC4; + put_unaligned_be16(2 + 1 + nbits + nvals, p); + p += 2; + *p++ = class_id; + memcpy(p, bits, nbits); + p += nbits; + memcpy(p, vals, nvals); + p += nvals; + *dst = p; +} + +size_t helix_jpeg_write_headers(u8 *buf, u32 width, u32 height, + const u8 *qtable, u32 visible_width, u32 visible_height) +{ + const u8 *lum_q = qtable ? qtable : helix_qtable; + const u8 *chr_q = qtable ? (qtable + 64) : (helix_qtable + 64); + u32 vw = visible_width ? visible_width : width; + u32 vh = visible_height ? visible_height : height; + u8 *p = buf; + + memcpy(p, jpeg_header_jfif, sizeof(jpeg_header_jfif)); + p += sizeof(jpeg_header_jfif); + + jpeg_write_dqt(&p, 0, lum_q); + jpeg_write_dqt(&p, 1, chr_q); + + jpeg_write_sof0(&p, (u16)vh, (u16)vw); + + jpeg_write_dht(&p, 0x00, dc_lum_bits, sizeof(dc_lum_bits), + dc_lum_vals, sizeof(dc_lum_vals)); + jpeg_write_dht(&p, 0x10, ac_lum_bits, sizeof(ac_lum_bits), + ac_lum_vals, sizeof(ac_lum_vals)); + jpeg_write_dht(&p, 0x01, dc_chr_bits, sizeof(dc_chr_bits), + dc_chr_vals, sizeof(dc_chr_vals)); + jpeg_write_dht(&p, 0x11, ac_chr_bits, sizeof(ac_chr_bits), + ac_chr_vals, sizeof(ac_chr_vals)); + + memcpy(p, jpeg_sos_header, sizeof(jpeg_sos_header)); + p += sizeof(jpeg_sos_header); + + return (size_t)(p - buf); +} + +void helix_jpeg_write_eoi(u8 *buf, size_t offset) +{ + buf[offset] = 0xFF; + buf[offset + 1] = 0xD9; +} + +int helix_jpeg_hw_start(struct helix_dev *hdev, + dma_addr_t src_y_dma, dma_addr_t src_uv_dma, + dma_addr_t dst_dma, u32 width, u32 height, + u32 y_stride, u32 uv_stride, u32 fourcc, + const u32 *qmem) +{ + struct helix_vdma_task task; + const u32 *qm = qmem ? qmem : helix_qmem; + u32 efe_ctrl; + unsigned int i; + + if ((fourcc != V4L2_PIX_FMT_NV12 && fourcc != V4L2_PIX_FMT_NV21) || + width & 15 || height & 15 || + y_stride != width || uv_stride != width || + (src_y_dma | src_uv_dma | dst_dma) & 127 || + hdev->bounce_size != HELIX_BOUNCE_BUF_SIZE) + return -EINVAL; + + efe_ctrl = (fourcc == V4L2_PIX_FMT_NV21) ? EFE_CTRL_NV21 : EFE_CTRL_NV12; + + helix_vdma_task_init(&task, hdev->task_vaddr, hdev->task_paddr, + HELIX_TASK_SIZE); + + helix_vdma_append(&task, REG_TCSM_FLUSH, 0); + helix_vdma_append(&task, REG_JPGC_GLBI, JPGC_GLBI_INIT); + + for (i = 0; i < ARRAY_SIZE(helix_hufe_table); i++) + helix_vdma_append(&task, REG_JPGC_HUFE + i * 4, + helix_hufe_table[i]); + + for (i = 0; i < 256; i++) + helix_vdma_append(&task, REG_JPGC_QMEM + i * 4, qm[i]); + + helix_vdma_append(&task, REG_JPGC_STAT, 0); + helix_vdma_append(&task, REG_JPGC_BSA, dst_dma); + helix_vdma_append(&task, REG_JPGC_P0A, HELIX_INTERNAL_RAM); + helix_vdma_append(&task, REG_JPGC_NMCU, + (width / 16) * (height / 16) - 1); + helix_vdma_append(&task, REG_JPGC_NRSM, 0); + helix_vdma_append(&task, REG_JPGC_P0C, JPGC_P0C_DEFAULT); + helix_vdma_append(&task, REG_JPGC_P1C, JPGC_P1C_DEFAULT); + helix_vdma_append(&task, REG_JPGC_P2C, JPGC_P2C_DEFAULT); + helix_vdma_append(&task, REG_JPGC_MAX_BS, HELIX_BOUNCE_BUF_SIZE); + helix_vdma_append(&task, REG_JPGC_GLBI, JPGC_GLBI_RUN); + helix_vdma_append(&task, REG_JPGC_TRIG, JPGC_TRIG_ENCODE); + + helix_vdma_append(&task, REG_EFE_GEOM, + ((height / 16 - 1) << 16) | (width / 16 - 1)); + helix_vdma_append(&task, REG_EFE_RAWY_SBA, src_y_dma); + helix_vdma_append(&task, REG_EFE_RAWC_SBA, src_uv_dma); + helix_vdma_append(&task, REG_EFE_RAWV_SBA, 0); + helix_vdma_append(&task, REG_EFE_RAW_STRD, + (y_stride << 16) | uv_stride); + helix_vdma_append(&task, REG_EFE_RAW_DBA, HELIX_INTERNAL_RAM); + helix_vdma_append(&task, REG_EFE_CTRL, efe_ctrl); + + helix_vdma_terminate(&task); + + return helix_vdma_kick(hdev, &task); +} diff --git a/drivers/media/platform/ingenic/helix/helix_rc.c b/drivers/media/platform/ingenic/helix/helix_rc.c new file mode 100644 index 0000000000..558dd69690 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_rc.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/* + * Frame-level H.264 CBR/VBR rate control. Allocates bit budget per GOP + * window based on frame complexity and regulates QP step sizes. + */ +#include +#include +#include + +#include "helix_rc.h" + +#define RC_I 0 +#define RC_P 1 +/* P frames get a Qstep 2^(2/6) above the I frame, in Q8 */ +#define RC_KP 323 + +/* H.264 Qstep in Q8: 0.625 at QP 0, doubling every 6 */ +static u32 rc_qstep(u32 qp) +{ + static const u16 base[6] = { 160, 176, 208, 224, 256, 288 }; + + return base[qp % 6] << (qp / 6); +} + +/* The QP whose Qstep is nearest @q on a log scale */ +static u32 rc_qp_for_qstep(u64 q) +{ + u32 qp; + + for (qp = 0; qp < 51 && rc_qstep(qp) < q; qp++) + ; + if (qp && q * q < (u64)rc_qstep(qp) * rc_qstep(qp - 1)) + qp--; + return qp; +} + +static u64 rc_frame_bits(const struct helix_rc *rc, u32 bitrate) +{ + return max_t(u64, div_u64((u64)bitrate * rc->tpf_num, rc->tpf_den), 1); +} + +u32 helix_rc_fps(const struct helix_rc *rc) +{ + return max(DIV_ROUND_CLOSEST(rc->tpf_den, rc->tpf_num), 1U); +} + +void helix_rc_init(struct helix_rc *rc) +{ + rc->enabled = false; + rc->mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR; + rc->bitrate = 2000000; + rc->peak = 4000000; + rc->tpf_num = 1; + rc->tpf_den = 25; + helix_rc_reset(rc); +} + +void helix_rc_reset(struct helix_rc *rc) +{ + rc->started = false; + rc->complexity[RC_I] = 0; + rc->complexity[RC_P] = 0; + rc->left = 0; + rc->frames = 0; + rc->up = 2; +} + +static void rc_start_window(struct helix_rc *rc, u32 frames) +{ + s64 budget = rc_frame_bits(rc, rc->bitrate); + s64 second = budget * helix_rc_fps(rc); + s64 carry = rc->started ? rc->left - budget * rc->frames : 0; + s64 total; + + if (rc->mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) + carry = clamp(carry, -2 * second, second / 2); + else + carry = clamp(carry, -2 * second, 4 * second); + total = budget * frames + carry; + if (rc->mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) + total = min_t(s64, total, + rc_frame_bits(rc, max(rc->peak, rc->bitrate)) * frames); + rc->left = max(total, budget * frames / 4); + rc->frames = frames; + rc->started = true; +} + +u32 helix_rc_qp(struct helix_rc *rc, bool idr, u32 window, u32 init_qp, + u32 min_qp, u32 max_qp) +{ + int type = idr ? RC_I : RC_P; + s64 budget = rc_frame_bits(rc, rc->bitrate); + u64 floor = max_t(s64, budget / 8, 1); + u64 xi, xp, w; + s64 target; + u32 qp; + + if (idr || !rc->started || !rc->frames) + rc_start_window(rc, max(window, 1U)); + + if (!rc->complexity[type] || (idr && !rc->complexity[RC_P])) { + rc->target = budget; + return clamp(init_qp, min_qp, max_qp); + } + + if (idr) { + xi = max_t(u64, rc->complexity[RC_I] >> 8, 1); + xp = rc->complexity[RC_P] >> 8; + w = div64_u64(xi * RC_KP << 16, + xi * RC_KP + (u64)(rc->frames - 1) * xp * 256); + target = rc->left > 0 ? (u64)rc->left * w >> 16 : 0; + } else { + target = budget + div_s64(rc->left - budget * rc->frames, + max(rc->frames, helix_rc_fps(rc))); + } + rc->target = max_t(s64, target, floor); + qp = rc_qp_for_qstep(div64_u64(rc->complexity[type], rc->target)); + + if (idr) { + u32 p = DIV_ROUND_CLOSEST(rc->mean_qp, 16); + u32 below = rc->mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ? 2 : 4; + + qp = clamp(qp, p > below ? p - below : 0, p); + } else { + u32 p = rc->last_qp[RC_P]; + + qp = clamp(qp, p ? p - 1 : 0, p + rc->up); + } + return clamp(qp, min_qp, max_qp); +} + +void helix_rc_update(struct helix_rc *rc, bool idr, u32 qp, u32 bits) +{ + int type = idr ? RC_I : RC_P; + u64 x = max_t(u64, (u64)bits * rc_qstep(qp), 1); + + if (idr) { + rc->complexity[RC_I] = x; + } else if (!rc->complexity[RC_P]) { + rc->complexity[RC_P] = x; + rc->mean_qp = qp * 16; + } else { + rc->complexity[RC_P] = (3 * rc->complexity[RC_P] + x) / 4; + rc->mean_qp += ((int)qp * 16 - (int)rc->mean_qp) / 4; + } + rc->up = bits > 4 * rc->target ? 6 : bits > 2 * rc->target ? 4 : 2; + rc->last_qp[type] = qp; + rc->left -= bits; + if (rc->frames) + rc->frames--; +} diff --git a/drivers/media/platform/ingenic/helix/helix_rc.h b/drivers/media/platform/ingenic/helix/helix_rc.h new file mode 100644 index 0000000000..5872a40d8a --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_rc.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ + +#ifndef _HELIX_RC_H_ +#define _HELIX_RC_H_ + +#include + +/* Frame-level H.264 rate control state of one context */ +struct helix_rc { + bool enabled; + u32 mode; /* V4L2_MPEG_VIDEO_BITRATE_MODE_* */ + u32 bitrate; /* bits per second */ + u32 peak; + u32 tpf_num; /* seconds per frame */ + u32 tpf_den; + + bool started; + u64 complexity[2]; /* bits * Qstep (Q8): last I, mean of recent P */ + u32 last_qp[2]; + u32 mean_qp; /* of recent P frames, Q4 */ + u32 up; /* largest QP rise for the next P frame */ + u64 target; /* of the frame being coded */ + s64 left; /* bits left in the current window */ + u32 frames; /* frames left in the current window */ +}; + +void helix_rc_init(struct helix_rc *rc); +void helix_rc_reset(struct helix_rc *rc); +u32 helix_rc_fps(const struct helix_rc *rc); +/* + * QP for the next frame. @idr starts a window of @window frames; + * @init_qp is used until the frame type has a history. + */ +u32 helix_rc_qp(struct helix_rc *rc, bool idr, u32 window, u32 init_qp, + u32 min_qp, u32 max_qp); +void helix_rc_update(struct helix_rc *rc, bool idr, u32 qp, u32 bits); + +#endif diff --git a/drivers/media/platform/ingenic/helix/helix_tables.h b/drivers/media/platform/ingenic/helix/helix_tables.h new file mode 100644 index 0000000000..7e0a4e5533 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_tables.h @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#ifndef HELIX_TABLES_H +#define HELIX_TABLES_H + +#include + +static const u8 helix_base_lum_qtable[64] = { + 16, 11, 10, 16, 24, 40, 51, 61, + 12, 12, 14, 19, 26, 58, 60, 55, + 14, 13, 16, 24, 40, 57, 69, 56, + 14, 17, 22, 29, 51, 87, 80, 62, + 18, 22, 37, 56, 68, 109, 103, 77, + 24, 35, 55, 64, 81, 104, 113, 92, + 49, 64, 78, 87, 103, 121, 120, 101, + 72, 92, 95, 98, 112, 100, 103, 99 +}; + +static const u8 helix_base_chr_qtable[64] = { + 17, 18, 24, 47, 99, 99, 99, 99, + 18, 21, 26, 66, 99, 99, 99, 99, + 24, 26, 56, 99, 99, 99, 99, 99, + 47, 66, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99 +}; + +static const u8 helix_zigzag[64] = { + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, + 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63 +}; + +static const u8 helix_qtable[128] = { + 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, + 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, + 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, + 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, + 9, 9, 9, 12, 11, 12, 24, 13, 13, 24, 50, 33, 28, 33, 50, 50, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, + 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, +}; + +static const u32 helix_qmem[256] = { + 0x00000100, 0x00000155, 0x00000155, 0x00000a49, + 0x00000155, 0x00000b33, 0x00000100, 0x00000a49, + 0x00000a49, 0x00000a49, 0x000009c7, 0x000009c7, + 0x00000100, 0x000000cd, 0x00000955, 0x000008cd, + 0x0000093b, 0x00000955, 0x000012e9, 0x000012e9, + 0x00000955, 0x0000251f, 0x000011c7, 0x000011af, + 0x00000911, 0x000008cd, 0x00001a35, 0x0000113b, + 0x00002421, 0x00001111, 0x00001a35, 0x0000113b, + 0x00001a49, 0x00001a49, 0x00000880, 0x000019c7, + 0x000010b2, 0x000010d2, 0x00000880, 0x000010f1, + 0x000010ba, 0x000010ea, 0x00001a49, 0x00001a49, + 0x000010cd, 0x00001095, 0x0000231f, 0x000010ba, + 0x00001955, 0x0000229d, 0x0000193b, 0x0000193b, + 0x0000193b, 0x00002421, 0x000010d2, 0x0000223f, + 0x00002219, 0x00002249, 0x000010a4, 0x00001911, + 0x000010b2, 0x00002d05, 0x0000193b, 0x000010a4, + 0x000009c7, 0x000009c7, 0x000009c7, 0x00000955, + 0x000012e9, 0x00000955, 0x00001155, 0x0000093b, + 0x0000093b, 0x00001155, 0x000010a4, 0x000023e1, + 0x00001a49, 0x000023e1, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, + 0x000010a4, 0x000010a4, 0x000010a4, 0x000010a4, +}; + +static const u32 stock_qmem_lut[256] = { + 0x00000000, 0x00000001, 0x00000400, 0x000002ab, 0x00000200, 0x00000b33, 0x00000155, 0x00000a49, + 0x00000100, 0x000009c7, 0x000000cd, 0x000012e9, 0x00000955, 0x0000093b, 0x00001249, 0x00000911, + 0x00000080, 0x000008f1, 0x000011c7, 0x000011af, 0x000008cd, 0x000008c3, 0x000008ba, 0x000008b2, + 0x00001155, 0x0000251f, 0x0000113b, 0x00001a5f, 0x00001a49, 0x00001a35, 0x00001111, 0x00002421, + 0x00000880, 0x000023e1, 0x000010f1, 0x000010ea, 0x000019c7, 0x000019bb, 0x000019af, 0x000010d2, + 0x000010cd, 0x0000231f, 0x000010c3, 0x0000197d, 0x000010ba, 0x000010b6, 0x000010b2, 0x000022b9, + 0x00001955, 0x0000229d, 0x000010a4, 0x00002d05, 0x0000193b, 0x00001935, 0x0000225f, 0x00001095, + 0x00002249, 0x0000223f, 0x00002235, 0x00002c57, 0x00001911, 0x00002219, 0x00001084, 0x00001082, + 0x00001080, 0x000018fc, 0x000018f8, 0x000021e9, 0x000018f1, 0x000021db, 0x000018ea, 0x00002b9b, + 0x000021c7, 0x000021c1, 0x000021bb, 0x000021b5, 0x000021af, 0x00002b53, 0x000018d2, 0x0000367b, + 0x000018cd, 0x00002b29, 0x000018c8, 0x0000218b, 0x000018c3, 0x00002b03, 0x0000217d, 0x00002af1, + 0x000018ba, 0x000018b8, 0x000018b6, 0x000018b4, 0x000018b2, 0x00002ac1, 0x00002ab9, 0x00002159, + 0x00002155, 0x00003547, 0x00002a9d, 0x0000214b, 0x000018a4, 0x00002a89, 0x00002141, 0x0000189f, + 0x0000213b, 0x0000189c, 0x00002135, 0x000034c9, 0x00002a5f, 0x00002a59, 0x00001895, 0x0000349d, + 0x00002a49, 0x00001891, 0x00002a3f, 0x0000211d, 0x00002a35, 0x0000188c, 0x00002a2b, 0x0000344d, + 0x00002111, 0x0000343b, 0x00002a19, 0x00002a15, 0x00001884, 0x00003419, 0x00001882, 0x00001881, + 0x00001880, 0x000020fe, 0x000020fc, 0x000033e9, 0x000020f8, 0x00003fb3, 0x000029e9, 0x000033cb, + 0x000020f1, 0x000033bd, 0x000029db, 0x000033af, 0x000020ea, 0x000029d1, 0x000020e7, 0x00003395, + 0x000029c7, 0x000020e2, 0x000029c1, 0x000020df, 0x000029bb, 0x000020dc, 0x000029b5, 0x000020d9, + 0x000029af, 0x00003359, 0x00003353, 0x000029a7, 0x000020d2, 0x00003343, 0x0000299f, 0x000020ce, + 0x000020cd, 0x00002997, 0x00003329, 0x000020c9, 0x000020c8, 0x0000298d, 0x0000298b, 0x00003311, + 0x000020c3, 0x00003e0f, 0x00003303, 0x00003dfd, 0x0000297d, 0x0000297b, 0x00002979, 0x000032ed, + 0x000020ba, 0x00003dc9, 0x000020b8, 0x000020b7, 0x000020b6, 0x000020b5, 0x000020b4, 0x000020b3, + 0x000020b2, 0x00003d89, 0x000020b0, 0x000032bd, 0x0000295d, 0x00003d6b, 0x00002959, 0x00002957, + 0x00002955, 0x000032a7, 0x000020a9, 0x000020a8, 0x000020a7, 0x00003299, 0x0000294b, 0x00003293, + 0x000020a4, 0x000020a3, 0x00003289, 0x00002943, 0x00002941, 0x00003cff, 0x0000209f, 0x00003279, + 0x0000293b, 0x00003273, 0x0000209c, 0x0000326d, 0x00002935, 0x00003ccf, 0x00002099, 0x00003cc3, + 0x0000325f, 0x00002097, 0x00003259, 0x00003cad, 0x00002095, 0x00003251, 0x00002927, 0x00002093, + 0x00003249, 0x00003c8d, 0x00002091, 0x00003c83, 0x0000323f, 0x00003c79, 0x0000291d, 0x00003c6f, + 0x00003235, 0x00003c65, 0x0000208c, 0x00002917, 0x0000208b, 0x00003229, 0x00003227, 0x00003c49, + 0x00002911, 0x00002088, 0x0000290f, 0x00003c37, 0x00003219, 0x00003217, 0x00003215, 0x00003c25, + 0x00002084, 0x00003c1d, 0x00002083, 0x00002905, 0x00002082, 0x00002903, 0x00002081, 0x00002901, +}; + +#endif diff --git a/drivers/media/platform/ingenic/helix/helix_v4l2.c b/drivers/media/platform/ingenic/helix/helix_v4l2.c new file mode 100644 index 0000000000..63191e50b7 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_v4l2.c @@ -0,0 +1,1501 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include "helix_drv.h" +#include "helix_h264.h" + +static const struct helix_fmt helix_formats[] = { + { + .fourcc = V4L2_PIX_FMT_NV12, + .types = V4L2_BUF_TYPE_VIDEO_OUTPUT, + .num_planes = 1, + .depth = { 12 }, + }, + { + .fourcc = V4L2_PIX_FMT_NV21, + .types = V4L2_BUF_TYPE_VIDEO_OUTPUT, + .num_planes = 1, + .depth = { 12 }, + }, + { + .fourcc = V4L2_PIX_FMT_YUYV, + .types = V4L2_BUF_TYPE_VIDEO_OUTPUT, + .num_planes = 1, + .depth = { 16 }, + }, + { + .fourcc = V4L2_PIX_FMT_JPEG, + .types = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .num_planes = 1, + .depth = { 0 }, + }, + { + .fourcc = V4L2_PIX_FMT_H264, + .types = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .num_planes = 1, + .depth = { 0 }, + }, +}; + +const struct helix_fmt *helix_find_fmt(u32 fourcc, u32 type) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(helix_formats); i++) { + if (helix_formats[i].fourcc == fourcc && + helix_formats[i].types == type) + return &helix_formats[i]; + } + return NULL; +} + +static int helix_queue_setup(struct vb2_queue *vq, + unsigned int *nbuffers, unsigned int *nplanes, + unsigned int sizes[], struct device *alloc_devs[]) +{ + struct helix_ctx *ctx = vb2_get_drv_priv(vq); + struct v4l2_format *f = V4L2_TYPE_IS_OUTPUT(vq->type) ? + &ctx->src_fmt : &ctx->dst_fmt; + unsigned int min_size = f->fmt.pix.sizeimage; + unsigned int alloc_size = min_size; + + if (V4L2_TYPE_IS_OUTPUT(vq->type) && + (f->fmt.pix.pixelformat == V4L2_PIX_FMT_NV12 || + f->fmt.pix.pixelformat == V4L2_PIX_FMT_NV21)) { + u32 cw = ALIGN(f->fmt.pix.width, 16); + u32 ch = ALIGN(f->fmt.pix.height, 16); + + alloc_size = max_t(unsigned int, min_size, cw * ch * 3 / 2); + } + + if (*nplanes) + return *nplanes != 1 || sizes[0] < min_size ? -EINVAL : 0; + + *nplanes = 1; + sizes[0] = alloc_size; + alloc_devs[0] = ctx->hdev->dev; + + return 0; +} + +static void helix_queue_eos(struct helix_ctx *ctx) +{ + static const struct v4l2_event eos_ev = { + .type = V4L2_EVENT_EOS, + }; + + v4l2_event_queue_fh(&ctx->fh, &eos_ev); +} + +static int helix_buf_prepare(struct vb2_buffer *vb) +{ + struct helix_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct v4l2_format *f = V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type) ? + &ctx->src_fmt : &ctx->dst_fmt; + + if (READ_ONCE(ctx->hdev->disconnected)) + return -ENODEV; + if (vb2_plane_size(vb, 0) < f->fmt.pix.sizeimage) + return -EINVAL; + + if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { + if (vbuf->field == V4L2_FIELD_ANY) + vbuf->field = V4L2_FIELD_NONE; + if (vbuf->field != V4L2_FIELD_NONE) + return -EINVAL; + if (!vb2_get_plane_payload(vb, 0)) + vb2_set_plane_payload(vb, 0, f->fmt.pix.sizeimage); + else if (vb2_get_plane_payload(vb, 0) < f->fmt.pix.sizeimage) + return -EINVAL; + } else { + vb2_set_plane_payload(vb, 0, 0); + } + + return 0; +} + +static void helix_buf_queue(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct helix_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); + + if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && + vb2_is_streaming(vb->vb2_queue) && + v4l2_m2m_dst_buf_is_last(ctx->m2m_ctx)) { + vb2_set_plane_payload(vb, 0, 0); + vbuf->field = V4L2_FIELD_NONE; + vbuf->sequence = ctx->sequence++; + v4l2_m2m_last_buffer_done(ctx->m2m_ctx, vbuf); + helix_queue_eos(ctx); + return; + } + + v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf); +} + +static void helix_return_buffers(struct vb2_queue *vq, + enum vb2_buffer_state state) +{ + struct helix_ctx *ctx = vb2_get_drv_priv(vq); + struct vb2_v4l2_buffer *vb; + + for (;;) { + vb = V4L2_TYPE_IS_OUTPUT(vq->type) ? + v4l2_m2m_src_buf_remove(ctx->m2m_ctx) : + v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); + if (!vb) + break; + v4l2_m2m_buf_done(vb, state); + } +} + +static int helix_start_streaming(struct vb2_queue *vq, unsigned int count) +{ + struct helix_ctx *ctx = vb2_get_drv_priv(vq); + struct helix_dev *hdev = ctx->hdev; + + WRITE_ONCE(ctx->stopping, false); + if (READ_ONCE(hdev->disconnected)) { + helix_return_buffers(vq, VB2_BUF_STATE_QUEUED); + return -ENODEV; + } + /* + * q->lock (dev_mutex) is already held. A failed reset leaves the VPU + * owning the previous recon buffers, so retry it before reusing them. + */ + if (READ_ONCE(hdev->fatal) || READ_ONCE(hdev->hw_running)) { + int ret; + + mutex_lock(&hdev->hw_lock); + ret = helix_hw_quiesce(hdev); + mutex_unlock(&hdev->hw_lock); + if (ret) { + helix_return_buffers(vq, VB2_BUF_STATE_QUEUED); + return ret; + } + } + if (!ctx->dma_ref) { + bool need_scratch = + (ctx->dst_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_H264); + int ret; + + mutex_lock(&hdev->hw_lock); + ret = helix_get_dma(hdev, need_scratch); + mutex_unlock(&hdev->hw_lock); + if (ret) { + helix_return_buffers(vq, VB2_BUF_STATE_QUEUED); + return ret; + } + ctx->dma_ref = true; + } + v4l2_m2m_update_start_streaming_state(ctx->m2m_ctx, vq); + if (V4L2_TYPE_IS_OUTPUT(vq->type)) { + ctx->frame_num = 0; + ctx->sequence = 0; + ctx->idr_pic_id = 0; + ctx->gop_count = 0; + helix_rc_reset(&ctx->rc); + } else if (ctx->dst_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_H264 && + !ctx->h264_bufs_allocated) { + u32 coded_w = ALIGN(ctx->crop.width, 16); + u32 coded_h = ALIGN(ctx->crop.height, 16); + int ret = 0; + + mutex_lock(&hdev->hw_lock); + if (!hdev->scratch_vaddr) { + hdev->scratch_vaddr = dma_alloc_coherent( + hdev->dev, HELIX_SCRATCH_BUF_SIZE, + &hdev->scratch_paddr, GFP_KERNEL); + if (!hdev->scratch_vaddr) + ret = -ENOMEM; + else + hdev->scratch_size = HELIX_SCRATCH_BUF_SIZE; + } + mutex_unlock(&hdev->hw_lock); + if (!ret) + ret = helix_alloc_h264_buffers(hdev, &ctx->h264_bufs, + coded_w, coded_h); + + if (ret) { + struct vb2_queue *src_vq = v4l2_m2m_get_src_vq(ctx->m2m_ctx); + + /* stop_streaming will not run for this queue. */ + if (!vb2_is_streaming(src_vq)) { + mutex_lock(&hdev->hw_lock); + helix_put_dma(hdev); + mutex_unlock(&hdev->hw_lock); + ctx->dma_ref = false; + } + helix_return_buffers(vq, VB2_BUF_STATE_QUEUED); + return ret; + } + ctx->h264_bufs_allocated = true; + ctx->frame_num = 0; + ctx->sequence = 0; + ctx->idr_pic_id = 0; + ctx->gop_count = 0; + helix_rc_reset(&ctx->rc); + } + return 0; +} + +/* hw_lock held. Keep recon/scratch DMA if this context's task may still be running. */ +static void helix_release_h264_dma(struct helix_ctx *ctx) +{ + struct helix_dev *hdev = ctx->hdev; + + lockdep_assert_held(&hdev->hw_lock); + if (!ctx->h264_bufs_allocated) + return; + + if (READ_ONCE(hdev->hw_running) && hdev->dma_owner == ctx) { + if (hdev->h264_hold_valid) { + dev_err(hdev->dev, + "dropping H.264 DMA tracking; hold slot is full\n"); + WRITE_ONCE(hdev->fatal, true); + } else { + hdev->h264_hold = ctx->h264_bufs; + hdev->h264_hold_valid = true; + dev_err(hdev->dev, + "holding H.264 DMA buffers until the VPU resets\n"); + } + memset(&ctx->h264_bufs, 0, sizeof(ctx->h264_bufs)); + ctx->h264_bufs_allocated = false; + hdev->dma_owner = NULL; + return; + } + + helix_free_h264_buffers(hdev, &ctx->h264_bufs); + ctx->h264_bufs_allocated = false; +} + +static void helix_stop_streaming(struct vb2_queue *vq) +{ + struct helix_ctx *ctx = vb2_get_drv_priv(vq); + struct helix_dev *hdev = ctx->hdev; + + WRITE_ONCE(ctx->stopping, true); + if (hdev->workqueue && !READ_ONCE(hdev->disconnected)) { + mutex_unlock(&hdev->dev_mutex); + flush_workqueue(hdev->workqueue); + mutex_lock(&hdev->dev_mutex); + } + v4l2_m2m_update_stop_streaming_state(ctx->m2m_ctx, vq); + if (V4L2_TYPE_IS_OUTPUT(vq->type) && v4l2_m2m_has_stopped(ctx->m2m_ctx)) + helix_queue_eos(ctx); + + mutex_lock(&hdev->hw_lock); + if (!V4L2_TYPE_IS_OUTPUT(vq->type)) + helix_release_h264_dma(ctx); + if (ctx->dma_ref) { + struct vb2_queue *other_vq = V4L2_TYPE_IS_OUTPUT(vq->type) ? + v4l2_m2m_get_dst_vq(ctx->m2m_ctx) : + v4l2_m2m_get_src_vq(ctx->m2m_ctx); + + if (!vb2_is_streaming(other_vq)) { + helix_put_dma(hdev); + ctx->dma_ref = false; + } + } + mutex_unlock(&hdev->hw_lock); + helix_return_buffers(vq, VB2_BUF_STATE_ERROR); +} + +static const struct vb2_ops helix_qops = { + .queue_setup = helix_queue_setup, + .buf_prepare = helix_buf_prepare, + .buf_queue = helix_buf_queue, + .start_streaming = helix_start_streaming, + .stop_streaming = helix_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int helix_queue_init(void *priv, struct vb2_queue *src_vq, + struct vb2_queue *dst_vq) +{ + struct helix_ctx *ctx = priv; + struct helix_dev *hdev = ctx->hdev; + int ret; + + src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; + src_vq->io_modes = VB2_MMAP | VB2_DMABUF; + src_vq->drv_priv = ctx; + src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); + src_vq->ops = &helix_qops; + src_vq->mem_ops = &vb2_dma_contig_memops; + src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + src_vq->lock = &hdev->dev_mutex; + src_vq->dev = hdev->dev; + + ret = vb2_queue_init(src_vq); + if (ret) + return ret; + + dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; + dst_vq->drv_priv = ctx; + dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); + dst_vq->ops = &helix_qops; + dst_vq->mem_ops = &vb2_dma_contig_memops; + dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + dst_vq->lock = &hdev->dev_mutex; + dst_vq->dev = hdev->dev; + + return vb2_queue_init(dst_vq); +} + +static void helix_prepare_input_frame(const u8 *src, u8 *dst, + u32 src_w, u32 src_h, + u32 vis_w, u32 vis_h, + u32 coded_w, u32 coded_h, + u32 fourcc) +{ + u32 y, x; + + if (fourcc == V4L2_PIX_FMT_NV12 || fourcc == V4L2_PIX_FMT_NV21) { + const u8 *src_y = src; + const u8 *src_uv = src + (size_t)src_w * src_h; + u8 *dst_y = dst; + u8 *dst_uv = dst + coded_w * coded_h; + u32 vis_uv_h = vis_h / 2; + u32 coded_uv_h = coded_h / 2; + + /* Copy Y plane and replicate edges */ + for (y = 0; y < vis_h; y++) { + memcpy(dst_y + y * coded_w, src_y + (size_t)y * src_w, + vis_w); + if (coded_w > vis_w) + memset(dst_y + y * coded_w + vis_w, + src_y[(size_t)y * src_w + vis_w - 1], + coded_w - vis_w); + } + for (y = vis_h; y < coded_h; y++) + memcpy(dst_y + y * coded_w, + dst_y + (vis_h - 1) * coded_w, + coded_w); + + /* Copy UV plane and replicate edges */ + for (y = 0; y < vis_uv_h; y++) { + memcpy(dst_uv + y * coded_w, src_uv + (size_t)y * src_w, + vis_w); + if (coded_w > vis_w) { + u8 b0 = src_uv[(size_t)y * src_w + vis_w - 2]; + u8 b1 = src_uv[(size_t)y * src_w + vis_w - 1]; + + for (x = vis_w; x < coded_w; x += 2) { + dst_uv[y * coded_w + x] = b0; + dst_uv[y * coded_w + x + 1] = b1; + } + } + } + for (y = vis_uv_h; y < coded_uv_h; y++) + memcpy(dst_uv + y * coded_w, + dst_uv + (vis_uv_h - 1) * coded_w, + coded_w); + } else if (fourcc == V4L2_PIX_FMT_YUYV) { + u8 *dst_y = dst; + u8 *dst_uv = dst + coded_w * coded_h; + u32 vis_uv_h = vis_h / 2; + u32 coded_uv_h = coded_h / 2; + + for (y = 0; y < vis_h; y++) { + const u8 *src_line = src + (size_t)y * (src_w * 2); + u8 *dst_line = dst_y + y * coded_w; + + for (x = 0; x < vis_w; x++) + dst_line[x] = src_line[x * 2]; + if (coded_w > vis_w) + memset(dst_line + vis_w, dst_line[vis_w - 1], coded_w - vis_w); + } + for (y = vis_h; y < coded_h; y++) + memcpy(dst_y + y * coded_w, + dst_y + (vis_h - 1) * coded_w, + coded_w); + + for (y = 0; y < vis_uv_h; y++) { + const u8 *src_line = src + (size_t)(y * 2) * (src_w * 2); + u8 *dst_line = dst_uv + y * coded_w; + + for (x = 0; x < vis_w; x += 2) { + dst_line[x] = src_line[x * 2 + 1]; /* U */ + dst_line[x + 1] = src_line[x * 2 + 3]; /* V */ + } + if (coded_w > vis_w) { + u8 last_u = dst_line[vis_w - 2]; + u8 last_v = dst_line[vis_w - 1]; + + for (x = vis_w; x < coded_w; x += 2) { + dst_line[x] = last_u; + dst_line[x + 1] = last_v; + } + } + } + for (y = vis_uv_h; y < coded_uv_h; y++) + memcpy(dst_uv + y * coded_w, + dst_uv + (vis_uv_h - 1) * coded_w, + coded_w); + } +} + +static int helix_encode_h264(struct helix_dev *hdev, struct helix_ctx *ctx, + struct vb2_v4l2_buffer *src, + struct vb2_v4l2_buffer *dst, + dma_addr_t src_y_dma, dma_addr_t src_uv_dma, + u32 hw_fourcc, + u8 *out, u32 vis_w, u32 vis_h, + u32 coded_w, u32 coded_h) +{ + bool is_idr = (ctx->gop_count == 0) || ctx->force_key_frame || + (ctx->gop_size > 0 && (ctx->gop_count % ctx->gop_size == 0)); + u32 qp = is_idr ? ctx->i_frame_qp : ctx->p_frame_qp; + size_t header_len = 0; + u32 status, len; + int ret; + + if (ctx->rc.enabled) { + u32 window = ctx->gop_size ? + ctx->gop_size - ctx->gop_count % ctx->gop_size : + helix_rc_fps(&ctx->rc); + + qp = helix_rc_qp(&ctx->rc, is_idr, window, qp, + ctx->min_qp, ctx->max_qp); + } + + ctx->force_key_frame = false; + if (is_idr) { + ctx->frame_num = 0; + if (ctx->repeat_seq_header || + ctx->header_mode == V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME || + ctx->gop_count == 0) { + ssize_t n; + + n = helix_h264_write_sps(ctx, out + header_len, + vb2_plane_size(&dst->vb2_buf, 0) - header_len, + vis_w, vis_h, coded_w, coded_h); + if (n < 0) + return n; + header_len += n; + + n = helix_h264_write_pps(ctx, out + header_len, + vb2_plane_size(&dst->vb2_buf, 0) - header_len, + qp); + if (n < 0) + return n; + header_len += n; + } + } + + if (READ_ONCE(hdev->aborting) || READ_ONCE(hdev->disconnected)) + return -ECANCELED; + + reinit_completion(&hdev->completion); + WRITE_ONCE(hdev->irq_status, 0); + hdev->bslen = 0; + + ret = helix_h264_hw_start(hdev, ctx, + src_y_dma, src_uv_dma, + hdev->bounce_paddr, + coded_w, coded_h, + coded_w, coded_w, hw_fourcc, + is_idr, qp); + if (ret) + return ret; + + hdev->dma_owner = ctx; + WRITE_ONCE(hdev->hw_running, true); + hdev->hw_busy = true; + ret = helix_vdma_wait(hdev, 2000); + hdev->hw_busy = false; + + if (ret || READ_ONCE(hdev->aborting) || READ_ONCE(hdev->disconnected)) { + int reset_ret = helix_hw_quiesce(hdev); + + ctx->force_key_frame = true; + if (ret && ret != -ECANCELED) + dev_err(hdev->dev, "H.264 encode failed or timed out: %d, irq_status %#x\n", + ret, READ_ONCE(hdev->irq_status)); + if (reset_ret) + dev_err(hdev->dev, "H.264 reset failed: %d; DMA buffers stay reserved\n", + reset_ret); + return ret ? ret : -ECANCELED; + } + WRITE_ONCE(hdev->hw_running, false); + hdev->dma_owner = NULL; + + status = READ_ONCE(hdev->irq_status); + len = hdev->bslen; + if (!len && (status & SCH_STAT_ENDFLAG)) { + int retries = 3; + + while (retries-- > 0 && !len) { + msleep(2); + len = readl(hdev->base + REG_SDE_CFG9); + } + hdev->bslen = len; + } + + if (READ_ONCE(hdev->aborting) || READ_ONCE(hdev->disconnected) || + !(status & SCH_STAT_ENDFLAG) || + !len || len > hdev->bounce_size || + header_len + len + 64 > vb2_plane_size(&dst->vb2_buf, 0)) { + ctx->force_key_frame = true; + return -EIO; + } + + ctx->h264_bufs.cur_slot = 1 - ctx->h264_bufs.cur_slot; + dma_rmb(); + + { + ssize_t n = helix_h264_write_slice(ctx, out + header_len, + vb2_plane_size(&dst->vb2_buf, 0) - header_len, + is_idr, qp, hdev->bounce_vaddr, len); + if (n < 0) { + ctx->force_key_frame = true; + return n; + } + header_len += n; + } + if (ctx->rc.enabled) + helix_rc_update(&ctx->rc, is_idr, + clamp(qp, ctx->min_qp, ctx->max_qp), + header_len * 8); + + vb2_set_plane_payload(&dst->vb2_buf, 0, header_len); + dst->vb2_buf.timestamp = src->vb2_buf.timestamp; + dst->timecode = src->timecode; + dst->flags &= ~(V4L2_BUF_FLAG_TSTAMP_SRC_MASK | V4L2_BUF_FLAG_TIMECODE); + dst->flags |= src->flags & (V4L2_BUF_FLAG_TSTAMP_SRC_MASK | V4L2_BUF_FLAG_TIMECODE); + src->sequence = ctx->sequence; + dst->sequence = ctx->sequence++; + ctx->frame_num++; + ctx->gop_count++; + dst->field = V4L2_FIELD_NONE; + dst->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME); + if (is_idr) + dst->flags |= V4L2_BUF_FLAG_KEYFRAME; + else + dst->flags |= V4L2_BUF_FLAG_PFRAME; + + return 0; +} + +static int helix_encode_jpeg(struct helix_dev *hdev, struct helix_ctx *ctx, + struct vb2_v4l2_buffer *src, + struct vb2_v4l2_buffer *dst, + dma_addr_t src_y_dma, dma_addr_t src_uv_dma, + u32 hw_fourcc, + u8 *out, u32 vis_w, u32 vis_h, + u32 coded_w, u32 coded_h) +{ + size_t header; + u32 status, len; + int ret; + + helix_calc_qtable(ctx->jpeg_quality, ctx->qtable, ctx->qmem); + header = helix_jpeg_write_headers(out, coded_w, coded_h, ctx->qtable, vis_w, vis_h); + + if (READ_ONCE(hdev->aborting) || READ_ONCE(hdev->disconnected)) + return -ECANCELED; + + reinit_completion(&hdev->completion); + WRITE_ONCE(hdev->irq_status, 0); + hdev->bslen = 0; + + ret = helix_jpeg_hw_start(hdev, src_y_dma, src_uv_dma, + hdev->bounce_paddr, coded_w, coded_h, + coded_w, coded_w, + hw_fourcc, + ctx->qmem); + if (ret) + return ret; + + WRITE_ONCE(hdev->hw_running, true); + hdev->hw_busy = true; + ret = helix_vdma_wait(hdev, 2000); + hdev->hw_busy = false; + + if (ret || READ_ONCE(hdev->aborting) || READ_ONCE(hdev->disconnected)) { + int reset_ret = helix_hw_quiesce(hdev); + + if (ret && ret != -ECANCELED) + dev_err(hdev->dev, "encoding failed or timed out: %d, irq_status %#x\n", + ret, READ_ONCE(hdev->irq_status)); + if (reset_ret) + dev_err(hdev->dev, "JPEG reset failed: %d\n", reset_ret); + return ret ? ret : -ECANCELED; + } + WRITE_ONCE(hdev->hw_running, false); + hdev->dma_owner = NULL; + + status = READ_ONCE(hdev->irq_status); + len = hdev->bslen; + if (!len && (status & (SCH_STAT_ENDFLAG | SCH_STAT_JPGEND))) { + int retries = 3; + + while (retries-- > 0 && !len) { + msleep(2); + len = readl(hdev->base + REG_JPGC_STAT) & JPGC_STAT_LEN_MASK; + } + hdev->bslen = len; + } + + if (READ_ONCE(hdev->aborting) || READ_ONCE(hdev->disconnected) || + (status & (SCH_STAT_ENDFLAG | SCH_STAT_JPGEND)) != + (SCH_STAT_ENDFLAG | SCH_STAT_JPGEND) || + !len || len > hdev->bounce_size || + header + len + 2 > vb2_plane_size(&dst->vb2_buf, 0)) { + return -EIO; + } + + dma_rmb(); + + memcpy(out + header, hdev->bounce_vaddr, len); + + if (len < 2 || out[header + len - 2] != 0xff || + out[header + len - 1] != 0xd9) { + helix_jpeg_write_eoi(out, header + len); + len += 2; + } + + vb2_set_plane_payload(&dst->vb2_buf, 0, header + len); + dst->vb2_buf.timestamp = src->vb2_buf.timestamp; + dst->timecode = src->timecode; + dst->flags &= ~(V4L2_BUF_FLAG_TSTAMP_SRC_MASK | V4L2_BUF_FLAG_TIMECODE); + dst->flags |= src->flags & (V4L2_BUF_FLAG_TSTAMP_SRC_MASK | V4L2_BUF_FLAG_TIMECODE); + src->sequence = ctx->sequence; + dst->sequence = ctx->sequence++; + ctx->frame_num++; + dst->field = V4L2_FIELD_NONE; + dst->flags &= ~(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME); + dst->flags |= V4L2_BUF_FLAG_KEYFRAME; + + return 0; +} + +void helix_job_work(struct work_struct *work) +{ + struct helix_dev *hdev = container_of(work, struct helix_dev, work); + struct helix_ctx *ctx; + struct vb2_v4l2_buffer *src, *dst; + enum vb2_buffer_state state = VB2_BUF_STATE_ERROR; + u32 src_w, src_h, vis_w, vis_h, coded_w, coded_h, src_fourcc, hw_fourcc; + dma_addr_t src_y_dma, src_uv_dma; + size_t zc_min_bytes; + bool zero_copy; + u8 *out; + int ret; + + mutex_lock(&hdev->hw_lock); + ctx = v4l2_m2m_get_curr_priv(hdev->m2m_dev); + if (!ctx) { + mutex_unlock(&hdev->hw_lock); + return; + } + WRITE_ONCE(hdev->curr_ctx, ctx); + + src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); + dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); + if (!src || !dst || READ_ONCE(hdev->disconnected) || + READ_ONCE(hdev->aborting)) + goto unlock; + + out = vb2_plane_vaddr(&dst->vb2_buf, 0); + if (!out) + goto unlock; + + src_w = ctx->src_fmt.fmt.pix.width; + src_h = ctx->src_fmt.fmt.pix.height; + vis_w = ctx->crop.width; + vis_h = ctx->crop.height; + coded_w = ALIGN(vis_w, 16); + coded_h = ALIGN(vis_h, 16); + src_fourcc = ctx->src_fmt.fmt.pix.pixelformat; + hw_fourcc = (src_fourcc == V4L2_PIX_FMT_YUYV) ? + V4L2_PIX_FMT_NV12 : src_fourcc; + + src_y_dma = vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0); + src_uv_dma = src_y_dma + (size_t)coded_w * vis_h; + zc_min_bytes = (size_t)coded_w * vis_h + (size_t)coded_w * (coded_h / 2); + zero_copy = (src_fourcc == V4L2_PIX_FMT_NV12 || + src_fourcc == V4L2_PIX_FMT_NV21) && + vis_w == src_w && vis_h == src_h && + vis_w == coded_w && vis_h == coded_h && + !((src_y_dma | src_uv_dma) & 127) && + vb2_plane_size(&src->vb2_buf, 0) >= zc_min_bytes; + + if (!zero_copy) { + u8 *src_vaddr = vb2_plane_vaddr(&src->vb2_buf, 0); + + if (!src_vaddr) + goto unlock; + if (helix_ensure_input_dma(hdev, (size_t)coded_w * coded_h * 3 / 2)) + goto unlock; + helix_prepare_input_frame(src_vaddr, hdev->input_vaddr, + src_w, src_h, vis_w, vis_h, + coded_w, coded_h, src_fourcc); + src_y_dma = hdev->input_paddr; + src_uv_dma = hdev->input_paddr + (size_t)coded_w * coded_h; + } + dma_wmb(); + + helix_cp0_config7_enable(); + if (helix_hw_quiesce(hdev)) + goto unlock; + + if (ctx->dst_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_H264) + ret = helix_encode_h264(hdev, ctx, src, dst, + src_y_dma, src_uv_dma, hw_fourcc, out, + vis_w, vis_h, coded_w, coded_h); + else + ret = helix_encode_jpeg(hdev, ctx, src, dst, + src_y_dma, src_uv_dma, hw_fourcc, out, + vis_w, vis_h, coded_w, coded_h); + + if (!ret) + state = VB2_BUF_STATE_DONE; + if (src && v4l2_m2m_is_last_draining_src_buf(ctx->m2m_ctx, src)) { + if (dst) + dst->flags |= V4L2_BUF_FLAG_LAST; + v4l2_m2m_mark_stopped(ctx->m2m_ctx); + helix_queue_eos(ctx); + } + +unlock: + if (src) + v4l2_m2m_buf_done(src, state); + if (dst) { + if (state != VB2_BUF_STATE_DONE) + vb2_set_plane_payload(&dst->vb2_buf, 0, 0); + v4l2_m2m_buf_done(dst, state); + } + WRITE_ONCE(hdev->curr_ctx, NULL); + mutex_unlock(&hdev->hw_lock); + v4l2_m2m_job_finish(hdev->m2m_dev, ctx->m2m_ctx); +} + +static void helix_device_run(void *priv) +{ + struct helix_ctx *ctx = priv; + struct helix_dev *hdev = ctx->hdev; + + if (READ_ONCE(hdev->disconnected)) { + helix_return_buffers(v4l2_m2m_get_src_vq(ctx->m2m_ctx), + VB2_BUF_STATE_ERROR); + helix_return_buffers(v4l2_m2m_get_dst_vq(ctx->m2m_ctx), + VB2_BUF_STATE_ERROR); + v4l2_m2m_job_finish(hdev->m2m_dev, ctx->m2m_ctx); + return; + } + WRITE_ONCE(hdev->aborting, false); + queue_work(hdev->workqueue, &hdev->work); +} + +static void helix_job_abort(void *priv) +{ + struct helix_ctx *ctx = priv; + struct helix_dev *hdev = ctx->hdev; + + if (READ_ONCE(hdev->curr_ctx) == ctx) { + WRITE_ONCE(hdev->aborting, true); + complete(&hdev->completion); + } + if (hdev->workqueue) + flush_workqueue(hdev->workqueue); +} + +static int helix_job_ready(void *priv) +{ + struct helix_ctx *ctx = priv; + + return !READ_ONCE(ctx->stopping) && + !READ_ONCE(ctx->hdev->disconnected); +} + +const struct v4l2_m2m_ops helix_m2m_ops = { + .device_run = helix_device_run, + .job_abort = helix_job_abort, + .job_ready = helix_job_ready, +}; + +static int vidioc_querycap(struct file *file, void *priv, + struct v4l2_capability *cap) +{ + struct helix_ctx *ctx = priv; + struct helix_dev *hdev = ctx->hdev; + + strscpy(cap->driver, HELIX_NAME, sizeof(cap->driver)); + strscpy(cap->card, "Ingenic Helix Video Encoder", sizeof(cap->card)); + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", + dev_name(hdev->dev)); + cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; + + return 0; +} + +static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, + struct v4l2_fmtdesc *f) +{ + int idx = 0, i; + + for (i = 0; i < ARRAY_SIZE(helix_formats); i++) { + if (helix_formats[i].types == V4L2_BUF_TYPE_VIDEO_OUTPUT) { + if (idx == f->index) { + f->pixelformat = helix_formats[i].fourcc; + return 0; + } + idx++; + } + } + return -EINVAL; +} + +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, + struct v4l2_fmtdesc *f) +{ + int idx = 0, i; + + for (i = 0; i < ARRAY_SIZE(helix_formats); i++) { + if (helix_formats[i].types == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + if (idx == f->index) { + f->pixelformat = helix_formats[i].fourcc; + return 0; + } + idx++; + } + } + return -EINVAL; +} + +static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct helix_ctx *ctx = priv; + struct vb2_queue *vq; + + vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); + if (!vq) + return -EINVAL; + + if (V4L2_TYPE_IS_OUTPUT(f->type)) + *f = ctx->src_fmt; + else + *f = ctx->dst_fmt; + + return 0; +} + +static int helix_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct helix_ctx *ctx = container_of(ctrl->handler, struct helix_ctx, hdl); + + switch (ctrl->id) { + case V4L2_CID_JPEG_COMPRESSION_QUALITY: + ctx->jpeg_quality = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_GOP_SIZE: + ctx->gop_size = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: + ctx->i_frame_qp = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: + ctx->p_frame_qp = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: + ctx->min_qp = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: + ctx->max_qp = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_H264_PROFILE: + ctx->h264_profile = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_H264_LEVEL: + ctx->h264_level = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_HEADER_MODE: + ctx->header_mode = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME: + ctx->force_key_frame = true; + break; + case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER: + ctx->repeat_seq_header = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: + ctx->rc.enabled = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: + ctx->rc.mode = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_BITRATE: + ctx->rc.bitrate = ctrl->val; + break; + case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: + ctx->rc.peak = ctrl->val; + break; + default: + return -EINVAL; + } + return 0; +} + +static const struct v4l2_ctrl_ops helix_ctrl_ops = { + .s_ctrl = helix_s_ctrl, +}; + +/* The frame rate is the rate control's time base; it does not pace jobs. */ +static int vidioc_g_parm(struct file *file, void *priv, + struct v4l2_streamparm *a) +{ + struct helix_ctx *ctx = priv; + + if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) + return -EINVAL; + + memset(&a->parm.output, 0, sizeof(a->parm.output)); + a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; + a->parm.output.timeperframe.numerator = ctx->rc.tpf_num; + a->parm.output.timeperframe.denominator = ctx->rc.tpf_den; + return 0; +} + +static int vidioc_s_parm(struct file *file, void *priv, + struct v4l2_streamparm *a) +{ + struct helix_ctx *ctx = priv; + struct v4l2_fract *tpf = &a->parm.output.timeperframe; + + if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) + return -EINVAL; + + /* 1 to 120 frames per second */ + if (tpf->numerator && tpf->denominator && + tpf->denominator <= 120 * tpf->numerator && + tpf->numerator <= tpf->denominator) { + /* A job reads the pair under hw_lock. */ + mutex_lock(&ctx->hdev->hw_lock); + ctx->rc.tpf_num = tpf->numerator; + ctx->rc.tpf_den = tpf->denominator; + mutex_unlock(&ctx->hdev->hw_lock); + } + return vidioc_g_parm(file, priv, a); +} + +static int vidioc_enum_framesizes(struct file *file, void *priv, + struct v4l2_frmsizeenum *fsize) +{ + if (fsize->index) + return -EINVAL; + if (!helix_find_fmt(fsize->pixel_format, V4L2_BUF_TYPE_VIDEO_CAPTURE) && + !helix_find_fmt(fsize->pixel_format, V4L2_BUF_TYPE_VIDEO_OUTPUT)) + return -EINVAL; + + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; + fsize->stepwise.min_width = HELIX_MIN_W; + fsize->stepwise.max_width = HELIX_MAX_W; + fsize->stepwise.step_width = 2; + fsize->stepwise.min_height = HELIX_MIN_H; + fsize->stepwise.max_height = HELIX_MAX_H; + fsize->stepwise.step_height = 2; + return 0; +} + +static int vidioc_g_selection(struct file *file, void *priv, + struct v4l2_selection *s) +{ + struct helix_ctx *ctx = priv; + + if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) + return -EINVAL; + + switch (s->target) { + case V4L2_SEL_TGT_CROP: + s->r = ctx->crop; + break; + case V4L2_SEL_TGT_CROP_DEFAULT: + case V4L2_SEL_TGT_CROP_BOUNDS: + s->r.left = 0; + s->r.top = 0; + s->r.width = ctx->src_fmt.fmt.pix.width; + s->r.height = ctx->src_fmt.fmt.pix.height; + break; + default: + return -EINVAL; + } + return 0; +} + +static u32 helix_calc_dst_sizeimage(u32 fourcc, u32 width, u32 height, + u32 requested) +{ + u32 raw = ALIGN(width, 16) * ALIGN(height, 16) * 3 / 2; + u32 min_sz, max_sz; + + if (fourcc == V4L2_PIX_FMT_H264) { + max_sz = HELIX_BOUNCE_BUF_SIZE; + min_sz = clamp_val(PAGE_ALIGN(raw / 2), 128U * 1024U, max_sz); + } else { + max_sz = HELIX_BOUNCE_BUF_SIZE + 4096U; + min_sz = clamp_val(PAGE_ALIGN(raw / 2 + 4096U), 128U * 1024U, + max_sz); + } + if (requested > min_sz) + return PAGE_ALIGN(min(requested, max_sz)); + return min_sz; +} + +static int vidioc_s_selection(struct file *file, void *priv, + struct v4l2_selection *s) +{ + struct helix_ctx *ctx = priv; + + if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) + return -EINVAL; + + if (s->target != V4L2_SEL_TGT_CROP) + return -EINVAL; + + if (vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->m2m_ctx)) || + vb2_is_busy(v4l2_m2m_get_dst_vq(ctx->m2m_ctx))) + return -EBUSY; + + s->r.left = 0; + s->r.top = 0; + s->r.width = clamp_val(s->r.width, HELIX_MIN_W, ctx->src_fmt.fmt.pix.width); + s->r.height = clamp_val(s->r.height, HELIX_MIN_H, ctx->src_fmt.fmt.pix.height); + s->r.width = ALIGN_DOWN(s->r.width, 2); + s->r.height = ALIGN_DOWN(s->r.height, 2); + + ctx->crop = s->r; + ctx->dst_fmt.fmt.pix.width = s->r.width; + ctx->dst_fmt.fmt.pix.height = s->r.height; + ctx->dst_fmt.fmt.pix.sizeimage = helix_calc_dst_sizeimage( + ctx->dst_fmt.fmt.pix.pixelformat, s->r.width, s->r.height, 0); + return 0; +} + +static void helix_sanitize_colorspace(u32 fourcc, u32 *colorspace, + u32 *xfer_func, u32 *ycbcr_enc, + u32 *quantization) +{ + if (*colorspace == V4L2_COLORSPACE_DEFAULT || + *colorspace >= V4L2_COLORSPACE_LAST || + *colorspace == V4L2_COLORSPACE_BT878 || + (fourcc != V4L2_PIX_FMT_JPEG && + *colorspace == V4L2_COLORSPACE_JPEG)) + *colorspace = V4L2_COLORSPACE_SRGB; + if (*xfer_func == V4L2_XFER_FUNC_DEFAULT || + *xfer_func >= V4L2_XFER_FUNC_LAST) + *xfer_func = V4L2_XFER_FUNC_SRGB; + if (*ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT || + *ycbcr_enc > V4L2_YCBCR_ENC_SMPTE240M) + *ycbcr_enc = V4L2_YCBCR_ENC_601; + if (*quantization == V4L2_QUANTIZATION_DEFAULT || + *quantization > V4L2_QUANTIZATION_LIM_RANGE) + *quantization = V4L2_QUANTIZATION_FULL_RANGE; +} + +static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct helix_ctx *ctx = priv; + const struct helix_fmt *fmt; + u32 req_size = f->fmt.pix.sizeimage; + u32 colorspace = f->fmt.pix.colorspace; + u32 xfer_func = f->fmt.pix.xfer_func; + u32 ycbcr_enc = f->fmt.pix.ycbcr_enc; + u32 quantization = f->fmt.pix.quantization; + + fmt = helix_find_fmt(f->fmt.pix.pixelformat, f->type); + if (!fmt) { + if (V4L2_TYPE_IS_OUTPUT(f->type)) + fmt = &helix_formats[0]; + else + fmt = &helix_formats[3]; + f->fmt.pix.pixelformat = fmt->fourcc; + } + + f->fmt.pix.width = clamp_val(f->fmt.pix.width, HELIX_MIN_W, HELIX_MAX_W); + f->fmt.pix.height = clamp_val(f->fmt.pix.height, HELIX_MIN_H, HELIX_MAX_H); + f->fmt.pix.field = V4L2_FIELD_NONE; + f->fmt.pix.flags = 0; + + if (V4L2_TYPE_IS_OUTPUT(f->type)) { + u32 w = ALIGN_DOWN(f->fmt.pix.width, 2); + u32 h = ALIGN_DOWN(f->fmt.pix.height, 2); + + helix_sanitize_colorspace(f->fmt.pix.pixelformat, &colorspace, + &xfer_func, &ycbcr_enc, + &quantization); + f->fmt.pix.colorspace = colorspace; + f->fmt.pix.xfer_func = xfer_func; + f->fmt.pix.ycbcr_enc = ycbcr_enc; + f->fmt.pix.quantization = quantization; + f->fmt.pix.width = w; + f->fmt.pix.height = h; + if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) { + f->fmt.pix.bytesperline = w * 2; + f->fmt.pix.sizeimage = w * h * 2; + } else { + f->fmt.pix.bytesperline = w; + f->fmt.pix.sizeimage = w * h * 3 / 2; + } + } else { + f->fmt.pix.colorspace = ctx->src_fmt.fmt.pix.colorspace; + f->fmt.pix.xfer_func = ctx->src_fmt.fmt.pix.xfer_func; + f->fmt.pix.ycbcr_enc = ctx->src_fmt.fmt.pix.ycbcr_enc; + f->fmt.pix.quantization = ctx->src_fmt.fmt.pix.quantization; + f->fmt.pix.width = ctx->crop.width; + f->fmt.pix.height = ctx->crop.height; + f->fmt.pix.bytesperline = 0; + f->fmt.pix.sizeimage = helix_calc_dst_sizeimage( + f->fmt.pix.pixelformat, f->fmt.pix.width, + f->fmt.pix.height, req_size); + } + + return 0; +} + +static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct helix_ctx *ctx = priv; + struct vb2_queue *vq; + int ret; + + vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); + if (!vq || vb2_is_busy(vq)) + return -EBUSY; + if (V4L2_TYPE_IS_OUTPUT(f->type) && + vb2_is_busy(v4l2_m2m_get_vq(ctx->m2m_ctx, + V4L2_BUF_TYPE_VIDEO_CAPTURE))) + return -EBUSY; + + ret = vidioc_try_fmt(file, priv, f); + if (ret) + return ret; + + if (V4L2_TYPE_IS_OUTPUT(f->type)) { + ctx->src_fmt = *f; + ctx->crop.left = 0; + ctx->crop.top = 0; + ctx->crop.width = f->fmt.pix.width; + ctx->crop.height = f->fmt.pix.height; + + ctx->dst_fmt.fmt.pix.width = f->fmt.pix.width; + ctx->dst_fmt.fmt.pix.height = f->fmt.pix.height; + ctx->dst_fmt.fmt.pix.colorspace = f->fmt.pix.colorspace; + ctx->dst_fmt.fmt.pix.ycbcr_enc = f->fmt.pix.ycbcr_enc; + ctx->dst_fmt.fmt.pix.quantization = f->fmt.pix.quantization; + ctx->dst_fmt.fmt.pix.xfer_func = f->fmt.pix.xfer_func; + ctx->dst_fmt.fmt.pix.sizeimage = helix_calc_dst_sizeimage( + ctx->dst_fmt.fmt.pix.pixelformat, f->fmt.pix.width, + f->fmt.pix.height, 0); + } else { + ctx->dst_fmt = *f; + } + + return 0; +} + +static int helix_encoder_cmd(struct file *file, void *priv, + struct v4l2_encoder_cmd *ec) +{ + struct helix_ctx *ctx = priv; + int ret; + + ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec); + if (ret < 0) + return ret; + + if (ec->cmd == V4L2_ENC_CMD_STOP && ctx->hdev->workqueue) { + /* + * Flush any already-popped frame on the workqueue before + * inspecting ready queues for V4L2_BUF_FLAG_LAST. + */ + mutex_unlock(&ctx->hdev->dev_mutex); + flush_workqueue(ctx->hdev->workqueue); + mutex_lock(&ctx->hdev->dev_mutex); + if (READ_ONCE(ctx->hdev->disconnected)) + return -ENODEV; + } + + ret = v4l2_m2m_encoder_cmd(file, ctx->m2m_ctx, ec); + if (ret < 0) + return ret; + + if (ec->cmd == V4L2_ENC_CMD_STOP && v4l2_m2m_has_stopped(ctx->m2m_ctx)) + helix_queue_eos(ctx); + + return 0; +} + +static int helix_subscribe_event(struct v4l2_fh *fh, + const struct v4l2_event_subscription *sub) +{ + switch (sub->type) { + case V4L2_EVENT_EOS: + return v4l2_event_subscribe(fh, sub, 2, NULL); + default: + return v4l2_ctrl_subscribe_event(fh, sub); + } +} + +const struct v4l2_ioctl_ops helix_v4l2_ioctl_ops = { + .vidioc_querycap = vidioc_querycap, + + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, + .vidioc_g_fmt_vid_cap = vidioc_g_fmt, + .vidioc_try_fmt_vid_cap = vidioc_try_fmt, + .vidioc_s_fmt_vid_cap = vidioc_s_fmt, + + .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, + .vidioc_g_fmt_vid_out = vidioc_g_fmt, + .vidioc_try_fmt_vid_out = vidioc_try_fmt, + .vidioc_s_fmt_vid_out = vidioc_s_fmt, + + .vidioc_g_selection = vidioc_g_selection, + .vidioc_s_selection = vidioc_s_selection, + + .vidioc_g_parm = vidioc_g_parm, + .vidioc_s_parm = vidioc_s_parm, + .vidioc_enum_framesizes = vidioc_enum_framesizes, + + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, + + .vidioc_streamon = v4l2_m2m_ioctl_streamon, + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, + + .vidioc_encoder_cmd = helix_encoder_cmd, + .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, + + .vidioc_subscribe_event = helix_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, +}; + +static int helix_v4l2_open(struct file *file) +{ + struct helix_dev *hdev = video_drvdata(file); + struct helix_ctx *ctx; + int ret; + + if (mutex_lock_interruptible(&hdev->dev_mutex)) + return -ERESTARTSYS; + + if (hdev->disconnected) { + mutex_unlock(&hdev->dev_mutex); + return -ENODEV; + } + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) { + mutex_unlock(&hdev->dev_mutex); + return -ENOMEM; + } + + BUILD_BUG_ON(offsetof(struct helix_ctx, fh) != 0); + ctx->hdev = hdev; + file->private_data = &ctx->fh; + + ret = helix_v4l2_init_ctx(ctx); + if (ret) { + kfree(ctx); + mutex_unlock(&hdev->dev_mutex); + return ret; + } + if (list_empty(&hdev->contexts)) { + helix_cp0_config7_enable(); + mutex_lock(&hdev->hw_lock); + ret = helix_hw_quiesce(hdev); + mutex_unlock(&hdev->hw_lock); + if (ret) { + helix_cp0_config7_disable(); + helix_v4l2_cleanup_ctx(ctx); + kfree(ctx); + mutex_unlock(&hdev->dev_mutex); + return ret; + } + } + list_add_tail(&ctx->list, &hdev->contexts); + + mutex_unlock(&hdev->dev_mutex); + return 0; +} + +static int helix_v4l2_release(struct file *file) +{ + struct helix_ctx *ctx = file->private_data; + struct helix_dev *hdev = ctx->hdev; + + WRITE_ONCE(ctx->stopping, true); + helix_job_abort(ctx); + + mutex_lock(&hdev->dev_mutex); + list_del(&ctx->list); + if (list_empty(&hdev->contexts)) + helix_cp0_config7_disable(); + mutex_unlock(&hdev->dev_mutex); + + helix_v4l2_cleanup_ctx(ctx); + kfree(ctx); + + return 0; +} + +const struct v4l2_file_operations helix_v4l2_fops = { + .owner = THIS_MODULE, + .open = helix_v4l2_open, + .release = helix_v4l2_release, + .poll = v4l2_m2m_fop_poll, + .unlocked_ioctl = video_ioctl2, + .mmap = v4l2_m2m_fop_mmap, +}; + +int helix_v4l2_init_ctx(struct helix_ctx *ctx) +{ + struct helix_dev *hdev = ctx->hdev; + int ret; + + v4l2_fh_init(&ctx->fh, &hdev->vfd); + v4l2_fh_add(&ctx->fh); + + ctx->m2m_ctx = v4l2_m2m_ctx_init(hdev->m2m_dev, ctx, helix_queue_init); + if (IS_ERR(ctx->m2m_ctx)) { + ret = PTR_ERR(ctx->m2m_ctx); + v4l2_fh_del(&ctx->fh); + v4l2_fh_exit(&ctx->fh); + return ret; + } + ctx->fh.m2m_ctx = ctx->m2m_ctx; + + ctx->jpeg_quality = 75; + helix_calc_qtable(ctx->jpeg_quality, ctx->qtable, ctx->qmem); + ctx->gop_size = 30; + ctx->i_frame_qp = 26; + ctx->p_frame_qp = 28; + ctx->min_qp = 10; + ctx->max_qp = 51; + ctx->h264_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH; + ctx->h264_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0; + ctx->header_mode = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME; + ctx->force_key_frame = false; + ctx->repeat_seq_header = false; + ctx->frame_num = 0; + ctx->sequence = 0; + ctx->idr_pic_id = 0; + ctx->gop_count = 0; + ctx->h264_bufs_allocated = false; + helix_rc_init(&ctx->rc); + + v4l2_ctrl_handler_init(&ctx->hdl, 16); + v4l2_ctrl_new_std(&ctx->hdl, NULL, + V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 1, 1, 1); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, 1000, 1, 30); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 26); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 28); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 10); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51); + v4l2_ctrl_new_std_menu(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_H264_PROFILE, + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, + ~(1U << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH), + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); + v4l2_ctrl_new_std_menu(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_H264_LEVEL, + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, + ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) | + (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) | + (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)), + V4L2_MPEG_VIDEO_H264_LEVEL_4_0); + v4l2_ctrl_new_std_menu(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_HEADER_MODE, + V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, + ~(1U << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME), + V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 0, 1, 1, 0); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 0); + v4l2_ctrl_new_std_menu(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, + V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0, + V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_BITRATE, + 16000, 20000000, 1000, 2000000); + v4l2_ctrl_new_std(&ctx->hdl, &helix_ctrl_ops, + V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, + 16000, 20000000, 1000, 4000000); + + if (ctx->hdl.error) { + ret = ctx->hdl.error; + v4l2_ctrl_handler_free(&ctx->hdl); + v4l2_m2m_ctx_release(ctx->m2m_ctx); + v4l2_fh_del(&ctx->fh); + v4l2_fh_exit(&ctx->fh); + return ret; + } + + ctx->fh.ctrl_handler = &ctx->hdl; + v4l2_ctrl_handler_setup(&ctx->hdl); + + ctx->crop.left = 0; + ctx->crop.top = 0; + ctx->crop.width = HELIX_DEF_W; + ctx->crop.height = HELIX_DEF_H; + + /* Dimensions must contain complete 16x16 MCUs. */ + ctx->src_fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; + ctx->src_fmt.fmt.pix.width = HELIX_DEF_W; + ctx->src_fmt.fmt.pix.height = HELIX_DEF_H; + ctx->src_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12; + ctx->src_fmt.fmt.pix.field = V4L2_FIELD_NONE; + ctx->src_fmt.fmt.pix.bytesperline = HELIX_DEF_W; + ctx->src_fmt.fmt.pix.sizeimage = HELIX_DEF_W * HELIX_DEF_H * 3 / 2; + ctx->src_fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; + ctx->src_fmt.fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_601; + ctx->src_fmt.fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE; + ctx->src_fmt.fmt.pix.xfer_func = V4L2_XFER_FUNC_SRGB; + + ctx->dst_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + ctx->dst_fmt.fmt.pix.width = HELIX_DEF_W; + ctx->dst_fmt.fmt.pix.height = HELIX_DEF_H; + ctx->dst_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG; + ctx->dst_fmt.fmt.pix.field = V4L2_FIELD_NONE; + ctx->dst_fmt.fmt.pix.bytesperline = 0; + ctx->dst_fmt.fmt.pix.sizeimage = helix_calc_dst_sizeimage( + V4L2_PIX_FMT_JPEG, HELIX_DEF_W, HELIX_DEF_H, 0); + ctx->dst_fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; + ctx->dst_fmt.fmt.pix.ycbcr_enc = V4L2_YCBCR_ENC_601; + ctx->dst_fmt.fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE; + ctx->dst_fmt.fmt.pix.xfer_func = V4L2_XFER_FUNC_SRGB; + + return 0; +} + +void helix_v4l2_cleanup_ctx(struct helix_ctx *ctx) +{ + mutex_lock(&ctx->hdev->hw_lock); + helix_release_h264_dma(ctx); + mutex_unlock(&ctx->hdev->hw_lock); + v4l2_ctrl_handler_free(&ctx->hdl); + v4l2_m2m_ctx_release(ctx->m2m_ctx); + v4l2_fh_del(&ctx->fh); + v4l2_fh_exit(&ctx->fh); +} diff --git a/drivers/media/platform/ingenic/helix/helix_vdma.c b/drivers/media/platform/ingenic/helix/helix_vdma.c new file mode 100644 index 0000000000..32c3e6f4ff --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_vdma.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include "helix_drv.h" +#include "helix_vdma.h" + +void helix_vdma_task_init(struct helix_vdma_task *task, __le32 *vaddr, + dma_addr_t paddr, size_t max_bytes) +{ + task->vaddr = vaddr; + task->paddr = paddr; + task->max_words = max_bytes / sizeof(__le32); + task->count = 0; + task->overflow = false; +} + +void helix_vdma_append(struct helix_vdma_task *task, u32 reg, u32 val) +{ + if (task->count + 2 > task->max_words) { + task->overflow = true; + return; + } + + task->vaddr[task->count++] = cpu_to_le32(val); + task->vaddr[task->count++] = cpu_to_le32(VDMA_ACFG_VLD | VDMA_ACFG_IDX(reg)); +} + +void helix_vdma_terminate(struct helix_vdma_task *task) +{ + if (task->overflow || task->count < 2) + return; + + task->vaddr[task->count - 1] |= cpu_to_le32(VDMA_ACFG_TERM); +} + +int helix_vdma_kick(struct helix_dev *hdev, struct helix_vdma_task *task) +{ + if (WARN_ON_ONCE(task->overflow)) + return -ENOSPC; + if (task->count < 2) + return -EINVAL; + + writel(SCH_GLBC_HIAXI | SCH_INTE_ENDF | SCH_INTE_TLBERR | + SCH_INTE_BSERR | SCH_INTE_ACFGERR | SCH_INTE_RESERR, + hdev->base + REG_SCH_GLBC); + dma_wmb(); + writel(VDMA_ACFG_DHA(task->paddr) | VDMA_ACFG_RUN, hdev->base + REG_VDMA_TASKRG_T21); + return 0; +} + +int helix_vdma_wait(struct helix_dev *hdev, unsigned int timeout_ms) +{ + unsigned long timeout = msecs_to_jiffies(timeout_ms ? timeout_ms : 2000); + long ret; + + ret = wait_for_completion_timeout(&hdev->completion, timeout); + if (ret == 0) + WRITE_ONCE(hdev->irq_status, readl(hdev->base + REG_SCH_STAT)); + writel(0, hdev->base + REG_SCH_GLBC); + synchronize_irq(hdev->irq); + + if (ret == 0) + return -ETIMEDOUT; + if (READ_ONCE(hdev->aborting) && !READ_ONCE(hdev->irq_status)) + return -ECANCELED; + + if (!(hdev->irq_status & SCH_STAT_ENDFLAG) || + hdev->irq_status & (SCH_STAT_BSERR | SCH_STAT_ACFGERR | SCH_STAT_ORESERR)) + return -EIO; + + return 0; +} + +int helix_alloc_h264_buffers(struct helix_dev *hdev, + struct helix_h264_buffers *bufs, + u32 width, u32 height) +{ + u32 coded_w = ALIGN(width, 16); + u32 coded_h = ALIGN(height, 16); + u32 pad = 64; + size_t y_size = (coded_h + pad) * coded_w; + size_t c_size = (coded_h / 2 + (pad / 2)) * coded_w; + int i; + + memset(bufs, 0, sizeof(*bufs)); + bufs->recon_y_size = y_size; + bufs->recon_c_size = c_size; + bufs->cur_slot = 0; + + for (i = 0; i < 2; i++) { + bufs->recon_y_vaddr[i] = dma_alloc_coherent(hdev->dev, y_size, + &bufs->recon_y_paddr[i], + GFP_KERNEL); + if (!bufs->recon_y_vaddr[i]) + goto err_free; + + memset(bufs->recon_y_vaddr[i], 0, y_size); + + bufs->recon_c_vaddr[i] = dma_alloc_coherent(hdev->dev, c_size, + &bufs->recon_c_paddr[i], + GFP_KERNEL); + if (!bufs->recon_c_vaddr[i]) + goto err_free; + + memset(bufs->recon_c_vaddr[i], 0x80, c_size); + } + + return 0; + +err_free: + helix_free_h264_buffers(hdev, bufs); + return -ENOMEM; +} + +void helix_free_h264_buffers(struct helix_dev *hdev, + struct helix_h264_buffers *bufs) +{ + int i; + + for (i = 0; i < 2; i++) { + if (bufs->recon_c_vaddr[i]) { + dma_free_coherent(hdev->dev, bufs->recon_c_size, + bufs->recon_c_vaddr[i], + bufs->recon_c_paddr[i]); + bufs->recon_c_vaddr[i] = NULL; + } + if (bufs->recon_y_vaddr[i]) { + dma_free_coherent(hdev->dev, bufs->recon_y_size, + bufs->recon_y_vaddr[i], + bufs->recon_y_paddr[i]); + bufs->recon_y_vaddr[i] = NULL; + } + } +} diff --git a/drivers/media/platform/ingenic/helix/helix_vdma.h b/drivers/media/platform/ingenic/helix/helix_vdma.h new file mode 100644 index 0000000000..e08e565b49 --- /dev/null +++ b/drivers/media/platform/ingenic/helix/helix_vdma.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2026 Difan Zhang */ + +#ifndef _HELIX_VDMA_H_ +#define _HELIX_VDMA_H_ + +#include +#include +#include "helix_hw.h" + +struct helix_dev; + +struct helix_vdma_task { + __le32 *vaddr; + dma_addr_t paddr; + size_t max_words; + size_t count; + bool overflow; +}; + +struct helix_h264_buffers { + void *recon_y_vaddr[2]; + dma_addr_t recon_y_paddr[2]; + void *recon_c_vaddr[2]; + dma_addr_t recon_c_paddr[2]; + size_t recon_y_size; + size_t recon_c_size; + + int cur_slot; +}; + +void helix_vdma_task_init(struct helix_vdma_task *task, __le32 *vaddr, + dma_addr_t paddr, size_t max_bytes); +void helix_vdma_append(struct helix_vdma_task *task, u32 reg, u32 val); +void helix_vdma_terminate(struct helix_vdma_task *task); +int helix_vdma_kick(struct helix_dev *hdev, struct helix_vdma_task *task); +int helix_vdma_wait(struct helix_dev *hdev, unsigned int timeout_ms); + +int helix_alloc_h264_buffers(struct helix_dev *hdev, + struct helix_h264_buffers *bufs, + u32 width, u32 height); +void helix_free_h264_buffers(struct helix_dev *hdev, + struct helix_h264_buffers *bufs); + +#endif From ef7e81a333d58fb43fb52f9a7258f596464fc00b Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:25:01 -0800 Subject: [PATCH 68/78] dt-bindings: media: Add the Ingenic T23 IPU converter Describe the T23 IPU converter register windows, clocks, interrupts and reset resources. Signed-off-by: Difan Zhang --- .../bindings/media/ingenic,t23-ipu.yaml | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/ingenic,t23-ipu.yaml diff --git a/Documentation/devicetree/bindings/media/ingenic,t23-ipu.yaml b/Documentation/devicetree/bindings/media/ingenic,t23-ipu.yaml new file mode 100644 index 0000000000..280f055495 --- /dev/null +++ b/Documentation/devicetree/bindings/media/ingenic,t23-ipu.yaml @@ -0,0 +1,55 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/ingenic,t23-ipu.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Ingenic T23 Image Processing Unit (IPU v1.3) + +maintainers: + - Paul Cercueil + +description: + Hardware NV12/NV21 to packed RGB32/NV21 color-space converter in Ingenic + T23 SoCs. + +properties: + compatible: + const: ingenic,t23-ipu + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + + clock-names: + const: ipu + + memory-region: + maxItems: 1 + +required: + - compatible + - reg + - interrupts + - clocks + - clock-names + +additionalProperties: false + +examples: + - | + #include + + ipu@13080000 { + compatible = "ingenic,t23-ipu"; + reg = <0x13080000 0x8000>; + interrupt-parent = <&intc>; + interrupts = <6>; + clocks = <&cgu CLK_IPU>; + clock-names = "ipu"; + }; From 8f22ca8412eab89350bfc3b7bfd7f670a2fc1c57 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 07:05:02 -0800 Subject: [PATCH 69/78] media: platform: ingenic: Add the T23 IPU V4L2 converter Add the T23 IPU v1.3 memory-to-memory color converter with DMA-BUF import and NV12/NV21, RGB565 and ARGB32/XRGB32 formats. Signed-off-by: Difan Zhang --- drivers/media/platform/ingenic/Kconfig | 1 + drivers/media/platform/ingenic/Makefile | 1 + drivers/media/platform/ingenic/ipu/Kconfig | 17 + drivers/media/platform/ingenic/ipu/Makefile | 5 + .../media/platform/ingenic/ipu/jz_ipu_v13.c | 1075 +++++++++++++++++ .../media/platform/ingenic/ipu/jz_regs_v13.h | 249 ++++ 6 files changed, 1348 insertions(+) create mode 100644 drivers/media/platform/ingenic/ipu/Kconfig create mode 100644 drivers/media/platform/ingenic/ipu/Makefile create mode 100644 drivers/media/platform/ingenic/ipu/jz_ipu_v13.c create mode 100644 drivers/media/platform/ingenic/ipu/jz_regs_v13.h diff --git a/drivers/media/platform/ingenic/Kconfig b/drivers/media/platform/ingenic/Kconfig index c13234ad64..33c3ec996c 100644 --- a/drivers/media/platform/ingenic/Kconfig +++ b/drivers/media/platform/ingenic/Kconfig @@ -21,3 +21,4 @@ config VIDEO_INGENIC_PTZ_MOTOR source "drivers/media/platform/ingenic/tx-isp/Kconfig" source "drivers/media/platform/ingenic/helix/Kconfig" +source "drivers/media/platform/ingenic/ipu/Kconfig" diff --git a/drivers/media/platform/ingenic/Makefile b/drivers/media/platform/ingenic/Makefile index a0f3023d6f..1b16e1d670 100644 --- a/drivers/media/platform/ingenic/Makefile +++ b/drivers/media/platform/ingenic/Makefile @@ -2,3 +2,4 @@ obj-$(CONFIG_VIDEO_INGENIC_PTZ_MOTOR) += ptz-motor.o obj-$(CONFIG_VIDEO_INGENIC_ISP) += tx-isp/ obj-$(CONFIG_VIDEO_INGENIC_HELIX) += helix/ +obj-$(CONFIG_VIDEO_INGENIC_IPU) += ipu/ diff --git a/drivers/media/platform/ingenic/ipu/Kconfig b/drivers/media/platform/ingenic/ipu/Kconfig new file mode 100644 index 0000000000..2422e3f941 --- /dev/null +++ b/drivers/media/platform/ingenic/ipu/Kconfig @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +config VIDEO_INGENIC_IPU + tristate "Ingenic T23 IPU v13 V4L2 M2M image processor" + depends on V4L_MEM2MEM_DRIVERS + depends on VIDEO_DEV + depends on MACH_INGENIC || COMPILE_TEST + depends on OF && HAS_DMA + select VIDEOBUF2_DMA_CONTIG + select V4L2_MEM2MEM_DEV + help + V4L2 memory-to-memory NV12/NV21 and RGB format conversion and + crop/compose driver for Ingenic T23 IPU v13 hardware. + + To compile this driver as a module, choose M here: the module + will be called ingenic-ipu. diff --git a/drivers/media/platform/ingenic/ipu/Makefile b/drivers/media/platform/ingenic/ipu/Makefile new file mode 100644 index 0000000000..dd2a759268 --- /dev/null +++ b/drivers/media/platform/ingenic/ipu/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026 Difan Zhang + +obj-$(CONFIG_VIDEO_INGENIC_IPU) += ingenic-ipu.o +ingenic-ipu-y := jz_ipu_v13.o diff --git a/drivers/media/platform/ingenic/ipu/jz_ipu_v13.c b/drivers/media/platform/ingenic/ipu/jz_ipu_v13.c new file mode 100644 index 0000000000..21ed552b2d --- /dev/null +++ b/drivers/media/platform/ingenic/ipu/jz_ipu_v13.c @@ -0,0 +1,1075 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2015 Ingenic Semiconductor Co., Ltd. + * Copyright (C) 2026 Difan Zhang + * Ingenic T23 IPU format conversion. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "jz_regs_v13.h" + +struct ipu_ctx; + +struct ipu_dev { + struct device *dev; + struct platform_device *dma_pdev; + struct kref ref; + struct v4l2_device v4l2; + struct video_device *video; + struct v4l2_m2m_dev *m2m; + struct mutex lock; + struct list_head contexts; + void __iomem *regs; + struct clk *clk; + int irq; + struct work_struct work; + struct workqueue_struct *workqueue; + struct completion done; + struct ipu_ctx *active_ctx; + u32 irq_status; + bool removed; + bool failed; + bool reserved_mem; + bool quarantined; +}; + +struct ipu_ctx { + struct ipu_dev *ipu; + struct v4l2_fh fh; + struct v4l2_ctrl_handler ctrl_handler; + struct list_head list; + struct v4l2_pix_format src; + struct v4l2_pix_format dst; + struct v4l2_rect crop; + struct v4l2_rect compose; + u32 alpha; + u32 src_sequence; + u32 dst_sequence; + atomic_t abort; +}; + +static struct ipu_ctx *fh_to_ctx(void *fh) +{ + return container_of(fh, struct ipu_ctx, fh); +} + +static int ipu_s_ctrl(struct v4l2_ctrl *ctrl) +{ + struct ipu_ctx *ctx = container_of(ctrl->handler, struct ipu_ctx, ctrl_handler); + + switch (ctrl->id) { + case V4L2_CID_ALPHA_COMPONENT: + ctx->alpha = ctrl->val; + break; + default: + return -EINVAL; + } + return 0; +} + +static const struct v4l2_ctrl_ops ipu_ctrl_ops = { + .s_ctrl = ipu_s_ctrl, +}; + +static struct v4l2_pix_format *ipu_format(struct ipu_ctx *ctx, + enum v4l2_buf_type type) +{ + return V4L2_TYPE_IS_OUTPUT(type) ? &ctx->src : &ctx->dst; +} + +/* + * The IPU writes every output line rounded up to a multiple of 64 pixels, + * and needs 8-byte aligned line addresses. + */ +#define IPU_LINE_ALIGN 64 +#define IPU_X_ALIGN 8 + +static void ipu_pix_format(struct v4l2_pix_format *p, u32 fourcc, + unsigned int width, unsigned int height) +{ + memset(p, 0, sizeof(*p)); + p->width = width; + p->height = height; + p->pixelformat = fourcc; + p->field = V4L2_FIELD_NONE; + p->colorspace = V4L2_COLORSPACE_SMPTE170M; + p->xfer_func = V4L2_XFER_FUNC_709; + p->ycbcr_enc = V4L2_YCBCR_ENC_601; + + switch (fourcc) { + case V4L2_PIX_FMT_ABGR32: + case V4L2_PIX_FMT_ARGB32: + case V4L2_PIX_FMT_XBGR32: + case V4L2_PIX_FMT_XRGB32: + p->bytesperline = ALIGN(width, IPU_LINE_ALIGN) * 4; + p->sizeimage = p->bytesperline * height; + p->quantization = V4L2_QUANTIZATION_FULL_RANGE; + break; + case V4L2_PIX_FMT_NV21: + p->bytesperline = ALIGN(width, IPU_LINE_ALIGN); + p->sizeimage = p->bytesperline * height * 3 / 2; + p->quantization = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_PIX_FMT_NV12: + default: + p->bytesperline = width; + p->sizeimage = width * height * 3 / 2; + p->quantization = V4L2_QUANTIZATION_LIM_RANGE; + break; + } +} + +/* The IPU converts NV12 without scaling; it has no resizer. */ +static const u32 ipu_src_formats[] = { + V4L2_PIX_FMT_NV12, +}; + +static const u32 ipu_dst_formats[] = { + V4L2_PIX_FMT_NV21, + V4L2_PIX_FMT_ABGR32, + V4L2_PIX_FMT_ARGB32, + V4L2_PIX_FMT_XBGR32, + V4L2_PIX_FMT_XRGB32, +}; + +/* IPU_D_FMT output codes; the hardware names RGB orders from the low byte. */ +static u32 ipu_d_fmt(u32 fourcc) +{ + switch (fourcc) { + case V4L2_PIX_FMT_ABGR32: + case V4L2_PIX_FMT_XBGR32: + return IPU_D_FMT_OUT_BGRA8888; /* B, G, R, A in memory */ + case V4L2_PIX_FMT_ARGB32: + case V4L2_PIX_FMT_XRGB32: + return IPU_D_FMT_OUT_ARGB8888; /* A, R, G, B in memory */ + default: + return IPU_D_FMT_OUT_NV21; /* NV21 */ + } +} + +static unsigned int ipu_bpp(u32 fourcc) +{ + return fourcc == V4L2_PIX_FMT_NV21 ? 1 : 4; +} + +/* + * Compose has the crop size and lies inside the destination. Unless its width + * is a multiple of the line alignment, it ends at the right edge and the + * rounded-up line still fits the padded line, so the extra pixels land in + * the bytesperline padding. + */ +static bool ipu_compose_ok(struct ipu_ctx *ctx, const struct v4l2_rect *r) +{ + u32 line = ctx->dst.bytesperline / ipu_bpp(ctx->dst.pixelformat); + + return r->width == ctx->crop.width && r->height == ctx->crop.height && + r->left + r->width <= ctx->dst.width && + r->top + r->height <= ctx->dst.height && + r->left + ALIGN(r->width, IPU_LINE_ALIGN) <= line && + (r->width % IPU_LINE_ALIGN == 0 || + r->left + r->width == ctx->dst.width); +} + +static bool ipu_geometry_ok(struct ipu_ctx *ctx) +{ + return ipu_compose_ok(ctx, &ctx->compose); +} + +/* Fit compose to the crop size, keeping its position where allowed. */ +static void ipu_fit_compose(struct ipu_ctx *ctx) +{ + struct v4l2_rect *r = &ctx->compose; + + r->width = ctx->crop.width; + r->height = ctx->crop.height; + if (r->width > ctx->dst.width || r->height > ctx->dst.height) + return; + if (r->left + r->width > ctx->dst.width || + r->width % IPU_LINE_ALIGN) + r->left = ctx->dst.width - r->width; + if (r->top + r->height > ctx->dst.height) + r->top = (ctx->dst.height - r->height) & ~1; +} + +static int ipu_querycap(struct file *file, void *priv, + struct v4l2_capability *cap) +{ + struct ipu_dev *ipu = video_drvdata(file); + + strscpy(cap->driver, "t23-ipu", sizeof(cap->driver)); + strscpy(cap->card, "T23 IPU", sizeof(cap->card)); + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", + dev_name(ipu->dev)); + return 0; +} + +static int ipu_enum_fmt(struct file *file, void *priv, + struct v4l2_fmtdesc *f) +{ + const u32 *formats; + unsigned int num_formats; + + if (V4L2_TYPE_IS_OUTPUT(f->type)) { + formats = ipu_src_formats; + num_formats = ARRAY_SIZE(ipu_src_formats); + } else { + formats = ipu_dst_formats; + num_formats = ARRAY_SIZE(ipu_dst_formats); + } + + if (f->index >= num_formats) + return -EINVAL; + + f->pixelformat = formats[f->index]; + return 0; +} + +static int ipu_get_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + f->fmt.pix = *ipu_format(fh_to_ctx(priv), f->type); + return 0; +} + +static int ipu_try_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct ipu_ctx *ctx = fh_to_ctx(priv); + u32 fourcc = f->fmt.pix.pixelformat; + u32 colorspace = f->fmt.pix.colorspace; + u32 xfer_func = f->fmt.pix.xfer_func; + u32 ycbcr_enc = f->fmt.pix.ycbcr_enc; + u32 quantization = f->fmt.pix.quantization; + unsigned int width, height; + const u32 *formats; + unsigned int num_formats, i; + bool found = false; + + if (V4L2_TYPE_IS_OUTPUT(f->type)) { + formats = ipu_src_formats; + num_formats = ARRAY_SIZE(ipu_src_formats); + } else { + formats = ipu_dst_formats; + num_formats = ARRAY_SIZE(ipu_dst_formats); + } + + for (i = 0; i < num_formats; i++) { + if (formats[i] == fourcc) { + found = true; + break; + } + } + if (!found) + fourcc = formats[0]; + + width = clamp_t(unsigned int, f->fmt.pix.width, 32, 1920) & ~(IPU_X_ALIGN - 1); + height = clamp_t(unsigned int, f->fmt.pix.height, 32, 1080) & ~1; + + ipu_pix_format(&f->fmt.pix, fourcc, width, height); + if (V4L2_TYPE_IS_OUTPUT(f->type)) { + if (colorspace >= V4L2_COLORSPACE_SMPTE170M && + colorspace < V4L2_COLORSPACE_LAST && + colorspace != V4L2_COLORSPACE_BT878 && + colorspace != V4L2_COLORSPACE_JPEG) + f->fmt.pix.colorspace = colorspace; + if (xfer_func != V4L2_XFER_FUNC_DEFAULT && + xfer_func < V4L2_XFER_FUNC_LAST) + f->fmt.pix.xfer_func = xfer_func; + if (ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT && + ycbcr_enc <= V4L2_YCBCR_ENC_SMPTE240M) + f->fmt.pix.ycbcr_enc = ycbcr_enc; + if (quantization != V4L2_QUANTIZATION_DEFAULT && + quantization <= V4L2_QUANTIZATION_LIM_RANGE) + f->fmt.pix.quantization = quantization; + } else { + f->fmt.pix.colorspace = ctx->src.colorspace; + f->fmt.pix.xfer_func = ctx->src.xfer_func; + f->fmt.pix.ycbcr_enc = ctx->src.ycbcr_enc; + f->fmt.pix.quantization = ctx->src.quantization; + } + return 0; +} + +static int ipu_set_fmt(struct file *file, void *priv, struct v4l2_format *f) +{ + struct ipu_ctx *ctx = fh_to_ctx(priv); + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + + if (vb2_is_busy(v4l2_m2m_get_src_vq(m2m)) || + vb2_is_busy(v4l2_m2m_get_dst_vq(m2m))) + return -EBUSY; + + ipu_try_fmt(file, priv, f); + *ipu_format(ctx, f->type) = f->fmt.pix; + + if (V4L2_TYPE_IS_OUTPUT(f->type)) { + ctx->crop.left = 0; + ctx->crop.top = 0; + ctx->crop.width = f->fmt.pix.width; + ctx->crop.height = f->fmt.pix.height; + ctx->dst.colorspace = ctx->src.colorspace; + ctx->dst.xfer_func = ctx->src.xfer_func; + ctx->dst.ycbcr_enc = ctx->src.ycbcr_enc; + ctx->dst.quantization = ctx->src.quantization; + } + ctx->compose.left = 0; + ctx->compose.top = 0; + ipu_fit_compose(ctx); + + return 0; +} + +static int ipu_g_selection(struct file *file, void *priv, + struct v4l2_selection *s) +{ + struct ipu_ctx *ctx = fh_to_ctx(priv); + + if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { + switch (s->target) { + case V4L2_SEL_TGT_CROP: + s->r = ctx->crop; + return 0; + case V4L2_SEL_TGT_CROP_DEFAULT: + case V4L2_SEL_TGT_CROP_BOUNDS: + s->r.left = 0; + s->r.top = 0; + s->r.width = ctx->src.width; + s->r.height = ctx->src.height; + return 0; + default: + return -EINVAL; + } + } else if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + switch (s->target) { + case V4L2_SEL_TGT_COMPOSE: + s->r = ctx->compose; + return 0; + case V4L2_SEL_TGT_COMPOSE_DEFAULT: + s->r.left = 0; + s->r.top = 0; + s->r.width = ctx->crop.width; + s->r.height = ctx->crop.height; + return 0; + case V4L2_SEL_TGT_COMPOSE_BOUNDS: + s->r.left = 0; + s->r.top = 0; + s->r.width = ctx->dst.width; + s->r.height = ctx->dst.height; + return 0; + default: + return -EINVAL; + } + } + + return -EINVAL; +} + +static int ipu_s_selection(struct file *file, void *priv, + struct v4l2_selection *s) +{ + struct ipu_ctx *ctx = fh_to_ctx(priv); + struct v4l2_m2m_ctx *m2m = ctx->fh.m2m_ctx; + + /* A running job reads the geometry without the device lock. */ + if (vb2_is_busy(v4l2_m2m_get_src_vq(m2m)) || + vb2_is_busy(v4l2_m2m_get_dst_vq(m2m))) + return -EBUSY; + + if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && + s->target == V4L2_SEL_TGT_CROP) { + s->r.left = clamp_t(u32, s->r.left, 0, ctx->src.width - 32) & + ~(IPU_X_ALIGN - 1); + s->r.top = clamp_t(u32, s->r.top, 0, ctx->src.height - 32) & ~1; + s->r.width = clamp_t(u32, s->r.width, 32, ctx->src.width - s->r.left) & + ~(IPU_X_ALIGN - 1); + s->r.height = clamp_t(u32, s->r.height, 32, ctx->src.height - s->r.top) & ~1; + ctx->crop = s->r; + ipu_fit_compose(ctx); + return 0; + } else if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && + s->target == V4L2_SEL_TGT_COMPOSE) { + /* Only the position can change: the IPU does not scale. */ + if (ctx->crop.width > ctx->dst.width || + ctx->crop.height > ctx->dst.height) + return -EINVAL; + s->r.left = clamp_t(s32, s->r.left, 0, + ctx->dst.width - ctx->crop.width) & + ~(IPU_X_ALIGN - 1); + s->r.top = clamp_t(s32, s->r.top, 0, + ctx->dst.height - ctx->crop.height) & ~1; + s->r.width = ctx->crop.width; + s->r.height = ctx->crop.height; + if (ctx->crop.width % IPU_LINE_ALIGN) + s->r.left = ctx->dst.width - ctx->crop.width; + if (!ipu_compose_ok(ctx, &s->r)) + return -EINVAL; + ctx->compose = s->r; + return 0; + } + + return -EINVAL; +} + +static int ipu_queue_setup(struct vb2_queue *q, unsigned int *count, + unsigned int *planes, unsigned int sizes[], + struct device *alloc_devs[]) +{ + struct ipu_ctx *ctx = vb2_get_drv_priv(q); + struct v4l2_pix_format *fmt = ipu_format(ctx, q->type); + + if (READ_ONCE(ctx->ipu->removed)) + return -ENODEV; + if (*planes) + return *planes == 1 && sizes[0] >= fmt->sizeimage ? 0 : -EINVAL; + *planes = 1; + sizes[0] = fmt->sizeimage; + return 0; +} + +static int ipu_buf_prepare(struct vb2_buffer *vb) +{ + struct ipu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); + struct v4l2_pix_format *fmt = ipu_format(ctx, vb->type); + struct vb2_v4l2_buffer *buf = to_vb2_v4l2_buffer(vb); + + if (READ_ONCE(ctx->ipu->removed)) + return -ENODEV; + if (READ_ONCE(ctx->ipu->failed)) + return -EIO; + if (vb2_plane_size(vb, 0) < fmt->sizeimage) + return -EINVAL; + if (V4L2_TYPE_IS_OUTPUT(vb->type) && buf->field != V4L2_FIELD_NONE && + buf->field != V4L2_FIELD_ANY) + return -EINVAL; + if (V4L2_TYPE_IS_OUTPUT(vb->type)) { + if (!vb2_get_plane_payload(vb, 0)) + vb2_set_plane_payload(vb, 0, fmt->sizeimage); + else if (vb2_get_plane_payload(vb, 0) < fmt->sizeimage) + return -EINVAL; + } else { + vb2_set_plane_payload(vb, 0, fmt->sizeimage); + } + to_vb2_v4l2_buffer(vb)->field = V4L2_FIELD_NONE; + return 0; +} + +static void ipu_buf_queue(struct vb2_buffer *vb) +{ + struct ipu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); + + v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb)); +} + +static void ipu_return_buffers(struct vb2_queue *q, enum vb2_buffer_state state) +{ + struct ipu_ctx *ctx = vb2_get_drv_priv(q); + struct vb2_v4l2_buffer *buf; + + for (;;) { + buf = V4L2_TYPE_IS_OUTPUT(q->type) ? + v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx) : + v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); + if (!buf) + break; + v4l2_m2m_buf_done(buf, state); + } +} + +static int ipu_start_streaming(struct vb2_queue *q, unsigned int count) +{ + struct ipu_ctx *ctx = vb2_get_drv_priv(q); + + if (READ_ONCE(ctx->ipu->removed) || READ_ONCE(ctx->ipu->failed)) { + ipu_return_buffers(q, VB2_BUF_STATE_QUEUED); + return -ENODEV; + } + if (!ipu_geometry_ok(ctx)) { + ipu_return_buffers(q, VB2_BUF_STATE_QUEUED); + return -EINVAL; + } + if (V4L2_TYPE_IS_OUTPUT(q->type)) + ctx->src_sequence = 0; + else + ctx->dst_sequence = 0; + atomic_set(&ctx->abort, 0); + return 0; +} + +static void ipu_stop_streaming(struct vb2_queue *q) +{ + /* The mem2mem core has already waited for job_abort to finish. */ + ipu_return_buffers(q, VB2_BUF_STATE_ERROR); +} + +static const struct vb2_ops ipu_queue_ops = { + .queue_setup = ipu_queue_setup, + .buf_prepare = ipu_buf_prepare, + .buf_queue = ipu_buf_queue, + .start_streaming = ipu_start_streaming, + .stop_streaming = ipu_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int ipu_queue_init(void *priv, struct vb2_queue *src, + struct vb2_queue *dst) +{ + struct ipu_ctx *ctx = priv; + struct vb2_queue *queues[] = { src, dst }; + unsigned int i; + int ret; + + for (i = 0; i < ARRAY_SIZE(queues); i++) { + struct vb2_queue *q = queues[i]; + + q->type = i ? V4L2_BUF_TYPE_VIDEO_CAPTURE : + V4L2_BUF_TYPE_VIDEO_OUTPUT; + q->io_modes = VB2_MMAP | VB2_DMABUF; + q->drv_priv = ctx; + q->buf_struct_size = sizeof(struct v4l2_m2m_buffer); + q->ops = &ipu_queue_ops; + q->mem_ops = &vb2_dma_contig_memops; + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + q->lock = &ctx->ipu->lock; + q->dev = &ctx->ipu->dma_pdev->dev; + ret = vb2_queue_init(q); + if (ret) + return ret; + } + return 0; +} + +static int ipu_quiesce(struct ipu_dev *ipu) +{ + u32 status; + + writel(0, ipu->regs + IPU_GLB_CTRL); + writel(readl(ipu->regs + IPU_TRIGGER) | IPU_STOP, + ipu->regs + IPU_TRIGGER); + writel(readl(ipu->regs + IPU_TRIGGER) | IPU_RESET, + ipu->regs + IPU_TRIGGER); + return readl_poll_timeout(ipu->regs + IPU_STATUS, status, + !(status & MSTATUS_MSK << MSTATUS_SFT), + 10, 100000); +} + +static void ipu_program(struct ipu_ctx *ctx, dma_addr_t src, dma_addr_t dst) +{ + struct ipu_dev *ipu = ctx->ipu; + const struct v4l2_pix_format *in = &ctx->src, *out = &ctx->dst; + const struct v4l2_rect *c = &ctx->crop, *o = &ctx->compose; + u32 bpp = ipu_bpp(out->pixelformat); + + writel((c->width << 16) | c->height, ipu->regs + IPU_IN_FM_GS); + writel(ipu_d_fmt(out->pixelformat), ipu->regs + IPU_D_FMT); + writel(0, ipu->regs + IPU_FM_CTRL); + writel(in->bytesperline, ipu->regs + IPU_Y_STRIDE); + writel(in->bytesperline | (in->bytesperline << 16), + ipu->regs + IPU_UV_STRIDE); + writel(src + c->top * in->bytesperline + c->left, ipu->regs + IPU_Y_ADDR); + writel(src + in->bytesperline * in->height + + c->top / 2 * in->bytesperline + c->left, ipu->regs + IPU_U_ADDR); + writel(out->bytesperline, ipu->regs + IPU_OUT_STRIDE); + writel(out->bytesperline, ipu->regs + IPU_NV_OUT_STRIDE); + writel(dst + o->top * out->bytesperline + o->left * bpp, + ipu->regs + IPU_OUT_ADDR); + writel(dst + out->bytesperline * out->height + + o->top / 2 * out->bytesperline + o->left, + ipu->regs + IPU_NV_OUT_ADDR); + + /* The NV12 background layer carries the output alpha. */ + writel(IPU_OSD_CH0_PARA_DEFAULT, ipu->regs + IPU_OSD_CH0_PARA); + writel(IPU_OSD_CH_BK_PARA_DEFAULT | CH_BK_GLOBAL_ALPHA | + CH_BK_GLO_A(ctx->alpha), ipu->regs + IPU_OSD_CH_BK_PARA); + writel(0, ipu->regs + IPU_OSD_CH1_PARA); + writel(0, ipu->regs + IPU_OSD_CH2_PARA); + writel(0, ipu->regs + IPU_OSD_CH3_PARA); + + /* CSC uses the background input and the channel-zero matrix. */ + writel(IPU_CSC_C0_BT601, ipu->regs + IPU_CH0_CSC_C0_COEF); + writel(IPU_CSC_C1_BT601, ipu->regs + IPU_CH0_CSC_C1_COEF); + writel(IPU_CSC_C2_BT601, ipu->regs + IPU_CH0_CSC_C2_COEF); + writel(IPU_CSC_C3_BT601, ipu->regs + IPU_CH0_CSC_C3_COEF); + writel(IPU_CSC_C4_BT601, ipu->regs + IPU_CH0_CSC_C4_COEF); + writel(IPU_CSC_OFSET_BT601, ipu->regs + IPU_CH0_CSC_OFSET_PARA); + writel(readl(ipu->regs + IPU_STATUS) & ~(OUT_END | SIZE_ERR), + ipu->regs + IPU_STATUS); + writel(IRQ_EN, ipu->regs + IPU_GLB_CTRL); + writel(U32_MAX, ipu->regs + IPU_REG_CTRL); + dma_wmb(); + writel(readl(ipu->regs + IPU_TRIGGER) | IPU_RUN, + ipu->regs + IPU_TRIGGER); +} + +static irqreturn_t ipu_irq(int irq, void *data) +{ + struct ipu_dev *ipu = data; + u32 status = readl(ipu->regs + IPU_STATUS); + + if (!(status & (OUT_END | SIZE_ERR))) + return IRQ_NONE; + writel(0, ipu->regs + IPU_GLB_CTRL); + writel(status & ~(OUT_END | SIZE_ERR), ipu->regs + IPU_STATUS); + WRITE_ONCE(ipu->irq_status, status); + complete(&ipu->done); + return IRQ_HANDLED; +} + +/* + * Keep a buffer's memory allocated when a stuck IPU may still write to it. + */ +static bool ipu_pin_buffer(struct ipu_dev *ipu, struct vb2_buffer *vb) +{ + struct dma_buf *dbuf; + + if (vb->memory == VB2_MEMORY_DMABUF) { + get_dma_buf(vb->planes[0].dbuf); + return true; + } + if (!vb->vb2_queue->mem_ops->get_dmabuf) + return false; + dbuf = vb->vb2_queue->mem_ops->get_dmabuf(vb, vb->planes[0].mem_priv, + O_RDWR); + if (IS_ERR_OR_NULL(dbuf)) { + dev_err_once(ipu->dev, + "cannot pin buffer %u under a running DMA\n", + vb->index); + return false; + } + return true; +} + +static void ipu_work(struct work_struct *work) +{ + struct ipu_dev *ipu = container_of(work, struct ipu_dev, work); + struct ipu_ctx *ctx = READ_ONCE(ipu->active_ctx); + struct vb2_v4l2_buffer *src, *dst; + enum vb2_buffer_state state = VB2_BUF_STATE_ERROR; + int ret; + + src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); + dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); + if (!src || !dst) + goto finish; + if (READ_ONCE(ipu->removed) || READ_ONCE(ipu->failed) || + atomic_read(&ctx->abort) || !ipu_geometry_ok(ctx)) + goto finish; + ret = clk_prepare_enable(ipu->clk); + if (ret) + goto finish; + ret = ipu_quiesce(ipu); + if (ret) { + WRITE_ONCE(ipu->failed, true); + goto disable_clock; + } + synchronize_irq(ipu->irq); + reinit_completion(&ipu->done); + WRITE_ONCE(ipu->irq_status, 0); + if (!atomic_read(&ctx->abort) && !READ_ONCE(ipu->removed)) { + ipu_program(ctx, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0), + vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0)); + ret = wait_for_completion_timeout(&ipu->done, + msecs_to_jiffies(2000)); + if (ret && !atomic_read(&ctx->abort) && + (READ_ONCE(ipu->irq_status) & (OUT_END | SIZE_ERR)) == OUT_END) + state = VB2_BUF_STATE_DONE; + else if (!ret) + dev_err(ipu->dev, "conversion timed out\n"); + } + ret = ipu_quiesce(ipu); + if (ret) { + bool pinned; + + WRITE_ONCE(ipu->failed, true); + ipu->quarantined = true; + pinned = ipu_pin_buffer(ipu, &src->vb2_buf) & + ipu_pin_buffer(ipu, &dst->vb2_buf); + state = VB2_BUF_STATE_ERROR; + dev_err(ipu->dev, + "DMA stop timed out; IPU disabled, buffers %s\n", + pinned ? "retained" : "may not be pinned"); + } +disable_clock: + synchronize_irq(ipu->irq); + clk_disable_unprepare(ipu->clk); +finish: + if (dst) + vb2_set_plane_payload(&dst->vb2_buf, 0, + state == VB2_BUF_STATE_DONE ? + ctx->dst.sizeimage : 0); + if (src && dst) { + v4l2_m2m_buf_copy_metadata(src, dst, true); + src->sequence = ctx->src_sequence++; + dst->sequence = ctx->dst_sequence++; + } + if (src) + v4l2_m2m_buf_done(src, state); + if (dst) + v4l2_m2m_buf_done(dst, state); + v4l2_m2m_job_finish(ipu->m2m, ctx->fh.m2m_ctx); +} + +static void ipu_device_run(void *priv) +{ + struct ipu_ctx *ctx = priv; + + WRITE_ONCE(ctx->ipu->active_ctx, ctx); + queue_work(ctx->ipu->workqueue, &ctx->ipu->work); +} + +static int ipu_job_ready(void *priv) +{ + struct ipu_ctx *ctx = priv; + + return !READ_ONCE(ctx->ipu->removed) && + !READ_ONCE(ctx->ipu->failed) && + !atomic_read(&ctx->abort); +} + +static void ipu_job_abort(void *priv) +{ + struct ipu_ctx *ctx = priv; + + atomic_set(&ctx->abort, 1); + complete(&ctx->ipu->done); + flush_workqueue(ctx->ipu->workqueue); +} + +static const struct v4l2_m2m_ops ipu_m2m_ops = { + .device_run = ipu_device_run, + .job_ready = ipu_job_ready, + .job_abort = ipu_job_abort, +}; + +static void ipu_destroy(struct kref *ref) +{ + struct ipu_dev *ipu = container_of(ref, struct ipu_dev, ref); + + destroy_workqueue(ipu->workqueue); + v4l2_m2m_release(ipu->m2m); + v4l2_device_unregister(&ipu->v4l2); + /* Pinned buffers still belong to the DMA device. */ + if (!ipu->quarantined) { + if (ipu->reserved_mem) + of_reserved_mem_device_release(&ipu->dma_pdev->dev); + platform_device_unregister(ipu->dma_pdev); + } + put_device(ipu->dev); + kfree(ipu); +} + +static int ipu_open(struct file *file) +{ + struct ipu_dev *ipu = video_drvdata(file); + struct ipu_ctx *ctx; + int ret; + + mutex_lock(&ipu->lock); + if (ipu->removed) { + ret = -ENODEV; + goto unlock; + } + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) { + ret = -ENOMEM; + goto unlock; + } + ctx->ipu = ipu; + atomic_set(&ctx->abort, 0); + ipu_pix_format(&ctx->src, V4L2_PIX_FMT_NV12, 640, 480); + ipu_pix_format(&ctx->dst, V4L2_PIX_FMT_NV21, 640, 480); + ctx->crop.left = 0; + ctx->crop.top = 0; + ctx->crop.width = 640; + ctx->crop.height = 480; + ctx->compose.left = 0; + ctx->compose.top = 0; + ctx->compose.width = 640; + ctx->compose.height = 480; + ctx->alpha = 255; + + v4l2_ctrl_handler_init(&ctx->ctrl_handler, 1); + v4l2_ctrl_new_std(&ctx->ctrl_handler, &ipu_ctrl_ops, + V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255); + if (ctx->ctrl_handler.error) { + ret = ctx->ctrl_handler.error; + v4l2_ctrl_handler_free(&ctx->ctrl_handler); + kfree(ctx); + goto unlock; + } + + v4l2_fh_init(&ctx->fh, ipu->video); + ctx->fh.ctrl_handler = &ctx->ctrl_handler; + file->private_data = &ctx->fh; + ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(ipu->m2m, ctx, ipu_queue_init); + if (IS_ERR(ctx->fh.m2m_ctx)) { + ret = PTR_ERR(ctx->fh.m2m_ctx); + v4l2_ctrl_handler_free(&ctx->ctrl_handler); + v4l2_fh_exit(&ctx->fh); + kfree(ctx); + goto unlock; + } + v4l2_ctrl_handler_setup(&ctx->ctrl_handler); + kref_get(&ipu->ref); + list_add_tail(&ctx->list, &ipu->contexts); + v4l2_fh_add(&ctx->fh); + ret = 0; +unlock: + mutex_unlock(&ipu->lock); + return ret; +} + +static int ipu_release(struct file *file) +{ + struct ipu_ctx *ctx = fh_to_ctx(file->private_data); + struct ipu_dev *ipu = ctx->ipu; + + mutex_lock(&ipu->lock); + atomic_set(&ctx->abort, 1); + flush_workqueue(ipu->workqueue); + v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); + v4l2_ctrl_handler_free(&ctx->ctrl_handler); + list_del(&ctx->list); + v4l2_fh_del(&ctx->fh); + v4l2_fh_exit(&ctx->fh); + mutex_unlock(&ipu->lock); + kfree(ctx); + kref_put(&ipu->ref, ipu_destroy); + return 0; +} + +static const struct v4l2_ioctl_ops ipu_ioctl_ops = { + .vidioc_querycap = ipu_querycap, + .vidioc_enum_fmt_vid_cap = ipu_enum_fmt, + .vidioc_enum_fmt_vid_out = ipu_enum_fmt, + .vidioc_g_fmt_vid_cap = ipu_get_fmt, + .vidioc_g_fmt_vid_out = ipu_get_fmt, + .vidioc_try_fmt_vid_cap = ipu_try_fmt, + .vidioc_try_fmt_vid_out = ipu_try_fmt, + .vidioc_s_fmt_vid_cap = ipu_set_fmt, + .vidioc_s_fmt_vid_out = ipu_set_fmt, + .vidioc_g_selection = ipu_g_selection, + .vidioc_s_selection = ipu_s_selection, + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_streamon = v4l2_m2m_ioctl_streamon, + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, +}; + +static const struct v4l2_file_operations ipu_fops = { + .owner = THIS_MODULE, + .open = ipu_open, + .release = ipu_release, + .unlocked_ioctl = video_ioctl2, + .poll = v4l2_m2m_fop_poll, + .mmap = v4l2_m2m_fop_mmap, +}; + +static void ipu_video_release(struct video_device *video) +{ + struct ipu_dev *ipu = video_get_drvdata(video); + + video_device_release(video); + kref_put(&ipu->ref, ipu_destroy); +} + +static int ipu_probe(struct platform_device *pdev) +{ + struct ipu_dev *ipu; + struct video_device *video; + int ret; + + ipu = kzalloc(sizeof(*ipu), GFP_KERNEL); + if (!ipu) + return -ENOMEM; + ipu->dev = get_device(&pdev->dev); + kref_init(&ipu->ref); + mutex_init(&ipu->lock); + INIT_LIST_HEAD(&ipu->contexts); + INIT_WORK(&ipu->work, ipu_work); + init_completion(&ipu->done); + ipu->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(ipu->regs)) { + ret = PTR_ERR(ipu->regs); + goto free; + } + ipu->clk = devm_clk_get(&pdev->dev, "ipu"); + if (IS_ERR(ipu->clk)) { + ret = PTR_ERR(ipu->clk); + dev_err(&pdev->dev, "failed to get ipu clock: %d\n", ret); + goto free; + } + ipu->irq = platform_get_irq(pdev, 0); + if (ipu->irq < 0) { + ret = ipu->irq; + dev_err(&pdev->dev, "failed to get IRQ: %d\n", ret); + goto free; + } + ipu->dma_pdev = platform_device_alloc("t23-ipu-dma", + PLATFORM_DEVID_AUTO); + if (!ipu->dma_pdev) { + ret = -ENOMEM; + goto free; + } + ipu->dma_pdev->dev.parent = &pdev->dev; + ipu->dma_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); + ipu->dma_pdev->dev.dma_mask = &ipu->dma_pdev->dev.coherent_dma_mask; + ret = platform_device_add(ipu->dma_pdev); + if (ret) { + platform_device_put(ipu->dma_pdev); + goto free; + } + ret = of_dma_configure(&ipu->dma_pdev->dev, pdev->dev.of_node, true); + if (ret) + goto unregister_dma; + ret = dma_set_mask_and_coherent(&ipu->dma_pdev->dev, DMA_BIT_MASK(32)); + if (ret) + goto unregister_dma; + vb2_dma_contig_set_max_seg_size(&ipu->dma_pdev->dev, DMA_BIT_MASK(32)); + ret = of_reserved_mem_device_init_by_idx(&ipu->dma_pdev->dev, + pdev->dev.of_node, 0); + if (!ret) + ipu->reserved_mem = true; + else if (ret != -ENODEV && ret != -EBUSY) + goto unregister_dma; + ret = clk_prepare_enable(ipu->clk); + if (ret) { + dev_err(&pdev->dev, "failed to enable clock: %d\n", ret); + goto release_mem; + } + ret = ipu_quiesce(ipu); + clk_disable_unprepare(ipu->clk); + if (ret) { + dev_err(&pdev->dev, "failed to quiesce IPU: %d\n", ret); + goto release_mem; + } + ret = devm_request_irq(&pdev->dev, ipu->irq, ipu_irq, 0, + dev_name(&pdev->dev), ipu); + if (ret) { + dev_err(&pdev->dev, "failed to request IRQ %d: %d\n", ipu->irq, ret); + goto release_mem; + } + ret = v4l2_device_register(&pdev->dev, &ipu->v4l2); + if (ret) + goto release_mem; + ipu->m2m = v4l2_m2m_init(&ipu_m2m_ops); + if (IS_ERR(ipu->m2m)) { + ret = PTR_ERR(ipu->m2m); + goto unregister_v4l2; + } + ipu->workqueue = alloc_ordered_workqueue("t23-ipu", WQ_MEM_RECLAIM); + if (!ipu->workqueue) { + ret = -ENOMEM; + goto release_m2m; + } + video = video_device_alloc(); + if (!video) { + ret = -ENOMEM; + goto destroy_workqueue; + } + ipu->video = video; + strscpy(video->name, "T23 IPU", sizeof(video->name)); + video->v4l2_dev = &ipu->v4l2; + video->fops = &ipu_fops; + video->ioctl_ops = &ipu_ioctl_ops; + video->lock = &ipu->lock; + video->release = ipu_video_release; + video->vfl_dir = VFL_DIR_M2M; + video->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; + video_set_drvdata(video, ipu); + kref_get(&ipu->ref); + ret = video_register_device(video, VFL_TYPE_VIDEO, -1); + if (ret) { + video_device_release(video); + kref_put(&ipu->ref, ipu_destroy); + goto destroy_workqueue; + } + + platform_set_drvdata(pdev, ipu); + return 0; +destroy_workqueue: + destroy_workqueue(ipu->workqueue); +release_m2m: + v4l2_m2m_release(ipu->m2m); +unregister_v4l2: + v4l2_device_unregister(&ipu->v4l2); +release_mem: + if (ipu->reserved_mem) + of_reserved_mem_device_release(&ipu->dma_pdev->dev); +unregister_dma: + platform_device_unregister(ipu->dma_pdev); +free: + put_device(ipu->dev); + kfree(ipu); + return ret; +} + +static void ipu_remove(struct platform_device *pdev) +{ + struct ipu_dev *ipu = platform_get_drvdata(pdev); + struct ipu_ctx *ctx; + + mutex_lock(&ipu->lock); + WRITE_ONCE(ipu->removed, true); + complete(&ipu->done); + flush_workqueue(ipu->workqueue); + list_for_each_entry(ctx, &ipu->contexts, list) { + v4l2_m2m_streamoff(NULL, ctx->fh.m2m_ctx, + V4L2_BUF_TYPE_VIDEO_OUTPUT); + v4l2_m2m_streamoff(NULL, ctx->fh.m2m_ctx, + V4L2_BUF_TYPE_VIDEO_CAPTURE); + vb2_queue_error(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)); + vb2_queue_error(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)); + } + v4l2_device_disconnect(&ipu->v4l2); + mutex_unlock(&ipu->lock); + + video_unregister_device(ipu->video); + kref_put(&ipu->ref, ipu_destroy); +} + +static const struct of_device_id ipu_of_match[] = { + { .compatible = "ingenic,t23-ipu" }, + { } +}; +MODULE_DEVICE_TABLE(of, ipu_of_match); + +static struct platform_driver ipu_driver = { + .probe = ipu_probe, + .remove = ipu_remove, + .driver = { + .name = "t23-ipu", + .of_match_table = ipu_of_match, + }, +}; +module_platform_driver(ipu_driver); + +MODULE_DESCRIPTION("Ingenic T23 IPU V4L2 mem2mem converter"); +MODULE_AUTHOR("Difan Zhang"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS(DMA_BUF); diff --git a/drivers/media/platform/ingenic/ipu/jz_regs_v13.h b/drivers/media/platform/ingenic/ipu/jz_regs_v13.h new file mode 100644 index 0000000000..332fa33ce3 --- /dev/null +++ b/drivers/media/platform/ingenic/ipu/jz_regs_v13.h @@ -0,0 +1,249 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Ingenic T23 IPU v13 register definitions + * Copyright (c) 2015 Ingenic Semiconductor Co., Ltd. + */ + +#ifndef _JZ_REGS_V13_H_ +#define _JZ_REGS_V13_H_ + +#include +#include + +#ifdef __KERNEL__ + +/* Module for CLKGR */ +#define IPU_CLOCK BIT(13) + +/* Register offset */ +#define IPU_FM_CTRL 0x00 +#define IPU_STATUS 0x04 +#define IPU_D_FMT 0x08 +#define IPU_Y_ADDR 0x0c +#define IPU_U_ADDR 0x10 +#define IPU_IN_FM_GS 0x18 +#define IPU_Y_STRIDE 0x1c +#define IPU_UV_STRIDE 0x20 + +#define IPU_OUT_ADDR 0x24 +#define IPU_OUT_STRIDE 0x2c +#define IPU_OUT_V_ADDR 0x30 +#define IPU_OUT_V_STRIDE 0x34 + +#define IPU_REG_CTRL 0x64 +#define IPU_TRIGGER 0x74 +#define IPU_GLB_CTRL 0x7c + +#define IPU_NV_OUT_ADDR 0xb4 +#define IPU_NV_OUT_STRIDE 0xb8 +#define IPU_OSD_IN_CH0_Y_ADDR 0xbc +#define IPU_OSD_IN_CH0_Y_STRIDE 0xc0 +#define IPU_OSD_IN_CH0_UV_ADDR 0xc4 +#define IPU_OSD_IN_CH0_UV_STRIDE 0xc8 +#define IPU_OSD_CH0_GS 0xfc +#define IPU_OSD_CH0_POS 0x10c +#define IPU_OSD_CH0_PARA 0x11c +#define IPU_OSD_CH1_PARA 0x120 +#define IPU_OSD_CH2_PARA 0x124 +#define IPU_OSD_CH3_PARA 0x128 +#define IPU_OSD_CH_BK_PARA 0x12c + +#define IPU_OSD_CH0_BAK_ARGB 0x130 + +#define IPU_CH0_CSC_C0_COEF 0x148 +#define IPU_CH0_CSC_C1_COEF 0x14c +#define IPU_CH0_CSC_C2_COEF 0x150 +#define IPU_CH0_CSC_C3_COEF 0x154 +#define IPU_CH0_CSC_C4_COEF 0x158 +#define IPU_CH0_CSC_OFSET_PARA 0x15c + +#define IPU_RD_ARB_CTL 0x1a8 +#define IPU_CLK_NUM_ONE_FRA 0x1ac +#define IPU_OSD_LAY_PADDING_ARGB 0x1b0 +#define IPU_TEST_1B4 0x1b4 + +/* SET CH_BK_PARA Register */ +#define CH_BK_EN BIT(0) +#define CH_BK_PIXEL_ALPHA (0 << 2) /* default */ +#define CH_BK_GLOBAL_ALPHA BIT(1) +#define CH_BK_PALPHA_X_GALPHA BIT(2) +#define CH_BK_GLO_A(val) ((val) << 3) + +#define CH_BK_PIC_TYPE 11 +#define CH_BK_PIC_TYPE_ARGB (0 << CH_BK_PIC_TYPE) +#define CH_BK_PIC_TYPE_RGB (1 << CH_BK_PIC_TYPE) +#define CH_BK_PIC_TYPE_NV12 (2 << CH_BK_PIC_TYPE) +#define CH_BK_PIC_TYPE_NV21 (3 << CH_BK_PIC_TYPE) + +#define CH_BK_ARGB_TYPE 14 +#define CH_BK_ARGB_TYPE_ARGB (0x0 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_ARBG (0x1 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_AGRB (0x2 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_AGBR (0x3 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_ABRG (0x4 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_ABGR (0x5 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_RGBA (0x8 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_RBGA (0x9 << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_GRBA (0xa << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_GBRA (0xb << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_BRGA (0xc << CH_BK_ARGB_TYPE) +#define CH_BK_ARGB_TYPE_BGRA (0xd << CH_BK_ARGB_TYPE) + +#define CH_BK_PREM BIT(18) +#define CH_BK_MASK_EN BIT(19) +#define CH_BK_WHOLE_PIC_EN BIT(20) + +/* OSD Parameters */ +#define IPU_OSD_CH0_PARA_DEFAULT 0x010400fa /* stock default: BIT(24) | CH_BK_PREM | CH_BK_GLO_A(0x1f) | CH_BK_GLOBAL_ALPHA */ +#define IPU_OSD_CH_BK_PARA_DEFAULT (CH_BK_WHOLE_PIC_EN | CH_BK_PREM | CH_BK_PIC_TYPE_NV12) /* 0x141000 */ + +/* OSD_CHN_EN */ +#define CHOOSE_OSD_CHN_RESIZE BIT(5) + +/* IPU_GLB_CTRL Register */ +#define IRQ_EN BIT(0) + +/* IPU_OSD_CTRL Register */ +#define OSD_PM BIT(2) + +/* IPU_TRIG Register */ +#define IPU_RUN BIT(0) +#define IPU_STOP BIT(1) +#define IPU_RESET BIT(3) + +/* IPU_D_FMT Register */ +#define BLK_SEL BIT(4) +#define RGB_POS BIT(5) +#define AL_PIX_EN BIT(6) + +#define IPU_D_FMT_IN_YUV420 0x01 +#define IPU_D_FMT_OUT_ARGB8888 0x00 +#define IPU_D_FMT_OUT_NV21 0x03 +#define IPU_D_FMT_OUT_ABGR8888 0x28 +#define IPU_D_FMT_OUT_RGBA8888 0x40 +#define IPU_D_FMT_OUT_BGRA8888 0x68 + +/* IPU_STATUS bitfields */ +#define OUT_END BIT(0) +#define SIZE_ERR BIT(2) +#define ID BIT(16) + +#define MSTATUS_SFT 4 +#define MSTATUS_MSK 3 +#define MSTATUS_IPU_FREE (0 << MSTATUS_SFT) +#define MSTATUS_IPU_RUNNING (1 << MSTATUS_SFT) +#define MSTATUS (2 << MSTATUS_SFT) + +/* REG_IPU_ADDR_CTRL */ +#define Y_RY BIT(0) +#define U_RY BIT(1) +#define V_RY BIT(2) +#define D_RY BIT(3) +#define PTS_RY BIT(4) +#define CTRL_RY BIT(6) +#define DF_RY BIT(7) +#define NV_D_RY BIT(17) +#define NV_STR_RY BIT(20) +#define OSD_CH0_RY BIT(21) +#define OSD_CH1_RY BIT(22) +#define OSD_CH2_RY BIT(23) +#define OSD_CH3_RY BIT(24) +#define RD_ARB_RY BIT(26) +#define OSD_BK_RY BIT(27) + +/* IPU_OSD_CTRL bitfields */ +#define GLB_ALPHA(val) ((val) << 8) +#define MOD_OSD(val) ((val) << 0) + +/* Frame XY offset bitfields */ +#define SCREEN_YOFT(val) ((val) << 16) +#define SCREEN_XOFT(val) ((val) << 0) + +/* Input geometry bitfields */ +#define IN_FM_W(val) ((val) << 16) +#define IN_FM_H(val) ((val) << 0) + +/* Output geometry bitfields */ +#define OUT_FM_W(val) ((val) << 16) +#define OUT_FM_H(val) ((val) << 0) + +/* UV stride bitfields */ +#define U_STRIDE(val) ((val) << 16) +#define V_STRIDE(val) ((val) << 0) + +/* BT.601 Color Space Conversion Matrix */ +#define IPU_CSC_C0_BT601 0x4ad +#define IPU_CSC_C1_BT601 (0x669 | (0x4ad << 20)) +#define IPU_CSC_C2_BT601 (0x193 | (0x344 << 20)) +#define IPU_CSC_C3_BT601 (0x4ad | (0x81a << 20)) +#define IPU_CSC_C4_BT601 0x00 +#define IPU_CSC_OFSET_BT601 (0x10 | (0x80 << 16)) + +/* Legacy matrix aliases */ +#define YUV_CSC_C0 IPU_CSC_C0_BT601 +#define YUV_CSC_C1 IPU_CSC_C1_BT601 +#define YUV_CSC_C2 IPU_CSC_C2_BT601 +#define YUV_CSC_C3 IPU_CSC_C3_BT601 +#define YUV_CSC_C4 IPU_CSC_C4_BT601 +#define YUV_CSC_OFFSET_PARA IPU_CSC_OFSET_BT601 + +/* Channel select */ +#define BK_OSD_PIC_MASK 18 +#define BK_OSD_PIC_NV21 (1 << BK_OSD_PIC_MASK) +#define BK_OSD_PIC_NV12 (2 << BK_OSD_PIC_MASK) +#define BK_OSD_PIC_ARGB (3 << BK_OSD_PIC_MASK) +#define CH3_OSD_PIC_MASK 15 +#define CH3_OSD_PIC_NV21 (1 << CH3_OSD_PIC_MASK) +#define CH3_OSD_PIC_NV12 (2 << CH3_OSD_PIC_MASK) +#define CH3_OSD_PIC_ARGB (3 << CH3_OSD_PIC_MASK) +#define CH2_OSD_PIC_MASK 12 +#define CH2_OSD_PIC_NV21 (1 << CH2_OSD_PIC_MASK) +#define CH2_OSD_PIC_NV12 (2 << CH2_OSD_PIC_MASK) +#define CH2_OSD_PIC_ARGB (3 << CH2_OSD_PIC_MASK) +#define CH1_OSD_PIC_MASK 9 +#define CH1_OSD_PIC_NV21 (1 << CH1_OSD_PIC_MASK) +#define CH1_OSD_PIC_NV12 (2 << CH1_OSD_PIC_MASK) +#define CH1_OSD_PIC_ARGB (3 << CH1_OSD_PIC_MASK) +#define CH0_OSD_PIC_MASK 6 +#define CH0_OSD_PIC_NV21 (1 << CH0_OSD_PIC_MASK) +#define CH0_OSD_PIC_NV12 (2 << CH0_OSD_PIC_MASK) +#define CH0_OSD_PIC_ARGB (3 << CH0_OSD_PIC_MASK) +#define OSD_CHN_EN_MASK 0 +#define OSD_CHN_EN_CH0 (1 << OSD_CHN_EN_MASK) +#define OSD_CHN_EN_CH1 (2 << OSD_CHN_EN_MASK) +#define OSD_CHN_EN_CH2 (4 << OSD_CHN_EN_MASK) +#define OSD_CHN_EN_CH3 (8 << OSD_CHN_EN_MASK) +#define OSD_CHN_EN_BKG (16 << OSD_CHN_EN_MASK) +#define OSD_CHN_EN_DST (32 << OSD_CHN_EN_MASK) + +#endif /* #ifdef __KERNEL__ */ + +/* Data Format Register */ +#define OUT_FMT_RGB555 0 +#define OUT_FMT_RGB565 1 +#define OUT_FMT_RGB888 2 +#define OUT_FMT_YUV422 3 +#define OUT_FMT_RGBAAA 4 +#define OUT_FMT_NV12 5 +#define OUT_FMT_NV21 6 +#define OUT_FMT_HSV 7 +#define OUT_FMT_ARGB_8888 8 +#define OUT_FMT_ABGR_8888 9 +#define OUT_FMT_RGBA_8888 0xa +#define OUT_FMT_BGRA_8888 0xb + +#define IN_FMT_BIT 0 +#define IN_FMT_YUV420 1 +#define IN_FMT_YUV420_B 2 +#define IN_FMT_YUV422 3 +#define IN_FMT_YUV444 4 +#define IN_FMT_RGB_555 5 +#define IN_FMT_RGB_888 6 +#define IN_FMT_RGB_565 7 +#define IN_FMT_YUV411 8 + +#define NV_SEL BIT(7) +#define NV_MODE_NV12 (0 << 8) +#define NV_MODE_NV21 BIT(8) + +#endif /* _JZ_REGS_V13_H_ */ From e14a182aff6109eec365fa6b229992e2b2743598 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 08:41:57 -0800 Subject: [PATCH 70/78] MIPS: dts: ingenic: Add the T23 SoC description Describe the T23 CPU, clocks, interrupts, memory-mapped peripherals and media blocks with board-specific devices disabled by default. Signed-off-by: Difan Zhang --- arch/mips/boot/dts/ingenic/t23.dtsi | 617 ++++++++++++++++++++++++++++ 1 file changed, 617 insertions(+) create mode 100644 arch/mips/boot/dts/ingenic/t23.dtsi diff --git a/arch/mips/boot/dts/ingenic/t23.dtsi b/arch/mips/boot/dts/ingenic/t23.dtsi new file mode 100644 index 0000000000..75c610ce43 --- /dev/null +++ b/arch/mips/boot/dts/ingenic/t23.dtsi @@ -0,0 +1,617 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +#include +#include +#include + +/ { + #address-cells = <1>; + #size-cells = <1>; + compatible = "ingenic,t23"; + + cpus { + #address-cells = <1>; + #size-cells = <0>; + + cpu0: cpu@0 { + device_type = "cpu"; + compatible = "ingenic,xburst-fpu2.0-mxu2.0"; + reg = <0>; + clocks = <&cgu CLK_CCLK>; + }; + }; + + cpuintc: interrupt-controller { + #address-cells = <0>; + #interrupt-cells = <1>; + interrupt-controller; + compatible = "mti,cpu-interrupt-controller"; + }; + + ext: ext { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = <24000000>; + clock-output-names = "ext"; + }; + + rtc_clk: rtc-clk { + compatible = "fixed-clock"; + #clock-cells = <0>; + clock-frequency = <32768>; + clock-output-names = "rtc"; + }; + + aic_loop: aic-loop { + compatible = "ingenic,t23-aic-loop"; + ingenic,aic = <&aic>; + #sound-dai-cells = <0>; + dmas = <&pdma 5 0xffffffff>; + dma-names = "rx"; + status = "disabled"; + }; + + soc: soc { + compatible = "simple-bus"; + #address-cells = <1>; + #size-cells = <1>; + ranges; + + cgu: clock-controller@10000000 { + compatible = "ingenic,t23-cgu", "syscon", "simple-mfd"; + reg = <0x10000000 0x100>; + #address-cells = <1>; + #size-cells = <1>; + ranges = <0x0 0x10000000 0x100>; + #clock-cells = <1>; + clocks = <&ext>, <&rtc_clk>; + clock-names = "ext", "rtc"; + + cpm_reset: reset-controller { + compatible = "ingenic,t23-reset"; + #reset-cells = <1>; + }; + + mac_phy_ctrl: mac-phy-ctrl@e8 { + compatible = "ingenic,t23-mac-phy-ctrl", "syscon"; + reg = <0xe8 0x4>; + }; + }; + + intc: interrupt-controller@10001000 { + compatible = "ingenic,t23-intc", "ingenic,jz4780-intc"; + reg = <0x10001000 0x40>; + interrupt-controller; + #interrupt-cells = <1>; + interrupt-parent = <&cpuintc>; + interrupts = <2>; + }; + + tcu: timer@10002000 { + compatible = "ingenic,t23-tcu", "ingenic,x1000-tcu", + "syscon", "simple-mfd"; + reg = <0x10002000 0x1000>; + #address-cells = <1>; + #size-cells = <1>; + ranges = <0x0 0x10002000 0x1000>; + #clock-cells = <1>; + clocks = <&rtc_clk>, <&ext>, + <&cgu CLK_PCLK>, <&cgu CLK_TCU>; + clock-names = "rtc", "ext", "pclk", "tcu"; + interrupt-controller; + #interrupt-cells = <1>; + interrupt-parent = <&intc>; + interrupts = <27 26 25>; + ingenic,pwm-channels-mask = <0x0f>; + status = "disabled"; + + watchdog: watchdog@0 { + compatible = "ingenic,t23-watchdog", + "ingenic,jz4740-watchdog"; + reg = <0x0 0x10>; + clocks = <&tcu TCU_CLK_WDT>; + clock-names = "wdt"; + status = "disabled"; + }; + + pwm: pwm@40 { + compatible = "ingenic,t23-pwm", + "ingenic,x1000-pwm"; + reg = <0x40 0x80>; + #pwm-cells = <3>; + clocks = <&tcu TCU_CLK_TIMER0>, + <&tcu TCU_CLK_TIMER1>, + <&tcu TCU_CLK_TIMER2>, + <&tcu TCU_CLK_TIMER3>; + clock-names = "timer0", "timer1", + "timer2", "timer3"; + status = "disabled"; + }; + }; + + rtc: rtc@10003000 { + compatible = "ingenic,t23-rtc", "ingenic,jz4780-rtc"; + reg = <0x10003000 0x4c>; + interrupt-parent = <&intc>; + interrupts = <32>; + clocks = <&cgu CLK_RTC>; + clock-names = "rtc"; + status = "disabled"; + }; + + pinctrl: pinctrl@10010000 { + compatible = "ingenic,t23-pinctrl"; + reg = <0x10010000 0x9000>; + #address-cells = <1>; + #size-cells = <0>; + status = "disabled"; + + gpa: gpio@0 { + compatible = "ingenic,t23-gpio"; + reg = <0>; + gpio-controller; + gpio-ranges = <&pinctrl 0 0 32>; + #gpio-cells = <2>; + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&intc>; + interrupts = <17>; + }; + + gpb: gpio@1 { + compatible = "ingenic,t23-gpio"; + reg = <1>; + gpio-controller; + gpio-ranges = <&pinctrl 0 32 32>; + #gpio-cells = <2>; + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&intc>; + interrupts = <16>; + }; + + gpc: gpio@2 { + compatible = "ingenic,t23-gpio"; + reg = <2>; + gpio-controller; + gpio-ranges = <&pinctrl 0 64 32>; + #gpio-cells = <2>; + interrupt-controller; + #interrupt-cells = <2>; + interrupt-parent = <&intc>; + interrupts = <15>; + }; + + pins_uart0_b: uart0-data-b { + function = "uart0"; + groups = "uart0-data"; + bias-disable; + }; + + pins_uart1_a: uart1-data-a { + function = "uart1"; + groups = "uart1-data-a"; + bias-disable; + }; + + pins_uart1_b: uart1-data-b { + function = "uart1"; + groups = "uart1-data-b"; + bias-disable; + }; + + pins_uart2_b: uart2-data-b { + function = "uart2"; + groups = "uart2-data-b"; + bias-disable; + }; + + pins_uart2_c: uart2-data-c { + function = "uart2"; + groups = "uart2-data-c"; + bias-disable; + }; + + pins_sfc: sfc-data { + function = "sfc"; + groups = "sfc-data"; + bias-disable; + }; + + pins_sfc_quad: sfc-quad { + function = "sfc"; + groups = "sfc-data", "sfc-quad"; + bias-disable; + }; + + pins_mmc0: mmc0 { + function = "mmc0"; + groups = "mmc0-1bit", "mmc0-4bit"; + bias-disable; + }; + + pins_mmc1_c: mmc1-c { + function = "mmc1"; + groups = "mmc1-1bit-c", "mmc1-4bit-c"; + bias-disable; + }; + + pins_i2c0: i2c0-data { + function = "i2c0"; + groups = "i2c0-data"; + bias-disable; + }; + + pins_cim_mclk: cim-mclk { + function = "cim"; + groups = "cim-mclk"; + bias-disable; + }; + + pins_i2c1_a: i2c1-data-a { + function = "i2c1"; + groups = "i2c1-data-a"; + bias-disable; + }; + + pins_i2c1_b: i2c1-data-b { + function = "i2c1"; + groups = "i2c1-data-b"; + bias-disable; + }; + + pins_i2c1_c: i2c1-data-c { + function = "i2c1"; + groups = "i2c1-data-c"; + bias-disable; + }; + + pins_i2c2_b: i2c2-data-b { + function = "i2c2"; + groups = "i2c2-data-b"; + bias-disable; + }; + + pins_ssi0_b: ssi0-data-b { + function = "ssi0"; + groups = "ssi0-data-b"; + bias-disable; + }; + + pins_mac: mac-rmii { + function = "mac"; + groups = "mac-rmii"; + bias-disable; + }; + + pins_vic_switch: vic-switch { + function = "vic"; + groups = "vic-switch"; + bias-disable; + }; + }; + + aic: audio-controller@10020000 { + compatible = "ingenic,t23-aic", "ingenic,jz4780-i2s"; + reg = <0x10020000 0x70>; + #sound-dai-cells = <0>; + interrupt-parent = <&intc>; + interrupts = <1>; + clocks = <&cgu CLK_CGU_I2S_SPK>, <&cgu CLK_CGU_I2S_MIC>, + <&cgu CLK_AIC>; + clock-names = "cgu_i2s_spk", "cgu_i2s_mic", "aic"; + dmas = <&pdma 7 0xffffffff>, <&pdma 6 0xffffffff>; + dma-names = "rx", "tx"; + status = "disabled"; + }; + + codec: audio-codec@10021000 { + compatible = "ingenic,t23-codec"; + reg = <0x10021000 0x140>; + #sound-dai-cells = <1>; + status = "disabled"; + }; + + uart0: serial@10030000 { + compatible = "ingenic,t23-uart", "ingenic,x1000-uart"; + reg = <0x10030000 0x100>; + interrupt-parent = <&intc>; + interrupts = <51>; + clocks = <&ext>, <&cgu CLK_UART0>; + clock-names = "baud", "module"; + status = "disabled"; + }; + + uart1: serial@10031000 { + compatible = "ingenic,t23-uart", "ingenic,x1000-uart"; + reg = <0x10031000 0x100>; + interrupt-parent = <&intc>; + interrupts = <50>; + clocks = <&ext>, <&cgu CLK_UART1>; + clock-names = "baud", "module"; + status = "disabled"; + }; + + uart2: serial@10032000 { + compatible = "ingenic,t23-uart", "ingenic,x1000-uart"; + reg = <0x10032000 0x100>; + interrupt-parent = <&intc>; + interrupts = <49>; + clocks = <&ext>, <&cgu CLK_UART2>; + clock-names = "baud", "module"; + status = "disabled"; + }; + + ssi0: spi@10043000 { + compatible = "ingenic,t23-spi", "ingenic,x1000-spi"; + reg = <0x10043000 0x20>; + #address-cells = <1>; + #size-cells = <0>; + interrupt-parent = <&intc>; + interrupts = <9>; + clocks = <&cgu CLK_SSI0>; + clock-names = "spi"; + status = "disabled"; + }; + + i2c0: i2c@10050000 { + compatible = "ingenic,t23-i2c", "ingenic,x1000-i2c"; + reg = <0x10050000 0x1000>; + #address-cells = <1>; + #size-cells = <0>; + interrupt-parent = <&intc>; + interrupts = <60>; + clocks = <&cgu CLK_I2C0>; + clock-names = "i2c"; + status = "disabled"; + }; + + i2c1: i2c@10051000 { + compatible = "ingenic,t23-i2c", "ingenic,x1000-i2c"; + reg = <0x10051000 0x1000>; + #address-cells = <1>; + #size-cells = <0>; + interrupt-parent = <&intc>; + interrupts = <59>; + clocks = <&cgu CLK_I2C1>; + clock-names = "i2c"; + status = "disabled"; + }; + + i2c2: i2c@10052000 { + compatible = "ingenic,t23-i2c", "ingenic,x1000-i2c"; + reg = <0x10052000 0x1000>; + #address-cells = <1>; + #size-cells = <0>; + interrupt-parent = <&intc>; + interrupts = <58>; + clocks = <&cgu CLK_I2C2>; + clock-names = "i2c"; + status = "disabled"; + }; + + sadc: adc@10070000 { + compatible = "ingenic,t23-sadc"; + reg = <0x10070000 0x34>; + #io-channel-cells = <1>; + interrupt-parent = <&intc>; + interrupts = <18>; + clocks = <&cgu CLK_SADC>; + clock-names = "sadc"; + status = "disabled"; + }; + + dtrng: rng@10072000 { + compatible = "ingenic,t23-dtrng", "ingenic,x1830-dtrng"; + reg = <0x10072000 0x10>; + interrupt-parent = <&intc>; + interrupts = <34>; + clocks = <&cgu CLK_DTRNG>; + status = "disabled"; + }; + + ost: timer@12000000 { + compatible = "ingenic,t23-ost", "ingenic,x1000-ost"; + reg = <0x12000000 0x3c>; + #clock-cells = <1>; + interrupt-parent = <&cpuintc>; + interrupts = <4>; + clocks = <&cgu CLK_SYS_OST>; + clock-names = "ost"; + status = "disabled"; + }; + + ipu: ipu@13080000 { + compatible = "ingenic,t23-ipu"; + reg = <0x13080000 0x8000>; + interrupt-parent = <&intc>; + interrupts = <6>; + clocks = <&cgu CLK_IPU>; + clock-names = "ipu"; + status = "disabled"; + }; + + helix: video-codec@13100000 { + compatible = "ingenic,t23-helix"; + reg = <0x13100000 0x100000>; + interrupt-parent = <&intc>; + interrupts = <40>; + clocks = <&cgu CLK_CGU_VPU>, <&cgu CLK_AVPU>, + <&cgu CLK_IVDC>; + clock-names = "vpu", "gate", "ivdc"; + resets = <&cpm_reset T23_RESET_HELIX>; + status = "disabled"; + }; + + coproc: coprocessor@132a0000 { + compatible = "ingenic,t23-coprocessor", + "ingenic,t23-riscv"; + reg = <0x132a0000 0x1000>, + <0x132c0000 0x4000>; + reg-names = "aux", "tcsm"; + clocks = <&cgu CLK_RISCV>; + clock-names = "coproc"; + ingenic,cpm = <&cgu>; + interrupt-parent = <&intc>; + interrupts = <4>; + interrupt-names = "doorbell"; + firmware-name = "ingenic/t23_riscv.elf"; + status = "disabled"; + }; + + isp: isp@13300000 { + compatible = "ingenic,t23-isp"; + reg = <0x13300000 0x90000>, + <0x133e0000 0x10000>, + <0x10023000 0x1000>, + <0x10022000 0x1000>, + <0x13200000 0x10000>; + reg-names = "core", "vic", "csi", "phy", "ivdc"; + interrupt-parent = <&intc>; + interrupts = <29>, <30>, <38>; + interrupt-names = "core", "vic", "ivdc"; + clocks = <&cgu CLK_CGU_ISP>, <&cgu CLK_ISP>, + <&cgu CLK_CSI>, <&cgu CLK_CGU_VPU>, + <&cgu CLK_IVDC>; + clock-names = "cgu_isp", "isp", "csi", "cgu_vpu", + "ivdc"; + resets = <&cpm_reset T23_RESET_ISP>; + reset-names = "isp"; + status = "disabled"; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + }; + + port@1 { + reg = <1>; + }; + }; + }; + + pdma: dma-controller@13420000 { + compatible = "ingenic,t23-dma", "ingenic,x1830-dma"; + reg = <0x13420000 0x400>, <0x13421000 0x40>; + #dma-cells = <2>; + interrupt-parent = <&intc>; + interrupts = <10>; + clocks = <&cgu CLK_PDMA>; + status = "disabled"; + }; + + aes: crypto@13430000 { + compatible = "ingenic,t23-aes"; + reg = <0x13430000 0x1000>; + interrupt-parent = <&intc>; + interrupts = <23>; + clocks = <&cgu CLK_AES>; + clock-names = "aes"; + status = "disabled"; + }; + + sfc: spi@13440000 { + compatible = "ingenic,t23-sfc"; + reg = <0x13440000 0x1000>; + #address-cells = <1>; + #size-cells = <0>; + interrupt-parent = <&intc>; + interrupts = <7>; + clocks = <&cgu CLK_CGU_SFC>, <&cgu CLK_SFC>; + clock-names = "sfc", "gate"; + status = "disabled"; + }; + + msc0: mmc@13450000 { + compatible = "ingenic,t23-mmc", "ingenic,x1000-mmc"; + reg = <0x13450000 0x1000>; + dmas = <&pdma 0x1a 0xffffffff>, <&pdma 0x1b 0xffffffff>; + dma-names = "tx", "rx"; + interrupt-parent = <&intc>; + interrupts = <37>; + clocks = <&cgu CLK_CGU_MSC0>, <&cgu CLK_MSC0>; + clock-names = "mmc", "gate"; + cap-sd-highspeed; + cap-mmc-highspeed; + status = "disabled"; + }; + + msc1: mmc@13460000 { + compatible = "ingenic,t23-mmc", "ingenic,x1000-mmc"; + reg = <0x13460000 0x1000>; + dmas = <&pdma 0x1c 0xffffffff>, <&pdma 0x1d 0xffffffff>; + dma-names = "tx", "rx"; + interrupt-parent = <&intc>; + interrupts = <36>; + clocks = <&cgu CLK_CGU_MSC1>, <&cgu CLK_MSC1>; + clock-names = "mmc", "gate"; + cap-sd-highspeed; + cap-mmc-highspeed; + status = "disabled"; + }; + + mac: ethernet@134b0000 { + compatible = "ingenic,t23-mac", "snps,dwmac"; + reg = <0x134b0000 0x2000>; + interrupt-parent = <&intc>; + interrupts = <55>; + interrupt-names = "macirq"; + clocks = <&cgu CLK_GMAC>, <&cgu CLK_CGU_MACPHY>; + clock-names = "stmmaceth", "macphy"; + mode-reg = <&mac_phy_ctrl>; + pinctrl-names = "default"; + pinctrl-0 = <&pins_mac>; + phy-mode = "rmii"; + status = "disabled"; + + mdio: mdio { + compatible = "snps,dwmac-mdio"; + #address-cells = <1>; + #size-cells = <0>; + status = "disabled"; + }; + }; + + ddr: memory-controller@134f0000 { + compatible = "ingenic,t23-ddr"; + reg = <0x134f0000 0x400>, + <0x13011000 0x100>, + <0x13012000 0x100>; + reg-names = "controller", "phy", "apb"; + }; + + otg_phy: usb-phy@1000003c { + compatible = "ingenic,t23-phy"; + reg = <0x1000003c 0x10>, <0x10060000 0x1000>; + reg-names = "cpm", "tuning"; + ingenic,cpm = <&cgu>; + clocks = <&cgu CLK_OTG>; + clock-names = "otg"; + #phy-cells = <0>; + status = "disabled"; + }; + + otg: usb@13500000 { + compatible = "ingenic,t23-otg", "snps,dwc2"; + reg = <0x13500000 0x40000>; + interrupt-parent = <&intc>; + interrupts = <21>; + clocks = <&cgu CLK_OTG>; + clock-names = "otg"; + phys = <&otg_phy>; + phy-names = "usb2-phy"; + dr_mode = "host"; + status = "disabled"; + }; + + efuse: efuse@13540200 { + compatible = "ingenic,t23-efuse"; + reg = <0x13540200 0x40>; + clocks = <&cgu CLK_EFUSE>; + clock-names = "efuse"; + }; + }; +}; From cd45f21b45d9877513a9ec1748b9865f519688b1 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 08:41:57 -0800 Subject: [PATCH 71/78] MIPS: dts: ingenic: Add the Jooan JA-A12 T23N camera Describe the JA-A12 64 MiB board, 24 MiB reusable camera CMA, SPI NOR layout, dual SC1A4T endpoints, USB Wi-Fi power and audio routes. Name the IR-cut GPIO lines for userspace ownership. Signed-off-by: Difan Zhang --- arch/mips/boot/dts/ingenic/Makefile | 2 + arch/mips/boot/dts/ingenic/ja-a12.dts | 350 ++++++++++++++++++++++++++ 2 files changed, 352 insertions(+) create mode 100644 arch/mips/boot/dts/ingenic/ja-a12.dts diff --git a/arch/mips/boot/dts/ingenic/Makefile b/arch/mips/boot/dts/ingenic/Makefile index 54aa0c4e60..8504da0896 100644 --- a/arch/mips/boot/dts/ingenic/Makefile +++ b/arch/mips/boot/dts/ingenic/Makefile @@ -6,4 +6,6 @@ dtb-$(CONFIG_JZ4780_CI20) += ci20.dtb dtb-$(CONFIG_X1000_CU1000_NEO) += cu1000-neo.dtb dtb-$(CONFIG_X1830_CU1830_NEO) += cu1830-neo.dtb +dtb-$(CONFIG_MACH_T23) += ja-a12.dtb + obj-$(CONFIG_BUILTIN_DTB) += $(addsuffix .o, $(dtb-y)) diff --git a/arch/mips/boot/dts/ingenic/ja-a12.dts b/arch/mips/boot/dts/ingenic/ja-a12.dts new file mode 100644 index 0000000000..81af4109e5 --- /dev/null +++ b/arch/mips/boot/dts/ingenic/ja-a12.dts @@ -0,0 +1,350 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/dts-v1/; + +#include "t23.dtsi" +#include +#include + +/ { + model = "Jooan JA-A12 (Ingenic T23N)"; + compatible = "jooan,ja-a12", "ingenic,t23"; + + aliases { + serial0 = &uart0; + serial1 = &uart1; + serial2 = &uart2; + }; + + chosen { + bootargs = "earlycon console=ttyS1,115200n8 loglevel=7"; + stdout-path = "serial1:115200n8"; + }; + + memory@0 { + device_type = "memory"; + reg = <0x00000000 0x04000000>; + }; + + reserved-memory { + #address-cells = <1>; + #size-cells = <1>; + ranges; + + camera_reserved: camera@2800000 { + compatible = "shared-dma-pool"; + reg = <0x02800000 0x01800000>; + reusable; + linux,cma-default; + }; + }; + + illumination { + compatible = "gpio-leds"; + + led-white { + label = "white:illumination"; + gpios = <&gpb 28 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + + led-infrared { + label = "ir:illumination"; + gpios = <&gpb 27 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + }; + + keys { + compatible = "gpio-keys"; + + key-reset { + label = "Reset"; + gpios = <&gpb 30 GPIO_ACTIVE_LOW>; + linux,code = ; + }; + }; + + wifi_pwr: wifi-regulator { + compatible = "regulator-fixed"; + regulator-name = "wifi_power"; + gpio = <&gpa 6 GPIO_ACTIVE_HIGH>; + enable-active-high; + regulator-boot-on; + regulator-always-on; + }; + + speaker_amp: speaker-amplifier { + compatible = "simple-audio-amplifier"; + enable-gpios = <&gpb 31 GPIO_ACTIVE_LOW>; + sound-name-prefix = "Speaker Amp"; + }; + + sound { + compatible = "simple-audio-card"; + simple-audio-card,name = "ja-a12-audio"; + #address-cells = <1>; + #size-cells = <0>; + + simple-audio-card,widgets = + "Speaker", "Speaker", + "Microphone", "Mic"; + simple-audio-card,routing = + "Speaker Amp INL", "LOUT", + "Speaker Amp INR", "ROUT", + "Speaker", "Speaker Amp OUTL", + "Speaker", "Speaker Amp OUTR", + "MICIN", "Mic"; + + simple-audio-card,aux-devs = <&speaker_amp>; + + simple-audio-card,dai-link@0 { + reg = <0>; + format = "i2s"; + bitclock-master = <&sound_codec>; + frame-master = <&sound_codec>; + + cpu { + sound-dai = <&aic>; + }; + + sound_codec: codec { + sound-dai = <&codec 0>; + }; + }; + + simple-audio-card,dai-link@1 { + reg = <1>; + + cpu { + sound-dai = <&aic_loop>; + }; + + codec { + sound-dai = <&codec 1>; + }; + }; + }; + + motor { + compatible = "ingenic,t23-motor"; + pan-gpios = <&gpb 22 GPIO_ACTIVE_HIGH>, + <&gpb 21 GPIO_ACTIVE_HIGH>, + <&gpb 20 GPIO_ACTIVE_HIGH>, + <&gpb 19 GPIO_ACTIVE_HIGH>; + switch-gpios = <&gpa 17 GPIO_ACTIVE_HIGH>; + status = "okay"; + }; +}; + +&pinctrl { + status = "okay"; +}; + +&gpb { + gpio-line-names = "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "ircut-out", "ircut-in"; +}; + +&ost { + status = "okay"; +}; + +&tcu { + status = "okay"; +}; + +&pwm { + status = "okay"; +}; + +&watchdog { + status = "okay"; +}; + +&pdma { + status = "okay"; +}; + +&uart1 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_uart1_b>; + status = "okay"; +}; + +&sfc { + pinctrl-names = "default"; + pinctrl-0 = <&pins_sfc>; + status = "okay"; + + flash@0 { + compatible = "jedec,spi-nor"; + reg = <0>; + label = "jz_sfc"; + spi-max-frequency = <50000000>; + spi-rx-bus-width = <2>; + spi-tx-bus-width = <2>; + + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + label = "boot"; + reg = <0x000000 0x040000>; + }; + + partition@40000 { + label = "bootenv"; + reg = <0x040000 0x008000>; + }; + + partition@48000 { + label = "kernel"; + reg = <0x048000 0x200000>; + }; + + partition@248000 { + label = "rootfs"; + reg = <0x248000 0x300000>; + }; + + partition@548000 { + label = "overlay"; + reg = <0x548000 0x2b8000>; + }; + }; + }; +}; + +&msc0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_mmc0>; + bus-width = <4>; + max-frequency = <25000000>; + cd-gpios = <&gpb 29 GPIO_ACTIVE_LOW>; + status = "okay"; +}; + +&otg_phy { + status = "okay"; +}; + +&otg { + status = "okay"; +}; + +&aic { + status = "okay"; +}; + +&aic_loop { + status = "okay"; +}; + +&codec { + status = "okay"; +}; + +&i2c0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_i2c0>; + clock-frequency = <100000>; + status = "okay"; + + sensor@30 { + compatible = "smartsens,sc1a4t"; + reg = <0x30>; + clocks = <&cgu CLK_CGU_CIM>; + clock-names = "xvclk"; + pwdn-gpios = <&gpa 18 GPIO_ACTIVE_LOW>; + reset-gpios = <&gpa 16 GPIO_ACTIVE_LOW>; + smartsens,secondary-i2c-addr = <0x32>; + + port { + sensor0_out: endpoint { + remote-endpoint = <&isp_in0>; + clock-lanes = <0>; + data-lanes = <1>; + link-frequencies = /bits/ 64 <360000000>; + }; + }; + }; + + sensor@32 { + compatible = "smartsens,sc1a4t"; + reg = <0x32>; + clocks = <&cgu CLK_CGU_CIM>; + clock-names = "xvclk"; + smartsens,frame-sync-slave; + + port { + sensor1_out: endpoint { + remote-endpoint = <&isp_in1>; + clock-lanes = <0>; + data-lanes = <1>; + link-frequencies = /bits/ 64 <360000000>; + }; + }; + }; +}; + +&isp { + assigned-clocks = <&cgu CLK_CGU_ISP>; + assigned-clock-parents = <&cgu CLK_SCLKA>; + assigned-clock-rates = <240000000>; + memory-region = <&camera_reserved>; + ingenic,frame-sync-source-port = <0>; + pinctrl-names = "default"; + pinctrl-0 = <&pins_vic_switch &pins_cim_mclk>; + status = "okay"; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + isp_in0: endpoint { + remote-endpoint = <&sensor0_out>; + clock-lanes = <0>; + data-lanes = <1>; + }; + }; + + port@1 { + reg = <1>; + isp_in1: endpoint { + remote-endpoint = <&sensor1_out>; + clock-lanes = <0>; + data-lanes = <1>; + }; + }; + }; +}; + +&ipu { + memory-region = <&camera_reserved>; + status = "okay"; +}; + +&helix { + assigned-clocks = <&cgu CLK_CGU_VPU>; + assigned-clock-rates = <450000000>; + memory-region = <&camera_reserved>; + status = "okay"; +}; + +&aes { + status = "okay"; +}; + +&dtrng { + status = "okay"; +}; + +&sadc { + status = "okay"; +}; From 98983093fed56c1bd9eb9992669bd26302689781 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 08:41:57 -0800 Subject: [PATCH 72/78] MIPS: dts: ingenic: Add the Jooan JA-A10 T23DL camera Describe the JA-A10 board with 32 MiB RAM, 8 MiB reusable camera CMA, sensor, audio and GPIO-controlled peripherals. Signed-off-by: Difan Zhang --- arch/mips/boot/dts/ingenic/Makefile | 2 +- arch/mips/boot/dts/ingenic/ja-a10.dts | 309 ++++++++++++++++++++++++++ 2 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 arch/mips/boot/dts/ingenic/ja-a10.dts diff --git a/arch/mips/boot/dts/ingenic/Makefile b/arch/mips/boot/dts/ingenic/Makefile index 8504da0896..5ec4f7a2e1 100644 --- a/arch/mips/boot/dts/ingenic/Makefile +++ b/arch/mips/boot/dts/ingenic/Makefile @@ -6,6 +6,6 @@ dtb-$(CONFIG_JZ4780_CI20) += ci20.dtb dtb-$(CONFIG_X1000_CU1000_NEO) += cu1000-neo.dtb dtb-$(CONFIG_X1830_CU1830_NEO) += cu1830-neo.dtb -dtb-$(CONFIG_MACH_T23) += ja-a12.dtb +dtb-$(CONFIG_MACH_T23) += ja-a12.dtb ja-a10.dtb obj-$(CONFIG_BUILTIN_DTB) += $(addsuffix .o, $(dtb-y)) diff --git a/arch/mips/boot/dts/ingenic/ja-a10.dts b/arch/mips/boot/dts/ingenic/ja-a10.dts new file mode 100644 index 0000000000..3ec3ac2e5d --- /dev/null +++ b/arch/mips/boot/dts/ingenic/ja-a10.dts @@ -0,0 +1,309 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/dts-v1/; + +#include "t23.dtsi" +#include +#include + +/ { + model = "Jooan JA-A10 (Ingenic T23DL)"; + compatible = "jooan,ja-a10", "ingenic,t23"; + + aliases { + serial0 = &uart0; + serial1 = &uart1; + serial2 = &uart2; + }; + + chosen { + bootargs = "earlycon console=ttyS1,115200n8 loglevel=7"; + stdout-path = "serial1:115200n8"; + }; + + memory@0 { + device_type = "memory"; + reg = <0x00000000 0x02000000>; + }; + + reserved-memory { + #address-cells = <1>; + #size-cells = <1>; + ranges; + + camera_reserved: camera@1800000 { + compatible = "shared-dma-pool"; + reg = <0x01800000 0x00800000>; + reusable; + linux,cma-default; + }; + }; + + illumination { + compatible = "gpio-leds"; + + led-white { + label = "white:illumination"; + gpios = <&gpb 28 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + + led-infrared { + label = "ir:illumination"; + gpios = <&gpb 27 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + + led-status { + label = "status:indicator"; + gpios = <&gpb 26 GPIO_ACTIVE_HIGH>; + default-state = "off"; + }; + }; + + keys { + compatible = "gpio-keys"; + + key-reset { + label = "Reset"; + gpios = <&gpb 30 GPIO_ACTIVE_LOW>; + linux,code = ; + }; + + key-call { + label = "Call"; + gpios = <&gpb 25 GPIO_ACTIVE_LOW>; + linux,code = ; + }; + }; + + wifi_pwr: wifi-regulator { + compatible = "regulator-fixed"; + regulator-name = "wifi_power"; + gpio = <&gpa 6 GPIO_ACTIVE_HIGH>; + enable-active-high; + regulator-boot-on; + regulator-always-on; + }; + + speaker_amp: speaker-amplifier { + compatible = "simple-audio-amplifier"; + enable-gpios = <&gpb 31 GPIO_ACTIVE_HIGH>; + sound-name-prefix = "Speaker Amp"; + }; + + sound { + compatible = "simple-audio-card"; + simple-audio-card,name = "ja-a10-audio"; + #address-cells = <1>; + #size-cells = <0>; + + simple-audio-card,widgets = + "Speaker", "Speaker", + "Microphone", "Mic"; + simple-audio-card,routing = + "Speaker Amp INL", "LOUT", + "Speaker Amp INR", "ROUT", + "Speaker", "Speaker Amp OUTL", + "Speaker", "Speaker Amp OUTR", + "MICIN", "Mic"; + + simple-audio-card,aux-devs = <&speaker_amp>; + + simple-audio-card,dai-link@0 { + reg = <0>; + format = "i2s"; + bitclock-master = <&sound_codec>; + frame-master = <&sound_codec>; + + cpu { + sound-dai = <&aic>; + }; + + sound_codec: codec { + sound-dai = <&codec 0>; + }; + }; + + simple-audio-card,dai-link@1 { + reg = <1>; + + cpu { + sound-dai = <&aic_loop>; + }; + + codec { + sound-dai = <&codec 1>; + }; + }; + }; + + motor { + compatible = "ingenic,t23-motor"; + pan-gpios = <&gpb 13 GPIO_ACTIVE_HIGH>, + <&gpb 14 GPIO_ACTIVE_HIGH>, + <&gpb 15 GPIO_ACTIVE_HIGH>, + <&gpb 16 GPIO_ACTIVE_HIGH>; + tilt-gpios = <&gpb 20 GPIO_ACTIVE_HIGH>, + <&gpb 21 GPIO_ACTIVE_HIGH>, + <&gpb 22 GPIO_ACTIVE_HIGH>, + <&gpb 19 GPIO_ACTIVE_HIGH>; + ingenic,pan-max-steps = <4100>; + ingenic,tilt-max-steps = <1200>; + ingenic,tilt-reset-steps = <700>; + status = "okay"; + }; +}; + +&pinctrl { + status = "okay"; +}; + +&gpb { + gpio-line-names = "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", + "", "ircut-out", "ircut-in"; +}; + +&ost { + status = "okay"; +}; + +&tcu { + status = "okay"; +}; + +&pwm { + status = "okay"; +}; + +&watchdog { + status = "okay"; +}; + +&pdma { + status = "okay"; +}; + +&uart1 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_uart1_b>; + status = "okay"; +}; + +&sfc { + pinctrl-names = "default"; + pinctrl-0 = <&pins_sfc>; + status = "okay"; + + flash@0 { + compatible = "jedec,spi-nor"; + reg = <0>; + label = "jz_sfc"; + spi-max-frequency = <50000000>; + spi-rx-bus-width = <2>; + spi-tx-bus-width = <2>; + }; +}; + +&msc0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_mmc0>; + bus-width = <4>; + max-frequency = <25000000>; + cd-gpios = <&gpb 29 GPIO_ACTIVE_LOW>; + status = "okay"; +}; + +&otg_phy { + status = "okay"; +}; + +&otg { + status = "okay"; +}; + +&aic { + status = "okay"; +}; + +&aic_loop { + status = "okay"; +}; + +&codec { + status = "okay"; +}; + +&i2c0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_i2c0>; + clock-frequency = <100000>; + status = "okay"; + + sensor@30 { + compatible = "smartsens,sc1a4t"; + reg = <0x30>; + clocks = <&cgu CLK_CGU_CIM>; + clock-names = "xvclk"; + pwdn-gpios = <&gpa 18 GPIO_ACTIVE_LOW>; + reset-gpios = <&gpa 16 GPIO_ACTIVE_LOW>; + + port { + sensor0_out: endpoint { + remote-endpoint = <&isp_in0>; + clock-lanes = <0>; + data-lanes = <1>; + link-frequencies = /bits/ 64 <360000000>; + }; + }; + }; +}; + +&isp { + assigned-clocks = <&cgu CLK_CGU_ISP>; + assigned-clock-parents = <&cgu CLK_SCLKA>; + assigned-clock-rates = <240000000>; + memory-region = <&camera_reserved>; + pinctrl-names = "default"; + pinctrl-0 = <&pins_cim_mclk>; + status = "okay"; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + isp_in0: endpoint { + remote-endpoint = <&sensor0_out>; + clock-lanes = <0>; + data-lanes = <1>; + }; + }; + }; +}; + +&ipu { + memory-region = <&camera_reserved>; + status = "okay"; +}; + +&helix { + assigned-clocks = <&cgu CLK_CGU_VPU>; + assigned-clock-rates = <450000000>; + memory-region = <&camera_reserved>; + status = "okay"; +}; + +&aes { + status = "okay"; +}; + +&dtrng { + status = "okay"; +}; + +&sadc { + status = "okay"; +}; From dfe4e262e4ca416ad37b83f53f6c8cb4c678f87c Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 08:41:57 -0800 Subject: [PATCH 73/78] MIPS: dts: ingenic: Add T23 QEMU camera profiles Add QEMU profiles for the dual SC1A4T, GC2053 and SC2336 pipelines based on the JA-A12 description. Enable the virtual UART and Ethernet interface for emulated regression tests. Signed-off-by: Difan Zhang --- arch/mips/boot/dts/ingenic/Makefile | 4 +- .../mips/boot/dts/ingenic/t23-qemu-gc2053.dts | 48 +++++++++++++++++++ .../mips/boot/dts/ingenic/t23-qemu-sc2336.dts | 48 +++++++++++++++++++ arch/mips/boot/dts/ingenic/t23-qemu.dts | 28 +++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 arch/mips/boot/dts/ingenic/t23-qemu-gc2053.dts create mode 100644 arch/mips/boot/dts/ingenic/t23-qemu-sc2336.dts create mode 100644 arch/mips/boot/dts/ingenic/t23-qemu.dts diff --git a/arch/mips/boot/dts/ingenic/Makefile b/arch/mips/boot/dts/ingenic/Makefile index 5ec4f7a2e1..cefee3c826 100644 --- a/arch/mips/boot/dts/ingenic/Makefile +++ b/arch/mips/boot/dts/ingenic/Makefile @@ -5,7 +5,7 @@ dtb-$(CONFIG_JZ4770_GCW0) += gcw0.dtb dtb-$(CONFIG_JZ4780_CI20) += ci20.dtb dtb-$(CONFIG_X1000_CU1000_NEO) += cu1000-neo.dtb dtb-$(CONFIG_X1830_CU1830_NEO) += cu1830-neo.dtb - -dtb-$(CONFIG_MACH_T23) += ja-a12.dtb ja-a10.dtb +dtb-$(CONFIG_MACH_T23) += ja-a12.dtb ja-a10.dtb t23-qemu.dtb \ + t23-qemu-gc2053.dtb t23-qemu-sc2336.dtb obj-$(CONFIG_BUILTIN_DTB) += $(addsuffix .o, $(dtb-y)) diff --git a/arch/mips/boot/dts/ingenic/t23-qemu-gc2053.dts b/arch/mips/boot/dts/ingenic/t23-qemu-gc2053.dts new file mode 100644 index 0000000000..3cc0738ab9 --- /dev/null +++ b/arch/mips/boot/dts/ingenic/t23-qemu-gc2053.dts @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include "t23-qemu.dts" + +/delete-node/ &{/soc/i2c@10050000/sensor@30}; +/delete-node/ &{/soc/i2c@10050000/sensor@32}; +/delete-node/ &{/soc/isp@13300000/ports/port@0/endpoint}; +/delete-node/ &{/soc/isp@13300000/ports/port@1/endpoint}; + +&i2c0 { + sensor@37 { + compatible = "galaxycore,gc2053"; + reg = <0x37>; + clocks = <&cgu CLK_CGU_CIM>; + clock-names = "xvclk"; + pwdn-gpios = <&gpa 18 GPIO_ACTIVE_LOW>; + reset-gpios = <&gpa 16 GPIO_ACTIVE_LOW>; + + port { + gc2053_out: endpoint { + remote-endpoint = <&gc_isp_in0>; + clock-lanes = <0>; + data-lanes = <1 2>; + link-frequencies = /bits/ 64 <297000000>; + }; + }; + }; +}; + +&isp { + /delete-property/ ingenic,frame-sync-source-port; + pinctrl-0 = <&pins_cim_mclk>; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + gc_isp_in0: endpoint { + remote-endpoint = <&gc2053_out>; + clock-lanes = <0>; + data-lanes = <1 2>; + }; + }; + }; +}; diff --git a/arch/mips/boot/dts/ingenic/t23-qemu-sc2336.dts b/arch/mips/boot/dts/ingenic/t23-qemu-sc2336.dts new file mode 100644 index 0000000000..1dda04f650 --- /dev/null +++ b/arch/mips/boot/dts/ingenic/t23-qemu-sc2336.dts @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include "t23-qemu.dts" + +/delete-node/ &{/soc/i2c@10050000/sensor@30}; +/delete-node/ &{/soc/i2c@10050000/sensor@32}; +/delete-node/ &{/soc/isp@13300000/ports/port@0/endpoint}; +/delete-node/ &{/soc/isp@13300000/ports/port@1/endpoint}; + +&i2c0 { + sensor@30 { + compatible = "smartsens,sc2336"; + reg = <0x30>; + clocks = <&cgu CLK_CGU_CIM>; + clock-names = "xvclk"; + pwdn-gpios = <&gpa 18 GPIO_ACTIVE_LOW>; + reset-gpios = <&gpa 16 GPIO_ACTIVE_LOW>; + + port { + sc2336_out: endpoint { + remote-endpoint = <&sc_isp_in0>; + clock-lanes = <0>; + data-lanes = <1 2>; + link-frequencies = /bits/ 64 <198000000>; + }; + }; + }; +}; + +&isp { + /delete-property/ ingenic,frame-sync-source-port; + pinctrl-0 = <&pins_cim_mclk>; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + sc_isp_in0: endpoint { + remote-endpoint = <&sc2336_out>; + clock-lanes = <0>; + data-lanes = <1 2>; + }; + }; + }; +}; diff --git a/arch/mips/boot/dts/ingenic/t23-qemu.dts b/arch/mips/boot/dts/ingenic/t23-qemu.dts new file mode 100644 index 0000000000..8453b7c589 --- /dev/null +++ b/arch/mips/boot/dts/ingenic/t23-qemu.dts @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang + +#include "ja-a12.dts" + +/ { + model = "OpenIPC T23N QEMU Virtual IPC"; + compatible = "openipc,t23-qemu", "jooan,ja-a12", "ingenic,t23"; +}; + +&uart0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_uart0_b>; + status = "okay"; +}; + +&mac { + phy-handle = <ðphy0>; + status = "okay"; +}; + +&mdio { + status = "okay"; + + ethphy0: ethernet-phy@0 { + reg = <0>; + }; +}; From dd4d0e70e8ba0e077e150bf7893a30a1ace6074a Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Mon, 16 Nov 2026 08:41:58 -0800 Subject: [PATCH 74/78] MIPS: configs: Add the T23 camera defconfig Configure the T23 camera kernel for LZMA-compressed SPI NOR boot with appended DTB, reusable CMA and writable overlay storage. Build media, sensor, audio and wireless support as modules to fit the flash and RAM budgets. Signed-off-by: Difan Zhang --- arch/mips/configs/t23_defconfig | 229 ++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 arch/mips/configs/t23_defconfig diff --git a/arch/mips/configs/t23_defconfig b/arch/mips/configs/t23_defconfig new file mode 100644 index 0000000000..f87e951f70 --- /dev/null +++ b/arch/mips/configs/t23_defconfig @@ -0,0 +1,229 @@ +# CONFIG_LOCALVERSION_AUTO is not set +CONFIG_KERNEL_LZMA=y +CONFIG_SYSVIPC=y +# CONFIG_CROSS_MEMORY_ATTACH is not set +CONFIG_NO_HZ_IDLE=y +CONFIG_HIGH_RES_TIMERS=y +CONFIG_PREEMPT=y +CONFIG_LOG_BUF_SHIFT=14 +CONFIG_BLK_DEV_INITRD=y +# CONFIG_RD_BZIP2 is not set +# CONFIG_RD_LZMA is not set +# CONFIG_RD_XZ is not set +# CONFIG_RD_LZO is not set +# CONFIG_RD_LZ4 is not set +# CONFIG_RD_ZSTD is not set +CONFIG_CC_OPTIMIZE_FOR_SIZE=y +CONFIG_EXPERT=y +# CONFIG_SGETMASK_SYSCALL is not set +# CONFIG_SYSFS_SYSCALL is not set +# CONFIG_FHANDLE is not set +# CONFIG_BUG is not set +# CONFIG_AIO is not set +# CONFIG_IO_URING is not set +# CONFIG_ADVISE_SYSCALLS is not set +# CONFIG_MEMBARRIER is not set +# CONFIG_RSEQ is not set +# CONFIG_CACHESTAT_SYSCALL is not set +# CONFIG_KALLSYMS is not set +CONFIG_MACH_INGENIC_SOC=y +CONFIG_JZ_T23_JA_A12=y +CONFIG_HZ_100=y +CONFIG_MIPS_RAW_APPENDED_DTB=y +CONFIG_MIPS_CMDLINE_DTB_EXTEND=y +# CONFIG_SUSPEND is not set +# CONFIG_SECCOMP is not set +# CONFIG_STACKPROTECTOR_STRONG is not set +CONFIG_MODULES=y +CONFIG_MODULE_UNLOAD=y +# CONFIG_MQ_IOSCHED_DEADLINE is not set +# CONFIG_MQ_IOSCHED_KYBER is not set +# CONFIG_IOSCHED_BFQ is not set +# CONFIG_COREDUMP is not set +# CONFIG_SWAP is not set +CONFIG_SLUB_TINY=y +# CONFIG_COMPAT_BRK is not set +# CONFIG_COMPACTION is not set +CONFIG_CMA=y +# CONFIG_VM_EVENT_COUNTERS is not set +CONFIG_NET=y +CONFIG_PACKET=m +CONFIG_UNIX=y +CONFIG_INET=y +# CONFIG_INET_DIAG is not set +# CONFIG_IPV6 is not set +CONFIG_CFG80211=m +CONFIG_CFG80211_CERTIFICATION_ONUS=y +# CONFIG_CFG80211_REQUIRE_SIGNED_REGDB is not set +CONFIG_CFG80211_WEXT=y +CONFIG_MAC80211=m +# CONFIG_ETHTOOL_NETLINK is not set +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +# CONFIG_ALLOW_DEV_COREDUMP is not set +CONFIG_MTD=y +CONFIG_MTD_CMDLINE_PARTS=y +CONFIG_MTD_BLOCK=y +CONFIG_MTD_SPI_NOR=y +CONFIG_NETDEVICES=y +CONFIG_WIREGUARD=m +# CONFIG_NET_VENDOR_ALACRITECH is not set +# CONFIG_NET_VENDOR_AMAZON is not set +# CONFIG_NET_VENDOR_AQUANTIA is not set +# CONFIG_NET_VENDOR_ARC is not set +# CONFIG_NET_VENDOR_ASIX is not set +# CONFIG_NET_VENDOR_BROADCOM is not set +# CONFIG_NET_VENDOR_CADENCE is not set +# CONFIG_NET_VENDOR_CAVIUM is not set +# CONFIG_NET_VENDOR_CORTINA is not set +# CONFIG_NET_VENDOR_DAVICOM is not set +# CONFIG_NET_VENDOR_ENGLEDER is not set +# CONFIG_NET_VENDOR_EZCHIP is not set +# CONFIG_NET_VENDOR_FUNGIBLE is not set +# CONFIG_NET_VENDOR_GOOGLE is not set +# CONFIG_NET_VENDOR_HUAWEI is not set +# CONFIG_NET_VENDOR_INTEL is not set +# CONFIG_NET_VENDOR_ADI is not set +# CONFIG_NET_VENDOR_LITEX is not set +# CONFIG_NET_VENDOR_MARVELL is not set +# CONFIG_NET_VENDOR_MELLANOX is not set +# CONFIG_NET_VENDOR_META is not set +# CONFIG_NET_VENDOR_MICREL is not set +# CONFIG_NET_VENDOR_MICROCHIP is not set +# CONFIG_NET_VENDOR_MICROSEMI is not set +# CONFIG_NET_VENDOR_MICROSOFT is not set +# CONFIG_NET_VENDOR_NI is not set +# CONFIG_NET_VENDOR_NATSEMI is not set +# CONFIG_NET_VENDOR_NETRONOME is not set +# CONFIG_NET_VENDOR_PENSANDO is not set +# CONFIG_NET_VENDOR_QUALCOMM is not set +# CONFIG_NET_VENDOR_RENESAS is not set +# CONFIG_NET_VENDOR_ROCKER is not set +# CONFIG_NET_VENDOR_SAMSUNG is not set +# CONFIG_NET_VENDOR_SEEQ is not set +# CONFIG_NET_VENDOR_SOLARFLARE is not set +# CONFIG_NET_VENDOR_SMSC is not set +# CONFIG_NET_VENDOR_SOCIONEXT is not set +CONFIG_STMMAC_ETH=m +# CONFIG_NET_VENDOR_SYNOPSYS is not set +# CONFIG_NET_VENDOR_VERTEXCOM is not set +# CONFIG_NET_VENDOR_VIA is not set +# CONFIG_NET_VENDOR_WANGXUN is not set +# CONFIG_NET_VENDOR_WIZNET is not set +# CONFIG_NET_VENDOR_XILINX is not set +CONFIG_ICPLUS_PHY=m +# CONFIG_WLAN is not set +CONFIG_INPUT=m +CONFIG_INPUT_EVDEV=m +# CONFIG_KEYBOARD_ATKBD is not set +CONFIG_KEYBOARD_GPIO=m +# CONFIG_INPUT_MOUSE is not set +# CONFIG_SERIO is not set +# CONFIG_VT is not set +# CONFIG_LEGACY_PTYS is not set +# CONFIG_LEGACY_TIOCSTI is not set +CONFIG_SERIAL_8250=y +CONFIG_SERIAL_8250_CONSOLE=y +CONFIG_SERIAL_8250_NR_UARTS=3 +CONFIG_SERIAL_8250_RUNTIME_UARTS=3 +CONFIG_SERIAL_8250_INGENIC=y +CONFIG_HW_RANDOM=y +CONFIG_I2C=y +CONFIG_I2C_CHARDEV=y +CONFIG_I2C_JZ4780=y +CONFIG_SPI=y +CONFIG_SPI_INGENIC=m +CONFIG_SPI_INGENIC_SFC=y +CONFIG_PTP_1588_CLOCK=m +CONFIG_GPIO_SYSFS=y +# CONFIG_HWMON is not set +CONFIG_WATCHDOG=y +CONFIG_JZ4740_WDT=y +CONFIG_REGULATOR=y +CONFIG_REGULATOR_FIXED_VOLTAGE=y +CONFIG_MEDIA_SUPPORT=m +CONFIG_MEDIA_SUPPORT_FILTER=y +CONFIG_MEDIA_CAMERA_SUPPORT=y +CONFIG_MEDIA_PLATFORM_SUPPORT=y +CONFIG_V4L_PLATFORM_DRIVERS=y +CONFIG_V4L_MEM2MEM_DRIVERS=y +CONFIG_VIDEO_INGENIC_PTZ_MOTOR=m +CONFIG_VIDEO_INGENIC_ISP=m +CONFIG_VIDEO_INGENIC_HELIX=m +CONFIG_VIDEO_INGENIC_IPU=m +CONFIG_VIDEO_GC2053=m +CONFIG_VIDEO_SC1A4T=m +CONFIG_VIDEO_SC2336P=m +CONFIG_VIDEO_SC2337P=m +CONFIG_SOUND=m +CONFIG_SND=m +# CONFIG_SND_SUPPORT_OLD_API is not set +# CONFIG_SND_VERBOSE_PROCFS is not set +# CONFIG_SND_DRIVERS is not set +# CONFIG_SND_SPI is not set +# CONFIG_SND_MIPS is not set +# CONFIG_SND_USB is not set +CONFIG_SND_SOC=m +CONFIG_SND_JZ4740_SOC_I2S=m +CONFIG_SND_SOC_INGENIC_T23_CODEC=m +CONFIG_SND_SOC_SIMPLE_AMPLIFIER=m +CONFIG_SND_SIMPLE_CARD=m +# CONFIG_HID_SUPPORT is not set +CONFIG_USB=m +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y +CONFIG_USB_DWC2=m +CONFIG_MMC=m +CONFIG_MMC_JZ4740=m +CONFIG_NEW_LEDS=y +CONFIG_LEDS_CLASS=y +CONFIG_LEDS_GPIO=y +CONFIG_RTC_CLASS=y +CONFIG_RTC_DRV_JZ4740=y +CONFIG_DMADEVICES=y +CONFIG_DMA_JZ4780=y +# CONFIG_INGENIC_TIMER is not set +CONFIG_INGENIC_SYSOST=y +# CONFIG_IOMMU_SUPPORT is not set +CONFIG_REMOTEPROC=y +CONFIG_INGENIC_T23_REMOTEPROC=m +CONFIG_MEMORY=y +CONFIG_INGENIC_T23_DDR=y +CONFIG_IIO=m +CONFIG_INGENIC_T23_SADC=m +CONFIG_PWM=y +CONFIG_PWM_JZ4740=y +CONFIG_NVMEM_INGENIC_EFUSE=y +# CONFIG_DNOTIFY is not set +CONFIG_OVERLAY_FS=y +CONFIG_VFAT_FS=m +# CONFIG_PROC_PAGE_MONITOR is not set +CONFIG_TMPFS=y +CONFIG_JFFS2_FS=y +CONFIG_JFFS2_FS_XATTR=y +# CONFIG_JFFS2_FS_POSIX_ACL is not set +# CONFIG_JFFS2_FS_SECURITY is not set +CONFIG_SQUASHFS=y +# CONFIG_SQUASHFS_ZLIB is not set +CONFIG_SQUASHFS_XZ=y +# CONFIG_NETWORK_FILESYSTEMS is not set +CONFIG_NLS_CODEPAGE_437=m +CONFIG_NLS_ISO8859_1=m +CONFIG_CRYPTO_ECHAINIV=m +CONFIG_CRYPTO_USER_API_SKCIPHER=m +CONFIG_CRYPTO_DEV_INGENIC_AES=m +CONFIG_CRC32_SARWATE=y +# CONFIG_XZ_DEC_X86 is not set +# CONFIG_XZ_DEC_POWERPC is not set +# CONFIG_XZ_DEC_ARM is not set +# CONFIG_XZ_DEC_ARMTHUMB is not set +# CONFIG_XZ_DEC_ARM64 is not set +# CONFIG_XZ_DEC_SPARC is not set +# CONFIG_XZ_DEC_RISCV is not set +CONFIG_DMA_CMA=y +CONFIG_CMA_SIZE_MBYTES=0 +CONFIG_CMA_ALIGNMENT=4 +CONFIG_PRINTK_TIME=y +# CONFIG_SYMBOLIC_ERRNAME is not set +# CONFIG_DEBUG_MISC is not set +# CONFIG_RCU_TRACE is not set +# CONFIG_FTRACE is not set From 2717f5e6f14ba3cd8770e7791844aa30eabe4d92 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Tue, 17 Nov 2026 18:44:10 -0800 Subject: [PATCH 75/78] dt-bindings: mips: ingenic: Add Generic OpenIPC T23 board Document the openipc,t23 compatible string for generic OpenIPC Ingenic T23 IP camera boards. Signed-off-by: Difan Zhang --- Documentation/devicetree/bindings/mips/ingenic/devices.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Documentation/devicetree/bindings/mips/ingenic/devices.yaml b/Documentation/devicetree/bindings/mips/ingenic/devices.yaml index 2f6f0c6c33..62666b02f7 100644 --- a/Documentation/devicetree/bindings/mips/ingenic/devices.yaml +++ b/Documentation/devicetree/bindings/mips/ingenic/devices.yaml @@ -67,6 +67,11 @@ properties: - const: jooan,ja-a12 - const: ingenic,t23 + - description: OpenIPC T23 Generic IPC + items: + - const: openipc,t23 + - const: ingenic,t23 + - description: Generic Ingenic T23 SoC const: ingenic,t23 From 1a2ce93e2a23e7a754c6b94907008ecca3fceb8c Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Tue, 17 Nov 2026 18:44:11 -0800 Subject: [PATCH 76/78] MIPS: dts: ingenic: Add generic OpenIPC T23 board Add a generic OpenIPC Ingenic T23 device tree (t23-openipc.dts) covering standard SoC peripherals, 64 MiB RAM with a 24 MiB CMA pool for camera buffers, jz_sfc 8 MiB OpenIPC NOR partition layout, RMII Ethernet MAC/MDIO, USB OTG, MMC0 with GPIO card detect, internal audio codec, and a default 2-lane GC2053 MIPI CSI-2 sensor on I2C0 without vendor board-specific GPIOs. Signed-off-by: Difan Zhang --- arch/mips/boot/dts/ingenic/Makefile | 5 +- arch/mips/boot/dts/ingenic/t23-openipc.dts | 281 +++++++++++++++++++++ 2 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 arch/mips/boot/dts/ingenic/t23-openipc.dts diff --git a/arch/mips/boot/dts/ingenic/Makefile b/arch/mips/boot/dts/ingenic/Makefile index cefee3c826..8dd22f133c 100644 --- a/arch/mips/boot/dts/ingenic/Makefile +++ b/arch/mips/boot/dts/ingenic/Makefile @@ -5,7 +5,8 @@ dtb-$(CONFIG_JZ4770_GCW0) += gcw0.dtb dtb-$(CONFIG_JZ4780_CI20) += ci20.dtb dtb-$(CONFIG_X1000_CU1000_NEO) += cu1000-neo.dtb dtb-$(CONFIG_X1830_CU1830_NEO) += cu1830-neo.dtb -dtb-$(CONFIG_MACH_T23) += ja-a12.dtb ja-a10.dtb t23-qemu.dtb \ - t23-qemu-gc2053.dtb t23-qemu-sc2336.dtb +dtb-$(CONFIG_MACH_T23) += ja-a12.dtb ja-a10.dtb t23-openipc.dtb \ + t23-qemu.dtb t23-qemu-gc2053.dtb \ + t23-qemu-sc2336.dtb obj-$(CONFIG_BUILTIN_DTB) += $(addsuffix .o, $(dtb-y)) diff --git a/arch/mips/boot/dts/ingenic/t23-openipc.dts b/arch/mips/boot/dts/ingenic/t23-openipc.dts new file mode 100644 index 0000000000..7f297e3255 --- /dev/null +++ b/arch/mips/boot/dts/ingenic/t23-openipc.dts @@ -0,0 +1,281 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2026 Difan Zhang +/dts-v1/; + +#include "t23.dtsi" +#include + +/ { + model = "ingenic T23 OpenIPC Generic IPC"; + compatible = "openipc,t23", "ingenic,t23"; + + aliases { + serial0 = &uart0; + serial1 = &uart1; + serial2 = &uart2; + }; + + chosen { + bootargs = "console=ttyS1,115200n8 root=/dev/mtdblock3 rootfstype=squashfs init=/init"; + stdout-path = "serial1:115200n8"; + }; + + memory@0 { + device_type = "memory"; + reg = <0x00000000 0x04000000>; + }; + + reserved-memory { + #address-cells = <1>; + #size-cells = <1>; + ranges; + + camera { + compatible = "shared-dma-pool"; + size = <0x01800000>; + alloc-ranges = <0x02800000 0x01800000>; + reusable; + linux,cma-default; + }; + }; + + sound { + compatible = "simple-audio-card"; + simple-audio-card,name = "t23-audio"; + #address-cells = <1>; + #size-cells = <0>; + + simple-audio-card,widgets = + "Speaker", "Speaker", + "Microphone", "Mic"; + simple-audio-card,routing = + "Speaker", "LOUT", + "Speaker", "ROUT", + "MICIN", "Mic"; + + simple-audio-card,dai-link@0 { + reg = <0>; + format = "i2s"; + bitclock-master = <&sound_codec>; + frame-master = <&sound_codec>; + + cpu { + sound-dai = <&aic>; + }; + + sound_codec: codec { + sound-dai = <&codec 0>; + }; + }; + + simple-audio-card,dai-link@1 { + reg = <1>; + + cpu { + sound-dai = <&aic_loop>; + }; + + codec { + sound-dai = <&codec 1>; + }; + }; + }; +}; + +&pinctrl { + status = "okay"; +}; + +&ost { + status = "okay"; +}; + +&tcu { + status = "okay"; +}; + +&pwm { + status = "okay"; +}; + +&watchdog { + status = "okay"; +}; + +&pdma { + status = "okay"; +}; + +&uart0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_uart0_b>; + status = "okay"; +}; + +&uart1 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_uart1_b>; + status = "okay"; +}; + +&sfc { + pinctrl-names = "default"; + pinctrl-0 = <&pins_sfc>; + status = "okay"; + + flash@0 { + compatible = "jedec,spi-nor"; + reg = <0>; + label = "jz_sfc"; + spi-max-frequency = <50000000>; + spi-rx-bus-width = <2>; + spi-tx-bus-width = <2>; + + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + label = "boot"; + reg = <0x000000 0x040000>; + }; + + partition@40000 { + label = "env"; + reg = <0x040000 0x010000>; + }; + + partition@50000 { + label = "kernel"; + reg = <0x050000 0x200000>; + }; + + partition@250000 { + label = "rootfs"; + reg = <0x250000 0x500000>; + }; + + partition@750000 { + label = "rootfs_data"; + reg = <0x750000 0x0b0000>; + }; + }; + }; +}; + +&msc0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_mmc0>; + bus-width = <4>; + max-frequency = <25000000>; + cd-gpios = <&gpb 27 GPIO_ACTIVE_LOW>; + status = "okay"; +}; + +&mac { + phy-handle = <ðphy0>; + status = "okay"; +}; + +&mdio { + status = "okay"; + + ethphy0: ethernet-phy@0 { + reg = <0>; + }; +}; + +&otg_phy { + status = "okay"; +}; + +&otg { + status = "okay"; +}; + +&aic { + status = "okay"; +}; + +&aic_loop { + status = "okay"; +}; + +&codec { + status = "okay"; +}; + +&i2c0 { + pinctrl-names = "default"; + pinctrl-0 = <&pins_i2c0>; + clock-frequency = <100000>; + status = "okay"; + + sensor@37 { + compatible = "galaxycore,gc2053"; + reg = <0x37>; + clocks = <&cgu CLK_CGU_CIM>; + clock-names = "xvclk"; + pwdn-gpios = <&gpa 18 GPIO_ACTIVE_LOW>; + reset-gpios = <&gpa 16 GPIO_ACTIVE_LOW>; + + port { + gc2053_out: endpoint { + remote-endpoint = <&isp_in0>; + clock-lanes = <0>; + data-lanes = <1 2>; + link-frequencies = /bits/ 64 <297000000>; + }; + }; + }; +}; + +&isp { + assigned-clocks = <&cgu CLK_CGU_ISP>; + assigned-clock-parents = <&cgu CLK_SCLKA>; + assigned-clock-rates = <240000000>; + pinctrl-names = "default"; + pinctrl-0 = <&pins_cim_mclk>; + status = "okay"; + + ports { + #address-cells = <1>; + #size-cells = <0>; + + port@0 { + reg = <0>; + isp_in0: endpoint { + remote-endpoint = <&gc2053_out>; + clock-lanes = <0>; + data-lanes = <1 2>; + }; + }; + }; +}; + +&ipu { + status = "okay"; +}; + +&helix { + assigned-clocks = <&cgu CLK_CGU_VPU>; + assigned-clock-rates = <450000000>; + status = "okay"; +}; + +&aes { + status = "okay"; +}; + +&dtrng { + status = "okay"; +}; + +&sadc { + status = "okay"; +}; + +&efuse { + status = "okay"; +}; From 43157562b29b69982aa80d470c4bcf79ace3e830 Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Tue, 17 Nov 2026 18:44:12 -0800 Subject: [PATCH 77/78] hwrng: ingenic: Allow building DTRNG driver on T23 Commit f399df5a8a2e ("hwrng: ingenic: Add the T23 DTRNG compatible") added the ingenic,t23-dtrng compatible to ingenic-trng.c, but HW_RANDOM_INGENIC_TRNG in Kconfig still depended only on MACH_X1830 || COMPILE_TEST. Add MACH_T23 to the dependency so the T23 DTRNG driver can be enabled on T23-only builds. Signed-off-by: Difan Zhang --- drivers/char/hw_random/Kconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig index f0dde77f50..59de0cf068 100644 --- a/drivers/char/hw_random/Kconfig +++ b/drivers/char/hw_random/Kconfig @@ -272,11 +272,11 @@ config HW_RANDOM_INGENIC_RNG config HW_RANDOM_INGENIC_TRNG tristate "Ingenic True Random Number Generator support" - depends on MACH_X1830 || COMPILE_TEST + depends on MACH_X1830 || MACH_T23 || COMPILE_TEST default HW_RANDOM help This driver provides kernel-side support for the True Random Number Generator - hardware found in ingenic X1830 SoC. YSH & ATIL CU1830-Neo uses X1830 SoC. + hardware found in ingenic X1830 and T23 SoCs. YSH & ATIL CU1830-Neo uses X1830 SoC. To compile this driver as a module, choose M here: the module will be called ingenic-trng. From 12dfae2605a3160464c87d9b2c944ed98e5148dc Mon Sep 17 00:00:00 2001 From: Difan Zhang Date: Tue, 17 Nov 2026 18:44:12 -0800 Subject: [PATCH 78/78] MIPS: configs: Modularize t23_defconfig drivers for OpenIPC size budget Keep core boot/bus drivers (I2C, regulator, watchdog, SFC) built in while modularizing media, Ethernet/PHY, TUN, LEDs, RTC, PWM, and DTRNG so vmlinux.bin stays comfortably under the 4.5 MiB U-Boot decompression cap. Signed-off-by: Difan Zhang --- arch/mips/configs/t23_defconfig | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/mips/configs/t23_defconfig b/arch/mips/configs/t23_defconfig index f87e951f70..1af0f109ab 100644 --- a/arch/mips/configs/t23_defconfig +++ b/arch/mips/configs/t23_defconfig @@ -67,6 +67,7 @@ CONFIG_MTD_BLOCK=y CONFIG_MTD_SPI_NOR=y CONFIG_NETDEVICES=y CONFIG_WIREGUARD=m +CONFIG_TUN=m # CONFIG_NET_VENDOR_ALACRITECH is not set # CONFIG_NET_VENDOR_AMAZON is not set # CONFIG_NET_VENDOR_AQUANTIA is not set @@ -105,6 +106,7 @@ CONFIG_WIREGUARD=m # CONFIG_NET_VENDOR_SMSC is not set # CONFIG_NET_VENDOR_SOCIONEXT is not set CONFIG_STMMAC_ETH=m +# CONFIG_DWMAC_GENERIC is not set # CONFIG_NET_VENDOR_SYNOPSYS is not set # CONFIG_NET_VENDOR_VERTEXCOM is not set # CONFIG_NET_VENDOR_VIA is not set @@ -127,9 +129,8 @@ CONFIG_SERIAL_8250_CONSOLE=y CONFIG_SERIAL_8250_NR_UARTS=3 CONFIG_SERIAL_8250_RUNTIME_UARTS=3 CONFIG_SERIAL_8250_INGENIC=y -CONFIG_HW_RANDOM=y CONFIG_I2C=y -CONFIG_I2C_CHARDEV=y +CONFIG_I2C_CHARDEV=m CONFIG_I2C_JZ4780=y CONFIG_SPI=y CONFIG_SPI_INGENIC=m @@ -175,10 +176,10 @@ CONFIG_USB_DWC2=m CONFIG_MMC=m CONFIG_MMC_JZ4740=m CONFIG_NEW_LEDS=y -CONFIG_LEDS_CLASS=y -CONFIG_LEDS_GPIO=y +CONFIG_LEDS_CLASS=m +CONFIG_LEDS_GPIO=m CONFIG_RTC_CLASS=y -CONFIG_RTC_DRV_JZ4740=y +CONFIG_RTC_DRV_JZ4740=m CONFIG_DMADEVICES=y CONFIG_DMA_JZ4780=y # CONFIG_INGENIC_TIMER is not set @@ -191,7 +192,7 @@ CONFIG_INGENIC_T23_DDR=y CONFIG_IIO=m CONFIG_INGENIC_T23_SADC=m CONFIG_PWM=y -CONFIG_PWM_JZ4740=y +CONFIG_PWM_JZ4740=m CONFIG_NVMEM_INGENIC_EFUSE=y # CONFIG_DNOTIFY is not set CONFIG_OVERLAY_FS=y