1 /*
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>
15 #include <linux/slab.h>
16 
17 #ifdef CONFIG_PMAC_BACKLIGHT
18 #include <asm/backlight.h>
19 #endif
20 
21 #define MAX_RADEON_LEVEL 0xFF
22 
23 struct radeon_bl_privdata {
24 	struct radeonfb_info *rinfo;
25 	uint8_t negative;
26 };
27 
28 static int radeon_bl_get_level_brightness(struct radeon_bl_privdata *pdata,
29 		int level)
30 {
31 	int rlevel;
32 
33 	/* Get and convert the value */
34 	/* No locking of bl_curve since we read a single value */
35 	rlevel = pdata->rinfo->info->bl_curve[level] *
36 		 FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL;
37 
38 	if (rlevel < 0)
39 		rlevel = 0;
40 	else if (rlevel > MAX_RADEON_LEVEL)
41 		rlevel = MAX_RADEON_LEVEL;
42 
43 	if (pdata->negative)
44 		rlevel = MAX_RADEON_LEVEL - rlevel;
45 
46 	return rlevel;
47 }
48 
49 static int radeon_bl_update_status(struct backlight_device *bd)
50 {
51 	struct radeon_bl_privdata *pdata = bl_get_data(bd);
52 	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 	 */
63         if (bd->props.power != FB_BLANK_UNBLANK ||
64 	    bd->props.fb_blank != FB_BLANK_UNBLANK)
65 		level = 0;
66 	else
67 		level = bd->props.brightness;
68 
69 	del_timer_sync(&rinfo->lvds_timer);
70 	radeon_engine_idle();
71 
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 
126 static int radeon_bl_get_brightness(struct backlight_device *bd)
127 {
128 	return bd->props.brightness;
129 }
130 
131 static const struct backlight_ops radeon_bl_data = {
132 	.get_brightness = radeon_bl_get_brightness,
133 	.update_status	= radeon_bl_update_status,
134 };
135 
136 void radeonfb_bl_init(struct radeonfb_info *rinfo)
137 {
138 	struct backlight_properties props;
139 	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 
160 	memset(&props, 0, sizeof(struct backlight_properties));
161 	props.type = BACKLIGHT_RAW;
162 	props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
163 	bd = backlight_device_register(name, rinfo->info->dev, pdata,
164 				       &radeon_bl_data, &props);
165 	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 ||
184 		of_machine_is_compatible("PowerBook4,3") ||
185 		of_machine_is_compatible("PowerBook6,3") ||
186 		of_machine_is_compatible("PowerBook6,5");
187 #endif
188 
189 	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);
193 
194 	bd->props.brightness = bd->props.max_brightness;
195 	bd->props.power = FB_BLANK_UNBLANK;
196 	backlight_update_status(bd);
197 
198 	printk("radeonfb: Backlight initialized (%s)\n", name);
199 
200 	return;
201 
202 error:
203 	kfree(pdata);
204 	return;
205 }
206 
207 void radeonfb_bl_exit(struct radeonfb_info *rinfo)
208 {
209 	struct backlight_device *bd = rinfo->info->bl_dev;
210 
211 	if (bd) {
212 		struct radeon_bl_privdata *pdata;
213 
214 		pdata = bl_get_data(bd);
215 		backlight_device_unregister(bd);
216 		kfree(pdata);
217 		rinfo->info->bl_dev = NULL;
218 
219 		printk("radeonfb: Backlight unloaded\n");
220 	}
221 }
222