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;
54 int convertint(char s[])
58 for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
60 n = 10 * n + (s[i] - '0');
65 static ssize_t backlight_write(struct device *dev,
66 struct device_attribute *attr, char *buf)
69 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
72 number = convertint(buf);
74 rk29_bl_info->min_brightness=number;
79 static ssize_t backlight_read(struct device *dev,
80 struct device_attribute *attr, char *buf)
82 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
84 printk("rk29_bl_info->min_brightness=%d\n",rk29_bl_info->min_brightness);
86 static DEVICE_ATTR(rk29backlight, 0777, backlight_read, backlight_write);
88 static int rk29_bl_update_status(struct backlight_device *bl)
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;
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;
101 div_total = read_pwm_reg(id, PWM_REG_LRC);
103 divh = div_total*(bl->props.brightness)/BL_STEP;
105 divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
107 write_pwm_reg(id, PWM_REG_HRC, divh);
109 DBG(">>>%s-->%d brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
113 static int rk29_bl_get_brightness(struct backlight_device *bl)
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;
120 div_total = read_pwm_reg(id, PWM_REG_LRC);
121 divh = read_pwm_reg(id, PWM_REG_HRC);
127 return BL_STEP*divh/div_total;
129 return BL_STEP-(BL_STEP*divh/div_total);
133 static struct backlight_ops rk29_bl_ops = {
134 .update_status = rk29_bl_update_status,
135 .get_brightness = rk29_bl_get_brightness,
138 static void rk29_backlight_work_func(struct work_struct *work)
141 rk29_bl_update_status(rk29_bl);
143 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
145 #ifdef CONFIG_HAS_EARLYSUSPEND
146 static void rk29_bl_suspend(struct early_suspend *h)
148 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
149 int brightness = rk29_bl->props.brightness;
151 cancel_delayed_work_sync(&rk29_backlight_work);
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;
160 clk_disable(pwm_clk);
161 if (rk29_bl_info->pwm_suspend)
162 rk29_bl_info->pwm_suspend();
168 static void rk29_bl_resume(struct early_suspend *h)
170 struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
171 DBG("%s : %s\n", __FILE__, __FUNCTION__);
173 if (rk29_bl_info->pwm_resume)
174 rk29_bl_info->pwm_resume();
178 schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
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,
187 void rk29_backlight_set(bool on)
189 printk("%s: set %d\n", __func__, on);
191 rk29_bl_resume(NULL);
193 rk29_bl_suspend(NULL);
196 EXPORT_SYMBOL(rk29_backlight_set);
199 static int rk29_backlight_probe(struct platform_device *pdev)
202 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
203 u32 id = rk29_bl_info->pwm_id;
205 unsigned long pwm_clk_rate;
206 struct backlight_properties props;
209 printk(KERN_CRIT "%s: backlight device register has existed \n",
214 if (!rk29_bl_info->delay_ms)
215 rk29_bl_info->delay_ms = 100;
217 if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
218 rk29_bl_info->min_brightness = 52;
220 if (rk29_bl_info && rk29_bl_info->io_init) {
221 rk29_bl_info->io_init();
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);
229 printk(KERN_CRIT "%s: backlight device register error\n",
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");
242 if (IS_ERR(pwm_clk)) {
243 printk(KERN_ERR "failed to get pwm clock source\n");
246 pwm_clk_rate = clk_get_rate(pwm_clk);
247 div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
249 div_total >>= (1 + (PWM_DIV >> 9));
250 div_total = (div_total) ? div_total : 1;
252 if(rk29_bl_info->bl_ref) {
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);
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;
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);
273 dev_err(&pdev->dev, "failed to create sysfs file\n");
276 register_early_suspend(&bl_early_suspend);
278 printk("RK29 Backlight Driver Initialized.\n");
282 static int rk29_backlight_remove(struct platform_device *pdev)
284 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
287 backlight_device_unregister(rk29_bl);
288 unregister_early_suspend(&bl_early_suspend);
289 clk_disable(pwm_clk);
291 if (rk29_bl_info && rk29_bl_info->io_deinit) {
292 rk29_bl_info->io_deinit();
296 DBG(KERN_CRIT "%s: no backlight device has registered\n",
302 static void rk29_backlight_shutdown(struct platform_device *pdev)
304 struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
306 unregister_early_suspend(&bl_early_suspend);
308 rk29_bl->props.brightness >>= 1;
309 rk29_bl_update_status(rk29_bl);
312 rk29_bl->props.brightness >>= 1;
313 rk29_bl_update_status(rk29_bl);
316 rk29_bl->props.brightness = 0;
317 rk29_bl_update_status(rk29_bl);
319 if (rk29_bl_info && rk29_bl_info->io_deinit)
320 rk29_bl_info->io_deinit();
323 static struct platform_driver rk29_backlight_driver = {
324 .probe = rk29_backlight_probe,
325 .remove = rk29_backlight_remove,
327 .name = "rk29_backlight",
328 .owner = THIS_MODULE,
330 .shutdown = rk29_backlight_shutdown,
333 static int __init rk29_backlight_init(void)
335 platform_driver_register(&rk29_backlight_driver);
338 fs_initcall_sync(rk29_backlight_init);