From patchwork Tue Mar 14 11:55:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vaishnav Achath X-Patchwork-Id: 69532 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1717899wrd; Tue, 14 Mar 2023 05:10:42 -0700 (PDT) X-Google-Smtp-Source: AK7set+OAPPXA+5X2VVQQT6BGmtOvWXyRoDEh1gz5hhnJ+dwrFeu8FF87pCtlMNMVC5VRsoT2HEt X-Received: by 2002:a17:903:4cd:b0:19c:ed28:66ec with SMTP id jm13-20020a17090304cd00b0019ced2866ecmr31290367plb.21.1678795842056; Tue, 14 Mar 2023 05:10:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678795842; cv=none; d=google.com; s=arc-20160816; b=I0wWmxTtWJ1eMw6FGIY6nLxaWx/H/Ynm2AfBR6HEpftj4xe6ORNI1IJ+5IJ8M/k682 tftrxPCO4xejYCI9QnGuXw/kYyTk9NIcQ0Ja/Ngd+wmXPITHvZi0E7ejP3UIEw7cu36t 4/MfNWOXVRMN2G6athtfieXZurSTb5ey24DuzuV3sQJIFvEuz/YvD8DPOgqPlua9iKtZ B7nJ5cF6hq8aE8yOxyM9CXRn4a8rvpr67gZJ9CxSmaciDFW3fcCf1cYJctTQznOR7dFb MOA1diDRASTBul7ycXvGVjyr40LSoo5YjTplua2bdC36MFszJXUCnWtWiTH/B08eLrIZ UMEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=IFJsUQk232s7e8N9uoqezRx3OBmYY8+n+kXrVuwBMoI=; b=o6tmB5Y5CNZW1FrQEFRPaLgTzYjdWstST17iWYFJxZmd4hoOlE7GoIr+j3z0NrXC6F WCtSv8rSMu3nBH+waSaQdMTiF1zOy5barYri9+nSzz7svP1XpQI9PEfntEnHuAZ7W9c6 iEKYI4xoETUiYuhy8kWcFpuB05DhgEC7cWhlN98yj9DlAgFEZL6Ki+29XgyDQscUK/Ku SkWWtJi/uIFKEfe0nZkbV2CEg5vDM1KFac4Q2bxG1dmEdYuDQw7ck5zQV4GYvljzUI3l Xw1ofRpbtow8xuYZJThFElnh3nEVmDI7xmN+QkocmjKyXOyFL4cjdLUDJEcnferGZWqu 1vMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=iL8UVw+V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ko12-20020a17090307cc00b0019a71e14c19si2368447plb.320.2023.03.14.05.10.26; Tue, 14 Mar 2023 05:10:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=iL8UVw+V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231572AbjCNLzw (ORCPT + 99 others); Tue, 14 Mar 2023 07:55:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231526AbjCNLzr (ORCPT ); Tue, 14 Mar 2023 07:55:47 -0400 Received: from fllv0016.ext.ti.com (fllv0016.ext.ti.com [198.47.19.142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 082348A3AE; Tue, 14 Mar 2023 04:55:45 -0700 (PDT) Received: from lelv0266.itg.ti.com ([10.180.67.225]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id 32EBtP8m105671; Tue, 14 Mar 2023 06:55:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1678794925; bh=IFJsUQk232s7e8N9uoqezRx3OBmYY8+n+kXrVuwBMoI=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=iL8UVw+V8s4vUm8sj2/h7wrug3M7mhZRcR1T5acfpLO9/ylMq4AjTuq4+4VH2kEdk +d4b2kOa92/XWqIU+RSZ9Cs+tGyKasqlvP97igKH2A5r6lQhUvE7+MJIqru6j7PBNh GxKp/ysJzsrvMD/DE2/Cr29aQQOy7ruRNbbwwfpQ= Received: from DLEE106.ent.ti.com (dlee106.ent.ti.com [157.170.170.36]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 32EBtPp1112494 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 14 Mar 2023 06:55:25 -0500 Received: from DLEE109.ent.ti.com (157.170.170.41) by DLEE106.ent.ti.com (157.170.170.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.16; Tue, 14 Mar 2023 06:55:24 -0500 Received: from lelv0327.itg.ti.com (10.180.67.183) by DLEE109.ent.ti.com (157.170.170.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.16 via Frontend Transport; Tue, 14 Mar 2023 06:55:24 -0500 Received: from localhost (ileaxei01-snat2.itg.ti.com [10.180.69.6]) by lelv0327.itg.ti.com (8.15.2/8.15.2) with ESMTP id 32EBtNok111799; Tue, 14 Mar 2023 06:55:24 -0500 From: Vaishnav Achath To: , , , , , , , , CC: , , , , , , , , , , Subject: [PATCH v7 03/13] media: cadence: csi2rx: Add get_fmt and set_fmt pad ops Date: Tue, 14 Mar 2023 17:25:06 +0530 Message-ID: <20230314115516.667-4-vaishnav.a@ti.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230314115516.667-1-vaishnav.a@ti.com> References: <20230314115516.667-1-vaishnav.a@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760345028516203799?= X-GMAIL-MSGID: =?utf-8?q?1760345028516203799?= From: Pratyush Yadav The format is needed to calculate the link speed for the external DPHY configuration. It is not right to query the format from the source subdev. Add get_fmt and set_fmt pad operations so that the format can be configured and correct bpp be selected. Signed-off-by: Pratyush Yadav Signed-off-by: Vaishnav Achath Reviewed-by: Laurent Pinchart --- Changes in v7: - Add Laurent's R-by. Changes in v6: - Move the lock around the dereference for framefmt in csi2rx_{get,set}_fmt() instead of when we get the pointer. - Do not return an error when an unsupported format is set. Instead adjust the code to the first format in the list. - Not adding Laurent's R-by since I am making changes in csi2rx_set_fmt() that he didn't explicitly recommend. Changes in v5: - Use YUV 1X16 formats instead of 2X8. - New in v5. drivers/media/platform/cadence/cdns-csi2rx.c | 131 +++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c index 2547903f2e8e..ae3ebdb3890d 100644 --- a/drivers/media/platform/cadence/cdns-csi2rx.c +++ b/drivers/media/platform/cadence/cdns-csi2rx.c @@ -54,6 +54,11 @@ enum csi2rx_pads { CSI2RX_PAD_MAX, }; +struct csi2rx_fmt { + u32 code; + u8 bpp; +}; + struct csi2rx_priv { struct device *dev; unsigned int count; @@ -79,12 +84,43 @@ struct csi2rx_priv { struct v4l2_subdev subdev; struct v4l2_async_notifier notifier; struct media_pad pads[CSI2RX_PAD_MAX]; + struct v4l2_mbus_framefmt fmt; /* Remote source */ struct v4l2_subdev *source_subdev; int source_pad; }; +static const struct csi2rx_fmt formats[] = { + { + .code = MEDIA_BUS_FMT_YUYV8_1X16, + .bpp = 16, + }, + { + .code = MEDIA_BUS_FMT_UYVY8_1X16, + .bpp = 16, + }, + { + .code = MEDIA_BUS_FMT_YVYU8_1X16, + .bpp = 16, + }, + { + .code = MEDIA_BUS_FMT_VYUY8_1X16, + .bpp = 16, + }, +}; + +static const struct csi2rx_fmt *csi2rx_get_fmt_by_code(u32 code) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(formats); i++) + if (formats[i].code == code) + return &formats[i]; + + return NULL; +} + static inline struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev) { @@ -236,12 +272,103 @@ static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable) return ret; } +static struct v4l2_mbus_framefmt * +csi2rx_get_pad_format(struct csi2rx_priv *csi2rx, + struct v4l2_subdev_state *state, + unsigned int pad, u32 which) +{ + switch (which) { + case V4L2_SUBDEV_FORMAT_TRY: + return v4l2_subdev_get_try_format(&csi2rx->subdev, state, pad); + case V4L2_SUBDEV_FORMAT_ACTIVE: + return &csi2rx->fmt; + default: + return NULL; + } +} + +static int csi2rx_get_fmt(struct v4l2_subdev *subdev, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *format) +{ + struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev); + struct v4l2_mbus_framefmt *framefmt; + + framefmt = csi2rx_get_pad_format(csi2rx, state, format->pad, + format->which); + if (!framefmt) + return -EINVAL; + + mutex_lock(&csi2rx->lock); + format->format = *framefmt; + mutex_unlock(&csi2rx->lock); + + return 0; +} + +static int csi2rx_set_fmt(struct v4l2_subdev *subdev, + struct v4l2_subdev_state *state, + struct v4l2_subdev_format *format) +{ + struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev); + struct v4l2_mbus_framefmt *framefmt; + + /* No transcoding, source and sink formats must match. */ + if (format->pad != CSI2RX_PAD_SINK) + return csi2rx_get_fmt(subdev, state, format); + + if (!csi2rx_get_fmt_by_code(format->format.code)) + format->format.code = formats[0].code; + + format->format.field = V4L2_FIELD_NONE; + + framefmt = csi2rx_get_pad_format(csi2rx, state, format->pad, + format->which); + if (!framefmt) + return -EINVAL; + + mutex_lock(&csi2rx->lock); + *framefmt = format->format; + mutex_unlock(&csi2rx->lock); + + return 0; +} + +static int csi2rx_init_cfg(struct v4l2_subdev *subdev, + struct v4l2_subdev_state *state) +{ + struct v4l2_subdev_format format = { + .which = state ? V4L2_SUBDEV_FORMAT_TRY + : V4L2_SUBDEV_FORMAT_ACTIVE, + .pad = CSI2RX_PAD_SINK, + .format = { + .width = 640, + .height = 480, + .code = MEDIA_BUS_FMT_UYVY8_1X16, + .field = V4L2_FIELD_NONE, + .colorspace = V4L2_COLORSPACE_SRGB, + .ycbcr_enc = V4L2_YCBCR_ENC_601, + .quantization = V4L2_QUANTIZATION_LIM_RANGE, + .xfer_func = V4L2_XFER_FUNC_SRGB, + }, + }; + + return csi2rx_set_fmt(subdev, state, &format); +} + +static const struct v4l2_subdev_pad_ops csi2rx_pad_ops = { + .get_fmt = csi2rx_get_fmt, + .set_fmt = csi2rx_set_fmt, + .init_cfg = csi2rx_init_cfg, +}; + static const struct v4l2_subdev_video_ops csi2rx_video_ops = { .s_stream = csi2rx_s_stream, }; static const struct v4l2_subdev_ops csi2rx_subdev_ops = { .video = &csi2rx_video_ops, + .pad = &csi2rx_pad_ops, }; static int csi2rx_async_bound(struct v4l2_async_notifier *notifier, @@ -457,6 +584,10 @@ static int csi2rx_probe(struct platform_device *pdev) if (ret) goto err_cleanup; + ret = csi2rx_init_cfg(&csi2rx->subdev, NULL); + if (ret) + goto err_cleanup; + ret = v4l2_async_register_subdev(&csi2rx->subdev); if (ret < 0) goto err_cleanup;