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