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)
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))
49 static struct clk *pwm_clk;
50 static struct backlight_device *rk29_bl;
51 static int suspend_flag = 0;
53 static int rk29_bl_update_status(struct backlight_device *bl)
56 struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
57 u32 id = rk29_bl_info->pwm_id;
58 u32 ref = rk29_bl_info->bl_ref;
63 if (bl->props.brightness < rk29_bl_info->min_brightness) /*avoid can't view screen when close backlight*/
64 bl->props.brightness = rk29_bl_info->min_brightness;
66 div_total = read_pwm_reg(id, PWM_REG_LRC);
68 divh = div_total*(bl->props.brightness)/BL_STEP;
70 divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
72 write_pwm_reg(id, PWM_REG_HRC, divh);
74 DBG(">>>%s-->%d brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
78 static int rk29_bl_get_brightness(struct backlight_device *bl)
81 struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
82 u32 id = rk29_bl_info->pwm_id;
83 u32 ref = rk29_bl_info->bl_ref;
85 div_total = read_pwm_reg(id, PWM_REG_LRC);
86 divh = read_pwm_reg(id, PWM_REG_HRC);
92 return BL_STEP*divh/div_total;
94 return BL_STEP-(BL_STEP*divh/div_total);
98 static struct backlight_ops rk29_bl_ops = {
99 .update_status = rk29_bl_update_status,
100 .get_brightness = rk29_bl_get_brightness,
103 static void rk29_backlight_work_func(struct work_struct *work)
106 rk29_bl_update_status(rk29_bl);
108 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
110 #ifdef CONFIG_HAS_EARLYSUSPEND
111 static void rk29_bl_suspend(struct early_suspend *h)
113 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
114 int brightness = rk29_bl->props.brightness;
116 cancel_delayed_work_sync(&rk29_backlight_work);
118 if (rk29_bl->props.brightness) {
119 rk29_bl->props.brightness = 0;
120 rk29_bl_update_status(rk29_bl);
121 rk29_bl->props.brightness = brightness;
125 clk_disable(pwm_clk);
126 if (rk29_bl_info->pwm_suspend)
127 rk29_bl_info->pwm_suspend();
133 static void rk29_bl_resume(struct early_suspend *h)
135 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
136 DBG("%s : %s\n", __FILE__, __FUNCTION__);
138 if (rk29_bl_info->pwm_resume)
139 rk29_bl_info->pwm_resume();
143 schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
146 static struct early_suspend bl_early_suspend = {
147 .suspend = rk29_bl_suspend,
148 .resume = rk29_bl_resume,
149 .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
152 void rk29_backlight_set(bool on)
154 printk("%s: set %d\n", __func__, on);
156 rk29_bl_resume(NULL);
158 rk29_bl_suspend(NULL);
161 EXPORT_SYMBOL(rk29_backlight_set);
164 static int rk29_backlight_probe(struct platform_device *pdev)
167 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
168 u32 id = rk29_bl_info->pwm_id;
170 unsigned long pwm_clk_rate;
171 struct backlight_properties props;
174 printk(KERN_CRIT "%s: backlight device register has existed \n",
179 if (!rk29_bl_info->delay_ms)
180 rk29_bl_info->delay_ms = 100;
182 if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
183 rk29_bl_info->min_brightness = 52;
185 if (rk29_bl_info && rk29_bl_info->io_init) {
186 rk29_bl_info->io_init();
189 memset(&props, 0, sizeof(struct backlight_properties));
190 props.type = BACKLIGHT_RAW;
191 props.max_brightness = BL_STEP;
192 rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
194 printk(KERN_CRIT "%s: backlight device register error\n",
199 #if defined(CONFIG_ARCH_RK29)
200 pwm_clk = clk_get(NULL, "pwm");
201 #elif defined(CONFIG_ARCH_RK30)
202 if (id == 0 || id == 1)
203 pwm_clk = clk_get(NULL, "pwm01");
204 else if (id == 2 || id == 3)
205 pwm_clk = clk_get(NULL, "pwm23");
207 if (IS_ERR(pwm_clk)) {
208 printk(KERN_ERR "failed to get pwm clock source\n");
211 pwm_clk_rate = clk_get_rate(pwm_clk);
212 div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
214 div_total >>= (1 + (PWM_DIV >> 9));
215 div_total = (div_total) ? div_total : 1;
217 if(rk29_bl_info->bl_ref) {
224 write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
225 write_pwm_reg(id, PWM_REG_LRC, div_total);
226 write_pwm_reg(id, PWM_REG_HRC, divh);
227 write_pwm_reg(id, PWM_REG_CNTR, 0x0);
228 write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
230 rk29_bl->props.power = FB_BLANK_UNBLANK;
231 rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
232 rk29_bl->props.brightness = BL_STEP / 2;
234 schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
236 register_early_suspend(&bl_early_suspend);
238 printk("RK29 Backlight Driver Initialized.\n");
242 static int rk29_backlight_remove(struct platform_device *pdev)
244 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
247 backlight_device_unregister(rk29_bl);
248 unregister_early_suspend(&bl_early_suspend);
249 clk_disable(pwm_clk);
251 if (rk29_bl_info && rk29_bl_info->io_deinit) {
252 rk29_bl_info->io_deinit();
256 DBG(KERN_CRIT "%s: no backlight device has registered\n",
262 static void rk29_backlight_shutdown(struct platform_device *pdev)
264 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
266 unregister_early_suspend(&bl_early_suspend);
268 rk29_bl->props.brightness >>= 1;
269 rk29_bl_update_status(rk29_bl);
272 rk29_bl->props.brightness >>= 1;
273 rk29_bl_update_status(rk29_bl);
276 rk29_bl->props.brightness = 0;
277 rk29_bl_update_status(rk29_bl);
279 if (rk29_bl_info && rk29_bl_info->io_deinit)
280 rk29_bl_info->io_deinit();
283 static struct platform_driver rk29_backlight_driver = {
284 .probe = rk29_backlight_probe,
285 .remove = rk29_backlight_remove,
287 .name = "rk29_backlight",
288 .owner = THIS_MODULE,
290 .shutdown = rk29_backlight_shutdown,
293 static int __init rk29_backlight_init(void)
295 platform_driver_register(&rk29_backlight_driver);
298 fs_initcall_sync(rk29_backlight_init);