xref: /linux/drivers/gpu/drm/drm_probe_helper.c (revision aa8a8c7e4641ebcef02dd6fa387ca334857367f4)
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  *
5  * DRM core CRTC related functions
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that copyright
10  * notice and this permission notice appear in supporting documentation, and
11  * that the name of the copyright holders not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  The copyright holders make no representations
14  * about the suitability of this software for any purpose.  It is provided "as
15  * is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  *
25  * Authors:
26  *      Keith Packard
27  *	Eric Anholt <eric@anholt.net>
28  *      Dave Airlie <airlied@linux.ie>
29  *      Jesse Barnes <jesse.barnes@intel.com>
30  */
31 
32 #include <linux/export.h>
33 #include <linux/moduleparam.h>
34 
35 #include <drm/drm_bridge.h>
36 #include <drm/drm_client_event.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_managed.h>
41 #include <drm/drm_modeset_helper_vtables.h>
42 #include <drm/drm_print.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/drm_sysfs.h>
45 
46 #include "drm_crtc_helper_internal.h"
47 
48 /**
49  * DOC: output probing helper overview
50  *
51  * This library provides some helper code for output probing. It provides an
52  * implementation of the core &drm_connector_funcs.fill_modes interface with
53  * drm_helper_probe_single_connector_modes().
54  *
55  * It also provides support for polling connectors with a work item and for
56  * generic hotplug interrupt handling where the driver doesn't or cannot keep
57  * track of a per-connector hpd interrupt.
58  *
59  * This helper library can be used independently of the modeset helper library.
60  * Drivers can also overwrite different parts e.g. use their own hotplug
61  * handling code to avoid probing unrelated outputs.
62  *
63  * The probe helpers share the function table structures with other display
64  * helper libraries. See &struct drm_connector_helper_funcs for the details.
65  */
66 
67 static bool drm_kms_helper_poll = true;
68 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
69 
70 static enum drm_mode_status
71 drm_mode_validate_flag(const struct drm_display_mode *mode,
72 		       int flags)
73 {
74 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
75 	    !(flags & DRM_MODE_FLAG_INTERLACE))
76 		return MODE_NO_INTERLACE;
77 
78 	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
79 	    !(flags & DRM_MODE_FLAG_DBLSCAN))
80 		return MODE_NO_DBLESCAN;
81 
82 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
83 	    !(flags & DRM_MODE_FLAG_3D_MASK))
84 		return MODE_NO_STEREO;
85 
86 	return MODE_OK;
87 }
88 
89 static int
90 drm_mode_validate_pipeline(struct drm_display_mode *mode,
91 			   struct drm_connector *connector,
92 			   struct drm_modeset_acquire_ctx *ctx,
93 			   enum drm_mode_status *status)
94 {
95 	struct drm_device *dev = connector->dev;
96 	struct drm_encoder *encoder;
97 	int ret;
98 
99 	/* Step 1: Validate against connector */
100 	ret = drm_connector_mode_valid(connector, mode, ctx, status);
101 	if (ret || *status != MODE_OK)
102 		return ret;
103 
104 	/* Step 2: Validate against encoders and crtcs */
105 	drm_connector_for_each_possible_encoder(connector, encoder) {
106 		struct drm_bridge *bridge;
107 		struct drm_crtc *crtc;
108 
109 		*status = drm_encoder_mode_valid(encoder, mode);
110 		if (*status != MODE_OK) {
111 			/* No point in continuing for crtc check as this encoder
112 			 * will not accept the mode anyway. If all encoders
113 			 * reject the mode then, at exit, ret will not be
114 			 * MODE_OK. */
115 			continue;
116 		}
117 
118 		bridge = drm_bridge_chain_get_first_bridge(encoder);
119 		*status = drm_bridge_chain_mode_valid(bridge,
120 						      &connector->display_info,
121 						      mode);
122 		drm_bridge_put(bridge);
123 		if (*status != MODE_OK) {
124 			/* There is also no point in continuing for crtc check
125 			 * here. */
126 			continue;
127 		}
128 
129 		drm_for_each_crtc(crtc, dev) {
130 			if (!drm_encoder_crtc_ok(encoder, crtc))
131 				continue;
132 
133 			*status = drm_crtc_mode_valid(crtc, mode);
134 			if (*status == MODE_OK) {
135 				/* If we get to this point there is at least
136 				 * one combination of encoder+crtc that works
137 				 * for this mode. Lets return now. */
138 				return 0;
139 			}
140 		}
141 	}
142 
143 	return 0;
144 }
145 
146 static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
147 {
148 	struct drm_cmdline_mode *cmdline_mode;
149 	struct drm_display_mode *mode;
150 
151 	cmdline_mode = &connector->cmdline_mode;
152 	if (!cmdline_mode->specified)
153 		return 0;
154 
155 	/* Only add a GTF mode if we find no matching probed modes */
156 	list_for_each_entry(mode, &connector->probed_modes, head) {
157 		if (mode->hdisplay != cmdline_mode->xres ||
158 		    mode->vdisplay != cmdline_mode->yres)
159 			continue;
160 
161 		if (cmdline_mode->refresh_specified) {
162 			/* The probed mode's vrefresh is set until later */
163 			if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
164 				continue;
165 		}
166 
167 		/* Mark the matching mode as being preferred by the user */
168 		mode->type |= DRM_MODE_TYPE_USERDEF;
169 		return 0;
170 	}
171 
172 	mode = drm_mode_create_from_cmdline_mode(connector->dev,
173 						 cmdline_mode);
174 	if (mode == NULL)
175 		return 0;
176 
177 	drm_mode_probed_add(connector, mode);
178 	return 1;
179 }
180 
181 enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
182 					 const struct drm_display_mode *mode)
183 {
184 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
185 
186 	if (!crtc_funcs || !crtc_funcs->mode_valid)
187 		return MODE_OK;
188 
189 	return crtc_funcs->mode_valid(crtc, mode);
190 }
191 
192 enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
193 					    const struct drm_display_mode *mode)
194 {
195 	const struct drm_encoder_helper_funcs *encoder_funcs =
196 		encoder->helper_private;
197 
198 	if (!encoder_funcs || !encoder_funcs->mode_valid)
199 		return MODE_OK;
200 
201 	return encoder_funcs->mode_valid(encoder, mode);
202 }
203 
204 int
205 drm_connector_mode_valid(struct drm_connector *connector,
206 			 const struct drm_display_mode *mode,
207 			 struct drm_modeset_acquire_ctx *ctx,
208 			 enum drm_mode_status *status)
209 {
210 	const struct drm_connector_helper_funcs *connector_funcs =
211 		connector->helper_private;
212 	int ret = 0;
213 
214 	if (!connector_funcs)
215 		*status = MODE_OK;
216 	else if (connector_funcs->mode_valid_ctx)
217 		ret = connector_funcs->mode_valid_ctx(connector, mode, ctx,
218 						      status);
219 	else if (connector_funcs->mode_valid)
220 		*status = connector_funcs->mode_valid(connector, mode);
221 	else
222 		*status = MODE_OK;
223 
224 	return ret;
225 }
226 
227 static void drm_kms_helper_disable_hpd(struct drm_device *dev)
228 {
229 	struct drm_connector *connector;
230 	struct drm_connector_list_iter conn_iter;
231 
232 	drm_connector_list_iter_begin(dev, &conn_iter);
233 	drm_for_each_connector_iter(connector, &conn_iter) {
234 		const struct drm_connector_helper_funcs *funcs =
235 			connector->helper_private;
236 
237 		if (funcs && funcs->disable_hpd)
238 			funcs->disable_hpd(connector);
239 	}
240 	drm_connector_list_iter_end(&conn_iter);
241 }
242 
243 static bool drm_kms_helper_enable_hpd(struct drm_device *dev)
244 {
245 	bool poll = false;
246 	struct drm_connector *connector;
247 	struct drm_connector_list_iter conn_iter;
248 
249 	drm_connector_list_iter_begin(dev, &conn_iter);
250 	drm_for_each_connector_iter(connector, &conn_iter) {
251 		const struct drm_connector_helper_funcs *funcs =
252 			connector->helper_private;
253 
254 		if (funcs && funcs->enable_hpd)
255 			funcs->enable_hpd(connector);
256 
257 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
258 					 DRM_CONNECTOR_POLL_DISCONNECT))
259 			poll = true;
260 	}
261 	drm_connector_list_iter_end(&conn_iter);
262 
263 	return poll;
264 }
265 
266 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
267 static void reschedule_output_poll_work(struct drm_device *dev)
268 {
269 	unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
270 
271 	if (dev->mode_config.delayed_event)
272 		/*
273 		 * FIXME:
274 		 *
275 		 * Use short (1s) delay to handle the initial delayed event.
276 		 * This delay should not be needed, but Optimus/nouveau will
277 		 * fail in a mysterious way if the delayed event is handled as
278 		 * soon as possible like it is done in
279 		 * drm_helper_probe_single_connector_modes() in case the poll
280 		 * was enabled before.
281 		 */
282 		delay = HZ;
283 
284 	schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
285 }
286 
287 /**
288  * drm_kms_helper_poll_enable - re-enable output polling.
289  * @dev: drm_device
290  *
291  * This function re-enables the output polling work, after it has been
292  * temporarily disabled using drm_kms_helper_poll_disable(), for example over
293  * suspend/resume.
294  *
295  * Drivers can call this helper from their device resume implementation. It is
296  * not an error to call this even when output polling isn't enabled.
297  *
298  * If device polling was never initialized before, this call will trigger a
299  * warning and return.
300  *
301  * Note that calls to enable and disable polling must be strictly ordered, which
302  * is automatically the case when they're only call from suspend/resume
303  * callbacks.
304  */
305 void drm_kms_helper_poll_enable(struct drm_device *dev)
306 {
307 	if (drm_WARN_ON_ONCE(dev, !dev->mode_config.poll_enabled) ||
308 	    !drm_kms_helper_poll || dev->mode_config.poll_running)
309 		return;
310 
311 	if (drm_kms_helper_enable_hpd(dev) ||
312 	    dev->mode_config.delayed_event)
313 		reschedule_output_poll_work(dev);
314 
315 	dev->mode_config.poll_running = true;
316 }
317 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
318 
319 /**
320  * drm_kms_helper_poll_reschedule - reschedule the output polling work
321  * @dev: drm_device
322  *
323  * This function reschedules the output polling work, after polling for a
324  * connector has been enabled.
325  *
326  * Drivers must call this helper after enabling polling for a connector by
327  * setting %DRM_CONNECTOR_POLL_CONNECT / %DRM_CONNECTOR_POLL_DISCONNECT flags
328  * in drm_connector::polled. Note that after disabling polling by clearing these
329  * flags for a connector will stop the output polling work automatically if
330  * the polling is disabled for all other connectors as well.
331  *
332  * The function can be called only after polling has been enabled by calling
333  * drm_kms_helper_poll_init() / drm_kms_helper_poll_enable().
334  */
335 void drm_kms_helper_poll_reschedule(struct drm_device *dev)
336 {
337 	if (dev->mode_config.poll_running)
338 		reschedule_output_poll_work(dev);
339 }
340 EXPORT_SYMBOL(drm_kms_helper_poll_reschedule);
341 
342 static int detect_connector_status(struct drm_connector *connector,
343 				   struct drm_modeset_acquire_ctx *ctx,
344 				   bool force)
345 {
346 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
347 
348 	if (funcs->detect_ctx)
349 		return funcs->detect_ctx(connector, ctx, force);
350 	else if (connector->funcs->detect)
351 		return connector->funcs->detect(connector, force);
352 
353 	return connector_status_connected;
354 }
355 
356 static enum drm_connector_status
357 drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
358 {
359 	struct drm_modeset_acquire_ctx ctx;
360 	int ret;
361 
362 	drm_modeset_acquire_init(&ctx, 0);
363 
364 retry:
365 	ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
366 	if (!ret)
367 		ret = detect_connector_status(connector, &ctx, force);
368 
369 	if (ret == -EDEADLK) {
370 		drm_modeset_backoff(&ctx);
371 		goto retry;
372 	}
373 
374 	if (WARN_ON(ret < 0))
375 		ret = connector_status_unknown;
376 
377 	if (ret != connector->status)
378 		connector->epoch_counter += 1;
379 
380 	drm_modeset_drop_locks(&ctx);
381 	drm_modeset_acquire_fini(&ctx);
382 
383 	return ret;
384 }
385 
386 /**
387  * drm_helper_probe_detect - probe connector status
388  * @connector: connector to probe
389  * @ctx: acquire_ctx, or NULL to let this function handle locking.
390  * @force: Whether destructive probe operations should be performed.
391  *
392  * This function calls the detect callbacks of the connector.
393  * This function returns &drm_connector_status, or
394  * if @ctx is set, it might also return -EDEADLK.
395  */
396 int
397 drm_helper_probe_detect(struct drm_connector *connector,
398 			struct drm_modeset_acquire_ctx *ctx,
399 			bool force)
400 {
401 	struct drm_device *dev = connector->dev;
402 	int ret;
403 
404 	if (!ctx)
405 		return drm_helper_probe_detect_ctx(connector, force);
406 
407 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
408 	if (ret)
409 		return ret;
410 
411 	ret = detect_connector_status(connector, ctx, force);
412 
413 	if (ret != connector->status)
414 		connector->epoch_counter += 1;
415 
416 	return ret;
417 }
418 EXPORT_SYMBOL(drm_helper_probe_detect);
419 
420 static int drm_helper_probe_get_modes(struct drm_connector *connector)
421 {
422 	const struct drm_connector_helper_funcs *connector_funcs =
423 		connector->helper_private;
424 	int count;
425 
426 	count = connector_funcs->get_modes(connector);
427 
428 	/* The .get_modes() callback should not return negative values. */
429 	if (count < 0) {
430 		drm_err(connector->dev, ".get_modes() returned %pe\n",
431 			ERR_PTR(count));
432 		count = 0;
433 	}
434 
435 	/*
436 	 * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
437 	 * override/firmware EDID.
438 	 */
439 	if (count == 0 && connector->status == connector_status_connected)
440 		count = drm_edid_override_connector_update(connector);
441 
442 	return count;
443 }
444 
445 static int __drm_helper_update_and_validate(struct drm_connector *connector,
446 					    uint32_t maxX, uint32_t maxY,
447 					    struct drm_modeset_acquire_ctx *ctx)
448 {
449 	struct drm_device *dev = connector->dev;
450 	struct drm_display_mode *mode;
451 	int mode_flags = 0;
452 	int ret;
453 
454 	drm_connector_list_update(connector);
455 
456 	if (connector->interlace_allowed)
457 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
458 	if (connector->doublescan_allowed)
459 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
460 	if (connector->stereo_allowed)
461 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
462 
463 	list_for_each_entry(mode, &connector->modes, head) {
464 		if (mode->status != MODE_OK)
465 			continue;
466 
467 		mode->status = drm_mode_validate_driver(dev, mode);
468 		if (mode->status != MODE_OK)
469 			continue;
470 
471 		mode->status = drm_mode_validate_size(mode, maxX, maxY);
472 		if (mode->status != MODE_OK)
473 			continue;
474 
475 		mode->status = drm_mode_validate_flag(mode, mode_flags);
476 		if (mode->status != MODE_OK)
477 			continue;
478 
479 		mode->status = drm_mode_validate_ycbcr420(mode, connector);
480 		if (mode->status != MODE_OK)
481 			continue;
482 
483 		ret = drm_mode_validate_pipeline(mode, connector, ctx,
484 						 &mode->status);
485 		if (ret) {
486 			drm_dbg_kms(dev,
487 				    "drm_mode_validate_pipeline failed: %d\n",
488 				    ret);
489 
490 			if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK))
491 				mode->status = MODE_ERROR;
492 			else
493 				return -EDEADLK;
494 		}
495 	}
496 
497 	return 0;
498 }
499 
500 /**
501  * drm_helper_probe_single_connector_modes - get complete set of display modes
502  * @connector: connector to probe
503  * @maxX: max width for modes
504  * @maxY: max height for modes
505  *
506  * Based on the helper callbacks implemented by @connector in struct
507  * &drm_connector_helper_funcs try to detect all valid modes.  Modes will first
508  * be added to the connector's probed_modes list, then culled (based on validity
509  * and the @maxX, @maxY parameters) and put into the normal modes list.
510  *
511  * Intended to be used as a generic implementation of the
512  * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
513  * for output mode filtering and detection.
514  *
515  * The basic procedure is as follows
516  *
517  * 1. All modes currently on the connector's modes list are marked as stale
518  *
519  * 2. New modes are added to the connector's probed_modes list with
520  *    drm_mode_probed_add(). New modes start their life with status as OK.
521  *    Modes are added from a single source using the following priority order.
522  *
523  *    - &drm_connector_helper_funcs.get_modes vfunc
524  *    - if the connector status is connector_status_connected, standard
525  *      VESA DMT modes up to 1024x768 are automatically added
526  *      (drm_add_modes_noedid())
527  *
528  *    Finally modes specified via the kernel command line (video=...) are
529  *    added in addition to what the earlier probes produced
530  *    (drm_helper_probe_add_cmdline_mode()). These modes are generated
531  *    using the VESA GTF/CVT formulas.
532  *
533  * 3. Modes are moved from the probed_modes list to the modes list. Potential
534  *    duplicates are merged together (see drm_connector_list_update()).
535  *    After this step the probed_modes list will be empty again.
536  *
537  * 4. Any non-stale mode on the modes list then undergoes validation
538  *
539  *    - drm_mode_validate_basic() performs basic sanity checks
540  *    - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
541  *      (if specified)
542  *    - drm_mode_validate_flag() checks the modes against basic connector
543  *      capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
544  *    - the optional &drm_connector_helper_funcs.mode_valid or
545  *      &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
546  *      and/or sink specific checks
547  *    - the optional &drm_crtc_helper_funcs.mode_valid,
548  *      &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
549  *      helpers can perform driver and/or source specific checks which are also
550  *      enforced by the modeset/atomic helpers
551  *
552  * 5. Any mode whose status is not OK is pruned from the connector's modes list,
553  *    accompanied by a debug message indicating the reason for the mode's
554  *    rejection (see drm_mode_prune_invalid()).
555  *
556  * Returns:
557  * The number of modes found on @connector.
558  */
559 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
560 					    uint32_t maxX, uint32_t maxY)
561 {
562 	const struct drm_connector_helper_funcs *funcs = connector->helper_private;
563 	struct drm_device *dev = connector->dev;
564 	struct drm_display_mode *mode;
565 	int count = 0, ret;
566 	enum drm_connector_status old_status;
567 	struct drm_modeset_acquire_ctx ctx;
568 
569 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
570 
571 	drm_modeset_acquire_init(&ctx, 0);
572 
573 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n", connector->base.id,
574 		    connector->name);
575 
576 retry:
577 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
578 	if (ret == -EDEADLK) {
579 		drm_modeset_backoff(&ctx);
580 		goto retry;
581 	} else
582 		WARN_ON(ret < 0);
583 
584 	/* set all old modes to the stale state */
585 	list_for_each_entry(mode, &connector->modes, head)
586 		mode->status = MODE_STALE;
587 
588 	old_status = connector->status;
589 
590 	if (connector->force) {
591 		if (connector->force == DRM_FORCE_ON ||
592 		    connector->force == DRM_FORCE_ON_DIGITAL)
593 			connector->status = connector_status_connected;
594 		else
595 			connector->status = connector_status_disconnected;
596 
597 		if (funcs && funcs->force_ctx) {
598 			ret = funcs->force_ctx(connector, &ctx);
599 			if (ret == -EDEADLK) {
600 				connector->status = old_status;
601 				drm_modeset_backoff(&ctx);
602 				goto retry;
603 			} else if (ret < 0) {
604 				drm_dbg_kms(dev, "[CONNECTOR:%d:%s] force_ctx failed: %d\n",
605 					    connector->base.id, connector->name, ret);
606 			}
607 		} else if (connector->funcs->force) {
608 			connector->funcs->force(connector);
609 		}
610 	} else {
611 		ret = drm_helper_probe_detect(connector, &ctx, true);
612 
613 		if (ret == -EDEADLK) {
614 			drm_modeset_backoff(&ctx);
615 			goto retry;
616 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
617 			ret = connector_status_unknown;
618 
619 		connector->status = ret;
620 	}
621 
622 	/*
623 	 * Normally either the driver's hpd code or the poll loop should
624 	 * pick up any changes and fire the hotplug event. But if
625 	 * userspace sneaks in a probe, we might miss a change. Hence
626 	 * check here, and if anything changed start the hotplug code.
627 	 */
628 	if (old_status != connector->status) {
629 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
630 			    connector->base.id, connector->name,
631 			    drm_get_connector_status_name(old_status),
632 			    drm_get_connector_status_name(connector->status));
633 
634 		/*
635 		 * The hotplug event code might call into the fb
636 		 * helpers, and so expects that we do not hold any
637 		 * locks. Fire up the poll struct instead, it will
638 		 * disable itself again.
639 		 */
640 		dev->mode_config.delayed_event = true;
641 		if (dev->mode_config.poll_enabled)
642 			mod_delayed_work(system_percpu_wq,
643 					 &dev->mode_config.output_poll_work,
644 					 0);
645 	}
646 
647 	/*
648 	 * Re-enable polling in case the global poll config changed but polling
649 	 * is still initialized.
650 	 */
651 	if (dev->mode_config.poll_enabled)
652 		drm_kms_helper_poll_enable(dev);
653 
654 	if (connector->status == connector_status_disconnected) {
655 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] disconnected\n",
656 			    connector->base.id, connector->name);
657 		drm_connector_update_edid_property(connector, NULL);
658 		drm_mode_prune_invalid(dev, &connector->modes, false);
659 		goto exit;
660 	}
661 
662 	count = drm_helper_probe_get_modes(connector);
663 
664 	if (count == 0 && (connector->status == connector_status_connected ||
665 			   connector->status == connector_status_unknown)) {
666 		count = drm_add_modes_noedid(connector, 1024, 768);
667 
668 		/*
669 		 * Section 4.2.2.6 (EDID Corruption Detection) of the DP 1.4a
670 		 * Link CTS specifies that 640x480 (the official "failsafe"
671 		 * mode) needs to be the default if there's no EDID.
672 		 */
673 		if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
674 			drm_set_preferred_mode(connector, 640, 480);
675 	}
676 	count += drm_helper_probe_add_cmdline_mode(connector);
677 	if (count != 0) {
678 		ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
679 		if (ret == -EDEADLK) {
680 			drm_modeset_backoff(&ctx);
681 			goto retry;
682 		}
683 	}
684 
685 	drm_mode_prune_invalid(dev, &connector->modes, true);
686 
687 	/*
688 	 * Displayport spec section 5.2.1.2 ("Video Timing Format") says that
689 	 * all detachable sinks shall support 640x480 @60Hz as a fail safe
690 	 * mode. If all modes were pruned, perhaps because they need more
691 	 * lanes or a higher pixel clock than available, at least try to add
692 	 * in 640x480.
693 	 */
694 	if (list_empty(&connector->modes) &&
695 	    connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
696 		count = drm_add_modes_noedid(connector, 640, 480);
697 		ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
698 		if (ret == -EDEADLK) {
699 			drm_modeset_backoff(&ctx);
700 			goto retry;
701 		}
702 		drm_mode_prune_invalid(dev, &connector->modes, true);
703 	}
704 
705 exit:
706 	drm_modeset_drop_locks(&ctx);
707 	drm_modeset_acquire_fini(&ctx);
708 
709 	if (list_empty(&connector->modes))
710 		return 0;
711 
712 	drm_mode_sort(&connector->modes);
713 
714 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] probed modes:\n",
715 		    connector->base.id, connector->name);
716 
717 	list_for_each_entry(mode, &connector->modes, head) {
718 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
719 		drm_dbg_kms(dev, "Probed mode: " DRM_MODE_FMT "\n",
720 			    DRM_MODE_ARG(mode));
721 	}
722 
723 	return count;
724 }
725 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
726 
727 /**
728  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
729  * @dev: drm_device whose connector state changed
730  *
731  * This function fires off the uevent for userspace and also calls the
732  * client hotplug function, which is most commonly used to inform the fbdev
733  * emulation code and allow it to update the fbcon output configuration.
734  *
735  * Drivers should call this from their hotplug handling code when a change is
736  * detected. Note that this function does not do any output detection of its
737  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
738  * driver already.
739  *
740  * This function must be called from process context with no mode
741  * setting locks held.
742  *
743  * If only a single connector has changed, consider calling
744  * drm_kms_helper_connector_hotplug_event() instead.
745  */
746 void drm_kms_helper_hotplug_event(struct drm_device *dev)
747 {
748 	drm_sysfs_hotplug_event(dev);
749 	drm_client_dev_hotplug(dev);
750 }
751 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
752 
753 /**
754  * drm_kms_helper_connector_hotplug_event - fire off a KMS connector hotplug event
755  * @connector: drm_connector which has changed
756  *
757  * This is the same as drm_kms_helper_hotplug_event(), except it fires a more
758  * fine-grained uevent for a single connector.
759  */
760 void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector)
761 {
762 	struct drm_device *dev = connector->dev;
763 
764 	drm_sysfs_connector_hotplug_event(connector);
765 	drm_client_dev_hotplug(dev);
766 }
767 EXPORT_SYMBOL(drm_kms_helper_connector_hotplug_event);
768 
769 static void output_poll_execute(struct work_struct *work)
770 {
771 	struct delayed_work *delayed_work = to_delayed_work(work);
772 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
773 	struct drm_connector *connector;
774 	struct drm_connector_list_iter conn_iter;
775 	enum drm_connector_status old_status;
776 	bool repoll = false, changed = false;
777 	u64 old_epoch_counter;
778 
779 	if (!dev->mode_config.poll_enabled)
780 		return;
781 
782 	if (!drm_kms_helper_poll) {
783 		if (dev->mode_config.poll_running) {
784 			drm_kms_helper_disable_hpd(dev);
785 			dev->mode_config.poll_running = false;
786 		}
787 
788 		scoped_guard(mutex, &dev->mode_config.mutex) {
789 			changed = dev->mode_config.delayed_event;
790 			dev->mode_config.delayed_event = false;
791 		}
792 
793 		if (changed)
794 			drm_kms_helper_hotplug_event(dev);
795 
796 		return;
797 	}
798 
799 	if (!mutex_trylock(&dev->mode_config.mutex)) {
800 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
801 		return;
802 	}
803 
804 	drm_connector_list_iter_begin(dev, &conn_iter);
805 	drm_for_each_connector_iter(connector, &conn_iter) {
806 		/* Ignore forced connectors. */
807 		if (connector->force)
808 			continue;
809 
810 		/* Ignore HPD capable connectors and connectors where we don't
811 		 * want any hotplug detection at all for polling. */
812 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
813 			continue;
814 
815 		old_status = connector->status;
816 		/* if we are connected and don't want to poll for disconnect
817 		   skip it */
818 		if (old_status == connector_status_connected &&
819 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
820 			continue;
821 
822 		repoll = true;
823 
824 		old_epoch_counter = connector->epoch_counter;
825 		connector->status = drm_helper_probe_detect(connector, NULL, false);
826 		if (old_epoch_counter != connector->epoch_counter) {
827 			const char *old, *new;
828 
829 			/*
830 			 * The poll work sets force=false when calling detect so
831 			 * that drivers can avoid to do disruptive tests (e.g.
832 			 * when load detect cycles could cause flickering on
833 			 * other, running displays). This bears the risk that we
834 			 * flip-flop between unknown here in the poll work and
835 			 * the real state when userspace forces a full detect
836 			 * call after receiving a hotplug event due to this
837 			 * change.
838 			 *
839 			 * Hence clamp an unknown detect status to the old
840 			 * value.
841 			 */
842 			if (connector->status == connector_status_unknown) {
843 				connector->status = old_status;
844 				continue;
845 			}
846 
847 			old = drm_get_connector_status_name(old_status);
848 			new = drm_get_connector_status_name(connector->status);
849 
850 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
851 				    connector->base.id, connector->name,
852 				    old, new);
853 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
854 				    connector->base.id, connector->name,
855 				    old_epoch_counter, connector->epoch_counter);
856 
857 			drm_sysfs_connector_hotplug_event(connector);
858 			changed = true;
859 		}
860 	}
861 	drm_connector_list_iter_end(&conn_iter);
862 
863 	/* Pick up any changes detected by the probe functions. */
864 	if (dev->mode_config.delayed_event) {
865 		dev->mode_config.delayed_event = false;
866 		changed = true;
867 		drm_sysfs_hotplug_event(dev);
868 	}
869 
870 	mutex_unlock(&dev->mode_config.mutex);
871 
872 	if (changed)
873 		drm_client_dev_hotplug(dev);
874 
875 	if (repoll)
876 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
877 }
878 
879 /**
880  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
881  *
882  * Determine if %current task is an output poll worker.  This can be used
883  * to select distinct code paths for output polling versus other contexts.
884  *
885  * One use case is to avoid a deadlock between the output poll worker and
886  * the autosuspend worker wherein the latter waits for polling to finish
887  * upon calling drm_kms_helper_poll_disable(), while the former waits for
888  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
889  * connector ->detect hook.
890  */
891 bool drm_kms_helper_is_poll_worker(void)
892 {
893 	struct work_struct *work = current_work();
894 
895 	return work && work->func == output_poll_execute;
896 }
897 EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
898 
899 /**
900  * drm_kms_helper_poll_disable - disable output polling
901  * @dev: drm_device
902  *
903  * This function disables the output polling work.
904  *
905  * Drivers can call this helper from their device suspend implementation. It is
906  * not an error to call this even when output polling isn't enabled or already
907  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
908  *
909  * If however, the polling was never initialized, this call will trigger a
910  * warning and return.
911  *
912  * Note that calls to enable and disable polling must be strictly ordered, which
913  * is automatically the case when they're only call from suspend/resume
914  * callbacks.
915  */
916 void drm_kms_helper_poll_disable(struct drm_device *dev)
917 {
918 	if (drm_WARN_ON(dev, !dev->mode_config.poll_enabled))
919 		return;
920 
921 	if (dev->mode_config.poll_running)
922 		drm_kms_helper_disable_hpd(dev);
923 
924 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
925 
926 	dev->mode_config.poll_running = false;
927 }
928 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
929 
930 /**
931  * drm_kms_helper_poll_init - initialize and enable output polling
932  * @dev: drm_device
933  *
934  * This function initializes and then also enables output polling support for
935  * @dev. Drivers which do not have reliable hotplug support in hardware can use
936  * this helper infrastructure to regularly poll such connectors for changes in
937  * their connection state.
938  *
939  * Drivers can control which connectors are polled by setting the
940  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
941  * connectors where probing live outputs can result in visual distortion drivers
942  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
943  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
944  * completely ignored by the polling logic.
945  *
946  * Note that a connector can be both polled and probed from the hotplug handler,
947  * in case the hotplug interrupt is known to be unreliable.
948  */
949 void drm_kms_helper_poll_init(struct drm_device *dev)
950 {
951 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
952 	dev->mode_config.poll_enabled = true;
953 
954 	drm_kms_helper_poll_enable(dev);
955 }
956 EXPORT_SYMBOL(drm_kms_helper_poll_init);
957 
958 /**
959  * drm_kms_helper_poll_fini - disable output polling and clean it up
960  * @dev: drm_device
961  */
962 void drm_kms_helper_poll_fini(struct drm_device *dev)
963 {
964 	if (!dev->mode_config.poll_enabled)
965 		return;
966 
967 	drm_kms_helper_poll_disable(dev);
968 
969 	dev->mode_config.poll_enabled = false;
970 }
971 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
972 
973 static void drm_kms_helper_poll_init_release(struct drm_device *dev, void *res)
974 {
975 	drm_kms_helper_poll_fini(dev);
976 }
977 
978 /**
979  * drmm_kms_helper_poll_init - initialize and enable output polling
980  * @dev: drm_device
981  *
982  * This function initializes and then also enables output polling support for
983  * @dev similar to drm_kms_helper_poll_init(). Polling will automatically be
984  * cleaned up when the DRM device goes away.
985  *
986  * See drm_kms_helper_poll_init() for more information.
987  */
988 void drmm_kms_helper_poll_init(struct drm_device *dev)
989 {
990 	int ret;
991 
992 	drm_kms_helper_poll_init(dev);
993 
994 	ret = drmm_add_action_or_reset(dev, drm_kms_helper_poll_init_release, dev);
995 	if (ret)
996 		drm_warn(dev, "Connector status will not be updated, error %d\n", ret);
997 }
998 EXPORT_SYMBOL(drmm_kms_helper_poll_init);
999 
1000 static bool check_connector_changed(struct drm_connector *connector)
1001 {
1002 	struct drm_device *dev = connector->dev;
1003 	enum drm_connector_status old_status;
1004 	u64 old_epoch_counter;
1005 
1006 	/* Only handle HPD capable connectors. */
1007 	drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
1008 
1009 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
1010 
1011 	old_status = connector->status;
1012 	old_epoch_counter = connector->epoch_counter;
1013 	connector->status = drm_helper_probe_detect(connector, NULL, false);
1014 
1015 	if (old_epoch_counter == connector->epoch_counter) {
1016 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
1017 			    connector->base.id,
1018 			    connector->name,
1019 			    connector->epoch_counter);
1020 
1021 		return false;
1022 	}
1023 
1024 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
1025 		    connector->base.id,
1026 		    connector->name,
1027 		    drm_get_connector_status_name(old_status),
1028 		    drm_get_connector_status_name(connector->status));
1029 
1030 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
1031 		    connector->base.id,
1032 		    connector->name,
1033 		    old_epoch_counter,
1034 		    connector->epoch_counter);
1035 
1036 	return true;
1037 }
1038 
1039 /**
1040  * drm_connector_helper_hpd_irq_event - hotplug processing
1041  * @connector: drm_connector
1042  *
1043  * Drivers can use this helper function to run a detect cycle on a connector
1044  * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
1045  *
1046  * This helper function is useful for drivers which can track hotplug
1047  * interrupts for a single connector. Drivers that want to send a
1048  * hotplug event for all connectors or can't track hotplug interrupts
1049  * per connector need to use drm_helper_hpd_irq_event().
1050  *
1051  * This function must be called from process context with no mode
1052  * setting locks held.
1053  *
1054  * Note that a connector can be both polled and probed from the hotplug
1055  * handler, in case the hotplug interrupt is known to be unreliable.
1056  *
1057  * Returns:
1058  * A boolean indicating whether the connector status changed or not
1059  */
1060 bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
1061 {
1062 	struct drm_device *dev = connector->dev;
1063 	bool changed;
1064 
1065 	mutex_lock(&dev->mode_config.mutex);
1066 	changed = check_connector_changed(connector);
1067 	mutex_unlock(&dev->mode_config.mutex);
1068 
1069 	if (changed) {
1070 		drm_kms_helper_connector_hotplug_event(connector);
1071 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
1072 			    connector->base.id,
1073 			    connector->name);
1074 	}
1075 
1076 	return changed;
1077 }
1078 EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
1079 
1080 /**
1081  * drm_helper_hpd_irq_event - hotplug processing
1082  * @dev: drm_device
1083  *
1084  * Drivers can use this helper function to run a detect cycle on all connectors
1085  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
1086  * other connectors are ignored, which is useful to avoid reprobing fixed
1087  * panels.
1088  *
1089  * This helper function is useful for drivers which can't or don't track hotplug
1090  * interrupts for each connector.
1091  *
1092  * Drivers which support hotplug interrupts for each connector individually and
1093  * which have a more fine-grained detect logic can use
1094  * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
1095  * code and directly call drm_kms_helper_hotplug_event() in case the connector
1096  * state changed.
1097  *
1098  * This function must be called from process context with no mode
1099  * setting locks held.
1100  *
1101  * Note that a connector can be both polled and probed from the hotplug handler,
1102  * in case the hotplug interrupt is known to be unreliable.
1103  *
1104  * Returns:
1105  * A boolean indicating whether the connector status changed or not
1106  */
1107 bool drm_helper_hpd_irq_event(struct drm_device *dev)
1108 {
1109 	struct drm_connector_list_iter conn_iter;
1110 	struct drm_connector *connector;
1111 	bool changed = false;
1112 
1113 	if (!dev->mode_config.poll_enabled)
1114 		return false;
1115 
1116 	mutex_lock(&dev->mode_config.mutex);
1117 	drm_connector_list_iter_begin(dev, &conn_iter);
1118 	drm_for_each_connector_iter(connector, &conn_iter) {
1119 		/* Only handle HPD capable connectors. */
1120 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1121 			continue;
1122 
1123 		if (check_connector_changed(connector)) {
1124 			changed = true;
1125 			drm_sysfs_connector_hotplug_event(connector);
1126 		}
1127 	}
1128 	drm_connector_list_iter_end(&conn_iter);
1129 	mutex_unlock(&dev->mode_config.mutex);
1130 
1131 	if (changed)
1132 		drm_client_dev_hotplug(dev);
1133 
1134 	return changed;
1135 }
1136 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1137 
1138 /**
1139  * drm_crtc_helper_mode_valid_fixed - Validates a display mode
1140  * @crtc: the crtc
1141  * @mode: the mode to validate
1142  * @fixed_mode: the display hardware's mode
1143  *
1144  * Returns:
1145  * MODE_OK on success, or another mode-status code otherwise.
1146  */
1147 enum drm_mode_status drm_crtc_helper_mode_valid_fixed(struct drm_crtc *crtc,
1148 						      const struct drm_display_mode *mode,
1149 						      const struct drm_display_mode *fixed_mode)
1150 {
1151 	if (mode->hdisplay != fixed_mode->hdisplay && mode->vdisplay != fixed_mode->vdisplay)
1152 		return MODE_ONE_SIZE;
1153 	else if (mode->hdisplay != fixed_mode->hdisplay)
1154 		return MODE_ONE_WIDTH;
1155 	else if (mode->vdisplay != fixed_mode->vdisplay)
1156 		return MODE_ONE_HEIGHT;
1157 
1158 	return MODE_OK;
1159 }
1160 EXPORT_SYMBOL(drm_crtc_helper_mode_valid_fixed);
1161 
1162 /**
1163  * drm_connector_helper_get_modes_fixed - Duplicates a display mode for a connector
1164  * @connector: the connector
1165  * @fixed_mode: the display hardware's mode
1166  *
1167  * This function duplicates a display modes for a connector. Drivers for hardware
1168  * that only supports a single fixed mode can use this function in their connector's
1169  * get_modes helper.
1170  *
1171  * Returns:
1172  * The number of created modes.
1173  */
1174 int drm_connector_helper_get_modes_fixed(struct drm_connector *connector,
1175 					 const struct drm_display_mode *fixed_mode)
1176 {
1177 	struct drm_device *dev = connector->dev;
1178 	struct drm_display_mode *mode;
1179 
1180 	mode = drm_mode_duplicate(dev, fixed_mode);
1181 	if (!mode) {
1182 		drm_err(dev, "Failed to duplicate mode " DRM_MODE_FMT "\n",
1183 			DRM_MODE_ARG(fixed_mode));
1184 		return 0;
1185 	}
1186 
1187 	if (mode->name[0] == '\0')
1188 		drm_mode_set_name(mode);
1189 
1190 	mode->type |= DRM_MODE_TYPE_PREFERRED;
1191 	drm_mode_probed_add(connector, mode);
1192 
1193 	if (mode->width_mm)
1194 		connector->display_info.width_mm = mode->width_mm;
1195 	if (mode->height_mm)
1196 		connector->display_info.height_mm = mode->height_mm;
1197 
1198 	return 1;
1199 }
1200 EXPORT_SYMBOL(drm_connector_helper_get_modes_fixed);
1201 
1202 /**
1203  * drm_connector_helper_get_modes - Read EDID and update connector.
1204  * @connector: The connector
1205  *
1206  * Read the EDID using drm_edid_read() (which requires that connector->ddc is
1207  * set), and update the connector using the EDID.
1208  *
1209  * This can be used as the "default" connector helper .get_modes() hook if the
1210  * driver does not need any special processing. This is sets the example what
1211  * custom .get_modes() hooks should do regarding EDID read and connector update.
1212  *
1213  * Returns: Number of modes.
1214  */
1215 int drm_connector_helper_get_modes(struct drm_connector *connector)
1216 {
1217 	const struct drm_edid *drm_edid;
1218 	int count;
1219 
1220 	drm_edid = drm_edid_read(connector);
1221 
1222 	/*
1223 	 * Unconditionally update the connector. If the EDID was read
1224 	 * successfully, fill in the connector information derived from the
1225 	 * EDID. Otherwise, if the EDID is NULL, clear the connector
1226 	 * information.
1227 	 */
1228 	drm_edid_connector_update(connector, drm_edid);
1229 
1230 	count = drm_edid_connector_add_modes(connector);
1231 
1232 	drm_edid_free(drm_edid);
1233 
1234 	return count;
1235 }
1236 EXPORT_SYMBOL(drm_connector_helper_get_modes);
1237 
1238 /**
1239  * drm_connector_helper_tv_get_modes - Fills the modes availables to a TV connector
1240  * @connector: The connector
1241  *
1242  * Fills the available modes for a TV connector based on the supported
1243  * TV modes, and the default mode expressed by the kernel command line.
1244  *
1245  * This can be used as the default TV connector helper .get_modes() hook
1246  * if the driver does not need any special processing.
1247  *
1248  * Returns:
1249  * The number of modes added to the connector.
1250  */
1251 int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
1252 {
1253 	struct drm_device *dev = connector->dev;
1254 	struct drm_property *tv_mode_property =
1255 		dev->mode_config.tv_mode_property;
1256 	struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
1257 	unsigned int ntsc_modes = BIT(DRM_MODE_TV_MODE_NTSC) |
1258 		BIT(DRM_MODE_TV_MODE_NTSC_443) |
1259 		BIT(DRM_MODE_TV_MODE_NTSC_J) |
1260 		BIT(DRM_MODE_TV_MODE_PAL_M);
1261 	unsigned int pal_modes = BIT(DRM_MODE_TV_MODE_PAL) |
1262 		BIT(DRM_MODE_TV_MODE_PAL_N) |
1263 		BIT(DRM_MODE_TV_MODE_SECAM);
1264 	unsigned int tv_modes[2] = { UINT_MAX, UINT_MAX };
1265 	unsigned int i, supported_tv_modes = 0;
1266 
1267 	if (!tv_mode_property)
1268 		return 0;
1269 
1270 	for (i = 0; i < tv_mode_property->num_values; i++)
1271 		supported_tv_modes |= BIT(tv_mode_property->values[i]);
1272 
1273 	if (((supported_tv_modes & ntsc_modes) &&
1274 	     (supported_tv_modes & pal_modes)) ||
1275 	    (supported_tv_modes & BIT(DRM_MODE_TV_MODE_MONOCHROME))) {
1276 		uint64_t default_mode;
1277 
1278 		if (drm_object_property_get_default_value(&connector->base,
1279 							  tv_mode_property,
1280 							  &default_mode))
1281 			return 0;
1282 
1283 		if (cmdline->tv_mode_specified)
1284 			default_mode = cmdline->tv_mode;
1285 
1286 		if (BIT(default_mode) & ntsc_modes) {
1287 			tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
1288 			tv_modes[1] = DRM_MODE_TV_MODE_PAL;
1289 		} else {
1290 			tv_modes[0] = DRM_MODE_TV_MODE_PAL;
1291 			tv_modes[1] = DRM_MODE_TV_MODE_NTSC;
1292 		}
1293 	} else if (supported_tv_modes & ntsc_modes) {
1294 		tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
1295 	} else if (supported_tv_modes & pal_modes) {
1296 		tv_modes[0] = DRM_MODE_TV_MODE_PAL;
1297 	} else {
1298 		return 0;
1299 	}
1300 
1301 	for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
1302 		struct drm_display_mode *mode;
1303 
1304 		if (tv_modes[i] == DRM_MODE_TV_MODE_NTSC)
1305 			mode = drm_mode_analog_ntsc_480i(dev);
1306 		else if (tv_modes[i] == DRM_MODE_TV_MODE_PAL)
1307 			mode = drm_mode_analog_pal_576i(dev);
1308 		else
1309 			break;
1310 		if (!mode)
1311 			return i;
1312 		if (!i)
1313 			mode->type |= DRM_MODE_TYPE_PREFERRED;
1314 		drm_mode_probed_add(connector, mode);
1315 	}
1316 
1317 	return i;
1318 }
1319 EXPORT_SYMBOL(drm_connector_helper_tv_get_modes);
1320 
1321 /**
1322  * drm_connector_helper_detect_from_ddc - Read EDID and detect connector status.
1323  * @connector: The connector
1324  * @ctx: Acquire context
1325  * @force: Perform screen-destructive operations, if necessary
1326  *
1327  * Detects the connector status by reading the EDID using drm_probe_ddc(),
1328  * which requires connector->ddc to be set. Returns connector_status_connected
1329  * on success or connector_status_disconnected on failure.
1330  *
1331  * Returns:
1332  * The connector status as defined by enum drm_connector_status.
1333  */
1334 int drm_connector_helper_detect_from_ddc(struct drm_connector *connector,
1335 					 struct drm_modeset_acquire_ctx *ctx,
1336 					 bool force)
1337 {
1338 	struct i2c_adapter *ddc = connector->ddc;
1339 
1340 	if (!ddc)
1341 		return connector_status_unknown;
1342 
1343 	if (drm_probe_ddc(ddc))
1344 		return connector_status_connected;
1345 
1346 	return connector_status_disconnected;
1347 }
1348 EXPORT_SYMBOL(drm_connector_helper_detect_from_ddc);
1349