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 level
= backlight_get_brightness(bd
);
62 del_timer_sync(&rinfo
->lvds_timer
);
65 lvds_gen_cntl
= INREG(LVDS_GEN_CNTL
);
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
;
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
));
82 lvds_gen_cntl
&= ~LVDS_BL_MOD_LEVEL_MASK
;
84 (radeon_bl_get_level_brightness(pdata
, level
) <<
85 LVDS_BL_MOD_LEVEL_SHIFT
);
86 OUTREG(LVDS_GEN_CNTL
, lvds_gen_cntl
);
88 rinfo
->init_state
.lvds_gen_cntl
&= ~LVDS_STATE_MASK
;
89 rinfo
->init_state
.lvds_gen_cntl
|= rinfo
->pending_lvds_gen_cntl
92 /* Asic bug, when turning off LVDS_ON, we have to make sure
93 RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
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
);
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
);
113 rinfo
->init_state
.lvds_gen_cntl
&= ~LVDS_STATE_MASK
;
114 rinfo
->init_state
.lvds_gen_cntl
|= (lvds_gen_cntl
& LVDS_STATE_MASK
);
119 static const struct backlight_ops radeon_bl_data
= {
120 .update_status
= radeon_bl_update_status
,
123 void radeonfb_bl_init(struct radeonfb_info
*rinfo
)
125 struct backlight_properties props
;
126 struct backlight_device
*bd
;
127 struct radeon_bl_privdata
*pdata
;
130 if (rinfo
->mon1_type
!= MT_LCD
)
133 #ifdef CONFIG_PMAC_BACKLIGHT
134 if (!pmac_has_backlight_type("ati") &&
135 !pmac_has_backlight_type("mnca"))
139 pdata
= kmalloc(sizeof(struct radeon_bl_privdata
), GFP_KERNEL
);
141 printk("radeonfb: Memory allocation failed\n");
145 snprintf(name
, sizeof(name
), "radeonbl%d", rinfo
->info
->node
);
147 memset(&props
, 0, sizeof(struct backlight_properties
));
148 props
.type
= BACKLIGHT_RAW
;
149 props
.max_brightness
= FB_BACKLIGHT_LEVELS
- 1;
150 bd
= backlight_device_register(name
, rinfo
->info
->device
, pdata
,
151 &radeon_bl_data
, &props
);
153 rinfo
->info
->bl_dev
= NULL
;
154 printk("radeonfb: Backlight registration failed\n");
158 pdata
->rinfo
= rinfo
;
160 /* Pardon me for that hack... maybe some day we can figure out in what
161 * direction backlight should work on a given panel?
164 (rinfo
->family
!= CHIP_FAMILY_RV200
&&
165 rinfo
->family
!= CHIP_FAMILY_RV250
&&
166 rinfo
->family
!= CHIP_FAMILY_RV280
&&
167 rinfo
->family
!= CHIP_FAMILY_RV350
);
169 #ifdef CONFIG_PMAC_BACKLIGHT
170 pdata
->negative
= pdata
->negative
||
171 of_machine_is_compatible("PowerBook4,3") ||
172 of_machine_is_compatible("PowerBook6,3") ||
173 of_machine_is_compatible("PowerBook6,5");
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
);
181 bd
->props
.brightness
= bd
->props
.max_brightness
;
182 bd
->props
.power
= BACKLIGHT_POWER_ON
;
183 backlight_update_status(bd
);
185 printk("radeonfb: Backlight initialized (%s)\n", name
);
194 void radeonfb_bl_exit(struct radeonfb_info
*rinfo
)
196 struct backlight_device
*bd
= rinfo
->info
->bl_dev
;
199 struct radeon_bl_privdata
*pdata
;
201 pdata
= bl_get_data(bd
);
202 backlight_device_unregister(bd
);
204 rinfo
->info
->bl_dev
= NULL
;
206 printk("radeonfb: Backlight unloaded\n");