Merge remote-tracking branch 'linux-2.6.32.y/master' into develop
[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
168         if (rk29_bl) {
169                 DBG(KERN_CRIT "%s: backlight device register has existed \n",
170                                 __func__);
171                 return -EEXIST;         
172         }
173
174         if (!rk29_bl_info->delay_ms)
175                 rk29_bl_info->delay_ms = 30;
176
177         if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
178                 rk29_bl_info->min_brightness = 52;
179
180         if (rk29_bl_info && rk29_bl_info->io_init) {
181                 rk29_bl_info->io_init();
182         }
183
184         rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops);
185         if (!rk29_bl) {
186                 DBG(KERN_CRIT "%s: backlight device register error\n",
187                                 __func__);
188                 return -ENODEV;         
189         }
190
191         pwm_clk = clk_get(NULL, "pwm");
192         if (IS_ERR(pwm_clk)) {
193                 printk(KERN_ERR "failed to get pwm clock source\n");
194                 return -ENODEV; 
195         }
196         pwm_clk_rate = clk_get_rate(pwm_clk);
197         div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
198
199         div_total >>= (1 + (PWM_DIV >> 9));
200         div_total = (div_total) ? div_total : 1;
201
202         if(rk29_bl_info->bl_ref) {
203                 divh = 0;
204         } else {
205                 divh = div_total;
206         }
207
208         clk_enable(pwm_clk);
209         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
210         write_pwm_reg(id, PWM_REG_LRC, div_total);
211         write_pwm_reg(id, PWM_REG_HRC, divh);
212         write_pwm_reg(id, PWM_REG_CNTR, 0x0);
213         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
214
215         rk29_bl->props.power = FB_BLANK_UNBLANK;
216         rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
217         rk29_bl->props.max_brightness = BL_STEP;
218         rk29_bl->props.brightness = BL_STEP / 2;
219
220         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
221
222         register_early_suspend(&bl_early_suspend);
223
224         printk("RK29 Backlight Driver Initialized.\n");
225         return ret;
226 }
227
228 static int rk29_backlight_remove(struct platform_device *pdev)
229 {               
230         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
231
232         if (rk29_bl) {
233                 backlight_device_unregister(rk29_bl);
234                 unregister_early_suspend(&bl_early_suspend);
235                 clk_disable(pwm_clk);
236                 clk_put(pwm_clk);
237                 if (rk29_bl_info && rk29_bl_info->io_deinit) {
238                         rk29_bl_info->io_deinit();
239                 }
240                 return 0;
241         } else {
242                 DBG(KERN_CRIT "%s: no backlight device has registered\n",
243                                 __func__);
244                 return -ENODEV;
245         }
246 }
247
248 static void rk29_backlight_shutdown(struct platform_device *pdev)
249 {
250         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
251
252         rk29_bl->props.brightness >>= 1;
253         rk29_bl_update_status(rk29_bl);
254         mdelay(100);
255
256         rk29_bl->props.brightness >>= 1;
257         rk29_bl_update_status(rk29_bl);
258         mdelay(100);
259
260         rk29_bl->props.brightness = 0;
261         rk29_bl_update_status(rk29_bl);
262
263         if (rk29_bl_info && rk29_bl_info->io_deinit)
264                 rk29_bl_info->io_deinit();
265 }
266
267 static struct platform_driver rk29_backlight_driver = {
268         .probe  = rk29_backlight_probe,
269         .remove = rk29_backlight_remove,
270         .driver = {
271                 .name   = "rk29_backlight",
272                 .owner  = THIS_MODULE,
273         },
274         .shutdown       = rk29_backlight_shutdown,
275 };
276
277 static int __init rk29_backlight_init(void)
278 {
279         platform_driver_register(&rk29_backlight_driver);
280         return 0;
281 }
282 fs_initcall_sync(rk29_backlight_init);