2 * Backlight code for ATI Radeon based graphic cards
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>
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.
14 #include <linux/backlight.h>
16 #ifdef CONFIG_PMAC_BACKLIGHT
17 #include <asm/backlight.h>
20 #define MAX_RADEON_LEVEL 0xFF
22 struct radeon_bl_privdata
{
23 struct radeonfb_info
*rinfo
;
27 static int radeon_bl_get_level_brightness(struct radeon_bl_privdata
*pdata
,
32 /* Get and convert the value */
33 /* No locking of bl_curve since we read a single value */
34 rlevel
= pdata
->rinfo
->info
->bl_curve
[level
] *
35 FB_BACKLIGHT_MAX
/ MAX_RADEON_LEVEL
;
39 else if (rlevel
> MAX_RADEON_LEVEL
)
40 rlevel
= MAX_RADEON_LEVEL
;
43 rlevel
= MAX_RADEON_LEVEL
- rlevel
;
48 static int radeon_bl_update_status(struct backlight_device
*bd
)
50 struct radeon_bl_privdata
*pdata
= bl_get_data(bd
);
51 struct radeonfb_info
*rinfo
= pdata
->rinfo
;
52 u32 lvds_gen_cntl
, tmpPixclksCntl
;
55 if (rinfo
->mon1_type
!= MT_LCD
)
58 /* We turn off the LCD completely instead of just dimming the
59 * backlight. This provides some greater power saving and the display
60 * is useless without backlight anyway.
62 if (bd
->props
.power
!= FB_BLANK_UNBLANK
||
63 bd
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
66 level
= bd
->props
.brightness
;
68 del_timer_sync(&rinfo
->lvds_timer
);
71 lvds_gen_cntl
= INREG(LVDS_GEN_CNTL
);
73 lvds_gen_cntl
&= ~LVDS_DISPLAY_DIS
;
74 if (!(lvds_gen_cntl
& LVDS_BLON
) || !(lvds_gen_cntl
& LVDS_ON
)) {
75 lvds_gen_cntl
|= (rinfo
->init_state
.lvds_gen_cntl
& LVDS_DIGON
);
76 lvds_gen_cntl
|= LVDS_BLON
| LVDS_EN
;
77 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
78 lvds_gen_cntl
&= ~LVDS_BL_MOD_LEVEL_MASK
;
80 (radeon_bl_get_level_brightness(pdata
, level
) <<
81 LVDS_BL_MOD_LEVEL_SHIFT
);
82 lvds_gen_cntl
|= LVDS_ON
;
83 lvds_gen_cntl
|= (rinfo
->init_state
.lvds_gen_cntl
& LVDS_BL_MOD_EN
);
84 rinfo
->pending_lvds_gen_cntl
= lvds_gen_cntl
;
85 mod_timer(&rinfo
->lvds_timer
,
86 jiffies
+ msecs_to_jiffies(rinfo
->panel_info
.pwr_delay
));
88 lvds_gen_cntl
&= ~LVDS_BL_MOD_LEVEL_MASK
;
90 (radeon_bl_get_level_brightness(pdata
, level
) <<
91 LVDS_BL_MOD_LEVEL_SHIFT
);
92 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
94 rinfo
->init_state
.lvds_gen_cntl
&= ~LVDS_STATE_MASK
;
95 rinfo
->init_state
.lvds_gen_cntl
|= rinfo
->pending_lvds_gen_cntl
98 /* Asic bug, when turning off LVDS_ON, we have to make sure
99 RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
101 tmpPixclksCntl
= INPLL(PIXCLKS_CNTL
);
102 if (rinfo
->is_mobility
|| rinfo
->is_IGP
)
103 OUTPLLP(PIXCLKS_CNTL
, 0, ~PIXCLK_LVDS_ALWAYS_ONb
);
104 lvds_gen_cntl
&= ~(LVDS_BL_MOD_LEVEL_MASK
| LVDS_BL_MOD_EN
);
105 lvds_gen_cntl
|= (radeon_bl_get_level_brightness(pdata
, 0) <<
106 LVDS_BL_MOD_LEVEL_SHIFT
);
107 lvds_gen_cntl
|= LVDS_DISPLAY_DIS
;
108 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
110 lvds_gen_cntl
&= ~(LVDS_ON
| LVDS_EN
);
111 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
112 lvds_gen_cntl
&= ~(LVDS_DIGON
);
113 rinfo
->pending_lvds_gen_cntl
= lvds_gen_cntl
;
114 mod_timer(&rinfo
->lvds_timer
,
115 jiffies
+ msecs_to_jiffies(rinfo
->panel_info
.pwr_delay
));
116 if (rinfo
->is_mobility
|| rinfo
->is_IGP
)
117 OUTPLL(PIXCLKS_CNTL
, tmpPixclksCntl
);
119 rinfo
->init_state
.lvds_gen_cntl
&= ~LVDS_STATE_MASK
;
120 rinfo
->init_state
.lvds_gen_cntl
|= (lvds_gen_cntl
& LVDS_STATE_MASK
);
125 static int radeon_bl_get_brightness(struct backlight_device
*bd
)
127 return bd
->props
.brightness
;
130 static struct backlight_ops radeon_bl_data
= {
131 .get_brightness
= radeon_bl_get_brightness
,
132 .update_status
= radeon_bl_update_status
,
135 void radeonfb_bl_init(struct radeonfb_info
*rinfo
)
137 struct backlight_properties props
;
138 struct backlight_device
*bd
;
139 struct radeon_bl_privdata
*pdata
;
142 if (rinfo
->mon1_type
!= MT_LCD
)
145 #ifdef CONFIG_PMAC_BACKLIGHT
146 if (!pmac_has_backlight_type("ati") &&
147 !pmac_has_backlight_type("mnca"))
151 pdata
= kmalloc(sizeof(struct radeon_bl_privdata
), GFP_KERNEL
);
153 printk("radeonfb: Memory allocation failed\n");
157 snprintf(name
, sizeof(name
), "radeonbl%d", rinfo
->info
->node
);
159 memset(&props
, 0, sizeof(struct backlight_properties
));
160 props
.max_brightness
= FB_BACKLIGHT_LEVELS
- 1;
161 bd
= backlight_device_register(name
, rinfo
->info
->dev
, pdata
,
162 &radeon_bl_data
, &props
);
164 rinfo
->info
->bl_dev
= NULL
;
165 printk("radeonfb: Backlight registration failed\n");
169 pdata
->rinfo
= rinfo
;
171 /* Pardon me for that hack... maybe some day we can figure out in what
172 * direction backlight should work on a given panel?
175 (rinfo
->family
!= CHIP_FAMILY_RV200
&&
176 rinfo
->family
!= CHIP_FAMILY_RV250
&&
177 rinfo
->family
!= CHIP_FAMILY_RV280
&&
178 rinfo
->family
!= CHIP_FAMILY_RV350
);
180 #ifdef CONFIG_PMAC_BACKLIGHT
181 pdata
->negative
= pdata
->negative
||
182 of_machine_is_compatible("PowerBook4,3") ||
183 of_machine_is_compatible("PowerBook6,3") ||
184 of_machine_is_compatible("PowerBook6,5");
187 rinfo
->info
->bl_dev
= bd
;
188 fb_bl_default_curve(rinfo
->info
, 0,
189 63 * FB_BACKLIGHT_MAX
/ MAX_RADEON_LEVEL
,
190 217 * FB_BACKLIGHT_MAX
/ MAX_RADEON_LEVEL
);
192 bd
->props
.brightness
= bd
->props
.max_brightness
;
193 bd
->props
.power
= FB_BLANK_UNBLANK
;
194 backlight_update_status(bd
);
196 printk("radeonfb: Backlight initialized (%s)\n", name
);
205 void radeonfb_bl_exit(struct radeonfb_info
*rinfo
)
207 struct backlight_device
*bd
= rinfo
->info
->bl_dev
;
210 struct radeon_bl_privdata
*pdata
;
212 pdata
= bl_get_data(bd
);
213 backlight_device_unregister(bd
);
215 rinfo
->info
->bl_dev
= NULL
;
217 printk("radeonfb: Backlight unloaded\n");