From patchwork Sun Dec 23 04:57:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Fertic X-Patchwork-Id: 10741561 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3FA8F13AD for ; Sun, 23 Dec 2018 04:58:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DE9D2842D for ; Sun, 23 Dec 2018 04:58:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2158D2843B; Sun, 23 Dec 2018 04:58:48 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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 C36662842D for ; Sun, 23 Dec 2018 04:58:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726382AbeLWE6k (ORCPT ); Sat, 22 Dec 2018 23:58:40 -0500 Received: from mail-io1-f67.google.com ([209.85.166.67]:40276 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725888AbeLWE6i (ORCPT ); Sat, 22 Dec 2018 23:58:38 -0500 Received: by mail-io1-f67.google.com with SMTP id a11so4375853ioq.7; Sat, 22 Dec 2018 20:58:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hbV2rZQ67LuJPX6YUHnnKWTvJhORDui2wg3k/8TbKxY=; b=uM7vIrg1kODY4nh5lBOZezgmmNgNRj5OFh4MwhlaAbQO8BkMwZ9vzPt8j+SrKUmV/R wcc71DW91AgCH8rgOnKi1JF8TwLYWNr8diqzXJuaTBlzJ2Bupba0ZG3yR6spWDJDc6jT x/CYcp4Dkk5VJWcPtkNLI84H+ei2079kxjdyzy+r6DjbYZG6XuoHn+xh8jLGlaxva538 Kzg9njR8cCcwtlstCSrZs5Od6oo8VCyZT91555y1HgFjZS6nbZvHgTDMkGDwn+1dP/d3 udQzIji5/byY4epJdumAyaQTgwAp3M8hSONvvErls5aTKxBeggBTz61ioydKt8RAWja7 73ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hbV2rZQ67LuJPX6YUHnnKWTvJhORDui2wg3k/8TbKxY=; b=BkdGUksqAgHeLzTed/qY9M8H3q3GsPFOBNqam0sE3D2HQDPC2v4neCUVfcT7g5mOZn QKGfWHf+VN1fiO82mPwfzlDv3avy1wiS298PqKvUmJaoZ6gL6M8Ky/0ZYFiprj5a/1Ei obElyyFPIQxE2J6Tlokw6M7rHDXOk7hGnwFXS6WBbOJHhGCL0c+ULCjFqpqL9Yxh6zpI d7wPtMGsOIWhivFU1zO9jFgAnAt6QLrbRF/N6qs1GJb5qmJEdEEdBkOv8+1f/01iM91G 6/qBLokf/ccewwQ3nZ3adJLb/ieGhMiuz5RwgByh3cg92w3hoyiIiqMzeUgFzxUeResc HXRw== X-Gm-Message-State: AJcUukdWHSCoCR8pixncb5P1k2WdGcTj5ZSGvzAUdWu3KHxZh2qLHoQV /NJSxAR/iHGqjEIRGlikcik= X-Google-Smtp-Source: ALg8bN5h5xy3arU6MQX/ghft3R+OOPcP+oPq+P2C/OygtNGjtaH5V8SZSx3T5m8o777D4wj6a87xkg== X-Received: by 2002:a5e:c914:: with SMTP id z20mr5747178iol.72.1545541116919; Sat, 22 Dec 2018 20:58:36 -0800 (PST) Received: from r2700x.localdomain (c-75-70-96-103.hsd1.co.comcast.net. [75.70.96.103]) by smtp.gmail.com with ESMTPSA id 15sm8435378itv.11.2018.12.22.20.58.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 22 Dec 2018 20:58:36 -0800 (PST) From: Jeremy Fertic To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Hartmut Knaack , Peter Meerwald-Stadler , Greg Kroah-Hartman , linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Jeremy Fertic Subject: [PATCH v2 4/4] staging: iio: adt7316: fix the dac write calculation Date: Sat, 22 Dec 2018 21:57:43 -0700 Message-Id: <20181223045743.10716-5-jeremyfertic@gmail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181223045743.10716-1-jeremyfertic@gmail.com> References: <20181223045743.10716-1-jeremyfertic@gmail.com> MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The lsb calculation is not masking the correct bits from the user input. Subtract 1 from (1 << offset) to correctly set up the mask to be applied to user input. The lsb register stores its value starting at the bit 7 position. adt7316_store_DAC() currently assumes the value is at the other end of the register. Shift the lsb value before storing it in a new variable lsb_reg, and write this variable to the lsb register. Fixes: 35f6b6b86ede ("staging: iio: new ADT7316/7/8 and ADT7516/7/9 driver") Signed-off-by: Jeremy Fertic --- drivers/staging/iio/addac/adt7316.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/staging/iio/addac/adt7316.c b/drivers/staging/iio/addac/adt7316.c index d7f3d68e525b..6f7891b567b9 100644 --- a/drivers/staging/iio/addac/adt7316.c +++ b/drivers/staging/iio/addac/adt7316.c @@ -1431,7 +1431,7 @@ static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip, static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip, int channel, const char *buf, size_t len) { - u8 msb, lsb, offset; + u8 msb, lsb, lsb_reg, offset; u16 data; int ret; @@ -1449,9 +1449,13 @@ static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip, return -EINVAL; if (chip->dac_bits > 8) { - lsb = data & (1 << offset); + lsb = data & ((1 << offset) - 1); + if (chip->dac_bits == 12) + lsb_reg = lsb << ADT7316_DA_12_BIT_LSB_SHIFT; + else + lsb_reg = lsb << ADT7316_DA_10_BIT_LSB_SHIFT; ret = chip->bus.write(chip->bus.client, - ADT7316_DA_DATA_BASE + channel * 2, lsb); + ADT7316_DA_DATA_BASE + channel * 2, lsb_reg); if (ret) return -EIO; }