pwm-regulator.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.
 */

#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>
22
#include <linux/gpio/consumer.h>
23

24 25 26 27 28 29
struct pwm_continuous_reg_data {
	unsigned int min_uV_dutycycle;
	unsigned int max_uV_dutycycle;
	unsigned int dutycycle_unit;
};

30
struct pwm_regulator_data {
31
	/*  Shared */
32
	struct pwm_device *pwm;
33 34 35

	/* Voltage table */
	struct pwm_voltages *duty_cycle_table;
36

37 38 39
	/* Continuous mode info */
	struct pwm_continuous_reg_data continuous;

40 41 42 43 44 45
	/* regulator descriptor */
	struct regulator_desc desc;

	/* Regulator ops */
	struct regulator_ops ops;

46
	int state;
47

48 49
	/* Enable GPIO */
	struct gpio_desc *enb_gpio;
50 51 52 53 54 55 56
};

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

57 58 59
/**
 * Voltage table call-backs
 */
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static void pwm_regulator_init_state(struct regulator_dev *rdev)
{
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
	struct pwm_state pwm_state;
	unsigned int dutycycle;
	int i;

	pwm_get_state(drvdata->pwm, &pwm_state);
	dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);

	for (i = 0; i < rdev->desc->n_voltages; i++) {
		if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
			drvdata->state = i;
			return;
		}
	}
}

78
static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
79
{
80
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
81

82 83 84
	if (drvdata->state < 0)
		pwm_regulator_init_state(rdev);

85 86 87
	return drvdata->state;
}

88
static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
89 90
					 unsigned selector)
{
91
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
92
	struct pwm_state pstate;
93 94
	int ret;

95 96 97
	pwm_init_state(drvdata->pwm, &pstate);
	pwm_set_relative_duty_cycle(&pstate,
			drvdata->duty_cycle_table[selector].dutycycle, 100);
98

99
	ret = pwm_apply_state(drvdata->pwm, &pstate);
100
	if (ret) {
101
		dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
102 103 104 105 106 107 108 109
		return ret;
	}

	drvdata->state = selector;

	return 0;
}

110
static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
111 112
				      unsigned selector)
{
113
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
114

115
	if (selector >= rdev->desc->n_voltages)
116 117 118 119
		return -EINVAL;

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

121 122 123 124
static int pwm_regulator_enable(struct regulator_dev *dev)
{
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);

125 126 127
	if (drvdata->enb_gpio)
		gpiod_set_value_cansleep(drvdata->enb_gpio, 1);

128 129 130 131 132 133 134 135 136
	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);

137 138 139
	if (drvdata->enb_gpio)
		gpiod_set_value_cansleep(drvdata->enb_gpio, 0);

140 141 142 143 144 145 146
	return 0;
}

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

147 148 149
	if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
		return false;

150 151 152
	return pwm_is_enabled(drvdata->pwm);
}

153 154 155
static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
{
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
156 157 158
	unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
	unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
	unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
159
	int min_uV = rdev->constraints->min_uV;
160 161
	int max_uV = rdev->constraints->max_uV;
	int diff_uV = max_uV - min_uV;
162
	struct pwm_state pstate;
163 164
	unsigned int diff_duty;
	unsigned int voltage;
165

166 167
	pwm_get_state(drvdata->pwm, &pstate);

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);

	/*
	 * The dutycycle for min_uV might be greater than the one for max_uV.
	 * This is happening when the user needs an inversed polarity, but the
	 * PWM device does not support inversing it in hardware.
	 */
	if (max_uV_duty < min_uV_duty) {
		voltage = min_uV_duty - voltage;
		diff_duty = min_uV_duty - max_uV_duty;
	} else {
		voltage = voltage - min_uV_duty;
		diff_duty = max_uV_duty - min_uV_duty;
	}

	voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);

	return voltage + min_uV;
186 187 188
}

static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
189 190
				     int req_min_uV, int req_max_uV,
				     unsigned int *selector)
191 192
{
	struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
193 194 195 196 197 198
	unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
	unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
	unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
	int min_uV = rdev->constraints->min_uV;
	int max_uV = rdev->constraints->max_uV;
	int diff_uV = max_uV - min_uV;
199
	struct pwm_state pstate;
200 201
	unsigned int diff_duty;
	unsigned int dutycycle;
202 203
	int ret;

204
	pwm_init_state(drvdata->pwm, &pstate);
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	/*
	 * The dutycycle for min_uV might be greater than the one for max_uV.
	 * This is happening when the user needs an inversed polarity, but the
	 * PWM device does not support inversing it in hardware.
	 */
	if (max_uV_duty < min_uV_duty)
		diff_duty = min_uV_duty - max_uV_duty;
	else
		diff_duty = max_uV_duty - min_uV_duty;

	dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
					  diff_duty,
					  diff_uV);

	if (max_uV_duty < min_uV_duty)
		dutycycle = min_uV_duty - dutycycle;
	else
		dutycycle = min_uV_duty + dutycycle;

	pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
