drm_probe_helper.c 40 KB

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