3 * Copyright (C) 2013 ROCKCHIP, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/kernel.h>
19 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/regulator/rockchip-pwm-regulator.h>
24 #include <linux/gpio.h>
25 #include <linux/of_gpio.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/pwm.h>
32 #include <linux/of_device.h>
33 #include <linux/regulator/of_regulator.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/machine.h>
39 #define DBG(x...) printk(KERN_INFO x)
44 const static int pwm_voltage_map[] = {
45 925000 , 950000, 975000, 1000000, 1025000, 1050000,
46 1075000, 1100000, 1125000, 1150000, 1175000,
47 1200000, 1225000, 1250000, 1275000, 1300000,
48 1325000, 1350000, 1375000, 1400000
51 static int pwm_set_rate(struct pwm_platform_data *pdata, u32 rate)
54 DBG("%s:id=%d,rate=%d clkrate =%d\n", __func__, pdata->pwm_id, rate, pdata->period);
56 duty_cycle = (rate * (pdata->period) / 100) ;
58 pwm_config(pdata->pwm, duty_cycle, pdata->period);
60 pwm_enable(pdata->pwm);
65 static int pwm_regulator_list_voltage(struct regulator_dev *dev, unsigned int index)
67 struct pwm_platform_data *pdata = rdev_get_drvdata(dev);
69 DBG("%s:line=%d,pdata=%p,pwm_id=%d\n", __func__,
70 __LINE__, pdata, pdata->pwm_id);
71 if (index < (pdata->pwm_vol_map_count + 1))
72 return pdata->pwm_voltage_map[index];
77 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
79 DBG("Enter %s\n", __func__);
83 static int pwm_regulator_enable(struct regulator_dev *dev)
85 DBG("Enter %s\n", __func__);
89 static int pwm_regulator_disable(struct regulator_dev *dev)
91 DBG("Enter %s\n", __func__);
95 static int pwm_regulator_get_voltage(struct regulator_dev *dev)
98 struct pwm_platform_data *pdata = rdev_get_drvdata(dev);
100 DBG("%s:line=%d,pdata=%p,pwm_id=%d\n", __func__, __LINE__,
101 pdata, pdata->pwm_id);
102 vol = pdata->pwm_voltage;
107 static int pwm_regulator_set_voltage(struct regulator_dev *dev,
108 int min_uV, int max_uV, unsigned *selector)
110 struct pwm_platform_data *pdata = rdev_get_drvdata(dev);
111 const int *voltage_map = pdata->pwm_voltage_map;
112 int max = pdata->max_uV;
113 int coefficient = pdata->coefficient;
114 u32 size = pdata->pwm_vol_map_count;
115 u32 i, vol, pwm_value;
117 DBG("%s: min_uV = %d, max_uV = %d\n", __func__, min_uV, max_uV);
118 DBG("%s:line=%d,pdata=%p,pwm_id=%d\n", __func__, __LINE__,
119 pdata, pdata->pwm_id);
120 mutex_lock(&pdata->mutex_pwm);
122 if (min_uV < voltage_map[0] || max_uV > voltage_map[size-1]) {
123 printk("%s: voltage_map voltage is out of table\n", __func__);
124 mutex_unlock(&pdata->mutex_pwm);
128 for (i = 0; i < size; i++) {
129 if (voltage_map[i] >= min_uV)
132 vol = voltage_map[i];
133 pdata->pwm_voltage = vol;
135 /* VDD12 = 1.40 - 0.455*D , DΪPWMÕ¼¿Õ±È*/
136 pwm_value = (max-vol) / coefficient / 10;
137 /*pwm_value %, coefficient *1000*/
139 if (pwm_set_rate(pdata, pwm_value) != 0) {
140 printk("%s:fail to set pwm rate,pwm_value=%d\n", __func__, pwm_value);
141 mutex_unlock(&pdata->mutex_pwm);
147 mutex_unlock(&pdata->mutex_pwm);
149 DBG("%s:ok,vol=%d,pwm_value=%d %d\n", __func__, vol,
150 pwm_value, pdata->pwm_voltage);
156 static struct regulator_ops pwm_voltage_ops = {
157 .list_voltage = pwm_regulator_list_voltage,
158 .set_voltage = pwm_regulator_set_voltage,
159 .get_voltage = pwm_regulator_get_voltage,
160 .enable = pwm_regulator_enable,
161 .disable = pwm_regulator_disable,
162 .is_enabled = pwm_regulator_is_enabled,
164 static struct regulator_desc regulators[] = {
169 .ops = &pwm_voltage_ops,
170 .n_voltages = ARRAY_SIZE(pwm_voltage_map),
171 .type = REGULATOR_VOLTAGE,
172 .owner = THIS_MODULE,
178 .ops = &pwm_voltage_ops,
179 .n_voltages = ARRAY_SIZE(pwm_voltage_map),
180 .type = REGULATOR_VOLTAGE,
181 .owner = THIS_MODULE,
187 static struct of_device_id pwm_of_match[] = {
188 { .compatible = "rockchip_pwm_regulator"},
191 MODULE_DEVICE_TABLE(of, pwm_of_match);
193 static struct of_regulator_match pwm_matches[] = {
194 { .name = "pwm_dcdc1", .driver_data = (void *)0 },
195 { .name = "pwm_dcdc2", .driver_data = (void *)1 },
198 static struct pwm_regulator_board *pwm_regulator_parse_dt(
199 struct platform_device *pdev,
200 struct of_regulator_match **pwm_reg_matches)
202 struct pwm_regulator_board *pwm_plat_data;
203 struct device_node *np, *regulators;
204 struct of_regulator_match *matches;
205 int idx = 0, ret, count;
206 struct property *prop;
208 const __be32 *init_vol, *max_vol, *min_vol, *suspend_vol, *coefficient, *id;
210 DBG("%s,line=%d\n", __func__, __LINE__);
212 pwm_plat_data = devm_kzalloc(&pdev->dev, sizeof(*pwm_plat_data),
215 if (!pwm_plat_data) {
216 dev_err(&pdev->dev, "Failure to alloc pdata for regulators.\n");
220 np = of_node_get(pdev->dev.of_node);
221 regulators = of_find_node_by_name(np, "regulators");
223 dev_err(&pdev->dev, "regulator node not found\n");
226 count = ARRAY_SIZE(pwm_matches);
227 matches = pwm_matches;
228 ret = of_regulator_match(&pdev->dev, regulators, matches, count);
229 of_node_put(regulators);
231 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
236 pwm_plat_data->num_regulators = count;
237 *pwm_reg_matches = matches;
239 for (idx = 0; idx < count; idx++) {
240 if (!matches[idx].init_data || !matches[idx].of_node)
242 pwm_plat_data->pwm_init_data[idx] = matches[idx].init_data;
243 pwm_plat_data->of_node[idx] = matches[idx].of_node;
246 init_vol = of_get_property(np, "rockchip,pwm_voltage", NULL);
248 pwm_plat_data->pwm_init_vol = be32_to_cpu(*init_vol);
250 max_vol = of_get_property(np, "rockchip,pwm_max_voltage", NULL);
252 pwm_plat_data->pwm_max_vol = be32_to_cpu(*max_vol);
254 min_vol = of_get_property(np, "rockchip,pwm_min_voltage", NULL);
256 pwm_plat_data->pwm_min_vol = be32_to_cpu(*min_vol);
258 suspend_vol = of_get_property(np, "rockchip,pwm_suspend_voltage", NULL);
260 pwm_plat_data->pwm_suspend_vol = be32_to_cpu(*suspend_vol);
262 coefficient = of_get_property(np, "rockchip,pwm_coefficient", NULL);
264 pwm_plat_data->pwm_coefficient = be32_to_cpu(*coefficient);
266 id = of_get_property(np, "rockchip,pwm_id", NULL);
268 pwm_plat_data->pwm_id = be32_to_cpu(*id);
270 prop = of_find_property(np, "rockchip,pwm_voltage_map", &length);
273 pwm_plat_data->pwm_vol_map_count = length / sizeof(u32);
274 if (pwm_plat_data->pwm_vol_map_count > 0) {
275 size_t size = sizeof(*pwm_plat_data->pwm_voltage_map) * pwm_plat_data->pwm_vol_map_count;
277 pwm_plat_data->pwm_voltage_map = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
278 if (!pwm_plat_data->pwm_voltage_map)
280 ret = of_property_read_u32_array(np, "rockchip,pwm_voltage_map",
281 pwm_plat_data->pwm_voltage_map, pwm_plat_data->pwm_vol_map_count);
283 printk("pwm voltage map not specified\n");
285 return pwm_plat_data;
288 static inline struct pwm_regulator_board *pwm_regulator_parse_dt(
289 struct platform_device *pdev,
290 struct of_regulator_match **pwm_reg_matches)
297 static int __init pwm_regulator_probe(struct platform_device *pdev)
299 struct pwm_platform_data *pdata = pdev->dev.platform_data;
300 struct pwm_regulator_board *pwm_pdev;
301 struct of_regulator_match *pwm_reg_matches = NULL;
302 struct regulator_init_data *reg_data;
303 struct regulator_config config = { };
304 const char *rail_name = NULL;
305 struct regulator_dev *pwm_rdev;
307 struct regulator *dc;
309 pwm_pdev = devm_kzalloc(&pdev->dev, sizeof(*pwm_pdev),
311 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
314 if (pdev->dev.of_node)
315 pwm_pdev = pwm_regulator_parse_dt(pdev, &pwm_reg_matches);
318 dev_err(&pdev->dev, "Platform data not found\n");
322 if (!pwm_pdev->pwm_init_vol)
323 pdata->pwm_voltage = 1100000; /* default 1.1v*/
325 pdata->pwm_voltage = pwm_pdev->pwm_init_vol;
327 if (!pwm_pdev->pwm_max_vol)
328 pdata->max_uV = 1400000;
330 pdata->max_uV = pwm_pdev->pwm_max_vol;
332 if (!pwm_pdev->pwm_min_vol)
333 pdata->min_uV = 1000000;
335 pdata->min_uV = pwm_pdev->pwm_min_vol;
337 if (pwm_pdev->pwm_suspend_vol < pwm_pdev->pwm_min_vol)
338 pdata->suspend_voltage = pwm_pdev->pwm_min_vol;
339 else if (pwm_pdev->pwm_suspend_vol > pwm_pdev->pwm_max_vol)
340 pdata->suspend_voltage = pwm_pdev->pwm_max_vol;
342 pdata->suspend_voltage = pwm_pdev->pwm_suspend_vol;
344 pdata->pwm_voltage_map = pwm_pdev->pwm_voltage_map;
345 pdata->pwm_id = pwm_pdev->pwm_id;
346 pdata->coefficient = pwm_pdev->pwm_coefficient;
347 pdata->pwm_vol_map_count = pwm_pdev->pwm_vol_map_count;
349 pdata->pwm = devm_pwm_get(&pdev->dev, NULL);
350 if (IS_ERR(pdata->pwm)) {
351 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
353 pdata->pwm = pwm_request(pdata->pwm_id, "pwm-regulator");
354 if (IS_ERR(pdata->pwm)) {
355 dev_err(&pdev->dev, "unable to request legacy PWM\n");
356 ret = PTR_ERR(pdata->pwm);
360 if (pdata->pwm_period_ns > 0)
361 pwm_set_period(pdata->pwm, pdata->pwm_period_ns);
363 pdata->period = pwm_get_period(pdata->pwm);
365 mutex_init(&pdata->mutex_pwm);
368 pdata->num_regulators = pwm_pdev->num_regulators;
369 pdata->rdev = kcalloc(pdata->num_regulators, sizeof(struct regulator_dev *), GFP_KERNEL);
373 /* Instantiate the regulators */
374 for (i = 0; i < pdata->num_regulators; i++) {
375 reg_data = pwm_pdev->pwm_init_data[i];
378 config.dev = &pdev->dev;
379 config.driver_data = pdata;
380 if (&pdev->dev.of_node)
381 config.of_node = pwm_pdev->of_node[i];
382 if (reg_data && reg_data->constraints.name)
383 rail_name = reg_data->constraints.name;
385 rail_name = regulators[i].name;
386 reg_data->supply_regulator = rail_name;
388 config.init_data = reg_data;
390 pwm_rdev = regulator_register(®ulators[i], &config);
391 if (IS_ERR(pwm_rdev)) {
392 printk("failed to register %d regulator\n", i);
395 pdata->rdev[i] = pwm_rdev;
397 /*********set pwm vol by defult***********/
398 dc = regulator_get(NULL, rail_name);
399 regulator_set_voltage(dc, pdata->pwm_voltage, pdata->pwm_voltage);
401 /**************************************/
406 printk("%s:error\n", __func__);
411 void pwm_suspend_voltage(void)
416 void pwm_resume_voltage(void)
421 static int pwm_regulator_remove(struct platform_device *pdev)
423 struct pwm_platform_data *pdata = pdev->dev.platform_data;
426 for (i = 0; i < pdata->num_regulators; i++)
428 regulator_unregister(pdata->rdev[i]);
433 static struct platform_driver pwm_regulator_driver = {
435 .name = "pwm-voltage-regulator",
436 .of_match_table = of_match_ptr(pwm_of_match),
438 .remove = pwm_regulator_remove,
442 static int __init pwm_regulator_module_init(void)
444 return platform_driver_probe(&pwm_regulator_driver, pwm_regulator_probe);
447 static void __exit pwm_regulator_module_exit(void)
449 platform_driver_unregister(&pwm_regulator_driver);
452 /*fs_initcall(pwm_regulator_module_init);*/
453 module_init(pwm_regulator_module_init);
454 module_exit(pwm_regulator_module_exit);
455 MODULE_LICENSE("GPL");