drm_probe_helper.c 15 KB

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