xref: /linux/drivers/gpu/drm/drm_probe_helper.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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 	struct drm_device *dev = connector->dev;
563 	struct drm_display_mode *mode;
564 	int count = 0, ret;
565 	enum drm_connector_status old_status;
566 	struct drm_modeset_acquire_ctx ctx;
567 
568 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
569 
570 	drm_modeset_acquire_init(&ctx, 0);
571 
572 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n", connector->base.id,
573 		    connector->name);
574 
575 retry:
576 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
577 	if (ret == -EDEADLK) {
578 		drm_modeset_backoff(&ctx);
579 		goto retry;
580 	} else
581 		WARN_ON(ret < 0);
582 
583 	/* set all old modes to the stale state */
584 	list_for_each_entry(mode, &connector->modes, head)
585 		mode->status = MODE_STALE;
586 
587 	old_status = connector->status;
588 
589 	if (connector->force) {
590 		if (connector->force == DRM_FORCE_ON ||
591 		    connector->force == DRM_FORCE_ON_DIGITAL)
592 			connector->status = connector_status_connected;
593 		else
594 			connector->status = connector_status_disconnected;
595 		if (connector->funcs->force)
596 			connector->funcs->force(connector);
597 	} else {
598 		ret = drm_helper_probe_detect(connector, &ctx, true);
599 
600 		if (ret == -EDEADLK) {
601 			drm_modeset_backoff(&ctx);
602 			goto retry;
603 		} else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
604 			ret = connector_status_unknown;
605 
606 		connector->status = ret;
607 	}
608 
609 	/*
610 	 * Normally either the driver's hpd code or the poll loop should
611 	 * pick up any changes and fire the hotplug event. But if
612 	 * userspace sneaks in a probe, we might miss a change. Hence
613 	 * check here, and if anything changed start the hotplug code.
614 	 */
615 	if (old_status != connector->status) {
616 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
617 			    connector->base.id, connector->name,
618 			    drm_get_connector_status_name(old_status),
619 			    drm_get_connector_status_name(connector->status));
620 
621 		/*
622 		 * The hotplug event code might call into the fb
623 		 * helpers, and so expects that we do not hold any
624 		 * locks. Fire up the poll struct instead, it will
625 		 * disable itself again.
626 		 */
627 		dev->mode_config.delayed_event = true;
628 		if (dev->mode_config.poll_enabled)
629 			mod_delayed_work(system_wq,
630 					 &dev->mode_config.output_poll_work,
631 					 0);
632 	}
633 
634 	/*
635 	 * Re-enable polling in case the global poll config changed but polling
636 	 * is still initialized.
637 	 */
638 	if (dev->mode_config.poll_enabled)
639 		drm_kms_helper_poll_enable(dev);
640 
641 	if (connector->status == connector_status_disconnected) {
642 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] disconnected\n",
643 			    connector->base.id, connector->name);
644 		drm_connector_update_edid_property(connector, NULL);
645 		drm_mode_prune_invalid(dev, &connector->modes, false);
646 		goto exit;
647 	}
648 
649 	count = drm_helper_probe_get_modes(connector);
650 
651 	if (count == 0 && (connector->status == connector_status_connected ||
652 			   connector->status == connector_status_unknown)) {
653 		count = drm_add_modes_noedid(connector, 1024, 768);
654 
655 		/*
656 		 * Section 4.2.2.6 (EDID Corruption Detection) of the DP 1.4a
657 		 * Link CTS specifies that 640x480 (the official "failsafe"
658 		 * mode) needs to be the default if there's no EDID.
659 		 */
660 		if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
661 			drm_set_preferred_mode(connector, 640, 480);
662 	}
663 	count += drm_helper_probe_add_cmdline_mode(connector);
664 	if (count != 0) {
665 		ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
666 		if (ret == -EDEADLK) {
667 			drm_modeset_backoff(&ctx);
668 			goto retry;
669 		}
670 	}
671 
672 	drm_mode_prune_invalid(dev, &connector->modes, true);
673 
674 	/*
675 	 * Displayport spec section 5.2.1.2 ("Video Timing Format") says that
676 	 * all detachable sinks shall support 640x480 @60Hz as a fail safe
677 	 * mode. If all modes were pruned, perhaps because they need more
678 	 * lanes or a higher pixel clock than available, at least try to add
679 	 * in 640x480.
680 	 */
681 	if (list_empty(&connector->modes) &&
682 	    connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
683 		count = drm_add_modes_noedid(connector, 640, 480);
684 		ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
685 		if (ret == -EDEADLK) {
686 			drm_modeset_backoff(&ctx);
687 			goto retry;
688 		}
689 		drm_mode_prune_invalid(dev, &connector->modes, true);
690 	}
691 
692 exit:
693 	drm_modeset_drop_locks(&ctx);
694 	drm_modeset_acquire_fini(&ctx);
695 
696 	if (list_empty(&connector->modes))
697 		return 0;
698 
699 	drm_mode_sort(&connector->modes);
700 
701 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] probed modes:\n",
702 		    connector->base.id, connector->name);
703 
704 	list_for_each_entry(mode, &connector->modes, head) {
705 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
706 		drm_dbg_kms(dev, "Probed mode: " DRM_MODE_FMT "\n",
707 			    DRM_MODE_ARG(mode));
708 	}
709 
710 	return count;
711 }
712 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
713 
714 /**
715  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
716  * @dev: drm_device whose connector state changed
717  *
718  * This function fires off the uevent for userspace and also calls the
719  * client hotplug function, which is most commonly used to inform the fbdev
720  * emulation code and allow it to update the fbcon output configuration.
721  *
722  * Drivers should call this from their hotplug handling code when a change is
723  * detected. Note that this function does not do any output detection of its
724  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
725  * driver already.
726  *
727  * This function must be called from process context with no mode
728  * setting locks held.
729  *
730  * If only a single connector has changed, consider calling
731  * drm_kms_helper_connector_hotplug_event() instead.
732  */
733 void drm_kms_helper_hotplug_event(struct drm_device *dev)
734 {
735 	drm_sysfs_hotplug_event(dev);
736 	drm_client_dev_hotplug(dev);
737 }
738 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
739 
740 /**
741  * drm_kms_helper_connector_hotplug_event - fire off a KMS connector hotplug event
742  * @connector: drm_connector which has changed
743  *
744  * This is the same as drm_kms_helper_hotplug_event(), except it fires a more
745  * fine-grained uevent for a single connector.
746  */
747 void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector)
748 {
749 	struct drm_device *dev = connector->dev;
750 
751 	drm_sysfs_connector_hotplug_event(connector);
752 	drm_client_dev_hotplug(dev);
753 }
754 EXPORT_SYMBOL(drm_kms_helper_connector_hotplug_event);
755 
756 static void output_poll_execute(struct work_struct *work)
757 {
758 	struct delayed_work *delayed_work = to_delayed_work(work);
759 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
760 	struct drm_connector *connector;
761 	struct drm_connector_list_iter conn_iter;
762 	enum drm_connector_status old_status;
763 	bool repoll = false, changed;
764 	u64 old_epoch_counter;
765 
766 	if (!dev->mode_config.poll_enabled)
767 		return;
768 
769 	/* Pick up any changes detected by the probe functions. */
770 	changed = dev->mode_config.delayed_event;
771 	dev->mode_config.delayed_event = false;
772 
773 	if (!drm_kms_helper_poll) {
774 		if (dev->mode_config.poll_running) {
775 			drm_kms_helper_disable_hpd(dev);
776 			dev->mode_config.poll_running = false;
777 		}
778 		goto out;
779 	}
780 
781 	if (!mutex_trylock(&dev->mode_config.mutex)) {
782 		repoll = true;
783 		goto out;
784 	}
785 
786 	drm_connector_list_iter_begin(dev, &conn_iter);
787 	drm_for_each_connector_iter(connector, &conn_iter) {
788 		/* Ignore forced connectors. */
789 		if (connector->force)
790 			continue;
791 
792 		/* Ignore HPD capable connectors and connectors where we don't
793 		 * want any hotplug detection at all for polling. */
794 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
795 			continue;
796 
797 		old_status = connector->status;
798 		/* if we are connected and don't want to poll for disconnect
799 		   skip it */
800 		if (old_status == connector_status_connected &&
801 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
802 			continue;
803 
804 		repoll = true;
805 
806 		old_epoch_counter = connector->epoch_counter;
807 		connector->status = drm_helper_probe_detect(connector, NULL, false);
808 		if (old_epoch_counter != connector->epoch_counter) {
809 			const char *old, *new;
810 
811 			/*
812 			 * The poll work sets force=false when calling detect so
813 			 * that drivers can avoid to do disruptive tests (e.g.
814 			 * when load detect cycles could cause flickering on
815 			 * other, running displays). This bears the risk that we
816 			 * flip-flop between unknown here in the poll work and
817 			 * the real state when userspace forces a full detect
818 			 * call after receiving a hotplug event due to this
819 			 * change.
820 			 *
821 			 * Hence clamp an unknown detect status to the old
822 			 * value.
823 			 */
824 			if (connector->status == connector_status_unknown) {
825 				connector->status = old_status;
826 				continue;
827 			}
828 
829 			old = drm_get_connector_status_name(old_status);
830 			new = drm_get_connector_status_name(connector->status);
831 
832 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
833 				    connector->base.id, connector->name,
834 				    old, new);
835 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
836 				    connector->base.id, connector->name,
837 				    old_epoch_counter, connector->epoch_counter);
838 
839 			changed = true;
840 		}
841 	}
842 	drm_connector_list_iter_end(&conn_iter);
843 
844 	mutex_unlock(&dev->mode_config.mutex);
845 
846 out:
847 	if (changed)
848 		drm_kms_helper_hotplug_event(dev);
849 
850 	if (repoll)
851 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
852 }
853 
854 /**
855  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
856  *
857  * Determine if %current task is an output poll worker.  This can be used
858  * to select distinct code paths for output polling versus other contexts.
859  *
860  * One use case is to avoid a deadlock between the output poll worker and
861  * the autosuspend worker wherein the latter waits for polling to finish
862  * upon calling drm_kms_helper_poll_disable(), while the former waits for
863  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
864  * connector ->detect hook.
865  */
866 bool drm_kms_helper_is_poll_worker(void)
867 {
868 	struct work_struct *work = current_work();
869 
870 	return work && work->func == output_poll_execute;
871 }
872 EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
873 
874 /**
875  * drm_kms_helper_poll_disable - disable output polling
876  * @dev: drm_device
877  *
878  * This function disables the output polling work.
879  *
880  * Drivers can call this helper from their device suspend implementation. It is
881  * not an error to call this even when output polling isn't enabled or already
882  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
883  *
884  * If however, the polling was never initialized, this call will trigger a
885  * warning and return.
886  *
887  * Note that calls to enable and disable polling must be strictly ordered, which
888  * is automatically the case when they're only call from suspend/resume
889  * callbacks.
890  */
891 void drm_kms_helper_poll_disable(struct drm_device *dev)
892 {
893 	if (drm_WARN_ON(dev, !dev->mode_config.poll_enabled))
894 		return;
895 
896 	if (dev->mode_config.poll_running)
897 		drm_kms_helper_disable_hpd(dev);
898 
899 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
900 
901 	dev->mode_config.poll_running = false;
902 }
903 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
904 
905 /**
906  * drm_kms_helper_poll_init - initialize and enable output polling
907  * @dev: drm_device
908  *
909  * This function initializes and then also enables output polling support for
910  * @dev. Drivers which do not have reliable hotplug support in hardware can use
911  * this helper infrastructure to regularly poll such connectors for changes in
912  * their connection state.
913  *
914  * Drivers can control which connectors are polled by setting the
915  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
916  * connectors where probing live outputs can result in visual distortion drivers
917  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
918  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
919  * completely ignored by the polling logic.
920  *
921  * Note that a connector can be both polled and probed from the hotplug handler,
922  * in case the hotplug interrupt is known to be unreliable.
923  */
924 void drm_kms_helper_poll_init(struct drm_device *dev)
925 {
926 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
927 	dev->mode_config.poll_enabled = true;
928 
929 	drm_kms_helper_poll_enable(dev);
930 }
931 EXPORT_SYMBOL(drm_kms_helper_poll_init);
932 
933 /**
934  * drm_kms_helper_poll_fini - disable output polling and clean it up
935  * @dev: drm_device
936  */
937 void drm_kms_helper_poll_fini(struct drm_device *dev)
938 {
939 	if (!dev->mode_config.poll_enabled)
940 		return;
941 
942 	drm_kms_helper_poll_disable(dev);
943 
944 	dev->mode_config.poll_enabled = false;
945 }
946 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
947 
948 static void drm_kms_helper_poll_init_release(struct drm_device *dev, void *res)
949 {
950 	drm_kms_helper_poll_fini(dev);
951 }
952 
953 /**
954  * drmm_kms_helper_poll_init - initialize and enable output polling
955  * @dev: drm_device
956  *
957  * This function initializes and then also enables output polling support for
958  * @dev similar to drm_kms_helper_poll_init(). Polling will automatically be
959  * cleaned up when the DRM device goes away.
960  *
961  * See drm_kms_helper_poll_init() for more information.
962  */
963 void drmm_kms_helper_poll_init(struct drm_device *dev)
964 {
965 	int ret;
966 
967 	drm_kms_helper_poll_init(dev);
968 
969 	ret = drmm_add_action_or_reset(dev, drm_kms_helper_poll_init_release, dev);
970 	if (ret)
971 		drm_warn(dev, "Connector status will not be updated, error %d\n", ret);
972 }
973 EXPORT_SYMBOL(drmm_kms_helper_poll_init);
974 
975 static bool check_connector_changed(struct drm_connector *connector)
976 {
977 	struct drm_device *dev = connector->dev;
978 	enum drm_connector_status old_status;
979 	u64 old_epoch_counter;
980 
981 	/* Only handle HPD capable connectors. */
982 	drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
983 
984 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
985 
986 	old_status = connector->status;
987 	old_epoch_counter = connector->epoch_counter;
988 	connector->status = drm_helper_probe_detect(connector, NULL, false);
989 
990 	if (old_epoch_counter == connector->epoch_counter) {
991 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
992 			    connector->base.id,
993 			    connector->name,
994 			    connector->epoch_counter);
995 
996 		return false;
997 	}
998 
999 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
1000 		    connector->base.id,
1001 		    connector->name,
1002 		    drm_get_connector_status_name(old_status),
1003 		    drm_get_connector_status_name(connector->status));
1004 
1005 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
1006 		    connector->base.id,
1007 		    connector->name,
1008 		    old_epoch_counter,
1009 		    connector->epoch_counter);
1010 
1011 	return true;
1012 }
1013 
1014 /**
1015  * drm_connector_helper_hpd_irq_event - hotplug processing
1016  * @connector: drm_connector
1017  *
1018  * Drivers can use this helper function to run a detect cycle on a connector
1019  * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
1020  *
1021  * This helper function is useful for drivers which can track hotplug
1022  * interrupts for a single connector. Drivers that want to send a
1023  * hotplug event for all connectors or can't track hotplug interrupts
1024  * per connector need to use drm_helper_hpd_irq_event().
1025  *
1026  * This function must be called from process context with no mode
1027  * setting locks held.
1028  *
1029  * Note that a connector can be both polled and probed from the hotplug
1030  * handler, in case the hotplug interrupt is known to be unreliable.
1031  *
1032  * Returns:
1033  * A boolean indicating whether the connector status changed or not
1034  */
1035 bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
1036 {
1037 	struct drm_device *dev = connector->dev;
1038 	bool changed;
1039 
1040 	mutex_lock(&dev->mode_config.mutex);
1041 	changed = check_connector_changed(connector);
1042 	mutex_unlock(&dev->mode_config.mutex);
1043 
1044 	if (changed) {
1045 		drm_kms_helper_connector_hotplug_event(connector);
1046 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
1047 			    connector->base.id,
1048 			    connector->name);
1049 	}
1050 
1051 	return changed;
1052 }
1053 EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
1054 
1055 /**
1056  * drm_helper_hpd_irq_event - hotplug processing
1057  * @dev: drm_device
1058  *
1059  * Drivers can use this helper function to run a detect cycle on all connectors
1060  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
1061  * other connectors are ignored, which is useful to avoid reprobing fixed
1062  * panels.
1063  *
1064  * This helper function is useful for drivers which can't or don't track hotplug
1065  * interrupts for each connector.
1066  *
1067  * Drivers which support hotplug interrupts for each connector individually and
1068  * which have a more fine-grained detect logic can use
1069  * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
1070  * code and directly call drm_kms_helper_hotplug_event() in case the connector
1071  * state changed.
1072  *
1073  * This function must be called from process context with no mode
1074  * setting locks held.
1075  *
1076  * Note that a connector can be both polled and probed from the hotplug handler,
1077  * in case the hotplug interrupt is known to be unreliable.
1078  *
1079  * Returns:
1080  * A boolean indicating whether the connector status changed or not
1081  */
1082 bool drm_helper_hpd_irq_event(struct drm_device *dev)
1083 {
1084 	struct drm_connector *connector, *first_changed_connector = NULL;
1085 	struct drm_connector_list_iter conn_iter;
1086 	int changed = 0;
1087 
1088 	if (!dev->mode_config.poll_enabled)
1089 		return false;
1090 
1091 	mutex_lock(&dev->mode_config.mutex);
1092 	drm_connector_list_iter_begin(dev, &conn_iter);
1093 	drm_for_each_connector_iter(connector, &conn_iter) {
1094 		/* Only handle HPD capable connectors. */
1095 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1096 			continue;
1097 
1098 		if (check_connector_changed(connector)) {
1099 			if (!first_changed_connector) {
1100 				drm_connector_get(connector);
1101 				first_changed_connector = connector;
1102 			}
1103 
1104 			changed++;
1105 		}
1106 	}
1107 	drm_connector_list_iter_end(&conn_iter);
1108 	mutex_unlock(&dev->mode_config.mutex);
1109 
1110 	if (changed == 1)
1111 		drm_kms_helper_connector_hotplug_event(first_changed_connector);
1112 	else if (changed > 0)
1113 		drm_kms_helper_hotplug_event(dev);
1114 
1115 	if (first_changed_connector)
1116 		drm_connector_put(first_changed_connector);
1117 
1118 	return changed;
1119 }
1120 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1121 
1122 /**
1123  * drm_crtc_helper_mode_valid_fixed - Validates a display mode
1124  * @crtc: the crtc
1125  * @mode: the mode to validate
1126  * @fixed_mode: the display hardware's mode
1127  *
1128  * Returns:
1129  * MODE_OK on success, or another mode-status code otherwise.
1130  */
1131 enum drm_mode_status drm_crtc_helper_mode_valid_fixed(struct drm_crtc *crtc,
1132 						      const struct drm_display_mode *mode,
1133 						      const struct drm_display_mode *fixed_mode)
1134 {
1135 	if (mode->hdisplay != fixed_mode->hdisplay && mode->vdisplay != fixed_mode->vdisplay)
1136 		return MODE_ONE_SIZE;
1137 	else if (mode->hdisplay != fixed_mode->hdisplay)
1138 		return MODE_ONE_WIDTH;
1139 	else if (mode->vdisplay != fixed_mode->vdisplay)
1140 		return MODE_ONE_HEIGHT;
1141 
1142 	return MODE_OK;
1143 }
1144 EXPORT_SYMBOL(drm_crtc_helper_mode_valid_fixed);
1145 
1146 /**
1147  * drm_connector_helper_get_modes_fixed - Duplicates a display mode for a connector
1148  * @connector: the connector
1149  * @fixed_mode: the display hardware's mode
1150  *
1151  * This function duplicates a display modes for a connector. Drivers for hardware
1152  * that only supports a single fixed mode can use this function in their connector's
1153  * get_modes helper.
1154  *
1155  * Returns:
1156  * The number of created modes.
1157  */
1158 int drm_connector_helper_get_modes_fixed(struct drm_connector *connector,
1159 					 const struct drm_display_mode *fixed_mode)
1160 {
1161 	struct drm_device *dev = connector->dev;
1162 	struct drm_display_mode *mode;
1163 
1164 	mode = drm_mode_duplicate(dev, fixed_mode);
1165 	if (!mode) {
1166 		drm_err(dev, "Failed to duplicate mode " DRM_MODE_FMT "\n",
1167 			DRM_MODE_ARG(fixed_mode));
1168 		return 0;
1169 	}
1170 
1171 	if (mode->name[0] == '\0')
1172 		drm_mode_set_name(mode);
1173 
1174 	mode->type |= DRM_MODE_TYPE_PREFERRED;
1175 	drm_mode_probed_add(connector, mode);
1176 
1177 	if (mode->width_mm)
1178 		connector->display_info.width_mm = mode->width_mm;
1179 	if (mode->height_mm)
1180 		connector->display_info.height_mm = mode->height_mm;
1181 
1182 	return 1;
1183 }
1184 EXPORT_SYMBOL(drm_connector_helper_get_modes_fixed);
1185 
1186 /**
1187  * drm_connector_helper_get_modes - Read EDID and update connector.
1188  * @connector: The connector
1189  *
1190  * Read the EDID using drm_edid_read() (which requires that connector->ddc is
1191  * set), and update the connector using the EDID.
1192  *
1193  * This can be used as the "default" connector helper .get_modes() hook if the
1194  * driver does not need any special processing. This is sets the example what
1195  * custom .get_modes() hooks should do regarding EDID read and connector update.
1196  *
1197  * Returns: Number of modes.
1198  */
1199 int drm_connector_helper_get_modes(struct drm_connector *connector)
1200 {
1201 	const struct drm_edid *drm_edid;
1202 	int count;
1203 
1204 	drm_edid = drm_edid_read(connector);
1205 
1206 	/*
1207 	 * Unconditionally update the connector. If the EDID was read
1208 	 * successfully, fill in the connector information derived from the
1209 	 * EDID. Otherwise, if the EDID is NULL, clear the connector
1210 	 * information.
1211 	 */
1212 	drm_edid_connector_update(connector, drm_edid);
1213 
1214 	count = drm_edid_connector_add_modes(connector);
1215 
1216 	drm_edid_free(drm_edid);
1217 
1218 	return count;
1219 }
1220 EXPORT_SYMBOL(drm_connector_helper_get_modes);
1221 
1222 /**
1223  * drm_connector_helper_tv_get_modes - Fills the modes availables to a TV connector
1224  * @connector: The connector
1225  *
1226  * Fills the available modes for a TV connector based on the supported
1227  * TV modes, and the default mode expressed by the kernel command line.
1228  *
1229  * This can be used as the default TV connector helper .get_modes() hook
1230  * if the driver does not need any special processing.
1231  *
1232  * Returns:
1233  * The number of modes added to the connector.
1234  */
1235 int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
1236 {
1237 	struct drm_device *dev = connector->dev;
1238 	struct drm_property *tv_mode_property =
1239 		dev->mode_config.tv_mode_property;
1240 	struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
1241 	unsigned int ntsc_modes = BIT(DRM_MODE_TV_MODE_NTSC) |
1242 		BIT(DRM_MODE_TV_MODE_NTSC_443) |
1243 		BIT(DRM_MODE_TV_MODE_NTSC_J) |
1244 		BIT(DRM_MODE_TV_MODE_PAL_M);
1245 	unsigned int pal_modes = BIT(DRM_MODE_TV_MODE_PAL) |
1246 		BIT(DRM_MODE_TV_MODE_PAL_N) |
1247 		BIT(DRM_MODE_TV_MODE_SECAM);
1248 	unsigned int tv_modes[2] = { UINT_MAX, UINT_MAX };
1249 	unsigned int i, supported_tv_modes = 0;
1250 
1251 	if (!tv_mode_property)
1252 		return 0;
1253 
1254 	for (i = 0; i < tv_mode_property->num_values; i++)
1255 		supported_tv_modes |= BIT(tv_mode_property->values[i]);
1256 
1257 	if (((supported_tv_modes & ntsc_modes) &&
1258 	     (supported_tv_modes & pal_modes)) ||
1259 	    (supported_tv_modes & BIT(DRM_MODE_TV_MODE_MONOCHROME))) {
1260 		uint64_t default_mode;
1261 
1262 		if (drm_object_property_get_default_value(&connector->base,
1263 							  tv_mode_property,
1264 							  &default_mode))
1265 			return 0;
1266 
1267 		if (cmdline->tv_mode_specified)
1268 			default_mode = cmdline->tv_mode;
1269 
1270 		if (BIT(default_mode) & ntsc_modes) {
1271 			tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
1272 			tv_modes[1] = DRM_MODE_TV_MODE_PAL;
1273 		} else {
1274 			tv_modes[0] = DRM_MODE_TV_MODE_PAL;
1275 			tv_modes[1] = DRM_MODE_TV_MODE_NTSC;
1276 		}
1277 	} else if (supported_tv_modes & ntsc_modes) {
1278 		tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
1279 	} else if (supported_tv_modes & pal_modes) {
1280 		tv_modes[0] = DRM_MODE_TV_MODE_PAL;
1281 	} else {
1282 		return 0;
1283 	}
1284 
1285 	for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
1286 		struct drm_display_mode *mode;
1287 
1288 		if (tv_modes[i] == DRM_MODE_TV_MODE_NTSC)
1289 			mode = drm_mode_analog_ntsc_480i(dev);
1290 		else if (tv_modes[i] == DRM_MODE_TV_MODE_PAL)
1291 			mode = drm_mode_analog_pal_576i(dev);
1292 		else
1293 			break;
1294 		if (!mode)
1295 			return i;
1296 		if (!i)
1297 			mode->type |= DRM_MODE_TYPE_PREFERRED;
1298 		drm_mode_probed_add(connector, mode);
1299 	}
1300 
1301 	return i;
1302 }
1303 EXPORT_SYMBOL(drm_connector_helper_tv_get_modes);
1304 
1305 /**
1306  * drm_connector_helper_detect_from_ddc - Read EDID and detect connector status.
1307  * @connector: The connector
1308  * @ctx: Acquire context
1309  * @force: Perform screen-destructive operations, if necessary
1310  *
1311  * Detects the connector status by reading the EDID using drm_probe_ddc(),
1312  * which requires connector->ddc to be set. Returns connector_status_connected
1313  * on success or connector_status_disconnected on failure.
1314  *
1315  * Returns:
1316  * The connector status as defined by enum drm_connector_status.
1317  */
1318 int drm_connector_helper_detect_from_ddc(struct drm_connector *connector,
1319 					 struct drm_modeset_acquire_ctx *ctx,
1320 					 bool force)
1321 {
1322 	struct i2c_adapter *ddc = connector->ddc;
1323 
1324 	if (!ddc)
1325 		return connector_status_unknown;
1326 
1327 	if (drm_probe_ddc(ddc))
1328 		return connector_status_connected;
1329 
1330 	return connector_status_disconnected;
1331 }
1332 EXPORT_SYMBOL(drm_connector_helper_detect_from_ddc);
1333