ubifs: don't bother checking for encryption key in ->mmap()
[linux/fpc-iii.git] / drivers / regulator / pwm-regulator.c
blob1b88e0e15a70ce2fed55456c11c57d1ee9a64c69
1 /*
2 * Regulator driver for PWM Regulators
4 * Copyright (C) 2014 - STMicroelectronics Inc.
6 * Author: Lee Jones <lee.jones@linaro.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/pwm.h>
22 #include <linux/gpio/consumer.h>
24 struct pwm_continuous_reg_data {
25 unsigned int min_uV_dutycycle;
26 unsigned int max_uV_dutycycle;
27 unsigned int dutycycle_unit;
30 struct pwm_regulator_data {
31 /* Shared */
32 struct pwm_device *pwm;
34 /* Voltage table */
35 struct pwm_voltages *duty_cycle_table;
37 /* Continuous mode info */
38 struct pwm_continuous_reg_data continuous;
40 /* regulator descriptor */
41 struct regulator_desc desc;
43 /* Regulator ops */
44 struct regulator_ops ops;
46 int state;
48 /* Enable GPIO */
49 struct gpio_desc *enb_gpio;
52 struct pwm_voltages {
53 unsigned int uV;
54 unsigned int dutycycle;
57 /**
58 * Voltage table call-backs
60 static void pwm_regulator_init_state(struct regulator_dev *rdev)
62 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
63 struct pwm_state pwm_state;
64 unsigned int dutycycle;
65 int i;
67 pwm_get_state(drvdata->pwm, &pwm_state);
68 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
70 for (i = 0; i < rdev->desc->n_voltages; i++) {
71 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
72 drvdata->state = i;
73 return;
78 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
80 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
82 if (drvdata->state < 0)
83 pwm_regulator_init_state(rdev);
85 return drvdata->state;
88 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
89 unsigned selector)
91 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
92 struct pwm_state pstate;
93 int ret;
95 pwm_init_state(drvdata->pwm, &pstate);
96 pwm_set_relative_duty_cycle(&pstate,
97 drvdata->duty_cycle_table[selector].dutycycle, 100);
99 ret = pwm_apply_state(drvdata->pwm, &pstate);
100 if (ret) {
101 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
102 return ret;
105 drvdata->state = selector;
107 return 0;
110 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
111 unsigned selector)
113 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
115 if (selector >= rdev->desc->n_voltages)
116 return -EINVAL;
118 return drvdata->duty_cycle_table[selector].uV;
121 static int pwm_regulator_enable(struct regulator_dev *dev)
123 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
125 if (drvdata->enb_gpio)
126 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
128 return pwm_enable(drvdata->pwm);
131 static int pwm_regulator_disable(struct regulator_dev *dev)
133 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
135 pwm_disable(drvdata->pwm);
137 if (drvdata->enb_gpio)
138 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
140 return 0;
143 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
145 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
147 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
148 return false;
150 return pwm_is_enabled(drvdata->pwm);
153 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
155 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
156 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
157 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
158 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
159 int min_uV = rdev->constraints->min_uV;
160 int max_uV = rdev->constraints->max_uV;
161 int diff_uV = max_uV - min_uV;
162 struct pwm_state pstate;
163 unsigned int diff_duty;
164 unsigned int voltage;
166 pwm_get_state(drvdata->pwm, &pstate);
168 voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
171 * The dutycycle for min_uV might be greater than the one for max_uV.
172 * This is happening when the user needs an inversed polarity, but the
173 * PWM device does not support inversing it in hardware.
175 if (max_uV_duty < min_uV_duty) {
176 voltage = min_uV_duty - voltage;
177 diff_duty = min_uV_duty - max_uV_duty;
178 } else {
179 voltage = voltage - min_uV_duty;
180 diff_duty = max_uV_duty - min_uV_duty;
183 voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
185 return voltage + min_uV;
188 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
189 int req_min_uV, int req_max_uV,
190 unsigned int *selector)
192 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
193 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
194 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
195 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
196 int min_uV = rdev->constraints->min_uV;
197 int max_uV = rdev->constraints->max_uV;
198 int diff_uV = max_uV - min_uV;
199 struct pwm_state pstate;
200 unsigned int diff_duty;
201 unsigned int dutycycle;
202 int ret;
204 pwm_init_state(drvdata->pwm, &pstate);
207 * The dutycycle for min_uV might be greater than the one for max_uV.
208 * This is happening when the user needs an inversed polarity, but the
209 * PWM device does not support inversing it in hardware.
211 if (max_uV_duty < min_uV_duty)
212 diff_duty = min_uV_duty - max_uV_duty;
213 else
214 diff_duty = max_uV_duty - min_uV_duty;
216 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
217 diff_duty,
218 diff_uV);
220 if (max_uV_duty < min_uV_duty)
221 dutycycle = min_uV_duty - dutycycle;
222 else
223 dutycycle = min_uV_duty + dutycycle;
225 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
227 ret = pwm_apply_state(drvdata->pwm, &pstate);
228 if (ret) {
229 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
230 return ret;
233 return 0;
236 static struct regulator_ops pwm_regulator_voltage_table_ops = {
237 .set_voltage_sel = pwm_regulator_set_voltage_sel,
238 .get_voltage_sel = pwm_regulator_get_voltage_sel,
239 .list_voltage = pwm_regulator_list_voltage,
240 .map_voltage = regulator_map_voltage_iterate,
241 .enable = pwm_regulator_enable,
242 .disable = pwm_regulator_disable,
243 .is_enabled = pwm_regulator_is_enabled,
246 static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
247 .get_voltage = pwm_regulator_get_voltage,
248 .set_voltage = pwm_regulator_set_voltage,
249 .enable = pwm_regulator_enable,
250 .disable = pwm_regulator_disable,
251 .is_enabled = pwm_regulator_is_enabled,
254 static struct regulator_desc pwm_regulator_desc = {
255 .name = "pwm-regulator",
256 .type = REGULATOR_VOLTAGE,
257 .owner = THIS_MODULE,
258 .supply_name = "pwm",
261 static int pwm_regulator_init_table(struct platform_device *pdev,
262 struct pwm_regulator_data *drvdata)
264 struct device_node *np = pdev->dev.of_node;
265 struct pwm_voltages *duty_cycle_table;
266 unsigned int length = 0;
267 int ret;
269 of_find_property(np, "voltage-table", &length);
271 if ((length < sizeof(*duty_cycle_table)) ||
272 (length % sizeof(*duty_cycle_table))) {
273 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
274 length);
275 return -EINVAL;
278 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
279 if (!duty_cycle_table)
280 return -ENOMEM;
282 ret = of_property_read_u32_array(np, "voltage-table",
283 (u32 *)duty_cycle_table,
284 length / sizeof(u32));
285 if (ret) {
286 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
287 return ret;
290 drvdata->state = -EINVAL;
291 drvdata->duty_cycle_table = duty_cycle_table;
292 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
293 sizeof(drvdata->ops));
294 drvdata->desc.ops = &drvdata->ops;
295 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
297 return 0;
300 static int pwm_regulator_init_continuous(struct platform_device *pdev,
301 struct pwm_regulator_data *drvdata)
303 u32 dutycycle_range[2] = { 0, 100 };
304 u32 dutycycle_unit = 100;
306 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
307 sizeof(drvdata->ops));
308 drvdata->desc.ops = &drvdata->ops;
309 drvdata->desc.continuous_voltage_range = true;
311 of_property_read_u32_array(pdev->dev.of_node,
312 "pwm-dutycycle-range",
313 dutycycle_range, 2);
314 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
315 &dutycycle_unit);
317 if (dutycycle_range[0] > dutycycle_unit ||
318 dutycycle_range[1] > dutycycle_unit)
319 return -EINVAL;
321 drvdata->continuous.dutycycle_unit = dutycycle_unit;
322 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
323 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
325 return 0;
328 static int pwm_regulator_probe(struct platform_device *pdev)
330 const struct regulator_init_data *init_data;
331 struct pwm_regulator_data *drvdata;
332 struct regulator_dev *regulator;
333 struct regulator_config config = { };
334 struct device_node *np = pdev->dev.of_node;
335 enum gpiod_flags gpio_flags;
336 int ret;
338 if (!np) {
339 dev_err(&pdev->dev, "Device Tree node missing\n");
340 return -EINVAL;
343 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
344 if (!drvdata)
345 return -ENOMEM;
347 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 else
352 ret = pwm_regulator_init_continuous(pdev, drvdata);
353 if (ret)
354 return ret;
356 init_data = of_get_regulator_init_data(&pdev->dev, np,
357 &drvdata->desc);
358 if (!init_data)
359 return -ENOMEM;
361 config.of_node = np;
362 config.dev = &pdev->dev;
363 config.driver_data = drvdata;
364 config.init_data = init_data;
366 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
367 if (IS_ERR(drvdata->pwm)) {
368 ret = PTR_ERR(drvdata->pwm);
369 dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret);
370 return ret;
373 if (init_data->constraints.boot_on || init_data->constraints.always_on)
374 gpio_flags = GPIOD_OUT_HIGH;
375 else
376 gpio_flags = GPIOD_OUT_LOW;
377 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
378 gpio_flags);
379 if (IS_ERR(drvdata->enb_gpio)) {
380 ret = PTR_ERR(drvdata->enb_gpio);
381 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
382 return ret;
385 ret = pwm_adjust_config(drvdata->pwm);
386 if (ret)
387 return ret;
389 regulator = devm_regulator_register(&pdev->dev,
390 &drvdata->desc, &config);
391 if (IS_ERR(regulator)) {
392 ret = PTR_ERR(regulator);
393 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
394 drvdata->desc.name, ret);
395 return ret;
398 return 0;
401 static const struct of_device_id pwm_of_match[] = {
402 { .compatible = "pwm-regulator" },
403 { },
405 MODULE_DEVICE_TABLE(of, pwm_of_match);
407 static struct platform_driver pwm_regulator_driver = {
408 .driver = {
409 .name = "pwm-regulator",
410 .of_match_table = of_match_ptr(pwm_of_match),
412 .probe = pwm_regulator_probe,
415 module_platform_driver(pwm_regulator_driver);
417 MODULE_LICENSE("GPL");
418 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
419 MODULE_DESCRIPTION("PWM Regulator Driver");
420 MODULE_ALIAS("platform:pwm-regulator");