blob: d412adc67403aeb24faa0923e152d8dc206fe312 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2009 Red Hat <mjg@redhat.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26/*
27 * Authors:
28 * Matthew Garrett <mjg@redhat.com>
29 *
30 * Register locations derived from NVClock by Roderick Colenbrander
31 */
32
33#include <linux/backlight.h>
Ben Skeggs5bead792010-11-04 09:56:12 +100034#include <linux/acpi.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100035
36#include "drmP.h"
37#include "nouveau_drv.h"
38#include "nouveau_drm.h"
39#include "nouveau_reg.h"
40
Ben Skeggs73076482011-08-02 18:54:43 +100041static int
42nv40_get_intensity(struct backlight_device *bd)
Ben Skeggs6ee73862009-12-11 19:24:15 +100043{
44 struct drm_device *dev = bl_get_data(bd);
45 int val = (nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK)
46 >> 16;
47
48 return val;
49}
50
Ben Skeggs73076482011-08-02 18:54:43 +100051static int
52nv40_set_intensity(struct backlight_device *bd)
Ben Skeggs6ee73862009-12-11 19:24:15 +100053{
54 struct drm_device *dev = bl_get_data(bd);
55 int val = bd->props.brightness;
56 int reg = nv_rd32(dev, NV40_PMC_BACKLIGHT);
57
58 nv_wr32(dev, NV40_PMC_BACKLIGHT,
59 (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
60
61 return 0;
62}
63
Lionel Debrouxacc24722010-11-16 14:14:02 +010064static const struct backlight_ops nv40_bl_ops = {
Ben Skeggs6ee73862009-12-11 19:24:15 +100065 .options = BL_CORE_SUSPENDRESUME,
66 .get_brightness = nv40_get_intensity,
67 .update_status = nv40_set_intensity,
68};
69
Ben Skeggs73076482011-08-02 18:54:43 +100070static int
71nv40_backlight_init(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +100072{
Matthew Garrett7eae3ef2011-03-22 16:30:24 -070073 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100074 struct drm_nouveau_private *dev_priv = dev->dev_private;
Matthew Garrett7eae3ef2011-03-22 16:30:24 -070075 struct backlight_properties props;
Ben Skeggs6ee73862009-12-11 19:24:15 +100076 struct backlight_device *bd;
77
78 if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
79 return 0;
80
Matthew Garretta19a6ee2010-02-17 16:39:44 -050081 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -070082 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -050083 props.max_brightness = 31;
Matthew Garrett7eae3ef2011-03-22 16:30:24 -070084 bd = backlight_device_register("nv_backlight", &connector->kdev, dev,
Matthew Garretta19a6ee2010-02-17 16:39:44 -050085 &nv40_bl_ops, &props);
Ben Skeggs6ee73862009-12-11 19:24:15 +100086 if (IS_ERR(bd))
87 return PTR_ERR(bd);
88
89 dev_priv->backlight = bd;
Ben Skeggs6ee73862009-12-11 19:24:15 +100090 bd->props.brightness = nv40_get_intensity(bd);
91 backlight_update_status(bd);
92
93 return 0;
94}
95
Ben Skeggs73076482011-08-02 18:54:43 +100096static int
97nv50_get_intensity(struct backlight_device *bd)
98{
99 struct drm_device *dev = bl_get_data(bd);
100
101 return nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT);
102}
103
104static int
105nv50_set_intensity(struct backlight_device *bd)
106{
107 struct drm_device *dev = bl_get_data(bd);
108 int val = bd->props.brightness;
109
110 nv_wr32(dev, NV50_PDISPLAY_SOR_BACKLIGHT,
111 val | NV50_PDISPLAY_SOR_BACKLIGHT_ENABLE);
112 return 0;
113}
114
115static const struct backlight_ops nv50_bl_ops = {
116 .options = BL_CORE_SUSPENDRESUME,
117 .get_brightness = nv50_get_intensity,
118 .update_status = nv50_set_intensity,
119};
120
121static int
122nv50_backlight_init(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123{
Matthew Garrett7eae3ef2011-03-22 16:30:24 -0700124 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000125 struct drm_nouveau_private *dev_priv = dev->dev_private;
Matthew Garrett7eae3ef2011-03-22 16:30:24 -0700126 struct backlight_properties props;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000127 struct backlight_device *bd;
128
129 if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT))
130 return 0;
131
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500132 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700133 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500134 props.max_brightness = 1025;
Matthew Garrett7eae3ef2011-03-22 16:30:24 -0700135 bd = backlight_device_register("nv_backlight", &connector->kdev, dev,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500136 &nv50_bl_ops, &props);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000137 if (IS_ERR(bd))
138 return PTR_ERR(bd);
139
140 dev_priv->backlight = bd;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141 bd->props.brightness = nv50_get_intensity(bd);
142 backlight_update_status(bd);
143 return 0;
144}
145
Ben Skeggs73076482011-08-02 18:54:43 +1000146int
147nouveau_backlight_init(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148{
Matthew Garrett7eae3ef2011-03-22 16:30:24 -0700149 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000150 struct drm_nouveau_private *dev_priv = dev->dev_private;
151
Ben Skeggs5bead792010-11-04 09:56:12 +1000152#ifdef CONFIG_ACPI
153 if (acpi_video_backlight_support()) {
154 NV_INFO(dev, "ACPI backlight interface available, "
155 "not registering our own\n");
156 return 0;
157 }
158#endif
159
Ben Skeggs6ee73862009-12-11 19:24:15 +1000160 switch (dev_priv->card_type) {
161 case NV_40:
Ben Skeggs73076482011-08-02 18:54:43 +1000162 return nv40_backlight_init(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000163 case NV_50:
Ben Skeggs73076482011-08-02 18:54:43 +1000164 return nv50_backlight_init(connector);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000165 default:
166 break;
167 }
168
169 return 0;
170}
171
Ben Skeggs73076482011-08-02 18:54:43 +1000172void
173nouveau_backlight_exit(struct drm_connector *connector)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000174{
Matthew Garrett7eae3ef2011-03-22 16:30:24 -0700175 struct drm_device *dev = connector->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000176 struct drm_nouveau_private *dev_priv = dev->dev_private;
177
178 if (dev_priv->backlight) {
179 backlight_device_unregister(dev_priv->backlight);
180 dev_priv->backlight = NULL;
181 }
182}