From patchwork Tue Apr 24 08:24:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Ceresoli X-Patchwork-Id: 10358959 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 86B4A60225 for ; Tue, 24 Apr 2018 08:25:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 735B228CF7 for ; Tue, 24 Apr 2018 08:25:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 67E2F28D3B; Tue, 24 Apr 2018 08:25:40 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DBE9928D3F for ; Tue, 24 Apr 2018 08:25:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756745AbeDXIZf (ORCPT ); Tue, 24 Apr 2018 04:25:35 -0400 Received: from srv-hp10-72.netsons.net ([94.141.22.72]:58457 "EHLO srv-hp10-72.netsons.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756643AbeDXIYg (ORCPT ); Tue, 24 Apr 2018 04:24:36 -0400 Received: from [109.168.11.45] (port=48286 helo=pc-ceresoli.dev.aim) by srv-hp10.netsons.net with esmtpa (Exim 4.89_1) (envelope-from ) id 1fAtFZ-000Mfg-O3; Tue, 24 Apr 2018 10:24:33 +0200 From: Luca Ceresoli To: linux-media@vger.kernel.org Cc: Luca Ceresoli , Leon Luo , Mauro Carvalho Chehab , linux-kernel@vger.kernel.org Subject: [PATCH v2 12/13] media: imx274: add helper function to fill a reg_8 table chunk Date: Tue, 24 Apr 2018 10:24:17 +0200 Message-Id: <1524558258-530-13-git-send-email-luca@lucaceresoli.net> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1524558258-530-1-git-send-email-luca@lucaceresoli.net> References: <1524558258-530-1-git-send-email-luca@lucaceresoli.net> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - srv-hp10.netsons.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lucaceresoli.net X-Get-Message-Sender-Via: srv-hp10.netsons.net: authenticated_id: luca+lucaceresoli.net/only user confirmed/virtual account not confirmed X-Authenticated-Sender: srv-hp10.netsons.net: luca@lucaceresoli.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Tables of struct reg_8 are used to simplify multi-byte register assignment. However filling these snippets with values computed at runtime is currently implemented by very similar functions doing the needed shift & mask manipulation. Replace all those functions with a unique helper function to fill reg_8 tables in a simple and clean way. Signed-off-by: Luca Ceresoli --- Changed v1 -> v2: - add "media: " prefix to commit message --- drivers/media/i2c/imx274.c | 90 ++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/drivers/media/i2c/imx274.c b/drivers/media/i2c/imx274.c index bc307eb65a10..b6c54712f2aa 100644 --- a/drivers/media/i2c/imx274.c +++ b/drivers/media/i2c/imx274.c @@ -597,6 +597,58 @@ static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd) } /* + * Fill consecutive reg_8 items in order to set a multibyte register. + * + * The sensor has many 2-bytes registers and a 3-byte register. This + * simplifies code to set them by filling consecutive entries of a + * struct reg_8 table with the data to set a register. + * + * The number of table entries that is filled is the minimum needed + * for the given number of bits (i.e. nbits / 8, rounded up). If nbits + * is not a multiple of 8, extra bits in the most significant byte are + * zeroed. + * + * Example: + * Calling prepare_reg(®s[10], 0x3000, 0xcafe, 12) will set: + * regs[10] = { .addr = 0x3000, .val = 0xfe } + * regs[11] = { .addr = 0x3001, .val = 0x0a } + * + * @table_base: Pointer to the first reg_8 struct to be filled. The + * following entries will also be set, make sure they are + * properly allocated! + * @addr_lsb: Address of the LSB register. Other registers must be + * consecutive, least-to-most significant. + * @value: Value to be written to the register. + * @nbits: Number of bits to write (range: [1..24]) + */ +static void prepare_reg(struct reg_8 *table_base, + u16 addr_lsb, + u32 value, + size_t nbits) +{ + struct reg_8 *cmd = table_base; + u16 addr = addr_lsb; + size_t bits; /* how many bits at this round */ + + WARN_ON(nbits > 24); + + if (nbits > 24) + nbits = 24; + + while (nbits > 0) { + bits = min_t(size_t, 8, nbits); + + cmd->addr = addr; + cmd->val = value & ((1 << bits) - 1); + + nbits -= bits; + value >>= 8; + addr++; + cmd++; + } +} + +/* * Writing a register table * * @priv: Pointer to device @@ -1163,15 +1215,6 @@ static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain) reg_val & IMX274_MASK_LSB_4_BITS); } -static inline void imx274_calculate_gain_regs(struct reg_8 regs[2], u16 gain) -{ - regs->addr = IMX274_ANALOG_GAIN_ADDR_MSB; - regs->val = (gain >> IMX274_SHIFT_8_BITS) & IMX274_MASK_LSB_3_BITS; - - (regs + 1)->addr = IMX274_ANALOG_GAIN_ADDR_LSB; - (regs + 1)->val = (gain) & IMX274_MASK_LSB_8_BITS; -} - /* * imx274_set_gain - Function called when setting gain * @priv: Pointer to device structure @@ -1229,7 +1272,7 @@ static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl) if (gain_reg > IMX274_GAIN_REG_MAX) gain_reg = IMX274_GAIN_REG_MAX; - imx274_calculate_gain_regs(reg_list, (u16)gain_reg); + prepare_reg(reg_list, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg, 11); for (i = 0; i < ARRAY_SIZE(reg_list); i++) { err = imx274_write_reg(priv, reg_list[i].addr, @@ -1258,16 +1301,6 @@ static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl) return err; } -static inline void imx274_calculate_coarse_time_regs(struct reg_8 regs[2], - u32 coarse_time) -{ - regs->addr = IMX274_SHR_REG_MSB; - regs->val = (coarse_time >> IMX274_SHIFT_8_BITS) - & IMX274_MASK_LSB_8_BITS; - (regs + 1)->addr = IMX274_SHR_REG_LSB; - (regs + 1)->val = (coarse_time) & IMX274_MASK_LSB_8_BITS; -} - /* * imx274_set_coarse_time - Function called when setting SHR value * @priv: Pointer to device structure @@ -1292,7 +1325,7 @@ static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val) goto fail; /* prepare SHR registers */ - imx274_calculate_coarse_time_regs(reg_list, coarse_time); + prepare_reg(reg_list, IMX274_SHR_REG_LSB, coarse_time, 16); /* write to SHR registers */ for (i = 0; i < ARRAY_SIZE(reg_list); i++) { @@ -1429,19 +1462,6 @@ static int imx274_set_test_pattern(struct stimx274 *priv, int val) return err; } -static inline void imx274_calculate_frame_length_regs(struct reg_8 regs[3], - u32 frame_length) -{ - regs->addr = IMX274_VMAX_REG_1; - regs->val = (frame_length >> IMX274_SHIFT_16_BITS) - & IMX274_MASK_LSB_4_BITS; - (regs + 1)->addr = IMX274_VMAX_REG_2; - (regs + 1)->val = (frame_length >> IMX274_SHIFT_8_BITS) - & IMX274_MASK_LSB_8_BITS; - (regs + 2)->addr = IMX274_VMAX_REG_3; - (regs + 2)->val = (frame_length) & IMX274_MASK_LSB_8_BITS; -} - /* * imx274_set_frame_length - Function called when setting frame length * @priv: Pointer to device structure @@ -1463,7 +1483,7 @@ static int imx274_set_frame_length(struct stimx274 *priv, u32 val) frame_length = (u32)val; - imx274_calculate_frame_length_regs(reg_list, frame_length); + prepare_reg(reg_list, IMX274_VMAX_REG_3, frame_length, 20); for (i = 0; i < ARRAY_SIZE(reg_list); i++) { err = imx274_write_reg(priv, reg_list[i].addr, reg_list[i].val);