Merge remote-tracking branch 'origin/develop-3.0-rk2928' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / video / backlight / rk29_backlight.c
1 /* drivers/video/backlight/rk29_backlight.c
2  *
3  * Copyright (C) 2009-2011 Rockchip Corporation.
4  *
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.
8  *
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.
13  *
14  */
15
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>
23 #include <linux/fb.h>
24 #include <linux/clk.h>
25
26 #include <linux/earlysuspend.h>
27 #include <asm/io.h>
28 #include <mach/board.h>
29
30 #include "rk2818_backlight.h"
31
32 /*
33  * Debug
34  */
35 #if 0
36 #define DBG(x...)       printk(KERN_INFO x)
37 #else
38 #define DBG(x...)
39 #endif
40
41 #if defined(CONFIG_ARCH_RK30) || defined(CONFIG_ARCH_RK31)
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
45 #elif defined(CONFIG_ARCH_RK2928)
46 #define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK2928_PWM_BASE+id*0x10))
47 #define read_pwm_reg(id, addr)              __raw_readl(addr+(RK2928_PWM_BASE+id*0x10))
48
49 #elif defined(CONFIG_ARCH_RK29)
50 #define write_pwm_reg(id, addr, val)        __raw_writel(val, addr+(RK29_PWM_BASE+id*0x10))
51 #define read_pwm_reg(id, addr)              __raw_readl(addr+(RK29_PWM_BASE+id*0x10))    
52 #endif
53
54 static struct clk *pwm_clk;
55 static struct backlight_device *rk29_bl;
56 static int suspend_flag = 0;
57
58
59 int convertint(char s[])  
60 {  
61     int i;  
62     int n = 0;  
63     for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)  
64     {  
65         n = 10 * n + (s[i] - '0');  
66     }  
67     return n;  
68
69
70 static ssize_t backlight_write(struct device *dev,
71                 struct device_attribute *attr, char *buf)
72 {
73    
74         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
75         int number;
76
77         number = convertint(buf);
78         
79         rk29_bl_info->min_brightness=number;
80         return 0;
81 }
82
83
84 static ssize_t backlight_read(struct device *dev,
85                 struct device_attribute *attr, char *buf)
86 {
87         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
88
89         printk("rk29_bl_info->min_brightness=%d\n",rk29_bl_info->min_brightness);
90 }
91 static DEVICE_ATTR(rk29backlight, 0777, backlight_read, backlight_write);
92
93 static int rk29_bl_update_status(struct backlight_device *bl)
94 {
95         u32 divh,div_total;
96         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
97         u32 id = rk29_bl_info->pwm_id;
98         u32 ref = rk29_bl_info->bl_ref;
99
100         if (suspend_flag)
101                 return 0;
102
103         if (bl->props.brightness < rk29_bl_info->min_brightness)        /*avoid can't view screen when close backlight*/
104                 bl->props.brightness = rk29_bl_info->min_brightness;
105
106         div_total = read_pwm_reg(id, PWM_REG_LRC);
107         if (ref) {
108                 divh = div_total*(bl->props.brightness)/BL_STEP;
109         } else {
110                 divh = div_total*(BL_STEP-bl->props.brightness)/BL_STEP;
111         }
112         write_pwm_reg(id, PWM_REG_HRC, divh);
113
114         DBG(">>>%s-->%d brightness = %d, div_total = %d, divh = %d\n",__FUNCTION__,__LINE__,bl->props.brightness, div_total, divh);
115         return 0;
116 }
117
118 static int rk29_bl_get_brightness(struct backlight_device *bl)
119 {
120         u32 divh,div_total;
121         struct rk29_bl_info *rk29_bl_info = bl_get_data(bl);
122         u32 id = rk29_bl_info->pwm_id;
123         u32 ref = rk29_bl_info->bl_ref;
124
125         div_total = read_pwm_reg(id, PWM_REG_LRC);
126         divh = read_pwm_reg(id, PWM_REG_HRC);
127
128         if (!div_total)
129                 return 0;
130
131         if (ref) {
132                 return BL_STEP*divh/div_total;
133         } else {
134                 return BL_STEP-(BL_STEP*divh/div_total);
135         }
136 }
137
138 static struct backlight_ops rk29_bl_ops = {
139         .update_status  = rk29_bl_update_status,
140         .get_brightness = rk29_bl_get_brightness,
141 };
142
143 static void rk29_backlight_work_func(struct work_struct *work)
144 {
145         suspend_flag = 0;
146         rk29_bl_update_status(rk29_bl);
147 }
148 static DECLARE_DELAYED_WORK(rk29_backlight_work, rk29_backlight_work_func);
149
150 #ifdef CONFIG_HAS_EARLYSUSPEND
151 static void rk29_bl_suspend(struct early_suspend *h)
152 {
153         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
154         int brightness = rk29_bl->props.brightness;
155
156         cancel_delayed_work_sync(&rk29_backlight_work);
157
158         if (rk29_bl->props.brightness) {
159                 rk29_bl->props.brightness = 0;
160                 rk29_bl_update_status(rk29_bl);
161                 rk29_bl->props.brightness = brightness;
162         }
163
164         if (!suspend_flag) {
165                 clk_disable(pwm_clk);
166                 if (rk29_bl_info->pwm_suspend)
167                         rk29_bl_info->pwm_suspend();
168         }
169
170         suspend_flag = 1;
171 }
172
173 static void rk29_bl_resume(struct early_suspend *h)
174 {
175         struct rk29_bl_info *rk29_bl_info = bl_get_data(rk29_bl);
176         DBG("%s : %s\n", __FILE__, __FUNCTION__);
177
178         if (rk29_bl_info->pwm_resume)
179                 rk29_bl_info->pwm_resume();
180
181         clk_enable(pwm_clk);
182
183         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
184 }
185
186 static struct early_suspend bl_early_suspend = {
187         .suspend = rk29_bl_suspend,
188         .resume = rk29_bl_resume,
189         .level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN - 1,
190 };
191
192 void rk29_backlight_set(bool on)
193 {
194         printk("%s: set %d\n", __func__, on);
195         if(on)
196                 rk29_bl_resume(NULL);
197         else
198                 rk29_bl_suspend(NULL);
199         return;
200 }
201 EXPORT_SYMBOL(rk29_backlight_set);
202 #endif
203
204 static int rk29_backlight_probe(struct platform_device *pdev)
205 {               
206         int ret = 0;
207         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
208         u32 id  =  rk29_bl_info->pwm_id;
209         u32 divh, div_total;
210         unsigned long pwm_clk_rate;
211         struct backlight_properties props;
212
213         if (rk29_bl) {
214                 printk(KERN_CRIT "%s: backlight device register has existed \n",
215                                 __func__);
216                 return -EEXIST;         
217         }
218
219         if (!rk29_bl_info->delay_ms)
220                 rk29_bl_info->delay_ms = 100;
221
222         if (rk29_bl_info->min_brightness < 0 || rk29_bl_info->min_brightness > BL_STEP)
223                 rk29_bl_info->min_brightness = 52;
224
225         if (rk29_bl_info && rk29_bl_info->io_init) {
226                 rk29_bl_info->io_init();
227         }
228
229         memset(&props, 0, sizeof(struct backlight_properties));
230         props.type = BACKLIGHT_RAW;
231         props.max_brightness = BL_STEP;
232         rk29_bl = backlight_device_register("rk28_bl", &pdev->dev, rk29_bl_info, &rk29_bl_ops, &props);
233         if (!rk29_bl) {
234                 printk(KERN_CRIT "%s: backlight device register error\n",
235                                 __func__);
236                 return -ENODEV;         
237         }
238
239 #if defined(CONFIG_ARCH_RK29)
240         pwm_clk = clk_get(NULL, "pwm");
241 #elif defined(CONFIG_ARCH_RK30) || defined(CONFIG_ARCH_RK31) || defined(CONFIG_ARCH_RK2928)
242         if (id == 0 || id == 1)
243                 pwm_clk = clk_get(NULL, "pwm01");
244         else if (id == 2 || id == 3)
245                 pwm_clk = clk_get(NULL, "pwm23");
246 #endif
247         if (IS_ERR(pwm_clk) || !pwm_clk) {
248                 printk(KERN_ERR "failed to get pwm clock source\n");
249                 return -ENODEV;
250         }
251         pwm_clk_rate = clk_get_rate(pwm_clk);
252         div_total = pwm_clk_rate / PWM_APB_PRE_DIV;
253
254         div_total >>= (1 + (PWM_DIV >> 9));
255         div_total = (div_total) ? div_total : 1;
256
257         if(rk29_bl_info->bl_ref) {
258                 divh = 0;
259         } else {
260                 divh = div_total;
261         }
262
263         clk_enable(pwm_clk);
264         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_RESET);
265         write_pwm_reg(id, PWM_REG_LRC, div_total);
266         write_pwm_reg(id, PWM_REG_HRC, divh);
267         write_pwm_reg(id, PWM_REG_CNTR, 0x0);
268         write_pwm_reg(id, PWM_REG_CTRL, PWM_DIV|PWM_ENABLE|PWM_TIME_EN);
269
270         rk29_bl->props.power = FB_BLANK_UNBLANK;
271         rk29_bl->props.fb_blank = FB_BLANK_UNBLANK;
272         rk29_bl->props.brightness = BL_STEP / 2;
273
274         schedule_delayed_work(&rk29_backlight_work, msecs_to_jiffies(rk29_bl_info->delay_ms));
275         ret = device_create_file(&pdev->dev,&dev_attr_rk29backlight);
276         if(ret)
277         {
278                 dev_err(&pdev->dev, "failed to create sysfs file\n");
279         }
280
281         register_early_suspend(&bl_early_suspend);
282
283         printk("RK29 Backlight Driver Initialized.\n");
284         return ret;
285 }
286
287 static int rk29_backlight_remove(struct platform_device *pdev)
288 {               
289         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
290
291         if (rk29_bl) {
292                 backlight_device_unregister(rk29_bl);
293                 unregister_early_suspend(&bl_early_suspend);
294                 clk_disable(pwm_clk);
295                 clk_put(pwm_clk);
296                 if (rk29_bl_info && rk29_bl_info->io_deinit) {
297                         rk29_bl_info->io_deinit();
298                 }
299                 return 0;
300         } else {
301                 DBG(KERN_CRIT "%s: no backlight device has registered\n",
302                                 __func__);
303                 return -ENODEV;
304         }
305 }
306
307 static void rk29_backlight_shutdown(struct platform_device *pdev)
308 {
309         struct rk29_bl_info *rk29_bl_info = pdev->dev.platform_data;
310
311         unregister_early_suspend(&bl_early_suspend);
312
313         rk29_bl->props.brightness >>= 1;
314         rk29_bl_update_status(rk29_bl);
315         mdelay(100);
316
317         rk29_bl->props.brightness >>= 1;
318         rk29_bl_update_status(rk29_bl);
319         mdelay(100);
320
321         rk29_bl->props.brightness = 0;
322         rk29_bl_update_status(rk29_bl);
323
324         if (rk29_bl_info && rk29_bl_info->io_deinit)
325                 rk29_bl_info->io_deinit();
326 }
327
328 static struct platform_driver rk29_backlight_driver = {
329         .probe  = rk29_backlight_probe,
330         .remove = rk29_backlight_remove,
331         .driver = {
332                 .name   = "rk29_backlight",
333                 .owner  = THIS_MODULE,
334         },
335         .shutdown       = rk29_backlight_shutdown,
336 };
337
338 static int __init rk29_backlight_init(void)
339 {
340         platform_driver_register(&rk29_backlight_driver);
341         return 0;
342 }
343 fs_initcall_sync(rk29_backlight_init);