blob: fc3166dfcbfaa455b5105ef8e7715d3e17c56d6b [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 */
Lee Jones4773be12015-07-07 16:06:51 +010033 int volt_uV;
Chris Zhongaa66cc62014-09-28 10:28:53 +080034};
35
36struct pwm_voltages {
37 unsigned int uV;
38 unsigned int dutycycle;
39};
40
Lee Jones4773be12015-07-07 16:06:51 +010041/**
42 * Voltage table call-backs
43 */
Lee Jonesab101e32015-06-05 19:42:47 +010044static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080045{
Lee Jonesab101e32015-06-05 19:42:47 +010046 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080047
48 return drvdata->state;
49}
50
Lee Jonesab101e32015-06-05 19:42:47 +010051static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080052 unsigned selector)
53{
Lee Jonesab101e32015-06-05 19:42:47 +010054 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080055 unsigned int pwm_reg_period;
56 int dutycycle;
57 int ret;
58
59 pwm_reg_period = pwm_get_period(drvdata->pwm);
60
61 dutycycle = (pwm_reg_period *
62 drvdata->duty_cycle_table[selector].dutycycle) / 100;
63
64 ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
65 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010066 dev_err(&rdev->dev, "Failed to configure PWM\n");
Chris Zhongaa66cc62014-09-28 10:28:53 +080067 return ret;
68 }
69
70 drvdata->state = selector;
71
Lee Jonesc779ceb2015-06-05 19:42:46 +010072 ret = pwm_enable(drvdata->pwm);
73 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010074 dev_err(&rdev->dev, "Failed to enable PWM\n");
Lee Jonesc779ceb2015-06-05 19:42:46 +010075 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +080076 }
77
78 return 0;
79}
80
Lee Jonesab101e32015-06-05 19:42:47 +010081static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080082 unsigned selector)
83{
Lee Jonesab101e32015-06-05 19:42:47 +010084 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080085
Lee Jonesab101e32015-06-05 19:42:47 +010086 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +080087 return -EINVAL;
88
89 return drvdata->duty_cycle_table[selector].uV;
90}
Lee Jones4773be12015-07-07 16:06:51 +010091
92/**
93 * Continuous voltage call-backs
94 */
Lee Jonesf3f64392015-07-09 16:35:28 +010095static int pwm_voltage_to_duty_cycle_percentage(struct regulator_dev *rdev, int req_uV)
Lee Jones4773be12015-07-07 16:06:51 +010096{
Lee Jonescae897d2015-07-07 16:06:52 +010097 int min_uV = rdev->constraints->min_uV;
98 int max_uV = rdev->constraints->max_uV;
99 int diff = max_uV - min_uV;
Lee Jones4773be12015-07-07 16:06:51 +0100100
Lee Jonesf3f64392015-07-09 16:35:28 +0100101 return 100 - (((req_uV * 100) - (min_uV * 100)) / diff);
Lee Jones4773be12015-07-07 16:06:51 +0100102}
103
104static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
105{
106 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
107
108 return drvdata->volt_uV;
109}
110
111static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
112 int min_uV, int max_uV,
113 unsigned *selector)
114{
115 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
116 unsigned int ramp_delay = rdev->constraints->ramp_delay;
Lee Jonesf3f64392015-07-09 16:35:28 +0100117 unsigned int period = pwm_get_period(drvdata->pwm);
Lee Jones4773be12015-07-07 16:06:51 +0100118 int duty_cycle;
119 int ret;
120
Lee Jonesf3f64392015-07-09 16:35:28 +0100121 duty_cycle = pwm_voltage_to_duty_cycle_percentage(rdev, min_uV);
Lee Jones4773be12015-07-07 16:06:51 +0100122
Lee Jonesf3f64392015-07-09 16:35:28 +0100123 ret = pwm_config(drvdata->pwm, (period / 100) * duty_cycle, period);
Lee Jones4773be12015-07-07 16:06:51 +0100124 if (ret) {
125 dev_err(&rdev->dev, "Failed to configure PWM\n");
126 return ret;
127 }
128
129 ret = pwm_enable(drvdata->pwm);
130 if (ret) {
131 dev_err(&rdev->dev, "Failed to enable PWM\n");
132 return ret;
133 }
134 drvdata->volt_uV = min_uV;
135
136 /* Delay required by PWM regulator to settle to the new voltage */
137 usleep_range(ramp_delay, ramp_delay + 1000);
138
139 return 0;
140}
141
Lee Jonesf9178da2015-07-06 09:58:29 +0100142static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800143 .set_voltage_sel = pwm_regulator_set_voltage_sel,
144 .get_voltage_sel = pwm_regulator_get_voltage_sel,
145 .list_voltage = pwm_regulator_list_voltage,
146 .map_voltage = regulator_map_voltage_iterate,
147};
148
Lee Jones4773be12015-07-07 16:06:51 +0100149static struct regulator_ops pwm_regulator_voltage_continuous_ops = {
150 .get_voltage = pwm_regulator_get_voltage,
151 .set_voltage = pwm_regulator_set_voltage,
152};
153
Lee Jonesb6f55e72015-06-05 19:42:45 +0100154static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +0800155 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800156 .type = REGULATOR_VOLTAGE,
157 .owner = THIS_MODULE,
158 .supply_name = "pwm",
159};
160
Lee Jonesf9178da2015-07-06 09:58:29 +0100161static int pwm_regulator_init_table(struct platform_device *pdev,
162 struct pwm_regulator_data *drvdata)
163{
164 struct device_node *np = pdev->dev.of_node;
165 struct pwm_voltages *duty_cycle_table;
Lee Jones60cb65e2015-07-10 08:45:36 +0100166 unsigned int length = 0;
Lee Jonesf9178da2015-07-06 09:58:29 +0100167 int ret;
168
169 of_find_property(np, "voltage-table", &length);
170
171 if ((length < sizeof(*duty_cycle_table)) ||
172 (length % sizeof(*duty_cycle_table))) {
173 dev_err(&pdev->dev,
174 "voltage-table length(%d) is invalid\n",
175 length);
176 return -EINVAL;
177 }
178
179 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
180 if (!duty_cycle_table)
181 return -ENOMEM;
182
183 ret = of_property_read_u32_array(np, "voltage-table",
184 (u32 *)duty_cycle_table,
185 length / sizeof(u32));
186 if (ret) {
187 dev_err(&pdev->dev, "Failed to read voltage-table\n");
188 return ret;
189 }
190
191 drvdata->duty_cycle_table = duty_cycle_table;
192 pwm_regulator_desc.ops = &pwm_regulator_voltage_table_ops;
193 pwm_regulator_desc.n_voltages = length / sizeof(*duty_cycle_table);
194
195 return 0;
196}
197
Lee Jones4773be12015-07-07 16:06:51 +0100198static int pwm_regulator_init_continuous(struct platform_device *pdev,
199 struct pwm_regulator_data *drvdata)
200{
Lee Jones4773be12015-07-07 16:06:51 +0100201 pwm_regulator_desc.ops = &pwm_regulator_voltage_continuous_ops;
202 pwm_regulator_desc.continuous_voltage_range = true;
203
204 return 0;
205}
206
Chris Zhongaa66cc62014-09-28 10:28:53 +0800207static int pwm_regulator_probe(struct platform_device *pdev)
208{
Lee Jones5ad2cb12015-07-07 16:06:53 +0100209 const struct regulator_init_data *init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800210 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800211 struct regulator_dev *regulator;
212 struct regulator_config config = { };
213 struct device_node *np = pdev->dev.of_node;
Lee Jonesf9178da2015-07-06 09:58:29 +0100214 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800215
216 if (!np) {
217 dev_err(&pdev->dev, "Device Tree node missing\n");
218 return -EINVAL;
219 }
220
221 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
222 if (!drvdata)
223 return -ENOMEM;
224
Lee Jones4773be12015-07-07 16:06:51 +0100225 if (of_find_property(np, "voltage-table", NULL))
Lee Jonesf9178da2015-07-06 09:58:29 +0100226 ret = pwm_regulator_init_table(pdev, drvdata);
Lee Jones4773be12015-07-07 16:06:51 +0100227 else
228 ret = pwm_regulator_init_continuous(pdev, drvdata);
229 if (ret)
230 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800231
Lee Jones5ad2cb12015-07-07 16:06:53 +0100232 init_data = of_get_regulator_init_data(&pdev->dev, np,
233 &pwm_regulator_desc);
234 if (!init_data)
Chris Zhongaa66cc62014-09-28 10:28:53 +0800235 return -ENOMEM;
236
237 config.of_node = np;
238 config.dev = &pdev->dev;
239 config.driver_data = drvdata;
Lee Jones5ad2cb12015-07-07 16:06:53 +0100240 config.init_data = init_data;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800241
242 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
243 if (IS_ERR(drvdata->pwm)) {
244 dev_err(&pdev->dev, "Failed to get PWM\n");
245 return PTR_ERR(drvdata->pwm);
246 }
247
248 regulator = devm_regulator_register(&pdev->dev,
Lee Jonesb6f55e72015-06-05 19:42:45 +0100249 &pwm_regulator_desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800250 if (IS_ERR(regulator)) {
251 dev_err(&pdev->dev, "Failed to register regulator %s\n",
Lee Jonesb6f55e72015-06-05 19:42:45 +0100252 pwm_regulator_desc.name);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800253 return PTR_ERR(regulator);
254 }
255
256 return 0;
257}
258
259static const struct of_device_id pwm_of_match[] = {
260 { .compatible = "pwm-regulator" },
261 { },
262};
263MODULE_DEVICE_TABLE(of, pwm_of_match);
264
265static struct platform_driver pwm_regulator_driver = {
266 .driver = {
267 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800268 .of_match_table = of_match_ptr(pwm_of_match),
269 },
270 .probe = pwm_regulator_probe,
271};
272
273module_platform_driver(pwm_regulator_driver);
274
275MODULE_LICENSE("GPL");
276MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
277MODULE_DESCRIPTION("PWM Regulator Driver");
278MODULE_ALIAS("platform:pwm-regulator");