]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/video/nvidia/nv_backlight.c
backlight: Allow properties to be passed at registration
[net-next-2.6.git] / drivers / video / nvidia / nv_backlight.c
CommitLineData
5474c120
MH
1/*
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>
b9860ecf
DJ
15
16#ifdef CONFIG_PMAC_BACKLIGHT
17#include <asm/backlight.h>
18#endif
19
5474c120
MH
20#include "nv_local.h"
21#include "nv_type.h"
22#include "nv_proto.h"
23
5474c120
MH
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
e01af038 29#define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
5474c120 30
5474c120
MH
31static int nvidia_bl_get_level_brightness(struct nvidia_par *par,
32 int level)
33{
34 struct fb_info *info = pci_get_drvdata(par->pci_dev);
35 int nlevel;
36
37 /* Get and convert the value */
37ce69a5 38 /* No locking of bl_curve since we read a single value */
e01af038 39 nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
5474c120
MH
40
41 if (nlevel < 0)
42 nlevel = 0;
43 else if (nlevel < MIN_LEVEL)
44 nlevel = MIN_LEVEL;
45 else if (nlevel > MAX_LEVEL)
46 nlevel = MAX_LEVEL;
47
48 return nlevel;
49}
50
37ce69a5 51static int nvidia_bl_update_status(struct backlight_device *bd)
5474c120 52{
655bfd7a 53 struct nvidia_par *par = bl_get_data(bd);
5474c120
MH
54 u32 tmp_pcrt, tmp_pmc, fpcontrol;
55 int level;
56
57 if (!par->FlatPanel)
58 return 0;
59
599a52d1
RP
60 if (bd->props.power != FB_BLANK_UNBLANK ||
61 bd->props.fb_blank != FB_BLANK_UNBLANK)
5474c120
MH
62 level = 0;
63 else
599a52d1 64 level = bd->props.brightness;
5474c120
MH
65
66 tmp_pmc = NV_RD32(par->PMC, 0x10F0) & 0x0000FFFF;
67 tmp_pcrt = NV_RD32(par->PCRTC0, 0x081C) & 0xFFFFFFFC;
68 fpcontrol = NV_RD32(par->PRAMDAC, 0x0848) & 0xCFFFFFCC;
69
70 if (level > 0) {
71 tmp_pcrt |= 0x1;
72 tmp_pmc |= (1 << 31); /* backlight bit */
73 tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16;
74 fpcontrol |= par->fpSyncs;
75 } else
76 fpcontrol |= 0x20000022;
77
78 NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt);
79 NV_WR32(par->PMC, 0x10F0, tmp_pmc);
80 NV_WR32(par->PRAMDAC, 0x848, fpcontrol);
81
82 return 0;
83}
84
85static int nvidia_bl_get_brightness(struct backlight_device *bd)
86{
599a52d1 87 return bd->props.brightness;
5474c120
MH
88}
89
599a52d1 90static struct backlight_ops nvidia_bl_ops = {
5474c120
MH
91 .get_brightness = nvidia_bl_get_brightness,
92 .update_status = nvidia_bl_update_status,
5474c120
MH
93};
94
95void nvidia_bl_init(struct nvidia_par *par)
96{
a19a6ee6 97 struct backlight_properties props;
5474c120
MH
98 struct fb_info *info = pci_get_drvdata(par->pci_dev);
99 struct backlight_device *bd;
100 char name[12];
101
102 if (!par->FlatPanel)
103 return;
104
105#ifdef CONFIG_PMAC_BACKLIGHT
106 if (!machine_is(powermac) ||
107 !pmac_has_backlight_type("mnca"))
108 return;
109#endif
110
111 snprintf(name, sizeof(name), "nvidiabl%d", info->node);
112
a19a6ee6
MG
113 memset(&props, 0, sizeof(struct backlight_properties));
114 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
115 bd = backlight_device_register(name, info->dev, par, &nvidia_bl_ops,
116 &props);
5474c120
MH
117 if (IS_ERR(bd)) {
118 info->bl_dev = NULL;
98a3c781 119 printk(KERN_WARNING "nvidia: Backlight registration failed\n");
5474c120
MH
120 goto error;
121 }
122
5474c120
MH
123 info->bl_dev = bd;
124 fb_bl_default_curve(info, 0,
125 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
126 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
5474c120 127
238576e1 128 bd->props.brightness = bd->props.max_brightness;
599a52d1 129 bd->props.power = FB_BLANK_UNBLANK;
28ee086d 130 backlight_update_status(bd);
5474c120 131
5474c120
MH
132 printk("nvidia: Backlight initialized (%s)\n", name);
133
134 return;
135
136error:
137 return;
138}
139
140void nvidia_bl_exit(struct nvidia_par *par)
141{
142 struct fb_info *info = pci_get_drvdata(par->pci_dev);
37ce69a5 143 struct backlight_device *bd = info->bl_dev;
5474c120 144
321709c5
RP
145 backlight_device_unregister(bd);
146 printk("nvidia: Backlight unloaded\n");
5474c120 147}