pwm-regulator.c 9.0 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
#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>
23
#include <linux/gpio/consumer.h>
24 25

struct pwm_regulator_data {
26
	/*  Shared */
27
	struct pwm_device *pwm;
28 29 30

	/* Voltage table */
	struct pwm_voltages *duty_cycle_table;
31 32 33 34 35 36 37

	/* regulator descriptor */
	struct regulator_desc desc;

	/* Regulator ops */
	struct regulator_ops ops;

38
	int state;
39 40 41

	/* Continuous voltage */
	int volt_uV;
42 43 44

	/* Enable GPIO */
	struct gpio_desc *enb_gpio;
45 46 47 48 49 50 51
};

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

52 53 54
/**
 * Voltage table call-backs
 */
55
static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
56
{
57
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
58 59 60 61

	return drvdata->state;
}

62
static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
63 64
					 unsigned selector)
{
65
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
66
	struct pwm_args pargs;
67 68 69
	int dutycycle;
	int ret;

70
	pwm_get_args(drvdata->pwm, &pargs);
71

72
	dutycycle = (pargs.period *
73 74
		    drvdata->duty_cycle_table[selector].dutycycle) / 100;

75
	ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
76
	if (ret) {
77
		dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
78 79 80 81 82 83 84 85
		return ret;
	}

	drvdata->state = selector;

	return 0;
}

86
static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
87 88
				      unsigned selector)
{
89
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
90

91
	if (selector >= rdev->desc->n_voltages)
92 93 94 95
		return -EINVAL;

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

97 98 99 100
static int pwm_regulator_enable(struct regulator_dev *dev)
{
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);

101 102 103
	if (drvdata->enb_gpio)
		gpiod_set_value_cansleep(drvdata->enb_gpio, 1);

104 105 106 107 108 109 110 111 112
	return pwm_enable(drvdata->pwm);
}

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

	pwm_disable(drvdata->pwm);

113 114 115
	if (drvdata->enb_gpio)
		gpiod_set_value_cansleep(drvdata->enb_gpio, 0);

116 117 118 119 120 121 122
	return 0;
}

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

123 124 125
	if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
		return false;

126 127 128
	return pwm_is_enabled(drvdata->pwm);
}

129 130 131 132 133 134 135 136 137 138 139 140 141
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;
142
	struct pwm_args pargs;
143 144 145 146 147
	unsigned int req_diff = min_uV - rdev->constraints->min_uV;
	unsigned int diff;
	unsigned int duty_pulse;
	u64 req_period;
	u32 rem;
148
	int old_uV = pwm_regulator_get_voltage(rdev);
149 150
	int ret;

151
	pwm_get_args(drvdata->pwm, &pargs);
152 153 154 155 156 157 158 159 160
	diff = rdev->constraints->max_uV - rdev->constraints->min_uV;

	/* First try to find out if we get the iduty cycle time which is
	 * factor of PWM period time. If (request_diff_to_min * pwm_period)
	 * is perfect divided by voltage_range_diff then it is possible to
	 * get duty cycle time which is factor of PWM period. This will help
	 * to get output voltage nearer to requested value as there is no
	 * calculation loss.
	 */
161
	req_period = req_diff * pargs.period;
162 163 164 165 166
	div_u64_rem(req_period, diff, &rem);
	if (!rem) {
		do_div(req_period, diff);
		duty_pulse = (unsigned int)req_period;
	} else {
167
		duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff);
168
	}
169

170
	ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period);
171
	if (ret) {
172
		dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
173 174 175 176 177
		return ret;
	}

	drvdata->volt_uV = min_uV;

178 179 180 181 182 183
	if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
		return 0;

	/* Ramp delay is in uV/uS. Adjust to uS and delay */
	ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay);
	usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
184 185 186 187

	return 0;
}

188
static struct regulator_ops pwm_regulator_voltage_table_ops = {
189 190 191 192
	.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,
193 194 195
	.enable          = pwm_regulator_enable,
	.disable         = pwm_regulator_disable,
	.is_enabled      = pwm_regulator_is_enabled,
196 197
};

