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