blob: 8471008aa6ff6b97c60425d09238174f3a4644e8 [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>
Dave Jonesb9860ec2007-03-06 01:42:00 -080015
16#ifdef CONFIG_PMAC_BACKLIGHT
17#include <asm/backlight.h>
18#endif
19
Michael Hanselmann5474c122006-06-25 05:47:08 -070020#include "nv_local.h"
21#include "nv_type.h"
22#include "nv_proto.h"
23
Michael Hanselmann5474c122006-06-25 05:47:08 -070024/* 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
Michael Hanselmann5474c122006-06-25 05:47:08 -070031static 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 */
Richard Purdie37ce69a2007-02-10 14:10:33 +000038 /* No locking of bl_curve since we read a single value */
Michael Hanselmanne01af032006-07-10 04:44:45 -070039 nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
Michael Hanselmann5474c122006-06-25 05:47:08 -070040
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
Richard Purdie37ce69a2007-02-10 14:10:33 +000051static int nvidia_bl_update_status(struct backlight_device *bd)
Michael Hanselmann5474c122006-06-25 05:47:08 -070052{
Richard Purdie655bfd72007-07-09 12:17:24 +010053 struct nvidia_par *par = bl_get_data(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -070054 u32 tmp_pcrt, tmp_pmc, fpcontrol;
55 int level;
56
57 if (!par->FlatPanel)
58 return 0;
59
Richard Purdie599a52d2007-02-10 23:07:48 +000060 if (bd->props.power != FB_BLANK_UNBLANK ||
61 bd->props.fb_blank != FB_BLANK_UNBLANK)
Michael Hanselmann5474c122006-06-25 05:47:08 -070062 level = 0;
63 else
Richard Purdie599a52d2007-02-10 23:07:48 +000064 level = bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -070065
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{
Richard Purdie599a52d2007-02-10 23:07:48 +000087 return bd->props.brightness;
Michael Hanselmann5474c122006-06-25 05:47:08 -070088}
89
Lionel Debrouxacc24722010-11-16 14:14:02 +010090static const struct backlight_ops nvidia_bl_ops = {
Michael Hanselmann5474c122006-06-25 05:47:08 -070091 .get_brightness = nvidia_bl_get_brightness,
92 .update_status = nvidia_bl_update_status,
Michael Hanselmann5474c122006-06-25 05:47:08 -070093};
94
Michael Hanselmann5474c122006-06-25 05:47:08 -070095void nvidia_bl_init(struct nvidia_par *par)
96{
Matthew Garretta19a6ee2010-02-17 16:39:44 -050097 struct backlight_properties props;
Michael Hanselmann5474c122006-06-25 05:47:08 -070098 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
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500113 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700114 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500115 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
116 bd = backlight_device_register(name, info->dev, par, &nvidia_bl_ops,
117 &props);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700118 if (IS_ERR(bd)) {
119 info->bl_dev = NULL;
Benjamin Herrenschmidt98a3c782006-08-31 14:04:34 +1000120 printk(KERN_WARNING "nvidia: Backlight registration failed\n");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700121 goto error;
122 }
123
Michael Hanselmann5474c122006-06-25 05:47:08 -0700124 info->bl_dev = bd;
125 fb_bl_default_curve(info, 0,
126 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL,
127 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700128
Richard Purdie238576e2007-02-26 22:06:09 +0000129 bd->props.brightness = bd->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000130 bd->props.power = FB_BLANK_UNBLANK;
Richard Purdie28ee0862007-02-08 22:25:09 +0000131 backlight_update_status(bd);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700132
Michael Hanselmann5474c122006-06-25 05:47:08 -0700133 printk("nvidia: Backlight initialized (%s)\n", name);
134
135 return;
136
137error:
138 return;
139}
140
141void nvidia_bl_exit(struct nvidia_par *par)
142{
143 struct fb_info *info = pci_get_drvdata(par->pci_dev);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000144 struct backlight_device *bd = info->bl_dev;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700145
Richard Purdie321709c2007-02-10 15:04:08 +0000146 backlight_device_unregister(bd);
147 printk("nvidia: Backlight unloaded\n");
Michael Hanselmann5474c122006-06-25 05:47:08 -0700148}