drm_probe_helper.c 19 KB

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