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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 16 | |
| 17 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 18 | #include <asm/backlight.h> |
| 19 | #endif |
| 20 | |
| 21 | #define MAX_RADEON_LEVEL 0xFF |
| 22 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 23 | struct radeon_bl_privdata { |
| 24 | struct radeonfb_info *rinfo; |
| 25 | uint8_t negative; |
| 26 | }; |
| 27 | |
| 28 | static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata, |
| 29 | int level) |
| 30 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 31 | int rlevel; |
| 32 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 33 | /* Get and convert the value */ |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 34 | /* No locking of bl_curve since we read a single value */ |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 35 | rlevel = pdata->rinfo->info->bl_curve[level] * |
| 36 | FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL; |
| 37 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 38 | if (rlevel < 0) |
| 39 | rlevel = 0; |
| 40 | else if (rlevel > MAX_RADEON_LEVEL) |
| 41 | rlevel = MAX_RADEON_LEVEL; |
| 42 | |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 43 | if (pdata->negative) |
| 44 | rlevel = MAX_RADEON_LEVEL - rlevel; |
| 45 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 46 | return rlevel; |
| 47 | } |
| 48 | |
| 49 | static int radeon_bl_update_status(struct backlight_device *bd) |
| 50 | { |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 51 | struct radeon_bl_privdata *pdata = bl_get_data(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 52 | struct radeonfb_info *rinfo = pdata->rinfo; |
| 53 | u32 lvds_gen_cntl, tmpPixclksCntl; |
| 54 | int level; |
| 55 | |
| 56 | if (rinfo->mon1_type != MT_LCD) |
| 57 | return 0; |
| 58 | |
| 59 | /* We turn off the LCD completely instead of just dimming the |
| 60 | * backlight. This provides some greater power saving and the display |
| 61 | * is useless without backlight anyway. |
| 62 | */ |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 63 | if (bd->props.power != FB_BLANK_UNBLANK || |
| 64 | bd->props.fb_blank != FB_BLANK_UNBLANK) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 65 | level = 0; |
| 66 | else |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 67 | level = bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 68 | |
| 69 | del_timer_sync(&rinfo->lvds_timer); |
Linus Torvalds | 6c34bc2 | 2008-12-10 09:26:17 -0800 | [diff] [blame] | 70 | radeon_engine_idle(); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 71 | |
| 72 | lvds_gen_cntl = INREG(LVDS_GEN_CNTL); |
| 73 | if (level > 0) { |
| 74 | lvds_gen_cntl &= ~LVDS_DISPLAY_DIS; |
| 75 | if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) { |
| 76 | lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON); |
| 77 | lvds_gen_cntl |= LVDS_BLON | LVDS_EN; |
| 78 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); |
| 79 | lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; |
| 80 | lvds_gen_cntl |= |
| 81 | (radeon_bl_get_level_brightness(pdata, level) << |
| 82 | LVDS_BL_MOD_LEVEL_SHIFT); |
| 83 | lvds_gen_cntl |= LVDS_ON; |
| 84 | lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_BL_MOD_EN); |
| 85 | rinfo->pending_lvds_gen_cntl = lvds_gen_cntl; |
| 86 | mod_timer(&rinfo->lvds_timer, |
| 87 | jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay)); |
| 88 | } else { |
| 89 | lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; |
| 90 | lvds_gen_cntl |= |
| 91 | (radeon_bl_get_level_brightness(pdata, level) << |
| 92 | LVDS_BL_MOD_LEVEL_SHIFT); |
| 93 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); |
| 94 | } |
| 95 | rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; |
| 96 | rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl |
| 97 | & LVDS_STATE_MASK; |
| 98 | } else { |
| 99 | /* Asic bug, when turning off LVDS_ON, we have to make sure |
| 100 | RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off |
| 101 | */ |
| 102 | tmpPixclksCntl = INPLL(PIXCLKS_CNTL); |
| 103 | if (rinfo->is_mobility || rinfo->is_IGP) |
| 104 | OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb); |
| 105 | lvds_gen_cntl &= ~(LVDS_BL_MOD_LEVEL_MASK | LVDS_BL_MOD_EN); |
| 106 | lvds_gen_cntl |= (radeon_bl_get_level_brightness(pdata, 0) << |
| 107 | LVDS_BL_MOD_LEVEL_SHIFT); |
| 108 | lvds_gen_cntl |= LVDS_DISPLAY_DIS; |
| 109 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); |
| 110 | udelay(100); |
| 111 | lvds_gen_cntl &= ~(LVDS_ON | LVDS_EN); |
| 112 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); |
| 113 | lvds_gen_cntl &= ~(LVDS_DIGON); |
| 114 | rinfo->pending_lvds_gen_cntl = lvds_gen_cntl; |
| 115 | mod_timer(&rinfo->lvds_timer, |
| 116 | jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay)); |
| 117 | if (rinfo->is_mobility || rinfo->is_IGP) |
| 118 | OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl); |
| 119 | } |
| 120 | rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; |
| 121 | rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int radeon_bl_get_brightness(struct backlight_device *bd) |
| 127 | { |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 128 | return bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 131 | static const struct backlight_ops radeon_bl_data = { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 132 | .get_brightness = radeon_bl_get_brightness, |
| 133 | .update_status = radeon_bl_update_status, |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | void radeonfb_bl_init(struct radeonfb_info *rinfo) |
| 137 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 138 | struct backlight_properties props; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 139 | struct backlight_device *bd; |
| 140 | struct radeon_bl_privdata *pdata; |
| 141 | char name[12]; |
| 142 | |
| 143 | if (rinfo->mon1_type != MT_LCD) |
| 144 | return; |
| 145 | |
| 146 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 147 | if (!pmac_has_backlight_type("ati") && |
| 148 | !pmac_has_backlight_type("mnca")) |
| 149 | return; |
| 150 | #endif |
| 151 | |
| 152 | pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL); |
| 153 | if (!pdata) { |
| 154 | printk("radeonfb: Memory allocation failed\n"); |
| 155 | goto error; |
| 156 | } |
| 157 | |
| 158 | snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node); |
| 159 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 160 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 161 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 162 | props.max_brightness = FB_BACKLIGHT_LEVELS - 1; |
| 163 | bd = backlight_device_register(name, rinfo->info->dev, pdata, |
| 164 | &radeon_bl_data, &props); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 165 | if (IS_ERR(bd)) { |
| 166 | rinfo->info->bl_dev = NULL; |
| 167 | printk("radeonfb: Backlight registration failed\n"); |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | pdata->rinfo = rinfo; |
| 172 | |
| 173 | /* Pardon me for that hack... maybe some day we can figure out in what |
| 174 | * direction backlight should work on a given panel? |
| 175 | */ |
| 176 | pdata->negative = |
| 177 | (rinfo->family != CHIP_FAMILY_RV200 && |
| 178 | rinfo->family != CHIP_FAMILY_RV250 && |
| 179 | rinfo->family != CHIP_FAMILY_RV280 && |
| 180 | rinfo->family != CHIP_FAMILY_RV350); |
| 181 | |
| 182 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 183 | pdata->negative = pdata->negative || |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 184 | of_machine_is_compatible("PowerBook4,3") || |
| 185 | of_machine_is_compatible("PowerBook6,3") || |
| 186 | of_machine_is_compatible("PowerBook6,5"); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 187 | #endif |
| 188 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 189 | rinfo->info->bl_dev = bd; |
| 190 | fb_bl_default_curve(rinfo->info, 0, |
| 191 | 63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL, |
| 192 | 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 193 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 194 | bd->props.brightness = bd->props.max_brightness; |
| 195 | bd->props.power = FB_BLANK_UNBLANK; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 196 | backlight_update_status(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 197 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 198 | printk("radeonfb: Backlight initialized (%s)\n", name); |
| 199 | |
| 200 | return; |
| 201 | |
| 202 | error: |
| 203 | kfree(pdata); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | void radeonfb_bl_exit(struct radeonfb_info *rinfo) |
| 208 | { |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 209 | struct backlight_device *bd = rinfo->info->bl_dev; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 210 | |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 211 | if (bd) { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 212 | struct radeon_bl_privdata *pdata; |
| 213 | |
Richard Purdie | 655bfd7 | 2007-07-09 12:17:24 +0100 | [diff] [blame] | 214 | pdata = bl_get_data(bd); |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 215 | backlight_device_unregister(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 216 | kfree(pdata); |
| 217 | rinfo->info->bl_dev = NULL; |
| 218 | |
| 219 | printk("radeonfb: Backlight unloaded\n"); |
| 220 | } |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 221 | } |