Subversion Repositories Kolibri OS

Rev

Rev 6088 | Rev 6296 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5060 serge 1
/*
2
 * Copyright (c) 2006-2008 Intel Corporation
3
 * Copyright (c) 2007 Dave Airlie 
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 
28
 *      Dave Airlie 
29
 *      Jesse Barnes 
30
 */
31
 
32
#include 
33
#include 
34
 
35
#include 
36
#include 
37
#include 
38
#include 
39
#include 
40
#include 
41
 
42
/**
43
 * DOC: output probing helper overview
44
 *
45
 * This library provides some helper code for output probing. It provides an
46
 * implementation of the core connector->fill_modes interface with
47
 * drm_helper_probe_single_connector_modes.
48
 *
49
 * It also provides support for polling connectors with a work item and for
50
 * generic hotplug interrupt handling where the driver doesn't or cannot keep
51
 * track of a per-connector hpd interrupt.
52
 *
53
 * This helper library can be used independently of the modeset helper library.
54
 * Drivers can also overwrite different parts e.g. use their own hotplug
55
 * handling code to avoid probing unrelated outputs.
56
 */
57
 
58
static bool drm_kms_helper_poll = true;
59
module_param_named(poll, drm_kms_helper_poll, bool, 0600);
60
 
6084 serge 61
static enum drm_mode_status
62
drm_mode_validate_flag(const struct drm_display_mode *mode,
63
		       int flags)
5060 serge 64
{
6084 serge 65
	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
66
	    !(flags & DRM_MODE_FLAG_INTERLACE))
67
		return MODE_NO_INTERLACE;
68
 
69
	if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
70
	    !(flags & DRM_MODE_FLAG_DBLSCAN))
71
		return MODE_NO_DBLESCAN;
72
 
73
	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
74
	    !(flags & DRM_MODE_FLAG_3D_MASK))
75
		return MODE_NO_STEREO;
76
 
77
	return MODE_OK;
78
}
79
 
80
static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
81
{
5060 serge 82
	struct drm_display_mode *mode;
83
 
6084 serge 84
	if (!connector->cmdline_mode.specified)
85
		return 0;
86
 
87
	mode = drm_mode_create_from_cmdline_mode(connector->dev,
88
						 &connector->cmdline_mode);
89
	if (mode == NULL)
90
		return 0;
91
 
92
	drm_mode_probed_add(connector, mode);
93
	return 1;
94
}
95
 
96
#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
97
/**
98
 * drm_kms_helper_poll_enable_locked - re-enable output polling.
99
 * @dev: drm_device
100
 *
101
 * This function re-enables the output polling work without
102
 * locking the mode_config mutex.
103
 *
104
 * This is like drm_kms_helper_poll_enable() however it is to be
105
 * called from a context where the mode_config mutex is locked
106
 * already.
107
 */
108
void drm_kms_helper_poll_enable_locked(struct drm_device *dev)
109
{
110
	bool poll = false;
111
	struct drm_connector *connector;
112
 
113
	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
114
 
115
	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
5060 serge 116
		return;
117
 
6084 serge 118
	drm_for_each_connector(connector, dev) {
119
		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
120
					 DRM_CONNECTOR_POLL_DISCONNECT))
121
			poll = true;
5060 serge 122
	}
123
 
124
}
6084 serge 125
EXPORT_SYMBOL(drm_kms_helper_poll_enable_locked);
5060 serge 126
 
6084 serge 127
 
5060 serge 128
static int drm_helper_probe_single_connector_modes_merge_bits(struct drm_connector *connector,
129
							      uint32_t maxX, uint32_t maxY, bool merge_type_bits)
130
{
131
	struct drm_device *dev = connector->dev;
132
	struct drm_display_mode *mode;
6084 serge 133
	const struct drm_connector_helper_funcs *connector_funcs =
5060 serge 134
		connector->helper_private;
135
	int count = 0;
136
	int mode_flags = 0;
137
	bool verbose_prune = true;
6084 serge 138
	enum drm_connector_status old_status;
5060 serge 139
 
140
	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
141
 
142
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
143
			connector->name);
144
	/* set all modes to the unverified state */
145
	list_for_each_entry(mode, &connector->modes, head)
146
		mode->status = MODE_UNVERIFIED;
147
 
