drm/i915: Add and document the drm.i915.enable_execlists tunable.
[dragonfly.git] / sys / dev / drm / i915 / intel_panel.c
1 /*
2  * Copyright © 2006-2010 Intel Corporation
3  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *      Eric Anholt <eric@anholt.net>
26  *      Dave Airlie <airlied@linux.ie>
27  *      Jesse Barnes <jesse.barnes@intel.com>
28  *      Chris Wilson <chris@chris-wilson.co.uk>
29  */
30
31 #include <linux/moduleparam.h>
32 #include "intel_drv.h"
33
34 void
35 intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
36                        struct drm_display_mode *adjusted_mode)
37 {
38         drm_mode_copy(adjusted_mode, fixed_mode);
39
40         drm_mode_set_crtcinfo(adjusted_mode, 0);
41 }
42
43 /**
44  * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
45  * @dev: drm device
46  * @fixed_mode : panel native mode
47  * @connector: LVDS/eDP connector
48  *
49  * Return downclock_avail
50  * Find the reduced downclock for LVDS/eDP in EDID.
51  */
52 struct drm_display_mode *
53 intel_find_panel_downclock(struct drm_device *dev,
54                         struct drm_display_mode *fixed_mode,
55                         struct drm_connector *connector)
56 {
57         struct drm_display_mode *scan, *tmp_mode;
58         int temp_downclock;
59
60         temp_downclock = fixed_mode->clock;
61         tmp_mode = NULL;
62
63         list_for_each_entry(scan, &connector->probed_modes, head) {
64                 /*
65                  * If one mode has the same resolution with the fixed_panel
66                  * mode while they have the different refresh rate, it means
67                  * that the reduced downclock is found. In such
68                  * case we can set the different FPx0/1 to dynamically select
69                  * between low and high frequency.
70                  */
71                 if (scan->hdisplay == fixed_mode->hdisplay &&
72                     scan->hsync_start == fixed_mode->hsync_start &&
73                     scan->hsync_end == fixed_mode->hsync_end &&
74                     scan->htotal == fixed_mode->htotal &&
75                     scan->vdisplay == fixed_mode->vdisplay &&
76                     scan->vsync_start == fixed_mode->vsync_start &&
77                     scan->vsync_end == fixed_mode->vsync_end &&
78                     scan->vtotal == fixed_mode->vtotal) {
79                         if (scan->clock < temp_downclock) {
80                                 /*
81                                  * The downclock is already found. But we
82                                  * expect to find the lower downclock.
83                                  */
84                                 temp_downclock = scan->clock;
85                                 tmp_mode = scan;
86                         }
87                 }
88         }
89
90         if (temp_downclock < fixed_mode->clock)
91                 return drm_mode_duplicate(dev, tmp_mode);
92         else
93                 return NULL;
94 }
95
96 /* adjusted_mode has been preset to be the panel's fixed mode */
97 void
98 intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
99                         struct intel_crtc_state *pipe_config,
100                         int fitting_mode)
101 {
102         struct drm_display_mode *adjusted_mode;
103         int x, y, width, height;
104
105         adjusted_mode = &pipe_config->base.adjusted_mode;
106
107         x = y = width = height = 0;
108
109         /* Native modes don't need fitting */
110         if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
111             adjusted_mode->vdisplay == pipe_config->pipe_src_h)
112                 goto done;
113
114         switch (fitting_mode) {
115         case DRM_MODE_SCALE_CENTER:
116                 width = pipe_config->pipe_src_w;
117                 height = pipe_config->pipe_src_h;
118                 x = (adjusted_mode->hdisplay - width + 1)/2;
119                 y = (adjusted_mode->vdisplay - height + 1)/2;
120                 break;
121
122         case DRM_MODE_SCALE_ASPECT:
123                 /* Scale but preserve the aspect ratio */
124                 {
125                         u32 scaled_width = adjusted_mode->hdisplay
126                                 * pipe_config->pipe_src_h;
127                         u32 scaled_height = pipe_config->pipe_src_w
128                                 * adjusted_mode->vdisplay;
129                         if (scaled_width > scaled_height) { /* pillar */
130                                 width = scaled_height / pipe_config->pipe_src_h;
131                                 if (width & 1)
132                                         width++;
133                                 x = (adjusted_mode->hdisplay - width + 1) / 2;
134                                 y = 0;
135                                 height = adjusted_mode->vdisplay;
136                         } else if (scaled_width < scaled_height) { /* letter */
137                                 height = scaled_width / pipe_config->pipe_src_w;
138                                 if (height & 1)
139                                     height++;
140                                 y = (adjusted_mode->vdisplay - height + 1) / 2;
141                                 x = 0;
142                                 width = adjusted_mode->hdisplay;
143                         } else {
144                                 x = y = 0;
145                                 width = adjusted_mode->hdisplay;
146                                 height = adjusted_mode->vdisplay;
147                         }
148                 }
149                 break;
150
151         case DRM_MODE_SCALE_FULLSCREEN:
152                 x = y = 0;
153                 width = adjusted_mode->hdisplay;
154                 height = adjusted_mode->vdisplay;
155                 break;
156
157         default:
158                 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
159                 return;
160         }
161
162 done:
163         pipe_config->pch_pfit.pos = (x << 16) | y;
164         pipe_config->pch_pfit.size = (width << 16) | height;
165         pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
166 }
167
168 static void
169 centre_horizontally(struct drm_display_mode *mode,
170                     int width)
171 {
172         u32 border, sync_pos, blank_width, sync_width;
173
174         /* keep the hsync and hblank widths constant */
175         sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
176         blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
177         sync_pos = (blank_width - sync_width + 1) / 2;
178
179         border = (mode->hdisplay - width + 1) / 2;
180         border += border & 1; /* make the border even */
181
182         mode->crtc_hdisplay = width;
183         mode->crtc_hblank_start = width + border;
184         mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
185
186         mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
187         mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
188 }
189
190 static void
191 centre_vertically(struct drm_display_mode *mode,
192                   int height)
193 {
194         u32 border, sync_pos, blank_width, sync_width;
195
196         /* keep the vsync and vblank widths constant */
197         sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
198         blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
199         sync_pos = (blank_width - sync_width + 1) / 2;
200
201         border = (mode->vdisplay - height + 1) / 2;
202
203         mode->crtc_vdisplay = height;
204         mode->crtc_vblank_start = height + border;
205         mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
206
207         mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
208         mode->crtc_vsync_end = mode->crtc_vsync_start + sync_width;
209 }
210
211 static inline u32 panel_fitter_scaling(u32 source, u32 target)
212 {
213         /*
214          * Floating point operation is not supported. So the FACTOR
215          * is defined, which can avoid the floating point computation
216          * when calculating the panel ratio.
217          */
218 #define ACCURACY 12
219 #define FACTOR (1 << ACCURACY)
220         u32 ratio = source * FACTOR / target;
221         return (FACTOR * ratio + FACTOR/2) / FACTOR;
222 }
223
224 static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
225                               u32 *pfit_control)
226 {
227         struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
228         u32 scaled_width = adjusted_mode->hdisplay *
229                 pipe_config->pipe_src_h;
230         u32 scaled_height = pipe_config->pipe_src_w *
231                 adjusted_mode->vdisplay;
232
233         /* 965+ is easy, it does everything in hw */
234         if (scaled_width > scaled_height)
235                 *pfit_control |= PFIT_ENABLE |
236                         PFIT_SCALING_PILLAR;
237         else if (scaled_width < scaled_height)
238                 *pfit_control |= PFIT_ENABLE |
239                         PFIT_SCALING_LETTER;
240         else if (adjusted_mode->hdisplay != pipe_config->pipe_src_w)
241                 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
242 }
243
244 static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
245                               u32 *pfit_control, u32 *pfit_pgm_ratios,
246                               u32 *border)
247 {
248         struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
249         u32 scaled_width = adjusted_mode->hdisplay *
250                 pipe_config->pipe_src_h;
251         u32 scaled_height = pipe_config->pipe_src_w *
252                 adjusted_mode->vdisplay;
253         u32 bits;
254
255         /*
256          * For earlier chips we have to calculate the scaling
257          * ratio by hand and program it into the
258          * PFIT_PGM_RATIO register
259          */
260         if (scaled_width > scaled_height) { /* pillar */
261                 centre_horizontally(adjusted_mode,
262                                     scaled_height /
263                                     pipe_config->pipe_src_h);
264
265                 *border = LVDS_BORDER_ENABLE;
266                 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay) {
267                         bits = panel_fitter_scaling(pipe_config->pipe_src_h,
268                                                     adjusted_mode->vdisplay);
269
270                         *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
271                                              bits << PFIT_VERT_SCALE_SHIFT);
272                         *pfit_control |= (PFIT_ENABLE |
273                                           VERT_INTERP_BILINEAR |
274                                           HORIZ_INTERP_BILINEAR);
275                 }
276         } else if (scaled_width < scaled_height) { /* letter */
277                 centre_vertically(adjusted_mode,
278                                   scaled_width /
279                                   pipe_config->pipe_src_w);
280
281                 *border = LVDS_BORDER_ENABLE;
282                 if (pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
283                         bits = panel_fitter_scaling(pipe_config->pipe_src_w,
284                                                     adjusted_mode->hdisplay);
285
286                         *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
287                                              bits << PFIT_VERT_SCALE_SHIFT);
288                         *pfit_control |= (PFIT_ENABLE |
289                                           VERT_INTERP_BILINEAR |
290                                           HORIZ_INTERP_BILINEAR);
291                 }
292         } else {
293                 /* Aspects match, Let hw scale both directions */
294                 *pfit_control |= (PFIT_ENABLE |
295                                   VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
296                                   VERT_INTERP_BILINEAR |
297                                   HORIZ_INTERP_BILINEAR);
298         }
299 }
300
301 void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
302                               struct intel_crtc_state *pipe_config,
303                               int fitting_mode)
304 {
305         struct drm_device *dev = intel_crtc->base.dev;
306         u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
307         struct drm_display_mode *adjusted_mode;
308
309         adjusted_mode = &pipe_config->base.adjusted_mode;
310
311         /* Native modes don't need fitting */
312         if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
313             adjusted_mode->vdisplay == pipe_config->pipe_src_h)
314                 goto out;
315
316         switch (fitting_mode) {
317         case DRM_MODE_SCALE_CENTER:
318                 /*
319                  * For centered modes, we have to calculate border widths &
320                  * heights and modify the values programmed into the CRTC.
321                  */
322                 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
323                 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
324                 border = LVDS_BORDER_ENABLE;
325                 break;
326         case DRM_MODE_SCALE_ASPECT:
327                 /* Scale but preserve the aspect ratio */
328                 if (INTEL_INFO(dev)->gen >= 4)
329                         i965_scale_aspect(pipe_config, &pfit_control);
330                 else
331                         i9xx_scale_aspect(pipe_config, &pfit_control,
332                                           &pfit_pgm_ratios, &border);
333                 break;
334         case DRM_MODE_SCALE_FULLSCREEN:
335                 /*
336                  * Full scaling, even if it changes the aspect ratio.
337                  * Fortunately this is all done for us in hw.
338                  */
339                 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay ||
340                     pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
341                         pfit_control |= PFIT_ENABLE;
342                         if (INTEL_INFO(dev)->gen >= 4)
343                                 pfit_control |= PFIT_SCALING_AUTO;
344                         else
345                                 pfit_control |= (VERT_AUTO_SCALE |
346                                                  VERT_INTERP_BILINEAR |
347                                                  HORIZ_AUTO_SCALE |
348                                                  HORIZ_INTERP_BILINEAR);
349                 }
350                 break;
351         default:
352                 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
353                 return;
354         }
355
356         /* 965+ wants fuzzy fitting */
357         /* FIXME: handle multiple panels by failing gracefully */
358         if (INTEL_INFO(dev)->gen >= 4)
359                 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
360                                  PFIT_FILTER_FUZZY);
361
362 out:
363         if ((pfit_control & PFIT_ENABLE) == 0) {
364                 pfit_control = 0;
365                 pfit_pgm_ratios = 0;
366         }
367
368         /* Make sure pre-965 set dither correctly for 18bpp panels. */
369         if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
370                 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
371
372         pipe_config->gmch_pfit.control = pfit_control;
373         pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
374         pipe_config->gmch_pfit.lvds_border_bits = border;
375 }
376
377 enum drm_connector_status
378 intel_panel_detect(struct drm_device *dev)
379 {
380         struct drm_i915_private *dev_priv = dev->dev_private;
381
382         /* Assume that the BIOS does not lie through the OpRegion... */
383         if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
384                 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
385                         connector_status_connected :
386                         connector_status_disconnected;
387         }
388
389         switch (i915.panel_ignore_lid) {
390         case -2:
391                 return connector_status_connected;
392         case -1:
393                 return connector_status_disconnected;
394         default:
395                 return connector_status_unknown;
396         }
397 }
398
399 /**
400  * scale - scale values from one range to another
401  *
402  * @source_val: value in range [@source_min..@source_max]
403  *
404  * Return @source_val in range [@source_min..@source_max] scaled to range
405  * [@target_min..@target_max].
406  */
407 static uint32_t scale(uint32_t source_val,
408                       uint32_t source_min, uint32_t source_max,
409                       uint32_t target_min, uint32_t target_max)
410 {
411         uint64_t target_val;
412
413         WARN_ON(source_min > source_max);
414         WARN_ON(target_min > target_max);
415
416         /* defensive */
417         source_val = clamp(source_val, source_min, source_max);
418
419         /* avoid overflows */
420         target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
421                         (target_max - target_min), source_max - source_min);
422         target_val += target_min;
423
424         return target_val;
425 }
426
427 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
428 static inline u32 scale_user_to_hw(struct intel_connector *connector,
429                                    u32 user_level, u32 user_max)
430 {
431         struct intel_panel *panel = &connector->panel;
432
433         return scale(user_level, 0, user_max,
434                      panel->backlight.min, panel->backlight.max);
435 }
436
437 /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
438  * to [hw_min..hw_max]. */
439 static inline u32 clamp_user_to_hw(struct intel_connector *connector,
440                                    u32 user_level, u32 user_max)
441 {
442         struct intel_panel *panel = &connector->panel;
443         u32 hw_level;
444
445         hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
446         hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
447
448         return hw_level;
449 }
450
451 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
452 static inline u32 scale_hw_to_user(struct intel_connector *connector,
453                                    u32 hw_level, u32 user_max)
454 {
455         struct intel_panel *panel = &connector->panel;
456
457         return scale(hw_level, panel->backlight.min, panel->backlight.max,
458                      0, user_max);
459 }
460
461 static u32 intel_panel_compute_brightness(struct intel_connector *connector,
462                                           u32 val)
463 {
464         struct drm_device *dev = connector->base.dev;
465         struct drm_i915_private *dev_priv = dev->dev_private;
466         struct intel_panel *panel = &connector->panel;
467
468         WARN_ON(panel->backlight.max == 0);
469
470         if (i915.invert_brightness < 0)
471                 return val;
472
473         if (i915.invert_brightness > 0 ||
474             dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
475                 return panel->backlight.max - val;
476         }
477
478         return val;
479 }
480
481 static u32 bdw_get_backlight(struct intel_connector *connector)
482 {
483         struct drm_device *dev = connector->base.dev;
484         struct drm_i915_private *dev_priv = dev->dev_private;
485
486         return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
487 }
488
489 static u32 pch_get_backlight(struct intel_connector *connector)
490 {
491         struct drm_device *dev = connector->base.dev;
492         struct drm_i915_private *dev_priv = dev->dev_private;
493
494         return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
495 }
496
497 static u32 i9xx_get_backlight(struct intel_connector *connector)
498 {
499         struct drm_device *dev = connector->base.dev;
500         struct drm_i915_private *dev_priv = dev->dev_private;
501         struct intel_panel *panel = &connector->panel;
502         u32 val;
503
504         val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
505         if (INTEL_INFO(dev)->gen < 4)
506                 val >>= 1;
507
508         if (panel->backlight.combination_mode) {
509                 u8 lbpc;
510
511                 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
512                 val *= lbpc;
513         }
514
515         return val;
516 }
517
518 static u32 _vlv_get_backlight(struct drm_device *dev, enum i915_pipe pipe)
519 {
520         struct drm_i915_private *dev_priv = dev->dev_private;
521
522         if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
523                 return 0;
524
525         return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
526 }
527
528 static u32 vlv_get_backlight(struct intel_connector *connector)
529 {
530         struct drm_device *dev = connector->base.dev;
531         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
532
533         return _vlv_get_backlight(dev, pipe);
534 }
535
536 #if 0
537 static u32 intel_panel_get_backlight(struct intel_connector *connector)
538 {
539         struct drm_device *dev = connector->base.dev;
540         struct drm_i915_private *dev_priv = dev->dev_private;
541         struct intel_panel *panel = &connector->panel;
542         u32 val = 0;
543
544         mutex_lock(&dev_priv->backlight_lock);
545
546         if (panel->backlight.enabled) {
547                 val = dev_priv->display.get_backlight(connector);
548                 val = intel_panel_compute_brightness(connector, val);
549         }
550
551         mutex_unlock(&dev_priv->backlight_lock);
552
553         DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
554         return val;
555 }
556 #endif
557
558 static void bdw_set_backlight(struct intel_connector *connector, u32 level)
559 {
560         struct drm_device *dev = connector->base.dev;
561         struct drm_i915_private *dev_priv = dev->dev_private;
562         u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
563         I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
564 }
565
566 static void pch_set_backlight(struct intel_connector *connector, u32 level)
567 {
568         struct drm_device *dev = connector->base.dev;
569         struct drm_i915_private *dev_priv = dev->dev_private;
570         u32 tmp;
571
572         tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
573         I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
574 }
575
576 static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
577 {
578         struct drm_device *dev = connector->base.dev;
579         struct drm_i915_private *dev_priv = dev->dev_private;
580         struct intel_panel *panel = &connector->panel;
581         u32 tmp, mask;
582
583         WARN_ON(panel->backlight.max == 0);
584
585         if (panel->backlight.combination_mode) {
586                 u8 lbpc;
587
588                 lbpc = level * 0xfe / panel->backlight.max + 1;
589                 level /= lbpc;
590                 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
591         }
592
593         if (IS_GEN4(dev)) {
594                 mask = BACKLIGHT_DUTY_CYCLE_MASK;
595         } else {
596                 level <<= 1;
597                 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
598         }
599
600         tmp = I915_READ(BLC_PWM_CTL) & ~mask;
601         I915_WRITE(BLC_PWM_CTL, tmp | level);
602 }
603
604 static void vlv_set_backlight(struct intel_connector *connector, u32 level)
605 {
606         struct drm_device *dev = connector->base.dev;
607         struct drm_i915_private *dev_priv = dev->dev_private;
608         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
609         u32 tmp;
610
611         if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
612                 return;
613
614         tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
615         I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
616 }
617
618 static void
619 intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
620 {
621         struct drm_device *dev = connector->base.dev;
622         struct drm_i915_private *dev_priv = dev->dev_private;
623
624         DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
625
626         level = intel_panel_compute_brightness(connector, level);
627         dev_priv->display.set_backlight(connector, level);
628 }
629
630 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
631 static void intel_panel_set_backlight(struct intel_connector *connector,
632                                       u32 user_level, u32 user_max)
633 {
634         struct drm_device *dev = connector->base.dev;
635         struct drm_i915_private *dev_priv = dev->dev_private;
636         struct intel_panel *panel = &connector->panel;
637         u32 hw_level;
638
639         if (!panel->backlight.present)
640                 return;
641
642         mutex_lock(&dev_priv->backlight_lock);
643
644         WARN_ON(panel->backlight.max == 0);
645
646         hw_level = scale_user_to_hw(connector, user_level, user_max);
647         panel->backlight.level = hw_level;
648
649         if (panel->backlight.enabled)
650                 intel_panel_actually_set_backlight(connector, hw_level);
651
652         mutex_unlock(&dev_priv->backlight_lock);
653 }
654
655 /* set backlight brightness to level in range [0..max], assuming hw min is
656  * respected.
657  */
658 void intel_panel_set_backlight_acpi(struct intel_connector *connector,
659                                     u32 user_level, u32 user_max)
660 {
661         struct drm_device *dev = connector->base.dev;
662         struct drm_i915_private *dev_priv = dev->dev_private;
663         struct intel_panel *panel = &connector->panel;
664         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
665         u32 hw_level;
666
667         /*
668          * INVALID_PIPE may occur during driver init because
669          * connection_mutex isn't held across the entire backlight
670          * setup + modeset readout, and the BIOS can issue the
671          * requests at any time.
672          */
673         if (!panel->backlight.present || pipe == INVALID_PIPE)
674                 return;
675
676         mutex_lock(&dev_priv->backlight_lock);
677
678         WARN_ON(panel->backlight.max == 0);
679
680         hw_level = clamp_user_to_hw(connector, user_level, user_max);
681         panel->backlight.level = hw_level;
682
683         if (panel->backlight.device)
684                 panel->backlight.device->props.brightness =
685                         scale_hw_to_user(connector,
686                                          panel->backlight.level,
687                                          panel->backlight.device->props.max_brightness);
688
689         if (panel->backlight.enabled)
690                 intel_panel_actually_set_backlight(connector, hw_level);
691
692         mutex_unlock(&dev_priv->backlight_lock);
693 }
694
695 static void pch_disable_backlight(struct intel_connector *connector)
696 {
697         struct drm_device *dev = connector->base.dev;
698         struct drm_i915_private *dev_priv = dev->dev_private;
699         u32 tmp;
700
701         intel_panel_actually_set_backlight(connector, 0);
702
703         tmp = I915_READ(BLC_PWM_CPU_CTL2);
704         I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
705
706         tmp = I915_READ(BLC_PWM_PCH_CTL1);
707         I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
708 }
709
710 static void i9xx_disable_backlight(struct intel_connector *connector)
711 {
712         intel_panel_actually_set_backlight(connector, 0);
713 }
714
715 static void i965_disable_backlight(struct intel_connector *connector)
716 {
717         struct drm_device *dev = connector->base.dev;
718         struct drm_i915_private *dev_priv = dev->dev_private;
719         u32 tmp;
720
721         intel_panel_actually_set_backlight(connector, 0);
722
723         tmp = I915_READ(BLC_PWM_CTL2);
724         I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
725 }
726
727 static void vlv_disable_backlight(struct intel_connector *connector)
728 {
729         struct drm_device *dev = connector->base.dev;
730         struct drm_i915_private *dev_priv = dev->dev_private;
731         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
732         u32 tmp;
733
734         if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
735                 return;
736
737         intel_panel_actually_set_backlight(connector, 0);
738
739         tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
740         I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
741 }
742
743 void intel_panel_disable_backlight(struct intel_connector *connector)
744 {
745         struct drm_device *dev = connector->base.dev;
746         struct drm_i915_private *dev_priv = dev->dev_private;
747         struct intel_panel *panel = &connector->panel;
748
749         if (!panel->backlight.present)
750                 return;
751
752         /*
753          * Do not disable backlight on the vgaswitcheroo path. When switching
754          * away from i915, the other client may depend on i915 to handle the
755          * backlight. This will leave the backlight on unnecessarily when
756          * another client is not activated.
757          */
758         if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
759                 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
760                 return;
761         }
762
763         mutex_lock(&dev_priv->backlight_lock);
764
765         if (panel->backlight.device)
766                 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
767         panel->backlight.enabled = false;
768         dev_priv->display.disable_backlight(connector);
769
770         mutex_unlock(&dev_priv->backlight_lock);
771 }
772
773 static void bdw_enable_backlight(struct intel_connector *connector)
774 {
775         struct drm_device *dev = connector->base.dev;
776         struct drm_i915_private *dev_priv = dev->dev_private;
777         struct intel_panel *panel = &connector->panel;
778         u32 pch_ctl1, pch_ctl2;
779
780         pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
781         if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
782                 DRM_DEBUG_KMS("pch backlight already enabled\n");
783                 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
784                 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
785         }
786
787         pch_ctl2 = panel->backlight.max << 16;
788         I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
789
790         pch_ctl1 = 0;
791         if (panel->backlight.active_low_pwm)
792                 pch_ctl1 |= BLM_PCH_POLARITY;
793
794         /* After LPT, override is the default. */
795         if (HAS_PCH_LPT(dev_priv))
796                 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
797
798         I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
799         POSTING_READ(BLC_PWM_PCH_CTL1);
800         I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
801
802         /* This won't stick until the above enable. */
803         intel_panel_actually_set_backlight(connector, panel->backlight.level);
804 }
805
806 static void pch_enable_backlight(struct intel_connector *connector)
807 {
808         struct drm_device *dev = connector->base.dev;
809         struct drm_i915_private *dev_priv = dev->dev_private;
810         struct intel_panel *panel = &connector->panel;
811         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
812         enum transcoder cpu_transcoder =
813                 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
814         u32 cpu_ctl2, pch_ctl1, pch_ctl2;
815
816         cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
817         if (cpu_ctl2 & BLM_PWM_ENABLE) {
818                 DRM_DEBUG_KMS("cpu backlight already enabled\n");
819                 cpu_ctl2 &= ~BLM_PWM_ENABLE;
820                 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
821         }
822
823         pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
824         if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
825                 DRM_DEBUG_KMS("pch backlight already enabled\n");
826                 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
827                 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
828         }
829
830         if (cpu_transcoder == TRANSCODER_EDP)
831                 cpu_ctl2 = BLM_TRANSCODER_EDP;
832         else
833                 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
834         I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
835         POSTING_READ(BLC_PWM_CPU_CTL2);
836         I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
837
838         /* This won't stick until the above enable. */
839         intel_panel_actually_set_backlight(connector, panel->backlight.level);
840
841         pch_ctl2 = panel->backlight.max << 16;
842         I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
843
844         pch_ctl1 = 0;
845         if (panel->backlight.active_low_pwm)
846                 pch_ctl1 |= BLM_PCH_POLARITY;
847
848         I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
849         POSTING_READ(BLC_PWM_PCH_CTL1);
850         I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
851 }
852
853 static void i9xx_enable_backlight(struct intel_connector *connector)
854 {
855         struct drm_device *dev = connector->base.dev;
856         struct drm_i915_private *dev_priv = dev->dev_private;
857         struct intel_panel *panel = &connector->panel;
858         u32 ctl, freq;
859
860         ctl = I915_READ(BLC_PWM_CTL);
861         if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
862                 DRM_DEBUG_KMS("backlight already enabled\n");
863                 I915_WRITE(BLC_PWM_CTL, 0);
864         }
865
866         freq = panel->backlight.max;
867         if (panel->backlight.combination_mode)
868                 freq /= 0xff;
869
870         ctl = freq << 17;
871         if (panel->backlight.combination_mode)
872                 ctl |= BLM_LEGACY_MODE;
873         if (IS_PINEVIEW(dev) && panel->backlight.active_low_pwm)
874                 ctl |= BLM_POLARITY_PNV;
875
876         I915_WRITE(BLC_PWM_CTL, ctl);
877         POSTING_READ(BLC_PWM_CTL);
878
879         /* XXX: combine this into above write? */
880         intel_panel_actually_set_backlight(connector, panel->backlight.level);
881 }
882
883 static void i965_enable_backlight(struct intel_connector *connector)
884 {
885         struct drm_device *dev = connector->base.dev;
886         struct drm_i915_private *dev_priv = dev->dev_private;
887         struct intel_panel *panel = &connector->panel;
888         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
889         u32 ctl, ctl2, freq;
890
891         ctl2 = I915_READ(BLC_PWM_CTL2);
892         if (ctl2 & BLM_PWM_ENABLE) {
893                 DRM_DEBUG_KMS("backlight already enabled\n");
894                 ctl2 &= ~BLM_PWM_ENABLE;
895                 I915_WRITE(BLC_PWM_CTL2, ctl2);
896         }
897
898         freq = panel->backlight.max;
899         if (panel->backlight.combination_mode)
900                 freq /= 0xff;
901
902         ctl = freq << 16;
903         I915_WRITE(BLC_PWM_CTL, ctl);
904
905         ctl2 = BLM_PIPE(pipe);
906         if (panel->backlight.combination_mode)
907                 ctl2 |= BLM_COMBINATION_MODE;
908         if (panel->backlight.active_low_pwm)
909                 ctl2 |= BLM_POLARITY_I965;
910         I915_WRITE(BLC_PWM_CTL2, ctl2);
911         POSTING_READ(BLC_PWM_CTL2);
912         I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
913
914         intel_panel_actually_set_backlight(connector, panel->backlight.level);
915 }
916
917 static void vlv_enable_backlight(struct intel_connector *connector)
918 {
919         struct drm_device *dev = connector->base.dev;
920         struct drm_i915_private *dev_priv = dev->dev_private;
921         struct intel_panel *panel = &connector->panel;
922         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
923         u32 ctl, ctl2;
924
925         if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
926                 return;
927
928         ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
929         if (ctl2 & BLM_PWM_ENABLE) {
930                 DRM_DEBUG_KMS("backlight already enabled\n");
931                 ctl2 &= ~BLM_PWM_ENABLE;
932                 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
933         }
934
935         ctl = panel->backlight.max << 16;
936         I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
937
938         /* XXX: combine this into above write? */
939         intel_panel_actually_set_backlight(connector, panel->backlight.level);
940
941         ctl2 = 0;
942         if (panel->backlight.active_low_pwm)
943                 ctl2 |= BLM_POLARITY_I965;
944         I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
945         POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
946         I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
947 }
948
949 void intel_panel_enable_backlight(struct intel_connector *connector)
950 {
951         struct drm_device *dev = connector->base.dev;
952         struct drm_i915_private *dev_priv = dev->dev_private;
953         struct intel_panel *panel = &connector->panel;
954         enum i915_pipe pipe = intel_get_pipe_from_connector(connector);
955
956         if (!panel->backlight.present)
957                 return;
958
959         DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
960
961         mutex_lock(&dev_priv->backlight_lock);
962
963         WARN_ON(panel->backlight.max == 0);
964
965         if (panel->backlight.level <= panel->backlight.min) {
966                 panel->backlight.level = panel->backlight.max;
967                 if (panel->backlight.device)
968                         panel->backlight.device->props.brightness =
969                                 scale_hw_to_user(connector,
970                                                  panel->backlight.level,
971                                                  panel->backlight.device->props.max_brightness);
972         }
973
974         dev_priv->display.enable_backlight(connector);
975         panel->backlight.enabled = true;
976         if (panel->backlight.device)
977                 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
978
979         mutex_unlock(&dev_priv->backlight_lock);
980 }
981
982 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
983 static int intel_backlight_device_update_status(struct backlight_device *bd)
984 {
985 #if 0
986         struct intel_connector *connector = bl_get_data(bd);
987         struct intel_panel *panel = &connector->panel;
988         struct drm_device *dev = connector->base.dev;
989
990         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
991         DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
992                       bd->props.brightness, bd->props.max_brightness);
993         intel_panel_set_backlight(connector, bd->props.brightness,
994                                   bd->props.max_brightness);
995
996         /*
997          * Allow flipping bl_power as a sub-state of enabled. Sadly the
998          * backlight class device does not make it easy to to differentiate
999          * between callbacks for brightness and bl_power, so our backlight_power
1000          * callback needs to take this into account.
1001          */
1002         if (panel->backlight.enabled) {
1003                 if (panel->backlight_power) {
1004                         bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1005                                 bd->props.brightness != 0;
1006                         panel->backlight_power(connector, enable);
1007                 }
1008         } else {
1009                 bd->props.power = FB_BLANK_POWERDOWN;
1010         }
1011
1012         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1013 #endif
1014         return 0;
1015 }
1016
1017 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1018 {
1019 #if 0
1020         struct intel_connector *connector = bl_get_data(bd);
1021         struct drm_device *dev = connector->base.dev;
1022         struct drm_i915_private *dev_priv = dev->dev_private;
1023         u32 hw_level;
1024         int ret;
1025
1026         intel_runtime_pm_get(dev_priv);
1027         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1028
1029         hw_level = intel_panel_get_backlight(connector);
1030         ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1031
1032         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1033         intel_runtime_pm_put(dev_priv);
1034
1035         return ret;
1036 #endif
1037         return 0;
1038 }
1039
1040 static const struct backlight_ops intel_backlight_device_ops = {
1041         .update_status = intel_backlight_device_update_status,
1042         .get_brightness = intel_backlight_device_get_brightness,
1043 };
1044
1045 static int intel_backlight_device_register(struct intel_connector *connector)
1046 {
1047         struct intel_panel *panel = &connector->panel;
1048         struct backlight_properties props;
1049
1050         if (WARN_ON(panel->backlight.device))
1051                 return -ENODEV;
1052
1053         if (!panel->backlight.present)
1054                 return 0;
1055
1056         WARN_ON(panel->backlight.max == 0);
1057
1058         memset(&props, 0, sizeof(props));
1059         props.type = BACKLIGHT_RAW;
1060
1061         /*
1062          * Note: Everything should work even if the backlight device max
1063          * presented to the userspace is arbitrarily chosen.
1064          */
1065         props.max_brightness = panel->backlight.max;
1066         props.brightness = scale_hw_to_user(connector,
1067                                             panel->backlight.level,
1068                                             props.max_brightness);
1069
1070         if (panel->backlight.enabled)
1071                 props.power = FB_BLANK_UNBLANK;
1072         else
1073                 props.power = FB_BLANK_POWERDOWN;
1074
1075         /*
1076          * Note: using the same name independent of the connector prevents
1077          * registration of multiple backlight devices in the driver.
1078          */
1079 #if 0
1080         panel->backlight.device =
1081                 backlight_device_register("intel_backlight",
1082                                           connector->base.kdev,
1083                                           connector,
1084                                           &intel_backlight_device_ops, &props);
1085
1086         if (IS_ERR(panel->backlight.device)) {
1087                 DRM_ERROR("Failed to register backlight: %ld\n",
1088                           PTR_ERR(panel->backlight.device));
1089                 panel->backlight.device = NULL;
1090                 return -ENODEV;
1091         }
1092 #endif
1093
1094         DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1095                       connector->base.name);
1096
1097         return 0;
1098 }
1099
1100 static void intel_backlight_device_unregister(struct intel_connector *connector)
1101 {
1102 #if 0
1103         struct intel_panel *panel = &connector->panel;
1104
1105         if (panel->backlight.device) {
1106                 backlight_device_unregister(panel->backlight.device);
1107                 panel->backlight.device = NULL;
1108         }
1109 #endif
1110 }
1111 #else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1112 static int intel_backlight_device_register(struct intel_connector *connector)
1113 {
1114         return 0;
1115 }
1116 static void intel_backlight_device_unregister(struct intel_connector *connector)
1117 {
1118 }
1119 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1120
1121 /*
1122  * Note: The setup hooks can't assume pipe is set!
1123  *
1124  * XXX: Query mode clock or hardware clock and program PWM modulation frequency
1125  * appropriately when it's 0. Use VBT and/or sane defaults.
1126  */
1127 static u32 get_backlight_min_vbt(struct intel_connector *connector)
1128 {
1129         struct drm_device *dev = connector->base.dev;
1130         struct drm_i915_private *dev_priv = dev->dev_private;
1131         struct intel_panel *panel = &connector->panel;
1132         int min;
1133
1134         WARN_ON(panel->backlight.max == 0);
1135
1136         /*
1137          * XXX: If the vbt value is 255, it makes min equal to max, which leads
1138          * to problems. There are such machines out there. Either our
1139          * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1140          * against this by letting the minimum be at most (arbitrarily chosen)
1141          * 25% of the max.
1142          */
1143         min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1144         if (min != dev_priv->vbt.backlight.min_brightness) {
1145                 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1146                               dev_priv->vbt.backlight.min_brightness, min);
1147         }
1148
1149         /* vbt value is a coefficient in range [0..255] */
1150         return scale(min, 0, 255, 0, panel->backlight.max);
1151 }
1152
1153 static int bdw_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
1154 {
1155         struct drm_device *dev = connector->base.dev;
1156         struct drm_i915_private *dev_priv = dev->dev_private;
1157         struct intel_panel *panel = &connector->panel;
1158         u32 pch_ctl1, pch_ctl2, val;
1159
1160         pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1161         panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1162
1163         pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1164         panel->backlight.max = pch_ctl2 >> 16;
1165         if (!panel->backlight.max)
1166                 return -ENODEV;
1167
1168         panel->backlight.min = get_backlight_min_vbt(connector);
1169
1170         val = bdw_get_backlight(connector);
1171         panel->backlight.level = intel_panel_compute_brightness(connector, val);
1172
1173         panel->backlight.enabled = (pch_ctl1 & BLM_PCH_PWM_ENABLE) &&
1174                 panel->backlight.level != 0;
1175
1176         return 0;
1177 }
1178
1179 static int pch_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
1180 {
1181         struct drm_device *dev = connector->base.dev;
1182         struct drm_i915_private *dev_priv = dev->dev_private;
1183         struct intel_panel *panel = &connector->panel;
1184         u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1185
1186         pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1187         panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1188
1189         pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1190         panel->backlight.max = pch_ctl2 >> 16;
1191         if (!panel->backlight.max)
1192                 return -ENODEV;
1193
1194         panel->backlight.min = get_backlight_min_vbt(connector);
1195
1196         val = pch_get_backlight(connector);
1197         panel->backlight.level = intel_panel_compute_brightness(connector, val);
1198
1199         cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1200         panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1201                 (pch_ctl1 & BLM_PCH_PWM_ENABLE) && panel->backlight.level != 0;
1202
1203         return 0;
1204 }
1205
1206 static int i9xx_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
1207 {
1208         struct drm_device *dev = connector->base.dev;
1209         struct drm_i915_private *dev_priv = dev->dev_private;
1210         struct intel_panel *panel = &connector->panel;
1211         u32 ctl, val;
1212
1213         ctl = I915_READ(BLC_PWM_CTL);
1214
1215         if (IS_GEN2(dev) || IS_I915GM(dev) || IS_I945GM(dev))
1216                 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1217
1218         if (IS_PINEVIEW(dev))
1219                 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1220
1221         panel->backlight.max = ctl >> 17;
1222         if (panel->backlight.combination_mode)
1223                 panel->backlight.max *= 0xff;
1224
1225         if (!panel->backlight.max)
1226                 return -ENODEV;
1227
1228         panel->backlight.min = get_backlight_min_vbt(connector);
1229
1230         val = i9xx_get_backlight(connector);
1231         panel->backlight.level = intel_panel_compute_brightness(connector, val);
1232
1233         panel->backlight.enabled = panel->backlight.level != 0;
1234
1235         return 0;
1236 }
1237
1238 static int i965_setup_backlight(struct intel_connector *connector, enum i915_pipe unused)
1239 {
1240         struct drm_device *dev = connector->base.dev;
1241         struct drm_i915_private *dev_priv = dev->dev_private;
1242         struct intel_panel *panel = &connector->panel;
1243         u32 ctl, ctl2, val;
1244
1245         ctl2 = I915_READ(BLC_PWM_CTL2);
1246         panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1247         panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1248
1249         ctl = I915_READ(BLC_PWM_CTL);
1250         panel->backlight.max = ctl >> 16;
1251         if (panel->backlight.combination_mode)
1252                 panel->backlight.max *= 0xff;
1253
1254         if (!panel->backlight.max)
1255                 return -ENODEV;
1256
1257         panel->backlight.min = get_backlight_min_vbt(connector);
1258
1259         val = i9xx_get_backlight(connector);
1260         panel->backlight.level = intel_panel_compute_brightness(connector, val);
1261
1262         panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1263                 panel->backlight.level != 0;
1264
1265         return 0;
1266 }
1267
1268 static int vlv_setup_backlight(struct intel_connector *connector, enum i915_pipe pipe)
1269 {
1270         struct drm_device *dev = connector->base.dev;
1271         struct drm_i915_private *dev_priv = dev->dev_private;
1272         struct intel_panel *panel = &connector->panel;
1273         enum i915_pipe p;
1274         u32 ctl, ctl2, val;
1275
1276         for_each_pipe(dev_priv, p) {
1277                 u32 cur_val = I915_READ(VLV_BLC_PWM_CTL(p));
1278
1279                 /* Skip if the modulation freq is already set */
1280                 if (cur_val & ~BACKLIGHT_DUTY_CYCLE_MASK)
1281                         continue;
1282
1283                 cur_val &= BACKLIGHT_DUTY_CYCLE_MASK;
1284                 I915_WRITE(VLV_BLC_PWM_CTL(p), (0xf42 << 16) |
1285                            cur_val);
1286         }
1287
1288         if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1289                 return -ENODEV;
1290
1291         ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1292         panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1293
1294         ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
1295         panel->backlight.max = ctl >> 16;
1296         if (!panel->backlight.max)
1297                 return -ENODEV;
1298
1299         panel->backlight.min = get_backlight_min_vbt(connector);
1300
1301         val = _vlv_get_backlight(dev, pipe);
1302         panel->backlight.level = intel_panel_compute_brightness(connector, val);
1303
1304         panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1305                 panel->backlight.level != 0;
1306
1307         return 0;
1308 }
1309
1310 #ifdef __DragonFly__
1311 /*
1312  * Read max backlight level
1313  */
1314 static int
1315 sysctl_backlight_max(SYSCTL_HANDLER_ARGS)
1316 {
1317         int err, val;
1318         struct intel_connector *connector = arg1;
1319         struct drm_device *dev = connector->base.dev;
1320         struct drm_i915_private *dev_priv = dev->dev_private;
1321         struct intel_panel *panel = &connector->panel;
1322
1323         mutex_lock(&dev_priv->backlight_lock);
1324         val = panel->backlight.max;
1325         mutex_unlock(&dev_priv->backlight_lock);
1326
1327         err = sysctl_handle_int(oidp, &val, 0, req);
1328         return(err);
1329 }
1330
1331 /*
1332  * Read/write backlight level
1333  */
1334 static int
1335 sysctl_backlight_handler(SYSCTL_HANDLER_ARGS)
1336 {
1337         struct intel_connector *connector = arg1;
1338         struct drm_device *dev = connector->base.dev;
1339         struct drm_i915_private *dev_priv = dev->dev_private;
1340         struct intel_panel *panel = &connector->panel;
1341         int err, val;
1342         u32 user_level, max_brightness;
1343
1344         mutex_lock(&dev_priv->backlight_lock);
1345         max_brightness = panel->backlight.max;
1346         user_level = scale_hw_to_user(connector, panel->backlight.level,
1347             max_brightness);
1348         mutex_unlock(&dev_priv->backlight_lock);
1349
1350         val = user_level;
1351         err = sysctl_handle_int(oidp, &val, 0, req);
1352         if (err != 0 || req->newptr == NULL) {
1353                 return(err);
1354         }
1355
1356         if (val != user_level && val >= 0 && val <= max_brightness) {
1357                 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1358                 intel_panel_set_backlight(arg1, val, max_brightness);
1359                 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1360         }
1361
1362         return(err);
1363 }
1364 #endif /* __DragonFly__ */
1365
1366 int intel_panel_setup_backlight(struct drm_connector *connector, enum i915_pipe pipe)
1367 {
1368         struct drm_device *dev = connector->dev;
1369         struct drm_i915_private *dev_priv = dev->dev_private;
1370         struct intel_connector *intel_connector = to_intel_connector(connector);
1371         struct intel_panel *panel = &intel_connector->panel;
1372         int ret;
1373
1374         if (!dev_priv->vbt.backlight.present) {
1375                 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1376                         DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1377                 } else {
1378                         DRM_DEBUG_KMS("no backlight present per VBT\n");
1379                         return 0;
1380                 }
1381         }
1382
1383         /* set level and max in panel struct */
1384         mutex_lock(&dev_priv->backlight_lock);
1385         ret = dev_priv->display.setup_backlight(intel_connector, pipe);
1386         mutex_unlock(&dev_priv->backlight_lock);
1387
1388         if (ret) {
1389                 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
1390                               connector->name);
1391                 return ret;
1392         }
1393
1394         panel->backlight.present = true;
1395
1396 #ifdef __DragonFly__
1397         SYSCTL_ADD_PROC(&connector->dev->sysctl->ctx, &sysctl__hw_children,
1398                         OID_AUTO, "backlight_max",
1399                         CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_ANYBODY,
1400                         connector, sizeof(int),
1401                         sysctl_backlight_max,
1402                         "I", "Max backlight level");
1403         SYSCTL_ADD_PROC(&connector->dev->sysctl->ctx, &sysctl__hw_children,
1404                         OID_AUTO, "backlight_level",
1405                         CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
1406                         connector, sizeof(int),
1407                         sysctl_backlight_handler,
1408                         "I", "Backlight level");
1409 #endif
1410
1411         DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1412                       connector->name,
1413                       panel->backlight.enabled ? "enabled" : "disabled",
1414                       panel->backlight.level, panel->backlight.max);
1415
1416         return 0;
1417 }
1418
1419 void intel_panel_destroy_backlight(struct drm_connector *connector)
1420 {
1421         struct intel_connector *intel_connector = to_intel_connector(connector);
1422         struct intel_panel *panel = &intel_connector->panel;
1423
1424         panel->backlight.present = false;
1425 }
1426
1427 /* Set up chip specific backlight functions */
1428 void intel_panel_init_backlight_funcs(struct drm_device *dev)
1429 {
1430         struct drm_i915_private *dev_priv = dev->dev_private;
1431
1432         if (IS_BROADWELL(dev) || (INTEL_INFO(dev)->gen >= 9)) {
1433                 dev_priv->display.setup_backlight = bdw_setup_backlight;
1434                 dev_priv->display.enable_backlight = bdw_enable_backlight;
1435                 dev_priv->display.disable_backlight = pch_disable_backlight;
1436                 dev_priv->display.set_backlight = bdw_set_backlight;
1437                 dev_priv->display.get_backlight = bdw_get_backlight;
1438         } else if (HAS_PCH_SPLIT(dev)) {
1439                 dev_priv->display.setup_backlight = pch_setup_backlight;
1440                 dev_priv->display.enable_backlight = pch_enable_backlight;
1441                 dev_priv->display.disable_backlight = pch_disable_backlight;
1442                 dev_priv->display.set_backlight = pch_set_backlight;
1443                 dev_priv->display.get_backlight = pch_get_backlight;
1444         } else if (IS_VALLEYVIEW(dev)) {
1445                 dev_priv->display.setup_backlight = vlv_setup_backlight;
1446                 dev_priv->display.enable_backlight = vlv_enable_backlight;
1447                 dev_priv->display.disable_backlight = vlv_disable_backlight;
1448                 dev_priv->display.set_backlight = vlv_set_backlight;
1449                 dev_priv->display.get_backlight = vlv_get_backlight;
1450         } else if (IS_GEN4(dev)) {
1451                 dev_priv->display.setup_backlight = i965_setup_backlight;
1452                 dev_priv->display.enable_backlight = i965_enable_backlight;
1453                 dev_priv->display.disable_backlight = i965_disable_backlight;
1454                 dev_priv->display.set_backlight = i9xx_set_backlight;
1455                 dev_priv->display.get_backlight = i9xx_get_backlight;
1456         } else {
1457                 dev_priv->display.setup_backlight = i9xx_setup_backlight;
1458                 dev_priv->display.enable_backlight = i9xx_enable_backlight;
1459                 dev_priv->display.disable_backlight = i9xx_disable_backlight;
1460                 dev_priv->display.set_backlight = i9xx_set_backlight;
1461                 dev_priv->display.get_backlight = i9xx_get_backlight;
1462         }
1463 }
1464
1465 int intel_panel_init(struct intel_panel *panel,
1466                      struct drm_display_mode *fixed_mode,
1467                      struct drm_display_mode *downclock_mode)
1468 {
1469         panel->fixed_mode = fixed_mode;
1470         panel->downclock_mode = downclock_mode;
1471
1472         return 0;
1473 }
1474
1475 void intel_panel_fini(struct intel_panel *panel)
1476 {
1477         struct intel_connector *intel_connector =
1478                 container_of(panel, struct intel_connector, panel);
1479
1480         if (panel->fixed_mode)
1481                 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
1482
1483         if (panel->downclock_mode)
1484                 drm_mode_destroy(intel_connector->base.dev,
1485                                 panel->downclock_mode);
1486 }
1487
1488 void intel_backlight_register(struct drm_device *dev)
1489 {
1490         struct intel_connector *connector;
1491
1492         list_for_each_entry(connector, &dev->mode_config.connector_list, base.head)
1493                 intel_backlight_device_register(connector);
1494 }
1495
1496 void intel_backlight_unregister(struct drm_device *dev)
1497 {
1498         struct intel_connector *connector;
1499
1500         list_for_each_entry(connector, &dev->mode_config.connector_list, base.head)
1501                 intel_backlight_device_unregister(connector);
1502 }