2 * Backlight code for nVidia based graphic cards
4 * Copyright 2004 Antonino Daplas <adaplas@pol.net>
5 * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
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.
12 #include <linux/backlight.h>
14 #include <linux/pci.h>
19 #ifdef CONFIG_PMAC_BACKLIGHT
20 #include <asm/backlight.h>
21 #include <asm/machdep.h>
24 /* We do not have any information about which values are allowed, thus
25 * we used safe values.
27 #define MIN_LEVEL 0x158
28 #define MAX_LEVEL 0x534
29 #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
31 static struct backlight_properties nvidia_bl_data;
33 /* Call with fb_info->bl_mutex held */
34 static int nvidia_bl_get_level_brightness(struct nvidia_par *par,
37 struct fb_info *info = pci_get_drvdata(par->pci_dev);
40 /* Get and convert the value */
41 nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
45 else if (nlevel < MIN_LEVEL)
47 else if (nlevel > MAX_LEVEL)
53 /* Call with fb_info->bl_mutex held */
54 static int __nvidia_bl_update_status(struct backlight_device *bd)
56 struct nvidia_par *par = class_get_devdata(&bd->class_dev);
57 u32 tmp_pcrt, tmp_pmc, fpcontrol;
63 if (bd->props->power != FB_BLANK_UNBLANK ||
64 bd->props->fb_blank != FB_BLANK_UNBLANK)
67 level = bd->props->brightness;
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;
75 tmp_pmc |= (1 << 31); /* backlight bit */
76 tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16;
77 fpcontrol |= par->fpSyncs;
79 fpcontrol |= 0x20000022;
81 NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt);
82 NV_WR32(par->PMC, 0x10F0, tmp_pmc);
83 NV_WR32(par->PRAMDAC, 0x848, fpcontrol);
88 static int nvidia_bl_update_status(struct backlight_device *bd)
90 struct nvidia_par *par = class_get_devdata(&bd->class_dev);
91 struct fb_info *info = pci_get_drvdata(par->pci_dev);
94 mutex_lock(&info->bl_mutex);
95 ret = __nvidia_bl_update_status(bd);
96 mutex_unlock(&info->bl_mutex);
101 static int nvidia_bl_get_brightness(struct backlight_device *bd)
103 return bd->props->brightness;
106 static 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),
113 void nvidia_bl_set_power(struct fb_info *info, int power)
115 mutex_lock(&info->bl_mutex);
118 down(&info->bl_dev->sem);
119 info->bl_dev->props->power = power;
120 __nvidia_bl_update_status(info->bl_dev);
121 up(&info->bl_dev->sem);
124 mutex_unlock(&info->bl_mutex);
127 void nvidia_bl_init(struct nvidia_par *par)
129 struct fb_info *info = pci_get_drvdata(par->pci_dev);
130 struct backlight_device *bd;
136 #ifdef CONFIG_PMAC_BACKLIGHT
137 if (!machine_is(powermac) ||
138 !pmac_has_backlight_type("mnca"))
142 snprintf(name, sizeof(name), "nvidiabl%d", info->node);
144 bd = backlight_device_register(name, par, &nvidia_bl_data);
147 printk(KERN_WARNING "nvidia: Backlight registration failed\n");
151 mutex_lock(&info->bl_mutex);
153 fb_bl_default_curve(info, 0,
154 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
155 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
156 mutex_unlock(&info->bl_mutex);
159 bd->props->brightness = nvidia_bl_data.max_brightness;
160 bd->props->power = FB_BLANK_UNBLANK;
161 bd->props->update_status(bd);
164 #ifdef CONFIG_PMAC_BACKLIGHT
165 mutex_lock(&pmac_backlight_mutex);
168 mutex_unlock(&pmac_backlight_mutex);
171 printk("nvidia: Backlight initialized (%s)\n", name);
179 void nvidia_bl_exit(struct nvidia_par *par)
181 struct fb_info *info = pci_get_drvdata(par->pci_dev);
183 #ifdef CONFIG_PMAC_BACKLIGHT
184 mutex_lock(&pmac_backlight_mutex);
187 mutex_lock(&info->bl_mutex);
189 #ifdef CONFIG_PMAC_BACKLIGHT
190 if (pmac_backlight == info->bl_dev)
191 pmac_backlight = NULL;
194 backlight_device_unregister(info->bl_dev);
196 printk("nvidia: Backlight unloaded\n");
198 mutex_unlock(&info->bl_mutex);
200 #ifdef CONFIG_PMAC_BACKLIGHT
201 mutex_unlock(&pmac_backlight_mutex);