148
	if (connector->force) {
5271 serge 149
		if (connector->force == DRM_FORCE_ON ||
150
		    connector->force == DRM_FORCE_ON_DIGITAL)
5060 serge 151
			connector->status = connector_status_connected;
152
		else
153
			connector->status = connector_status_disconnected;
154
		if (connector->funcs->force)
155
			connector->funcs->force(connector);
156
	} else {
6084 serge 157
		old_status = connector->status;
158
 
5060 serge 159
		connector->status = connector->funcs->detect(connector, true);
160
	}
161
 
162
	/* Re-enable polling in case the global poll config changed. */
163
	if (drm_kms_helper_poll != dev->mode_config.poll_running)
6084 serge 164
		drm_kms_helper_poll_enable_locked(dev);
5060 serge 165
 
166
	dev->mode_config.poll_running = drm_kms_helper_poll;
167
 
168
	if (connector->status == connector_status_disconnected) {
169
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
170
			connector->base.id, connector->name);
171
		drm_mode_connector_update_edid_property(connector, NULL);
172
		verbose_prune = false;
173
		goto prune;
174
	}
175
 
176
#ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
177
	count = drm_load_edid_firmware(connector);
178
	if (count == 0)
179
#endif
180
	{
181
		if (connector->override_edid) {
182
			struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
183
 
184
			count = drm_add_edid_modes(connector, edid);
6084 serge 185
			drm_edid_to_eld(connector, edid);
5060 serge 186
		} else
6084 serge 187
			count = (*connector_funcs->get_modes)(connector);
5060 serge 188
	}
189
 
190
	if (count == 0 && connector->status == connector_status_connected)
191
		count = drm_add_modes_noedid(connector, 1024, 768);
6088 serge 192
	count += drm_helper_probe_add_cmdline_mode(connector);
5060 serge 193
	if (count == 0)
194
		goto prune;
195
 
196
	drm_mode_connector_list_update(connector, merge_type_bits);
197
 
198
	if (connector->interlace_allowed)
199
		mode_flags |= DRM_MODE_FLAG_INTERLACE;
200
	if (connector->doublescan_allowed)
201
		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
202
	if (connector->stereo_allowed)
203
		mode_flags |= DRM_MODE_FLAG_3D_MASK;
204
 
205
	list_for_each_entry(mode, &connector->modes, head) {
6084 serge 206
		if (mode->status == MODE_OK)
207
			mode->status = drm_mode_validate_basic(mode);
208
 
209
		if (mode->status == MODE_OK)
210
			mode->status = drm_mode_validate_size(mode, maxX, maxY);
211
 
212
		if (mode->status == MODE_OK)
213
			mode->status = drm_mode_validate_flag(mode, mode_flags);
214
 
5060 serge 215
		if (mode->status == MODE_OK && connector_funcs->mode_valid)
216
			mode->status = connector_funcs->mode_valid(connector,
217
								   mode);
218
	}
219
 
220
prune:
221
	drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
222
 
223
	if (list_empty(&connector->modes))
224
		return 0;
225
 
226
	list_for_each_entry(mode, &connector->modes, head)
227
		mode->vrefresh = drm_mode_vrefresh(mode);
228
 
229
	drm_mode_sort(&connector->modes);
230
 
231
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
232
			connector->name);
233
	list_for_each_entry(mode, &connector->modes, head) {
234
		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
235
		drm_mode_debug_printmodeline(mode);
236
	}
237
 
238
	return count;
239
}
240
 
241
/**
242
 * drm_helper_probe_single_connector_modes - get complete set of display modes
243
 * @connector: connector to probe
244
 * @maxX: max width for modes
245
 * @maxY: max height for modes
246
 *
247
 * Based on the helper callbacks implemented by @connector try to detect all
248
 * valid modes.  Modes will first be added to the connector's probed_modes list,
249
 * then culled (based on validity and the @maxX, @maxY parameters) and put into
250
 * the normal modes list.
251
 *
252
 * Intended to be use as a generic implementation of the ->fill_modes()
253
 * @connector vfunc for drivers that use the crtc helpers for output mode
254
 * filtering and detection.
255
 *
256
 * Returns:
257
 * The number of modes found on @connector.
258
 */
259
int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
260
					    uint32_t maxX, uint32_t maxY)
261
{
262
	return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, true);
263
}
264
EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
265
 
266
/**
267
 * drm_helper_probe_single_connector_modes_nomerge - get complete set of display modes
268
 * @connector: connector to probe
269
 * @maxX: max width for modes
270
 * @maxY: max height for modes
271
 *
272
 * This operates like drm_hehlper_probe_single_connector_modes except it
273
 * replaces the mode bits instead of merging them for preferred modes.
274
 */
