backlight:fix hdmi switch black screen
[firefly-linux-kernel-4.4.55.git] / drivers / video / backlight / rk29_backlight.c
index c83ecf426f8fc6cb85d95391a03f8f52e7e91cd7..9a8fedf254d1f7ad0301fc240ab05258623cf53d 100755 (executable)
 
 #include <linux/earlysuspend.h>
 #include <asm/io.h>
-#include <mach/rk29_iomap.h>
 #include <mach/board.h>
+#include <plat/pwm.h>
 
-#include "rk2818_backlight.h"
+#define PWM_DIV              PWM_DIV2
+#define PWM_APB_PRE_DIV      1000
+#define BL_STEP              255
 
 /*
  * Debug
 #define DBG(x...)
 #endif
 
-
-#define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK29_PWM_BASE+id*0x10))
-#define read_pwm_reg(id, addr)              __raw_readl(addr+(RK29_PWM_BASE+id*0x10))    
+#define read_pwm_reg(addr)              __raw_readl(pwm_base + addr)
 
 static struct clk *pwm_clk;
+static void __iomem *pwm_base;
 static struct backlight_device *rk29_bl;
 static int suspend_flag = 0;
 
+
+int convertint(const char s[])  
+{  
+    int i;  
+    int n = 0;  
+    for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)  
+    {  
+        n = 10 * n + (s[i] - '0');  
+    }  
+    return n;  
+} 
+
+static ssize_t backlight_write(struct device *dev, 
+               struct device_attribute *attr,const char *buf, size_t count)
+{
+   
+       struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
+       int number;
+
+       number = convertint(buf);
+       
+       rk29_bl_info->min_brightness=number;
+       return 0;
+}
+
+
+static ssize_t backlight_read(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       DBG("rk29_bl_info->min_brightness=%d\n",rk29_bl_info->min_brightness);
+       return 0;
+}
+static DEVICE_ATTR(rk29backlight, 0660, backlight_read, backlight_write);
+
+static DEFINE_MUTEX(backlight_mutex);
+
 static int rk29_bl_update_status(struct backlight_device *bl)
 {
        u32 divh,div_total;
        struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
        u32 id = rk29_bl_info->pwm_id;
        u32 ref = rk29_bl_info->bl_ref;
+       int brightness = 0;
+       
+       mutex_lock(&backlight_mutex);
+//BL_CORE_DRIVER2 is the flag if backlight is into early_suspend.
+       if (suspend_flag && (bl->props.state & BL_CORE_DRIVER2))
+           goto out;
+
+       brightness = bl->props.brightness;
+       if(rk29_bl_info->min_brightness){
+           if(brightness){
+               brightness = brightness*(256 - rk29_bl_info->min_brightness);
+               brightness = (brightness>>8) + rk29_bl_info->min_brightness;
+           }
+           if(brightness > 255)
+               brightness = 255;
+       }
 
-       if (suspend_flag)
-               return 0;
+       if (bl->props.power != FB_BLANK_UNBLANK)
+               brightness = 0;
 
-       if (bl->props.brightness < rk29_bl_info->min_brightness)        /*avoid can't view screen when close backlight*/
-               bl->props.brightness = rk29_bl_info->min_brightness;
+       if (bl->props.fb_blank != FB_BLANK_UNBLANK)
+               brightness = 0; 
 
-       div_total = read_pwm_reg(id, PWM_REG_LRC);
+       if (bl->props.state & BL_CORE_DRIVER3)
+               brightness = 0; 
+
+       if ((bl->props.state & BL_CORE_DRIVER2) && !suspend_flag ){
+               brightness = 0;
+               suspend_flag = 1;
+       }else if(!(bl->props.state & BL_CORE_DRIVER2) && suspend_flag ){
+               suspend_flag = 0;
+       }
+       div_total = read_pwm_reg(PWM_REG_LRC);
        if (ref) {
-               divh = div_total*(bl->props.brightness)/BL_STEP;
+               divh = div_total*brightness/BL_STEP;
        } else {
-               divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
+               divh = div_total*(BL_STEP-brightness)/BL_STEP;
        }
