blob: db572df7e1ef0fddd4d267523bf29daef6584f03 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070016
17#ifdef CONFIG_PMAC_BACKLIGHT
18#include <asm/backlight.h>
19#endif
20
21#define MAX_RADEON_LEVEL 0xFF
22
Michael Hanselmann5474c122006-06-25 05:47:08 -070023struct radeon_bl_privdata {
24 struct radeonfb_info *rinfo;
25 uint8_t negative;
26};
27
28static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata,
29 int level)
30{
Michael Hanselmann5474c122006-06-25 05:47:08 -070031 int rlevel;
32
Michael Hanselmann5474c122006-06-25 05:47:08 -070033 /* Get and convert the value */
Richard Purdie37ce69a2007-02-10 14:10:33 +000034 /* No locking of bl_curve since we read a single value */
Michael Hanselmann5474c122006-06-25 05:47:08 -070035 rlevel = pdata->rinfo->info->bl_curve[level] *
36 FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL;
37
Michael Hanselmann5474c122006-06-25 05:47:08 -070038 if (rlevel < 0)
39 rlevel = 0;
40 else if (rlevel > MAX_RADEON_LEVEL)
41 rlevel = MAX_RADEON_LEVEL;
42
Michael Hanselmanncab267c2006-06-28 04:26:55 -070043 if (pdata->negative)
44 rlevel = MAX_RADEON_LEVEL - rlevel;
45
Michael Hanselmann5474c122006-06-25 05:47:08 -070046 return rlevel;
47}
48
49static int radeon_bl_update_status(struct backlight_device *bd)
50{
Richard Purdie655bfd72007-07-09 12:17:24 +010051 struct radeon_bl_privdata *pdata = bl_get_data(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -070052 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 Purdie599a52d2007-02-10 23:07:48 +000063 if (bd->props.power != FB_BLANK_UNBLANK ||
64 bd->props.fb_blank != FB_BLANK_UNBLANK)
Michael Hanselmann5474c122006-06-25 05:47:08 -070065 level = 0;
66 else
Richard Purdie599a52d2007-02-10 23:07:48 +000067 level = bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -070068
69 del_timer_sync(&rinfo->lvds_timer);
Linus Torvalds6c34bc22008-12-10 09:26:17 -080070 radeon_engine_idle();
Michael Hanselmann5474c122006-06-25 05:47:08 -070071
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
126static int radeon_bl_get_brightness(struct backlight_device *bd)
127{
Richard Purdie599a52d2007-02-10 23:07:48 +0000128 return bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700129}
130
Lionel Debrouxacc24722010-11-16 14:14:02 +0100131static const struct backlight_ops radeon_bl_data = {
Michael Hanselmann5474c122006-06-25 05:47:08 -0700132 .get_brightness = radeon_bl_get_brightness,
133 .update_status = radeon_bl_update_status,
Michael Hanselmann5474c122006-06-25 05:47:08 -0700134};
135
136void radeonfb_bl_init(struct radeonfb_info *rinfo)
137{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500138 struct backlight_properties props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700139 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 Garretta19a6ee2010-02-17 16:39:44 -0500160 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700161 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500162 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
163 bd = backlight_device_register(name, rinfo->info->dev, pdata,
164 &radeon_bl_data, &props);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700165 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 Likely71a157e2010-02-01 21:34:14 -0700184 of_machine_is_compatible("PowerBook4,3") ||
185 of_machine_is_compatible("PowerBook6,3") ||
186 of_machine_is_compatible("PowerBook6,5");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700187#endif
188
Michael Hanselmann5474c122006-06-25 05:47:08 -0700189 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 Hanselmann5474c122006-06-25 05:47:08 -0700193
Richard Purdie599a52d2007-02-10 23:07:48 +0000194 bd->props.brightness = bd->props.max_brightness;
195 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +0000196 backlight_update_status(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700197
Michael Hanselmann5474c122006-06-25 05:47:08 -0700198 printk("radeonfb: Backlight initialized (%s)\n", name);
199
200 return;
201
202error:
203 kfree(pdata);
204 return;
205}
206
207void radeonfb_bl_exit(struct radeonfb_info *rinfo)
208{
Richard Purdie37ce69a2007-02-10 14:10:33 +0000209 struct backlight_device *bd = rinfo->info->bl_dev;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700210
Richard Purdie37ce69a2007-02-10 14:10:33 +0000211 if (bd) {
Michael Hanselmann5474c122006-06-25 05:47:08 -0700212 struct radeon_bl_privdata *pdata;
213
Richard Purdie655bfd72007-07-09 12:17:24 +0100214 pdata = bl_get_data(bd);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000215 backlight_device_unregister(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700216 kfree(pdata);
217 rinfo->info->bl_dev = NULL;
218
219 printk("radeonfb: Backlight unloaded\n");
220 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700221}