rkfb: select current screen in lcdc driver
[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 int rk29_bl_update_status(struct backlight_device *bl)
94 {
95         u32 divh,div_total;
96         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
97         u32 id = rk29_bl_info->pwm_id;
98         u32 ref = rk29_bl_info->bl_ref;
99         int brightness = bl->props.brightness;
100         
101         if (suspend_flag)
102                 return 0;       
103
104         if (bl->props.power != FB_BLANK_UNBLANK)
105                 brightness = 0;
106
107         if (bl->props.fb_blank != FB_BLANK_UNBLANK)
108                 brightness = 0; 
109
110         if (bl->props.state & BL_CORE_SUSPENDED)
111                 brightness = 0;
112
113         if (bl->props.brightness < rk29_bl_info->min_brightness)        /*avoid can't view screen when close backlight*/
114                 brightness = rk29_bl_info->min_brightness;
115
116         div_total = read_pwm_reg(id, PWM_REG_LRC);
117         if (ref) {
118                 divh = div_total*brightness/BL_STEP;
119         } else {
120                 divh = div_total*(BL_STEP-brightness)/BL_STEP;
121         }
122         write_pwm_reg(id, PWM_REG_HRC, divh);
123
124         DBG("%s:line=%d,brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,brightness, div_total, divh);
125         return 0;
126 }
127
128 static int rk29_bl_get_brightness(struct backlight_device *bl)
129 {
130         u32 divh,div_total;
131         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
132         u32 id = rk29_bl_info->pwm_id;
133         u32 ref = rk29_bl_info->bl_ref;
134
135         div_total = read_pwm_reg(id, PWM_REG_LRC);
136         divh = read_pwm_reg(id, PWM_REG_HRC);
137
138         if (!div_total)
139                 return 0;
140
141         if (ref) {
142                 return BL_STEP*divh/div_total;
143         } else {
144                 return BL_STEP-(BL_STEP*divh/div_total);
145         }
146 }
147
148 static struct backlight_ops rk29_bl_ops = {
149         .update_status  = rk29_bl_update_status,
150         .get_brightness = rk29_bl_get_brightness,
151 };
152
153 static void rk29_backlight_work_func(struct work_struct *work)
154 {
155         suspend_flag = 0;
156         rk29_bl_update_status(rk29_bl);
157 }
158 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
159
160 #ifdef CONFIG_HAS_EARLYSUSPEND
161 static void rk29_bl_suspend(struct early_suspend *h)
162 {
163         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
164         int brightness = rk29_bl->props.brightness;
165
166         cancel_delayed_work_sync(&rk29_backlight_work);
167
168         if (rk29_bl->props.brightness) {
169                 rk29_bl->props.brightness = 0;
170                 rk29_bl_update_status(rk29_bl);
171                 rk29_bl->props.brightness = brightness;
172         }
173
174         if (!suspend_flag) {
175                 clk_disable(pwm_clk);
176                 if (rk29_bl_info->pwm_suspend)
177                         rk29_bl_info->pwm_suspend();
178         }
179
180         suspend_flag = 1;
181 }
182
183 static void rk29_bl_resume(struct early_suspend *h)
184 {
185         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
186         DBG("%s : %s\n", __FILE__, __FUNCTION__);
187
188         if (rk29_bl_info->pwm_resume)
189                 rk29_bl_info->pwm_resume();
190
191         clk_enable(pwm_clk);
192
193         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
194 }
195
196 static struct early_suspend bl_early_suspend = {
197         .suspend = rk29_bl_suspend,
198         .resume = rk29_bl_resume,
199         .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
200 };
201
202 void rk29_backlight_set(bool on)
203 {
204         printk("%s: set %d\n", __func__, on);
205         if(on)
206                 rk29_bl_resume(NULL);
207         else
208                 rk29_bl_suspend(NULL);
209         return;
210 }
211 EXPORT_SYMBOL(rk29_backlight_set);
212 #endif
213
214 static int rk29_backlight_probe(struct platform_device *pdev)
215 {               
216         int ret = 0;
217         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
218         u32 id  =  rk29_bl_info->pwm_id;
219         u32 divh, div_total;
220         unsigned long pwm_clk_rate;
221         struct backlight_properties props;
222         int pre_div = PWM_APB_PRE_DIV;
223
224         if (rk29_bl) {
225                 printk(KERN_CRIT "%s: backlight device register has existed \n",
226                                 __func__);
227                 return -EEXIST;         
228         }
229
230         if (!rk29_bl_info->delay_ms)
231                 rk29_bl_info->delay_ms = 100;
232
233         if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
234                 rk29_bl_info->min_brightness = 52;
235
236         if (rk29_bl_info && rk29_bl_info->io_init) {
237                 rk29_bl_info->io_init();
238         }
239
240         if(rk29_bl_info->pre_div > 0)
241                 pre_div = rk29_bl_info->pre_div;
242
243         memset(&props, 0, sizeof(struct backlight_properties));
244         props.type = BACKLIGHT_RAW;
245         props.max_brightness = BL_STEP;
246         rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
247         if (!rk29_bl) {
248                 printk(KERN_CRIT "%s: backlight device register error\n",
249                                 __func__);
250                 return -ENODEV;         
251         }
252
253 #if defined(CONFIG_ARCH_RK29)
254         pwm_clk = clk_get(NULL, "pwm");
255 #elif defined(CONFIG_ARCH_RK30) || defined(CONFIG_ARCH_RK2928)
256         if (id == 0 || id == 1)
257                 pwm_clk = clk_get(NULL, "pwm01");
258         else if (id == 2 || id == 3)
259                 pwm_clk = clk_get(NULL, "pwm23");
260 #endif
261         if (IS_ERR(pwm_clk) || !pwm_clk) {
262                 printk(KERN_ERR "failed to get pwm clock source\n");
263                 return -ENODEV;
264         }
265         pwm_clk_rate = clk_get_rate(pwm_clk);
266         div_total = pwm_clk_rate / pre_div;
267
268         div_total >>= (1 + (PWM_DIV >> 9));
269         div_total = (div_total) ? div_total : 1;
270
271         if(rk29_bl_info->bl_ref) {
272                 divh = 0;
273         } else {
274                 divh = div_total;
275         }
276
277         clk_enable(pwm_clk);
278         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
279         write_pwm_reg(id, PWM_REG_LRC, div_total);
280         write_pwm_reg(id, PWM_REG_HRC, divh);
281         write_pwm_reg(id, PWM_REG_CNTR, 0x0);
282         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
283
284         rk29_bl->props.power = FB_BLANK_UNBLANK;
285         rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
286         rk29_bl->props.brightness = BL_STEP / 2;
287
288         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
289         ret = device_create_file(&pdev->dev,&dev_attr_rk29backlight);
290         if(ret)
291         {
292                 dev_err(&pdev->dev, "failed to create sysfs file\n");
293         }
294
295         register_early_suspend(&bl_early_suspend);
296
297         printk("RK29 Backlight Driver Initialized.\n");
298         return ret;
299 }
300
301 static int rk29_backlight_remove(struct platform_device *pdev)
302 {               
303         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
304
305         if (rk29_bl) {
306                 backlight_device_unregister(rk29_bl);
307                 unregister_early_suspend(&bl_early_suspend);
308                 clk_disable(pwm_clk);
309                 clk_put(pwm_clk);
310                 if (rk29_bl_info && rk29_bl_info->io_deinit) {
311                         rk29_bl_info->io_deinit();
312                 }
313                 return 0;
314         } else {
315                 DBG(KERN_CRIT "%s: no backlight device has registered\n",
316                                 __func__);
317                 return -ENODEV;
318         }
319 }
320
321 static void rk29_backlight_shutdown(struct platform_device *pdev)
322 {
323         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
324
325         unregister_early_suspend(&bl_early_suspend);
326
327         rk29_bl->props.brightness >>= 1;
328         rk29_bl_update_status(rk29_bl);
329         mdelay(100);
330
331         rk29_bl->props.brightness >>= 1;
332         rk29_bl_update_status(rk29_bl);
333         mdelay(100);
334
335         rk29_bl->props.brightness = 0;
336         rk29_bl_update_status(rk29_bl);
337
338         if (rk29_bl_info && rk29_bl_info->io_deinit)
339                 rk29_bl_info->io_deinit();
340 }
341
342 static struct platform_driver rk29_backlight_driver = {
343         .probe  = rk29_backlight_probe,
344         .remove = rk29_backlight_remove,
345         .driver = {
346                 .name   = "rk29_backlight",
347                 .owner  = THIS_MODULE,
348         },
349         .shutdown       = rk29_backlight_shutdown,
350 };
351
352 static int __init rk29_backlight_init(void)
353 {
354         platform_driver_register(&rk29_backlight_driver);
355         return 0;
356 }
357 fs_initcall_sync(rk29_backlight_init);