Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 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 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 22 | struct radeon_bl_privdata { |
| 23 | struct radeonfb_info *rinfo; |
| 24 | uint8_t negative; |
| 25 | }; |
| 26 | |
| 27 | static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata, |
| 28 | int level) |
| 29 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 30 | int rlevel; |
| 31 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 32 | /* Get and convert the value */ |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 33 | /* No locking of bl_curve since we read a single value */ |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 34 | rlevel = pdata->rinfo->info->bl_curve[level] * |
| 35 | FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL; |
| 36 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 37 | if (rlevel < 0) |
| 38 | rlevel = 0; |
| 39 | else if (rlevel > MAX_RADEON_LEVEL) |
| 40 | rlevel = MAX_RADEON_LEVEL; |
| 41 | |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 42 | if (pdata->negative) |
| 43 | rlevel = MAX_RADEON_LEVEL - rlevel; |
| 44 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 45 | return rlevel; |
| 46 | } |
| 47 | |
| 48 | static int radeon_bl_update_status(struct backlight_device *bd) |
| 49 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 50 | struct radeon_bl_privdata *pdata = bl_get_data(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 51 | struct radeonfb_info *rinfo = pdata->rinfo; |
| 52 | u32 lvds_gen_cntl, tmpPixclksCntl; |
| 53 | int level; |
| 54 | |
| 55 | if (rinfo->mon1_type != MT_LCD) |
| 56 | return 0; |
| 57 | |
| 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. |
| 61 | */ |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 62 | if (bd->props.power != FB_BLANK_UNBLANK || |
| 63 | bd->props.fb_blank != FB_BLANK_UNBLANK) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 64 | level = 0; |
| 65 | else |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 66 | level = bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 67 | |
| 68 | del_timer_sync(&rinfo->lvds_timer); |
| 69 | radeon_engine_idle(); |
| 70 | |
| 71 | lvds_gen_cntl = INREG(LVDS_GEN_CNTL); |
| 72 | if (level > 0) { |
| 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; |
| 79 | lvds_gen_cntl |= |
| 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)); |
| 87 | } else { |
| 88 | lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; |
| 89 | lvds_gen_cntl |= |
| 90 | (radeon_bl_get_level_brightness(pdata, level) << |
| 91 | LVDS_BL_MOD_LEVEL_SHIFT); |
| 92 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); |
| 93 | } |
| 94 | rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; |
| 95 | rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl |
| 96 | & LVDS_STATE_MASK; |
| 97 | } else { |
| 98 | /* Asic bug, when turning off LVDS_ON, we have to make sure |
| 99 | RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off |
| 100 | */ |
| 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); |
| 109 | udelay(100); |
| 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); |
| 118 | } |
| 119 | rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; |
| 120 | rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int radeon_bl_get_brightness(struct backlight_device *bd) |
| 126 | { |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 127 | return bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 130 | static struct backlight_ops radeon_bl_data = { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 131 | .get_brightness = radeon_bl_get_brightness, |
| 132 | .update_status = radeon_bl_update_status, |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | void radeonfb_bl_init(struct radeonfb_info *rinfo) |
| 136 | { |
| 137 | struct backlight_device *bd; |
| 138 | struct radeon_bl_privdata *pdata; |
| 139 | char name[12]; |
| 140 | |
| 141 | if (rinfo->mon1_type != MT_LCD) |
| 142 | return; |
| 143 | |
| 144 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 145 | if (!pmac_has_backlight_type("ati") && |
| 146 | !pmac_has_backlight_type("mnca")) |
| 147 | return; |
| 148 | #endif |
| 149 | |
| 150 | pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL); |
| 151 | if (!pdata) { |
| 152 | printk("radeonfb: Memory allocation failed\n"); |
| 153 | goto error; |
| 154 | } |
| 155 | |
| 156 | snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node); |
| 157 | |
James Simmons | a8274d5 | 2006-12-19 12:56:16 -0800 | [diff] [blame] | 158 | bd = backlight_device_register(name, rinfo->info->dev, pdata, &radeon_bl_data); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 159 | if (IS_ERR(bd)) { |
| 160 | rinfo->info->bl_dev = NULL; |
| 161 | printk("radeonfb: Backlight registration failed\n"); |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | pdata->rinfo = rinfo; |
| 166 | |
| 167 | /* Pardon me for that hack... maybe some day we can figure out in what |
| 168 | * direction backlight should work on a given panel? |
| 169 | */ |
| 170 | pdata->negative = |
| 171 | (rinfo->family != CHIP_FAMILY_RV200 && |
| 172 | rinfo->family != CHIP_FAMILY_RV250 && |
| 173 | rinfo->family != CHIP_FAMILY_RV280 && |
| 174 | rinfo->family != CHIP_FAMILY_RV350); |
| 175 | |
| 176 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 177 | pdata->negative = pdata->negative || |
| 178 | machine_is_compatible("PowerBook4,3") || |
| 179 | machine_is_compatible("PowerBook6,3") || |
| 180 | machine_is_compatible("PowerBook6,5"); |
| 181 | #endif |
| 182 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 183 | rinfo->info->bl_dev = bd; |
| 184 | fb_bl_default_curve(rinfo->info, 0, |
| 185 | 63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL, |
| 186 | 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 187 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 188 | bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1; |
| 189 | bd->props.brightness = bd->props.max_brightness; |
| 190 | bd->props.power = FB_BLANK_UNBLANK; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 191 | backlight_update_status(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 192 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 193 | printk("radeonfb: Backlight initialized (%s)\n", name); |
| 194 | |
| 195 | return; |
| 196 | |
| 197 | error: |
| 198 | kfree(pdata); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | void radeonfb_bl_exit(struct radeonfb_info *rinfo) |
| 203 | { |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 204 | struct backlight_device *bd = rinfo->info->bl_dev; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 205 | |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 206 | if (bd) { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 207 | struct radeon_bl_privdata *pdata; |
| 208 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 209 | pdata = bl_get_data(bd); |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 210 | backlight_device_unregister(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 211 | kfree(pdata); |
| 212 | rinfo->info->bl_dev = NULL; |
| 213 | |
| 214 | printk("radeonfb: Backlight unloaded\n"); |
| 215 | } |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 216 | } |