1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight code for ATI Radeon based graphic cards
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>
11 #include <linux/backlight.h>
12 #include <linux/slab.h>
14 #ifdef CONFIG_PMAC_BACKLIGHT
15 #include <asm/backlight.h>
18 #define MAX_RADEON_LEVEL 0xFF
20 struct radeon_bl_privdata
{
21 struct radeonfb_info
*rinfo
;
25 static int radeon_bl_get_level_brightness(struct radeon_bl_privdata
*pdata
,
30 /* Get and convert the value */
31 /* No locking of bl_curve since we read a single value */
32 rlevel
= pdata
->rinfo
->info
->bl_curve
[level
] *
33 FB_BACKLIGHT_MAX
/ MAX_RADEON_LEVEL
;
37 else if (rlevel
> MAX_RADEON_LEVEL
)
38 rlevel
= MAX_RADEON_LEVEL
;
41 rlevel
= MAX_RADEON_LEVEL
- rlevel
;
46 static int radeon_bl_update_status(struct backlight_device
*bd
)
48 struct radeon_bl_privdata
*pdata
= bl_get_data(bd
);
49 struct radeonfb_info
*rinfo
= pdata
->rinfo
;
50 u32 lvds_gen_cntl
, tmpPixclksCntl
;
53 if (rinfo
->mon1_type
!= MT_LCD
)
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.
60 if (bd
->props
.power
!= FB_BLANK_UNBLANK
||
61 bd
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
64 level
= bd
->props
.brightness
;
66 del_timer_sync(&rinfo
->lvds_timer
);
69 lvds_gen_cntl
= INREG(LVDS_GEN_CNTL
);
71 lvds_gen_cntl
&= ~LVDS_DISPLAY_DIS
;
72 if (!(lvds_gen_cntl
& LVDS_BLON
) || !(lvds_gen_cntl
& LVDS_ON
)) {
73 lvds_gen_cntl
|= (rinfo
->init_state
.lvds_gen_cntl
& LVDS_DIGON
);
74 lvds_gen_cntl
|= LVDS_BLON
| LVDS_EN
;
75 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
76 lvds_gen_cntl
&= ~LVDS_BL_MOD_LEVEL_MASK
;
78 (radeon_bl_get_level_brightness(pdata
, level
) <<
79 LVDS_BL_MOD_LEVEL_SHIFT
);
80 lvds_gen_cntl
|= LVDS_ON
;
81 lvds_gen_cntl
|= (rinfo
->init_state
.lvds_gen_cntl
& LVDS_BL_MOD_EN
);
82 rinfo
->pending_lvds_gen_cntl
= lvds_gen_cntl
;
83 mod_timer(&rinfo
->lvds_timer
,
84 jiffies
+ msecs_to_jiffies(rinfo
->panel_info
.pwr_delay
));
86 lvds_gen_cntl
&= ~LVDS_BL_MOD_LEVEL_MASK
;
88 (radeon_bl_get_level_brightness(pdata
, level
) <<
89 LVDS_BL_MOD_LEVEL_SHIFT
);
90 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
92 rinfo
->init_state
.lvds_gen_cntl
&= ~LVDS_STATE_MASK
;
93 rinfo
->init_state
.lvds_gen_cntl
|= rinfo
->pending_lvds_gen_cntl
96 /* Asic bug, when turning off LVDS_ON, we have to make sure
97 RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
99 tmpPixclksCntl
= INPLL(PIXCLKS_CNTL
);
100 if (rinfo
->is_mobility
|| rinfo
->is_IGP
)
101 OUTPLLP(PIXCLKS_CNTL
, 0, ~PIXCLK_LVDS_ALWAYS_ONb
);
102 lvds_gen_cntl
&= ~(LVDS_BL_MOD_LEVEL_MASK
| LVDS_BL_MOD_EN
);
103 lvds_gen_cntl
|= (radeon_bl_get_level_brightness(pdata
, 0) <<
104 LVDS_BL_MOD_LEVEL_SHIFT
);
105 lvds_gen_cntl
|= LVDS_DISPLAY_DIS
;
106 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
108 lvds_gen_cntl
&= ~(LVDS_ON
| LVDS_EN
);
109 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
110 lvds_gen_cntl
&= ~(LVDS_DIGON
);
111 rinfo
->pending_lvds_gen_cntl
= lvds_gen_cntl
;
112 mod_timer(&rinfo
->lvds_timer
,
113 jiffies
+ msecs_to_jiffies(rinfo
->panel_info
.pwr_delay
));
114 if (rinfo
->is_mobility
|| rinfo
->is_IGP
)
115 OUTPLL(PIXCLKS_CNTL
, tmpPixclksCntl
);
117 rinfo
->init_state
.lvds_gen_cntl
&= ~LVDS_STATE_MASK
;
118 rinfo
->init_state
.lvds_gen_cntl
|= (lvds_gen_cntl
& LVDS_STATE_MASK
);
123 static const struct backlight_ops radeon_bl_data
= {
124 .update_status
= radeon_bl_update_status
,
127 void radeonfb_bl_init(struct radeonfb_info
*rinfo
)
129 struct backlight_properties props
;
130 struct backlight_device
*bd
;
131 struct radeon_bl_privdata
*pdata
;
134 if (rinfo
->mon1_type
!= MT_LCD
)
137 #ifdef CONFIG_PMAC_BACKLIGHT
138 if (!pmac_has_backlight_type("ati") &&
139 !pmac_has_backlight_type("mnca"))
143 pdata
= kmalloc(sizeof(struct radeon_bl_privdata
), GFP_KERNEL
);
145 printk("radeonfb: Memory allocation failed\n");
149 snprintf(name
, sizeof(name
), "radeonbl%d", rinfo
->info
->node
);
151 memset(&props
, 0, sizeof(struct backlight_properties
));
152 props
.type
= BACKLIGHT_RAW
;
153 props
.max_brightness
= FB_BACKLIGHT_LEVELS
- 1;
154 bd
= backlight_device_register(name
, rinfo
->info
->dev
, pdata
,
155 &radeon_bl_data
, &props
);
157 rinfo
->info
->bl_dev
= NULL
;
158 printk("radeonfb: Backlight registration failed\n");
162 pdata
->rinfo
= rinfo
;
164 /* Pardon me for that hack... maybe some day we can figure out in what
165 * direction backlight should work on a given panel?
168 (rinfo
->family
!= CHIP_FAMILY_RV200
&&
169 rinfo
->family
!= CHIP_FAMILY_RV250
&&
170 rinfo
->family
!= CHIP_FAMILY_RV280
&&
171 rinfo
->family
!= CHIP_FAMILY_RV350
);
173 #ifdef CONFIG_PMAC_BACKLIGHT
174 pdata
->negative
= pdata
->negative
||
175 of_machine_is_compatible("PowerBook4,3") ||
176 of_machine_is_compatible("PowerBook6,3") ||
177 of_machine_is_compatible("PowerBook6,5");
180 rinfo
->info
->bl_dev
= bd
;
181 fb_bl_default_curve(rinfo
->info
, 0,
182 63 * FB_BACKLIGHT_MAX
/ MAX_RADEON_LEVEL
,
183 217 * FB_BACKLIGHT_MAX
/ MAX_RADEON_LEVEL
);
185 bd
->props
.brightness
= bd
->props
.max_brightness
;
186 bd
->props
.power
= FB_BLANK_UNBLANK
;
187 backlight_update_status(bd
);
189 printk("radeonfb: Backlight initialized (%s)\n", name
);
198 void radeonfb_bl_exit(struct radeonfb_info
*rinfo
)
200 struct backlight_device
*bd
= rinfo
->info
->bl_dev
;
203 struct radeon_bl_privdata
*pdata
;
205 pdata
= bl_get_data(bd
);
206 backlight_device_unregister(bd
);
208 rinfo
->info
->bl_dev
= NULL
;
210 printk("radeonfb: Backlight unloaded\n");