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 static struct backlight_properties radeon_bl_data;
24 struct radeon_bl_privdata {
25 struct radeonfb_info *rinfo;
29 static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata,
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;
41 else if (rlevel > MAX_RADEON_LEVEL)
42 rlevel = MAX_RADEON_LEVEL;
45 rlevel = MAX_RADEON_LEVEL - rlevel;
50 static int radeon_bl_update_status(struct backlight_device *bd)
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;
57 if (rinfo->mon1_type != MT_LCD)
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.
64 if (bd->props->power != FB_BLANK_UNBLANK ||
65 bd->props->fb_blank != FB_BLANK_UNBLANK)
68 level = bd->props->brightness;
70 del_timer_sync(&rinfo->lvds_timer);
73 lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
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;
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));
90 lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
92 (radeon_bl_get_level_brightness(pdata, level) <<
93 LVDS_BL_MOD_LEVEL_SHIFT);
94 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
96 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
97 rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
100 /* Asic bug, when turning off LVDS_ON, we have to make sure
101 RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
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);
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);
121 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
122 rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
127 static int radeon_bl_get_brightness(struct backlight_device *bd)
129 return bd->props->brightness;
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),
138 void radeonfb_bl_init(struct radeonfb_info *rinfo)
140 struct backlight_device *bd;
141 struct radeon_bl_privdata *pdata;
144 if (rinfo->mon1_type != MT_LCD)
147 #ifdef CONFIG_PMAC_BACKLIGHT
148 if (!pmac_has_backlight_type("ati") &&
149 !pmac_has_backlight_type("mnca"))
153 pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
155 printk("radeonfb: Memory allocation failed\n");
159 snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
161 bd = backlight_device_register(name, rinfo->info->dev, pdata, &radeon_bl_data);
163 rinfo->info->bl_dev = NULL;
164 printk("radeonfb: Backlight registration failed\n");
168 pdata->rinfo = rinfo;
170 /* Pardon me for that hack... maybe some day we can figure out in what
171 * direction backlight should work on a given panel?
174 (rinfo->family != CHIP_FAMILY_RV200 &&
175 rinfo->family != CHIP_FAMILY_RV250 &&
176 rinfo->family != CHIP_FAMILY_RV280 &&
177 rinfo->family != CHIP_FAMILY_RV350);
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");
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);
191 bd->props->brightness = radeon_bl_data.max_brightness;
192 bd->props->power = FB_BLANK_UNBLANK;
193 backlight_update_status(bd);
195 #ifdef CONFIG_PMAC_BACKLIGHT
196 mutex_lock(&pmac_backlight_mutex);
199 mutex_unlock(&pmac_backlight_mutex);
202 printk("radeonfb: Backlight initialized (%s)\n", name);
211 void radeonfb_bl_exit(struct radeonfb_info *rinfo)
213 struct backlight_device *bd = rinfo->info->bl_dev;
216 struct radeon_bl_privdata *pdata;
218 #ifdef CONFIG_PMAC_BACKLIGHT
219 mutex_lock(&pmac_backlight_mutex);
220 if (pmac_backlight == bd)
221 pmac_backlight = NULL;
222 mutex_unlock(&pmac_backlight_mutex);
224 pdata = class_get_devdata(&bd->class_dev);
225 backlight_device_unregister(bd);
227 rinfo->info->bl_dev = NULL;
229 printk("radeonfb: Backlight unloaded\n");