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