1/*
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>
34#include <linux/acpi.h>
35
36#include "drmP.h"
37#include "nouveau_drv.h"
38#include "nouveau_drm.h"
39#include "nouveau_reg.h"
40#include "nouveau_encoder.h"
41
42static int
43nv40_get_intensity(struct backlight_device *bd)
44{
45	struct drm_device *dev = bl_get_data(bd);
46	int val = (nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK)
47									>> 16;
48
49	return val;
50}
51
52static int
53nv40_set_intensity(struct backlight_device *bd)
54{
55	struct drm_device *dev = bl_get_data(bd);
56	int val = bd->props.brightness;
57	int reg = nv_rd32(dev, NV40_PMC_BACKLIGHT);
58
59	nv_wr32(dev, NV40_PMC_BACKLIGHT,
60		 (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
61
62	return 0;
63}
64
65static const struct backlight_ops nv40_bl_ops = {
66	.options = BL_CORE_SUSPENDRESUME,
67	.get_brightness = nv40_get_intensity,
68	.update_status = nv40_set_intensity,
69};
70
71static int
72nv40_backlight_init(struct drm_connector *connector)
73{
74	struct drm_device *dev = connector->dev;
75	struct drm_nouveau_private *dev_priv = dev->dev_private;
76	struct backlight_properties props;
77	struct backlight_device *bd;
78
79	if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
80		return 0;
81
82	memset(&props, 0, sizeof(struct backlight_properties));
83	props.type = BACKLIGHT_RAW;
84	props.max_brightness = 31;
85	bd = backlight_device_register("nv_backlight", &connector->kdev, dev,
86				       &nv40_bl_ops, &props);
87	if (IS_ERR(bd))
88		return PTR_ERR(bd);
89
90	dev_priv->backlight = bd;
91	bd->props.brightness = nv40_get_intensity(bd);
92	backlight_update_status(bd);
93
94	return 0;
95}
96
97static int
98nv50_get_intensity(struct backlight_device *bd)
99{
100	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
101	struct drm_device *dev = nv_encoder->base.base.dev;
102	int or = nv_encoder->or;
103	u32 div = 1025;
104	u32 val;
105
106	val  = nv_rd32(dev, NV50_PDISP_SOR_PWM_CTL(or));
107	val &= NV50_PDISP_SOR_PWM_CTL_VAL;
108	return ((val * 100) + (div / 2)) / div;
109}
110
111static int
112nv50_set_intensity(struct backlight_device *bd)
113{
114	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
115	struct drm_device *dev = nv_encoder->base.base.dev;
116	int or = nv_encoder->or;
117	u32 div = 1025;
118	u32 val = (bd->props.brightness * div) / 100;
119
120	nv_wr32(dev, NV50_PDISP_SOR_PWM_CTL(or),
121		     NV50_PDISP_SOR_PWM_CTL_NEW | val);
122	return 0;
123}
124
125static const struct backlight_ops nv50_bl_ops = {
126	.options = BL_CORE_SUSPENDRESUME,
127	.get_brightness = nv50_get_intensity,
128	.update_status = nv50_set_intensity,
129};
130
131static int
132nva3_get_intensity(struct backlight_device *bd)
133{
134	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
135	struct drm_device *dev = nv_encoder->base.base.dev;
136	int or = nv_encoder->or;
137	u32 div, val;
138
139	div  = nv_rd32(dev, NV50_PDISP_SOR_PWM_DIV(or));
140	val  = nv_rd32(dev, NV50_PDISP_SOR_PWM_CTL(or));
141	val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
142	if (div && div >= val)
143		return ((val * 100) + (div / 2)) / div;
144
145	return 100;
146}
147
148static int
149nva3_set_intensity(struct backlight_device *bd)
150{
151	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
152	struct drm_device *dev = nv_encoder->base.base.dev;
153	int or = nv_encoder->or;
154	u32 div, val;
155
156	div = nv_rd32(dev, NV50_PDISP_SOR_PWM_DIV(or));
157	val = (bd->props.brightness * div) / 100;
158	if (div) {
159		nv_wr32(dev, NV50_PDISP_SOR_PWM_CTL(or), val |
160			     NV50_PDISP_SOR_PWM_CTL_NEW |
161			     NVA3_PDISP_SOR_PWM_CTL_UNK);
162		return 0;
163	}
164
165	return -EINVAL;
166}
167
168static const struct backlight_ops nva3_bl_ops = {
169	.options = BL_CORE_SUSPENDRESUME,
170	.get_brightness = nva3_get_intensity,
171	.update_status = nva3_set_intensity,
172};
173
174static int
175nv50_backlight_init(struct drm_connector *connector)
176{
177	struct drm_device *dev = connector->dev;
178	struct drm_nouveau_private *dev_priv = dev->dev_private;
179	struct nouveau_encoder *nv_encoder;
180	struct backlight_properties props;
181	struct backlight_device *bd;
182	const struct backlight_ops *ops;
183
184	nv_encoder = find_encoder(connector, OUTPUT_LVDS);
185	if (!nv_encoder) {
186		nv_encoder = find_encoder(connector, OUTPUT_DP);
187		if (!nv_encoder)
188			return -ENODEV;
189	}
190
191	if (!nv_rd32(dev, NV50_PDISP_SOR_PWM_CTL(nv_encoder->or)))
192		return 0;
193
194	if (dev_priv->chipset <= 0xa0 ||
195	    dev_priv->chipset == 0xaa ||
196	    dev_priv->chipset == 0xac)
197		ops = &nv50_bl_ops;
198	else
199		ops = &nva3_bl_ops;
200
201	memset(&props, 0, sizeof(struct backlight_properties));
202	props.type = BACKLIGHT_RAW;
203	props.max_brightness = 100;
204	bd = backlight_device_register("nv_backlight", &connector->kdev,
205				       nv_encoder, ops, &props);
206	if (IS_ERR(bd))
207		return PTR_ERR(bd);
208
209	dev_priv->backlight = bd;
210	bd->props.brightness = bd->ops->get_brightness(bd);
211	backlight_update_status(bd);
212	return 0;
213}
214
215int
216nouveau_backlight_init(struct drm_device *dev)
217{
218	struct drm_nouveau_private *dev_priv = dev->dev_private;
219	struct drm_connector *connector;
220
221#ifdef CONFIG_ACPI
222	if (acpi_video_backlight_support()) {
223		NV_INFO(dev, "ACPI backlight interface available, "
224			     "not registering our own\n");
225		return 0;
226	}
227#endif
228
229	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
230		if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
231		    connector->connector_type != DRM_MODE_CONNECTOR_eDP)
232			continue;
233
234		switch (dev_priv->card_type) {
235		case NV_40:
236			return nv40_backlight_init(connector);
237		case NV_50:
238			return nv50_backlight_init(connector);
239		default:
240			break;
241		}
242	}
243
244
245	return 0;
246}
247
248void
249nouveau_backlight_exit(struct drm_device *dev)
250{
251	struct drm_nouveau_private *dev_priv = dev->dev_private;
252
253	if (dev_priv->backlight) {
254		backlight_device_unregister(dev_priv->backlight);
255		dev_priv->backlight = NULL;
256	}
257}
258