backlight: Fix external uses of backlight internal semaphore
[safe/jmp/linux-2.6] / drivers / video / aty / radeon_backlight.c
1 /*
2  * Backlight code for ATI Radeon based graphic cards
3  *
4  * Copyright (c) 2000 Ani Joshi <ajoshi@kernel.crashing.org>
5  * Copyright (c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
6  * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include "radeonfb.h"
14 #include <linux/backlight.h>
15
16 #ifdef CONFIG_PMAC_BACKLIGHT
17 #include <asm/backlight.h>
18 #endif
19
20 #define MAX_RADEON_LEVEL 0xFF
21
22 static struct backlight_properties radeon_bl_data;
23
24 struct radeon_bl_privdata {
25         struct radeonfb_info *rinfo;
26         uint8_t negative;
27 };
28
29 static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata,
30                 int level)
31 {
32         struct fb_info *info = pdata->rinfo->info;
33         int rlevel;
34
35         mutex_lock(&info->bl_mutex);
36
37         /* Get and convert the value */
38         rlevel = pdata->rinfo->info->bl_curve[level] *
39                  FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL;
40
41         mutex_unlock(&info->bl_mutex);
42
43         if (rlevel < 0)
44                 rlevel = 0;
45         else if (rlevel > MAX_RADEON_LEVEL)
46                 rlevel = MAX_RADEON_LEVEL;
47
48         if (pdata->negative)
49                 rlevel = MAX_RADEON_LEVEL - rlevel;
50
51         return rlevel;
52 }
53
54 static int radeon_bl_update_status(struct backlight_device *bd)
55 {
56         struct radeon_bl_privdata *pdata = class_get_devdata(&bd->class_dev);
57         struct radeonfb_info *rinfo = pdata->rinfo;
58         u32 lvds_gen_cntl, tmpPixclksCntl;
59         int level;
60
61         if (rinfo->mon1_type != MT_LCD)
62                 return 0;
63
64         /* We turn off the LCD completely instead of just dimming the
65          * backlight. This provides some greater power saving and the display
66          * is useless without backlight anyway.
67          */
68         if (bd->props->power != FB_BLANK_UNBLANK ||
69             bd->props->fb_blank != FB_BLANK_UNBLANK)
70                 level = 0;
71         else
72                 level = bd->props->brightness;
73
74         del_timer_sync(&rinfo->lvds_timer);
75         radeon_engine_idle();
76
77         lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
78         if (level > 0) {
79                 lvds_gen_cntl &= ~LVDS_DISPLAY_DIS;
80                 if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) {
81                         lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON);
82                         lvds_gen_cntl |= LVDS_BLON | LVDS_EN;
83                         OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
84                         lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
85                         lvds_gen_cntl |=
86                                 (radeon_bl_get_level_brightness(pdata, level) <<
87                                  LVDS_BL_MOD_LEVEL_SHIFT);
88                         lvds_gen_cntl |= LVDS_ON;
89                         lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_BL_MOD_EN);
90                         rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
91                         mod_timer(&rinfo->lvds_timer,
92                                   jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
93                 } else {
94                         lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
95                         lvds_gen_cntl |=
96                                 (radeon_bl_get_level_brightness(pdata, level) <<
97                                  LVDS_BL_MOD_LEVEL_SHIFT);
98                         OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
99                 }
100                 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
101                 rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
102                         & LVDS_STATE_MASK;
103         } else {
104                 /* Asic bug, when turning off LVDS_ON, we have to make sure
105                    RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
106                 */
107                 tmpPixclksCntl = INPLL(PIXCLKS_CNTL);
108                 if (rinfo->is_mobility || rinfo->is_IGP)
109                         OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb);
110                 lvds_gen_cntl &= ~(LVDS_BL_MOD_LEVEL_MASK | LVDS_BL_MOD_EN);
111                 lvds_gen_cntl |= (radeon_bl_get_level_brightness(pdata, 0) <<
112                                   LVDS_BL_MOD_LEVEL_SHIFT);
113                 lvds_gen_cntl |= LVDS_DISPLAY_DIS;
114                 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
115                 udelay(100);
116                 lvds_gen_cntl &= ~(LVDS_ON | LVDS_EN);
117                 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
118                 lvds_gen_cntl &= ~(LVDS_DIGON);
119                 rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
120                 mod_timer(&rinfo->lvds_timer,
121                           jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
122                 if (rinfo->is_mobility || rinfo->is_IGP)
123                         OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl);
124         }
125         rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
126         rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
127
128         return 0;
129 }
130
131 static int radeon_bl_get_brightness(struct backlight_device *bd)
132 {
133         return bd->props->brightness;
134 }
135
136 static struct backlight_properties radeon_bl_data = {
137         .get_brightness = radeon_bl_get_brightness,
138         .update_status  = radeon_bl_update_status,
139         .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
140 };
141
142 void radeonfb_bl_init(struct radeonfb_info *rinfo)
143 {
144         struct backlight_device *bd;
145         struct radeon_bl_privdata *pdata;
146         char name[12];
147
148         if (rinfo->mon1_type != MT_LCD)
149                 return;
150
151 #ifdef CONFIG_PMAC_BACKLIGHT
152         if (!pmac_has_backlight_type("ati") &&
153             !pmac_has_backlight_type("mnca"))
154                 return;
155 #endif
156
157         pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
158         if (!pdata) {
159                 printk("radeonfb: Memory allocation failed\n");
160                 goto error;
161         }
162
163         snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
164
165         bd = backlight_device_register(name, rinfo->info->dev, pdata, &radeon_bl_data);
166         if (IS_ERR(bd)) {
167                 rinfo->info->bl_dev = NULL;
168                 printk("radeonfb: Backlight registration failed\n");
169                 goto error;
170         }
171
172         pdata->rinfo = rinfo;
173
174         /* Pardon me for that hack... maybe some day we can figure out in what
175          * direction backlight should work on a given panel?
176          */
177         pdata->negative =
178                 (rinfo->family != CHIP_FAMILY_RV200 &&
179                  rinfo->family != CHIP_FAMILY_RV250 &&
180                  rinfo->family != CHIP_FAMILY_RV280 &&
181                  rinfo->family != CHIP_FAMILY_RV350);
182
183 #ifdef CONFIG_PMAC_BACKLIGHT
184         pdata->negative = pdata->negative ||
185                 machine_is_compatible("PowerBook4,3") ||
186                 machine_is_compatible("PowerBook6,3") ||
187                 machine_is_compatible("PowerBook6,5");
188 #endif
189
190         mutex_lock(&rinfo->info->bl_mutex);
191         rinfo->info->bl_dev = bd;
192         fb_bl_default_curve(rinfo->info, 0,
193                  63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL,
194                 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL);
195         mutex_unlock(&rinfo->info->bl_mutex);
196
197         bd->props->brightness = radeon_bl_data.max_brightness;
198         bd->props->power = FB_BLANK_UNBLANK;
199         backlight_update_status(bd);
200
201 #ifdef CONFIG_PMAC_BACKLIGHT
202         mutex_lock(&pmac_backlight_mutex);
203         if (!pmac_backlight)
204                 pmac_backlight = bd;
205         mutex_unlock(&pmac_backlight_mutex);
206 #endif
207
208         printk("radeonfb: Backlight initialized (%s)\n", name);
209
210         return;
211
212 error:
213         kfree(pdata);
214         return;
215 }
216
217 void radeonfb_bl_exit(struct radeonfb_info *rinfo)
218 {
219 #ifdef CONFIG_PMAC_BACKLIGHT
220         mutex_lock(&pmac_backlight_mutex);
221 #endif
222
223         mutex_lock(&rinfo->info->bl_mutex);
224         if (rinfo->info->bl_dev) {
225                 struct radeon_bl_privdata *pdata;
226
227 #ifdef CONFIG_PMAC_BACKLIGHT
228                 if (pmac_backlight == rinfo->info->bl_dev)
229                         pmac_backlight = NULL;
230 #endif
231
232                 pdata = class_get_devdata(&rinfo->info->bl_dev->class_dev);
233                 backlight_device_unregister(rinfo->info->bl_dev);
234                 kfree(pdata);
235                 rinfo->info->bl_dev = NULL;
236
237                 printk("radeonfb: Backlight unloaded\n");
238         }
239         mutex_unlock(&rinfo->info->bl_mutex);
240
241 #ifdef CONFIG_PMAC_BACKLIGHT
242         mutex_unlock(&pmac_backlight_mutex);
243 #endif
244 }