From patchwork Fri Apr 17 14:51:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ivan T. Ivanov" X-Patchwork-Id: 6234401 X-Patchwork-Delegate: agross@codeaurora.org Return-Path: X-Original-To: patchwork-linux-arm-msm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D145BBF4A6 for ; Fri, 17 Apr 2015 14:51:26 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 09D222038D for ; Fri, 17 Apr 2015 14:51:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 57F1820381 for ; Fri, 17 Apr 2015 14:51:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965047AbbDQOvS (ORCPT ); Fri, 17 Apr 2015 10:51:18 -0400 Received: from mail-wi0-f171.google.com ([209.85.212.171]:33980 "EHLO mail-wi0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965424AbbDQOvP (ORCPT ); Fri, 17 Apr 2015 10:51:15 -0400 Received: by widjs5 with SMTP id js5so35669411wid.1 for ; Fri, 17 Apr 2015 07:51:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=dsV9aUWfRQjMDbjfUTB5o9df87X7FEId0EGXPjmu5wQ=; b=Aq3xAe+k+KRShNlI3Rg7XeiNEV3F8W7/InxbJnVZfyDJobekALoNZwChcRm3HSzd4A Go6BuLwTPDlq4Cl0atC5+9zv1a/L3D1BMdPlQho2DBT5fNeq3qRzckarOa4UxI36ZQVK eTlJ+ZjvFNEOS16g0Wt0RIFr72cb1dsFTLWU+2pBJpT+4djbq3hhC16RVdndLl8JeGhG jVbds4kMEjrMBl0X9jP4zInll2NQkfmnECIcIxCO/+fBUS+wutR+/PVuhPjbh6Ikqi+/ VH0rkqTdbxPhQeoSkpBnlegiH43U1AgTT0gm1eJRUW+BUfxfe1LQUWlUSMdpPLR+FhoM OV0Q== X-Gm-Message-State: ALoCoQkCc+Z7Pz7TivOijtuDyGqv6Exwo6gJ1yE7auLVNd5zNdoOouW8H0sJuIzrLmvqxf8Q3E2h X-Received: by 10.194.143.20 with SMTP id sa20mr6570453wjb.16.1429282274743; Fri, 17 Apr 2015 07:51:14 -0700 (PDT) Received: from localhost.localdomain ([37.157.136.206]) by mx.google.com with ESMTPSA id az2sm14982425wjb.4.2015.04.17.07.51.13 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 17 Apr 2015 07:51:14 -0700 (PDT) From: "Ivan T. Ivanov" To: Jonathan Cameron Cc: Stanimir Varbanov , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org Subject: [PATCH] iio: adc: spmi-vadc: Fix overflow in output value normalization Date: Fri, 17 Apr 2015 17:51:08 +0300 Message-Id: <1429282268-30501-1-git-send-email-ivan.ivanov@linaro.org> X-Mailer: git-send-email 1.9.1 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With 'dx' equal to 0.625V and 15 bit ADC, calculations overflow when difference against GND is ~20% of the ADC range. Fix this. Signed-off-by: Ivan T. Ivanov --- drivers/iio/adc/qcom-spmi-vadc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) -- 1.9.1 -- To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/iio/adc/qcom-spmi-vadc.c b/drivers/iio/adc/qcom-spmi-vadc.c index 3211729..0c4618b 100644 --- a/drivers/iio/adc/qcom-spmi-vadc.c +++ b/drivers/iio/adc/qcom-spmi-vadc.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -471,11 +472,11 @@ static s32 vadc_calibrate(struct vadc_priv *vadc, const struct vadc_channel_prop *prop, u16 adc_code) { const struct vadc_prescale_ratio *prescale; - s32 voltage; + s64 voltage; voltage = adc_code - vadc->graph[prop->calibration].gnd; voltage *= vadc->graph[prop->calibration].dx; - voltage = voltage / vadc->graph[prop->calibration].dy; + voltage = div64_s64(voltage, vadc->graph[prop->calibration].dy); if (prop->calibration == VADC_CALIB_ABSOLUTE) voltage += vadc->graph[prop->calibration].dx; @@ -487,7 +488,7 @@ static s32 vadc_calibrate(struct vadc_priv *vadc, voltage = voltage * prescale->den; - return voltage / prescale->num; + return div64_s64(voltage, prescale->num); } static int vadc_decimation_from_dt(u32 value)