drm_probe_helper.c 18 KB

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