275
int drm_helper_probe_single_connector_modes_nomerge(struct drm_connector *connector,
276
					    uint32_t maxX, uint32_t maxY)
277
{
278
	return drm_helper_probe_single_connector_modes_merge_bits(connector, maxX, maxY, false);
279
}
280
EXPORT_SYMBOL(drm_helper_probe_single_connector_modes_nomerge);
281
 
282
/**
283
 * drm_kms_helper_hotplug_event - fire off KMS hotplug events
284
 * @dev: drm_device whose connector state changed
285
 *
286
 * This function fires off the uevent for userspace and also calls the
287
 * output_poll_changed function, which is most commonly used to inform the fbdev
288
 * emulation code and allow it to update the fbcon output configuration.
289
 *
290
 * Drivers should call this from their hotplug handling code when a change is
291
 * detected. Note that this function does not do any output detection of its
292
 * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
293
 * driver already.
294
 *
295
 * This function must be called from process context with no mode
296
 * setting locks held.
297
 */
298
void drm_kms_helper_hotplug_event(struct drm_device *dev)
299
{
300
	/* send a uevent + call fbdev */
301
//   drm_sysfs_hotplug_event(dev);
302
//   if (dev->mode_config.funcs->output_poll_changed)
303
//       dev->mode_config.funcs->output_poll_changed(dev);
304
}
305
EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
306
 
307
static void output_poll_execute(struct work_struct *work)
308
{
309
	struct delayed_work *delayed_work = to_delayed_work(work);
310
	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
311
	struct drm_connector *connector;
312
	enum drm_connector_status old_status;
6084 serge 313
	bool repoll = false, changed;
5060 serge 314
 
6084 serge 315
	/* Pick up any changes detected by the probe functions. */
316
	changed = dev->mode_config.delayed_event;
317
	dev->mode_config.delayed_event = false;
318
 
5060 serge 319
	if (!drm_kms_helper_poll)
6084 serge 320
		goto out;
5060 serge 321
 
322
	mutex_lock(&dev->mode_config.mutex);
6084 serge 323
	drm_for_each_connector(connector, dev) {
5060 serge 324
 
325
		/* Ignore forced connectors. */
326
		if (connector->force)
327
			continue;
328
 
329
		/* Ignore HPD capable connectors and connectors where we don't
330
		 * want any hotplug detection at all for polling. */
331
		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
332
			continue;
333
 
334
		old_status = connector->status;
335
		/* if we are connected and don't want to poll for disconnect
336
		   skip it */
337
		if (old_status == connector_status_connected &&
338
		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
339
			continue;
340
 
6084 serge 341
		repoll = true;
342
 
5060 serge 343
		connector->status = connector->funcs->detect(connector, false);
344
		if (old_status != connector->status) {
345
			const char *old, *new;
346
 
6084 serge 347
			/*
348
			 * The poll work sets force=false when calling detect so
349
			 * that drivers can avoid to do disruptive tests (e.g.
350
			 * when load detect cycles could cause flickering on
351
			 * other, running displays). This bears the risk that we
352
			 * flip-flop between unknown here in the poll work and
353
			 * the real state when userspace forces a full detect
354
			 * call after receiving a hotplug event due to this
355
			 * change.
356
			 *
357
			 * Hence clamp an unknown detect status to the old
358
			 * value.
359
			 */
360
			if (connector->status == connector_status_unknown) {
361
				connector->status = old_status;
362
				continue;
363
			}
364
 
5060 serge 365
			old = drm_get_connector_status_name(old_status);
366
			new = drm_get_connector_status_name(connector->status);
367
 
368
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
369
				      "status updated from %s to %s\n",
370
				      connector->base.id,
371
				      connector->name,
372
				      old, new);
373
 
374
			changed = true;
375
		}
376
	}
377
 
378
	mutex_unlock(&dev->mode_config.mutex);
379
 
6283 serge 380
out:;
6084 serge 381
 
5060 serge 382
 
383
}
384
 
385
/**
386
 * drm_kms_helper_poll_disable - disable output polling
387
 * @dev: drm_device
388
 *
389
 * This function disables the output polling work.
390
 *
391
 * Drivers can call this helper from their device suspend implementation. It is
392
 * not an error to call this even when output polling isn't enabled or arlready
393
 * disabled.
394
 */
