xref: /linux/drivers/gpu/drm/drm_connector.c (revision 7b1166dee847d5018c1f3cc781218e806078f752)
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <drm/drm_auth.h>
24 #include <drm/drm_connector.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_edid.h>
27 #include <drm/drm_encoder.h>
28 #include <drm/drm_file.h>
29 #include <drm/drm_managed.h>
30 #include <drm/drm_panel.h>
31 #include <drm/drm_print.h>
32 #include <drm/drm_privacy_screen_consumer.h>
33 #include <drm/drm_sysfs.h>
34 #include <drm/drm_utils.h>
35 
36 #include <linux/platform_device.h>
37 #include <linux/property.h>
38 #include <linux/uaccess.h>
39 
40 #include <video/cmdline.h>
41 
42 #include "drm_crtc_internal.h"
43 #include "drm_internal.h"
44 
45 /**
46  * DOC: overview
47  *
48  * In DRM connectors are the general abstraction for display sinks, and include
49  * also fixed panels or anything else that can display pixels in some form. As
50  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
51  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
52  * Hence they are reference-counted using drm_connector_get() and
53  * drm_connector_put().
54  *
55  * KMS driver must create, initialize, register and attach at a &struct
56  * drm_connector for each such sink. The instance is created as other KMS
57  * objects and initialized by setting the following fields. The connector is
58  * initialized with a call to drm_connector_init() with a pointer to the
59  * &struct drm_connector_funcs and a connector type, and then exposed to
60  * userspace with a call to drm_connector_register().
61  *
62  * Connectors must be attached to an encoder to be used. For devices that map
63  * connectors to encoders 1:1, the connector should be attached at
64  * initialization time with a call to drm_connector_attach_encoder(). The
65  * driver must also set the &drm_connector.encoder field to point to the
66  * attached encoder.
67  *
68  * For connectors which are not fixed (like built-in panels) the driver needs to
69  * support hotplug notifications. The simplest way to do that is by using the
70  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
71  * hardware support for hotplug interrupts. Connectors with hardware hotplug
72  * support can instead use e.g. drm_helper_hpd_irq_event().
73  */
74 
75 /*
76  * Global connector list for drm_connector_find_by_fwnode().
77  * Note drm_connector_[un]register() first take connector->lock and then
78  * take the connector_list_lock.
79  */
80 static DEFINE_MUTEX(connector_list_lock);
81 static LIST_HEAD(connector_list);
82 
83 struct drm_conn_prop_enum_list {
84 	int type;
85 	const char *name;
86 	struct ida ida;
87 };
88 
89 /*
90  * Connector and encoder types.
91  */
92 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
93 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
94 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
95 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
96 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
97 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
98 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
99 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
100 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
101 	{ DRM_MODE_CONNECTOR_Component, "Component" },
102 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
103 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
104 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
105 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
106 	{ DRM_MODE_CONNECTOR_TV, "TV" },
107 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
108 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
109 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
110 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
111 	{ DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
112 	{ DRM_MODE_CONNECTOR_SPI, "SPI" },
113 	{ DRM_MODE_CONNECTOR_USB, "USB" },
114 };
115 
116 void drm_connector_ida_init(void)
117 {
118 	int i;
119 
120 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
121 		ida_init(&drm_connector_enum_list[i].ida);
122 }
123 
124 void drm_connector_ida_destroy(void)
125 {
126 	int i;
127 
128 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
129 		ida_destroy(&drm_connector_enum_list[i].ida);
130 }
131 
132 /**
133  * drm_get_connector_type_name - return a string for connector type
134  * @type: The connector type (DRM_MODE_CONNECTOR_*)
135  *
136  * Returns: the name of the connector type, or NULL if the type is not valid.
137  */
138 const char *drm_get_connector_type_name(unsigned int type)
139 {
140 	if (type < ARRAY_SIZE(drm_connector_enum_list))
141 		return drm_connector_enum_list[type].name;
142 
143 	return NULL;
144 }
145 EXPORT_SYMBOL(drm_get_connector_type_name);
146 
147 /**
148  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
149  * @connector: connector to query
150  *
151  * The kernel supports per-connector configuration of its consoles through
152  * use of the video= parameter. This function parses that option and
153  * extracts the user's specified mode (or enable/disable status) for a
154  * particular connector. This is typically only used during the early fbdev
155  * setup.
156  */
157 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
158 {
159 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
160 	const char *option;
161 
162 	option = video_get_options(connector->name);
163 	if (!option)
164 		return;
165 
166 	if (!drm_mode_parse_command_line_for_connector(option,
167 						       connector,
168 						       mode))
169 		return;
170 
171 	if (mode->force) {
172 		DRM_INFO("forcing %s connector %s\n", connector->name,
173 			 drm_get_connector_force_name(mode->force));
174 		connector->force = mode->force;
175 	}
176 
177 	if (mode->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN) {
178 		DRM_INFO("cmdline forces connector %s panel_orientation to %d\n",
179 			 connector->name, mode->panel_orientation);
180 		drm_connector_set_panel_orientation(connector,
181 						    mode->panel_orientation);
182 	}
183 
184 	DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
185 		      connector->name, mode->name,
186 		      mode->xres, mode->yres,
187 		      mode->refresh_specified ? mode->refresh : 60,
188 		      mode->rb ? " reduced blanking" : "",
189 		      mode->margins ? " with margins" : "",
190 		      mode->interlace ?  " interlaced" : "");
191 }
192 
193 static void drm_connector_free(struct kref *kref)
194 {
195 	struct drm_connector *connector =
196 		container_of(kref, struct drm_connector, base.refcount);
197 	struct drm_device *dev = connector->dev;
198 
199 	drm_mode_object_unregister(dev, &connector->base);
200 	connector->funcs->destroy(connector);
201 }
202 
203 void drm_connector_free_work_fn(struct work_struct *work)
204 {
205 	struct drm_connector *connector, *n;
206 	struct drm_device *dev =
207 		container_of(work, struct drm_device, mode_config.connector_free_work);
208 	struct drm_mode_config *config = &dev->mode_config;
209 	unsigned long flags;
210 	struct llist_node *freed;
211 
212 	spin_lock_irqsave(&config->connector_list_lock, flags);
213 	freed = llist_del_all(&config->connector_free_list);
214 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
215 
216 	llist_for_each_entry_safe(connector, n, freed, free_node) {
217 		drm_mode_object_unregister(dev, &connector->base);
218 		connector->funcs->destroy(connector);
219 	}
220 }
221 
222 static int drm_connector_init_only(struct drm_device *dev,
223 				   struct drm_connector *connector,
224 				   const struct drm_connector_funcs *funcs,
225 				   int connector_type,
226 				   struct i2c_adapter *ddc)
227 {
228 	struct drm_mode_config *config = &dev->mode_config;
229 	int ret;
230 	struct ida *connector_ida =
231 		&drm_connector_enum_list[connector_type].ida;
232 
233 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
234 		(!funcs->atomic_destroy_state ||
235 		 !funcs->atomic_duplicate_state));
236 
237 	ret = __drm_mode_object_add(dev, &connector->base,
238 				    DRM_MODE_OBJECT_CONNECTOR,
239 				    false, drm_connector_free);
240 	if (ret)
241 		return ret;
242 
243 	connector->base.properties = &connector->properties;
244 	connector->dev = dev;
245 	connector->funcs = funcs;
246 
247 	/* connector index is used with 32bit bitmasks */
248 	ret = ida_alloc_max(&config->connector_ida, 31, GFP_KERNEL);
249 	if (ret < 0) {
250 		DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
251 			      drm_connector_enum_list[connector_type].name,
252 			      ret);
253 		goto out_put;
254 	}
255 	connector->index = ret;
256 	ret = 0;
257 
258 	connector->connector_type = connector_type;
259 	connector->connector_type_id =
260 		ida_alloc_min(connector_ida, 1, GFP_KERNEL);
261 	if (connector->connector_type_id < 0) {
262 		ret = connector->connector_type_id;
263 		goto out_put_id;
264 	}
265 	connector->name =
266 		kasprintf(GFP_KERNEL, "%s-%d",
267 			  drm_connector_enum_list[connector_type].name,
268 			  connector->connector_type_id);
269 	if (!connector->name) {
270 		ret = -ENOMEM;
271 		goto out_put_type_id;
272 	}
273 
274 	/* provide ddc symlink in sysfs */
275 	connector->ddc = ddc;
276 
277 	INIT_LIST_HEAD(&connector->head);
278 	INIT_LIST_HEAD(&connector->global_connector_list_entry);
279 	INIT_LIST_HEAD(&connector->probed_modes);
280 	INIT_LIST_HEAD(&connector->modes);
281 	mutex_init(&connector->mutex);
282 	mutex_init(&connector->cec.mutex);
283 	mutex_init(&connector->eld_mutex);
284 	mutex_init(&connector->edid_override_mutex);
285 	mutex_init(&connector->hdmi.infoframes.lock);
286 	mutex_init(&connector->hdmi_audio.lock);
287 	connector->edid_blob_ptr = NULL;
288 	connector->epoch_counter = 0;
289 	connector->tile_blob_ptr = NULL;
290 	connector->status = connector_status_unknown;
291 	connector->display_info.panel_orientation =
292 		DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
293 
294 	drm_connector_get_cmdline_mode(connector);
295 
296 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
297 	    connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
298 		drm_connector_attach_edid_property(connector);
299 
300 	drm_object_attach_property(&connector->base,
301 				      config->dpms_property, 0);
302 
303 	drm_object_attach_property(&connector->base,
304 				   config->link_status_property,
305 				   0);
306 
307 	drm_object_attach_property(&connector->base,
308 				   config->non_desktop_property,
309 				   0);
310 	drm_object_attach_property(&connector->base,
311 				   config->tile_property,
312 				   0);
313 
314 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
315 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
316 	}
317 
318 	connector->debugfs_entry = NULL;
319 out_put_type_id:
320 	if (ret)
321 		ida_free(connector_ida, connector->connector_type_id);
322 out_put_id:
323 	if (ret)
324 		ida_free(&config->connector_ida, connector->index);
325 out_put:
326 	if (ret)
327 		drm_mode_object_unregister(dev, &connector->base);
328 
329 	return ret;
330 }
331 
332 static void drm_connector_add(struct drm_connector *connector)
333 {
334 	struct drm_device *dev = connector->dev;
335 	struct drm_mode_config *config = &dev->mode_config;
336 
337 	if (drm_WARN_ON(dev, !list_empty(&connector->head)))
338 		return;
339 
340 	spin_lock_irq(&config->connector_list_lock);
341 	list_add_tail(&connector->head, &config->connector_list);
342 	config->num_connector++;
343 	spin_unlock_irq(&config->connector_list_lock);
344 }
345 
346 static void drm_connector_remove(struct drm_connector *connector)
347 {
348 	struct drm_device *dev = connector->dev;
349 
350 	/*
351 	 * For dynamic connectors drm_connector_cleanup() can call this function
352 	 * before the connector is registered and added to the list.
353 	 */
354 	if (list_empty(&connector->head))
355 		return;
356 
357 	spin_lock_irq(&dev->mode_config.connector_list_lock);
358 	list_del_init(&connector->head);
359 	dev->mode_config.num_connector--;
360 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
361 }
362 
363 static int drm_connector_init_and_add(struct drm_device *dev,
364 				      struct drm_connector *connector,
365 				      const struct drm_connector_funcs *funcs,
366 				      int connector_type,
367 				      struct i2c_adapter *ddc)
368 {
369 	int ret;
370 
371 	ret = drm_connector_init_only(dev, connector, funcs, connector_type, ddc);
372 	if (ret)
373 		return ret;
374 
375 	drm_connector_add(connector);
376 
377 	return 0;
378 }
379 
380 /**
381  * drm_connector_init - Init a preallocated connector
382  * @dev: DRM device
383  * @connector: the connector to init
384  * @funcs: callbacks for this connector
385  * @connector_type: user visible type of the connector
386  *
387  * Initialises a preallocated connector. Connectors should be
388  * subclassed as part of driver connector objects.
389  *
390  * At driver unload time the driver's &drm_connector_funcs.destroy hook
391  * should call drm_connector_cleanup() and free the connector structure.
392  * The connector structure should not be allocated with devm_kzalloc().
393  *
394  * Note: consider using drmm_connector_init() instead of
395  * drm_connector_init() to let the DRM managed resource infrastructure
396  * take care of cleanup and deallocation.
397  *
398  * Returns:
399  * Zero on success, error code on failure.
400  */
401 int drm_connector_init(struct drm_device *dev,
402 		       struct drm_connector *connector,
403 		       const struct drm_connector_funcs *funcs,
404 		       int connector_type)
405 {
406 	if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
407 		return -EINVAL;
408 
409 	return drm_connector_init_and_add(dev, connector, funcs, connector_type, NULL);
410 }
411 EXPORT_SYMBOL(drm_connector_init);
412 
413 /**
414  * drm_connector_dynamic_init - Init a preallocated dynamic connector
415  * @dev: DRM device
416  * @connector: the connector to init
417  * @funcs: callbacks for this connector
418  * @connector_type: user visible type of the connector
419  * @ddc: pointer to the associated ddc adapter
420  *
421  * Initialises a preallocated dynamic connector. Connectors should be
422  * subclassed as part of driver connector objects. The connector
423  * structure should not be allocated with devm_kzalloc().
424  *
425  * Drivers should call this for dynamic connectors which can be hotplugged
426  * after drm_dev_register() has been called already, e.g. DP MST connectors.
427  * For all other - static - connectors, drivers should call one of the
428  * drm_connector_init*()/drmm_connector_init*() functions.
429  *
430  * After calling this function the drivers must call
431  * drm_connector_dynamic_register().
432  *
433  * To remove the connector the driver must call drm_connector_unregister()
434  * followed by drm_connector_put(). Putting the last reference will call the
435  * driver's &drm_connector_funcs.destroy hook, which in turn must call
436  * drm_connector_cleanup() and free the connector structure.
437  *
438  * Returns:
439  * Zero on success, error code on failure.
440  */
441 int drm_connector_dynamic_init(struct drm_device *dev,
442 			       struct drm_connector *connector,
443 			       const struct drm_connector_funcs *funcs,
444 			       int connector_type,
445 			       struct i2c_adapter *ddc)
446 {
447 	if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
448 		return -EINVAL;
449 
450 	return drm_connector_init_only(dev, connector, funcs, connector_type, ddc);
451 }
452 EXPORT_SYMBOL(drm_connector_dynamic_init);
453 
454 /**
455  * drm_connector_init_with_ddc - Init a preallocated connector
456  * @dev: DRM device
457  * @connector: the connector to init
458  * @funcs: callbacks for this connector
459  * @connector_type: user visible type of the connector
460  * @ddc: pointer to the associated ddc adapter
461  *
462  * Initialises a preallocated connector. Connectors should be
463  * subclassed as part of driver connector objects.
464  *
465  * At driver unload time the driver's &drm_connector_funcs.destroy hook
466  * should call drm_connector_cleanup() and free the connector structure.
467  * The connector structure should not be allocated with devm_kzalloc().
468  *
469  * Ensures that the ddc field of the connector is correctly set.
470  *
471  * Note: consider using drmm_connector_init() instead of
472  * drm_connector_init_with_ddc() to let the DRM managed resource
473  * infrastructure take care of cleanup and deallocation.
474  *
475  * Returns:
476  * Zero on success, error code on failure.
477  */
478 int drm_connector_init_with_ddc(struct drm_device *dev,
479 				struct drm_connector *connector,
480 				const struct drm_connector_funcs *funcs,
481 				int connector_type,
482 				struct i2c_adapter *ddc)
483 {
484 	if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
485 		return -EINVAL;
486 
487 	return drm_connector_init_and_add(dev, connector, funcs, connector_type, ddc);
488 }
489 EXPORT_SYMBOL(drm_connector_init_with_ddc);
490 
491 static void drm_connector_cleanup_action(struct drm_device *dev,
492 					 void *ptr)
493 {
494 	struct drm_connector *connector = ptr;
495 
496 	drm_connector_cleanup(connector);
497 }
498 
499 /**
500  * drmm_connector_init - Init a preallocated connector
501  * @dev: DRM device
502  * @connector: the connector to init
503  * @funcs: callbacks for this connector
504  * @connector_type: user visible type of the connector
505  * @ddc: optional pointer to the associated ddc adapter
506  *
507  * Initialises a preallocated connector. Connectors should be
508  * subclassed as part of driver connector objects.
509  *
510  * Cleanup is automatically handled with a call to
511  * drm_connector_cleanup() in a DRM-managed action.
512  *
513  * The connector structure should be allocated with drmm_kzalloc().
514  *
515  * The @drm_connector_funcs.destroy hook must be NULL.
516  *
517  * Returns:
518  * Zero on success, error code on failure.
519  */
520 int drmm_connector_init(struct drm_device *dev,
521 			struct drm_connector *connector,
522 			const struct drm_connector_funcs *funcs,
523 			int connector_type,
524 			struct i2c_adapter *ddc)
525 {
526 	int ret;
527 
528 	if (drm_WARN_ON(dev, funcs && funcs->destroy))
529 		return -EINVAL;
530 
531 	ret = drm_connector_init_and_add(dev, connector, funcs, connector_type, ddc);
532 	if (ret)
533 		return ret;
534 
535 	ret = drmm_add_action_or_reset(dev, drm_connector_cleanup_action,
536 				       connector);
537 	if (ret)
538 		return ret;
539 
540 	return 0;
541 }
542 EXPORT_SYMBOL(drmm_connector_init);
543 
544 /**
545  * drmm_connector_hdmi_init - Init a preallocated HDMI connector
546  * @dev: DRM device
547  * @connector: A pointer to the HDMI connector to init
548  * @vendor: HDMI Controller Vendor name
549  * @product: HDMI Controller Product name
550  * @funcs: callbacks for this connector
551  * @hdmi_funcs: HDMI-related callbacks for this connector
552  * @connector_type: user visible type of the connector
553  * @ddc: optional pointer to the associated ddc adapter
554  * @supported_formats: Bitmask of @hdmi_colorspace listing supported output formats
555  * @max_bpc: Maximum bits per char the HDMI connector supports
556  *
557  * Initialises a preallocated HDMI connector. Connectors can be
558  * subclassed as part of driver connector objects.
559  *
560  * Cleanup is automatically handled with a call to
561  * drm_connector_cleanup() in a DRM-managed action.
562  *
563  * The connector structure should be allocated with drmm_kzalloc().
564  *
565  * The @drm_connector_funcs.destroy hook must be NULL.
566  *
567  * Returns:
568  * Zero on success, error code on failure.
569  */
570 int drmm_connector_hdmi_init(struct drm_device *dev,
571 			     struct drm_connector *connector,
572 			     const char *vendor, const char *product,
573 			     const struct drm_connector_funcs *funcs,
574 			     const struct drm_connector_hdmi_funcs *hdmi_funcs,
575 			     int connector_type,
576 			     struct i2c_adapter *ddc,
577 			     unsigned long supported_formats,
578 			     unsigned int max_bpc)
579 {
580 	int ret;
581 
582 	if (!vendor || !product)
583 		return -EINVAL;
584 
585 	if ((strlen(vendor) > DRM_CONNECTOR_HDMI_VENDOR_LEN) ||
586 	    (strlen(product) > DRM_CONNECTOR_HDMI_PRODUCT_LEN))
587 		return -EINVAL;
588 
589 	if (!(connector_type == DRM_MODE_CONNECTOR_HDMIA ||
590 	      connector_type == DRM_MODE_CONNECTOR_HDMIB))
591 		return -EINVAL;
592 
593 	if (!supported_formats || !(supported_formats & BIT(HDMI_COLORSPACE_RGB)))
594 		return -EINVAL;
595 
596 	if (connector->ycbcr_420_allowed != !!(supported_formats & BIT(HDMI_COLORSPACE_YUV420)))
597 		return -EINVAL;
598 
599 	if (!(max_bpc == 8 || max_bpc == 10 || max_bpc == 12))
600 		return -EINVAL;
601 
602 	ret = drmm_connector_init(dev, connector, funcs, connector_type, ddc);
603 	if (ret)
604 		return ret;
605 
606 	connector->hdmi.supported_formats = supported_formats;
607 	strtomem_pad(connector->hdmi.vendor, vendor, 0);
608 	strtomem_pad(connector->hdmi.product, product, 0);
609 
610 	/*
611 	 * drm_connector_attach_max_bpc_property() requires the
612 	 * connector to have a state.
613 	 */
614 	if (connector->funcs->reset)
615 		connector->funcs->reset(connector);
616 
617 	drm_connector_attach_max_bpc_property(connector, 8, max_bpc);
618 	connector->max_bpc = max_bpc;
619 
620 	if (max_bpc > 8)
621 		drm_connector_attach_hdr_output_metadata_property(connector);
622 
623 	connector->hdmi.funcs = hdmi_funcs;
624 
625 	return 0;
626 }
627 EXPORT_SYMBOL(drmm_connector_hdmi_init);
628 
629 /**
630  * drm_connector_attach_edid_property - attach edid property.
631  * @connector: the connector
632  *
633  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
634  * edid property attached by default.  This function can be used to
635  * explicitly enable the edid property in these cases.
636  */
637 void drm_connector_attach_edid_property(struct drm_connector *connector)
638 {
639 	struct drm_mode_config *config = &connector->dev->mode_config;
640 
641 	drm_object_attach_property(&connector->base,
642 				   config->edid_property,
643 				   0);
644 }
645 EXPORT_SYMBOL(drm_connector_attach_edid_property);
646 
647 /**
648  * drm_connector_attach_encoder - attach a connector to an encoder
649  * @connector: connector to attach
650  * @encoder: encoder to attach @connector to
651  *
652  * This function links up a connector to an encoder. Note that the routing
653  * restrictions between encoders and crtcs are exposed to userspace through the
654  * possible_clones and possible_crtcs bitmasks.
655  *
656  * Returns:
657  * Zero on success, negative errno on failure.
658  */
659 int drm_connector_attach_encoder(struct drm_connector *connector,
660 				 struct drm_encoder *encoder)
661 {
662 	/*
663 	 * In the past, drivers have attempted to model the static association
664 	 * of connector to encoder in simple connector/encoder devices using a
665 	 * direct assignment of connector->encoder = encoder. This connection
666 	 * is a logical one and the responsibility of the core, so drivers are
667 	 * expected not to mess with this.
668 	 *
669 	 * Note that the error return should've been enough here, but a large
670 	 * majority of drivers ignores the return value, so add in a big WARN
671 	 * to get people's attention.
672 	 */
673 	if (WARN_ON(connector->encoder))
674 		return -EINVAL;
675 
676 	connector->possible_encoders |= drm_encoder_mask(encoder);
677 
678 	return 0;
679 }
680 EXPORT_SYMBOL(drm_connector_attach_encoder);
681 
682 /**
683  * drm_connector_has_possible_encoder - check if the connector and encoder are
684  * associated with each other
685  * @connector: the connector
686  * @encoder: the encoder
687  *
688  * Returns:
689  * True if @encoder is one of the possible encoders for @connector.
690  */
691 bool drm_connector_has_possible_encoder(struct drm_connector *connector,
692 					struct drm_encoder *encoder)
693 {
694 	return connector->possible_encoders & drm_encoder_mask(encoder);
695 }
696 EXPORT_SYMBOL(drm_connector_has_possible_encoder);
697 
698 static void drm_mode_remove(struct drm_connector *connector,
699 			    struct drm_display_mode *mode)
700 {
701 	list_del(&mode->head);
702 	drm_mode_destroy(connector->dev, mode);
703 }
704 
705 /**
706  * drm_connector_cec_phys_addr_invalidate - invalidate CEC physical address
707  * @connector: connector undergoing CEC operation
708  *
709  * Invalidated CEC physical address set for this DRM connector.
710  */
711 void drm_connector_cec_phys_addr_invalidate(struct drm_connector *connector)
712 {
713 	mutex_lock(&connector->cec.mutex);
714 
715 	if (connector->cec.funcs &&
716 	    connector->cec.funcs->phys_addr_invalidate)
717 		connector->cec.funcs->phys_addr_invalidate(connector);
718 
719 	mutex_unlock(&connector->cec.mutex);
720 }
721 EXPORT_SYMBOL(drm_connector_cec_phys_addr_invalidate);
722 
723 /**
724  * drm_connector_cec_phys_addr_set - propagate CEC physical address
725  * @connector: connector undergoing CEC operation
726  *
727  * Propagate CEC physical address from the display_info to this DRM connector.
728  */
729 void drm_connector_cec_phys_addr_set(struct drm_connector *connector)
730 {
731 	u16 addr;
732 
733 	mutex_lock(&connector->cec.mutex);
734 
735 	addr = connector->display_info.source_physical_address;
736 
737 	if (connector->cec.funcs &&
738 	    connector->cec.funcs->phys_addr_set)
739 		connector->cec.funcs->phys_addr_set(connector, addr);
740 
741 	mutex_unlock(&connector->cec.mutex);
742 }
743 EXPORT_SYMBOL(drm_connector_cec_phys_addr_set);
744 
745 /**
746  * drm_connector_cleanup - cleans up an initialised connector
747  * @connector: connector to cleanup
748  *
749  * Cleans up the connector but doesn't free the object.
750  */
751 void drm_connector_cleanup(struct drm_connector *connector)
752 {
753 	struct drm_device *dev = connector->dev;
754 	struct drm_display_mode *mode, *t;
755 
756 	/* The connector should have been removed from userspace long before
757 	 * it is finally destroyed.
758 	 */
759 	if (WARN_ON(connector->registration_state ==
760 		    DRM_CONNECTOR_REGISTERED))
761 		drm_connector_unregister(connector);
762 
763 	platform_device_unregister(connector->hdmi_audio.codec_pdev);
764 
765 	if (connector->privacy_screen) {
766 		drm_privacy_screen_put(connector->privacy_screen);
767 		connector->privacy_screen = NULL;
768 	}
769 
770 	if (connector->tile_group) {
771 		drm_mode_put_tile_group(dev, connector->tile_group);
772 		connector->tile_group = NULL;
773 	}
774 
775 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
776 		drm_mode_remove(connector, mode);
777 
778 	list_for_each_entry_safe(mode, t, &connector->modes, head)
779 		drm_mode_remove(connector, mode);
780 
781 	ida_free(&drm_connector_enum_list[connector->connector_type].ida,
782 			  connector->connector_type_id);
783 
784 	ida_free(&dev->mode_config.connector_ida, connector->index);
785 
786 	kfree(connector->display_info.bus_formats);
787 	kfree(connector->display_info.vics);
788 	drm_mode_object_unregister(dev, &connector->base);
789 	kfree(connector->name);
790 	connector->name = NULL;
791 	fwnode_handle_put(connector->fwnode);
792 	connector->fwnode = NULL;
793 
794 	drm_connector_remove(connector);
795 
796 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
797 	if (connector->state && connector->funcs->atomic_destroy_state)
798 		connector->funcs->atomic_destroy_state(connector,
799 						       connector->state);
800 
801 	mutex_destroy(&connector->hdmi_audio.lock);
802 	mutex_destroy(&connector->hdmi.infoframes.lock);
803 	mutex_destroy(&connector->mutex);
804 
805 	memset(connector, 0, sizeof(*connector));
806 
807 	if (dev->registered)
808 		drm_sysfs_hotplug_event(dev);
809 }
810 EXPORT_SYMBOL(drm_connector_cleanup);
811 
812 /**
813  * drm_connector_register - register a connector
814  * @connector: the connector to register
815  *
816  * Register userspace interfaces for a connector. Drivers shouldn't call this
817  * function. Static connectors will be registered automatically by DRM core
818  * from drm_dev_register(), dynamic connectors (MST) should be registered by
819  * drivers calling drm_connector_dynamic_register().
820  *
821  * When the connector is no longer available, callers must call
822  * drm_connector_unregister().
823  *
824  * Note: Existing uses of this function in drivers should be a nop already and
825  * are scheduled to be removed.
826  *
827  * Returns:
828  * Zero on success, error code on failure.
829  */
830 int drm_connector_register(struct drm_connector *connector)
831 {
832 	int ret = 0;
833 
834 	if (!connector->dev->registered)
835 		return 0;
836 
837 	mutex_lock(&connector->mutex);
838 	if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
839 		goto unlock;
840 
841 	ret = drm_sysfs_connector_add(connector);
842 	if (ret)
843 		goto unlock;
844 
845 	drm_debugfs_connector_add(connector);
846 
847 	if (connector->funcs->late_register) {
848 		ret = connector->funcs->late_register(connector);
849 		if (ret)
850 			goto err_debugfs;
851 	}
852 
853 	ret = drm_sysfs_connector_add_late(connector);
854 	if (ret)
855 		goto err_late_register;
856 
857 	drm_mode_object_register(connector->dev, &connector->base);
858 
859 	connector->registration_state = DRM_CONNECTOR_REGISTERED;
860 
861 	/* Let userspace know we have a new connector */
862 	drm_sysfs_connector_hotplug_event(connector);
863 
864 	if (connector->privacy_screen)
865 		drm_privacy_screen_register_notifier(connector->privacy_screen,
866 					   &connector->privacy_screen_notifier);
867 
868 	mutex_lock(&connector_list_lock);
869 	list_add_tail(&connector->global_connector_list_entry, &connector_list);
870 	mutex_unlock(&connector_list_lock);
871 	goto unlock;
872 
873 err_late_register:
874 	if (connector->funcs->early_unregister)
875 		connector->funcs->early_unregister(connector);
876 err_debugfs:
877 	drm_debugfs_connector_remove(connector);
878 	drm_sysfs_connector_remove(connector);
879 unlock:
880 	mutex_unlock(&connector->mutex);
881 	return ret;
882 }
883 EXPORT_SYMBOL(drm_connector_register);
884 
885 /**
886  * drm_connector_dynamic_register - register a dynamic connector
887  * @connector: the connector to register
888  *
889  * Register userspace interfaces for a connector. Only call this for connectors
890  * initialized by calling drm_connector_dynamic_init(). All other connectors
891  * will be registered automatically when calling drm_dev_register().
892  *
893  * When the connector is no longer available the driver must call
894  * drm_connector_unregister().
895  *
896  * Returns:
897  * Zero on success, error code on failure.
898  */
899 int drm_connector_dynamic_register(struct drm_connector *connector)
900 {
901 	/* Was the connector inited already? */
902 	if (WARN_ON(!(connector->funcs && connector->funcs->destroy)))
903 		return -EINVAL;
904 
905 	drm_connector_add(connector);
906 
907 	return drm_connector_register(connector);
908 }
909 EXPORT_SYMBOL(drm_connector_dynamic_register);
910 
911 /**
912  * drm_connector_unregister - unregister a connector
913  * @connector: the connector to unregister
914  *
915  * Unregister userspace interfaces for a connector. Drivers should call this
916  * for dynamic connectors (MST) only, which were registered explicitly by
917  * calling drm_connector_dynamic_register(). All other - static - connectors
918  * will be unregistered automatically by DRM core and drivers shouldn't call
919  * this function for those.
920  *
921  * Note: Existing uses of this function in drivers for static connectors
922  * should be a nop already and are scheduled to be removed.
923  */
924 void drm_connector_unregister(struct drm_connector *connector)
925 {
926 	mutex_lock(&connector->mutex);
927 	if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
928 		mutex_unlock(&connector->mutex);
929 		return;
930 	}
931 
932 	mutex_lock(&connector_list_lock);
933 	list_del_init(&connector->global_connector_list_entry);
934 	mutex_unlock(&connector_list_lock);
935 
936 	if (connector->privacy_screen)
937 		drm_privacy_screen_unregister_notifier(
938 					connector->privacy_screen,
939 					&connector->privacy_screen_notifier);
940 
941 	drm_sysfs_connector_remove_early(connector);
942 
943 	if (connector->funcs->early_unregister)
944 		connector->funcs->early_unregister(connector);
945 
946 	drm_debugfs_connector_remove(connector);
947 	drm_sysfs_connector_remove(connector);
948 
949 	connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
950 	mutex_unlock(&connector->mutex);
951 }
952 EXPORT_SYMBOL(drm_connector_unregister);
953 
954 void drm_connector_unregister_all(struct drm_device *dev)
955 {
956 	struct drm_connector *connector;
957 	struct drm_connector_list_iter conn_iter;
958 
959 	drm_connector_list_iter_begin(dev, &conn_iter);
960 	drm_for_each_connector_iter(connector, &conn_iter)
961 		drm_connector_unregister(connector);
962 	drm_connector_list_iter_end(&conn_iter);
963 }
964 
965 int drm_connector_register_all(struct drm_device *dev)
966 {
967 	struct drm_connector *connector;
968 	struct drm_connector_list_iter conn_iter;
969 	int ret = 0;
970 
971 	drm_connector_list_iter_begin(dev, &conn_iter);
972 	drm_for_each_connector_iter(connector, &conn_iter) {
973 		ret = drm_connector_register(connector);
974 		if (ret)
975 			break;
976 	}
977 	drm_connector_list_iter_end(&conn_iter);
978 
979 	if (ret)
980 		drm_connector_unregister_all(dev);
981 	return ret;
982 }
983 
984 /**
985  * drm_get_connector_status_name - return a string for connector status
986  * @status: connector status to compute name of
987  *
988  * In contrast to the other drm_get_*_name functions this one here returns a
989  * const pointer and hence is threadsafe.
990  *
991  * Returns: connector status string
992  */
993 const char *drm_get_connector_status_name(enum drm_connector_status status)
994 {
995 	if (status == connector_status_connected)
996 		return "connected";
997 	else if (status == connector_status_disconnected)
998 		return "disconnected";
999 	else
1000 		return "unknown";
1001 }
1002 EXPORT_SYMBOL(drm_get_connector_status_name);
1003 
1004 /**
1005  * drm_get_connector_force_name - return a string for connector force
1006  * @force: connector force to get name of
1007  *
1008  * Returns: const pointer to name.
1009  */
1010 const char *drm_get_connector_force_name(enum drm_connector_force force)
1011 {
1012 	switch (force) {
1013 	case DRM_FORCE_UNSPECIFIED:
1014 		return "unspecified";
1015 	case DRM_FORCE_OFF:
1016 		return "off";
1017 	case DRM_FORCE_ON:
1018 		return "on";
1019 	case DRM_FORCE_ON_DIGITAL:
1020 		return "digital";
1021 	default:
1022 		return "unknown";
1023 	}
1024 }
1025 
1026 #ifdef CONFIG_LOCKDEP
1027 static struct lockdep_map connector_list_iter_dep_map = {
1028 	.name = "drm_connector_list_iter"
1029 };
1030 #endif
1031 
1032 /**
1033  * drm_connector_list_iter_begin - initialize a connector_list iterator
1034  * @dev: DRM device
1035  * @iter: connector_list iterator
1036  *
1037  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
1038  * must always be cleaned up again by calling drm_connector_list_iter_end().
1039  * Iteration itself happens using drm_connector_list_iter_next() or
1040  * drm_for_each_connector_iter().
1041  */
1042 void drm_connector_list_iter_begin(struct drm_device *dev,
1043 				   struct drm_connector_list_iter *iter)
1044 {
1045 	iter->dev = dev;
1046 	iter->conn = NULL;
1047 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
1048 }
1049 EXPORT_SYMBOL(drm_connector_list_iter_begin);
1050 
1051 /*
1052  * Extra-safe connector put function that works in any context. Should only be
1053  * used from the connector_iter functions, where we never really expect to
1054  * actually release the connector when dropping our final reference.
1055  */
1056 static void
1057 __drm_connector_put_safe(struct drm_connector *conn)
1058 {
1059 	struct drm_mode_config *config = &conn->dev->mode_config;
1060 
1061 	lockdep_assert_held(&config->connector_list_lock);
1062 
1063 	if (!refcount_dec_and_test(&conn->base.refcount.refcount))
1064 		return;
1065 
1066 	llist_add(&conn->free_node, &config->connector_free_list);
1067 	schedule_work(&config->connector_free_work);
1068 }
1069 
1070 /**
1071  * drm_connector_list_iter_next - return next connector
1072  * @iter: connector_list iterator
1073  *
1074  * Returns: the next connector for @iter, or NULL when the list walk has
1075  * completed.
1076  */
1077 struct drm_connector *
1078 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
1079 {
1080 	struct drm_connector *old_conn = iter->conn;
1081 	struct drm_mode_config *config = &iter->dev->mode_config;
1082 	struct list_head *lhead;
1083 	unsigned long flags;
1084 
1085 	spin_lock_irqsave(&config->connector_list_lock, flags);
1086 	lhead = old_conn ? &old_conn->head : &config->connector_list;
1087 
1088 	do {
1089 		if (lhead->next == &config->connector_list) {
1090 			iter->conn = NULL;
1091 			break;
1092 		}
1093 
1094 		lhead = lhead->next;
1095 		iter->conn = list_entry(lhead, struct drm_connector, head);
1096 
1097 		/* loop until it's not a zombie connector */
1098 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
1099 
1100 	if (old_conn)
1101 		__drm_connector_put_safe(old_conn);
1102 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
1103 
1104 	return iter->conn;
1105 }
1106 EXPORT_SYMBOL(drm_connector_list_iter_next);
1107 
1108 /**
1109  * drm_connector_list_iter_end - tear down a connector_list iterator
1110  * @iter: connector_list iterator
1111  *
1112  * Tears down @iter and releases any resources (like &drm_connector references)
1113  * acquired while walking the list. This must always be called, both when the
1114  * iteration completes fully or when it was aborted without walking the entire
1115  * list.
1116  */
1117 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
1118 {
1119 	struct drm_mode_config *config = &iter->dev->mode_config;
1120 	unsigned long flags;
1121 
1122 	iter->dev = NULL;
1123 	if (iter->conn) {
1124 		spin_lock_irqsave(&config->connector_list_lock, flags);
1125 		__drm_connector_put_safe(iter->conn);
1126 		spin_unlock_irqrestore(&config->connector_list_lock, flags);
1127 	}
1128 	lock_release(&connector_list_iter_dep_map, _RET_IP_);
1129 }
1130 EXPORT_SYMBOL(drm_connector_list_iter_end);
1131 
1132 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
1133 	{ SubPixelUnknown, "Unknown" },
1134 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
1135 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
1136 	{ SubPixelVerticalRGB, "Vertical RGB" },
1137 	{ SubPixelVerticalBGR, "Vertical BGR" },
1138 	{ SubPixelNone, "None" },
1139 };
1140 
1141 /**
1142  * drm_get_subpixel_order_name - return a string for a given subpixel enum
1143  * @order: enum of subpixel_order
1144  *
1145  * Note you could abuse this and return something out of bounds, but that
1146  * would be a caller error.  No unscrubbed user data should make it here.
1147  *
1148  * Returns: string describing an enumerated subpixel property
1149  */
1150 const char *drm_get_subpixel_order_name(enum subpixel_order order)
1151 {
1152 	return drm_subpixel_enum_list[order].name;
1153 }
1154 EXPORT_SYMBOL(drm_get_subpixel_order_name);
1155 
1156 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
1157 	{ DRM_MODE_DPMS_ON, "On" },
1158 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
1159 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
1160 	{ DRM_MODE_DPMS_OFF, "Off" }
1161 };
1162 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
1163 
1164 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
1165 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
1166 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
1167 };
1168 
1169 /**
1170  * drm_display_info_set_bus_formats - set the supported bus formats
1171  * @info: display info to store bus formats in
1172  * @formats: array containing the supported bus formats
1173  * @num_formats: the number of entries in the fmts array
1174  *
1175  * Store the supported bus formats in display info structure.
1176  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
1177  * a full list of available formats.
1178  *
1179  * Returns:
1180  * 0 on success or a negative error code on failure.
1181  */
1182 int drm_display_info_set_bus_formats(struct drm_display_info *info,
1183 				     const u32 *formats,
1184 				     unsigned int num_formats)
1185 {
1186 	u32 *fmts = NULL;
1187 
1188 	if (!formats && num_formats)
1189 		return -EINVAL;
1190 
1191 	if (formats && num_formats) {
1192 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
1193 			       GFP_KERNEL);
1194 		if (!fmts)
1195 			return -ENOMEM;
1196 	}
1197 
1198 	kfree(info->bus_formats);
1199 	info->bus_formats = fmts;
1200 	info->num_bus_formats = num_formats;
1201 
1202 	return 0;
1203 }
1204 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
1205 
1206 /* Optional connector properties. */
1207 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
1208 	{ DRM_MODE_SCALE_NONE, "None" },
1209 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
1210 	{ DRM_MODE_SCALE_CENTER, "Center" },
1211 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
1212 };
1213 
1214 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
1215 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
1216 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
1217 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
1218 };
1219 
1220 static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
1221 	{ DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
1222 	{ DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
1223 	{ DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
1224 	{ DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
1225 	{ DRM_MODE_CONTENT_TYPE_GAME, "Game" },
1226 };
1227 
1228 static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
1229 	{ DRM_MODE_PANEL_ORIENTATION_NORMAL,	"Normal"	},
1230 	{ DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP,	"Upside Down"	},
1231 	{ DRM_MODE_PANEL_ORIENTATION_LEFT_UP,	"Left Side Up"	},
1232 	{ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,	"Right Side Up"	},
1233 };
1234 
1235 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
1236 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1237 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
1238 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
1239 };
1240 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
1241 
1242 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
1243 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
1244 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
1245 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
1246 };
1247 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
1248 		 drm_dvi_i_subconnector_enum_list)
1249 
1250 static const struct drm_prop_enum_list drm_tv_mode_enum_list[] = {
1251 	{ DRM_MODE_TV_MODE_NTSC, "NTSC" },
1252 	{ DRM_MODE_TV_MODE_NTSC_443, "NTSC-443" },
1253 	{ DRM_MODE_TV_MODE_NTSC_J, "NTSC-J" },
1254 	{ DRM_MODE_TV_MODE_PAL, "PAL" },
1255 	{ DRM_MODE_TV_MODE_PAL_M, "PAL-M" },
1256 	{ DRM_MODE_TV_MODE_PAL_N, "PAL-N" },
1257 	{ DRM_MODE_TV_MODE_SECAM, "SECAM" },
1258 	{ DRM_MODE_TV_MODE_MONOCHROME, "Mono" },
1259 };
1260 DRM_ENUM_NAME_FN(drm_get_tv_mode_name, drm_tv_mode_enum_list)
1261 
1262 /**
1263  * drm_get_tv_mode_from_name - Translates a TV mode name into its enum value
1264  * @name: TV Mode name we want to convert
1265  * @len: Length of @name
1266  *
1267  * Translates @name into an enum drm_connector_tv_mode.
1268  *
1269  * Returns: the enum value on success, a negative errno otherwise.
1270  */
1271 int drm_get_tv_mode_from_name(const char *name, size_t len)
1272 {
1273 	unsigned int i;
1274 
1275 	for (i = 0; i < ARRAY_SIZE(drm_tv_mode_enum_list); i++) {
1276 		const struct drm_prop_enum_list *item = &drm_tv_mode_enum_list[i];
1277 
1278 		if (strlen(item->name) == len && !strncmp(item->name, name, len))
1279 			return item->type;
1280 	}
1281 
1282 	return -EINVAL;
1283 }
1284 EXPORT_SYMBOL(drm_get_tv_mode_from_name);
1285 
1286 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
1287 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1288 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1289 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1290 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1291 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1292 };
1293 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
1294 
1295 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
1296 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
1297 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1298 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1299 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1300 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1301 };
1302 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
1303 		 drm_tv_subconnector_enum_list)
1304 
1305 static const struct drm_prop_enum_list drm_dp_subconnector_enum_list[] = {
1306 	{ DRM_MODE_SUBCONNECTOR_Unknown,     "Unknown"   }, /* DVI-I, TV-out and DP */
1307 	{ DRM_MODE_SUBCONNECTOR_VGA,	     "VGA"       }, /* DP */
1308 	{ DRM_MODE_SUBCONNECTOR_DVID,	     "DVI-D"     }, /* DP */
1309 	{ DRM_MODE_SUBCONNECTOR_HDMIA,	     "HDMI"      }, /* DP */
1310 	{ DRM_MODE_SUBCONNECTOR_DisplayPort, "DP"        }, /* DP */
1311 	{ DRM_MODE_SUBCONNECTOR_Wireless,    "Wireless"  }, /* DP */
1312 	{ DRM_MODE_SUBCONNECTOR_Native,	     "Native"    }, /* DP */
1313 };
1314 
1315 DRM_ENUM_NAME_FN(drm_get_dp_subconnector_name,
1316 		 drm_dp_subconnector_enum_list)
1317 
1318 
1319 static const char * const colorspace_names[] = {
1320 	/* For Default case, driver will set the colorspace */
1321 	[DRM_MODE_COLORIMETRY_DEFAULT] = "Default",
1322 	/* Standard Definition Colorimetry based on CEA 861 */
1323 	[DRM_MODE_COLORIMETRY_SMPTE_170M_YCC] = "SMPTE_170M_YCC",
1324 	[DRM_MODE_COLORIMETRY_BT709_YCC] = "BT709_YCC",
1325 	/* Standard Definition Colorimetry based on IEC 61966-2-4 */
1326 	[DRM_MODE_COLORIMETRY_XVYCC_601] = "XVYCC_601",
1327 	/* High Definition Colorimetry based on IEC 61966-2-4 */
1328 	[DRM_MODE_COLORIMETRY_XVYCC_709] = "XVYCC_709",
1329 	/* Colorimetry based on IEC 61966-2-1/Amendment 1 */
1330 	[DRM_MODE_COLORIMETRY_SYCC_601] = "SYCC_601",
1331 	/* Colorimetry based on IEC 61966-2-5 [33] */
1332 	[DRM_MODE_COLORIMETRY_OPYCC_601] = "opYCC_601",
1333 	/* Colorimetry based on IEC 61966-2-5 */
1334 	[DRM_MODE_COLORIMETRY_OPRGB] = "opRGB",
1335 	/* Colorimetry based on ITU-R BT.2020 */
1336 	[DRM_MODE_COLORIMETRY_BT2020_CYCC] = "BT2020_CYCC",
1337 	/* Colorimetry based on ITU-R BT.2020 */
1338 	[DRM_MODE_COLORIMETRY_BT2020_RGB] = "BT2020_RGB",
1339 	/* Colorimetry based on ITU-R BT.2020 */
1340 	[DRM_MODE_COLORIMETRY_BT2020_YCC] = "BT2020_YCC",
1341 	/* Added as part of Additional Colorimetry Extension in 861.G */
1342 	[DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65] = "DCI-P3_RGB_D65",
1343 	[DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER] = "DCI-P3_RGB_Theater",
1344 	[DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED] = "RGB_WIDE_FIXED",
1345 	/* Colorimetry based on scRGB (IEC 61966-2-2) */
1346 	[DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT] = "RGB_WIDE_FLOAT",
1347 	[DRM_MODE_COLORIMETRY_BT601_YCC] = "BT601_YCC",
1348 };
1349 
1350 /**
1351  * drm_get_colorspace_name - return a string for color encoding
1352  * @colorspace: color space to compute name of
1353  *
1354  * In contrast to the other drm_get_*_name functions this one here returns a
1355  * const pointer and hence is threadsafe.
1356  */
1357 const char *drm_get_colorspace_name(enum drm_colorspace colorspace)
1358 {
1359 	if (colorspace < ARRAY_SIZE(colorspace_names) && colorspace_names[colorspace])
1360 		return colorspace_names[colorspace];
1361 	else
1362 		return "(null)";
1363 }
1364 
1365 static const u32 hdmi_colorspaces =
1366 	BIT(DRM_MODE_COLORIMETRY_SMPTE_170M_YCC) |
1367 	BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
1368 	BIT(DRM_MODE_COLORIMETRY_XVYCC_601) |
1369 	BIT(DRM_MODE_COLORIMETRY_XVYCC_709) |
1370 	BIT(DRM_MODE_COLORIMETRY_SYCC_601) |
1371 	BIT(DRM_MODE_COLORIMETRY_OPYCC_601) |
1372 	BIT(DRM_MODE_COLORIMETRY_OPRGB) |
1373 	BIT(DRM_MODE_COLORIMETRY_BT2020_CYCC) |
1374 	BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
1375 	BIT(DRM_MODE_COLORIMETRY_BT2020_YCC) |
1376 	BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65) |
1377 	BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER);
1378 
1379 /*
1380  * As per DP 1.4a spec, 2.2.5.7.5 VSC SDP Payload for Pixel Encoding/Colorimetry
1381  * Format Table 2-120
1382  */
1383 static const u32 dp_colorspaces =
1384 	BIT(DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED) |
1385 	BIT(DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT) |
1386 	BIT(DRM_MODE_COLORIMETRY_OPRGB) |
1387 	BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65) |
1388 	BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
1389 	BIT(DRM_MODE_COLORIMETRY_BT601_YCC) |
1390 	BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
1391 	BIT(DRM_MODE_COLORIMETRY_XVYCC_601) |
1392 	BIT(DRM_MODE_COLORIMETRY_XVYCC_709) |
1393 	BIT(DRM_MODE_COLORIMETRY_SYCC_601) |
1394 	BIT(DRM_MODE_COLORIMETRY_OPYCC_601) |
1395 	BIT(DRM_MODE_COLORIMETRY_BT2020_CYCC) |
1396 	BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
1397 
1398 static const struct drm_prop_enum_list broadcast_rgb_names[] = {
1399 	{ DRM_HDMI_BROADCAST_RGB_AUTO, "Automatic" },
1400 	{ DRM_HDMI_BROADCAST_RGB_FULL, "Full" },
1401 	{ DRM_HDMI_BROADCAST_RGB_LIMITED, "Limited 16:235" },
1402 };
1403 
1404 /*
1405  * drm_hdmi_connector_get_broadcast_rgb_name - Return a string for HDMI connector RGB broadcast selection
1406  * @broadcast_rgb: Broadcast RGB selection to compute name of
1407  *
1408  * Returns: the name of the Broadcast RGB selection, or NULL if the type
1409  * is not valid.
1410  */
1411 const char *
1412 drm_hdmi_connector_get_broadcast_rgb_name(enum drm_hdmi_broadcast_rgb broadcast_rgb)
1413 {
1414 	if (broadcast_rgb >= ARRAY_SIZE(broadcast_rgb_names))
1415 		return NULL;
1416 
1417 	return broadcast_rgb_names[broadcast_rgb].name;
1418 }
1419 EXPORT_SYMBOL(drm_hdmi_connector_get_broadcast_rgb_name);
1420 
1421 static const char * const output_format_str[] = {
1422 	[HDMI_COLORSPACE_RGB]		= "RGB",
1423 	[HDMI_COLORSPACE_YUV420]	= "YUV 4:2:0",
1424 	[HDMI_COLORSPACE_YUV422]	= "YUV 4:2:2",
1425 	[HDMI_COLORSPACE_YUV444]	= "YUV 4:4:4",
1426 };
1427 
1428 /*
1429  * drm_hdmi_connector_get_output_format_name() - Return a string for HDMI connector output format
1430  * @fmt: Output format to compute name of
1431  *
1432  * Returns: the name of the output format, or NULL if the type is not
1433  * valid.
1434  */
1435 const char *
1436 drm_hdmi_connector_get_output_format_name(enum hdmi_colorspace fmt)
1437 {
1438 	if (fmt >= ARRAY_SIZE(output_format_str))
1439 		return NULL;
1440 
1441 	return output_format_str[fmt];
1442 }
1443 EXPORT_SYMBOL(drm_hdmi_connector_get_output_format_name);
1444 
1445 /**
1446  * DOC: standard connector properties
1447  *
1448  * DRM connectors have a few standardized properties:
1449  *
1450  * EDID:
1451  * 	Blob property which contains the current EDID read from the sink. This
1452  * 	is useful to parse sink identification information like vendor, model
1453  * 	and serial. Drivers should update this property by calling
1454  * 	drm_connector_update_edid_property(), usually after having parsed
1455  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
1456  * 	property.
1457  *
1458  * 	User-space should not parse the EDID to obtain information exposed via
1459  * 	other KMS properties (because the kernel might apply limits, quirks or
1460  * 	fixups to the EDID). For instance, user-space should not try to parse
1461  * 	mode lists from the EDID.
1462  * DPMS:
1463  * 	Legacy property for setting the power state of the connector. For atomic
1464  * 	drivers this is only provided for backwards compatibility with existing
1465  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
1466  * 	connector is linked to. Drivers should never set this property directly,
1467  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
1468  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
1469  * 	implemented in the DRM core.
1470  *
1471  * 	On atomic drivers any DPMS setproperty ioctl where the value does not
1472  * 	change is completely skipped, otherwise a full atomic commit will occur.
1473  * 	On legacy drivers the exact behavior is driver specific.
1474  *
1475  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
1476  * 	userspace must use "ACTIVE" on the CRTC instead.
1477  *
1478  * 	WARNING:
1479  *
1480  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
1481  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
1482  * 	returned by the GETCONNECTOR actually reflecting reality, because many
1483  * 	drivers fail to update it. For atomic drivers this is taken care of in
1484  * 	drm_atomic_helper_update_legacy_modeset_state().
1485  *
1486  * 	The second issue is that the DPMS state is only well-defined when the
1487  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
1488  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
1489  *
1490  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
1491  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
1492  * 	the software value on legacy drivers.
1493  *
1494  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
1495  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
1496  * 	never read back the value of "DPMS" because it can be incorrect.
1497  * PATH:
1498  * 	Connector path property to identify how this sink is physically
1499  * 	connected. Used by DP MST. This should be set by calling
1500  * 	drm_connector_set_path_property(), in the case of DP MST with the
1501  * 	path property the MST manager created. Userspace cannot change this
1502  * 	property.
1503  *
1504  * 	In the case of DP MST, the property has the format
1505  * 	``mst:<parent>-<ports>`` where ``<parent>`` is the KMS object ID of the
1506  * 	parent connector and ``<ports>`` is a hyphen-separated list of DP MST
1507  * 	port numbers. Note, KMS object IDs are not guaranteed to be stable
1508  * 	across reboots.
1509  * TILE:
1510  * 	Connector tile group property to indicate how a set of DRM connector
1511  * 	compose together into one logical screen. This is used by both high-res
1512  * 	external screens (often only using a single cable, but exposing multiple
1513  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
1514  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
1515  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
1516  * 	tiling and virtualise both &drm_crtc and &drm_plane if needed. Drivers
1517  * 	should update this value using drm_connector_set_tile_property().
1518  * 	Userspace cannot change this property.
1519  * link-status:
1520  *      Connector link-status property to indicate the status of link. The
1521  *      default value of link-status is "GOOD". If something fails during or
1522  *      after modeset, the kernel driver may set this to "BAD" and issue a
1523  *      hotplug uevent. Drivers should update this value using
1524  *      drm_connector_set_link_status_property().
1525  *
1526  *      When user-space receives the hotplug uevent and detects a "BAD"
1527  *      link-status, the sink doesn't receive pixels anymore (e.g. the screen
1528  *      becomes completely black). The list of available modes may have
1529  *      changed. User-space is expected to pick a new mode if the current one
1530  *      has disappeared and perform a new modeset with link-status set to
1531  *      "GOOD" to re-enable the connector.
1532  *
1533  *      If multiple connectors share the same CRTC and one of them gets a "BAD"
1534  *      link-status, the other are unaffected (ie. the sinks still continue to
1535  *      receive pixels).
1536  *
1537  *      When user-space performs an atomic commit on a connector with a "BAD"
1538  *      link-status without resetting the property to "GOOD", the sink may
1539  *      still not receive pixels. When user-space performs an atomic commit
1540  *      which resets the link-status property to "GOOD" without the
1541  *      ALLOW_MODESET flag set, it might fail because a modeset is required.
1542  *
1543  *      User-space can only change link-status to "GOOD", changing it to "BAD"
1544  *      is a no-op.
1545  *
1546  *      For backwards compatibility with non-atomic userspace the kernel
1547  *      tries to automatically set the link-status back to "GOOD" in the
1548  *      SETCRTC IOCTL. This might fail if the mode is no longer valid, similar
1549  *      to how it might fail if a different screen has been connected in the
1550  *      interim.
1551  * non_desktop:
1552  * 	Indicates the output should be ignored for purposes of displaying a
1553  * 	standard desktop environment or console. This is most likely because
1554  * 	the output device is not rectilinear.
1555  * Content Protection:
1556  *	This property is used by userspace to request the kernel protect future
1557  *	content communicated over the link. When requested, kernel will apply
1558  *	the appropriate means of protection (most often HDCP), and use the
1559  *	property to tell userspace the protection is active.
1560  *
1561  *	Drivers can set this up by calling
1562  *	drm_connector_attach_content_protection_property() on initialization.
1563  *
1564  *	The value of this property can be one of the following:
1565  *
1566  *	DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
1567  *		The link is not protected, content is transmitted in the clear.
1568  *	DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
1569  *		Userspace has requested content protection, but the link is not
1570  *		currently protected. When in this state, kernel should enable
1571  *		Content Protection as soon as possible.
1572  *	DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
1573  *		Userspace has requested content protection, and the link is
1574  *		protected. Only the driver can set the property to this value.
1575  *		If userspace attempts to set to ENABLED, kernel will return
1576  *		-EINVAL.
1577  *
1578  *	A few guidelines:
1579  *
1580  *	- DESIRED state should be preserved until userspace de-asserts it by
1581  *	  setting the property to UNDESIRED. This means ENABLED should only
1582  *	  transition to UNDESIRED when the user explicitly requests it.
1583  *	- If the state is DESIRED, kernel should attempt to re-authenticate the
1584  *	  link whenever possible. This includes across disable/enable, dpms,
1585  *	  hotplug, downstream device changes, link status failures, etc..
1586  *	- Kernel sends uevent with the connector id and property id through
1587  *	  @drm_hdcp_update_content_protection, upon below kernel triggered
1588  *	  scenarios:
1589  *
1590  *		- DESIRED -> ENABLED (authentication success)
1591  *		- ENABLED -> DESIRED (termination of authentication)
1592  *	- Please note no uevents for userspace triggered property state changes,
1593  *	  which can't fail such as
1594  *
1595  *		- DESIRED/ENABLED -> UNDESIRED
1596  *		- UNDESIRED -> DESIRED
1597  *	- Userspace is responsible for polling the property or listen to uevents
1598  *	  to determine when the value transitions from ENABLED to DESIRED.
1599  *	  This signifies the link is no longer protected and userspace should
1600  *	  take appropriate action (whatever that might be).
1601  *
1602  * HDCP Content Type:
1603  *	This Enum property is used by the userspace to declare the content type
1604  *	of the display stream, to kernel. Here display stream stands for any
1605  *	display content that userspace intended to display through HDCP
1606  *	encryption.
1607  *
1608  *	Content Type of a stream is decided by the owner of the stream, as
1609  *	"HDCP Type0" or "HDCP Type1".
1610  *
1611  *	The value of the property can be one of the below:
1612  *	  - "HDCP Type0": DRM_MODE_HDCP_CONTENT_TYPE0 = 0
1613  *	  - "HDCP Type1": DRM_MODE_HDCP_CONTENT_TYPE1 = 1
1614  *
1615  *	When kernel starts the HDCP authentication (see "Content Protection"
1616  *	for details), it uses the content type in "HDCP Content Type"
1617  *	for performing the HDCP authentication with the display sink.
1618  *
1619  *	Please note in HDCP spec versions, a link can be authenticated with
1620  *	HDCP 2.2 for Content Type 0/Content Type 1. Where as a link can be
1621  *	authenticated with HDCP1.4 only for Content Type 0(though it is implicit
1622  *	in nature. As there is no reference for Content Type in HDCP1.4).
1623  *
1624  *	HDCP2.2 authentication protocol itself takes the "Content Type" as a
1625  *	parameter, which is a input for the DP HDCP2.2 encryption algo.
1626  *
1627  *	In case of Type 0 content protection request, kernel driver can choose
1628  *	either of HDCP spec versions 1.4 and 2.2. When HDCP2.2 is used for
1629  *	"HDCP Type 0", a HDCP 2.2 capable repeater in the downstream can send
1630  *	that content to a HDCP 1.4 authenticated HDCP sink (Type0 link).
1631  *	But if the content is classified as "HDCP Type 1", above mentioned
1632  *	HDCP 2.2 repeater wont send the content to the HDCP sink as it can't
1633  *	authenticate the HDCP1.4 capable sink for "HDCP Type 1".
1634  *
1635  *	Please note userspace can be ignorant of the HDCP versions used by the
1636  *	kernel driver to achieve the "HDCP Content Type".
1637  *
1638  *	At current scenario, classifying a content as Type 1 ensures that the
1639  *	content will be displayed only through the HDCP2.2 encrypted link.
1640  *
1641  *	Note that the HDCP Content Type property is introduced at HDCP 2.2, and
1642  *	defaults to type 0. It is only exposed by drivers supporting HDCP 2.2
1643  *	(hence supporting Type 0 and Type 1). Based on how next versions of
1644  *	HDCP specs are defined content Type could be used for higher versions
1645  *	too.
1646  *
1647  *	If content type is changed when "Content Protection" is not UNDESIRED,
1648  *	then kernel will disable the HDCP and re-enable with new type in the
1649  *	same atomic commit. And when "Content Protection" is ENABLED, it means
1650  *	that link is HDCP authenticated and encrypted, for the transmission of
1651  *	the Type of stream mentioned at "HDCP Content Type".
1652  *
1653  * HDR_OUTPUT_METADATA:
1654  *	Connector property to enable userspace to send HDR Metadata to
1655  *	driver. This metadata is based on the composition and blending
1656  *	policies decided by user, taking into account the hardware and
1657  *	sink capabilities. The driver gets this metadata and creates a
1658  *	Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
1659  *	SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
1660  *	sent to sink. This notifies the sink of the upcoming frame's Color
1661  *	Encoding and Luminance parameters.
1662  *
1663  *	Userspace first need to detect the HDR capabilities of sink by
1664  *	reading and parsing the EDID. Details of HDR metadata for HDMI
1665  *	are added in CTA 861.G spec. For DP , its defined in VESA DP
1666  *	Standard v1.4. It needs to then get the metadata information
1667  *	of the video/game/app content which are encoded in HDR (basically
1668  *	using HDR transfer functions). With this information it needs to
1669  *	decide on a blending policy and compose the relevant
1670  *	layers/overlays into a common format. Once this blending is done,
1671  *	userspace will be aware of the metadata of the composed frame to
1672  *	be send to sink. It then uses this property to communicate this
1673  *	metadata to driver which then make a Infoframe packet and sends
1674  *	to sink based on the type of encoder connected.
1675  *
1676  *	Userspace will be responsible to do Tone mapping operation in case:
1677  *		- Some layers are HDR and others are SDR
1678  *		- HDR layers luminance is not same as sink
1679  *
1680  *	It will even need to do colorspace conversion and get all layers
1681  *	to one common colorspace for blending. It can use either GL, Media
1682  *	or display engine to get this done based on the capabilities of the
1683  *	associated hardware.
1684  *
1685  *	Driver expects metadata to be put in &struct hdr_output_metadata
1686  *	structure from userspace. This is received as blob and stored in
1687  *	&drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1688  *	sink metadata in &struct hdr_sink_metadata, as
1689  *	&drm_connector.hdr_sink_metadata.  Driver uses
1690  *	drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1691  *	hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1692  *	HDMI encoder.
1693  *
1694  * max bpc:
1695  *	This range property is used by userspace to limit the bit depth. When
1696  *	used the driver would limit the bpc in accordance with the valid range
1697  *	supported by the hardware and sink. Drivers to use the function
1698  *	drm_connector_attach_max_bpc_property() to create and attach the
1699  *	property to the connector during initialization.
1700  *
1701  * Connectors also have one standardized atomic property:
1702  *
1703  * CRTC_ID:
1704  * 	Mode object ID of the &drm_crtc this connector should be connected to.
1705  *
1706  * Connectors for LCD panels may also have one standardized property:
1707  *
1708  * panel orientation:
1709  *	On some devices the LCD panel is mounted in the casing in such a way
1710  *	that the up/top side of the panel does not match with the top side of
1711  *	the device. Userspace can use this property to check for this.
1712  *	Note that input coordinates from touchscreens (input devices with
1713  *	INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
1714  *	coordinates, so if userspace rotates the picture to adjust for
1715  *	the orientation it must also apply the same transformation to the
1716  *	touchscreen input coordinates. This property is initialized by calling
1717  *	drm_connector_set_panel_orientation() or
1718  *	drm_connector_set_panel_orientation_with_quirk()
1719  *
1720  * scaling mode:
1721  *	This property defines how a non-native mode is upscaled to the native
1722  *	mode of an LCD panel:
1723  *
1724  *	None:
1725  *		No upscaling happens, scaling is left to the panel. Not all
1726  *		drivers expose this mode.
1727  *	Full:
1728  *		The output is upscaled to the full resolution of the panel,
1729  *		ignoring the aspect ratio.
1730  *	Center:
1731  *		No upscaling happens, the output is centered within the native
1732  *		resolution the panel.
1733  *	Full aspect:
1734  *		The output is upscaled to maximize either the width or height
1735  *		while retaining the aspect ratio.
1736  *
1737  *	This property should be set up by calling
1738  *	drm_connector_attach_scaling_mode_property(). Note that drivers
1739  *	can also expose this property to external outputs, in which case they
1740  *	must support "None", which should be the default (since external screens
1741  *	have a built-in scaler).
1742  *
1743  * subconnector:
1744  *	This property is used by DVI-I, TVout and DisplayPort to indicate different
1745  *	connector subtypes. Enum values more or less match with those from main
1746  *	connector types.
1747  *	For DVI-I and TVout there is also a matching property "select subconnector"
1748  *	allowing to switch between signal types.
1749  *	DP subconnector corresponds to a downstream port.
1750  *
1751  * privacy-screen sw-state, privacy-screen hw-state:
1752  *	These 2 optional properties can be used to query the state of the
1753  *	electronic privacy screen that is available on some displays; and in
1754  *	some cases also control the state. If a driver implements these
1755  *	properties then both properties must be present.
1756  *
1757  *	"privacy-screen hw-state" is read-only and reflects the actual state
1758  *	of the privacy-screen, possible values: "Enabled", "Disabled,
1759  *	"Enabled-locked", "Disabled-locked". The locked states indicate
1760  *	that the state cannot be changed through the DRM API. E.g. there
1761  *	might be devices where the firmware-setup options, or a hardware
1762  *	slider-switch, offer always on / off modes.
1763  *
1764  *	"privacy-screen sw-state" can be set to change the privacy-screen state
1765  *	when not locked. In this case the driver must update the hw-state
1766  *	property to reflect the new state on completion of the commit of the
1767  *	sw-state property. Setting the sw-state property when the hw-state is
1768  *	locked must be interpreted by the driver as a request to change the
1769  *	state to the set state when the hw-state becomes unlocked. E.g. if
1770  *	"privacy-screen hw-state" is "Enabled-locked" and the sw-state
1771  *	gets set to "Disabled" followed by the user unlocking the state by
1772  *	changing the slider-switch position, then the driver must set the
1773  *	state to "Disabled" upon receiving the unlock event.
1774  *
1775  *	In some cases the privacy-screen's actual state might change outside of
1776  *	control of the DRM code. E.g. there might be a firmware handled hotkey
1777  *	which toggles the actual state, or the actual state might be changed
1778  *	through another userspace API such as writing /proc/acpi/ibm/lcdshadow.
1779  *	In this case the driver must update both the hw-state and the sw-state
1780  *	to reflect the new value, overwriting any pending state requests in the
1781  *	sw-state. Any pending sw-state requests are thus discarded.
1782  *
1783  *	Note that the ability for the state to change outside of control of
1784  *	the DRM master process means that userspace must not cache the value
1785  *	of the sw-state. Caching the sw-state value and including it in later
1786  *	atomic commits may lead to overriding a state change done through e.g.
1787  *	a firmware handled hotkey. Therefor userspace must not include the
1788  *	privacy-screen sw-state in an atomic commit unless it wants to change
1789  *	its value.
1790  *
1791  * left margin, right margin, top margin, bottom margin:
1792  *	Add margins to the connector's viewport. This is typically used to
1793  *	mitigate overscan on TVs.
1794  *
1795  *	The value is the size in pixels of the black border which will be
1796  *	added. The attached CRTC's content will be scaled to fill the whole
1797  *	area inside the margin.
1798  *
1799  *	The margins configuration might be sent to the sink, e.g. via HDMI AVI
1800  *	InfoFrames.
1801  *
1802  *	Drivers can set up these properties by calling
1803  *	drm_mode_create_tv_margin_properties().
1804  */
1805 
1806 int drm_connector_create_standard_properties(struct drm_device *dev)
1807 {
1808 	struct drm_property *prop;
1809 
1810 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1811 				   DRM_MODE_PROP_IMMUTABLE,
1812 				   "EDID", 0);
1813 	if (!prop)
1814 		return -ENOMEM;
1815 	dev->mode_config.edid_property = prop;
1816 
1817 	prop = drm_property_create_enum(dev, 0,
1818 				   "DPMS", drm_dpms_enum_list,
1819 				   ARRAY_SIZE(drm_dpms_enum_list));
1820 	if (!prop)
1821 		return -ENOMEM;
1822 	dev->mode_config.dpms_property = prop;
1823 
1824 	prop = drm_property_create(dev,
1825 				   DRM_MODE_PROP_BLOB |
1826 				   DRM_MODE_PROP_IMMUTABLE,
1827 				   "PATH", 0);
1828 	if (!prop)
1829 		return -ENOMEM;
1830 	dev->mode_config.path_property = prop;
1831 
1832 	prop = drm_property_create(dev,
1833 				   DRM_MODE_PROP_BLOB |
1834 				   DRM_MODE_PROP_IMMUTABLE,
1835 				   "TILE", 0);
1836 	if (!prop)
1837 		return -ENOMEM;
1838 	dev->mode_config.tile_property = prop;
1839 
1840 	prop = drm_property_create_enum(dev, 0, "link-status",
1841 					drm_link_status_enum_list,
1842 					ARRAY_SIZE(drm_link_status_enum_list));
1843 	if (!prop)
1844 		return -ENOMEM;
1845 	dev->mode_config.link_status_property = prop;
1846 
1847 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1848 	if (!prop)
1849 		return -ENOMEM;
1850 	dev->mode_config.non_desktop_property = prop;
1851 
1852 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1853 				   "HDR_OUTPUT_METADATA", 0);
1854 	if (!prop)
1855 		return -ENOMEM;
1856 	dev->mode_config.hdr_output_metadata_property = prop;
1857 
1858 	return 0;
1859 }
1860 
1861 /**
1862  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1863  * @dev: DRM device
1864  *
1865  * Called by a driver the first time a DVI-I connector is made.
1866  *
1867  * Returns: %0
1868  */
1869 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1870 {
1871 	struct drm_property *dvi_i_selector;
1872 	struct drm_property *dvi_i_subconnector;
1873 
1874 	if (dev->mode_config.dvi_i_select_subconnector_property)
1875 		return 0;
1876 
1877 	dvi_i_selector =
1878 		drm_property_create_enum(dev, 0,
1879 				    "select subconnector",
1880 				    drm_dvi_i_select_enum_list,
1881 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
1882 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1883 
1884 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1885 				    "subconnector",
1886 				    drm_dvi_i_subconnector_enum_list,
1887 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1888 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1889 
1890 	return 0;
1891 }
1892 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1893 
1894 /**
1895  * drm_connector_attach_dp_subconnector_property - create subconnector property for DP
1896  * @connector: drm_connector to attach property
1897  *
1898  * Called by a driver when DP connector is created.
1899  */
1900 void drm_connector_attach_dp_subconnector_property(struct drm_connector *connector)
1901 {
1902 	struct drm_mode_config *mode_config = &connector->dev->mode_config;
1903 
1904 	if (!mode_config->dp_subconnector_property)
1905 		mode_config->dp_subconnector_property =
1906 			drm_property_create_enum(connector->dev,
1907 				DRM_MODE_PROP_IMMUTABLE,
1908 				"subconnector",
1909 				drm_dp_subconnector_enum_list,
1910 				ARRAY_SIZE(drm_dp_subconnector_enum_list));
1911 
1912 	drm_object_attach_property(&connector->base,
1913 				   mode_config->dp_subconnector_property,
1914 				   DRM_MODE_SUBCONNECTOR_Unknown);
1915 }
1916 EXPORT_SYMBOL(drm_connector_attach_dp_subconnector_property);
1917 
1918 /**
1919  * DOC: HDMI connector properties
1920  *
1921  * Broadcast RGB (HDMI specific)
1922  *      Indicates the Quantization Range (Full vs Limited) used. The color
1923  *      processing pipeline will be adjusted to match the value of the
1924  *      property, and the Infoframes will be generated and sent accordingly.
1925  *
1926  *      This property is only relevant if the HDMI output format is RGB. If
1927  *      it's one of the YCbCr variant, it will be ignored.
1928  *
1929  *      The CRTC attached to the connector must be configured by user-space to
1930  *      always produce full-range pixels.
1931  *
1932  *      The value of this property can be one of the following:
1933  *
1934  *      Automatic:
1935  *              The quantization range is selected automatically based on the
1936  *              mode according to the HDMI specifications (HDMI 1.4b - Section
1937  *              6.6 - Video Quantization Ranges).
1938  *
1939  *      Full:
1940  *              Full quantization range is forced.
1941  *
1942  *      Limited 16:235:
1943  *              Limited quantization range is forced. Unlike the name suggests,
1944  *              this works for any number of bits-per-component.
1945  *
1946  *      Property values other than Automatic can result in colors being off (if
1947  *      limited is selected but the display expects full), or a black screen
1948  *      (if full is selected but the display expects limited).
1949  *
1950  *      Drivers can set up this property by calling
1951  *      drm_connector_attach_broadcast_rgb_property().
1952  *
1953  * content type (HDMI specific):
1954  *	Indicates content type setting to be used in HDMI infoframes to indicate
1955  *	content type for the external device, so that it adjusts its display
1956  *	settings accordingly.
1957  *
1958  *	The value of this property can be one of the following:
1959  *
1960  *	No Data:
1961  *		Content type is unknown
1962  *	Graphics:
1963  *		Content type is graphics
1964  *	Photo:
1965  *		Content type is photo
1966  *	Cinema:
1967  *		Content type is cinema
1968  *	Game:
1969  *		Content type is game
1970  *
1971  *	The meaning of each content type is defined in CTA-861-G table 15.
1972  *
1973  *	Drivers can set up this property by calling
1974  *	drm_connector_attach_content_type_property(). Decoding to
1975  *	infoframe values is done through drm_hdmi_avi_infoframe_content_type().
1976  */
1977 
1978 /*
1979  * TODO: Document the properties:
1980  *   - brightness
1981  *   - contrast
1982  *   - flicker reduction
1983  *   - hue
1984  *   - mode
1985  *   - overscan
1986  *   - saturation
1987  *   - select subconnector
1988  */
1989 /**
1990  * DOC: Analog TV Connector Properties
1991  *
1992  * TV Mode:
1993  *	Indicates the TV Mode used on an analog TV connector. The value
1994  *	of this property can be one of the following:
1995  *
1996  *	NTSC:
1997  *		TV Mode is CCIR System M (aka 525-lines) together with
1998  *		the NTSC Color Encoding.
1999  *
2000  *	NTSC-443:
2001  *
2002  *		TV Mode is CCIR System M (aka 525-lines) together with
2003  *		the NTSC Color Encoding, but with a color subcarrier
2004  *		frequency of 4.43MHz
2005  *
2006  *	NTSC-J:
2007  *
2008  *		TV Mode is CCIR System M (aka 525-lines) together with
2009  *		the NTSC Color Encoding, but with a black level equal to
2010  *		the blanking level.
2011  *
2012  *	PAL:
2013  *
2014  *		TV Mode is CCIR System B (aka 625-lines) together with
2015  *		the PAL Color Encoding.
2016  *
2017  *	PAL-M:
2018  *
2019  *		TV Mode is CCIR System M (aka 525-lines) together with
2020  *		the PAL Color Encoding.
2021  *
2022  *	PAL-N:
2023  *
2024  *		TV Mode is CCIR System N together with the PAL Color
2025  *		Encoding, a color subcarrier frequency of 3.58MHz, the
2026  *		SECAM color space, and narrower channels than other PAL
2027  *		variants.
2028  *
2029  *	SECAM:
2030  *
2031  *		TV Mode is CCIR System B (aka 625-lines) together with
2032  *		the SECAM Color Encoding.
2033  *
2034  *	Mono:
2035  *
2036  *		Use timings appropriate to the DRM mode, including
2037  *		equalizing pulses for a 525-line or 625-line mode,
2038  *		with no pedestal or color encoding.
2039  *
2040  *	Drivers can set up this property by calling
2041  *	drm_mode_create_tv_properties().
2042  */
2043 
2044 /**
2045  * drm_connector_attach_content_type_property - attach content-type property
2046  * @connector: connector to attach content type property on.
2047  *
2048  * Called by a driver the first time a HDMI connector is made.
2049  *
2050  * Returns: %0
2051  */
2052 int drm_connector_attach_content_type_property(struct drm_connector *connector)
2053 {
2054 	if (!drm_mode_create_content_type_property(connector->dev))
2055 		drm_object_attach_property(&connector->base,
2056 					   connector->dev->mode_config.content_type_property,
2057 					   DRM_MODE_CONTENT_TYPE_NO_DATA);
2058 	return 0;
2059 }
2060 EXPORT_SYMBOL(drm_connector_attach_content_type_property);
2061 
2062 /**
2063  * drm_connector_attach_tv_margin_properties - attach TV connector margin
2064  * 	properties
2065  * @connector: DRM connector
2066  *
2067  * Called by a driver when it needs to attach TV margin props to a connector.
2068  * Typically used on SDTV and HDMI connectors.
2069  */
2070 void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
2071 {
2072 	struct drm_device *dev = connector->dev;
2073 
2074 	drm_object_attach_property(&connector->base,
2075 				   dev->mode_config.tv_left_margin_property,
2076 				   0);
2077 	drm_object_attach_property(&connector->base,
2078 				   dev->mode_config.tv_right_margin_property,
2079 				   0);
2080 	drm_object_attach_property(&connector->base,
2081 				   dev->mode_config.tv_top_margin_property,
2082 				   0);
2083 	drm_object_attach_property(&connector->base,
2084 				   dev->mode_config.tv_bottom_margin_property,
2085 				   0);
2086 }
2087 EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
2088 
2089 /**
2090  * drm_mode_create_tv_margin_properties - create TV connector margin properties
2091  * @dev: DRM device
2092  *
2093  * Called by a driver's HDMI connector initialization routine, this function
2094  * creates the TV margin properties for a given device. No need to call this
2095  * function for an SDTV connector, it's already called from
2096  * drm_mode_create_tv_properties_legacy().
2097  *
2098  * Returns:
2099  * 0 on success or a negative error code on failure.
2100  */
2101 int drm_mode_create_tv_margin_properties(struct drm_device *dev)
2102 {
2103 	if (dev->mode_config.tv_left_margin_property)
2104 		return 0;
2105 
2106 	dev->mode_config.tv_left_margin_property =
2107 		drm_property_create_range(dev, 0, "left margin", 0, 100);
2108 	if (!dev->mode_config.tv_left_margin_property)
2109 		return -ENOMEM;
2110 
2111 	dev->mode_config.tv_right_margin_property =
2112 		drm_property_create_range(dev, 0, "right margin", 0, 100);
2113 	if (!dev->mode_config.tv_right_margin_property)
2114 		return -ENOMEM;
2115 
2116 	dev->mode_config.tv_top_margin_property =
2117 		drm_property_create_range(dev, 0, "top margin", 0, 100);
2118 	if (!dev->mode_config.tv_top_margin_property)
2119 		return -ENOMEM;
2120 
2121 	dev->mode_config.tv_bottom_margin_property =
2122 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
2123 	if (!dev->mode_config.tv_bottom_margin_property)
2124 		return -ENOMEM;
2125 
2126 	return 0;
2127 }
2128 EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
2129 
2130 /**
2131  * drm_mode_create_tv_properties_legacy - create TV specific connector properties
2132  * @dev: DRM device
2133  * @num_modes: number of different TV formats (modes) supported
2134  * @modes: array of pointers to strings containing name of each format
2135  *
2136  * Called by a driver's TV initialization routine, this function creates
2137  * the TV specific connector properties for a given device.  Caller is
2138  * responsible for allocating a list of format names and passing them to
2139  * this routine.
2140  *
2141  * NOTE: This functions registers the deprecated "mode" connector
2142  * property to select the analog TV mode (ie, NTSC, PAL, etc.). New
2143  * drivers must use drm_mode_create_tv_properties() instead.
2144  *
2145  * Returns:
2146  * 0 on success or a negative error code on failure.
2147  */
2148 int drm_mode_create_tv_properties_legacy(struct drm_device *dev,
2149 					 unsigned int num_modes,
2150 					 const char * const modes[])
2151 {
2152 	struct drm_property *tv_selector;
2153 	struct drm_property *tv_subconnector;
2154 	unsigned int i;
2155 
2156 	if (dev->mode_config.tv_select_subconnector_property)
2157 		return 0;
2158 
2159 	/*
2160 	 * Basic connector properties
2161 	 */
2162 	tv_selector = drm_property_create_enum(dev, 0,
2163 					  "select subconnector",
2164 					  drm_tv_select_enum_list,
2165 					  ARRAY_SIZE(drm_tv_select_enum_list));
2166 	if (!tv_selector)
2167 		goto nomem;
2168 
2169 	dev->mode_config.tv_select_subconnector_property = tv_selector;
2170 
2171 	tv_subconnector =
2172 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2173 				    "subconnector",
2174 				    drm_tv_subconnector_enum_list,
2175 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
2176 	if (!tv_subconnector)
2177 		goto nomem;
2178 	dev->mode_config.tv_subconnector_property = tv_subconnector;
2179 
2180 	/*
2181 	 * Other, TV specific properties: margins & TV modes.
2182 	 */
2183 	if (drm_mode_create_tv_margin_properties(dev))
2184 		goto nomem;
2185 
2186 	if (num_modes) {
2187 		dev->mode_config.legacy_tv_mode_property =
2188 			drm_property_create(dev, DRM_MODE_PROP_ENUM,
2189 					    "mode", num_modes);
2190 		if (!dev->mode_config.legacy_tv_mode_property)
2191 			goto nomem;
2192 
2193 		for (i = 0; i < num_modes; i++)
2194 			drm_property_add_enum(dev->mode_config.legacy_tv_mode_property,
2195 					      i, modes[i]);
2196 	}
2197 
2198 	dev->mode_config.tv_brightness_property =
2199 		drm_property_create_range(dev, 0, "brightness", 0, 100);
2200 	if (!dev->mode_config.tv_brightness_property)
2201 		goto nomem;
2202 
2203 	dev->mode_config.tv_contrast_property =
2204 		drm_property_create_range(dev, 0, "contrast", 0, 100);
2205 	if (!dev->mode_config.tv_contrast_property)
2206 		goto nomem;
2207 
2208 	dev->mode_config.tv_flicker_reduction_property =
2209 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
2210 	if (!dev->mode_config.tv_flicker_reduction_property)
2211 		goto nomem;
2212 
2213 	dev->mode_config.tv_overscan_property =
2214 		drm_property_create_range(dev, 0, "overscan", 0, 100);
2215 	if (!dev->mode_config.tv_overscan_property)
2216 		goto nomem;
2217 
2218 	dev->mode_config.tv_saturation_property =
2219 		drm_property_create_range(dev, 0, "saturation", 0, 100);
2220 	if (!dev->mode_config.tv_saturation_property)
2221 		goto nomem;
2222 
2223 	dev->mode_config.tv_hue_property =
2224 		drm_property_create_range(dev, 0, "hue", 0, 100);
2225 	if (!dev->mode_config.tv_hue_property)
2226 		goto nomem;
2227 
2228 	return 0;
2229 nomem:
2230 	return -ENOMEM;
2231 }
2232 EXPORT_SYMBOL(drm_mode_create_tv_properties_legacy);
2233 
2234 /**
2235  * drm_mode_create_tv_properties - create TV specific connector properties
2236  * @dev: DRM device
2237  * @supported_tv_modes: Bitmask of TV modes supported (See DRM_MODE_TV_MODE_*)
2238  *
2239  * Called by a driver's TV initialization routine, this function creates
2240  * the TV specific connector properties for a given device.
2241  *
2242  * Returns:
2243  * 0 on success or a negative error code on failure.
2244  */
2245 int drm_mode_create_tv_properties(struct drm_device *dev,
2246 				  unsigned int supported_tv_modes)
2247 {
2248 	struct drm_prop_enum_list tv_mode_list[DRM_MODE_TV_MODE_MAX];
2249 	struct drm_property *tv_mode;
2250 	unsigned int i, len = 0;
2251 
2252 	if (dev->mode_config.tv_mode_property)
2253 		return 0;
2254 
2255 	for (i = 0; i < DRM_MODE_TV_MODE_MAX; i++) {
2256 		if (!(supported_tv_modes & BIT(i)))
2257 			continue;
2258 
2259 		tv_mode_list[len].type = i;
2260 		tv_mode_list[len].name = drm_get_tv_mode_name(i);
2261 		len++;
2262 	}
2263 
2264 	tv_mode = drm_property_create_enum(dev, 0, "TV mode",
2265 					   tv_mode_list, len);
2266 	if (!tv_mode)
2267 		return -ENOMEM;
2268 
2269 	dev->mode_config.tv_mode_property = tv_mode;
2270 
2271 	return drm_mode_create_tv_properties_legacy(dev, 0, NULL);
2272 }
2273 EXPORT_SYMBOL(drm_mode_create_tv_properties);
2274 
2275 /**
2276  * drm_mode_create_scaling_mode_property - create scaling mode property
2277  * @dev: DRM device
2278  *
2279  * Called by a driver the first time it's needed, must be attached to desired
2280  * connectors.
2281  *
2282  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
2283  * instead to correctly assign &drm_connector_state.scaling_mode
2284  * in the atomic state.
2285  *
2286  * Returns: %0
2287  */
2288 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
2289 {
2290 	struct drm_property *scaling_mode;
2291 
2292 	if (dev->mode_config.scaling_mode_property)
2293 		return 0;
2294 
2295 	scaling_mode =
2296 		drm_property_create_enum(dev, 0, "scaling mode",
2297 				drm_scaling_mode_enum_list,
2298 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
2299 
2300 	dev->mode_config.scaling_mode_property = scaling_mode;
2301 
2302 	return 0;
2303 }
2304 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
2305 
2306 /**
2307  * DOC: Variable refresh properties
2308  *
2309  * Variable refresh rate capable displays can dynamically adjust their
2310  * refresh rate by extending the duration of their vertical front porch
2311  * until page flip or timeout occurs. This can reduce or remove stuttering
2312  * and latency in scenarios where the page flip does not align with the
2313  * vblank interval.
2314  *
2315  * An example scenario would be an application flipping at a constant rate
2316  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
2317  * interval and the same contents will be displayed twice. This can be
2318  * observed as stuttering for content with motion.
2319  *
2320  * If variable refresh rate was active on a display that supported a
2321  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
2322  * for the example scenario. The minimum supported variable refresh rate of
2323  * 35Hz is below the page flip frequency and the vertical front porch can
2324  * be extended until the page flip occurs. The vblank interval will be
2325  * directly aligned to the page flip rate.
2326  *
2327  * Not all userspace content is suitable for use with variable refresh rate.
2328  * Large and frequent changes in vertical front porch duration may worsen
2329  * perceived stuttering for input sensitive applications.
2330  *
2331  * Panel brightness will also vary with vertical front porch duration. Some
2332  * panels may have noticeable differences in brightness between the minimum
2333  * vertical front porch duration and the maximum vertical front porch duration.
2334  * Large and frequent changes in vertical front porch duration may produce
2335  * observable flickering for such panels.
2336  *
2337  * Userspace control for variable refresh rate is supported via properties
2338  * on the &drm_connector and &drm_crtc objects.
2339  *
2340  * "vrr_capable":
2341  *	Optional &drm_connector boolean property that drivers should attach
2342  *	with drm_connector_attach_vrr_capable_property() on connectors that
2343  *	could support variable refresh rates. Drivers should update the
2344  *	property value by calling drm_connector_set_vrr_capable_property().
2345  *
2346  *	Absence of the property should indicate absence of support.
2347  *
2348  * "VRR_ENABLED":
2349  *	Default &drm_crtc boolean property that notifies the driver that the
2350  *	content on the CRTC is suitable for variable refresh rate presentation.
2351  *	The driver will take this property as a hint to enable variable
2352  *	refresh rate support if the receiver supports it, ie. if the
2353  *	"vrr_capable" property is true on the &drm_connector object. The
2354  *	vertical front porch duration will be extended until page-flip or
2355  *	timeout when enabled.
2356  *
2357  *	The minimum vertical front porch duration is defined as the vertical
2358  *	front porch duration for the current mode.
2359  *
2360  *	The maximum vertical front porch duration is greater than or equal to
2361  *	the minimum vertical front porch duration. The duration is derived
2362  *	from the minimum supported variable refresh rate for the connector.
2363  *
2364  *	The driver may place further restrictions within these minimum
2365  *	and maximum bounds.
2366  */
2367 
2368 /**
2369  * drm_connector_attach_vrr_capable_property - creates the
2370  * vrr_capable property
2371  * @connector: connector to create the vrr_capable property on.
2372  *
2373  * This is used by atomic drivers to add support for querying
2374  * variable refresh rate capability for a connector.
2375  *
2376  * Returns:
2377  * Zero on success, negative errno on failure.
2378  */
2379 int drm_connector_attach_vrr_capable_property(
2380 	struct drm_connector *connector)
2381 {
2382 	struct drm_device *dev = connector->dev;
2383 	struct drm_property *prop;
2384 
2385 	if (!connector->vrr_capable_property) {
2386 		prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
2387 			"vrr_capable");
2388 		if (!prop)
2389 			return -ENOMEM;
2390 
2391 		connector->vrr_capable_property = prop;
2392 		drm_object_attach_property(&connector->base, prop, 0);
2393 	}
2394 
2395 	return 0;
2396 }
2397 EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
2398 
2399 /**
2400  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
2401  * @connector: connector to attach scaling mode property on.
2402  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
2403  *
2404  * This is used to add support for scaling mode to atomic drivers.
2405  * The scaling mode will be set to &drm_connector_state.scaling_mode
2406  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
2407  *
2408  * This is the atomic version of drm_mode_create_scaling_mode_property().
2409  *
2410  * Returns:
2411  * Zero on success, negative errno on failure.
2412  */
2413 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
2414 					       u32 scaling_mode_mask)
2415 {
2416 	struct drm_device *dev = connector->dev;
2417 	struct drm_property *scaling_mode_property;
2418 	int i;
2419 	const unsigned valid_scaling_mode_mask =
2420 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
2421 
2422 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
2423 		    scaling_mode_mask & ~valid_scaling_mode_mask))
2424 		return -EINVAL;
2425 
2426 	scaling_mode_property =
2427 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
2428 				    hweight32(scaling_mode_mask));
2429 
2430 	if (!scaling_mode_property)
2431 		return -ENOMEM;
2432 
2433 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
2434 		int ret;
2435 
2436 		if (!(BIT(i) & scaling_mode_mask))
2437 			continue;
2438 
2439 		ret = drm_property_add_enum(scaling_mode_property,
2440 					    drm_scaling_mode_enum_list[i].type,
2441 					    drm_scaling_mode_enum_list[i].name);
2442 
2443 		if (ret) {
2444 			drm_property_destroy(dev, scaling_mode_property);
2445 
2446 			return ret;
2447 		}
2448 	}
2449 
2450 	drm_object_attach_property(&connector->base,
2451 				   scaling_mode_property, 0);
2452 
2453 	connector->scaling_mode_property = scaling_mode_property;
2454 
2455 	return 0;
2456 }
2457 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
2458 
2459 /**
2460  * drm_mode_create_aspect_ratio_property - create aspect ratio property
2461  * @dev: DRM device
2462  *
2463  * Called by a driver the first time it's needed, must be attached to desired
2464  * connectors.
2465  *
2466  * Returns:
2467  * Zero on success, negative errno on failure.
2468  */
2469 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
2470 {
2471 	if (dev->mode_config.aspect_ratio_property)
2472 		return 0;
2473 
2474 	dev->mode_config.aspect_ratio_property =
2475 		drm_property_create_enum(dev, 0, "aspect ratio",
2476 				drm_aspect_ratio_enum_list,
2477 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
2478 
2479 	if (dev->mode_config.aspect_ratio_property == NULL)
2480 		return -ENOMEM;
2481 
2482 	return 0;
2483 }
2484 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
2485 
2486 /**
2487  * DOC: standard connector properties
2488  *
2489  * Colorspace:
2490  *	This property is used to inform the driver about the color encoding
2491  *	user space configured the pixel operation properties to produce.
2492  *	The variants set the colorimetry, transfer characteristics, and which
2493  *	YCbCr conversion should be used when necessary.
2494  *	The transfer characteristics from HDR_OUTPUT_METADATA takes precedence
2495  *	over this property.
2496  *	User space always configures the pixel operation properties to produce
2497  *	full quantization range data (see the Broadcast RGB property).
2498  *
2499  *	Drivers inform the sink about what colorimetry, transfer
2500  *	characteristics, YCbCr conversion, and quantization range to expect
2501  *	(this can depend on the output mode, output format and other
2502  *	properties). Drivers also convert the user space provided data to what
2503  *	the sink expects.
2504  *
2505  *	User space has to check if the sink supports all of the possible
2506  *	colorimetries that the driver is allowed to pick by parsing the EDID.
2507  *
2508  *	For historical reasons this property exposes a number of variants which
2509  *	result in undefined behavior.
2510  *
2511  *	Default:
2512  *		The behavior is driver-specific.
2513  *
2514  *	BT2020_RGB:
2515  *
2516  *	BT2020_YCC:
2517  *		User space configures the pixel operation properties to produce
2518  *		RGB content with Rec. ITU-R BT.2020 colorimetry, Rec.
2519  *		ITU-R BT.2020 (Table 4, RGB) transfer characteristics and full
2520  *		quantization range.
2521  *		User space can use the HDR_OUTPUT_METADATA property to set the
2522  *		transfer characteristics to PQ (Rec. ITU-R BT.2100 Table 4) or
2523  *		HLG (Rec. ITU-R BT.2100 Table 5) in which case, user space
2524  *		configures pixel operation properties to produce content with
2525  *		the respective transfer characteristics.
2526  *		User space has to make sure the sink supports Rec.
2527  *		ITU-R BT.2020 R'G'B' and Rec. ITU-R BT.2020 Y'C'BC'R
2528  *		colorimetry.
2529  *		Drivers can configure the sink to use an RGB format, tell the
2530  *		sink to expect Rec. ITU-R BT.2020 R'G'B' colorimetry and convert
2531  *		to the appropriate quantization range.
2532  *		Drivers can configure the sink to use a YCbCr format, tell the
2533  *		sink to expect Rec. ITU-R BT.2020 Y'C'BC'R colorimetry, convert
2534  *		to YCbCr using the Rec. ITU-R BT.2020 non-constant luminance
2535  *		conversion matrix and convert to the appropriate quantization
2536  *		range.
2537  *		The variants BT2020_RGB and BT2020_YCC are equivalent and the
2538  *		driver chooses between RGB and YCbCr on its own.
2539  *
2540  *	SMPTE_170M_YCC:
2541  *	BT709_YCC:
2542  *	XVYCC_601:
2543  *	XVYCC_709:
2544  *	SYCC_601:
2545  *	opYCC_601:
2546  *	opRGB:
2547  *	BT2020_CYCC:
2548  *	DCI-P3_RGB_D65:
2549  *	DCI-P3_RGB_Theater:
2550  *	RGB_WIDE_FIXED:
2551  *	RGB_WIDE_FLOAT:
2552  *
2553  *	BT601_YCC:
2554  *		The behavior is undefined.
2555  *
2556  * Because between HDMI and DP have different colorspaces,
2557  * drm_mode_create_hdmi_colorspace_property() is used for HDMI connector and
2558  * drm_mode_create_dp_colorspace_property() is used for DP connector.
2559  */
2560 
2561 static int drm_mode_create_colorspace_property(struct drm_connector *connector,
2562 					u32 supported_colorspaces)
2563 {
2564 	struct drm_device *dev = connector->dev;
2565 	u32 colorspaces = supported_colorspaces | BIT(DRM_MODE_COLORIMETRY_DEFAULT);
2566 	struct drm_prop_enum_list enum_list[DRM_MODE_COLORIMETRY_COUNT];
2567 	int i, len;
2568 
2569 	if (connector->colorspace_property)
2570 		return 0;
2571 
2572 	if (!supported_colorspaces) {
2573 		drm_err(dev, "No supported colorspaces provded on [CONNECTOR:%d:%s]\n",
2574 			    connector->base.id, connector->name);
2575 		return -EINVAL;
2576 	}
2577 
2578 	if ((supported_colorspaces & -BIT(DRM_MODE_COLORIMETRY_COUNT)) != 0) {
2579 		drm_err(dev, "Unknown colorspace provded on [CONNECTOR:%d:%s]\n",
2580 			    connector->base.id, connector->name);
2581 		return -EINVAL;
2582 	}
2583 
2584 	len = 0;
2585 	for (i = 0; i < DRM_MODE_COLORIMETRY_COUNT; i++) {
2586 		if ((colorspaces & BIT(i)) == 0)
2587 			continue;
2588 
2589 		enum_list[len].type = i;
2590 		enum_list[len].name = colorspace_names[i];
2591 		len++;
2592 	}
2593 
2594 	connector->colorspace_property =
2595 		drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
2596 					enum_list,
2597 					len);
2598 
2599 	if (!connector->colorspace_property)
2600 		return -ENOMEM;
2601 
2602 	return 0;
2603 }
2604 
2605 /**
2606  * drm_mode_create_hdmi_colorspace_property - create hdmi colorspace property
2607  * @connector: connector to create the Colorspace property on.
2608  * @supported_colorspaces: bitmap of supported color spaces
2609  *
2610  * Called by a driver the first time it's needed, must be attached to desired
2611  * HDMI connectors.
2612  *
2613  * Returns:
2614  * Zero on success, negative errno on failure.
2615  */
2616 int drm_mode_create_hdmi_colorspace_property(struct drm_connector *connector,
2617 					     u32 supported_colorspaces)
2618 {
2619 	u32 colorspaces;
2620 
2621 	if (supported_colorspaces)
2622 		colorspaces = supported_colorspaces & hdmi_colorspaces;
2623 	else
2624 		colorspaces = hdmi_colorspaces;
2625 
2626 	return drm_mode_create_colorspace_property(connector, colorspaces);
2627 }
2628 EXPORT_SYMBOL(drm_mode_create_hdmi_colorspace_property);
2629 
2630 /**
2631  * drm_mode_create_dp_colorspace_property - create dp colorspace property
2632  * @connector: connector to create the Colorspace property on.
2633  * @supported_colorspaces: bitmap of supported color spaces
2634  *
2635  * Called by a driver the first time it's needed, must be attached to desired
2636  * DP connectors.
2637  *
2638  * Returns:
2639  * Zero on success, negative errno on failure.
2640  */
2641 int drm_mode_create_dp_colorspace_property(struct drm_connector *connector,
2642 					   u32 supported_colorspaces)
2643 {
2644 	u32 colorspaces;
2645 
2646 	if (supported_colorspaces)
2647 		colorspaces = supported_colorspaces & dp_colorspaces;
2648 	else
2649 		colorspaces = dp_colorspaces;
2650 
2651 	return drm_mode_create_colorspace_property(connector, colorspaces);
2652 }
2653 EXPORT_SYMBOL(drm_mode_create_dp_colorspace_property);
2654 
2655 /**
2656  * drm_mode_create_content_type_property - create content type property
2657  * @dev: DRM device
2658  *
2659  * Called by a driver the first time it's needed, must be attached to desired
2660  * connectors.
2661  *
2662  * Returns:
2663  * Zero on success, negative errno on failure.
2664  */
2665 int drm_mode_create_content_type_property(struct drm_device *dev)
2666 {
2667 	if (dev->mode_config.content_type_property)
2668 		return 0;
2669 
2670 	dev->mode_config.content_type_property =
2671 		drm_property_create_enum(dev, 0, "content type",
2672 					 drm_content_type_enum_list,
2673 					 ARRAY_SIZE(drm_content_type_enum_list));
2674 
2675 	if (dev->mode_config.content_type_property == NULL)
2676 		return -ENOMEM;
2677 
2678 	return 0;
2679 }
2680 EXPORT_SYMBOL(drm_mode_create_content_type_property);
2681 
2682 /**
2683  * drm_mode_create_suggested_offset_properties - create suggests offset properties
2684  * @dev: DRM device
2685  *
2686  * Create the suggested x/y offset property for connectors.
2687  *
2688  * Returns:
2689  * 0 on success or a negative error code on failure.
2690  */
2691 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
2692 {
2693 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
2694 		return 0;
2695 
2696 	dev->mode_config.suggested_x_property =
2697 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
2698 
2699 	dev->mode_config.suggested_y_property =
2700 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
2701 
2702 	if (dev->mode_config.suggested_x_property == NULL ||
2703 	    dev->mode_config.suggested_y_property == NULL)
2704 		return -ENOMEM;
2705 	return 0;
2706 }
2707 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
2708 
2709 /**
2710  * drm_connector_set_path_property - set tile property on connector
2711  * @connector: connector to set property on.
2712  * @path: path to use for property; must not be NULL.
2713  *
2714  * This creates a property to expose to userspace to specify a
2715  * connector path. This is mainly used for DisplayPort MST where
2716  * connectors have a topology and we want to allow userspace to give
2717  * them more meaningful names.
2718  *
2719  * Returns:
2720  * Zero on success, negative errno on failure.
2721  */
2722 int drm_connector_set_path_property(struct drm_connector *connector,
2723 				    const char *path)
2724 {
2725 	struct drm_device *dev = connector->dev;
2726 	int ret;
2727 
2728 	ret = drm_property_replace_global_blob(dev,
2729 					       &connector->path_blob_ptr,
2730 					       strlen(path) + 1,
2731 					       path,
2732 					       &connector->base,
2733 					       dev->mode_config.path_property);
2734 	return ret;
2735 }
2736 EXPORT_SYMBOL(drm_connector_set_path_property);
2737 
2738 /**
2739  * drm_connector_set_tile_property - set tile property on connector
2740  * @connector: connector to set property on.
2741  *
2742  * This looks up the tile information for a connector, and creates a
2743  * property for userspace to parse if it exists. The property is of
2744  * the form of 8 integers using ':' as a separator.
2745  * This is used for dual port tiled displays with DisplayPort SST
2746  * or DisplayPort MST connectors.
2747  *
2748  * Returns:
2749  * Zero on success, errno on failure.
2750  */
2751 int drm_connector_set_tile_property(struct drm_connector *connector)
2752 {
2753 	struct drm_device *dev = connector->dev;
2754 	char tile[256];
2755 	int ret;
2756 
2757 	if (!connector->has_tile) {
2758 		ret  = drm_property_replace_global_blob(dev,
2759 							&connector->tile_blob_ptr,
2760 							0,
2761 							NULL,
2762 							&connector->base,
2763 							dev->mode_config.tile_property);
2764 		return ret;
2765 	}
2766 
2767 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
2768 		 connector->tile_group->id, connector->tile_is_single_monitor,
2769 		 connector->num_h_tile, connector->num_v_tile,
2770 		 connector->tile_h_loc, connector->tile_v_loc,
2771 		 connector->tile_h_size, connector->tile_v_size);
2772 
2773 	ret = drm_property_replace_global_blob(dev,
2774 					       &connector->tile_blob_ptr,
2775 					       strlen(tile) + 1,
2776 					       tile,
2777 					       &connector->base,
2778 					       dev->mode_config.tile_property);
2779 	return ret;
2780 }
2781 EXPORT_SYMBOL(drm_connector_set_tile_property);
2782 
2783 /**
2784  * drm_connector_set_link_status_property - Set link status property of a connector
2785  * @connector: drm connector
2786  * @link_status: new value of link status property (0: Good, 1: Bad)
2787  *
2788  * In usual working scenario, this link status property will always be set to
2789  * "GOOD". If something fails during or after a mode set, the kernel driver
2790  * may set this link status property to "BAD". The caller then needs to send a
2791  * hotplug uevent for userspace to re-check the valid modes through
2792  * GET_CONNECTOR_IOCTL and retry modeset.
2793  *
2794  * Note: Drivers cannot rely on userspace to support this property and
2795  * issue a modeset. As such, they may choose to handle issues (like
2796  * re-training a link) without userspace's intervention.
2797  *
2798  * The reason for adding this property is to handle link training failures, but
2799  * it is not limited to DP or link training. For example, if we implement
2800  * asynchronous setcrtc, this property can be used to report any failures in that.
2801  */
2802 void drm_connector_set_link_status_property(struct drm_connector *connector,
2803 					    uint64_t link_status)
2804 {
2805 	struct drm_device *dev = connector->dev;
2806 
2807 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2808 	connector->state->link_status = link_status;
2809 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2810 }
2811 EXPORT_SYMBOL(drm_connector_set_link_status_property);
2812 
2813 /**
2814  * drm_connector_attach_max_bpc_property - attach "max bpc" property
2815  * @connector: connector to attach max bpc property on.
2816  * @min: The minimum bit depth supported by the connector.
2817  * @max: The maximum bit depth supported by the connector.
2818  *
2819  * This is used to add support for limiting the bit depth on a connector.
2820  *
2821  * Returns:
2822  * Zero on success, negative errno on failure.
2823  */
2824 int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
2825 					  int min, int max)
2826 {
2827 	struct drm_device *dev = connector->dev;
2828 	struct drm_property *prop;
2829 
2830 	prop = connector->max_bpc_property;
2831 	if (!prop) {
2832 		prop = drm_property_create_range(dev, 0, "max bpc", min, max);
2833 		if (!prop)
2834 			return -ENOMEM;
2835 
2836 		connector->max_bpc_property = prop;
2837 	}
2838 
2839 	drm_object_attach_property(&connector->base, prop, max);
2840 	connector->state->max_requested_bpc = max;
2841 	connector->state->max_bpc = max;
2842 
2843 	return 0;
2844 }
2845 EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
2846 
2847 /**
2848  * drm_connector_attach_hdr_output_metadata_property - attach "HDR_OUTPUT_METADA" property
2849  * @connector: connector to attach the property on.
2850  *
2851  * This is used to allow the userspace to send HDR Metadata to the
2852  * driver.
2853  *
2854  * Returns:
2855  * Zero on success, negative errno on failure.
2856  */
2857 int drm_connector_attach_hdr_output_metadata_property(struct drm_connector *connector)
2858 {
2859 	struct drm_device *dev = connector->dev;
2860 	struct drm_property *prop = dev->mode_config.hdr_output_metadata_property;
2861 
2862 	drm_object_attach_property(&connector->base, prop, 0);
2863 
2864 	return 0;
2865 }
2866 EXPORT_SYMBOL(drm_connector_attach_hdr_output_metadata_property);
2867 
2868 /**
2869  * drm_connector_attach_broadcast_rgb_property - attach "Broadcast RGB" property
2870  * @connector: connector to attach the property on.
2871  *
2872  * This is used to add support for forcing the RGB range on a connector
2873  *
2874  * Returns:
2875  * Zero on success, negative errno on failure.
2876  */
2877 int drm_connector_attach_broadcast_rgb_property(struct drm_connector *connector)
2878 {
2879 	struct drm_device *dev = connector->dev;
2880 	struct drm_property *prop;
2881 
2882 	prop = connector->broadcast_rgb_property;
2883 	if (!prop) {
2884 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
2885 						"Broadcast RGB",
2886 						broadcast_rgb_names,
2887 						ARRAY_SIZE(broadcast_rgb_names));
2888 		if (!prop)
2889 			return -EINVAL;
2890 
2891 		connector->broadcast_rgb_property = prop;
2892 	}
2893 
2894 	drm_object_attach_property(&connector->base, prop,
2895 				   DRM_HDMI_BROADCAST_RGB_AUTO);
2896 
2897 	return 0;
2898 }
2899 EXPORT_SYMBOL(drm_connector_attach_broadcast_rgb_property);
2900 
2901 /**
2902  * drm_connector_attach_colorspace_property - attach "Colorspace" property
2903  * @connector: connector to attach the property on.
2904  *
2905  * This is used to allow the userspace to signal the output colorspace
2906  * to the driver.
2907  *
2908  * Returns:
2909  * Zero on success, negative errno on failure.
2910  */
2911 int drm_connector_attach_colorspace_property(struct drm_connector *connector)
2912 {
2913 	struct drm_property *prop = connector->colorspace_property;
2914 
2915 	drm_object_attach_property(&connector->base, prop, DRM_MODE_COLORIMETRY_DEFAULT);
2916 
2917 	return 0;
2918 }
2919 EXPORT_SYMBOL(drm_connector_attach_colorspace_property);
2920 
2921 /**
2922  * drm_connector_atomic_hdr_metadata_equal - checks if the hdr metadata changed
2923  * @old_state: old connector state to compare
2924  * @new_state: new connector state to compare
2925  *
2926  * This is used by HDR-enabled drivers to test whether the HDR metadata
2927  * have changed between two different connector state (and thus probably
2928  * requires a full blown mode change).
2929  *
2930  * Returns:
2931  * True if the metadata are equal, False otherwise
2932  */
2933 bool drm_connector_atomic_hdr_metadata_equal(struct drm_connector_state *old_state,
2934 					     struct drm_connector_state *new_state)
2935 {
2936 	struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
2937 	struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
2938 
2939 	if (!old_blob || !new_blob)
2940 		return old_blob == new_blob;
2941 
2942 	if (old_blob->length != new_blob->length)
2943 		return false;
2944 
2945 	return !memcmp(old_blob->data, new_blob->data, old_blob->length);
2946 }
2947 EXPORT_SYMBOL(drm_connector_atomic_hdr_metadata_equal);
2948 
2949 /**
2950  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
2951  * capable property for a connector
2952  * @connector: drm connector
2953  * @capable: True if the connector is variable refresh rate capable
2954  *
2955  * Should be used by atomic drivers to update the indicated support for
2956  * variable refresh rate over a connector.
2957  */
2958 void drm_connector_set_vrr_capable_property(
2959 		struct drm_connector *connector, bool capable)
2960 {
2961 	if (!connector->vrr_capable_property)
2962 		return;
2963 
2964 	drm_object_property_set_value(&connector->base,
2965 				      connector->vrr_capable_property,
2966 				      capable);
2967 }
2968 EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
2969 
2970 /**
2971  * drm_connector_set_panel_orientation - sets the connector's panel_orientation
2972  * @connector: connector for which to set the panel-orientation property.
2973  * @panel_orientation: drm_panel_orientation value to set
2974  *
2975  * This function sets the connector's panel_orientation and attaches
2976  * a "panel orientation" property to the connector.
2977  *
2978  * Calling this function on a connector where the panel_orientation has
2979  * already been set is a no-op (e.g. the orientation has been overridden with
2980  * a kernel commandline option).
2981  *
2982  * It is allowed to call this function with a panel_orientation of
2983  * DRM_MODE_PANEL_ORIENTATION_UNKNOWN, in which case it is a no-op.
2984  *
2985  * The function shouldn't be called in panel after drm is registered (i.e.
2986  * drm_dev_register() is called in drm).
2987  *
2988  * Returns:
2989  * Zero on success, negative errno on failure.
2990  */
2991 int drm_connector_set_panel_orientation(
2992 	struct drm_connector *connector,
2993 	enum drm_panel_orientation panel_orientation)
2994 {
2995 	struct drm_device *dev = connector->dev;
2996 	struct drm_display_info *info = &connector->display_info;
2997 	struct drm_property *prop;
2998 
2999 	/* Already set? */
3000 	if (info->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
3001 		return 0;
3002 
3003 	/* Don't attach the property if the orientation is unknown */
3004 	if (panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
3005 		return 0;
3006 
3007 	info->panel_orientation = panel_orientation;
3008 
3009 	prop = dev->mode_config.panel_orientation_property;
3010 	if (!prop) {
3011 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
3012 				"panel orientation",
3013 				drm_panel_orientation_enum_list,
3014 				ARRAY_SIZE(drm_panel_orientation_enum_list));
3015 		if (!prop)
3016 			return -ENOMEM;
3017 
3018 		dev->mode_config.panel_orientation_property = prop;
3019 	}
3020 
3021 	drm_object_attach_property(&connector->base, prop,
3022 				   info->panel_orientation);
3023 	return 0;
3024 }
3025 EXPORT_SYMBOL(drm_connector_set_panel_orientation);
3026 
3027 /**
3028  * drm_connector_set_panel_orientation_with_quirk - set the
3029  *	connector's panel_orientation after checking for quirks
3030  * @connector: connector for which to init the panel-orientation property.
3031  * @panel_orientation: drm_panel_orientation value to set
3032  * @width: width in pixels of the panel, used for panel quirk detection
3033  * @height: height in pixels of the panel, used for panel quirk detection
3034  *
3035  * Like drm_connector_set_panel_orientation(), but with a check for platform
3036  * specific (e.g. DMI based) quirks overriding the passed in panel_orientation.
3037  *
3038  * Returns:
3039  * Zero on success, negative errno on failure.
3040  */
3041 int drm_connector_set_panel_orientation_with_quirk(
3042 	struct drm_connector *connector,
3043 	enum drm_panel_orientation panel_orientation,
3044 	int width, int height)
3045 {
3046 	int orientation_quirk;
3047 
3048 	orientation_quirk = drm_get_panel_orientation_quirk(width, height);
3049 	if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
3050 		panel_orientation = orientation_quirk;
3051 
3052 	return drm_connector_set_panel_orientation(connector,
3053 						   panel_orientation);
3054 }
3055 EXPORT_SYMBOL(drm_connector_set_panel_orientation_with_quirk);
3056 
3057 /**
3058  * drm_connector_set_orientation_from_panel -
3059  *	set the connector's panel_orientation from panel's callback.
3060  * @connector: connector for which to init the panel-orientation property.
3061  * @panel: panel that can provide orientation information.
3062  *
3063  * Drm drivers should call this function before drm_dev_register().
3064  * Orientation is obtained from panel's .get_orientation() callback.
3065  *
3066  * Returns:
3067  * Zero on success, negative errno on failure.
3068  */
3069 int drm_connector_set_orientation_from_panel(
3070 	struct drm_connector *connector,
3071 	struct drm_panel *panel)
3072 {
3073 	enum drm_panel_orientation orientation;
3074 
3075 	if (panel && panel->funcs && panel->funcs->get_orientation)
3076 		orientation = panel->funcs->get_orientation(panel);
3077 	else
3078 		orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
3079 
3080 	return drm_connector_set_panel_orientation(connector, orientation);
3081 }
3082 EXPORT_SYMBOL(drm_connector_set_orientation_from_panel);
3083 
3084 static const struct drm_prop_enum_list privacy_screen_enum[] = {
3085 	{ PRIVACY_SCREEN_DISABLED,		"Disabled" },
3086 	{ PRIVACY_SCREEN_ENABLED,		"Enabled" },
3087 	{ PRIVACY_SCREEN_DISABLED_LOCKED,	"Disabled-locked" },
3088 	{ PRIVACY_SCREEN_ENABLED_LOCKED,	"Enabled-locked" },
3089 };
3090 
3091 /**
3092  * drm_connector_create_privacy_screen_properties - create the drm connecter's
3093  *    privacy-screen properties.
3094  * @connector: connector for which to create the privacy-screen properties
3095  *
3096  * This function creates the "privacy-screen sw-state" and "privacy-screen
3097  * hw-state" properties for the connector. They are not attached.
3098  */
3099 void
3100 drm_connector_create_privacy_screen_properties(struct drm_connector *connector)
3101 {
3102 	if (connector->privacy_screen_sw_state_property)
3103 		return;
3104 
3105 	/* Note sw-state only supports the first 2 values of the enum */
3106 	connector->privacy_screen_sw_state_property =
3107 		drm_property_create_enum(connector->dev, DRM_MODE_PROP_ENUM,
3108 				"privacy-screen sw-state",
3109 				privacy_screen_enum, 2);
3110 
3111 	connector->privacy_screen_hw_state_property =
3112 		drm_property_create_enum(connector->dev,
3113 				DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ENUM,
3114 				"privacy-screen hw-state",
3115 				privacy_screen_enum,
3116 				ARRAY_SIZE(privacy_screen_enum));
3117 }
3118 EXPORT_SYMBOL(drm_connector_create_privacy_screen_properties);
3119 
3120 /**
3121  * drm_connector_attach_privacy_screen_properties - attach the drm connecter's
3122  *    privacy-screen properties.
3123  * @connector: connector on which to attach the privacy-screen properties
3124  *
3125  * This function attaches the "privacy-screen sw-state" and "privacy-screen
3126  * hw-state" properties to the connector. The initial state of both is set
3127  * to "Disabled".
3128  */
3129 void
3130 drm_connector_attach_privacy_screen_properties(struct drm_connector *connector)
3131 {
3132 	if (!connector->privacy_screen_sw_state_property)
3133 		return;
3134 
3135 	drm_object_attach_property(&connector->base,
3136 				   connector->privacy_screen_sw_state_property,
3137 				   PRIVACY_SCREEN_DISABLED);
3138 
3139 	drm_object_attach_property(&connector->base,
3140 				   connector->privacy_screen_hw_state_property,
3141 				   PRIVACY_SCREEN_DISABLED);
3142 }
3143 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_properties);
3144 
3145 static void drm_connector_update_privacy_screen_properties(
3146 	struct drm_connector *connector, bool set_sw_state)
3147 {
3148 	enum drm_privacy_screen_status sw_state, hw_state;
3149 
3150 	drm_privacy_screen_get_state(connector->privacy_screen,
3151 				     &sw_state, &hw_state);
3152 
3153 	if (set_sw_state)
3154 		connector->state->privacy_screen_sw_state = sw_state;
3155 	drm_object_property_set_value(&connector->base,
3156 			connector->privacy_screen_hw_state_property, hw_state);
3157 }
3158 
3159 static int drm_connector_privacy_screen_notifier(
3160 	struct notifier_block *nb, unsigned long action, void *data)
3161 {
3162 	struct drm_connector *connector =
3163 		container_of(nb, struct drm_connector, privacy_screen_notifier);
3164 	struct drm_device *dev = connector->dev;
3165 
3166 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
3167 	drm_connector_update_privacy_screen_properties(connector, true);
3168 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
3169 
3170 	drm_sysfs_connector_property_event(connector,
3171 					   connector->privacy_screen_sw_state_property);
3172 	drm_sysfs_connector_property_event(connector,
3173 					   connector->privacy_screen_hw_state_property);
3174 
3175 	return NOTIFY_DONE;
3176 }
3177 
3178 /**
3179  * drm_connector_attach_privacy_screen_provider - attach a privacy-screen to
3180  *    the connector
3181  * @connector: connector to attach the privacy-screen to
3182  * @priv: drm_privacy_screen to attach
3183  *
3184  * Create and attach the standard privacy-screen properties and register
3185  * a generic notifier for generating sysfs-connector-status-events
3186  * on external changes to the privacy-screen status.
3187  * This function takes ownership of the passed in drm_privacy_screen and will
3188  * call drm_privacy_screen_put() on it when the connector is destroyed.
3189  */
3190 void drm_connector_attach_privacy_screen_provider(
3191 	struct drm_connector *connector, struct drm_privacy_screen *priv)
3192 {
3193 	connector->privacy_screen = priv;
3194 	connector->privacy_screen_notifier.notifier_call =
3195 		drm_connector_privacy_screen_notifier;
3196 
3197 	drm_connector_create_privacy_screen_properties(connector);
3198 	drm_connector_update_privacy_screen_properties(connector, true);
3199 	drm_connector_attach_privacy_screen_properties(connector);
3200 }
3201 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_provider);
3202 
3203 /**
3204  * drm_connector_update_privacy_screen - update connector's privacy-screen sw-state
3205  * @connector_state: connector-state to update the privacy-screen for
3206  *
3207  * This function calls drm_privacy_screen_set_sw_state() on the connector's
3208  * privacy-screen.
3209  *
3210  * If the connector has no privacy-screen, then this is a no-op.
3211  */
3212 void drm_connector_update_privacy_screen(const struct drm_connector_state *connector_state)
3213 {
3214 	struct drm_connector *connector = connector_state->connector;
3215 	int ret;
3216 
3217 	if (!connector->privacy_screen)
3218 		return;
3219 
3220 	ret = drm_privacy_screen_set_sw_state(connector->privacy_screen,
3221 					      connector_state->privacy_screen_sw_state);
3222 	if (ret) {
3223 		drm_err(connector->dev, "Error updating privacy-screen sw_state\n");
3224 		return;
3225 	}
3226 
3227 	/* The hw_state property value may have changed, update it. */
3228 	drm_connector_update_privacy_screen_properties(connector, false);
3229 }
3230 EXPORT_SYMBOL(drm_connector_update_privacy_screen);
3231 
3232 int drm_connector_set_obj_prop(struct drm_mode_object *obj,
3233 				    struct drm_property *property,
3234 				    uint64_t value)
3235 {
3236 	int ret = -EINVAL;
3237 	struct drm_connector *connector = obj_to_connector(obj);
3238 
3239 	/* Do DPMS ourselves */
3240 	if (property == connector->dev->mode_config.dpms_property) {
3241 		ret = (*connector->funcs->dpms)(connector, (int)value);
3242 	} else if (connector->funcs->set_property)
3243 		ret = connector->funcs->set_property(connector, property, value);
3244 
3245 	if (!ret)
3246 		drm_object_property_set_value(&connector->base, property, value);
3247 	return ret;
3248 }
3249 
3250 int drm_connector_property_set_ioctl(struct drm_device *dev,
3251 				     void *data, struct drm_file *file_priv)
3252 {
3253 	struct drm_mode_connector_set_property *conn_set_prop = data;
3254 	struct drm_mode_obj_set_property obj_set_prop = {
3255 		.value = conn_set_prop->value,
3256 		.prop_id = conn_set_prop->prop_id,
3257 		.obj_id = conn_set_prop->connector_id,
3258 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
3259 	};
3260 
3261 	/* It does all the locking and checking we need */
3262 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3263 }
3264 
3265 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
3266 {
3267 	/* For atomic drivers only state objects are synchronously updated and
3268 	 * protected by modeset locks, so check those first.
3269 	 */
3270 	if (connector->state)
3271 		return connector->state->best_encoder;
3272 	return connector->encoder;
3273 }
3274 
3275 static bool
3276 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
3277 			     const struct list_head *modes,
3278 			     const struct drm_file *file_priv)
3279 {
3280 	/*
3281 	 * If user-space hasn't configured the driver to expose the stereo 3D
3282 	 * modes, don't expose them.
3283 	 */
3284 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
3285 		return false;
3286 	/*
3287 	 * If user-space hasn't configured the driver to expose the modes
3288 	 * with aspect-ratio, don't expose them. However if such a mode
3289 	 * is unique, let it be exposed, but reset the aspect-ratio flags
3290 	 * while preparing the list of user-modes.
3291 	 */
3292 	if (!file_priv->aspect_ratio_allowed) {
3293 		const struct drm_display_mode *mode_itr;
3294 
3295 		list_for_each_entry(mode_itr, modes, head) {
3296 			if (mode_itr->expose_to_userspace &&
3297 			    drm_mode_match(mode_itr, mode,
3298 					   DRM_MODE_MATCH_TIMINGS |
3299 					   DRM_MODE_MATCH_CLOCK |
3300 					   DRM_MODE_MATCH_FLAGS |
3301 					   DRM_MODE_MATCH_3D_FLAGS))
3302 				return false;
3303 		}
3304 	}
3305 
3306 	return true;
3307 }
3308 
3309 int drm_mode_getconnector(struct drm_device *dev, void *data,
3310 			  struct drm_file *file_priv)
3311 {
3312 	struct drm_mode_get_connector *out_resp = data;
3313 	struct drm_connector *connector;
3314 	struct drm_encoder *encoder;
3315 	struct drm_display_mode *mode;
3316 	int mode_count = 0;
3317 	int encoders_count = 0;
3318 	int ret = 0;
3319 	int copied = 0;
3320 	struct drm_mode_modeinfo u_mode;
3321 	struct drm_mode_modeinfo __user *mode_ptr;
3322 	uint32_t __user *encoder_ptr;
3323 	bool is_current_master;
3324 
3325 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3326 		return -EOPNOTSUPP;
3327 
3328 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
3329 
3330 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
3331 	if (!connector)
3332 		return -ENOENT;
3333 
3334 	encoders_count = hweight32(connector->possible_encoders);
3335 
3336 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
3337 		copied = 0;
3338 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
3339 
3340 		drm_connector_for_each_possible_encoder(connector, encoder) {
3341 			if (put_user(encoder->base.id, encoder_ptr + copied)) {
3342 				ret = -EFAULT;
3343 				goto out;
3344 			}
3345 			copied++;
3346 		}
3347 	}
3348 	out_resp->count_encoders = encoders_count;
3349 
3350 	out_resp->connector_id = connector->base.id;
3351 	out_resp->connector_type = connector->connector_type;
3352 	out_resp->connector_type_id = connector->connector_type_id;
3353 
3354 	is_current_master = drm_is_current_master(file_priv);
3355 
3356 	mutex_lock(&dev->mode_config.mutex);
3357 	if (out_resp->count_modes == 0) {
3358 		if (is_current_master)
3359 			connector->funcs->fill_modes(connector,
3360 						     dev->mode_config.max_width,
3361 						     dev->mode_config.max_height);
3362 		else
3363 			drm_dbg_kms(dev, "User-space requested a forced probe on [CONNECTOR:%d:%s] but is not the DRM master, demoting to read-only probe\n",
3364 				    connector->base.id, connector->name);
3365 	}
3366 
3367 	out_resp->mm_width = connector->display_info.width_mm;
3368 	out_resp->mm_height = connector->display_info.height_mm;
3369 	out_resp->subpixel = connector->display_info.subpixel_order;
3370 	out_resp->connection = connector->status;
3371 
3372 	/* delayed so we get modes regardless of pre-fill_modes state */
3373 	list_for_each_entry(mode, &connector->modes, head) {
3374 		WARN_ON(mode->expose_to_userspace);
3375 
3376 		if (drm_mode_expose_to_userspace(mode, &connector->modes,
3377 						 file_priv)) {
3378 			mode->expose_to_userspace = true;
3379 			mode_count++;
3380 		}
3381 	}
3382 
3383 	/*
3384 	 * This ioctl is called twice, once to determine how much space is
3385 	 * needed, and the 2nd time to fill it.
3386 	 */
3387 	if ((out_resp->count_modes >= mode_count) && mode_count) {
3388 		copied = 0;
3389 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
3390 		list_for_each_entry(mode, &connector->modes, head) {
3391 			if (!mode->expose_to_userspace)
3392 				continue;
3393 
3394 			/* Clear the tag for the next time around */
3395 			mode->expose_to_userspace = false;
3396 
3397 			drm_mode_convert_to_umode(&u_mode, mode);
3398 			/*
3399 			 * Reset aspect ratio flags of user-mode, if modes with
3400 			 * aspect-ratio are not supported.
3401 			 */
3402 			if (!file_priv->aspect_ratio_allowed)
3403 				u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
3404 			if (copy_to_user(mode_ptr + copied,
3405 					 &u_mode, sizeof(u_mode))) {
3406 				ret = -EFAULT;
3407 
3408 				/*
3409 				 * Clear the tag for the rest of
3410 				 * the modes for the next time around.
3411 				 */
3412 				list_for_each_entry_continue(mode, &connector->modes, head)
3413 					mode->expose_to_userspace = false;
3414 
3415 				mutex_unlock(&dev->mode_config.mutex);
3416 
3417 				goto out;
3418 			}
3419 			copied++;
3420 		}
3421 	} else {
3422 		/* Clear the tag for the next time around */
3423 		list_for_each_entry(mode, &connector->modes, head)
3424 			mode->expose_to_userspace = false;
3425 	}
3426 
3427 	out_resp->count_modes = mode_count;
3428 	mutex_unlock(&dev->mode_config.mutex);
3429 
3430 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
3431 	encoder = drm_connector_get_encoder(connector);
3432 	if (encoder)
3433 		out_resp->encoder_id = encoder->base.id;
3434 	else
3435 		out_resp->encoder_id = 0;
3436 
3437 	/* Only grab properties after probing, to make sure EDID and other
3438 	 * properties reflect the latest status.
3439 	 */
3440 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
3441 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
3442 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
3443 			&out_resp->count_props);
3444 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
3445 
3446 out:
3447 	drm_connector_put(connector);
3448 
3449 	return ret;
3450 }
3451 
3452 /**
3453  * drm_connector_find_by_fwnode - Find a connector based on the associated fwnode
3454  * @fwnode: fwnode for which to find the matching drm_connector
3455  *
3456  * This functions looks up a drm_connector based on its associated fwnode. When
3457  * a connector is found a reference to the connector is returned. The caller must
3458  * call drm_connector_put() to release this reference when it is done with the
3459  * connector.
3460  *
3461  * Returns: A reference to the found connector or an ERR_PTR().
3462  */
3463 struct drm_connector *drm_connector_find_by_fwnode(struct fwnode_handle *fwnode)
3464 {
3465 	struct drm_connector *connector, *found = ERR_PTR(-ENODEV);
3466 
3467 	if (!fwnode)
3468 		return ERR_PTR(-ENODEV);
3469 
3470 	mutex_lock(&connector_list_lock);
3471 
3472 	list_for_each_entry(connector, &connector_list, global_connector_list_entry) {
3473 		if (connector->fwnode == fwnode ||
3474 		    (connector->fwnode && connector->fwnode->secondary == fwnode)) {
3475 			drm_connector_get(connector);
3476 			found = connector;
3477 			break;
3478 		}
3479 	}
3480 
3481 	mutex_unlock(&connector_list_lock);
3482 
3483 	return found;
3484 }
3485 
3486 /**
3487  * drm_connector_oob_hotplug_event - Report out-of-band hotplug event to connector
3488  * @connector_fwnode: fwnode_handle to report the event on
3489  * @status: hot plug detect logical state
3490  *
3491  * On some hardware a hotplug event notification may come from outside the display
3492  * driver / device. An example of this is some USB Type-C setups where the hardware
3493  * muxes the DisplayPort data and aux-lines but does not pass the altmode HPD
3494  * status bit to the GPU's DP HPD pin.
3495  *
3496  * This function can be used to report these out-of-band events after obtaining
3497  * a drm_connector reference through calling drm_connector_find_by_fwnode().
3498  */
3499 void drm_connector_oob_hotplug_event(struct fwnode_handle *connector_fwnode,
3500 				     enum drm_connector_status status)
3501 {
3502 	struct drm_connector *connector;
3503 
3504 	connector = drm_connector_find_by_fwnode(connector_fwnode);
3505 	if (IS_ERR(connector))
3506 		return;
3507 
3508 	if (connector->funcs->oob_hotplug_event)
3509 		connector->funcs->oob_hotplug_event(connector, status);
3510 
3511 	drm_connector_put(connector);
3512 }
3513 EXPORT_SYMBOL(drm_connector_oob_hotplug_event);
3514 
3515 
3516 /**
3517  * DOC: Tile group
3518  *
3519  * Tile groups are used to represent tiled monitors with a unique integer
3520  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
3521  * we store this in a tile group, so we have a common identifier for all tiles
3522  * in a monitor group. The property is called "TILE". Drivers can manage tile
3523  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
3524  * drm_mode_get_tile_group(). But this is only needed for internal panels where
3525  * the tile group information is exposed through a non-standard way.
3526  */
3527 
3528 static void drm_tile_group_free(struct kref *kref)
3529 {
3530 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
3531 	struct drm_device *dev = tg->dev;
3532 
3533 	mutex_lock(&dev->mode_config.idr_mutex);
3534 	idr_remove(&dev->mode_config.tile_idr, tg->id);
3535 	mutex_unlock(&dev->mode_config.idr_mutex);
3536 	kfree(tg);
3537 }
3538 
3539 /**
3540  * drm_mode_put_tile_group - drop a reference to a tile group.
3541  * @dev: DRM device
3542  * @tg: tile group to drop reference to.
3543  *
3544  * drop reference to tile group and free if 0.
3545  */
3546 void drm_mode_put_tile_group(struct drm_device *dev,
3547 			     struct drm_tile_group *tg)
3548 {
3549 	kref_put(&tg->refcount, drm_tile_group_free);
3550 }
3551 EXPORT_SYMBOL(drm_mode_put_tile_group);
3552 
3553 /**
3554  * drm_mode_get_tile_group - get a reference to an existing tile group
3555  * @dev: DRM device
3556  * @topology: 8-bytes unique per monitor.
3557  *
3558  * Use the unique bytes to get a reference to an existing tile group.
3559  *
3560  * RETURNS:
3561  * tile group or NULL if not found.
3562  */
3563 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
3564 					       const char topology[8])
3565 {
3566 	struct drm_tile_group *tg;
3567 	int id;
3568 
3569 	mutex_lock(&dev->mode_config.idr_mutex);
3570 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
3571 		if (!memcmp(tg->group_data, topology, 8)) {
3572 			if (!kref_get_unless_zero(&tg->refcount))
3573 				tg = NULL;
3574 			mutex_unlock(&dev->mode_config.idr_mutex);
3575 			return tg;
3576 		}
3577 	}
3578 	mutex_unlock(&dev->mode_config.idr_mutex);
3579 	return NULL;
3580 }
3581 EXPORT_SYMBOL(drm_mode_get_tile_group);
3582 
3583 /**
3584  * drm_mode_create_tile_group - create a tile group from a displayid description
3585  * @dev: DRM device
3586  * @topology: 8-bytes unique per monitor.
3587  *
3588  * Create a tile group for the unique monitor, and get a unique
3589  * identifier for the tile group.
3590  *
3591  * RETURNS:
3592  * new tile group or NULL.
3593  */
3594 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
3595 						  const char topology[8])
3596 {
3597 	struct drm_tile_group *tg;
3598 	int ret;
3599 
3600 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
3601 	if (!tg)
3602 		return NULL;
3603 
3604 	kref_init(&tg->refcount);
3605 	memcpy(tg->group_data, topology, 8);
3606 	tg->dev = dev;
3607 
3608 	mutex_lock(&dev->mode_config.idr_mutex);
3609 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
3610 	if (ret >= 0) {
3611 		tg->id = ret;
3612 	} else {
3613 		kfree(tg);
3614 		tg = NULL;
3615 	}
3616 
3617 	mutex_unlock(&dev->mode_config.idr_mutex);
3618 	return tg;
3619 }
3620 EXPORT_SYMBOL(drm_mode_create_tile_group);
3621