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
23struct radeon_bl_privdata {
24	struct radeonfb_info *rinfo;
25	uint8_t negative;
26};
27
28static 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
49static 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
126static const struct backlight_ops radeon_bl_data = {
127	.update_status	= radeon_bl_update_status,
128};
129
130void radeonfb_bl_init(struct radeonfb_info *rinfo)
131{
132	struct backlight_properties props;
133	struct backlight_device *bd;
134	struct radeon_bl_privdata *pdata;
135	char name[12];
136
137	if (rinfo->mon1_type != MT_LCD)
138		return;
139
140#ifdef CONFIG_PMAC_BACKLIGHT
141	if (!pmac_has_backlight_type("ati") &&
142	    !pmac_has_backlight_type("mnca"))
143		return;
144#endif
145
146	pdata = kmalloc(sizeof(struct radeon_bl_privdata), GFP_KERNEL);
147	if (!pdata) {
148		printk("radeonfb: Memory allocation failed\n");
149		goto error;
150	}
151
152	snprintf(name, sizeof(name), "radeonbl%d", rinfo->info->node);
153
154	memset(&props, 0, sizeof(struct backlight_properties));
155	props.type = BACKLIGHT_RAW;
156	props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
157	bd = backlight_device_register(name, rinfo->info->dev, pdata,
158				       &radeon_bl_data, &props);
159	if (IS_ERR(bd)) {
160		rinfo->info->bl_dev = NULL;
161		printk("radeonfb: Backlight registration failed\n");
162		goto error;
163	}
164
165	pdata->rinfo = rinfo;
166
167	/* Pardon me for that hack... maybe some day we can figure out in what
168	 * direction backlight should work on a given panel?
169	 */
170	pdata->negative =
171		(rinfo->family != CHIP_FAMILY_RV200 &&
172		 rinfo->family != CHIP_FAMILY_RV250 &&
173		 rinfo->family != CHIP_FAMILY_RV280 &&
174		 rinfo->family != CHIP_FAMILY_RV350);
175
176#ifdef CONFIG_PMAC_BACKLIGHT
177	pdata->negative = pdata->negative ||
178		of_machine_is_compatible("PowerBook4,3") ||
179		of_machine_is_compatible("PowerBook6,3") ||
180		of_machine_is_compatible("PowerBook6,5");
181#endif
182
183	rinfo->info->bl_dev = bd;
184	fb_bl_default_curve(rinfo->info, 0,
185		 63 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL,
186		217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL);
187
188	bd->props.brightness = bd->props.max_brightness;
189	bd->props.power = FB_BLANK_UNBLANK;
190	backlight_update_status(bd);
191
192	printk("radeonfb: Backlight initialized (%s)\n", name);
193
194	return;
195
196error:
197	kfree(pdata);
198	return;
199}
200
201void radeonfb_bl_exit(struct radeonfb_info *rinfo)
202{
203	struct backlight_device *bd = rinfo->info->bl_dev;
204
205	if (bd) {
206		struct radeon_bl_privdata *pdata;
207
208		pdata = bl_get_data(bd);
209		backlight_device_unregister(bd);
210		kfree(pdata);
211		rinfo->info->bl_dev = NULL;
212
213		printk("radeonfb: Backlight unloaded\n");
214	}
215}
216