3 * Copyright (C) 2013 ROCKCHIP, Inc.
\r
5 * This software is licensed under the terms of the GNU General Public
\r
6 * License version 2, as published by the Free Software Foundation, and
\r
7 * may be copied, distributed, and modified under those terms.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
15 #include <linux/bug.h>
\r
16 #include <linux/err.h>
\r
17 #include <linux/platform_device.h>
\r
18 #include <linux/kernel.h>
\r
19 #include <linux/clk.h>
\r
21 #include <linux/delay.h>
\r
22 #include <linux/slab.h>
\r
23 #include <linux/regulator/rockchip-pwm-regulator.h>
\r
24 #include <linux/gpio.h>
\r
25 #include <linux/of_gpio.h>
\r
26 #include <linux/module.h>
\r
27 #include <linux/init.h>
\r
28 #include <linux/err.h>
\r
29 #include <linux/pwm.h>
\r
31 #include <linux/of.h>
\r
32 #include <linux/of_device.h>
\r
33 #include <linux/regulator/of_regulator.h>
\r
34 #include <linux/regulator/driver.h>
\r
35 #include <linux/regulator/machine.h>
\r
39 #define DBG(x...) printk(KERN_INFO x)
\r
44 const static int pwm_voltage_map[] = {
\r
45 925000 ,950000, 975000,1000000, 1025000, 1050000, 1075000, 1100000, 1125000, 1150000, 1175000, 1200000, 1225000, 1250000, 1275000, 1300000, 1325000, 1350000,1375000,1400000
\r
48 static struct pwm_platform_data *g_pdata;
\r
50 static int pwm_set_rate(struct pwm_platform_data *pdata,u32 rate)
\r
53 DBG("%s:id=%d,rate=%d clkrate =%d\n",__func__,pdata->pwm_id,rate,pdata->period);
\r
55 duty_cycle = (rate * (pdata->period ) / 100) ;
\r
57 pwm_config(pdata->pwm, duty_cycle, pdata->period);
\r
59 pwm_enable(pdata->pwm);
\r
64 static int pwm_regulator_list_voltage(struct regulator_dev *dev,unsigned int index)
\r
66 if (index <( g_pdata->pwm_vol_map_count +1))
\r
67 return g_pdata->pwm_voltage_map[index];
\r
72 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
\r
74 DBG("Enter %s\n",__FUNCTION__);
\r
78 static int pwm_regulator_enable(struct regulator_dev *dev)
\r
80 DBG("Enter %s\n",__FUNCTION__);
\r
84 static int pwm_regulator_disable(struct regulator_dev *dev)
\r
86 DBG("Enter %s\n",__FUNCTION__);
\r
90 static int pwm_regulator_get_voltage(struct regulator_dev *dev)
\r
94 DBG("Enter %s\n",__FUNCTION__);
\r
96 vol = g_pdata->pwm_voltage;
\r
101 static int pwm_regulator_set_voltage(struct regulator_dev *dev,
\r
102 int min_uV, int max_uV, unsigned *selector)
\r
104 const int *voltage_map = g_pdata->pwm_voltage_map;
\r
105 int max = g_pdata->max_uV;
\r
106 int coefficient = g_pdata->coefficient;
\r
107 u32 size = g_pdata->pwm_vol_map_count;
\r
108 u32 i, vol,pwm_value;
\r
110 DBG("%s: min_uV = %d, max_uV = %d\n",__FUNCTION__, min_uV,max_uV);
\r
112 if (min_uV < voltage_map[0] ||max_uV > voltage_map[size-1])
\r
114 printk("%s: voltage_map voltage is out of table\n",__func__);
\r
118 for (i = 0; i < size; i++)
\r
120 if (voltage_map[i] >= min_uV)
\r
123 vol = voltage_map[i];
\r
124 g_pdata->pwm_voltage = vol;
\r
126 // VDD12 = 1.40 - 0.455*D , ÆäÖÐDΪPWMÕ¼¿Õ±È,
\r
127 pwm_value = (max-vol)/coefficient/10; // pwm_value %, coefficient *1000
\r
129 if (pwm_set_rate(g_pdata,pwm_value)!=0)
\r
131 printk("%s:fail to set pwm rate,pwm_value=%d\n",__func__,pwm_value);
\r
137 DBG("%s:ok,vol=%d,pwm_value=%d %d\n",__FUNCTION__,vol,pwm_value,g_pdata->pwm_voltage);
\r
143 static struct regulator_ops pwm_voltage_ops = {
\r
144 .list_voltage = pwm_regulator_list_voltage,
\r
145 .set_voltage =pwm_regulator_set_voltage,
\r
146 .get_voltage = pwm_regulator_get_voltage,
\r
147 .enable = pwm_regulator_enable,
\r
148 .disable = pwm_regulator_disable,
\r
149 .is_enabled = pwm_regulator_is_enabled,
\r
151 static struct regulator_desc regulators[] = {
\r
154 .name = "PWM_DCDC1",
\r
156 .ops = &pwm_voltage_ops,
\r
157 .n_voltages = ARRAY_SIZE(pwm_voltage_map),
\r
158 .type = REGULATOR_VOLTAGE,
\r
159 .owner = THIS_MODULE,
\r
164 static struct of_device_id pwm_of_match[] = {
\r
165 { .compatible = "rockchip_pwm_regulator"},
\r
168 MODULE_DEVICE_TABLE(of, pwm_of_match);
\r
170 static struct of_regulator_match pwm_matches[] = {
\r
171 { .name = "pwm_dcdc1", .driver_data = (void *)0 },
\r
174 static struct pwm_regulator_board *pwm_regulator_parse_dt(
\r
175 struct platform_device *pdev,
\r
176 struct of_regulator_match **pwm_reg_matches)
\r
178 struct pwm_regulator_board *pwm_plat_data;
\r
179 struct device_node *np, *regulators;
\r
180 struct of_regulator_match *matches;
\r
181 int idx = 0, ret, count;
\r
182 struct property *prop;
\r
184 const __be32 *init_vol, *max_vol,*min_vol,*suspend_vol,*coefficient,*id;
\r
186 DBG("%s,line=%d\n", __func__,__LINE__);
\r
188 pwm_plat_data = devm_kzalloc(&pdev->dev, sizeof(*pwm_plat_data),
\r
191 if (!pwm_plat_data) {
\r
192 dev_err(&pdev->dev, "Failure to alloc pdata for regulators.\n");
\r
196 np = of_node_get(pdev->dev.of_node);
\r
197 regulators = of_find_node_by_name(np, "regulators");
\r
199 dev_err(&pdev->dev, "regulator node not found\n");
\r
202 count = ARRAY_SIZE(pwm_matches);
\r
203 matches = pwm_matches;
\r
204 ret = of_regulator_match(&pdev->dev, regulators, matches, count);
\r
205 of_node_put(regulators);
\r
207 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
\r
212 pwm_plat_data ->num_regulators = count;
\r
213 *pwm_reg_matches = matches;
\r
215 for (idx = 0; idx < count; idx++) {
\r
216 if (!matches[idx].init_data || !matches[idx].of_node)
\r
218 pwm_plat_data->pwm_init_data[idx] =matches[idx].init_data;
\r
219 pwm_plat_data->of_node[idx]=matches[idx].of_node;
\r
222 init_vol = of_get_property(np, "rockchip,pwm_voltage", NULL);
\r
224 pwm_plat_data->pwm_init_vol = be32_to_cpu(*init_vol);
\r
226 max_vol = of_get_property(np, "rockchip,pwm_max_voltage", NULL);
\r
228 pwm_plat_data->pwm_max_vol = be32_to_cpu(*max_vol);
\r
230 min_vol = of_get_property(np, "rockchip,pwm_min_voltage", NULL);
\r
232 pwm_plat_data->pwm_min_vol = be32_to_cpu(*min_vol);
\r
234 suspend_vol = of_get_property(np, "rockchip,pwm_suspend_voltage", NULL);
\r
236 pwm_plat_data->pwm_suspend_vol = be32_to_cpu(*suspend_vol);
\r
238 coefficient = of_get_property(np, "rockchip,pwm_coefficient", NULL);
\r
240 pwm_plat_data->pwm_coefficient = be32_to_cpu(*coefficient );
\r
242 id = of_get_property(np, "rockchip,pwm_id", NULL);
\r
244 pwm_plat_data->pwm_id = be32_to_cpu(*id );
\r
246 prop = of_find_property(np, "rockchip,pwm_voltage_map", &length);
\r
249 pwm_plat_data->pwm_vol_map_count = length / sizeof(u32);
\r
250 if (pwm_plat_data->pwm_vol_map_count > 0) {
\r
251 size_t size = sizeof(*pwm_plat_data->pwm_voltage_map) * pwm_plat_data->pwm_vol_map_count;
\r
253 pwm_plat_data->pwm_voltage_map = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
\r
254 if (! pwm_plat_data->pwm_voltage_map)
\r
256 ret = of_property_read_u32_array(np, "rockchip,pwm_voltage_map",pwm_plat_data->pwm_voltage_map, pwm_plat_data->pwm_vol_map_count);
\r
258 printk("pwm voltage map not specified\n");
\r
260 return pwm_plat_data;
\r
263 static inline struct pwm_regulator_board *pwm_regulator_parse_dt(
\r
264 struct platform_device *pdev,
\r
265 struct of_regulator_match **pwm_reg_matches)
\r
272 static int __init pwm_regulator_probe(struct platform_device *pdev)
\r
274 struct pwm_platform_data *pdata = pdev->dev.platform_data;
\r
275 struct pwm_regulator_board *pwm_pdev;
\r
276 struct of_regulator_match *pwm_reg_matches = NULL;
\r
277 struct regulator_init_data *reg_data;
\r
278 struct regulator_config config = { };
\r
279 const char *rail_name = NULL;
\r
280 struct regulator_dev *pwm_rdev;
\r
282 struct regulator *dc;
\r
284 pwm_pdev = devm_kzalloc(&pdev->dev, sizeof(*pwm_pdev),
\r
286 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
\r
289 if (pdev->dev.of_node)
\r
290 pwm_pdev = pwm_regulator_parse_dt(pdev,&pwm_reg_matches);
\r
293 dev_err(&pdev->dev, "Platform data not found\n");
\r
297 if (!pwm_pdev->pwm_init_vol)
\r
298 pdata->pwm_voltage = 1100000; // default 1.1v
\r
300 pdata->pwm_voltage = pwm_pdev->pwm_init_vol;
\r
302 if(!pwm_pdev->pwm_max_vol)
\r
303 pdata->max_uV = 1400000;
\r
305 pdata->max_uV = pwm_pdev->pwm_max_vol;
\r
307 if(!pwm_pdev->pwm_min_vol)
\r
308 pdata->min_uV = 1000000;
\r
310 pdata->min_uV = pwm_pdev->pwm_min_vol;
\r
312 if(pwm_pdev->pwm_suspend_vol < pwm_pdev->pwm_min_vol)
\r
313 pdata->suspend_voltage = pwm_pdev->pwm_min_vol;
\r
314 else if(pwm_pdev->pwm_suspend_vol > pwm_pdev->pwm_max_vol)
\r
315 pdata->suspend_voltage = pwm_pdev->pwm_max_vol;
\r
317 pdata->suspend_voltage = pwm_pdev->pwm_suspend_vol;
\r
319 pdata->pwm_voltage_map = pwm_pdev->pwm_voltage_map;
\r
320 pdata->pwm_id = pwm_pdev->pwm_id;
\r
321 pdata->coefficient = pwm_pdev->pwm_coefficient;
\r
322 pdata->pwm_vol_map_count = pwm_pdev->pwm_vol_map_count;
\r
324 pdata->pwm = devm_pwm_get(&pdev->dev, NULL);
\r
325 if (IS_ERR(pdata->pwm)) {
\r
326 dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
\r
328 pdata->pwm = pwm_request(pdata->pwm_id, "pwm-regulator");
\r
329 if (IS_ERR(pdata->pwm)) {
\r
330 dev_err(&pdev->dev, "unable to request legacy PWM\n");
\r
331 ret = PTR_ERR(pdata->pwm);
\r
335 if (pdata->pwm_period_ns > 0)
\r
336 pwm_set_period(pdata->pwm, pdata->pwm_period_ns);
\r
338 pdata->period = pwm_get_period(pdata->pwm);
\r
343 pdata->num_regulators = pwm_pdev ->num_regulators;
\r
344 pdata->rdev = kcalloc(pdata->num_regulators,sizeof(struct regulator_dev *), GFP_KERNEL);
\r
345 if (!pdata->rdev) {
\r
348 /* Instantiate the regulators */
\r
349 for (i = 0; i < pdata->num_regulators; i++) {
\r
350 reg_data = pwm_pdev->pwm_init_data[i];
\r
353 config.dev = &pdev->dev;
\r
354 config.driver_data = pdata;
\r
355 if (&pdev->dev.of_node)
\r
356 config.of_node = pwm_pdev->of_node[i];
\r
357 if (reg_data && reg_data->constraints.name)
\r
358 rail_name = reg_data->constraints.name;
\r
360 rail_name = regulators[i].name;
\r
361 reg_data->supply_regulator = rail_name;
\r
363 config.init_data =reg_data;
\r
365 pwm_rdev = regulator_register(®ulators[i],&config);
\r
366 if (IS_ERR(pwm_rdev)) {
\r
367 printk("failed to register %d regulator\n",i);
\r
370 pdata->rdev[i] = pwm_rdev;
\r
372 /*********set pwm vol by defult***********/
\r
373 dc = regulator_get(NULL,rail_name);
\r
374 regulator_set_voltage(dc,pdata->pwm_voltage,pdata->pwm_voltage);
\r
376 /**************************************/
\r
381 printk("%s:error\n",__func__);
\r
386 void pwm_suspend_voltage(void)
\r
391 void pwm_resume_voltage(void)
\r
396 static int pwm_regulator_remove(struct platform_device *pdev)
\r
398 struct pwm_platform_data *pdata = pdev->dev.platform_data;
\r
401 for (i = 0; i < pdata->num_regulators; i++)
\r
402 if (pdata->rdev[i])
\r
403 regulator_unregister(pdata->rdev[i]);
\r
404 kfree(pdata->rdev);
\r
408 static struct platform_driver pwm_regulator_driver = {
\r
410 .name = "pwm-voltage-regulator",
\r
411 .of_match_table =of_match_ptr(pwm_of_match),
\r
413 .remove = pwm_regulator_remove,
\r
417 static int __init pwm_regulator_module_init(void)
\r
419 return platform_driver_probe(&pwm_regulator_driver, pwm_regulator_probe);
\r
422 static void __exit pwm_regulator_module_exit(void)
\r
424 platform_driver_unregister(&pwm_regulator_driver);
\r
427 //fs_initcall(pwm_regulator_module_init);
\r
428 module_init(pwm_regulator_module_init);
\r
429 module_exit(pwm_regulator_module_exit);
\r
430 MODULE_LICENSE("GPL");
\r