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 	level = backlight_get_brightness(bd);
61 
62 	del_timer_sync(&rinfo->lvds_timer);
63 	radeon_engine_idle();
64 
65 	lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
66 	if (level > 0) {
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;
73 			lvds_gen_cntl |=
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));
81 		} else {
82 			lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
83 			lvds_gen_cntl |=
84 				(radeon_bl_get_level_brightness(pdata, level) <<
85 				 LVDS_BL_MOD_LEVEL_SHIFT);
86 			OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
87 		}
88 		rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
89 		rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
90 			& LVDS_STATE_MASK;
91 	} else {
92 		/* Asic bug, when turning off LVDS_ON, we have to make sure
93 		   RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
94 		*/
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);
103 		udelay(100);
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);
112 	}
113 	rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
114 	rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
115 
116 	return 0;
117 }
118 
119 static const struct backlight_ops radeon_bl_data = {
120 	.update_status	= radeon_bl_update_status,
121 };
122 
123 void radeonfb_bl_init(struct radeonfb_info *rinfo)
124 {
125 	struct backlight_properties props;
126 	struct backlight_device *bd;
127 	struct radeon_bl_privdata *pdata;
128 	char name[12];
129 
130 	if (rinfo->mon1_type != MT_LCD)
131 		return;
132 
133 #ifdef CONFIG_PMAC_BACKLIGHT
134 	if (!pmac_has_backlight_type("ati") &&
135 	    !pmac_has_backlight_type("mnca"))
136 		return;
137 #endif
138 
139 	pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
140 	if (!pdata) {
141 		printk("radeonfb: Memory allocation failed\n");
142 		goto error;
143 	}
144 
145 	snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
146 
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);
152 	if (IS_ERR(bd)) {
153 		rinfo->info->bl_dev = NULL;
154 		printk("radeonfb: Backlight registration failed\n");
155 		goto error;
156 	}
157 
158 	pdata->rinfo = rinfo;
159 
160 	/* Pardon me for that hack... maybe some day we can figure out in what
161 	 * direction backlight should work on a given panel?
162 	 */
163 	pdata->negative =
164 		(rinfo->family != CHIP_FAMILY_RV200 &&
165 		 rinfo->family != CHIP_FAMILY_RV250 &&
166 		 rinfo->family != CHIP_FAMILY_RV280 &&
167 		 rinfo->family != CHIP_FAMILY_RV350);
168 
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");
174 #endif
175 
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);
180 
181 	bd->props.brightness = bd->props.max_brightness;
182 	bd->props.power = FB_BLANK_UNBLANK;
183 	backlight_update_status(bd);
184 
185 	printk("radeonfb: Backlight initialized (%s)\n", name);
186 
187 	return;
188 
189 error:
190 	kfree(pdata);
191 	return;
192 }
193 
194 void radeonfb_bl_exit(struct radeonfb_info *rinfo)
195 {
196 	struct backlight_device *bd = rinfo->info->bl_dev;
197 
198 	if (bd) {
199 		struct radeon_bl_privdata *pdata;
200 
201 		pdata = bl_get_data(bd);
202 		backlight_device_unregister(bd);
203 		kfree(pdata);
204 		rinfo->info->bl_dev = NULL;
205 
206 		printk("radeonfb: Backlight unloaded\n");
207 	}
208 }
209