blob: b45f577094ac8bbf189c3ec2b4e8b83ad1f3c322 [file] [log] [blame]
Michael Hanselmann5474c122006-06-25 05:47:08 -07001/*
2 * Backlight code for nVidia based graphic cards
3 *
4 * Copyright 2004 Antonino Daplas <adaplas@pol.net>
5 * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/backlight.h>
13#include <linux/fb.h>
14#include <linux/pci.h>
15#include "nv_local.h"
16#include "nv_type.h"
17#include "nv_proto.h"
18
19#ifdef CONFIG_PMAC_BACKLIGHT
20#include <asm/backlight.h>
21#include <asm/machdep.h>
22#endif
23
24/* We do not have any information about which values are allowed, thus
25 * we used safe values.
26 */
27#define MIN_LEVEL 0x158
28#define MAX_LEVEL 0x534
Michael Hanselmanne01af032006-07-10 04:44:45 -070029#define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
Michael Hanselmann5474c122006-06-25 05:47:08 -070030
31static struct backlight_properties nvidia_bl_data;
32
Michael Hanselmanne01af032006-07-10 04:44:45 -070033/* Call with fb_info->bl_mutex held */
Michael Hanselmann5474c122006-06-25 05:47:08 -070034static int nvidia_bl_get_level_brightness(struct nvidia_par *par,
35 int level)
36{
37 struct fb_info *info = pci_get_drvdata(par->pci_dev);
38 int nlevel;
39
40 /* Get and convert the value */
Michael Hanselmanne01af032006-07-10 04:44:45 -070041 nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
Michael Hanselmann5474c122006-06-25 05:47:08 -070042
43 if (nlevel < 0)
44 nlevel = 0;
45 else if (nlevel < MIN_LEVEL)
46 nlevel = MIN_LEVEL;
47 else if (nlevel > MAX_LEVEL)
48 nlevel = MAX_LEVEL;
49
50 return nlevel;
51}
52
Michael Hanselmanne01af032006-07-10 04:44:45 -070053/* Call with fb_info->bl_mutex held */
54static int __nvidia_bl_update_status(struct backlight_device *bd)
Michael Hanselmann5474c122006-06-25 05:47:08 -070055{
56 struct nvidia_par *par = class_get_devdata(&bd->class_dev);
57 u32 tmp_pcrt, tmp_pmc, fpcontrol;
58 int level;
59
60 if (!par->FlatPanel)
61 return 0;
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 tmp_pmc = NV_RD32(par->PMC, 0x10F0) & 0x0000FFFF;
70 tmp_pcrt = NV_RD32(par->PCRTC0, 0x081C) & 0xFFFFFFFC;
71 fpcontrol = NV_RD32(par->PRAMDAC, 0x0848) & 0xCFFFFFCC;
72
73 if (level > 0) {
74 tmp_pcrt |= 0x1;
75 tmp_pmc |= (1 << 31); /* backlight bit */
76 tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16;
77 fpcontrol |= par->fpSyncs;
78 } else
79 fpcontrol |= 0x20000022;
80
81 NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt);
82 NV_WR32(par->PMC, 0x10F0, tmp_pmc);
83 NV_WR32(par->PRAMDAC, 0x848, fpcontrol);
84
85 return 0;
86}
87
Michael Hanselmanne01af032006-07-10 04:44:45 -070088static int nvidia_bl_update_status(struct backlight_device *bd)
89{
90 struct nvidia_par *par = class_get_devdata(&bd->class_dev);
91 struct fb_info *info = pci_get_drvdata(par->pci_dev);
92 int ret;
93
94 mutex_lock(&info->bl_mutex);
95 ret = __nvidia_bl_update_status(bd);
96 mutex_unlock(&info->bl_mutex);
97
98 return ret;
99}
100
Michael Hanselmann5474c122006-06-25 05:47:08 -0700101static int nvidia_bl_get_brightness(struct backlight_device *bd)
102{
103 return bd->props->brightness;
104}
105
106static struct backlight_properties nvidia_bl_data = {
107 .owner = THIS_MODULE,
108 .get_brightness = nvidia_bl_get_brightness,
109 .update_status = nvidia_bl_update_status,
110 .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
111};
112
Michael Hanselmanne01af032006-07-10 04:44:45 -0700113void nvidia_bl_set_power(struct fb_info *info, int power)
114{
115 mutex_lock(&info->bl_mutex);
116 up(&info->bl_dev->sem);
117 info->bl_dev->props->power = power;
118 __nvidia_bl_update_status(info->bl_dev);
119 down(&info->bl_dev->sem);
120 mutex_unlock(&info->bl_mutex);
121}
122
Michael Hanselmann5474c122006-06-25 05:47:08 -0700123void nvidia_bl_init(struct nvidia_par *par)
124{
125 struct fb_info *info = pci_get_drvdata(par->pci_dev);
126 struct backlight_device *bd;
127 char name[12];
128
129 if (!par->FlatPanel)
130 return;
131
132#ifdef CONFIG_PMAC_BACKLIGHT
133 if (!machine_is(powermac) ||
134 !pmac_has_backlight_type("mnca"))
135 return;
136#endif
137
138 snprintf(name, sizeof(name), "nvidiabl%d", info->node);
139
140 bd = backlight_device_register(name, par, &nvidia_bl_data);
141 if (IS_ERR(bd)) {
142 info->bl_dev = NULL;
143 printk("nvidia: Backlight registration failed\n");
144 goto error;
145 }
146
147 mutex_lock(&info->bl_mutex);
148 info->bl_dev = bd;
149 fb_bl_default_curve(info, 0,
150 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
151 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
152 mutex_unlock(&info->bl_mutex);
153
154 up(&bd->sem);
155 bd->props->brightness = nvidia_bl_data.max_brightness;
156 bd->props->power = FB_BLANK_UNBLANK;
157 bd->props->update_status(bd);
158 down(&bd->sem);
159
160#ifdef CONFIG_PMAC_BACKLIGHT
161 mutex_lock(&pmac_backlight_mutex);
162 if (!pmac_backlight)
163 pmac_backlight = bd;
164 mutex_unlock(&pmac_backlight_mutex);
165#endif
166
167 printk("nvidia: Backlight initialized (%s)\n", name);
168
169 return;
170
171error:
172 return;
173}
174
175void nvidia_bl_exit(struct nvidia_par *par)
176{
177 struct fb_info *info = pci_get_drvdata(par->pci_dev);
178
179#ifdef CONFIG_PMAC_BACKLIGHT
180 mutex_lock(&pmac_backlight_mutex);
181#endif
182
183 mutex_lock(&info->bl_mutex);
184 if (info->bl_dev) {
185#ifdef CONFIG_PMAC_BACKLIGHT
186 if (pmac_backlight == info->bl_dev)
187 pmac_backlight = NULL;
188#endif
189
190 backlight_device_unregister(info->bl_dev);
191
192 printk("nvidia: Backlight unloaded\n");
193 }
194 mutex_unlock(&info->bl_mutex);
195
196#ifdef CONFIG_PMAC_BACKLIGHT
197 mutex_unlock(&pmac_backlight_mutex);
198#endif
199}