drm_probe_helper.c 17 KB

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