Home | History | Annotate | Line # | Download | only in drm
drm_probe_helper.c revision 1.2.2.2
      1 /*
      2  * Copyright (c) 2006-2008 Intel Corporation
      3  * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
      4  *
      5  * DRM core CRTC related functions
      6  *
      7  * Permission to use, copy, modify, distribute, and sell this software and its
      8  * documentation for any purpose is hereby granted without fee, provided that
      9  * the above copyright notice appear in all copies and that both that copyright
     10  * notice and this permission notice appear in supporting documentation, and
     11  * that the name of the copyright holders not be used in advertising or
     12  * publicity pertaining to distribution of the software without specific,
     13  * written prior permission.  The copyright holders make no representations
     14  * about the suitability of this software for any purpose.  It is provided "as
     15  * is" without express or implied warranty.
     16  *
     17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     18  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     19  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     23  * OF THIS SOFTWARE.
     24  *
     25  * Authors:
     26  *      Keith Packard
     27  *	Eric Anholt <eric (at) anholt.net>
     28  *      Dave Airlie <airlied (at) linux.ie>
     29  *      Jesse Barnes <jesse.barnes (at) intel.com>
     30  */
     31 
     32 #include <asm/param.h>
     33 #include <linux/export.h>
     34 #include <linux/moduleparam.h>
     35 
     36 #include <drm/drmP.h>
     37 #include <drm/drm_crtc.h>
     38 #include <drm/drm_fourcc.h>
     39 #include <drm/drm_crtc_helper.h>
     40 #include <drm/drm_fb_helper.h>
     41 #include <drm/drm_edid.h>
     42 
     43 /**
     44  * DOC: output probing helper overview
     45  *
     46  * This library provides some helper code for output probing. It provides an
     47  * implementation of the core connector->fill_modes interface with
     48  * drm_helper_probe_single_connector_modes.
     49  *
     50  * It also provides support for polling connectors with a work item and for
     51  * generic hotplug interrupt handling where the driver doesn't or cannot keep
     52  * track of a per-connector hpd interrupt.
     53  *
     54  * This helper library can be used independently of the modeset helper library.
     55  * Drivers can also overwrite different parts e.g. use their own hotplug
     56  * handling code to avoid probing unrelated outputs.
     57  */
     58 
     59 static bool drm_kms_helper_poll = true;
     60 module_param_named(poll, drm_kms_helper_poll, bool, 0600);
     61 
     62 static void drm_mode_validate_flag(struct drm_connector *connector,
     63 				   int flags)
     64 {
     65 	struct drm_display_mode *mode;
     66 
     67 	if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE |
     68 		      DRM_MODE_FLAG_3D_MASK))
     69 		return;
     70 
     71 	list_for_each_entry(mode, &connector->modes, head) {
     72 		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
     73 				!(flags & DRM_MODE_FLAG_INTERLACE))
     74 			mode->status = MODE_NO_INTERLACE;
     75 		if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
     76 				!(flags & DRM_MODE_FLAG_DBLSCAN))
     77 			mode->status = MODE_NO_DBLESCAN;
     78 		if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
     79 				!(flags & DRM_MODE_FLAG_3D_MASK))
     80 			mode->status = MODE_NO_STEREO;
     81 	}
     82 
     83 	return;
     84 }
     85 
     86 /**
     87  * drm_helper_probe_single_connector_modes - get complete set of display modes
     88  * @connector: connector to probe
     89  * @maxX: max width for modes
     90  * @maxY: max height for modes
     91  *
     92  * Based on the helper callbacks implemented by @connector try to detect all
     93  * valid modes.  Modes will first be added to the connector's probed_modes list,
     94  * then culled (based on validity and the @maxX, @maxY parameters) and put into
     95  * the normal modes list.
     96  *
     97  * Intended to be use as a generic implementation of the ->fill_modes()
     98  * @connector vfunc for drivers that use the crtc helpers for output mode
     99  * filtering and detection.
    100  *
    101  * Returns:
    102  * The number of modes found on @connector.
    103  */
    104 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
    105 					    uint32_t maxX, uint32_t maxY)
    106 {
    107 	struct drm_device *dev = connector->dev;
    108 	struct drm_display_mode *mode;
    109 	struct drm_connector_helper_funcs *connector_funcs =
    110 		connector->helper_private;
    111 	int count = 0;
    112 	int mode_flags = 0;
    113 	bool verbose_prune = true;
    114 
    115 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
    116 
    117 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
    118 			drm_get_connector_name(connector));
    119 	/* set all modes to the unverified state */
    120 	list_for_each_entry(mode, &connector->modes, head)
    121 		mode->status = MODE_UNVERIFIED;
    122 
    123 	if (connector->force) {
    124 		if (connector->force == DRM_FORCE_ON)
    125 			connector->status = connector_status_connected;
    126 		else
    127 			connector->status = connector_status_disconnected;
    128 		if (connector->funcs->force)
    129 			connector->funcs->force(connector);
    130 	} else {
    131 		connector->status = connector->funcs->detect(connector, true);
    132 	}
    133 
    134 	/* Re-enable polling in case the global poll config changed. */
    135 	if (drm_kms_helper_poll != dev->mode_config.poll_running)
    136 		drm_kms_helper_poll_enable(dev);
    137 
    138 	dev->mode_config.poll_running = drm_kms_helper_poll;
    139 
    140 	if (connector->status == connector_status_disconnected) {
    141 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
    142 			connector->base.id, drm_get_connector_name(connector));
    143 		drm_mode_connector_update_edid_property(connector, NULL);
    144 		verbose_prune = false;
    145 		goto prune;
    146 	}
    147 
    148 #ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
    149 	count = drm_load_edid_firmware(connector);
    150 	if (count == 0)
    151 #endif
    152 		count = (*connector_funcs->get_modes)(connector);
    153 
    154 	if (count == 0 && connector->status == connector_status_connected)
    155 		count = drm_add_modes_noedid(connector, 1024, 768);
    156 	if (count == 0)
    157 		goto prune;
    158 
    159 	drm_mode_connector_list_update(connector);
    160 
    161 	if (maxX && maxY)
    162 		drm_mode_validate_size(dev, &connector->modes, maxX, maxY);
    163 
    164 	if (connector->interlace_allowed)
    165 		mode_flags |= DRM_MODE_FLAG_INTERLACE;
    166 	if (connector->doublescan_allowed)
    167 		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
    168 	if (connector->stereo_allowed)
    169 		mode_flags |= DRM_MODE_FLAG_3D_MASK;
    170 	drm_mode_validate_flag(connector, mode_flags);
    171 
    172 	list_for_each_entry(mode, &connector->modes, head) {
    173 		if (mode->status == MODE_OK)
    174 			mode->status = connector_funcs->mode_valid(connector,
    175 								   mode);
    176 	}
    177 
    178 prune:
    179 	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
    180 
    181 	if (list_empty(&connector->modes))
    182 		return 0;
    183 
    184 	list_for_each_entry(mode, &connector->modes, head)
    185 		mode->vrefresh = drm_mode_vrefresh(mode);
    186 
    187 	drm_mode_sort(&connector->modes);
    188 
    189 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
    190 			drm_get_connector_name(connector));
    191 	list_for_each_entry(mode, &connector->modes, head) {
    192 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
    193 		drm_mode_debug_printmodeline(mode);
    194 	}
    195 
    196 	return count;
    197 }
    198 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
    199 
    200 /**
    201  * drm_kms_helper_hotplug_event - fire off KMS hotplug events
    202  * @dev: drm_device whose connector state changed
    203  *
    204  * This function fires off the uevent for userspace and also calls the
    205  * output_poll_changed function, which is most commonly used to inform the fbdev
    206  * emulation code and allow it to update the fbcon output configuration.
    207  *
    208  * Drivers should call this from their hotplug handling code when a change is
    209  * detected. Note that this function does not do any output detection of its
    210  * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
    211  * driver already.
    212  *
    213  * This function must be called from process context with no mode
    214  * setting locks held.
    215  */
    216 void drm_kms_helper_hotplug_event(struct drm_device *dev)
    217 {
    218 	/* send a uevent + call fbdev */
    219 	drm_sysfs_hotplug_event(dev);
    220 	if (dev->mode_config.funcs->output_poll_changed)
    221 		dev->mode_config.funcs->output_poll_changed(dev);
    222 }
    223 EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
    224 
    225 #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
    226 static void output_poll_execute(struct work_struct *work)
    227 {
    228 	struct delayed_work *delayed_work = to_delayed_work(work);
    229 	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
    230 	struct drm_connector *connector;
    231 	enum drm_connector_status old_status;
    232 	bool repoll = false, changed = false;
    233 
    234 	if (!drm_kms_helper_poll)
    235 		return;
    236 
    237 	mutex_lock(&dev->mode_config.mutex);
    238 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
    239 
    240 		/* Ignore forced connectors. */
    241 		if (connector->force)
    242 			continue;
    243 
    244 		/* Ignore HPD capable connectors and connectors where we don't
    245 		 * want any hotplug detection at all for polling. */
    246 		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
    247 			continue;
    248 
    249 		repoll = true;
    250 
    251 		old_status = connector->status;
    252 		/* if we are connected and don't want to poll for disconnect
    253 		   skip it */
    254 		if (old_status == connector_status_connected &&
    255 		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
    256 			continue;
    257 
    258 		connector->status = connector->funcs->detect(connector, false);
    259 		if (old_status != connector->status) {
    260 			const char *old, *new;
    261 
    262 			old = drm_get_connector_status_name(old_status);
    263 			new = drm_get_connector_status_name(connector->status);
    264 
    265 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
    266 				      "status updated from %s to %s\n",
    267 				      connector->base.id,
    268 				      drm_get_connector_name(connector),
    269 				      old, new);
    270 
    271 			changed = true;
    272 		}
    273 	}
    274 
    275 	mutex_unlock(&dev->mode_config.mutex);
    276 
    277 	if (changed)
    278 		drm_kms_helper_hotplug_event(dev);
    279 
    280 	if (repoll)
    281 		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
    282 }
    283 
    284 /**
    285  * drm_kms_helper_poll_disable - disable output polling
    286  * @dev: drm_device
    287  *
    288  * This function disables the output polling work.
    289  *
    290  * Drivers can call this helper from their device suspend implementation. It is
    291  * not an error to call this even when output polling isn't enabled or arlready
    292  * disabled.
    293  */
    294 void drm_kms_helper_poll_disable(struct drm_device *dev)
    295 {
    296 	if (!dev->mode_config.poll_enabled)
    297 		return;
    298 	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
    299 }
    300 EXPORT_SYMBOL(drm_kms_helper_poll_disable);
    301 
    302 /**
    303  * drm_kms_helper_poll_enable - re-enable output polling.
    304  * @dev: drm_device
    305  *
    306  * This function re-enables the output polling work.
    307  *
    308  * Drivers can call this helper from their device resume implementation. It is
    309  * an error to call this when the output polling support has not yet been set
    310  * up.
    311  */
    312 void drm_kms_helper_poll_enable(struct drm_device *dev)
    313 {
    314 	bool poll = false;
    315 	struct drm_connector *connector;
    316 
    317 	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
    318 		return;
    319 
    320 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
    321 		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
    322 					 DRM_CONNECTOR_POLL_DISCONNECT))
    323 			poll = true;
    324 	}
    325 
    326 	if (poll)
    327 		schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
    328 }
    329 EXPORT_SYMBOL(drm_kms_helper_poll_enable);
    330 
    331 /**
    332  * drm_kms_helper_poll_init - initialize and enable output polling
    333  * @dev: drm_device
    334  *
    335  * This function intializes and then also enables output polling support for
    336  * @dev. Drivers which do not have reliable hotplug support in hardware can use
    337  * this helper infrastructure to regularly poll such connectors for changes in
    338  * their connection state.
    339  *
    340  * Drivers can control which connectors are polled by setting the
    341  * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
    342  * connectors where probing live outputs can result in visual distortion drivers
    343  * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
    344  * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
    345  * completely ignored by the polling logic.
    346  *
    347  * Note that a connector can be both polled and probed from the hotplug handler,
    348  * in case the hotplug interrupt is known to be unreliable.
    349  */
    350 void drm_kms_helper_poll_init(struct drm_device *dev)
    351 {
    352 	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
    353 	dev->mode_config.poll_enabled = true;
    354 
    355 	drm_kms_helper_poll_enable(dev);
    356 }
    357 EXPORT_SYMBOL(drm_kms_helper_poll_init);
    358 
    359 /**
    360  * drm_kms_helper_poll_fini - disable output polling and clean it up
    361  * @dev: drm_device
    362  */
    363 void drm_kms_helper_poll_fini(struct drm_device *dev)
    364 {
    365 	drm_kms_helper_poll_disable(dev);
    366 }
    367 EXPORT_SYMBOL(drm_kms_helper_poll_fini);
    368 
    369 /**
    370  * drm_helper_hpd_irq_event - hotplug processing
    371  * @dev: drm_device
    372  *
    373  * Drivers can use this helper function to run a detect cycle on all connectors
    374  * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
    375  * other connectors are ignored, which is useful to avoid reprobing fixed
    376  * panels.
    377  *
    378  * This helper function is useful for drivers which can't or don't track hotplug
    379  * interrupts for each connector.
    380  *
    381  * Drivers which support hotplug interrupts for each connector individually and
    382  * which have a more fine-grained detect logic should bypass this code and
    383  * directly call drm_kms_helper_hotplug_event() in case the connector state
    384  * changed.
    385  *
    386  * This function must be called from process context with no mode
    387  * setting locks held.
    388  *
    389  * Note that a connector can be both polled and probed from the hotplug handler,
    390  * in case the hotplug interrupt is known to be unreliable.
    391  */
    392 bool drm_helper_hpd_irq_event(struct drm_device *dev)
    393 {
    394 	struct drm_connector *connector;
    395 	enum drm_connector_status old_status;
    396 	bool changed = false;
    397 
    398 	if (!dev->mode_config.poll_enabled)
    399 		return false;
    400 
    401 	mutex_lock(&dev->mode_config.mutex);
    402 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
    403 
    404 		/* Only handle HPD capable connectors. */
    405 		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
    406 			continue;
    407 
    408 		old_status = connector->status;
    409 
    410 		connector->status = connector->funcs->detect(connector, false);
    411 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
    412 			      connector->base.id,
    413 			      drm_get_connector_name(connector),
    414 			      drm_get_connector_status_name(old_status),
    415 			      drm_get_connector_status_name(connector->status));
    416 		if (old_status != connector->status)
    417 			changed = true;
    418 	}
    419 
    420 	mutex_unlock(&dev->mode_config.mutex);
    421 
    422 	if (changed)
    423 		drm_kms_helper_hotplug_event(dev);
    424 
    425 	return changed;
    426 }
    427 EXPORT_SYMBOL(drm_helper_hpd_irq_event);
    428