xref: /linux/drivers/gpu/drm/drm_probe_helper.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
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_percpu_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 = false;
764 	u64 old_epoch_counter;
765 
766 	if (!dev->mode_config.poll_enabled)
767 		return;
768 
769 	if (!drm_kms_helper_poll) {
770 		if (dev->mode_config.poll_running) {
771 			drm_kms_helper_disable_hpd(dev);
772 			dev->mode_config.poll_running = false;
773 		}
774 
775 		scoped_guard(mutex, &dev->mode_config.mutex) {
776 			changed = dev->mode_config.delayed_event;
777 			dev->mode_config.delayed_event = false;
778 		}
779 
780 		if (changed)
781 			drm_kms_helper_hotplug_event(dev);
782 
783 		return;
784 	}
785 
786 	if (!mutex_trylock(&dev->mode_config.mutex)) {
787 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
788 		return;
789 	}
790 
791 	drm_connector_list_iter_begin(dev, &conn_iter);
792 	drm_for_each_connector_iter(connector, &conn_iter) {
793 		/* Ignore forced connectors. */
794 		if (connector->force)
795 			continue;
796 
797 		/* Ignore HPD capable connectors and connectors where we don't
798 		 * want any hotplug detection at all for polling. */
799 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
800 			continue;
801 
802 		old_status = connector->status;
803 		/* if we are connected and don't want to poll for disconnect
804 		   skip it */
805 		if (old_status == connector_status_connected &&
806 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
807 			continue;
808 
809 		repoll = true;
810 
811 		old_epoch_counter = connector->epoch_counter;
812 		connector->status = drm_helper_probe_detect(connector, NULL, false);
813 		if (old_epoch_counter != connector->epoch_counter) {
814 			const char *old, *new;
815 
816 			/*
817 			 * The poll work sets force=false when calling detect so
818 			 * that drivers can avoid to do disruptive tests (e.g.
819 			 * when load detect cycles could cause flickering on
820 			 * other, running displays). This bears the risk that we
821 			 * flip-flop between unknown here in the poll work and
822 			 * the real state when userspace forces a full detect
823 			 * call after receiving a hotplug event due to this
824 			 * change.
825 			 *
826 			 * Hence clamp an unknown detect status to the old
827 			 * value.
828 			 */
829 			if (connector->status == connector_status_unknown) {
830 				connector->status = old_status;
831 				continue;
832 			}
833 
834 			old = drm_get_connector_status_name(old_status);
835 			new = drm_get_connector_status_name(connector->status);
836 
837 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
838 				    connector->base.id, connector->name,
839 				    old, new);
840 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
841 				    connector->base.id, connector->name,
842 				    old_epoch_counter, connector->epoch_counter);
843 
844 			drm_sysfs_connector_hotplug_event(connector);
845 			changed = true;
846 		}
847 	}
848 	drm_connector_list_iter_end(&conn_iter);
849 
850 	/* Pick up any changes detected by the probe functions. */
851 	if (dev->mode_config.delayed_event) {
852 		dev->mode_config.delayed_event = false;
853 		changed = true;
854 		drm_sysfs_hotplug_event(dev);
855 	}
856 
857 	mutex_unlock(&dev->mode_config.mutex);
858 
859 	if (changed)
860 		drm_client_dev_hotplug(dev);
861 
862 	if (repoll)
863 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
864 }
865 
866 /**
867  * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
868  *
869  * Determine if %current task is an output poll worker.  This can be used
870  * to select distinct code paths for output polling versus other contexts.
871  *
872  * One use case is to avoid a deadlock between the output poll worker and
873  * the autosuspend worker wherein the latter waits for polling to finish
874  * upon calling drm_kms_helper_poll_disable(), while the former waits for
875  * runtime suspend to finish upon calling pm_runtime_get_sync() in a
876  * connector ->detect hook.
877  */
878 bool drm_kms_helper_is_poll_worker(void)
879 {
880 	struct work_struct *work = current_work();
881 
882 	return work && work->func == output_poll_execute;
883 }
884 EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
885 
886 /**
887  * drm_kms_helper_poll_disable - disable output polling
888  * @dev: drm_device
889  *
890  * This function disables the output polling work.
891  *
892  * Drivers can call this helper from their device suspend implementation. It is
893  * not an error to call this even when output polling isn't enabled or already
894  * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
895  *
896  * If however, the polling was never initialized, this call will trigger a
897  * warning and return.
898  *
899  * Note that calls to enable and disable polling must be strictly ordered, which
900  * is automatically the case when they're only call from suspend/resume
901  * callbacks.
902  */
903 void drm_kms_helper_poll_disable(struct drm_device *dev)
904 {
905 	if (drm_WARN_ON(dev, !dev->mode_config.poll_enabled))
906 		return;
907 
908 	if (dev->mode_config.poll_running)
909 		drm_kms_helper_disable_hpd(dev);
910 
911 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
912 
913 	dev->mode_config.poll_running = false;
914 }
915 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
916 
917 /**
918  * drm_kms_helper_poll_init - initialize and enable output polling
919  * @dev: drm_device
920  *
921  * This function initializes and then also enables output polling support for
922  * @dev. Drivers which do not have reliable hotplug support in hardware can use
923  * this helper infrastructure to regularly poll such connectors for changes in
924  * their connection state.
925  *
926  * Drivers can control which connectors are polled by setting the
927  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
928  * connectors where probing live outputs can result in visual distortion drivers
929  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
930  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
931  * completely ignored by the polling logic.
932  *
933  * Note that a connector can be both polled and probed from the hotplug handler,
934  * in case the hotplug interrupt is known to be unreliable.
935  */
936 void drm_kms_helper_poll_init(struct drm_device *dev)
937 {
938 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
939 	dev->mode_config.poll_enabled = true;
940 
941 	drm_kms_helper_poll_enable(dev);
942 }
943 EXPORT_SYMBOL(drm_kms_helper_poll_init);
944 
945 /**
946  * drm_kms_helper_poll_fini - disable output polling and clean it up
947  * @dev: drm_device
948  */
949 void drm_kms_helper_poll_fini(struct drm_device *dev)
950 {
951 	if (!dev->mode_config.poll_enabled)
952 		return;
953 
954 	drm_kms_helper_poll_disable(dev);
955 
956 	dev->mode_config.poll_enabled = false;
957 }
958 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
959 
960 static void drm_kms_helper_poll_init_release(struct drm_device *dev, void *res)
961 {
962 	drm_kms_helper_poll_fini(dev);
963 }
964 
965 /**
966  * drmm_kms_helper_poll_init - initialize and enable output polling
967  * @dev: drm_device
968  *
969  * This function initializes and then also enables output polling support for
970  * @dev similar to drm_kms_helper_poll_init(). Polling will automatically be
971  * cleaned up when the DRM device goes away.
972  *
973  * See drm_kms_helper_poll_init() for more information.
974  */
975 void drmm_kms_helper_poll_init(struct drm_device *dev)
976 {
977 	int ret;
978 
979 	drm_kms_helper_poll_init(dev);
980 
981 	ret = drmm_add_action_or_reset(dev, drm_kms_helper_poll_init_release, dev);
982 	if (ret)
983 		drm_warn(dev, "Connector status will not be updated, error %d\n", ret);
984 }
985 EXPORT_SYMBOL(drmm_kms_helper_poll_init);
986 
987 static bool check_connector_changed(struct drm_connector *connector)
988 {
989 	struct drm_device *dev = connector->dev;
990 	enum drm_connector_status old_status;
991 	u64 old_epoch_counter;
992 
993 	/* Only handle HPD capable connectors. */
994 	drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
995 
996 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
997 
998 	old_status = connector->status;
999 	old_epoch_counter = connector->epoch_counter;
1000 	connector->status = drm_helper_probe_detect(connector, NULL, false);
1001 
1002 	if (old_epoch_counter == connector->epoch_counter) {
1003 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
1004 			    connector->base.id,
1005 			    connector->name,
1006 			    connector->epoch_counter);
1007 
1008 		return false;
1009 	}
1010 
1011 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
1012 		    connector->base.id,
1013 		    connector->name,
1014 		    drm_get_connector_status_name(old_status),
1015 		    drm_get_connector_status_name(connector->status));
1016 
1017 	drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
1018 		    connector->base.id,
1019 		    connector->name,
1020 		    old_epoch_counter,
1021 		    connector->epoch_counter);
1022 
1023 	return true;
1024 }
1025 
1026 /**
1027  * drm_connector_helper_hpd_irq_event - hotplug processing
1028  * @connector: drm_connector
1029  *
1030  * Drivers can use this helper function to run a detect cycle on a connector
1031  * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
1032  *
1033  * This helper function is useful for drivers which can track hotplug
1034  * interrupts for a single connector. Drivers that want to send a
1035  * hotplug event for all connectors or can't track hotplug interrupts
1036  * per connector need to use drm_helper_hpd_irq_event().
1037  *
1038  * This function must be called from process context with no mode
1039  * setting locks held.
1040  *
1041  * Note that a connector can be both polled and probed from the hotplug
1042  * handler, in case the hotplug interrupt is known to be unreliable.
1043  *
1044  * Returns:
1045  * A boolean indicating whether the connector status changed or not
1046  */
1047 bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
1048 {
1049 	struct drm_device *dev = connector->dev;
1050 	bool changed;
1051 
1052 	mutex_lock(&dev->mode_config.mutex);
1053 	changed = check_connector_changed(connector);
1054 	mutex_unlock(&dev->mode_config.mutex);
1055 
1056 	if (changed) {
1057 		drm_kms_helper_connector_hotplug_event(connector);
1058 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
1059 			    connector->base.id,
1060 			    connector->name);
1061 	}
1062 
1063 	return changed;
1064 }
1065 EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
1066 
1067 /**
1068  * drm_helper_hpd_irq_event - hotplug processing
1069  * @dev: drm_device
1070  *
1071  * Drivers can use this helper function to run a detect cycle on all connectors
1072  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
1073  * other connectors are ignored, which is useful to avoid reprobing fixed
1074  * panels.
1075  *
1076  * This helper function is useful for drivers which can't or don't track hotplug
1077  * interrupts for each connector.
1078  *
1079  * Drivers which support hotplug interrupts for each connector individually and
1080  * which have a more fine-grained detect logic can use
1081  * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
1082  * code and directly call drm_kms_helper_hotplug_event() in case the connector
1083  * state changed.
1084  *
1085  * This function must be called from process context with no mode
1086  * setting locks held.
1087  *
1088  * Note that a connector can be both polled and probed from the hotplug handler,
1089  * in case the hotplug interrupt is known to be unreliable.
1090  *
1091  * Returns:
1092  * A boolean indicating whether the connector status changed or not
1093  */
1094 bool drm_helper_hpd_irq_event(struct drm_device *dev)
1095 {
1096 	struct drm_connector_list_iter conn_iter;
1097 	struct drm_connector *connector;
1098 	bool changed = false;
1099 
1100 	if (!dev->mode_config.poll_enabled)
1101 		return false;
1102 
1103 	mutex_lock(&dev->mode_config.mutex);
1104 	drm_connector_list_iter_begin(dev, &conn_iter);
1105 	drm_for_each_connector_iter(connector, &conn_iter) {
1106 		/* Only handle HPD capable connectors. */
1107 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1108 			continue;
1109 
1110 		if (check_connector_changed(connector)) {
1111 			changed = true;
1112 			drm_sysfs_connector_hotplug_event(connector);
1113 		}
1114 	}
1115 	drm_connector_list_iter_end(&conn_iter);
1116 	mutex_unlock(&dev->mode_config.mutex);
1117 
1118 	if (changed)
1119 		drm_client_dev_hotplug(dev);
1120 
1121 	return changed;
1122 }
1123 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1124 
1125 /**
1126  * drm_crtc_helper_mode_valid_fixed - Validates a display mode
1127  * @crtc: the crtc
1128  * @mode: the mode to validate
1129  * @fixed_mode: the display hardware's mode
1130  *
1131  * Returns:
1132  * MODE_OK on success, or another mode-status code otherwise.
1133  */
1134 enum drm_mode_status drm_crtc_helper_mode_valid_fixed(struct drm_crtc *crtc,
1135 						      const struct drm_display_mode *mode,
1136 						      const struct drm_display_mode *fixed_mode)
1137 {
1138 	if (mode->hdisplay != fixed_mode->hdisplay && mode->vdisplay != fixed_mode->vdisplay)
1139 		return MODE_ONE_SIZE;
1140 	else if (mode->hdisplay != fixed_mode->hdisplay)
1141 		return MODE_ONE_WIDTH;
1142 	else if (mode->vdisplay != fixed_mode->vdisplay)
1143 		return MODE_ONE_HEIGHT;
1144 
1145 	return MODE_OK;
1146 }
1147 EXPORT_SYMBOL(drm_crtc_helper_mode_valid_fixed);
1148 
1149 /**
1150  * drm_connector_helper_get_modes_fixed - Duplicates a display mode for a connector
1151  * @connector: the connector
1152  * @fixed_mode: the display hardware's mode
1153  *
1154  * This function duplicates a display modes for a connector. Drivers for hardware
1155  * that only supports a single fixed mode can use this function in their connector's
1156  * get_modes helper.
1157  *
1158  * Returns:
1159  * The number of created modes.
1160  */
1161 int drm_connector_helper_get_modes_fixed(struct drm_connector *connector,
1162 					 const struct drm_display_mode *fixed_mode)
1163 {
1164 	struct drm_device *dev = connector->dev;
1165 	struct drm_display_mode *mode;
1166 
1167 	mode = drm_mode_duplicate(dev, fixed_mode);
1168 	if (!mode) {
1169 		drm_err(dev, "Failed to duplicate mode " DRM_MODE_FMT "\n",
1170 			DRM_MODE_ARG(fixed_mode));
1171 		return 0;
1172 	}
1173 
1174 	if (mode->name[0] == '\0')
1175 		drm_mode_set_name(mode);
1176 
1177 	mode->type |= DRM_MODE_TYPE_PREFERRED;
1178 	drm_mode_probed_add(connector, mode);
1179 
1180 	if (mode->width_mm)
1181 		connector->display_info.width_mm = mode->width_mm;
1182 	if (mode->height_mm)
1183 		connector->display_info.height_mm = mode->height_mm;
1184 
1185 	return 1;
1186 }
1187 EXPORT_SYMBOL(drm_connector_helper_get_modes_fixed);
1188 
1189 /**
1190  * drm_connector_helper_get_modes - Read EDID and update connector.
1191  * @connector: The connector
1192  *
1193  * Read the EDID using drm_edid_read() (which requires that connector->ddc is
1194  * set), and update the connector using the EDID.
1195  *
1196  * This can be used as the "default" connector helper .get_modes() hook if the
1197  * driver does not need any special processing. This is sets the example what
1198  * custom .get_modes() hooks should do regarding EDID read and connector update.
1199  *
1200  * Returns: Number of modes.
1201  */
1202 int drm_connector_helper_get_modes(struct drm_connector *connector)
1203 {
1204 	const struct drm_edid *drm_edid;
1205 	int count;
1206 
1207 	drm_edid = drm_edid_read(connector);
1208 
1209 	/*
1210 	 * Unconditionally update the connector. If the EDID was read
1211 	 * successfully, fill in the connector information derived from the
1212 	 * EDID. Otherwise, if the EDID is NULL, clear the connector
1213 	 * information.
1214 	 */
1215 	drm_edid_connector_update(connector, drm_edid);
1216 
1217 	count = drm_edid_connector_add_modes(connector);
1218 
1219 	drm_edid_free(drm_edid);
1220 
1221 	return count;
1222 }
1223 EXPORT_SYMBOL(drm_connector_helper_get_modes);
1224 
1225 /**
1226  * drm_connector_helper_tv_get_modes - Fills the modes availables to a TV connector
1227  * @connector: The connector
1228  *
1229  * Fills the available modes for a TV connector based on the supported
1230  * TV modes, and the default mode expressed by the kernel command line.
1231  *
1232  * This can be used as the default TV connector helper .get_modes() hook
1233  * if the driver does not need any special processing.
1234  *
1235  * Returns:
1236  * The number of modes added to the connector.
1237  */
1238 int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
1239 {
1240 	struct drm_device *dev = connector->dev;
1241 	struct drm_property *tv_mode_property =
1242 		dev->mode_config.tv_mode_property;
1243 	struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
1244 	unsigned int ntsc_modes = BIT(DRM_MODE_TV_MODE_NTSC) |
1245 		BIT(DRM_MODE_TV_MODE_NTSC_443) |
1246 		BIT(DRM_MODE_TV_MODE_NTSC_J) |
1247 		BIT(DRM_MODE_TV_MODE_PAL_M);
1248 	unsigned int pal_modes = BIT(DRM_MODE_TV_MODE_PAL) |
1249 		BIT(DRM_MODE_TV_MODE_PAL_N) |
1250 		BIT(DRM_MODE_TV_MODE_SECAM);
1251 	unsigned int tv_modes[2] = { UINT_MAX, UINT_MAX };
1252 	unsigned int i, supported_tv_modes = 0;
1253 
1254 	if (!tv_mode_property)
1255 		return 0;
1256 
1257 	for (i = 0; i < tv_mode_property->num_values; i++)
1258 		supported_tv_modes |= BIT(tv_mode_property->values[i]);
1259 
1260 	if (((supported_tv_modes & ntsc_modes) &&
1261 	     (supported_tv_modes & pal_modes)) ||
1262 	    (supported_tv_modes & BIT(DRM_MODE_TV_MODE_MONOCHROME))) {
1263 		uint64_t default_mode;
1264 
1265 		if (drm_object_property_get_default_value(&connector->base,
1266 							  tv_mode_property,
1267 							  &default_mode))
1268 			return 0;
1269 
1270 		if (cmdline->tv_mode_specified)
1271 			default_mode = cmdline->tv_mode;
1272 
1273 		if (BIT(default_mode) & ntsc_modes) {
1274 			tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
1275 			tv_modes[1] = DRM_MODE_TV_MODE_PAL;
1276 		} else {
1277 			tv_modes[0] = DRM_MODE_TV_MODE_PAL;
1278 			tv_modes[1] = DRM_MODE_TV_MODE_NTSC;
1279 		}
1280 	} else if (supported_tv_modes & ntsc_modes) {
1281 		tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
1282 	} else if (supported_tv_modes & pal_modes) {
1283 		tv_modes[0] = DRM_MODE_TV_MODE_PAL;
1284 	} else {
1285 		return 0;
1286 	}
1287 
1288 	for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
1289 		struct drm_display_mode *mode;
1290 
1291 		if (tv_modes[i] == DRM_MODE_TV_MODE_NTSC)
1292 			mode = drm_mode_analog_ntsc_480i(dev);
1293 		else if (tv_modes[i] == DRM_MODE_TV_MODE_PAL)
1294 			mode = drm_mode_analog_pal_576i(dev);
1295 		else
1296 			break;
1297 		if (!mode)
1298 			return i;
1299 		if (!i)
1300 			mode->type |= DRM_MODE_TYPE_PREFERRED;
1301 		drm_mode_probed_add(connector, mode);
1302 	}
1303 
1304 	return i;
1305 }
1306 EXPORT_SYMBOL(drm_connector_helper_tv_get_modes);
1307 
1308 /**
1309  * drm_connector_helper_detect_from_ddc - Read EDID and detect connector status.
1310  * @connector: The connector
1311  * @ctx: Acquire context
1312  * @force: Perform screen-destructive operations, if necessary
1313  *
1314  * Detects the connector status by reading the EDID using drm_probe_ddc(),
1315  * which requires connector->ddc to be set. Returns connector_status_connected
1316  * on success or connector_status_disconnected on failure.
1317  *
1318  * Returns:
1319  * The connector status as defined by enum drm_connector_status.
1320  */
1321 int drm_connector_helper_detect_from_ddc(struct drm_connector *connector,
1322 					 struct drm_modeset_acquire_ctx *ctx,
1323 					 bool force)
1324 {
1325 	struct i2c_adapter *ddc = connector->ddc;
1326 
1327 	if (!ddc)
1328 		return connector_status_unknown;
1329 
1330 	if (drm_probe_ddc(ddc))
1331 		return connector_status_connected;
1332 
1333 	return connector_status_disconnected;
1334 }
1335 EXPORT_SYMBOL(drm_connector_helper_detect_from_ddc);
1336