Skip to content

Commit

Permalink
Merge remote-tracking branch 'regulator/topic/pwm' into regulator-dri…
Browse files Browse the repository at this point in the history
…vers

Conflicts:
	drivers/regulator/Kconfig
  • Loading branch information
broonie committed Sep 28, 2014
2 parents 6a64250 + fbf7974 commit d1c3f7c
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 197 deletions.
27 changes: 27 additions & 0 deletions Documentation/devicetree/bindings/regulator/pwm-regulator.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pwm regulator bindings

Required properties:
- compatible: Should be "pwm-regulator"
- pwms: OF device-tree PWM specification (see PWM binding pwm.txt)
- voltage-table: voltage and duty table, include 2 members in each set of
brackets, first one is voltage(unit: uv), the next is duty(unit: percent)

Any property defined as part of the core regulator binding defined in
regulator.txt can also be used.

Example:
pwm_regulator {
compatible = "pwm-regulator;
pwms = <&pwm1 0 8448 0>;

voltage-table = <1114000 0>,
<1095000 10>,
<1076000 20>,
<1056000 30>,
<1036000 40>,
<1016000 50>;

regulator-min-microvolt = <1016000>;
regulator-max-microvolt = <1114000>;
regulator-name = "vdd_logic";
};
13 changes: 7 additions & 6 deletions drivers/regulator/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@ config REGULATOR_PFUZE100
Say y here to support the regulators found on the Freescale
PFUZE100/PFUZE200 PMIC.

config REGULATOR_PWM
tristate "PWM voltage regulator"
depends on PWM
help
This driver supports PWM controlled voltage regulators. PWM
duty cycle can increase or decrease the voltage.

config REGULATOR_QCOM_RPM
tristate "Qualcomm RPM regulator driver"
depends on MFD_QCOM_RPM
Expand Down Expand Up @@ -495,12 +502,6 @@ config REGULATOR_S5M8767
via I2C bus. S5M8767A have 9 Bucks and 28 LDOs output and
supports DVS mode with 8bits of output voltage control.

config REGULATOR_ST_PWM
tristate "STMicroelectronics PWM voltage regulator"
depends on ARCH_STI
help
This driver supports ST's PWM controlled voltage regulators.

config REGULATOR_TI_ABB
tristate "TI Adaptive Body Bias on-chip LDO"
depends on ARCH_OMAP
Expand Down
2 changes: 1 addition & 1 deletion drivers/regulator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ obj-$(CONFIG_REGULATOR_MC13XXX_CORE) += mc13xxx-regulator-core.o
obj-$(CONFIG_REGULATOR_QCOM_RPM) += qcom_rpm-regulator.o
obj-$(CONFIG_REGULATOR_PALMAS) += palmas-regulator.o
obj-$(CONFIG_REGULATOR_PFUZE100) += pfuze100-regulator.o
obj-$(CONFIG_REGULATOR_PWM) += pwm-regulator.o
obj-$(CONFIG_REGULATOR_TPS51632) += tps51632-regulator.o
obj-$(CONFIG_REGULATOR_PBIAS) += pbias-regulator.o
obj-$(CONFIG_REGULATOR_PCAP) += pcap-regulator.o
Expand All @@ -66,7 +67,6 @@ obj-$(CONFIG_REGULATOR_RC5T583) += rc5t583-regulator.o
obj-$(CONFIG_REGULATOR_S2MPA01) += s2mpa01.o
obj-$(CONFIG_REGULATOR_S2MPS11) += s2mps11.o
obj-$(CONFIG_REGULATOR_S5M8767) += s5m8767.o
obj-$(CONFIG_REGULATOR_ST_PWM) += st-pwm.o
obj-$(CONFIG_REGULATOR_STW481X_VMMC) += stw481x-vmmc.o
obj-$(CONFIG_REGULATOR_TI_ABB) += ti-abb-regulator.o
obj-$(CONFIG_REGULATOR_TPS6105X) += tps6105x-regulator.o
Expand Down
197 changes: 197 additions & 0 deletions drivers/regulator/pwm-regulator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Regulator driver for PWM Regulators
*
* Copyright (C) 2014 - STMicroelectronics Inc.
*
* Author: Lee Jones <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/of_regulator.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/pwm.h>

struct pwm_regulator_data {
struct regulator_desc desc;
struct pwm_voltages *duty_cycle_table;
struct pwm_device *pwm;
bool enabled;
int state;
};

