Merge commit 'v2.6.34-rc2' into perf/core
[linux-2.6-block.git] / drivers / video / backlight / cr_bllcd.c
CommitLineData
dbe7e429
AH
1/*
2 * Copyright (c) Intel Corp. 2007.
3 * All Rights Reserved.
4 *
5 * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
6 * develop this driver.
7 *
8 * This file is part of the Carillo Ranch video subsystem driver.
9 * The Carillo Ranch video subsystem driver is free software;
10 * you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The Carillo Ranch video subsystem driver is distributed
16 * in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this driver; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
27 * Alan Hourihane <alanh-at-tungstengraphics-dot-com>
28 */
29
30#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/platform_device.h>
34#include <linux/mutex.h>
35#include <linux/fb.h>
36#include <linux/backlight.h>
37#include <linux/lcd.h>
38#include <linux/pci.h>
dbe7e429
AH
39
40/* The LVDS- and panel power controls sits on the
41 * GPIO port of the ISA bridge.
42 */
43
44#define CRVML_DEVICE_LPC 0x27B8
45#define CRVML_REG_GPIOBAR 0x48
46#define CRVML_REG_GPIOEN 0x4C
47#define CRVML_GPIOEN_BIT (1 << 4)
48#define CRVML_PANEL_PORT 0x38
49#define CRVML_LVDS_ON 0x00000001
50#define CRVML_PANEL_ON 0x00000002
51#define CRVML_BACKLIGHT_OFF 0x00000004
52
53/* The PLL Clock register sits on Host bridge */
54#define CRVML_DEVICE_MCH 0x5001
55#define CRVML_REG_MCHBAR 0x44
56#define CRVML_REG_MCHEN 0x54
57#define CRVML_MCHEN_BIT (1 << 28)
58#define CRVML_MCHMAP_SIZE 4096
59#define CRVML_REG_CLOCK 0xc3c
60#define CRVML_CLOCK_SHIFT 8
61#define CRVML_CLOCK_MASK 0x00000f00
62
63static struct pci_dev *lpc_dev;
64static u32 gpio_bar;
65
66struct cr_panel {
67 struct backlight_device *cr_backlight_device;
68 struct lcd_device *cr_lcd_device;
69};
70
71static int cr_backlight_set_intensity(struct backlight_device *bd)
72{
73 int intensity = bd->props.brightness;
74 u32 addr = gpio_bar + CRVML_PANEL_PORT;
75 u32 cur = inl(addr);
76
77 if (bd->props.power == FB_BLANK_UNBLANK)
78 intensity = FB_BLANK_UNBLANK;
79 if (bd->props.fb_blank == FB_BLANK_UNBLANK)
80 intensity = FB_BLANK_UNBLANK;
81 if (bd->props.power == FB_BLANK_POWERDOWN)
82 intensity = FB_BLANK_POWERDOWN;
83 if (bd->props.fb_blank == FB_BLANK_POWERDOWN)
84 intensity = FB_BLANK_POWERDOWN;
85
86 if (intensity == FB_BLANK_UNBLANK) { /* FULL ON */
87 cur &= ~CRVML_BACKLIGHT_OFF;
88 outl(cur, addr);
89 } else if (intensity == FB_BLANK_POWERDOWN) { /* OFF */
90 cur |= CRVML_BACKLIGHT_OFF;
91 outl(cur, addr);
92 } /* anything else, don't bother */
93
94 return 0;
95}
96
97static int cr_backlight_get_intensity(struct backlight_device *bd)
98{
99 u32 addr = gpio_bar + CRVML_PANEL_PORT;
100 u32 cur = inl(addr);
101 u8 intensity;
102
103 if (cur & CRVML_BACKLIGHT_OFF)
104 intensity = FB_BLANK_POWERDOWN;
105 else
106 intensity = FB_BLANK_UNBLANK;
107
108 return intensity;
109}
110
9905a43b 111static const struct backlight_ops cr_backlight_ops = {
dbe7e429
AH
112 .get_brightness = cr_backlight_get_intensity,
113 .update_status = cr_backlight_set_intensity,
114};
115
116static void cr_panel_on(void)
117{
118 u32 addr = gpio_bar + CRVML_PANEL_PORT;
119 u32 cur = inl(addr);
120
121 if (!(cur & CRVML_PANEL_ON)) {
122 /* Make sure LVDS controller is down. */
123 if (cur & 0x00000001) {
124 cur &= ~CRVML_LVDS_ON;
125 outl(cur, addr);
126 }
127 /* Power up Panel */
128 schedule_timeout(HZ / 10);
129 cur |= CRVML_PANEL_ON;
130 outl(cur, addr);
131 }
132
133 /* Power up LVDS controller */
134
135 if (!(cur & CRVML_LVDS_ON)) {
136 schedule_timeout(HZ / 10);
137 outl(cur | CRVML_LVDS_ON, addr);
138 }
139}
140
141static void cr_panel_off(void)
142{
143 u32 addr = gpio_bar + CRVML_PANEL_PORT;
144 u32 cur = inl(addr);
145
146 /* Power down LVDS controller first to avoid high currents */
147 if (cur & CRVML_LVDS_ON) {
148 cur &= ~CRVML_LVDS_ON;
149 outl(cur, addr);
150 }
151 if (cur & CRVML_PANEL_ON) {
152 schedule_timeout(HZ / 10);
153 outl(cur & ~CRVML_PANEL_ON, addr);
154 }
155}
156
157static int cr_lcd_set_power(struct lcd_device *ld, int power)
158{
159 if (power == FB_BLANK_UNBLANK)
160 cr_panel_on();
161 if (power == FB_BLANK_POWERDOWN)
162 cr_panel_off();
163
164 return 0;
165}
166
167static struct lcd_ops cr_lcd_ops = {
168 .set_power = cr_lcd_set_power,
169};
170
171static int cr_backlight_probe(struct platform_device *pdev)
172{
a19a6ee6 173 struct backlight_properties props;
0b75f2df
JJ
174 struct backlight_device *bdp;
175 struct lcd_device *ldp;
dbe7e429
AH
176 struct cr_panel *crp;
177 u8 dev_en;
178
dbe7e429
AH
179 lpc_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
180 CRVML_DEVICE_LPC, NULL);
181 if (!lpc_dev) {
182 printk("INTEL CARILLO RANCH LPC not found.\n");
183 return -ENODEV;
184 }
185
186 pci_read_config_byte(lpc_dev, CRVML_REG_GPIOEN, &dev_en);
187 if (!(dev_en & CRVML_GPIOEN_BIT)) {
188 printk(KERN_ERR
189 "Carillo Ranch GPIO device was not enabled.\n");
190 pci_dev_put(lpc_dev);
191 return -ENODEV;
192 }
193
a19a6ee6
MG
194 memset(&props, 0, sizeof(struct backlight_properties));
195 bdp = backlight_device_register("cr-backlight", &pdev->dev, NULL,
196 &cr_backlight_ops, &props);
0b75f2df 197 if (IS_ERR(bdp)) {
dbe7e429 198 pci_dev_put(lpc_dev);
0b75f2df 199 return PTR_ERR(bdp);
dbe7e429
AH
200 }
201
0b75f2df
JJ
202 ldp = lcd_device_register("cr-lcd", &pdev->dev, NULL, &cr_lcd_ops);
203 if (IS_ERR(ldp)) {
204 backlight_device_unregister(bdp);
dbe7e429 205 pci_dev_put(lpc_dev);
5b0582ea 206 return PTR_ERR(ldp);
dbe7e429
AH
207 }
208
209 pci_read_config_dword(lpc_dev, CRVML_REG_GPIOBAR,
210 &gpio_bar);
211 gpio_bar &= ~0x3F;
212
0b75f2df
JJ
213 crp = kzalloc(sizeof(*crp), GFP_KERNEL);
214 if (!crp) {
215 lcd_device_unregister(ldp);
216 backlight_device_unregister(bdp);
217 pci_dev_put(lpc_dev);
218 return -ENOMEM;
219 }
220
221 crp->cr_backlight_device = bdp;
222 crp->cr_lcd_device = ldp;
dbe7e429
AH
223 crp->cr_backlight_device->props.power = FB_BLANK_UNBLANK;
224 crp->cr_backlight_device->props.brightness = 0;
dbe7e429 225 cr_backlight_set_intensity(crp->cr_backlight_device);
dbe7e429
AH
226 cr_lcd_set_power(crp->cr_lcd_device, FB_BLANK_UNBLANK);
227
228 platform_set_drvdata(pdev, crp);
229
230 return 0;
231}
232
233static int cr_backlight_remove(struct platform_device *pdev)
234{
235 struct cr_panel *crp = platform_get_drvdata(pdev);
236 crp->cr_backlight_device->props.power = FB_BLANK_POWERDOWN;
237 crp->cr_backlight_device->props.brightness = 0;
238 crp->cr_backlight_device->props.max_brightness = 0;
239 cr_backlight_set_intensity(crp->cr_backlight_device);
240 cr_lcd_set_power(crp->cr_lcd_device, FB_BLANK_POWERDOWN);
241 backlight_device_unregister(crp->cr_backlight_device);
242 lcd_device_unregister(crp->cr_lcd_device);
243 pci_dev_put(lpc_dev);
244
245 return 0;
246}
247
248static struct platform_driver cr_backlight_driver = {
249 .probe = cr_backlight_probe,
250 .remove = cr_backlight_remove,
251 .driver = {
252 .name = "cr_backlight",
253 },
254};
255
256static struct platform_device *crp;
257
258static int __init cr_backlight_init(void)
259{
260 int ret = platform_driver_register(&cr_backlight_driver);
261
b4a11d3d
AM
262 if (ret)
263 return ret;
dbe7e429 264
b4a11d3d
AM
265 crp = platform_device_register_simple("cr_backlight", -1, NULL, 0);
266 if (IS_ERR(crp)) {
267 platform_driver_unregister(&cr_backlight_driver);
268 return PTR_ERR(crp);
dbe7e429
AH
269 }
270
271 printk("Carillo Ranch Backlight Driver Initialized.\n");
272
b4a11d3d 273 return 0;
dbe7e429
AH
274}
275
276static void __exit cr_backlight_exit(void)
277{
278 platform_device_unregister(crp);
279 platform_driver_unregister(&cr_backlight_driver);
280}
281
282module_init(cr_backlight_init);
283module_exit(cr_backlight_exit);
284
285MODULE_AUTHOR("Tungsten Graphics Inc.");
286MODULE_DESCRIPTION("Carillo Ranch Backlight Driver");
287MODULE_LICENSE("GPL");