1 // SPDX-License-Identifier: GPL-2.0-only
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>
8  */
9 
10 #include "radeonfb.h"
11 #include <linux/backlight.h>
12 #include <linux/slab.h>
13 
14 #ifdef CONFIG_PMAC_BACKLIGHT
15 #include <asm/backlight.h>
16 #endif
17 
18 #define MAX_RADEON_LEVEL 0xFF
19 
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 {
28 	int rlevel;
29 
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;
34 
35 	if (rlevel < 0)
36 		rlevel = 0;
37 	else if (rlevel > MAX_RADEON_LEVEL)
38 		rlevel = MAX_RADEON_LEVEL;
39 
40 	if (pdata->negative)
41 		rlevel = MAX_RADEON_LEVEL - rlevel;
42 
43 	return rlevel;
44 }
45 
46 static int radeon_bl_update_status(struct backlight_device *bd)
47 {
48 	struct radeon_bl_privdata *pdata = bl_get_data(bd);
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 	 */
60         if (bd->props.power != FB_BLANK_UNBLANK ||
61 	    bd->props.fb_blank != FB_BLANK_UNBLANK)
62 		level = 0;
63 	else
64 		level = bd->props.brightness;
65 
66 	del_timer_sync(&rinfo->lvds_timer);
67 	radeon_engine_idle();
68 
69 	lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
70 	if (level > 0) {
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;
77 			lvds_gen_cntl |=
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));
85 		} else {
86 			lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
87 			lvds_gen_cntl |=
88 				(radeon_bl_get_level_brightness(pdata, level) <<
89 				 LVDS_BL_MOD_LEVEL_SHIFT);
90 			OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
91 		}
92 		rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
93 		rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
94 			& LVDS_STATE_MASK;
95 	} else {
96 		/* Asic bug, when turning off LVDS_ON, we have to make sure
97 		   RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
98 		*/
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);
107 		udelay(100);
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);
116 	}
117 	rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
118 	rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
119 
120 	return 0;
121 }
122 
123 static const struct backlight_ops radeon_bl_data = {
124 	.update_status	= radeon_bl_update_status,
125 };
126 
127 void radeonfb_bl_init(struct radeonfb_info *rinfo)
128 {
129 	struct backlight_properties props;
130 	struct backlight_device *bd;
131 	struct radeon_bl_privdata *pdata;
132 	char name[12];
133 
134 	if (rinfo->mon1_type != MT_LCD)
135 		return;
136 
137 #ifdef CONFIG_PMAC_BACKLIGHT
138 	if (!pmac_has_backlight_type("ati") &&
139 	    !pmac_has_backlight_type("mnca"))
140 		return;
141 #endif
142 
143 	pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
144 	if (!pdata) {
145 		printk("radeonfb: Memory allocation failed\n");
146 		goto error;
147 	}
148 
149 	snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
150 
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);
156 	if (IS_ERR(bd)) {
157 		rinfo->info->bl_dev = NULL;
158 		printk("radeonfb: Backlight registration failed\n");
159 		goto error;
160 	}
161 
162 	pdata->rinfo = rinfo;
163 
164 	/* Pardon me for that hack... maybe some day we can figure out in what
165 	 * direction backlight should work on a given panel?
166 	 */
167 	pdata->negative =
168 		(rinfo->family != CHIP_FAMILY_RV200 &&
169 		 rinfo->family != CHIP_FAMILY_RV250 &&
170 		 rinfo->family != CHIP_FAMILY_RV280 &&
171 		 rinfo->family != CHIP_FAMILY_RV350);
172 
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");
178 #endif
179 
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);
184 
185 	bd->props.brightness = bd->props.max_brightness;
186 	bd->props.power = FB_BLANK_UNBLANK;
187 	backlight_update_status(bd);
188 
189 	printk("radeonfb: Backlight initialized (%s)\n", name);
190 
191 	return;
192 
193 error:
194 	kfree(pdata);
195 	return;
196 }
197 
198 void radeonfb_bl_exit(struct radeonfb_info *rinfo)
199 {
200 	struct backlight_device *bd = rinfo->info->bl_dev;
201 
202 	if (bd) {
203 		struct radeon_bl_privdata *pdata;
204 
205 		pdata = bl_get_data(bd);
206 		backlight_device_unregister(bd);
207 		kfree(pdata);
208 		rinfo->info->bl_dev = NULL;
209 
210 		printk("radeonfb: Backlight unloaded\n");
211 	}
212 }
213