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