backlight: add get_backlight_status api for fb backlight ctrl
[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/board.h>
29
30 #include "rk2818_backlight.h"
31
32 /*
33  * Debug
34  */
35 #if 0
36 #define DBG(x...)       printk(KERN_INFO x)
37 #else
38 #define DBG(x...)
39 #endif
40
41 #if defined(CONFIG_ARCH_RK30)
42 #define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK30_PWM01_BASE+(id>>1)*0x20000)+id*0x10)
43 #define read_pwm_reg(id, addr)              __raw_readl(addr+(RK30_PWM01_BASE+(id>>1)*0x20000+id*0x10))
44
45 #elif defined(CONFIG_ARCH_RK2928)
46 #define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK2928_PWM_BASE+id*0x10))
47 #define read_pwm_reg(id, addr)              __raw_readl(addr+(RK2928_PWM_BASE+id*0x10))
48
49 #elif defined(CONFIG_ARCH_RK29)
50 #define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK29_PWM_BASE+id*0x10))
51 #define read_pwm_reg(id, addr)              __raw_readl(addr+(RK29_PWM_BASE+id*0x10))    
52 #endif
53
54 static struct clk *pwm_clk;
55 static struct backlight_device *rk29_bl;
56 static int suspend_flag = 0;
57
58
59 int convertint(char s[])  
60 {  
61     int i;  
62     int n = 0;  
63     for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)  
64     {  
65         n = 10 * n + (s[i] - '0');  
66     }  
67     return n;  
68
69
70 static ssize_t backlight_write(struct device *dev,
71                 struct device_attribute *attr, char *buf)
72 {
73    
74         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
75         int number;
76
77         number = convertint(buf);
78         
79         rk29_bl_info->min_brightness=number;
80         return 0;
81 }
82
83
84 static ssize_t backlight_read(struct device *dev,
85                 struct device_attribute *attr, char *buf)
86 {
87         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
88
89         DBG("rk29_bl_info->min_brightness=%d\n",rk29_bl_info->min_brightness);
90 }
91 static DEVICE_ATTR(rk29backlight, 0660, backlight_read, backlight_write);
92
93 static DEFINE_MUTEX(backlight_mutex);
94
95 static int rk29_bl_update_status(struct backlight_device *bl)
96 {
97         u32 divh,div_total;
98         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
99         u32 id = rk29_bl_info->pwm_id;
100         u32 ref = rk29_bl_info->bl_ref;
101         int brightness = 0;
102         
103         mutex_lock(&backlight_mutex);
104 //BL_CORE_DRIVER2 is the flag if backlight is into early_suspend.
105         if (suspend_flag && (bl->props.state & BL_CORE_DRIVER2))
106             goto out;
107
108         brightness = bl->props.brightness;
109         if(rk29_bl_info->min_brightness){
110             if(brightness){
111                 brightness = brightness*(256 - rk29_bl_info->min_brightness);
112                 brightness = (brightness>>8) + rk29_bl_info->min_brightness;
113             }
114             if(brightness > 255)
115                 brightness = 255;
116         }
117
118         if (bl->props.power != FB_BLANK_UNBLANK)
119                 brightness = 0;
120
121         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
122                 brightness = 0; 
123
124         if ((bl->props.state & BL_CORE_DRIVER2) && !suspend_flag ){
125                 brightness = 0;
126                 suspend_flag = 1;
127         }else if(!(bl->props.state & BL_CORE_DRIVER2) && suspend_flag ){
128                 suspend_flag = 0;
129         }
130
131 //BL_CORE_DRIVER1 is the flag if backlight pwm is closed.
132         if ((bl->props.state & BL_CORE_DRIVER1) && brightness ==0 ){  
133                 bl->props.state &= ~BL_CORE_DRIVER1;
134                 clk_disable(pwm_clk);
135                 if (rk29_bl_info->pwm_suspend)
136                         rk29_bl_info->pwm_suspend();
137         }else if(!(bl->props.state & BL_CORE_DRIVER1) && brightness != 0){
138                 bl->props.state |= BL_CORE_DRIVER1;
139                 if (rk29_bl_info->pwm_resume)
140                         rk29_bl_info->pwm_resume();
141                 clk_enable(pwm_clk);
142                 msleep(1);
143         }
144
145         div_total = read_pwm_reg(id, PWM_REG_LRC);
146         if (ref) {
147                 divh = div_total*brightness/BL_STEP;
148         } else {
149                 divh = div_total*(BL_STEP-brightness)/BL_STEP;
150         }
151         write_pwm_reg(id, PWM_REG_HRC, divh);
152
153         DBG("%s:line=%d,brightness = %d, div_total = %d, divh = %d state=%x \n",__FUNCTION__,__LINE__,brightness, div_total, divh,bl->props.state);
154 out:
155         mutex_unlock(&backlight_mutex);
156         return 0;
157 }
158
159 static int rk29_bl_get_brightness(struct backlight_device *bl)
160 {
161         u32 divh,div_total;
162         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
163         u32 id = rk29_bl_info->pwm_id;
164         u32 ref = rk29_bl_info->bl_ref;
165
166         div_total = read_pwm_reg(id, PWM_REG_LRC);
167         divh = read_pwm_reg(id, PWM_REG_HRC);
168
169         if (!div_total)
170                 return 0;
171
172         if (ref) {
173                 return BL_STEP*divh/div_total;
174         } else {
175                 return BL_STEP-(BL_STEP*divh/div_total);
176         }
177 }
178
179 static struct backlight_ops rk29_bl_ops = {
180         .update_status  = rk29_bl_update_status,
181         .get_brightness = rk29_bl_get_brightness,
182 };
183
184 static void rk29_backlight_work_func(struct work_struct *work)
185 {
186         rk29_bl->props.state &= ~BL_CORE_DRIVER2;
187         rk29_bl_update_status(rk29_bl);
188 }
189 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
190
191 #ifdef CONFIG_HAS_EARLYSUSPEND
192 static void rk29_bl_suspend(struct early_suspend *h)
193 {
194         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
195         int brightness = rk29_bl->props.brightness;
196
197         cancel_delayed_work_sync(&rk29_backlight_work);
198
199         rk29_bl->props.state |= BL_CORE_DRIVER2;
200
201         if (rk29_bl->props.brightness) {
202                 rk29_bl->props.brightness = 0;
203                 rk29_bl_update_status(rk29_bl);
204                 rk29_bl->props.brightness = brightness;
205         }
206
207 }
208
209 static void rk29_bl_resume(struct early_suspend *h)
210 {
211         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
212         DBG("%s : %s\n", __FILE__, __FUNCTION__);
213         
214         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
215 }
216
217 static struct early_suspend bl_early_suspend = {
218         .suspend = rk29_bl_suspend,
219         .resume = rk29_bl_resume,
220         .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
221 };
222
223 bool rk29_get_backlight_status()
224 {
225         return (rk29_bl->props.state & BL_CORE_DRIVER1)?true:false;
226 }
227 EXPORT_SYMBOL(rk29_get_backlight_status);
228
229 void rk29_backlight_set(bool on)
230 {
231         printk("%s: set %d\n", __func__, on);
232         if(on)
233                 rk29_bl_resume(NULL);
234         else
235                 rk29_bl_suspend(NULL);
236         return;
237 }
238 EXPORT_SYMBOL(rk29_backlight_set);
239 #endif
240
241 static int rk29_backlight_probe(struct platform_device *pdev)
242 {               
243         int ret = 0;
244         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
245         u32 id  =  rk29_bl_info->pwm_id;
246         u32 divh, div_total;
247         unsigned long pwm_clk_rate;
248         struct backlight_properties props;
249         int pre_div = PWM_APB_PRE_DIV;
250
251         if (rk29_bl) {
252                 printk(KERN_CRIT "%s: backlight device register has existed \n",
253                                 __func__);
254                 return -EEXIST;         
255         }
256
257         if (!rk29_bl_info->delay_ms)
258                 rk29_bl_info->delay_ms = 100;
259
260         if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
261                 rk29_bl_info->min_brightness = 52;
262
263         if (rk29_bl_info && rk29_bl_info->io_init) {
264                 rk29_bl_info->io_init();
265         }
266
267         if(rk29_bl_info->pre_div > 0)
268                 pre_div = rk29_bl_info->pre_div;
269
270         memset(&props, 0, sizeof(struct backlight_properties));
271         props.type = BACKLIGHT_RAW;
272         props.max_brightness = BL_STEP;
273         rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
274         if (!rk29_bl) {
275                 printk(KERN_CRIT "%s: backlight device register error\n",
276                                 __func__);
277                 return -ENODEV;         
278         }
279
280 #if defined(CONFIG_ARCH_RK29)
281         pwm_clk = clk_get(NULL, "pwm");
282 #elif defined(CONFIG_ARCH_RK30) || defined(CONFIG_ARCH_RK2928)
283         if (id == 0 || id == 1)
284                 pwm_clk = clk_get(NULL, "pwm01");
285         else if (id == 2 || id == 3)
286                 pwm_clk = clk_get(NULL, "pwm23");
287 #endif
288         if (IS_ERR(pwm_clk) || !pwm_clk) {
289                 printk(KERN_ERR "failed to get pwm clock source\n");
290                 return -ENODEV;
291         }
292         pwm_clk_rate = clk_get_rate(pwm_clk);
293         div_total = pwm_clk_rate / pre_div;
294
295         div_total >>= (1 + (PWM_DIV >> 9));
296         div_total = (div_total) ? div_total : 1;
297
298         if(rk29_bl_info->bl_ref) {
299                 divh = 0;
300         } else {
301                 divh = div_total;
302         }
303
304         clk_enable(pwm_clk);
305         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
306         write_pwm_reg(id, PWM_REG_LRC, div_total);
307         write_pwm_reg(id, PWM_REG_HRC, divh);
308         write_pwm_reg(id, PWM_REG_CNTR, 0x0);
309         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
310
311         rk29_bl->props.power = FB_BLANK_UNBLANK;
312         rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
313         rk29_bl->props.brightness = BL_STEP / 2;
314         rk29_bl->props.state = BL_CORE_DRIVER1;         
315
316         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
317         ret = device_create_file(&pdev->dev,&dev_attr_rk29backlight);
318         if(ret)
319         {
320                 dev_err(&pdev->dev, "failed to create sysfs file\n");
321         }
322
323         register_early_suspend(&bl_early_suspend);
324
325         printk("RK29 Backlight Driver Initialized.\n");
326         return ret;
327 }
328
329 static int rk29_backlight_remove(struct platform_device *pdev)
330 {               
331         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
332
333         if (rk29_bl) {
334                 backlight_device_unregister(rk29_bl);
335                 unregister_early_suspend(&bl_early_suspend);
336                 clk_disable(pwm_clk);
337                 clk_put(pwm_clk);
338                 if (rk29_bl_info && rk29_bl_info->io_deinit) {
339                         rk29_bl_info->io_deinit();
340                 }
341                 return 0;
342         } else {
343                 DBG(KERN_CRIT "%s: no backlight device has registered\n",
344                                 __func__);
345                 return -ENODEV;
346         }
347 }
348
349 static void rk29_backlight_shutdown(struct platform_device *pdev)
350 {
351         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
352
353         unregister_early_suspend(&bl_early_suspend);
354
355         rk29_bl->props.brightness >>= 1;
356         rk29_bl_update_status(rk29_bl);
357         mdelay(100);
358
359         rk29_bl->props.brightness >>= 1;
360         rk29_bl_update_status(rk29_bl);
361         mdelay(100);
362
363         rk29_bl->props.brightness = 0;
364         rk29_bl_update_status(rk29_bl);
365
366         if (rk29_bl_info && rk29_bl_info->io_deinit)
367                 rk29_bl_info->io_deinit();
368 }
369
370 static struct platform_driver rk29_backlight_driver = {
371         .probe  = rk29_backlight_probe,
372         .remove = rk29_backlight_remove,
373         .driver = {
374                 .name   = "rk29_backlight",
375                 .owner  = THIS_MODULE,
376         },
377         .shutdown       = rk29_backlight_shutdown,
378 };
379
380 static int __init rk29_backlight_init(void)
381 {
382         platform_driver_register(&rk29_backlight_driver);
383         return 0;
384 }
385 fs_initcall_sync(rk29_backlight_init);