blob: 0bc35545e86a6beb3d9410754698205518c22aa0 [file] [log] [blame]
Daniel Vetter52217192016-08-12 22:48:50 +02001/*
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>
Laurent Pinchart93382032016-11-28 20:51:09 +020026#include <drm/drm_encoder.h>
Hans de Goede8d70f392017-11-25 20:35:49 +010027#include <drm/drm_utils.h>
Daniel Vetter52217192016-08-12 22:48:50 +020028
29#include "drm_crtc_internal.h"
30#include "drm_internal.h"
31
Daniel Vetterae2a6da2016-08-12 22:48:53 +020032/**
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.
Thierry Redingad093602017-02-28 15:46:39 +010039 * Hence they are reference-counted using drm_connector_get() and
40 * drm_connector_put().
Daniel Vetterae2a6da2016-08-12 22:48:53 +020041 *
Daniel Vetterd5745282017-01-25 07:26:45 +010042 * 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
Daniel Vetteraec97462017-01-25 07:26:48 +010044 * 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().
Daniel Vetterae2a6da2016-08-12 22:48:53 +020048 *
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
Daniel Vetterd5745282017-01-25 07:26:45 +010052 * driver must also set the &drm_connector.encoder field to point to the
Daniel Vetterae2a6da2016-08-12 22:48:53 +020053 * 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
Daniel Vetter52217192016-08-12 22:48:50 +020062struct 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 */
71static 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
92void 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
100void 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 *
Daniel Vetterae2a6da2016-08-12 22:48:53 +0200112 * The kernel supports per-connector configuration of its consoles through
Daniel Vetter52217192016-08-12 22:48:50 +0200113 * 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 */
118static 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) {
Jani Nikula6140cf22017-02-20 10:51:48 +0200132 DRM_INFO("forcing %s connector %s\n", connector->name,
133 drm_get_connector_force_name(mode->force));
Daniel Vetter52217192016-08-12 22:48:50 +0200134 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
146static 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 */
169int 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
Thierry Reding2135ea72017-02-28 15:46:37 +0100179 ret = __drm_mode_object_add(dev, &connector->base,
180 DRM_MODE_OBJECT_CONNECTOR,
181 false, drm_connector_free);
Daniel Vetter52217192016-08-12 22:48:50 +0200182 if (ret)
Daniel Vetter613051d2016-12-14 00:08:06 +0100183 return ret;
Daniel Vetter52217192016-08-12 22:48:50 +0200184
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);
Daniel Vettere73ab002016-12-18 14:35:45 +0100213 mutex_init(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200214 connector->edid_blob_ptr = NULL;
215 connector->status = connector_status_unknown;
Hans de Goede8d70f392017-11-25 20:35:49 +0100216 connector->display_info.panel_orientation =
217 DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
Daniel Vetter52217192016-08-12 22:48:50 +0200218
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. */
Daniel Vetter613051d2016-12-14 00:08:06 +0100223 spin_lock_irq(&config->connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200224 list_add_tail(&connector->head, &config->connector_list);
225 config->num_connector++;
Daniel Vetter613051d2016-12-14 00:08:06 +0100226 spin_unlock_irq(&config->connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200227
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
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200236 drm_object_attach_property(&connector->base,
237 config->link_status_property,
238 0);
239
Dave Airlie66660d42017-10-16 05:08:09 +0100240 drm_object_attach_property(&connector->base,
241 config->non_desktop_property,
242 0);
243
Daniel Vetter52217192016-08-12 22:48:50 +0200244 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;
249out_put_type_id:
250 if (ret)
Christophe JAILLET587680c2016-10-02 08:01:22 +0200251 ida_simple_remove(connector_ida, connector->connector_type_id);
Daniel Vetter52217192016-08-12 22:48:50 +0200252out_put_id:
253 if (ret)
Christophe JAILLET587680c2016-10-02 08:01:22 +0200254 ida_simple_remove(&config->connector_ida, connector->index);
Daniel Vetter52217192016-08-12 22:48:50 +0200255out_put:
256 if (ret)
257 drm_mode_object_unregister(dev, &connector->base);
258
Daniel Vetter52217192016-08-12 22:48:50 +0200259 return ret;
260}
261EXPORT_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 */
275int 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}
302EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
303
304static 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 */
317void 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
Christophe JAILLET9a47dba2016-10-07 09:27:41 +0200339 ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
340 connector->connector_type_id);
Daniel Vetter52217192016-08-12 22:48:50 +0200341
Christophe JAILLET9a47dba2016-10-07 09:27:41 +0200342 ida_simple_remove(&dev->mode_config.connector_ida,
343 connector->index);
Daniel Vetter52217192016-08-12 22:48:50 +0200344
345 kfree(connector->display_info.bus_formats);
346 drm_mode_object_unregister(dev, &connector->base);
347 kfree(connector->name);
348 connector->name = NULL;
Daniel Vetter613051d2016-12-14 00:08:06 +0100349 spin_lock_irq(&dev->mode_config.connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200350 list_del(&connector->head);
351 dev->mode_config.num_connector--;
Daniel Vetter613051d2016-12-14 00:08:06 +0100352 spin_unlock_irq(&dev->mode_config.connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200353
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
Daniel Vettere73ab002016-12-18 14:35:45 +0100359 mutex_destroy(&connector->mutex);
360
Daniel Vetter52217192016-08-12 22:48:50 +0200361 memset(connector, 0, sizeof(*connector));
362}
363EXPORT_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 */
374int drm_connector_register(struct drm_connector *connector)
375{
Daniel Vettere73ab002016-12-18 14:35:45 +0100376 int ret = 0;
Daniel Vetter52217192016-08-12 22:48:50 +0200377
Daniel Vettere6e7b482017-01-12 17:15:56 +0100378 if (!connector->dev->registered)
379 return 0;
380
Daniel Vettere73ab002016-12-18 14:35:45 +0100381 mutex_lock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200382 if (connector->registered)
Daniel Vettere73ab002016-12-18 14:35:45 +0100383 goto unlock;
Daniel Vetter52217192016-08-12 22:48:50 +0200384
385 ret = drm_sysfs_connector_add(connector);
386 if (ret)
Daniel Vettere73ab002016-12-18 14:35:45 +0100387 goto unlock;
Daniel Vetter52217192016-08-12 22:48:50 +0200388
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;
Daniel Vettere73ab002016-12-18 14:35:45 +0100403 goto unlock;
Daniel Vetter52217192016-08-12 22:48:50 +0200404
405err_debugfs:
406 drm_debugfs_connector_remove(connector);
407err_sysfs:
408 drm_sysfs_connector_remove(connector);
Daniel Vettere73ab002016-12-18 14:35:45 +0100409unlock:
410 mutex_unlock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200411 return ret;
412}
413EXPORT_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 */
421void drm_connector_unregister(struct drm_connector *connector)
422{
Daniel Vettere73ab002016-12-18 14:35:45 +0100423 mutex_lock(&connector->mutex);
424 if (!connector->registered) {
425 mutex_unlock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200426 return;
Daniel Vettere73ab002016-12-18 14:35:45 +0100427 }
Daniel Vetter52217192016-08-12 22:48:50 +0200428
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;
Daniel Vettere73ab002016-12-18 14:35:45 +0100436 mutex_unlock(&connector->mutex);
Daniel Vetter52217192016-08-12 22:48:50 +0200437}
438EXPORT_SYMBOL(drm_connector_unregister);
439
440void drm_connector_unregister_all(struct drm_device *dev)
441{
442 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100443 struct drm_connector_list_iter conn_iter;
Daniel Vetter52217192016-08-12 22:48:50 +0200444
Thierry Redingb982dab2017-02-28 15:46:43 +0100445 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100446 drm_for_each_connector_iter(connector, &conn_iter)
Daniel Vetter52217192016-08-12 22:48:50 +0200447 drm_connector_unregister(connector);
Thierry Redingb982dab2017-02-28 15:46:43 +0100448 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter52217192016-08-12 22:48:50 +0200449}
450
451int drm_connector_register_all(struct drm_device *dev)
452{
453 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100454 struct drm_connector_list_iter conn_iter;
455 int ret = 0;
Daniel Vetter52217192016-08-12 22:48:50 +0200456
Thierry Redingb982dab2017-02-28 15:46:43 +0100457 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100458 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vetter52217192016-08-12 22:48:50 +0200459 ret = drm_connector_register(connector);
460 if (ret)
Daniel Vetter613051d2016-12-14 00:08:06 +0100461 break;
Daniel Vetter52217192016-08-12 22:48:50 +0200462 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100463 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter52217192016-08-12 22:48:50 +0200464
Daniel Vetter613051d2016-12-14 00:08:06 +0100465 if (ret)
466 drm_connector_unregister_all(dev);
Daniel Vetter52217192016-08-12 22:48:50 +0200467 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 */
477const 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}
486EXPORT_SYMBOL(drm_get_connector_status_name);
487
Jani Nikula6140cf22017-02-20 10:51:48 +0200488/**
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 */
494const 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
Daniel Vetter613051d2016-12-14 00:08:06 +0100510#ifdef CONFIG_LOCKDEP
511static struct lockdep_map connector_list_iter_dep_map = {
512 .name = "drm_connector_list_iter"
513};
514#endif
515
516/**
Thierry Redingb982dab2017-02-28 15:46:43 +0100517 * drm_connector_list_iter_begin - initialize a connector_list iterator
Daniel Vetter613051d2016-12-14 00:08:06 +0100518 * @dev: DRM device
519 * @iter: connector_list iterator
520 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100521 * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
Thierry Redingb982dab2017-02-28 15:46:43 +0100522 * must always be cleaned up again by calling drm_connector_list_iter_end().
Daniel Vetter613051d2016-12-14 00:08:06 +0100523 * Iteration itself happens using drm_connector_list_iter_next() or
524 * drm_for_each_connector_iter().
525 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100526void drm_connector_list_iter_begin(struct drm_device *dev,
527 struct drm_connector_list_iter *iter)
Daniel Vetter613051d2016-12-14 00:08:06 +0100528{
529 iter->dev = dev;
530 iter->conn = NULL;
531 lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
532}
Thierry Redingb982dab2017-02-28 15:46:43 +0100533EXPORT_SYMBOL(drm_connector_list_iter_begin);
Daniel Vetter613051d2016-12-14 00:08:06 +0100534
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 */
542struct drm_connector *
543drm_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)
Thierry Redingad093602017-02-28 15:46:39 +0100567 drm_connector_put(old_conn);
Daniel Vetter613051d2016-12-14 00:08:06 +0100568
569 return iter->conn;
570}
571EXPORT_SYMBOL(drm_connector_list_iter_next);
572
573/**
Thierry Redingb982dab2017-02-28 15:46:43 +0100574 * drm_connector_list_iter_end - tear down a connector_list iterator
Daniel Vetter613051d2016-12-14 00:08:06 +0100575 * @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 */
Thierry Redingb982dab2017-02-28 15:46:43 +0100582void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
Daniel Vetter613051d2016-12-14 00:08:06 +0100583{
584 iter->dev = NULL;
585 if (iter->conn)
Thierry Redingad093602017-02-28 15:46:39 +0100586 drm_connector_put(iter->conn);
Daniel Vetter613051d2016-12-14 00:08:06 +0100587 lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
588}
Thierry Redingb982dab2017-02-28 15:46:43 +0100589EXPORT_SYMBOL(drm_connector_list_iter_end);
Daniel Vetter613051d2016-12-14 00:08:06 +0100590
Daniel Vetter52217192016-08-12 22:48:50 +0200591static 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 */
607const char *drm_get_subpixel_order_name(enum subpixel_order order)
608{
609 return drm_subpixel_enum_list[order].name;
610}
611EXPORT_SYMBOL(drm_get_subpixel_order_name);
612
613static 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};
619DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
620
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200621static 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};
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200625
Daniel Vetterb3c6c8b2016-08-12 22:48:55 +0200626/**
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 */
636int 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}
658EXPORT_SYMBOL(drm_display_info_set_bus_formats);
659
Daniel Vetter52217192016-08-12 22:48:50 +0200660/* Optional connector properties. */
661static 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
668static 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
Hans de Goede8d70f392017-11-25 20:35:49 +0100674static 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
Daniel Vetter52217192016-08-12 22:48:50 +0200681static 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};
686DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
687
688static 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};
693DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
694 drm_dvi_i_subconnector_enum_list)
695
696static 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};
703DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
704
705static 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};
712DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
713 drm_tv_subconnector_enum_list)
714
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100715/**
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,
Daniel Vetterd5745282017-01-25 07:26:45 +0100732 * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
Daniel Vetter144a7992017-07-25 14:02:04 +0200733 * 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.
Daniel Vetterd0d1aee2017-09-21 00:59:57 +0200736 *
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.
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100759 * 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.
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200775 * 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().
Dave Airlie66660d42017-10-16 05:08:09 +0100780 * 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.
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100784 *
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.
Hans de Goede8d70f392017-11-25 20:35:49 +0100789 *
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.
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100801 */
802
Daniel Vetter52217192016-08-12 22:48:50 +0200803int 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
Manasi Navare40ee6fb2016-12-16 12:29:06 +0200837 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
Dave Airlie66660d42017-10-16 05:08:09 +0100844 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
Daniel Vetter52217192016-08-12 22:48:50 +0200849 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 */
858int 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}
881EXPORT_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 */
894int 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;
990nomem:
991 return -ENOMEM;
992}
993EXPORT_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.
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001001 *
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.
Daniel Vetter52217192016-08-12 22:48:50 +02001005 */
1006int 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}
1022EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1023
1024/**
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001025 * 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 */
1038int 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}
1082EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1083
1084/**
Daniel Vetter52217192016-08-12 22:48:50 +02001085 * 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 */
1094int 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}
1109EXPORT_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 */
1117int 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}
1133EXPORT_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 */
1148int 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}
1162EXPORT_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 */
1175int 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}
1205EXPORT_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 */
1218int 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
Keith Packard170178f2017-12-13 00:44:26 -08001232 /* Set the display info, using edid if available, otherwise
1233 * reseting the values to defaults. This duplicates the work
1234 * done in drm_add_edid_modes, but that function is not
1235 * consistently called before this one in all drivers and the
1236 * computation is cheap enough that it seems better to
1237 * duplicate it rather than attempt to ensure some arbitrary
1238 * ordering of calls.
1239 */
1240 if (edid)
1241 drm_add_display_info(connector, edid);
1242 else
1243 drm_reset_display_info(connector);
1244
Dave Airlie66660d42017-10-16 05:08:09 +01001245 drm_object_property_set_value(&connector->base,
1246 dev->mode_config.non_desktop_property,
1247 connector->display_info.non_desktop);
1248
Daniel Vetter52217192016-08-12 22:48:50 +02001249 ret = drm_property_replace_global_blob(dev,
1250 &connector->edid_blob_ptr,
1251 size,
1252 edid,
1253 &connector->base,
1254 dev->mode_config.edid_property);
1255 return ret;
1256}
1257EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1258
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001259/**
1260 * drm_mode_connector_set_link_status_property - Set link status property of a connector
1261 * @connector: drm connector
1262 * @link_status: new value of link status property (0: Good, 1: Bad)
1263 *
1264 * In usual working scenario, this link status property will always be set to
1265 * "GOOD". If something fails during or after a mode set, the kernel driver
1266 * may set this link status property to "BAD". The caller then needs to send a
1267 * hotplug uevent for userspace to re-check the valid modes through
1268 * GET_CONNECTOR_IOCTL and retry modeset.
1269 *
1270 * Note: Drivers cannot rely on userspace to support this property and
1271 * issue a modeset. As such, they may choose to handle issues (like
1272 * re-training a link) without userspace's intervention.
1273 *
1274 * The reason for adding this property is to handle link training failures, but
1275 * it is not limited to DP or link training. For example, if we implement
1276 * asynchronous setcrtc, this property can be used to report any failures in that.
1277 */
1278void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
1279 uint64_t link_status)
1280{
1281 struct drm_device *dev = connector->dev;
1282
1283 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1284 connector->state->link_status = link_status;
1285 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1286}
1287EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
1288
Hans de Goede8d70f392017-11-25 20:35:49 +01001289/**
1290 * drm_connector_init_panel_orientation_property -
1291 * initialize the connecters panel_orientation property
1292 * @connector: connector for which to init the panel-orientation property.
1293 * @width: width in pixels of the panel, used for panel quirk detection
1294 * @height: height in pixels of the panel, used for panel quirk detection
1295 *
1296 * This function should only be called for built-in panels, after setting
1297 * connector->display_info.panel_orientation first (if known).
1298 *
1299 * This function will check for platform specific (e.g. DMI based) quirks
1300 * overriding display_info.panel_orientation first, then if panel_orientation
1301 * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
1302 * "panel orientation" property to the connector.
1303 *
1304 * Returns:
1305 * Zero on success, negative errno on failure.
1306 */
1307int drm_connector_init_panel_orientation_property(
1308 struct drm_connector *connector, int width, int height)
1309{
1310 struct drm_device *dev = connector->dev;
1311 struct drm_display_info *info = &connector->display_info;
1312 struct drm_property *prop;
1313 int orientation_quirk;
1314
1315 orientation_quirk = drm_get_panel_orientation_quirk(width, height);
1316 if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1317 info->panel_orientation = orientation_quirk;
1318
1319 if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1320 return 0;
1321
1322 prop = dev->mode_config.panel_orientation_property;
1323 if (!prop) {
1324 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1325 "panel orientation",
1326 drm_panel_orientation_enum_list,
1327 ARRAY_SIZE(drm_panel_orientation_enum_list));
1328 if (!prop)
1329 return -ENOMEM;
1330
1331 dev->mode_config.panel_orientation_property = prop;
1332 }
1333
1334 drm_object_attach_property(&connector->base, prop,
1335 info->panel_orientation);
1336 return 0;
1337}
1338EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
1339
Daniel Vetter52217192016-08-12 22:48:50 +02001340int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1341 struct drm_property *property,
1342 uint64_t value)
1343{
1344 int ret = -EINVAL;
1345 struct drm_connector *connector = obj_to_connector(obj);
1346
1347 /* Do DPMS ourselves */
1348 if (property == connector->dev->mode_config.dpms_property) {
1349 ret = (*connector->funcs->dpms)(connector, (int)value);
1350 } else if (connector->funcs->set_property)
1351 ret = connector->funcs->set_property(connector, property, value);
1352
Daniel Vetter144a7992017-07-25 14:02:04 +02001353 if (!ret)
Daniel Vetter52217192016-08-12 22:48:50 +02001354 drm_object_property_set_value(&connector->base, property, value);
1355 return ret;
1356}
1357
1358int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1359 void *data, struct drm_file *file_priv)
1360{
1361 struct drm_mode_connector_set_property *conn_set_prop = data;
1362 struct drm_mode_obj_set_property obj_set_prop = {
1363 .value = conn_set_prop->value,
1364 .prop_id = conn_set_prop->prop_id,
1365 .obj_id = conn_set_prop->connector_id,
1366 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1367 };
1368
1369 /* It does all the locking and checking we need */
1370 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1371}
1372
1373static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1374{
1375 /* For atomic drivers only state objects are synchronously updated and
1376 * protected by modeset locks, so check those first. */
1377 if (connector->state)
1378 return connector->state->best_encoder;
1379 return connector->encoder;
1380}
1381
1382static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1383 const struct drm_file *file_priv)
1384{
1385 /*
1386 * If user-space hasn't configured the driver to expose the stereo 3D
1387 * modes, don't expose them.
1388 */
1389 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1390 return false;
1391
1392 return true;
1393}
1394
1395int drm_mode_getconnector(struct drm_device *dev, void *data,
1396 struct drm_file *file_priv)
1397{
1398 struct drm_mode_get_connector *out_resp = data;
1399 struct drm_connector *connector;
1400 struct drm_encoder *encoder;
1401 struct drm_display_mode *mode;
1402 int mode_count = 0;
1403 int encoders_count = 0;
1404 int ret = 0;
1405 int copied = 0;
1406 int i;
1407 struct drm_mode_modeinfo u_mode;
1408 struct drm_mode_modeinfo __user *mode_ptr;
1409 uint32_t __user *encoder_ptr;
1410
1411 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1412 return -EINVAL;
1413
1414 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1415
Keith Packard418da172017-03-14 23:25:07 -07001416 connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
Daniel Vetter91eefc02016-12-14 00:08:10 +01001417 if (!connector)
1418 return -ENOENT;
Daniel Vetter52217192016-08-12 22:48:50 +02001419
Daniel Vetter91eefc02016-12-14 00:08:10 +01001420 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1421 if (connector->encoder_ids[i] != 0)
1422 encoders_count++;
1423
1424 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1425 copied = 0;
1426 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1427 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1428 if (connector->encoder_ids[i] != 0) {
1429 if (put_user(connector->encoder_ids[i],
1430 encoder_ptr + copied)) {
1431 ret = -EFAULT;
Daniel Vettere94ac352017-06-20 22:28:37 +02001432 goto out;
Daniel Vetter91eefc02016-12-14 00:08:10 +01001433 }
1434 copied++;
1435 }
1436 }
1437 }
1438 out_resp->count_encoders = encoders_count;
1439
1440 out_resp->connector_id = connector->base.id;
1441 out_resp->connector_type = connector->connector_type;
1442 out_resp->connector_type_id = connector->connector_type_id;
1443
1444 mutex_lock(&dev->mode_config.mutex);
1445 if (out_resp->count_modes == 0) {
1446 connector->funcs->fill_modes(connector,
1447 dev->mode_config.max_width,
1448 dev->mode_config.max_height);
1449 }
1450
1451 out_resp->mm_width = connector->display_info.width_mm;
1452 out_resp->mm_height = connector->display_info.height_mm;
1453 out_resp->subpixel = connector->display_info.subpixel_order;
1454 out_resp->connection = connector->status;
1455
1456 /* delayed so we get modes regardless of pre-fill_modes state */
1457 list_for_each_entry(mode, &connector->modes, head)
1458 if (drm_mode_expose_to_userspace(mode, file_priv))
1459 mode_count++;
1460
Daniel Vetter52217192016-08-12 22:48:50 +02001461 /*
1462 * This ioctl is called twice, once to determine how much space is
1463 * needed, and the 2nd time to fill it.
1464 */
1465 if ((out_resp->count_modes >= mode_count) && mode_count) {
1466 copied = 0;
1467 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1468 list_for_each_entry(mode, &connector->modes, head) {
1469 if (!drm_mode_expose_to_userspace(mode, file_priv))
1470 continue;
1471
1472 drm_mode_convert_to_umode(&u_mode, mode);
1473 if (copy_to_user(mode_ptr + copied,
1474 &u_mode, sizeof(u_mode))) {
1475 ret = -EFAULT;
Daniel Vettere94ac352017-06-20 22:28:37 +02001476 mutex_unlock(&dev->mode_config.mutex);
1477
Daniel Vetter52217192016-08-12 22:48:50 +02001478 goto out;
1479 }
1480 copied++;
1481 }
1482 }
1483 out_resp->count_modes = mode_count;
Daniel Vetter52217192016-08-12 22:48:50 +02001484 mutex_unlock(&dev->mode_config.mutex);
Daniel Vettere94ac352017-06-20 22:28:37 +02001485
1486 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1487 encoder = drm_connector_get_encoder(connector);
1488 if (encoder)
1489 out_resp->encoder_id = encoder->base.id;
1490 else
1491 out_resp->encoder_id = 0;
1492
1493 /* Only grab properties after probing, to make sure EDID and other
1494 * properties reflect the latest status. */
1495 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1496 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1497 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1498 &out_resp->count_props);
1499 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1500
1501out:
Thierry Redingad093602017-02-28 15:46:39 +01001502 drm_connector_put(connector);
Daniel Vetter52217192016-08-12 22:48:50 +02001503
1504 return ret;
1505}
1506
Daniel Vetter9498c192016-11-14 12:58:24 +01001507
1508/**
1509 * DOC: Tile group
1510 *
1511 * Tile groups are used to represent tiled monitors with a unique integer
1512 * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1513 * we store this in a tile group, so we have a common identifier for all tiles
1514 * in a monitor group. The property is called "TILE". Drivers can manage tile
1515 * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1516 * drm_mode_get_tile_group(). But this is only needed for internal panels where
1517 * the tile group information is exposed through a non-standard way.
1518 */
1519
1520static void drm_tile_group_free(struct kref *kref)
1521{
1522 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1523 struct drm_device *dev = tg->dev;
1524 mutex_lock(&dev->mode_config.idr_mutex);
1525 idr_remove(&dev->mode_config.tile_idr, tg->id);
1526 mutex_unlock(&dev->mode_config.idr_mutex);
1527 kfree(tg);
1528}
1529
1530/**
1531 * drm_mode_put_tile_group - drop a reference to a tile group.
1532 * @dev: DRM device
1533 * @tg: tile group to drop reference to.
1534 *
1535 * drop reference to tile group and free if 0.
1536 */
1537void drm_mode_put_tile_group(struct drm_device *dev,
1538 struct drm_tile_group *tg)
1539{
1540 kref_put(&tg->refcount, drm_tile_group_free);
1541}
1542EXPORT_SYMBOL(drm_mode_put_tile_group);
1543
1544/**
1545 * drm_mode_get_tile_group - get a reference to an existing tile group
1546 * @dev: DRM device
1547 * @topology: 8-bytes unique per monitor.
1548 *
1549 * Use the unique bytes to get a reference to an existing tile group.
1550 *
1551 * RETURNS:
1552 * tile group or NULL if not found.
1553 */
1554struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1555 char topology[8])
1556{
1557 struct drm_tile_group *tg;
1558 int id;
1559 mutex_lock(&dev->mode_config.idr_mutex);
1560 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1561 if (!memcmp(tg->group_data, topology, 8)) {
1562 if (!kref_get_unless_zero(&tg->refcount))
1563 tg = NULL;
1564 mutex_unlock(&dev->mode_config.idr_mutex);
1565 return tg;
1566 }
1567 }
1568 mutex_unlock(&dev->mode_config.idr_mutex);
1569 return NULL;
1570}
1571EXPORT_SYMBOL(drm_mode_get_tile_group);
1572
1573/**
1574 * drm_mode_create_tile_group - create a tile group from a displayid description
1575 * @dev: DRM device
1576 * @topology: 8-bytes unique per monitor.
1577 *
1578 * Create a tile group for the unique monitor, and get a unique
1579 * identifier for the tile group.
1580 *
1581 * RETURNS:
1582 * new tile group or error.
1583 */
1584struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1585 char topology[8])
1586{
1587 struct drm_tile_group *tg;
1588 int ret;
1589
1590 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1591 if (!tg)
1592 return ERR_PTR(-ENOMEM);
1593
1594 kref_init(&tg->refcount);
1595 memcpy(tg->group_data, topology, 8);
1596 tg->dev = dev;
1597
1598 mutex_lock(&dev->mode_config.idr_mutex);
1599 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1600 if (ret >= 0) {
1601 tg->id = ret;
1602 } else {
1603 kfree(tg);
1604 tg = ERR_PTR(ret);
1605 }
1606
1607 mutex_unlock(&dev->mode_config.idr_mutex);
1608 return tg;
1609}
1610EXPORT_SYMBOL(drm_mode_create_tile_group);