xref: /linux/drivers/gpu/drm/nouveau/nouveau_backlight.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
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/apple-gmux.h>
34 #include <linux/backlight.h>
35 #include <linux/idr.h>
36 
37 #include "nouveau_drv.h"
38 #include "nouveau_reg.h"
39 #include "nouveau_encoder.h"
40 #include "nouveau_connector.h"
41 
42 static struct ida bl_ida;
43 #define BL_NAME_SIZE 15 // 12 for name + 2 for digits + 1 for '\0'
44 
45 static bool
46 nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE],
47 			   struct nouveau_backlight *bl)
48 {
49 	const int nb = ida_simple_get(&bl_ida, 0, 0, GFP_KERNEL);
50 	if (nb < 0 || nb >= 100)
51 		return false;
52 	if (nb > 0)
53 		snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
54 	else
55 		snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
56 	bl->id = nb;
57 	return true;
58 }
59 
60 static int
61 nv40_get_intensity(struct backlight_device *bd)
62 {
63 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
64 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
65 	struct nvif_object *device = &drm->client.device.object;
66 	int val = (nvif_rd32(device, NV40_PMC_BACKLIGHT) &
67 		   NV40_PMC_BACKLIGHT_MASK) >> 16;
68 
69 	return val;
70 }
71 
72 static int
73 nv40_set_intensity(struct backlight_device *bd)
74 {
75 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
76 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
77 	struct nvif_object *device = &drm->client.device.object;
78 	int val = bd->props.brightness;
79 	int reg = nvif_rd32(device, NV40_PMC_BACKLIGHT);
80 
81 	nvif_wr32(device, NV40_PMC_BACKLIGHT,
82 		  (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
83 
84 	return 0;
85 }
86 
87 static const struct backlight_ops nv40_bl_ops = {
88 	.options = BL_CORE_SUSPENDRESUME,
89 	.get_brightness = nv40_get_intensity,
90 	.update_status = nv40_set_intensity,
91 };
92 
93 static int
94 nv40_backlight_init(struct nouveau_encoder *encoder,
95 		    struct backlight_properties *props,
96 		    const struct backlight_ops **ops)
97 {
98 	struct nouveau_drm *drm = nouveau_drm(encoder->base.base.dev);
99 	struct nvif_object *device = &drm->client.device.object;
100 
101 	if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
102 		return -ENODEV;
103 
104 	props->max_brightness = 31;
105 	*ops = &nv40_bl_ops;
106 	return 0;
107 }
108 
109 static int
110 nv50_get_intensity(struct backlight_device *bd)
111 {
112 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
113 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
114 	struct nvif_object *device = &drm->client.device.object;
115 	int or = ffs(nv_encoder->dcb->or) - 1;
116 	u32 div = 1025;
117 	u32 val;
118 
119 	val  = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
120 	val &= NV50_PDISP_SOR_PWM_CTL_VAL;
121 	return ((val * 100) + (div / 2)) / div;
122 }
123 
124 static int
125 nv50_set_intensity(struct backlight_device *bd)
126 {
127 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
128 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
129 	struct nvif_object *device = &drm->client.device.object;
130 	int or = ffs(nv_encoder->dcb->or) - 1;
131 	u32 div = 1025;
132 	u32 val = (bd->props.brightness * div) / 100;
133 
134 	nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
135 		  NV50_PDISP_SOR_PWM_CTL_NEW | val);
136 	return 0;
137 }
138 
139 static const struct backlight_ops nv50_bl_ops = {
140 	.options = BL_CORE_SUSPENDRESUME,
141 	.get_brightness = nv50_get_intensity,
142 	.update_status = nv50_set_intensity,
143 };
144 
145 /*
146  * eDP brightness callbacks need to happen under lock, since we need to
147  * enable/disable the backlight ourselves for modesets
148  */
149 static int
150 nv50_edp_get_brightness(struct backlight_device *bd)
151 {
152 	struct drm_connector *connector = dev_get_drvdata(bd->dev.parent);
153 	struct drm_device *dev = connector->dev;
154 	struct drm_crtc *crtc;
155 	struct drm_modeset_acquire_ctx ctx;
156 	int ret = 0;
157 
158 	drm_modeset_acquire_init(&ctx, 0);
159 
160 retry:
161 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
162 	if (ret == -EDEADLK)
163 		goto deadlock;
164 	else if (ret < 0)
165 		goto out;
166 
167 	crtc = connector->state->crtc;
168 	if (!crtc)
169 		goto out;
170 
171 	ret = drm_modeset_lock(&crtc->mutex, &ctx);
172 	if (ret == -EDEADLK)
173 		goto deadlock;
174 	else if (ret < 0)
175 		goto out;
176 
177 	if (!crtc->state->active)
178 		goto out;
179 
180 	ret = bd->props.brightness;
181 out:
182 	drm_modeset_drop_locks(&ctx);
183 	drm_modeset_acquire_fini(&ctx);
184 	return ret;
185 deadlock:
186 	drm_modeset_backoff(&ctx);
187 	goto retry;
188 }
189 
190 static int
191 nv50_edp_set_brightness(struct backlight_device *bd)
192 {
193 	struct drm_connector *connector = dev_get_drvdata(bd->dev.parent);
194 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
195 	struct drm_device *dev = connector->dev;
196 	struct drm_crtc *crtc;
197 	struct drm_dp_aux *aux = &nv_connector->aux;
198 	struct nouveau_backlight *nv_bl = nv_connector->backlight;
199 	struct drm_modeset_acquire_ctx ctx;
200 	int ret = 0;
201 
202 	drm_modeset_acquire_init(&ctx, 0);
203 retry:
204 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
205 	if (ret == -EDEADLK)
206 		goto deadlock;
207 	else if (ret < 0)
208 		goto out;
209 
210 	crtc = connector->state->crtc;
211 	if (!crtc)
212 		goto out;
213 
214 	ret = drm_modeset_lock(&crtc->mutex, &ctx);
215 	if (ret == -EDEADLK)
216 		goto deadlock;
217 	else if (ret < 0)
218 		goto out;
219 
220 	if (crtc->state->active)
221 		ret = drm_edp_backlight_set_level(aux, &nv_bl->edp_info, bd->props.brightness);
222 
223 out:
224 	drm_modeset_drop_locks(&ctx);
225 	drm_modeset_acquire_fini(&ctx);
226 	return ret;
227 deadlock:
228 	drm_modeset_backoff(&ctx);
229 	goto retry;
230 }
231 
232 static const struct backlight_ops nv50_edp_bl_ops = {
233 	.get_brightness = nv50_edp_get_brightness,
234 	.update_status = nv50_edp_set_brightness,
235 };
236 
237 static int
238 nva3_get_intensity(struct backlight_device *bd)
239 {
240 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
241 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
242 	struct nvif_object *device = &drm->client.device.object;
243 	int or = ffs(nv_encoder->dcb->or) - 1;
244 	u32 div, val;
245 
246 	div  = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
247 	val  = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
248 	val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
249 	if (div && div >= val)
250 		return ((val * 100) + (div / 2)) / div;
251 
252 	return 100;
253 }
254 
255 static int
256 nva3_set_intensity(struct backlight_device *bd)
257 {
258 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
259 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
260 	struct nvif_object *device = &drm->client.device.object;
261 	int or = ffs(nv_encoder->dcb->or) - 1;
262 	u32 div, val;
263 
264 	div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
265 	val = (bd->props.brightness * div) / 100;
266 	if (div) {
267 		nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
268 			  val |
269 			  NV50_PDISP_SOR_PWM_CTL_NEW |
270 			  NVA3_PDISP_SOR_PWM_CTL_UNK);
271 		return 0;
272 	}
273 
274 	return -EINVAL;
275 }
276 
277 static const struct backlight_ops nva3_bl_ops = {
278 	.options = BL_CORE_SUSPENDRESUME,
279 	.get_brightness = nva3_get_intensity,
280 	.update_status = nva3_set_intensity,
281 };
282 
283 /* FIXME: perform backlight probing for eDP _before_ this, this only gets called after connector
284  * registration which happens after the initial modeset
285  */
286 static int
287 nv50_backlight_init(struct nouveau_backlight *bl,
288 		    struct nouveau_connector *nv_conn,
289 		    struct nouveau_encoder *nv_encoder,
290 		    struct backlight_properties *props,
291 		    const struct backlight_ops **ops)
292 {
293 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
294 	struct nvif_object *device = &drm->client.device.object;
295 
296 	if (!nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(ffs(nv_encoder->dcb->or) - 1)) ||
297 	    nv_conn->base.status != connector_status_connected)
298 		return -ENODEV;
299 
300 	if (nv_conn->type == DCB_CONNECTOR_eDP) {
301 		int ret;
302 		u16 current_level;
303 		u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
304 		u8 current_mode;
305 
306 		ret = drm_dp_dpcd_read(&nv_conn->aux, DP_EDP_DPCD_REV, edp_dpcd,
307 				       EDP_DISPLAY_CTL_CAP_SIZE);
308 		if (ret < 0)
309 			return ret;
310 
311 		/* TODO: Add support for hybrid PWM/DPCD panels */
312 		if (drm_edp_backlight_supported(edp_dpcd) &&
313 		    (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP) &&
314 		    (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)) {
315 			NV_DEBUG(drm, "DPCD backlight controls supported on %s\n",
316 				 nv_conn->base.name);
317 
318 			ret = drm_edp_backlight_init(&nv_conn->aux, &bl->edp_info, 0, edp_dpcd,
319 						     &current_level, &current_mode);
320 			if (ret < 0)
321 				return ret;
322 
323 			ret = drm_edp_backlight_enable(&nv_conn->aux, &bl->edp_info, current_level);
324 			if (ret < 0) {
325 				NV_ERROR(drm, "Failed to enable backlight on %s: %d\n",
326 					 nv_conn->base.name, ret);
327 				return ret;
328 			}
329 
330 			*ops = &nv50_edp_bl_ops;
331 			props->brightness = current_level;
332 			props->max_brightness = bl->edp_info.max;
333 			bl->uses_dpcd = true;
334 			return 0;
335 		}
336 	}
337 
338 	if (drm->client.device.info.chipset <= 0xa0 ||
339 	    drm->client.device.info.chipset == 0xaa ||
340 	    drm->client.device.info.chipset == 0xac)
341 		*ops = &nv50_bl_ops;
342 	else
343 		*ops = &nva3_bl_ops;
344 
345 	props->max_brightness = 100;
346 
347 	return 0;
348 }
349 
350 int
351 nouveau_backlight_init(struct drm_connector *connector)
352 {
353 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
354 	struct nouveau_backlight *bl;
355 	struct nouveau_encoder *nv_encoder = NULL;
356 	struct nvif_device *device = &drm->client.device;
357 	char backlight_name[BL_NAME_SIZE];
358 	struct backlight_properties props = {0};
359 	const struct backlight_ops *ops;
360 	int ret;
361 
362 	if (apple_gmux_present()) {
363 		NV_INFO_ONCE(drm, "Apple GMUX detected: not registering Nouveau backlight interface\n");
364 		return 0;
365 	}
366 
367 	if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
368 		nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
369 	else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
370 		nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
371 	else
372 		return 0;
373 
374 	if (!nv_encoder)
375 		return 0;
376 
377 	bl = kzalloc(sizeof(*bl), GFP_KERNEL);
378 	if (!bl)
379 		return -ENOMEM;
380 
381 	switch (device->info.family) {
382 	case NV_DEVICE_INFO_V0_CURIE:
383 		ret = nv40_backlight_init(nv_encoder, &props, &ops);
384 		break;
385 	case NV_DEVICE_INFO_V0_TESLA:
386 	case NV_DEVICE_INFO_V0_FERMI:
387 	case NV_DEVICE_INFO_V0_KEPLER:
388 	case NV_DEVICE_INFO_V0_MAXWELL:
389 	case NV_DEVICE_INFO_V0_PASCAL:
390 	case NV_DEVICE_INFO_V0_VOLTA:
391 	case NV_DEVICE_INFO_V0_TURING:
392 	case NV_DEVICE_INFO_V0_AMPERE: //XXX: not confirmed
393 		ret = nv50_backlight_init(bl, nouveau_connector(connector),
394 					  nv_encoder, &props, &ops);
395 		break;
396 	default:
397 		ret = 0;
398 		goto fail_alloc;
399 	}
400 
401 	if (ret) {
402 		if (ret == -ENODEV)
403 			ret = 0;
404 		goto fail_alloc;
405 	}
406 
407 	if (!nouveau_get_backlight_name(backlight_name, bl)) {
408 		NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
409 		goto fail_alloc;
410 	}
411 
412 	props.type = BACKLIGHT_RAW;
413 	bl->dev = backlight_device_register(backlight_name, connector->kdev,
414 					    nv_encoder, ops, &props);
415 	if (IS_ERR(bl->dev)) {
416 		if (bl->id >= 0)
417 			ida_simple_remove(&bl_ida, bl->id);
418 		ret = PTR_ERR(bl->dev);
419 		goto fail_alloc;
420 	}
421 
422 	nouveau_connector(connector)->backlight = bl;
423 	if (!bl->dev->props.brightness)
424 		bl->dev->props.brightness =
425 			bl->dev->ops->get_brightness(bl->dev);
426 	backlight_update_status(bl->dev);
427 
428 	return 0;
429 
430 fail_alloc:
431 	kfree(bl);
432 	return ret;
433 }
434 
435 void
436 nouveau_backlight_fini(struct drm_connector *connector)
437 {
438 	struct nouveau_connector *nv_conn = nouveau_connector(connector);
439 	struct nouveau_backlight *bl = nv_conn->backlight;
440 
441 	if (!bl)
442 		return;
443 
444 	if (bl->id >= 0)
445 		ida_simple_remove(&bl_ida, bl->id);
446 
447 	backlight_device_unregister(bl->dev);
448 	nv_conn->backlight = NULL;
449 	kfree(bl);
450 }
451 
452 void
453 nouveau_backlight_ctor(void)
454 {
455 	ida_init(&bl_ida);
456 }
457 
458 void
459 nouveau_backlight_dtor(void)
460 {
461 	ida_destroy(&bl_ida);
462 }
463