drm: Update edid-derived drm_display_info fields at edid property set [v2]
[linux-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
23#include <drm/drmP.h>
24#include <drm/drm_connector.h>
25#include <drm/drm_edid.h>
9338203c 26#include <drm/drm_encoder.h>
52217195
DV
27
28#include "drm_crtc_internal.h"
29#include "drm_internal.h"
30
ae2a6da8
DV
31/**
32 * DOC: overview
33 *
34 * In DRM connectors are the general abstraction for display sinks, and include
35 * als fixed panels or anything else that can display pixels in some form. As
36 * opposed to all other KMS objects representing hardware (like CRTC, encoder or
37 * plane abstractions) connectors can be hotplugged and unplugged at runtime.
ad093607
TR
38 * Hence they are reference-counted using drm_connector_get() and
39 * drm_connector_put().
ae2a6da8 40 *
d574528a
DV
41 * KMS driver must create, initialize, register and attach at a &struct
42 * drm_connector for each such sink. The instance is created as other KMS
aec97460
DV
43 * objects and initialized by setting the following fields. The connector is
44 * initialized with a call to drm_connector_init() with a pointer to the
45 * &struct drm_connector_funcs and a connector type, and then exposed to
46 * userspace with a call to drm_connector_register().
ae2a6da8
DV
47 *
48 * Connectors must be attached to an encoder to be used. For devices that map
49 * connectors to encoders 1:1, the connector should be attached at
50 * initialization time with a call to drm_mode_connector_attach_encoder(). The
d574528a 51 * driver must also set the &drm_connector.encoder field to point to the
ae2a6da8
DV
52 * attached encoder.
53 *
54 * For connectors which are not fixed (like built-in panels) the driver needs to
55 * support hotplug notifications. The simplest way to do that is by using the
56 * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
57 * hardware support for hotplug interrupts. Connectors with hardware hotplug
58 * support can instead use e.g. drm_helper_hpd_irq_event().
59 */
60
52217195
DV
61struct drm_conn_prop_enum_list {
62 int type;
63 const char *name;
64 struct ida ida;
65};
66
67/*
68 * Connector and encoder types.
69 */
70static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
71 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
72 { DRM_MODE_CONNECTOR_VGA, "VGA" },
73 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
74 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
75 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
76 { DRM_MODE_CONNECTOR_Composite, "Composite" },
77 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
78 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
79 { DRM_MODE_CONNECTOR_Component, "Component" },
80 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
81 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
82 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
83 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
84 { DRM_MODE_CONNECTOR_TV, "TV" },
85 { DRM_MODE_CONNECTOR_eDP, "eDP" },
86 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
87 { DRM_MODE_CONNECTOR_DSI, "DSI" },
88 { DRM_MODE_CONNECTOR_DPI, "DPI" },
89};
90
91void drm_connector_ida_init(void)
92{
93 int i;
94
95 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
96 ida_init(&drm_connector_enum_list[i].ida);
97}
98
99void drm_connector_ida_destroy(void)
100{
101 int i;
102
103 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
104 ida_destroy(&drm_connector_enum_list[i].ida);
105}
106
107/**
108 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
109 * @connector: connector to quwery
110 *
ae2a6da8 111 * The kernel supports per-connector configuration of its consoles through
52217195
DV
112 * use of the video= parameter. This function parses that option and
113 * extracts the user's specified mode (or enable/disable status) for a
114 * particular connector. This is typically only used during the early fbdev
115 * setup.
116 */
117static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
118{
119 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
120 char *option = NULL;
121
122 if (fb_get_options(connector->name, &option))
123 return;
124
125 if (!drm_mode_parse_command_line_for_connector(option,
126 connector,
127 mode))
128 return;
129
130 if (mode->force) {
6140cf20
JN
131 DRM_INFO("forcing %s connector %s\n", connector->name,
132 drm_get_connector_force_name(mode->force));
52217195
DV
133 connector->force = mode->force;
134 }
135
136 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
137 connector->name,
138 mode->xres, mode->yres,
139 mode->refresh_specified ? mode->refresh : 60,
140 mode->rb ? " reduced blanking" : "",
141 mode->margins ? " with margins" : "",
142 mode->interlace ? " interlaced" : "");
143}
144
145static void drm_connector_free(struct kref *kref)
146{
147 struct drm_connector *connector =
148 container_of(kref, struct drm_connector, base.refcount);
149 struct drm_device *dev = connector->dev;
150
151 drm_mode_object_unregister(dev, &connector->base);
152 connector->funcs->destroy(connector);
153}
154
a703c550
DV
155static void drm_connector_free_work_fn(struct work_struct *work)
156{
157 struct drm_connector *connector =
158 container_of(work, struct drm_connector, free_work);
159 struct drm_device *dev = connector->dev;
160
161 drm_mode_object_unregister(dev, &connector->base);
162 connector->funcs->destroy(connector);
163}
164
52217195
DV
165/**
166 * drm_connector_init - Init a preallocated connector
167 * @dev: DRM device
168 * @connector: the connector to init
169 * @funcs: callbacks for this connector
170 * @connector_type: user visible type of the connector
171 *
172 * Initialises a preallocated connector. Connectors should be
173 * subclassed as part of driver connector objects.
174 *
175 * Returns:
176 * Zero on success, error code on failure.
177 */
178int drm_connector_init(struct drm_device *dev,
179 struct drm_connector *connector,
180 const struct drm_connector_funcs *funcs,
181 int connector_type)
182{
183 struct drm_mode_config *config = &dev->mode_config;
184 int ret;
185 struct ida *connector_ida =
186 &drm_connector_enum_list[connector_type].ida;
187
2135ea7a
TR
188 ret = __drm_mode_object_add(dev, &connector->base,
189 DRM_MODE_OBJECT_CONNECTOR,
190 false, drm_connector_free);
52217195 191 if (ret)
613051da 192 return ret;
52217195 193
a703c550
DV
194 INIT_WORK(&connector->free_work, drm_connector_free_work_fn);
195
52217195
DV
196 connector->base.properties = &connector->properties;
197 connector->dev = dev;
198 connector->funcs = funcs;
199
200 ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
201 if (ret < 0)
202 goto out_put;
203 connector->index = ret;
204 ret = 0;
205
206 connector->connector_type = connector_type;
207 connector->connector_type_id =
208 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
209 if (connector->connector_type_id < 0) {
210 ret = connector->connector_type_id;
211 goto out_put_id;
212 }
213 connector->name =
214 kasprintf(GFP_KERNEL, "%s-%d",
215 drm_connector_enum_list[connector_type].name,
216 connector->connector_type_id);
217 if (!connector->name) {
218 ret = -ENOMEM;
219 goto out_put_type_id;
220 }
221
222 INIT_LIST_HEAD(&connector->probed_modes);
223 INIT_LIST_HEAD(&connector->modes);
e73ab00e 224 mutex_init(&connector->mutex);
52217195
DV
225 connector->edid_blob_ptr = NULL;
226 connector->status = connector_status_unknown;
227
228 drm_connector_get_cmdline_mode(connector);
229
230 /* We should add connectors at the end to avoid upsetting the connector
231 * index too much. */
613051da 232 spin_lock_irq(&config->connector_list_lock);
52217195
DV
233 list_add_tail(&connector->head, &config->connector_list);
234 config->num_connector++;
613051da 235 spin_unlock_irq(&config->connector_list_lock);
52217195
DV
236
237 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
238 drm_object_attach_property(&connector->base,
239 config->edid_property,
240 0);
241
242 drm_object_attach_property(&connector->base,
243 config->dpms_property, 0);
244
40ee6fbe
MN
245 drm_object_attach_property(&connector->base,
246 config->link_status_property,
247 0);
248
66660d4c
DA
249 drm_object_attach_property(&connector->base,
250 config->non_desktop_property,
251 0);
252
52217195
DV
253 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
254 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
255 }
256
257 connector->debugfs_entry = NULL;
258out_put_type_id:
259 if (ret)
587680c1 260 ida_simple_remove(connector_ida, connector->connector_type_id);
52217195
DV
261out_put_id:
262 if (ret)
587680c1 263 ida_simple_remove(&config->connector_ida, connector->index);
52217195
DV
264out_put:
265 if (ret)
266 drm_mode_object_unregister(dev, &connector->base);
267
52217195
DV
268 return ret;
269}
270EXPORT_SYMBOL(drm_connector_init);
271
272/**
273 * drm_mode_connector_attach_encoder - attach a connector to an encoder
274 * @connector: connector to attach
275 * @encoder: encoder to attach @connector to
276 *
277 * This function links up a connector to an encoder. Note that the routing
278 * restrictions between encoders and crtcs are exposed to userspace through the
279 * possible_clones and possible_crtcs bitmasks.
280 *
281 * Returns:
282 * Zero on success, negative errno on failure.
283 */
284int drm_mode_connector_attach_encoder(struct drm_connector *connector,
285 struct drm_encoder *encoder)
286{
287 int i;
288
289 /*
290 * In the past, drivers have attempted to model the static association
291 * of connector to encoder in simple connector/encoder devices using a
292 * direct assignment of connector->encoder = encoder. This connection
293 * is a logical one and the responsibility of the core, so drivers are
294 * expected not to mess with this.
295 *
296 * Note that the error return should've been enough here, but a large
297 * majority of drivers ignores the return value, so add in a big WARN
298 * to get people's attention.
299 */
300 if (WARN_ON(connector->encoder))
301 return -EINVAL;
302
303 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
304 if (connector->encoder_ids[i] == 0) {
305 connector->encoder_ids[i] = encoder->base.id;
306 return 0;
307 }
308 }
309 return -ENOMEM;
310}
311EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
312
313static void drm_mode_remove(struct drm_connector *connector,
314 struct drm_display_mode *mode)
315{
316 list_del(&mode->head);
317 drm_mode_destroy(connector->dev, mode);
318}
319
320/**
321 * drm_connector_cleanup - cleans up an initialised connector
322 * @connector: connector to cleanup
323 *
324 * Cleans up the connector but doesn't free the object.
325 */
326void drm_connector_cleanup(struct drm_connector *connector)
327{
328 struct drm_device *dev = connector->dev;
329 struct drm_display_mode *mode, *t;
330
331 /* The connector should have been removed from userspace long before
332 * it is finally destroyed.
333 */
334 if (WARN_ON(connector->registered))
335 drm_connector_unregister(connector);
336
337 if (connector->tile_group) {
338 drm_mode_put_tile_group(dev, connector->tile_group);
339 connector->tile_group = NULL;
340 }
341
342 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
343 drm_mode_remove(connector, mode);
344
345 list_for_each_entry_safe(mode, t, &connector->modes, head)
346 drm_mode_remove(connector, mode);
347
9a47dba1
CJ
348 ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
349 connector->connector_type_id);
52217195 350
9a47dba1
CJ
351 ida_simple_remove(&dev->mode_config.connector_ida,
352 connector->index);
52217195
DV
353
354 kfree(connector->display_info.bus_formats);
355 drm_mode_object_unregister(dev, &connector->base);
356 kfree(connector->name);
357 connector->name = NULL;
613051da 358 spin_lock_irq(&dev->mode_config.connector_list_lock);
52217195
DV
359 list_del(&connector->head);
360 dev->mode_config.num_connector--;
613051da 361 spin_unlock_irq(&dev->mode_config.connector_list_lock);
52217195
DV
362
363 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
364 if (connector->state && connector->funcs->atomic_destroy_state)
365 connector->funcs->atomic_destroy_state(connector,
366 connector->state);
367
e73ab00e
DV
368 mutex_destroy(&connector->mutex);
369
52217195
DV
370 memset(connector, 0, sizeof(*connector));
371}
372EXPORT_SYMBOL(drm_connector_cleanup);
373
374/**
375 * drm_connector_register - register a connector
376 * @connector: the connector to register
377 *
378 * Register userspace interfaces for a connector
379 *
380 * Returns:
381 * Zero on success, error code on failure.
382 */
383int drm_connector_register(struct drm_connector *connector)
384{
e73ab00e 385 int ret = 0;
52217195 386
e6e7b48b
DV
387 if (!connector->dev->registered)
388 return 0;
389
e73ab00e 390 mutex_lock(&connector->mutex);
52217195 391 if (connector->registered)
e73ab00e 392 goto unlock;
52217195
DV
393
394 ret = drm_sysfs_connector_add(connector);
395 if (ret)
e73ab00e 396 goto unlock;
52217195
DV
397
398 ret = drm_debugfs_connector_add(connector);
399 if (ret) {
400 goto err_sysfs;
401 }
402
403 if (connector->funcs->late_register) {
404 ret = connector->funcs->late_register(connector);
405 if (ret)
406 goto err_debugfs;
407 }
408
409 drm_mode_object_register(connector->dev, &connector->base);
410
411 connector->registered = true;
e73ab00e 412 goto unlock;
52217195
DV
413
414err_debugfs:
415 drm_debugfs_connector_remove(connector);
416err_sysfs:
417 drm_sysfs_connector_remove(connector);
e73ab00e
DV
418unlock:
419 mutex_unlock(&connector->mutex);
52217195
DV
420 return ret;
421}
422EXPORT_SYMBOL(drm_connector_register);
423
424/**
425 * drm_connector_unregister - unregister a connector
426 * @connector: the connector to unregister
427 *
428 * Unregister userspace interfaces for a connector
429 */
430void drm_connector_unregister(struct drm_connector *connector)
431{
e73ab00e
DV
432 mutex_lock(&connector->mutex);
433 if (!connector->registered) {
434 mutex_unlock(&connector->mutex);
52217195 435 return;
e73ab00e 436 }
52217195
DV
437
438 if (connector->funcs->early_unregister)
439 connector->funcs->early_unregister(connector);
440
441 drm_sysfs_connector_remove(connector);
442 drm_debugfs_connector_remove(connector);
443
444 connector->registered = false;
e73ab00e 445 mutex_unlock(&connector->mutex);
52217195
DV
446}
447EXPORT_SYMBOL(drm_connector_unregister);
448
449void drm_connector_unregister_all(struct drm_device *dev)
450{
451 struct drm_connector *connector;
613051da 452 struct drm_connector_list_iter conn_iter;
52217195 453
b982dab1 454 drm_connector_list_iter_begin(dev, &conn_iter);
613051da 455 drm_for_each_connector_iter(connector, &conn_iter)
52217195 456 drm_connector_unregister(connector);
b982dab1 457 drm_connector_list_iter_end(&conn_iter);
52217195
DV
458}
459
460int drm_connector_register_all(struct drm_device *dev)
461{
462 struct drm_connector *connector;
613051da
DV
463 struct drm_connector_list_iter conn_iter;
464 int ret = 0;
52217195 465
b982dab1 466 drm_connector_list_iter_begin(dev, &conn_iter);
613051da 467 drm_for_each_connector_iter(connector, &conn_iter) {
52217195
DV
468 ret = drm_connector_register(connector);
469 if (ret)
613051da 470 break;
52217195 471 }
b982dab1 472 drm_connector_list_iter_end(&conn_iter);
52217195 473
613051da
DV
474 if (ret)
475 drm_connector_unregister_all(dev);
52217195
DV
476 return ret;
477}
478
479/**
480 * drm_get_connector_status_name - return a string for connector status
481 * @status: connector status to compute name of
482 *
483 * In contrast to the other drm_get_*_name functions this one here returns a
484 * const pointer and hence is threadsafe.
485 */
486const char *drm_get_connector_status_name(enum drm_connector_status status)
487{
488 if (status == connector_status_connected)
489 return "connected";
490 else if (status == connector_status_disconnected)
491 return "disconnected";
492 else
493 return "unknown";
494}
495EXPORT_SYMBOL(drm_get_connector_status_name);
496
6140cf20
JN
497/**
498 * drm_get_connector_force_name - return a string for connector force
499 * @force: connector force to get name of
500 *
501 * Returns: const pointer to name.
502 */
503const char *drm_get_connector_force_name(enum drm_connector_force force)
504{
505 switch (force) {
506 case DRM_FORCE_UNSPECIFIED:
507 return "unspecified";
508 case DRM_FORCE_OFF:
509 return "off";
510 case DRM_FORCE_ON:
511 return "on";
512 case DRM_FORCE_ON_DIGITAL:
513 return "digital";
514 default:
515 return "unknown";
516 }
517}
518
613051da
DV
519#ifdef CONFIG_LOCKDEP
520static struct lockdep_map connector_list_iter_dep_map = {
521 .name = "drm_connector_list_iter"
522};
523#endif
524
525/**
b982dab1 526 * drm_connector_list_iter_begin - initialize a connector_list iterator
613051da
DV
527 * @dev: DRM device
528 * @iter: connector_list iterator
529 *
d574528a 530 * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
b982dab1 531 * must always be cleaned up again by calling drm_connector_list_iter_end().
613051da
DV
532 * Iteration itself happens using drm_connector_list_iter_next() or
533 * drm_for_each_connector_iter().
534 */
b982dab1
TR
535void drm_connector_list_iter_begin(struct drm_device *dev,
536 struct drm_connector_list_iter *iter)
613051da
DV
537{
538 iter->dev = dev;
539 iter->conn = NULL;
540 lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
541}
b982dab1 542EXPORT_SYMBOL(drm_connector_list_iter_begin);
613051da 543
a703c550
DV
544/*
545 * Extra-safe connector put function that works in any context. Should only be
546 * used from the connector_iter functions, where we never really expect to
547 * actually release the connector when dropping our final reference.
548 */
549static void
550drm_connector_put_safe(struct drm_connector *conn)
551{
552 if (refcount_dec_and_test(&conn->base.refcount.refcount))
553 schedule_work(&conn->free_work);
554}
555
613051da
DV
556/**
557 * drm_connector_list_iter_next - return next connector
558 * @iter: connectr_list iterator
559 *
560 * Returns the next connector for @iter, or NULL when the list walk has
561 * completed.
562 */
563struct drm_connector *
564drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
565{
566 struct drm_connector *old_conn = iter->conn;
567 struct drm_mode_config *config = &iter->dev->mode_config;
568 struct list_head *lhead;
569 unsigned long flags;
570
571 spin_lock_irqsave(&config->connector_list_lock, flags);
572 lhead = old_conn ? &old_conn->head : &config->connector_list;
573
574 do {
575 if (lhead->next == &config->connector_list) {
576 iter->conn = NULL;
577 break;
578 }
579
580 lhead = lhead->next;
581 iter->conn = list_entry(lhead, struct drm_connector, head);
582
583 /* loop until it's not a zombie connector */
584 } while (!kref_get_unless_zero(&iter->conn->base.refcount));
585 spin_unlock_irqrestore(&config->connector_list_lock, flags);
586
587 if (old_conn)
a703c550 588 drm_connector_put_safe(old_conn);
613051da
DV
589
590 return iter->conn;
591}
592EXPORT_SYMBOL(drm_connector_list_iter_next);
593
594/**
b982dab1 595 * drm_connector_list_iter_end - tear down a connector_list iterator
613051da
DV
596 * @iter: connector_list iterator
597 *
598 * Tears down @iter and releases any resources (like &drm_connector references)
599 * acquired while walking the list. This must always be called, both when the
600 * iteration completes fully or when it was aborted without walking the entire
601 * list.
602 */
b982dab1 603void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
613051da
DV
604{
605 iter->dev = NULL;
606 if (iter->conn)
a703c550 607 drm_connector_put_safe(iter->conn);
613051da
DV
608 lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
609}
b982dab1 610EXPORT_SYMBOL(drm_connector_list_iter_end);
613051da 611
52217195
DV
612static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
613 { SubPixelUnknown, "Unknown" },
614 { SubPixelHorizontalRGB, "Horizontal RGB" },
615 { SubPixelHorizontalBGR, "Horizontal BGR" },
616 { SubPixelVerticalRGB, "Vertical RGB" },
617 { SubPixelVerticalBGR, "Vertical BGR" },
618 { SubPixelNone, "None" },
619};
620
621/**
622 * drm_get_subpixel_order_name - return a string for a given subpixel enum
623 * @order: enum of subpixel_order
624 *
625 * Note you could abuse this and return something out of bounds, but that
626 * would be a caller error. No unscrubbed user data should make it here.
627 */
628const char *drm_get_subpixel_order_name(enum subpixel_order order)
629{
630 return drm_subpixel_enum_list[order].name;
631}
632EXPORT_SYMBOL(drm_get_subpixel_order_name);
633
634static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
635 { DRM_MODE_DPMS_ON, "On" },
636 { DRM_MODE_DPMS_STANDBY, "Standby" },
637 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
638 { DRM_MODE_DPMS_OFF, "Off" }
639};
640DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
641
40ee6fbe
MN
642static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
643 { DRM_MODE_LINK_STATUS_GOOD, "Good" },
644 { DRM_MODE_LINK_STATUS_BAD, "Bad" },
645};
40ee6fbe 646
b3c6c8bf
DV
647/**
648 * drm_display_info_set_bus_formats - set the supported bus formats
649 * @info: display info to store bus formats in
650 * @formats: array containing the supported bus formats
651 * @num_formats: the number of entries in the fmts array
652 *
653 * Store the supported bus formats in display info structure.
654 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
655 * a full list of available formats.
656 */
657int drm_display_info_set_bus_formats(struct drm_display_info *info,
658 const u32 *formats,
659 unsigned int num_formats)
660{
661 u32 *fmts = NULL;
662
663 if (!formats && num_formats)
664 return -EINVAL;
665
666 if (formats && num_formats) {
667 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
668 GFP_KERNEL);
669 if (!fmts)
670 return -ENOMEM;
671 }
672
673 kfree(info->bus_formats);
674 info->bus_formats = fmts;
675 info->num_bus_formats = num_formats;
676
677 return 0;
678}
679EXPORT_SYMBOL(drm_display_info_set_bus_formats);
680
52217195
DV
681/* Optional connector properties. */
682static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
683 { DRM_MODE_SCALE_NONE, "None" },
684 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
685 { DRM_MODE_SCALE_CENTER, "Center" },
686 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
687};
688
689static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
690 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
691 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
692 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
693};
694
695static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
696 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
697 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
698 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
699};
700DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
701
702static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
703 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
704 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
705 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
706};
707DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
708 drm_dvi_i_subconnector_enum_list)
709
710static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
711 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
712 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
713 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
714 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
715 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
716};
717DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
718
719static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
720 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
721 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
722 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
723 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
724 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
725};
726DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
727 drm_tv_subconnector_enum_list)
728
4ada6f22
DV
729/**
730 * DOC: standard connector properties
731 *
732 * DRM connectors have a few standardized properties:
733 *
734 * EDID:
735 * Blob property which contains the current EDID read from the sink. This
736 * is useful to parse sink identification information like vendor, model
737 * and serial. Drivers should update this property by calling
738 * drm_mode_connector_update_edid_property(), usually after having parsed
739 * the EDID using drm_add_edid_modes(). Userspace cannot change this
740 * property.
741 * DPMS:
742 * Legacy property for setting the power state of the connector. For atomic
743 * drivers this is only provided for backwards compatibility with existing
744 * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
745 * connector is linked to. Drivers should never set this property directly,
d574528a 746 * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
144a7999
DV
747 * callback. For atomic drivers the remapping to the "ACTIVE" property is
748 * implemented in the DRM core. This is the only standard connector
749 * property that userspace can change.
d0d1aee5
DV
750 *
751 * Note that this property cannot be set through the MODE_ATOMIC ioctl,
752 * userspace must use "ACTIVE" on the CRTC instead.
753 *
754 * WARNING:
755 *
756 * For userspace also running on legacy drivers the "DPMS" semantics are a
757 * lot more complicated. First, userspace cannot rely on the "DPMS" value
758 * returned by the GETCONNECTOR actually reflecting reality, because many
759 * drivers fail to update it. For atomic drivers this is taken care of in
760 * drm_atomic_helper_update_legacy_modeset_state().
761 *
762 * The second issue is that the DPMS state is only well-defined when the
763 * connector is connected to a CRTC. In atomic the DRM core enforces that
764 * "ACTIVE" is off in such a case, no such checks exists for "DPMS".
765 *
766 * Finally, when enabling an output using the legacy SETCONFIG ioctl then
767 * "DPMS" is forced to ON. But see above, that might not be reflected in
768 * the software value on legacy drivers.
769 *
770 * Summarizing: Only set "DPMS" when the connector is known to be enabled,
771 * assume that a successful SETCONFIG call also sets "DPMS" to on, and
772 * never read back the value of "DPMS" because it can be incorrect.
4ada6f22
DV
773 * PATH:
774 * Connector path property to identify how this sink is physically
775 * connected. Used by DP MST. This should be set by calling
776 * drm_mode_connector_set_path_property(), in the case of DP MST with the
777 * path property the MST manager created. Userspace cannot change this
778 * property.
779 * TILE:
780 * Connector tile group property to indicate how a set of DRM connector
781 * compose together into one logical screen. This is used by both high-res
782 * external screens (often only using a single cable, but exposing multiple
783 * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
784 * are not gen-locked. Note that for tiled panels which are genlocked, like
785 * dual-link LVDS or dual-link DSI, the driver should try to not expose the
786 * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
787 * should update this value using drm_mode_connector_set_tile_property().
788 * Userspace cannot change this property.
40ee6fbe
MN
789 * link-status:
790 * Connector link-status property to indicate the status of link. The default
791 * value of link-status is "GOOD". If something fails during or after modeset,
792 * the kernel driver may set this to "BAD" and issue a hotplug uevent. Drivers
793 * should update this value using drm_mode_connector_set_link_status_property().
66660d4c
DA
794 * non_desktop:
795 * Indicates the output should be ignored for purposes of displaying a
796 * standard desktop environment or console. This is most likely because
797 * the output device is not rectilinear.
4ada6f22
DV
798 *
799 * Connectors also have one standardized atomic property:
800 *
801 * CRTC_ID:
802 * Mode object ID of the &drm_crtc this connector should be connected to.
803 */
804
52217195
DV
805int drm_connector_create_standard_properties(struct drm_device *dev)
806{
807 struct drm_property *prop;
808
809 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
810 DRM_MODE_PROP_IMMUTABLE,
811 "EDID", 0);
812 if (!prop)
813 return -ENOMEM;
814 dev->mode_config.edid_property = prop;
815
816 prop = drm_property_create_enum(dev, 0,
817 "DPMS", drm_dpms_enum_list,
818 ARRAY_SIZE(drm_dpms_enum_list));
819 if (!prop)
820 return -ENOMEM;
821 dev->mode_config.dpms_property = prop;
822
823 prop = drm_property_create(dev,
824 DRM_MODE_PROP_BLOB |
825 DRM_MODE_PROP_IMMUTABLE,
826 "PATH", 0);
827 if (!prop)
828 return -ENOMEM;
829 dev->mode_config.path_property = prop;
830
831 prop = drm_property_create(dev,
832 DRM_MODE_PROP_BLOB |
833 DRM_MODE_PROP_IMMUTABLE,
834 "TILE", 0);
835 if (!prop)
836 return -ENOMEM;
837 dev->mode_config.tile_property = prop;
838
40ee6fbe
MN
839 prop = drm_property_create_enum(dev, 0, "link-status",
840 drm_link_status_enum_list,
841 ARRAY_SIZE(drm_link_status_enum_list));
842 if (!prop)
843 return -ENOMEM;
844 dev->mode_config.link_status_property = prop;
845
66660d4c
DA
846 prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
847 if (!prop)
848 return -ENOMEM;
849 dev->mode_config.non_desktop_property = prop;
850
52217195
DV
851 return 0;
852}
853
854/**
855 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
856 * @dev: DRM device
857 *
858 * Called by a driver the first time a DVI-I connector is made.
859 */
860int drm_mode_create_dvi_i_properties(struct drm_device *dev)
861{
862 struct drm_property *dvi_i_selector;
863 struct drm_property *dvi_i_subconnector;
864
865 if (dev->mode_config.dvi_i_select_subconnector_property)
866 return 0;
867
868 dvi_i_selector =
869 drm_property_create_enum(dev, 0,
870 "select subconnector",
871 drm_dvi_i_select_enum_list,
872 ARRAY_SIZE(drm_dvi_i_select_enum_list));
873 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
874
875 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
876 "subconnector",
877 drm_dvi_i_subconnector_enum_list,
878 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
879 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
880
881 return 0;
882}
883EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
884
885/**
886 * drm_create_tv_properties - create TV specific connector properties
887 * @dev: DRM device
888 * @num_modes: number of different TV formats (modes) supported
889 * @modes: array of pointers to strings containing name of each format
890 *
891 * Called by a driver's TV initialization routine, this function creates
892 * the TV specific connector properties for a given device. Caller is
893 * responsible for allocating a list of format names and passing them to
894 * this routine.
895 */
896int drm_mode_create_tv_properties(struct drm_device *dev,
897 unsigned int num_modes,
898 const char * const modes[])
899{
900 struct drm_property *tv_selector;
901 struct drm_property *tv_subconnector;
902 unsigned int i;
903
904 if (dev->mode_config.tv_select_subconnector_property)
905 return 0;
906
907 /*
908 * Basic connector properties
909 */
910 tv_selector = drm_property_create_enum(dev, 0,
911 "select subconnector",
912 drm_tv_select_enum_list,
913 ARRAY_SIZE(drm_tv_select_enum_list));
914 if (!tv_selector)
915 goto nomem;
916
917 dev->mode_config.tv_select_subconnector_property = tv_selector;
918
919 tv_subconnector =
920 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
921 "subconnector",
922 drm_tv_subconnector_enum_list,
923 ARRAY_SIZE(drm_tv_subconnector_enum_list));
924 if (!tv_subconnector)
925 goto nomem;
926 dev->mode_config.tv_subconnector_property = tv_subconnector;
927
928 /*
929 * Other, TV specific properties: margins & TV modes.
930 */
931 dev->mode_config.tv_left_margin_property =
932 drm_property_create_range(dev, 0, "left margin", 0, 100);
933 if (!dev->mode_config.tv_left_margin_property)
934 goto nomem;
935
936 dev->mode_config.tv_right_margin_property =
937 drm_property_create_range(dev, 0, "right margin", 0, 100);
938 if (!dev->mode_config.tv_right_margin_property)
939 goto nomem;
940
941 dev->mode_config.tv_top_margin_property =
942 drm_property_create_range(dev, 0, "top margin", 0, 100);
943 if (!dev->mode_config.tv_top_margin_property)
944 goto nomem;
945
946 dev->mode_config.tv_bottom_margin_property =
947 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
948 if (!dev->mode_config.tv_bottom_margin_property)
949 goto nomem;
950
951 dev->mode_config.tv_mode_property =
952 drm_property_create(dev, DRM_MODE_PROP_ENUM,
953 "mode", num_modes);
954 if (!dev->mode_config.tv_mode_property)
955 goto nomem;
956
957 for (i = 0; i < num_modes; i++)
958 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
959 i, modes[i]);
960
961 dev->mode_config.tv_brightness_property =
962 drm_property_create_range(dev, 0, "brightness", 0, 100);
963 if (!dev->mode_config.tv_brightness_property)
964 goto nomem;
965
966 dev->mode_config.tv_contrast_property =
967 drm_property_create_range(dev, 0, "contrast", 0, 100);
968 if (!dev->mode_config.tv_contrast_property)
969 goto nomem;
970
971 dev->mode_config.tv_flicker_reduction_property =
972 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
973 if (!dev->mode_config.tv_flicker_reduction_property)
974 goto nomem;
975
976 dev->mode_config.tv_overscan_property =
977 drm_property_create_range(dev, 0, "overscan", 0, 100);
978 if (!dev->mode_config.tv_overscan_property)
979 goto nomem;
980
981 dev->mode_config.tv_saturation_property =
982 drm_property_create_range(dev, 0, "saturation", 0, 100);
983 if (!dev->mode_config.tv_saturation_property)
984 goto nomem;
985
986 dev->mode_config.tv_hue_property =
987 drm_property_create_range(dev, 0, "hue", 0, 100);
988 if (!dev->mode_config.tv_hue_property)
989 goto nomem;
990
991 return 0;
992nomem:
993 return -ENOMEM;
994}
995EXPORT_SYMBOL(drm_mode_create_tv_properties);
996
997/**
998 * drm_mode_create_scaling_mode_property - create scaling mode property
999 * @dev: DRM device
1000 *
1001 * Called by a driver the first time it's needed, must be attached to desired
1002 * connectors.
8f6e1e22
ML
1003 *
1004 * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1005 * instead to correctly assign &drm_connector_state.picture_aspect_ratio
1006 * in the atomic state.
52217195
DV
1007 */
1008int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1009{
1010 struct drm_property *scaling_mode;
1011
1012 if (dev->mode_config.scaling_mode_property)
1013 return 0;
1014
1015 scaling_mode =
1016 drm_property_create_enum(dev, 0, "scaling mode",
1017 drm_scaling_mode_enum_list,
1018 ARRAY_SIZE(drm_scaling_mode_enum_list));
1019
1020 dev->mode_config.scaling_mode_property = scaling_mode;
1021
1022 return 0;
1023}
1024EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1025
8f6e1e22
ML
1026/**
1027 * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1028 * @connector: connector to attach scaling mode property on.
1029 * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1030 *
1031 * This is used to add support for scaling mode to atomic drivers.
1032 * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
1033 * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1034 *
1035 * This is the atomic version of drm_mode_create_scaling_mode_property().
1036 *
1037 * Returns:
1038 * Zero on success, negative errno on failure.
1039 */
1040int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1041 u32 scaling_mode_mask)
1042{
1043 struct drm_device *dev = connector->dev;
1044 struct drm_property *scaling_mode_property;
1045 int i, j = 0;
1046 const unsigned valid_scaling_mode_mask =
1047 (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1048
1049 if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1050 scaling_mode_mask & ~valid_scaling_mode_mask))
1051 return -EINVAL;
1052
1053 scaling_mode_property =
1054 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1055 hweight32(scaling_mode_mask));
1056
1057 if (!scaling_mode_property)
1058 return -ENOMEM;
1059
1060 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1061 int ret;
1062
1063 if (!(BIT(i) & scaling_mode_mask))
1064 continue;
1065
1066 ret = drm_property_add_enum(scaling_mode_property, j++,
1067 drm_scaling_mode_enum_list[i].type,
1068 drm_scaling_mode_enum_list[i].name);
1069
1070 if (ret) {
1071 drm_property_destroy(dev, scaling_mode_property);
1072
1073 return ret;
1074 }
1075 }
1076
1077 drm_object_attach_property(&connector->base,
1078 scaling_mode_property, 0);
1079
1080 connector->scaling_mode_property = scaling_mode_property;
1081
1082 return 0;
1083}
1084EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1085
52217195
DV
1086/**
1087 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1088 * @dev: DRM device
1089 *
1090 * Called by a driver the first time it's needed, must be attached to desired
1091 * connectors.
1092 *
1093 * Returns:
1094 * Zero on success, negative errno on failure.
1095 */
1096int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1097{
1098 if (dev->mode_config.aspect_ratio_property)
1099 return 0;
1100
1101 dev->mode_config.aspect_ratio_property =
1102 drm_property_create_enum(dev, 0, "aspect ratio",
1103 drm_aspect_ratio_enum_list,
1104 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1105
1106 if (dev->mode_config.aspect_ratio_property == NULL)
1107 return -ENOMEM;
1108
1109 return 0;
1110}
1111EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1112
1113/**
1114 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1115 * @dev: DRM device
1116 *
1117 * Create the the suggested x/y offset property for connectors.
1118 */
1119int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1120{
1121 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1122 return 0;
1123
1124 dev->mode_config.suggested_x_property =
1125 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1126
1127 dev->mode_config.suggested_y_property =
1128 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1129
1130 if (dev->mode_config.suggested_x_property == NULL ||
1131 dev->mode_config.suggested_y_property == NULL)
1132 return -ENOMEM;
1133 return 0;
1134}
1135EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1136
1137/**
1138 * drm_mode_connector_set_path_property - set tile property on connector
1139 * @connector: connector to set property on.
1140 * @path: path to use for property; must not be NULL.
1141 *
1142 * This creates a property to expose to userspace to specify a
1143 * connector path. This is mainly used for DisplayPort MST where
1144 * connectors have a topology and we want to allow userspace to give
1145 * them more meaningful names.
1146 *
1147 * Returns:
1148 * Zero on success, negative errno on failure.
1149 */
1150int drm_mode_connector_set_path_property(struct drm_connector *connector,
1151 const char *path)
1152{
1153 struct drm_device *dev = connector->dev;
1154 int ret;
1155
1156 ret = drm_property_replace_global_blob(dev,
1157 &connector->path_blob_ptr,
1158 strlen(path) + 1,
1159 path,
1160 &connector->base,
1161 dev->mode_config.path_property);
1162 return ret;
1163}
1164EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1165
1166/**
1167 * drm_mode_connector_set_tile_property - set tile property on connector
1168 * @connector: connector to set property on.
1169 *
1170 * This looks up the tile information for a connector, and creates a
1171 * property for userspace to parse if it exists. The property is of
1172 * the form of 8 integers using ':' as a separator.
1173 *
1174 * Returns:
1175 * Zero on success, errno on failure.
1176 */
1177int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1178{
1179 struct drm_device *dev = connector->dev;
1180 char tile[256];
1181 int ret;
1182
1183 if (!connector->has_tile) {
1184 ret = drm_property_replace_global_blob(dev,
1185 &connector->tile_blob_ptr,
1186 0,
1187 NULL,
1188 &connector->base,
1189 dev->mode_config.tile_property);
1190 return ret;
1191 }
1192
1193 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1194 connector->tile_group->id, connector->tile_is_single_monitor,
1195 connector->num_h_tile, connector->num_v_tile,
1196 connector->tile_h_loc, connector->tile_v_loc,
1197 connector->tile_h_size, connector->tile_v_size);
1198
1199 ret = drm_property_replace_global_blob(dev,
1200 &connector->tile_blob_ptr,
1201 strlen(tile) + 1,
1202 tile,
1203 &connector->base,
1204 dev->mode_config.tile_property);
1205 return ret;
1206}
1207EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1208
1209/**
1210 * drm_mode_connector_update_edid_property - update the edid property of a connector
1211 * @connector: drm connector
1212 * @edid: new value of the edid property
1213 *
1214 * This function creates a new blob modeset object and assigns its id to the
1215 * connector's edid property.
1216 *
1217 * Returns:
1218 * Zero on success, negative errno on failure.
1219 */
1220int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1221 const struct edid *edid)
1222{
1223 struct drm_device *dev = connector->dev;
1224 size_t size = 0;
1225 int ret;
1226
1227 /* ignore requests to set edid when overridden */
1228 if (connector->override_edid)
1229 return 0;
1230
1231 if (edid)
1232 size = EDID_LENGTH * (1 + edid->extensions);
1233
4b4df570
KP
1234 /* Set the display info, using edid if available, otherwise
1235 * reseting the values to defaults. This duplicates the work
1236 * done in drm_add_edid_modes, but that function is not
1237 * consistently called before this one in all drivers and the
1238 * computation is cheap enough that it seems better to
1239 * duplicate it rather than attempt to ensure some arbitrary
1240 * ordering of calls.
1241 */
1242 if (edid)
1243 drm_add_display_info(connector, edid);
1244 else
1245 drm_reset_display_info(connector);
1246
66660d4c
DA
1247 drm_object_property_set_value(&connector->base,
1248 dev->mode_config.non_desktop_property,
1249 connector->display_info.non_desktop);
1250
52217195
DV
1251 ret = drm_property_replace_global_blob(dev,
1252 &connector->edid_blob_ptr,
1253 size,
1254 edid,
1255 &connector->base,
1256 dev->mode_config.edid_property);
1257 return ret;
1258}
1259EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1260
40ee6fbe
MN
1261/**
1262 * drm_mode_connector_set_link_status_property - Set link status property of a connector
1263 * @connector: drm connector
1264 * @link_status: new value of link status property (0: Good, 1: Bad)
1265 *
1266 * In usual working scenario, this link status property will always be set to
1267 * "GOOD". If something fails during or after a mode set, the kernel driver
1268 * may set this link status property to "BAD". The caller then needs to send a
1269 * hotplug uevent for userspace to re-check the valid modes through
1270 * GET_CONNECTOR_IOCTL and retry modeset.
1271 *
1272 * Note: Drivers cannot rely on userspace to support this property and
1273 * issue a modeset. As such, they may choose to handle issues (like
1274 * re-training a link) without userspace's intervention.
1275 *
1276 * The reason for adding this property is to handle link training failures, but
1277 * it is not limited to DP or link training. For example, if we implement
1278 * asynchronous setcrtc, this property can be used to report any failures in that.
1279 */
1280void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
1281 uint64_t link_status)
1282{
1283 struct drm_device *dev = connector->dev;
1284
1285 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1286 connector->state->link_status = link_status;
1287 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1288}
1289EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
1290
52217195
DV
1291int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1292 struct drm_property *property,
1293 uint64_t value)
1294{
1295 int ret = -EINVAL;
1296 struct drm_connector *connector = obj_to_connector(obj);
1297
1298 /* Do DPMS ourselves */
1299 if (property == connector->dev->mode_config.dpms_property) {
1300 ret = (*connector->funcs->dpms)(connector, (int)value);
1301 } else if (connector->funcs->set_property)
1302 ret = connector->funcs->set_property(connector, property, value);
1303
144a7999 1304 if (!ret)
52217195
DV
1305 drm_object_property_set_value(&connector->base, property, value);
1306 return ret;
1307}
1308
1309int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1310 void *data, struct drm_file *file_priv)
1311{
1312 struct drm_mode_connector_set_property *conn_set_prop = data;
1313 struct drm_mode_obj_set_property obj_set_prop = {
1314 .value = conn_set_prop->value,
1315 .prop_id = conn_set_prop->prop_id,
1316 .obj_id = conn_set_prop->connector_id,
1317 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1318 };
1319
1320 /* It does all the locking and checking we need */
1321 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1322}
1323
1324static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1325{
1326 /* For atomic drivers only state objects are synchronously updated and
1327 * protected by modeset locks, so check those first. */
1328 if (connector->state)
1329 return connector->state->best_encoder;
1330 return connector->encoder;
1331}
1332
1333static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1334 const struct drm_file *file_priv)
1335{
1336 /*
1337 * If user-space hasn't configured the driver to expose the stereo 3D
1338 * modes, don't expose them.
1339 */
1340 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1341 return false;
1342
1343 return true;
1344}
1345
1346int drm_mode_getconnector(struct drm_device *dev, void *data,
1347 struct drm_file *file_priv)
1348{
1349 struct drm_mode_get_connector *out_resp = data;
1350 struct drm_connector *connector;
1351 struct drm_encoder *encoder;
1352 struct drm_display_mode *mode;
1353 int mode_count = 0;
1354 int encoders_count = 0;
1355 int ret = 0;
1356 int copied = 0;
1357 int i;
1358 struct drm_mode_modeinfo u_mode;
1359 struct drm_mode_modeinfo __user *mode_ptr;
1360 uint32_t __user *encoder_ptr;
1361
1362 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1363 return -EINVAL;
1364
1365 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1366
418da172 1367 connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
91eefc05
DV
1368 if (!connector)
1369 return -ENOENT;
1370
52217195
DV
1371 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1372 if (connector->encoder_ids[i] != 0)
1373 encoders_count++;
1374
91eefc05
DV
1375 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1376 copied = 0;
1377 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1378 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1379 if (connector->encoder_ids[i] != 0) {
1380 if (put_user(connector->encoder_ids[i],
1381 encoder_ptr + copied)) {
1382 ret = -EFAULT;
e94ac351 1383 goto out;
91eefc05
DV
1384 }
1385 copied++;
1386 }
1387 }
1388 }
1389 out_resp->count_encoders = encoders_count;
1390
1391 out_resp->connector_id = connector->base.id;
1392 out_resp->connector_type = connector->connector_type;
1393 out_resp->connector_type_id = connector->connector_type_id;
1394
1395 mutex_lock(&dev->mode_config.mutex);
52217195
DV
1396 if (out_resp->count_modes == 0) {
1397 connector->funcs->fill_modes(connector,
1398 dev->mode_config.max_width,
1399 dev->mode_config.max_height);
1400 }
1401
52217195
DV
1402 out_resp->mm_width = connector->display_info.width_mm;
1403 out_resp->mm_height = connector->display_info.height_mm;
1404 out_resp->subpixel = connector->display_info.subpixel_order;
1405 out_resp->connection = connector->status;
1406
91eefc05
DV
1407 /* delayed so we get modes regardless of pre-fill_modes state */
1408 list_for_each_entry(mode, &connector->modes, head)
1409 if (drm_mode_expose_to_userspace(mode, file_priv))
1410 mode_count++;
52217195
DV
1411
1412 /*
1413 * This ioctl is called twice, once to determine how much space is
1414 * needed, and the 2nd time to fill it.
1415 */
1416 if ((out_resp->count_modes >= mode_count) && mode_count) {
1417 copied = 0;
1418 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1419 list_for_each_entry(mode, &connector->modes, head) {
1420 if (!drm_mode_expose_to_userspace(mode, file_priv))
1421 continue;
1422
1423 drm_mode_convert_to_umode(&u_mode, mode);
1424 if (copy_to_user(mode_ptr + copied,
1425 &u_mode, sizeof(u_mode))) {
1426 ret = -EFAULT;
e94ac351
DV
1427 mutex_unlock(&dev->mode_config.mutex);
1428
52217195
DV
1429 goto out;
1430 }
1431 copied++;
1432 }
1433 }
1434 out_resp->count_modes = mode_count;
52217195 1435 mutex_unlock(&dev->mode_config.mutex);
e94ac351
DV
1436
1437 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1438 encoder = drm_connector_get_encoder(connector);
1439 if (encoder)
1440 out_resp->encoder_id = encoder->base.id;
1441 else
1442 out_resp->encoder_id = 0;
1443
1444 /* Only grab properties after probing, to make sure EDID and other
1445 * properties reflect the latest status. */
1446 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1447 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1448 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1449 &out_resp->count_props);
1450 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1451
1452out:
ad093607 1453 drm_connector_put(connector);
52217195
DV
1454
1455 return ret;
1456}
1457
9498c19b
DV
1458
1459/**
1460 * DOC: Tile group
1461 *
1462 * Tile groups are used to represent tiled monitors with a unique integer
1463 * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1464 * we store this in a tile group, so we have a common identifier for all tiles
1465 * in a monitor group. The property is called "TILE". Drivers can manage tile
1466 * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1467 * drm_mode_get_tile_group(). But this is only needed for internal panels where
1468 * the tile group information is exposed through a non-standard way.
1469 */
1470
1471static void drm_tile_group_free(struct kref *kref)
1472{
1473 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1474 struct drm_device *dev = tg->dev;
1475 mutex_lock(&dev->mode_config.idr_mutex);
1476 idr_remove(&dev->mode_config.tile_idr, tg->id);
1477 mutex_unlock(&dev->mode_config.idr_mutex);
1478 kfree(tg);
1479}
1480
1481/**
1482 * drm_mode_put_tile_group - drop a reference to a tile group.
1483 * @dev: DRM device
1484 * @tg: tile group to drop reference to.
1485 *
1486 * drop reference to tile group and free if 0.
1487 */
1488void drm_mode_put_tile_group(struct drm_device *dev,
1489 struct drm_tile_group *tg)
1490{
1491 kref_put(&tg->refcount, drm_tile_group_free);
1492}
1493EXPORT_SYMBOL(drm_mode_put_tile_group);
1494
1495/**
1496 * drm_mode_get_tile_group - get a reference to an existing tile group
1497 * @dev: DRM device
1498 * @topology: 8-bytes unique per monitor.
1499 *
1500 * Use the unique bytes to get a reference to an existing tile group.
1501 *
1502 * RETURNS:
1503 * tile group or NULL if not found.
1504 */
1505struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1506 char topology[8])
1507{
1508 struct drm_tile_group *tg;
1509 int id;
1510 mutex_lock(&dev->mode_config.idr_mutex);
1511 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1512 if (!memcmp(tg->group_data, topology, 8)) {
1513 if (!kref_get_unless_zero(&tg->refcount))
1514 tg = NULL;
1515 mutex_unlock(&dev->mode_config.idr_mutex);
1516 return tg;
1517 }
1518 }
1519 mutex_unlock(&dev->mode_config.idr_mutex);
1520 return NULL;
1521}
1522EXPORT_SYMBOL(drm_mode_get_tile_group);
1523
1524/**
1525 * drm_mode_create_tile_group - create a tile group from a displayid description
1526 * @dev: DRM device
1527 * @topology: 8-bytes unique per monitor.
1528 *
1529 * Create a tile group for the unique monitor, and get a unique
1530 * identifier for the tile group.
1531 *
1532 * RETURNS:
1533 * new tile group or error.
1534 */
1535struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1536 char topology[8])
1537{
1538 struct drm_tile_group *tg;
1539 int ret;
1540
1541 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1542 if (!tg)
1543 return ERR_PTR(-ENOMEM);
1544
1545 kref_init(&tg->refcount);
1546 memcpy(tg->group_data, topology, 8);
1547 tg->dev = dev;
1548
1549 mutex_lock(&dev->mode_config.idr_mutex);
1550 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1551 if (ret >= 0) {
1552 tg->id = ret;
1553 } else {
1554 kfree(tg);
1555 tg = ERR_PTR(ret);
1556 }
1557
1558 mutex_unlock(&dev->mode_config.idr_mutex);
1559 return tg;
1560}
1561EXPORT_SYMBOL(drm_mode_create_tile_group);