pwm-regulator.c 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Regulator driver for PWM Regulators
 *
 * Copyright (C) 2014 - STMicroelectronics Inc.
 *
 * Author: Lee Jones <lee.jones@linaro.org>
 *
 * 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.
 */

13
#include <linux/delay.h>
14 15 16 17 18 19 20 21 22 23 24
#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 {
25
	/*  Shared */
26
	struct pwm_device *pwm;
27 28 29

	/* Voltage table */
	struct pwm_voltages *duty_cycle_table;
30
	int state;
31 32 33 34

	/* Continuous voltage */
	u32 max_duty_cycle;
	int volt_uV;
35 36 37 38 39 40 41
};

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

42 43 44
/**
 * Voltage table call-backs
 */
45
static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
46
{
47
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
48 49 50 51

	return drvdata->state;
}

52
static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
53 54
					 unsigned selector)
{
55
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
56 57 58 59 60 61 62 63 64 65 66
	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) {
67
		dev_err(&rdev->dev, "Failed to configure PWM\n");
68 69 70 71 72
		return ret;
	}

	drvdata->state = selector;

73 74
	ret = pwm_enable(drvdata->pwm);
	if (ret) {
75
		dev_err(&rdev->dev, "Failed to enable PWM\n");
76
		return ret;
77 78 79 80 81
	}

	return 0;
}

82
static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
83 84
				      unsigned selector)
{
85
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
86

87
	if (selector >= rdev->desc->n_voltages)
88 89 90 91
		return -EINVAL;

	return drvdata->duty_cycle_table[selector].uV;
}
92 93 94 95

/**
 * Continuous voltage call-backs
 */
96
static int pwm_voltage_to_duty_cycle(struct regulator_dev *rdev, int req_uV)
97
{
98 99 100 101 102
	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));
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
}

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;

121
	duty_cycle = pwm_voltage_to_duty_cycle(rdev, min_uV);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	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;
}

144
static struct regulator_ops pwm_regulator_voltage_table_ops = {
145 146 147 148 149 150
	.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,
};

151 152 153 154 155
static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
	.get_voltage = pwm_regulator_get_voltage,
	.set_voltage = pwm_regulator_set_voltage,
};

156
static struct regulator_desc pwm_regulator_desc = {
157 158 159 160 161 162
	.name		= "pwm-regulator",
	.type		= REGULATOR_VOLTAGE,
	.owner		= THIS_MODULE,
	.supply_name    = "pwm",
};

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static int pwm_regulator_init_table(struct platform_device *pdev,
				    struct pwm_regulator_data *drvdata)
{
	struct device_node *np = pdev->dev.of_node;
	struct pwm_voltages *duty_cycle_table;
	int length;
	int ret;

	of_find_property(np, "voltage-table", &length);

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

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

	ret = of_property_read_u32_array(np, "voltage-table",
					 (u32 *)duty_cycle_table,
					 length / sizeof(u32));
	if (ret) {
		dev_err(&pdev->dev, "Failed to read voltage-table\n");
		return ret;
	}

	drvdata->duty_cycle_table	= duty_cycle_table;
	pwm_regulator_desc.ops		= &pwm_regulator_voltage_table_ops;
	pwm_regulator_desc.n_voltages	= length / sizeof(*duty_cycle_table);

	return 0;
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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;
}

219 220 221 222 223 224
static int pwm_regulator_probe(struct platform_device *pdev)
{
	struct pwm_regulator_data *drvdata;
	struct regulator_dev *regulator;
	struct regulator_config config = { };
	struct device_node *np = pdev->dev.of_node;
225
	int ret;
226 227 228 229 230 231 232 233 234 235

	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;

236
	if (of_find_property(np, "voltage-table", NULL))
237
		ret = pwm_regulator_init_table(pdev, drvdata);
238 239 240 241
	else
		ret = pwm_regulator_init_continuous(pdev, drvdata);
	if (ret)
		return ret;
242

243
	config.init_data = of_get_regulator_init_data(&pdev->dev, np,
244
						      &pwm_regulator_desc);
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	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,
259
					    &pwm_regulator_desc, &config);
260 261
	if (IS_ERR(regulator)) {
		dev_err(&pdev->dev, "Failed to register regulator %s\n",
262
			pwm_regulator_desc.name);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		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",
		.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 <lee.jones@linaro.org>");
MODULE_DESCRIPTION("PWM Regulator Driver");
MODULE_ALIAS("platform:pwm-regulator");