xref: /linux/drivers/gpu/drm/i915/display/intel_panel.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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/kernel.h>
32 #include <linux/pwm.h>
33 
34 #include <drm/drm_edid.h>
35 #include <drm/drm_print.h>
36 
37 #include "intel_backlight.h"
38 #include "intel_connector.h"
39 #include "intel_display_core.h"
40 #include "intel_display_driver.h"
41 #include "intel_display_types.h"
42 #include "intel_drrs.h"
43 #include "intel_panel.h"
44 #include "intel_quirks.h"
45 #include "intel_vrr.h"
46 
47 bool intel_panel_use_ssc(struct intel_display *display)
48 {
49 	if (display->params.panel_use_ssc >= 0)
50 		return display->params.panel_use_ssc != 0;
51 	return display->vbt.lvds_use_ssc &&
52 		!intel_has_quirk(display, QUIRK_LVDS_SSC_DISABLE);
53 }
54 
55 const struct drm_display_mode *
56 intel_panel_preferred_fixed_mode(struct intel_connector *connector)
57 {
58 	return list_first_entry_or_null(&connector->panel.fixed_modes,
59 					struct drm_display_mode, head);
60 }
61 
62 static bool is_best_fixed_mode(struct intel_connector *connector,
63 			       int vrefresh, int fixed_mode_vrefresh,
64 			       const struct drm_display_mode *best_mode)
65 {
66 	/* we want to always return something */
67 	if (!best_mode)
68 		return true;
69 
70 	/* pick the fixed_mode that is closest in terms of vrefresh */
71 	return abs(fixed_mode_vrefresh - vrefresh) <
72 		abs(drm_mode_vrefresh(best_mode) - vrefresh);
73 }
74 
75 static bool is_vrr_compatible(const struct drm_display_mode *mode1,
76 			      const struct drm_display_mode *mode2)
77 {
78 	return drm_mode_match(mode1, mode2,
79 			      DRM_MODE_MATCH_CLOCK |
80 			      DRM_MODE_MATCH_TIMINGS_VRR |
81 			      DRM_MODE_MATCH_FLAGS |
82 			      DRM_MODE_MATCH_3D_FLAGS);
83 }
84 
85 static const struct drm_display_mode *
86 intel_panel_fixed_mode_vrr(struct intel_connector *connector,
87 			   const struct drm_display_mode *mode,
88 			   const struct drm_display_mode *vrr_ref_mode)
89 {
90 	const struct drm_display_mode *fixed_mode, *best_mode = NULL;
91 	int vrefresh = drm_mode_vrefresh(mode);
92 
93 	if (!intel_vrr_is_in_range(connector, vrefresh))
94 		return NULL;
95 
96 	if (vrr_ref_mode &&
97 	    !intel_vrr_is_in_range(connector, drm_mode_vrefresh(vrr_ref_mode)))
98 		return NULL;
99 
100 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
101 		int fixed_mode_vrefresh = drm_mode_vrefresh(fixed_mode);
102 
103 		if (!intel_vrr_is_in_range(connector, fixed_mode_vrefresh))
104 			continue;
105 
106 		/*
107 		 * With VRR always pick a mode with equal/higher than requested
108 		 * vrefresh, which we can then reduce to match the requested
109 		 * vrefresh by extending the vblank length.
110 		 */
111 		if (fixed_mode_vrefresh < vrefresh)
112 			continue;
113 
114 		if (vrr_ref_mode &&
115 		    !is_vrr_compatible(fixed_mode, vrr_ref_mode))
116 			continue;
117 
118 		if (is_best_fixed_mode(connector, vrefresh,
119 				       fixed_mode_vrefresh, best_mode))
120 			best_mode = fixed_mode;
121 	}
122 
123 	return best_mode;
124 }
125 
126 const struct drm_display_mode *
127 intel_panel_fixed_mode(struct intel_connector *connector,
128 		       const struct drm_display_mode *mode)
129 {
130 	const struct drm_display_mode *fixed_mode, *best_mode = NULL;
131 	int vrefresh = drm_mode_vrefresh(mode);
132 
133 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
134 		int fixed_mode_vrefresh = drm_mode_vrefresh(fixed_mode);
135 
136 		if (is_best_fixed_mode(connector, vrefresh,
137 				       fixed_mode_vrefresh, best_mode))
138 			best_mode = fixed_mode;
139 	}
140 
141 	return best_mode;
142 }
143 
144 static bool is_alt_drrs_mode(const struct drm_display_mode *mode,
145 			     const struct drm_display_mode *preferred_mode)
146 {
147 	return drm_mode_match(mode, preferred_mode,
148 			      DRM_MODE_MATCH_TIMINGS |
149 			      DRM_MODE_MATCH_FLAGS |
150 			      DRM_MODE_MATCH_3D_FLAGS) &&
151 		mode->clock != preferred_mode->clock;
152 }
153 
154 static bool is_alt_fixed_mode(const struct drm_display_mode *mode,
155 			      const struct drm_display_mode *preferred_mode)
156 {
157 	u32 sync_flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC |
158 		DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC;
159 
160 	return (mode->flags & ~sync_flags) == (preferred_mode->flags & ~sync_flags) &&
161 		mode->hdisplay == preferred_mode->hdisplay &&
162 		mode->vdisplay == preferred_mode->vdisplay;
163 }
164 
165 const struct drm_display_mode *
166 intel_panel_downclock_mode(struct intel_connector *connector,
167 			   const struct drm_display_mode *adjusted_mode)
168 {
169 	const struct drm_display_mode *fixed_mode, *best_mode = NULL;
170 	int min_vrefresh = connector->panel.vbt.seamless_drrs_min_refresh_rate;
171 	int max_vrefresh = drm_mode_vrefresh(adjusted_mode);
172 
173 	/* pick the fixed_mode with the lowest refresh rate */
174 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
175 		int vrefresh = drm_mode_vrefresh(fixed_mode);
176 
177 		if (is_alt_drrs_mode(fixed_mode, adjusted_mode) &&
178 		    vrefresh >= min_vrefresh && vrefresh < max_vrefresh) {
179 			max_vrefresh = vrefresh;
180 			best_mode = fixed_mode;
181 		}
182 	}
183 
184 	return best_mode;
185 }
186 
187 const struct drm_display_mode *
188 intel_panel_highest_mode(struct intel_connector *connector,
189 			 const struct drm_display_mode *adjusted_mode)
190 {
191 	const struct drm_display_mode *fixed_mode, *best_mode = adjusted_mode;
192 
193 	/* pick the fixed_mode that has the highest clock */
194 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
195 		if (fixed_mode->clock > best_mode->clock)
196 			best_mode = fixed_mode;
197 	}
198 
199 	return best_mode;
200 }
201 
202 int intel_panel_get_modes(struct intel_connector *connector)
203 {
204 	const struct drm_display_mode *fixed_mode;
205 	int num_modes = 0;
206 
207 	list_for_each_entry(fixed_mode, &connector->panel.fixed_modes, head) {
208 		struct drm_display_mode *mode;
209 
210 		mode = drm_mode_duplicate(connector->base.dev, fixed_mode);
211 		if (mode) {
212 			drm_mode_probed_add(&connector->base, mode);
213 			num_modes++;
214 		}
215 	}
216 
217 	return num_modes;
218 }
219 
220 static bool has_drrs_modes(struct intel_connector *connector)
221 {
222 	const struct drm_display_mode *mode1;
223 
224 	list_for_each_entry(mode1, &connector->panel.fixed_modes, head) {
225 		const struct drm_display_mode *mode2 = mode1;
226 
227 		list_for_each_entry_continue(mode2, &connector->panel.fixed_modes, head) {
228 			if (is_alt_drrs_mode(mode1, mode2))
229 				return true;
230 		}
231 	}
232 
233 	return false;
234 }
235 
236 enum drrs_type intel_panel_drrs_type(struct intel_connector *connector)
237 {
238 	return connector->panel.vbt.drrs_type;
239 }
240 
241 static int intel_panel_compute_config_vrr(struct intel_atomic_state *state,
242 					  struct intel_crtc_state *crtc_state,
243 					  struct intel_connector *connector)
244 {
245 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
246 	const struct drm_display_mode *fixed_mode = NULL;
247 	int vrefresh, fixed_mode_vrefresh;
248 
249 	/*
250 	 * Attempt a VRR based refresh rate change if possible
251 	 * when userspace has forbidden a full modeset.
252 	 */
253 	if (!state->base.allow_modeset) {
254 		struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
255 		const struct intel_crtc_state *old_crtc_state =
256 			intel_atomic_get_old_crtc_state(state, crtc);
257 
258 		if (old_crtc_state->hw.enable &&
259 		    old_crtc_state->uapi.encoder_mask == crtc_state->uapi.encoder_mask)
260 			fixed_mode = intel_panel_fixed_mode_vrr(connector, adjusted_mode,
261 								&old_crtc_state->hw.adjusted_mode);
262 	}
263 
264 	if (!fixed_mode)
265 		fixed_mode = intel_panel_fixed_mode_vrr(connector, adjusted_mode, NULL);
266 
267 	if (!fixed_mode)
268 		return -EINVAL;
269 
270 	vrefresh = drm_mode_vrefresh(adjusted_mode);
271 	fixed_mode_vrefresh = drm_mode_vrefresh(fixed_mode);
272 
273 	drm_mode_copy(adjusted_mode, fixed_mode);
274 
275 	if (fixed_mode_vrefresh != vrefresh) {
276 		int vsync_start_offset = adjusted_mode->vtotal - adjusted_mode->vsync_start;
277 		int vsync_end_offset = adjusted_mode->vtotal - adjusted_mode->vsync_end;
278 
279 		adjusted_mode->vtotal =
280 			DIV_ROUND_CLOSEST(adjusted_mode->clock * 1000,
281 					  adjusted_mode->htotal * vrefresh);
282 
283 		adjusted_mode->vsync_start = adjusted_mode->vtotal - vsync_start_offset;
284 		adjusted_mode->vsync_end = adjusted_mode->vtotal - vsync_end_offset;
285 	}
286 
287 	drm_mode_set_crtcinfo(adjusted_mode, 0);
288 
289 	return 0;
290 }
291 
292 static int intel_panel_compute_config_fixed_rr(struct intel_atomic_state *state,
293 					       struct intel_crtc_state *crtc_state,
294 					       struct intel_connector *connector)
295 {
296 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
297 	const struct drm_display_mode *fixed_mode;
298 	int vrefresh, fixed_mode_vrefresh;
299 
300 	fixed_mode = intel_panel_fixed_mode(connector, adjusted_mode);
301 	if (!fixed_mode)
302 		return 0;
303 
304 	vrefresh = drm_mode_vrefresh(adjusted_mode);
305 	fixed_mode_vrefresh = drm_mode_vrefresh(fixed_mode);
306 
307 	/*
308 	 * We don't want to lie too much to the user about the refresh
309 	 * rate they're going to get. But we have to allow a bit of latitude
310 	 * for Xorg since it likes to automagically cook up modes with slightly
311 	 * off refresh rates.
312 	 */
313 	if (abs(vrefresh - fixed_mode_vrefresh) > 1) {
314 		drm_dbg_kms(connector->base.dev,
315 			    "[CONNECTOR:%d:%s] Requested mode vrefresh (%d Hz) does not match fixed mode vrefresh (%d Hz)\n",
316 			    connector->base.base.id, connector->base.name,
317 			    vrefresh, fixed_mode_vrefresh);
318 
319 		return -EINVAL;
320 	}
321 
322 	drm_mode_copy(adjusted_mode, fixed_mode);
323 
324 	drm_mode_set_crtcinfo(adjusted_mode, 0);
325 
326 	return 0;
327 }
328 
329 int intel_panel_compute_config(struct intel_atomic_state *state,
330 			       struct intel_crtc_state *crtc_state,
331 			       struct intel_connector *connector)
332 {
333 	int ret;
334 
335 	ret = intel_panel_compute_config_vrr(state, crtc_state, connector);
336 	if (ret)
337 		ret = intel_panel_compute_config_fixed_rr(state, crtc_state, connector);
338 
339 	return ret;
340 }
341 
342 static void intel_panel_add_edid_alt_fixed_modes(struct intel_connector *connector)
343 {
344 	struct intel_display *display = to_intel_display(connector);
345 	const struct drm_display_mode *preferred_mode =
346 		intel_panel_preferred_fixed_mode(connector);
347 	struct drm_display_mode *mode, *next;
348 
349 	list_for_each_entry_safe(mode, next, &connector->base.probed_modes, head) {
350 		if (!is_alt_fixed_mode(mode, preferred_mode))
351 			continue;
352 
353 		drm_dbg_kms(display->drm,
354 			    "[CONNECTOR:%d:%s] using alternate EDID fixed mode: " DRM_MODE_FMT "\n",
355 			    connector->base.base.id, connector->base.name,
356 			    DRM_MODE_ARG(mode));
357 
358 		list_move_tail(&mode->head, &connector->panel.fixed_modes);
359 	}
360 }
361 
362 static void intel_panel_add_edid_preferred_mode(struct intel_connector *connector)
363 {
364 	struct intel_display *display = to_intel_display(connector);
365 	struct drm_display_mode *scan, *fixed_mode = NULL;
366 
367 	if (list_empty(&connector->base.probed_modes))
368 		return;
369 
370 	/* make sure the preferred mode is first */
371 	list_for_each_entry(scan, &connector->base.probed_modes, head) {
372 		if (scan->type & DRM_MODE_TYPE_PREFERRED) {
373 			fixed_mode = scan;
374 			break;
375 		}
376 	}
377 
378 	if (!fixed_mode)
379 		fixed_mode = list_first_entry(&connector->base.probed_modes,
380 					      typeof(*fixed_mode), head);
381 
382 	drm_dbg_kms(display->drm,
383 		    "[CONNECTOR:%d:%s] using %s EDID fixed mode: " DRM_MODE_FMT "\n",
384 		    connector->base.base.id, connector->base.name,
385 		    fixed_mode->type & DRM_MODE_TYPE_PREFERRED ? "preferred" : "first",
386 		    DRM_MODE_ARG(fixed_mode));
387 
388 	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
389 
390 	list_move_tail(&fixed_mode->head, &connector->panel.fixed_modes);
391 }
392 
393 static void intel_panel_destroy_probed_modes(struct intel_connector *connector)
394 {
395 	struct intel_display *display = to_intel_display(connector);
396 	struct drm_display_mode *mode, *next;
397 
398 	list_for_each_entry_safe(mode, next, &connector->base.probed_modes, head) {
399 		drm_dbg_kms(display->drm,
400 			    "[CONNECTOR:%d:%s] not using EDID mode: " DRM_MODE_FMT "\n",
401 			    connector->base.base.id, connector->base.name,
402 			    DRM_MODE_ARG(mode));
403 		list_del(&mode->head);
404 		drm_mode_destroy(display->drm, mode);
405 	}
406 }
407 
408 void intel_panel_add_edid_fixed_modes(struct intel_connector *connector,
409 				      bool use_alt_fixed_modes)
410 {
411 	intel_panel_add_edid_preferred_mode(connector);
412 	if (intel_panel_preferred_fixed_mode(connector) && use_alt_fixed_modes)
413 		intel_panel_add_edid_alt_fixed_modes(connector);
414 	intel_panel_destroy_probed_modes(connector);
415 }
416 
417 static void intel_panel_add_fixed_mode(struct intel_connector *connector,
418 				       struct drm_display_mode *fixed_mode,
419 				       const char *type)
420 {
421 	struct intel_display *display = to_intel_display(connector);
422 	struct drm_display_info *info = &connector->base.display_info;
423 
424 	if (!fixed_mode)
425 		return;
426 
427 	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
428 
429 	info->width_mm = fixed_mode->width_mm;
430 	info->height_mm = fixed_mode->height_mm;
431 
432 	drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] using %s fixed mode: " DRM_MODE_FMT "\n",
433 		    connector->base.base.id, connector->base.name, type,
434 		    DRM_MODE_ARG(fixed_mode));
435 
436 	list_add_tail(&fixed_mode->head, &connector->panel.fixed_modes);
437 }
438 
439 void intel_panel_add_vbt_lfp_fixed_mode(struct intel_connector *connector)
440 {
441 	struct intel_display *display = to_intel_display(connector);
442 	const struct drm_display_mode *mode;
443 
444 	mode = connector->panel.vbt.lfp_vbt_mode;
445 	if (!mode)
446 		return;
447 
448 	intel_panel_add_fixed_mode(connector,
449 				   drm_mode_duplicate(display->drm, mode),
450 				   "VBT LFP");
451 }
452 
453 void intel_panel_add_vbt_sdvo_fixed_mode(struct intel_connector *connector)
454 {
455 	struct intel_display *display = to_intel_display(connector);
456 	const struct drm_display_mode *mode;
457 
458 	mode = connector->panel.vbt.sdvo_lvds_vbt_mode;
459 	if (!mode)
460 		return;
461 
462 	intel_panel_add_fixed_mode(connector,
463 				   drm_mode_duplicate(display->drm, mode),
464 				   "VBT SDVO");
465 }
466 
467 void intel_panel_add_encoder_fixed_mode(struct intel_connector *connector,
468 					struct intel_encoder *encoder)
469 {
470 	intel_panel_add_fixed_mode(connector,
471 				   intel_encoder_current_mode(encoder),
472 				   "current (BIOS)");
473 }
474 
475 enum drm_connector_status
476 intel_panel_detect(struct drm_connector *connector, bool force)
477 {
478 	struct intel_display *display = to_intel_display(connector->dev);
479 
480 	if (!intel_display_device_enabled(display))
481 		return connector_status_disconnected;
482 
483 	if (!intel_display_driver_check_access(display))
484 		return connector->status;
485 
486 	return connector_status_connected;
487 }
488 
489 enum drm_mode_status
490 intel_panel_mode_valid(struct intel_connector *connector,
491 		       const struct drm_display_mode *mode,
492 		       int *target_clock)
493 {
494 	const struct drm_display_mode *fixed_mode =
495 		intel_panel_fixed_mode(connector, mode);
496 
497 	if (target_clock)
498 		*target_clock = mode->clock;
499 
500 	if (!fixed_mode)
501 		return MODE_OK;
502 
503 	if (mode->hdisplay != fixed_mode->hdisplay)
504 		return MODE_PANEL;
505 
506 	if (mode->vdisplay != fixed_mode->vdisplay)
507 		return MODE_PANEL;
508 
509 	if (drm_mode_vrefresh(mode) != drm_mode_vrefresh(fixed_mode))
510 		return MODE_PANEL;
511 
512 	if (target_clock)
513 		*target_clock = fixed_mode->clock;
514 
515 	return MODE_OK;
516 }
517 
518 void intel_panel_init_alloc(struct intel_connector *connector)
519 {
520 	struct intel_panel *panel = &connector->panel;
521 
522 	connector->panel.vbt.panel_type = -1;
523 	connector->panel.vbt.backlight.controller = -1;
524 	INIT_LIST_HEAD(&panel->fixed_modes);
525 }
526 
527 int intel_panel_init(struct intel_connector *connector,
528 		     const struct drm_edid *fixed_edid)
529 {
530 	struct intel_panel *panel = &connector->panel;
531 
532 	panel->fixed_edid = fixed_edid;
533 
534 	intel_backlight_init_funcs(panel);
535 
536 	if (!has_drrs_modes(connector))
537 		connector->panel.vbt.drrs_type = DRRS_TYPE_NONE;
538 
539 	drm_dbg_kms(connector->base.dev,
540 		    "[CONNECTOR:%d:%s] DRRS type: %s\n",
541 		    connector->base.base.id, connector->base.name,
542 		    intel_drrs_type_str(intel_panel_drrs_type(connector)));
543 
544 	return 0;
545 }
546 
547 void intel_panel_fini(struct intel_connector *connector)
548 {
549 	struct intel_panel *panel = &connector->panel;
550 	struct drm_display_mode *fixed_mode, *next;
551 
552 	if (!IS_ERR_OR_NULL(panel->fixed_edid))
553 		drm_edid_free(panel->fixed_edid);
554 
555 	intel_backlight_destroy(panel);
556 
557 	intel_bios_fini_panel(panel);
558 
559 	list_for_each_entry_safe(fixed_mode, next, &panel->fixed_modes, head) {
560 		list_del(&fixed_mode->head);
561 		drm_mode_destroy(connector->base.dev, fixed_mode);
562 	}
563 }
564 
565 /*
566  * If the panel was already enabled at probe, and we took over the state, the
567  * panel prepared state is out of sync, and the panel followers won't be
568  * notified. We need to call drm_panel_prepare() on enabled panels.
569  *
570  * It would be natural to handle this e.g. in the connector ->sync_state hook at
571  * intel_modeset_readout_hw_state(), but that's unfortunately too early. We
572  * don't have drm_connector::kdev at that time. For now, figure out the state at
573  * ->late_register, and sync there.
574  */
575 static void intel_panel_sync_state(struct intel_connector *connector)
576 {
577 	struct intel_display *display = to_intel_display(connector);
578 	struct drm_connector_state *conn_state;
579 	struct intel_crtc *crtc;
580 	int ret;
581 
582 	ret = drm_modeset_lock(&display->drm->mode_config.connection_mutex, NULL);
583 	if (ret)
584 		return;
585 
586 	conn_state = connector->base.state;
587 
588 	crtc = to_intel_crtc(conn_state->crtc);
589 	if (crtc) {
590 		struct intel_crtc_state *crtc_state;
591 
592 		crtc_state = to_intel_crtc_state(crtc->base.state);
593 
594 		if (crtc_state->hw.active) {
595 			drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] Panel prepare\n",
596 				    connector->base.base.id, connector->base.name);
597 			intel_panel_prepare(crtc_state, conn_state);
598 		}
599 	}
600 
601 	drm_modeset_unlock(&display->drm->mode_config.connection_mutex);
602 }
603 
604 static const struct drm_panel_funcs dummy_panel_funcs = {
605 };
606 
607 int intel_panel_register(struct intel_connector *connector)
608 {
609 	struct intel_display *display = to_intel_display(connector);
610 	struct intel_panel *panel = &connector->panel;
611 	int ret;
612 
613 	ret = intel_backlight_device_register(connector);
614 	if (ret)
615 		return ret;
616 
617 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI ||
618 	    connector->base.connector_type == DRM_MODE_CONNECTOR_eDP) {
619 		struct device *dev = connector->base.kdev;
620 		struct drm_panel *base;
621 
622 		/* Sanity check. */
623 		if (drm_WARN_ON(display->drm, !dev))
624 			goto out;
625 
626 		/*
627 		 * We need drm_connector::kdev for allocating the panel, to make
628 		 * drm_panel_add_follower() lookups work. The kdev is
629 		 * initialized in drm_sysfs_connector_add(), just before the
630 		 * connector .late_register() hooks. So we can't allocate the
631 		 * panel at connector init time, and can't allocate struct
632 		 * intel_panel with a drm_panel sub-struct. For now, use
633 		 * __devm_drm_panel_alloc() directly.
634 		 *
635 		 * The lookups also depend on drm_connector::fwnode being set in
636 		 * intel_acpi_assign_connector_fwnodes(). However, if that's
637 		 * missing, it will gracefully lead to -EPROBE_DEFER in
638 		 * drm_panel_add_follower().
639 		 */
640 		base = __devm_drm_panel_alloc(dev, sizeof(*base), 0,
641 					      &dummy_panel_funcs,
642 					      connector->base.connector_type);
643 		if (IS_ERR(base)) {
644 			ret = PTR_ERR(base);
645 			goto err;
646 		}
647 
648 		panel->base = base;
649 
650 		drm_panel_add(panel->base);
651 
652 		drm_dbg_kms(display->drm, "[CONNECTOR:%d:%s] Registered panel device '%s', has fwnode: %s\n",
653 			    connector->base.base.id, connector->base.name,
654 			    dev_name(dev), str_yes_no(dev_fwnode(dev)));
655 
656 		intel_panel_sync_state(connector);
657 	}
658 
659 out:
660 	return 0;
661 
662 err:
663 	intel_backlight_device_unregister(connector);
664 
665 	return ret;
666 }
667 
668 void intel_panel_unregister(struct intel_connector *connector)
669 {
670 	struct intel_panel *panel = &connector->panel;
671 
672 	if (panel->base)
673 		drm_panel_remove(panel->base);
674 
675 	intel_backlight_device_unregister(connector);
676 }
677 
678 /* Notify followers, if any, about power being up. */
679 void intel_panel_prepare(const struct intel_crtc_state *crtc_state,
680 			 const struct drm_connector_state *conn_state)
681 {
682 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
683 	struct intel_panel *panel = &connector->panel;
684 
685 	drm_panel_prepare(panel->base);
686 }
687 
688 /* Notify followers, if any, about power going down. */
689 void intel_panel_unprepare(const struct drm_connector_state *old_conn_state)
690 {
691 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
692 	struct intel_panel *panel = &connector->panel;
693 
694 	drm_panel_unprepare(panel->base);
695 }
696