blob: f94e4616788d9761c78ad554f72631474d7d6065 [file] [log] [blame]
Michael Hanselmann5474c122006-06-25 05:47:08 -07001/*
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
22static struct backlight_properties radeon_bl_data;
23
24struct radeon_bl_privdata {
25 struct radeonfb_info *rinfo;
26 uint8_t negative;
27};
28
29static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata,
30 int level)
31{
Michael Hanselmann5474c122006-06-25 05:47:08 -070032 int rlevel;
33
Michael Hanselmann5474c122006-06-25 05:47:08 -070034 /* Get and convert the value */
Richard Purdie37ce69a2007-02-10 14:10:33 +000035 /* No locking of bl_curve since we read a single value */
Michael Hanselmann5474c122006-06-25 05:47:08 -070036 rlevel = pdata->rinfo->info->bl_curve[level] *
37 FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL;
38
Michael Hanselmann5474c122006-06-25 05:47:08 -070039 if (rlevel < 0)
40 rlevel = 0;
41 else if (rlevel > MAX_RADEON_LEVEL)
42 rlevel = MAX_RADEON_LEVEL;
43
Michael Hanselmanncab267c2006-06-28 04:26:55 -070044 if (pdata->negative)
45 rlevel = MAX_RADEON_LEVEL - rlevel;
46
Michael Hanselmann5474c122006-06-25 05:47:08 -070047 return rlevel;
48}
49
50static int radeon_bl_update_status(struct backlight_device *bd)
51{
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;
55 int level;
56
57 if (rinfo->mon1_type != MT_LCD)
58 return 0;
59
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.
63 */
64 if (bd->props->power != FB_BLANK_UNBLANK ||
65 bd->props->fb_blank != FB_BLANK_UNBLANK)
66 level = 0;
67 else
68 level = bd->props->brightness;
69
70 del_timer_sync(&rinfo->lvds_timer);
71 radeon_engine_idle();
72
73 lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
74 if (level > 0) {
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;
81 lvds_gen_cntl |=
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));
89 } else {
90 lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
91 lvds_gen_cntl |=
92 (radeon_bl_get_level_brightness(pdata, level) <<
93 LVDS_BL_MOD_LEVEL_SHIFT);
94 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
95 }
96 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
97 rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
98 & LVDS_STATE_MASK;
99 } else {
100 /* Asic bug, when turning off LVDS_ON, we have to make sure
101 RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
102 */
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);
111 udelay(100);
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);
120 }
121 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
122 rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
123
124 return 0;
125}
126
127static int radeon_bl_get_brightness(struct backlight_device *bd)
128{
129 return bd->props->brightness;
130}
131
132static struct backlight_properties radeon_bl_data = {
Michael Hanselmann5474c122006-06-25 05:47:08 -0700133 .get_brightness = radeon_bl_get_brightness,
134 .update_status = radeon_bl_update_status,
135 .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
136};
137
138void radeonfb_bl_init(struct radeonfb_info *rinfo)
139{
140 struct backlight_device *bd;
141 struct radeon_bl_privdata *pdata;
142 char name[12];
143
144 if (rinfo->mon1_type != MT_LCD)
145 return;
146
147#ifdef CONFIG_PMAC_BACKLIGHT
148 if (!pmac_has_backlight_type("ati") &&
149 !pmac_has_backlight_type("mnca"))
150 return;
151#endif
152
153 pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
154 if (!pdata) {
155 printk("radeonfb: Memory allocation failed\n");
156 goto error;
157 }
158
159 snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
160
James Simmonsa8274d52006-12-19 12:56:16 -0800161 bd = backlight_device_register(name, rinfo->info->dev, pdata, &radeon_bl_data);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700162 if (IS_ERR(bd)) {
163 rinfo->info->bl_dev = NULL;
164 printk("radeonfb: Backlight registration failed\n");
165 goto error;
166 }
167
168 pdata->rinfo = rinfo;
169
170 /* Pardon me for that hack... maybe some day we can figure out in what
171 * direction backlight should work on a given panel?
172 */
173 pdata->negative =
174 (rinfo->family != CHIP_FAMILY_RV200 &&
175 rinfo->family != CHIP_FAMILY_RV250 &&
176 rinfo->family != CHIP_FAMILY_RV280 &&
177 rinfo->family != CHIP_FAMILY_RV350);
178
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");
184#endif
185
Michael Hanselmann5474c122006-06-25 05:47:08 -0700186 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);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700190
Michael Hanselmann5474c122006-06-25 05:47:08 -0700191 bd->props->brightness = radeon_bl_data.max_brightness;
192 bd->props->power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +0000193 backlight_update_status(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700194
195#ifdef CONFIG_PMAC_BACKLIGHT
196 mutex_lock(&pmac_backlight_mutex);
197 if (!pmac_backlight)
198 pmac_backlight = bd;
199 mutex_unlock(&pmac_backlight_mutex);
200#endif
201
202 printk("radeonfb: Backlight initialized (%s)\n", name);
203
204 return;
205
206error:
207 kfree(pdata);
208 return;
209}
210
211void radeonfb_bl_exit(struct radeonfb_info *rinfo)
212{
Richard Purdie37ce69a2007-02-10 14:10:33 +0000213 struct backlight_device *bd = rinfo->info->bl_dev;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700214
Richard Purdie37ce69a2007-02-10 14:10:33 +0000215 if (bd) {
Michael Hanselmann5474c122006-06-25 05:47:08 -0700216 struct radeon_bl_privdata *pdata;
217
218#ifdef CONFIG_PMAC_BACKLIGHT
Richard Purdie37ce69a2007-02-10 14:10:33 +0000219 mutex_lock(&pmac_backlight_mutex);
220 if (pmac_backlight == bd)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700221 pmac_backlight = NULL;
Richard Purdie37ce69a2007-02-10 14:10:33 +0000222 mutex_unlock(&pmac_backlight_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700223#endif
Richard Purdie37ce69a2007-02-10 14:10:33 +0000224 pdata = class_get_devdata(&bd->class_dev);
225 backlight_device_unregister(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700226 kfree(pdata);
227 rinfo->info->bl_dev = NULL;
228
229 printk("radeonfb: Backlight unloaded\n");
230 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700231}