198 199 200
static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
	.get_voltage = pwm_regulator_get_voltage,
	.set_voltage = pwm_regulator_set_voltage,
201 202 203
	.enable          = pwm_regulator_enable,
	.disable         = pwm_regulator_disable,
	.is_enabled      = pwm_regulator_is_enabled,
204 205
};

206
static struct regulator_desc pwm_regulator_desc = {
207 208 209 210 211 212
	.name		= "pwm-regulator",
	.type		= REGULATOR_VOLTAGE,
	.owner		= THIS_MODULE,
	.supply_name    = "pwm",
};

213 214 215 216 217
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;
218
	unsigned int length = 0;
219 220 221 222 223 224
	int ret;

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

	if ((length < sizeof(*duty_cycle_table)) ||
	    (length % sizeof(*duty_cycle_table))) {
225
		dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
226 227 228 229 230 231 232 233 234 235 236 237
			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) {
238
		dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
239 240 241 242
		return ret;
	}

	drvdata->duty_cycle_table	= duty_cycle_table;
243 244 245 246
	memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
	       sizeof(drvdata->ops));
	drvdata->desc.ops = &drvdata->ops;
	drvdata->desc.n_voltages	= length / sizeof(*duty_cycle_table);
247 248 249 250

	return 0;
}

251 252 253
static int pwm_regulator_init_continuous(struct platform_device *pdev,
					 struct pwm_regulator_data *drvdata)
{
254 255 256 257
	memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
	       sizeof(drvdata->ops));
	drvdata->desc.ops = &drvdata->ops;
	drvdata->desc.continuous_voltage_range = true;
258 259 260 261

	return 0;
}

262 263
static int pwm_regulator_probe(struct platform_device *pdev)
{
264
	const struct regulator_init_data *init_data;
265 266 267 268
	struct pwm_regulator_data *drvdata;
	struct regulator_dev *regulator;
	struct regulator_config config = { };
	struct device_node *np = pdev->dev.of_node;
269
	enum gpiod_flags gpio_flags;
270
	int ret;
271 272 273 274 275 276 277 278 279 280

	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;

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

283
	if (of_find_property(np, "voltage-table", NULL))
284
		ret = pwm_regulator_init_table(pdev, drvdata);
285 286 287 288
	else
		ret = pwm_regulator_init_continuous(pdev, drvdata);
	if (ret)
		return ret;
289

290
	init_data = of_get_regulator_init_data(&pdev->dev, np,
291
					       &drvdata->desc);
292
	if (!init_data)
293 294 295 296 297
		return -ENOMEM;

	config.of_node = np;
	config.dev = &pdev->dev;
	config.driver_data = drvdata;
298
	config.init_data = init_data;
299 300 301

	drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
	if (IS_ERR(drvdata->pwm)) {
302 303 304
		ret = PTR_ERR(drvdata->pwm);
		dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
		return ret;
305 306
	}

307 308 309 310 311 312 313 314 315 316 317 318
	if (init_data->constraints.boot_on || init_data->constraints.always_on)
		gpio_flags = GPIOD_OUT_HIGH;
	else
		gpio_flags = GPIOD_OUT_LOW;
	drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
						    gpio_flags);
	if (IS_ERR(drvdata->enb_gpio)) {
		ret = PTR_ERR(drvdata->enb_gpio);
		dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
		return ret;
	}

319 320 321 322 323 324
	/*
	 * FIXME: pwm_apply_args() should be removed when switching to the
	 * atomic PWM API.
	 */
	pwm_apply_args(drvdata->pwm);

325
	regulator = devm_regulator_register(&pdev->dev,
326
					    &drvdata->desc, &config);
327
	if (IS_ERR(regulator)) {
328 329 330 331
		ret = PTR_ERR(regulator);
		dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
			drvdata->desc.name, ret);
		return ret;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	}

	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");