Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp3049346imm; Fri, 24 Aug 2018 09:38:00 -0700 (PDT) X-Google-Smtp-Source: ANB0Vda8GC4pu4eRGUAuFOwCil9l5SduPCIAqL576WNSQqxFyZ1mpdQBFVB0HGhC+LDmgvjOxjrG X-Received: by 2002:a63:a1e:: with SMTP id 30-v6mr2497313pgk.318.1535128680426; Fri, 24 Aug 2018 09:38:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535128680; cv=none; d=google.com; s=arc-20160816; b=VAttCArLw2r7AXipxb6VvC9BRNvhBGV/DNp1iMN6LdmJ9gi9SZNy2biGK4XwzxeLbQ tmk2pwdNJSyxN/asETgQzpWRE9HTQXFzBrsl+nHmJl3XJAt0QaKkK5XAeBlCa/RSHUop jTI/3yrS8GO4BH1CmeOGRNbcCZxO39yjMEwyoZrgCV7an4iv+4DUSRx0JActSjOPTH4P 7gNPtMzA6458zlLYeDo3Ln2+paaloxiHi3QK5jp1ry6kbZWnewoZR9S4v5EYVJ/XWtaq ydLVFKXvEDFNe/gwpkHO62QQFOMqauvCRDdB4kEop6EG0iWZS15QotHGQncJ65HA5hbU 6H6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=9qyJAbtqdKgJZXm8FNfOjfciPKb8GtuIjNgzDCTaIuQ=; b=qxYAwJ6QJHSL9746G88+zhOBZkBUuMTvykiukVM5yop90JFDk0EwWmH9df6ST+xoGR AemgpFbOV6DFgWY9AVESkdz8YTZRDZcDTGpQYAb/RtxJkRmjVgrWvuMMxSqy0ietvtwp hfGXZnXwzH1SXfTrdurAgOSXh6w9oMTcb1VwuY88bV9wNdxxicjtIus5aMyqGICFUsl0 MXPfbInpt1snwvL31snIdOQM14BDuIa8s2m6dL8pkn5rkUaNrsfZRvzSYbtplYtIbvsA tZGZEsd3+ze6JSmUGogrAi75nzR1eUWXJlAcCDAWm8PlUZBc2/MtHckMov7qfkSSbxxN NmaA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e10-v6si7561401pln.161.2018.08.24.09.37.45; Fri, 24 Aug 2018 09:38:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728262AbeHXULk (ORCPT + 99 others); Fri, 24 Aug 2018 16:11:40 -0400 Received: from srv-hp10-72.netsons.net ([94.141.22.72]:37647 "EHLO srv-hp10-72.netsons.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727808AbeHXULf (ORCPT ); Fri, 24 Aug 2018 16:11:35 -0400 Received: from [109.168.11.45] (port=58220 helo=pc-ceresoli.dev.aim) by srv-hp10.netsons.net with esmtpa (Exim 4.91) (envelope-from ) id 1ftF4A-00EAi1-3j; Fri, 24 Aug 2018 18:36:06 +0200 From: Luca Ceresoli To: linux-media@vger.kernel.org Cc: Luca Ceresoli , Leon Luo , Mauro Carvalho Chehab , Sakari Ailus , linux-kernel@vger.kernel.org Subject: [PATCH 6/7] media: imx274: add helper to read multibyte registers Date: Fri, 24 Aug 2018 18:35:24 +0200 Message-Id: <20180824163525.12694-7-luca@lucaceresoli.net> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180824163525.12694-1-luca@lucaceresoli.net> References: <20180824163525.12694-1-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently 2-bytes and 3-bytes registers are read one byte at a time, doing the needed shift & mask each time. Replace all of this code by a unique helper function that calls regmap_bulk_read(), which has two advantages: - reads all the bytes in a unique I2C transaction - simplifies code to read multibyte registers Signed-off-by: Luca Ceresoli --- drivers/media/i2c/imx274.c | 93 +++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/drivers/media/i2c/imx274.c b/drivers/media/i2c/imx274.c index 07bc41f537c5..783277068b45 100644 --- a/drivers/media/i2c/imx274.c +++ b/drivers/media/i2c/imx274.c @@ -659,6 +659,41 @@ static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val) return err; } +/** + * Read a multibyte register. + * + * Uses a bulk read where possible. + * + * @priv: Pointer to device structure + * @addr: Address of the LSB register. Other registers must be + * consecutive, least-to-most significant. + * @val: Pointer to store the register value (cpu endianness) + * @nbytes: Number of bytes to read (range: [1..3]). + * Other bytes are zet to 0. + * + * Return: 0 on success, errors otherwise + */ +static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val, + size_t nbytes) +{ + __le32 val_le = 0; + int err; + + err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes); + if (err) { + dev_err(&priv->client->dev, + "%s : i2c bulk read failed, %x (%zu bytes)\n", + __func__, addr, nbytes); + } else { + *val = le32_to_cpu(val_le); + dev_dbg(&priv->client->dev, + "%s : addr 0x%x, val=0x%x (%zu bytes)\n", + __func__, addr, *val, nbytes); + } + + return err; +} + /** * Write a multibyte register. * @@ -1377,37 +1412,17 @@ static int imx274_s_stream(struct v4l2_subdev *sd, int on) static int imx274_get_frame_length(struct stimx274 *priv, u32 *val) { int err; - u16 svr; + u32 svr; u32 vmax; - u8 reg_val[3]; - /* svr */ - err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, ®_val[0]); + err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2); if (err) goto fail; - err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, ®_val[1]); + err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3); if (err) goto fail; - svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; - - /* vmax */ - err = imx274_read_reg(priv, IMX274_VMAX_REG_3, ®_val[0]); - if (err) - goto fail; - - err = imx274_read_reg(priv, IMX274_VMAX_REG_2, ®_val[1]); - if (err) - goto fail; - - err = imx274_read_reg(priv, IMX274_VMAX_REG_1, ®_val[2]); - if (err) - goto fail; - - vmax = ((reg_val[2] & IMX274_MASK_LSB_3_BITS) << IMX274_SHIFT_16_BITS) - + (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; - *val = vmax * (svr + 1); return 0; @@ -1588,8 +1603,7 @@ static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val) static int imx274_set_exposure(struct stimx274 *priv, int val) { int err; - u16 hmax; - u8 reg_val[2]; + u32 hmax; u32 coarse_time; /* exposure time in unit of line (HMAX)*/ dev_dbg(&priv->client->dev, @@ -1597,14 +1611,10 @@ static int imx274_set_exposure(struct stimx274 *priv, int val) /* step 1: convert input exposure_time (val) into number of 1[HMAX] */ - /* obtain HMAX value */ - err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, ®_val[0]); - if (err) - goto fail; - err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, ®_val[1]); + err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2); if (err) goto fail; - hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; + if (hmax == 0) { err = -EINVAL; goto fail; @@ -1739,9 +1749,8 @@ static int imx274_set_frame_interval(struct stimx274 *priv, { int err; u32 frame_length, req_frame_rate; - u16 svr; - u16 hmax; - u8 reg_val[2]; + u32 svr; + u32 hmax; dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d", __func__, frame_interval.numerator, @@ -1769,25 +1778,17 @@ static int imx274_set_frame_interval(struct stimx274 *priv, * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX */ - /* SVR */ - err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, ®_val[0]); + err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2); if (err) goto fail; - err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, ®_val[1]); - if (err) - goto fail; - svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; + dev_dbg(&priv->client->dev, "%s : register SVR = %d\n", __func__, svr); - /* HMAX */ - err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, ®_val[0]); + err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2); if (err) goto fail; - err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, ®_val[1]); - if (err) - goto fail; - hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; + dev_dbg(&priv->client->dev, "%s : register HMAX = %d\n", __func__, hmax); -- 2.17.1