drm_hdmi_state_helper.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // SPDX-License-Identifier: MIT
  2. #include <drm/drm_atomic.h>
  3. #include <drm/drm_connector.h>
  4. #include <drm/drm_edid.h>
  5. #include <drm/drm_print.h>
  6. #include <drm/display/drm_hdmi_helper.h>
  7. #include <drm/display/drm_hdmi_state_helper.h>
  8. /**
  9. * __drm_atomic_helper_connector_hdmi_reset() - Initializes all HDMI @drm_connector_state resources
  10. * @connector: DRM connector
  11. * @new_conn_state: connector state to reset
  12. *
  13. * Initializes all HDMI resources from a @drm_connector_state without
  14. * actually allocating it. This is useful for HDMI drivers, in
  15. * combination with __drm_atomic_helper_connector_reset() or
  16. * drm_atomic_helper_connector_reset().
  17. */
  18. void __drm_atomic_helper_connector_hdmi_reset(struct drm_connector *connector,
  19. struct drm_connector_state *new_conn_state)
  20. {
  21. unsigned int max_bpc = connector->max_bpc;
  22. new_conn_state->max_bpc = max_bpc;
  23. new_conn_state->max_requested_bpc = max_bpc;
  24. new_conn_state->hdmi.broadcast_rgb = DRM_HDMI_BROADCAST_RGB_AUTO;
  25. }
  26. EXPORT_SYMBOL(__drm_atomic_helper_connector_hdmi_reset);
  27. static const struct drm_display_mode *
  28. connector_state_get_mode(const struct drm_connector_state *conn_state)
  29. {
  30. struct drm_atomic_state *state;
  31. struct drm_crtc_state *crtc_state;
  32. struct drm_crtc *crtc;
  33. state = conn_state->state;
  34. if (!state)
  35. return NULL;
  36. crtc = conn_state->crtc;
  37. if (!crtc)
  38. return NULL;
  39. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  40. if (!crtc_state)
  41. return NULL;
  42. return &crtc_state->mode;
  43. }
  44. static bool hdmi_is_limited_range(const struct drm_connector *connector,
  45. const struct drm_connector_state *conn_state)
  46. {
  47. const struct drm_display_info *info = &connector->display_info;
  48. const struct drm_display_mode *mode =
  49. connector_state_get_mode(conn_state);
  50. /*
  51. * The Broadcast RGB property only applies to RGB format, and
  52. * i915 just assumes limited range for YCbCr output, so let's
  53. * just do the same.
  54. */
  55. if (conn_state->hdmi.output_format != HDMI_COLORSPACE_RGB)
  56. return true;
  57. if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_FULL)
  58. return false;
  59. if (conn_state->hdmi.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_LIMITED)
  60. return true;
  61. if (!info->is_hdmi)
  62. return false;
  63. return drm_default_rgb_quant_range(mode) == HDMI_QUANTIZATION_RANGE_LIMITED;
  64. }
  65. static bool
  66. sink_supports_format_bpc(const struct drm_connector *connector,
  67. const struct drm_display_info *info,
  68. const struct drm_display_mode *mode,
  69. unsigned int format, unsigned int bpc)
  70. {
  71. struct drm_device *dev = connector->dev;
  72. u8 vic = drm_match_cea_mode(mode);
  73. /*
  74. * CTA-861-F, section 5.4 - Color Coding & Quantization states
  75. * that the bpc must be 8, 10, 12 or 16 except for the default
  76. * 640x480 VIC1 where the value must be 8.
  77. *
  78. * The definition of default here is ambiguous but the spec
  79. * refers to VIC1 being the default timing in several occasions
  80. * so our understanding is that for the default timing (ie,
  81. * VIC1), the bpc must be 8.
  82. */
  83. if (vic == 1 && bpc != 8) {
  84. drm_dbg_kms(dev, "VIC1 requires a bpc of 8, got %u\n", bpc);
  85. return false;
  86. }
  87. if (!info->is_hdmi &&
  88. (format != HDMI_COLORSPACE_RGB || bpc != 8)) {
  89. drm_dbg_kms(dev, "DVI Monitors require an RGB output at 8 bpc\n");
  90. return false;
  91. }
  92. if (!(connector->hdmi.supported_formats & BIT(format))) {
  93. drm_dbg_kms(dev, "%s format unsupported by the connector.\n",
  94. drm_hdmi_connector_get_output_format_name(format));
  95. return false;
  96. }
  97. switch (format) {
  98. case HDMI_COLORSPACE_RGB:
  99. drm_dbg_kms(dev, "RGB Format, checking the constraints.\n");
  100. /*
  101. * In some cases, like when the EDID readout fails, or
  102. * is not an HDMI compliant EDID for some reason, the
  103. * color_formats field will be blank and not report any
  104. * format supported. In such a case, assume that RGB is
  105. * supported so we can keep things going and light up
  106. * the display.
  107. */
  108. if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444))
  109. drm_warn(dev, "HDMI Sink doesn't support RGB, something's wrong.\n");
  110. if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) {
  111. drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
  112. return false;
  113. }
  114. if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) {
  115. drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
  116. return false;
  117. }
  118. drm_dbg_kms(dev, "RGB format supported in that configuration.\n");
  119. return true;
  120. case HDMI_COLORSPACE_YUV420:
  121. /* TODO: YUV420 is unsupported at the moment. */
  122. drm_dbg_kms(dev, "YUV420 format isn't supported yet.\n");
  123. return false;
  124. case HDMI_COLORSPACE_YUV422:
  125. drm_dbg_kms(dev, "YUV422 format, checking the constraints.\n");
  126. if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) {
  127. drm_dbg_kms(dev, "Sink doesn't support YUV422.\n");
  128. return false;
  129. }
  130. if (bpc > 12) {
  131. drm_dbg_kms(dev, "YUV422 only supports 12 bpc or lower.\n");
  132. return false;
  133. }
  134. /*
  135. * HDMI Spec 1.3 - Section 6.5 Pixel Encodings and Color Depth
  136. * states that Deep Color is not relevant for YUV422 so we
  137. * don't need to check the Deep Color bits in the EDIDs here.
  138. */
  139. drm_dbg_kms(dev, "YUV422 format supported in that configuration.\n");
  140. return true;
  141. case HDMI_COLORSPACE_YUV444:
  142. drm_dbg_kms(dev, "YUV444 format, checking the constraints.\n");
  143. if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) {
  144. drm_dbg_kms(dev, "Sink doesn't support YUV444.\n");
  145. return false;
  146. }
  147. if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) {
  148. drm_dbg_kms(dev, "10 BPC but sink doesn't support Deep Color 30.\n");
  149. return false;
  150. }
  151. if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) {
  152. drm_dbg_kms(dev, "12 BPC but sink doesn't support Deep Color 36.\n");
  153. return false;
  154. }
  155. drm_dbg_kms(dev, "YUV444 format supported in that configuration.\n");
  156. return true;
  157. }
  158. drm_dbg_kms(dev, "Unsupported pixel format.\n");
  159. return false;
  160. }
  161. static enum drm_mode_status
  162. hdmi_clock_valid(const struct drm_connector *connector,
  163. const struct drm_display_mode *mode,
  164. unsigned long long clock)
  165. {
  166. const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
  167. const struct drm_display_info *info = &connector->display_info;
  168. if (info->max_tmds_clock && clock > info->max_tmds_clock * 1000)
  169. return MODE_CLOCK_HIGH;
  170. if (funcs && funcs->tmds_char_rate_valid) {
  171. enum drm_mode_status status;
  172. status = funcs->tmds_char_rate_valid(connector, mode, clock);
  173. if (status != MODE_OK)
  174. return status;
  175. }
  176. return MODE_OK;
  177. }
  178. static int
  179. hdmi_compute_clock(const struct drm_connector *connector,
  180. struct drm_connector_state *conn_state,
  181. const struct drm_display_mode *mode,
  182. unsigned int bpc, enum hdmi_colorspace fmt)
  183. {
  184. enum drm_mode_status status;
  185. unsigned long long clock;
  186. clock = drm_hdmi_compute_mode_clock(mode, bpc, fmt);
  187. if (!clock)
  188. return -EINVAL;
  189. status = hdmi_clock_valid(connector, mode, clock);
  190. if (status != MODE_OK)
  191. return -EINVAL;
  192. conn_state->hdmi.tmds_char_rate = clock;
  193. return 0;
  194. }
  195. static bool
  196. hdmi_try_format_bpc(const struct drm_connector *connector,
  197. struct drm_connector_state *conn_state,
  198. const struct drm_display_mode *mode,
  199. unsigned int bpc, enum hdmi_colorspace fmt)
  200. {
  201. const struct drm_display_info *info = &connector->display_info;
  202. struct drm_device *dev = connector->dev;
  203. int ret;
  204. drm_dbg_kms(dev, "Trying %s output format\n",
  205. drm_hdmi_connector_get_output_format_name(fmt));
  206. if (!sink_supports_format_bpc(connector, info, mode, fmt, bpc)) {
  207. drm_dbg_kms(dev, "%s output format not supported with %u bpc\n",
  208. drm_hdmi_connector_get_output_format_name(fmt),
  209. bpc);
  210. return false;
  211. }
  212. ret = hdmi_compute_clock(connector, conn_state, mode, bpc, fmt);
  213. if (ret) {
  214. drm_dbg_kms(dev, "Couldn't compute clock for %s output format and %u bpc\n",
  215. drm_hdmi_connector_get_output_format_name(fmt),
  216. bpc);
  217. return false;
  218. }
  219. drm_dbg_kms(dev, "%s output format supported with %u (TMDS char rate: %llu Hz)\n",
  220. drm_hdmi_connector_get_output_format_name(fmt),
  221. bpc, conn_state->hdmi.tmds_char_rate);
  222. return true;
  223. }
  224. static int
  225. hdmi_compute_format(const struct drm_connector *connector,
  226. struct drm_connector_state *conn_state,
  227. const struct drm_display_mode *mode,
  228. unsigned int bpc)
  229. {
  230. struct drm_device *dev = connector->dev;
  231. /*
  232. * TODO: Add support for YCbCr420 output for HDMI 2.0 capable
  233. * devices, for modes that only support YCbCr420.
  234. */
  235. if (hdmi_try_format_bpc(connector, conn_state, mode, bpc, HDMI_COLORSPACE_RGB)) {
  236. conn_state->hdmi.output_format = HDMI_COLORSPACE_RGB;
  237. return 0;
  238. }
  239. drm_dbg_kms(dev, "Failed. No Format Supported for that bpc count.\n");
  240. return -EINVAL;
  241. }
  242. static int
  243. hdmi_compute_config(const struct drm_connector *connector,
  244. struct drm_connector_state *conn_state,
  245. const struct drm_display_mode *mode)
  246. {
  247. struct drm_device *dev = connector->dev;
  248. unsigned int max_bpc = clamp_t(unsigned int,
  249. conn_state->max_bpc,
  250. 8, connector->max_bpc);
  251. unsigned int bpc;
  252. int ret;
  253. for (bpc = max_bpc; bpc >= 8; bpc -= 2) {
  254. drm_dbg_kms(dev, "Trying with a %d bpc output\n", bpc);
  255. ret = hdmi_compute_format(connector, conn_state, mode, bpc);
  256. if (ret)
  257. continue;
  258. conn_state->hdmi.output_bpc = bpc;
  259. drm_dbg_kms(dev,
  260. "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n",
  261. mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode),
  262. conn_state->hdmi.output_bpc,
  263. drm_hdmi_connector_get_output_format_name(conn_state->hdmi.output_format),
  264. conn_state->hdmi.tmds_char_rate);
  265. return 0;
  266. }
  267. return -EINVAL;
  268. }
  269. static int hdmi_generate_avi_infoframe(const struct drm_connector *connector,
  270. struct drm_connector_state *conn_state)
  271. {
  272. const struct drm_display_mode *mode =
  273. connector_state_get_mode(conn_state);
  274. struct drm_connector_hdmi_infoframe *infoframe =
  275. &conn_state->hdmi.infoframes.avi;
  276. struct hdmi_avi_infoframe *frame =
  277. &infoframe->data.avi;
  278. bool is_limited_range = conn_state->hdmi.is_limited_range;
  279. enum hdmi_quantization_range rgb_quant_range =
  280. is_limited_range ? HDMI_QUANTIZATION_RANGE_LIMITED : HDMI_QUANTIZATION_RANGE_FULL;
  281. int ret;
  282. infoframe->set = false;
  283. ret = drm_hdmi_avi_infoframe_from_display_mode(frame, connector, mode);
  284. if (ret)
  285. return ret;
  286. frame->colorspace = conn_state->hdmi.output_format;
  287. /*
  288. * FIXME: drm_hdmi_avi_infoframe_quant_range() doesn't handle
  289. * YUV formats at all at the moment, so if we ever support YUV
  290. * formats this needs to be revised.
  291. */
  292. drm_hdmi_avi_infoframe_quant_range(frame, connector, mode, rgb_quant_range);
  293. drm_hdmi_avi_infoframe_colorimetry(frame, conn_state);
  294. drm_hdmi_avi_infoframe_bars(frame, conn_state);
  295. infoframe->set = true;
  296. return 0;
  297. }
  298. static int hdmi_generate_spd_infoframe(const struct drm_connector *connector,
  299. struct drm_connector_state *conn_state)
  300. {
  301. struct drm_connector_hdmi_infoframe *infoframe =
  302. &conn_state->hdmi.infoframes.spd;
  303. struct hdmi_spd_infoframe *frame =
  304. &infoframe->data.spd;
  305. int ret;
  306. infoframe->set = false;
  307. ret = hdmi_spd_infoframe_init(frame,
  308. connector->hdmi.vendor,
  309. connector->hdmi.product);
  310. if (ret)
  311. return ret;
  312. frame->sdi = HDMI_SPD_SDI_PC;
  313. infoframe->set = true;
  314. return 0;
  315. }
  316. static int hdmi_generate_hdr_infoframe(const struct drm_connector *connector,
  317. struct drm_connector_state *conn_state)
  318. {
  319. struct drm_connector_hdmi_infoframe *infoframe =
  320. &conn_state->hdmi.infoframes.hdr_drm;
  321. struct hdmi_drm_infoframe *frame =
  322. &infoframe->data.drm;
  323. int ret;
  324. infoframe->set = false;
  325. if (connector->max_bpc < 10)
  326. return 0;
  327. if (!conn_state->hdr_output_metadata)
  328. return 0;
  329. ret = drm_hdmi_infoframe_set_hdr_metadata(frame, conn_state);
  330. if (ret)
  331. return ret;
  332. infoframe->set = true;
  333. return 0;
  334. }
  335. static int hdmi_generate_hdmi_vendor_infoframe(const struct drm_connector *connector,
  336. struct drm_connector_state *conn_state)
  337. {
  338. const struct drm_display_info *info = &connector->display_info;
  339. const struct drm_display_mode *mode =
  340. connector_state_get_mode(conn_state);
  341. struct drm_connector_hdmi_infoframe *infoframe =
  342. &conn_state->hdmi.infoframes.hdmi;
  343. struct hdmi_vendor_infoframe *frame =
  344. &infoframe->data.vendor.hdmi;
  345. int ret;
  346. infoframe->set = false;
  347. if (!info->has_hdmi_infoframe)
  348. return 0;
  349. ret = drm_hdmi_vendor_infoframe_from_display_mode(frame, connector, mode);
  350. if (ret)
  351. return ret;
  352. infoframe->set = true;
  353. return 0;
  354. }
  355. static int
  356. hdmi_generate_infoframes(const struct drm_connector *connector,
  357. struct drm_connector_state *conn_state)
  358. {
  359. const struct drm_display_info *info = &connector->display_info;
  360. int ret;
  361. if (!info->is_hdmi)
  362. return 0;
  363. ret = hdmi_generate_avi_infoframe(connector, conn_state);
  364. if (ret)
  365. return ret;
  366. ret = hdmi_generate_spd_infoframe(connector, conn_state);
  367. if (ret)
  368. return ret;
  369. /*
  370. * Audio Infoframes will be generated by ALSA, and updated by
  371. * drm_atomic_helper_connector_hdmi_update_audio_infoframe().
  372. */
  373. ret = hdmi_generate_hdr_infoframe(connector, conn_state);
  374. if (ret)
  375. return ret;
  376. ret = hdmi_generate_hdmi_vendor_infoframe(connector, conn_state);
  377. if (ret)
  378. return ret;
  379. return 0;
  380. }
  381. /**
  382. * drm_atomic_helper_connector_hdmi_check() - Helper to check HDMI connector atomic state
  383. * @connector: DRM Connector
  384. * @state: the DRM State object
  385. *
  386. * Provides a default connector state check handler for HDMI connectors.
  387. * Checks that a desired connector update is valid, and updates various
  388. * fields of derived state.
  389. *
  390. * RETURNS:
  391. * Zero on success, or an errno code otherwise.
  392. */
  393. int drm_atomic_helper_connector_hdmi_check(struct drm_connector *connector,
  394. struct drm_atomic_state *state)
  395. {
  396. struct drm_connector_state *old_conn_state =
  397. drm_atomic_get_old_connector_state(state, connector);
  398. struct drm_connector_state *new_conn_state =
  399. drm_atomic_get_new_connector_state(state, connector);
  400. const struct drm_display_mode *mode =
  401. connector_state_get_mode(new_conn_state);
  402. int ret;
  403. new_conn_state->hdmi.is_limited_range = hdmi_is_limited_range(connector, new_conn_state);
  404. ret = hdmi_compute_config(connector, new_conn_state, mode);
  405. if (ret)
  406. return ret;
  407. ret = hdmi_generate_infoframes(connector, new_conn_state);
  408. if (ret)
  409. return ret;
  410. if (old_conn_state->hdmi.broadcast_rgb != new_conn_state->hdmi.broadcast_rgb ||
  411. old_conn_state->hdmi.output_bpc != new_conn_state->hdmi.output_bpc ||
  412. old_conn_state->hdmi.output_format != new_conn_state->hdmi.output_format) {
  413. struct drm_crtc *crtc = new_conn_state->crtc;
  414. struct drm_crtc_state *crtc_state;
  415. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  416. if (IS_ERR(crtc_state))
  417. return PTR_ERR(crtc_state);
  418. crtc_state->mode_changed = true;
  419. }
  420. return 0;
  421. }
  422. EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_check);
  423. static int clear_device_infoframe(struct drm_connector *connector,
  424. enum hdmi_infoframe_type type)
  425. {
  426. const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
  427. struct drm_device *dev = connector->dev;
  428. int ret;
  429. drm_dbg_kms(dev, "Clearing infoframe type 0x%x\n", type);
  430. if (!funcs || !funcs->clear_infoframe) {
  431. drm_dbg_kms(dev, "Function not implemented, bailing.\n");
  432. return 0;
  433. }
  434. ret = funcs->clear_infoframe(connector, type);
  435. if (ret) {
  436. drm_dbg_kms(dev, "Call failed: %d\n", ret);
  437. return ret;
  438. }
  439. return 0;
  440. }
  441. static int clear_infoframe(struct drm_connector *connector,
  442. struct drm_connector_hdmi_infoframe *old_frame)
  443. {
  444. int ret;
  445. ret = clear_device_infoframe(connector, old_frame->data.any.type);
  446. if (ret)
  447. return ret;
  448. return 0;
  449. }
  450. static int write_device_infoframe(struct drm_connector *connector,
  451. union hdmi_infoframe *frame)
  452. {
  453. const struct drm_connector_hdmi_funcs *funcs = connector->hdmi.funcs;
  454. struct drm_device *dev = connector->dev;
  455. u8 buffer[HDMI_INFOFRAME_SIZE(MAX)];
  456. int ret;
  457. int len;
  458. drm_dbg_kms(dev, "Writing infoframe type %x\n", frame->any.type);
  459. if (!funcs || !funcs->write_infoframe) {
  460. drm_dbg_kms(dev, "Function not implemented, bailing.\n");
  461. return -EINVAL;
  462. }
  463. len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
  464. if (len < 0)
  465. return len;
  466. ret = funcs->write_infoframe(connector, frame->any.type, buffer, len);
  467. if (ret) {
  468. drm_dbg_kms(dev, "Call failed: %d\n", ret);
  469. return ret;
  470. }
  471. return 0;
  472. }
  473. static int write_infoframe(struct drm_connector *connector,
  474. struct drm_connector_hdmi_infoframe *new_frame)
  475. {
  476. int ret;
  477. ret = write_device_infoframe(connector, &new_frame->data);
  478. if (ret)
  479. return ret;
  480. return 0;
  481. }
  482. static int write_or_clear_infoframe(struct drm_connector *connector,
  483. struct drm_connector_hdmi_infoframe *old_frame,
  484. struct drm_connector_hdmi_infoframe *new_frame)
  485. {
  486. if (new_frame->set)
  487. return write_infoframe(connector, new_frame);
  488. if (old_frame->set && !new_frame->set)
  489. return clear_infoframe(connector, old_frame);
  490. return 0;
  491. }
  492. /**
  493. * drm_atomic_helper_connector_hdmi_update_infoframes - Update the Infoframes
  494. * @connector: A pointer to the HDMI connector
  495. * @state: The HDMI connector state to generate the infoframe from
  496. *
  497. * This function is meant for HDMI connector drivers to write their
  498. * infoframes. It will typically be used in a
  499. * @drm_connector_helper_funcs.atomic_enable implementation.
  500. *
  501. * Returns:
  502. * Zero on success, error code on failure.
  503. */
  504. int drm_atomic_helper_connector_hdmi_update_infoframes(struct drm_connector *connector,
  505. struct drm_atomic_state *state)
  506. {
  507. struct drm_connector_state *old_conn_state =
  508. drm_atomic_get_old_connector_state(state, connector);
  509. struct drm_connector_state *new_conn_state =
  510. drm_atomic_get_new_connector_state(state, connector);
  511. struct drm_display_info *info = &connector->display_info;
  512. int ret;
  513. if (!info->is_hdmi)
  514. return 0;
  515. mutex_lock(&connector->hdmi.infoframes.lock);
  516. ret = write_or_clear_infoframe(connector,
  517. &old_conn_state->hdmi.infoframes.avi,
  518. &new_conn_state->hdmi.infoframes.avi);
  519. if (ret)
  520. goto out;
  521. if (connector->hdmi.infoframes.audio.set) {
  522. ret = write_infoframe(connector,
  523. &connector->hdmi.infoframes.audio);
  524. if (ret)
  525. goto out;
  526. }
  527. ret = write_or_clear_infoframe(connector,
  528. &old_conn_state->hdmi.infoframes.hdr_drm,
  529. &new_conn_state->hdmi.infoframes.hdr_drm);
  530. if (ret)
  531. goto out;
  532. ret = write_or_clear_infoframe(connector,
  533. &old_conn_state->hdmi.infoframes.spd,
  534. &new_conn_state->hdmi.infoframes.spd);
  535. if (ret)
  536. goto out;
  537. if (info->has_hdmi_infoframe) {
  538. ret = write_or_clear_infoframe(connector,
  539. &old_conn_state->hdmi.infoframes.hdmi,
  540. &new_conn_state->hdmi.infoframes.hdmi);
  541. if (ret)
  542. goto out;
  543. }
  544. out:
  545. mutex_unlock(&connector->hdmi.infoframes.lock);
  546. return ret;
  547. }
  548. EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_infoframes);
  549. /**
  550. * drm_atomic_helper_connector_hdmi_update_audio_infoframe - Update the Audio Infoframe
  551. * @connector: A pointer to the HDMI connector
  552. * @frame: A pointer to the audio infoframe to write
  553. *
  554. * This function is meant for HDMI connector drivers to update their
  555. * audio infoframe. It will typically be used in one of the ALSA hooks
  556. * (most likely prepare).
  557. *
  558. * Returns:
  559. * Zero on success, error code on failure.
  560. */
  561. int
  562. drm_atomic_helper_connector_hdmi_update_audio_infoframe(struct drm_connector *connector,
  563. struct hdmi_audio_infoframe *frame)
  564. {
  565. struct drm_connector_hdmi_infoframe *infoframe =
  566. &connector->hdmi.infoframes.audio;
  567. struct drm_display_info *info = &connector->display_info;
  568. int ret;
  569. if (!info->is_hdmi)
  570. return 0;
  571. mutex_lock(&connector->hdmi.infoframes.lock);
  572. memcpy(&infoframe->data, frame, sizeof(infoframe->data));
  573. infoframe->set = true;
  574. ret = write_infoframe(connector, infoframe);
  575. mutex_unlock(&connector->hdmi.infoframes.lock);
  576. return ret;
  577. }
  578. EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_update_audio_infoframe);
  579. /**
  580. * drm_atomic_helper_connector_hdmi_clear_audio_infoframe - Stop sending the Audio Infoframe
  581. * @connector: A pointer to the HDMI connector
  582. *
  583. * This function is meant for HDMI connector drivers to stop sending their
  584. * audio infoframe. It will typically be used in one of the ALSA hooks
  585. * (most likely shutdown).
  586. *
  587. * Returns:
  588. * Zero on success, error code on failure.
  589. */
  590. int
  591. drm_atomic_helper_connector_hdmi_clear_audio_infoframe(struct drm_connector *connector)
  592. {
  593. struct drm_connector_hdmi_infoframe *infoframe =
  594. &connector->hdmi.infoframes.audio;
  595. struct drm_display_info *info = &connector->display_info;
  596. int ret;
  597. if (!info->is_hdmi)
  598. return 0;
  599. mutex_lock(&connector->hdmi.infoframes.lock);
  600. infoframe->set = false;
  601. ret = clear_infoframe(connector, infoframe);
  602. memset(&infoframe->data, 0, sizeof(infoframe->data));
  603. mutex_unlock(&connector->hdmi.infoframes.lock);
  604. return ret;
  605. }
  606. EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_clear_audio_infoframe);