-       write_pwm_reg(id, PWM_REG_HRC, divh);
+       rk_pwm_setup(id, PWM_DIV, divh, div_total);
+
+//BL_CORE_DRIVER1 is the flag if backlight pwm is closed.
+       if ((bl->props.state & BL_CORE_DRIVER1) && brightness ==0 ){  
+               bl->props.state &= ~BL_CORE_DRIVER1;
+               clk_disable(pwm_clk);
+               if (rk29_bl_info->pwm_suspend)
+                       rk29_bl_info->pwm_suspend();
+       }else if(!(bl->props.state & BL_CORE_DRIVER1) && brightness != 0){
+               bl->props.state |= BL_CORE_DRIVER1;
+               if (rk29_bl_info->pwm_resume)
+                       rk29_bl_info->pwm_resume();
+               clk_enable(pwm_clk);
+               msleep(1);
+               div_total = read_pwm_reg(PWM_REG_LRC);
+               if (ref) {
+                       divh = div_total*brightness/BL_STEP;
+               } else {
+                       divh = div_total*(BL_STEP-brightness)/BL_STEP;
+               }
+               rk_pwm_setup(id, PWM_DIV, divh, div_total);
+       }
+
 
-       DBG(">>>%s-->%d brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
+
+       DBG("%s:line=%d,brightness = %d, div_total = %d, divh = %d state=%x \n",__FUNCTION__,__LINE__,brightness, div_total, divh,bl->props.state);
+out:
+       mutex_unlock(&backlight_mutex);
        return 0;
 }
 
