Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1299318imu; Tue, 11 Dec 2018 16:59:24 -0800 (PST) X-Google-Smtp-Source: AFSGD/WodTl6H1gOge720h61y0yU3RkqPKhjSU4ZJsBe2S5xcWh0dUXrE+x7XhovWPkR27NyiOwC X-Received: by 2002:a63:4044:: with SMTP id n65mr16045316pga.90.1544576364101; Tue, 11 Dec 2018 16:59:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544576364; cv=none; d=google.com; s=arc-20160816; b=BWtiTZL7o1iXCbz3/lbgCKpDZxBbpgwPqjLUClm04o7lCpZT/O6NOzKIoqyO0CuYnK TBox5KOjVuw7PbB9muFnCGSWHhRBsgHLNMzeGByVhU2CONDFv2UPATRCRhyZdIr8kdX7 Ra+xZKLB3JVp234cvZKZw2a+9eIzI5YfcYou/OhxSPxkFJD5JrcIcmaEnp9HhvBVHnJ4 iqf1xqTU6dGaCnjEcWTp8ZoNQiEzKO9LhrpwtJs3XPM9VdqSNxDk+ZcS4k+p5dKhd1k9 TfxcJHgjCjhTV7gZ1SA6w9Ew6Ra/6lu04ehP4PJZ1zBG4GUq09NJD8exYLETJ8AZn63B IEvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8x3EfAi+dwJT7LG9r3AqAVh9KBm9FxEhlGxf1nFjco4=; b=WKulGjEoQSCT4+EWCfqw6wm5LubcWh9+HP1i3r4OKxuZrZtBsbGZ+AMcuwZaLVFmOF goiopHGmzEVMxu2EhWiqC+PMQ5fsdZoINrgA6R/z792Gpwad+cwrm5oMJgPUtEGYKQNw dX3ToYHe6URIqvasVndPC3aihA71Vglt9PSVmczpEOniNJXQOqlnGGBcB0k2nnpo0L9Z taRAEc+6Qpk2VOKiAdMUAcZxko+qIv42hzRl0HsVNeE4cZnpszCV8W2in8muNP6MKWjV TbWBD6X3FPMZcG7rzV1eMFgWgnthrZS2LxzyWm4qP6xWi4dYZ72sI4Rtesg4FQRY2VW7 7gxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ONp0a3Ws; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h91si13567500pld.411.2018.12.11.16.59.08; Tue, 11 Dec 2018 16:59:24 -0800 (PST) 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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ONp0a3Ws; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726468AbeLLA40 (ORCPT + 99 others); Tue, 11 Dec 2018 19:56:26 -0500 Received: from mail-it1-f194.google.com ([209.85.166.194]:52910 "EHLO mail-it1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726238AbeLLAzu (ORCPT ); Tue, 11 Dec 2018 19:55:50 -0500 Received: by mail-it1-f194.google.com with SMTP id g76so6587968itg.2; Tue, 11 Dec 2018 16:55:49 -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=8x3EfAi+dwJT7LG9r3AqAVh9KBm9FxEhlGxf1nFjco4=; b=ONp0a3WsAdCvn6qEPX78E5QC9Mz6d6oRqo1iXX2JUGBV985+YrC2aEi0zru+q7VFfK 6csxPZpiGZhsFx4/ttWg8jxX8prv2ZmZtOYSulIvoDhSAHs2OwBK4J87Jho8O1K43AAX 60hYcWdRVgTjsEmP2KwN/Qz6QCfK2LaYMDZyyFjmmkjupBeKC0RyyzNCkUPXOw+Pk2PV /HsXdOdgZgnijJgsVkq+aFnnoGtwmEUdaYnJu9VoUw51j5JhX05u0bKdBJW8V5pGLgDH A+CQiHENTWdqQ1QUtkoOHWQw+FEdEj3oOXkMZo1yD8gcVMTgWcBB2XLFJc/V6zVgaiYg /Q0Q== 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=8x3EfAi+dwJT7LG9r3AqAVh9KBm9FxEhlGxf1nFjco4=; b=d6paOC2xvJ+iXWrVObNlnHWeYIVqADW6oEZEJu6vGjktQt9zXuLQkqDMM52ZGzflyM O+4RQuf00TXmKxVr/bv2ftqxsKDwnnGr2oxlvrDH1ZpNeuj3sgu7jbTTeLtXe13yl+vC nGPQi1Uvku+ORoGXtJBqzdqWu6gxjhBvbTPoN2cf+M/fogewPKswI3dc2SeYkY3A5Kou 4WTkjJhfCR6ykTy7HXy4xjHti4zxwXxq9tp8vlGXypqCtHgMPQGbuihGzAieVf0zcNLO 9bUWV7P8MW80rpn/6Hhmv3f8kqQNpJEoJ7GsGvbs7/as5JQnetP2hx3Reh764oXCfjzL eDjQ== X-Gm-Message-State: AA+aEWaoDInGH14gWcC2S0I5XgzfjXm0E5Fz4iUmVoape9A0julPzxHK 3Ix9gc5KlsC+l2YdQB0cj5w= X-Received: by 2002:a24:1c04:: with SMTP id c4mr4403205itc.79.1544576149095; Tue, 11 Dec 2018 16:55:49 -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 l186sm1952445itl.22.2018.12.11.16.55.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Dec 2018 16:55:48 -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 06/11] staging: iio: adt7316: fix the dac write calculation Date: Tue, 11 Dec 2018 17:54:58 -0700 Message-Id: <20181212005503.28054-7-jeremyfertic@gmail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181212005503.28054-1-jeremyfertic@gmail.com> References: <20181212005503.28054-1-jeremyfertic@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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 b7d12d003ddc..77ef3c209b67 100644 --- a/drivers/staging/iio/addac/adt7316.c +++ b/drivers/staging/iio/addac/adt7316.c @@ -1442,7 +1442,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; @@ -1460,9 +1460,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; } -- 2.19.1