1 /* drivers/video/backlight/rk29_backlight.c
3 * Copyright (C) 2009 Rockchip Corporation.
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.
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.
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/debugfs.h>
23 #include <linux/backlight.h>
25 #include <linux/cpufreq.h>
26 #include <linux/clk.h>
28 #include <linux/earlysuspend.h>
30 //#include <mach/typedef.h>
31 #include <mach/iomux.h>
32 #include <mach/gpio.h>
33 #include <mach/rk29_iomap.h>
34 #include <mach/board.h>
36 #include "rk2818_backlight.h"
42 #define DBG(x...) printk(KERN_INFO x)
48 #define write_pwm_reg(id, addr, val) __raw_writel(val, addr+(RK29_PWM_BASE+id*0x10))
49 #define read_pwm_reg(id, addr) __raw_readl(addr+(RK29_PWM_BASE+id*0x10))
50 #define mask_pwm_reg(id, addr, msk, val) write_dma_reg(id, addr, (val)|((~(msk))&read_dma_reg(id, addr)))
52 static struct clk *pwm_clk;
53 static unsigned long pwm_clk_rate;
54 static struct backlight_device *rk29_bl;
55 static int suspend_flag = 0;
56 #define BACKLIGHT_SEE_MINVALUE 52
58 static s32 rk29_bl_update_status(struct backlight_device *bl)
61 struct rk29_bl_info *rk29_bl_info = bl->dev.parent->platform_data;
62 u32 id = rk29_bl_info->pwm_id;
63 u32 ref = rk29_bl_info->bl_ref;
68 div_total = read_pwm_reg(id, PWM_REG_LRC);
71 divh = div_total*(bl->props.brightness)/BL_STEP;
72 DBG(">>>%s-->%d bl->props.brightness == %d, div_total == %d , divh == %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
74 DBG(">>>%s-->%d bl->props.brightness == %d\n",__FUNCTION__,__LINE__,bl->props.brightness);
75 if(bl->props.brightness < BACKLIGHT_SEE_MINVALUE) /*avoid can't view screen when close backlight*/
76 bl->props.brightness = BACKLIGHT_SEE_MINVALUE;
77 divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
79 write_pwm_reg(id, PWM_REG_HRC, divh);
83 static s32 rk29_bl_get_brightness(struct backlight_device *bl)
86 struct rk29_bl_info *rk29_bl_info = bl->dev.parent->platform_data;
87 u32 id = rk29_bl_info->pwm_id;
88 u32 ref = rk29_bl_info->bl_ref;
90 div_total = read_pwm_reg(id, PWM_REG_LRC);
91 divh = read_pwm_reg(id, PWM_REG_HRC);
93 DBG("%s:: %d div_total: %d\n",__func__,__LINE__,div_total);
96 return BL_STEP*divh/div_total;
98 return BL_STEP-(BL_STEP*divh/div_total);
102 static struct backlight_ops rk29_bl_ops = {
103 .update_status = rk29_bl_update_status,
104 .get_brightness = rk29_bl_get_brightness,
107 #ifdef CONFIG_CPU_FREQ
108 static int rk29_bl_change_clk(struct notifier_block *nb, unsigned long val, void *data)
110 struct rk29_bl_info *rk29_bl_info;
114 int is_suspended = suspend_flag;
117 DBG(KERN_CRIT "%s: backlight device does not exist\n", __func__);
122 case CPUFREQ_PRECHANGE:
124 case CPUFREQ_POSTCHANGE:
125 if (clk_get_rate(pwm_clk) == pwm_clk_rate)
127 pwm_clk_rate = clk_get_rate(pwm_clk);
128 rk29_bl_info = rk29_bl->dev.parent->platform_data;
129 id = rk29_bl_info->pwm_id;
133 divl = read_pwm_reg(id, PWM_REG_LRC);
134 divh = read_pwm_reg(id, PWM_REG_HRC);
136 tmp = pwm_clk_rate / PWM_APB_PRE_DIV;
137 tmp >>= (1 + (PWM_DIV >> 9));
140 div_total = (tmp) ? tmp : 1;
141 tmp = div_total*divh/divl;
144 clk_disable(pwm_clk);
145 write_pwm_reg(id, PWM_REG_LRC, div_total);
146 write_pwm_reg(id, PWM_REG_HRC, tmp);
147 write_pwm_reg(id, PWM_REG_CNTR, 0);
156 static void rk29_delaybacklight_timer(unsigned long data)
158 struct rk29_bl_info *rk29_bl_info = (struct rk29_bl_info *)data;
162 id = rk29_bl_info->pwm_id;
163 brightness = rk29_bl->props.brightness;
164 div_total = read_pwm_reg(id, PWM_REG_LRC);
165 if (rk29_bl_info->bl_ref) {
166 divh = div_total*(brightness)/BL_STEP;
168 divh = div_total*(BL_STEP-brightness)/BL_STEP;
170 write_pwm_reg(id, PWM_REG_HRC, divh);
174 #ifdef CONFIG_HAS_EARLYSUSPEND
175 static void rk29_bl_suspend(struct early_suspend *h)
177 struct rk29_bl_info *rk29_bl_info;
181 rk29_bl_info = rk29_bl->dev.parent->platform_data;
183 id = rk29_bl_info->pwm_id;
185 div_total = read_pwm_reg(id, PWM_REG_LRC);
187 if(rk29_bl_info->bl_ref) {
193 write_pwm_reg(id, PWM_REG_HRC, divh);
195 clk_disable(pwm_clk);
201 static void rk29_bl_resume(struct early_suspend *h)
203 struct rk29_bl_info *rk29_bl_info;
204 // u32 id, brightness;
205 //u32 div_total, divh;
206 DBG("%s : %s\n", __FILE__, __FUNCTION__);
207 rk29_bl_info = rk29_bl->dev.parent->platform_data;
209 rk29_bl_info->timer.expires = jiffies + 30;
210 add_timer(&rk29_bl_info->timer);
212 id = rk28_bl_info->pwm_id;
213 brightness = rk28_bl->props.brightness;
215 div_total = read_pwm_reg(id, PWM_REG_LRC);
216 if (rk28_bl_info->bl_ref) {
217 divh = div_total*(brightness)/BL_STEP;
219 divh = div_total*(BL_STEP-brightness)/BL_STEP;
222 write_pwm_reg(id, PWM_REG_HRC, divh);
228 static struct early_suspend bl_early_suspend;
231 static int rk29_backlight_probe(struct platform_device *pdev)
234 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
235 u32 id = rk29_bl_info->pwm_id;
239 DBG(KERN_CRIT "%s: backlight device register has existed \n",
244 rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, NULL, &rk29_bl_ops);
246 DBG(KERN_CRIT "%s: backlight device register error\n",
252 pwm_clk = clk_get(NULL, "pwm");
253 if (!pwm_clk || IS_ERR(pwm_clk)) {
254 printk(KERN_ERR "failed to get pwm clock source\n");
257 pwm_clk_rate = clk_get_rate(pwm_clk);
258 div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
262 div_total >>= (1 + (PWM_DIV >> 9));
263 div_total = (div_total) ? div_total : 1;
265 /// if(rk29_bl_info->bl_ref) {
268 divh = div_total / 2;
271 /*init timer to dispose workqueue */
272 setup_timer(&rk29_bl_info->timer, rk29_delaybacklight_timer, (unsigned long)rk29_bl_info);
274 #ifdef CONFIG_CPU_FREQ
275 rk29_bl_info->freq_transition.notifier_call = rk29_bl_change_clk;
276 cpufreq_register_notifier(&rk29_bl_info->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
279 // clk_disable(pwm_clk);
280 write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
281 write_pwm_reg(id, PWM_REG_LRC, div_total);
282 write_pwm_reg(id, PWM_REG_HRC, divh);
283 write_pwm_reg(id, PWM_REG_CNTR, 0x0);
284 write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
287 rk29_bl->props.power = FB_BLANK_UNBLANK;
288 rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
289 rk29_bl->props.max_brightness = BL_STEP;
290 rk29_bl->props.brightness = rk29_bl_get_brightness(rk29_bl);
292 #ifdef CONFIG_HAS_EARLYSUSPEND
293 bl_early_suspend.suspend = rk29_bl_suspend;
294 bl_early_suspend.resume = rk29_bl_resume;
295 bl_early_suspend.level = ~0x0;
296 register_early_suspend(&bl_early_suspend);
299 if (rk29_bl_info && rk29_bl_info->io_init) {
300 rk29_bl_info->io_init();
306 static int rk29_backlight_remove(struct platform_device *pdev)
308 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
311 backlight_device_unregister(rk29_bl);
312 #ifdef CONFIG_HAS_EARLYSUSPEND
313 unregister_early_suspend(&bl_early_suspend);
315 #ifdef CONFIG_CPU_FREQ
316 cpufreq_unregister_notifier(&rk29_bl_info->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
318 clk_disable(pwm_clk);
320 if (rk29_bl_info && rk29_bl_info->io_deinit) {
321 rk29_bl_info->io_deinit();
325 DBG(KERN_CRIT "%s: no backlight device has registered\n",
330 static void rk29_backlight_shutdown(struct platform_device *pdev)
334 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
335 u32 id = rk29_bl_info->pwm_id;
337 brightness = rk29_bl->props.brightness;
339 div_total = read_pwm_reg(id, PWM_REG_LRC);
341 if (rk29_bl_info->bl_ref) {
342 divh = div_total*(brightness)/BL_STEP;
344 divh = div_total*(BL_STEP-brightness)/BL_STEP;
346 write_pwm_reg(id, PWM_REG_HRC, divh);
347 //printk("divh=%d\n",divh);
351 if (rk29_bl_info->bl_ref) {
352 divh = div_total*(brightness)/BL_STEP;
354 divh = div_total*(BL_STEP-brightness)/BL_STEP;
357 write_pwm_reg(id, PWM_REG_HRC, divh);
359 /*set PF1=1 PF2=1 for close backlight*/
361 if(rk29_bl_info->bl_ref) {
366 write_pwm_reg(id, PWM_REG_HRC, divh);
370 static struct platform_driver rk29_backlight_driver = {
371 .probe = rk29_backlight_probe,
372 .remove = rk29_backlight_remove,
374 .name = "rk29_backlight",
375 .owner = THIS_MODULE,
377 .shutdown=rk29_backlight_shutdown,
381 static int __init rk29_backlight_init(void)
383 platform_driver_register(&rk29_backlight_driver);
386 //rootfs_initcall(rk29_backlight_init);
388 late_initcall(rk29_backlight_init);
389 //module_init(rk29_backlight_init);