vc4_hdmi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2013 Red Hat
  5. * Author: Rob Clark <robdclark@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * DOC: VC4 Falcon HDMI module
  21. *
  22. * The HDMI core has a state machine and a PHY. Most of the unit
  23. * operates off of the HSM clock from CPRMAN. It also internally uses
  24. * the PLLH_PIX clock for the PHY.
  25. */
  26. #include "drm_atomic_helper.h"
  27. #include "drm_crtc_helper.h"
  28. #include "drm_edid.h"
  29. #include "linux/clk.h"
  30. #include "linux/component.h"
  31. #include "linux/i2c.h"
  32. #include "linux/of_gpio.h"
  33. #include "linux/of_platform.h"
  34. #include "vc4_drv.h"
  35. #include "vc4_regs.h"
  36. /* General HDMI hardware state. */
  37. struct vc4_hdmi {
  38. struct platform_device *pdev;
  39. struct drm_encoder *encoder;
  40. struct drm_connector *connector;
  41. struct i2c_adapter *ddc;
  42. void __iomem *hdmicore_regs;
  43. void __iomem *hd_regs;
  44. int hpd_gpio;
  45. bool hpd_active_low;
  46. struct clk *pixel_clock;
  47. struct clk *hsm_clock;
  48. };
  49. #define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
  50. #define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
  51. #define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
  52. #define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
  53. /* VC4 HDMI encoder KMS struct */
  54. struct vc4_hdmi_encoder {
  55. struct vc4_encoder base;
  56. bool hdmi_monitor;
  57. bool limited_rgb_range;
  58. bool rgb_range_selectable;
  59. };
  60. static inline struct vc4_hdmi_encoder *
  61. to_vc4_hdmi_encoder(struct drm_encoder *encoder)
  62. {
  63. return container_of(encoder, struct vc4_hdmi_encoder, base.base);
  64. }
  65. /* VC4 HDMI connector KMS struct */
  66. struct vc4_hdmi_connector {
  67. struct drm_connector base;
  68. /* Since the connector is attached to just the one encoder,
  69. * this is the reference to it so we can do the best_encoder()
  70. * hook.
  71. */
  72. struct drm_encoder *encoder;
  73. };
  74. static inline struct vc4_hdmi_connector *
  75. to_vc4_hdmi_connector(struct drm_connector *connector)
  76. {
  77. return container_of(connector, struct vc4_hdmi_connector, base);
  78. }
  79. #define HDMI_REG(reg) { reg, #reg }
  80. static const struct {
  81. u32 reg;
  82. const char *name;
  83. } hdmi_regs[] = {
  84. HDMI_REG(VC4_HDMI_CORE_REV),
  85. HDMI_REG(VC4_HDMI_SW_RESET_CONTROL),
  86. HDMI_REG(VC4_HDMI_HOTPLUG_INT),
  87. HDMI_REG(VC4_HDMI_HOTPLUG),
  88. HDMI_REG(VC4_HDMI_RAM_PACKET_CONFIG),
  89. HDMI_REG(VC4_HDMI_HORZA),
  90. HDMI_REG(VC4_HDMI_HORZB),
  91. HDMI_REG(VC4_HDMI_FIFO_CTL),
  92. HDMI_REG(VC4_HDMI_SCHEDULER_CONTROL),
  93. HDMI_REG(VC4_HDMI_VERTA0),
  94. HDMI_REG(VC4_HDMI_VERTA1),
  95. HDMI_REG(VC4_HDMI_VERTB0),
  96. HDMI_REG(VC4_HDMI_VERTB1),
  97. HDMI_REG(VC4_HDMI_TX_PHY_RESET_CTL),
  98. };
  99. static const struct {
  100. u32 reg;
  101. const char *name;
  102. } hd_regs[] = {
  103. HDMI_REG(VC4_HD_M_CTL),
  104. HDMI_REG(VC4_HD_MAI_CTL),
  105. HDMI_REG(VC4_HD_VID_CTL),
  106. HDMI_REG(VC4_HD_CSC_CTL),
  107. HDMI_REG(VC4_HD_FRAME_COUNT),
  108. };
  109. #ifdef CONFIG_DEBUG_FS
  110. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
  111. {
  112. struct drm_info_node *node = (struct drm_info_node *)m->private;
  113. struct drm_device *dev = node->minor->dev;
  114. struct vc4_dev *vc4 = to_vc4_dev(dev);
  115. int i;
  116. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  117. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  118. hdmi_regs[i].name, hdmi_regs[i].reg,
  119. HDMI_READ(hdmi_regs[i].reg));
  120. }
  121. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  122. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  123. hd_regs[i].name, hd_regs[i].reg,
  124. HD_READ(hd_regs[i].reg));
  125. }
  126. return 0;
  127. }
  128. #endif /* CONFIG_DEBUG_FS */
  129. static void vc4_hdmi_dump_regs(struct drm_device *dev)
  130. {
  131. struct vc4_dev *vc4 = to_vc4_dev(dev);
  132. int i;
  133. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  134. DRM_INFO("0x%04x (%s): 0x%08x\n",
  135. hdmi_regs[i].reg, hdmi_regs[i].name,
  136. HDMI_READ(hdmi_regs[i].reg));
  137. }
  138. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  139. DRM_INFO("0x%04x (%s): 0x%08x\n",
  140. hd_regs[i].reg, hd_regs[i].name,
  141. HD_READ(hd_regs[i].reg));
  142. }
  143. }
  144. static enum drm_connector_status
  145. vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
  146. {
  147. struct drm_device *dev = connector->dev;
  148. struct vc4_dev *vc4 = to_vc4_dev(dev);
  149. if (vc4->hdmi->hpd_gpio) {
  150. if (gpio_get_value_cansleep(vc4->hdmi->hpd_gpio) ^
  151. vc4->hdmi->hpd_active_low)
  152. return connector_status_connected;
  153. else
  154. return connector_status_disconnected;
  155. }
  156. if (drm_probe_ddc(vc4->hdmi->ddc))
  157. return connector_status_connected;
  158. if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
  159. return connector_status_connected;
  160. else
  161. return connector_status_disconnected;
  162. }
  163. static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
  164. {
  165. drm_connector_unregister(connector);
  166. drm_connector_cleanup(connector);
  167. }
  168. static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
  169. {
  170. struct vc4_hdmi_connector *vc4_connector =
  171. to_vc4_hdmi_connector(connector);
  172. struct drm_encoder *encoder = vc4_connector->encoder;
  173. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  174. struct drm_device *dev = connector->dev;
  175. struct vc4_dev *vc4 = to_vc4_dev(dev);
  176. int ret = 0;
  177. struct edid *edid;
  178. edid = drm_get_edid(connector, vc4->hdmi->ddc);
  179. if (!edid)
  180. return -ENODEV;
  181. vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
  182. if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
  183. vc4_encoder->rgb_range_selectable =
  184. drm_rgb_quant_range_selectable(edid);
  185. }
  186. drm_mode_connector_update_edid_property(connector, edid);
  187. ret = drm_add_edid_modes(connector, edid);
  188. return ret;
  189. }
  190. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  191. .dpms = drm_atomic_helper_connector_dpms,
  192. .detect = vc4_hdmi_connector_detect,
  193. .fill_modes = drm_helper_probe_single_connector_modes,
  194. .destroy = vc4_hdmi_connector_destroy,
  195. .reset = drm_atomic_helper_connector_reset,
  196. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  197. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  198. };
  199. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  200. .get_modes = vc4_hdmi_connector_get_modes,
  201. };
  202. static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
  203. struct drm_encoder *encoder)
  204. {
  205. struct drm_connector *connector = NULL;
  206. struct vc4_hdmi_connector *hdmi_connector;
  207. int ret = 0;
  208. hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
  209. GFP_KERNEL);
  210. if (!hdmi_connector) {
  211. ret = -ENOMEM;
  212. goto fail;
  213. }
  214. connector = &hdmi_connector->base;
  215. hdmi_connector->encoder = encoder;
  216. drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
  217. DRM_MODE_CONNECTOR_HDMIA);
  218. drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
  219. connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  220. DRM_CONNECTOR_POLL_DISCONNECT);
  221. connector->interlace_allowed = 1;
  222. connector->doublescan_allowed = 0;
  223. drm_mode_connector_attach_encoder(connector, encoder);
  224. return connector;
  225. fail:
  226. if (connector)
  227. vc4_hdmi_connector_destroy(connector);
  228. return ERR_PTR(ret);
  229. }
  230. static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
  231. {
  232. drm_encoder_cleanup(encoder);
  233. }
  234. static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
  235. .destroy = vc4_hdmi_encoder_destroy,
  236. };
  237. static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
  238. enum hdmi_infoframe_type type)
  239. {
  240. struct drm_device *dev = encoder->dev;
  241. struct vc4_dev *vc4 = to_vc4_dev(dev);
  242. u32 packet_id = type - 0x80;
  243. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  244. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
  245. return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  246. BIT(packet_id)), 100);
  247. }
  248. static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
  249. union hdmi_infoframe *frame)
  250. {
  251. struct drm_device *dev = encoder->dev;
  252. struct vc4_dev *vc4 = to_vc4_dev(dev);
  253. u32 packet_id = frame->any.type - 0x80;
  254. u32 packet_reg = VC4_HDMI_GCP_0 + VC4_HDMI_PACKET_STRIDE * packet_id;
  255. uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
  256. ssize_t len, i;
  257. int ret;
  258. WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  259. VC4_HDMI_RAM_PACKET_ENABLE),
  260. "Packet RAM has to be on to store the packet.");
  261. len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
  262. if (len < 0)
  263. return;
  264. ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
  265. if (ret) {
  266. DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
  267. return;
  268. }
  269. for (i = 0; i < len; i += 7) {
  270. HDMI_WRITE(packet_reg,
  271. buffer[i + 0] << 0 |
  272. buffer[i + 1] << 8 |
  273. buffer[i + 2] << 16);
  274. packet_reg += 4;
  275. HDMI_WRITE(packet_reg,
  276. buffer[i + 3] << 0 |
  277. buffer[i + 4] << 8 |
  278. buffer[i + 5] << 16 |
  279. buffer[i + 6] << 24);
  280. packet_reg += 4;
  281. }
  282. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  283. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
  284. ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  285. BIT(packet_id)), 100);
  286. if (ret)
  287. DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
  288. }
  289. static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
  290. {
  291. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  292. struct drm_crtc *crtc = encoder->crtc;
  293. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  294. union hdmi_infoframe frame;
  295. int ret;
  296. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
  297. if (ret < 0) {
  298. DRM_ERROR("couldn't fill AVI infoframe\n");
  299. return;
  300. }
  301. drm_hdmi_avi_infoframe_quant_range(&frame.avi, mode,
  302. vc4_encoder->limited_rgb_range ?
  303. HDMI_QUANTIZATION_RANGE_LIMITED :
  304. HDMI_QUANTIZATION_RANGE_FULL,
  305. vc4_encoder->rgb_range_selectable);
  306. vc4_hdmi_write_infoframe(encoder, &frame);
  307. }
  308. static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
  309. {
  310. union hdmi_infoframe frame;
  311. int ret;
  312. ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
  313. if (ret < 0) {
  314. DRM_ERROR("couldn't fill SPD infoframe\n");
  315. return;
  316. }
  317. frame.spd.sdi = HDMI_SPD_SDI_PC;
  318. vc4_hdmi_write_infoframe(encoder, &frame);
  319. }
  320. static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
  321. {
  322. vc4_hdmi_set_avi_infoframe(encoder);
  323. vc4_hdmi_set_spd_infoframe(encoder);
  324. }
  325. static void vc4_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  326. struct drm_display_mode *unadjusted_mode,
  327. struct drm_display_mode *mode)
  328. {
  329. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  330. struct drm_device *dev = encoder->dev;
  331. struct vc4_dev *vc4 = to_vc4_dev(dev);
  332. bool debug_dump_regs = false;
  333. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  334. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  335. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  336. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  337. u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  338. VC4_HDMI_VERTA_VSP) |
  339. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  340. VC4_HDMI_VERTA_VFP) |
  341. VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
  342. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  343. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
  344. VC4_HDMI_VERTB_VBP));
  345. u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  346. VC4_SET_FIELD(mode->crtc_vtotal -
  347. mode->crtc_vsync_end -
  348. interlaced,
  349. VC4_HDMI_VERTB_VBP));
  350. u32 csc_ctl;
  351. if (debug_dump_regs) {
  352. DRM_INFO("HDMI regs before:\n");
  353. vc4_hdmi_dump_regs(dev);
  354. }
  355. HD_WRITE(VC4_HD_VID_CTL, 0);
  356. clk_set_rate(vc4->hdmi->pixel_clock, mode->clock * 1000 *
  357. ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
  358. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  359. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  360. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  361. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  362. HDMI_WRITE(VC4_HDMI_HORZA,
  363. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  364. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  365. VC4_SET_FIELD(mode->hdisplay * pixel_rep,
  366. VC4_HDMI_HORZA_HAP));
  367. HDMI_WRITE(VC4_HDMI_HORZB,
  368. VC4_SET_FIELD((mode->htotal -
  369. mode->hsync_end) * pixel_rep,
  370. VC4_HDMI_HORZB_HBP) |
  371. VC4_SET_FIELD((mode->hsync_end -
  372. mode->hsync_start) * pixel_rep,
  373. VC4_HDMI_HORZB_HSP) |
  374. VC4_SET_FIELD((mode->hsync_start -
  375. mode->hdisplay) * pixel_rep,
  376. VC4_HDMI_HORZB_HFP));
  377. HDMI_WRITE(VC4_HDMI_VERTA0, verta);
  378. HDMI_WRITE(VC4_HDMI_VERTA1, verta);
  379. HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
  380. HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
  381. HD_WRITE(VC4_HD_VID_CTL,
  382. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  383. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  384. csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  385. VC4_HD_CSC_CTL_ORDER);
  386. if (vc4_encoder->hdmi_monitor &&
  387. drm_default_rgb_quant_range(mode) ==
  388. HDMI_QUANTIZATION_RANGE_LIMITED) {
  389. /* CEA VICs other than #1 requre limited range RGB
  390. * output unless overridden by an AVI infoframe.
  391. * Apply a colorspace conversion to squash 0-255 down
  392. * to 16-235. The matrix here is:
  393. *
  394. * [ 0 0 0.8594 16]
  395. * [ 0 0.8594 0 16]
  396. * [ 0.8594 0 0 16]
  397. * [ 0 0 0 1]
  398. */
  399. csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
  400. csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
  401. csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
  402. VC4_HD_CSC_CTL_MODE);
  403. HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
  404. HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
  405. HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
  406. HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
  407. HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
  408. HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
  409. vc4_encoder->limited_rgb_range = true;
  410. } else {
  411. vc4_encoder->limited_rgb_range = false;
  412. }
  413. /* The RGB order applies even when CSC is disabled. */
  414. HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
  415. HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  416. if (debug_dump_regs) {
  417. DRM_INFO("HDMI regs after:\n");
  418. vc4_hdmi_dump_regs(dev);
  419. }
  420. }
  421. static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
  422. {
  423. struct drm_device *dev = encoder->dev;
  424. struct vc4_dev *vc4 = to_vc4_dev(dev);
  425. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
  426. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  427. HD_WRITE(VC4_HD_VID_CTL,
  428. HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  429. }
  430. static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
  431. {
  432. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  433. struct drm_device *dev = encoder->dev;
  434. struct vc4_dev *vc4 = to_vc4_dev(dev);
  435. int ret;
  436. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
  437. HD_WRITE(VC4_HD_VID_CTL,
  438. HD_READ(VC4_HD_VID_CTL) |
  439. VC4_HD_VID_CTL_ENABLE |
  440. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  441. VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
  442. if (vc4_encoder->hdmi_monitor) {
  443. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  444. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  445. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  446. ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  447. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
  448. WARN_ONCE(ret, "Timeout waiting for "
  449. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  450. } else {
  451. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  452. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  453. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  454. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  455. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  456. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  457. ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  458. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
  459. WARN_ONCE(ret, "Timeout waiting for "
  460. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  461. }
  462. if (vc4_encoder->hdmi_monitor) {
  463. u32 drift;
  464. WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  465. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  466. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  467. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  468. VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
  469. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  470. VC4_HDMI_RAM_PACKET_ENABLE);
  471. vc4_hdmi_set_infoframes(encoder);
  472. drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
  473. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  474. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  475. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  476. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  477. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  478. udelay(1000);
  479. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  480. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  481. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  482. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  483. ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
  484. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  485. WARN_ONCE(ret, "Timeout waiting for "
  486. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  487. }
  488. }
  489. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  490. .mode_set = vc4_hdmi_encoder_mode_set,
  491. .disable = vc4_hdmi_encoder_disable,
  492. .enable = vc4_hdmi_encoder_enable,
  493. };
  494. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  495. {
  496. struct platform_device *pdev = to_platform_device(dev);
  497. struct drm_device *drm = dev_get_drvdata(master);
  498. struct vc4_dev *vc4 = drm->dev_private;
  499. struct vc4_hdmi *hdmi;
  500. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  501. struct device_node *ddc_node;
  502. u32 value;
  503. int ret;
  504. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  505. if (!hdmi)
  506. return -ENOMEM;
  507. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  508. GFP_KERNEL);
  509. if (!vc4_hdmi_encoder)
  510. return -ENOMEM;
  511. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  512. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  513. hdmi->pdev = pdev;
  514. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  515. if (IS_ERR(hdmi->hdmicore_regs))
  516. return PTR_ERR(hdmi->hdmicore_regs);
  517. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  518. if (IS_ERR(hdmi->hd_regs))
  519. return PTR_ERR(hdmi->hd_regs);
  520. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  521. if (IS_ERR(hdmi->pixel_clock)) {
  522. DRM_ERROR("Failed to get pixel clock\n");
  523. return PTR_ERR(hdmi->pixel_clock);
  524. }
  525. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  526. if (IS_ERR(hdmi->hsm_clock)) {
  527. DRM_ERROR("Failed to get HDMI state machine clock\n");
  528. return PTR_ERR(hdmi->hsm_clock);
  529. }
  530. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  531. if (!ddc_node) {
  532. DRM_ERROR("Failed to find ddc node in device tree\n");
  533. return -ENODEV;
  534. }
  535. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  536. of_node_put(ddc_node);
  537. if (!hdmi->ddc) {
  538. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  539. return -EPROBE_DEFER;
  540. }
  541. /* Enable the clocks at startup. We can't quite recover from
  542. * turning off the pixel clock during disable/enables yet, so
  543. * it's always running.
  544. */
  545. ret = clk_prepare_enable(hdmi->pixel_clock);
  546. if (ret) {
  547. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  548. goto err_put_i2c;
  549. }
  550. /* This is the rate that is set by the firmware. The number
  551. * needs to be a bit higher than the pixel clock rate
  552. * (generally 148.5Mhz).
  553. */
  554. ret = clk_set_rate(hdmi->hsm_clock, 163682864);
  555. if (ret) {
  556. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  557. goto err_unprepare_pix;
  558. }
  559. ret = clk_prepare_enable(hdmi->hsm_clock);
  560. if (ret) {
  561. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  562. ret);
  563. goto err_unprepare_pix;
  564. }
  565. /* Only use the GPIO HPD pin if present in the DT, otherwise
  566. * we'll use the HDMI core's register.
  567. */
  568. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  569. enum of_gpio_flags hpd_gpio_flags;
  570. hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
  571. "hpd-gpios", 0,
  572. &hpd_gpio_flags);
  573. if (hdmi->hpd_gpio < 0) {
  574. ret = hdmi->hpd_gpio;
  575. goto err_unprepare_hsm;
  576. }
  577. hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
  578. }
  579. vc4->hdmi = hdmi;
  580. /* HDMI core must be enabled. */
  581. if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
  582. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
  583. udelay(1);
  584. HD_WRITE(VC4_HD_M_CTL, 0);
  585. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
  586. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
  587. VC4_HDMI_SW_RESET_HDMI |
  588. VC4_HDMI_SW_RESET_FORMAT_DETECT);
  589. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
  590. /* PHY should be in reset, like
  591. * vc4_hdmi_encoder_disable() does.
  592. */
  593. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  594. }
  595. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  596. DRM_MODE_ENCODER_TMDS, NULL);
  597. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  598. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  599. if (IS_ERR(hdmi->connector)) {
  600. ret = PTR_ERR(hdmi->connector);
  601. goto err_destroy_encoder;
  602. }
  603. return 0;
  604. err_destroy_encoder:
  605. vc4_hdmi_encoder_destroy(hdmi->encoder);
  606. err_unprepare_hsm:
  607. clk_disable_unprepare(hdmi->hsm_clock);
  608. err_unprepare_pix:
  609. clk_disable_unprepare(hdmi->pixel_clock);
  610. err_put_i2c:
  611. put_device(&hdmi->ddc->dev);
  612. return ret;
  613. }
  614. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  615. void *data)
  616. {
  617. struct drm_device *drm = dev_get_drvdata(master);
  618. struct vc4_dev *vc4 = drm->dev_private;
  619. struct vc4_hdmi *hdmi = vc4->hdmi;
  620. vc4_hdmi_connector_destroy(hdmi->connector);
  621. vc4_hdmi_encoder_destroy(hdmi->encoder);
  622. clk_disable_unprepare(hdmi->pixel_clock);
  623. clk_disable_unprepare(hdmi->hsm_clock);
  624. put_device(&hdmi->ddc->dev);
  625. vc4->hdmi = NULL;
  626. }
  627. static const struct component_ops vc4_hdmi_ops = {
  628. .bind = vc4_hdmi_bind,
  629. .unbind = vc4_hdmi_unbind,
  630. };
  631. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  632. {
  633. return component_add(&pdev->dev, &vc4_hdmi_ops);
  634. }
  635. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  636. {
  637. component_del(&pdev->dev, &vc4_hdmi_ops);
  638. return 0;
  639. }
  640. static const struct of_device_id vc4_hdmi_dt_match[] = {
  641. { .compatible = "brcm,bcm2835-hdmi" },
  642. {}
  643. };
  644. struct platform_driver vc4_hdmi_driver = {
  645. .probe = vc4_hdmi_dev_probe,
  646. .remove = vc4_hdmi_dev_remove,
  647. .driver = {
  648. .name = "vc4_hdmi",
  649. .of_match_table = vc4_hdmi_dt_match,
  650. },
  651. };