]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/drm_connector.c
drm: Don't include <drm/drm_encoder.h> in <drm/drm_crtc.h>
[karo-tx-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
28 #include "drm_crtc_internal.h"
29 #include "drm_internal.h"
30
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.
38  * Hence they are reference-counted using drm_connector_reference() and
39  * drm_connector_unreference().
40  *
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
43  * objects and initialized by setting the following fields.
44  *
45  * The connector is then registered with a call to drm_connector_init() with a
46  * pointer to the connector functions and a connector type, and exposed through
47  * sysfs 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 struct &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                 const char *s;
133
134                 switch (mode->force) {
135                 case DRM_FORCE_OFF:
136                         s = "OFF";
137                         break;
138                 case DRM_FORCE_ON_DIGITAL:
139                         s = "ON - dig";
140                         break;
141                 default:
142                 case DRM_FORCE_ON:
143                         s = "ON";
144                         break;
145                 }
146
147                 DRM_INFO("forcing %s connector %s\n", connector->name, s);
148                 connector->force = mode->force;
149         }
150
151         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
152                       connector->name,
153                       mode->xres, mode->yres,
154                       mode->refresh_specified ? mode->refresh : 60,
155                       mode->rb ? " reduced blanking" : "",
156                       mode->margins ? " with margins" : "",
157                       mode->interlace ?  " interlaced" : "");
158 }
159
160 static void drm_connector_free(struct kref *kref)
161 {
162         struct drm_connector *connector =
163                 container_of(kref, struct drm_connector, base.refcount);
164         struct drm_device *dev = connector->dev;
165
166         drm_mode_object_unregister(dev, &connector->base);
167         connector->funcs->destroy(connector);
168 }
169
170 /**
171  * drm_connector_init - Init a preallocated connector
172  * @dev: DRM device
173  * @connector: the connector to init
174  * @funcs: callbacks for this connector
175  * @connector_type: user visible type of the connector
176  *
177  * Initialises a preallocated connector. Connectors should be
178  * subclassed as part of driver connector objects.
179  *
180  * Returns:
181  * Zero on success, error code on failure.
182  */
183 int drm_connector_init(struct drm_device *dev,
184                        struct drm_connector *connector,
185                        const struct drm_connector_funcs *funcs,
186                        int connector_type)
187 {
188         struct drm_mode_config *config = &dev->mode_config;
189         int ret;
190         struct ida *connector_ida =
191                 &drm_connector_enum_list[connector_type].ida;
192
193         drm_modeset_lock_all(dev);
194
195         ret = drm_mode_object_get_reg(dev, &connector->base,
196                                       DRM_MODE_OBJECT_CONNECTOR,
197                                       false, drm_connector_free);
198         if (ret)
199                 goto out_unlock;
200
201         connector->base.properties = &connector->properties;
202         connector->dev = dev;
203         connector->funcs = funcs;
204
205         ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
206         if (ret < 0)
207                 goto out_put;
208         connector->index = ret;
209         ret = 0;
210
211         connector->connector_type = connector_type;
212         connector->connector_type_id =
213                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
214         if (connector->connector_type_id < 0) {
215                 ret = connector->connector_type_id;
216                 goto out_put_id;
217         }
218         connector->name =
219                 kasprintf(GFP_KERNEL, "%s-%d",
220                           drm_connector_enum_list[connector_type].name,
221                           connector->connector_type_id);
222         if (!connector->name) {
223                 ret = -ENOMEM;
224                 goto out_put_type_id;
225         }
226
227         INIT_LIST_HEAD(&connector->probed_modes);
228         INIT_LIST_HEAD(&connector->modes);
229         connector->edid_blob_ptr = NULL;
230         connector->status = connector_status_unknown;
231
232         drm_connector_get_cmdline_mode(connector);
233
234         /* We should add connectors at the end to avoid upsetting the connector
235          * index too much. */
236         list_add_tail(&connector->head, &config->connector_list);
237         config->num_connector++;
238
239         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
240                 drm_object_attach_property(&connector->base,
241                                               config->edid_property,
242                                               0);
243
244         drm_object_attach_property(&connector->base,
245                                       config->dpms_property, 0);
246
247         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
248                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
249         }
250
251         connector->debugfs_entry = NULL;
252 out_put_type_id:
253         if (ret)
254                 ida_simple_remove(connector_ida, connector->connector_type_id);
255 out_put_id:
256         if (ret)
257                 ida_simple_remove(&config->connector_ida, connector->index);
258 out_put:
259         if (ret)
260                 drm_mode_object_unregister(dev, &connector->base);
261
262 out_unlock:
263         drm_modeset_unlock_all(dev);
264
265         return ret;
266 }
267 EXPORT_SYMBOL(drm_connector_init);
268
269 /**
270  * drm_mode_connector_attach_encoder - attach a connector to an encoder
271  * @connector: connector to attach
272  * @encoder: encoder to attach @connector to
273  *
274  * This function links up a connector to an encoder. Note that the routing
275  * restrictions between encoders and crtcs are exposed to userspace through the
276  * possible_clones and possible_crtcs bitmasks.
277  *
278  * Returns:
279  * Zero on success, negative errno on failure.
280  */
281 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
282                                       struct drm_encoder *encoder)
283 {
284         int i;
285
286         /*
287          * In the past, drivers have attempted to model the static association
288          * of connector to encoder in simple connector/encoder devices using a
289          * direct assignment of connector->encoder = encoder. This connection
290          * is a logical one and the responsibility of the core, so drivers are
291          * expected not to mess with this.
292          *
293          * Note that the error return should've been enough here, but a large
294          * majority of drivers ignores the return value, so add in a big WARN
295          * to get people's attention.
296          */
297         if (WARN_ON(connector->encoder))
298                 return -EINVAL;
299
300         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
301                 if (connector->encoder_ids[i] == 0) {
302                         connector->encoder_ids[i] = encoder->base.id;
303                         return 0;
304                 }
305         }
306         return -ENOMEM;
307 }
308 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
309
310 static void drm_mode_remove(struct drm_connector *connector,
311                             struct drm_display_mode *mode)
312 {
313         list_del(&mode->head);
314         drm_mode_destroy(connector->dev, mode);
315 }
316
317 /**
318  * drm_connector_cleanup - cleans up an initialised connector
319  * @connector: connector to cleanup
320  *
321  * Cleans up the connector but doesn't free the object.
322  */
323 void drm_connector_cleanup(struct drm_connector *connector)
324 {
325         struct drm_device *dev = connector->dev;
326         struct drm_display_mode *mode, *t;
327
328         /* The connector should have been removed from userspace long before
329          * it is finally destroyed.
330          */
331         if (WARN_ON(connector->registered))
332                 drm_connector_unregister(connector);
333
334         if (connector->tile_group) {
335                 drm_mode_put_tile_group(dev, connector->tile_group);
336                 connector->tile_group = NULL;
337         }
338
339         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
340                 drm_mode_remove(connector, mode);
341
342         list_for_each_entry_safe(mode, t, &connector->modes, head)
343                 drm_mode_remove(connector, mode);
344
345         ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
346                           connector->connector_type_id);
347
348         ida_simple_remove(&dev->mode_config.connector_ida,
349                           connector->index);
350
351         kfree(connector->display_info.bus_formats);
352         drm_mode_object_unregister(dev, &connector->base);
353         kfree(connector->name);
354         connector->name = NULL;
355         list_del(&connector->head);
356         dev->mode_config.num_connector--;
357
358         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
359         if (connector->state && connector->funcs->atomic_destroy_state)
360                 connector->funcs->atomic_destroy_state(connector,
361                                                        connector->state);
362
363         memset(connector, 0, sizeof(*connector));
364 }
365 EXPORT_SYMBOL(drm_connector_cleanup);
366
367 /**
368  * drm_connector_register - register a connector
369  * @connector: the connector to register
370  *
371  * Register userspace interfaces for a connector
372  *
373  * Returns:
374  * Zero on success, error code on failure.
375  */
376 int drm_connector_register(struct drm_connector *connector)
377 {
378         int ret;
379
380         if (connector->registered)
381                 return 0;
382
383         ret = drm_sysfs_connector_add(connector);
384         if (ret)
385                 return ret;
386
387         ret = drm_debugfs_connector_add(connector);
388         if (ret) {
389                 goto err_sysfs;
390         }
391
392         if (connector->funcs->late_register) {
393                 ret = connector->funcs->late_register(connector);
394                 if (ret)
395                         goto err_debugfs;
396         }
397
398         drm_mode_object_register(connector->dev, &connector->base);
399
400         connector->registered = true;
401         return 0;
402
403 err_debugfs:
404         drm_debugfs_connector_remove(connector);
405 err_sysfs:
406         drm_sysfs_connector_remove(connector);
407         return ret;
408 }
409 EXPORT_SYMBOL(drm_connector_register);
410
411 /**
412  * drm_connector_unregister - unregister a connector
413  * @connector: the connector to unregister
414  *
415  * Unregister userspace interfaces for a connector
416  */
417 void drm_connector_unregister(struct drm_connector *connector)
418 {
419         if (!connector->registered)
420                 return;
421
422         if (connector->funcs->early_unregister)
423                 connector->funcs->early_unregister(connector);
424
425         drm_sysfs_connector_remove(connector);
426         drm_debugfs_connector_remove(connector);
427
428         connector->registered = false;
429 }
430 EXPORT_SYMBOL(drm_connector_unregister);
431
432 void drm_connector_unregister_all(struct drm_device *dev)
433 {
434         struct drm_connector *connector;
435
436         /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
437         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
438                 drm_connector_unregister(connector);
439 }
440
441 int drm_connector_register_all(struct drm_device *dev)
442 {
443         struct drm_connector *connector;
444         int ret;
445
446         /* FIXME: taking the mode config mutex ends up in a clash with
447          * fbcon/backlight registration */
448         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
449                 ret = drm_connector_register(connector);
450                 if (ret)
451                         goto err;
452         }
453
454         return 0;
455
456 err:
457         mutex_unlock(&dev->mode_config.mutex);
458         drm_connector_unregister_all(dev);
459         return ret;
460 }
461
462 /**
463  * drm_get_connector_status_name - return a string for connector status
464  * @status: connector status to compute name of
465  *
466  * In contrast to the other drm_get_*_name functions this one here returns a
467  * const pointer and hence is threadsafe.
468  */
469 const char *drm_get_connector_status_name(enum drm_connector_status status)
470 {
471         if (status == connector_status_connected)
472                 return "connected";
473         else if (status == connector_status_disconnected)
474                 return "disconnected";
475         else
476                 return "unknown";
477 }
478 EXPORT_SYMBOL(drm_get_connector_status_name);
479
480 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
481         { SubPixelUnknown, "Unknown" },
482         { SubPixelHorizontalRGB, "Horizontal RGB" },
483         { SubPixelHorizontalBGR, "Horizontal BGR" },
484         { SubPixelVerticalRGB, "Vertical RGB" },
485         { SubPixelVerticalBGR, "Vertical BGR" },
486         { SubPixelNone, "None" },
487 };
488
489 /**
490  * drm_get_subpixel_order_name - return a string for a given subpixel enum
491  * @order: enum of subpixel_order
492  *
493  * Note you could abuse this and return something out of bounds, but that
494  * would be a caller error.  No unscrubbed user data should make it here.
495  */
496 const char *drm_get_subpixel_order_name(enum subpixel_order order)
497 {
498         return drm_subpixel_enum_list[order].name;
499 }
500 EXPORT_SYMBOL(drm_get_subpixel_order_name);
501
502 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
503         { DRM_MODE_DPMS_ON, "On" },
504         { DRM_MODE_DPMS_STANDBY, "Standby" },
505         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
506         { DRM_MODE_DPMS_OFF, "Off" }
507 };
508 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
509
510 /**
511  * drm_display_info_set_bus_formats - set the supported bus formats
512  * @info: display info to store bus formats in
513  * @formats: array containing the supported bus formats
514  * @num_formats: the number of entries in the fmts array
515  *
516  * Store the supported bus formats in display info structure.
517  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
518  * a full list of available formats.
519  */
520 int drm_display_info_set_bus_formats(struct drm_display_info *info,
521                                      const u32 *formats,
522                                      unsigned int num_formats)
523 {
524         u32 *fmts = NULL;
525
526         if (!formats && num_formats)
527                 return -EINVAL;
528
529         if (formats && num_formats) {
530                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
531                                GFP_KERNEL);
532                 if (!fmts)
533                         return -ENOMEM;
534         }
535
536         kfree(info->bus_formats);
537         info->bus_formats = fmts;
538         info->num_bus_formats = num_formats;
539
540         return 0;
541 }
542 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
543
544 /* Optional connector properties. */
545 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
546         { DRM_MODE_SCALE_NONE, "None" },
547         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
548         { DRM_MODE_SCALE_CENTER, "Center" },
549         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
550 };
551
552 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
553         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
554         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
555         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
556 };
557
558 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
559         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
560         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
561         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
562 };
563 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
564
565 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
566         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
567         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
568         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
569 };
570 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
571                  drm_dvi_i_subconnector_enum_list)
572
573 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
574         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
575         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
576         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
577         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
578         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
579 };
580 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
581
582 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
583         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
584         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
585         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
586         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
587         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
588 };
589 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
590                  drm_tv_subconnector_enum_list)
591
592 /**
593  * DOC: standard connector properties
594  *
595  * DRM connectors have a few standardized properties:
596  *
597  * EDID:
598  *      Blob property which contains the current EDID read from the sink. This
599  *      is useful to parse sink identification information like vendor, model
600  *      and serial. Drivers should update this property by calling
601  *      drm_mode_connector_update_edid_property(), usually after having parsed
602  *      the EDID using drm_add_edid_modes(). Userspace cannot change this
603  *      property.
604  * DPMS:
605  *      Legacy property for setting the power state of the connector. For atomic
606  *      drivers this is only provided for backwards compatibility with existing
607  *      drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
608  *      connector is linked to. Drivers should never set this property directly,
609  *      it is handled by the DRM core by calling the ->dpms() callback in
610  *      &drm_connector_funcs. Atomic drivers should implement this hook using
611  *      drm_atomic_helper_connector_dpms(). This is the only property standard
612  *      connector property that userspace can change.
613  * PATH:
614  *      Connector path property to identify how this sink is physically
615  *      connected. Used by DP MST. This should be set by calling
616  *      drm_mode_connector_set_path_property(), in the case of DP MST with the
617  *      path property the MST manager created. Userspace cannot change this
618  *      property.
619  * TILE:
620  *      Connector tile group property to indicate how a set of DRM connector
621  *      compose together into one logical screen. This is used by both high-res
622  *      external screens (often only using a single cable, but exposing multiple
623  *      DP MST sinks), or high-res integrated panels (like dual-link DSI) which
624  *      are not gen-locked. Note that for tiled panels which are genlocked, like
625  *      dual-link LVDS or dual-link DSI, the driver should try to not expose the
626  *      tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
627  *      should update this value using drm_mode_connector_set_tile_property().
628  *      Userspace cannot change this property.
629  *
630  * Connectors also have one standardized atomic property:
631  *
632  * CRTC_ID:
633  *      Mode object ID of the &drm_crtc this connector should be connected to.
634  */
635
636 int drm_connector_create_standard_properties(struct drm_device *dev)
637 {
638         struct drm_property *prop;
639
640         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
641                                    DRM_MODE_PROP_IMMUTABLE,
642                                    "EDID", 0);
643         if (!prop)
644                 return -ENOMEM;
645         dev->mode_config.edid_property = prop;
646
647         prop = drm_property_create_enum(dev, 0,
648                                    "DPMS", drm_dpms_enum_list,
649                                    ARRAY_SIZE(drm_dpms_enum_list));
650         if (!prop)
651                 return -ENOMEM;
652         dev->mode_config.dpms_property = prop;
653
654         prop = drm_property_create(dev,
655                                    DRM_MODE_PROP_BLOB |
656                                    DRM_MODE_PROP_IMMUTABLE,
657                                    "PATH", 0);
658         if (!prop)
659                 return -ENOMEM;
660         dev->mode_config.path_property = prop;
661
662         prop = drm_property_create(dev,
663                                    DRM_MODE_PROP_BLOB |
664                                    DRM_MODE_PROP_IMMUTABLE,
665                                    "TILE", 0);
666         if (!prop)
667                 return -ENOMEM;
668         dev->mode_config.tile_property = prop;
669
670         return 0;
671 }
672
673 /**
674  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
675  * @dev: DRM device
676  *
677  * Called by a driver the first time a DVI-I connector is made.
678  */
679 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
680 {
681         struct drm_property *dvi_i_selector;
682         struct drm_property *dvi_i_subconnector;
683
684         if (dev->mode_config.dvi_i_select_subconnector_property)
685                 return 0;
686
687         dvi_i_selector =
688                 drm_property_create_enum(dev, 0,
689                                     "select subconnector",
690                                     drm_dvi_i_select_enum_list,
691                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
692         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
693
694         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
695                                     "subconnector",
696                                     drm_dvi_i_subconnector_enum_list,
697                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
698         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
699
700         return 0;
701 }
702 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
703
704 /**
705  * drm_create_tv_properties - create TV specific connector properties
706  * @dev: DRM device
707  * @num_modes: number of different TV formats (modes) supported
708  * @modes: array of pointers to strings containing name of each format
709  *
710  * Called by a driver's TV initialization routine, this function creates
711  * the TV specific connector properties for a given device.  Caller is
712  * responsible for allocating a list of format names and passing them to
713  * this routine.
714  */
715 int drm_mode_create_tv_properties(struct drm_device *dev,
716                                   unsigned int num_modes,
717                                   const char * const modes[])
718 {
719         struct drm_property *tv_selector;
720         struct drm_property *tv_subconnector;
721         unsigned int i;
722
723         if (dev->mode_config.tv_select_subconnector_property)
724                 return 0;
725
726         /*
727          * Basic connector properties
728          */
729         tv_selector = drm_property_create_enum(dev, 0,
730                                           "select subconnector",
731                                           drm_tv_select_enum_list,
732                                           ARRAY_SIZE(drm_tv_select_enum_list));
733         if (!tv_selector)
734                 goto nomem;
735
736         dev->mode_config.tv_select_subconnector_property = tv_selector;
737
738         tv_subconnector =
739                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
740                                     "subconnector",
741                                     drm_tv_subconnector_enum_list,
742                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
743         if (!tv_subconnector)
744                 goto nomem;
745         dev->mode_config.tv_subconnector_property = tv_subconnector;
746
747         /*
748          * Other, TV specific properties: margins & TV modes.
749          */
750         dev->mode_config.tv_left_margin_property =
751                 drm_property_create_range(dev, 0, "left margin", 0, 100);
752         if (!dev->mode_config.tv_left_margin_property)
753                 goto nomem;
754
755         dev->mode_config.tv_right_margin_property =
756                 drm_property_create_range(dev, 0, "right margin", 0, 100);
757         if (!dev->mode_config.tv_right_margin_property)
758                 goto nomem;
759
760         dev->mode_config.tv_top_margin_property =
761                 drm_property_create_range(dev, 0, "top margin", 0, 100);
762         if (!dev->mode_config.tv_top_margin_property)
763                 goto nomem;
764
765         dev->mode_config.tv_bottom_margin_property =
766                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
767         if (!dev->mode_config.tv_bottom_margin_property)
768                 goto nomem;
769
770         dev->mode_config.tv_mode_property =
771                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
772                                     "mode", num_modes);
773         if (!dev->mode_config.tv_mode_property)
774                 goto nomem;
775
776         for (i = 0; i < num_modes; i++)
777                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
778                                       i, modes[i]);
779
780         dev->mode_config.tv_brightness_property =
781                 drm_property_create_range(dev, 0, "brightness", 0, 100);
782         if (!dev->mode_config.tv_brightness_property)
783                 goto nomem;
784
785         dev->mode_config.tv_contrast_property =
786                 drm_property_create_range(dev, 0, "contrast", 0, 100);
787         if (!dev->mode_config.tv_contrast_property)
788                 goto nomem;
789
790         dev->mode_config.tv_flicker_reduction_property =
791                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
792         if (!dev->mode_config.tv_flicker_reduction_property)
793                 goto nomem;
794
795         dev->mode_config.tv_overscan_property =
796                 drm_property_create_range(dev, 0, "overscan", 0, 100);
797         if (!dev->mode_config.tv_overscan_property)
798                 goto nomem;
799
800         dev->mode_config.tv_saturation_property =
801                 drm_property_create_range(dev, 0, "saturation", 0, 100);
802         if (!dev->mode_config.tv_saturation_property)
803                 goto nomem;
804
805         dev->mode_config.tv_hue_property =
806                 drm_property_create_range(dev, 0, "hue", 0, 100);
807         if (!dev->mode_config.tv_hue_property)
808                 goto nomem;
809
810         return 0;
811 nomem:
812         return -ENOMEM;
813 }
814 EXPORT_SYMBOL(drm_mode_create_tv_properties);
815
816 /**
817  * drm_mode_create_scaling_mode_property - create scaling mode property
818  * @dev: DRM device
819  *
820  * Called by a driver the first time it's needed, must be attached to desired
821  * connectors.
822  */
823 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
824 {
825         struct drm_property *scaling_mode;
826
827         if (dev->mode_config.scaling_mode_property)
828                 return 0;
829
830         scaling_mode =
831                 drm_property_create_enum(dev, 0, "scaling mode",
832                                 drm_scaling_mode_enum_list,
833                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
834
835         dev->mode_config.scaling_mode_property = scaling_mode;
836
837         return 0;
838 }
839 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
840
841 /**
842  * drm_mode_create_aspect_ratio_property - create aspect ratio property
843  * @dev: DRM device
844  *
845  * Called by a driver the first time it's needed, must be attached to desired
846  * connectors.
847  *
848  * Returns:
849  * Zero on success, negative errno on failure.
850  */
851 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
852 {
853         if (dev->mode_config.aspect_ratio_property)
854                 return 0;
855
856         dev->mode_config.aspect_ratio_property =
857                 drm_property_create_enum(dev, 0, "aspect ratio",
858                                 drm_aspect_ratio_enum_list,
859                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
860
861         if (dev->mode_config.aspect_ratio_property == NULL)
862                 return -ENOMEM;
863
864         return 0;
865 }
866 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
867
868 /**
869  * drm_mode_create_suggested_offset_properties - create suggests offset properties
870  * @dev: DRM device
871  *
872  * Create the the suggested x/y offset property for connectors.
873  */
874 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
875 {
876         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
877                 return 0;
878
879         dev->mode_config.suggested_x_property =
880                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
881
882         dev->mode_config.suggested_y_property =
883                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
884
885         if (dev->mode_config.suggested_x_property == NULL ||
886             dev->mode_config.suggested_y_property == NULL)
887                 return -ENOMEM;
888         return 0;
889 }
890 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
891
892 /**
893  * drm_mode_connector_set_path_property - set tile property on connector
894  * @connector: connector to set property on.
895  * @path: path to use for property; must not be NULL.
896  *
897  * This creates a property to expose to userspace to specify a
898  * connector path. This is mainly used for DisplayPort MST where
899  * connectors have a topology and we want to allow userspace to give
900  * them more meaningful names.
901  *
902  * Returns:
903  * Zero on success, negative errno on failure.
904  */
905 int drm_mode_connector_set_path_property(struct drm_connector *connector,
906                                          const char *path)
907 {
908         struct drm_device *dev = connector->dev;
909         int ret;
910
911         ret = drm_property_replace_global_blob(dev,
912                                                &connector->path_blob_ptr,
913                                                strlen(path) + 1,
914                                                path,
915                                                &connector->base,
916                                                dev->mode_config.path_property);
917         return ret;
918 }
919 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
920
921 /**
922  * drm_mode_connector_set_tile_property - set tile property on connector
923  * @connector: connector to set property on.
924  *
925  * This looks up the tile information for a connector, and creates a
926  * property for userspace to parse if it exists. The property is of
927  * the form of 8 integers using ':' as a separator.
928  *
929  * Returns:
930  * Zero on success, errno on failure.
931  */
932 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
933 {
934         struct drm_device *dev = connector->dev;
935         char tile[256];
936         int ret;
937
938         if (!connector->has_tile) {
939                 ret  = drm_property_replace_global_blob(dev,
940                                                         &connector->tile_blob_ptr,
941                                                         0,
942                                                         NULL,
943                                                         &connector->base,
944                                                         dev->mode_config.tile_property);
945                 return ret;
946         }
947
948         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
949                  connector->tile_group->id, connector->tile_is_single_monitor,
950                  connector->num_h_tile, connector->num_v_tile,
951                  connector->tile_h_loc, connector->tile_v_loc,
952                  connector->tile_h_size, connector->tile_v_size);
953
954         ret = drm_property_replace_global_blob(dev,
955                                                &connector->tile_blob_ptr,
956                                                strlen(tile) + 1,
957                                                tile,
958                                                &connector->base,
959                                                dev->mode_config.tile_property);
960         return ret;
961 }
962 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
963
964 /**
965  * drm_mode_connector_update_edid_property - update the edid property of a connector
966  * @connector: drm connector
967  * @edid: new value of the edid property
968  *
969  * This function creates a new blob modeset object and assigns its id to the
970  * connector's edid property.
971  *
972  * Returns:
973  * Zero on success, negative errno on failure.
974  */
975 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
976                                             const struct edid *edid)
977 {
978         struct drm_device *dev = connector->dev;
979         size_t size = 0;
980         int ret;
981
982         /* ignore requests to set edid when overridden */
983         if (connector->override_edid)
984                 return 0;
985
986         if (edid)
987                 size = EDID_LENGTH * (1 + edid->extensions);
988
989         ret = drm_property_replace_global_blob(dev,
990                                                &connector->edid_blob_ptr,
991                                                size,
992                                                edid,
993                                                &connector->base,
994                                                dev->mode_config.edid_property);
995         return ret;
996 }
997 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
998
999 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1000                                     struct drm_property *property,
1001                                     uint64_t value)
1002 {
1003         int ret = -EINVAL;
1004         struct drm_connector *connector = obj_to_connector(obj);
1005
1006         /* Do DPMS ourselves */
1007         if (property == connector->dev->mode_config.dpms_property) {
1008                 ret = (*connector->funcs->dpms)(connector, (int)value);
1009         } else if (connector->funcs->set_property)
1010                 ret = connector->funcs->set_property(connector, property, value);
1011
1012         /* store the property value if successful */
1013         if (!ret)
1014                 drm_object_property_set_value(&connector->base, property, value);
1015         return ret;
1016 }
1017
1018 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1019                                        void *data, struct drm_file *file_priv)
1020 {
1021         struct drm_mode_connector_set_property *conn_set_prop = data;
1022         struct drm_mode_obj_set_property obj_set_prop = {
1023                 .value = conn_set_prop->value,
1024                 .prop_id = conn_set_prop->prop_id,
1025                 .obj_id = conn_set_prop->connector_id,
1026                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1027         };
1028
1029         /* It does all the locking and checking we need */
1030         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1031 }
1032
1033 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1034 {
1035         /* For atomic drivers only state objects are synchronously updated and
1036          * protected by modeset locks, so check those first. */
1037         if (connector->state)
1038                 return connector->state->best_encoder;
1039         return connector->encoder;
1040 }
1041
1042 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1043                                          const struct drm_file *file_priv)
1044 {
1045         /*
1046          * If user-space hasn't configured the driver to expose the stereo 3D
1047          * modes, don't expose them.
1048          */
1049         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1050                 return false;
1051
1052         return true;
1053 }
1054
1055 int drm_mode_getconnector(struct drm_device *dev, void *data,
1056                           struct drm_file *file_priv)
1057 {
1058         struct drm_mode_get_connector *out_resp = data;
1059         struct drm_connector *connector;
1060         struct drm_encoder *encoder;
1061         struct drm_display_mode *mode;
1062         int mode_count = 0;
1063         int encoders_count = 0;
1064         int ret = 0;
1065         int copied = 0;
1066         int i;
1067         struct drm_mode_modeinfo u_mode;
1068         struct drm_mode_modeinfo __user *mode_ptr;
1069         uint32_t __user *encoder_ptr;
1070
1071         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1072                 return -EINVAL;
1073
1074         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1075
1076         mutex_lock(&dev->mode_config.mutex);
1077
1078         connector = drm_connector_lookup(dev, out_resp->connector_id);
1079         if (!connector) {
1080                 ret = -ENOENT;
1081                 goto out_unlock;
1082         }
1083
1084         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1085                 if (connector->encoder_ids[i] != 0)
1086                         encoders_count++;
1087
1088         if (out_resp->count_modes == 0) {
1089                 connector->funcs->fill_modes(connector,
1090                                              dev->mode_config.max_width,
1091                                              dev->mode_config.max_height);
1092         }
1093
1094         /* delayed so we get modes regardless of pre-fill_modes state */
1095         list_for_each_entry(mode, &connector->modes, head)
1096                 if (drm_mode_expose_to_userspace(mode, file_priv))
1097                         mode_count++;
1098
1099         out_resp->connector_id = connector->base.id;
1100         out_resp->connector_type = connector->connector_type;
1101         out_resp->connector_type_id = connector->connector_type_id;
1102         out_resp->mm_width = connector->display_info.width_mm;
1103         out_resp->mm_height = connector->display_info.height_mm;
1104         out_resp->subpixel = connector->display_info.subpixel_order;
1105         out_resp->connection = connector->status;
1106
1107         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1108         encoder = drm_connector_get_encoder(connector);
1109         if (encoder)
1110                 out_resp->encoder_id = encoder->base.id;
1111         else
1112                 out_resp->encoder_id = 0;
1113
1114         /*
1115          * This ioctl is called twice, once to determine how much space is
1116          * needed, and the 2nd time to fill it.
1117          */
1118         if ((out_resp->count_modes >= mode_count) && mode_count) {
1119                 copied = 0;
1120                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1121                 list_for_each_entry(mode, &connector->modes, head) {
1122                         if (!drm_mode_expose_to_userspace(mode, file_priv))
1123                                 continue;
1124
1125                         drm_mode_convert_to_umode(&u_mode, mode);
1126                         if (copy_to_user(mode_ptr + copied,
1127                                          &u_mode, sizeof(u_mode))) {
1128                                 ret = -EFAULT;
1129                                 goto out;
1130                         }
1131                         copied++;
1132                 }
1133         }
1134         out_resp->count_modes = mode_count;
1135
1136         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1137                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1138                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1139                         &out_resp->count_props);
1140         if (ret)
1141                 goto out;
1142
1143         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1144                 copied = 0;
1145                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1146                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1147                         if (connector->encoder_ids[i] != 0) {
1148                                 if (put_user(connector->encoder_ids[i],
1149                                              encoder_ptr + copied)) {
1150                                         ret = -EFAULT;
1151                                         goto out;
1152                                 }
1153                                 copied++;
1154                         }
1155                 }
1156         }
1157         out_resp->count_encoders = encoders_count;
1158
1159 out:
1160         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1161
1162         drm_connector_unreference(connector);
1163 out_unlock:
1164         mutex_unlock(&dev->mode_config.mutex);
1165
1166         return ret;
1167 }
1168
1169
1170 /**
1171  * DOC: Tile group
1172  *
1173  * Tile groups are used to represent tiled monitors with a unique integer
1174  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1175  * we store this in a tile group, so we have a common identifier for all tiles
1176  * in a monitor group. The property is called "TILE". Drivers can manage tile
1177  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1178  * drm_mode_get_tile_group(). But this is only needed for internal panels where
1179  * the tile group information is exposed through a non-standard way.
1180  */
1181
1182 static void drm_tile_group_free(struct kref *kref)
1183 {
1184         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1185         struct drm_device *dev = tg->dev;
1186         mutex_lock(&dev->mode_config.idr_mutex);
1187         idr_remove(&dev->mode_config.tile_idr, tg->id);
1188         mutex_unlock(&dev->mode_config.idr_mutex);
1189         kfree(tg);
1190 }
1191
1192 /**
1193  * drm_mode_put_tile_group - drop a reference to a tile group.
1194  * @dev: DRM device
1195  * @tg: tile group to drop reference to.
1196  *
1197  * drop reference to tile group and free if 0.
1198  */
1199 void drm_mode_put_tile_group(struct drm_device *dev,
1200                              struct drm_tile_group *tg)
1201 {
1202         kref_put(&tg->refcount, drm_tile_group_free);
1203 }
1204 EXPORT_SYMBOL(drm_mode_put_tile_group);
1205
1206 /**
1207  * drm_mode_get_tile_group - get a reference to an existing tile group
1208  * @dev: DRM device
1209  * @topology: 8-bytes unique per monitor.
1210  *
1211  * Use the unique bytes to get a reference to an existing tile group.
1212  *
1213  * RETURNS:
1214  * tile group or NULL if not found.
1215  */
1216 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1217                                                char topology[8])
1218 {
1219         struct drm_tile_group *tg;
1220         int id;
1221         mutex_lock(&dev->mode_config.idr_mutex);
1222         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1223                 if (!memcmp(tg->group_data, topology, 8)) {
1224                         if (!kref_get_unless_zero(&tg->refcount))
1225                                 tg = NULL;
1226                         mutex_unlock(&dev->mode_config.idr_mutex);
1227                         return tg;
1228                 }
1229         }
1230         mutex_unlock(&dev->mode_config.idr_mutex);
1231         return NULL;
1232 }
1233 EXPORT_SYMBOL(drm_mode_get_tile_group);
1234
1235 /**
1236  * drm_mode_create_tile_group - create a tile group from a displayid description
1237  * @dev: DRM device
1238  * @topology: 8-bytes unique per monitor.
1239  *
1240  * Create a tile group for the unique monitor, and get a unique
1241  * identifier for the tile group.
1242  *
1243  * RETURNS:
1244  * new tile group or error.
1245  */
1246 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1247                                                   char topology[8])
1248 {
1249         struct drm_tile_group *tg;
1250         int ret;
1251
1252         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1253         if (!tg)
1254                 return ERR_PTR(-ENOMEM);
1255
1256         kref_init(&tg->refcount);
1257         memcpy(tg->group_data, topology, 8);
1258         tg->dev = dev;
1259
1260         mutex_lock(&dev->mode_config.idr_mutex);
1261         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1262         if (ret >= 0) {
1263                 tg->id = ret;
1264         } else {
1265                 kfree(tg);
1266                 tg = ERR_PTR(ret);
1267         }
1268
1269         mutex_unlock(&dev->mode_config.idr_mutex);
1270         return tg;
1271 }
1272 EXPORT_SYMBOL(drm_mode_create_tile_group);