drm_probe_helper.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. #include <drm/drm_modeset_helper_vtables.h>
  40. #include "drm_crtc_helper_internal.h"
  41. /**
  42. * DOC: output probing helper overview
  43. *
  44. * This library provides some helper code for output probing. It provides an
  45. * implementation of the core &drm_connector_funcs.fill_modes interface with
  46. * drm_helper_probe_single_connector_modes().
  47. *
  48. * It also provides support for polling connectors with a work item and for
  49. * generic hotplug interrupt handling where the driver doesn't or cannot keep
  50. * track of a per-connector hpd interrupt.
  51. *
  52. * This helper library can be used independently of the modeset helper library.
  53. * Drivers can also overwrite different parts e.g. use their own hotplug
  54. * handling code to avoid probing unrelated outputs.
  55. *
  56. * The probe helpers share the function table structures with other display
  57. * helper libraries. See &struct drm_connector_helper_funcs for the details.
  58. */
  59. static bool drm_kms_helper_poll = true;
  60. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  61. static enum drm_mode_status
  62. drm_mode_validate_flag(const struct drm_display_mode *mode,
  63. int flags)
  64. {
  65. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  66. !(flags & DRM_MODE_FLAG_INTERLACE))
  67. return MODE_NO_INTERLACE;
  68. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  69. !(flags & DRM_MODE_FLAG_DBLSCAN))
  70. return MODE_NO_DBLESCAN;
  71. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
  72. !(flags & DRM_MODE_FLAG_3D_MASK))
  73. return MODE_NO_STEREO;
  74. return MODE_OK;
  75. }
  76. static enum drm_mode_status
  77. drm_mode_validate_pipeline(struct drm_display_mode *mode,
  78. struct drm_connector *connector)
  79. {
  80. struct drm_device *dev = connector->dev;
  81. uint32_t *ids = connector->encoder_ids;
  82. enum drm_mode_status ret = MODE_OK;
  83. unsigned int i;
  84. /* Step 1: Validate against connector */
  85. ret = drm_connector_mode_valid(connector, mode);
  86. if (ret != MODE_OK)
  87. return ret;
  88. /* Step 2: Validate against encoders and crtcs */
  89. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  90. struct drm_encoder *encoder = drm_encoder_find(dev, ids[i]);
  91. struct drm_crtc *crtc;
  92. if (!encoder)
  93. continue;
  94. ret = drm_encoder_mode_valid(encoder, mode);
  95. if (ret != MODE_OK) {
  96. /* No point in continuing for crtc check as this encoder
  97. * will not accept the mode anyway. If all encoders
  98. * reject the mode then, at exit, ret will not be
  99. * MODE_OK. */
  100. continue;
  101. }
  102. ret = drm_bridge_mode_valid(encoder->bridge, mode);
  103. if (ret != MODE_OK) {
  104. /* There is also no point in continuing for crtc check
  105. * here. */
  106. continue;
  107. }
  108. drm_for_each_crtc(crtc, dev) {
  109. if (!drm_encoder_crtc_ok(encoder, crtc))
  110. continue;
  111. ret = drm_crtc_mode_valid(crtc, mode);
  112. if (ret == MODE_OK) {
  113. /* If we get to this point there is at least
  114. * one combination of encoder+crtc that works
  115. * for this mode. Lets return now. */
  116. return ret;
  117. }
  118. }
  119. }
  120. return ret;
  121. }
  122. static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
  123. {
  124. struct drm_cmdline_mode *cmdline_mode;
  125. struct drm_display_mode *mode;
  126. cmdline_mode = &connector->cmdline_mode;
  127. if (!cmdline_mode->specified)
  128. return 0;
  129. /* Only add a GTF mode if we find no matching probed modes */
  130. list_for_each_entry(mode, &connector->probed_modes, head) {
  131. if (mode->hdisplay != cmdline_mode->xres ||
  132. mode->vdisplay != cmdline_mode->yres)
  133. continue;
  134. if (cmdline_mode->refresh_specified) {
  135. /* The probed mode's vrefresh is set until later */
  136. if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
  137. continue;
  138. }
  139. return 0;
  140. }
  141. mode = drm_mode_create_from_cmdline_mode(connector->dev,
  142. cmdline_mode);
  143. if (mode == NULL)
  144. return 0;
  145. drm_mode_probed_add(connector, mode);
  146. return 1;
  147. }
  148. enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
  149. const struct drm_display_mode *mode)
  150. {
  151. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  152. if (!crtc_funcs || !crtc_funcs->mode_valid)
  153. return MODE_OK;
  154. return crtc_funcs->mode_valid(crtc, mode);
  155. }
  156. enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
  157. const struct drm_display_mode *mode)
  158. {
  159. const struct drm_encoder_helper_funcs *encoder_funcs =
  160. encoder->helper_private;
  161. if (!encoder_funcs || !encoder_funcs->mode_valid)
  162. return MODE_OK;
  163. return encoder_funcs->mode_valid(encoder, mode);
  164. }
  165. enum drm_mode_status drm_connector_mode_valid(struct drm_connector *connector,
  166. struct drm_display_mode *mode)
  167. {
  168. const struct drm_connector_helper_funcs *connector_funcs =
  169. connector->helper_private;
  170. if (!connector_funcs || !connector_funcs->mode_valid)
  171. return MODE_OK;
  172. return connector_funcs->mode_valid(connector, mode);
  173. }
  174. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  175. /**
  176. * drm_kms_helper_poll_enable - re-enable output polling.
  177. * @dev: drm_device
  178. *
  179. * This function re-enables the output polling work, after it has been
  180. * temporarily disabled using drm_kms_helper_poll_disable(), for example over
  181. * suspend/resume.
  182. *
  183. * Drivers can call this helper from their device resume implementation. It is
  184. * an error to call this when the output polling support has not yet been set
  185. * up.
  186. *
  187. * Note that calls to enable and disable polling must be strictly ordered, which
  188. * is automatically the case when they're only call from suspend/resume
  189. * callbacks.
  190. */
  191. void drm_kms_helper_poll_enable(struct drm_device *dev)
  192. {
  193. bool poll = false;
  194. struct drm_connector *connector;
  195. struct drm_connector_list_iter conn_iter;
  196. unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
  197. if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
  198. return;
  199. drm_connector_list_iter_begin(dev, &conn_iter);
  200. drm_for_each_connector_iter(connector, &conn_iter) {
  201. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  202. DRM_CONNECTOR_POLL_DISCONNECT))
  203. poll = true;
  204. }
  205. drm_connector_list_iter_end(&conn_iter);
  206. if (dev->mode_config.delayed_event) {
  207. /*
  208. * FIXME:
  209. *
  210. * Use short (1s) delay to handle the initial delayed event.
  211. * This delay should not be needed, but Optimus/nouveau will
  212. * fail in a mysterious way if the delayed event is handled as
  213. * soon as possible like it is done in
  214. * drm_helper_probe_single_connector_modes() in case the poll
  215. * was enabled before.
  216. */
  217. poll = true;
  218. delay = HZ;
  219. }
  220. if (poll)
  221. schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
  222. }
  223. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  224. static enum drm_connector_status
  225. drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
  226. {
  227. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  228. struct drm_modeset_acquire_ctx ctx;
  229. int ret;
  230. drm_modeset_acquire_init(&ctx, 0);
  231. retry:
  232. ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
  233. if (!ret) {
  234. if (funcs->detect_ctx)
  235. ret = funcs->detect_ctx(connector, &ctx, force);
  236. else if (connector->funcs->detect)
  237. ret = connector->funcs->detect(connector, force);
  238. else
  239. ret = connector_status_connected;
  240. }
  241. if (ret == -EDEADLK) {
  242. drm_modeset_backoff(&ctx);
  243. goto retry;
  244. }
  245. if (WARN_ON(ret < 0))
  246. ret = connector_status_unknown;
  247. drm_modeset_drop_locks(&ctx);
  248. drm_modeset_acquire_fini(&ctx);
  249. return ret;
  250. }
  251. /**
  252. * drm_helper_probe_detect - probe connector status
  253. * @connector: connector to probe
  254. * @ctx: acquire_ctx, or NULL to let this function handle locking.
  255. * @force: Whether destructive probe operations should be performed.
  256. *
  257. * This function calls the detect callbacks of the connector.
  258. * This function returns &drm_connector_status, or
  259. * if @ctx is set, it might also return -EDEADLK.
  260. */
  261. int
  262. drm_helper_probe_detect(struct drm_connector *connector,
  263. struct drm_modeset_acquire_ctx *ctx,
  264. bool force)
  265. {
  266. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  267. struct drm_device *dev = connector->dev;
  268. int ret;
  269. if (!ctx)
  270. return drm_helper_probe_detect_ctx(connector, force);
  271. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  272. if (ret)
  273. return ret;
  274. if (funcs->detect_ctx)
  275. return funcs->detect_ctx(connector, ctx, force);
  276. else if (connector->funcs->detect)
  277. return connector->funcs->detect(connector, force);
  278. else
  279. return connector_status_connected;
  280. }
  281. EXPORT_SYMBOL(drm_helper_probe_detect);
  282. /**
  283. * drm_helper_probe_single_connector_modes - get complete set of display modes
  284. * @connector: connector to probe
  285. * @maxX: max width for modes
  286. * @maxY: max height for modes
  287. *
  288. * Based on the helper callbacks implemented by @connector in struct
  289. * &drm_connector_helper_funcs try to detect all valid modes. Modes will first
  290. * be added to the connector's probed_modes list, then culled (based on validity
  291. * and the @maxX, @maxY parameters) and put into the normal modes list.
  292. *
  293. * Intended to be used as a generic implementation of the
  294. * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
  295. * for output mode filtering and detection.
  296. *
  297. * The basic procedure is as follows
  298. *
  299. * 1. All modes currently on the connector's modes list are marked as stale
  300. *
  301. * 2. New modes are added to the connector's probed_modes list with
  302. * drm_mode_probed_add(). New modes start their life with status as OK.
  303. * Modes are added from a single source using the following priority order.
  304. *
  305. * - debugfs 'override_edid' (used for testing only)
  306. * - firmware EDID (drm_load_edid_firmware())
  307. * - &drm_connector_helper_funcs.get_modes vfunc
  308. * - if the connector status is connector_status_connected, standard
  309. * VESA DMT modes up to 1024x768 are automatically added
  310. * (drm_add_modes_noedid())
  311. *
  312. * Finally modes specified via the kernel command line (video=...) are
  313. * added in addition to what the earlier probes produced
  314. * (drm_helper_probe_add_cmdline_mode()). These modes are generated
  315. * using the VESA GTF/CVT formulas.
  316. *
  317. * 3. Modes are moved from the probed_modes list to the modes list. Potential
  318. * duplicates are merged together (see drm_mode_connector_list_update()).
  319. * After this step the probed_modes list will be empty again.
  320. *
  321. * 4. Any non-stale mode on the modes list then undergoes validation
  322. *
  323. * - drm_mode_validate_basic() performs basic sanity checks
  324. * - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
  325. * (if specified)
  326. * - drm_mode_validate_flag() checks the modes against basic connector
  327. * capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
  328. * - the optional &drm_connector_helper_funcs.mode_valid helper can perform
  329. * driver and/or sink specific checks
  330. * - the optional &drm_crtc_helper_funcs.mode_valid,
  331. * &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
  332. * helpers can perform driver and/or source specific checks which are also
  333. * enforced by the modeset/atomic helpers
  334. *
  335. * 5. Any mode whose status is not OK is pruned from the connector's modes list,
  336. * accompanied by a debug message indicating the reason for the mode's
  337. * rejection (see drm_mode_prune_invalid()).
  338. *
  339. * Returns:
  340. * The number of modes found on @connector.
  341. */
  342. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  343. uint32_t maxX, uint32_t maxY)
  344. {
  345. struct drm_device *dev = connector->dev;
  346. struct drm_display_mode *mode;
  347. const struct drm_connector_helper_funcs *connector_funcs =
  348. connector->helper_private;
  349. int count = 0, ret;
  350. int mode_flags = 0;
  351. bool verbose_prune = true;
  352. enum drm_connector_status old_status;
  353. struct drm_modeset_acquire_ctx ctx;
  354. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  355. drm_modeset_acquire_init(&ctx, 0);
  356. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
  357. connector->name);
  358. retry:
  359. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  360. if (ret == -EDEADLK) {
  361. drm_modeset_backoff(&ctx);
  362. goto retry;
  363. } else
  364. WARN_ON(ret < 0);
  365. /* set all old modes to the stale state */
  366. list_for_each_entry(mode, &connector->modes, head)
  367. mode->status = MODE_STALE;
  368. old_status = connector->status;
  369. if (connector->force) {
  370. if (connector->force == DRM_FORCE_ON ||
  371. connector->force == DRM_FORCE_ON_DIGITAL)
  372. connector->status = connector_status_connected;
  373. else
  374. connector->status = connector_status_disconnected;
  375. if (connector->funcs->force)
  376. connector->funcs->force(connector);
  377. } else {
  378. ret = drm_helper_probe_detect(connector, &ctx, true);
  379. if (ret == -EDEADLK) {
  380. drm_modeset_backoff(&ctx);
  381. goto retry;
  382. } else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
  383. ret = connector_status_unknown;
  384. connector->status = ret;
  385. }
  386. /*
  387. * Normally either the driver's hpd code or the poll loop should
  388. * pick up any changes and fire the hotplug event. But if
  389. * userspace sneaks in a probe, we might miss a change. Hence
  390. * check here, and if anything changed start the hotplug code.
  391. */
  392. if (old_status != connector->status) {
  393. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  394. connector->base.id,
  395. connector->name,
  396. drm_get_connector_status_name(old_status),
  397. drm_get_connector_status_name(connector->status));
  398. /*
  399. * The hotplug event code might call into the fb
  400. * helpers, and so expects that we do not hold any
  401. * locks. Fire up the poll struct instead, it will
  402. * disable itself again.
  403. */
  404. dev->mode_config.delayed_event = true;
  405. if (dev->mode_config.poll_enabled)
  406. schedule_delayed_work(&dev->mode_config.output_poll_work,
  407. 0);
  408. }
  409. /* Re-enable polling in case the global poll config changed. */
  410. if (drm_kms_helper_poll != dev->mode_config.poll_running)
  411. drm_kms_helper_poll_enable(dev);
  412. dev->mode_config.poll_running = drm_kms_helper_poll;
  413. if (connector->status == connector_status_disconnected) {
  414. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
  415. connector->base.id, connector->name);
  416. drm_mode_connector_update_edid_property(connector, NULL);
  417. verbose_prune = false;
  418. goto prune;
  419. }
  420. if (connector->override_edid) {
  421. struct edid *edid = (struct edid *) connector->edid_blob_ptr->data;
  422. count = drm_add_edid_modes(connector, edid);
  423. drm_edid_to_eld(connector, edid);
  424. } else {
  425. struct edid *edid = drm_load_edid_firmware(connector);
  426. if (!IS_ERR_OR_NULL(edid)) {
  427. drm_mode_connector_update_edid_property(connector, edid);
  428. count = drm_add_edid_modes(connector, edid);
  429. drm_edid_to_eld(connector, edid);
  430. kfree(edid);
  431. }
  432. if (count == 0)
  433. count = (*connector_funcs->get_modes)(connector);
  434. }
  435. if (count == 0 && connector->status == connector_status_connected)
  436. count = drm_add_modes_noedid(connector, 1024, 768);
  437. count += drm_helper_probe_add_cmdline_mode(connector);
  438. if (count == 0)
  439. goto prune;
  440. drm_mode_connector_list_update(connector);
  441. if (connector->interlace_allowed)
  442. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  443. if (connector->doublescan_allowed)
  444. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  445. if (connector->stereo_allowed)
  446. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  447. list_for_each_entry(mode, &connector->modes, head) {
  448. if (mode->status == MODE_OK)
  449. mode->status = drm_mode_validate_basic(mode);
  450. if (mode->status == MODE_OK)
  451. mode->status = drm_mode_validate_size(mode, maxX, maxY);
  452. if (mode->status == MODE_OK)
  453. mode->status = drm_mode_validate_flag(mode, mode_flags);
  454. if (mode->status == MODE_OK)
  455. mode->status = drm_mode_validate_pipeline(mode,
  456. connector);
  457. if (mode->status == MODE_OK)
  458. mode->status = drm_mode_validate_ycbcr420(mode,
  459. connector);
  460. }
  461. prune:
  462. drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
  463. drm_modeset_drop_locks(&ctx);
  464. drm_modeset_acquire_fini(&ctx);
  465. if (list_empty(&connector->modes))
  466. return 0;
  467. list_for_each_entry(mode, &connector->modes, head)
  468. mode->vrefresh = drm_mode_vrefresh(mode);
  469. drm_mode_sort(&connector->modes);
  470. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
  471. connector->name);
  472. list_for_each_entry(mode, &connector->modes, head) {
  473. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  474. drm_mode_debug_printmodeline(mode);
  475. }
  476. return count;
  477. }
  478. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  479. /**
  480. * drm_kms_helper_hotplug_event - fire off KMS hotplug events
  481. * @dev: drm_device whose connector state changed
  482. *
  483. * This function fires off the uevent for userspace and also calls the
  484. * output_poll_changed function, which is most commonly used to inform the fbdev
  485. * emulation code and allow it to update the fbcon output configuration.
  486. *
  487. * Drivers should call this from their hotplug handling code when a change is
  488. * detected. Note that this function does not do any output detection of its
  489. * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
  490. * driver already.
  491. *
  492. * This function must be called from process context with no mode
  493. * setting locks held.
  494. */
  495. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  496. {
  497. /* send a uevent + call fbdev */
  498. drm_sysfs_hotplug_event(dev);
  499. if (dev->mode_config.funcs->output_poll_changed)
  500. dev->mode_config.funcs->output_poll_changed(dev);
  501. }
  502. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  503. static void output_poll_execute(struct work_struct *work)
  504. {
  505. struct delayed_work *delayed_work = to_delayed_work(work);
  506. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  507. struct drm_connector *connector;
  508. struct drm_connector_list_iter conn_iter;
  509. enum drm_connector_status old_status;
  510. bool repoll = false, changed;
  511. /* Pick up any changes detected by the probe functions. */
  512. changed = dev->mode_config.delayed_event;
  513. dev->mode_config.delayed_event = false;
  514. if (!drm_kms_helper_poll)
  515. goto out;
  516. if (!mutex_trylock(&dev->mode_config.mutex)) {
  517. repoll = true;
  518. goto out;
  519. }
  520. drm_connector_list_iter_begin(dev, &conn_iter);
  521. drm_for_each_connector_iter(connector, &conn_iter) {
  522. /* Ignore forced connectors. */
  523. if (connector->force)
  524. continue;
  525. /* Ignore HPD capable connectors and connectors where we don't
  526. * want any hotplug detection at all for polling. */
  527. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  528. continue;
  529. old_status = connector->status;
  530. /* if we are connected and don't want to poll for disconnect
  531. skip it */
  532. if (old_status == connector_status_connected &&
  533. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  534. continue;
  535. repoll = true;
  536. connector->status = drm_helper_probe_detect(connector, NULL, false);
  537. if (old_status != connector->status) {
  538. const char *old, *new;
  539. /*
  540. * The poll work sets force=false when calling detect so
  541. * that drivers can avoid to do disruptive tests (e.g.
  542. * when load detect cycles could cause flickering on
  543. * other, running displays). This bears the risk that we
  544. * flip-flop between unknown here in the poll work and
  545. * the real state when userspace forces a full detect
  546. * call after receiving a hotplug event due to this
  547. * change.
  548. *
  549. * Hence clamp an unknown detect status to the old
  550. * value.
  551. */
  552. if (connector->status == connector_status_unknown) {
  553. connector->status = old_status;
  554. continue;
  555. }
  556. old = drm_get_connector_status_name(old_status);
  557. new = drm_get_connector_status_name(connector->status);
  558. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
  559. "status updated from %s to %s\n",
  560. connector->base.id,
  561. connector->name,
  562. old, new);
  563. changed = true;
  564. }
  565. }
  566. drm_connector_list_iter_end(&conn_iter);
  567. mutex_unlock(&dev->mode_config.mutex);
  568. out:
  569. if (changed)
  570. drm_kms_helper_hotplug_event(dev);
  571. if (repoll)
  572. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  573. }
  574. /**
  575. * drm_kms_helper_poll_disable - disable output polling
  576. * @dev: drm_device
  577. *
  578. * This function disables the output polling work.
  579. *
  580. * Drivers can call this helper from their device suspend implementation. It is
  581. * not an error to call this even when output polling isn't enabled or already
  582. * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
  583. *
  584. * Note that calls to enable and disable polling must be strictly ordered, which
  585. * is automatically the case when they're only call from suspend/resume
  586. * callbacks.
  587. */
  588. void drm_kms_helper_poll_disable(struct drm_device *dev)
  589. {
  590. if (!dev->mode_config.poll_enabled)
  591. return;
  592. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  593. }
  594. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  595. /**
  596. * drm_kms_helper_poll_init - initialize and enable output polling
  597. * @dev: drm_device
  598. *
  599. * This function intializes and then also enables output polling support for
  600. * @dev. Drivers which do not have reliable hotplug support in hardware can use
  601. * this helper infrastructure to regularly poll such connectors for changes in
  602. * their connection state.
  603. *
  604. * Drivers can control which connectors are polled by setting the
  605. * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
  606. * connectors where probing live outputs can result in visual distortion drivers
  607. * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
  608. * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
  609. * completely ignored by the polling logic.
  610. *
  611. * Note that a connector can be both polled and probed from the hotplug handler,
  612. * in case the hotplug interrupt is known to be unreliable.
  613. */
  614. void drm_kms_helper_poll_init(struct drm_device *dev)
  615. {
  616. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  617. dev->mode_config.poll_enabled = true;
  618. drm_kms_helper_poll_enable(dev);
  619. }
  620. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  621. /**
  622. * drm_kms_helper_poll_fini - disable output polling and clean it up
  623. * @dev: drm_device
  624. */
  625. void drm_kms_helper_poll_fini(struct drm_device *dev)
  626. {
  627. drm_kms_helper_poll_disable(dev);
  628. }
  629. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  630. /**
  631. * drm_helper_hpd_irq_event - hotplug processing
  632. * @dev: drm_device
  633. *
  634. * Drivers can use this helper function to run a detect cycle on all connectors
  635. * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
  636. * other connectors are ignored, which is useful to avoid reprobing fixed
  637. * panels.
  638. *
  639. * This helper function is useful for drivers which can't or don't track hotplug
  640. * interrupts for each connector.
  641. *
  642. * Drivers which support hotplug interrupts for each connector individually and
  643. * which have a more fine-grained detect logic should bypass this code and
  644. * directly call drm_kms_helper_hotplug_event() in case the connector state
  645. * changed.
  646. *
  647. * This function must be called from process context with no mode
  648. * setting locks held.
  649. *
  650. * Note that a connector can be both polled and probed from the hotplug handler,
  651. * in case the hotplug interrupt is known to be unreliable.
  652. */
  653. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  654. {
  655. struct drm_connector *connector;
  656. struct drm_connector_list_iter conn_iter;
  657. enum drm_connector_status old_status;
  658. bool changed = false;
  659. if (!dev->mode_config.poll_enabled)
  660. return false;
  661. mutex_lock(&dev->mode_config.mutex);
  662. drm_connector_list_iter_begin(dev, &conn_iter);
  663. drm_for_each_connector_iter(connector, &conn_iter) {
  664. /* Only handle HPD capable connectors. */
  665. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  666. continue;
  667. old_status = connector->status;
  668. connector->status = drm_helper_probe_detect(connector, NULL, false);
  669. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
  670. connector->base.id,
  671. connector->name,
  672. drm_get_connector_status_name(old_status),
  673. drm_get_connector_status_name(connector->status));
  674. if (old_status != connector->status)
  675. changed = true;
  676. }
  677. drm_connector_list_iter_end(&conn_iter);
  678. mutex_unlock(&dev->mode_config.mutex);
  679. if (changed)
  680. drm_kms_helper_hotplug_event(dev);
  681. return changed;
  682. }
  683. EXPORT_SYMBOL(drm_helper_hpd_irq_event);