drm_probe_helper.c 25 KB

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