blob: cb482089050b8f9af10c502014094a22e6cdd9ba [file] [log] [blame]
Chris Zhongaa66cc62014-09-28 10:28:53 +08001/*
2 * Regulator driver for PWM Regulators
3 *
4 * Copyright (C) 2014 - STMicroelectronics Inc.
5 *
6 * Author: Lee Jones <lee.jones@linaro.org>
7 *
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.
11 */
12
Lee Jones4773be12015-07-07 16:06:51 +010013#include <linux/delay.h>
Chris Zhongaa66cc62014-09-28 10:28:53 +080014#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/err.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/machine.h>
19#include <linux/regulator/of_regulator.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/pwm.h>
23
24struct pwm_regulator_data {
Lee Jones4773be12015-07-07 16:06:51 +010025 /* Shared */
Chris Zhongaa66cc62014-09-28 10:28:53 +080026 struct pwm_device *pwm;
Lee Jones4773be12015-07-07 16:06:51 +010027
28 /* Voltage table */
29 struct pwm_voltages *duty_cycle_table;
Chris Zhongaa66cc62014-09-28 10:28:53 +080030 int state;
Lee Jones4773be12015-07-07 16:06:51 +010031
32 /* Continuous voltage */
33 u32 max_duty_cycle;
34 int volt_uV;
Chris Zhongaa66cc62014-09-28 10:28:53 +080035};
36
37struct pwm_voltages {
38 unsigned int uV;
39 unsigned int dutycycle;
40};
41
Lee Jones4773be12015-07-07 16:06:51 +010042/**
43 * Voltage table call-backs
44 */
Lee Jonesab101e32015-06-05 19:42:47 +010045static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080046{
Lee Jonesab101e32015-06-05 19:42:47 +010047 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080048
49 return drvdata->state;
50}
51
Lee Jonesab101e32015-06-05 19:42:47 +010052static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080053 unsigned selector)
54{
Lee Jonesab101e32015-06-05 19:42:47 +010055 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080056 unsigned int pwm_reg_period;
57 int dutycycle;
58 int ret;
59
60 pwm_reg_period = pwm_get_period(drvdata->pwm);
61
62 dutycycle = (pwm_reg_period *
63 drvdata->duty_cycle_table[selector].dutycycle) / 100;
64
65 ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
66 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010067 dev_err(&rdev->dev, "Failed to configure PWM\n");
Chris Zhongaa66cc62014-09-28 10:28:53 +080068 return ret;
69 }
70
71 drvdata->state = selector;
72
Lee Jonesc779ceb2015-06-05 19:42:46 +010073 ret = pwm_enable(drvdata->pwm);
74 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010075 dev_err(&rdev->dev, "Failed to enable PWM\n");
Lee Jonesc779ceb2015-06-05 19:42:46 +010076 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +080077 }
78
79 return 0;
80}
81
Lee Jonesab101e32015-06-05 19:42:47 +010082static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080083 unsigned selector)
84{
Lee Jonesab101e32015-06-05 19:42:47 +010085 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080086
Lee Jonesab101e32015-06-05 19:42:47 +010087 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +080088 return -EINVAL;
89
90 return drvdata->duty_cycle_table[selector].uV;
91}
Lee Jones4773be12015-07-07 16:06:51 +010092
93/**
94 * Continuous voltage call-backs
95 */
Lee Jonescae897d2015-07-07 16:06:52 +010096static int pwm_voltage_to_duty_cycle(struct regulator_dev *rdev, int req_uV)
Lee Jones4773be12015-07-07 16:06:51 +010097{
Lee Jonescae897d2015-07-07 16:06:52 +010098 int min_uV = rdev->constraints->min_uV;
99 int max_uV = rdev->constraints->max_uV;
100 int diff = max_uV - min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100101
Lee Jonescae897d2015-07-07 16:06:52 +0100102 return 100 - ((((req_uV * 100) - (min_uV * 100)) / diff));
Lee Jones4773be12015-07-07 16:06:51 +0100103}
104
105static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
106{
107 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
108
109 return drvdata->volt_uV;
110}
111
112static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
113 int min_uV, int max_uV,
114 unsigned *selector)
115{
116 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
117 unsigned int ramp_delay = rdev->constraints->ramp_delay;
118 int duty_cycle;
119 int ret;
120
Lee Jonescae897d2015-07-07 16:06:52 +0100121 duty_cycle = pwm_voltage_to_duty_cycle(rdev, min_uV);
Lee Jones4773be12015-07-07 16:06:51 +0100122
123 ret = pwm_config(drvdata->pwm,
124 (drvdata->pwm->period / 100) * duty_cycle,
125 drvdata->pwm->period);
126 if (ret) {
127 dev_err(&rdev->dev, "Failed to configure PWM\n");
128 return ret;
129 }
130
131 ret = pwm_enable(drvdata->pwm);
132 if (ret) {
133 dev_err(&rdev->dev, "Failed to enable PWM\n");
134 return ret;
135 }
136 drvdata->volt_uV = min_uV;
137
138 /* Delay required by PWM regulator to settle to the new voltage */
139 usleep_range(ramp_delay, ramp_delay + 1000);
140
141 return 0;
142}
143
Lee Jonesf9178da2015-07-06 09:58:29 +0100144static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800145 .set_voltage_sel = pwm_regulator_set_voltage_sel,
146 .get_voltage_sel = pwm_regulator_get_voltage_sel,
147 .list_voltage = pwm_regulator_list_voltage,
148 .map_voltage = regulator_map_voltage_iterate,
149};
150
Lee Jones4773be12015-07-07 16:06:51 +0100151static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
152 .get_voltage = pwm_regulator_get_voltage,
153 .set_voltage = pwm_regulator_set_voltage,
154};
155
Lee Jonesb6f55e72015-06-05 19:42:45 +0100156static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800157 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800158 .type = REGULATOR_VOLTAGE,
159 .owner = THIS_MODULE,
160 .supply_name = "pwm",
161};
162
Lee Jonesf9178da2015-07-06 09:58:29 +0100163static int pwm_regulator_init_table(struct platform_device *pdev,
164 struct pwm_regulator_data *drvdata)
165{
166 struct device_node *np = pdev->dev.of_node;
167 struct pwm_voltages *duty_cycle_table;
168 int length;
169 int ret;
170
171 of_find_property(np, "voltage-table", &length);
172
173 if ((length < sizeof(*duty_cycle_table)) ||
174 (length % sizeof(*duty_cycle_table))) {
175 dev_err(&pdev->dev,
176 "voltage-table length(%d) is invalid\n",
177 length);
178 return -EINVAL;
179 }
180
181 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
182 if (!duty_cycle_table)
183 return -ENOMEM;
184
185 ret = of_property_read_u32_array(np, "voltage-table",
186 (u32 *)duty_cycle_table,
187 length / sizeof(u32));
188 if (ret) {
189 dev_err(&pdev->dev, "Failed to read voltage-table\n");
190 return ret;
191 }
192
193 drvdata->duty_cycle_table = duty_cycle_table;
194 pwm_regulator_desc.ops = &pwm_regulator_voltage_table_ops;
195 pwm_regulator_desc.n_voltages = length / sizeof(*duty_cycle_table);
196
197 return 0;
198}
199
Lee Jones4773be12015-07-07 16:06:51 +0100200static int pwm_regulator_init_continuous(struct platform_device *pdev,
201 struct pwm_regulator_data *drvdata)
202{
203 struct device_node *np = pdev->dev.of_node;
204 int ret;
205
206 ret = of_property_read_u32(np, "max-duty-cycle",
207 &drvdata->max_duty_cycle);
208 if (ret) {
209 dev_err(&pdev->dev, "Failed to read \"pwm-max-value\"\n");
210 return ret;
211 }
212
213 pwm_regulator_desc.ops = &pwm_regulator_voltage_continuous_ops;
214 pwm_regulator_desc.continuous_voltage_range = true;
215
216 return 0;
217}
218
Chris Zhongaa66cc62014-09-28 10:28:53 +0800219static int pwm_regulator_probe(struct platform_device *pdev)
220{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100221 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800222 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800223 struct regulator_dev *regulator;
224 struct regulator_config config = { };
225 struct device_node *np = pdev->dev.of_node;
Lee Jonesf9178da2015-07-06 09:58:29 +0100226 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800227
228 if (!np) {
229 dev_err(&pdev->dev, "Device Tree node missing\n");
230 return -EINVAL;
231 }
232
233 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
234 if (!drvdata)
235 return -ENOMEM;
236
Lee Jones4773be12015-07-07 16:06:51 +0100237 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100238 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100239 else
240 ret = pwm_regulator_init_continuous(pdev, drvdata);
241 if (ret)
242 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800243
Lee Jones5ad2cb12015-07-07 16:06:53 +0100244 init_data = of_get_regulator_init_data(&pdev->dev, np,
245 &pwm_regulator_desc);
246 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800247 return -ENOMEM;
248
249 config.of_node = np;
250 config.dev = &pdev->dev;
251 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100252 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800253
254 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
255 if (IS_ERR(drvdata->pwm)) {
256 dev_err(&pdev->dev, "Failed to get PWM\n");
257 return PTR_ERR(drvdata->pwm);
258 }
259
260 regulator = devm_regulator_register(&pdev->dev,
Lee Jonesb6f55e72015-06-05 19:42:45 +0100261 &pwm_regulator_desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800262 if (IS_ERR(regulator)) {
263 dev_err(&pdev->dev, "Failed to register regulator %s\n",
Lee Jonesb6f55e72015-06-05 19:42:45 +0100264 pwm_regulator_desc.name);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800265 return PTR_ERR(regulator);
266 }
267
268 return 0;
269}
270
271static const struct of_device_id pwm_of_match[] = {
272 { .compatible = "pwm-regulator" },
273 { },
274};
275MODULE_DEVICE_TABLE(of, pwm_of_match);
276
277static struct platform_driver pwm_regulator_driver = {
278 .driver = {
279 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800280 .of_match_table = of_match_ptr(pwm_of_match),
281 },
282 .probe = pwm_regulator_probe,
283};
284
285module_platform_driver(pwm_regulator_driver);
286
287MODULE_LICENSE("GPL");
288MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
289MODULE_DESCRIPTION("PWM Regulator Driver");
290MODULE_ALIAS("platform:pwm-regulator");