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