1 /* drivers/video/backlight/rk29_backlight.c
3 * Copyright (C) 2009-2011 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/backlight.h>
24 #include <linux/clk.h>
26 #include <linux/earlysuspend.h>
28 #include <mach/board.h>
30 #include "rk2818_backlight.h"
36 #define DBG(x...) printk(KERN_INFO x)
41 #if defined(CONFIG_ARCH_RK30)
43 #define write_pwm_reg(id, addr, val) __raw_writel(val, addr+(RK30_PWM01_BASE+(id>>1)*0x20000)+id*10)
44 #define read_pwm_reg(id, addr) __raw_readl(addr+(RK30_PWM01_BASE+(id>>1)*0x20000+id*0x10))
45 #else defined(CONFIG_ARCH_RK29)
46 #include <mach/rk29_iomap.h>
47 #define write_pwm_reg(id, addr, val) __raw_writel(val, addr+(RK29_PWM_BASE+id*0x10))
48 #define read_pwm_reg(id, addr) __raw_readl(addr+(RK29_PWM_BASE+id*0x10))
51 static struct clk *pwm_clk;
52 static struct backlight_device *rk29_bl;
53 static int suspend_flag = 0;
55 static int rk29_bl_update_status(struct backlight_device *bl)
58 struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
59 u32 id = rk29_bl_info->pwm_id;
60 u32 ref = rk29_bl_info->bl_ref;
65 if (bl->props.brightness < rk29_bl_info->min_brightness) /*avoid can't view screen when close backlight*/
66 bl->props.brightness = rk29_bl_info->min_brightness;
68 div_total = read_pwm_reg(id, PWM_REG_LRC);
70 divh = div_total*(bl->props.brightness)/BL_STEP;
72 divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
74 write_pwm_reg(id, PWM_REG_HRC, divh);
76 DBG(">>>%s-->%d brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
80 static int rk29_bl_get_brightness(struct backlight_device *bl)
83 struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
84 u32 id = rk29_bl_info->pwm_id;
85 u32 ref = rk29_bl_info->bl_ref;
87 div_total = read_pwm_reg(id, PWM_REG_LRC);
88 divh = read_pwm_reg(id, PWM_REG_HRC);
94 return BL_STEP*divh/div_total;
96 return BL_STEP-(BL_STEP*divh/div_total);
100 static struct backlight_ops rk29_bl_ops = {
101 .update_status = rk29_bl_update_status,
102 .get_brightness = rk29_bl_get_brightness,
105 static void rk29_backlight_work_func(struct work_struct *work)
108 rk29_bl_update_status(rk29_bl);
110 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
112 #ifdef CONFIG_HAS_EARLYSUSPEND
113 static void rk29_bl_suspend(struct early_suspend *h)
115 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
116 int brightness = rk29_bl->props.brightness;
118 cancel_delayed_work_sync(&rk29_backlight_work);
120 if (rk29_bl->props.brightness) {
121 rk29_bl->props.brightness = 0;
122 rk29_bl_update_status(rk29_bl);
123 rk29_bl->props.brightness = brightness;
127 clk_disable(pwm_clk);
128 if (rk29_bl_info->pwm_suspend)
129 rk29_bl_info->pwm_suspend();
135 static void rk29_bl_resume(struct early_suspend *h)
137 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
138 DBG("%s : %s\n", __FILE__, __FUNCTION__);
140 if (rk29_bl_info->pwm_resume)
141 rk29_bl_info->pwm_resume();
145 schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
148 static struct early_suspend bl_early_suspend = {
149 .suspend = rk29_bl_suspend,
150 .resume = rk29_bl_resume,
151 .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
154 void rk29_backlight_set(bool on)
156 printk("%s: set %d\n", __func__, on);
158 rk29_bl_resume(NULL);
160 rk29_bl_suspend(NULL);
163 EXPORT_SYMBOL(rk29_backlight_set);
166 static int rk29_backlight_probe(struct platform_device *pdev)
169 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
170 u32 id = rk29_bl_info->pwm_id;
172 unsigned long pwm_clk_rate;
173 struct backlight_properties props;
176 printk(KERN_CRIT "%s: backlight device register has existed \n",
181 if (!rk29_bl_info->delay_ms)
182 rk29_bl_info->delay_ms = 100;
184 if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
185 rk29_bl_info->min_brightness = 52;
187 if (rk29_bl_info && rk29_bl_info->io_init) {
188 rk29_bl_info->io_init();
191 memset(&props, 0, sizeof(struct backlight_properties));
192 props.type = BACKLIGHT_RAW;
193 props.max_brightness = BL_STEP;
194 rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
196 printk(KERN_CRIT "%s: backlight device register error\n",
201 pwm_clk = clk_get(NULL, "pwm");
202 if (IS_ERR(pwm_clk)) {
203 printk(KERN_ERR "failed to get pwm clock source\n");
206 pwm_clk_rate = clk_get_rate(pwm_clk);
207 div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
209 div_total >>= (1 + (PWM_DIV >> 9));
210 div_total = (div_total) ? div_total : 1;
212 if(rk29_bl_info->bl_ref) {
219 write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
220 write_pwm_reg(id, PWM_REG_LRC, div_total);
221 write_pwm_reg(id, PWM_REG_HRC, divh);
222 write_pwm_reg(id, PWM_REG_CNTR, 0x0);
223 write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
225 rk29_bl->props.power = FB_BLANK_UNBLANK;
226 rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
227 rk29_bl->props.brightness = BL_STEP / 2;
229 schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
231 register_early_suspend(&bl_early_suspend);
233 printk("RK29 Backlight Driver Initialized.\n");
237 static int rk29_backlight_remove(struct platform_device *pdev)
239 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
242 backlight_device_unregister(rk29_bl);
243 unregister_early_suspend(&bl_early_suspend);
244 clk_disable(pwm_clk);
246 if (rk29_bl_info && rk29_bl_info->io_deinit) {
247 rk29_bl_info->io_deinit();
251 DBG(KERN_CRIT "%s: no backlight device has registered\n",
257 static void rk29_backlight_shutdown(struct platform_device *pdev)
259 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
261 rk29_bl->props.brightness >>= 1;
262 rk29_bl_update_status(rk29_bl);
265 rk29_bl->props.brightness >>= 1;
266 rk29_bl_update_status(rk29_bl);
269 rk29_bl->props.brightness = 0;
270 rk29_bl_update_status(rk29_bl);
272 if (rk29_bl_info && rk29_bl_info->io_deinit)
273 rk29_bl_info->io_deinit();
276 static struct platform_driver rk29_backlight_driver = {
277 .probe = rk29_backlight_probe,
278 .remove = rk29_backlight_remove,
280 .name = "rk29_backlight",
281 .owner = THIS_MODULE,
283 .shutdown = rk29_backlight_shutdown,
286 static int __init rk29_backlight_init(void)
288 platform_driver_register(&rk29_backlight_driver);
291 fs_initcall_sync(rk29_backlight_init);