From patchwork Thu Jun 22 15:12:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugues FRUCHET X-Patchwork-Id: 9804669 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5E83E60329 for ; Thu, 22 Jun 2017 15:13:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C2501FF52 for ; Thu, 22 Jun 2017 15:13:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 00C0328415; Thu, 22 Jun 2017 15:13:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 85F2D1FF52 for ; Thu, 22 Jun 2017 15:13:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=RMmZTlR7NVxZScEWyG3E9N8oH+u2tUtSoraJZ8145jM=; b=pXao8NKhaqe9GH PE9s/FUZXeQ6hfDQdO6sopVwhUf5N3oQMRLoy4aZJeOy4G9bt29wg0jLs1i9h00vUsQaDgKNcBl0d YVEVXxi6ohu9PrJqh9OaSaPaP84ffZj/ATij5Y/yYl3AbYmtdRPZ3m4AhwC6sq6OVRix7cjkjdpn4 QSaezVkeTlalZBlvHYFR7LCyMKlxV74ZAs4CEB/rwM+stBL7HAC+ZP01SDg0AUYI6H0N6kgLhrT+C qDVdE6FH8Ta1XMI+h2ghVOKXkXd5YXfzlFfd6LeUZPEmjbpFZwaj3Df9r4KBWrrkJRk6C07EDviRj MEVEvfmvlgSoAEQ5qXsw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dO3nu-0005H8-Fg; Thu, 22 Jun 2017 15:13:54 +0000 Received: from mx08-00178001.pphosted.com ([91.207.212.93] helo=mx07-00178001.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dO3nd-00051K-Sa for linux-arm-kernel@lists.infradead.org; Thu, 22 Jun 2017 15:13:40 +0000 Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx08-.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v5MF9SEs012638; Thu, 22 Jun 2017 17:13:17 +0200 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx08-.pphosted.com with ESMTP id 2b8d2m9502-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 22 Jun 2017 17:13:17 +0200 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 1061431; Thu, 22 Jun 2017 15:13:16 +0000 (GMT) Received: from Webmail-eu.st.com (Safex1hubcas23.st.com [10.75.90.46]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id E8ECA2648; Thu, 22 Jun 2017 15:13:15 +0000 (GMT) Received: from localhost (10.201.23.73) by webmail-ga.st.com (10.75.90.48) with Microsoft SMTP Server (TLS) id 14.3.339.0; Thu, 22 Jun 2017 17:13:15 +0200 From: Hugues Fruchet To: Maxime Coquelin , Alexandre Torgue , Mauro Carvalho Chehab , Hans Verkuil Subject: [PATCH v1 3/5] [media] stm32-dcmi: crop sensor image to match user resolution Date: Thu, 22 Jun 2017 17:12:49 +0200 Message-ID: <1498144371-13310-4-git-send-email-hugues.fruchet@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1498144371-13310-1-git-send-email-hugues.fruchet@st.com> References: <1498144371-13310-1-git-send-email-hugues.fruchet@st.com> MIME-Version: 1.0 X-Originating-IP: [10.201.23.73] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-06-22_06:, , signatures=0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170622_081338_246806_6D273E06 X-CRM114-Status: GOOD ( 13.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Yannick Fertre , Benjamin Gaignard , Hugues Fruchet , linux-arm-kernel@lists.infradead.org, linux-media@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add flexibility on supported resolutions by cropping sensor image to fit user resolution format request. Signed-off-by: Hugues Fruchet --- drivers/media/platform/stm32/stm32-dcmi.c | 54 ++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/stm32/stm32-dcmi.c b/drivers/media/platform/stm32/stm32-dcmi.c index 75d53aa..bc5e052 100644 --- a/drivers/media/platform/stm32/stm32-dcmi.c +++ b/drivers/media/platform/stm32/stm32-dcmi.c @@ -131,6 +131,8 @@ struct stm32_dcmi { struct v4l2_async_notifier notifier; struct dcmi_graph_entity entity; struct v4l2_format fmt; + struct v4l2_rect crop; + bool do_crop; const struct dcmi_format **user_formats; unsigned int num_user_formats; @@ -538,6 +540,27 @@ static int dcmi_start_streaming(struct vb2_queue *vq, unsigned int count) if (dcmi->bus.flags & V4L2_MBUS_PCLK_SAMPLE_RISING) val |= CR_PCKPOL; + if (dcmi->do_crop) { + u32 size, start; + + /* Crop resolution */ + size = ((dcmi->crop.height - 1) << 16) | + ((dcmi->crop.width << 1) - 1); + reg_write(dcmi->regs, DCMI_CWSIZE, size); + + /* Crop start point */ + start = ((dcmi->crop.top) << 16) | + ((dcmi->crop.left << 1)); + reg_write(dcmi->regs, DCMI_CWSTRT, start); + + dev_dbg(dcmi->dev, "Cropping to %ux%u@%u:%u\n", + dcmi->crop.width, dcmi->crop.height, + dcmi->crop.left, dcmi->crop.top); + + /* Enable crop */ + val |= CR_CROP; + }; + reg_write(dcmi->regs, DCMI_CR, val); /* Enable dcmi */ @@ -707,6 +730,8 @@ static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f, .which = V4L2_SUBDEV_FORMAT_TRY, }; int ret; + __u32 width, height; + struct v4l2_mbus_framefmt *mf = &format.format; dcmi_fmt = find_format_by_fourcc(dcmi, pixfmt->pixelformat); if (!dcmi_fmt) { @@ -724,8 +749,18 @@ static int dcmi_try_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f, if (ret < 0) return ret; + /* Align format on what sensor can do */ + width = pixfmt->width; + height = pixfmt->height; v4l2_fill_pix_format(pixfmt, &format.format); + /* We can do any resolution thanks to crop */ + if ((mf->width > width) || (mf->height > height)) { + /* Restore width/height */ + pixfmt->width = width; + pixfmt->height = height; + }; + pixfmt->field = V4L2_FIELD_NONE; pixfmt->bytesperline = pixfmt->width * dcmi_fmt->bpp; pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height; @@ -741,6 +776,8 @@ static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f) struct v4l2_subdev_format format = { .which = V4L2_SUBDEV_FORMAT_ACTIVE, }; + struct v4l2_mbus_framefmt *mf = &format.format; + struct v4l2_pix_format *pixfmt = &f->fmt.pix; const struct dcmi_format *current_fmt; int ret; @@ -748,13 +785,28 @@ static int dcmi_set_fmt(struct stm32_dcmi *dcmi, struct v4l2_format *f) if (ret) return ret; - v4l2_fill_mbus_format(&format.format, &f->fmt.pix, + v4l2_fill_mbus_format(&format.format, pixfmt, current_fmt->mbus_code); ret = v4l2_subdev_call(dcmi->entity.subdev, pad, set_fmt, NULL, &format); if (ret < 0) return ret; + /* Enable crop if sensor resolution is larger than request */ + dcmi->do_crop = false; + if ((mf->width > pixfmt->width) || (mf->height > pixfmt->height)) { + dcmi->crop.width = pixfmt->width; + dcmi->crop.height = pixfmt->height; + dcmi->crop.left = (mf->width - pixfmt->width) / 2; + dcmi->crop.top = (mf->height - pixfmt->height) / 2; + dcmi->do_crop = true; + + dev_dbg(dcmi->dev, "%ux%u cropped to %ux%u@(%u,%u)\n", + mf->width, mf->height, + dcmi->crop.width, dcmi->crop.height, + dcmi->crop.left, dcmi->crop.top); + }; + dcmi->fmt = *f; dcmi->current_fmt = current_fmt;