Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5474c120 MH |
2 | /* |
3 | * Backlight code for ATI Radeon based graphic cards | |
4 | * | |
5 | * Copyright (c) 2000 Ani Joshi <ajoshi@kernel.crashing.org> | |
6 | * Copyright (c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org> | |
7 | * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> | |
5474c120 MH |
8 | */ |
9 | ||
10 | #include "radeonfb.h" | |
11 | #include <linux/backlight.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
5474c120 MH |
13 | |
14 | #ifdef CONFIG_PMAC_BACKLIGHT | |
15 | #include <asm/backlight.h> | |
16 | #endif | |
17 | ||
18 | #define MAX_RADEON_LEVEL 0xFF | |
19 | ||
5474c120 MH |
20 | struct radeon_bl_privdata { |
21 | struct radeonfb_info *rinfo; | |
22 | uint8_t negative; | |
23 | }; | |
24 | ||
25 | static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata, | |
26 | int level) | |
27 | { | |
5474c120 MH |
28 | int rlevel; |
29 | ||
5474c120 | 30 | /* Get and convert the value */ |
37ce69a5 | 31 | /* No locking of bl_curve since we read a single value */ |
5474c120 MH |
32 | rlevel = pdata->rinfo->info->bl_curve[level] * |
33 | FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL; | |
34 | ||
5474c120 MH |
35 | if (rlevel < 0) |
36 | rlevel = 0; | |
37 | else if (rlevel > MAX_RADEON_LEVEL) | |
38 | rlevel = MAX_RADEON_LEVEL; | |
39 | ||
cab267c6 MH |
40 | if (pdata->negative) |
41 | rlevel = MAX_RADEON_LEVEL - rlevel; | |
42 | ||
5474c120 MH |
43 | return rlevel; |
44 | } | |
45 | ||
46 | static int radeon_bl_update_status(struct backlight_device *bd) | |
47 | { | |
655bfd7a | 48 | struct radeon_bl_privdata *pdata = bl_get_data(bd); |
5474c120 MH |
49 | struct radeonfb_info *rinfo = pdata->rinfo; |
50 | u32 lvds_gen_cntl, tmpPixclksCntl; | |
51 | int level; | |
52 | ||
53 | if (rinfo->mon1_type != MT_LCD) | |
54 | return 0; | |
55 | ||
56 | /* We turn off the LCD completely instead of just dimming the | |
57 | * backlight. This provides some greater power saving and the display | |
58 | * is useless without backlight anyway. | |
59 | */ | |
c28509ef | 60 | level = backlight_get_brightness(bd); |
5474c120 MH |
61 | |
62 | del_timer_sync(&rinfo->lvds_timer); | |
6c34bc29 | 63 | radeon_engine_idle(); |
5474c120 MH |
64 | |
65 | lvds_gen_cntl = INREG(LVDS_GEN_CNTL); | |
66 | if (level > 0) { | |
67 | lvds_gen_cntl &= ~LVDS_DISPLAY_DIS; | |
68 | if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) { | |
69 | lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON); | |
70 | lvds_gen_cntl |= LVDS_BLON | LVDS_EN; | |
71 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); | |
72 | lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; | |
73 | lvds_gen_cntl |= | |
74 | (radeon_bl_get_level_brightness(pdata, level) << | |
75 | LVDS_BL_MOD_LEVEL_SHIFT); | |
76 | lvds_gen_cntl |= LVDS_ON; | |
77 | lvds_gen_cntl |= (rinfo->init_state.lvds_gen_cntl & LVDS_BL_MOD_EN); | |
78 | rinfo->pending_lvds_gen_cntl = lvds_gen_cntl; | |
79 | mod_timer(&rinfo->lvds_timer, | |
80 | jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay)); | |
81 | } else { | |
82 | lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK; | |
83 | lvds_gen_cntl |= | |
84 | (radeon_bl_get_level_brightness(pdata, level) << | |
85 | LVDS_BL_MOD_LEVEL_SHIFT); | |
86 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); | |
87 | } | |
88 | rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; | |
89 | rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl | |
90 | & LVDS_STATE_MASK; | |
91 | } else { | |
92 | /* Asic bug, when turning off LVDS_ON, we have to make sure | |
93 | RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off | |
94 | */ | |
95 | tmpPixclksCntl = INPLL(PIXCLKS_CNTL); | |
96 | if (rinfo->is_mobility || rinfo->is_IGP) | |
97 | OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb); | |
98 | lvds_gen_cntl &= ~(LVDS_BL_MOD_LEVEL_MASK | LVDS_BL_MOD_EN); | |
99 | lvds_gen_cntl |= (radeon_bl_get_level_brightness(pdata, 0) << | |
100 | LVDS_BL_MOD_LEVEL_SHIFT); | |
101 | lvds_gen_cntl |= LVDS_DISPLAY_DIS; | |
102 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); | |
103 | udelay(100); | |
104 | lvds_gen_cntl &= ~(LVDS_ON | LVDS_EN); | |
105 | OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl); | |
106 | lvds_gen_cntl &= ~(LVDS_DIGON); | |
107 | rinfo->pending_lvds_gen_cntl = lvds_gen_cntl; | |
108 | mod_timer(&rinfo->lvds_timer, | |
109 | jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay)); | |
110 | if (rinfo->is_mobility || rinfo->is_IGP) | |
111 | OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl); | |
112 | } | |
113 | rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK; | |
114 | rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK); | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
acc2472e | 119 | static const struct backlight_ops radeon_bl_data = { |
5474c120 | 120 | .update_status = radeon_bl_update_status, |
5474c120 MH |
121 | }; |
122 | ||
123 | void radeonfb_bl_init(struct radeonfb_info *rinfo) | |
124 | { | |
a19a6ee6 | 125 | struct backlight_properties props; |
5474c120 MH |
126 | struct backlight_device *bd; |
127 | struct radeon_bl_privdata *pdata; | |
128 | char name[12]; | |
129 | ||
130 | if (rinfo->mon1_type != MT_LCD) | |
131 | return; | |
132 | ||
133 | #ifdef CONFIG_PMAC_BACKLIGHT | |
134 | if (!pmac_has_backlight_type("ati") && | |
135 | !pmac_has_backlight_type("mnca")) | |
136 | return; | |
137 | #endif | |
138 | ||
139 | pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL); | |
140 | if (!pdata) { | |
141 | printk("radeonfb: Memory allocation failed\n"); | |
142 | goto error; | |
143 | } | |
144 | ||
145 | snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node); | |
146 | ||
a19a6ee6 | 147 | memset(&props, 0, sizeof(struct backlight_properties)); |
bb7ca747 | 148 | props.type = BACKLIGHT_RAW; |
a19a6ee6 MG |
149 | props.max_brightness = FB_BACKLIGHT_LEVELS - 1; |
150 | bd = backlight_device_register(name, rinfo->info->dev, pdata, | |
151 | &radeon_bl_data, &props); | |
5474c120 MH |
152 | if (IS_ERR(bd)) { |
153 | rinfo->info->bl_dev = NULL; | |
154 | printk("radeonfb: Backlight registration failed\n"); | |
155 | goto error; | |
156 | } | |
157 | ||
158 | pdata->rinfo = rinfo; | |
159 | ||
160 | /* Pardon me for that hack... maybe some day we can figure out in what | |
161 | * direction backlight should work on a given panel? | |
162 | */ | |
163 | pdata->negative = | |
164 | (rinfo->family != CHIP_FAMILY_RV200 && | |
165 | rinfo->family != CHIP_FAMILY_RV250 && | |
166 | rinfo->family != CHIP_FAMILY_RV280 && | |
167 | rinfo->family != CHIP_FAMILY_RV350); | |
168 | ||
169 | #ifdef CONFIG_PMAC_BACKLIGHT | |
170 | pdata->negative = pdata->negative || | |
71a157e8 GL |
171 | of_machine_is_compatible("PowerBook4,3") || |
172 | of_machine_is_compatible("PowerBook6,3") || | |
173 | of_machine_is_compatible("PowerBook6,5"); | |
5474c120 MH |
174 | #endif |
175 | ||
5474c120 MH |
176 | rinfo->info->bl_dev = bd; |
177 | fb_bl_default_curve(rinfo->info, 0, | |
178 | 63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL, | |
179 | 217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL); | |
5474c120 | 180 | |
599a52d1 RP |
181 | bd->props.brightness = bd->props.max_brightness; |
182 | bd->props.power = FB_BLANK_UNBLANK; | |
28ee086d | 183 | backlight_update_status(bd); |
5474c120 | 184 | |
5474c120 MH |
185 | printk("radeonfb: Backlight initialized (%s)\n", name); |
186 | ||
187 | return; | |
188 | ||
189 | error: | |
190 | kfree(pdata); | |
191 | return; | |
192 | } | |
193 | ||
194 | void radeonfb_bl_exit(struct radeonfb_info *rinfo) | |
195 | { | |
37ce69a5 | 196 | struct backlight_device *bd = rinfo->info->bl_dev; |
5474c120 | 197 | |
37ce69a5 | 198 | if (bd) { |
5474c120 MH |
199 | struct radeon_bl_privdata *pdata; |
200 | ||
655bfd7a | 201 | pdata = bl_get_data(bd); |
37ce69a5 | 202 | backlight_device_unregister(bd); |
5474c120 MH |
203 | kfree(pdata); |
204 | rinfo->info->bl_dev = NULL; | |
205 | ||
206 | printk("radeonfb: Backlight unloaded\n"); | |
207 | } | |
5474c120 | 208 | } |