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