]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/drm_connector.c
drm/bochs: Use dev_get_drvdata
[mirror_ubuntu-jammy-kernel.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
52217195
DV
23#include <drm/drm_connector.h>
24#include <drm/drm_edid.h>
9338203c 25#include <drm/drm_encoder.h>
8d70f395 26#include <drm/drm_utils.h>
99f45e32
DV
27#include <drm/drm_print.h>
28#include <drm/drm_drv.h>
29#include <drm/drm_file.h>
30
31#include <linux/uaccess.h>
52217195
DV
32
33#include "drm_crtc_internal.h"
34#include "drm_internal.h"
35
ae2a6da8
DV
36/**
37 * DOC: overview
38 *
39 * In DRM connectors are the general abstraction for display sinks, and include
40 * als fixed panels or anything else that can display pixels in some form. As
41 * opposed to all other KMS objects representing hardware (like CRTC, encoder or
42 * plane abstractions) connectors can be hotplugged and unplugged at runtime.
ad093607
TR
43 * Hence they are reference-counted using drm_connector_get() and
44 * drm_connector_put().
ae2a6da8 45 *
d574528a
DV
46 * KMS driver must create, initialize, register and attach at a &struct
47 * drm_connector for each such sink. The instance is created as other KMS
aec97460
DV
48 * objects and initialized by setting the following fields. The connector is
49 * initialized with a call to drm_connector_init() with a pointer to the
50 * &struct drm_connector_funcs and a connector type, and then exposed to
51 * userspace with a call to drm_connector_register().
ae2a6da8
DV
52 *
53 * Connectors must be attached to an encoder to be used. For devices that map
54 * connectors to encoders 1:1, the connector should be attached at
cde4c44d 55 * initialization time with a call to drm_connector_attach_encoder(). The
d574528a 56 * driver must also set the &drm_connector.encoder field to point to the
ae2a6da8
DV
57 * attached encoder.
58 *
59 * For connectors which are not fixed (like built-in panels) the driver needs to
60 * support hotplug notifications. The simplest way to do that is by using the
61 * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
62 * hardware support for hotplug interrupts. Connectors with hardware hotplug
63 * support can instead use e.g. drm_helper_hpd_irq_event().
64 */
65
52217195
DV
66struct drm_conn_prop_enum_list {
67 int type;
68 const char *name;
69 struct ida ida;
70};
71
72/*
73 * Connector and encoder types.
74 */
75static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
76 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
77 { DRM_MODE_CONNECTOR_VGA, "VGA" },
78 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
79 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
80 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
81 { DRM_MODE_CONNECTOR_Composite, "Composite" },
82 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
83 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
84 { DRM_MODE_CONNECTOR_Component, "Component" },
85 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
86 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
87 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
88 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
89 { DRM_MODE_CONNECTOR_TV, "TV" },
90 { DRM_MODE_CONNECTOR_eDP, "eDP" },
91 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
92 { DRM_MODE_CONNECTOR_DSI, "DSI" },
93 { DRM_MODE_CONNECTOR_DPI, "DPI" },
935774cd 94 { DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
fc06bf1d 95 { DRM_MODE_CONNECTOR_SPI, "SPI" },
52217195
DV
96};
97
98void drm_connector_ida_init(void)
99{
100 int i;
101
102 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
103 ida_init(&drm_connector_enum_list[i].ida);
104}
105
106void drm_connector_ida_destroy(void)
107{
108 int i;
109
110 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
111 ida_destroy(&drm_connector_enum_list[i].ida);
112}
113
114/**
115 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
116 * @connector: connector to quwery
117 *
ae2a6da8 118 * The kernel supports per-connector configuration of its consoles through
52217195
DV
119 * use of the video= parameter. This function parses that option and
120 * extracts the user's specified mode (or enable/disable status) for a
121 * particular connector. This is typically only used during the early fbdev
122 * setup.
123 */
124static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
125{
126 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
127 char *option = NULL;
128
129 if (fb_get_options(connector->name, &option))
130 return;
131
132 if (!drm_mode_parse_command_line_for_connector(option,
133 connector,
134 mode))
135 return;
136
137 if (mode->force) {
6140cf20
JN
138 DRM_INFO("forcing %s connector %s\n", connector->name,
139 drm_get_connector_force_name(mode->force));
52217195
DV
140 connector->force = mode->force;
141 }
142
3aeeb13d 143 DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
50b0946d 144 connector->name, mode->name,
52217195
DV
145 mode->xres, mode->yres,
146 mode->refresh_specified ? mode->refresh : 60,
147 mode->rb ? " reduced blanking" : "",
148 mode->margins ? " with margins" : "",
149 mode->interlace ? " interlaced" : "");
150}
151
152static void drm_connector_free(struct kref *kref)
153{
154 struct drm_connector *connector =
155 container_of(kref, struct drm_connector, base.refcount);
156 struct drm_device *dev = connector->dev;
157
158 drm_mode_object_unregister(dev, &connector->base);
159 connector->funcs->destroy(connector);
160}
161
ea497bb9 162void drm_connector_free_work_fn(struct work_struct *work)
a703c550 163{
ea497bb9
DV
164 struct drm_connector *connector, *n;
165 struct drm_device *dev =
166 container_of(work, struct drm_device, mode_config.connector_free_work);
167 struct drm_mode_config *config = &dev->mode_config;
168 unsigned long flags;
169 struct llist_node *freed;
a703c550 170
ea497bb9
DV
171 spin_lock_irqsave(&config->connector_list_lock, flags);
172 freed = llist_del_all(&config->connector_free_list);
173 spin_unlock_irqrestore(&config->connector_list_lock, flags);
174
175 llist_for_each_entry_safe(connector, n, freed, free_node) {
176 drm_mode_object_unregister(dev, &connector->base);
177 connector->funcs->destroy(connector);
178 }
a703c550
DV
179}
180
52217195
DV
181/**
182 * drm_connector_init - Init a preallocated connector
183 * @dev: DRM device
184 * @connector: the connector to init
185 * @funcs: callbacks for this connector
186 * @connector_type: user visible type of the connector
187 *
188 * Initialises a preallocated connector. Connectors should be
189 * subclassed as part of driver connector objects.
190 *
191 * Returns:
192 * Zero on success, error code on failure.
193 */
194int drm_connector_init(struct drm_device *dev,
195 struct drm_connector *connector,
196 const struct drm_connector_funcs *funcs,
197 int connector_type)
198{
199 struct drm_mode_config *config = &dev->mode_config;
200 int ret;
201 struct ida *connector_ida =
202 &drm_connector_enum_list[connector_type].ida;
203
ba1f665f
HM
204 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
205 (!funcs->atomic_destroy_state ||
206 !funcs->atomic_duplicate_state));
207
2135ea7a
TR
208 ret = __drm_mode_object_add(dev, &connector->base,
209 DRM_MODE_OBJECT_CONNECTOR,
210 false, drm_connector_free);
52217195 211 if (ret)
613051da 212 return ret;
52217195
DV
213
214 connector->base.properties = &connector->properties;
215 connector->dev = dev;
216 connector->funcs = funcs;
217
2a8d3eac
VS
218 /* connector index is used with 32bit bitmasks */
219 ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
220 if (ret < 0) {
221 DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
222 drm_connector_enum_list[connector_type].name,
223 ret);
52217195 224 goto out_put;
2a8d3eac 225 }
52217195
DV
226 connector->index = ret;
227 ret = 0;
228
229 connector->connector_type = connector_type;
230 connector->connector_type_id =
231 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
232 if (connector->connector_type_id < 0) {
233 ret = connector->connector_type_id;
234 goto out_put_id;
235 }
236 connector->name =
237 kasprintf(GFP_KERNEL, "%s-%d",
238 drm_connector_enum_list[connector_type].name,
239 connector->connector_type_id);
240 if (!connector->name) {
241 ret = -ENOMEM;
242 goto out_put_type_id;
243 }
244
245 INIT_LIST_HEAD(&connector->probed_modes);
246 INIT_LIST_HEAD(&connector->modes);
e73ab00e 247 mutex_init(&connector->mutex);
52217195 248 connector->edid_blob_ptr = NULL;
2de3a078 249 connector->tile_blob_ptr = NULL;
52217195 250 connector->status = connector_status_unknown;
8d70f395
HG
251 connector->display_info.panel_orientation =
252 DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
52217195
DV
253
254 drm_connector_get_cmdline_mode(connector);
255
256 /* We should add connectors at the end to avoid upsetting the connector
257 * index too much. */
613051da 258 spin_lock_irq(&config->connector_list_lock);
52217195
DV
259 list_add_tail(&connector->head, &config->connector_list);
260 config->num_connector++;
613051da 261 spin_unlock_irq(&config->connector_list_lock);
52217195 262
935774cd
BS
263 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
264 connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
6b7e2d5c 265 drm_connector_attach_edid_property(connector);
52217195
DV
266
267 drm_object_attach_property(&connector->base,
268 config->dpms_property, 0);
269
40ee6fbe
MN
270 drm_object_attach_property(&connector->base,
271 config->link_status_property,
272 0);
273
66660d4c
DA
274 drm_object_attach_property(&connector->base,
275 config->non_desktop_property,
276 0);
2de3a078
MN
277 drm_object_attach_property(&connector->base,
278 config->tile_property,
279 0);
66660d4c 280
52217195
DV
281 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
282 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
283 }
284
285 connector->debugfs_entry = NULL;
286out_put_type_id:
287 if (ret)
587680c1 288 ida_simple_remove(connector_ida, connector->connector_type_id);
52217195
DV
289out_put_id:
290 if (ret)
587680c1 291 ida_simple_remove(&config->connector_ida, connector->index);
52217195
DV
292out_put:
293 if (ret)
294 drm_mode_object_unregister(dev, &connector->base);
295
52217195
DV
296 return ret;
297}
298EXPORT_SYMBOL(drm_connector_init);
299
100163df
AP
300/**
301 * drm_connector_init_with_ddc - Init a preallocated connector
302 * @dev: DRM device
303 * @connector: the connector to init
304 * @funcs: callbacks for this connector
305 * @connector_type: user visible type of the connector
306 * @ddc: pointer to the associated ddc adapter
307 *
308 * Initialises a preallocated connector. Connectors should be
309 * subclassed as part of driver connector objects.
310 *
311 * Ensures that the ddc field of the connector is correctly set.
312 *
313 * Returns:
314 * Zero on success, error code on failure.
315 */
316int drm_connector_init_with_ddc(struct drm_device *dev,
317 struct drm_connector *connector,
318 const struct drm_connector_funcs *funcs,
319 int connector_type,
320 struct i2c_adapter *ddc)
321{
322 int ret;
323
324 ret = drm_connector_init(dev, connector, funcs, connector_type);
325 if (ret)
326 return ret;
327
328 /* provide ddc symlink in sysfs */
329 connector->ddc = ddc;
330
331 return ret;
332}
333EXPORT_SYMBOL(drm_connector_init_with_ddc);
334
6b7e2d5c
GH
335/**
336 * drm_connector_attach_edid_property - attach edid property.
6b7e2d5c
GH
337 * @connector: the connector
338 *
339 * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
340 * edid property attached by default. This function can be used to
341 * explicitly enable the edid property in these cases.
342 */
343void drm_connector_attach_edid_property(struct drm_connector *connector)
344{
345 struct drm_mode_config *config = &connector->dev->mode_config;
346
347 drm_object_attach_property(&connector->base,
348 config->edid_property,
349 0);
350}
351EXPORT_SYMBOL(drm_connector_attach_edid_property);
352
52217195 353/**
cde4c44d 354 * drm_connector_attach_encoder - attach a connector to an encoder
52217195
DV
355 * @connector: connector to attach
356 * @encoder: encoder to attach @connector to
357 *
358 * This function links up a connector to an encoder. Note that the routing
359 * restrictions between encoders and crtcs are exposed to userspace through the
360 * possible_clones and possible_crtcs bitmasks.
361 *
362 * Returns:
363 * Zero on success, negative errno on failure.
364 */
cde4c44d
DV
365int drm_connector_attach_encoder(struct drm_connector *connector,
366 struct drm_encoder *encoder)
52217195
DV
367{
368 int i;
369
370 /*
371 * In the past, drivers have attempted to model the static association
372 * of connector to encoder in simple connector/encoder devices using a
373 * direct assignment of connector->encoder = encoder. This connection
374 * is a logical one and the responsibility of the core, so drivers are
375 * expected not to mess with this.
376 *
377 * Note that the error return should've been enough here, but a large
378 * majority of drivers ignores the return value, so add in a big WARN
379 * to get people's attention.
380 */
381 if (WARN_ON(connector->encoder))
382 return -EINVAL;
383
83aefbb8 384 for (i = 0; i < ARRAY_SIZE(connector->encoder_ids); i++) {
52217195
DV
385 if (connector->encoder_ids[i] == 0) {
386 connector->encoder_ids[i] = encoder->base.id;
387 return 0;
388 }
389 }
390 return -ENOMEM;
391}
cde4c44d 392EXPORT_SYMBOL(drm_connector_attach_encoder);
52217195 393
38cb8d96
VS
394/**
395 * drm_connector_has_possible_encoder - check if the connector and encoder are assosicated with each other
396 * @connector: the connector
397 * @encoder: the encoder
398 *
399 * Returns:
400 * True if @encoder is one of the possible encoders for @connector.
401 */
402bool drm_connector_has_possible_encoder(struct drm_connector *connector,
403 struct drm_encoder *encoder)
404{
405 struct drm_encoder *enc;
406 int i;
407
408 drm_connector_for_each_possible_encoder(connector, enc, i) {
409 if (enc == encoder)
410 return true;
411 }
412
413 return false;
414}
415EXPORT_SYMBOL(drm_connector_has_possible_encoder);
416
52217195
DV
417static void drm_mode_remove(struct drm_connector *connector,
418 struct drm_display_mode *mode)
419{
420 list_del(&mode->head);
421 drm_mode_destroy(connector->dev, mode);
422}
423
424/**
425 * drm_connector_cleanup - cleans up an initialised connector
426 * @connector: connector to cleanup
427 *
428 * Cleans up the connector but doesn't free the object.
429 */
430void drm_connector_cleanup(struct drm_connector *connector)
431{
432 struct drm_device *dev = connector->dev;
433 struct drm_display_mode *mode, *t;
434
435 /* The connector should have been removed from userspace long before
436 * it is finally destroyed.
437 */
39b50c60
LP
438 if (WARN_ON(connector->registration_state ==
439 DRM_CONNECTOR_REGISTERED))
52217195
DV
440 drm_connector_unregister(connector);
441
442 if (connector->tile_group) {
443 drm_mode_put_tile_group(dev, connector->tile_group);
444 connector->tile_group = NULL;
445 }
446
447 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
448 drm_mode_remove(connector, mode);
449
450 list_for_each_entry_safe(mode, t, &connector->modes, head)
451 drm_mode_remove(connector, mode);
452
9a47dba1
CJ
453 ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
454 connector->connector_type_id);
52217195 455
9a47dba1
CJ
456 ida_simple_remove(&dev->mode_config.connector_ida,
457 connector->index);
52217195
DV
458
459 kfree(connector->display_info.bus_formats);
460 drm_mode_object_unregister(dev, &connector->base);
461 kfree(connector->name);
462 connector->name = NULL;
613051da 463 spin_lock_irq(&dev->mode_config.connector_list_lock);
52217195
DV
464 list_del(&connector->head);
465 dev->mode_config.num_connector--;
613051da 466 spin_unlock_irq(&dev->mode_config.connector_list_lock);
52217195
DV
467
468 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
469 if (connector->state && connector->funcs->atomic_destroy_state)
470 connector->funcs->atomic_destroy_state(connector,
471 connector->state);
472
e73ab00e
DV
473 mutex_destroy(&connector->mutex);
474
52217195
DV
475 memset(connector, 0, sizeof(*connector));
476}
477EXPORT_SYMBOL(drm_connector_cleanup);
478
479/**
480 * drm_connector_register - register a connector
481 * @connector: the connector to register
482 *
483 * Register userspace interfaces for a connector
484 *
485 * Returns:
486 * Zero on success, error code on failure.
487 */
488int drm_connector_register(struct drm_connector *connector)
489{
e73ab00e 490 int ret = 0;
52217195 491
e6e7b48b
DV
492 if (!connector->dev->registered)
493 return 0;
494
e73ab00e 495 mutex_lock(&connector->mutex);
39b50c60 496 if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
e73ab00e 497 goto unlock;
52217195
DV
498
499 ret = drm_sysfs_connector_add(connector);
500 if (ret)
e73ab00e 501 goto unlock;
52217195 502
b792e640 503 drm_debugfs_connector_add(connector);
52217195
DV
504
505 if (connector->funcs->late_register) {
506 ret = connector->funcs->late_register(connector);
507 if (ret)
508 goto err_debugfs;
509 }
510
511 drm_mode_object_register(connector->dev, &connector->base);
512
39b50c60 513 connector->registration_state = DRM_CONNECTOR_REGISTERED;
e73ab00e 514 goto unlock;
52217195
DV
515
516err_debugfs:
517 drm_debugfs_connector_remove(connector);
52217195 518 drm_sysfs_connector_remove(connector);
e73ab00e
DV
519unlock:
520 mutex_unlock(&connector->mutex);
52217195
DV
521 return ret;
522}
523EXPORT_SYMBOL(drm_connector_register);
524
525/**
526 * drm_connector_unregister - unregister a connector
527 * @connector: the connector to unregister
528 *
529 * Unregister userspace interfaces for a connector
530 */
531void drm_connector_unregister(struct drm_connector *connector)
532{
e73ab00e 533 mutex_lock(&connector->mutex);
39b50c60 534 if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
e73ab00e 535 mutex_unlock(&connector->mutex);
52217195 536 return;
e73ab00e 537 }
52217195
DV
538
539 if (connector->funcs->early_unregister)
540 connector->funcs->early_unregister(connector);
541
542 drm_sysfs_connector_remove(connector);
543 drm_debugfs_connector_remove(connector);
544
39b50c60 545 connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
e73ab00e 546 mutex_unlock(&connector->mutex);
52217195
DV
547}
548EXPORT_SYMBOL(drm_connector_unregister);
549
550void drm_connector_unregister_all(struct drm_device *dev)
551{
552 struct drm_connector *connector;
613051da 553 struct drm_connector_list_iter conn_iter;
52217195 554
b982dab1 555 drm_connector_list_iter_begin(dev, &conn_iter);
613051da 556 drm_for_each_connector_iter(connector, &conn_iter)
52217195 557 drm_connector_unregister(connector);
b982dab1 558 drm_connector_list_iter_end(&conn_iter);
52217195
DV
559}
560
561int drm_connector_register_all(struct drm_device *dev)
562{
563 struct drm_connector *connector;
613051da
DV
564 struct drm_connector_list_iter conn_iter;
565 int ret = 0;
52217195 566
b982dab1 567 drm_connector_list_iter_begin(dev, &conn_iter);
613051da 568 drm_for_each_connector_iter(connector, &conn_iter) {
52217195
DV
569 ret = drm_connector_register(connector);
570 if (ret)
613051da 571 break;
52217195 572 }
b982dab1 573 drm_connector_list_iter_end(&conn_iter);
52217195 574
613051da
DV
575 if (ret)
576 drm_connector_unregister_all(dev);
52217195
DV
577 return ret;
578}
579
580/**
581 * drm_get_connector_status_name - return a string for connector status
582 * @status: connector status to compute name of
583 *
584 * In contrast to the other drm_get_*_name functions this one here returns a
585 * const pointer and hence is threadsafe.
586 */
587const char *drm_get_connector_status_name(enum drm_connector_status status)
588{
589 if (status == connector_status_connected)
590 return "connected";
591 else if (status == connector_status_disconnected)
592 return "disconnected";
593 else
594 return "unknown";
595}
596EXPORT_SYMBOL(drm_get_connector_status_name);
597
6140cf20
JN
598/**
599 * drm_get_connector_force_name - return a string for connector force
600 * @force: connector force to get name of
601 *
602 * Returns: const pointer to name.
603 */
604const char *drm_get_connector_force_name(enum drm_connector_force force)
605{
606 switch (force) {
607 case DRM_FORCE_UNSPECIFIED:
608 return "unspecified";
609 case DRM_FORCE_OFF:
610 return "off";
611 case DRM_FORCE_ON:
612 return "on";
613 case DRM_FORCE_ON_DIGITAL:
614 return "digital";
615 default:
616 return "unknown";
617 }
618}
619
613051da
DV
620#ifdef CONFIG_LOCKDEP
621static struct lockdep_map connector_list_iter_dep_map = {
622 .name = "drm_connector_list_iter"
623};
624#endif
625
626/**
b982dab1 627 * drm_connector_list_iter_begin - initialize a connector_list iterator
613051da
DV
628 * @dev: DRM device
629 * @iter: connector_list iterator
630 *
d574528a 631 * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
b982dab1 632 * must always be cleaned up again by calling drm_connector_list_iter_end().
613051da
DV
633 * Iteration itself happens using drm_connector_list_iter_next() or
634 * drm_for_each_connector_iter().
635 */
b982dab1
TR
636void drm_connector_list_iter_begin(struct drm_device *dev,
637 struct drm_connector_list_iter *iter)
613051da
DV
638{
639 iter->dev = dev;
640 iter->conn = NULL;
641 lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
642}
b982dab1 643EXPORT_SYMBOL(drm_connector_list_iter_begin);
613051da 644
a703c550
DV
645/*
646 * Extra-safe connector put function that works in any context. Should only be
647 * used from the connector_iter functions, where we never really expect to
648 * actually release the connector when dropping our final reference.
649 */
650static void
ea497bb9 651__drm_connector_put_safe(struct drm_connector *conn)
a703c550 652{
ea497bb9
DV
653 struct drm_mode_config *config = &conn->dev->mode_config;
654
655 lockdep_assert_held(&config->connector_list_lock);
656
657 if (!refcount_dec_and_test(&conn->base.refcount.refcount))
658 return;
659
660 llist_add(&conn->free_node, &config->connector_free_list);
661 schedule_work(&config->connector_free_work);
a703c550
DV
662}
663
613051da
DV
664/**
665 * drm_connector_list_iter_next - return next connector
4f45c778 666 * @iter: connector_list iterator
613051da
DV
667 *
668 * Returns the next connector for @iter, or NULL when the list walk has
669 * completed.
670 */
671struct drm_connector *
672drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
673{
674 struct drm_connector *old_conn = iter->conn;
675 struct drm_mode_config *config = &iter->dev->mode_config;
676 struct list_head *lhead;
677 unsigned long flags;
678
679 spin_lock_irqsave(&config->connector_list_lock, flags);
680 lhead = old_conn ? &old_conn->head : &config->connector_list;
681
682 do {
683 if (lhead->next == &config->connector_list) {
684 iter->conn = NULL;
685 break;
686 }
687
688 lhead = lhead->next;
689 iter->conn = list_entry(lhead, struct drm_connector, head);
690
691 /* loop until it's not a zombie connector */
692 } while (!kref_get_unless_zero(&iter->conn->base.refcount));
613051da
DV
693
694 if (old_conn)
ea497bb9
DV
695 __drm_connector_put_safe(old_conn);
696 spin_unlock_irqrestore(&config->connector_list_lock, flags);
613051da
DV
697
698 return iter->conn;
699}
700EXPORT_SYMBOL(drm_connector_list_iter_next);
701
702/**
b982dab1 703 * drm_connector_list_iter_end - tear down a connector_list iterator
613051da
DV
704 * @iter: connector_list iterator
705 *
706 * Tears down @iter and releases any resources (like &drm_connector references)
707 * acquired while walking the list. This must always be called, both when the
708 * iteration completes fully or when it was aborted without walking the entire
709 * list.
710 */
b982dab1 711void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
613051da 712{
ea497bb9
DV
713 struct drm_mode_config *config = &iter->dev->mode_config;
714 unsigned long flags;
715
613051da 716 iter->dev = NULL;
ea497bb9
DV
717 if (iter->conn) {
718 spin_lock_irqsave(&config->connector_list_lock, flags);
719 __drm_connector_put_safe(iter->conn);
720 spin_unlock_irqrestore(&config->connector_list_lock, flags);
721 }
613051da
DV
722 lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
723}
b982dab1 724EXPORT_SYMBOL(drm_connector_list_iter_end);
613051da 725
52217195
DV
726static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
727 { SubPixelUnknown, "Unknown" },
728 { SubPixelHorizontalRGB, "Horizontal RGB" },
729 { SubPixelHorizontalBGR, "Horizontal BGR" },
730 { SubPixelVerticalRGB, "Vertical RGB" },
731 { SubPixelVerticalBGR, "Vertical BGR" },
732 { SubPixelNone, "None" },
733};
734
735/**
736 * drm_get_subpixel_order_name - return a string for a given subpixel enum
737 * @order: enum of subpixel_order
738 *
739 * Note you could abuse this and return something out of bounds, but that
740 * would be a caller error. No unscrubbed user data should make it here.
741 */
742const char *drm_get_subpixel_order_name(enum subpixel_order order)
743{
744 return drm_subpixel_enum_list[order].name;
745}
746EXPORT_SYMBOL(drm_get_subpixel_order_name);
747
748static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
749 { DRM_MODE_DPMS_ON, "On" },
750 { DRM_MODE_DPMS_STANDBY, "Standby" },
751 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
752 { DRM_MODE_DPMS_OFF, "Off" }
753};
754DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
755
40ee6fbe
MN
756static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
757 { DRM_MODE_LINK_STATUS_GOOD, "Good" },
758 { DRM_MODE_LINK_STATUS_BAD, "Bad" },
759};
40ee6fbe 760
b3c6c8bf
DV
761/**
762 * drm_display_info_set_bus_formats - set the supported bus formats
763 * @info: display info to store bus formats in
764 * @formats: array containing the supported bus formats
765 * @num_formats: the number of entries in the fmts array
766 *
767 * Store the supported bus formats in display info structure.
768 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
769 * a full list of available formats.
770 */
771int drm_display_info_set_bus_formats(struct drm_display_info *info,
772 const u32 *formats,
773 unsigned int num_formats)
774{
775 u32 *fmts = NULL;
776
777 if (!formats && num_formats)
778 return -EINVAL;
779
780 if (formats && num_formats) {
781 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
782 GFP_KERNEL);
783 if (!fmts)
784 return -ENOMEM;
785 }
786
787 kfree(info->bus_formats);
788 info->bus_formats = fmts;
789 info->num_bus_formats = num_formats;
790
791 return 0;
792}
793EXPORT_SYMBOL(drm_display_info_set_bus_formats);
794
52217195
DV
795/* Optional connector properties. */
796static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
797 { DRM_MODE_SCALE_NONE, "None" },
798 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
799 { DRM_MODE_SCALE_CENTER, "Center" },
800 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
801};
802
803static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
804 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
805 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
806 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
807};
808
50525c33
SL
809static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
810 { DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
811 { DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
812 { DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
813 { DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
814 { DRM_MODE_CONTENT_TYPE_GAME, "Game" },
815};
816
8d70f395
HG
817static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
818 { DRM_MODE_PANEL_ORIENTATION_NORMAL, "Normal" },
819 { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down" },
820 { DRM_MODE_PANEL_ORIENTATION_LEFT_UP, "Left Side Up" },
821 { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP, "Right Side Up" },
822};
823
52217195
DV
824static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
825 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
826 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
827 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
828};
829DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
830
831static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
832 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
833 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
834 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
835};
836DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
837 drm_dvi_i_subconnector_enum_list)
838
839static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
840 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
841 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
842 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
843 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
844 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
845};
846DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
847
848static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
849 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
850 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
851 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
852 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
853 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
854};
855DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
856 drm_tv_subconnector_enum_list)
857
d2c6a405
US
858static const struct drm_prop_enum_list hdmi_colorspaces[] = {
859 /* For Default case, driver will set the colorspace */
860 { DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
861 /* Standard Definition Colorimetry based on CEA 861 */
862 { DRM_MODE_COLORIMETRY_SMPTE_170M_YCC, "SMPTE_170M_YCC" },
863 { DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
864 /* Standard Definition Colorimetry based on IEC 61966-2-4 */
865 { DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
866 /* High Definition Colorimetry based on IEC 61966-2-4 */
867 { DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
868 /* Colorimetry based on IEC 61966-2-1/Amendment 1 */
869 { DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
870 /* Colorimetry based on IEC 61966-2-5 [33] */
871 { DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
872 /* Colorimetry based on IEC 61966-2-5 */
873 { DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
874 /* Colorimetry based on ITU-R BT.2020 */
875 { DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
876 /* Colorimetry based on ITU-R BT.2020 */
877 { DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
878 /* Colorimetry based on ITU-R BT.2020 */
879 { DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
880 /* Added as part of Additional Colorimetry Extension in 861.G */
881 { DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
882 { DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER, "DCI-P3_RGB_Theater" },
883};
884
4ada6f22
DV
885/**
886 * DOC: standard connector properties
887 *
888 * DRM connectors have a few standardized properties:
889 *
890 * EDID:
891 * Blob property which contains the current EDID read from the sink. This
892 * is useful to parse sink identification information like vendor, model
893 * and serial. Drivers should update this property by calling
c555f023 894 * drm_connector_update_edid_property(), usually after having parsed
4ada6f22
DV
895 * the EDID using drm_add_edid_modes(). Userspace cannot change this
896 * property.
897 * DPMS:
898 * Legacy property for setting the power state of the connector. For atomic
899 * drivers this is only provided for backwards compatibility with existing
900 * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
901 * connector is linked to. Drivers should never set this property directly,
d574528a 902 * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
144a7999
DV
903 * callback. For atomic drivers the remapping to the "ACTIVE" property is
904 * implemented in the DRM core. This is the only standard connector
905 * property that userspace can change.
d0d1aee5
DV
906 *
907 * Note that this property cannot be set through the MODE_ATOMIC ioctl,
908 * userspace must use "ACTIVE" on the CRTC instead.
909 *
910 * WARNING:
911 *
912 * For userspace also running on legacy drivers the "DPMS" semantics are a
913 * lot more complicated. First, userspace cannot rely on the "DPMS" value
914 * returned by the GETCONNECTOR actually reflecting reality, because many
915 * drivers fail to update it. For atomic drivers this is taken care of in
916 * drm_atomic_helper_update_legacy_modeset_state().
917 *
918 * The second issue is that the DPMS state is only well-defined when the
919 * connector is connected to a CRTC. In atomic the DRM core enforces that
920 * "ACTIVE" is off in such a case, no such checks exists for "DPMS".
921 *
922 * Finally, when enabling an output using the legacy SETCONFIG ioctl then
923 * "DPMS" is forced to ON. But see above, that might not be reflected in
924 * the software value on legacy drivers.
925 *
926 * Summarizing: Only set "DPMS" when the connector is known to be enabled,
927 * assume that a successful SETCONFIG call also sets "DPMS" to on, and
928 * never read back the value of "DPMS" because it can be incorrect.
4ada6f22
DV
929 * PATH:
930 * Connector path property to identify how this sink is physically
931 * connected. Used by DP MST. This should be set by calling
97e14fbe 932 * drm_connector_set_path_property(), in the case of DP MST with the
4ada6f22
DV
933 * path property the MST manager created. Userspace cannot change this
934 * property.
935 * TILE:
936 * Connector tile group property to indicate how a set of DRM connector
937 * compose together into one logical screen. This is used by both high-res
938 * external screens (often only using a single cable, but exposing multiple
939 * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
940 * are not gen-locked. Note that for tiled panels which are genlocked, like
941 * dual-link LVDS or dual-link DSI, the driver should try to not expose the
942 * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
97e14fbe 943 * should update this value using drm_connector_set_tile_property().
4ada6f22 944 * Userspace cannot change this property.
40ee6fbe 945 * link-status:
716719a3
SP
946 * Connector link-status property to indicate the status of link. The
947 * default value of link-status is "GOOD". If something fails during or
948 * after modeset, the kernel driver may set this to "BAD" and issue a
949 * hotplug uevent. Drivers should update this value using
97e14fbe 950 * drm_connector_set_link_status_property().
66660d4c
DA
951 * non_desktop:
952 * Indicates the output should be ignored for purposes of displaying a
953 * standard desktop environment or console. This is most likely because
954 * the output device is not rectilinear.
24557865
SP
955 * Content Protection:
956 * This property is used by userspace to request the kernel protect future
957 * content communicated over the link. When requested, kernel will apply
958 * the appropriate means of protection (most often HDCP), and use the
959 * property to tell userspace the protection is active.
960 *
961 * Drivers can set this up by calling
962 * drm_connector_attach_content_protection_property() on initialization.
963 *
964 * The value of this property can be one of the following:
965 *
bbeba09f 966 * DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
24557865 967 * The link is not protected, content is transmitted in the clear.
bbeba09f 968 * DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
24557865
SP
969 * Userspace has requested content protection, but the link is not
970 * currently protected. When in this state, kernel should enable
971 * Content Protection as soon as possible.
bbeba09f 972 * DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
24557865
SP
973 * Userspace has requested content protection, and the link is
974 * protected. Only the driver can set the property to this value.
975 * If userspace attempts to set to ENABLED, kernel will return
976 * -EINVAL.
977 *
978 * A few guidelines:
979 *
980 * - DESIRED state should be preserved until userspace de-asserts it by
981 * setting the property to UNDESIRED. This means ENABLED should only
982 * transition to UNDESIRED when the user explicitly requests it.
983 * - If the state is DESIRED, kernel should attempt to re-authenticate the
984 * link whenever possible. This includes across disable/enable, dpms,
985 * hotplug, downstream device changes, link status failures, etc..
986 * - Userspace is responsible for polling the property to determine when
987 * the value transitions from ENABLED to DESIRED. This signifies the link
988 * is no longer protected and userspace should take appropriate action
989 * (whatever that might be).
4ada6f22 990 *
a09db883
US
991 * HDR_OUTPUT_METADATA:
992 * Connector property to enable userspace to send HDR Metadata to
993 * driver. This metadata is based on the composition and blending
994 * policies decided by user, taking into account the hardware and
995 * sink capabilities. The driver gets this metadata and creates a
996 * Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
997 * SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
998 * sent to sink. This notifies the sink of the upcoming frame's Color
999 * Encoding and Luminance parameters.
1000 *
1001 * Userspace first need to detect the HDR capabilities of sink by
1002 * reading and parsing the EDID. Details of HDR metadata for HDMI
1003 * are added in CTA 861.G spec. For DP , its defined in VESA DP
1004 * Standard v1.4. It needs to then get the metadata information
1005 * of the video/game/app content which are encoded in HDR (basically
1006 * using HDR transfer functions). With this information it needs to
1007 * decide on a blending policy and compose the relevant
1008 * layers/overlays into a common format. Once this blending is done,
1009 * userspace will be aware of the metadata of the composed frame to
1010 * be send to sink. It then uses this property to communicate this
1011 * metadata to driver which then make a Infoframe packet and sends
1012 * to sink based on the type of encoder connected.
1013 *
1014 * Userspace will be responsible to do Tone mapping operation in case:
1015 * - Some layers are HDR and others are SDR
1016 * - HDR layers luminance is not same as sink
9f9b2559 1017 *
a09db883
US
1018 * It will even need to do colorspace conversion and get all layers
1019 * to one common colorspace for blending. It can use either GL, Media
1020 * or display engine to get this done based on the capabilties of the
1021 * associated hardware.
1022 *
1023 * Driver expects metadata to be put in &struct hdr_output_metadata
1024 * structure from userspace. This is received as blob and stored in
1025 * &drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1026 * sink metadata in &struct hdr_sink_metadata, as
1027 * &drm_connector.hdr_sink_metadata. Driver uses
1028 * drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1029 * hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1030 * HDMI encoder.
1031 *
47e22ff1
RS
1032 * max bpc:
1033 * This range property is used by userspace to limit the bit depth. When
1034 * used the driver would limit the bpc in accordance with the valid range
1035 * supported by the hardware and sink. Drivers to use the function
1036 * drm_connector_attach_max_bpc_property() to create and attach the
1037 * property to the connector during initialization.
1038 *
4ada6f22
DV
1039 * Connectors also have one standardized atomic property:
1040 *
1041 * CRTC_ID:
1042 * Mode object ID of the &drm_crtc this connector should be connected to.
8d70f395
HG
1043 *
1044 * Connectors for LCD panels may also have one standardized property:
1045 *
1046 * panel orientation:
1047 * On some devices the LCD panel is mounted in the casing in such a way
1048 * that the up/top side of the panel does not match with the top side of
1049 * the device. Userspace can use this property to check for this.
1050 * Note that input coordinates from touchscreens (input devices with
1051 * INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
1052 * coordinates, so if userspace rotates the picture to adjust for
1053 * the orientation it must also apply the same transformation to the
bbeba09f
DV
1054 * touchscreen input coordinates. This property is initialized by calling
1055 * drm_connector_init_panel_orientation_property().
1056 *
1057 * scaling mode:
1058 * This property defines how a non-native mode is upscaled to the native
1059 * mode of an LCD panel:
1060 *
1061 * None:
1062 * No upscaling happens, scaling is left to the panel. Not all
1063 * drivers expose this mode.
1064 * Full:
1065 * The output is upscaled to the full resolution of the panel,
1066 * ignoring the aspect ratio.
1067 * Center:
1068 * No upscaling happens, the output is centered within the native
1069 * resolution the panel.
1070 * Full aspect:
1071 * The output is upscaled to maximize either the width or height
1072 * while retaining the aspect ratio.
1073 *
1074 * This property should be set up by calling
1075 * drm_connector_attach_scaling_mode_property(). Note that drivers
1076 * can also expose this property to external outputs, in which case they
1077 * must support "None", which should be the default (since external screens
1078 * have a built-in scaler).
4ada6f22
DV
1079 */
1080
52217195
DV
1081int drm_connector_create_standard_properties(struct drm_device *dev)
1082{
1083 struct drm_property *prop;
1084
1085 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1086 DRM_MODE_PROP_IMMUTABLE,
1087 "EDID", 0);
1088 if (!prop)
1089 return -ENOMEM;
1090 dev->mode_config.edid_property = prop;
1091
1092 prop = drm_property_create_enum(dev, 0,
1093 "DPMS", drm_dpms_enum_list,
1094 ARRAY_SIZE(drm_dpms_enum_list));
1095 if (!prop)
1096 return -ENOMEM;
1097 dev->mode_config.dpms_property = prop;
1098
1099 prop = drm_property_create(dev,
1100 DRM_MODE_PROP_BLOB |
1101 DRM_MODE_PROP_IMMUTABLE,
1102 "PATH", 0);
1103 if (!prop)
1104 return -ENOMEM;
1105 dev->mode_config.path_property = prop;
1106
1107 prop = drm_property_create(dev,
1108 DRM_MODE_PROP_BLOB |
1109 DRM_MODE_PROP_IMMUTABLE,
1110 "TILE", 0);
1111 if (!prop)
1112 return -ENOMEM;
1113 dev->mode_config.tile_property = prop;
1114
40ee6fbe
MN
1115 prop = drm_property_create_enum(dev, 0, "link-status",
1116 drm_link_status_enum_list,
1117 ARRAY_SIZE(drm_link_status_enum_list));
1118 if (!prop)
1119 return -ENOMEM;
1120 dev->mode_config.link_status_property = prop;
1121
66660d4c
DA
1122 prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1123 if (!prop)
1124 return -ENOMEM;
1125 dev->mode_config.non_desktop_property = prop;
1126
fbb5d035
US
1127 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1128 "HDR_OUTPUT_METADATA", 0);
1129 if (!prop)
1130 return -ENOMEM;
1131 dev->mode_config.hdr_output_metadata_property = prop;
1132
52217195
DV
1133 return 0;
1134}
1135
1136/**
1137 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1138 * @dev: DRM device
1139 *
1140 * Called by a driver the first time a DVI-I connector is made.
1141 */
1142int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1143{
1144 struct drm_property *dvi_i_selector;
1145 struct drm_property *dvi_i_subconnector;
1146
1147 if (dev->mode_config.dvi_i_select_subconnector_property)
1148 return 0;
1149
1150 dvi_i_selector =
1151 drm_property_create_enum(dev, 0,
1152 "select subconnector",
1153 drm_dvi_i_select_enum_list,
1154 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1155 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1156
1157 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1158 "subconnector",
1159 drm_dvi_i_subconnector_enum_list,
1160 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1161 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1162
1163 return 0;
1164}
1165EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1166
50525c33
SL
1167/**
1168 * DOC: HDMI connector properties
1169 *
1170 * content type (HDMI specific):
1171 * Indicates content type setting to be used in HDMI infoframes to indicate
1e55a53a 1172 * content type for the external device, so that it adjusts its display
50525c33
SL
1173 * settings accordingly.
1174 *
1175 * The value of this property can be one of the following:
1176 *
1177 * No Data:
1178 * Content type is unknown
1179 * Graphics:
1180 * Content type is graphics
1181 * Photo:
1182 * Content type is photo
1183 * Cinema:
1184 * Content type is cinema
1185 * Game:
1186 * Content type is game
1187 *
1188 * Drivers can set up this property by calling
1189 * drm_connector_attach_content_type_property(). Decoding to
ba609631 1190 * infoframe values is done through drm_hdmi_avi_infoframe_content_type().
50525c33
SL
1191 */
1192
1193/**
1194 * drm_connector_attach_content_type_property - attach content-type property
1195 * @connector: connector to attach content type property on.
1196 *
1197 * Called by a driver the first time a HDMI connector is made.
1198 */
1199int drm_connector_attach_content_type_property(struct drm_connector *connector)
1200{
1201 if (!drm_mode_create_content_type_property(connector->dev))
1202 drm_object_attach_property(&connector->base,
1203 connector->dev->mode_config.content_type_property,
1204 DRM_MODE_CONTENT_TYPE_NO_DATA);
1205 return 0;
1206}
1207EXPORT_SYMBOL(drm_connector_attach_content_type_property);
1208
1209
1210/**
1211 * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
1212 * content type information, based
1213 * on correspondent DRM property.
1214 * @frame: HDMI AVI infoframe
1215 * @conn_state: DRM display connector state
1216 *
1217 */
1218void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
1219 const struct drm_connector_state *conn_state)
1220{
1221 switch (conn_state->content_type) {
1222 case DRM_MODE_CONTENT_TYPE_GRAPHICS:
1223 frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
1224 break;
1225 case DRM_MODE_CONTENT_TYPE_CINEMA:
1226 frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
1227 break;
1228 case DRM_MODE_CONTENT_TYPE_GAME:
1229 frame->content_type = HDMI_CONTENT_TYPE_GAME;
1230 break;
1231 case DRM_MODE_CONTENT_TYPE_PHOTO:
1232 frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
1233 break;
1234 default:
1235 /* Graphics is the default(0) */
1236 frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
1237 }
1238
1239 frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
1240}
1241EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
1242
52217195 1243/**
6c4f52dc
BB
1244 * drm_mode_attach_tv_margin_properties - attach TV connector margin properties
1245 * @connector: DRM connector
1246 *
1247 * Called by a driver when it needs to attach TV margin props to a connector.
1248 * Typically used on SDTV and HDMI connectors.
1249 */
1250void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
1251{
1252 struct drm_device *dev = connector->dev;
1253
1254 drm_object_attach_property(&connector->base,
1255 dev->mode_config.tv_left_margin_property,
1256 0);
1257 drm_object_attach_property(&connector->base,
1258 dev->mode_config.tv_right_margin_property,
1259 0);
1260 drm_object_attach_property(&connector->base,
1261 dev->mode_config.tv_top_margin_property,
1262 0);
1263 drm_object_attach_property(&connector->base,
1264 dev->mode_config.tv_bottom_margin_property,
1265 0);
1266}
1267EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
1268
1269/**
1270 * drm_mode_create_tv_margin_properties - create TV connector margin properties
1271 * @dev: DRM device
1272 *
1273 * Called by a driver's HDMI connector initialization routine, this function
1274 * creates the TV margin properties for a given device. No need to call this
1275 * function for an SDTV connector, it's already called from
1276 * drm_mode_create_tv_properties().
1277 */
1278int drm_mode_create_tv_margin_properties(struct drm_device *dev)
1279{
1280 if (dev->mode_config.tv_left_margin_property)
1281 return 0;
1282
1283 dev->mode_config.tv_left_margin_property =
1284 drm_property_create_range(dev, 0, "left margin", 0, 100);
1285 if (!dev->mode_config.tv_left_margin_property)
1286 return -ENOMEM;
1287
1288 dev->mode_config.tv_right_margin_property =
1289 drm_property_create_range(dev, 0, "right margin", 0, 100);
1290 if (!dev->mode_config.tv_right_margin_property)
1291 return -ENOMEM;
1292
1293 dev->mode_config.tv_top_margin_property =
1294 drm_property_create_range(dev, 0, "top margin", 0, 100);
1295 if (!dev->mode_config.tv_top_margin_property)
1296 return -ENOMEM;
1297
1298 dev->mode_config.tv_bottom_margin_property =
1299 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1300 if (!dev->mode_config.tv_bottom_margin_property)
1301 return -ENOMEM;
1302
1303 return 0;
1304}
1305EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
1306
52217195 1307/**
eda6887f 1308 * drm_mode_create_tv_properties - create TV specific connector properties
52217195
DV
1309 * @dev: DRM device
1310 * @num_modes: number of different TV formats (modes) supported
1311 * @modes: array of pointers to strings containing name of each format
1312 *
1313 * Called by a driver's TV initialization routine, this function creates
1314 * the TV specific connector properties for a given device. Caller is
1315 * responsible for allocating a list of format names and passing them to
1316 * this routine.
1317 */
1318int drm_mode_create_tv_properties(struct drm_device *dev,
1319 unsigned int num_modes,
1320 const char * const modes[])
1321{
1322 struct drm_property *tv_selector;
1323 struct drm_property *tv_subconnector;
1324 unsigned int i;
1325
1326 if (dev->mode_config.tv_select_subconnector_property)
1327 return 0;
1328
1329 /*
1330 * Basic connector properties
1331 */
1332 tv_selector = drm_property_create_enum(dev, 0,
1333 "select subconnector",
1334 drm_tv_select_enum_list,
1335 ARRAY_SIZE(drm_tv_select_enum_list));
1336 if (!tv_selector)
1337 goto nomem;
1338
1339 dev->mode_config.tv_select_subconnector_property = tv_selector;
1340
1341 tv_subconnector =
1342 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1343 "subconnector",
1344 drm_tv_subconnector_enum_list,
1345 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1346 if (!tv_subconnector)
1347 goto nomem;
1348 dev->mode_config.tv_subconnector_property = tv_subconnector;
1349
1350 /*
1351 * Other, TV specific properties: margins & TV modes.
1352 */
6c4f52dc 1353 if (drm_mode_create_tv_margin_properties(dev))
52217195
DV
1354 goto nomem;
1355
1356 dev->mode_config.tv_mode_property =
1357 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1358 "mode", num_modes);
1359 if (!dev->mode_config.tv_mode_property)
1360 goto nomem;
1361
1362 for (i = 0; i < num_modes; i++)
30e9db6d 1363 drm_property_add_enum(dev->mode_config.tv_mode_property,
52217195
DV
1364 i, modes[i]);
1365
1366 dev->mode_config.tv_brightness_property =
1367 drm_property_create_range(dev, 0, "brightness", 0, 100);
1368 if (!dev->mode_config.tv_brightness_property)
1369 goto nomem;
1370
1371 dev->mode_config.tv_contrast_property =
1372 drm_property_create_range(dev, 0, "contrast", 0, 100);
1373 if (!dev->mode_config.tv_contrast_property)
1374 goto nomem;
1375
1376 dev->mode_config.tv_flicker_reduction_property =
1377 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1378 if (!dev->mode_config.tv_flicker_reduction_property)
1379 goto nomem;
1380
1381 dev->mode_config.tv_overscan_property =
1382 drm_property_create_range(dev, 0, "overscan", 0, 100);
1383 if (!dev->mode_config.tv_overscan_property)
1384 goto nomem;
1385
1386 dev->mode_config.tv_saturation_property =
1387 drm_property_create_range(dev, 0, "saturation", 0, 100);
1388 if (!dev->mode_config.tv_saturation_property)
1389 goto nomem;
1390
1391 dev->mode_config.tv_hue_property =
1392 drm_property_create_range(dev, 0, "hue", 0, 100);
1393 if (!dev->mode_config.tv_hue_property)
1394 goto nomem;
1395
1396 return 0;
1397nomem:
1398 return -ENOMEM;
1399}
1400EXPORT_SYMBOL(drm_mode_create_tv_properties);
1401
1402/**
1403 * drm_mode_create_scaling_mode_property - create scaling mode property
1404 * @dev: DRM device
1405 *
1406 * Called by a driver the first time it's needed, must be attached to desired
1407 * connectors.
8f6e1e22
ML
1408 *
1409 * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1410 * instead to correctly assign &drm_connector_state.picture_aspect_ratio
1411 * in the atomic state.
52217195
DV
1412 */
1413int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1414{
1415 struct drm_property *scaling_mode;
1416
1417 if (dev->mode_config.scaling_mode_property)
1418 return 0;
1419
1420 scaling_mode =
1421 drm_property_create_enum(dev, 0, "scaling mode",
1422 drm_scaling_mode_enum_list,
1423 ARRAY_SIZE(drm_scaling_mode_enum_list));
1424
1425 dev->mode_config.scaling_mode_property = scaling_mode;
1426
1427 return 0;
1428}
1429EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1430
ab7a664f
NK
1431/**
1432 * DOC: Variable refresh properties
1433 *
1434 * Variable refresh rate capable displays can dynamically adjust their
1435 * refresh rate by extending the duration of their vertical front porch
1436 * until page flip or timeout occurs. This can reduce or remove stuttering
1437 * and latency in scenarios where the page flip does not align with the
1438 * vblank interval.
1439 *
1440 * An example scenario would be an application flipping at a constant rate
1441 * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1442 * interval and the same contents will be displayed twice. This can be
1443 * observed as stuttering for content with motion.
1444 *
1445 * If variable refresh rate was active on a display that supported a
1446 * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1447 * for the example scenario. The minimum supported variable refresh rate of
1448 * 35Hz is below the page flip frequency and the vertical front porch can
1449 * be extended until the page flip occurs. The vblank interval will be
1450 * directly aligned to the page flip rate.
1451 *
1452 * Not all userspace content is suitable for use with variable refresh rate.
1453 * Large and frequent changes in vertical front porch duration may worsen
1454 * perceived stuttering for input sensitive applications.
1455 *
1456 * Panel brightness will also vary with vertical front porch duration. Some
1457 * panels may have noticeable differences in brightness between the minimum
1458 * vertical front porch duration and the maximum vertical front porch duration.
1459 * Large and frequent changes in vertical front porch duration may produce
1460 * observable flickering for such panels.
1461 *
1462 * Userspace control for variable refresh rate is supported via properties
1463 * on the &drm_connector and &drm_crtc objects.
1464 *
1465 * "vrr_capable":
1466 * Optional &drm_connector boolean property that drivers should attach
1467 * with drm_connector_attach_vrr_capable_property() on connectors that
1468 * could support variable refresh rates. Drivers should update the
1469 * property value by calling drm_connector_set_vrr_capable_property().
1470 *
1471 * Absence of the property should indicate absence of support.
1472 *
77086014 1473 * "VRR_ENABLED":
ab7a664f
NK
1474 * Default &drm_crtc boolean property that notifies the driver that the
1475 * content on the CRTC is suitable for variable refresh rate presentation.
1476 * The driver will take this property as a hint to enable variable
1477 * refresh rate support if the receiver supports it, ie. if the
1478 * "vrr_capable" property is true on the &drm_connector object. The
1479 * vertical front porch duration will be extended until page-flip or
1480 * timeout when enabled.
1481 *
1482 * The minimum vertical front porch duration is defined as the vertical
1483 * front porch duration for the current mode.
1484 *
1485 * The maximum vertical front porch duration is greater than or equal to
1486 * the minimum vertical front porch duration. The duration is derived
1487 * from the minimum supported variable refresh rate for the connector.
1488 *
1489 * The driver may place further restrictions within these minimum
1490 * and maximum bounds.
ab7a664f
NK
1491 */
1492
ba1b0f6c
NK
1493/**
1494 * drm_connector_attach_vrr_capable_property - creates the
1495 * vrr_capable property
1496 * @connector: connector to create the vrr_capable property on.
1497 *
1498 * This is used by atomic drivers to add support for querying
1499 * variable refresh rate capability for a connector.
1500 *
1501 * Returns:
1502 * Zero on success, negative errono on failure.
1503 */
1504int drm_connector_attach_vrr_capable_property(
1505 struct drm_connector *connector)
1506{
1507 struct drm_device *dev = connector->dev;
1508 struct drm_property *prop;
1509
1510 if (!connector->vrr_capable_property) {
1511 prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
1512 "vrr_capable");
1513 if (!prop)
1514 return -ENOMEM;
1515
1516 connector->vrr_capable_property = prop;
1517 drm_object_attach_property(&connector->base, prop, 0);
1518 }
1519
1520 return 0;
1521}
1522EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
1523
8f6e1e22
ML
1524/**
1525 * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1526 * @connector: connector to attach scaling mode property on.
1527 * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1528 *
1529 * This is used to add support for scaling mode to atomic drivers.
1530 * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
1531 * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1532 *
1533 * This is the atomic version of drm_mode_create_scaling_mode_property().
1534 *
1535 * Returns:
1536 * Zero on success, negative errno on failure.
1537 */
1538int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1539 u32 scaling_mode_mask)
1540{
1541 struct drm_device *dev = connector->dev;
1542 struct drm_property *scaling_mode_property;
30e9db6d 1543 int i;
8f6e1e22
ML
1544 const unsigned valid_scaling_mode_mask =
1545 (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1546
1547 if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1548 scaling_mode_mask & ~valid_scaling_mode_mask))
1549 return -EINVAL;
1550
1551 scaling_mode_property =
1552 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1553 hweight32(scaling_mode_mask));
1554
1555 if (!scaling_mode_property)
1556 return -ENOMEM;
1557
1558 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1559 int ret;
1560
1561 if (!(BIT(i) & scaling_mode_mask))
1562 continue;
1563
30e9db6d 1564 ret = drm_property_add_enum(scaling_mode_property,
8f6e1e22
ML
1565 drm_scaling_mode_enum_list[i].type,
1566 drm_scaling_mode_enum_list[i].name);
1567
1568 if (ret) {
1569 drm_property_destroy(dev, scaling_mode_property);
1570
1571 return ret;
1572 }
1573 }
1574
1575 drm_object_attach_property(&connector->base,
1576 scaling_mode_property, 0);
1577
1578 connector->scaling_mode_property = scaling_mode_property;
1579
1580 return 0;
1581}
1582EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1583
52217195
DV
1584/**
1585 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1586 * @dev: DRM device
1587 *
1588 * Called by a driver the first time it's needed, must be attached to desired
1589 * connectors.
1590 *
1591 * Returns:
1592 * Zero on success, negative errno on failure.
1593 */
1594int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1595{
1596 if (dev->mode_config.aspect_ratio_property)
1597 return 0;
1598
1599 dev->mode_config.aspect_ratio_property =
1600 drm_property_create_enum(dev, 0, "aspect ratio",
1601 drm_aspect_ratio_enum_list,
1602 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1603
1604 if (dev->mode_config.aspect_ratio_property == NULL)
1605 return -ENOMEM;
1606
1607 return 0;
1608}
1609EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1610
d2c6a405
US
1611/**
1612 * DOC: standard connector properties
1613 *
1614 * Colorspace:
1615 * drm_mode_create_colorspace_property - create colorspace property
1616 * This property helps select a suitable colorspace based on the sink
1617 * capability. Modern sink devices support wider gamut like BT2020.
1618 * This helps switch to BT2020 mode if the BT2020 encoded video stream
1619 * is being played by the user, same for any other colorspace. Thereby
1620 * giving a good visual experience to users.
1621 *
1622 * The expectation from userspace is that it should parse the EDID
1623 * and get supported colorspaces. Use this property and switch to the
1624 * one supported. Sink supported colorspaces should be retrieved by
1625 * userspace from EDID and driver will not explicitly expose them.
1626 *
1627 * Basically the expectation from userspace is:
1628 * - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
1629 * colorspace
1630 * - Set this new property to let the sink know what it
1631 * converted the CRTC output to.
1632 * - This property is just to inform sink what colorspace
1633 * source is trying to drive.
1634 *
1635 * Called by a driver the first time it's needed, must be attached to desired
1636 * connectors.
1637 */
1638int drm_mode_create_colorspace_property(struct drm_connector *connector)
1639{
1640 struct drm_device *dev = connector->dev;
1641 struct drm_property *prop;
1642
1643 if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
1644 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
1645 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
1646 "Colorspace",
1647 hdmi_colorspaces,
1648 ARRAY_SIZE(hdmi_colorspaces));
1649 if (!prop)
1650 return -ENOMEM;
1651 } else {
1652 DRM_DEBUG_KMS("Colorspace property not supported\n");
1653 return 0;
1654 }
1655
1656 connector->colorspace_property = prop;
1657
1658 return 0;
1659}
1660EXPORT_SYMBOL(drm_mode_create_colorspace_property);
1661
50525c33
SL
1662/**
1663 * drm_mode_create_content_type_property - create content type property
1664 * @dev: DRM device
1665 *
1666 * Called by a driver the first time it's needed, must be attached to desired
1667 * connectors.
1668 *
1669 * Returns:
1670 * Zero on success, negative errno on failure.
1671 */
1672int drm_mode_create_content_type_property(struct drm_device *dev)
1673{
1674 if (dev->mode_config.content_type_property)
1675 return 0;
1676
1677 dev->mode_config.content_type_property =
1678 drm_property_create_enum(dev, 0, "content type",
1679 drm_content_type_enum_list,
1680 ARRAY_SIZE(drm_content_type_enum_list));
1681
1682 if (dev->mode_config.content_type_property == NULL)
1683 return -ENOMEM;
1684
1685 return 0;
1686}
1687EXPORT_SYMBOL(drm_mode_create_content_type_property);
1688
52217195
DV
1689/**
1690 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1691 * @dev: DRM device
1692 *
1693 * Create the the suggested x/y offset property for connectors.
1694 */
1695int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1696{
1697 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1698 return 0;
1699
1700 dev->mode_config.suggested_x_property =
1701 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1702
1703 dev->mode_config.suggested_y_property =
1704 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1705
1706 if (dev->mode_config.suggested_x_property == NULL ||
1707 dev->mode_config.suggested_y_property == NULL)
1708 return -ENOMEM;
1709 return 0;
1710}
1711EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1712
1713/**
97e14fbe 1714 * drm_connector_set_path_property - set tile property on connector
52217195
DV
1715 * @connector: connector to set property on.
1716 * @path: path to use for property; must not be NULL.
1717 *
1718 * This creates a property to expose to userspace to specify a
1719 * connector path. This is mainly used for DisplayPort MST where
1720 * connectors have a topology and we want to allow userspace to give
1721 * them more meaningful names.
1722 *
1723 * Returns:
1724 * Zero on success, negative errno on failure.
1725 */
97e14fbe
DV
1726int drm_connector_set_path_property(struct drm_connector *connector,
1727 const char *path)
52217195
DV
1728{
1729 struct drm_device *dev = connector->dev;
1730 int ret;
1731
1732 ret = drm_property_replace_global_blob(dev,
1733 &connector->path_blob_ptr,
1734 strlen(path) + 1,
1735 path,
1736 &connector->base,
1737 dev->mode_config.path_property);
1738 return ret;
1739}
97e14fbe 1740EXPORT_SYMBOL(drm_connector_set_path_property);
52217195
DV
1741
1742/**
97e14fbe 1743 * drm_connector_set_tile_property - set tile property on connector
52217195
DV
1744 * @connector: connector to set property on.
1745 *
1746 * This looks up the tile information for a connector, and creates a
1747 * property for userspace to parse if it exists. The property is of
1748 * the form of 8 integers using ':' as a separator.
2de3a078
MN
1749 * This is used for dual port tiled displays with DisplayPort SST
1750 * or DisplayPort MST connectors.
52217195
DV
1751 *
1752 * Returns:
1753 * Zero on success, errno on failure.
1754 */
97e14fbe 1755int drm_connector_set_tile_property(struct drm_connector *connector)
52217195
DV
1756{
1757 struct drm_device *dev = connector->dev;
1758 char tile[256];
1759 int ret;
1760
1761 if (!connector->has_tile) {
1762 ret = drm_property_replace_global_blob(dev,
1763 &connector->tile_blob_ptr,
1764 0,
1765 NULL,
1766 &connector->base,
1767 dev->mode_config.tile_property);
1768 return ret;
1769 }
1770
1771 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1772 connector->tile_group->id, connector->tile_is_single_monitor,
1773 connector->num_h_tile, connector->num_v_tile,
1774 connector->tile_h_loc, connector->tile_v_loc,
1775 connector->tile_h_size, connector->tile_v_size);
1776
1777 ret = drm_property_replace_global_blob(dev,
1778 &connector->tile_blob_ptr,
1779 strlen(tile) + 1,
1780 tile,
1781 &connector->base,
1782 dev->mode_config.tile_property);
1783 return ret;
1784}
97e14fbe 1785EXPORT_SYMBOL(drm_connector_set_tile_property);
52217195
DV
1786
1787/**
c555f023 1788 * drm_connector_update_edid_property - update the edid property of a connector
52217195
DV
1789 * @connector: drm connector
1790 * @edid: new value of the edid property
1791 *
1792 * This function creates a new blob modeset object and assigns its id to the
1793 * connector's edid property.
2de3a078
MN
1794 * Since we also parse tile information from EDID's displayID block, we also
1795 * set the connector's tile property here. See drm_connector_set_tile_property()
1796 * for more details.
52217195
DV
1797 *
1798 * Returns:
1799 * Zero on success, negative errno on failure.
1800 */
c555f023 1801int drm_connector_update_edid_property(struct drm_connector *connector,
97e14fbe 1802 const struct edid *edid)
52217195
DV
1803{
1804 struct drm_device *dev = connector->dev;
1805 size_t size = 0;
1806 int ret;
1807
1808 /* ignore requests to set edid when overridden */
1809 if (connector->override_edid)
1810 return 0;
1811
1812 if (edid)
1813 size = EDID_LENGTH * (1 + edid->extensions);
1814
170178fe
KP
1815 /* Set the display info, using edid if available, otherwise
1816 * reseting the values to defaults. This duplicates the work
1817 * done in drm_add_edid_modes, but that function is not
1818 * consistently called before this one in all drivers and the
1819 * computation is cheap enough that it seems better to
1820 * duplicate it rather than attempt to ensure some arbitrary
1821 * ordering of calls.
1822 */
1823 if (edid)
1824 drm_add_display_info(connector, edid);
1825 else
1826 drm_reset_display_info(connector);
1827
66660d4c
DA
1828 drm_object_property_set_value(&connector->base,
1829 dev->mode_config.non_desktop_property,
1830 connector->display_info.non_desktop);
1831
52217195
DV
1832 ret = drm_property_replace_global_blob(dev,
1833 &connector->edid_blob_ptr,
1834 size,
1835 edid,
1836 &connector->base,
1837 dev->mode_config.edid_property);
2de3a078
MN
1838 if (ret)
1839 return ret;
1840 return drm_connector_set_tile_property(connector);
52217195 1841}
c555f023 1842EXPORT_SYMBOL(drm_connector_update_edid_property);
52217195 1843
40ee6fbe 1844/**
97e14fbe 1845 * drm_connector_set_link_status_property - Set link status property of a connector
40ee6fbe
MN
1846 * @connector: drm connector
1847 * @link_status: new value of link status property (0: Good, 1: Bad)
1848 *
1849 * In usual working scenario, this link status property will always be set to
1850 * "GOOD". If something fails during or after a mode set, the kernel driver
1851 * may set this link status property to "BAD". The caller then needs to send a
1852 * hotplug uevent for userspace to re-check the valid modes through
1853 * GET_CONNECTOR_IOCTL and retry modeset.
1854 *
1855 * Note: Drivers cannot rely on userspace to support this property and
1856 * issue a modeset. As such, they may choose to handle issues (like
1857 * re-training a link) without userspace's intervention.
1858 *
1859 * The reason for adding this property is to handle link training failures, but
1860 * it is not limited to DP or link training. For example, if we implement
1861 * asynchronous setcrtc, this property can be used to report any failures in that.
1862 */
97e14fbe
DV
1863void drm_connector_set_link_status_property(struct drm_connector *connector,
1864 uint64_t link_status)
40ee6fbe
MN
1865{
1866 struct drm_device *dev = connector->dev;
1867
1868 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1869 connector->state->link_status = link_status;
1870 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1871}
97e14fbe 1872EXPORT_SYMBOL(drm_connector_set_link_status_property);
40ee6fbe 1873
47e22ff1
RS
1874/**
1875 * drm_connector_attach_max_bpc_property - attach "max bpc" property
1876 * @connector: connector to attach max bpc property on.
1877 * @min: The minimum bit depth supported by the connector.
1878 * @max: The maximum bit depth supported by the connector.
1879 *
1880 * This is used to add support for limiting the bit depth on a connector.
1881 *
1882 * Returns:
1883 * Zero on success, negative errno on failure.
1884 */
1885int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
1886 int min, int max)
1887{
1888 struct drm_device *dev = connector->dev;
1889 struct drm_property *prop;
1890
1891 prop = connector->max_bpc_property;
1892 if (!prop) {
1893 prop = drm_property_create_range(dev, 0, "max bpc", min, max);
1894 if (!prop)
1895 return -ENOMEM;
1896
1897 connector->max_bpc_property = prop;
1898 }
1899
1900 drm_object_attach_property(&connector->base, prop, max);
1901 connector->state->max_requested_bpc = max;
1902 connector->state->max_bpc = max;
1903
1904 return 0;
1905}
1906EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
1907
ba1b0f6c
NK
1908/**
1909 * drm_connector_set_vrr_capable_property - sets the variable refresh rate
1910 * capable property for a connector
1911 * @connector: drm connector
1912 * @capable: True if the connector is variable refresh rate capable
1913 *
1914 * Should be used by atomic drivers to update the indicated support for
1915 * variable refresh rate over a connector.
1916 */
1917void drm_connector_set_vrr_capable_property(
1918 struct drm_connector *connector, bool capable)
1919{
1920 drm_object_property_set_value(&connector->base,
1921 connector->vrr_capable_property,
1922 capable);
1923}
1924EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
1925
8d70f395
HG
1926/**
1927 * drm_connector_init_panel_orientation_property -
1928 * initialize the connecters panel_orientation property
1929 * @connector: connector for which to init the panel-orientation property.
1930 * @width: width in pixels of the panel, used for panel quirk detection
1931 * @height: height in pixels of the panel, used for panel quirk detection
1932 *
1933 * This function should only be called for built-in panels, after setting
1934 * connector->display_info.panel_orientation first (if known).
1935 *
1936 * This function will check for platform specific (e.g. DMI based) quirks
1937 * overriding display_info.panel_orientation first, then if panel_orientation
1938 * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
1939 * "panel orientation" property to the connector.
1940 *
1941 * Returns:
1942 * Zero on success, negative errno on failure.
1943 */
1944int drm_connector_init_panel_orientation_property(
1945 struct drm_connector *connector, int width, int height)
1946{
1947 struct drm_device *dev = connector->dev;
1948 struct drm_display_info *info = &connector->display_info;
1949 struct drm_property *prop;
1950 int orientation_quirk;
1951
1952 orientation_quirk = drm_get_panel_orientation_quirk(width, height);
1953 if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1954 info->panel_orientation = orientation_quirk;
1955
1956 if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1957 return 0;
1958
1959 prop = dev->mode_config.panel_orientation_property;
1960 if (!prop) {
1961 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1962 "panel orientation",
1963 drm_panel_orientation_enum_list,
1964 ARRAY_SIZE(drm_panel_orientation_enum_list));
1965 if (!prop)
1966 return -ENOMEM;
1967
1968 dev->mode_config.panel_orientation_property = prop;
1969 }
1970
1971 drm_object_attach_property(&connector->base, prop,
1972 info->panel_orientation);
1973 return 0;
1974}
1975EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
1976
97e14fbe 1977int drm_connector_set_obj_prop(struct drm_mode_object *obj,
52217195
DV
1978 struct drm_property *property,
1979 uint64_t value)
1980{
1981 int ret = -EINVAL;
1982 struct drm_connector *connector = obj_to_connector(obj);
1983
1984 /* Do DPMS ourselves */
1985 if (property == connector->dev->mode_config.dpms_property) {
1986 ret = (*connector->funcs->dpms)(connector, (int)value);
1987 } else if (connector->funcs->set_property)
1988 ret = connector->funcs->set_property(connector, property, value);
1989
144a7999 1990 if (!ret)
52217195
DV
1991 drm_object_property_set_value(&connector->base, property, value);
1992 return ret;
1993}
1994
97e14fbe
DV
1995int drm_connector_property_set_ioctl(struct drm_device *dev,
1996 void *data, struct drm_file *file_priv)
52217195
DV
1997{
1998 struct drm_mode_connector_set_property *conn_set_prop = data;
1999 struct drm_mode_obj_set_property obj_set_prop = {
2000 .value = conn_set_prop->value,
2001 .prop_id = conn_set_prop->prop_id,
2002 .obj_id = conn_set_prop->connector_id,
2003 .obj_type = DRM_MODE_OBJECT_CONNECTOR
2004 };
2005
2006 /* It does all the locking and checking we need */
2007 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
2008}
2009
2010static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2011{
2012 /* For atomic drivers only state objects are synchronously updated and
2013 * protected by modeset locks, so check those first. */
2014 if (connector->state)
2015 return connector->state->best_encoder;
2016 return connector->encoder;
2017}
2018
c3ff0cdb
AN
2019static bool
2020drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2021 const struct list_head *export_list,
2022 const struct drm_file *file_priv)
52217195
DV
2023{
2024 /*
2025 * If user-space hasn't configured the driver to expose the stereo 3D
2026 * modes, don't expose them.
2027 */
2028 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2029 return false;
c3ff0cdb
AN
2030 /*
2031 * If user-space hasn't configured the driver to expose the modes
2032 * with aspect-ratio, don't expose them. However if such a mode
2033 * is unique, let it be exposed, but reset the aspect-ratio flags
2034 * while preparing the list of user-modes.
2035 */
2036 if (!file_priv->aspect_ratio_allowed) {
2037 struct drm_display_mode *mode_itr;
2038
2039 list_for_each_entry(mode_itr, export_list, export_head)
2040 if (drm_mode_match(mode_itr, mode,
2041 DRM_MODE_MATCH_TIMINGS |
2042 DRM_MODE_MATCH_CLOCK |
2043 DRM_MODE_MATCH_FLAGS |
2044 DRM_MODE_MATCH_3D_FLAGS))
2045 return false;
2046 }
52217195
DV
2047
2048 return true;
2049}
2050
2051int drm_mode_getconnector(struct drm_device *dev, void *data,
2052 struct drm_file *file_priv)
2053{
2054 struct drm_mode_get_connector *out_resp = data;
2055 struct drm_connector *connector;
2056 struct drm_encoder *encoder;
2057 struct drm_display_mode *mode;
2058 int mode_count = 0;
2059 int encoders_count = 0;
2060 int ret = 0;
2061 int copied = 0;
2062 int i;
2063 struct drm_mode_modeinfo u_mode;
2064 struct drm_mode_modeinfo __user *mode_ptr;
2065 uint32_t __user *encoder_ptr;
c3ff0cdb 2066 LIST_HEAD(export_list);
52217195
DV
2067
2068 if (!drm_core_check_feature(dev, DRIVER_MODESET))
69fdf420 2069 return -EOPNOTSUPP;
52217195
DV
2070
2071 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2072
418da172 2073 connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
91eefc05
DV
2074 if (!connector)
2075 return -ENOENT;
2076
83aefbb8
VS
2077 drm_connector_for_each_possible_encoder(connector, encoder, i)
2078 encoders_count++;
52217195 2079
91eefc05
DV
2080 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2081 copied = 0;
2082 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
83aefbb8
VS
2083
2084 drm_connector_for_each_possible_encoder(connector, encoder, i) {
2085 if (put_user(encoder->base.id, encoder_ptr + copied)) {
2086 ret = -EFAULT;
2087 goto out;
91eefc05 2088 }
83aefbb8 2089 copied++;
91eefc05
DV
2090 }
2091 }
2092 out_resp->count_encoders = encoders_count;
2093
2094 out_resp->connector_id = connector->base.id;
2095 out_resp->connector_type = connector->connector_type;
2096 out_resp->connector_type_id = connector->connector_type_id;
2097
2098 mutex_lock(&dev->mode_config.mutex);
52217195
DV
2099 if (out_resp->count_modes == 0) {
2100 connector->funcs->fill_modes(connector,
2101 dev->mode_config.max_width,
2102 dev->mode_config.max_height);
2103 }
2104
52217195
DV
2105 out_resp->mm_width = connector->display_info.width_mm;
2106 out_resp->mm_height = connector->display_info.height_mm;
2107 out_resp->subpixel = connector->display_info.subpixel_order;
2108 out_resp->connection = connector->status;
2109
91eefc05
DV
2110 /* delayed so we get modes regardless of pre-fill_modes state */
2111 list_for_each_entry(mode, &connector->modes, head)
c3ff0cdb
AN
2112 if (drm_mode_expose_to_userspace(mode, &export_list,
2113 file_priv)) {
2114 list_add_tail(&mode->export_head, &export_list);
91eefc05 2115 mode_count++;
c3ff0cdb 2116 }
52217195
DV
2117
2118 /*
2119 * This ioctl is called twice, once to determine how much space is
2120 * needed, and the 2nd time to fill it.
c3ff0cdb
AN
2121 * The modes that need to be exposed to the user are maintained in the
2122 * 'export_list'. When the ioctl is called first time to determine the,
2123 * space, the export_list gets filled, to find the no.of modes. In the
2124 * 2nd time, the user modes are filled, one by one from the export_list.
52217195
DV
2125 */
2126 if ((out_resp->count_modes >= mode_count) && mode_count) {
2127 copied = 0;
2128 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
c3ff0cdb 2129 list_for_each_entry(mode, &export_list, export_head) {
52217195 2130 drm_mode_convert_to_umode(&u_mode, mode);
c3ff0cdb
AN
2131 /*
2132 * Reset aspect ratio flags of user-mode, if modes with
2133 * aspect-ratio are not supported.
2134 */
2135 if (!file_priv->aspect_ratio_allowed)
2136 u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
52217195
DV
2137 if (copy_to_user(mode_ptr + copied,
2138 &u_mode, sizeof(u_mode))) {
2139 ret = -EFAULT;
e94ac351
DV
2140 mutex_unlock(&dev->mode_config.mutex);
2141
52217195
DV
2142 goto out;
2143 }
2144 copied++;
2145 }
2146 }
2147 out_resp->count_modes = mode_count;
52217195 2148 mutex_unlock(&dev->mode_config.mutex);
e94ac351
DV
2149
2150 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2151 encoder = drm_connector_get_encoder(connector);
2152 if (encoder)
2153 out_resp->encoder_id = encoder->base.id;
2154 else
2155 out_resp->encoder_id = 0;
2156
2157 /* Only grab properties after probing, to make sure EDID and other
2158 * properties reflect the latest status. */
2159 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
2160 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2161 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2162 &out_resp->count_props);
2163 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2164
2165out:
ad093607 2166 drm_connector_put(connector);
52217195
DV
2167
2168 return ret;
2169}
2170
9498c19b
DV
2171
2172/**
2173 * DOC: Tile group
2174 *
2175 * Tile groups are used to represent tiled monitors with a unique integer
2176 * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
2177 * we store this in a tile group, so we have a common identifier for all tiles
2178 * in a monitor group. The property is called "TILE". Drivers can manage tile
2179 * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
2180 * drm_mode_get_tile_group(). But this is only needed for internal panels where
2181 * the tile group information is exposed through a non-standard way.
2182 */
2183
2184static void drm_tile_group_free(struct kref *kref)
2185{
2186 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2187 struct drm_device *dev = tg->dev;
2188 mutex_lock(&dev->mode_config.idr_mutex);
2189 idr_remove(&dev->mode_config.tile_idr, tg->id);
2190 mutex_unlock(&dev->mode_config.idr_mutex);
2191 kfree(tg);
2192}
2193
2194/**
2195 * drm_mode_put_tile_group - drop a reference to a tile group.
2196 * @dev: DRM device
2197 * @tg: tile group to drop reference to.
2198 *
2199 * drop reference to tile group and free if 0.
2200 */
2201void drm_mode_put_tile_group(struct drm_device *dev,
2202 struct drm_tile_group *tg)
2203{
2204 kref_put(&tg->refcount, drm_tile_group_free);
2205}
2206EXPORT_SYMBOL(drm_mode_put_tile_group);
2207
2208/**
2209 * drm_mode_get_tile_group - get a reference to an existing tile group
2210 * @dev: DRM device
2211 * @topology: 8-bytes unique per monitor.
2212 *
2213 * Use the unique bytes to get a reference to an existing tile group.
2214 *
2215 * RETURNS:
2216 * tile group or NULL if not found.
2217 */
2218struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2219 char topology[8])
2220{
2221 struct drm_tile_group *tg;
2222 int id;
2223 mutex_lock(&dev->mode_config.idr_mutex);
2224 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2225 if (!memcmp(tg->group_data, topology, 8)) {
2226 if (!kref_get_unless_zero(&tg->refcount))
2227 tg = NULL;
2228 mutex_unlock(&dev->mode_config.idr_mutex);
2229 return tg;
2230 }
2231 }
2232 mutex_unlock(&dev->mode_config.idr_mutex);
2233 return NULL;
2234}
2235EXPORT_SYMBOL(drm_mode_get_tile_group);
2236
2237/**
2238 * drm_mode_create_tile_group - create a tile group from a displayid description
2239 * @dev: DRM device
2240 * @topology: 8-bytes unique per monitor.
2241 *
2242 * Create a tile group for the unique monitor, and get a unique
2243 * identifier for the tile group.
2244 *
2245 * RETURNS:
705c8160 2246 * new tile group or NULL.
9498c19b
DV
2247 */
2248struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2249 char topology[8])
2250{
2251 struct drm_tile_group *tg;
2252 int ret;
2253
2254 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2255 if (!tg)
705c8160 2256 return NULL;
9498c19b
DV
2257
2258 kref_init(&tg->refcount);
2259 memcpy(tg->group_data, topology, 8);
2260 tg->dev = dev;
2261
2262 mutex_lock(&dev->mode_config.idr_mutex);
2263 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2264 if (ret >= 0) {
2265 tg->id = ret;
2266 } else {
2267 kfree(tg);
705c8160 2268 tg = NULL;
9498c19b
DV
2269 }
2270
2271 mutex_unlock(&dev->mode_config.idr_mutex);
2272 return tg;
2273}
2274EXPORT_SYMBOL(drm_mode_create_tile_group);