@@ -76,11 +163,10 @@ static int rk29_bl_get_brightness(struct backlight_device *bl)
 {
        u32 divh,div_total;
        struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
-       u32 id = rk29_bl_info->pwm_id;
        u32 ref = rk29_bl_info->bl_ref;
 
-       div_total = read_pwm_reg(id, PWM_REG_LRC);
-       divh = read_pwm_reg(id, PWM_REG_HRC);
+       div_total = read_pwm_reg(PWM_REG_LRC);
+       divh = read_pwm_reg(PWM_REG_HRC);
 
        if (!div_total)
                return 0;
@@ -99,7 +185,6 @@ static struct backlight_ops rk29_bl_ops = {
 
 static void rk29_backlight_work_func(struct work_struct *work)
 {
-       suspend_flag = 0;
        rk29_bl_update_status(rk29_bl);
 }
 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
@@ -107,36 +192,26 @@ static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
 #ifdef CONFIG_HAS_EARLYSUSPEND
 static void rk29_bl_suspend(struct early_suspend *h)
 {
-       struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
        int brightness = rk29_bl->props.brightness;
 
        cancel_delayed_work_sync(&rk29_backlight_work);
 
+       rk29_bl->props.state |= BL_CORE_DRIVER2;
+
        if (rk29_bl->props.brightness) {
                rk29_bl->props.brightness = 0;
                rk29_bl_update_status(rk29_bl);
                rk29_bl->props.brightness = brightness;
        }
 
-       if (!suspend_flag) {
-               clk_disable(pwm_clk);
-               if (rk29_bl_info->pwm_suspend)
-                       rk29_bl_info->pwm_suspend();
-       }
-
-       suspend_flag = 1;
 }
 
 static void rk29_bl_resume(struct early_suspend *h)
 {
        struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
        DBG("%s : %s\n", __FILE__, __FUNCTION__);
-
-       if (rk29_bl_info->pwm_resume)
-               rk29_bl_info->pwm_resume();
-
-       clk_enable(pwm_clk);
-
+       rk29_bl->props.state &= ~BL_CORE_DRIVER2;
+       
        schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
 }
 
@@ -145,6 +220,26 @@ static struct early_suspend bl_early_suspend = {
        .resume = rk29_bl_resume,
        .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
 };
+
+bool rk29_get_backlight_status(void)
+{
+       return ((rk29_bl->props.state & BL_CORE_DRIVER3)==BL_CORE_DRIVER3)?true:false;
+}
+EXPORT_SYMBOL(rk29_get_backlight_status);
+
+void rk29_backlight_set(bool on)
+{
+       printk("%s: set %d\n", __func__, on);
+       if(on){
+               rk29_bl->props.state &= ~BL_CORE_DRIVER3;
+               rk29_bl_update_status(rk29_bl);
+       }else{
+               rk29_bl->props.state |= BL_CORE_DRIVER3;
+               rk29_bl_update_status(rk29_bl);
+       }
+       return;
+}
+EXPORT_SYMBOL(rk29_backlight_set);
 #endif
 
 static int rk29_backlight_probe(struct platform_device *pdev)
@@ -154,15 +249,17 @@ static int rk29_backlight_probe(struct platform_device *pdev)
        u32 id  =  rk29_bl_info->pwm_id;
        u32 divh, div_total;
        unsigned long pwm_clk_rate;
+       struct backlight_properties props;
+       int pre_div = PWM_APB_PRE_DIV;
 
        if (rk29_bl) {
-               DBG(KERN_CRIT "%s: backlight device register has existed \n",
+               printk(KERN_CRIT "%s: backlight device register has existed \n",
                                __func__);
                return -EEXIST;         
        }
 
        if (!rk29_bl_info->delay_ms)
-               rk29_bl_info->delay_ms = 30;
+               rk29_bl_info->delay_ms = 100;
 
        if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
                rk29_bl_info->min_brightness = 52;
@@ -171,20 +268,27 @@ static int rk29_backlight_probe(struct platform_device *pdev)
                rk29_bl_info->io_init();
        }
 
-       rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops);
+       if(rk29_bl_info->pre_div > 0)
+               pre_div = rk29_bl_info->pre_div;
+
+       memset(&props, 0, sizeof(struct backlight_properties));
+       props.type = BACKLIGHT_RAW;
+       props.max_brightness = BL_STEP;
+       rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
        if (!rk29_bl) {
-               DBG(KERN_CRIT "%s: backlight device register error\n",
+               printk(KERN_CRIT "%s: backlight device register error\n",
                                __func__);
                return -ENODEV;         
        }
 
-       pwm_clk = clk_get(NULL, "pwm");
-       if (IS_ERR(pwm_clk)) {
+       pwm_base = rk_pwm_get_base(id);
+       pwm_clk = rk_pwm_get_clk(id);
+       if (IS_ERR(pwm_clk) || !pwm_clk) {
                printk(KERN_ERR "failed to get pwm clock source\n");
-               return -ENODEV; 
+               return -ENODEV;
        }
        pwm_clk_rate = clk_get_rate(pwm_clk);
-       div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
+       div_total = pwm_clk_rate / pre_div;
 
        div_total >>= (1 + (PWM_DIV >> 9));
        div_total = (div_total) ? div_total : 1;
@@ -196,18 +300,19 @@ static int rk29_backlight_probe(struct platform_device *pdev)
        }
 
        clk_enable(pwm_clk);
-       write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
-       write_pwm_reg(id, PWM_REG_LRC, div_total);
-       write_pwm_reg(id, PWM_REG_HRC, divh);
-       write_pwm_reg(id, PWM_REG_CNTR, 0x0);
-       write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
+       rk_pwm_setup(id, PWM_DIV, divh, div_total);
 
        rk29_bl->props.power = FB_BLANK_UNBLANK;
        rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
-       rk29_bl->props.max_brightness = BL_STEP;
        rk29_bl->props.brightness = BL_STEP / 2;
+       rk29_bl->props.state = BL_CORE_DRIVER1;         
 
        schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
+       ret = device_create_file(&pdev->dev,&dev_attr_rk29backlight);
+       if(ret)
+       {
+               dev_err(&pdev->dev, "failed to create sysfs file\n");
+       }
 
        register_early_suspend(&bl_early_suspend);
 
@@ -239,6 +344,8 @@ static void rk29_backlight_shutdown(struct platform_device *pdev)
 {
        struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
 
+       unregister_early_suspend(&bl_early_suspend);
+
        rk29_bl->props.brightness >>= 1;
        rk29_bl_update_status(rk29_bl);
        mdelay(100);