From patchwork Tue Jul 7 15:06:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 50829 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f200.google.com (mail-wi0-f200.google.com [209.85.212.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 410A9229FC for ; Tue, 7 Jul 2015 15:08:39 +0000 (UTC) Received: by wipp2 with SMTP id p2sf43524860wip.2 for ; Tue, 07 Jul 2015 08:08:38 -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=XsbqCIOyXeVfbSaDfw+V7TwikaPYjj3CyNuRgp8xHTI=; b=ABDhujYH3YNktZpqGIAHs9HTtp2miQmY4fC8XWI6+Zt8xTyC/7sPJhplWPmgwxtPSx T7qICgZHLHGAIqeCWfn2bBfGoh4PHdxyC24EPMSLPUaNhl4JIquvOhj4zljq0kE8VHkU ZvJVQ8pq6TRatqWVT38htbxAcoxv86E7FlPTieYFHn8C4PcrlYFtUDmpAcLqSTwnIHGL JZPoOCrXMjOqivkzOU6QNWNiVivE2ZC+MhKCgInGArczEWriuEQpeiLQ1hsoyTlFi4El yPb/0iU5Ej7Lx/GVxfXwkYcaLctORIcVARpprVleTqYlFWYwZvN1KVss/sJt0sUIt4cq dI+g== X-Gm-Message-State: ALoCoQkEDKKIPGe50nPBOAJzEvHUO4y9thItPDYT0FVmuzS1ZhaUDVxsPMsgQnWAkbc/ZSbXoNfS X-Received: by 10.152.26.226 with SMTP id o2mr2367564lag.0.1436281718430; Tue, 07 Jul 2015 08:08:38 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.37.69 with SMTP id w5ls838920laj.107.gmail; Tue, 07 Jul 2015 08:08:38 -0700 (PDT) X-Received: by 10.112.146.97 with SMTP id tb1mr4630262lbb.12.1436281718258; Tue, 07 Jul 2015 08:08:38 -0700 (PDT) Received: from mail-la0-f52.google.com (mail-la0-f52.google.com. [209.85.215.52]) by mx.google.com with ESMTPS id j1si18375831lbc.15.2015.07.07.08.08.38 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Jul 2015 08:08:38 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) client-ip=209.85.215.52; Received: by lagc2 with SMTP id c2so197208046lag.3 for ; Tue, 07 Jul 2015 08:08:38 -0700 (PDT) X-Received: by 10.112.198.74 with SMTP id ja10mr4470528lbc.19.1436281716943; Tue, 07 Jul 2015 08:08:36 -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 hn6csp2453195lbb; Tue, 7 Jul 2015 08:08:35 -0700 (PDT) X-Received: by 10.66.165.67 with SMTP id yw3mr9853000pab.95.1436281714248; Tue, 07 Jul 2015 08:08:34 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si35164918pdh.95.2015.07.07.08.08.31; Tue, 07 Jul 2015 08:08:34 -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 S932516AbbGGPI3 (ORCPT + 29 others); Tue, 7 Jul 2015 11:08:29 -0400 Received: from mail-wi0-f177.google.com ([209.85.212.177]:34068 "EHLO mail-wi0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932421AbbGGPHK (ORCPT ); Tue, 7 Jul 2015 11:07:10 -0400 Received: by wifm2 with SMTP id m2so62571286wif.1 for ; Tue, 07 Jul 2015 08:07:09 -0700 (PDT) X-Received: by 10.194.142.209 with SMTP id ry17mr9953802wjb.5.1436281628891; Tue, 07 Jul 2015 08:07:08 -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 x5sm1360101wif.21.2015.07.07.08.07.07 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 07 Jul 2015 08:07:08 -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: [PATCH v2 7/9] regulator: pwm-regulator: Add support for continuous-voltage Date: Tue, 7 Jul 2015 16:06:51 +0100 Message-Id: <1436281613-899-8-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1436281613-899-1-git-send-email-lee.jones@linaro.org> References: <1436281613-899-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.52 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: , The current version of PWM regulator only supports a static table approach, where pre-calculated values are supplied by the vendor and obtained via DT. The continuous-voltage method takes min_uV and max_uV, and divides the difference between them up into a number of slices. The number of slices depend on how large the duty cycle register is. This information is provided by a DT property. As the name alludes, this provides values for a continuous voltage range between min_uV and max_uV, which has obvious benefits over either limited voltage possibilities, or the requirement to provide a large voltage-table. Signed-off-by: Lee Jones --- drivers/regulator/pwm-regulator.c | 114 +++++++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 8 deletions(-) diff --git a/drivers/regulator/pwm-regulator.c b/drivers/regulator/pwm-regulator.c index 25560fc..dac145d 100644 --- a/drivers/regulator/pwm-regulator.c +++ b/drivers/regulator/pwm-regulator.c @@ -10,6 +10,7 @@ * published by the Free Software Foundation. */ +#include #include #include #include @@ -21,9 +22,16 @@ #include struct pwm_regulator_data { - struct pwm_voltages *duty_cycle_table; + /* Shared */ struct pwm_device *pwm; + + /* Voltage table */ + struct pwm_voltages *duty_cycle_table; int state; + + /* Continuous voltage */ + u32 max_duty_cycle; + int volt_uV; }; struct pwm_voltages { @@ -31,6 +39,9 @@ struct pwm_voltages { unsigned int dutycycle; }; +/** + * Voltage table call-backs + */ static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev) { struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); @@ -78,6 +89,71 @@ static int pwm_regulator_list_voltage(struct regulator_dev *rdev, return drvdata->duty_cycle_table[selector].uV; } + +/** + * Continuous voltage call-backs + */ +static int pwm_voltage_to_duty_cycle(struct regulator_dev *rdev, + int volt_mV) +{ + 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; +} + +static int pwm_regulator_get_voltage(struct regulator_dev *rdev) +{ + struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); + + return drvdata->volt_uV; +} + +static int pwm_regulator_set_voltage(struct regulator_dev *rdev, + int min_uV, int max_uV, + unsigned *selector) +{ + struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); + unsigned int ramp_delay = rdev->constraints->ramp_delay; + int duty_cycle; + int ret; + + duty_cycle = pwm_voltage_to_duty_cycle(rdev, min_uV / 1000); + + ret = pwm_config(drvdata->pwm, + (drvdata->pwm->period / 100) * duty_cycle, + drvdata->pwm->period); + if (ret) { + dev_err(&rdev->dev, "Failed to configure PWM\n"); + return ret; + } + + ret = pwm_enable(drvdata->pwm); + if (ret) { + dev_err(&rdev->dev, "Failed to enable PWM\n"); + return ret; + } + drvdata->volt_uV = min_uV; + + /* Delay required by PWM regulator to settle to the new voltage */ + usleep_range(ramp_delay, ramp_delay + 1000); + + return 0; +} + static struct regulator_ops pwm_regulator_voltage_table_ops = { .set_voltage_sel = pwm_regulator_set_voltage_sel, .get_voltage_sel = pwm_regulator_get_voltage_sel, @@ -85,6 +161,11 @@ static struct regulator_ops pwm_regulator_voltage_table_ops = { .map_voltage = regulator_map_voltage_iterate, }; +static struct regulator_ops pwm_regulator_voltage_continuous_ops = { + .get_voltage = pwm_regulator_get_voltage, + .set_voltage = pwm_regulator_set_voltage, +}; + static struct regulator_desc pwm_regulator_desc = { .name = "pwm-regulator", .type = REGULATOR_VOLTAGE, @@ -129,6 +210,25 @@ static int pwm_regulator_init_table(struct platform_device *pdev, return 0; } +static int pwm_regulator_init_continuous(struct platform_device *pdev, + struct pwm_regulator_data *drvdata) +{ + struct device_node *np = pdev->dev.of_node; + int ret; + + ret = of_property_read_u32(np, "max-duty-cycle", + &drvdata->max_duty_cycle); + if (ret) { + dev_err(&pdev->dev, "Failed to read \"pwm-max-value\"\n"); + return ret; + } + + pwm_regulator_desc.ops = &pwm_regulator_voltage_continuous_ops; + pwm_regulator_desc.continuous_voltage_range = true; + + return 0; +} + static int pwm_regulator_probe(struct platform_device *pdev) { struct pwm_regulator_data *drvdata; @@ -146,14 +246,12 @@ static int pwm_regulator_probe(struct platform_device *pdev) if (!drvdata) return -ENOMEM; - if (of_find_property(np, "voltage-table", NULL)) { + if (of_find_property(np, "voltage-table", NULL)) ret = pwm_regulator_init_table(pdev, drvdata); - if (ret) - return ret; - } else { - dev_err(&pdev->dev, "No \"voltage-table\" supplied\n"); - return -EINVAL; - } + else + ret = pwm_regulator_init_continuous(pdev, drvdata); + if (ret) + return ret; config.init_data = of_get_regulator_init_data(&pdev->dev, np, &pwm_regulator_desc);