drm_probe_helper.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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/drm_bridge.h>
  34. #include <drm/drm_client_event.h>
  35. #include <drm/drm_crtc.h>
  36. #include <drm/drm_edid.h>
  37. #include <drm/drm_fourcc.h>
  38. #include <drm/drm_managed.h>
  39. #include <drm/drm_modeset_helper_vtables.h>
  40. #include <drm/drm_print.h>
  41. #include <drm/drm_probe_helper.h>
  42. #include <drm/drm_sysfs.h>
  43. #include "drm_crtc_helper_internal.h"
  44. /**
  45. * DOC: output probing helper overview
  46. *
  47. * This library provides some helper code for output probing. It provides an
  48. * implementation of the core &drm_connector_funcs.fill_modes interface with
  49. * drm_helper_probe_single_connector_modes().
  50. *
  51. * It also provides support for polling connectors with a work item and for
  52. * generic hotplug interrupt handling where the driver doesn't or cannot keep
  53. * track of a per-connector hpd interrupt.
  54. *
  55. * This helper library can be used independently of the modeset helper library.
  56. * Drivers can also overwrite different parts e.g. use their own hotplug
  57. * handling code to avoid probing unrelated outputs.
  58. *
  59. * The probe helpers share the function table structures with other display
  60. * helper libraries. See &struct drm_connector_helper_funcs for the details.
  61. */
  62. static bool drm_kms_helper_poll = true;
  63. module_param_named(poll, drm_kms_helper_poll, bool, 0600);
  64. static enum drm_mode_status
  65. drm_mode_validate_flag(const struct drm_display_mode *mode,
  66. int flags)
  67. {
  68. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
  69. !(flags & DRM_MODE_FLAG_INTERLACE))
  70. return MODE_NO_INTERLACE;
  71. if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
  72. !(flags & DRM_MODE_FLAG_DBLSCAN))
  73. return MODE_NO_DBLESCAN;
  74. if ((mode->flags & DRM_MODE_FLAG_3D_MASK) &&
  75. !(flags & DRM_MODE_FLAG_3D_MASK))
  76. return MODE_NO_STEREO;
  77. return MODE_OK;
  78. }
  79. static int
  80. drm_mode_validate_pipeline(struct drm_display_mode *mode,
  81. struct drm_connector *connector,
  82. struct drm_modeset_acquire_ctx *ctx,
  83. enum drm_mode_status *status)
  84. {
  85. struct drm_device *dev = connector->dev;
  86. struct drm_encoder *encoder;
  87. int ret;
  88. /* Step 1: Validate against connector */
  89. ret = drm_connector_mode_valid(connector, mode, ctx, status);
  90. if (ret || *status != MODE_OK)
  91. return ret;
  92. /* Step 2: Validate against encoders and crtcs */
  93. drm_connector_for_each_possible_encoder(connector, encoder) {
  94. struct drm_bridge *bridge;
  95. struct drm_crtc *crtc;
  96. *status = drm_encoder_mode_valid(encoder, mode);
  97. if (*status != MODE_OK) {
  98. /* No point in continuing for crtc check as this encoder
  99. * will not accept the mode anyway. If all encoders
  100. * reject the mode then, at exit, ret will not be
  101. * MODE_OK. */
  102. continue;
  103. }
  104. bridge = drm_bridge_chain_get_first_bridge(encoder);
  105. *status = drm_bridge_chain_mode_valid(bridge,
  106. &connector->display_info,
  107. mode);
  108. drm_bridge_put(bridge);
  109. if (*status != MODE_OK) {
  110. /* There is also no point in continuing for crtc check
  111. * here. */
  112. continue;
  113. }
  114. drm_for_each_crtc(crtc, dev) {
  115. if (!drm_encoder_crtc_ok(encoder, crtc))
  116. continue;
  117. *status = drm_crtc_mode_valid(crtc, mode);
  118. if (*status == MODE_OK) {
  119. /* If we get to this point there is at least
  120. * one combination of encoder+crtc that works
  121. * for this mode. Lets return now. */
  122. return 0;
  123. }
  124. }
  125. }
  126. return 0;
  127. }
  128. static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector)
  129. {
  130. struct drm_cmdline_mode *cmdline_mode;
  131. struct drm_display_mode *mode;
  132. cmdline_mode = &connector->cmdline_mode;
  133. if (!cmdline_mode->specified)
  134. return 0;
  135. /* Only add a GTF mode if we find no matching probed modes */
  136. list_for_each_entry(mode, &connector->probed_modes, head) {
  137. if (mode->hdisplay != cmdline_mode->xres ||
  138. mode->vdisplay != cmdline_mode->yres)
  139. continue;
  140. if (cmdline_mode->refresh_specified) {
  141. /* The probed mode's vrefresh is set until later */
  142. if (drm_mode_vrefresh(mode) != cmdline_mode->refresh)
  143. continue;
  144. }
  145. /* Mark the matching mode as being preferred by the user */
  146. mode->type |= DRM_MODE_TYPE_USERDEF;
  147. return 0;
  148. }
  149. mode = drm_mode_create_from_cmdline_mode(connector->dev,
  150. cmdline_mode);
  151. if (mode == NULL)
  152. return 0;
  153. drm_mode_probed_add(connector, mode);
  154. return 1;
  155. }
  156. enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc,
  157. const struct drm_display_mode *mode)
  158. {
  159. const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  160. if (!crtc_funcs || !crtc_funcs->mode_valid)
  161. return MODE_OK;
  162. return crtc_funcs->mode_valid(crtc, mode);
  163. }
  164. enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder,
  165. const struct drm_display_mode *mode)
  166. {
  167. const struct drm_encoder_helper_funcs *encoder_funcs =
  168. encoder->helper_private;
  169. if (!encoder_funcs || !encoder_funcs->mode_valid)
  170. return MODE_OK;
  171. return encoder_funcs->mode_valid(encoder, mode);
  172. }
  173. int
  174. drm_connector_mode_valid(struct drm_connector *connector,
  175. const struct drm_display_mode *mode,
  176. struct drm_modeset_acquire_ctx *ctx,
  177. enum drm_mode_status *status)
  178. {
  179. const struct drm_connector_helper_funcs *connector_funcs =
  180. connector->helper_private;
  181. int ret = 0;
  182. if (!connector_funcs)
  183. *status = MODE_OK;
  184. else if (connector_funcs->mode_valid_ctx)
  185. ret = connector_funcs->mode_valid_ctx(connector, mode, ctx,
  186. status);
  187. else if (connector_funcs->mode_valid)
  188. *status = connector_funcs->mode_valid(connector, mode);
  189. else
  190. *status = MODE_OK;
  191. return ret;
  192. }
  193. static void drm_kms_helper_disable_hpd(struct drm_device *dev)
  194. {
  195. struct drm_connector *connector;
  196. struct drm_connector_list_iter conn_iter;
  197. drm_connector_list_iter_begin(dev, &conn_iter);
  198. drm_for_each_connector_iter(connector, &conn_iter) {
  199. const struct drm_connector_helper_funcs *funcs =
  200. connector->helper_private;
  201. if (funcs && funcs->disable_hpd)
  202. funcs->disable_hpd(connector);
  203. }
  204. drm_connector_list_iter_end(&conn_iter);
  205. }
  206. static bool drm_kms_helper_enable_hpd(struct drm_device *dev)
  207. {
  208. bool poll = false;
  209. struct drm_connector *connector;
  210. struct drm_connector_list_iter conn_iter;
  211. drm_connector_list_iter_begin(dev, &conn_iter);
  212. drm_for_each_connector_iter(connector, &conn_iter) {
  213. const struct drm_connector_helper_funcs *funcs =
  214. connector->helper_private;
  215. if (funcs && funcs->enable_hpd)
  216. funcs->enable_hpd(connector);
  217. if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
  218. DRM_CONNECTOR_POLL_DISCONNECT))
  219. poll = true;
  220. }
  221. drm_connector_list_iter_end(&conn_iter);
  222. return poll;
  223. }
  224. #define DRM_OUTPUT_POLL_PERIOD (10*HZ)
  225. static void reschedule_output_poll_work(struct drm_device *dev)
  226. {
  227. unsigned long delay = DRM_OUTPUT_POLL_PERIOD;
  228. if (dev->mode_config.delayed_event)
  229. /*
  230. * FIXME:
  231. *
  232. * Use short (1s) delay to handle the initial delayed event.
  233. * This delay should not be needed, but Optimus/nouveau will
  234. * fail in a mysterious way if the delayed event is handled as
  235. * soon as possible like it is done in
  236. * drm_helper_probe_single_connector_modes() in case the poll
  237. * was enabled before.
  238. */
  239. delay = HZ;
  240. schedule_delayed_work(&dev->mode_config.output_poll_work, delay);
  241. }
  242. /**
  243. * drm_kms_helper_poll_enable - re-enable output polling.
  244. * @dev: drm_device
  245. *
  246. * This function re-enables the output polling work, after it has been
  247. * temporarily disabled using drm_kms_helper_poll_disable(), for example over
  248. * suspend/resume.
  249. *
  250. * Drivers can call this helper from their device resume implementation. It is
  251. * not an error to call this even when output polling isn't enabled.
  252. *
  253. * If device polling was never initialized before, this call will trigger a
  254. * warning and return.
  255. *
  256. * Note that calls to enable and disable polling must be strictly ordered, which
  257. * is automatically the case when they're only call from suspend/resume
  258. * callbacks.
  259. */
  260. void drm_kms_helper_poll_enable(struct drm_device *dev)
  261. {
  262. if (drm_WARN_ON_ONCE(dev, !dev->mode_config.poll_enabled) ||
  263. !drm_kms_helper_poll || dev->mode_config.poll_running)
  264. return;
  265. if (drm_kms_helper_enable_hpd(dev) ||
  266. dev->mode_config.delayed_event)
  267. reschedule_output_poll_work(dev);
  268. dev->mode_config.poll_running = true;
  269. }
  270. EXPORT_SYMBOL(drm_kms_helper_poll_enable);
  271. /**
  272. * drm_kms_helper_poll_reschedule - reschedule the output polling work
  273. * @dev: drm_device
  274. *
  275. * This function reschedules the output polling work, after polling for a
  276. * connector has been enabled.
  277. *
  278. * Drivers must call this helper after enabling polling for a connector by
  279. * setting %DRM_CONNECTOR_POLL_CONNECT / %DRM_CONNECTOR_POLL_DISCONNECT flags
  280. * in drm_connector::polled. Note that after disabling polling by clearing these
  281. * flags for a connector will stop the output polling work automatically if
  282. * the polling is disabled for all other connectors as well.
  283. *
  284. * The function can be called only after polling has been enabled by calling
  285. * drm_kms_helper_poll_init() / drm_kms_helper_poll_enable().
  286. */
  287. void drm_kms_helper_poll_reschedule(struct drm_device *dev)
  288. {
  289. if (dev->mode_config.poll_running)
  290. reschedule_output_poll_work(dev);
  291. }
  292. EXPORT_SYMBOL(drm_kms_helper_poll_reschedule);
  293. static int detect_connector_status(struct drm_connector *connector,
  294. struct drm_modeset_acquire_ctx *ctx,
  295. bool force)
  296. {
  297. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  298. if (funcs->detect_ctx)
  299. return funcs->detect_ctx(connector, ctx, force);
  300. else if (connector->funcs->detect)
  301. return connector->funcs->detect(connector, force);
  302. return connector_status_connected;
  303. }
  304. static enum drm_connector_status
  305. drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force)
  306. {
  307. struct drm_modeset_acquire_ctx ctx;
  308. int ret;
  309. drm_modeset_acquire_init(&ctx, 0);
  310. retry:
  311. ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx);
  312. if (!ret)
  313. ret = detect_connector_status(connector, &ctx, force);
  314. if (ret == -EDEADLK) {
  315. drm_modeset_backoff(&ctx);
  316. goto retry;
  317. }
  318. if (WARN_ON(ret < 0))
  319. ret = connector_status_unknown;
  320. if (ret != connector->status)
  321. connector->epoch_counter += 1;
  322. drm_modeset_drop_locks(&ctx);
  323. drm_modeset_acquire_fini(&ctx);
  324. return ret;
  325. }
  326. /**
  327. * drm_helper_probe_detect - probe connector status
  328. * @connector: connector to probe
  329. * @ctx: acquire_ctx, or NULL to let this function handle locking.
  330. * @force: Whether destructive probe operations should be performed.
  331. *
  332. * This function calls the detect callbacks of the connector.
  333. * This function returns &drm_connector_status, or
  334. * if @ctx is set, it might also return -EDEADLK.
  335. */
  336. int
  337. drm_helper_probe_detect(struct drm_connector *connector,
  338. struct drm_modeset_acquire_ctx *ctx,
  339. bool force)
  340. {
  341. struct drm_device *dev = connector->dev;
  342. int ret;
  343. if (!ctx)
  344. return drm_helper_probe_detect_ctx(connector, force);
  345. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  346. if (ret)
  347. return ret;
  348. ret = detect_connector_status(connector, ctx, force);
  349. if (ret != connector->status)
  350. connector->epoch_counter += 1;
  351. return ret;
  352. }
  353. EXPORT_SYMBOL(drm_helper_probe_detect);
  354. static int drm_helper_probe_get_modes(struct drm_connector *connector)
  355. {
  356. const struct drm_connector_helper_funcs *connector_funcs =
  357. connector->helper_private;
  358. int count;
  359. count = connector_funcs->get_modes(connector);
  360. /* The .get_modes() callback should not return negative values. */
  361. if (count < 0) {
  362. drm_err(connector->dev, ".get_modes() returned %pe\n",
  363. ERR_PTR(count));
  364. count = 0;
  365. }
  366. /*
  367. * Fallback for when DDC probe failed in drm_get_edid() and thus skipped
  368. * override/firmware EDID.
  369. */
  370. if (count == 0 && connector->status == connector_status_connected)
  371. count = drm_edid_override_connector_update(connector);
  372. return count;
  373. }
  374. static int __drm_helper_update_and_validate(struct drm_connector *connector,
  375. uint32_t maxX, uint32_t maxY,
  376. struct drm_modeset_acquire_ctx *ctx)
  377. {
  378. struct drm_device *dev = connector->dev;
  379. struct drm_display_mode *mode;
  380. int mode_flags = 0;
  381. int ret;
  382. drm_connector_list_update(connector);
  383. if (connector->interlace_allowed)
  384. mode_flags |= DRM_MODE_FLAG_INTERLACE;
  385. if (connector->doublescan_allowed)
  386. mode_flags |= DRM_MODE_FLAG_DBLSCAN;
  387. if (connector->stereo_allowed)
  388. mode_flags |= DRM_MODE_FLAG_3D_MASK;
  389. list_for_each_entry(mode, &connector->modes, head) {
  390. if (mode->status != MODE_OK)
  391. continue;
  392. mode->status = drm_mode_validate_driver(dev, mode);
  393. if (mode->status != MODE_OK)
  394. continue;
  395. mode->status = drm_mode_validate_size(mode, maxX, maxY);
  396. if (mode->status != MODE_OK)
  397. continue;
  398. mode->status = drm_mode_validate_flag(mode, mode_flags);
  399. if (mode->status != MODE_OK)
  400. continue;
  401. mode->status = drm_mode_validate_ycbcr420(mode, connector);
  402. if (mode->status != MODE_OK)
  403. continue;
  404. ret = drm_mode_validate_pipeline(mode, connector, ctx,
  405. &mode->status);
  406. if (ret) {
  407. drm_dbg_kms(dev,
  408. "drm_mode_validate_pipeline failed: %d\n",
  409. ret);
  410. if (drm_WARN_ON_ONCE(dev, ret != -EDEADLK))
  411. mode->status = MODE_ERROR;
  412. else
  413. return -EDEADLK;
  414. }
  415. }
  416. return 0;
  417. }
  418. /**
  419. * drm_helper_probe_single_connector_modes - get complete set of display modes
  420. * @connector: connector to probe
  421. * @maxX: max width for modes
  422. * @maxY: max height for modes
  423. *
  424. * Based on the helper callbacks implemented by @connector in struct
  425. * &drm_connector_helper_funcs try to detect all valid modes. Modes will first
  426. * be added to the connector's probed_modes list, then culled (based on validity
  427. * and the @maxX, @maxY parameters) and put into the normal modes list.
  428. *
  429. * Intended to be used as a generic implementation of the
  430. * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers
  431. * for output mode filtering and detection.
  432. *
  433. * The basic procedure is as follows
  434. *
  435. * 1. All modes currently on the connector's modes list are marked as stale
  436. *
  437. * 2. New modes are added to the connector's probed_modes list with
  438. * drm_mode_probed_add(). New modes start their life with status as OK.
  439. * Modes are added from a single source using the following priority order.
  440. *
  441. * - &drm_connector_helper_funcs.get_modes vfunc
  442. * - if the connector status is connector_status_connected, standard
  443. * VESA DMT modes up to 1024x768 are automatically added
  444. * (drm_add_modes_noedid())
  445. *
  446. * Finally modes specified via the kernel command line (video=...) are
  447. * added in addition to what the earlier probes produced
  448. * (drm_helper_probe_add_cmdline_mode()). These modes are generated
  449. * using the VESA GTF/CVT formulas.
  450. *
  451. * 3. Modes are moved from the probed_modes list to the modes list. Potential
  452. * duplicates are merged together (see drm_connector_list_update()).
  453. * After this step the probed_modes list will be empty again.
  454. *
  455. * 4. Any non-stale mode on the modes list then undergoes validation
  456. *
  457. * - drm_mode_validate_basic() performs basic sanity checks
  458. * - drm_mode_validate_size() filters out modes larger than @maxX and @maxY
  459. * (if specified)
  460. * - drm_mode_validate_flag() checks the modes against basic connector
  461. * capabilities (interlace_allowed,doublescan_allowed,stereo_allowed)
  462. * - the optional &drm_connector_helper_funcs.mode_valid or
  463. * &drm_connector_helper_funcs.mode_valid_ctx helpers can perform driver
  464. * and/or sink specific checks
  465. * - the optional &drm_crtc_helper_funcs.mode_valid,
  466. * &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid
  467. * helpers can perform driver and/or source specific checks which are also
  468. * enforced by the modeset/atomic helpers
  469. *
  470. * 5. Any mode whose status is not OK is pruned from the connector's modes list,
  471. * accompanied by a debug message indicating the reason for the mode's
  472. * rejection (see drm_mode_prune_invalid()).
  473. *
  474. * Returns:
  475. * The number of modes found on @connector.
  476. */
  477. int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
  478. uint32_t maxX, uint32_t maxY)
  479. {
  480. struct drm_device *dev = connector->dev;
  481. struct drm_display_mode *mode;
  482. int count = 0, ret;
  483. enum drm_connector_status old_status;
  484. struct drm_modeset_acquire_ctx ctx;
  485. WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  486. drm_modeset_acquire_init(&ctx, 0);
  487. drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n", connector->base.id,
  488. connector->name);
  489. retry:
  490. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  491. if (ret == -EDEADLK) {
  492. drm_modeset_backoff(&ctx);
  493. goto retry;
  494. } else
  495. WARN_ON(ret < 0);
  496. /* set all old modes to the stale state */
  497. list_for_each_entry(mode, &connector->modes, head)
  498. mode->status = MODE_STALE;
  499. old_status = connector->status;
  500. if (connector->force) {
  501. if (connector->force == DRM_FORCE_ON ||
  502. connector->force == DRM_FORCE_ON_DIGITAL)
  503. connector->status = connector_status_connected;
  504. else
  505. connector->status = connector_status_disconnected;
  506. if (connector->funcs->force)
  507. connector->funcs->force(connector);
  508. } else {
  509. ret = drm_helper_probe_detect(connector, &ctx, true);
  510. if (ret == -EDEADLK) {
  511. drm_modeset_backoff(&ctx);
  512. goto retry;
  513. } else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret))
  514. ret = connector_status_unknown;
  515. connector->status = ret;
  516. }
  517. /*
  518. * Normally either the driver's hpd code or the poll loop should
  519. * pick up any changes and fire the hotplug event. But if
  520. * userspace sneaks in a probe, we might miss a change. Hence
  521. * check here, and if anything changed start the hotplug code.
  522. */
  523. if (old_status != connector->status) {
  524. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
  525. connector->base.id, connector->name,
  526. drm_get_connector_status_name(old_status),
  527. drm_get_connector_status_name(connector->status));
  528. /*
  529. * The hotplug event code might call into the fb
  530. * helpers, and so expects that we do not hold any
  531. * locks. Fire up the poll struct instead, it will
  532. * disable itself again.
  533. */
  534. dev->mode_config.delayed_event = true;
  535. if (dev->mode_config.poll_enabled)
  536. mod_delayed_work(system_wq,
  537. &dev->mode_config.output_poll_work,
  538. 0);
  539. }
  540. /*
  541. * Re-enable polling in case the global poll config changed but polling
  542. * is still initialized.
  543. */
  544. if (dev->mode_config.poll_enabled)
  545. drm_kms_helper_poll_enable(dev);
  546. if (connector->status == connector_status_disconnected) {
  547. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] disconnected\n",
  548. connector->base.id, connector->name);
  549. drm_connector_update_edid_property(connector, NULL);
  550. drm_mode_prune_invalid(dev, &connector->modes, false);
  551. goto exit;
  552. }
  553. count = drm_helper_probe_get_modes(connector);
  554. if (count == 0 && (connector->status == connector_status_connected ||
  555. connector->status == connector_status_unknown)) {
  556. count = drm_add_modes_noedid(connector, 1024, 768);
  557. /*
  558. * Section 4.2.2.6 (EDID Corruption Detection) of the DP 1.4a
  559. * Link CTS specifies that 640x480 (the official "failsafe"
  560. * mode) needs to be the default if there's no EDID.
  561. */
  562. if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
  563. drm_set_preferred_mode(connector, 640, 480);
  564. }
  565. count += drm_helper_probe_add_cmdline_mode(connector);
  566. if (count != 0) {
  567. ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
  568. if (ret == -EDEADLK) {
  569. drm_modeset_backoff(&ctx);
  570. goto retry;
  571. }
  572. }
  573. drm_mode_prune_invalid(dev, &connector->modes, true);
  574. /*
  575. * Displayport spec section 5.2.1.2 ("Video Timing Format") says that
  576. * all detachable sinks shall support 640x480 @60Hz as a fail safe
  577. * mode. If all modes were pruned, perhaps because they need more
  578. * lanes or a higher pixel clock than available, at least try to add
  579. * in 640x480.
  580. */
  581. if (list_empty(&connector->modes) &&
  582. connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
  583. count = drm_add_modes_noedid(connector, 640, 480);
  584. ret = __drm_helper_update_and_validate(connector, maxX, maxY, &ctx);
  585. if (ret == -EDEADLK) {
  586. drm_modeset_backoff(&ctx);
  587. goto retry;
  588. }
  589. drm_mode_prune_invalid(dev, &connector->modes, true);
  590. }
  591. exit:
  592. drm_modeset_drop_locks(&ctx);
  593. drm_modeset_acquire_fini(&ctx);
  594. if (list_empty(&connector->modes))
  595. return 0;
  596. drm_mode_sort(&connector->modes);
  597. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] probed modes:\n",
  598. connector->base.id, connector->name);
  599. list_for_each_entry(mode, &connector->modes, head) {
  600. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  601. drm_dbg_kms(dev, "Probed mode: " DRM_MODE_FMT "\n",
  602. DRM_MODE_ARG(mode));
  603. }
  604. return count;
  605. }
  606. EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
  607. /**
  608. * drm_kms_helper_hotplug_event - fire off KMS hotplug events
  609. * @dev: drm_device whose connector state changed
  610. *
  611. * This function fires off the uevent for userspace and also calls the
  612. * client hotplug function, which is most commonly used to inform the fbdev
  613. * emulation code and allow it to update the fbcon output configuration.
  614. *
  615. * Drivers should call this from their hotplug handling code when a change is
  616. * detected. Note that this function does not do any output detection of its
  617. * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the
  618. * driver already.
  619. *
  620. * This function must be called from process context with no mode
  621. * setting locks held.
  622. *
  623. * If only a single connector has changed, consider calling
  624. * drm_kms_helper_connector_hotplug_event() instead.
  625. */
  626. void drm_kms_helper_hotplug_event(struct drm_device *dev)
  627. {
  628. drm_sysfs_hotplug_event(dev);
  629. drm_client_dev_hotplug(dev);
  630. }
  631. EXPORT_SYMBOL(drm_kms_helper_hotplug_event);
  632. /**
  633. * drm_kms_helper_connector_hotplug_event - fire off a KMS connector hotplug event
  634. * @connector: drm_connector which has changed
  635. *
  636. * This is the same as drm_kms_helper_hotplug_event(), except it fires a more
  637. * fine-grained uevent for a single connector.
  638. */
  639. void drm_kms_helper_connector_hotplug_event(struct drm_connector *connector)
  640. {
  641. struct drm_device *dev = connector->dev;
  642. drm_sysfs_connector_hotplug_event(connector);
  643. drm_client_dev_hotplug(dev);
  644. }
  645. EXPORT_SYMBOL(drm_kms_helper_connector_hotplug_event);
  646. static void output_poll_execute(struct work_struct *work)
  647. {
  648. struct delayed_work *delayed_work = to_delayed_work(work);
  649. struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
  650. struct drm_connector *connector;
  651. struct drm_connector_list_iter conn_iter;
  652. enum drm_connector_status old_status;
  653. bool repoll = false, changed;
  654. u64 old_epoch_counter;
  655. if (!dev->mode_config.poll_enabled)
  656. return;
  657. /* Pick up any changes detected by the probe functions. */
  658. changed = dev->mode_config.delayed_event;
  659. dev->mode_config.delayed_event = false;
  660. if (!drm_kms_helper_poll) {
  661. if (dev->mode_config.poll_running) {
  662. drm_kms_helper_disable_hpd(dev);
  663. dev->mode_config.poll_running = false;
  664. }
  665. goto out;
  666. }
  667. if (!mutex_trylock(&dev->mode_config.mutex)) {
  668. repoll = true;
  669. goto out;
  670. }
  671. drm_connector_list_iter_begin(dev, &conn_iter);
  672. drm_for_each_connector_iter(connector, &conn_iter) {
  673. /* Ignore forced connectors. */
  674. if (connector->force)
  675. continue;
  676. /* Ignore HPD capable connectors and connectors where we don't
  677. * want any hotplug detection at all for polling. */
  678. if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
  679. continue;
  680. old_status = connector->status;
  681. /* if we are connected and don't want to poll for disconnect
  682. skip it */
  683. if (old_status == connector_status_connected &&
  684. !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
  685. continue;
  686. repoll = true;
  687. old_epoch_counter = connector->epoch_counter;
  688. connector->status = drm_helper_probe_detect(connector, NULL, false);
  689. if (old_epoch_counter != connector->epoch_counter) {
  690. const char *old, *new;
  691. /*
  692. * The poll work sets force=false when calling detect so
  693. * that drivers can avoid to do disruptive tests (e.g.
  694. * when load detect cycles could cause flickering on
  695. * other, running displays). This bears the risk that we
  696. * flip-flop between unknown here in the poll work and
  697. * the real state when userspace forces a full detect
  698. * call after receiving a hotplug event due to this
  699. * change.
  700. *
  701. * Hence clamp an unknown detect status to the old
  702. * value.
  703. */
  704. if (connector->status == connector_status_unknown) {
  705. connector->status = old_status;
  706. continue;
  707. }
  708. old = drm_get_connector_status_name(old_status);
  709. new = drm_get_connector_status_name(connector->status);
  710. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
  711. connector->base.id, connector->name,
  712. old, new);
  713. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] epoch counter %llu -> %llu\n",
  714. connector->base.id, connector->name,
  715. old_epoch_counter, connector->epoch_counter);
  716. changed = true;
  717. }
  718. }
  719. drm_connector_list_iter_end(&conn_iter);
  720. mutex_unlock(&dev->mode_config.mutex);
  721. out:
  722. if (changed)
  723. drm_kms_helper_hotplug_event(dev);
  724. if (repoll)
  725. schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
  726. }
  727. /**
  728. * drm_kms_helper_is_poll_worker - is %current task an output poll worker?
  729. *
  730. * Determine if %current task is an output poll worker. This can be used
  731. * to select distinct code paths for output polling versus other contexts.
  732. *
  733. * One use case is to avoid a deadlock between the output poll worker and
  734. * the autosuspend worker wherein the latter waits for polling to finish
  735. * upon calling drm_kms_helper_poll_disable(), while the former waits for
  736. * runtime suspend to finish upon calling pm_runtime_get_sync() in a
  737. * connector ->detect hook.
  738. */
  739. bool drm_kms_helper_is_poll_worker(void)
  740. {
  741. struct work_struct *work = current_work();
  742. return work && work->func == output_poll_execute;
  743. }
  744. EXPORT_SYMBOL(drm_kms_helper_is_poll_worker);
  745. /**
  746. * drm_kms_helper_poll_disable - disable output polling
  747. * @dev: drm_device
  748. *
  749. * This function disables the output polling work.
  750. *
  751. * Drivers can call this helper from their device suspend implementation. It is
  752. * not an error to call this even when output polling isn't enabled or already
  753. * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable().
  754. *
  755. * If however, the polling was never initialized, this call will trigger a
  756. * warning and return.
  757. *
  758. * Note that calls to enable and disable polling must be strictly ordered, which
  759. * is automatically the case when they're only call from suspend/resume
  760. * callbacks.
  761. */
  762. void drm_kms_helper_poll_disable(struct drm_device *dev)
  763. {
  764. if (drm_WARN_ON(dev, !dev->mode_config.poll_enabled))
  765. return;
  766. if (dev->mode_config.poll_running)
  767. drm_kms_helper_disable_hpd(dev);
  768. cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
  769. dev->mode_config.poll_running = false;
  770. }
  771. EXPORT_SYMBOL(drm_kms_helper_poll_disable);
  772. /**
  773. * drm_kms_helper_poll_init - initialize and enable output polling
  774. * @dev: drm_device
  775. *
  776. * This function initializes and then also enables output polling support for
  777. * @dev. Drivers which do not have reliable hotplug support in hardware can use
  778. * this helper infrastructure to regularly poll such connectors for changes in
  779. * their connection state.
  780. *
  781. * Drivers can control which connectors are polled by setting the
  782. * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On
  783. * connectors where probing live outputs can result in visual distortion drivers
  784. * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this.
  785. * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are
  786. * completely ignored by the polling logic.
  787. *
  788. * Note that a connector can be both polled and probed from the hotplug handler,
  789. * in case the hotplug interrupt is known to be unreliable.
  790. */
  791. void drm_kms_helper_poll_init(struct drm_device *dev)
  792. {
  793. INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
  794. dev->mode_config.poll_enabled = true;
  795. drm_kms_helper_poll_enable(dev);
  796. }
  797. EXPORT_SYMBOL(drm_kms_helper_poll_init);
  798. /**
  799. * drm_kms_helper_poll_fini - disable output polling and clean it up
  800. * @dev: drm_device
  801. */
  802. void drm_kms_helper_poll_fini(struct drm_device *dev)
  803. {
  804. if (!dev->mode_config.poll_enabled)
  805. return;
  806. drm_kms_helper_poll_disable(dev);
  807. dev->mode_config.poll_enabled = false;
  808. }
  809. EXPORT_SYMBOL(drm_kms_helper_poll_fini);
  810. static void drm_kms_helper_poll_init_release(struct drm_device *dev, void *res)
  811. {
  812. drm_kms_helper_poll_fini(dev);
  813. }
  814. /**
  815. * drmm_kms_helper_poll_init - initialize and enable output polling
  816. * @dev: drm_device
  817. *
  818. * This function initializes and then also enables output polling support for
  819. * @dev similar to drm_kms_helper_poll_init(). Polling will automatically be
  820. * cleaned up when the DRM device goes away.
  821. *
  822. * See drm_kms_helper_poll_init() for more information.
  823. */
  824. void drmm_kms_helper_poll_init(struct drm_device *dev)
  825. {
  826. int ret;
  827. drm_kms_helper_poll_init(dev);
  828. ret = drmm_add_action_or_reset(dev, drm_kms_helper_poll_init_release, dev);
  829. if (ret)
  830. drm_warn(dev, "Connector status will not be updated, error %d\n", ret);
  831. }
  832. EXPORT_SYMBOL(drmm_kms_helper_poll_init);
  833. static bool check_connector_changed(struct drm_connector *connector)
  834. {
  835. struct drm_device *dev = connector->dev;
  836. enum drm_connector_status old_status;
  837. u64 old_epoch_counter;
  838. /* Only handle HPD capable connectors. */
  839. drm_WARN_ON(dev, !(connector->polled & DRM_CONNECTOR_POLL_HPD));
  840. drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
  841. old_status = connector->status;
  842. old_epoch_counter = connector->epoch_counter;
  843. connector->status = drm_helper_probe_detect(connector, NULL, false);
  844. if (old_epoch_counter == connector->epoch_counter) {
  845. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Same epoch counter %llu\n",
  846. connector->base.id,
  847. connector->name,
  848. connector->epoch_counter);
  849. return false;
  850. }
  851. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s\n",
  852. connector->base.id,
  853. connector->name,
  854. drm_get_connector_status_name(old_status),
  855. drm_get_connector_status_name(connector->status));
  856. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Changed epoch counter %llu => %llu\n",
  857. connector->base.id,
  858. connector->name,
  859. old_epoch_counter,
  860. connector->epoch_counter);
  861. return true;
  862. }
  863. /**
  864. * drm_connector_helper_hpd_irq_event - hotplug processing
  865. * @connector: drm_connector
  866. *
  867. * Drivers can use this helper function to run a detect cycle on a connector
  868. * which has the DRM_CONNECTOR_POLL_HPD flag set in its &polled member.
  869. *
  870. * This helper function is useful for drivers which can track hotplug
  871. * interrupts for a single connector. Drivers that want to send a
  872. * hotplug event for all connectors or can't track hotplug interrupts
  873. * per connector need to use drm_helper_hpd_irq_event().
  874. *
  875. * This function must be called from process context with no mode
  876. * setting locks held.
  877. *
  878. * Note that a connector can be both polled and probed from the hotplug
  879. * handler, in case the hotplug interrupt is known to be unreliable.
  880. *
  881. * Returns:
  882. * A boolean indicating whether the connector status changed or not
  883. */
  884. bool drm_connector_helper_hpd_irq_event(struct drm_connector *connector)
  885. {
  886. struct drm_device *dev = connector->dev;
  887. bool changed;
  888. mutex_lock(&dev->mode_config.mutex);
  889. changed = check_connector_changed(connector);
  890. mutex_unlock(&dev->mode_config.mutex);
  891. if (changed) {
  892. drm_kms_helper_connector_hotplug_event(connector);
  893. drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Sent hotplug event\n",
  894. connector->base.id,
  895. connector->name);
  896. }
  897. return changed;
  898. }
  899. EXPORT_SYMBOL(drm_connector_helper_hpd_irq_event);
  900. /**
  901. * drm_helper_hpd_irq_event - hotplug processing
  902. * @dev: drm_device
  903. *
  904. * Drivers can use this helper function to run a detect cycle on all connectors
  905. * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All
  906. * other connectors are ignored, which is useful to avoid reprobing fixed
  907. * panels.
  908. *
  909. * This helper function is useful for drivers which can't or don't track hotplug
  910. * interrupts for each connector.
  911. *
  912. * Drivers which support hotplug interrupts for each connector individually and
  913. * which have a more fine-grained detect logic can use
  914. * drm_connector_helper_hpd_irq_event(). Alternatively, they should bypass this
  915. * code and directly call drm_kms_helper_hotplug_event() in case the connector
  916. * state changed.
  917. *
  918. * This function must be called from process context with no mode
  919. * setting locks held.
  920. *
  921. * Note that a connector can be both polled and probed from the hotplug handler,
  922. * in case the hotplug interrupt is known to be unreliable.
  923. *
  924. * Returns:
  925. * A boolean indicating whether the connector status changed or not
  926. */
  927. bool drm_helper_hpd_irq_event(struct drm_device *dev)
  928. {
  929. struct drm_connector *connector, *first_changed_connector = NULL;
  930. struct drm_connector_list_iter conn_iter;
  931. int changed = 0;
  932. if (!dev->mode_config.poll_enabled)
  933. return false;
  934. mutex_lock(&dev->mode_config.mutex);
  935. drm_connector_list_iter_begin(dev, &conn_iter);
  936. drm_for_each_connector_iter(connector, &conn_iter) {
  937. /* Only handle HPD capable connectors. */
  938. if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
  939. continue;
  940. if (check_connector_changed(connector)) {
  941. if (!first_changed_connector) {
  942. drm_connector_get(connector);
  943. first_changed_connector = connector;
  944. }
  945. changed++;
  946. }
  947. }
  948. drm_connector_list_iter_end(&conn_iter);
  949. mutex_unlock(&dev->mode_config.mutex);
  950. if (changed == 1)
  951. drm_kms_helper_connector_hotplug_event(first_changed_connector);
  952. else if (changed > 0)
  953. drm_kms_helper_hotplug_event(dev);
  954. if (first_changed_connector)
  955. drm_connector_put(first_changed_connector);
  956. return changed;
  957. }
  958. EXPORT_SYMBOL(drm_helper_hpd_irq_event);
  959. /**
  960. * drm_crtc_helper_mode_valid_fixed - Validates a display mode
  961. * @crtc: the crtc
  962. * @mode: the mode to validate
  963. * @fixed_mode: the display hardware's mode
  964. *
  965. * Returns:
  966. * MODE_OK on success, or another mode-status code otherwise.
  967. */
  968. enum drm_mode_status drm_crtc_helper_mode_valid_fixed(struct drm_crtc *crtc,
  969. const struct drm_display_mode *mode,
  970. const struct drm_display_mode *fixed_mode)
  971. {
  972. if (mode->hdisplay != fixed_mode->hdisplay && mode->vdisplay != fixed_mode->vdisplay)
  973. return MODE_ONE_SIZE;
  974. else if (mode->hdisplay != fixed_mode->hdisplay)
  975. return MODE_ONE_WIDTH;
  976. else if (mode->vdisplay != fixed_mode->vdisplay)
  977. return MODE_ONE_HEIGHT;
  978. return MODE_OK;
  979. }
  980. EXPORT_SYMBOL(drm_crtc_helper_mode_valid_fixed);
  981. /**
  982. * drm_connector_helper_get_modes_fixed - Duplicates a display mode for a connector
  983. * @connector: the connector
  984. * @fixed_mode: the display hardware's mode
  985. *
  986. * This function duplicates a display modes for a connector. Drivers for hardware
  987. * that only supports a single fixed mode can use this function in their connector's
  988. * get_modes helper.
  989. *
  990. * Returns:
  991. * The number of created modes.
  992. */
  993. int drm_connector_helper_get_modes_fixed(struct drm_connector *connector,
  994. const struct drm_display_mode *fixed_mode)
  995. {
  996. struct drm_device *dev = connector->dev;
  997. struct drm_display_mode *mode;
  998. mode = drm_mode_duplicate(dev, fixed_mode);
  999. if (!mode) {
  1000. drm_err(dev, "Failed to duplicate mode " DRM_MODE_FMT "\n",
  1001. DRM_MODE_ARG(fixed_mode));
  1002. return 0;
  1003. }
  1004. if (mode->name[0] == '\0')
  1005. drm_mode_set_name(mode);
  1006. mode->type |= DRM_MODE_TYPE_PREFERRED;
  1007. drm_mode_probed_add(connector, mode);
  1008. if (mode->width_mm)
  1009. connector->display_info.width_mm = mode->width_mm;
  1010. if (mode->height_mm)
  1011. connector->display_info.height_mm = mode->height_mm;
  1012. return 1;
  1013. }
  1014. EXPORT_SYMBOL(drm_connector_helper_get_modes_fixed);
  1015. /**
  1016. * drm_connector_helper_get_modes - Read EDID and update connector.
  1017. * @connector: The connector
  1018. *
  1019. * Read the EDID using drm_edid_read() (which requires that connector->ddc is
  1020. * set), and update the connector using the EDID.
  1021. *
  1022. * This can be used as the "default" connector helper .get_modes() hook if the
  1023. * driver does not need any special processing. This is sets the example what
  1024. * custom .get_modes() hooks should do regarding EDID read and connector update.
  1025. *
  1026. * Returns: Number of modes.
  1027. */
  1028. int drm_connector_helper_get_modes(struct drm_connector *connector)
  1029. {
  1030. const struct drm_edid *drm_edid;
  1031. int count;
  1032. drm_edid = drm_edid_read(connector);
  1033. /*
  1034. * Unconditionally update the connector. If the EDID was read
  1035. * successfully, fill in the connector information derived from the
  1036. * EDID. Otherwise, if the EDID is NULL, clear the connector
  1037. * information.
  1038. */
  1039. drm_edid_connector_update(connector, drm_edid);
  1040. count = drm_edid_connector_add_modes(connector);
  1041. drm_edid_free(drm_edid);
  1042. return count;
  1043. }
  1044. EXPORT_SYMBOL(drm_connector_helper_get_modes);
  1045. /**
  1046. * drm_connector_helper_tv_get_modes - Fills the modes availables to a TV connector
  1047. * @connector: The connector
  1048. *
  1049. * Fills the available modes for a TV connector based on the supported
  1050. * TV modes, and the default mode expressed by the kernel command line.
  1051. *
  1052. * This can be used as the default TV connector helper .get_modes() hook
  1053. * if the driver does not need any special processing.
  1054. *
  1055. * Returns:
  1056. * The number of modes added to the connector.
  1057. */
  1058. int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
  1059. {
  1060. struct drm_device *dev = connector->dev;
  1061. struct drm_property *tv_mode_property =
  1062. dev->mode_config.tv_mode_property;
  1063. struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
  1064. unsigned int ntsc_modes = BIT(DRM_MODE_TV_MODE_NTSC) |
  1065. BIT(DRM_MODE_TV_MODE_NTSC_443) |
  1066. BIT(DRM_MODE_TV_MODE_NTSC_J) |
  1067. BIT(DRM_MODE_TV_MODE_PAL_M);
  1068. unsigned int pal_modes = BIT(DRM_MODE_TV_MODE_PAL) |
  1069. BIT(DRM_MODE_TV_MODE_PAL_N) |
  1070. BIT(DRM_MODE_TV_MODE_SECAM);
  1071. unsigned int tv_modes[2] = { UINT_MAX, UINT_MAX };
  1072. unsigned int i, supported_tv_modes = 0;
  1073. if (!tv_mode_property)
  1074. return 0;
  1075. for (i = 0; i < tv_mode_property->num_values; i++)
  1076. supported_tv_modes |= BIT(tv_mode_property->values[i]);
  1077. if (((supported_tv_modes & ntsc_modes) &&
  1078. (supported_tv_modes & pal_modes)) ||
  1079. (supported_tv_modes & BIT(DRM_MODE_TV_MODE_MONOCHROME))) {
  1080. uint64_t default_mode;
  1081. if (drm_object_property_get_default_value(&connector->base,
  1082. tv_mode_property,
  1083. &default_mode))
  1084. return 0;
  1085. if (cmdline->tv_mode_specified)
  1086. default_mode = cmdline->tv_mode;
  1087. if (BIT(default_mode) & ntsc_modes) {
  1088. tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
  1089. tv_modes[1] = DRM_MODE_TV_MODE_PAL;
  1090. } else {
  1091. tv_modes[0] = DRM_MODE_TV_MODE_PAL;
  1092. tv_modes[1] = DRM_MODE_TV_MODE_NTSC;
  1093. }
  1094. } else if (supported_tv_modes & ntsc_modes) {
  1095. tv_modes[0] = DRM_MODE_TV_MODE_NTSC;
  1096. } else if (supported_tv_modes & pal_modes) {
  1097. tv_modes[0] = DRM_MODE_TV_MODE_PAL;
  1098. } else {
  1099. return 0;
  1100. }
  1101. for (i = 0; i < ARRAY_SIZE(tv_modes); i++) {
  1102. struct drm_display_mode *mode;
  1103. if (tv_modes[i] == DRM_MODE_TV_MODE_NTSC)
  1104. mode = drm_mode_analog_ntsc_480i(dev);
  1105. else if (tv_modes[i] == DRM_MODE_TV_MODE_PAL)
  1106. mode = drm_mode_analog_pal_576i(dev);
  1107. else
  1108. break;
  1109. if (!mode)
  1110. return i;
  1111. if (!i)
  1112. mode->type |= DRM_MODE_TYPE_PREFERRED;
  1113. drm_mode_probed_add(connector, mode);
  1114. }
  1115. return i;
  1116. }
  1117. EXPORT_SYMBOL(drm_connector_helper_tv_get_modes);
  1118. /**
  1119. * drm_connector_helper_detect_from_ddc - Read EDID and detect connector status.
  1120. * @connector: The connector
  1121. * @ctx: Acquire context
  1122. * @force: Perform screen-destructive operations, if necessary
  1123. *
  1124. * Detects the connector status by reading the EDID using drm_probe_ddc(),
  1125. * which requires connector->ddc to be set. Returns connector_status_connected
  1126. * on success or connector_status_disconnected on failure.
  1127. *
  1128. * Returns:
  1129. * The connector status as defined by enum drm_connector_status.
  1130. */
  1131. int drm_connector_helper_detect_from_ddc(struct drm_connector *connector,
  1132. struct drm_modeset_acquire_ctx *ctx,
  1133. bool force)
  1134. {
  1135. struct i2c_adapter *ddc = connector->ddc;
  1136. if (!ddc)
  1137. return connector_status_unknown;
  1138. if (drm_probe_ddc(ddc))
  1139. return connector_status_connected;
  1140. return connector_status_disconnected;
  1141. }
  1142. EXPORT_SYMBOL(drm_connector_helper_detect_from_ddc);