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