226

227
	ret = pwm_apply_state(drvdata->pwm, &pstate);
228
	if (ret) {
229
		dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
230 231 232 233 234 235
		return ret;
	}

	return 0;
}

236
static struct regulator_ops pwm_regulator_voltage_table_ops = {
237 238 239 240
	.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,
241 242 243
	.enable          = pwm_regulator_enable,
	.disable         = pwm_regulator_disable,
	.is_enabled      = pwm_regulator_is_enabled,
244 245
};

246 247 248
static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
	.get_voltage = pwm_regulator_get_voltage,
	.set_voltage = pwm_regulator_set_voltage,
249 250 251
	.enable          = pwm_regulator_enable,
	.disable         = pwm_regulator_disable,
	.is_enabled      = pwm_regulator_is_enabled,
252 253
};

254
static struct regulator_desc pwm_regulator_desc = {
255 256 257 258 259 260
	.name		= "pwm-regulator",
	.type		= REGULATOR_VOLTAGE,
	.owner		= THIS_MODULE,
	.supply_name    = "pwm",
};

261 262 263 264 265
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;
266
	unsigned int length = 0;
267 268 269 270 271 272
	int ret;

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

	if ((length < sizeof(*duty_cycle_table)) ||
	    (length % sizeof(*duty_cycle_table))) {
273
		dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
274 275 276 277 278 279 280 281 282 283 284 285
			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) {
286
		dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
287 288 289
		return ret;
	}

290
	drvdata->state			= -EINVAL;
291
	drvdata->duty_cycle_table	= duty_cycle_table;
292 293 294 295
	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);
296 297 298 299

	return 0;
}

300 301 302
static int pwm_regulator_init_continuous(struct platform_device *pdev,
					 struct pwm_regulator_data *drvdata)
{
303 304 305
	u32 dutycycle_range[2] = { 0, 100 };
	u32 dutycycle_unit = 100;

306 307 308 309
	memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
	       sizeof(drvdata->ops));
	drvdata->desc.ops = &drvdata->ops;
	drvdata->desc.continuous_voltage_range = true;
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324
	of_property_read_u32_array(pdev->dev.of_node,
				   "pwm-dutycycle-range",
				   dutycycle_range, 2);
	of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
			     &dutycycle_unit);

	if (dutycycle_range[0] > dutycycle_unit ||
	    dutycycle_range[1] > dutycycle_unit)
		return -EINVAL;

	drvdata->continuous.dutycycle_unit = dutycycle_unit;
	drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
	drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];

325 326 327
	return 0;
}

328 329
static int pwm_regulator_probe(struct platform_device *pdev)
{
330
	const struct regulator_init_data *init_data;
331 332 333 334
	struct pwm_regulator_data *drvdata;
	struct regulator_dev *regulator;
	struct regulator_config config = { };
	struct device_node *np = pdev->dev.of_node;
335
	enum gpiod_flags gpio_flags;
336
	int ret;
337 338 339 340 341 342 343 344 345 346

	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;

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

349
	if (of_find_property(np, "voltage-table", NULL))
350
		ret = pwm_regulator_init_table(pdev, drvdata);
351 352 353 354
	else
		ret = pwm_regulator_init_continuous(pdev, drvdata);
	if (ret)
		return ret;
355

356
	init_data = of_get_regulator_init_data(&pdev->dev, np,
357
					       &drvdata->desc);
358
	if (!init_data)
359 360 361 362 363
		return -ENOMEM;

	config.of_node = np;
	config.dev = &pdev->dev;
	config.driver_data = drvdata;
364
	config.init_data = init_data;
365 366 367

	drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
	if (IS_ERR(drvdata->pwm)) {
368 369 370
		ret = PTR_ERR(drvdata->pwm);
		dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
		return ret;
371 372
	}

373 374 375 376 377 378 379 380 381 382 383 384
	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;
	}

385 386 387
	ret = pwm_adjust_config(drvdata->pwm);
	if (ret)
		return ret;
388

389
	regulator = devm_regulator_register(&pdev->dev,
390
					    &drvdata->desc, &config);
391
	if (IS_ERR(regulator)) {
392 393 394 395
		ret = PTR_ERR(regulator);
		dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
			drvdata->desc.name, ret);
		return ret;
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	}

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