drm: add 10bit support for yuv format
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / rockchip-pwm-regulator.c
1 /*
2  *
3  * Copyright (C) 2013 ROCKCHIP, Inc.
4  *
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.
8  *
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.
13  *
14  */
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>
20 #include <asm/io.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>
30
31 #include <linux/of.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>
36
37
38 #if 0
39 #define DBG(x...)       printk(KERN_INFO x)
40 #else
41 #define DBG(x...)
42 #endif
43
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
49 };
50
51 static int pwm_set_rate(struct pwm_platform_data *pdata, u32 rate)
52 {
53         int duty_cycle;
54         DBG("%s:id=%d,rate=%d clkrate =%d\n", __func__, pdata->pwm_id, rate, pdata->period);
55
56         duty_cycle = (rate * (pdata->period) / 100) ;
57
58         pwm_config(pdata->pwm, duty_cycle, pdata->period);
59
60         pwm_enable(pdata->pwm);
61
62         return 0;
63 }
64
65 static int pwm_regulator_list_voltage(struct regulator_dev *dev, unsigned int index)
66 {
67         struct pwm_platform_data *pdata = rdev_get_drvdata(dev);
68
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];
73         else
74                 return -1;
75 }
76
77 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
78 {
79         DBG("Enter %s\n", __func__);
80         return 0;
81 }
82
83 static int pwm_regulator_enable(struct regulator_dev *dev)
84 {
85         DBG("Enter %s\n", __func__);
86         return 0;
87 }
88
89 static int pwm_regulator_disable(struct regulator_dev *dev)
90 {
91         DBG("Enter %s\n", __func__);
92         return 0;
93 }
94
95 static int pwm_regulator_get_voltage(struct regulator_dev *dev)
96 {
97         u32 vol;
98         struct pwm_platform_data *pdata = rdev_get_drvdata(dev);
99
100         DBG("%s:line=%d,pdata=%p,pwm_id=%d\n", __func__, __LINE__,
101                 pdata, pdata->pwm_id);
102         vol = pdata->pwm_voltage;
103
104         return vol;
105 }
106
107 static int pwm_regulator_set_voltage(struct regulator_dev *dev,
108                 int min_uV, int max_uV, unsigned *selector)
109 {
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;
116
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);
121
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);
125                 return -EINVAL;
126         }
127
128         for (i = 0; i < size; i++) {
129                 if (voltage_map[i] >= min_uV)
130                         break;
131         }
132         vol =  voltage_map[i];
133         pdata->pwm_voltage = vol;
134
135         /* VDD12 = 1.40 - 0.455*D , DΪPWMÕ¼¿Õ±È*/
136         pwm_value = (max-vol) / coefficient / 10;
137         /*pwm_value %, coefficient *1000*/
138
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);
142                 return -1;
143
144         }
145         *selector = i;
146
147         mutex_unlock(&pdata->mutex_pwm);
148
149         DBG("%s:ok,vol=%d,pwm_value=%d %d\n", __func__, vol,
150                 pwm_value, pdata->pwm_voltage);
151
152         return 0;
153
154 }
155
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,
163 };
164 static struct regulator_desc regulators[] = {
165
166         {
167                 .name = "PWM_DCDC1",
168                 .id = 0,
169                 .ops = &pwm_voltage_ops,
170                 .n_voltages =  ARRAY_SIZE(pwm_voltage_map),
171                 .type = REGULATOR_VOLTAGE,
172                 .owner = THIS_MODULE,
173         },
174
175         {
176                 .name = "PWM_DCDC2",
177                 .id = 1,
178                 .ops = &pwm_voltage_ops,
179                 .n_voltages =  ARRAY_SIZE(pwm_voltage_map),
180                 .type = REGULATOR_VOLTAGE,
181                 .owner = THIS_MODULE,
182         },
183
184 };
185
186 #ifdef CONFIG_OF
187 static struct of_device_id pwm_of_match[] = {
188         { .compatible = "rockchip_pwm_regulator"},
189         { },
190 };
191 MODULE_DEVICE_TABLE(of, pwm_of_match);
192
193 static struct of_regulator_match pwm_matches[] = {
194         { .name = "pwm_dcdc1",  .driver_data = (void *)0  },
195         { .name = "pwm_dcdc2",  .driver_data = (void *)1  },
196 };
197
198 static struct pwm_regulator_board *pwm_regulator_parse_dt(
199                 struct platform_device *pdev,
200                 struct of_regulator_match **pwm_reg_matches)
201 {
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;
207         int length;
208         const __be32 *init_vol, *max_vol, *min_vol, *suspend_vol, *coefficient, *id;
209
210         DBG("%s,line=%d\n", __func__, __LINE__);
211
212         pwm_plat_data = devm_kzalloc(&pdev->dev, sizeof(*pwm_plat_data),
213                                         GFP_KERNEL);
214
215         if (!pwm_plat_data) {
216                 dev_err(&pdev->dev, "Failure to alloc pdata for regulators.\n");
217                 return NULL;
218         }
219
220         np = of_node_get(pdev->dev.of_node);
221         regulators = of_find_node_by_name(np, "regulators");
222         if (!regulators) {
223                 dev_err(&pdev->dev, "regulator node not found\n");
224                 return NULL;
225         }
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);
230         if (ret < 0) {
231                 dev_err(&pdev->dev, "Error parsing regulator init data: %d\n",
232                         ret);
233                 return NULL;
234         }
235
236         pwm_plat_data->num_regulators = count;
237         *pwm_reg_matches = matches;
238
239         for (idx = 0; idx < count; idx++) {
240                 if (!matches[idx].init_data || !matches[idx].of_node)
241                         continue;
242                 pwm_plat_data->pwm_init_data[idx] = matches[idx].init_data;
243                 pwm_plat_data->of_node[idx] = matches[idx].of_node;
244         }
245
246         init_vol = of_get_property(np, "rockchip,pwm_voltage", NULL);
247         if (init_vol)
248                 pwm_plat_data->pwm_init_vol = be32_to_cpu(*init_vol);
249
250         max_vol = of_get_property(np, "rockchip,pwm_max_voltage", NULL);
251         if (max_vol)
252                 pwm_plat_data->pwm_max_vol = be32_to_cpu(*max_vol);
253
254         min_vol = of_get_property(np, "rockchip,pwm_min_voltage", NULL);
255         if (min_vol)
256                 pwm_plat_data->pwm_min_vol = be32_to_cpu(*min_vol);
257
258         suspend_vol = of_get_property(np, "rockchip,pwm_suspend_voltage", NULL);
259         if (suspend_vol)
260                 pwm_plat_data->pwm_suspend_vol = be32_to_cpu(*suspend_vol);
261
262         coefficient  = of_get_property(np, "rockchip,pwm_coefficient", NULL);
263         if (coefficient)
264                 pwm_plat_data->pwm_coefficient = be32_to_cpu(*coefficient);
265
266         id  = of_get_property(np, "rockchip,pwm_id", NULL);
267         if (id)
268                 pwm_plat_data->pwm_id = be32_to_cpu(*id);
269
270         prop = of_find_property(np, "rockchip,pwm_voltage_map", &length);
271         if (!prop)
272                 return NULL;
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;
276
277         pwm_plat_data->pwm_voltage_map = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
278         if (!pwm_plat_data->pwm_voltage_map)
279                 return NULL;
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);
282         if (ret < 0)
283                 printk("pwm voltage map not specified\n");
284         }
285         return pwm_plat_data;
286 }
287 #else
288 static inline struct pwm_regulator_board *pwm_regulator_parse_dt(
289                         struct platform_device *pdev,
290                         struct of_regulator_match **pwm_reg_matches)
291 {
292         return NULL;
293 }
294 #endif
295
296
297 static int __init pwm_regulator_probe(struct platform_device *pdev)
298 {
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;
306         int ret, i = 0;
307         struct regulator *dc;
308
309         pwm_pdev = devm_kzalloc(&pdev->dev, sizeof(*pwm_pdev),
310                                         GFP_KERNEL);
311         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata),
312                                         GFP_KERNEL);
313
314         if (pdev->dev.of_node)
315                 pwm_pdev = pwm_regulator_parse_dt(pdev, &pwm_reg_matches);
316
317         if (!pwm_pdev) {
318                 dev_err(&pdev->dev, "Platform data not found\n");
319                 return -EINVAL;
320         }
321
322         if (!pwm_pdev->pwm_init_vol)
323                 pdata->pwm_voltage = 1100000;   /* default 1.1v*/
324         else
325                 pdata->pwm_voltage = pwm_pdev->pwm_init_vol;
326
327         if (!pwm_pdev->pwm_max_vol)
328                 pdata->max_uV = 1400000;
329         else
330                 pdata->max_uV = pwm_pdev->pwm_max_vol;
331
332         if (!pwm_pdev->pwm_min_vol)
333                 pdata->min_uV = 1000000;
334         else
335                 pdata->min_uV = pwm_pdev->pwm_min_vol;
336
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;
341         else
342                 pdata->suspend_voltage = pwm_pdev->pwm_suspend_vol;
343
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;
348
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");
352
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);
357                         goto err;
358                 }
359         }
360         if (pdata->pwm_period_ns > 0)
361                 pwm_set_period(pdata->pwm, pdata->pwm_period_ns);
362
363         pdata->period = pwm_get_period(pdata->pwm);
364
365         mutex_init(&pdata->mutex_pwm);
366
367         if (pwm_pdev) {
368                 pdata->num_regulators = pwm_pdev->num_regulators;
369                 pdata->rdev = kcalloc(pdata->num_regulators, sizeof(struct regulator_dev *), GFP_KERNEL);
370                 if (!pdata->rdev) {
371                         return -ENOMEM;
372                 }
373                 /* Instantiate the regulators */
374                 for (i = 0; i < pdata->num_regulators; i++) {
375                 reg_data = pwm_pdev->pwm_init_data[i];
376                 if (!reg_data)
377                         continue;
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;
384                         else
385                                 rail_name = regulators[i].name;
386                         reg_data->supply_regulator = rail_name;
387
388                 config.init_data = reg_data;
389
390                 pwm_rdev = regulator_register(&regulators[i], &config);
391                 if (IS_ERR(pwm_rdev)) {
392                         printk("failed to register %d regulator\n", i);
393                 goto err;
394                 }
395                 pdata->rdev[i] = pwm_rdev;
396
397                 /*********set pwm vol by defult***********/
398                 dc = regulator_get(NULL, rail_name);
399                 regulator_set_voltage(dc, pdata->pwm_voltage, pdata->pwm_voltage);
400                 regulator_put(dc);
401                 /**************************************/
402                 }
403         }
404         return 0;
405 err:
406         printk("%s:error\n", __func__);
407         return ret;
408
409 }
410
411 void pwm_suspend_voltage(void)
412 {
413
414 }
415
416 void pwm_resume_voltage(void)
417 {
418
419 }
420
421 static int pwm_regulator_remove(struct platform_device *pdev)
422 {
423         struct pwm_platform_data *pdata = pdev->dev.platform_data;
424         int i;
425
426         for (i = 0; i < pdata->num_regulators; i++)
427                 if (pdata->rdev[i])
428                         regulator_unregister(pdata->rdev[i]);
429         kfree(pdata->rdev);
430         return 0;
431 }
432
433 static struct platform_driver pwm_regulator_driver = {
434         .driver = {
435                 .name = "pwm-voltage-regulator",
436                 .of_match_table = of_match_ptr(pwm_of_match),
437         },
438         .remove = pwm_regulator_remove,
439 };
440
441
442 static int __init pwm_regulator_module_init(void)
443 {
444         return platform_driver_probe(&pwm_regulator_driver, pwm_regulator_probe);
445 }
446
447 static void __exit pwm_regulator_module_exit(void)
448 {
449         platform_driver_unregister(&pwm_regulator_driver);
450 }
451
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");