Received: by 10.223.164.221 with SMTP id h29csp1674576wrb; Wed, 1 Nov 2017 22:07:19 -0700 (PDT) X-Google-Smtp-Source: ABhQp+TgxetS+YVlM985a81TGosxCeVaJiZo/9JmAZwR1v8J1lt4tZAE3+7kh6gYg688dWX5NW7m X-Received: by 10.84.238.136 with SMTP id v8mr1978788plk.37.1509599239818; Wed, 01 Nov 2017 22:07:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509599239; cv=none; d=google.com; s=arc-20160816; b=ylT200ltZdfks8viZwoPbXikJ0g/heud8ih6NV6Qmi8s8UKzmidyQKUK8CPEIa/JwW I1GFkwGdBV4oyNef+AZmRDxqo7RgcO/YlqiNSocahyJpqnvJ5qHaPqXFLdP3tc4KMGAN XtEF88Bi2HmFz8MAnAwK9z7V9MoS2PqYfgvjPeydvsN5MZtYbM3Lf9KcFDc52XHT0eCx cj/yvUIkU8N9ekz9UiOQHCNUzXjywcPchqG7Eo/l+9IzbDEcC1peLLh3UhJrxhUwixDR yv/p1bNQ5ZfCOGDJ8ceKlMbG+M7t5XGufIBxkME5BOrBWDEdHyVXpBSRO4sD7VDlDx20 DmcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dmarc-filter:dkim-signature:dkim-signature :arc-authentication-results; bh=vMt9cnXnxMTmbu86ryKhtXjiNftHpYI6a/v4gvMS+8c=; b=rUkY7ybVFqlBIY4sfGQ+rcR9iVf+0AfqhGoifR3FqHfUblry8bOYz22q6GVeGsFEAX sJCt/7VhWl0E6pR92gsZWdgrCzivN/ag0F57FprC1C88peyaVLc6Jh7Q5asAagkMwJp2 nxkGDu13e2ZrSVNOC/gLrn5jEF0gX0RpMoXdAJRR/kqVunoyKTodocjsUV2bJUPAzxm7 RZwGQqjHDUZs2sJcW/iJPO2eG9JRNhV/X/u5taM9CfKqK06gq7gSvD0a0Nu3Mk6dY5mW gyvqq8R/II8LRxAZT3yHy2NdzxBLy7ViNrUy71ECYlebIjF+YVVE8r1I1BxLhSvHTyjc cKyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=Y9LOpdM9; dkim=pass header.i=@codeaurora.org header.s=default header.b=Y9LOpdM9; 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 s59si608607plb.172.2017.11.01.22.07.04; Wed, 01 Nov 2017 22:07:19 -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; dkim=pass header.i=@codeaurora.org header.s=default header.b=Y9LOpdM9; dkim=pass header.i=@codeaurora.org header.s=default header.b=Y9LOpdM9; 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 S1751587AbdKBFGX (ORCPT + 99 others); Thu, 2 Nov 2017 01:06:23 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:59220 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750836AbdKBFGW (ORCPT ); Thu, 2 Nov 2017 01:06:22 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 8B67E60285; Thu, 2 Nov 2017 05:06:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1509599181; bh=gfp7p4A0PEA/vSdqnEBxWMTMDjVdMwONbPWccYhkDjc=; h=From:To:Cc:Subject:Date:From; b=Y9LOpdM9stQ8ZyxuZdPYNdKmrMJSXsvyroO9+Nf1WSN9RqFH5ZPZQ+NZNHsUFptR6 FSrSX3khrzoYtpVDbKdMHjPFQICP/3xgod4o4h8QL8TDvl3VfRhjAXq8FasTp1ig/Q idme1JeKvBSm66WLFhvqPMe83/qGSFY6NaKh46uw= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from sboyd-linux.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: sboyd@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id E61D260285; Thu, 2 Nov 2017 05:06:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1509599181; bh=gfp7p4A0PEA/vSdqnEBxWMTMDjVdMwONbPWccYhkDjc=; h=From:To:Cc:Subject:Date:From; b=Y9LOpdM9stQ8ZyxuZdPYNdKmrMJSXsvyroO9+Nf1WSN9RqFH5ZPZQ+NZNHsUFptR6 FSrSX3khrzoYtpVDbKdMHjPFQICP/3xgod4o4h8QL8TDvl3VfRhjAXq8FasTp1ig/Q idme1JeKvBSm66WLFhvqPMe83/qGSFY6NaKh46uw= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org E61D260285 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=sboyd@codeaurora.org From: Stephen Boyd To: Mark Brown Cc: linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org Subject: [PATCH] regulator: qcom_spmi: Include offset when translating voltages Date: Wed, 1 Nov 2017 22:06:19 -0700 Message-Id: <20171102050619.32204-1-sboyd@codeaurora.org> X-Mailer: git-send-email 2.15.0.rc0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This driver converts voltages from a non-linear range in hardware to a linear range in software and vice versa. During the conversion, we exclude certain voltages that are invalid to use because the software interface is more flexible than reality. For example, the FTSMPS2P5 regulators have a voltage range from 80000uV to 1355000uV that software could support, but we only want to use the range of 350000uV to 1355000uV. If we don't account for the hw selectors between 80000uV and 350000uV we'll pick a hw selector of 0 to mean 350000uV when it really means 80000uV. This can cause us to program voltages into the hardware that are significantly lower than what we're expecting. And when we read it back from the hardware we'll have the same problem, voltages that are in the invalid band will end up being calculated as some software selector that represents a larger voltage than what is programmed and the user will be confused. Fix all this by properly offsetting the software selector and hw selector when converting from one number space to another. Fixes: 1b5b19689278 ("regulator: qcom_spmi: Only use selector based regulator ops") Signed-off-by: Stephen Boyd --- drivers/regulator/qcom_spmi-regulator.c | 39 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/drivers/regulator/qcom_spmi-regulator.c b/drivers/regulator/qcom_spmi-regulator.c index 16c5f84e06a7..c372b244f3da 100644 --- a/drivers/regulator/qcom_spmi-regulator.c +++ b/drivers/regulator/qcom_spmi-regulator.c @@ -593,13 +593,20 @@ static int spmi_sw_selector_to_hw(struct spmi_regulator *vreg, u8 *voltage_sel) { const struct spmi_voltage_range *range, *end; + unsigned offset; range = vreg->set_points->range; end = range + vreg->set_points->count; for (; range < end; range++) { if (selector < range->n_voltages) { - *voltage_sel = selector; + /* + * hardware selectors between set point min and real + * min are invalid so we ignore them + */ + offset = range->set_point_min_uV - range->min_uV; + offset /= range->step_uV; + *voltage_sel = selector + offset; *range_sel = range->range_sel; return 0; } @@ -613,15 +620,35 @@ static int spmi_sw_selector_to_hw(struct spmi_regulator *vreg, static int spmi_hw_selector_to_sw(struct spmi_regulator *vreg, u8 hw_sel, const struct spmi_voltage_range *range) { - int sw_sel = hw_sel; + unsigned sw_sel = 0; + unsigned offset, max_hw_sel; const struct spmi_voltage_range *r = vreg->set_points->range; - - while (r != range) { + const struct spmi_voltage_range *end = r + vreg->set_points->count; + + for (; r < end; r++) { + if (r == range && range->n_voltages) { + /* + * hardware selectors between set point min and real + * min and between set point max and real max are + * invalid so we return an error if they're + * programmed into the hardware + */ + offset = range->set_point_min_uV - range->min_uV; + offset /= range->step_uV; + if (hw_sel < offset) + return -EINVAL; + + max_hw_sel = range->set_point_max_uV - range->min_uV; + max_hw_sel /= range->step_uV; + if (hw_sel > max_hw_sel) + return -EINVAL; + + return sw_sel + hw_sel - offset; + } sw_sel += r->n_voltages; - r++; } - return sw_sel; + return -EINVAL; } static const struct spmi_voltage_range * -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project From 1583308691077245386@xxx Mon Nov 06 09:33:53 +0000 2017 X-GM-THRID: 1582955390796182137 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread