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