]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/aty/radeon_backlight.c
backlight: Clean up pmac_backlight handling
[linux-2.6-omap-h63xx.git] / 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         int rlevel;
33
34         /* Get and convert the value */
35         /* No locking of bl_curve since we read a single value */
36         rlevel = pdata->rinfo->info->bl_curve[level] *
37                  FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL;
38
39         if (rlevel < 0)
40                 rlevel = 0;
41         else if (rlevel > MAX_RADEON_LEVEL)
42                 rlevel = MAX_RADEON_LEVEL;
43
44         if (pdata->negative)
45                 rlevel = MAX_RADEON_LEVEL - rlevel;
46
47         return rlevel;
48 }
49
50 static int radeon_bl_update_status(struct backlight_device *bd)
51 {
52         struct radeon_bl_privdata *pdata = class_get_devdata(&bd->class_dev);
53         struct radeonfb_info *rinfo = pdata->rinfo;
54         u32 lvds_gen_cntl, tmpPixclksCntl;
55         int level;
56
57         if (rinfo->mon1_type != MT_LCD)
58                 return 0;
59
60         /* We turn off the LCD completely instead of just dimming the
61          * backlight. This provides some greater power saving and the display
62          * is useless without backlight anyway.
63          */
64         if (bd->props->power != FB_BLANK_UNBLANK ||
65             bd->props->fb_blank != FB_BLANK_UNBLANK)
66                 level = 0;
67         else
68                 level = bd->props->brightness;
69
70         del_timer_sync(&rinfo->lvds_timer);
71         radeon_engine_idle();
72
73         lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
74         if (level > 0) {
75                 lvds_gen_cntl &= ~LVDS_DISPLAY_DIS;
76                 if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) {
77                         lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON);
78                         lvds_gen_cntl |= LVDS_BLON | LVDS_EN;
79                         OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
80                         lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
81                         lvds_gen_cntl |=
82                                 (radeon_bl_get_level_brightness(pdata, level) <<
83                                  LVDS_BL_MOD_LEVEL_SHIFT);
84                         lvds_gen_cntl |= LVDS_ON;
85                         lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_BL_MOD_EN);
86                         rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
87                         mod_timer(&rinfo->lvds_timer,
88                                   jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
89                 } else {
90                         lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
91                         lvds_gen_cntl |=
92                                 (radeon_bl_get_level_brightness(pdata, level) <<
93                                  LVDS_BL_MOD_LEVEL_SHIFT);
94                         OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
95                 }
96                 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
97                 rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
98                         & LVDS_STATE_MASK;
99         } else {
100                 /* Asic bug, when turning off LVDS_ON, we have to make sure
101                    RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
102                 */
103                 tmpPixclksCntl = INPLL(PIXCLKS_CNTL);
104                 if (rinfo->is_mobility || rinfo->is_IGP)
105                         OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb);
106                 lvds_gen_cntl &= ~(LVDS_BL_MOD_LEVEL_MASK | LVDS_BL_MOD_EN);
107                 lvds_gen_cntl |= (radeon_bl_get_level_brightness(pdata, 0) <<
108                                   LVDS_BL_MOD_LEVEL_SHIFT);
109                 lvds_gen_cntl |= LVDS_DISPLAY_DIS;
110                 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
111                 udelay(100);
112                 lvds_gen_cntl &= ~(LVDS_ON | LVDS_EN);
113                 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
114                 lvds_gen_cntl &= ~(LVDS_DIGON);
115                 rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
116                 mod_timer(&rinfo->lvds_timer,
117                           jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
118                 if (rinfo->is_mobility || rinfo->is_IGP)
119                         OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl);
120         }
121         rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
122         rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
123
124         return 0;
125 }
126
127 static int radeon_bl_get_brightness(struct backlight_device *bd)
128 {
129         return bd->props->brightness;
130 }
131
132 static struct backlight_properties radeon_bl_data = {
133         .get_brightness = radeon_bl_get_brightness,
134         .update_status  = radeon_bl_update_status,
135         .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
136 };
137
138 void radeonfb_bl_init(struct radeonfb_info *rinfo)
139 {
140         struct backlight_device *bd;
141         struct radeon_bl_privdata *pdata;
142         char name[12];
143
144         if (rinfo->mon1_type != MT_LCD)
145                 return;
146
147 #ifdef CONFIG_PMAC_BACKLIGHT
148         if (!pmac_has_backlight_type("ati") &&
149             !pmac_has_backlight_type("mnca"))
150                 return;
151 #endif
152
153         pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
154         if (!pdata) {
155                 printk("radeonfb: Memory allocation failed\n");
156                 goto error;
157         }
158
159         snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
160
161         bd = backlight_device_register(name, rinfo->info->dev, pdata, &radeon_bl_data);
162         if (IS_ERR(bd)) {
163                 rinfo->info->bl_dev = NULL;
164                 printk("radeonfb: Backlight registration failed\n");
165                 goto error;
166         }
167
168         pdata->rinfo = rinfo;
169
170         /* Pardon me for that hack... maybe some day we can figure out in what
171          * direction backlight should work on a given panel?
172          */
173         pdata->negative =
174                 (rinfo->family != CHIP_FAMILY_RV200 &&
175                  rinfo->family != CHIP_FAMILY_RV250 &&
176                  rinfo->family != CHIP_FAMILY_RV280 &&
177                  rinfo->family != CHIP_FAMILY_RV350);
178
179 #ifdef CONFIG_PMAC_BACKLIGHT
180         pdata->negative = pdata->negative ||
181                 machine_is_compatible("PowerBook4,3") ||
182                 machine_is_compatible("PowerBook6,3") ||
183                 machine_is_compatible("PowerBook6,5");
184 #endif
185
186         rinfo->info->bl_dev = bd;
187         fb_bl_default_curve(rinfo->info, 0,
188                  63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL,
189                 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL);
190
191         bd->props->brightness = radeon_bl_data.max_brightness;
192         bd->props->power = FB_BLANK_UNBLANK;
193         backlight_update_status(bd);
194
195         printk("radeonfb: Backlight initialized (%s)\n", name);
196
197         return;
198
199 error:
200         kfree(pdata);
201         return;
202 }
203
204 void radeonfb_bl_exit(struct radeonfb_info *rinfo)
205 {
206         struct backlight_device *bd = rinfo->info->bl_dev;
207
208         if (bd) {
209                 struct radeon_bl_privdata *pdata;
210
211                 pdata = class_get_devdata(&bd->class_dev);
212                 backlight_device_unregister(bd);
213                 kfree(pdata);
214                 rinfo->info->bl_dev = NULL;
215
216                 printk("radeonfb: Backlight unloaded\n");
217         }
218 }