From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233345; b=q+G952kzHp3sOIdPSdN3RWJX73esiOuxuIsxw9N478xQSQjT1SqOWeXRHXCGmeHtXUwq q2ip4Ip9+aJ/CZgBO5o9v7IeV852Lpu+07El/gmHoqJ2OWbHroTbRqtuwPbIGcHyhButX lxesEBczGLLvr7zX++mU4Pu803UdRo7MoPinaFMdgGsLnpLs693K4U0vGylN6HHcrjiQy X7K5MHLT6UwPsvdDF81DQ+++fH+32MzehHbkESZ3yPweiJAfCaxHmGW2Hn4wKOuAjjCUj /RYqQ+BDzxEZ6BPEz7xJ5M8jicPA5qc/0XYcPVgQ1iwGaH3n0v68HUcn9DYLsRURB/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233345; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=jgUdZaGhqWrc4BAHJXZyBPZNLZ60f3XoK2Gog1q3Uc8=; b=N4j+M0eK+I4boJvUeqik2zvMTgVgpFf1oq3won83smgJEW8p7LI/m7QpjrKov+J1Qe9V mOBbIa8yeg+u40bvo26cTxloQoPJ6FjiMg9Z7HI55BACARGVi8MJnbt7+pYgTGnTpLDLd LVtq6JSlwMI10godn5kZ8FP78Rm3ViG2hlHFcYT8czH5aIAyhgYqK3n0qolbEVHLRPPqk 9M8EEFPqgmkNnzya204wiDz1hNnoA9q69HXvOeH90+RosKpJAG0Yv9ymiJLNOGGVTugNd 8YbfzkB9IsZ83ptWYA5AWrzBUfH0zdgR4FfQ8R7qi8Qy6ViuklxsRMD0x70e75YIGaA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SiEljIY9; arc=none smtp.remote-ip=209.85.210.44 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DB6B606A331 for ; Sat, 21 Nov 2026 03:55:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3DB6B606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SiEljIY9 Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-86156cf8a7eso2045159a34.1 for ; Fri, 20 Nov 2026 19:55:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233342; x=1795838142; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jgUdZaGhqWrc4BAHJXZyBPZNLZ60f3XoK2Gog1q3Uc8=; b=SiEljIY9Mcg9FXo0sQwif4upe3XondP+ab2bFC/HoIvVbdg/GqptcMkpEJl5hwBtm4 sy0eskecI+6aqkXhijQf3zbOX2dxdX5NneFZQ43Z7k8bIzDm14FIXMrdn67DBBiw/c09 MWCDN0mobbKp9TG7406qRLkJSMMoh2nc4ldmxXp8WA7pwy0Sg3B3UBUk4ZAnVtIhhYSi 234qPgqzhM1Qw7sIUIKJgbZYc/J92mtifsVyZpb8aKTrdSVVQteKXH3lOzFDfvYgifUL uFXJoGKAF3j/GgJIbldi4jsTg6eGBdwW5EpNio+sXh31PRKETOfeQdK5mPmzEP6kf18G LnIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233342; x=1795838142; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jgUdZaGhqWrc4BAHJXZyBPZNLZ60f3XoK2Gog1q3Uc8=; b=StASsVwQeYRl1EVdtaLdk81K5YoEYtgnx1bnOR1OLwpTGN4YGzCL71+XIY61wQ6Vj4 PfeprehKKlHOA2kk3HBP/RVz9MrDf/NXIMGLp5TwCoTIuSCiIJdx3C/8RRyEjF9TTzFI BADFJ7clYIriTZh0hN4i/rMRnkyxcFbtNli9Q4FHj692NC0ZFan8lVqQcHniZ/USDGRT krxe5Z1xuPyPOM33pWrSlqGJBiOhz5bBM0RBD3+o157Yv0TCx/fd2S9fozafi1gUbbI/ rcxAPj/kK00K+JFrXbi7fR2H2TjjXa35mX+HHe74w3glgyw+7kDdXf8gY3Mz8SrAasev m3bg== X-Gm-Message-State: APuU4Z+7f8zJiDLF8KWI0V1OBm6Xk0mzXnIJlqUuDmR9MqEGKNf2gPvf sP+I/HXdqCXl2QDK4CLCdfjUaE/Z0LoerOkldqBpxfeO+USfasQLYTM= X-Gm-Gg: AYBFou3aKxiQNX5Gskl5HbZ2TX9RnLT0RdY82tT1qmJ04Dtn+CZU/cVvNdYUnFulPW9 wbu0Pi8tU8StVapfC5eTZ7olFcuxC0WmH5Aw4AX3YXCBEAQrtiGYzWRJ6kSh+F60EB7DtPzZLG8 stFGN2i8G9iAoZ998o0DaK35ae+bzato1l5TPYAg6NDxr/pfgRtDtHCyagxhH3DL1iqg77t8sB3 cr6YzqtL1pDXmY6SJG3dS1X8ezzBCFNo8xQgbtznugZoqRuHfm2aATLVolD3M66rVc8T3J2+eQ2 1+ssUsTDZv6YN+p+plye0x0JB7yQ6zxXRG9NugveMyNKqwgfovi76CmXTrk0Qk+mvMlsfPRksja NROAJM5klGQnTx5sF9Dcn/7uZNuiIwZ361vTWaRfTjDn6lisKHh1a1QBxJL+J9qd4vQMDC5ecsz 03tJEQr8Cb7C1vAeYQcw5fQH6gEPxneSbO/fhU3XZVvBJCytGAed7u49TYL2g44iPXUf9wmhVvu mGmCzI4Lv6yzvVAuUAkDktUEBUAf+xH7eTXBPRpSkpK1gGFb73ZAZ6qMDd+uyK4Z5riI/AQf23n mzMVV7fJsnxoP3Hmuyr89QPqXqphWOcqu0FJAFihfBEO4tNerUPxWJp/8Rq96b1xMEq1MoiVZ7E tHQ== X-Received: by 2002:a05:6820:4381:b0:71a:fcbf:2484 with SMTP id 006d021491bc7-71b395d4870mr2846540eaf.36.1795233342085; Fri, 20 Nov 2026 19:55:42 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:40 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux , Paul Kocialkowski Subject: [PATCH v7 01/12] media: tw9900: Disable regulator on runtime resume failure Date: Fri, 20 Nov 2026 21:55:12 -0600 Message-ID: <20261121035523.26764-2-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If writing the initialization registers fails, put the chip back in reset and disable the regulator before returning the error. Fixes this warning: WARNING: drivers/regulator/core.c:2657 at regulator_put+0x61/0x70 RIP: 0010:regulator_put+0x61/0x70 Call Trace: release_nodes+0x46/0xd0 devres_release_group+0x11f/0x150 i2c_device_remove+0x41/0xb0 device_release_driver_internal+0x19c/0x200 bus_remove_device+0xca/0x140 device_del+0x160/0x3e0 device_unregister+0x17/0x70 v4l2_device_unregister+0x9c/0xd0 [videodev] usb3hdcap_release+0x26/0x40 [usb3hdcap] v4l2_device_put+0x37/0x60 [videodev] release_card_device+0x51/0x90 [snd] device_release+0x37/0x90 kobject_put+0x8e/0x210 snd_card_file_remove+0x174/0x190 [snd] snd_ctl_release+0x14d/0x160 [snd] __fput+0xe6/0x2a0 __x64_sys_close+0x3d/0x80 do_syscall_64+0x120/0x1610 ? do_syscall_64+0x15e/0x1610 ? irqentry_exit+0x7b/0x590 entry_SYSCALL_64_after_hwframe+0x76/0x7e Fixes: 918b14a26b12 ("media: i2c: Introduce a driver for the Techwell TW990= 0 decoder") Cc: Paul Kocialkowski Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index 60c102aba750..e07924faa84b 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -624,13 +624,20 @@ static int tw9900_runtime_resume(struct device *dev) =20 ret =3D tw9900_write_array(tw9900->client, tw9900_init_regs, ARRAY_SIZE(tw9900_init_regs)); + if (ret) { + if (tw9900->reset_gpio) + gpiod_set_value_cansleep(tw9900->reset_gpio, 1); + regulator_disable(tw9900->regulator); + mutex_unlock(&tw9900->mutex); + return ret; + } =20 mutex_unlock(&tw9900->mutex); =20 /* This sleep is needed for the Horizontal Sync PLL to lock. */ msleep(300); =20 - return ret; + return 0; } =20 static int tw9900_runtime_suspend(struct device *dev) --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233347; b=2DqXFjGFgSnKo05xOpSlI25Iw7JAlM6S+mDdWOJjBbEkZFUKQkOxPNbG/JHjCBU9TrHg sYWkfyREww7VrHk+U24Rl0qQv6ZkRQCS7AqHi1N71YA0VLJ7BaWrmVQwUVn5qGUezwK4c SYQvXcqg51Vd1lPrjl1rD7Nit1hhBBfIl0GRJJhRShIJ8rlOdJrg85SOWIeb2uBqPpS/c IQAuivDWYjtu5uIqrPF8A5SruH1hAoP23o4JcgXQueruGeiUPTiC9x0++ZMkO5RzrBtr7 XxVoq6/fbGf5cDSrfrZLwCrylQkUEmC4r6ALAy86gUab6sckkEry/KIFAhOT0FPISEA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233347; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=4zJLU7TV4OK6TSVsIiKHn0bV6zmnjofg1PwBOjuNmRA=; b=W06y/nO1YNuvZSc4nEjXV/l2y52a7AEuMHfPLTxuil311Lf7NSvWr6paAI9ScQypkAEG NA4fvpsodyly1h0WZI+f/kj4wuHiAKPq+gpHBlTuwgR3Ymc6nvzwGdoz9TTF2fKZ9WFDg 8VsFVcGUdzuTUhQUvOLfHyvnIF2ydw5U5d6hsFCBID3cs4R54sWnA7gmAQDFucKxfwktE Jn0wkx0K3dr18FyDOaJJrDIno76uj9Hos/+EXHva8tsbLx+RxfzEdbySFv2BZFeiYJAaZ E0Plxv1spoKjw88r0MbcvhosBUhzjDl9FBj+eNhpiJpk7ikngnSzikx1xVCHC30TnmA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YPalnPgr; arc=none smtp.remote-ip=209.85.161.51 Received: from mail-oo1-f51.google.com (mail-oo1-f51.google.com [209.85.161.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5AD2606A6BE for ; Sat, 21 Nov 2026 03:55:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C5AD2606A6BE Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=YPalnPgr Received: by mail-oo1-f51.google.com with SMTP id 006d021491bc7-702dedeb70dso1021017eaf.1 for ; Fri, 20 Nov 2026 19:55:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233345; x=1795838145; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=4zJLU7TV4OK6TSVsIiKHn0bV6zmnjofg1PwBOjuNmRA=; b=YPalnPgrFTJrmqQNuFZXEBMnkOL5fJganovUfgw37I6U3eVfJfSu/EJMW/r/ptkiWM JuKw8XBwwJ4JmieONT28MymFBceHvD8Jvpgm8OVg86vSwned/LBLsJ0B+Po1f5niK7Ed AJNNK616YS6oRg/UzEYFk2QTRyWFPsCy6kKFxwcD9PfUJNKQt29vkGuTQSwf25SPYRD0 NI4wOv1nAx/xK10+DVjX17/NRB/g+6VxudPsBQ4u0MNyeK2hkqCMNfPHSfQ8OoiYn4q2 n3OOnqOtklVALiFuE6lPeBvnSBqvROTMq8u/pm0nxW6xfIv2QOTUuh5z3CJLE/gQ9XUq oA9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233345; x=1795838145; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=4zJLU7TV4OK6TSVsIiKHn0bV6zmnjofg1PwBOjuNmRA=; b=fMvK03FA903J2ICrIbRj4YhzDnOrUep66Ks/QwBznMiTlgW6j6EVUireQmoivdHP6V He7GgpkIW2F4/3lsE0snG08I2MposuATtUAo43r+qQmdABmFQsBRVpwyQBvuHzJ+0GRu grtilPEnv53x1lhSJSS8UoIr3ZAIZHO3jueQ1t6h65JrvlbsQ56GgOMZVKhQR4E5GAIY K2dgtfT4TqqRHPYJlFPjLDbk4VmSyHMNItdLVOyHQcLT+jThRIqt2clNV9eWQaRMAAkq 39GsEbEKRMv9eBt6Fi3lhSvvJgUMD43PJLbGhyDHKvhksd4/6i6thnUHkWopclRWMMVg VyVg== X-Gm-Message-State: APuU4Z/je6tR03xlufoZJNALigctiSXoBjoxxeyia2PXKM4FImks2jm7 bx7E78wRcrRyVVoTX9PONOzHpFy2ePoTruMzmB2UOT7gwX7nA9yyhsY= X-Gm-Gg: AYBFou0dtJ2EoDE7XkrVDXQgVJUDZ3JZQBnSfgyhOZ3HqUh19KbuYKuUZ95QZNgTwNz /DE24/5mfZWNG6fEG/xPss7z0ozORQS1EPehEjsmGoLL5fAVOZzZ4HeP+/hP8jn/OSny9C202Ie 6MkwJdzjI6CeKteOOcWwrKpsDpxds/KuTwH9v6EgNT+pJ90db+NiKWVF7GSzuvwaTrfFSr3lF9f i8Tf/rkEQOWzxWq+pkENHhXtUDP7Kp7sUuAL++Gers4ddIZ0wmBaXTrzajWvUbaNmYRHupPMaUV BPThcZc22Ep81ZXo9QzSTTrte40Hd2YWtSycqS5x41M/Y38MvV5CzmSGk8cRWAxzz0mK0e3k0wI SEjS8P4Ztez/i5H/XklWWkxMNPVXr5nzM/Y06O0QxDuxAXV76HGbropEpod60z2NxROzLHl8c2w dajtiW5wBmi9ddHKSo/C0SrZJGEDbLpOYZcZ5asv5DIQbNOVV+cTUDL5i2iKLYaFKSuySH4/NNJ 79Rh6a+bxXTgkAPGALWtAhe7egXf0RF5Wi8JEIDszANoiBgfzLoXYUlERi73tY1ftLNCvk3zWqd M97xKOGAEKgg6k6NpIPTubXrISqe5nuL64EeUp9Ci14PnYnbEeAC/Mbj X-Received: by 2002:a05:6820:c5d0:20b0:71a:f794:49b5 with SMTP id 006d021491bc7-71b395c1759mr2894179eaf.89.1795233344780; Fri, 20 Nov 2026 19:55:44 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:43 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 02/12] media: tw9900: release the device when stopping the stream fails Date: Fri, 20 Nov 2026 21:55:13 -0600 Message-ID: <20261121035523.26764-3-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If writing the register to stop streaming fails, the error path dropped the runtime PM reference but left tw9900->streaming set. Then, s_stream(0) again would drop the reference again and unbalance the usage count. At that point any s_stream() is ignored because the driver thinks it's streaming. Always clear the streaming state and drop the reference, and only report the error. Fixes: 918b14a26b12 ("media: i2c: Introduce a driver for the Techwell TW990= 0 decoder") Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index e07924faa84b..e9d384039b50 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -303,14 +303,14 @@ static int tw9900_s_stream(struct v4l2_subdev *sd, in= t on) =20 ret =3D tw9900_write_reg(client, TW9900_REG_OUT_FMT_CTL, TW9900_REG_OUT_FMT_CTL_STANDBY); - if (ret) - goto err_unlock; =20 tw9900->streaming =3D on; =20 mutex_unlock(&tw9900->mutex); =20 pm_runtime_put(&client->dev); + + return ret; } =20 return 0; --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233350; b=MFnJTTYB9KZwXwfK9CWxXydjQPEmt4B/CELnto43VDWi12EeDCNnuFY8nM8ekHatRQMN uvgdQmvWqDw/C7xDVJr/60ZNq93FL3rTvTPCg4DdCsj1jyTEuEdt8sq4+jB6Fzb1mguBF KurS9ASrIHn9QDESAq0xHMeqX3Dmgwyt4p/6iKF7t1QIHzjBv6X46jD6wYrllQYE4tQ0c jOhCxQIiWKpN2deQitdgKOm3N8llYnoH4qd3O5jgTNfHbKINUsWl1ZKOa09VsbvGQrlZc 96vDezpmxhSfTAWcEXj5JaXl1d81XFlVuI0VilgqrmzpLavnOpsEIws5ywyJky3xN3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233350; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=tltDCiy5cuzdulawCOgmvyY3l0pK0A9bj1d/Pm/aQJI=; b=tEZFj3irOzLm5JWXZzRYtxqCTCRV3YNvCKoxbcPqwHs2dhi8MOkjQd1egOaSk4AR8Mx+ Y9Fts5w9j6Fp0mLJAKLAqv99xAokElShPHwA7DcSPfS9vw9XuWjIzbmRYXQTU607+c3Zr EACUlJj1D6wPkPRLFuCJyywwbfbXCMiOTkzOsLntre+kxbKNN+YncEFEAHARsLLiB3wTq hdEQE6bh1kaA0geUz2bb2eXcpVCEmWkfFMLOoVgCLCIotv0iua9OBrN6slWAb8M+U2M5t isY+usWE+1b/WVExXCWpRvB/5KN/vxOfsNfqcZVDInwXBPkvP84snJN4diNKM6PDYnw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=wnqpGEHY; arc=none smtp.remote-ip=209.85.161.44 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A51CE60A2624 for ; Sat, 21 Nov 2026 03:55:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A51CE60A2624 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=wnqpGEHY Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-6b1afdf5196so604977eaf.2 for ; Fri, 20 Nov 2026 19:55:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233346; x=1795838146; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tltDCiy5cuzdulawCOgmvyY3l0pK0A9bj1d/Pm/aQJI=; b=wnqpGEHYxtDroPc3PZWXnrqOoc2sCIXECDGkhs7RGu970lvBqHo2idlO8jAyuN3wMP T6pbZXgc1lmmOWlHm++3+4qVzYdwcnFxwj1nuAeA3L9W0VyEhZ3gHs0c8RhTrVCmCM2w WSd2PfA3CC0CHxF+5mcst8wNoWOb2tuAVk8QDhok+vwe8p2XMbQRGGLy6I69LeDPSaLj egSe3N7k1du/MKe6gIJjVMApLLqZv/6IkPkj2mXbGMtkhTgLwFrbq0kZcddhQgbpNOda URh0TkDIHf1RFExGlGGTM+5bUVbGPm70/BmY3EyrK9OUF6KUsDglwF9SPl4/Lk72f0zw eg8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233346; x=1795838146; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=tltDCiy5cuzdulawCOgmvyY3l0pK0A9bj1d/Pm/aQJI=; b=fKO7ALTdlkjT6Tqq5TFstEYcpNT5FFfvey2pms3TA9chl7KoYCY4+TXgXQkOHEelva wXDXUK+nP5hDhKsF0+maBoEnU5uYUydwKX6Dgbgg2P+2IAuh/Dpx+7E6dx7FGKeFxrNb bBxK8ByBW42Xh3KY/RukSarEL/hQfSW3CrdapHOq4JfqpOMoRW1Yi9f2IR411ijwsww6 8gNFOQDQolm7cMsgrHexTjA5gd4tGv3xz9KBhtaVbyYPSaVmCA8fVgKuqwnI8axhzKux /+zWLKcm7doQ7s9njosYZhekieuFrrEyuYnHcfSVTiu/SP60ZzjrimGzmT9drqRtZbTN rrxg== X-Gm-Message-State: APuU4Z+0DSHpHLjhvdVQZnK8E6hqtAliVYqVLhgBkhZJPuUMXrKMjQG3 MksayLUjVQtqBmXoXP93DJjrg8XeOjrh4ebN+POipUxYthDkpfL2H0o= X-Gm-Gg: AYBFou2POlOP9yyhSqlTwEDN3SxSfaJvzrOjQyI7t7vSN9CuWkBhubUsdh3ojUgoomk HKpelrC82iMu0tR5aswPBmN5QayH40tEPaZb0LoRo5/Y3FhQ0aTSA9Sgs7tBLfWiagmAH/B/4MV e2oSs5eKHkWAVrdr4AlbjLRKRMaN9I8QGEbALSHqbj404aghk7yPHrJV/paRCldlFTiQo7OBoMy Nw7Pk2Qf++690XBgPOh1AI84/oLcRt0aVBuu52ckkR/hIsH8DSh1PwIK/HGXT8SF1tv6uHxY+pu iAVOmrwVICUOpeEQwS3e4MzAfEGaW6qQTg0LZ/cPI2dVSWlEcepul+TXJ2+CZ6LZKqI6IqDPR4Z KTNGDKZ0g4xy/dLhe1ppE/zwq3Trnoo+UZmr/8nahTO3AyyyeoQbYS7eVM+RCPn7TJVm4EpJk/e +qm+K/hgp9CvxYL5fNU/Z5e72pRd/V9UrYXiJOw2FOegLic3tiiTkEIBT565FXMl6YPBcGd8Mlt MJu0p78lN5tsKLmWq7DGERJ8yJzBsl/M6yYVMt/x7dts1zZtxUG2ejwTgck98vHtz3m9pwLcIec CWQ242/jgv9F2/EUfPxNm4p+CLLq2DYhwog00ktQfM/4cnYH52WncAp1 X-Received: by 2002:a05:6820:2488:b0:70a:347e:4e9f with SMTP id 006d021491bc7-71b3952cb48mr3400899eaf.59.1795233346574; Fri, 20 Nov 2026 19:55:46 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:45 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 03/12] media: tw9900: accept the TW9910 chip ID Date: Fri, 20 Nov 2026 21:55:14 -0600 Message-ID: <20261121035523.26764-4-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow tw9900.c to accept the tw9910's chip ID and revision fields as well as the tw9900's. The tw9910 is a superset of the tw9900 for the functionality used by the tw9900 driver, and USB3HDCAP/XCAPTURE-1 capture devices can have either chip. Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index e9d384039b50..df8c7b1f3450 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -23,6 +23,8 @@ #include =20 #define TW9900_REG_CHIP_ID 0x00 +#define TW9900_REG_CHIP_ID_ID_MASK GENMASK(7, 3) +#define TW9900_REG_CHIP_ID_REV_MASK GENMASK(2, 0) #define TW9900_REG_CHIP_STATUS 0x01 #define TW9900_REG_CHIP_STATUS_VDLOSS BIT(7) #define TW9900_REG_CHIP_STATUS_HLOCK BIT(6) @@ -51,6 +53,7 @@ #define TW9900_REG_VVBI 0x55 =20 #define TW9900_CHIP_ID 0x00 +#define TW9910_CHIP_ID 0x0B #define TW9900_STD_NTSC_M 0 #define TW9900_STD_PAL_BDGHI 1 #define TW9900_STD_AUTO 7 @@ -574,6 +577,7 @@ static int tw9900_check_id(struct tw9900 *tw9900, struct i2c_client *client) { struct device *dev =3D &tw9900->client->dev; + u8 id, rev; int ret; =20 ret =3D pm_runtime_resume_and_get(&tw9900->client->dev); @@ -581,7 +585,7 @@ static int tw9900_check_id(struct tw9900 *tw9900, return ret; =20 mutex_lock(&tw9900->mutex); - ret =3D tw9900_read_reg(client, TW9900_CHIP_ID); + ret =3D tw9900_read_reg(client, TW9900_REG_CHIP_ID); mutex_unlock(&tw9900->mutex); =20 pm_runtime_put(&tw9900->client->dev); @@ -589,11 +593,17 @@ static int tw9900_check_id(struct tw9900 *tw9900, if (ret < 0) return ret; =20 - if (ret !=3D TW9900_CHIP_ID) { - dev_err(dev, "Unexpected decoder id %#x\n", ret); + id =3D FIELD_GET(TW9900_REG_CHIP_ID_ID_MASK, ret); + rev =3D FIELD_GET(TW9900_REG_CHIP_ID_REV_MASK, ret); + + if (id !=3D TW9900_CHIP_ID && id !=3D TW9910_CHIP_ID) { + dev_err(dev, "Unexpected decoder id %#x (rev %u)\n", id, rev); return -ENODEV; } =20 + dev_dbg(dev, "Found %s revision %u\n", + id =3D=3D TW9910_CHIP_ID ? "TW9910" : "TW9900", rev); + return 0; } =20 --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233352; b=J6RwKqBEilrD6RLsZabEmhuAKk8aUvsajJQuimrJ9vy5+weWU5u1gyJTGQR4bWXPpZNv PmFx4BxNkP4CKiKcXxISAvxQa5nt+7Pb5Ais0swsAFLnvxPMIIe+6hgccFpPnqY21bd+f 2YzzdTYH75jsWVjsc69n0cpvMriDnF1fAb4BvBpsF4i5+hANaSoY9n4d5mk9KrIeKeYa3 x1sAfvkeK/HzouqM2hHZk/LOHbS9MNVe8CaJXuL2YrOYj3wuz/YZazgAxDYgtsvzloYcP 34sPkeciHE/vpPBv/p6IdKzpVWtMXBaJixsgP/O0dZYY5dQdFmGPiqMMr73XqU60yQQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233352; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=f26y5jc9tp63kZFVuaY6BdCWgQ3qpYUmHrk3jnvaA64=; b=CXDs5+q1I5Tx+UD394JxWjpsf91efE/iNuJDJEpe3NUa18h1KxsJ+ChRyAKk7/dqQ7au LSa8aBFHWHj0yxkFWgkzeE8tejIB0oApxBX0sPn2uaVHI2j7XDJbhfGPro+MmEbEHnQAN /XqxanPGTHt/jD0mdC4kdmJ4zuIaYjFuC9qD7trVFrZ9aTQja4F35aSQR4gOLOmnXkece BhRPRPIzwgZkCJrvX5VcPAzxMP67nzIojcT5p3AEWmQQwkbsIEN9S8JgaXzW80ZwNnwQZ bIy9qt7F4fIc/cRdcPYuKbZ4qO8sIaAE22CxhQGS15DtcrWwRndNQatrfgyK5/D0vEg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="W7/r/Wjv"; arc=none smtp.remote-ip=209.85.161.47 Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80E4F60A2638 for ; Sat, 21 Nov 2026 03:55:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 80E4F60A2638 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=W7/r/Wjv Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-717080a2d50so1455239eaf.0 for ; Fri, 20 Nov 2026 19:55:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233348; x=1795838148; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=f26y5jc9tp63kZFVuaY6BdCWgQ3qpYUmHrk3jnvaA64=; b=W7/r/Wjv56raxiWAGvFLwcwL9omFwPYkw8pdRgz3zyIPHpaNXJhNycB6BZyrpWa3Oa P8w/NXnVnc0okfD9WwEgF+wXDlZi4Sr4yp43lZMQHkRWnRkjxCkNAshXqZCzVzbxEo4r W2bx78Ec0gWNwUmfjUptsizWqOv9ijsvRZfDp5d94KQFubfixi4tvJKpnZIV6tRE3RY6 uvdEK8lD1fJWQnK2zUl4ZQZ8xwxeVmjDgDuD/spX8IZMpMcTDtXZXhsOUq/he+Sd3HIO 0s9eS829kVGj0cKytSOMN1dw0SywVdWSu+b/iiWbDqvJWJNNQ6917r8fWC9KAwq4vPwf SJnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233348; x=1795838148; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=f26y5jc9tp63kZFVuaY6BdCWgQ3qpYUmHrk3jnvaA64=; b=XrAIrWjz3YyFdFocEZ/2TTdK7TzP1Tx2K6J0ZFD6Xvj6hGTquVdv7S9wx59a3R9yk0 IFC56ys+ku64qmK3Sl5pFAT4B5NvRTpYuOb/6k9LnqY/gfX7VkDTt/IX7ebwJm0wrOkF KuL3uMl7MZMv5H0Wd49GGaCJnS/xmuYJGNTwi2HyLkYFcrsVDyMn37dh34UyFZf/eT5f bLbz9ksR3Bl2M6eABSFu/Y4+qX6cMT8bKVpIo+tRIeYfK2gCMK4U0puq7SUXMWOKupzw KZXEPsIntcn2k8z6C6IEHWiWx310j8AKPQHmjGteFc8aG2J97DFVX5KXkTiESDDg10/s 13Ig== X-Gm-Message-State: APuU4Z/7l6xRsqTLo51o3WVkp/4Hx1ELCVcgCcGVHAXStv78nqtn21tO xidvvkL+aeIwFymDsPvYL64WNE4CT0+9xUE1cb1iRpNCmVsh/s5Vq2M= X-Gm-Gg: AYBFou2ac56myJ9oQQGnhQJhDG33QE/Zyzh7GjZEFQc/5SaMZVzW1Ul04/SHgMjCV+o Gm1wzY18aVQvS/hhBEe8RzjumD9yN0oUETDl7mi0g+1Tv1jzJpfL/a/K+26xPA5YZAErSiRvID4 KUMHkgWdAlYlmLmJF5TqIXhk9KpgME+LgrsZXJQrRwJ0XGZFtp1Qn16J1HsIYVhB8TmhWtDu/t0 P7GMV7k0f48YBSjNebKhtzVao6UhvTzuCSPV32SLVzMRCapM8QvHlYx3TBRbfCeQj0crcIQ3hrN CMSCF7t12/4wZ37N4UZUZaixWzopn53/qbrtrOX4CNB7BeLD60aN0I8dFc2XacQrvMP1QidUF9u iuEe0XPNAiO/c+DgcwEKDDRn4cvbSOwGKXtkfIV+IAY3VwuMq21fPPOvxz2T5i3iTsOxJNOXXEl fE6/WFb84TU32ADJcN210DeZAvXBdgMJyx5o8kxq+PQwAFfCbquB3qNy1L/XsdYU+7SL9ia4uZR dXcB0flGW3UxWZvacDDwVb/LTgsNS3jCnmJxzn+W6YqaQ1h4UhWgnFE2H5jvxtt2gWU5i0iHfh2 UeY1EeIy6u6FZcaRWZo0TN5jvHylTdVuHyHIYCgo5cbDu9D//DLnGxtU X-Received: by 2002:a05:6820:1607:b0:716:bbe7:1ca2 with SMTP id 006d021491bc7-71b39447221mr4247611eaf.26.1795233348393; Fri, 20 Nov 2026 19:55:48 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:47 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 04/12] media: tw9900: add saturation, hue and sharpness controls Date: Fri, 20 Nov 2026 21:55:15 -0600 Message-ID: <20261121035523.26764-5-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add additional V4L2 controls for the tw9900 that modify the appropriate registers for saturation, hue, and sharpness, sourced from the datasheet. Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index df8c7b1f3450..42fc2afbbc77 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -41,6 +41,11 @@ #define TW9900_REG_CNTRL1 0x0C #define TW9900_REG_BRIGHT_CTL 0x10 #define TW9900_REG_CONTRAST_CTL 0x11 +#define TW9900_REG_SHARPNESS 0x12 +#define TW9900_REG_SHARPNESS_MASK GENMASK(3, 0) +#define TW9900_REG_SAT_U 0x13 +#define TW9900_REG_SAT_V 0x14 +#define TW9900_REG_HUE 0x15 #define TW9900_REG_VBI_CNTL 0x19 #define TW9900_REG_ANAL_CTL_II 0x1A #define TW9900_REG_OUT_CTRL_II 0x1B @@ -252,6 +257,26 @@ static int tw9900_s_ctrl(struct v4l2_ctrl *ctrl) ret =3D tw9900_write_reg(tw9900->client, TW9900_REG_CONTRAST_CTL, (u8)ctrl->val); break; + case V4L2_CID_SATURATION: + ret =3D tw9900_write_reg(tw9900->client, TW9900_REG_SAT_U, + (u8)ctrl->val); + if (ret) + break; + ret =3D tw9900_write_reg(tw9900->client, TW9900_REG_SAT_V, + (u8)ctrl->val); + break; + case V4L2_CID_HUE: + ret =3D tw9900_write_reg(tw9900->client, TW9900_REG_HUE, + (u8)ctrl->val); + break; + case V4L2_CID_SHARPNESS: + ret =3D tw9900_read_reg(tw9900->client, TW9900_REG_SHARPNESS); + if (ret < 0) + break; + ret =3D tw9900_write_reg(tw9900->client, TW9900_REG_SHARPNESS, + (ret & ~TW9900_REG_SHARPNESS_MASK) | + ctrl->val); + break; default: ret =3D -EINVAL; break; @@ -701,7 +726,7 @@ static int tw9900_probe(struct i2c_client *client) =20 hdl =3D &tw9900->hdl; =20 - ret =3D v4l2_ctrl_handler_init(hdl, 2); + ret =3D v4l2_ctrl_handler_init(hdl, 5); if (ret) goto err_destory_mutex; =20 @@ -711,6 +736,12 @@ static int tw9900_probe(struct i2c_client *client) -128, 127, 1, 0); v4l2_ctrl_new_std(hdl, &tw9900_ctrl_ops, V4L2_CID_CONTRAST, 0, 255, 1, 0x60); + v4l2_ctrl_new_std(hdl, &tw9900_ctrl_ops, V4L2_CID_SATURATION, + 0, 255, 1, 0x80); + v4l2_ctrl_new_std(hdl, &tw9900_ctrl_ops, V4L2_CID_HUE, + -128, 127, 1, 0); + v4l2_ctrl_new_std(hdl, &tw9900_ctrl_ops, V4L2_CID_SHARPNESS, + 0, 15, 1, 1); =20 tw9900->subdev.ctrl_handler =3D hdl; if (hdl->error) { --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233353; b=anGXkqQpQwbQoigw+WV8F0NBnN3dY+Su7NznYinRcM6TGXsR5nUHEg/JQrX81wDnERED aUnrrOTJXUDUhWfSThGPI5AAtjIC9Bh6ZYRBqPFWrsYRtAGZwMkWsOBXYFP3/EaO6JHX2 uvLjRL7SlplfxOHdRlaEyuIVZ9FPBdByB6SkPlWStKDU3Byok1/sGD8KeDzpUYGqR1l8/ MXA5ilX75d0WdZaXolP4IRebS6qtqDM5vdu57cQJr7g/3/k6GTIliB6Q+w1dS9U8NWwcu xje9AOgM2RLMfIaoDPpK3OqPD+S/+zuTZnRxv3Ru96GjIhPGqkzWd16DVBkFnzBXSMw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233353; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=VUr1L8X42Bzo0eJziGn2ZVZc2zLBEwew7i47qtMtnRA=; b=ilYn5fxgL89RdM+jZcdJV1EtdmONbrBjQRzaUUoJooWm52J8wsJSYvX2MiMADL5D6FPa EkMHNchk6709Lr38AKiXkUIZJSLk1JhhEtGS7lwXhLkg69oDSeII+9VAzJgn8kD6GufnT lKqcDuxftTegyz8m1fSk1TSYbMBe4mIRq3XmCegfg24XBh4N707lj0ICLuKR86Q7Gddzd RG4O7Q3ytj1FMqlokLwLvZ9DkdlYB5z7/hnKWjyUgfpU9YX7nLbZZBvggHfg1Sra2odqz OdpSmGVJlXVwbf5Q8igB5XDhjQlSjgQ9/RjUeB18gvRmsL6/6JDCAautk1SvFIRSGCg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=XQv8ONvL; arc=none smtp.remote-ip=209.85.161.52 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BE83606A6A5 for ; Sat, 21 Nov 2026 03:55:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2BE83606A6A5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=XQv8ONvL Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-716c0217635so1945104eaf.0 for ; Fri, 20 Nov 2026 19:55:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233351; x=1795838151; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=VUr1L8X42Bzo0eJziGn2ZVZc2zLBEwew7i47qtMtnRA=; b=XQv8ONvLZqRsaYpXcQGGBWQI/DnYZuuUCu/dTx4AvokqjkdNltxt6h6yw0HVz29w3T 0oS4R6ecdM49/IAbDF1mgNvDBKwnbLW5FGjoGFO4aFfTduMwWnWcpbPCF0LcUpswbhZA r1MpR4Y3CusBE4fu1KqqIgLkCZHQyTt2GYvMCPgUPaIeG2CW+BTFww8D3wmb3gH6F81A eMxOpW5XiCAdgPNKp+Z6IbaD0+T1CQBKBP03aTf9px1+cepQGuLsU22sUy8fV98exrm1 B3FLCKqsymlU4jhoqeBbSHAjklIXcDoogdTPcNWdadCg5uB0i4hFu2SdTdxs5Hz8zsIg 3SRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233351; x=1795838151; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=VUr1L8X42Bzo0eJziGn2ZVZc2zLBEwew7i47qtMtnRA=; b=t9YmlmSQ7LvM0bzA5BSUZtgyEWZAheKs+vL5rfK7vv8GW1DGiQJdIHJfiUMSYcFk34 Ecnpo8HDHnWtwYSW8uYIs6ONj6WjFx4Jm7MvImTUbnLZADFLAHQvT3BsrvgP4zg12vtM C4xpKzYIZj1D47XJjolCQIRzoOCj/wluxlt17tdijALQm13mpkxREk4QgBanA5ru7Fva 2pExA1Ce2q2lEVaJgxLCHZknnYGr/VryDqQOrKMZnaLFXnMACxOKtiioFDYoQjOTTQk5 Ql6Ob3CVhwMSAVRId2Jj9oGk7o97f+r/SdIVDNMSoQI5EoGkt4p6oPbkBEbok/Vas8PW Xmiw== X-Gm-Message-State: APuU4Z/4GB7n7lNSA1ZISYoCm2xE3TrZTvgD5W0jHa55VWro54YjnL+A +XHOF7Z8lyPvemZKBvKMFgJoWuOZZjdfOsN5k/uS0oAgXAoz56JVmog= X-Gm-Gg: AYBFou1e8HsAMIqLa3pcwDrUBAfixsIHHo8lj3xb0RmjYSMqtFzQB5MpTZPADbpz3xI OMtcbsH0/IS+jiY5ZmkGQVTR4GX1l7f3UdIG8K7zy3/IPHAO/YST29vkFtQtAiAXTo9vL/rgmlj eT0tiDM69YNAgQ33j3+6dPLeY577jDwm8P0dPN4jNPLwmhUwRzRLsmKPkOs1WBINwzlHglEZ9h7 yGwJi0R6IEfrb8aNfvrXSzLwouvQbD94EIN8koFNm0sDWpnRtwGQJ4+ec3eDFmP45vJQiVc6k7+ t9G7A0kMblau/b8FeBQty/s+25QgAWzz2xSGXYTiTaYKkUZLoYzkXawJ8YEUtF0hiNQvk4e/CjU M+lkaF3wDzs38wLeFPtx/fKmOaC2s/xhwOkfX5hq4CTX5Kgv4mGyZrZcbr5B4wDkBzswgXRN+uF A3CdexsoVsGGYoIaECpX+QsvSyU7oywMwqIiRqggBtRherbGmprcU7LLlW4ec1cRDXDmENkox/h PBSoWoZPgQ7VR3vW/+PXGzcbdfKO0RS0UPnLYdd+4D9E61UuYUjYUN1GsSxpI1GAwkoEwdm2XuJ 7n5q4ne2ZKjGXduSSvedShNbcX7alalb5Npw1CFrv1iog3yj6OFjVFIS X-Received: by 2002:a05:6820:c2d2:20b0:71a:e932:868e with SMTP id 006d021491bc7-71b392948d1mr2257944eaf.5.1795233351000; Fri, 20 Nov 2026 19:55:51 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:49 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 05/12] media: tw9900: program all mode registers in the PAL table Date: Fri, 20 Nov 2026 21:55:16 -0600 Message-ID: <20261121035523.26764-6-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tw9900_ntsc_regs set VDELAY_LO, VACTIVE_LO, CROP_HI, HACTIVE_LO and VBI_CNTL, but tw9900_pal_regs only set the standard register. PAL capture therefore relied on a runtime resume having reset the chip and rewritten the init table since the last NTSC stream. Make the PAL table write the same registers as the NTSC table. Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index 42fc2afbbc77..6309d9eda243 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -112,6 +112,12 @@ static const struct regval tw9900_init_regs[] =3D { }; =20 static const struct regval tw9900_pal_regs[] =3D { + { TW9900_REG_OUT_FMT_CTL, 0xA7 }, + { TW9900_REG_VDELAY_LO, 0x19 }, + { TW9900_REG_VACTIVE_LO, 0xF0 }, + { TW9900_REG_CROP_HI, 0x02 }, + { TW9900_REG_HACTIVE_LO, 0xD0 }, + { TW9900_REG_VBI_CNTL, 0x58 }, { TW9900_REG_STD, 0x01 }, }; =20 --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233360; b=VOcGK7HJagHkZaz+nKX8gu4QAuJUak73ZBAXRss4O4rUg8IIPw/reVGf9gqrmMAMancR m5YCzUi7han9Hl7OdOZnpoCo6HIgKsCzj7bP7cvHzD2AWgLfe+g+lIDK1+v2wcJLmC2+v 0Ldto16+2F9lRx4uEhmZIsZ9hncWV/1Dwxyi8mouYQeuem1x8UFNJT3VByegjuhE1AGhr DNma8RrLU9g+qWsTi+eeDBrXwAppsfWwADUTU9WXrcUjHvmNJmbQwdvNyHv3At5Re+f2Q lB55LZ7h5jCc+KWzfagCJi1SQN2l/laYfDE6Fg//yQISnUPF3qaj06x61Y4ma5H+cow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233360; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=jRUECku9MtNUtuZiZnRtB0skHYIdA8uwyD4cqahMADo=; b=i5oqG4fN9wfZrVg9NyPzm3Gp73ib7nO860iqAmCwtVpyiXYO6A+25hs44pxdczVRWrAR ox5nf8vRacZZvgdo5CYVHfQ54ZAsC5PwMhfkhqCJm55zRQozlMK0AIpf6wRYRyXecHJVt 6I5PC0PNanT/oWtxc6gpXQ7RLOoTG9L9J7n1aqqTbDeAqTjoQ4BZOevTyGCSf7+j/Nemv X5eGk84aygz+Wa/AEyuVO8YA34F0S6heupUz4mYPaKxDI239p/kWz7nquWC2mXKHdmggw F4upWr63dSRv5rXAIDCStWuNjMEGq3Tv5DWOuBH+2BNShcg1EncTr07oYiwTWeO+udg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=QgL4PwRw; arc=none smtp.remote-ip=209.85.161.41 Received: from mail-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B020860A3812 for ; Sat, 21 Nov 2026 03:55:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B020860A3812 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=QgL4PwRw Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-71b30ed75a1so973819eaf.0 for ; Fri, 20 Nov 2026 19:55:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233353; x=1795838153; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jRUECku9MtNUtuZiZnRtB0skHYIdA8uwyD4cqahMADo=; b=QgL4PwRwcRxr+9m7sI6/MUDwX3W+V/RKxXrAUvK25MfG/WP69Am1BH5KlMvxTbfYRD qHpVk0DCQnNEblxwl1KHEF9vE08qoZwOv7FgMiMgHXDAx7cqcmI/LrFQQ3higzRh8QoM Rxz3i3raL95e5FkcoMLE49APrjeOA7mCQ4u35yZGLvqmIV6Ung7H4weoPWLG60JQTRsK knYR065jJobM3+SQsKaUi6Wp+cbR6HG7jQgFTFo4J6teAeJ1okGeF0nn/fN2w9APDHer 4uFE1ouf4IOH5NygopYQf5NEkEAM4JBNeOpJZwZUcap7pNKVlX1MfWM1Utks7EpNfU+y 7lUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233353; x=1795838153; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jRUECku9MtNUtuZiZnRtB0skHYIdA8uwyD4cqahMADo=; b=riu6uyOPDO18xCUhh+D2zSH5qfyxHBqJ8gmJ7Il12o5xpbuTg+Nz2EiCveucPUEb4w sdqUwL5bVDE4i1+DJUg1KT0PVT/rRctM+KorpgqMwavz59JhqmqyPGSz7eYM3avFZut2 vK/35kKEV6AiyL+PVE/m/UxJu8DKl1RFFrd3R4/uHDknrYG5XQKJOf1KSewYbK21fMBF pwD4z6710T9rlJndOv4XaZlJi1qg4/jQyqGKDi40dF36ETxhl9yE5zaXJ20d7BNu76t/ dhlFcDHA8UH84cbjWYOecad/npB8ndy0PzsfmLnMnt6mkeS4X2bvGQoXtg/59lNK670w z6yA== X-Gm-Message-State: APuU4Z+G/xptA0ap+NTxhYVzqtp2zFw2kPKps53bfm0vaXsFXsUdjM5A KXsVQgYlEr8mYVZt/mjAyqf3f9Lt2uTlptYtBFOuVHtxvwRRkzzNnG0= X-Gm-Gg: AYBFou27LnVPU2M8XojEccc0qVGC9oCtSKgBReQmOJm6tC/HwsRjoHoFeoOpgckbIjL 6bWp+TSjMWET7/mD/M1RKnUsvIC7AMflzMZQ3IHDH3b2TOu8cxqyTngXfMkoWelahcWI02LHU8L AWoz15FfSTdjPd6gzFdXnGsLnLa4ma1XoGP02fvRiBsPKh3ehbrmpu1gAbRMP/lP/F2ZWFs+G52 6JGIBunKNny4mjnIYuydsvlJmBcgqjgzKFFpnIcS8TCsDK+cjzS27KoKbMI9L1AjSgzFSPBqtYM 8unMafD7oFLdiiz1+/3uZ/7iau7LPhCzTHvs/3VA2XIpOeaQuZz5I0Jm66o1BZ9VkoitIUV9xqR x88kXyNU8P8Ui/5kba/DoJ+r/a2wzm2OxWMslurKJ1+jly0l7KVVZT2YEe0QPTvI4eAwaBfUH5S ADVZhfSbjuBn6WXq1V86ICLmUjgEDNvRtWiCynkvUJ+KD0yPVKdpqFEFFCMegJB0oHv4DogmaQ/ ZvMd4i9H9scBJtRoPJ3f5bNrARbRCTS8G3TaizOVrZsNtn2dmTuzv4GCfvkiPdfJHiRz9UIYnjz H8SgC7YGOX95lyopIdIIwjyuqbYJdOyNk2aBMpu2o3vh7yLl92KgO/fK X-Received: by 2002:a05:6820:1ca7:b0:71a:d76b:fb39 with SMTP id 006d021491bc7-71b391e622fmr4923396eaf.2.1795233353562; Fri, 20 Nov 2026 19:55:53 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:52 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 06/12] media: tw9900: power off the device on remove if still active Date: Fri, 20 Nov 2026 21:55:17 -0600 Message-ID: <20261121035523.26764-7-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tw9900_remove() and the probe error path only call pm_runtime_disable(), which does not invoke the runtime suspend callback. If the device is active at that point, e.g. when a USB bridge is unplugged while streaming, the vdd regulator is left enabled and devres then puts it, which triggers: WARNING: drivers/regulator/core.c:2657 at regulator_put+0x61/0x70 ... release_nodes+0x46/0xd0 devres_release_group+0x11f/0x150 i2c_device_remove+0x41/0xb0 Suspend the device manually when it is not already suspended, then mark it suspended, following the usual pattern for sensor drivers. Fixes: 918b14a26b12 ("media: i2c: Introduce a driver for the Techwell TW990= 0 decoder") Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index 6309d9eda243..585509c1b41d 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -699,6 +699,14 @@ static int tw9900_runtime_suspend(struct device *dev) return 0; } =20 +static void tw9900_pm_cleanup(struct device *dev) +{ + pm_runtime_disable(dev); + if (!pm_runtime_status_suspended(dev)) + tw9900_runtime_suspend(dev); + pm_runtime_set_suspended(dev); +} + static int tw9900_probe(struct i2c_client *client) { struct device *dev =3D &client->dev; @@ -778,7 +786,7 @@ static int tw9900_probe(struct i2c_client *client) return 0; =20 err_disable_pm: - pm_runtime_disable(dev); + tw9900_pm_cleanup(dev); media_entity_cleanup(&tw9900->subdev.entity); err_free_handler: v4l2_ctrl_handler_free(hdl); @@ -797,7 +805,7 @@ static void tw9900_remove(struct i2c_client *client) media_entity_cleanup(&sd->entity); v4l2_ctrl_handler_free(sd->ctrl_handler); =20 - pm_runtime_disable(&client->dev); + tw9900_pm_cleanup(&client->dev); =20 mutex_destroy(&tw9900->mutex); } --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233361; b=08vY4dXZG2PtMjnGO95ZQCcVWpNTslXwCD9wjb9w2ocgJ3vJ4+i7fvSMRIdVAN65673B kykN+bqCo2QsGjuUx4KbmvzT0ijpKijZ8WX/hvHwAGk13Rhr30OIpohX82hxGAGkTpMKL fx49fuoH7Y9r9jngAx17ix4qi28A2mkKojnKv7O3BFRusGyRnL8rVGRVobSijbOF44MVr aC+iY3T3W5FDFBOmQP//Rz8/lOz9PQTxeQ3kTtTzV+p1YIkYCDQWoXEh2fWojEWWvpf8J ev280KZXverJ8GQmv5h7anx+wALtlkEPt2+VSj47hmihvggf/7VSILpq8sS4mzihTtA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233361; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=ZuGEAzWS3xcIcvSu/cuY37PG3g4bSwO/xoH2Fm6M/Eo=; b=04IOlDRihtfjsj26wPo6/O/uLs7YbwAaYJ9DVLS7rG100dzQdZSXvGmfnp6OIdMj9czr UQOf73CJpkBZ0k9gkMZcW9fzjq4OkpGG0TIJ7ohnVdc7tpQ03h2JVgcQMtJrijYl5gVz1 ZU/1IKuim8JCzPQzkOUtOuTFzPMFrd4b2GLkurliSpGudForj/CaqNRsj7GPFqOGcyiRw NSSoqqxEWPeEtkXVhe9FkRmXM3YH84xC1DA4Wm96j1esyoMv6Vqa0Tg7zXrUPKaQiqOvW 3oidgCObKd1Qz9P6FPpK4x/sil8LDX1Z5c35+WgjoUHcEJUzQkLKO3xtsFYaEXI9F8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="u4E/dKTK"; arc=none smtp.remote-ip=209.85.210.43 Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 519B860A2610 for ; Sat, 21 Nov 2026 03:55:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 519B860A2610 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=u4E/dKTK Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-8629ef48703so902303a34.3 for ; Fri, 20 Nov 2026 19:55:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233356; x=1795838156; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ZuGEAzWS3xcIcvSu/cuY37PG3g4bSwO/xoH2Fm6M/Eo=; b=u4E/dKTKtcwE3wAVyt2IeSMyuej0OsnaZnnOcwcl7gRRe6OseQ96iSIEbmoga3jc8y KpD5Y0GVkPD6LXLpGe0amAV1oNHKaZtNWMRMl4YgDfB3opcftNzDDDKtr7TCLY+RdtFv LEGM/bqSLaoULci5qbs4RJf9ipR6r1XGg01aJ5q18zmY0UyZxtUk6E++wXUM1vE4P4Uh Kkbri8opAFff5D17bJJgAWQs7VPdNA1YLb0T3yhdvAQyZOFlyRCkarP3RJFBF3rKumgT DZWsHmOSxITuATjA5J3AQyjxGR1aXnK0j7OWDmrDyt6Yf2TZBKtZyDEcZe4LxqlqJBIy jVsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233356; x=1795838156; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ZuGEAzWS3xcIcvSu/cuY37PG3g4bSwO/xoH2Fm6M/Eo=; b=h8u8Oo1SFIm2xqMNSKnl5E6lEZH9k9er9LaEk8QgDZ3wkhesnPnyRgYA8gecMFmYx/ YQpMZNyZEn/t3EKiQIEl+yIGu7AVSZ6anPI7R1BH9485BSG2IT/qmfrO/R1w3oQvRqLs qyRSb4IGV88L+l/Q4Kochpfm+oMHQdSbJ3/noQufStt1efKNw9ghSmobe6fdXO65ChQU bqQXwER2fMtV6EqUGC4GGtToerJnO/uTtjI1opH7L5jMICRaORIW1/LPde+z/iyVJIxO TrUBEk/+e9qFzV3prCG27OjoCRIq46mQFRfag0i8+UYZwXFUIKvMG6PWe149R9CLlQsn zkLQ== X-Gm-Message-State: APuU4Z/+/QYcmZqW+A3WNIGcGEv6XpDN4i2A4TrtA2MrGX9PW5hBdkVs qlBMgu+43ACtb8eof7RgBdvGNOo5rdIVhYacjxoA54O/pPncf5M8W+s= X-Gm-Gg: AYBFou27a7ApyQHFtkePNPLT0BkM9Twz725EL7glY9NG/S5l726V8Wm6LP9/LIS4cYN Escb649lvoSUJGSL7MtmDr4zW6GQSAUlL5Ej1YpFw0Mkq4T31vqFLqByBf/AuF/XbhDm3D4tK9K vfploMMhFdaEjwEBX9QBA1n1K7PlQRSmcYvr9BB0MtRKxn24apw0CFo1J2xweHoDLPxrmwsSkoV b3WZxsaX5H5TqSlHi5SKwL88vKZF3gLpfKn+4k9j2ZewOrI/IiB4nLYld86kjSFvCqKtvvgL7hL ZKivdXn3sDTGplwc/77RLGfCwoAB1xRrFTHUywBnHD8Oa6++ox2SCfBNV7bveGQ2RKyBhlpBRFE o10JfZkOZI+dZqRuLbkScUDYn6EmMMpiMqbNw8lTFWmakzPsZ5VyvrWeoDq027y20tEUxhCrezc TI2v2byE0PG/STqCJ6K/EnLeHdBN0BTkCjSVuN8vTqVxUFvjfEJDe42wJzbehmsHTtCAxGZTf2d ECE5Z9zXEGB88lo5yBz83LwdlxgHtfom0FquIjaW5P0lKTB+sTPxL/M2OxxZwODgnJTK2BluhWY nmgInBB+CYa2VqOl77DHDo4JvT1x1IFt3qrSqXjmB/nm8fgUb/tDnO82 X-Received: by 2002:a05:6820:80e:b0:71a:e1a9:4b74 with SMTP id 006d021491bc7-71b39616d74mr4329743eaf.72.1795233356164; Fri, 20 Nov 2026 19:55:56 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:54 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 07/12] media: tw9900: power down analog blocks in runtime suspend Date: Fri, 20 Nov 2026 21:55:18 -0600 Message-ID: <20261121035523.26764-8-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On boards without a controllable supply or reset GPIO, runtime suspend leaves the decoder fully powered. Set the CLK/Y/C power-down bits in ANALOG_CTL and the PLL power-down bit in ANAL_CTL_II before cutting power. Runtime resume rewrites both registers from the init table. Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index 585509c1b41d..b2b754e898c2 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -34,6 +34,9 @@ #define TW9900_REG_CKHY_HSDLY 0x04 #define TW9900_REG_OUT_CTRL_I 0x05 #define TW9900_REG_ANALOG_CTL 0x06 +#define TW9900_REG_ANALOG_CTL_CLK_PDN BIT(3) +#define TW9900_REG_ANALOG_CTL_Y_PDN BIT(2) +#define TW9900_REG_ANALOG_CTL_C_PDN BIT(1) #define TW9900_REG_CROP_HI 0x07 #define TW9900_REG_VDELAY_LO 0x08 #define TW9900_REG_VACTIVE_LO 0x09 @@ -48,6 +51,7 @@ #define TW9900_REG_HUE 0x15 #define TW9900_REG_VBI_CNTL 0x19 #define TW9900_REG_ANAL_CTL_II 0x1A +#define TW9900_REG_ANAL_CTL_II_PLL_PDN BIT(6) #define TW9900_REG_OUT_CTRL_II 0x1B #define TW9900_REG_STD 0x1C #define TW9900_REG_STD_AUTO_PROGRESS BIT(7) @@ -689,6 +693,13 @@ static int tw9900_runtime_suspend(struct device *dev) =20 mutex_lock(&tw9900->mutex); =20 + tw9900_write_reg(tw9900->client, TW9900_REG_ANALOG_CTL, + TW9900_REG_ANALOG_CTL_CLK_PDN | + TW9900_REG_ANALOG_CTL_Y_PDN | + TW9900_REG_ANALOG_CTL_C_PDN); + tw9900_write_reg(tw9900->client, TW9900_REG_ANAL_CTL_II, + TW9900_REG_ANAL_CTL_II_PLL_PDN); + if (tw9900->reset_gpio) gpiod_set_value_cansleep(tw9900->reset_gpio, 1); =20 --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233361; b=VzTjb0vkmAtRR3IK3pRuZpY/CgSWj2i7W5vl88r98UxdxL30PMl92pITx0nsE9fBMIP0 NRlKtly/ur5NS4dIrF03MItDVVKqiF38N7DlFv+5xd8Pf7B/lTTGoAZOmN9Gy3wfE1f95 Y4hx9cJIn5xG8GkZWxFrlenL3IKMm4bFaTJAgYooa41IdMxZCP99jPZQbkidYl5XEmKs5 nyRaPQIJt0UQZvjUotQaCU9qMfVoLzpIexpef2FdNWErt5bEUO76UBPges2FZAc3YqTTd TMsnGDvmlFdkOTX09vxI7Pkv1DpCaXLwSdTwajo3kc/icbnf519MWCe/9X8twMJWGyw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233361; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=DZHDPLNDBuON6r4+3mJFqDI1dwdMb/BwgwaZhh7obns=; b=1zOqcOTcyNvTZqZCiTLiLUvIZRTR4pkulxGNGS1gj2rU5PtR/CUMwLL/UITzE11hDZgl n9fytXL16xaveFxXgDDZgWIJa0QZ0/hgzB8U/5CjWDaH7J1GPGK+sXYWND43XmLTMB+RH I1AUsm9J67K/RDP9xeRtO27LTJCAJB4be+rpRhihglvEVZ8TraxLeee6iLLOZgdNPLv1W WJDKFNv5QBgyiZUXtnmsY82auc3//3FUBXBLVSN/4FVgGAafi8hzVRuJSFmqLSu43DGiP 7JcK6LG31qRRz8OpVTac2qDi9od3yThD1xepfyq1VqMDoJKVeBiBC8VKN759lXWpO4Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="Yqy8/pmK"; arc=none smtp.remote-ip=209.85.161.54 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05FE460A261C for ; Sat, 21 Nov 2026 03:55:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 05FE460A261C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Yqy8/pmK Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-703be215729so721358eaf.1 for ; Fri, 20 Nov 2026 19:55:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233359; x=1795838159; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DZHDPLNDBuON6r4+3mJFqDI1dwdMb/BwgwaZhh7obns=; b=Yqy8/pmK3Yqg507iSkvIyj2wvOgteWl4RxMpQrvEGH2B9FH5W+qUFsvQpPudb7RRvw a1+0qMj9lVYOyOJdSBbeTJCMiZh+hPsyxDvgU8jw9hMMWbpmkdSFe/lBc7qjKiygx/gm 5VicsIhWNhgFE7jaMwb5kwnkYbNh6q0jcXCtZyKm5pP7aT/JOS99yWpZzECz2oWDXUYV 8Q/aZiGcuodTOtxvsbEIc64F0T8daL0xq8dQMq7xcKvHbnxq0tNAh+Y9u+YnV+nuTPqC ePsSUhzyxMxoAD5r07r1XB6pmhwaDl998mToLE+Sbh10woiI2cJOq7wenID/tLVwi67t PO9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233359; x=1795838159; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=DZHDPLNDBuON6r4+3mJFqDI1dwdMb/BwgwaZhh7obns=; b=ossOTFuxjnR/ICnBlM1dTZhWCkFcQe0eMM2rXwg+we/mYh88CoDU9bMGCY7aEreZCE p1e8TnbOY4V72DO7CCeqmDnScEB+RLKxhQYrx1o8DwYChNMkZ0c3rL8Zf96V978gRqeV MbuWdc3kmKvP1u/iKkZc2UyXPL6E/7ygv2t7EcZ/aX7rc/ahI0sCAyuzyQl0Fx3e2NAA PA7rIuY/hKREFMPdqh5poxamEV0RCUeOfCkvQM57/nJKm9NaSaBbQgOuCMQ2JApcbhpj q+qOGb1e3aLYqraPmEXPsulxejfGKRBrDD/otDXOX7BcHUrbe5ITYBt6X90oWLoJJEua R/gg== X-Gm-Message-State: APuU4Z+gPcKGk/d6LTIDDqP4hj41cIqJwRPKUmaiTWGKEb7ruTis4Cii H2brs/Fq/3i5WhH95ddnzOH8SEwcGZ6ftKX+xosYIklPLCbLxStfkTY= X-Gm-Gg: AYBFou3ioHdFUEKT9VBrUOZI2Xha4+/SiWIqYMb1CUQYz0bBn11h++M1fQgkomv3hbF eamYshG+/DuYQ+5kW65lQw75Zpa0kRdsqwesuuodandRHfE8L884cK//RzXZ7GfNO3IqwFF12WD 2gVQKmY6E3OeJa/d3C7oDPYzUT90ZXto4Ty3hs2aKDcQZma0AUjfaOD2IkrwzrAgzis+Mh00tj4 LZ1uzqhPvsx9R+OoOUXkberz8I4o6IAMUyv0RP7eGaLMvbT6t8Qmffa6uxsi8PuGJTw+xaGo1i1 Rusn2UzTqzZVFVZyzBwp4lZUlhhfAM1GkT2iuQvyTqmRE8fDHYXKQRbnUiKqDcyRFvrWJ1xDIw9 RWUmZUKC+Lnop4p+pVN1XO6ZDOoR7zrp3AeeS66mWTyXh88poA+sFOdQYJWTQ6rc7JC6GhYZINJ q84SNDh32YhgJAraBV4/LZnak3pbFH4S2JVqlaz+VoQPJxUXCs5jHIF8ZiIR6WwRFHuuupaIIRQ g8idKzvcD/yMPk7+X7tNOC6Plcyf2nfxcRc6hjfqmLTNdyLXbQLmFu+wUR1Yfuw87PJz7VMvTqF Oa80FeFvG32ZxBqclLtXNOFGKp1oWTrd96j43kb9ac4Yt9A91/Gfucros4m4T1kzictfx9sjKkF xEA== X-Received: by 2002:a05:6820:a508:10b0:71b:16dd:fc2a with SMTP id 006d021491bc7-71b394d862emr3174040eaf.50.1795233358915; Fri, 20 Nov 2026 19:55:58 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:55:57 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 08/12] media: tw9900: use runtime PM autosuspend Date: Fri, 20 Nov 2026 21:55:19 -0600 Message-ID: <20261121035523.26764-9-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Each runtime resume enables the supply, toggles reset, rewrites the init table and waits 300 ms for the horizontal sync PLL to lock. Without autosuspend, a typical chain of s_routing, polling g_input_status, querystd, and s_stream would suspend and repower the device each operation, which adds ~350 ms per call and discards the signal lock. Use autosuspend with a 1sec delay so that closely spaced operations share a single power-up. Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index b2b754e898c2..b2c30e84acb8 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -68,6 +68,7 @@ #define TW9900_STD_AUTO 7 =20 #define TW9900_VIDEO_POLL_TRIES 20 +#define TW9900_AUTOSUSPEND_DELAY_MS 1000 =20 struct regval { u8 addr; @@ -346,7 +347,7 @@ static int tw9900_s_stream(struct v4l2_subdev *sd, int = on) =20 mutex_unlock(&tw9900->mutex); =20 - pm_runtime_put(&client->dev); + pm_runtime_put_autosuspend(&client->dev); =20 return ret; } @@ -355,7 +356,7 @@ static int tw9900_s_stream(struct v4l2_subdev *sd, int = on) =20 err_unlock: mutex_unlock(&tw9900->mutex); - pm_runtime_put(&client->dev); + pm_runtime_put_autosuspend(&client->dev); =20 return ret; } @@ -532,7 +533,7 @@ static int tw9900_querystd(struct v4l2_subdev *sd, v4l2= _std_id *std) =20 out_unlock: mutex_unlock(&tw9900->mutex); - pm_runtime_put(&tw9900->client->dev); + pm_runtime_put_autosuspend(&tw9900->client->dev); =20 return ret; } @@ -568,7 +569,7 @@ static int tw9900_g_input_status(struct v4l2_subdev *sd= , u32 *status) ret =3D tw9900_read_reg(tw9900->client, TW9900_REG_CHIP_STATUS); mutex_unlock(&tw9900->mutex); =20 - pm_runtime_put(&tw9900->client->dev); + pm_runtime_put_autosuspend(&tw9900->client->dev); =20 if (ret < 0) return ret; @@ -623,7 +624,7 @@ static int tw9900_check_id(struct tw9900 *tw9900, ret =3D tw9900_read_reg(client, TW9900_REG_CHIP_ID); mutex_unlock(&tw9900->mutex); =20 - pm_runtime_put(&tw9900->client->dev); + pm_runtime_put_autosuspend(&tw9900->client->dev); =20 if (ret < 0) return ret; @@ -712,6 +713,7 @@ static int tw9900_runtime_suspend(struct device *dev) =20 static void tw9900_pm_cleanup(struct device *dev) { + pm_runtime_dont_use_autosuspend(dev); pm_runtime_disable(dev); if (!pm_runtime_status_suspended(dev)) tw9900_runtime_suspend(dev); @@ -782,6 +784,8 @@ static int tw9900_probe(struct i2c_client *client) goto err_free_handler; =20 pm_runtime_set_suspended(dev); + pm_runtime_set_autosuspend_delay(dev, TW9900_AUTOSUSPEND_DELAY_MS); + pm_runtime_use_autosuspend(dev); pm_runtime_enable(dev); =20 ret =3D tw9900_check_id(tw9900, client); --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233366; b=gbNXaPxava9EeI+A4BsKsbZ8uk6tjHqlDjU0f5+m/7Bjs7fgO1Ut2HHmmPGMfZDIhCNc INPY4Ih7WU4M82D//0OYvkqrMKzdVtfOZgfN5/vc5OlAy7zTYsVcZOrg2WsQ+teq8sbc7 fprieshMn5ZXgSVs6WHkp+PgYlJ4YPwNZJ5x5rVgqZ6v5MfNyrCnr2FZVF4x2PIE5YuY7 /KdygN7pyu+N+7TZRTrgSX5TsQSXfkPBGZI4xEr/RJ4pczYzOvNS62eLoAZSBdQ/kMxZ6 JwAdTlix+4AQYuv6DgpJosCDETr7azLZzElvlAVRgETzpZGysLtlKkQ5PAX1gtLWeFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233366; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=7eqTUenxtATZH3mauJ4DkkeHABSU5674/8EXtC84wS8=; b=hbywRgipTmAUwUv488tSGyqQiLurZP6ANfgLq8RR84huKM2/kbrYY7lm9snS2iTM0+gS 8cV1aKaCVKIEAL/jVhbWB+TdmLvUcKMB9raGVya7MDjiNrcinCmTiyPGryF1bHffHW/8U dXcceql1xW50yvARrv4TCbOb4hZq7ufGOrAN/XrjPO8pDxu5ygE5nAAsKEJvxL01ANOEY c1ip9aIaSYN0L+j8M4bMyDyDbCIQwOOeIrDeS3t5s2eWT6d9VXmLD6OY1vC8ejlF04oBP l707usLHbLj0uUagF/pe9SJK+FbvUBVdtJS7g2dCzJsN5A7ml6vDgVrS8gD18L8FUKg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Ep5cz+92; arc=none smtp.remote-ip=209.85.161.52 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85FC560A262F for ; Sat, 21 Nov 2026 03:56:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 85FC560A262F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Ep5cz+92 Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-716d052dc23so738556eaf.1 for ; Fri, 20 Nov 2026 19:56:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233361; x=1795838161; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7eqTUenxtATZH3mauJ4DkkeHABSU5674/8EXtC84wS8=; b=Ep5cz+92TdvwmctIREgWYKEz8+yzbaqBf1DfJGNDp73GdETVeVHZ4rbpGQog4JA881 O2ODU/H0exd3q7n+sk4IUWIIVs+fecLqN5aM3d3Ynoahgn3ooaQqvqHguBAH6UM+LvLk qwIHDwTjPh40P0iZ0dQeUM02tyC4LhCwO4hok4bpX824sqeG7gjKb1Tbl2lcw1OBvSeX Cp2Hlp75pTdvKtFggMWIdod9wA9adp50vX1C4M1CzLeu2Ayqwj2tHNrqa4JEs1TJKtwg yhS8dYfdA6hgCBysudNQt0MJx/eWTTsfbo9NBJ7EHpk+J40fF00fVAgwDNaJjguaxRld G3Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233361; x=1795838161; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=7eqTUenxtATZH3mauJ4DkkeHABSU5674/8EXtC84wS8=; b=I9Fdt8KCQKWjy0zxlLpA1T1i0GJ8qXyrMmiAixiNSjOqkPF5UEOAUmkeHze1DtlSne TaQbnmv9RrXpFa6FCEMhQ1FqOCdpiXLsSdAjWq1/+iO6sWECRe+qPYhdaThqjHlP2qx4 CaaxucVyblqRmEeFDbtnieL5MhcgUWmw+f2xUTBaOr4Fqco0REmfJ0+n+KprK9ZuDY8+ rz/uoLIrb64v88USoij/xqfLAgzanAwwxSI9E2JW3id9mD3Zkv+efrgXA9CBuUkGW8Y9 auP/irLw/o4hFl3hKZve8SZLIw421muUgG4AuN1EG6LWn8nZx4+vh9jji1pupbJv8iPL TVYQ== X-Gm-Message-State: APuU4Z+7mAzJbs7XPS7VRFXM9ZfaYsnLKF2vIeGhKrzLzvnubEaPoQ+A TjAKZoB8vS9QVl/fJ+6oIIMP3vvxdvmq1aCFLc4LucLiveiPtuSluaI= X-Gm-Gg: AYBFou1ucHLJr6rGMyCf8rkRnV5FdMZhqacTNfFD6xbsZ59H0FhRuy+6h2SyXSEnvKi FgPdq5Upz4ofA0su0NTOwNOVI3cUjwFk8srgI22eqaXS7XK8FtSMHkcUxqQ0IpTxog5FVgasqj7 QK5/gf8vlgGMJUgwj1cqcscoAEIUU4oG/0mPf9P1t0+ddOfmL1wWOtu0XlyGUNv0dU4a1zoDgL5 FeuthzCZvsGEn6pErWKIWMGxCryobQrzNC1Vp4NpMxIsG60w6FZeZyLIxq9kvlixRJt5sT2iilD 3pMIcSQQ4PeJwuutZ7G/8gfHup2/NybOj65fz7G6/dSeYPhpC6/tkKQWNCY6zuKtAPHH69nxopU YtbZpuMRhhCho2yacRfTyu5VUhf2HvBTMj47HlxYtFmRrp7oQg1PE3OrOMRBvWiAx/rahpj+wWA tAapvPha8DLs6uukhz7rfb75K+CEMitIhwz9tVEugzAuWnVwfV8IGDRK0P9NT7BQRu1fXHgk5PR EyT/Iiddil8cIsH6a+/5Mpv0Zes6fAjvtk8RtioTTwODuO6W1es1F4X3RgvMRd2Hy/ZU5mnoTcL TgJARjKAudum/Ijae2DRNqrSIHgnJ9DUvP437aHgPkvZJkltyWnXxnD9 X-Received: by 2002:a05:6820:c44a:20b0:71a:e5de:2f06 with SMTP id 006d021491bc7-71b395bb2f7mr2870074eaf.89.1795233361439; Fri, 20 Nov 2026 19:56:01 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.55.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:56:00 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 09/12] media: tw9900: allow board-specific register tables via platform data Date: Fri, 20 Nov 2026 21:55:20 -0600 Message-ID: <20261121035523.26764-10-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow bridge drivers to supply initial configuration data for the tw9900. Behavior for the original configuration supported by the driver is unchanged. Fall back to the existing initialization data if none is supplied by the bridge device. Signed-off-by: Matthew Laux --- MAINTAINERS | 1 + drivers/media/i2c/tw9900.c | 49 ++++++++++++++++++++++++++------------ include/media/i2c/tw9900.h | 34 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 include/media/i2c/tw9900.h diff --git a/MAINTAINERS b/MAINTAINERS index c08aade46f61..102362c386fe 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27505,6 +27505,7 @@ M: Mehdi Djait L: linux-media@vger.kernel.org S: Maintained F: drivers/media/i2c/tw9900.c +F: include/media/i2c/tw9900.h =20 TECHWELL TW9910 VIDEO DECODER L: linux-media@vger.kernel.org diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index b2c30e84acb8..f4f07f060083 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -70,16 +71,11 @@ #define TW9900_VIDEO_POLL_TRIES 20 #define TW9900_AUTOSUSPEND_DELAY_MS 1000 =20 -struct regval { - u8 addr; - u8 val; -}; - struct tw9900_mode { u32 width; u32 height; u32 std; - const struct regval *reg_list; + const struct tw9900_regval *reg_list; int n_regs; }; =20 @@ -97,11 +93,12 @@ struct tw9900 { =20 bool streaming; const struct tw9900_mode *cur_mode; + const struct tw9900_platform_data *pdata; }; =20 #define to_tw9900(sd) container_of(sd, struct tw9900, subdev) =20 -static const struct regval tw9900_init_regs[] =3D { +static const struct tw9900_regval tw9900_init_regs[] =3D { { TW9900_REG_MISC_CTL_II, 0xE6 }, { TW9900_REG_MISSCNT, 0x24 }, { TW9900_REG_OUT_FMT_CTL, 0xA7 }, @@ -116,7 +113,7 @@ static const struct regval tw9900_init_regs[] =3D { { TW9900_REG_OUT_CTRL_I, 0x98 }, }; =20 -static const struct regval tw9900_pal_regs[] =3D { +static const struct tw9900_regval tw9900_pal_regs[] =3D { { TW9900_REG_OUT_FMT_CTL, 0xA7 }, { TW9900_REG_VDELAY_LO, 0x19 }, { TW9900_REG_VACTIVE_LO, 0xF0 }, @@ -126,7 +123,7 @@ static const struct regval tw9900_pal_regs[] =3D { { TW9900_REG_STD, 0x01 }, }; =20 -static const struct regval tw9900_ntsc_regs[] =3D { +static const struct tw9900_regval tw9900_ntsc_regs[] =3D { { TW9900_REG_OUT_FMT_CTL, 0xA4 }, { TW9900_REG_VDELAY_LO, 0x12 }, { TW9900_REG_VACTIVE_LO, 0xF0 }, @@ -165,7 +162,7 @@ static int tw9900_write_reg(struct i2c_client *client, = u8 reg, u8 val) } =20 static int tw9900_write_array(struct i2c_client *client, - const struct regval *regs, int n_regs) + const struct tw9900_regval *regs, int n_regs) { int i, ret =3D 0; =20 @@ -189,6 +186,23 @@ static int tw9900_read_reg(struct i2c_client *client, = u8 reg) return ret; } =20 +static int tw9900_write_mode(struct tw9900 *tw9900) +{ + const struct tw9900_platform_data *pdata =3D tw9900->pdata; + const struct tw9900_mode *mode =3D tw9900->cur_mode; + + if (!pdata) + return tw9900_write_array(tw9900->client, mode->reg_list, + mode->n_regs); + + if (mode->std & V4L2_STD_NTSC) + return tw9900_write_array(tw9900->client, pdata->ntsc_regs, + pdata->n_ntsc_regs); + + return tw9900_write_array(tw9900->client, pdata->pal_regs, + pdata->n_pal_regs); +} + static void tw9900_fill_fmt(const struct tw9900_mode *mode, struct v4l2_mbus_framefmt *fmt) { @@ -322,9 +336,7 @@ static int tw9900_s_stream(struct v4l2_subdev *sd, int = on) if (ret) goto err_unlock; =20 - ret =3D tw9900_write_array(tw9900->client, - tw9900->cur_mode->reg_list, - tw9900->cur_mode->n_regs); + ret =3D tw9900_write_mode(tw9900); if (ret) goto err_unlock; =20 @@ -668,8 +680,14 @@ static int tw9900_runtime_resume(struct device *dev) =20 usleep_range(1000, 2000); =20 - ret =3D tw9900_write_array(tw9900->client, tw9900_init_regs, - ARRAY_SIZE(tw9900_init_regs)); + if (tw9900->pdata) + ret =3D tw9900_write_array(tw9900->client, + tw9900->pdata->init_regs, + tw9900->pdata->n_init_regs); + else + ret =3D tw9900_write_array(tw9900->client, tw9900_init_regs, + ARRAY_SIZE(tw9900_init_regs)); + if (ret) { if (tw9900->reset_gpio) gpiod_set_value_cansleep(tw9900->reset_gpio, 1); @@ -733,6 +751,7 @@ static int tw9900_probe(struct i2c_client *client) =20 tw9900->client =3D client; tw9900->cur_mode =3D &supported_modes[0]; + tw9900->pdata =3D dev_get_platdata(dev); =20 tw9900->reset_gpio =3D devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); diff --git a/include/media/i2c/tw9900.h b/include/media/i2c/tw9900.h new file mode 100644 index 000000000000..11e3bfe38e2b --- /dev/null +++ b/include/media/i2c/tw9900.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Techwell TW9900 multi-standard video decoder + */ + +#ifndef _MEDIA_I2C_TW9900_H_ +#define _MEDIA_I2C_TW9900_H_ + +#include + +struct tw9900_regval { + u8 addr; + u8 val; +}; + +/** + * struct tw9900_platform_data - board-specific register setup + * @init_regs: written each time the decoder is powered up + * @n_init_regs: number of entries in @init_regs + * @ntsc_regs: written when streaming starts with a 525-line standard + * @n_ntsc_regs: number of entries in @ntsc_regs + * @pal_regs: written when streaming starts with a 625-line standard + * @n_pal_regs: number of entries in @pal_regs + */ +struct tw9900_platform_data { + const struct tw9900_regval *init_regs; + unsigned int n_init_regs; + const struct tw9900_regval *ntsc_regs; + unsigned int n_ntsc_regs; + const struct tw9900_regval *pal_regs; + unsigned int n_pal_regs; +}; + +#endif --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233370; b=D+tL4QH3pJWArn1C/4JaoGmKqv+wVbNzKS1F8ZFVY+KT0eBQtWeTUKiJr8uofcOb8rqJ j9d69EclEmXBEpsC1BG9UwGooZomM4AYwLLhBYZ3J/q7vqCyIiII1Zyx29SzGtSuWQvQp 2WhBgygp+xUP46JtKujTt6fwtZl4LVic5Ca91ji46W1GKWYc4UF0xnH0p50ecpaQ5D1at MEWLc3hMZA5zX0GyIiQIZ3qq0eL+9G+IDLRrfgNLdTwCg+ngKUGHtsWsWRZTHzpCP6cqc XnHscWmLG5Dk3WOGFRbVs0LIFXSvH2R3x4kWISfcpvVkuP72QKGxb9sCiIgKCixs3Rw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233370; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=JKQ9f3vNFqQ/p2IKdmnFvGUTbkyzoerNwSUU9f9lkho=; b=KCIPGE0WBUMt/OtTdbJ2TrFy2G4SLQK6XtpJB5x0ga+H7eRIlzfEOhIeYIo3aUfxncOh ru00E7JGr5K0K+15Aj3PNNDudSLkGyq2wWLAi6yx1Fr2d8w8bD+DuOlCU9UHw1yHTVB/x jrHObWXGrfNpfCgCzeIQHw0U1zfKO/NFB3popiGtlBJtQLW56lGkamK0df+YT+js9/2T1 O4SkEHOkAQZYd4BI1mzS3EduqXWsQLAVhDWzRnNggPMqLx5Cpvm5S/5T5tFQHQ3DoC8Im 8ObBGOUdwYlnOnuu5XAd8DE8BJkciYNj2SrJwoUJ3faq9QKaNNWlu8ifiq7gYVgXulg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rzIBXYVK; arc=none smtp.remote-ip=209.85.161.52 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B74C606A6AB for ; Sat, 21 Nov 2026 03:56:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2B74C606A6AB Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rzIBXYVK Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-6df7f544919so964985eaf.1 for ; Fri, 20 Nov 2026 19:56:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233364; x=1795838164; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=JKQ9f3vNFqQ/p2IKdmnFvGUTbkyzoerNwSUU9f9lkho=; b=rzIBXYVKzyx2bT/g7H6yV/0k17GfSmi3xJEEbzpShS8bvLiYExXd5tkSJvZnyfdRUT EDiXeYUF5FPpgYI1MZRTra+l4I9+xYd7aWd4n++ncb7UnJQjb4ZVvIY+brhpR4khkAeY 2pyH84RZtvYagmhXHht68aQcu4WTI8/uictiO7Yk09MO+Wuf+HJKg2doiw6Z0cDeXJY3 sqSkuzrwdpF4ViMAIVvOJ+re0EHIRS9dsTUO1GBAahznq9dQAqHbmjkndMm+dyJdMPgw 7z/jMWGLZrx/4IQQMLBpN+rKTvHyhFq93kW4OG3J6MSO5ltwOlurHas+3cDzdalMQS80 KHWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233364; x=1795838164; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=JKQ9f3vNFqQ/p2IKdmnFvGUTbkyzoerNwSUU9f9lkho=; b=G+q3f6QLWWv51zeBMaI2Au/3+dHdwwEbn5V447htx5pNfQoag+kGuFLlTOBCzdP+WN J5sWxoOKgJKrshYrCwre32W1QN0rFH5bGhtRIg5rPRMCpummEvV6xnxJ1zePjqFwU7Ez HdouGa9/W4P6TOxtbpChRDQUMJX8y6Cd8g+Wk7GQdLXqKmGodmhUYhOqCHSDptloU4au HMqbiln/YT3zs63aJK0RMsQ/jOK+KjhhuY/1PgCaxhPc6VyP10VKmd9N8CWH+bAha4Q7 z76s5DOK2ULnbl8aKmByhh3KLfCU9b+8yn27lW8MYAueDbCccYcudKP7hFP648h1D1s2 CLIA== X-Gm-Message-State: APuU4Z83J4qHZzUzoUzZRp/1i7D0eMW3RWaH64GD5oZgYK2+TgzgE7kP EI8HJE/uZXll7dJokc/nN+esT1B7Cjo38s/ShyRvo9VzwMQqM3zAqUDzF1XRaOgu6g== X-Gm-Gg: AYBFou1cgeJ3Sh0hPOj1KfXo2wPfo1F+X8cFWOY4C4SB6oyxShTli69Ck3fsvym/HbU U4RIs6hNadMfYI4r/ATuK6F+0Z0BlaE5lHc03D5xTQkTu+vIU4LPVarEE01rTk2VDqYs/a9c8kh 3m4x8djjp6oAbSi5w0Sh6/jQBqU7Yo9jO00RfOEFz0XtfoWguxEHjolRrvbU+H33NBPg/+WtIml z8oZRuLQRojUWTgI12pQXETQxpecLqBlhjNlDqTNIrOOvBYaE8tHprp4GAuKahb0vLAj/1DacV/ HfGqUS1QbywAy6njWPL7scVN4VEVf/fOQ1sqdwhR9lfar561+kneShPfIjyJMBAUIQz97x1rEJO ex3p3TZgrjJ+o4wD5/Dq5285hANDN7aDKJ5Ng2Tkzj0x6vimDRO2r5sXs87spvUMjBO3/yj7qil yniupS+53kEIcTboroquUjSlavmx1qWFlBjX1uhcp8ixwrBp9jt3/82A2/CT8xyIDm8wGPeh6gm riuh4hkdlJ46jNV/4kD8G6zypD+DRBnu5XWFVnbjzUJCy0mCb7RazVtJ/M2mZ4F1Ws9bMy98rH5 hxNIMlbS4XorZpR07tkF70mDsxHue5k1KU5xyEvbRKj5TPlbAl2+hJzixOuKp6/UWJb3jhl/XJD llg== X-Received: by 2002:a05:6820:c5cf:10b0:71a:f794:49ac with SMTP id 006d021491bc7-71b395bd407mr3249629eaf.89.1795233364075; Fri, 20 Nov 2026 19:56:04 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.56.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:56:02 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 10/12] media: tw9900: add S-Video input and progressive source detection Date: Fri, 20 Nov 2026 21:55:21 -0600 Message-ID: <20261121035523.26764-11-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for the S-Video Y/C input of the tw9900. Also, use the NINTL bit of the Chip Status II register to correctly report when a source is 240p/288p instead of 480i/576i. Signed-off-by: Matthew Laux --- drivers/media/i2c/tw9900.c | 117 +++++++++++++++++++++++++++++++++++-- include/media/i2c/tw9900.h | 4 ++ 2 files changed, 116 insertions(+), 5 deletions(-) diff --git a/drivers/media/i2c/tw9900.c b/drivers/media/i2c/tw9900.c index f4f07f060083..be939ce92c0d 100644 --- a/drivers/media/i2c/tw9900.c +++ b/drivers/media/i2c/tw9900.c @@ -29,12 +29,19 @@ #define TW9900_REG_CHIP_STATUS 0x01 #define TW9900_REG_CHIP_STATUS_VDLOSS BIT(7) #define TW9900_REG_CHIP_STATUS_HLOCK BIT(6) +#define TW9900_REG_CHIP_STATUS_VLOCK BIT(3) +#define TW9900_REG_INFORM 0x02 +#define TW9900_REG_INFORM_IFSEL_MASK GENMASK(5, 4) +#define TW9900_REG_INFORM_IFSEL_SVIDEO BIT(4) +#define TW9900_REG_INFORM_YSEL_MASK GENMASK(3, 2) +#define TW9900_REG_INFORM_YSEL_MUX1 BIT(2) #define TW9900_REG_OUT_FMT_CTL 0x03 #define TW9900_REG_OUT_FMT_CTL_STANDBY 0xA7 #define TW9900_REG_OUT_FMT_CTL_STREAMING 0xA0 #define TW9900_REG_CKHY_HSDLY 0x04 #define TW9900_REG_OUT_CTRL_I 0x05 #define TW9900_REG_ANALOG_CTL 0x06 +#define TW9900_REG_ANALOG_CTL_SRESET BIT(7) #define TW9900_REG_ANALOG_CTL_CLK_PDN BIT(3) #define TW9900_REG_ANALOG_CTL_Y_PDN BIT(2) #define TW9900_REG_ANALOG_CTL_C_PDN BIT(1) @@ -60,6 +67,8 @@ #define TW9900_REG_STDR 0x1D #define TW9900_REG_MISSCNT 0x26 #define TW9900_REG_MISC_CTL_II 0x2F +#define TW9900_REG_CHIP_STATUS_II 0x31 +#define TW9900_REG_CHIP_STATUS_II_NINTL BIT(3) #define TW9900_REG_VVBI 0x55 =20 #define TW9900_CHIP_ID 0x00 @@ -94,6 +103,12 @@ struct tw9900 { bool streaming; const struct tw9900_mode *cur_mode; const struct tw9900_platform_data *pdata; + + u32 input; + /* Set once s_routing has been called. */ + bool routed; + /* The source was 240p/288p as of the latest querystd. */ + bool progressive; }; =20 #define to_tw9900(sd) container_of(sd, struct tw9900, subdev) @@ -186,6 +201,39 @@ static int tw9900_read_reg(struct i2c_client *client, = u8 reg) return ret; } =20 +static int tw9900_apply_input(struct tw9900 *tw9900) +{ + int ret; + u8 val; + + lockdep_assert_held(&tw9900->mutex); + + ret =3D tw9900_read_reg(tw9900->client, TW9900_REG_INFORM); + if (ret < 0) + return ret; + + val =3D ret & ~(TW9900_REG_INFORM_IFSEL_MASK | + TW9900_REG_INFORM_YSEL_MASK); + if (tw9900->input =3D=3D TW9900_INPUT_SVIDEO) + val |=3D TW9900_REG_INFORM_IFSEL_SVIDEO | + TW9900_REG_INFORM_YSEL_MUX1; + + ret =3D tw9900_write_reg(tw9900->client, TW9900_REG_INFORM, val); + if (ret) + return ret; + + ret =3D tw9900_read_reg(tw9900->client, TW9900_REG_ANALOG_CTL); + if (ret < 0) + return ret; + + val =3D ret & ~(TW9900_REG_ANALOG_CTL_SRESET | + TW9900_REG_ANALOG_CTL_C_PDN); + if (tw9900->input =3D=3D TW9900_INPUT_COMPOSITE) + val |=3D TW9900_REG_ANALOG_CTL_C_PDN; + + return tw9900_write_reg(tw9900->client, TW9900_REG_ANALOG_CTL, val); +} + static int tw9900_write_mode(struct tw9900 *tw9900) { const struct tw9900_platform_data *pdata =3D tw9900->pdata; @@ -203,12 +251,14 @@ static int tw9900_write_mode(struct tw9900 *tw9900) pdata->n_pal_regs); } =20 -static void tw9900_fill_fmt(const struct tw9900_mode *mode, +static void tw9900_fill_fmt(struct tw9900 *tw9900, struct v4l2_mbus_framefmt *fmt) { + const struct tw9900_mode *mode =3D tw9900->cur_mode; + fmt->code =3D MEDIA_BUS_FMT_UYVY8_2X8; fmt->width =3D mode->width; - fmt->height =3D mode->height; + fmt->height =3D tw9900->progressive ? mode->height / 2 : mode->height; fmt->field =3D V4L2_FIELD_NONE; fmt->quantization =3D V4L2_QUANTIZATION_DEFAULT; fmt->colorspace =3D V4L2_COLORSPACE_SMPTE170M; @@ -224,7 +274,7 @@ static int tw9900_get_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mbus_fmt =3D &fmt->format; =20 mutex_lock(&tw9900->mutex); - tw9900_fill_fmt(tw9900->cur_mode, mbus_fmt); + tw9900_fill_fmt(tw9900, mbus_fmt); mutex_unlock(&tw9900->mutex); =20 return 0; @@ -245,7 +295,7 @@ static int tw9900_set_fmt(struct v4l2_subdev *sd, return -EBUSY; } =20 - tw9900_fill_fmt(tw9900->cur_mode, mbus_fmt); + tw9900_fill_fmt(tw9900, mbus_fmt); =20 mutex_unlock(&tw9900->mutex); =20 @@ -387,6 +437,42 @@ static int tw9900_subscribe_event(struct v4l2_subdev *= sd, } } =20 +static int tw9900_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, + u32 config) +{ + struct tw9900 *tw9900 =3D to_tw9900(sd); + int ret; + + if (input !=3D TW9900_INPUT_COMPOSITE && input !=3D TW9900_INPUT_SVIDEO) + return -EINVAL; + + mutex_lock(&tw9900->mutex); + + if (tw9900->streaming) { + mutex_unlock(&tw9900->mutex); + return -EBUSY; + } + + tw9900->input =3D input; + tw9900->routed =3D true; + /* A new input needs a new querystd to detect progressive sources. */ + tw9900->progressive =3D false; + + mutex_unlock(&tw9900->mutex); + + ret =3D pm_runtime_resume_and_get(&tw9900->client->dev); + if (ret < 0) + return ret; + + mutex_lock(&tw9900->mutex); + ret =3D tw9900_apply_input(tw9900); + mutex_unlock(&tw9900->mutex); + + pm_runtime_put_autosuspend(&tw9900->client->dev); + + return ret; +} + static int tw9900_s_std(struct v4l2_subdev *sd, v4l2_std_id std) { struct tw9900 *tw9900 =3D to_tw9900(sd); @@ -513,6 +599,9 @@ static int tw9900_querystd(struct v4l2_subdev *sd, v4l2= _std_id *std) return -EBUSY; } =20 + /* Drop the previous result; it is only set again on success. */ + tw9900->progressive =3D false; + mutex_unlock(&tw9900->mutex); =20 ret =3D pm_runtime_resume_and_get(&tw9900->client->dev); @@ -542,6 +631,15 @@ static int tw9900_querystd(struct v4l2_subdev *sd, v4l= 2_std_id *std) } =20 ret =3D tw9900_get_stream_std(tw9900, std); + if (ret || *std =3D=3D V4L2_STD_UNKNOWN) + goto out_unlock; + + ret =3D tw9900_read_reg(tw9900->client, TW9900_REG_CHIP_STATUS_II); + if (ret < 0) + goto out_unlock; + + tw9900->progressive =3D !!(ret & TW9900_REG_CHIP_STATUS_II_NINTL); + ret =3D 0; =20 out_unlock: mutex_unlock(&tw9900->mutex); @@ -586,7 +684,12 @@ static int tw9900_g_input_status(struct v4l2_subdev *s= d, u32 *status) if (ret < 0) return ret; =20 - *status =3D ret & TW9900_REG_CHIP_STATUS_HLOCK ? 0 : V4L2_IN_ST_NO_SIGNAL; + if (!(ret & TW9900_REG_CHIP_STATUS_HLOCK)) + *status =3D V4L2_IN_ST_NO_SIGNAL; + else if (!(ret & TW9900_REG_CHIP_STATUS_VLOCK)) + *status =3D V4L2_IN_ST_NO_V_LOCK; + else + *status =3D 0; =20 return 0; } @@ -597,6 +700,7 @@ static const struct v4l2_subdev_core_ops tw9900_core_op= s =3D { }; =20 static const struct v4l2_subdev_video_ops tw9900_video_ops =3D { + .s_routing =3D tw9900_s_routing, .s_std =3D tw9900_s_std, .g_std =3D tw9900_g_std, .querystd =3D tw9900_querystd, @@ -688,6 +792,9 @@ static int tw9900_runtime_resume(struct device *dev) ret =3D tw9900_write_array(tw9900->client, tw9900_init_regs, ARRAY_SIZE(tw9900_init_regs)); =20 + if (!ret && tw9900->routed) + ret =3D tw9900_apply_input(tw9900); + if (ret) { if (tw9900->reset_gpio) gpiod_set_value_cansleep(tw9900->reset_gpio, 1); diff --git a/include/media/i2c/tw9900.h b/include/media/i2c/tw9900.h index 11e3bfe38e2b..4de03873aeb4 100644 --- a/include/media/i2c/tw9900.h +++ b/include/media/i2c/tw9900.h @@ -8,6 +8,10 @@ =20 #include =20 +/* Inputs for the s_routing video op */ +#define TW9900_INPUT_COMPOSITE 0 /* composite on Mux0 */ +#define TW9900_INPUT_SVIDEO 1 /* Y on Mux1, C on the chroma input */ + struct tw9900_regval { u8 addr; u8 val; --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233372; b=Pl5yx1jwYR+GWsT8emtmz7iHl0m05alQpcGGni8BGtXXGS867puAhys4+efVCd/0q6ki mmf3E+vfuGkGXuDv0tuV5C5F0oA3VhQ0rpEjXYJBmIEfEcwHLYy9aaH/XmdDvdd8kCxGG waW3wP5uMH842p3Vgd5h16LufN43xNdXPLON/WvS0ducRMEMW4JybeOEYT2DRZZyQu+w2 Baf9U5pwqXIzQbuWungfhY668pFNoM7x+5kRpmvLST3Z+EUtkzQycrdFKrfLPpcuxjxgs 1bCMmDoB3oQhG3dktJCY0M+w740fXiF08eaWAN5CGcL/o/ZS9pQ8QMmwUzZG6pHPCSA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233372; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=eLZxih5z08b2fc+/M1y5EZmfTNGPdZhiTJAvtqxetN4=; b=NTQMt/cdCp7S/QGL722KRzQhxBGuM58i9yV4iQJyD3DVB//S7xd8DvGvXjVU1BVLia5d pkYdBY9la7FqXHOMWhxZ56mOmMYy7k3N8yTAn6E47JgGE2YcKx9lCEAo3ID9m3D8RTiw+ cEapNwo1wjx7UiuvCSdCGQqC0zpDF2eAND9dhx7Pt3bmmrbcWUpd8WHte2fQzDIkoA/1n loFoksCmr8Lh3bYfT8xTkQE23QZ3K2YgLOwb5cySsOhM6B5lYZuZquoK6wRx3Rhv0K0yW BJJ4kwjboIWRqJwrtyXpmHiH4UquqW2La6Mn48uDua4aJ4ebtKQXFXTTheVRzsSDFAQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=f1ixSw3R; arc=none smtp.remote-ip=209.85.161.44 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 04B1360A383E for ; Sat, 21 Nov 2026 03:56:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 04B1360A383E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=f1ixSw3R Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-71b45386310so541185eaf.3 for ; Fri, 20 Nov 2026 19:56:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233367; x=1795838167; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=eLZxih5z08b2fc+/M1y5EZmfTNGPdZhiTJAvtqxetN4=; b=f1ixSw3R1avXsaFg6QdNWcFVcV4FHmLJ0onhPPtxr7wrzzWpg83iY5DwKN87W79LfX 9tuGxtSkuQhy/rh0hNPhjauBlsN+BDH9iGUfRDzDI806VFDNdfxxB7uLARvuPvqV/Ycz P66MagtCP4s6ccQQb/wpawFDeKAtJu5cf4jH+LwVgdLU09HeGmEtJU3F8/7lQv4JjTMm UUB/O1JPiBS1zMxS9tqT77+hXwSiJhhuxTHqJGerpXRwany7IGEbVgAJKz6TcuMN0nnd CSTkwKjvM96NBrjRJCWdD8iz+dwAN4JXyk33nsaPFNMiJFYveXBWIv0AUPdZlogNpOia fWeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233367; x=1795838167; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=eLZxih5z08b2fc+/M1y5EZmfTNGPdZhiTJAvtqxetN4=; b=e+VEbUOcQlmj9cGkF/ZMS3re31zAw446TqktjoaN2oI5tl6H5cBTTws++cedDWDxIr sRuGEdc+XZzvvz7WoYa3weX7MjsLR2XR41B1DGJ3NYDHPAX8EQx0RPuCECC+l1rQzmiJ FwbqzwhsnVeCmIeAAk8wtFZugaX0TBj8EXVfnbfcJhKwwt7IHk33DUTEHON9tlJatG/c KJwqmGZx4Gunja2vh6V3vOWcMFzttT80VJrzXWOosz3KA7JU73b8gwZgnaHsm4jPGF5O DrFqZkPO77gL84rBjokdgmQp+D0YhettZfJ6udpJ4XRhxR9oUMDb+OuZCC75gQ4VCWNh p9CQ== X-Gm-Message-State: APuU4Z/azueYRfFxSS6dc4tSQTzRW5fhfN1M6eEeZbd4TJuyQdbM0gCZ 9M0jvXbhu54HEVV5ipROS8e8Yzw3tw2BzJE2HBmki+RZjdPUlZEh2YE= X-Gm-Gg: AYBFou0PTWkCJScgPQMN54sE16GWKhJmWXz3/f5EoQrl9Gi9ct9Tgmq+4ppSUhvxCWM p9gN35pD0iUC8aGLvYEVB7ixGkTtYmEW6QmzpnZm1sIzGtHDwesBw5INf7XMgdxXLCfUidNFhwp LMkLswgEvvCb/lJKq8fMz7JrQJ2Jf9aBIHmTybFAnDwNBj+CTVL36HpFGDU0wEc8hwth97LCiMD 4np2gk87D5ZDqgbGLKIYtQYsp8nYMkdOZ18h9yxWQJ3ab24wqyUfbDmbncc88kZNVHtGjoVyICh /m2tmwJquoeOlgmPA8tV9syMLGbanB1WF3kJicAXGfNBq4Lwd1sxaug2Z+0ajuZyA3PyZoIPnwi FzJ5lbpmd/P7FqgZ61JZB4Jo6Y5N6sASnuhLZtxf2RPIysOsV5ALDvzeRkSJ9X8v7PFxIXp3pXQ xSzB8dIGUsyiQBF9B9mLZkGeOu+2NoUeFbBhNuwQFSGEzb8b+s0xW2Td1NEd/BJyOFDcjDPmryE E55ECSbPRxD/BEbifCShVYnZ6zU+bHStbtDnWn6dWIqtKIN1NJ1SK9+FbeBGGbHSbIHvx79c82g ZI8Vd8tMQ6oow8C2jqREKiTf2ZKjgVt3ipxjwLAZYMfuGTWvyqE3ulxt X-Received: by 2002:a05:6820:4a19:b0:71a:ea6a:4f64 with SMTP id 006d021491bc7-71b394eff5cmr3917901eaf.50.1795233366717; Fri, 20 Nov 2026 19:56:06 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:56:05 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 11/12] media: Add subdevice driver for the Mstar MST3367 HDMI receiver Date: Fri, 20 Nov 2026 21:55:22 -0600 Message-ID: <20261121035523.26764-12-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement a V4L2 subdevice driver for the MST3367. This was developed through reverse engineering the Windows driver, and capturing USB packets. The driver supports both HDMI and analog component inputs. YPbPr and RGB HDMI sources have been tested. I also added a couple custom DV timings for non-interlaced 240p and 288p over component, which are also tested and working with this chip. I tested this with a ThinkPad's video output, MacBook (10-bit deep color), Nintendo Switch (both full and limited range) and PS4 720p/1080i/1080p. Signed-off-by: Matthew Laux --- MAINTAINERS | 7 + drivers/media/i2c/Kconfig | 11 + drivers/media/i2c/Makefile | 1 + drivers/media/i2c/mst3367.c | 1134 +++++++++++++++++++++++++++++++++++ include/media/i2c/mst3367.h | 27 + 5 files changed, 1180 insertions(+) create mode 100644 drivers/media/i2c/mst3367.c create mode 100644 include/media/i2c/mst3367.h diff --git a/MAINTAINERS b/MAINTAINERS index 102362c386fe..6cbe402ee9da 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -19039,6 +19039,13 @@ W: https://linuxtv.org Q: http://patchwork.linuxtv.org/project/linux-media/list/ F: drivers/media/usb/msi2500/ =20 +MSTAR MST3367 HDMI RECEIVER DRIVER +M: Matthew Laux +L: linux-media@vger.kernel.org +S: Maintained +F: drivers/media/i2c/mst3367.c +F: include/media/i2c/mst3367.h + MSTAR INTERRUPT CONTROLLER DRIVER M: Mark-PK Tsai M: Daniel Palmer diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig index 9e42b380e3f2..0b04f0a2a472 100644 --- a/drivers/media/i2c/Kconfig +++ b/drivers/media/i2c/Kconfig @@ -1456,6 +1456,17 @@ config VIDEO_ML86V7667 To compile this driver as a module, choose M here: the module will be called ml86v7667. =20 +config VIDEO_MST3367 + tristate "MStar MST3367 HDMI receiver and component ADC" + depends on VIDEO_DEV && I2C + select MEDIA_CONTROLLER + help + Support for the MStar MST3367 HDMI receiver, component video + ADC and scaler. + + To compile this driver as a module, choose M here: the + module will be called mst3367. + config VIDEO_SAA7110 tristate "Philips SAA7110 video decoder" depends on VIDEO_DEV && I2C diff --git a/drivers/media/i2c/Makefile b/drivers/media/i2c/Makefile index 604b19dbc7b7..b9541ec3f823 100644 --- a/drivers/media/i2c/Makefile +++ b/drivers/media/i2c/Makefile @@ -81,6 +81,7 @@ obj-$(CONFIG_VIDEO_MIRA016) +=3D mira016.o obj-$(CONFIG_VIDEO_MIRA220) +=3D mira220.o obj-$(CONFIG_VIDEO_ML86V7667) +=3D ml86v7667.o obj-$(CONFIG_VIDEO_MSP3400) +=3D msp3400.o +obj-$(CONFIG_VIDEO_MST3367) +=3D mst3367.o obj-$(CONFIG_VIDEO_MT9M001) +=3D mt9m001.o obj-$(CONFIG_VIDEO_MT9M111) +=3D mt9m111.o obj-$(CONFIG_VIDEO_MT9M114) +=3D mt9m114.o diff --git a/drivers/media/i2c/mst3367.c b/drivers/media/i2c/mst3367.c new file mode 100644 index 000000000000..0d71807a2a5d --- /dev/null +++ b/drivers/media/i2c/mst3367.c @@ -0,0 +1,1134 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * MStar MST3367 HDMI receiver and component ADC + * + * Based on reverse engineering of CY3014.X64.SYS + * (version 1.1.0.193 of Fri, 04 Jun 2021 09:37:45 UTC) + * HDMI path based on hdcapm by Steven Toth + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct mst3367 { + struct v4l2_subdev sd; + struct media_pad pads[MST3367_NUM_PADS]; + struct device *dev; + struct i2c_client *client; + + u32 input; + int current_bank; + bool bt601; + struct v4l2_dv_timings timings; +}; + +static inline struct mst3367 *to_mst3367(struct v4l2_subdev *sd) +{ + return container_of(sd, struct mst3367, sd); +} + +static int mst3367_write(struct mst3367 *mst, u8 reg, u8 val) +{ + return i2c_smbus_write_byte_data(mst->client, reg, val); +} + +static int mst3367_read(struct mst3367 *mst, u8 reg) +{ + return i2c_smbus_read_byte_data(mst->client, reg); +} + +static int mst3367_rmw(struct mst3367 *mst, u8 reg, u8 mask, u8 bits) +{ + int val =3D mst3367_read(mst, reg); + + if (val < 0) + return val; + return mst3367_write(mst, reg, (val & mask) | bits); +} + +static int mst3367_rmw_get_old(struct mst3367 *mst, u8 reg, u8 mask, u8 bi= ts, + u8 *old) +{ + int val =3D mst3367_read(mst, reg); + + if (val < 0) + return val; + *old =3D val; + return mst3367_write(mst, reg, (val & mask) | bits); +} + +static const struct v4l2_dv_timings_cap mst3367_hdmi_timings_cap =3D { + .type =3D V4L2_DV_BT_656_1120, + .bt =3D { + .min_width =3D 640, + .max_width =3D 1920, + .min_height =3D 480, + .max_height =3D 1080, + .min_pixelclock =3D 25175000, /* 640x480p60 */ + .max_pixelclock =3D 148500000, /* 1080p60 */ + .standards =3D V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT, + .capabilities =3D V4L2_DV_BT_CAP_PROGRESSIVE | + V4L2_DV_BT_CAP_INTERLACED, + }, +}; + +/* ------------------------------------------------------------------ */ +/* Component scaler mode table */ +/* ------------------------------------------------------------------ */ + +struct component_mode { + int refresh_rate; + /* MST3367 bank 0 ADC/scaler registers (ref: SetADParameters) */ + u8 pllgain, clpdly, clpdur, hsopw; + u8 adc_bw0, adc_bw1; + u16 scaler_htotal; + u16 hde_off; + u8 vde_off; + struct v4l2_dv_timings timings; +}; + +#define MST3367_DV_BT_CEA_720X240P59_94 { \ + .type =3D V4L2_DV_BT_656_1120, \ + V4L2_INIT_BT_TIMINGS(720, 240, 0, 0, \ + 13500000, 19, 62, 57, 4, 3, 15, 0, 0, 0, \ + V4L2_DV_BT_STD_CEA861, \ + V4L2_DV_FL_IS_CE_VIDEO | V4L2_DV_FL_HAS_PICTURE_ASPECT | \ + V4L2_DV_FL_HAS_CEA861_VIC, { 4, 3 }, 8) \ +} + +#define MST3367_DV_BT_CEA_720X288P50 { \ + .type =3D V4L2_DV_BT_656_1120, \ + V4L2_INIT_BT_TIMINGS(720, 288, 0, 0, \ + 13500000, 12, 63, 69, 2, 3, 19, 0, 0, 0, \ + V4L2_DV_BT_STD_CEA861, \ + V4L2_DV_FL_IS_CE_VIDEO | V4L2_DV_FL_HAS_PICTURE_ASPECT | \ + V4L2_DV_FL_HAS_CEA861_VIC, { 4, 3 }, 23) \ +} + +/* + * Values from Windows driver table $140303810 + 240p and 288p + */ +static const struct component_mode component_modes[] =3D { + /* pll clp clp hsop adc adc */ + /*Hz gain dly dur w bw0 bw1 sclht hde vde */ + { 60, 0x08, 0x08, 0x08, 0x18, 0x44, 0x04, 0x35a, 0x076, 0x12, + V4L2_DV_BT_CEA_720X480I59_94 }, + { 60, 0x08, 0x08, 0x08, 0x18, 0x44, 0x04, 0x35a, 0x077, 0x13, + MST3367_DV_BT_CEA_720X240P59_94 }, + { 50, 0x08, 0x08, 0x08, 0x18, 0x44, 0x04, 0x360, 0x083, 0x16, + V4L2_DV_BT_CEA_720X576I50 }, + { 50, 0x08, 0x08, 0x08, 0x18, 0x44, 0x04, 0x360, 0x083, 0x17, + MST3367_DV_BT_CEA_720X288P50 }, + { 60, 0x20, 0x08, 0x08, 0x18, 0x33, 0x03, 0x35a, 0x079, 0x24, + V4L2_DV_BT_CEA_720X480P59_94 }, + { 50, 0x18, 0x08, 0x08, 0x18, 0x33, 0x03, 0x360, 0x083, 0x2c, + V4L2_DV_BT_CEA_720X576P50 }, + { 24, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0xce4, 0x12b, 0x19, + V4L2_DV_BT_CEA_1280X720P24 }, + { 25, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0xf78, 0x12b, 0x19, + V4L2_DV_BT_CEA_1280X720P25 }, + { 30, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0xce4, 0x12b, 0x19, + V4L2_DV_BT_CEA_1280X720P30 }, + { 50, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0x7bc, 0x12b, 0x19, + V4L2_DV_BT_CEA_1280X720P50 }, + { 60, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0x672, 0x12b, 0x19, + V4L2_DV_BT_CEA_1280X720P60 }, + { 50, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0xa50, 0x0eb, 0x14, + V4L2_DV_BT_CEA_1920X1080I50 }, + { 60, 0x58, 0x38, 0x20, 0x30, 0x11, 0x01, 0x898, 0x0eb, 0x14, + V4L2_DV_BT_CEA_1920X1080I60 }, + { 24, 0xe8, 0x38, 0x30, 0x60, 0x00, 0x00, 0xabe, 0x0eb, 0x29, + V4L2_DV_BT_CEA_1920X1080P24 }, + { 25, 0xe8, 0x38, 0x30, 0x60, 0x00, 0x00, 0xa50, 0x0eb, 0x29, + V4L2_DV_BT_CEA_1920X1080P25 }, + { 30, 0xe8, 0x38, 0x30, 0x60, 0x00, 0x00, 0x898, 0x0eb, 0x29, + V4L2_DV_BT_CEA_1920X1080P30 }, + { 50, 0xe8, 0x38, 0x30, 0x60, 0x00, 0x00, 0xa50, 0x0eb, 0x29, + V4L2_DV_BT_CEA_1920X1080P50 }, + { 60, 0xe8, 0x38, 0x30, 0x60, 0x00, 0x00, 0x898, 0x0eb, 0x29, + V4L2_DV_BT_CEA_1920X1080P60 }, +}; + +static const struct v4l2_dv_timings_cap mst3367_component_timings_cap =3D { + .type =3D V4L2_DV_BT_656_1120, + .bt =3D { + .min_width =3D 720, + .max_width =3D 1920, + .min_height =3D 240, + .max_height =3D 1080, + .min_pixelclock =3D 13500000, /* 480i */ + .max_pixelclock =3D 148500000, /* 1080p60 */ + .standards =3D V4L2_DV_BT_STD_CEA861, + .capabilities =3D V4L2_DV_BT_CAP_PROGRESSIVE | + V4L2_DV_BT_CAP_INTERLACED, + }, +}; + +static int mst_bank(struct mst3367 *mst, int bank) +{ + int ret; + + if (mst->current_bank =3D=3D bank) + return 0; + ret =3D mst3367_write(mst, 0x00, bank); + mst->current_bank =3D ret ? -1 : bank; + return ret; +} + +static void mst3367_config(struct mst3367 *mst) +{ + bool hotplug; + + mst_bank(mst, 0x00); + mst3367_write(mst, 0x51, 0x80); + mst3367_write(mst, 0x13, 0x08); + /* RxTmdsHotPlug: all off */ + mst3367_rmw(mst, 0xb7, 0xff, 0x02); + /* RxGeneralInit */ + mst3367_write(mst, 0x41, 0x6f); + mst3367_write(mst, 0xb8, 0x00); + /* RxVideoInit */ + mst3367_write(mst, 0xb0, 0x14); + mst3367_rmw(mst, 0xae, ~0x04, 0x04); + mst3367_write(mst, 0xad, 0x05); /* low-pass filter */ + mst3367_write(mst, 0xb1, 0xc0); + mst3367_write(mst, 0xb2, 0x00); + mst3367_write(mst, 0xb3, 0x00); + mst3367_write(mst, 0xb4, 0x55); + /* RxAudioInit */ + mst3367_rmw(mst, 0xb4, ~0x03, 0x00); + + /* RxTmdsInit */ + mst_bank(mst, 0x01); + mst3367_write(mst, 0x0f, 0x02); + mst3367_write(mst, 0x16, 0x30); + mst3367_write(mst, 0x17, 0x00); + mst3367_write(mst, 0x18, 0x00); + mst3367_write(mst, 0x19, 0x00); + mst3367_write(mst, 0x1a, 0x50); + mst3367_rmw(mst, 0x2a, ~0x07, 0x07); + /* RxHdcpInit */ + mst3367_write(mst, 0x24, 0x40); + mst3367_write(mst, 0x25, 0x00); + mst3367_write(mst, 0x30, 0x80); + mst3367_write(mst, 0x31, 0x00); + mst3367_write(mst, 0x32, 0x00); + + mst_bank(mst, 0x02); + mst3367_write(mst, 0x08, 0x03); + /* RxAudioInit (cont'd) */ + mst3367_write(mst, 0x01, 0x61); + mst3367_write(mst, 0x02, 0xf5); + mst3367_rmw(mst, 0x03, ~0x02, 0x02); + mst3367_write(mst, 0x04, 0x01); + mst3367_write(mst, 0x05, 0x00); + mst3367_write(mst, 0x06, 0x08); + mst3367_write(mst, 0x1c, 0x1a); + mst3367_write(mst, 0x1d, 0x00); + mst3367_write(mst, 0x1e, 0x00); + mst3367_write(mst, 0x1f, 0x00); + mst3367_rmw(mst, 0x25, 0xff ^ 0xa2, 0xa2); + mst3367_rmw(mst, 0x02, 0x7f, 0x80); + mst3367_rmw(mst, 0x07, ~0x04, 0x04); + mst3367_write(mst, 0x17, 0xc0); + mst3367_write(mst, 0x19, 0xff); + mst3367_write(mst, 0x1a, 0xff); + mst3367_write(mst, 0x1b, 0xfc); + mst3367_write(mst, 0x20, 0x00); + mst3367_rmw(mst, 0x21, ~0x03, 0x00); + mst3367_write(mst, 0x22, 0x26); + mst3367_write(mst, 0x27, 0x00); + mst3367_rmw(mst, 0x2e, 0xff ^ 0xa1, 0xa1); + + mst_bank(mst, 0x00); + + hotplug =3D false; + v4l2_subdev_notify(&mst->sd, MST3367_HOTPLUG, &hotplug); + + mst3367_rmw(mst, 0xb7, 0xff, 0x02); + + mst3367_write(mst, 0xab, 0x15); /* COLOR.RANGE */ + mst3367_write(mst, 0xac, 0x15); /* COLOR.RANGE */ + + /* RxHdcpReset */ + mst3367_write(mst, 0xb8, 0x10); + mst3367_write(mst, 0xb8, 0x00); + + /* RxHdmiReset */ + mst_bank(mst, 0x02); + mst3367_write(mst, 0x07, 0xf4); + mst3367_write(mst, 0x07, 0x04); + + /* RxSwitchSource: 0x81 =3D HDMI TMDS.A */ + mst_bank(mst, 0x00); + mst3367_write(mst, 0x51, 0x81); + + hotplug =3D true; + v4l2_subdev_notify(&mst->sd, MST3367_HOTPLUG, &hotplug); + + mst3367_rmw(mst, 0xb7, 0xfd, 0x00); + msleep(200); + + /* FUN_00016640: RxHdmiInit / BT.656 output enable */ + mst_bank(mst, 0x02); + mst3367_rmw(mst, 0x01, 0x0f, 0x60); + mst3367_rmw(mst, 0x04, 0xff, 0x01); + mst3367_write(mst, 0x06, 0x08); + mst3367_rmw(mst, 0x09, 0xff, 0x20); + + mst_bank(mst, 0x00); + mst3367_rmw(mst, 0x54, 0xef, 0x00); + mst3367_rmw(mst, 0xac, 0xff, 0x80); + mst3367_rmw(mst, 0xce, 0xff, 0x80); + mst3367_rmw(mst, 0xcf, 0xfa, 0x02); + + mst3367_rmw(mst, 0x00, 0x7f, 0x00); + mst_bank(mst, 0x00); +} + +/* Identity CSC for component YPbPr */ +static const u8 csc_identity_component[] =3D { + 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, /* M11, M12, M13 */ + 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, /* M21, M22, M23 */ + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, /* M31, M32, M33 */ + 0x20, 0x00, 0x03, 0x80, 0x20, 0x00, /* A1, A2, A3 */ +}; + +/* Identity CSC for HDMI YCbCr */ +static const u8 csc_identity_hdmi[] =3D { + 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, +}; + +/* BT.601 for SD (width <=3D 720) */ +static const u8 csc_r2y_sd[] =3D { + 0x08, 0x02, 0x04, 0xc5, 0xfd, 0x4d, + 0xf9, 0x4a, 0x09, 0x5b, 0xfa, 0xb1, + 0xfe, 0xb4, 0x01, 0xd2, 0x08, 0x02, + 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, +}; + +/* BT.709 for HD (width > 720) */ +static const u8 csc_r2y_hd[] =3D { + 0x08, 0x02, 0x03, 0x65, 0xfe, 0x28, + 0xf8, 0xb9, 0x0b, 0x65, 0xf9, 0xd6, + 0xff, 0x45, 0x01, 0x27, 0x08, 0x02, + 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, +}; + +/* Bank 2 HDMI packet registers */ +#define MST3367_PKT_STATUS 0x0b +#define MST3367_PKT_STATUS_AVI BIT(3) +/* AVI InfoFrame data bytes PB1..PB5 */ +#define MST3367_AVI_PB1 0x48 + +/* + * Fill @avi from the AVI InfoFrame registers. Returns -ENODATA if no + * InfoFrame has been received, e.g. from a DVI source. + */ +static int mst3367_read_avi(struct mst3367 *mst, + struct hdmi_avi_infoframe *avi) +{ + u8 pb[5]; + int k, ret; + + ret =3D mst_bank(mst, 0x02); + if (ret) + return ret; + + ret =3D mst3367_read(mst, MST3367_PKT_STATUS); + if (ret < 0) + return ret; + if (!(ret & MST3367_PKT_STATUS_AVI)) + return -ENODATA; + + for (k =3D 0; k < ARRAY_SIZE(pb); k++) { + ret =3D mst3367_read(mst, MST3367_AVI_PB1 + k); + if (ret < 0) + return ret; + pb[k] =3D ret; + } + + avi->colorspace =3D (pb[0] >> 5) & 0x3; + avi->colorimetry =3D (pb[1] >> 6) & 0x3; + avi->itc =3D !!(pb[2] & BIT(7)); + avi->extended_colorimetry =3D (pb[2] >> 4) & 0x7; + avi->quantization_range =3D (pb[2] >> 2) & 0x3; + avi->video_code =3D pb[3]; + avi->ycc_quantization_range =3D (pb[4] >> 6) & 0x3; + avi->content_type =3D (pb[4] >> 4) & 0x3; + avi->pixel_repeat =3D pb[4] & 0xf; + + return 0; +} + +static void mst3367_write_csc(struct mst3367 *mst) +{ + bool comp =3D (mst->input =3D=3D MST3367_INPUT_COMPONENT); + const u8 *csc; + u8 old_ab =3D 0, reg_92; + bool limited =3D false; + /* this can be overridden by the source */ + bool bt601 =3D mst->timings.bt.width <=3D 720; + int k; + + if (comp) { + reg_92 =3D 0x66; + csc =3D csc_identity_component; + } else { + struct hdmi_avi_infoframe avi =3D {}; + struct v4l2_hdmi_colorimetry c; + int ret; + + ret =3D mst3367_read_avi(mst, &avi); + if (ret && ret !=3D -ENODATA) + dev_warn(mst->dev, "failed to read AVI InfoFrame: %d\n", + ret); + + c =3D v4l2_hdmi_rx_colorimetry(&avi, NULL, mst->timings.bt.height); + limited =3D c.quantization =3D=3D V4L2_QUANTIZATION_LIM_RANGE; + + dev_dbg(mst->dev, + "AVI: %s colorspace=3D%d colorimetry=3D%d ec=3D%d q=3D%d yq=3D%d vic=3D= %u\n", + ret ? "absent" : "present", avi.colorspace, + avi.colorimetry, avi.extended_colorimetry, + avi.quantization_range, avi.ycc_quantization_range, + avi.video_code); + + if (avi.colorspace =3D=3D HDMI_COLORSPACE_RGB) { + reg_92 =3D limited ? 0x55 : 0x40; + csc =3D bt601 ? csc_r2y_sd : csc_r2y_hd; + } else { + switch (c.ycbcr_enc) { + case V4L2_YCBCR_ENC_601: + case V4L2_YCBCR_ENC_XV601: + bt601 =3D true; + break; + case V4L2_YCBCR_ENC_709: + case V4L2_YCBCR_ENC_XV709: + bt601 =3D false; + break; + default: + /* keep the size default */ + break; + } + reg_92 =3D limited ? 0x66 : 0x62; + csc =3D csc_identity_hdmi; + } + + dev_info(mst->dev, "CSC: HDMI %s, %s, %s range\n", + avi.colorspace =3D=3D HDMI_COLORSPACE_RGB ? "RGB" : "YCbCr", + bt601 ? "BT.601" : "BT.709", + limited ? "limited" : "full"); + } + + mst->bt601 =3D bt601; + + mst_bank(mst, 0x00); + + /* BLANK.OUTPUT while changing CSC */ + mst3367_rmw_get_old(mst, 0xab, 0x7f, 0x80, &old_ab); + + /* COLOR.RANGE */ + mst3367_write(mst, 0x90, 0x15); + mst3367_write(mst, 0x91, 0x15); + mst3367_write(mst, 0x92, reg_92); + + mst3367_rmw(mst, 0xac, ~0x3f, 0x15); + mst3367_write(mst, 0xad, 0x05); + mst3367_write(mst, 0x1e, 0x11); + mst3367_write(mst, 0x1f, 0x01); + + for (k =3D 0; k < 24; k++) + mst3367_write(mst, 0x93 + k, csc[k]); + + if (!comp) + mst3367_write(mst, 0xa7, limited ? 0x04 : 0x00); + + /* RX_OUTPUT_YUV422 / 08.BITS / EMBEDDED SYNC */ + mst3367_rmw(mst, 0xb0, 0xc2, 0x21); + /* NORMAL.OUTPUT (un-blank) */ + mst3367_write(mst, 0xab, old_ab & 0x7f); + + mst3367_write(mst, 0xb3, 0x00); + + mst_bank(mst, 0x02); + mst3367_write(mst, 0x27, 0x00); + mst3367_write(mst, 0x20, 0x00); +} + +/* ------------------------------------------------------------------ */ +/* Signal detection polling */ +/* ------------------------------------------------------------------ */ + +#define HDMI_VTOTAL_TOLERANCE 5 +#define HDMI_VFREQ_TOLERANCE 5 + +static int mst3367_read_vfreq(struct mst3367 *mst) +{ + int hi, lo, period; + + mst_bank(mst, 0x00); + hi =3D mst3367_read(mst, 0x59); + lo =3D mst3367_read(mst, 0x5a); + if (hi < 0 || lo < 0) + return 0; + + period =3D (hi & 0x3f) << 8 | lo; + /* units of 0.1 Hz, ref: RxHdmiModeDetect */ + return period ? 1250000 / period : 0; +} + +static const struct v4l2_dv_timings *match_hdmi_timing(int hactive, int vt= otal, + int vfreq) +{ + const struct v4l2_dv_timings *best =3D NULL; + int best_err =3D INT_MAX; + int k; + + for (k =3D 0; v4l2_dv_timings_presets[k].bt.width; k++) { + const struct v4l2_dv_timings *t =3D &v4l2_dv_timings_presets[k]; + const struct v4l2_bt_timings *bt =3D &t->bt; + u32 frame_width =3D V4L2_DV_BT_FRAME_WIDTH(bt); + u32 frame_height =3D V4L2_DV_BT_FRAME_HEIGHT(bt); + int fields =3D bt->interlaced ? 2 : 1; + int vtotal_err, vfreq_err, err; + + if (bt->width !=3D hactive) + continue; + if (!v4l2_valid_dv_timings(t, &mst3367_hdmi_timings_cap, + NULL, NULL)) + continue; + + vtotal_err =3D abs(vtotal * fields - (int)frame_height); + if (vtotal_err > HDMI_VTOTAL_TOLERANCE) + continue; + + vfreq_err =3D abs(vfreq - + (int)DIV_ROUND_CLOSEST_ULL(bt->pixelclock * 10 * fields, + frame_width * frame_height)); + if (vfreq_err > HDMI_VFREQ_TOLERANCE) + continue; + + err =3D vtotal_err * 2 * HDMI_VFREQ_TOLERANCE + vfreq_err; + if (err < best_err) { + best_err =3D err; + best =3D t; + } + } + return best; +} + +static void mst3367_enable_auto_position(struct mst3367 *mst) +{ + mst_bank(mst, 0x00); + mst3367_write(mst, 0xe2, 0x80); + /* report the digital DE window */ + mst3367_write(mst, 0xe3, 0x08); + /* position threshold */ + mst3367_write(mst, 0xe4, 0x00); +} + +static int hdmi_poll_signal(struct mst3367 *mst, + struct v4l2_dv_timings *timings) +{ + int k, status; + int prev_htotal =3D -1, prev_vtotal =3D -1, prev_hactive =3D -1; + int stable_count =3D 0; + + mst3367_enable_auto_position(mst); + msleep(100); + + for (k =3D 0; k < 100; k++) { + int htotal, vtotal, hactive, vfreq, tmds_ratio; + const struct v4l2_dv_timings *std; + + msleep(100); + + mst_bank(mst, 0x00); + status =3D mst3367_read(mst, 0x55); + if (status < 0) + continue; + + if (!(status & 0x3c)) { + /* Not locked yet */ + mst3367_enable_auto_position(mst); + if (k % 10 =3D=3D 0) + dev_info(mst->dev, + "HDMI signal poll[%d]: lock=3D0x%02x (no signal)\n", + k, status); + stable_count =3D 0; + continue; + } + + htotal =3D (mst3367_read(mst, 0x6a) << 8 | + mst3367_read(mst, 0x6b)) & 0xfff; + vtotal =3D (mst3367_read(mst, 0x5b) << 8 | + mst3367_read(mst, 0x5c)) & 0x7ff; + + mst_bank(mst, 0x02); + hactive =3D (mst3367_read(mst, 0x29) << 8 | + mst3367_read(mst, 0x28)) & 0x1fff; + + if (k % 5 =3D=3D 0) + dev_info(mst->dev, + "HDMI poll[%d]: lock=3D0x%02x ht=3D%d vt=3D%d ha=3D%d stable=3D%d\n", + k, status, htotal, vtotal, hactive, stable_count); + + /* + * Require non-zero timings and consecutive matching readings + * before accepting (within one vertical line for interlaced) + */ + if (htotal =3D=3D 0 || vtotal =3D=3D 0 || hactive =3D=3D 0 || + htotal !=3D prev_htotal || abs(vtotal - prev_vtotal) > 1 || + hactive !=3D prev_hactive) { + prev_htotal =3D htotal; + prev_vtotal =3D vtotal; + prev_hactive =3D hactive; + stable_count =3D 0; + continue; + } + + if (++stable_count < 2) + continue; + + vfreq =3D mst3367_read_vfreq(mst); + + dev_info(mst->dev, + "HDMI locked: htotal=3D%d vtotal=3D%d hactive=3D%d vfreq=3D%d.%d\n", + htotal, vtotal, hactive, vfreq / 10, vfreq % 10); + + std =3D match_hdmi_timing(hactive, vtotal, vfreq); + if (!std) { + dev_err(mst->dev, + "unsupported HDMI timing: htotal=3D%d vtotal=3D%d hactive=3D%d vfreq= =3D%d.%d\n", + htotal, vtotal, hactive, vfreq / 10, vfreq % 10); + return -ERANGE; + } + + /* 100 =3D 8 bit, 125 =3D 10 bit, 150 =3D 12 bit deep color */ + tmds_ratio =3D htotal * 100 / V4L2_DV_BT_FRAME_WIDTH(&std->bt); + dev_info(mst->dev, + "HDMI mode: %ux%u%s, TMDS clock =3D %d.%02dx pixel clock\n", + std->bt.width, std->bt.height, + std->bt.interlaced ? "i" : "p", + tmds_ratio / 100, tmds_ratio % 100); + + *timings =3D *std; + + /* DISABLE AUTO POSITION */ + mst_bank(mst, 0x00); + mst3367_write(mst, 0xe2, 0x00); + + return 0; + } + + dev_err(mst->dev, "No HDMI signal lock after 10s\n"); + return -ETIMEDOUT; +} + +/* ------------------------------------------------------------------ */ +/* ADC front-end config (component/VGA only) */ +/* ------------------------------------------------------------------ */ + +static void mst3367_adc_config(struct mst3367 *mst) +{ + /* RxAdcInit */ + mst_bank(mst, 0x00); + mst3367_write(mst, 0x00, 0x80); + mst3367_write(mst, 0x24, 0xc0); + mst3367_write(mst, 0x00, 0x00); + mst3367_rmw(mst, 0x21, ~0x07, 0x01); + mst3367_write(mst, 0x04, 0x00); /* ADC phase */ + mst3367_rmw(mst, 0x10, 0x0f, 0xd0); + mst3367_write(mst, 0x11, 0x2d); + mst3367_write(mst, 0x20, 0xc0); + mst3367_write(mst, 0x21, 0x04); + /* bits 5:4 =3D sync source: 0x30 =3D SOG for component */ + mst3367_rmw(mst, 0x0e, 0xcf, 0x30); +} + +static const struct component_mode * +find_component_mode(const struct v4l2_dv_timings *t) +{ + int k; + + for (k =3D 0; k < ARRAY_SIZE(component_modes); k++) + if (v4l2_match_dv_timings(t, &component_modes[k].timings, 0, false)) + return &component_modes[k]; + return NULL; +} + +#define COMPONENT_VTOTAL_TOLERANCE 25 +#define COMPONENT_HFREQ_TOLERANCE 8 + +/* Picks the mode closest to the measured line count and line rate */ +static const struct component_mode *match_component_mode(int vtotal, int h= freq, + bool interlaced) +{ + const struct component_mode *best =3D NULL; + int best_err =3D INT_MAX; + int k; + + for (k =3D 0; k < ARRAY_SIZE(component_modes); k++) { + const struct component_mode *m =3D &component_modes[k]; + const struct v4l2_bt_timings *bt =3D &m->timings.bt; + int std_vtotal =3D V4L2_DV_BT_FRAME_HEIGHT(bt) / + (bt->interlaced ? 2 : 1); + int std_hfreq =3D (u32)bt->pixelclock / + (V4L2_DV_BT_FRAME_WIDTH(bt) * 100); + int vtotal_err =3D abs(vtotal - std_vtotal); + int hfreq_err =3D abs(hfreq - std_hfreq); + int err; + + if (vtotal_err > COMPONENT_VTOTAL_TOLERANCE || + hfreq_err > COMPONENT_HFREQ_TOLERANCE) + continue; + + err =3D vtotal_err + hfreq_err; + if (!bt->interlaced !=3D !interlaced) + err +=3D COMPONENT_VTOTAL_TOLERANCE + + COMPONENT_HFREQ_TOLERANCE + 1; + if (err < best_err) { + best_err =3D err; + best =3D m; + } + } + return best; +} + +/* ------------------------------------------------------------------ */ +/* Component scaler register write */ +/* ------------------------------------------------------------------ */ + +static void component_write_scaler(struct mst3367 *mst, + const struct component_mode *m) +{ + const struct v4l2_bt_timings *bt =3D &m->timings.bt; + int width =3D bt->width; + int height =3D bt->interlaced ? bt->height / 2 : bt->height; + int ht =3D m->scaler_htotal - 1; + int is_720_low_rr =3D width =3D=3D 1280 && height =3D=3D 720 && + (m->refresh_rate =3D=3D 24 || + m->refresh_rate =3D=3D 25 || + m->refresh_rate =3D=3D 30); + + mst_bank(mst, 0x00); + + /* + * edge case for low frame rate 720p: + * if (((*(int *)(uVar30 + 0x30) =3D=3D 0x500) && (*(int *)(uVar30 + 0x34= ) =3D=3D 0x2d0)) && + * ((*(int *)(uVar30 + 0x38) =3D=3D 0x1e || + * ((*(int *)(uVar30 + 0x38) =3D=3D 0x19 || (*(int *)(uVar30 + 0x38) = =3D=3D 0x18)))))) { + * uVar37 =3D 0; + * } else { + * uVar37 =3D 4; + * } + * send_mst(param_1,0,0x12,uVar37); + */ + mst3367_write(mst, 0x12, is_720_low_rr ? 0x00 : 0x04); + + /* SetADParameters */ + mst3367_write(mst, 0x03, m->pllgain); + mst3367_write(mst, 0x05, m->clpdly); + mst3367_write(mst, 0x06, m->clpdur); + mst3367_write(mst, 0x07, m->hsopw); + + /* ADC gain: Y, Cb, Cr */ + mst3367_write(mst, 0x08, 0xa0); + mst3367_write(mst, 0x09, 0xc0); + mst3367_write(mst, 0x0a, 0xa0); + + /* grade scale (clamp level): Y, Cb, Cr */ + mst3367_write(mst, 0x0b, 0x80); + mst3367_write(mst, 0x0c, 0x60); + mst3367_write(mst, 0x0d, 0x80); + + /* clamp offset: Y, Cb, Cr */ + mst3367_write(mst, 0x18, 0x10); + mst3367_write(mst, 0x19, 0x10); + mst3367_write(mst, 0x1a, 0x10); + mst3367_write(mst, 0x1b, 0x00); + mst3367_write(mst, 0x1c, 0x00); + mst3367_write(mst, 0x1d, 0x00); + + /* ADCSetAutoDetectFormat: output htotal */ + mst3367_write(mst, 0x01, ht >> 4); + mst3367_write(mst, 0x02, (ht & 0x0f) << 4); + mst3367_write(mst, 0x1e, m->adc_bw0); + mst3367_write(mst, 0x1f, m->adc_bw1); + + /* output data enable start pos after sync */ + mst3367_write(mst, 0x80, (m->hde_off >> 8) & 0x0f); + mst3367_write(mst, 0x81, m->hde_off & 0xff); + mst3367_write(mst, 0x82, (width >> 8) & 0xff); + mst3367_write(mst, 0x83, width & 0xff); + mst3367_write(mst, 0x84, m->vde_off); + mst3367_write(mst, 0x85, + ((height >> 8) & 0x0f) | (bt->interlaced ? 0x20 : 0x00)); + mst3367_write(mst, 0x86, height & 0xff); + + mst3367_write(mst, 0x2d, 0x11); + mst3367_write(mst, 0x2e, 0x11); + mst3367_write(mst, 0x39, (u8)(m->clpdly + m->clpdur - 0x78)); + mst3367_write(mst, 0x2c, 0x9d); + mst3367_write(mst, 0x3a, 0x0c); + mst3367_write(mst, 0x3b, 0x08); + + mst3367_rmw(mst, 0x10, 0xfa, 0x05); + mst3367_rmw(mst, 0x0f, 0xff, 0x20); + mst3367_rmw(mst, 0x17, 0xff, 0x02); + mst3367_rmw(mst, 0x2f, 0xff, 0x02); + mst3367_rmw(mst, 0x60, 0xfb, 0x00); +} + +/* ------------------------------------------------------------------ */ +/* Component ADC signal polling */ +/* ------------------------------------------------------------------ */ + +static int component_poll_signal(struct mst3367 *mst, + const struct component_mode **detected) +{ + const struct component_mode *mode; + int k, status, vtotal, hperiod, hfreq, interlaced; + int signal_count =3D 0; + + /* + * RxAdcModeDetect() + * regs[0x14] & 0x90 =3D=3D 0x90: H+V locked + * regs[0x14] & 0x90 =3D=3D 0x80: H lock only + * regs[0x14] =3D=3D 0x00 && regs[0x15] & 0x40: none + */ + for (k =3D 0; k < 100; k++) { + int signal_type; + + mst_bank(mst, 0x00); + + /* trigger timing measurement? */ + mst3367_write(mst, 0x0e, 0x40); + + status =3D mst3367_read(mst, 0x14); + mst3367_read(mst, 0x15); + + /* Check signal presence from 0x14 bits 7,4 */ + signal_type =3D status & 0x90; + if (signal_type =3D=3D 0x90 || signal_type =3D=3D 0x80 || + (signal_type =3D=3D 0x00 && (status & 0x02))) + signal_count++; + else + signal_count =3D 0; + + hperiod =3D ((mst3367_read(mst, 0x57) & 0x3f) << 8) | + mst3367_read(mst, 0x58); + + /* scale ADC sample rate to H period */ + if (hperiod > 0) + mst3367_write(mst, 0x11, + (hperiod < 0x401 ? 0x400 : hperiod) >> 7); + + /* V period (timer counts) */ + mst3367_read(mst, 0x59); + mst3367_read(mst, 0x5a); + + /* V total (lines) */ + mst3367_read(mst, 0x5b); + mst3367_read(mst, 0x5c); + + /* H total (pixels), interlace flag */ + mst3367_read(mst, 0x5d); + mst3367_read(mst, 0x5e); + interlaced =3D mst3367_read(mst, 0x5f); + interlaced =3D interlaced < 0 ? 0 : !!(interlaced & 0x02); + + mst_bank(mst, 0x02); + mst3367_read(mst, 0x11); + mst3367_read(mst, 0x12); + mst3367_write(mst, 0x20, 0x00); + + if (k % 10 =3D=3D 0) + dev_info(mst->dev, + "component poll[%d]: 0x14=3D0x%02x count=3D%d\n", + k, status, signal_count); + + if (signal_count < 3) { + msleep(100); + continue; + } + + /* Signal stable - read vtotal and determine format */ + mst_bank(mst, 0x00); + vtotal =3D ((mst3367_read(mst, 0x5b) & 0x07) << 8) | + mst3367_read(mst, 0x5c); + + /* units are 0.1 kHz, ref: RxAdcModeDetect */ + hfreq =3D hperiod > 0 ? 1600000 / hperiod : 0; + + dev_info(mst->dev, + "component locked: vtotal=3D%d hfreq=3D%d.%d kHz %s 0x14=3D0x%02x\n", + vtotal, hfreq / 10, hfreq % 10, + interlaced ? "interlaced" : "progressive", status); + + mode =3D match_component_mode(vtotal, hfreq, interlaced); + if (!mode) { + dev_err(mst->dev, + "unsupported component timing: vtotal=3D%d hfreq=3D%d.%d kHz\n", + vtotal, hfreq / 10, hfreq % 10); + return -ERANGE; + } + *detected =3D mode; + return 0; + } + + dev_err(mst->dev, "No component signal lock after 10s\n"); + return -ETIMEDOUT; +} + +/* ------------------------------------------------------------------ */ +/* Subdev ops */ +/* ------------------------------------------------------------------ */ + +static int mst3367_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, + u32 config) +{ + struct mst3367 *mst =3D to_mst3367(sd); + + if (input > MST3367_INPUT_NONE) + return -EINVAL; + + mst->current_bank =3D -1; + mst->input =3D input; + memset(&mst->timings, 0, sizeof(mst->timings)); + + if (input =3D=3D MST3367_INPUT_NONE) { + mst_bank(mst, 0x00); + mst3367_write(mst, 0x51, 0x80); + return 0; + } + + mst3367_config(mst); + + if (input =3D=3D MST3367_INPUT_COMPONENT) { + /* RxSwitchSource: 0x21 =3D component ADC (YPbPr) */ + mst_bank(mst, 0x00); + mst3367_write(mst, 0x51, 0x21); + + mst3367_adc_config(mst); + } + + return 0; +} + +static int mst3367_query_dv_timings(struct v4l2_subdev *sd, unsigned int p= ad, + struct v4l2_dv_timings *timings) +{ + struct mst3367 *mst =3D to_mst3367(sd); + const struct component_mode *mode; + int ret; + + switch (mst->input) { + case MST3367_INPUT_HDMI: + return hdmi_poll_signal(mst, timings); + case MST3367_INPUT_COMPONENT: + ret =3D component_poll_signal(mst, &mode); + if (ret < 0) + return ret; + *timings =3D mode->timings; + return 0; + default: + return -ENODATA; + } +} + +static int mst3367_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad, + struct v4l2_dv_timings *timings) +{ + struct mst3367 *mst =3D to_mst3367(sd); + const struct component_mode *mode; + + switch (mst->input) { + case MST3367_INPUT_HDMI: + if (!v4l2_valid_dv_timings(timings, &mst3367_hdmi_timings_cap, + NULL, NULL)) + return -ERANGE; + mst->timings =3D *timings; + break; + case MST3367_INPUT_COMPONENT: + mode =3D find_component_mode(timings); + if (!mode) + return -ERANGE; + mst->timings =3D *timings; + component_write_scaler(mst, mode); + break; + default: + return -ENODATA; + } + + mst3367_write_csc(mst); + return 0; +} + +static int mst3367_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad, + struct v4l2_dv_timings *timings) +{ + struct mst3367 *mst =3D to_mst3367(sd); + + if (!mst->timings.bt.width) + return -ENODATA; + + *timings =3D mst->timings; + return 0; +} + +static int mst3367_enum_dv_timings(struct v4l2_subdev *sd, + struct v4l2_enum_dv_timings *timings) +{ + switch (timings->pad) { + case MST3367_PAD_HDMI: + return v4l2_enum_dv_timings_cap(timings, + &mst3367_hdmi_timings_cap, + NULL, NULL); + case MST3367_PAD_COMPONENT: + if (timings->index >=3D ARRAY_SIZE(component_modes)) + return -EINVAL; + + timings->timings =3D component_modes[timings->index].timings; + return 0; + default: + return -EINVAL; + } +} + +static int mst3367_dv_timings_cap(struct v4l2_subdev *sd, + struct v4l2_dv_timings_cap *cap) +{ + unsigned int pad =3D cap->pad; + + switch (pad) { + case MST3367_PAD_HDMI: + *cap =3D mst3367_hdmi_timings_cap; + break; + case MST3367_PAD_COMPONENT: + *cap =3D mst3367_component_timings_cap; + break; + default: + return -EINVAL; + } + + cap->pad =3D pad; + return 0; +} + +static int mst3367_enum_mbus_code(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_mbus_code_enum *code) +{ + if (code->index > 0) + return -EINVAL; + + code->code =3D MEDIA_BUS_FMT_UYVY8_2X8; + return 0; +} + +static int mst3367_get_fmt(struct v4l2_subdev *sd, + struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_format *fmt) +{ + struct mst3367 *mst =3D to_mst3367(sd); + const struct v4l2_bt_timings *bt =3D &mst->timings.bt; + + fmt->format.code =3D MEDIA_BUS_FMT_UYVY8_2X8; + fmt->format.width =3D bt->width; + fmt->format.height =3D bt->interlaced ? bt->height / 2 : bt->height; + fmt->format.field =3D bt->interlaced ? + V4L2_FIELD_ALTERNATE : V4L2_FIELD_NONE; + fmt->format.colorspace =3D mst->bt601 ? + V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709; + return 0; +} + +static const struct v4l2_subdev_video_ops mst3367_video_ops =3D { + .s_routing =3D mst3367_s_routing, +}; + +static const struct v4l2_subdev_pad_ops mst3367_pad_ops =3D { + .enum_mbus_code =3D mst3367_enum_mbus_code, + .get_fmt =3D mst3367_get_fmt, + .s_dv_timings =3D mst3367_s_dv_timings, + .g_dv_timings =3D mst3367_g_dv_timings, + .query_dv_timings =3D mst3367_query_dv_timings, + .enum_dv_timings =3D mst3367_enum_dv_timings, + .dv_timings_cap =3D mst3367_dv_timings_cap, +}; + +static const struct v4l2_subdev_ops mst3367_ops =3D { + .video =3D &mst3367_video_ops, + .pad =3D &mst3367_pad_ops, +}; + +static int mst3367_probe(struct i2c_client *client) +{ + struct device *dev =3D &client->dev; + struct mst3367 *mst; + + if (!i2c_check_functionality(client->adapter, + I2C_FUNC_SMBUS_BYTE_DATA)) + return -EIO; + + mst =3D devm_kzalloc(dev, sizeof(*mst), GFP_KERNEL); + if (!mst) + return -ENOMEM; + + mst->dev =3D dev; + mst->client =3D client; + mst->input =3D MST3367_INPUT_NONE; + mst->current_bank =3D -1; + + v4l2_i2c_subdev_init(&mst->sd, client, &mst3367_ops); + + mst->pads[MST3367_PAD_HDMI].flags =3D MEDIA_PAD_FL_SINK; + mst->pads[MST3367_PAD_COMPONENT].flags =3D MEDIA_PAD_FL_SINK; + mst->pads[MST3367_PAD_SOURCE].flags =3D MEDIA_PAD_FL_SOURCE; + mst->sd.entity.function =3D MEDIA_ENT_F_DV_DECODER; + + return media_entity_pads_init(&mst->sd.entity, MST3367_NUM_PADS, + mst->pads); +} + +static void mst3367_remove(struct i2c_client *client) +{ + struct v4l2_subdev *sd =3D i2c_get_clientdata(client); + + v4l2_device_unregister_subdev(sd); + media_entity_cleanup(&sd->entity); +} + +static const struct i2c_device_id mst3367_id[] =3D { + { .name =3D "mst3367" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, mst3367_id); + +static struct i2c_driver mst3367_driver =3D { + .driver =3D { + .name =3D "mst3367", + }, + .probe =3D mst3367_probe, + .remove =3D mst3367_remove, + .id_table =3D mst3367_id, +}; +module_i2c_driver(mst3367_driver); + +MODULE_AUTHOR("Matthew Laux "); +MODULE_DESCRIPTION("MStar MST3367 HDMI receiver and component ADC driver"); +MODULE_LICENSE("GPL"); diff --git a/include/media/i2c/mst3367.h b/include/media/i2c/mst3367.h new file mode 100644 index 000000000000..88d4a4874a9d --- /dev/null +++ b/include/media/i2c/mst3367.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * MStar MST3367 HDMI receiver and component ADC + */ + +#ifndef _MEDIA_I2C_MST3367_H_ +#define _MEDIA_I2C_MST3367_H_ + +enum mst3367_pads { + MST3367_PAD_HDMI, + MST3367_PAD_COMPONENT, + MST3367_PAD_SOURCE, + MST3367_NUM_PADS, +}; + +/* Inputs for the s_routing video op */ +#define MST3367_INPUT_HDMI MST3367_PAD_HDMI +#define MST3367_INPUT_COMPONENT MST3367_PAD_COMPONENT +#define MST3367_INPUT_NONE MST3367_NUM_PADS + +/* + * Notification sent with v4l2_subdev_notify() before the receiver changes= its + * hotplug detect output, for bridge devices to use. The argument is a boo= l * + */ +#define MST3367_HOTPLUG 1 + +#endif --=20 2.47.3 From nobody Wed Nov 25 13:32:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795233374; b=kpIhGE1obttn1Hb6bHCQFT9lizxPUXaiWHhlJibiP6NEndmxKhtgSSq4j5TIUUS7Athi u2Z9cKLpyAh6IMH6PjSwZLe6vyrh3vSDtOeViqjJ//3ouHRdUC3n/I/dQs5o+s6nYHM4t M64HFgYqQRIcatZAFXMdo3SjRpjgYr6Uk/ZMnSS+/ndmus7kFhVlEGaGSA6VKhjx/5r4T ORi4eX8SUuXT0Lb1jlYJa7ri1b63HRyMei/EuTSEiaAhDzWm48mkntZtd8ZfQIIgEk39E rdfvO1VMFWLstoy4KjKmzPkm7q0Abiuzoi4m35uPA1NZfiz77jUf4UUGOyr/BLVH6Hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795233374; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=TzMnm+J4OOa30bNLFvLjedg3FPkNnTjaPLKdjs1W0Ec=; b=grE0geCY9nSikY/I5qO9qf7+EfZMw4c9Jv0KLMA26fi1V8ZKZ2pZts+nBFTX71lO5Tlb S7BZgIfIyDqXAygXArMcoqzDbriTtmC8GFuWq7zADvIENu0qJR4zC293D0yhK/LIPLQf/ EaYmsDK090cOpUGJUro+Ti0rIUx2qfa3RRdcQln1QwAxOC42I7Dmibcj0vvzcDTaJ7qhr oEtzNK18NAkEx/3T+XbIlfnch0l8dtpLUzRaXnkaFNSgsAD+0Qsckm0A4SUH/k25rCaUs inYRzU7Rv5z+HNshsJ+fBpBuERQaYJCOpKoyA98hmfg/hc0zV2Ss+yJGB7w/ufajciw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=R0DyLd4Z; arc=none smtp.remote-ip=209.85.161.46 Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 917A960A2618 for ; Sat, 21 Nov 2026 03:56:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 917A960A2618 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=R0DyLd4Z Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-71acdd32a53so665022eaf.3 for ; Fri, 20 Nov 2026 19:56:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795233369; x=1795838169; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=TzMnm+J4OOa30bNLFvLjedg3FPkNnTjaPLKdjs1W0Ec=; b=R0DyLd4Z5gsC1fZMBlyqkb1+AcAM5hi9e91ckjYQSEYwQSm/wm7JpzNcS0Ti6m93co PqQ47Hr8jgUQ7hKLeuR9Hjx2Gfutyk6Nwut4X1SKSrDZbkNf4UqxHuWAJRrvMDGwYEJ4 vRtsY6/9aiQTl3s+daxC+yPv97kdMPdBRpqlT6QeCWkDVPeIlOJrdFTZYr3mdTsGvfWG nAdFrkf8kRo3PkayXDP5wrAzSpFqXNdf5QEYksZJDKgIz38AX0K3IQCKRGGGteNW3YXE x6+a+hJaxMvWrPPeoFS3QUcN7KiPgjnpEMK5tmTqauJxtnXioWlcmtJCGOVfc8BQUM1k wAwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795233369; x=1795838169; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=TzMnm+J4OOa30bNLFvLjedg3FPkNnTjaPLKdjs1W0Ec=; b=2dFZOgVWGm4QyQzSZV8GD/LzxkTu0qlqZI65NW724lAhKmIAa2TElCGc0NUOzunPyB a3qM8IfQWY1Emz4OzFmyAjcntmW2uilugvNI0zmatapvjscTaov3BvHtI7G8oczd1gjC lxTXnaa7/jMjMQ5BcOgP/xEr7lcpNN4pwFs3yTXfew67rYSbolJqntFYdWqZJsOC6BeO DXLqnH/YP/uqI0nx7KIDVLFcJZseCWzDnuXOuGP2D//leVIYaJ8hAphLTz9cu729IDtH IKg2uXtl1vJE05aNsDdC4/WntlWxYxRDeNC1D1y9doCJE1Pl7fNVv0o/9hdAr0FtMmig zAJg== X-Gm-Message-State: APuU4Z8RvwkPXnwFc0kRO+h1xsWeyVzaY2Jt6ROxCFoR9+TvrHIbQ1V7 2u7dQnydmrZjIPOy0Q0pAgWgy39kGj33w2/6sIO2O6t8dn5UtOGx+DQ= X-Gm-Gg: AYBFou06vJd5yr8k8emnjI/+d6p7H5ilN5ssvooYN8NHx1rJMAQrP1B5QU44eip5/5v Lr5/CR2BggXNlS/pYgBS7mEH5q+5HTsTkaFLANV0Zm6/TEb77jtEgSsGY44hXYs7s1rWIt4cezA 0pGp2hqoWOAwQlz2WfCnkyxpPqIl2KmrWgSe5Yfn0J7J11O8SrH5I7zEJvkTDBCywkH0yYeTO1R RB2I1wvCbKWh4fObu79//gSElC//jns6rBRGl5vDLslqv5c75AUndkIyNGjUJ1F2VGWw2J5G6Zq 5FE+X7k5uxBYJ3SbJwLlwYdN6aLxBtHvZ6msxaVWttygzCcXyRYusF6cXuHn8VplQW3xESWou/v atbkkYuukV8lC7zfgMN2t2EA62fwZFoYPaV1CiknUqQe5VcLBbnOVCYOw5OkHWZYbfLUm18PEm8 jPrrSPulVHk0AmJv1ZyVjiKEBpIwXujZl8XTf4nzR/1fz78NKyIERR//myCXz2tn/nJM4da/3yS /CcmQ4r2vGY6nWpco/vaiy8h77pH08bIegFGNudRG4wNTWzBthtTj9wvAMdQjei/wyShVmrT2ZE oCxEGiiZGFEkFBONQr97eYICXhpuuUdjU0WxqIQ39oLP04d00joB8Vf7 X-Received: by 2002:a05:6820:230b:b0:71a:f1c6:181 with SMTP id 006d021491bc7-71b3920f4cfmr3472180eaf.5.1795233368891; Fri, 20 Nov 2026 19:56:08 -0800 (PST) Received: from miso.lan ([136.62.61.36]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b380b6f82sm2878076eaf.10.2026.11.20.19.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2026 19:56:08 -0800 (PST) From: Matthew Laux To: Mauro Carvalho Chehab , Hans Verkuil , Mehdi Djait Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Matthew Laux Subject: [PATCH v7 12/12] media: add driver for StarTech USB3HDCAP Date: Fri, 20 Nov 2026 21:55:23 -0600 Message-ID: <20261121035523.26764-13-matthew.laux@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261121035523.26764-1-matthew.laux@gmail.com> References: <20261121035523.26764-1-matthew.laux@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a V4L2 driver for the StarTech USB3HDCAP video capture device. This is a popular capture card based on the MST3367 HDMI receiver and TW9900 composite video decoder. Support is included for the composite, S-Video, component, and HDMI inputs, and these inputs have been tested at various resolutions. Audio support is provided by an ALSA device. Also includes preliminary support for the Micomsoft XCAPTURE-1 hardware variant. All inputs have been tested on that variant as well except for component. Signed-off-by: Matthew Laux --- MAINTAINERS | 6 + drivers/media/usb/Kconfig | 1 + drivers/media/usb/Makefile | 1 + drivers/media/usb/usb3hdcap/Kconfig | 15 + drivers/media/usb/usb3hdcap/Makefile | 8 + drivers/media/usb/usb3hdcap/usb3hdcap-audio.c | 323 +++++ .../media/usb/usb3hdcap/usb3hdcap-composite.c | 212 +++ drivers/media/usb/usb3hdcap/usb3hdcap-core.c | 1173 +++++++++++++++++ drivers/media/usb/usb3hdcap/usb3hdcap-hdmi.c | 200 +++ drivers/media/usb/usb3hdcap/usb3hdcap-video.c | 499 +++++++ drivers/media/usb/usb3hdcap/usb3hdcap.h | 241 ++++ 11 files changed, 2679 insertions(+) create mode 100644 drivers/media/usb/usb3hdcap/Kconfig create mode 100644 drivers/media/usb/usb3hdcap/Makefile create mode 100644 drivers/media/usb/usb3hdcap/usb3hdcap-audio.c create mode 100644 drivers/media/usb/usb3hdcap/usb3hdcap-composite.c create mode 100644 drivers/media/usb/usb3hdcap/usb3hdcap-core.c create mode 100644 drivers/media/usb/usb3hdcap/usb3hdcap-hdmi.c create mode 100644 drivers/media/usb/usb3hdcap/usb3hdcap-video.c create mode 100644 drivers/media/usb/usb3hdcap/usb3hdcap.h diff --git a/MAINTAINERS b/MAINTAINERS index 6cbe402ee9da..478557056947 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -26880,6 +26880,12 @@ S: Maintained F: Documentation/devicetree/bindings/pinctrl/starfive,jhb1*.yaml F: drivers/pinctrl/starfive/pinctrl-starfive-jhb1* =20 +STARTECH USB3HDCAP VIDEO CAPTURE DRIVER +M: Matthew Laux +L: linux-media@vger.kernel.org +S: Maintained +F: drivers/media/usb/usb3hdcap/ + STATIC BRANCH/CALL M: Peter Zijlstra M: Josh Poimboeuf diff --git a/drivers/media/usb/Kconfig b/drivers/media/usb/Kconfig index 813171d25ac5..9fd043459613 100644 --- a/drivers/media/usb/Kconfig +++ b/drivers/media/usb/Kconfig @@ -16,6 +16,7 @@ if MEDIA_CAMERA_SUPPORT source "drivers/media/usb/gspca/Kconfig" source "drivers/media/usb/pwc/Kconfig" source "drivers/media/usb/s2255/Kconfig" +source "drivers/media/usb/usb3hdcap/Kconfig" source "drivers/media/usb/usbtv/Kconfig" source "drivers/media/usb/uvc/Kconfig" =20 diff --git a/drivers/media/usb/Makefile b/drivers/media/usb/Makefile index 6d171beea20d..a9b7a398addb 100644 --- a/drivers/media/usb/Makefile +++ b/drivers/media/usb/Makefile @@ -29,4 +29,5 @@ obj-$(CONFIG_VIDEO_GO7007) +=3D go7007/ obj-$(CONFIG_VIDEO_HDPVR) +=3D hdpvr/ obj-$(CONFIG_VIDEO_PVRUSB2) +=3D pvrusb2/ obj-$(CONFIG_VIDEO_STK1160) +=3D stk1160/ +obj-$(CONFIG_VIDEO_USB3HDCAP) +=3D usb3hdcap/ obj-$(CONFIG_VIDEO_USBTV) +=3D usbtv/ diff --git a/drivers/media/usb/usb3hdcap/Kconfig b/drivers/media/usb/usb3hd= cap/Kconfig new file mode 100644 index 000000000000..b2c841448eb1 --- /dev/null +++ b/drivers/media/usb/usb3hdcap/Kconfig @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only +config VIDEO_USB3HDCAP + tristate "StarTech USB3HDCAP video capture support" + depends on VIDEO_DEV && I2C && SND + depends on GPIOLIB && PM + select SND_PCM + select VIDEOBUF2_VMALLOC + select VIDEO_MST3367 + select VIDEO_TW9900 + help + This is a video4linux2 driver for the StarTech USB3HDCAP and + Micomsoft XCAPTURE-1 USB 3.0 HD video capture devices. + + To compile this driver as a module, choose M here: the + module will be called usb3hdcap diff --git a/drivers/media/usb/usb3hdcap/Makefile b/drivers/media/usb/usb3h= dcap/Makefile new file mode 100644 index 000000000000..f8d833ea530a --- /dev/null +++ b/drivers/media/usb/usb3hdcap/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +usb3hdcap-y :=3D usb3hdcap-core.o \ + usb3hdcap-video.o \ + usb3hdcap-composite.o \ + usb3hdcap-hdmi.o \ + usb3hdcap-audio.o + +obj-$(CONFIG_VIDEO_USB3HDCAP) +=3D usb3hdcap.o diff --git a/drivers/media/usb/usb3hdcap/usb3hdcap-audio.c b/drivers/media/= usb/usb3hdcap/usb3hdcap-audio.c new file mode 100644 index 000000000000..61c28cd76f21 --- /dev/null +++ b/drivers/media/usb/usb3hdcap/usb3hdcap-audio.c @@ -0,0 +1,323 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * USB3HDCAP audio support - CS53L21 init + ALSA capture device + * + * Audio is embedded in the isochronous video stream as 48kHz stereo + * 16-bit LE PCM. Each video line contains 8, 12, or 16 bytes of audio dat= a. + */ + +#include +#include +#include +#include + +#include "usb3hdcap.h" + +#define HDCAP_AUDIO_BUFSZ (64 * 1024) + +static const struct snd_pcm_hardware snd_hdcap_hw =3D { + .info =3D SNDRV_PCM_INFO_BATCH | + SNDRV_PCM_INFO_MMAP | + SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_BLOCK_TRANSFER | + SNDRV_PCM_INFO_MMAP_VALID, + .formats =3D SNDRV_PCM_FMTBIT_S16_LE, + .rates =3D SNDRV_PCM_RATE_48000, + .rate_min =3D 48000, + .rate_max =3D 48000, + .channels_min =3D 2, + .channels_max =3D 2, + .period_bytes_min =3D 1024, + .period_bytes_max =3D 65536, + .periods_min =3D 2, + .periods_max =3D 128, + .buffer_bytes_max =3D HDCAP_AUDIO_BUFSZ, +}; + +/* ------------------------------------------------------------------ */ +/* PCM ops */ +/* ------------------------------------------------------------------ */ + +static int snd_hdcap_pcm_open(struct snd_pcm_substream *substream) +{ + struct usb3hdcap *hdcap =3D snd_pcm_substream_chip(substream); + + hdcap->snd_substream =3D substream; + substream->runtime->hw =3D snd_hdcap_hw; + + return 0; +} + +static int snd_hdcap_pcm_close(struct snd_pcm_substream *substream) +{ + struct usb3hdcap *hdcap =3D snd_pcm_substream_chip(substream); + + atomic_set(&hdcap->snd_stream, 0); + WRITE_ONCE(hdcap->snd_substream, NULL); + + return 0; +} + +static int snd_hdcap_prepare(struct snd_pcm_substream *substream) +{ + struct usb3hdcap *hdcap =3D snd_pcm_substream_chip(substream); + + hdcap->snd_buffer_pos =3D 0; + hdcap->snd_period_pos =3D 0; + + return 0; +} + +static int snd_hdcap_trigger(struct snd_pcm_substream *substream, int cmd) +{ + struct usb3hdcap *hdcap =3D snd_pcm_substream_chip(substream); + + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + case SNDRV_PCM_TRIGGER_RESUME: + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: + atomic_set(&hdcap->snd_stream, 1); + break; + case SNDRV_PCM_TRIGGER_STOP: + case SNDRV_PCM_TRIGGER_SUSPEND: + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: + atomic_set(&hdcap->snd_stream, 0); + break; + default: + return -EINVAL; + } + + return 0; +} + +static snd_pcm_uframes_t snd_hdcap_pointer(struct snd_pcm_substream *subst= ream) +{ + struct usb3hdcap *hdcap =3D snd_pcm_substream_chip(substream); + + return hdcap->snd_buffer_pos; +} + +static const struct snd_pcm_ops snd_hdcap_pcm_ops =3D { + .open =3D snd_hdcap_pcm_open, + .close =3D snd_hdcap_pcm_close, + .prepare =3D snd_hdcap_prepare, + .trigger =3D snd_hdcap_trigger, + .pointer =3D snd_hdcap_pointer, +}; + +/* ------------------------------------------------------------------ */ +/* Audio data feed */ +/* ------------------------------------------------------------------ */ + +void usb3hdcap_audio_data(struct usb3hdcap *hdcap, const u8 *data, int len) +{ + struct snd_pcm_substream *substream; + struct snd_pcm_runtime *runtime; + size_t frame_bytes, nframes; + size_t buffer_pos, period_pos; + unsigned long flags; + + substream =3D READ_ONCE(hdcap->snd_substream); + if (!substream) + return; + + snd_pcm_stream_lock_irqsave(substream, flags); + + if (!atomic_read(&hdcap->snd_stream)) + goto out; + + runtime =3D substream->runtime; + if (!runtime || !runtime->dma_area) + goto out; + + frame_bytes =3D runtime->frame_bits >> 3; + nframes =3D len / frame_bytes; + if (!nframes) + goto out; + + buffer_pos =3D hdcap->snd_buffer_pos; + period_pos =3D hdcap->snd_period_pos; + + /* Copy into ALSA ring buffer, handling wrap */ + if (buffer_pos + nframes >=3D runtime->buffer_size) { + size_t cnt =3D (runtime->buffer_size - buffer_pos) * frame_bytes; + + memcpy(runtime->dma_area + buffer_pos * frame_bytes, data, cnt); + memcpy(runtime->dma_area, data + cnt, nframes * frame_bytes - cnt); + } else { + memcpy(runtime->dma_area + buffer_pos * frame_bytes, data, + nframes * frame_bytes); + } + + buffer_pos +=3D nframes; + period_pos +=3D nframes; + + if (buffer_pos >=3D runtime->buffer_size) + buffer_pos -=3D runtime->buffer_size; + + hdcap->snd_buffer_pos =3D buffer_pos; + + if (period_pos >=3D runtime->period_size) { + hdcap->snd_period_pos =3D period_pos - runtime->period_size; + snd_pcm_period_elapsed_under_stream_lock(substream); + } else { + hdcap->snd_period_pos =3D period_pos; + } + +out: + snd_pcm_stream_unlock_irqrestore(substream, flags); +} + +/* ------------------------------------------------------------------ */ +/* CS53L21 codec init */ +/* ------------------------------------------------------------------ */ + +int usb3hdcap_cs53l21_init(struct usb3hdcap *hdcap) +{ + bool is_xcapture =3D + le16_to_cpu(hdcap->usb_dev->descriptor.idProduct) =3D=3D XCAPTURE1_PID; + u8 in_sel =3D 0x00; + u8 ch_mixer =3D 0x00; + int ret; + + if (is_xcapture) { + /* + * composite/S-Video audio is on AIN2A/AIN2B, and for the other inputs + * the ADC channels are swapped + */ + if (hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SVI= DEO) + in_sel =3D 0x50; + else + ch_mixer =3D 0x0f; + } + + /* MIC power control: power down mic B, mic A, and bias */ + /* SPEED =3D "10 - Half-Speed Mode (HSM) - 12.5 to 25 kHz sample rates" */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_MIC_PWR_CTL, 0x4e); + if (ret) + return ret; + + /* interface: I2S, master */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_IFACE_CTL, 0x44); + if (ret) + return ret; + + /* ADC input select: AIN1x, or AIN2x for XCAPTURE-1 SD inputs */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ADC_IN_SEL, in_sel); + if (ret) + return ret; + + /* ALC/PGA: 0 dB */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ALC_PGAA, 0x00); + if (ret) + return ret; + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ALC_PGAB, 0x00); + if (ret) + return ret; + + /* ADC attenuators: 0 dB */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ADCA_ATT, 0x00); + if (ret) + return ret; + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ADCB_ATT, 0x00); + if (ret) + return ret; + + /* ALC enable and attack rate =3D fastest attack */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ALC_EN_ATK, 0xc0); + if (ret) + return ret; + + /* interface: "SPE Processed ADC data to ADC serial port, SDOUT data." */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_IFACE_CTL, 0x46); + if (ret) + return ret; + + /* SPE control: SPE enable, "Soft Ramp" turned on */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_SPE_CTL, 0x42); + if (ret) + return ret; + + /* ADC mixer volume: +2.5 dB */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ADCA_MIX_VOL, 0x05); + if (ret) + return ret; + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_ADCB_MIX_VOL, 0x05); + if (ret) + return ret; + + /* channel mixer: off, or ADCA/ADCB swapped */ + ret =3D u3hc_i2c_write(hdcap, ADDR_CS53L21, CS53L21_CH_MIXER, ch_mixer); + if (ret) + return ret; + + return 0; +} + +/* ------------------------------------------------------------------ */ +/* ALSA card init / teardown */ +/* ------------------------------------------------------------------ */ + +static void usb3hdcap_snd_free(struct snd_card *card) +{ + struct usb3hdcap *hdcap =3D card->private_data; + + v4l2_device_put(&hdcap->v4l2_dev); +} + +int usb3hdcap_audio_init(struct usb3hdcap *hdcap) +{ + int ret; + struct snd_card *card; + struct snd_pcm *pcm; + + atomic_set(&hdcap->snd_stream, 0); + + ret =3D snd_card_new(hdcap->dev, SNDRV_DEFAULT_IDX1, "usb3hdcap", + THIS_MODULE, 0, &card); + if (ret < 0) + return ret; + + strscpy(card->driver, "usb3hdcap", sizeof(card->driver)); + strscpy(card->shortname, "USB3HDCAP Audio", sizeof(card->shortname)); + snprintf(card->longname, sizeof(card->longname), + "USB3HDCAP Audio at bus %d device %d", + hdcap->usb_dev->bus->busnum, hdcap->usb_dev->devnum); + + hdcap->snd =3D card; + card->private_data =3D hdcap; + card->private_free =3D usb3hdcap_snd_free; + v4l2_device_get(&hdcap->v4l2_dev); + + ret =3D snd_pcm_new(card, "USB3HDCAP Audio", 0, 0, 1, &pcm); + if (ret < 0) + goto err; + + strscpy(pcm->name, "USB3HDCAP Audio Input", sizeof(pcm->name)); + pcm->info_flags =3D 0; + pcm->private_data =3D hdcap; + + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdcap_pcm_ops); + snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, NULL, + HDCAP_AUDIO_BUFSZ, HDCAP_AUDIO_BUFSZ); + + ret =3D snd_card_register(card); + if (ret) + goto err; + + dev_info(hdcap->dev, "ALSA audio device registered\n"); + return 0; + +err: + hdcap->snd =3D NULL; + snd_card_free(card); + return ret; +} + +void usb3hdcap_audio_free(struct usb3hdcap *hdcap) +{ + if (hdcap->snd && hdcap->usb_dev) { + snd_card_free_when_closed(hdcap->snd); + hdcap->snd =3D NULL; + } +} diff --git a/drivers/media/usb/usb3hdcap/usb3hdcap-composite.c b/drivers/me= dia/usb/usb3hdcap/usb3hdcap-composite.c new file mode 100644 index 000000000000..dfc773776e5d --- /dev/null +++ b/drivers/media/usb/usb3hdcap/usb3hdcap-composite.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * USB3HDCAP composite video init - CPLD + TW9900 setup, signal lock + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "usb3hdcap.h" + +/* Main TW9900 configuration */ +static const struct tw9900_regval tw9900_init_regs[] =3D { + { TW9900_ACNTL_I, 0x00 }, + { TW9900_OPFORM, 0xa7 }, + { TW9900_OPCNTL_I, 0x01 }, + { TW9900_VDELAY_LO, 0x14 }, + { TW9900_VACTIVE_LO, 0xf2 }, + { TW9900_HDELAY_LO, 0x0b }, + { TW9900_HACTIVE_LO, 0xd2 }, + { TW9900_VBICNTL, 0x57 }, + { TW9900_ACNTL_II, 0x0f }, + { TW9900_OPCNTL_II, 0x00 }, + { TW9900_SDT, 0x07 }, /* auto-detect */ + { TW9900_VCNTL1, 0x0c }, + { TW9900_VCNTL2, 0x03 }, + { TW9900_MISC1, 0x07 }, + { TW9900_MISC2, 0x06 }, + { TW9900_ANAPLLCTL, 0x01 }, + { TW9900_VVBI, 0x00 }, + { TW9900_HSBEGIN, 0x26 }, + { TW9900_HSEND, 0x36 }, + { TW9900_OVSDLY, 0xf0 }, + { TW9900_OVSEND, 0x28 }, + { TW9900_ACNTL_I, 0x80 }, + /* "internal current reference 2" */ + { TW9900_ACNTL_I, 0x40 }, +}; + +/* Adjust for PAL vs NTSC */ +static const struct tw9900_regval tw9900_pal_regs[] =3D { + { TW9900_CROP_HI, 0x12 }, + { TW9900_VDELAY_LO, 0x19 }, + { TW9900_VACTIVE_LO, 0x20 }, + { TW9900_HDELAY_LO, 0x0a }, + { TW9900_HACTIVE_LO, 0xd0 }, + { TW9900_VVBI, 0x00 }, + { TW9900_CNTRL2, 0x00 }, + { TW9900_SDT_NOISE, 0x11 }, + { TW9900_LUMA_CTL, 0x00 }, +}; + +static const struct tw9900_regval tw9900_ntsc_regs[] =3D { + { TW9900_CROP_HI, 0x02 }, + { TW9900_VDELAY_LO, 0x14 }, + { TW9900_VACTIVE_LO, 0xf2 }, + { TW9900_HDELAY_LO, 0x10 }, + { TW9900_HACTIVE_LO, 0xd0 }, + { TW9900_VVBI, 0x10 }, + { TW9900_CNTRL2, 0x00 }, + { TW9900_SDT_NOISE, 0x11 }, + { TW9900_LUMA_CTL, 0x00 }, +}; + +static struct tw9900_platform_data tw9900_pdata =3D { + .init_regs =3D tw9900_init_regs, + .n_init_regs =3D ARRAY_SIZE(tw9900_init_regs), + .ntsc_regs =3D tw9900_ntsc_regs, + .n_ntsc_regs =3D ARRAY_SIZE(tw9900_ntsc_regs), + .pal_regs =3D tw9900_pal_regs, + .n_pal_regs =3D ARRAY_SIZE(tw9900_pal_regs), +}; + +static struct i2c_board_info tw9900_info =3D { + I2C_BOARD_INFO("tw9900", ADDR_TW9900 >> 1), + .platform_data =3D &tw9900_pdata, +}; + +int usb3hdcap_composite_register(struct usb3hdcap *hdcap) +{ + hdcap->sd_tw9900 =3D v4l2_i2c_new_subdev_board(&hdcap->v4l2_dev, + &hdcap->i2c_adap, + &tw9900_info, NULL); + if (!hdcap->sd_tw9900) { + dev_warn(hdcap->dev, "Could not register TW9900\n"); + return -ENODEV; + } + + hdcap->ctrl_brightness =3D v4l2_ctrl_find(&hdcap->ctrl, + V4L2_CID_BRIGHTNESS); + hdcap->ctrl_contrast =3D v4l2_ctrl_find(&hdcap->ctrl, V4L2_CID_CONTRAST); + hdcap->ctrl_saturation =3D v4l2_ctrl_find(&hdcap->ctrl, + V4L2_CID_SATURATION); + hdcap->ctrl_hue =3D v4l2_ctrl_find(&hdcap->ctrl, V4L2_CID_HUE); + hdcap->ctrl_sharpness =3D v4l2_ctrl_find(&hdcap->ctrl, + V4L2_CID_SHARPNESS); + if (!hdcap->ctrl_brightness || !hdcap->ctrl_contrast || + !hdcap->ctrl_saturation || !hdcap->ctrl_hue || + !hdcap->ctrl_sharpness) + return -ENODEV; + + v4l2_ctrl_s_ctrl(hdcap->ctrl_brightness, -9); + v4l2_ctrl_s_ctrl(hdcap->ctrl_contrast, 0x79); + v4l2_ctrl_s_ctrl(hdcap->ctrl_sharpness, 0x02); + + return 0; +} + +int usb3hdcap_composite_querystd(struct usb3hdcap *hdcap, v4l2_std_id *std) +{ + v4l2_std_id detected =3D V4L2_STD_UNKNOWN; + int ret; + + *std =3D V4L2_STD_UNKNOWN; + + ret =3D v4l2_subdev_call(hdcap->sd_tw9900, video, querystd, &detected); + if (ret =3D=3D -ETIMEDOUT) + return 0; + if (ret < 0) + return ret; + + if (detected & V4L2_STD_NTSC) + *std =3D V4L2_STD_NTSC; + else if (detected & V4L2_STD_PAL) + *std =3D V4L2_STD_PAL; + return 0; +} + +/* Make std the current standard and derive the capture format from it */ +int usb3hdcap_composite_set_std(struct usb3hdcap *hdcap, v4l2_std_id std) +{ + struct v4l2_subdev_format fmt =3D { + .which =3D V4L2_SUBDEV_FORMAT_ACTIVE, + }; + int full_h =3D (std & V4L2_STD_PAL) ? PAL_HEIGHT : NTSC_HEIGHT; + int half_h =3D full_h / 2; + int ret; + + ret =3D v4l2_subdev_call(hdcap->sd_tw9900, video, s_std, std); + if (ret < 0) + return ret; + + hdcap->std =3D std; + hdcap->width =3D SD_WIDTH; + hdcap->height =3D half_h; + hdcap->bpl =3D hdcap->width * 2; + + ret =3D v4l2_subdev_call(hdcap->sd_tw9900, pad, get_fmt, NULL, &fmt); + hdcap->interlaced =3D !(ret >=3D 0 && fmt.format.height =3D=3D half_h); + + dev_dbg(hdcap->dev, "%s %d%s\n", + (std & V4L2_STD_PAL) ? "PAL" : "NTSC", + hdcap->interlaced ? full_h : half_h, + hdcap->interlaced ? "i" : "p"); + return 0; +} + +int usb3hdcap_composite_init(struct usb3hdcap *hdcap) +{ + v4l2_std_id std =3D V4L2_STD_UNKNOWN; + int k, ret; + u32 status =3D 0; + + /* disable MST3367 */ + v4l2_subdev_call(hdcap->sd_mst3367, video, s_routing, + MST3367_INPUT_NONE, 0, 0); + + /* select TW9900 input of cpld? */ + /* from windows driver */ + if (u3hc_i2c_read(hdcap, ADDR_CPLD, 0x3b) !=3D 0) + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x3b, 0x80); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x20, 0x05); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x00, 0x01); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x10, hdcap->has_mcu ? 0xfc : 0xfe); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x20, 0x75); + + ret =3D v4l2_subdev_call(hdcap->sd_tw9900, video, s_routing, + hdcap->input =3D=3D INPUT_SVIDEO ? + TW9900_INPUT_SVIDEO : TW9900_INPUT_COMPOSITE, + 0, 0); + if (ret < 0) + return ret; + + for (k =3D 0; k < 10; k++) { + ret =3D v4l2_subdev_call(hdcap->sd_tw9900, video, g_input_status, + &status); + if (ret >=3D 0 && !status) { + dev_dbg(hdcap->dev, "Signal locked (%d ms)\n", k * 100); + break; + } + msleep(100); + } + + if (k < 10) { + ret =3D usb3hdcap_composite_querystd(hdcap, &std); + if (ret < 0) + return ret; + } else { + dev_dbg(hdcap->dev, "No signal lock after 1s (status=3D0x%02x)\n", + status); + } + + hdcap->no_signal =3D std =3D=3D V4L2_STD_UNKNOWN; + if (hdcap->no_signal) + std =3D V4L2_STD_NTSC; + + return usb3hdcap_composite_set_std(hdcap, std); +} diff --git a/drivers/media/usb/usb3hdcap/usb3hdcap-core.c b/drivers/media/u= sb/usb3hdcap/usb3hdcap-core.c new file mode 100644 index 000000000000..f8f8687d4534 --- /dev/null +++ b/drivers/media/usb/usb3hdcap/usb3hdcap-core.c @@ -0,0 +1,1173 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * StarTech USB3HDCAP USB 3.0 HD Video Capture Driver + * a.k.a. YUAN High-Tech Development Co., Ltd UB530 + * + * Supported inputs are composite, S-Video, component, and HDMI. I haven't + * attempted to support DVI or VGA. + * + * Tested video modes (all at 60fps): + * composite 240p and 480i + * S-Video 240p and 480i + * component 240p, 480i, 480p, 720p, 1080i + * HDMI 1080p + * + * Other video modes will probably "just work" if the appropriate entries + * are added in the mode tables and code is added to disambiguate using + * htotal if necessary. + * + * Audio is supported and should work with all inputs. + * + * Also partially supports the Micomsoft XCAPTURE-1 for composite, S-Video, + * and HDMI... component is untested but should work. + * + * Based on USBPcap analysis and reverse engineering of CY3014.X64.SYS + * (version 1.1.0.193 of Fri, 04 Jun 2021 09:37:45 UTC) + * MST3367 HDMI path based on hdcapm by Steven Toth + * Driver structure based heavily on usbtv + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "usb3hdcap.h" + +/* ------------------------------------------------------------------ */ +/* USB control transfer helpers */ +/* ------------------------------------------------------------------ */ + +int vendor_out(struct usb3hdcap *hdcap, u8 request, u16 value, u16 index, + u8 *data, u16 len) +{ + int ret; + u8 *buf =3D NULL; + + if (len > 0) { + buf =3D kmemdup(data, len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + } + + ret =3D usb_control_msg(hdcap->usb_dev, + usb_sndctrlpipe(hdcap->usb_dev, 0), + request, + USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + value, index, buf, len, 1000); + + kfree(buf); + + if (ret < 0) { + dev_err(hdcap->dev, "%s 0x%02x val=3D0x%04x idx=3D0x%04x: %d\n", + __func__, request, value, index, ret); + return ret; + } + + return 0; +} + +static int vendor_in(struct usb3hdcap *hdcap, u8 request, u16 value, u16 i= ndex, + u8 *data, u16 len) +{ + u8 *buf; + int ret; + + buf =3D kmalloc(len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret =3D usb_control_msg(hdcap->usb_dev, + usb_rcvctrlpipe(hdcap->usb_dev, 0), + request, + USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + value, index, buf, len, 1000); + + if (ret >=3D 0 && ret < len) { + dev_err(hdcap->dev, + "%s 0x%02x val=3D0x%04x idx=3D0x%04x: short read %d/%d\n", + __func__, request, value, index, ret, len); + ret =3D -EIO; + } + if (ret >=3D 0) + memcpy(data, buf, len); + + kfree(buf); + return ret; +} + +/* ------------------------------------------------------------------ */ +/* MCU I2C tunnel (XCAPTURE-1) */ +/* */ +/* The Nuvoton NUC100 MCU intercepts I2C traffic. Transactions are */ +/* tunneled through a different USB vendor protocol: */ +/* Write: bRequest=3D0xC0, wValue=3D0x5066 or addr|0x5000 = */ +/* Read: two-phase write+read, always through wValue=3D0x5066 */ +/* */ +/* Some addresses need the 0x66 "gateway" (target addr in payload), */ +/* others use the I2C addr directly in wValue with 0x5000 flag. */ +/* ------------------------------------------------------------------ */ + +static bool mcu_use_gateway(u8 addr) +{ + return ((u8)(addr + 0x78) & 0xeb) || addr =3D=3D 0x8c; +} + +static int mcu_i2c_write(struct usb3hdcap *hdcap, u8 addr, u8 reg, u8 val) +{ + u8 buf[3]; + u16 wvalue; + int len; + int ret; + + if (mcu_use_gateway(addr)) { + buf[0] =3D addr; + buf[1] =3D reg; + buf[2] =3D val; + wvalue =3D 0x5066; + len =3D 3; + } else { + buf[0] =3D reg; + buf[1] =3D val; + wvalue =3D addr | 0x5000; + len =3D 2; + } + + mutex_lock(&hdcap->mcu_lock); + ret =3D vendor_out(hdcap, REQ_I2C, wvalue, 0, buf, len); + mutex_unlock(&hdcap->mcu_lock); + return ret; +} + +static int mcu_i2c_read(struct usb3hdcap *hdcap, u8 addr, u8 reg) +{ + u8 tx[3] =3D { addr | 1, 1, reg }; + u8 val; + int ret; + + mutex_lock(&hdcap->mcu_lock); + + /* send target addr, rx length, and register */ + ret =3D vendor_out(hdcap, REQ_I2C, 0x5066, 0, tx, 3); + if (ret < 0) + goto out; + + /* Retrieve the result */ + ret =3D vendor_in(hdcap, REQ_I2C, 0x5066, 0, &val, 1); + if (ret < 0) { + dev_err(hdcap->dev, "%s 0x%02x reg 0x%02x: %d\n", + __func__, addr, reg, ret); + goto out; + } + ret =3D val; + +out: + mutex_unlock(&hdcap->mcu_lock); + return ret; +} + +int u3hc_i2c_write(struct usb3hdcap *hdcap, u8 addr, u8 reg, u8 val) +{ + if (addr =3D=3D ADDR_MST3367 && reg =3D=3D 0x00) + hdcap->mst_current_bank =3D val; + + if (hdcap->has_mcu) { + if (addr =3D=3D ADDR_MST3367 && reg !=3D 0x00) + mcu_i2c_write(hdcap, ADDR_MST3367, 0x00, hdcap->mst_current_bank); + return mcu_i2c_write(hdcap, addr, reg, val); + } + return vendor_out(hdcap, REQ_I2C, addr, reg, &val, 1); +} + +int u3hc_i2c_read(struct usb3hdcap *hdcap, u8 addr, u8 reg) +{ + u8 val; + int ret; + + if (hdcap->has_mcu) + return mcu_i2c_read(hdcap, addr, reg); + + ret =3D vendor_in(hdcap, REQ_I2C, addr, reg, &val, 1); + if (ret < 0) { + dev_err(hdcap->dev, "%s 0x%02x reg 0x%02x: %d\n", + __func__, addr, reg, ret); + return ret; + } + + return val; +} + +int u3hc_i2c_rmw(struct usb3hdcap *hdcap, u8 addr, u8 reg, u8 mask, u8 bit= s) +{ + int val =3D u3hc_i2c_read(hdcap, addr, reg); + + if (val < 0) + return val; + return u3hc_i2c_write(hdcap, addr, reg, (val & mask) | bits); +} + +/* ------------------------------------------------------------------ */ +/* I2C adapter for the TW9900 and MST3367 sub-devices */ +/* ------------------------------------------------------------------ */ + +static int usb3hdcap_smbus_xfer(struct i2c_adapter *adap, u16 addr, + unsigned short flags, char read_write, + u8 command, int size, + union i2c_smbus_data *data) +{ + struct usb3hdcap *hdcap =3D i2c_get_adapdata(adap); + int ret; + + if (!hdcap->usb_dev) + return -ENODEV; + + if (size !=3D I2C_SMBUS_BYTE_DATA) + return -EOPNOTSUPP; + + if (read_write =3D=3D I2C_SMBUS_WRITE) + return u3hc_i2c_write(hdcap, addr << 1, command, data->byte); + + ret =3D u3hc_i2c_read(hdcap, addr << 1, command); + if (ret < 0) + return ret; + + data->byte =3D ret; + return 0; +} + +static u32 usb3hdcap_i2c_functionality(struct i2c_adapter *adap) +{ + return I2C_FUNC_SMBUS_BYTE_DATA; +} + +static const struct i2c_algorithm usb3hdcap_i2c_algo =3D { + .smbus_xfer =3D usb3hdcap_smbus_xfer, + .functionality =3D usb3hdcap_i2c_functionality, +}; + +static int usb3hdcap_i2c_register(struct usb3hdcap *hdcap) +{ + hdcap->i2c_adap.owner =3D THIS_MODULE; + hdcap->i2c_adap.algo =3D &usb3hdcap_i2c_algo; + hdcap->i2c_adap.dev.parent =3D hdcap->dev; + strscpy(hdcap->i2c_adap.name, "usb3hdcap", sizeof(hdcap->i2c_adap.name)); + i2c_set_adapdata(&hdcap->i2c_adap, hdcap); + + return i2c_add_adapter(&hdcap->i2c_adap); +} + +/* CheckIsMCUExist() */ +static void usb3hdcap_probe_mcu(struct usb3hdcap *hdcap) +{ + u8 tx[5] =3D { 0xab, 0x03, 0x12, 0x34, 0x57 }; + u8 rx[3] =3D {}; + int k, ret; + + hdcap->has_mcu =3D 0; + + /* Assert CPLD GPIO 0x39 to wake MCU? */ + vendor_out(hdcap, REQ_GPIO, 0xc039, 0, NULL, 0); + vendor_out(hdcap, REQ_GPIO, 0x4134, 0, NULL, 0); + + /* Poll GPIO 0x39 until MCU signals ready */ + for (k =3D 0; k < 50; k++) { + msleep(20); + ret =3D vendor_in(hdcap, REQ_GPIO, 0x39, 0, rx, 1); + if (ret >=3D 0 && rx[0] =3D=3D 1) + break; + } + dev_dbg(hdcap->dev, "MCU freed, wait count=3D%d\n", k); + + /* repeatedly send {0x12, 0x34, 0x57} until response received */ + for (k =3D 0; k < 200; k++) { + ret =3D vendor_out(hdcap, REQ_I2C, 0x5066, 0, tx, sizeof(tx)); + if (ret < 0) + return; + + memset(rx, 0, sizeof(rx)); + ret =3D vendor_in(hdcap, REQ_I2C, 0x5066, 0, rx, 3); + if (ret < 0) + return; + + if (rx[0] =3D=3D 'Q' && rx[1] =3D=3D 0x10) { + hdcap->has_mcu =3D 1; + dev_info(hdcap->dev, + "Nuvoton NUC100 MCU detected after %d tries\n", + k + 1); + return; + } + + /* still starting up, ask again in 100 ms */ + if (rx[0] !=3D 0x33 || rx[1] !=3D 0x44 || rx[2] !=3D 0x55) + break; + msleep(100); + } + + if (k =3D=3D 200) + dev_warn(hdcap->dev, "MCU never became ready\n"); + else + dev_info(hdcap->dev, + "Unrecognized MCU probe response: %02x %02x %02x\n", + rx[0], rx[1], rx[2]); +} + +/* Device-level init + GPIO pin state? shared between all inputs */ +static int usb3hdcap_device_init(struct usb3hdcap *hdcap) +{ + bool is_xcapture =3D + le16_to_cpu(hdcap->usb_dev->descriptor.idProduct) =3D=3D XCAPTURE1_PID; + + if (is_xcapture) { + /* + * XCAPTURE-1: The Windows driver reads REQ_INIT and + * sends this when the read fails or returns non-zero, purpose unknown + */ + u8 unk_ec[] =3D { + 0xb8, 0x22, 0xb1, 0xd2, 0x03, 0xc0, 0xff, 0x00, 0x00, 0xca, + }; + u8 state =3D 0; + + if (vendor_in(hdcap, REQ_INIT, 0x0000, 0, &state, 1) < 0 || state) + vendor_out(hdcap, REQ_UNK_EC, 0x0000, 0, + unk_ec, sizeof(unk_ec)); + } + + vendor_out(hdcap, REQ_INIT, 0x0000, 0, NULL, 0); + vendor_out(hdcap, REQ_UNK_C7, 0x0064, 0, NULL, 0); + /* Windows driver does this but is always fails? */ + /* vendor_out(hdcap, 0xc5, 0x0000, 0, NULL, 0); */ + + /* + * just replay what the windows driver does: + * FUN_14022e3f4(param_1,0x402d); + * FUN_14022e3f4(param_1,0x12d); + * FUN_140001220(0x32); + * FUN_14022e3f4(param_1,0x2d); + * FUN_140001220(0x32); + * FUN_14022e3f4(param_1,0x12d); + * FUN_140001220(0x32); + * FUN_14022e3f4(param_1,0x802d); + */ + + vendor_out(hdcap, REQ_GPIO, 0x402d, 0, NULL, 0); + vendor_out(hdcap, REQ_GPIO, 0x012d, 0, NULL, 0); + msleep(50); + vendor_out(hdcap, REQ_GPIO, 0x002d, 0, NULL, 0); + msleep(50); + vendor_out(hdcap, REQ_GPIO, 0x012d, 0, NULL, 0); + msleep(50); + vendor_out(hdcap, REQ_GPIO, 0x802d, 0, NULL, 0); + + if (!hdcap->has_mcu && is_xcapture) + usb3hdcap_probe_mcu(hdcap); + + return 0; +} + +/* + * Route the current input and make it the current standard/timings. + * Fall back to a default if there is no signal. + * Only fails if the hardware can't be set up. + */ +static int usb3hdcap_hw_init(struct usb3hdcap *hdcap) +{ + usb3hdcap_cs53l21_init(hdcap); + + switch (hdcap->input) { + case INPUT_HDMI: + return usb3hdcap_hdmi_init(hdcap); + case INPUT_COMPONENT: + return usb3hdcap_component_init(hdcap); + case INPUT_COMPOSITE: + case INPUT_SVIDEO: + default: + return usb3hdcap_composite_init(hdcap); + } +} + +/* ------------------------------------------------------------------ */ +/* V4L2 ioctls */ +/* ------------------------------------------------------------------ */ + +static int usb3hdcap_querycap(struct file *file, void *priv, + struct v4l2_capability *cap) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + strscpy(cap->driver, "usb3hdcap", sizeof(cap->driver)); + strscpy(cap->card, hdcap->has_mcu ? + "Micomsoft XCAPTURE-1" : "StarTech USB3HDCAP", + sizeof(cap->card)); + usb_make_path(hdcap->usb_dev, cap->bus_info, sizeof(cap->bus_info)); + return 0; +} + +static int usb3hdcap_enum_input(struct file *file, void *priv, + struct v4l2_input *i) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (i->index > INPUT_HDMI) + return -EINVAL; + + i->type =3D V4L2_INPUT_TYPE_CAMERA; + /* as of the last S_INPUT, QUERYSTD or QUERY_DV_TIMINGS */ + if (i->index =3D=3D hdcap->input && hdcap->no_signal) + i->status =3D V4L2_IN_ST_NO_SIGNAL; + switch (i->index) { + case INPUT_COMPOSITE: + strscpy(i->name, "Composite", sizeof(i->name)); + i->std =3D USB3HDCAP_V4L2_STDS; + i->capabilities =3D V4L2_IN_CAP_STD; + break; + case INPUT_SVIDEO: + strscpy(i->name, "S-Video", sizeof(i->name)); + i->std =3D USB3HDCAP_V4L2_STDS; + i->capabilities =3D V4L2_IN_CAP_STD; + break; + case INPUT_COMPONENT: + strscpy(i->name, "Component", sizeof(i->name)); + i->std =3D 0; + i->capabilities =3D V4L2_IN_CAP_DV_TIMINGS; + break; + case INPUT_HDMI: + strscpy(i->name, "HDMI", sizeof(i->name)); + i->std =3D 0; + i->capabilities =3D V4L2_IN_CAP_DV_TIMINGS; + break; + } + return 0; +} + +static int usb3hdcap_enum_fmt_vid_cap(struct file *file, void *priv, + struct v4l2_fmtdesc *f) +{ + if (f->index > 0) + return -EINVAL; + + f->pixelformat =3D V4L2_PIX_FMT_YUYV; + return 0; +} + +static int usb3hdcap_fmt_vid_cap(struct file *file, void *priv, + struct v4l2_format *f) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + f->fmt.pix.width =3D hdcap->width; + f->fmt.pix.height =3D hdcap->height; + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_YUYV; + f->fmt.pix.field =3D hdcap->interlaced ? + V4L2_FIELD_ALTERNATE : V4L2_FIELD_NONE; + f->fmt.pix.bytesperline =3D hdcap->bpl; + f->fmt.pix.sizeimage =3D hdcap->bpl * hdcap->height; + if (hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SVID= EO || + hdcap->mst_bt601) + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SMPTE170M; + else + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_REC709; + + return 0; +} + +static int usb3hdcap_g_std(struct file *file, void *priv, v4l2_std_id *nor= m) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (!(hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SV= IDEO)) + return -ENODATA; + + *norm =3D hdcap->std; + return 0; +} + +static int usb3hdcap_s_std(struct file *file, void *priv, v4l2_std_id norm) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (!(hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SV= IDEO)) + return -ENODATA; + + if (!(norm & USB3HDCAP_V4L2_STDS)) + return -EINVAL; + + if (norm =3D=3D hdcap->std) + return 0; + + if (vb2_is_busy(&hdcap->vb2q)) + return -EBUSY; + + return usb3hdcap_composite_set_std(hdcap, norm); +} + +static int usb3hdcap_querystd(struct file *file, void *priv, v4l2_std_id *= norm) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + int ret; + + if (!(hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SV= IDEO)) + return -ENODATA; + + ret =3D usb3hdcap_composite_querystd(hdcap, norm); + if (ret < 0) + return ret; + + hdcap->no_signal =3D *norm =3D=3D V4L2_STD_UNKNOWN; + return 0; +} + +/* ------------------------------------------------------------------ */ +/* DV timings (HDMI + HD component) */ +/* ------------------------------------------------------------------ */ + +static unsigned int usb3hdcap_mst3367_pad(struct usb3hdcap *hdcap) +{ + return hdcap->input =3D=3D INPUT_HDMI ? + MST3367_PAD_HDMI : MST3367_PAD_COMPONENT; +} + +static bool usb3hdcap_component_timings_supported(const struct v4l2_dv_tim= ings *t, + void *handle) +{ + struct usb3hdcap *hdcap =3D handle; + struct v4l2_enum_dv_timings e =3D { .pad =3D MST3367_PAD_COMPONENT }; + + while (!v4l2_subdev_call(hdcap->sd_mst3367, pad, enum_dv_timings, &e)) { + if (v4l2_match_dv_timings(t, &e.timings, 0, false)) + return true; + e.index++; + } + return false; +} + +static int usb3hdcap_g_dv_timings(struct file *file, void *priv, + struct v4l2_dv_timings *timings) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (!(hdcap->input =3D=3D INPUT_COMPONENT || hdcap->input =3D=3D INPUT_HD= MI)) + return -ENODATA; + + *timings =3D hdcap->timings; + return 0; +} + +static int usb3hdcap_s_dv_timings(struct file *file, void *priv, + struct v4l2_dv_timings *timings) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + struct v4l2_dv_timings_cap cap =3D {}; + bool valid; + int ret; + + if (!(hdcap->input =3D=3D INPUT_COMPONENT || hdcap->input =3D=3D INPUT_HD= MI)) + return -ENODATA; + + cap.pad =3D usb3hdcap_mst3367_pad(hdcap); + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, dv_timings_cap, &cap); + if (ret < 0) + return ret; + + if (hdcap->input =3D=3D INPUT_COMPONENT) + valid =3D v4l2_valid_dv_timings(timings, &cap, + usb3hdcap_component_timings_supported, + hdcap); + else + valid =3D v4l2_valid_dv_timings(timings, &cap, NULL, NULL); + + if (!valid) + return -ERANGE; + + if (v4l2_match_dv_timings(timings, &hdcap->timings, 0, false)) + return 0; + + if (vb2_is_busy(&hdcap->vb2q)) + return -EBUSY; + + return usb3hdcap_hd_set_timings(hdcap, timings); +} + +static int usb3hdcap_enum_dv_timings(struct file *file, void *priv, + struct v4l2_enum_dv_timings *timings) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + int ret; + + if (!(hdcap->input =3D=3D INPUT_COMPONENT || hdcap->input =3D=3D INPUT_HD= MI)) + return -ENODATA; + + timings->pad =3D usb3hdcap_mst3367_pad(hdcap); + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, enum_dv_timings, timings= ); + timings->pad =3D 0; + return ret; +} + +static int usb3hdcap_dv_timings_cap(struct file *file, void *priv, + struct v4l2_dv_timings_cap *cap) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + int ret; + + /* if you're not on HDMI/component, DV timings are not supported */ + if (!(hdcap->input =3D=3D INPUT_COMPONENT || hdcap->input =3D=3D INPUT_HD= MI)) + return -ENODATA; + + cap->pad =3D usb3hdcap_mst3367_pad(hdcap); + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, dv_timings_cap, cap); + cap->pad =3D 0; + return ret; +} + +static int usb3hdcap_query_dv_timings(struct file *file, void *priv, + struct v4l2_dv_timings *timings) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + int ret; + + if (!(hdcap->input =3D=3D INPUT_COMPONENT || hdcap->input =3D=3D INPUT_HD= MI)) + return -ENODATA; + + if (vb2_is_streaming(&hdcap->vb2q)) + return -EBUSY; + + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, query_dv_timings, 0, + timings); + hdcap->no_signal =3D ret < 0; + return ret; +} + +static void usb3hdcap_activate_ctrls(struct usb3hdcap *hdcap, + enum usb3hdcap_input input) +{ + bool is_tw9900 =3D input =3D=3D INPUT_COMPOSITE || input =3D=3D INPUT_SVI= DEO; + + v4l2_ctrl_activate(hdcap->ctrl_brightness, is_tw9900); + v4l2_ctrl_activate(hdcap->ctrl_contrast, is_tw9900); + v4l2_ctrl_activate(hdcap->ctrl_saturation, is_tw9900); + v4l2_ctrl_activate(hdcap->ctrl_hue, is_tw9900); + v4l2_ctrl_activate(hdcap->ctrl_sharpness, is_tw9900); +} + +static int usb3hdcap_g_input(struct file *file, void *priv, unsigned int *= i) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + *i =3D hdcap->input; + return 0; +} + +/* Everything that selecting an input changes */ +struct usb3hdcap_input_state { + enum usb3hdcap_input input; + v4l2_std_id std; + struct v4l2_dv_timings timings; + bool no_signal; + bool mst_bt601; + int width; + int height; + int interlaced; + int bpl; +}; + +static void usb3hdcap_save_input(struct usb3hdcap *hdcap, + struct usb3hdcap_input_state *state) +{ + state->input =3D hdcap->input; + state->std =3D hdcap->std; + state->timings =3D hdcap->timings; + state->no_signal =3D hdcap->no_signal; + state->mst_bt601 =3D hdcap->mst_bt601; + state->width =3D hdcap->width; + state->height =3D hdcap->height; + state->interlaced =3D hdcap->interlaced; + state->bpl =3D hdcap->bpl; +} + +static void usb3hdcap_restore_input(struct usb3hdcap *hdcap, + const struct usb3hdcap_input_state *state) +{ + hdcap->input =3D state->input; + hdcap->std =3D state->std; + hdcap->timings =3D state->timings; + hdcap->no_signal =3D state->no_signal; + hdcap->mst_bt601 =3D state->mst_bt601; + hdcap->width =3D state->width; + hdcap->height =3D state->height; + hdcap->interlaced =3D state->interlaced; + hdcap->bpl =3D state->bpl; +} + +static int usb3hdcap_init_input(struct usb3hdcap *hdcap) +{ + int ret; + + ret =3D usb3hdcap_hw_init(hdcap); + hdcap->hw_inited =3D !ret; + if (ret < 0) + return ret; + + usb3hdcap_activate_ctrls(hdcap, hdcap->input); + + if (hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SVID= EO) + hdcap->video_dev.tvnorms =3D USB3HDCAP_V4L2_STDS; + else + hdcap->video_dev.tvnorms =3D 0; + + return 0; +} + +static int usb3hdcap_s_input(struct file *file, void *priv, unsigned int i) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + struct usb3hdcap_input_state old; + int ret; + + if (i > INPUT_HDMI) + return -EINVAL; + if (vb2_is_busy(&hdcap->vb2q)) + return i =3D=3D hdcap->input ? 0 : -EBUSY; + + usb3hdcap_save_input(hdcap, &old); + hdcap->input =3D i; + + ret =3D usb3hdcap_init_input(hdcap); + if (ret < 0) { + /* the hardware is in an unknown state, STREAMON will fail */ + usb3hdcap_restore_input(hdcap, &old); + return ret; + } + + return 0; +} + +static int usb3hdcap_enum_framesizes(struct file *file, void *priv, + struct v4l2_frmsizeenum *fsize) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (fsize->index > 0) + return -EINVAL; + if (fsize->pixel_format !=3D V4L2_PIX_FMT_YUYV) + return -EINVAL; + + fsize->type =3D V4L2_FRMSIZE_TYPE_DISCRETE; + fsize->discrete.width =3D hdcap->width; + fsize->discrete.height =3D hdcap->height; + return 0; +} + +static void fill_timeperframe(struct usb3hdcap *hdcap, struct v4l2_fract *= tf) +{ + /* + * using the frame rate here even for interlaced, spec isn't super + * clear on what to do, but adv7180.c does it this way + */ + + if (hdcap->input =3D=3D INPUT_COMPONENT || hdcap->input =3D=3D INPUT_HDMI= ) { + const struct v4l2_bt_timings *bt =3D &hdcap->timings.bt; + u32 htotal =3D V4L2_DV_BT_FRAME_WIDTH(bt); + u32 vtotal =3D V4L2_DV_BT_FRAME_HEIGHT(bt); + + tf->numerator =3D htotal * vtotal; + tf->denominator =3D (u32)bt->pixelclock; + v4l2_simplify_fraction(&tf->numerator, &tf->denominator, 8, 333); + } else if (hdcap->std & V4L2_STD_625_50) { + tf->numerator =3D 1; + tf->denominator =3D 25; + } else { + tf->numerator =3D 1001; + tf->denominator =3D 30000; + } +} + +static int usb3hdcap_g_parm(struct file *file, void *priv, + struct v4l2_streamparm *sp) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (sp->type !=3D V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + + sp->parm.capture.capability =3D V4L2_CAP_TIMEPERFRAME; + fill_timeperframe(hdcap, &sp->parm.capture.timeperframe); + sp->parm.capture.readbuffers =3D 4; + return 0; +} + +static int usb3hdcap_s_parm(struct file *file, void *priv, + struct v4l2_streamparm *sp) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (sp->type !=3D V4L2_BUF_TYPE_VIDEO_CAPTURE) + return -EINVAL; + + /* fixed framerate - just report what we have */ + sp->parm.capture.capability =3D V4L2_CAP_TIMEPERFRAME; + fill_timeperframe(hdcap, &sp->parm.capture.timeperframe); + sp->parm.capture.readbuffers =3D 4; + return 0; +} + +static int usb3hdcap_enum_frameintervals(struct file *file, void *priv, + struct v4l2_frmivalenum *fival) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + if (fival->index > 0) + return -EINVAL; + if (fival->pixel_format !=3D V4L2_PIX_FMT_YUYV) + return -EINVAL; + if (fival->width !=3D hdcap->width || fival->height !=3D hdcap->height) + return -EINVAL; + + fival->type =3D V4L2_FRMIVAL_TYPE_DISCRETE; + fill_timeperframe(hdcap, &fival->discrete); + return 0; +} + +static int usb3hdcap_log_status(struct file *file, void *priv) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + + dev_info(hdcap->dev, + "status: iso_cbs=3D%u iso_bytes=3D%lu markers=3D%u frames=3D%u ", + hdcap->iso_cb_count, hdcap->iso_bytes, + hdcap->markers_found, hdcap->frames_delivered); + dev_info(hdcap->dev, + "parse_len=3D%d frame_line=3D%d synced=3D%d was_blanking=3D%d ", + hdcap->parse_len, hdcap->frame_line, + hdcap->synced, hdcap->was_blanking); + dev_info(hdcap->dev, "cur_buf=3D%p\n", hdcap->cur_buf); + return 0; +} + +static int usb3hdcap_g_volatile_ctrl(struct v4l2_ctrl *ctrl) +{ + switch (ctrl->id) { + case V4L2_CID_DV_RX_POWER_PRESENT: + ctrl->val =3D 1; + return 0; + default: + return -EINVAL; + } +} + +static const struct v4l2_ctrl_ops usb3hdcap_ctrl_ops =3D { + .g_volatile_ctrl =3D usb3hdcap_g_volatile_ctrl, +}; + +static int usb3hdcap_subscribe_event(struct v4l2_fh *fh, + const struct v4l2_event_subscription *sub) +{ + switch (sub->type) { + case V4L2_EVENT_SOURCE_CHANGE: + return v4l2_src_change_event_subscribe(fh, sub); + default: + return v4l2_ctrl_subscribe_event(fh, sub); + } +} + +static const struct v4l2_ioctl_ops usb3hdcap_ioctl_ops =3D { + .vidioc_querycap =3D usb3hdcap_querycap, + .vidioc_enum_input =3D usb3hdcap_enum_input, + .vidioc_enum_fmt_vid_cap =3D usb3hdcap_enum_fmt_vid_cap, + .vidioc_g_fmt_vid_cap =3D usb3hdcap_fmt_vid_cap, + .vidioc_try_fmt_vid_cap =3D usb3hdcap_fmt_vid_cap, + .vidioc_s_fmt_vid_cap =3D usb3hdcap_fmt_vid_cap, + .vidioc_g_std =3D usb3hdcap_g_std, + .vidioc_s_std =3D usb3hdcap_s_std, + .vidioc_querystd =3D usb3hdcap_querystd, + .vidioc_g_dv_timings =3D usb3hdcap_g_dv_timings, + .vidioc_s_dv_timings =3D usb3hdcap_s_dv_timings, + .vidioc_enum_dv_timings =3D usb3hdcap_enum_dv_timings, + .vidioc_dv_timings_cap =3D usb3hdcap_dv_timings_cap, + .vidioc_query_dv_timings =3D usb3hdcap_query_dv_timings, + .vidioc_g_input =3D usb3hdcap_g_input, + .vidioc_s_input =3D usb3hdcap_s_input, + .vidioc_g_parm =3D usb3hdcap_g_parm, + .vidioc_s_parm =3D usb3hdcap_s_parm, + .vidioc_enum_framesizes =3D usb3hdcap_enum_framesizes, + .vidioc_enum_frameintervals =3D usb3hdcap_enum_frameintervals, + .vidioc_log_status =3D usb3hdcap_log_status, + + .vidioc_reqbufs =3D vb2_ioctl_reqbufs, + .vidioc_prepare_buf =3D vb2_ioctl_prepare_buf, + .vidioc_querybuf =3D vb2_ioctl_querybuf, + .vidioc_create_bufs =3D vb2_ioctl_create_bufs, + .vidioc_qbuf =3D vb2_ioctl_qbuf, + .vidioc_dqbuf =3D vb2_ioctl_dqbuf, + .vidioc_streamon =3D vb2_ioctl_streamon, + .vidioc_streamoff =3D vb2_ioctl_streamoff, + + .vidioc_subscribe_event =3D usb3hdcap_subscribe_event, + .vidioc_unsubscribe_event =3D v4l2_event_unsubscribe, +}; + +static int usb3hdcap_open(struct file *file) +{ + struct usb3hdcap *hdcap =3D video_drvdata(file); + int ret; + + if (mutex_lock_interruptible(&hdcap->lock)) + return -ERESTARTSYS; + + if (!hdcap->usb_dev) { + ret =3D -ENODEV; + goto unlock; + } + + ret =3D v4l2_fh_open(file); + if (ret < 0) + goto unlock; + + /* For applications that never call S_INPUT... */ + if (!hdcap->hw_inited && !vb2_is_busy(&hdcap->vb2q)) + usb3hdcap_init_input(hdcap); + +unlock: + mutex_unlock(&hdcap->lock); + return ret; +} + +static const struct v4l2_file_operations usb3hdcap_fops =3D { + .owner =3D THIS_MODULE, + .unlocked_ioctl =3D video_ioctl2, + .mmap =3D vb2_fop_mmap, + .open =3D usb3hdcap_open, + .release =3D vb2_fop_release, + .read =3D vb2_fop_read, + .poll =3D vb2_fop_poll, +}; + +static void usb3hdcap_notify(struct v4l2_subdev *sd, unsigned int notifica= tion, + void *arg) +{ + struct usb3hdcap *hdcap =3D container_of(sd->v4l2_dev, + struct usb3hdcap, v4l2_dev); + + switch (notification) { + case MST3367_HOTPLUG: + usb3hdcap_hdmi_hotplug(hdcap, *(bool *)arg); + break; + default: + break; + } +} + +static void usb3hdcap_release(struct v4l2_device *v4l2_dev) +{ + struct usb3hdcap *hdcap =3D container_of(v4l2_dev, + struct usb3hdcap, v4l2_dev); + + v4l2_ctrl_handler_free(&hdcap->ctrl); + v4l2_device_unregister(&hdcap->v4l2_dev); + i2c_del_adapter(&hdcap->i2c_adap); + kfree(hdcap); +} + +static int video_init(struct usb3hdcap *hdcap) +{ + int ret; + + mutex_init(&hdcap->lock); + mutex_init(&hdcap->mcu_lock); + spin_lock_init(&hdcap->buflock); + INIT_LIST_HEAD(&hdcap->bufs); + + hdcap->vb2q.type =3D V4L2_BUF_TYPE_VIDEO_CAPTURE; + hdcap->vb2q.io_modes =3D VB2_MMAP | VB2_USERPTR | VB2_READ; + hdcap->vb2q.drv_priv =3D hdcap; + hdcap->vb2q.buf_struct_size =3D sizeof(struct hdcap_buf); + hdcap->vb2q.ops =3D &usb3hdcap_vb2_ops; + hdcap->vb2q.mem_ops =3D &vb2_vmalloc_memops; + hdcap->vb2q.timestamp_flags =3D V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + hdcap->vb2q.min_queued_buffers =3D 2; + hdcap->vb2q.lock =3D &hdcap->lock; + ret =3D vb2_queue_init(&hdcap->vb2q); + if (ret < 0) { + dev_warn(hdcap->dev, "Could not initialize videobuf2 queue\n"); + return ret; + } + + v4l2_ctrl_handler_init(&hdcap->ctrl, 1); + hdcap->ctrl_rx_power =3D v4l2_ctrl_new_std(&hdcap->ctrl, + &usb3hdcap_ctrl_ops, + V4L2_CID_DV_RX_POWER_PRESENT, + 0, 1, 0, 1); + if (hdcap->ctrl_rx_power) + hdcap->ctrl_rx_power->flags |=3D V4L2_CTRL_FLAG_VOLATILE | + V4L2_CTRL_FLAG_READ_ONLY; + ret =3D hdcap->ctrl.error; + if (ret < 0) { + dev_warn(hdcap->dev, "Could not initialize controls\n"); + goto v4l2_fail; + } + + hdcap->v4l2_dev.ctrl_handler =3D &hdcap->ctrl; + hdcap->v4l2_dev.release =3D usb3hdcap_release; + hdcap->v4l2_dev.notify =3D usb3hdcap_notify; + ret =3D v4l2_device_register(hdcap->dev, &hdcap->v4l2_dev); + if (ret < 0) { + dev_warn(hdcap->dev, "Could not register v4l2 device\n"); + goto v4l2_fail; + } + + usb3hdcap_device_init(hdcap); + + ret =3D usb3hdcap_i2c_register(hdcap); + if (ret < 0) { + dev_warn(hdcap->dev, "Could not register I2C adapter\n"); + goto vdev_fail; + } + + ret =3D usb3hdcap_composite_register(hdcap); + if (ret < 0) + goto subdev_fail; + + ret =3D usb3hdcap_hdmi_register(hdcap); + if (ret < 0) + goto subdev_fail; + + strscpy(hdcap->video_dev.name, "usb3hdcap", sizeof(hdcap->video_dev.name)= ); + hdcap->video_dev.v4l2_dev =3D &hdcap->v4l2_dev; + hdcap->video_dev.release =3D video_device_release_empty; + hdcap->video_dev.fops =3D &usb3hdcap_fops; + hdcap->video_dev.ioctl_ops =3D &usb3hdcap_ioctl_ops; + hdcap->video_dev.tvnorms =3D USB3HDCAP_V4L2_STDS; + hdcap->video_dev.queue =3D &hdcap->vb2q; + hdcap->video_dev.lock =3D &hdcap->lock; + hdcap->video_dev.device_caps =3D V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWR= ITE | + V4L2_CAP_STREAMING; + video_set_drvdata(&hdcap->video_dev, hdcap); + ret =3D video_register_device(&hdcap->video_dev, VFL_TYPE_VIDEO, -1); + if (ret < 0) { + dev_warn(hdcap->dev, "Could not register video device\n"); + goto subdev_fail; + } + + dev_info(hdcap->dev, "%s: registered as %s\n", + __func__, video_device_node_name(&hdcap->video_dev)); + + return 0; + +subdev_fail: + v4l2_device_unregister(&hdcap->v4l2_dev); + i2c_del_adapter(&hdcap->i2c_adap); + goto v4l2_fail; + +vdev_fail: + v4l2_device_unregister(&hdcap->v4l2_dev); + +v4l2_fail: + v4l2_ctrl_handler_free(&hdcap->ctrl); + return ret; +} + +static int usb3hdcap_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct device *dev; + struct usb3hdcap *hdcap; + int ret, is_xcapture; + + /* not using the interrupt interface, just data */ + if (intf->cur_altsetting->desc.bInterfaceNumber !=3D 0) + return -ENODEV; + + /* none, Mult=3D0 for SD, Mult=3D1 for HD */ + if (intf->num_altsetting !=3D 3) + return -ENODEV; + + dev =3D &intf->dev; + + hdcap =3D kzalloc_obj(*hdcap, GFP_KERNEL); + if (!hdcap) + return -ENOMEM; + + hdcap->dev =3D dev; + hdcap->usb_dev =3D usb_get_dev(interface_to_usbdev(intf)); + hdcap->input =3D INPUT_COMPOSITE; + hdcap->std =3D V4L2_STD_NTSC; + hdcap->width =3D SD_WIDTH; + hdcap->height =3D NTSC_HEIGHT / 2; + hdcap->bpl =3D DEFAULT_BPL; + + usb_set_intfdata(intf, hdcap); + + ret =3D video_init(hdcap); + if (ret < 0) + goto video_fail; + + ret =3D usb3hdcap_audio_init(hdcap); + if (ret < 0) + dev_warn(dev, "audio init failed: %d (continuing without audio)\n", ret); + + is_xcapture =3D le16_to_cpu(hdcap->usb_dev->descriptor.idProduct) =3D=3D = XCAPTURE1_PID; + dev_info(dev, "%s USB 3.0 HD Video Capture Device", + is_xcapture ? "Micomsoft XCAPTURE-1" : "StarTech USB3HDCAP"); + + return 0; + +video_fail: + usb_set_intfdata(intf, NULL); + usb_put_dev(hdcap->usb_dev); + kfree(hdcap); + return ret; +} + +static void usb3hdcap_disconnect(struct usb_interface *intf) +{ + struct usb3hdcap *hdcap =3D usb_get_intfdata(intf); + + usb_set_intfdata(intf, NULL); + + if (!hdcap) + return; + + vb2_video_unregister_device(&hdcap->video_dev); + usb3hdcap_audio_free(hdcap); + v4l2_device_disconnect(&hdcap->v4l2_dev); + + mutex_lock(&hdcap->lock); + i2c_lock_bus(&hdcap->i2c_adap, I2C_LOCK_ROOT_ADAPTER); + usb_put_dev(hdcap->usb_dev); + hdcap->usb_dev =3D NULL; + i2c_unlock_bus(&hdcap->i2c_adap, I2C_LOCK_ROOT_ADAPTER); + mutex_unlock(&hdcap->lock); + + /* hdcap is freed by usb3hdcap_release when last user closes fd */ + v4l2_device_put(&hdcap->v4l2_dev); +} + +static const struct usb_device_id usb3hdcap_id_table[] =3D { + { USB_DEVICE(USB3HDCAP_VID, USB3HDCAP_PID) }, + { USB_DEVICE(USB3HDCAP_VID, XCAPTURE1_PID) }, + { }, +}; +MODULE_DEVICE_TABLE(usb, usb3hdcap_id_table); + +MODULE_AUTHOR("Matthew Laux "); +MODULE_DESCRIPTION("StarTech USB3HDCAP Driver"); +MODULE_LICENSE("GPL"); + +static struct usb_driver usb3hdcap_usb_driver =3D { + .name =3D "usb3hdcap", + .id_table =3D usb3hdcap_id_table, + .probe =3D usb3hdcap_probe, + .disconnect =3D usb3hdcap_disconnect, +}; +module_usb_driver(usb3hdcap_usb_driver); diff --git a/drivers/media/usb/usb3hdcap/usb3hdcap-hdmi.c b/drivers/media/u= sb/usb3hdcap/usb3hdcap-hdmi.c new file mode 100644 index 000000000000..8f7ea642cb12 --- /dev/null +++ b/drivers/media/usb/usb3hdcap/usb3hdcap-hdmi.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * USB3HDCAP MST3367 support: HDMI and component + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "usb3hdcap.h" + +static struct i2c_board_info mst3367_info =3D { + I2C_BOARD_INFO("mst3367", ADDR_MST3367 >> 1), +}; + +int usb3hdcap_hdmi_register(struct usb3hdcap *hdcap) +{ + hdcap->sd_mst3367 =3D v4l2_i2c_new_subdev_board(&hdcap->v4l2_dev, + &hdcap->i2c_adap, + &mst3367_info, NULL); + if (!hdcap->sd_mst3367) { + dev_warn(hdcap->dev, "Could not register MST3367\n"); + return -ENODEV; + } + + return 0; +} + +void usb3hdcap_hdmi_hotplug(struct usb3hdcap *hdcap, bool hotplug) +{ + if (!hdcap->has_mcu) + return; + + if (hotplug) + u3hc_i2c_rmw(hdcap, ADDR_CPLD, 0x01, 0xfd, 0x01); + else + u3hc_i2c_rmw(hdcap, ADDR_CPLD, 0x01, 0xff, 0x03); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x11, 0xfc); +} + +static void cpld_init(struct usb3hdcap *hdcap, u8 mux_val) +{ + int k, status; + + /* + * cVar3 =3D i2c_read(param_1,0x98,0x3b,0); + * if (cVar3 !=3D '\0') { + * uVar5 =3D *(uint *)(param_1 + 0x6840); + * if ((uVar5 =3D=3D 2) || (uVar5 =3D=3D 3)) { + * local_res8[0] =3D -0x80; + * i2c_write(param_1,0x98,0x3b,local_res8,1); + * uVar5 =3D *(uint *)(param_1 + 0x6840); + * } + * if (uVar5 < 2) { + * local_res8[0] =3D '\x02'; + * i2c_write(param_1,0x98,0x3b,local_res8,1); + * } + * } + */ + + if (u3hc_i2c_read(hdcap, ADDR_CPLD, 0x3b) !=3D 0) + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x3b, mux_val); + + /* + * local_res8[0] =3D '\x05'; + * i2c_write(param_1,0x98,0x20,local_res8,1); + * cVar3 =3D FUN_140226fc4(param_1); + * local_res8[0] =3D '\x01'; + * i2c_write(param_1,0x98,0,local_res8,1); + * local_res8[0] =3D (-(cVar3 !=3D '\0') & 0xfeU) - 2; + * i2c_write(param_1,0x98,0x10,local_res8,1); + */ + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x20, 0x05); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x00, 0x01); + + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x10, hdcap->has_mcu ? 0xfc : 0xfe); + + for (k =3D 0; k < 50; k++) { + status =3D u3hc_i2c_read(hdcap, ADDR_CPLD, 0x01); + if (status >=3D 0x03) + break; + msleep(20); + } + + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x01, hdcap->has_mcu ? 0x03 : 0x02); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x11, 0xfc); +} + +/* Make timings the current timings and derive the capture format from the= m */ +int usb3hdcap_hd_set_timings(struct usb3hdcap *hdcap, + struct v4l2_dv_timings *timings) +{ + struct v4l2_subdev_format fmt =3D { + .which =3D V4L2_SUBDEV_FORMAT_ACTIVE, + .pad =3D MST3367_PAD_SOURCE, + }; + int ret; + + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, s_dv_timings, 0, timings= ); + if (ret < 0) + return ret; + + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, get_fmt, NULL, &fmt); + if (ret < 0) + return ret; + + hdcap->mst_bt601 =3D fmt.format.colorspace =3D=3D V4L2_COLORSPACE_SMPTE17= 0M; + + hdcap->timings =3D *timings; + hdcap->width =3D timings->bt.width; + hdcap->interlaced =3D timings->bt.interlaced; + hdcap->height =3D timings->bt.interlaced + ? timings->bt.height / 2 : timings->bt.height; + hdcap->bpl =3D hdcap->width * 2; + + return 0; +} + +/* Start out with what is connected, default to 720p60 if no signal */ +static void usb3hdcap_hd_detect(struct usb3hdcap *hdcap, + struct v4l2_dv_timings *timings) +{ + static const struct v4l2_dv_timings def =3D V4L2_DV_BT_CEA_1280X720P60; + int ret; + + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, pad, query_dv_timings, 0, + timings); + hdcap->no_signal =3D ret < 0; + if (hdcap->no_signal) { + dev_dbg(hdcap->dev, "no timings detected (%d)\n", ret); + *timings =3D def; + } + + dev_dbg(hdcap->dev, "%dx%d%s\n", timings->bt.width, timings->bt.height, + timings->bt.interlaced ? "i" : "p"); +} + +/* ------------------------------------------------------------------ */ +/* Main HDMI init entry point */ +/* ------------------------------------------------------------------ */ + +int usb3hdcap_hdmi_init(struct usb3hdcap *hdcap) +{ + struct v4l2_dv_timings timings; + int ret; + + /* CPLD init with HDMI input mux */ + cpld_init(hdcap, 0x02); + + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, video, s_routing, + MST3367_INPUT_HDMI, 0, 0); + if (ret < 0) + return ret; + + usb3hdcap_hd_detect(hdcap, &timings); + + return usb3hdcap_hd_set_timings(hdcap, &timings); +} + +/* ------------------------------------------------------------------ */ +/* Main component init entry point */ +/* ------------------------------------------------------------------ */ + +int usb3hdcap_component_init(struct usb3hdcap *hdcap) +{ + struct v4l2_dv_timings timings; + int ret; + + /* CPLD init with component input mux */ + cpld_init(hdcap, 0x80); + + ret =3D v4l2_subdev_call(hdcap->sd_mst3367, video, s_routing, + MST3367_INPUT_COMPONENT, 0, 0); + if (ret < 0) + return ret; + + /* Component CPLD routing, bit 4 =3D component input? */ + u3hc_i2c_read(hdcap, ADDR_CPLD, 0x20); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x20, 0x15); + + /* + * Wait for ADC to stabilize, windows driver does this but might be able + * to decrease wait + */ + msleep(1000); + + /* Final CPLD verify */ + u3hc_i2c_read(hdcap, ADDR_CPLD, 0x20); + u3hc_i2c_write(hdcap, ADDR_CPLD, 0x20, 0x15); + + usb3hdcap_hd_detect(hdcap, &timings); + + u3hc_i2c_rmw(hdcap, ADDR_CPLD, 0x00, 0xff, 0x02); + + return usb3hdcap_hd_set_timings(hdcap, &timings); +} diff --git a/drivers/media/usb/usb3hdcap/usb3hdcap-video.c b/drivers/media/= usb/usb3hdcap/usb3hdcap-video.c new file mode 100644 index 000000000000..3a861db36d6c --- /dev/null +++ b/drivers/media/usb/usb3hdcap/usb3hdcap-video.c @@ -0,0 +1,499 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * USB3HDCAP video streaming - stream parser, ISO URB handling, vb2 ops + */ + +#include +#include +#include +#include +#include + +#include "usb3hdcap.h" + +/* ------------------------------------------------------------------ */ +/* Stream parser */ +/* ------------------------------------------------------------------ */ + +static void vyuy_to_yuyv(u8 *dst, const u8 *src, int len) +{ + int k; + + /* VYUY: V Y0 U Y1 -> YUYV: Y0 U Y1 V */ + for (k =3D 0; k < len; k +=3D 4) { + dst[k + 0] =3D src[k + 1]; /* Y0 */ + dst[k + 1] =3D src[k + 2]; /* U */ + dst[k + 2] =3D src[k + 3]; /* Y1 */ + dst[k + 3] =3D src[k + 0]; /* V */ + } +} + +static struct hdcap_buf *get_next_buf(struct usb3hdcap *hdcap) +{ + struct hdcap_buf *buf =3D NULL; + unsigned long flags; + + spin_lock_irqsave(&hdcap->buflock, flags); + if (!list_empty(&hdcap->bufs)) { + buf =3D list_first_entry(&hdcap->bufs, struct hdcap_buf, list); + list_del(&buf->list); + } + spin_unlock_irqrestore(&hdcap->buflock, flags); + + return buf; +} + +static void deliver_frame(struct usb3hdcap *hdcap) +{ + struct hdcap_buf *buf =3D hdcap->cur_buf; + u8 *vaddr; + int k; + + if (!buf) + return; + + hdcap->frames_delivered++; + + /* Pad remaining lines with black */ + vaddr =3D vb2_plane_vaddr(&buf->vb.vb2_buf, 0); + while (hdcap->frame_line < hdcap->height) { + u8 *row =3D vaddr + hdcap->frame_line * hdcap->bpl; + + for (k =3D 0; k < hdcap->bpl; k +=3D 4) { + row[k + 0] =3D 0x10; + row[k + 1] =3D 0x80; + row[k + 2] =3D 0x10; + row[k + 3] =3D 0x80; + } + hdcap->frame_line++; + } + + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, + hdcap->bpl * hdcap->height); + buf->vb.vb2_buf.timestamp =3D ktime_get_ns(); + if (hdcap->interlaced) { + bool bottom =3D hdcap->field_f; + + if (!bottom && hdcap->top_done) + hdcap->sequence++; + buf->vb.field =3D bottom ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP; + buf->vb.sequence =3D hdcap->sequence; + hdcap->top_done =3D !bottom; + if (bottom) + hdcap->sequence++; + } else { + buf->vb.field =3D V4L2_FIELD_NONE; + buf->vb.sequence =3D hdcap->sequence++; + } + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); + + hdcap->cur_buf =3D NULL; +} + +/* + * Process one video line (shared between composite and HDMI). + * 'video' points to the XY byte followed by pixel data. + */ +static void process_video_line(struct usb3hdcap *hdcap, const u8 *video) +{ + u8 sav_xy =3D video[0]; + int blanking =3D sav_xy & BT656_V_BIT; + + /* Blanking -> active transition */ + if (!blanking && hdcap->was_blanking) { + if (!hdcap->synced) + hdcap->synced =3D 1; + if (hdcap->cur_buf && hdcap->frame_line >=3D 100) + deliver_frame(hdcap); + hdcap->frame_line =3D 0; + if (!hdcap->cur_buf) + hdcap->cur_buf =3D get_next_buf(hdcap); + /* Dropping a field ends the frame */ + if (!hdcap->cur_buf && hdcap->top_done) { + hdcap->top_done =3D 0; + hdcap->sequence++; + } + } + hdcap->was_blanking =3D blanking; + + if (!hdcap->synced || !hdcap->cur_buf || blanking) + return; + + /* Deliver when field is full */ + if (hdcap->frame_line >=3D hdcap->height) { + deliver_frame(hdcap); + hdcap->frame_line =3D 0; + hdcap->cur_buf =3D get_next_buf(hdcap); + if (!hdcap->cur_buf) + return; + } + + if (hdcap->frame_line =3D=3D 0) { + hdcap->field_f =3D !!(sav_xy & BT656_F_BIT); + /* skip bottom field where the top was dropped */ + if (hdcap->interlaced && hdcap->field_f && !hdcap->top_done) + return; + } + + vyuy_to_yuyv(vb2_plane_vaddr(&hdcap->cur_buf->vb.vb2_buf, 0) + + hdcap->frame_line * hdcap->bpl, + video, hdcap->bpl); + hdcap->frame_line++; +} + +static void process_data(struct usb3hdcap *hdcap) +{ + u8 *buf =3D hdcap->parse_buf; + int line_size =3D SAV_LEN + hdcap->bpl; + int pos =3D 0; + + while (pos + 3 <=3D hdcap->parse_len) { + if (buf[pos] !=3D 0xff || buf[pos + 1] !=3D 0x00) { + pos++; + continue; + } + + if (buf[pos + 2] =3D=3D 0x00) { + /* SAV: video line */ + if (pos + line_size > hdcap->parse_len) + break; + /* -1 because XY byte occupies the first pixel's V position */ + process_video_line(hdcap, buf + pos + SAV_LEN - 1); + pos +=3D line_size; + } else if (buf[pos + 2] =3D=3D 0xff) { + u8 type; + int alen; + + if (pos + MARKER_LEN > hdcap->parse_len) + break; + type =3D buf[pos + 3]; + if (type < 0x02 || type > 0x04) { + pos++; + continue; + } + alen =3D type * 4; + if (pos + MARKER_LEN + alen > hdcap->parse_len) + break; + hdcap->markers_found++; + usb3hdcap_audio_data(hdcap, buf + pos + MARKER_LEN, alen); + pos +=3D MARKER_LEN + alen; + } else { + pos++; + } + } + + /* Shift unconsumed data to front */ + if (pos > 0) { + hdcap->parse_len -=3D pos; + if (hdcap->parse_len > 0) + memmove(buf, buf + pos, hdcap->parse_len); + } +} + +/* ------------------------------------------------------------------ */ +/* Isochronous URB handling */ +/* ------------------------------------------------------------------ */ + +static void usb3hdcap_iso_cb(struct urb *urb) +{ + struct usb3hdcap *hdcap =3D urb->context; + int k, ret; + + switch (urb->status) { + case 0: + break; + case -ENODEV: + case -ENOENT: + case -ECONNRESET: + case -ESHUTDOWN: + return; + default: + dev_warn(hdcap->dev, "ISO URB status %d\n", urb->status); + goto resubmit; + } + + hdcap->iso_cb_count++; + + for (k =3D 0; k < urb->number_of_packets; k++) { + int status =3D urb->iso_frame_desc[k].status; + int len =3D urb->iso_frame_desc[k].actual_length; + u8 *data =3D urb->transfer_buffer + + urb->iso_frame_desc[k].offset; + + if (status !=3D 0 || len =3D=3D 0) + continue; + + if (hdcap->parse_len + len > hdcap->parse_buf_size) + hdcap->parse_len =3D 0; + + memcpy(hdcap->parse_buf + hdcap->parse_len, data, len); + hdcap->parse_len +=3D len; + hdcap->iso_bytes +=3D len; + + process_data(hdcap); + } + +resubmit: + ret =3D usb_submit_urb(urb, GFP_ATOMIC); + /* endpoint going away when stopping is expected */ + if (ret < 0 && ret !=3D -ENOENT) + dev_warn(hdcap->dev, "ISO URB resubmit: %d\n", ret); +} + +static struct urb *usb3hdcap_alloc_urb(struct usb3hdcap *hdcap, int npkts) +{ + struct urb *urb; + int size =3D hdcap->iso_size; + int k; + + urb =3D usb_alloc_urb(npkts, GFP_KERNEL); + if (!urb) + return NULL; + + urb->dev =3D hdcap->usb_dev; + urb->context =3D hdcap; + urb->pipe =3D usb_rcvisocpipe(hdcap->usb_dev, EP_VIDEO); + urb->interval =3D 1; + urb->transfer_flags =3D URB_ISO_ASAP; + /* __NOWARN because it retries with fewer packets */ + urb->transfer_buffer =3D kcalloc(npkts, size, GFP_KERNEL | __GFP_NOWARN); + if (!urb->transfer_buffer) { + usb_free_urb(urb); + return NULL; + } + urb->complete =3D usb3hdcap_iso_cb; + urb->number_of_packets =3D npkts; + urb->transfer_buffer_length =3D size * npkts; + for (k =3D 0; k < npkts; k++) { + urb->iso_frame_desc[k].offset =3D size * k; + urb->iso_frame_desc[k].length =3D size; + } + + return urb; +} + +static void usb3hdcap_free_urbs(struct usb3hdcap *hdcap) +{ + int k; + + for (k =3D 0; k < NUM_XFERS; k++) { + struct urb *urb =3D hdcap->isoc_urbs[k]; + + if (!urb) + continue; + usb_kill_urb(urb); + kfree(urb->transfer_buffer); + usb_free_urb(urb); + hdcap->isoc_urbs[k] =3D NULL; + } +} + +static int usb3hdcap_alloc_urbs(struct usb3hdcap *hdcap) +{ + int npkts, k; + + /* Retry allocations until one succeeds */ + for (npkts =3D NUM_ISO_PKTS; npkts >=3D MIN_ISO_PKTS; npkts /=3D 2) { + for (k =3D 0; k < NUM_XFERS; k++) { + hdcap->isoc_urbs[k] =3D usb3hdcap_alloc_urb(hdcap, npkts); + if (!hdcap->isoc_urbs[k]) + break; + } + if (k =3D=3D NUM_XFERS) + return 0; + usb3hdcap_free_urbs(hdcap); + } + + return -ENOMEM; +} + +static void usb3hdcap_return_bufs(struct usb3hdcap *hdcap, + enum vb2_buffer_state buf_state) +{ + unsigned long flags; + + /* Return any in-progress buffer */ + if (hdcap->cur_buf) { + vb2_buffer_done(&hdcap->cur_buf->vb.vb2_buf, buf_state); + hdcap->cur_buf =3D NULL; + } + + spin_lock_irqsave(&hdcap->buflock, flags); + while (!list_empty(&hdcap->bufs)) { + struct hdcap_buf *buf =3D list_first_entry(&hdcap->bufs, + struct hdcap_buf, list); + list_del(&buf->list); + vb2_buffer_done(&buf->vb.vb2_buf, buf_state); + } + spin_unlock_irqrestore(&hdcap->buflock, flags); +} + +static void usb3hdcap_stop(struct usb3hdcap *hdcap, + enum vb2_buffer_state buf_state) +{ + usb3hdcap_free_urbs(hdcap); + usb_set_interface(hdcap->usb_dev, 0, 0); + + if (hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SVID= EO) + v4l2_subdev_call(hdcap->sd_tw9900, video, s_stream, 0); + + usb3hdcap_return_bufs(hdcap, buf_state); + + kvfree(hdcap->parse_buf); + hdcap->parse_buf =3D NULL; +} + +static int usb3hdcap_start(struct usb3hdcap *hdcap) +{ + struct usb_host_endpoint *ep; + int maxp, burst, mult; + int k, ret, alt; + + if (!hdcap->hw_inited) { + /* the last attempt to set up the input failed */ + ret =3D -EIO; + goto fail; + } + + hdcap->parse_len =3D 0; + hdcap->frame_line =3D 0; + hdcap->synced =3D 0; + hdcap->was_blanking =3D 0; + hdcap->field_f =3D 0; + hdcap->top_done =3D 0; + hdcap->cur_buf =3D NULL; + + if (hdcap->input =3D=3D INPUT_COMPOSITE || hdcap->input =3D=3D INPUT_SVID= EO) { + ret =3D v4l2_subdev_call(hdcap->sd_tw9900, video, s_stream, 1); + if (ret < 0) + goto fail; + } + + /* + * Select alt setting based on bandwidth: + * alt 1: maxp=3D1024 burst=3D16 mult=3D1 -> 16KB/pkt (SD) + * alt 2: maxp=3D1024 burst=3D16 mult=3D2 -> 32KB/pkt (HD) + */ + alt =3D (hdcap->width > 720 || hdcap->height > 576) ? 2 : 1; + dev_info(hdcap->dev, "%s: setting alt %d\n", __func__, alt); + ret =3D usb_set_interface(hdcap->usb_dev, 0, alt); + + if (ret < 0) { + dev_err(hdcap->dev, "%s: usb_set_interface failed: %d\n", + __func__, ret); + goto fail; + } + + /* Compute ISO packet size from SS endpoint companion descriptor */ + ep =3D usb_pipe_endpoint(hdcap->usb_dev, + usb_rcvisocpipe(hdcap->usb_dev, EP_VIDEO)); + if (!ep || !usb_endpoint_xfer_isoc(&ep->desc)) { + dev_err(hdcap->dev, "ISO endpoint 0x%02x not found\n", + EP_VIDEO); + ret =3D -ENODEV; + goto fail; + } + + maxp =3D usb_endpoint_maxp(&ep->desc); + burst =3D ep->ss_ep_comp.bMaxBurst; + mult =3D USB_SS_MULT(ep->ss_ep_comp.bmAttributes); + hdcap->iso_size =3D maxp * (burst + 1) * mult; + + hdcap->parse_buf_size =3D hdcap->iso_size + SAV_LEN + hdcap->bpl; + hdcap->parse_buf =3D kvmalloc(hdcap->parse_buf_size, GFP_KERNEL); + if (!hdcap->parse_buf) { + ret =3D -ENOMEM; + goto fail; + } + + ret =3D usb3hdcap_alloc_urbs(hdcap); + if (ret < 0) + goto fail; + + for (k =3D 0; k < NUM_XFERS; k++) { + ret =3D usb_submit_urb(hdcap->isoc_urbs[k], GFP_KERNEL); + if (ret < 0) + goto fail; + } + + return 0; + +fail: + usb3hdcap_stop(hdcap, VB2_BUF_STATE_QUEUED); + return ret; +} + +/* ------------------------------------------------------------------ */ +/* vb2 ops */ +/* ------------------------------------------------------------------ */ + +static int usb3hdcap_queue_setup(struct vb2_queue *vq, unsigned int *nbuff= ers, + unsigned int *nplanes, unsigned int sizes[], + struct device *alloc_devs[]) +{ + struct usb3hdcap *hdcap =3D vb2_get_drv_priv(vq); + unsigned int img_size =3D hdcap->bpl * hdcap->height; + + if (*nplanes) { + dev_info(hdcap->dev, "queue_setup: recheck sizes[0]=3D%u vs %u\n", + sizes[0], img_size); + return sizes[0] < img_size ? -EINVAL : 0; + } + *nplanes =3D 1; + sizes[0] =3D img_size; + + return 0; +} + +static int usb3hdcap_buf_prepare(struct vb2_buffer *vb) +{ + struct usb3hdcap *hdcap =3D vb2_get_drv_priv(vb->vb2_queue); + + if (vb2_plane_size(vb, 0) < hdcap->bpl * hdcap->height) + return -EINVAL; + + return 0; +} + +static void usb3hdcap_buf_queue(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf =3D to_vb2_v4l2_buffer(vb); + struct usb3hdcap *hdcap =3D vb2_get_drv_priv(vb->vb2_queue); + struct hdcap_buf *buf =3D container_of(vbuf, struct hdcap_buf, vb); + unsigned long flags; + + spin_lock_irqsave(&hdcap->buflock, flags); + list_add_tail(&buf->list, &hdcap->bufs); + spin_unlock_irqrestore(&hdcap->buflock, flags); +} + +static int usb3hdcap_start_streaming(struct vb2_queue *vq, unsigned int co= unt) +{ + struct usb3hdcap *hdcap =3D vb2_get_drv_priv(vq); + int ret; + + /* read() on fd that outlived the disconnect */ + if (!hdcap->usb_dev) { + usb3hdcap_return_bufs(hdcap, VB2_BUF_STATE_QUEUED); + return -ENODEV; + } + + hdcap->sequence =3D 0; + ret =3D usb3hdcap_start(hdcap); + return ret; +} + +static void usb3hdcap_stop_streaming(struct vb2_queue *vq) +{ + struct usb3hdcap *hdcap =3D vb2_get_drv_priv(vq); + + usb3hdcap_stop(hdcap, VB2_BUF_STATE_ERROR); +} + +const struct vb2_ops usb3hdcap_vb2_ops =3D { + .queue_setup =3D usb3hdcap_queue_setup, + .buf_prepare =3D usb3hdcap_buf_prepare, + .buf_queue =3D usb3hdcap_buf_queue, + .start_streaming =3D usb3hdcap_start_streaming, + .stop_streaming =3D usb3hdcap_stop_streaming, +}; diff --git a/drivers/media/usb/usb3hdcap/usb3hdcap.h b/drivers/media/usb/us= b3hdcap/usb3hdcap.h new file mode 100644 index 000000000000..c680ddf9c42d --- /dev/null +++ b/drivers/media/usb/usb3hdcap/usb3hdcap.h @@ -0,0 +1,241 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef USB3HDCAP_H +#define USB3HDCAP_H + +#include +#include +#include +#include +#include +#include + +struct dentry; +struct snd_card; +struct snd_pcm_substream; + +#define USB3HDCAP_V4L2_STDS (V4L2_STD_NTSC | V4L2_STD_PAL) + +/* NTSC SD frame format */ +#define SD_WIDTH 720 +#define NTSC_HEIGHT 480 +/* YUYV =3D 4 bytes/2 pixels */ +#define DEFAULT_BPL (SD_WIDTH * 2) +/* PAL SD frame format */ +#define PAL_HEIGHT 576 + +enum usb3hdcap_input { + INPUT_COMPOSITE =3D 0, + INPUT_SVIDEO =3D 1, + INPUT_COMPONENT =3D 2, + INPUT_HDMI =3D 3, +}; + +/* I2C 8-bit addresses */ +#define ADDR_MST3367 0x9c +#define ADDR_TW9900 0x88 +#define ADDR_CS53L21 0x94 +#define ADDR_CPLD 0x98 +#define ADDR_MCU 0x64 /* Nuvoton NUC100 on XCAPTURE-1 */ + +/* TW9900 registers */ +#define TW9900_CSTATUS 0x01 +#define TW9900_INFORM 0x02 +#define TW9900_OPFORM 0x03 +#define TW9900_OPCNTL_I 0x05 +#define TW9900_ACNTL_I 0x06 +#define TW9900_CROP_HI 0x07 +#define TW9900_VDELAY_LO 0x08 +#define TW9900_VACTIVE_LO 0x09 +#define TW9900_HDELAY_LO 0x0a +#define TW9900_HACTIVE_LO 0x0b +#define TW9900_CNTRL2 0x0d +#define TW9900_SDT_NOISE 0x0e +#define TW9900_LUMA_CTL 0x0f +#define TW9900_BRIGHT 0x10 +#define TW9900_CONTRAST 0x11 +#define TW9900_SHARPNESS 0x12 +#define TW9900_SAT_U 0x13 +#define TW9900_SAT_V 0x14 +#define TW9900_HUE 0x15 +#define TW9900_VBICNTL 0x19 +#define TW9900_ACNTL_II 0x1a +#define TW9900_OPCNTL_II 0x1b +#define TW9900_SDT 0x1c +#define TW9900_SDTR 0x1d +#define TW9900_VCNTL1 0x28 +#define TW9900_VCNTL2 0x29 +#define TW9900_MISC1 0x2d +#define TW9900_MISC2 0x2f +#define TW9900_CSTATUS_II 0x31 +#define TW9900_ANAPLLCTL 0x4c +#define TW9900_VVBI 0x55 +#define TW9900_HSBEGIN 0x6b +#define TW9900_HSEND 0x6c +#define TW9900_OVSDLY 0x6d +#define TW9900_OVSEND 0x6e + +/* CS53L21 registers */ +#define CS53L21_MIC_PWR_CTL 0x03 +#define CS53L21_IFACE_CTL 0x04 +#define CS53L21_ADC_IN_SEL 0x07 +#define CS53L21_SPE_CTL 0x09 +#define CS53L21_ALC_PGAA 0x0a +#define CS53L21_ALC_PGAB 0x0b +#define CS53L21_ADCA_ATT 0x0c +#define CS53L21_ADCB_ATT 0x0d +#define CS53L21_ADCA_MIX_VOL 0x0e +#define CS53L21_ADCB_MIX_VOL 0x0f +#define CS53L21_CH_MIXER 0x18 +#define CS53L21_ALC_EN_ATK 0x1c + +#define USB3HDCAP_VID 0x1164 +#define USB3HDCAP_PID 0xf533 +#define XCAPTURE1_PID 0xf531 + +/* Vendor request codes */ +#define REQ_I2C 0xc0 +#define REQ_GPIO 0xc1 +#define REQ_INIT 0xc2 +#define REQ_WATCH 0xc6 +#define REQ_UNK_C7 0xc7 +#define REQ_UNK_EC 0xec + +/* Isochronous transfer parameters */ +#define EP_VIDEO 0x83 +#define NUM_XFERS 8 +#define NUM_ISO_PKTS 64 +#define MIN_ISO_PKTS 8 + +/* + * Stream framing: + * SAV (ff 00 00 XY): bpl-1 pixel bytes, -1 because the XY is actually + * in the first pixel's V position... weird but it's correct + * Marker (ff 00 ff NN): audio - NN =3D number of stereo s16le + * samples, audio data =3D NN * 4 bytes + */ +#define SAV_LEN 4 +#define MARKER_LEN 4 + +/* BT.656 SAV XY bits */ +#define BT656_F_BIT 0x40 +#define BT656_V_BIT 0x20 + +struct hdcap_buf { + struct vb2_v4l2_buffer vb; + struct list_head list; +}; + +struct usb3hdcap { + struct device *dev; + struct usb_device *usb_dev; + struct v4l2_device v4l2_dev; + struct video_device video_dev; + struct vb2_queue vb2q; + + struct i2c_adapter i2c_adap; + struct v4l2_subdev *sd_tw9900; + struct v4l2_subdev *sd_mst3367; + + struct v4l2_ctrl_handler ctrl; + struct v4l2_ctrl *ctrl_brightness; + struct v4l2_ctrl *ctrl_contrast; + struct v4l2_ctrl *ctrl_saturation; + struct v4l2_ctrl *ctrl_hue; + struct v4l2_ctrl *ctrl_sharpness; + struct v4l2_ctrl *ctrl_rx_power; + + /* + * Serializes V4L2 ioctls, videobuf2 queue operations and device + * state. Also taken on disconnect to clear usb_dev. + */ + struct mutex lock; + /* serializes write then read for XCAPTURE-1 */ + struct mutex mcu_lock; + + /* buffer list */ + spinlock_t buflock; + struct list_head bufs; + + /* ISO URBs */ + int iso_size; + struct urb *isoc_urbs[NUM_XFERS]; + + /* frame tracking */ + unsigned int sequence; + + enum usb3hdcap_input input; + /* current standard, composite and S-Video only */ + v4l2_std_id std; + /* current timings, component and HDMI only */ + struct v4l2_dv_timings timings; + bool no_signal; + int width; + int height; + int interlaced; + int bpl; /* width * 2 */ + + /* stream parser state */ + u8 *parse_buf; + int parse_buf_size; + int parse_len; + struct hdcap_buf *cur_buf; + int frame_line; + int synced; + int was_blanking; + /* BT.656 F bit of the field being captured */ + u8 field_f; + u8 top_done; + + /* hardware init state */ + int hw_inited; + int has_mcu; /* Nuvoton MCU present (XCAPTURE-1) */ + + /* MST3367 state */ + int mst_current_bank; + bool mst_bt601; + + /* ALSA audio */ + struct snd_card *snd; + struct snd_pcm_substream *snd_substream; + atomic_t snd_stream; + size_t snd_buffer_pos; + size_t snd_period_pos; + + /* diagnostics */ + unsigned int iso_cb_count; + unsigned long iso_bytes; + unsigned int markers_found; + unsigned int frames_delivered; +}; + +/* USB control helpers (usb3hdcap-core.c) */ +int vendor_out(struct usb3hdcap *hdcap, u8 request, u16 value, u16 index, + u8 *data, u16 len); +int u3hc_i2c_write(struct usb3hdcap *hdcap, u8 addr, u8 reg, u8 val); +int u3hc_i2c_read(struct usb3hdcap *hdcap, u8 addr, u8 reg); +int u3hc_i2c_rmw(struct usb3hdcap *hdcap, u8 addr, u8 reg, u8 mask, u8 bit= s); + +/* Composite (usb3hdcap-composite.c) */ +int usb3hdcap_composite_register(struct usb3hdcap *hdcap); +int usb3hdcap_composite_init(struct usb3hdcap *hdcap); +int usb3hdcap_composite_querystd(struct usb3hdcap *hdcap, v4l2_std_id *std= ); +int usb3hdcap_composite_set_std(struct usb3hdcap *hdcap, v4l2_std_id std); + +/* HDMI + Component (usb3hdcap-hdmi.c) */ +int usb3hdcap_hdmi_register(struct usb3hdcap *hdcap); +int usb3hdcap_hdmi_init(struct usb3hdcap *hdcap); +int usb3hdcap_component_init(struct usb3hdcap *hdcap); +int usb3hdcap_hd_set_timings(struct usb3hdcap *hdcap, + struct v4l2_dv_timings *timings); +void usb3hdcap_hdmi_hotplug(struct usb3hdcap *hdcap, bool hotplug); + +/* Video/streaming (usb3hdcap-video.c) */ +extern const struct vb2_ops usb3hdcap_vb2_ops; + +/* Audio (usb3hdcap-audio.c) */ +int usb3hdcap_cs53l21_init(struct usb3hdcap *hdcap); +int usb3hdcap_audio_init(struct usb3hdcap *hdcap); +void usb3hdcap_audio_free(struct usb3hdcap *hdcap); +void usb3hdcap_audio_data(struct usb3hdcap *hdcap, const u8 *data, int len= ); + +#endif --=20 2.47.3