struct pwm_voltages {
unsigned int uV;
unsigned int dutycycle;
};

static int pwm_regulator_get_voltage_sel(struct regulator_dev *dev)
{
struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);

return drvdata->state;
}

static int pwm_regulator_set_voltage_sel(struct regulator_dev *dev,
unsigned selector)
{
struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
unsigned int pwm_reg_period;
int dutycycle;
int ret;

pwm_reg_period = pwm_get_period(drvdata->pwm);

dutycycle = (pwm_reg_period *
drvdata->duty_cycle_table[selector].dutycycle) / 100;

ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
if (ret) {
dev_err(&dev->dev, "Failed to configure PWM\n");
return ret;
}

drvdata->state = selector;

if (!drvdata->enabled) {
ret = pwm_enable(drvdata->pwm);
if (ret) {
dev_err(&dev->dev, "Failed to enable PWM\n");
return ret;
}
drvdata->enabled = true;
}

return 0;
}

static int pwm_regulator_list_voltage(struct regulator_dev *dev,
unsigned selector)
{
struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);

if (selector >= drvdata->desc.n_voltages)
return -EINVAL;

return drvdata->duty_cycle_table[selector].uV;
}

static struct regulator_ops pwm_regulator_voltage_ops = {
.set_voltage_sel = pwm_regulator_set_voltage_sel,
.get_voltage_sel = pwm_regulator_get_voltage_sel,
.list_voltage = pwm_regulator_list_voltage,
.map_voltage = regulator_map_voltage_iterate,
};

static const struct regulator_desc pwm_regulator_desc = {
.name = "pwm-regulator",
.ops = &pwm_regulator_voltage_ops,
.type = REGULATOR_VOLTAGE,
.owner = THIS_MODULE,
.supply_name = "pwm",
};

static int pwm_regulator_probe(struct platform_device *pdev)
{
struct pwm_regulator_data *drvdata;
struct property *prop;
struct regulator_dev *regulator;
struct regulator_config config = { };
struct device_node *np = pdev->dev.of_node;
int length, ret;

if (!np) {
dev_err(&pdev->dev, "Device Tree node missing\n");
return -EINVAL;
}

drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
if (!drvdata)
return -ENOMEM;

memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(pwm_regulator_desc));

/* determine the number of voltage-table */
prop = of_find_property(np, "voltage-table", &length);
if (!prop) {
dev_err(&pdev->dev, "No voltage-table\n");
return -EINVAL;
}

if ((length < sizeof(*drvdata->duty_cycle_table)) ||
(length % sizeof(*drvdata->duty_cycle_table))) {
dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
length);
return -EINVAL;
}

drvdata->desc.n_voltages = length / sizeof(*drvdata->duty_cycle_table);

drvdata->duty_cycle_table = devm_kzalloc(&pdev->dev,
length, GFP_KERNEL);
if (!drvdata->duty_cycle_table)
return -ENOMEM;

/* read voltage table from DT property */
ret = of_property_read_u32_array(np, "voltage-table",
(u32 *)drvdata->duty_cycle_table,
length / sizeof(u32));
if (ret < 0) {
dev_err(&pdev->dev, "read voltage-table failed\n");
return ret;
}

config.init_data = of_get_regulator_init_data(&pdev->dev, np);
if (!config.init_data)
return -ENOMEM;

config.of_node = np;
config.dev = &pdev->dev;
config.driver_data = drvdata;

drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
if (IS_ERR(drvdata->pwm)) {
dev_err(&pdev->dev, "Failed to get PWM\n");
return PTR_ERR(drvdata->pwm);
}

regulator = devm_regulator_register(&pdev->dev,
&drvdata->desc, &config);
if (IS_ERR(regulator)) {
dev_err(&pdev->dev, "Failed to register regulator %s\n",
drvdata->desc.name);
return PTR_ERR(regulator);
}

return 0;
}

static const struct of_device_id pwm_of_match[] = {
{ .compatible = "pwm-regulator" },
{ },
};
MODULE_DEVICE_TABLE(of, pwm_of_match);

static struct platform_driver pwm_regulator_driver = {
.driver = {
.name = "pwm-regulator",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(pwm_of_match),
},
.probe = pwm_regulator_probe,
};

module_platform_driver(pwm_regulator_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lee Jones <[email protected]>");
MODULE_DESCRIPTION("PWM Regulator Driver");
MODULE_ALIAS("platform:pwm-regulator");
Loading

0 comments on commit d1c3f7c

Please sign in to comment.