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