Merge remote-tracking branch 'kernel-2.6.32/develop' into develop-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / video / backlight / rk29_backlight.c
1 /* drivers/video/backlight/rk29_backlight.c
2  *
3  * Copyright (C) 2009-2011 Rockchip Corporation.
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
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
21 #include <linux/platform_device.h>
22 #include <linux/backlight.h>
23 #include <linux/fb.h>
24 #include <linux/clk.h>
25
26 #include <linux/earlysuspend.h>
27 #include <asm/io.h>
28 #include <mach/rk29_iomap.h>
29 #include <mach/board.h>
30
31 #include "rk2818_backlight.h"
32
33 /*
34  * Debug
35  */
36 #if 0
37 #define DBG(x...)       printk(KERN_INFO x)
38 #else
39 #define DBG(x...)
40 #endif
41
42
43 #define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK29_PWM_BASE+id*0x10))
44 #define read_pwm_reg(id, addr)              __raw_readl(addr+(RK29_PWM_BASE+id*0x10))    
45
46 static struct clk *pwm_clk;
47 static struct backlight_device *rk29_bl;
48 static int suspend_flag = 0;
49
50 static int rk29_bl_update_status(struct backlight_device *bl)
51 {
52         u32 divh,div_total;
53         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
54         u32 id = rk29_bl_info->pwm_id;
55         u32 ref = rk29_bl_info->bl_ref;
56
57         if (suspend_flag)
58                 return 0;
59
60         if (bl->props.brightness < rk29_bl_info->min_brightness)        /*avoid can't view screen when close backlight*/
61                 bl->props.brightness = rk29_bl_info->min_brightness;
62
63         div_total = read_pwm_reg(id, PWM_REG_LRC);
64         if (ref) {
65                 divh = div_total*(bl->props.brightness)/BL_STEP;
66         } else {
67                 divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
68         }
69         write_pwm_reg(id, PWM_REG_HRC, divh);
70
71         DBG(">>>%s-->%d brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
72         return 0;
73 }
74
75 static int rk29_bl_get_brightness(struct backlight_device *bl)
76 {
77         u32 divh,div_total;
78         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
79         u32 id = rk29_bl_info->pwm_id;
80         u32 ref = rk29_bl_info->bl_ref;
81
82         div_total = read_pwm_reg(id, PWM_REG_LRC);
83         divh = read_pwm_reg(id, PWM_REG_HRC);
84
85         if (!div_total)
86                 return 0;
87
88         if (ref) {
89                 return BL_STEP*divh/div_total;
90         } else {
91                 return BL_STEP-(BL_STEP*divh/div_total);
92         }
93 }
94
95 static struct backlight_ops rk29_bl_ops = {
96         .update_status  = rk29_bl_update_status,
97         .get_brightness = rk29_bl_get_brightness,
98 };
99
100 static void rk29_backlight_work_func(struct work_struct *work)
101 {
102         suspend_flag = 0;
103         rk29_bl_update_status(rk29_bl);
104 }
105 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
106
107 #ifdef CONFIG_HAS_EARLYSUSPEND
108 static void rk29_bl_suspend(struct early_suspend *h)
109 {
110         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
111         int brightness = rk29_bl->props.brightness;
112
113         cancel_delayed_work_sync(&rk29_backlight_work);
114
115         if (rk29_bl->props.brightness) {
116                 rk29_bl->props.brightness = 0;
117                 rk29_bl_update_status(rk29_bl);
118                 rk29_bl->props.brightness = brightness;
119         }
120
121         if (!suspend_flag) {
122                 clk_disable(pwm_clk);
123                 if (rk29_bl_info->pwm_suspend)
124                         rk29_bl_info->pwm_suspend();
125         }
126
127         suspend_flag = 1;
128 }
129
130 static void rk29_bl_resume(struct early_suspend *h)
131 {
132         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
133         DBG("%s : %s\n", __FILE__, __FUNCTION__);
134
135         if (rk29_bl_info->pwm_resume)
136                 rk29_bl_info->pwm_resume();
137
138         clk_enable(pwm_clk);
139
140         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
141 }
142
143 static struct early_suspend bl_early_suspend = {
144         .suspend = rk29_bl_suspend,
145         .resume = rk29_bl_resume,
146         .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
147 };
148 #endif
149 void rk29_backlight_set(bool on)
150 {
151         printk("%s: set %d\n", __func__, on);
152         if(on)
153                 rk29_bl_resume(NULL);
154         else
155                 rk29_bl_suspend(NULL);
156         return;
157 }
158 EXPORT_SYMBOL(rk29_backlight_set);
159
160 static int rk29_backlight_probe(struct platform_device *pdev)
161 {               
162         int ret = 0;
163         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
164         u32 id  =  rk29_bl_info->pwm_id;
165         u32 divh, div_total;
166         unsigned long pwm_clk_rate;
167         struct backlight_properties props;
168
169         if (rk29_bl) {
170                 DBG(KERN_CRIT "%s: backlight device register has existed \n",
171                                 __func__);
172                 return -EEXIST;         
173         }
174
175         if (!rk29_bl_info->delay_ms)
176                 rk29_bl_info->delay_ms = 100;
177
178         if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
179                 rk29_bl_info->min_brightness = 52;
180
181         if (rk29_bl_info && rk29_bl_info->io_init) {
182                 rk29_bl_info->io_init();
183         }
184
185         memset(&props, 0, sizeof(struct backlight_properties));
186         props.max_brightness = BL_STEP;
187         rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
188         if (!rk29_bl) {
189                 DBG(KERN_CRIT "%s: backlight device register error\n",
190                                 __func__);
191                 return -ENODEV;         
192         }
193
194         pwm_clk = clk_get(NULL, "pwm");
195         if (IS_ERR(pwm_clk)) {
196                 printk(KERN_ERR "failed to get pwm clock source\n");
197                 return -ENODEV; 
198         }
199         pwm_clk_rate = clk_get_rate(pwm_clk);
200         div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
201
202         div_total >>= (1 + (PWM_DIV >> 9));
203         div_total = (div_total) ? div_total : 1;
204
205         if(rk29_bl_info->bl_ref) {
206                 divh = 0;
207         } else {
208                 divh = div_total;
209         }
210
211         clk_enable(pwm_clk);
212         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
213         write_pwm_reg(id, PWM_REG_LRC, div_total);
214         write_pwm_reg(id, PWM_REG_HRC, divh);
215         write_pwm_reg(id, PWM_REG_CNTR, 0x0);
216         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
217
218         rk29_bl->props.power = FB_BLANK_UNBLANK;
219         rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
220         rk29_bl->props.brightness = BL_STEP / 2;
221
222         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
223
224         register_early_suspend(&bl_early_suspend);
225
226         printk("RK29 Backlight Driver Initialized.\n");
227         return ret;
228 }
229
230 static int rk29_backlight_remove(struct platform_device *pdev)
231 {               
232         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
233
234         if (rk29_bl) {
235                 backlight_device_unregister(rk29_bl);
236                 unregister_early_suspend(&bl_early_suspend);
237                 clk_disable(pwm_clk);
238                 clk_put(pwm_clk);
239                 if (rk29_bl_info && rk29_bl_info->io_deinit) {
240                         rk29_bl_info->io_deinit();
241                 }
242                 return 0;
243         } else {
244                 DBG(KERN_CRIT "%s: no backlight device has registered\n",
245                                 __func__);
246                 return -ENODEV;
247         }
248 }
249
250 static void rk29_backlight_shutdown(struct platform_device *pdev)
251 {
252         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
253
254         rk29_bl->props.brightness >>= 1;
255         rk29_bl_update_status(rk29_bl);
256         mdelay(100);
257
258         rk29_bl->props.brightness >>= 1;
259         rk29_bl_update_status(rk29_bl);
260         mdelay(100);
261
262         rk29_bl->props.brightness = 0;
263         rk29_bl_update_status(rk29_bl);
264
265         if (rk29_bl_info && rk29_bl_info->io_deinit)
266                 rk29_bl_info->io_deinit();
267 }
268
269 static struct platform_driver rk29_backlight_driver = {
270         .probe  = rk29_backlight_probe,
271         .remove = rk29_backlight_remove,
272         .driver = {
273                 .name   = "rk29_backlight",
274                 .owner  = THIS_MODULE,
275         },
276         .shutdown       = rk29_backlight_shutdown,
277 };
278
279 static int __init rk29_backlight_init(void)
280 {
281         platform_driver_register(&rk29_backlight_driver);
282         return 0;
283 }
284 fs_initcall_sync(rk29_backlight_init);