From patchwork Mon Jul 6 08:58:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 50696 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id BE884229FC for ; Mon, 6 Jul 2015 08:59:08 +0000 (UTC) Received: by wgbbj7 with SMTP id bj7sf47814624wgb.2 for ; Mon, 06 Jul 2015 01:59:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=qSUroISx3cCJiL80SgirvNlDfkCjkiYtcfYxYluFWi8=; b=auh6+UMwArWLvO/jhJZZJCEiUuiAEz9Yk1s90l+OaXtkqZXtaHO+C3zMm3On1XhvUC e3l+0twN+h8YlwZPmqcF1dbuWxxP5kCB5kmyu3H5GtayOOPK5KXMk1TiYsarQbFPqmVr 9WvbaUe1HQWyl53LCgyxtTFSlC1thTThhj33ZAj/ooL8hCrYQXw1g9Uwt+GdkE9v/QLc vxWMwGHG1WKQLWnT5J71d1NlXRw6lLCpYvQ5sapA6hJIZZhF6jGqWCHAoio9BATlBQ+4 w5QeWSHJ8bURWytSH4zKA5f3g0UJpr/QXNS8eBxyMo0gKEFK9MvWVZVszYK2I2tM21oG n4tQ== X-Gm-Message-State: ALoCoQkzK6K31KNE5UT8ox6XaxX1XAfCq26/zXQRAXx078kctqFrAmfAt9BIIunkr7hTgo74h2ly X-Received: by 10.112.122.12 with SMTP id lo12mr30040138lbb.5.1436173147964; Mon, 06 Jul 2015 01:59:07 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.22.97 with SMTP id c1ls619728laf.66.gmail; Mon, 06 Jul 2015 01:59:07 -0700 (PDT) X-Received: by 10.152.19.67 with SMTP id c3mr4458920lae.4.1436173147618; Mon, 06 Jul 2015 01:59:07 -0700 (PDT) Received: from mail-la0-f41.google.com (mail-la0-f41.google.com. [209.85.215.41]) by mx.google.com with ESMTPS id a4si14727915lae.4.2015.07.06.01.59.07 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jul 2015 01:59:07 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.41 as permitted sender) client-ip=209.85.215.41; Received: by lagx9 with SMTP id x9so146164489lag.1 for ; Mon, 06 Jul 2015 01:59:07 -0700 (PDT) X-Received: by 10.153.5.38 with SMTP id cj6mr47812707lad.36.1436173147502; Mon, 06 Jul 2015 01:59:07 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp1581755lbb; Mon, 6 Jul 2015 01:59:06 -0700 (PDT) X-Received: by 10.70.49.229 with SMTP id x5mr101806295pdn.81.1436173142140; Mon, 06 Jul 2015 01:59:02 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qa10si27921044pbc.83.2015.07.06.01.59.01; Mon, 06 Jul 2015 01:59:02 -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; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755048AbbGFI64 (ORCPT + 29 others); Mon, 6 Jul 2015 04:58:56 -0400 Received: from mail-wg0-f48.google.com ([74.125.82.48]:36375 "EHLO mail-wg0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754680AbbGFI6v (ORCPT ); Mon, 6 Jul 2015 04:58:51 -0400 Received: by wguu7 with SMTP id u7so134151146wgu.3 for ; Mon, 06 Jul 2015 01:58:50 -0700 (PDT) X-Received: by 10.180.90.228 with SMTP id bz4mr52178813wib.69.1436173129947; Mon, 06 Jul 2015 01:58:49 -0700 (PDT) Received: from localhost.localdomain (host86-178-207-130.range86-178.btcentralplus.com. [86.178.207.130]) by mx.google.com with ESMTPSA id d7sm18085910wij.0.2015.07.06.01.58.48 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 06 Jul 2015 01:58:49 -0700 (PDT) From: Lee Jones To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: kernel@stlinux.com, broonie@kernel.org, lgirdwood@gmail.com, Lee Jones Subject: [RESEND 09/10] regulator: pwm-regulator: Simplify voltage to duty-cycle call Date: Mon, 6 Jul 2015 09:58:31 +0100 Message-Id: <1436173112-21397-10-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1436173112-21397-1-git-send-email-lee.jones@linaro.org> References: <1436173112-21397-1-git-send-email-lee.jones@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: lee.jones@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.41 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , If we reverse some of the logic and change the formula used, we can simplify the function greatly. Signed-off-by: Lee Jones --- drivers/regulator/pwm-regulator.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/drivers/regulator/pwm-regulator.c b/drivers/regulator/pwm-regulator.c index b37b616..d5cb267 100644 --- a/drivers/regulator/pwm-regulator.c +++ b/drivers/regulator/pwm-regulator.c @@ -93,26 +93,13 @@ static int pwm_regulator_list_voltage(struct regulator_dev *rdev, /** * Continuous voltage call-backs */ -static int pwm_voltage_to_duty_cycle(struct regulator_dev *rdev, - int volt_mV) +static int pwm_voltage_to_duty_cycle(struct regulator_dev *rdev, int req_uV) { - struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); - int min_mV = rdev->constraints->min_uV / 1000; - int max_mV = rdev->constraints->max_uV / 1000; - int max_duty_cycle = drvdata->max_duty_cycle; - int vdiff = min_mV - max_mV; - int pwm_code; - int tmp; - - tmp = max_duty_cycle - min_mV * max_duty_cycle / vdiff; - pwm_code = tmp + max_duty_cycle * volt_mV / vdiff; - - if (pwm_code < 0) - pwm_code = 0; - if (pwm_code > max_duty_cycle) - pwm_code = max_duty_cycle; - - return pwm_code * 100 / max_duty_cycle; + int min_uV = rdev->constraints->min_uV; + int max_uV = rdev->constraints->max_uV; + int diff = max_uV - min_uV; + + return 100 - ((((req_uV * 100) - (min_uV * 100)) / diff)); } static int pwm_regulator_get_voltage(struct regulator_dev *rdev) @@ -131,7 +118,7 @@ static int pwm_regulator_set_voltage(struct regulator_dev *rdev, int duty_cycle; int ret; - duty_cycle = pwm_voltage_to_duty_cycle(rdev, min_uV / 1000); + duty_cycle = pwm_voltage_to_duty_cycle(rdev, min_uV); ret = pwm_config(drvdata->pwm, (drvdata->pwm->period / 100) * duty_cycle,