395
void drm_kms_helper_poll_disable(struct drm_device *dev)
396
{
397
	if (!dev->mode_config.poll_enabled)
398
		return;
399
//   cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
400
}
401
EXPORT_SYMBOL(drm_kms_helper_poll_disable);
402
 
403
/**
404
 * drm_kms_helper_poll_enable - re-enable output polling.
405
 * @dev: drm_device
406
 *
407
 * This function re-enables the output polling work.
408
 *
409
 * Drivers can call this helper from their device resume implementation. It is
410
 * an error to call this when the output polling support has not yet been set
411
 * up.
412
 */
413
void drm_kms_helper_poll_enable(struct drm_device *dev)
414
{
6084 serge 415
	mutex_lock(&dev->mode_config.mutex);
416
	drm_kms_helper_poll_enable_locked(dev);
417
	mutex_unlock(&dev->mode_config.mutex);
5060 serge 418
}
419
EXPORT_SYMBOL(drm_kms_helper_poll_enable);
420
 
421
/**
422
 * drm_kms_helper_poll_init - initialize and enable output polling
423
 * @dev: drm_device
424
 *
425
 * This function intializes and then also enables output polling support for
426
 * @dev. Drivers which do not have reliable hotplug support in hardware can use
427
 * this helper infrastructure to regularly poll such connectors for changes in
428
 * their connection state.
429
 *
430
 * Drivers can control which connectors are polled by setting the
431
 * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
432
 * connectors where probing live outputs can result in visual distortion drivers
433
 * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
434
 * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
435
 * completely ignored by the polling logic.
436
 *
437
 * Note that a connector can be both polled and probed from the hotplug handler,
438
 * in case the hotplug interrupt is known to be unreliable.
439
 */
440
void drm_kms_helper_poll_init(struct drm_device *dev)
441
{
442
	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
443
	dev->mode_config.poll_enabled = true;
444
 
445
	drm_kms_helper_poll_enable(dev);
446
}
447
EXPORT_SYMBOL(drm_kms_helper_poll_init);
448
 
449
/**
450
 * drm_kms_helper_poll_fini - disable output polling and clean it up
451
 * @dev: drm_device
452
 */
453
void drm_kms_helper_poll_fini(struct drm_device *dev)
454
{
455
	drm_kms_helper_poll_disable(dev);
456
}
457
EXPORT_SYMBOL(drm_kms_helper_poll_fini);
458
 
459
/**
460
 * drm_helper_hpd_irq_event - hotplug processing
461
 * @dev: drm_device
462
 *
463
 * Drivers can use this helper function to run a detect cycle on all connectors
464
 * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
465
 * other connectors are ignored, which is useful to avoid reprobing fixed
466
 * panels.
467
 *
468
 * This helper function is useful for drivers which can't or don't track hotplug
469
 * interrupts for each connector.
470
 *
471
 * Drivers which support hotplug interrupts for each connector individually and
472
 * which have a more fine-grained detect logic should bypass this code and
473
 * directly call drm_kms_helper_hotplug_event() in case the connector state
474
 * changed.
475
 *
476
 * This function must be called from process context with no mode
477
 * setting locks held.
478
 *
479
 * Note that a connector can be both polled and probed from the hotplug handler,
480
 * in case the hotplug interrupt is known to be unreliable.
481
 */
482
bool drm_helper_hpd_irq_event(struct drm_device *dev)
483
{
484
	struct drm_connector *connector;
485
	enum drm_connector_status old_status;
486
	bool changed = false;
487
 
488
	if (!dev->mode_config.poll_enabled)
489
		return false;
490
 
491
	mutex_lock(&dev->mode_config.mutex);
6084 serge 492
	drm_for_each_connector(connector, dev) {
5060 serge 493
 
494
		/* Only handle HPD capable connectors. */
495
		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
496
			continue;
497
 
498
		old_status = connector->status;
499
 
500
		connector->status = connector->funcs->detect(connector, false);
501
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
502
			      connector->base.id,
503
			      connector->name,
504
			      drm_get_connector_status_name(old_status),
505
			      drm_get_connector_status_name(connector->status));
506
		if (old_status != connector->status)
507
			changed = true;
508
	}
509
 
510
	mutex_unlock(&dev->mode_config.mutex);
511
 
512
	if (changed)
513
		drm_kms_helper_hotplug_event(dev);
514
 
515
	return changed;
516
}
517
EXPORT_SYMBOL(drm_helper_hpd_irq_event);