vc4_hdmi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. struct clk *pixel_clock;
  46. struct clk *hsm_clock;
  47. };
  48. #define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
  49. #define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
  50. #define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
  51. #define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
  52. /* VC4 HDMI encoder KMS struct */
  53. struct vc4_hdmi_encoder {
  54. struct vc4_encoder base;
  55. bool hdmi_monitor;
  56. };
  57. static inline struct vc4_hdmi_encoder *
  58. to_vc4_hdmi_encoder(struct drm_encoder *encoder)
  59. {
  60. return container_of(encoder, struct vc4_hdmi_encoder, base.base);
  61. }
  62. /* VC4 HDMI connector KMS struct */
  63. struct vc4_hdmi_connector {
  64. struct drm_connector base;
  65. /* Since the connector is attached to just the one encoder,
  66. * this is the reference to it so we can do the best_encoder()
  67. * hook.
  68. */
  69. struct drm_encoder *encoder;
  70. };
  71. static inline struct vc4_hdmi_connector *
  72. to_vc4_hdmi_connector(struct drm_connector *connector)
  73. {
  74. return container_of(connector, struct vc4_hdmi_connector, base);
  75. }
  76. #define HDMI_REG(reg) { reg, #reg }
  77. static const struct {
  78. u32 reg;
  79. const char *name;
  80. } hdmi_regs[] = {
  81. HDMI_REG(VC4_HDMI_CORE_REV),
  82. HDMI_REG(VC4_HDMI_SW_RESET_CONTROL),
  83. HDMI_REG(VC4_HDMI_HOTPLUG_INT),
  84. HDMI_REG(VC4_HDMI_HOTPLUG),
  85. HDMI_REG(VC4_HDMI_HORZA),
  86. HDMI_REG(VC4_HDMI_HORZB),
  87. HDMI_REG(VC4_HDMI_FIFO_CTL),
  88. HDMI_REG(VC4_HDMI_SCHEDULER_CONTROL),
  89. HDMI_REG(VC4_HDMI_VERTA0),
  90. HDMI_REG(VC4_HDMI_VERTA1),
  91. HDMI_REG(VC4_HDMI_VERTB0),
  92. HDMI_REG(VC4_HDMI_VERTB1),
  93. HDMI_REG(VC4_HDMI_TX_PHY_RESET_CTL),
  94. };
  95. static const struct {
  96. u32 reg;
  97. const char *name;
  98. } hd_regs[] = {
  99. HDMI_REG(VC4_HD_M_CTL),
  100. HDMI_REG(VC4_HD_MAI_CTL),
  101. HDMI_REG(VC4_HD_VID_CTL),
  102. HDMI_REG(VC4_HD_CSC_CTL),
  103. HDMI_REG(VC4_HD_FRAME_COUNT),
  104. };
  105. #ifdef CONFIG_DEBUG_FS
  106. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
  107. {
  108. struct drm_info_node *node = (struct drm_info_node *)m->private;
  109. struct drm_device *dev = node->minor->dev;
  110. struct vc4_dev *vc4 = to_vc4_dev(dev);
  111. int i;
  112. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  113. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  114. hdmi_regs[i].name, hdmi_regs[i].reg,
  115. HDMI_READ(hdmi_regs[i].reg));
  116. }
  117. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  118. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  119. hd_regs[i].name, hd_regs[i].reg,
  120. HD_READ(hd_regs[i].reg));
  121. }
  122. return 0;
  123. }
  124. #endif /* CONFIG_DEBUG_FS */
  125. static void vc4_hdmi_dump_regs(struct drm_device *dev)
  126. {
  127. struct vc4_dev *vc4 = to_vc4_dev(dev);
  128. int i;
  129. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  130. DRM_INFO("0x%04x (%s): 0x%08x\n",
  131. hdmi_regs[i].reg, hdmi_regs[i].name,
  132. HDMI_READ(hdmi_regs[i].reg));
  133. }
  134. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  135. DRM_INFO("0x%04x (%s): 0x%08x\n",
  136. hd_regs[i].reg, hd_regs[i].name,
  137. HD_READ(hd_regs[i].reg));
  138. }
  139. }
  140. static enum drm_connector_status
  141. vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
  142. {
  143. struct drm_device *dev = connector->dev;
  144. struct vc4_dev *vc4 = to_vc4_dev(dev);
  145. if (vc4->hdmi->hpd_gpio) {
  146. if (gpio_get_value(vc4->hdmi->hpd_gpio))
  147. return connector_status_connected;
  148. else
  149. return connector_status_disconnected;
  150. }
  151. if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
  152. return connector_status_connected;
  153. else
  154. return connector_status_disconnected;
  155. }
  156. static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
  157. {
  158. drm_connector_unregister(connector);
  159. drm_connector_cleanup(connector);
  160. }
  161. static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
  162. {
  163. struct vc4_hdmi_connector *vc4_connector =
  164. to_vc4_hdmi_connector(connector);
  165. struct drm_encoder *encoder = vc4_connector->encoder;
  166. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  167. struct drm_device *dev = connector->dev;
  168. struct vc4_dev *vc4 = to_vc4_dev(dev);
  169. int ret = 0;
  170. struct edid *edid;
  171. edid = drm_get_edid(connector, vc4->hdmi->ddc);
  172. if (!edid)
  173. return -ENODEV;
  174. vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
  175. drm_mode_connector_update_edid_property(connector, edid);
  176. ret = drm_add_edid_modes(connector, edid);
  177. return ret;
  178. }
  179. static struct drm_encoder *
  180. vc4_hdmi_connector_best_encoder(struct drm_connector *connector)
  181. {
  182. struct vc4_hdmi_connector *hdmi_connector =
  183. to_vc4_hdmi_connector(connector);
  184. return hdmi_connector->encoder;
  185. }
  186. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  187. .dpms = drm_atomic_helper_connector_dpms,
  188. .detect = vc4_hdmi_connector_detect,
  189. .fill_modes = drm_helper_probe_single_connector_modes,
  190. .destroy = vc4_hdmi_connector_destroy,
  191. .reset = drm_atomic_helper_connector_reset,
  192. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  193. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  194. };
  195. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  196. .get_modes = vc4_hdmi_connector_get_modes,
  197. .best_encoder = vc4_hdmi_connector_best_encoder,
  198. };
  199. static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
  200. struct drm_encoder *encoder)
  201. {
  202. struct drm_connector *connector = NULL;
  203. struct vc4_hdmi_connector *hdmi_connector;
  204. int ret = 0;
  205. hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
  206. GFP_KERNEL);
  207. if (!hdmi_connector) {
  208. ret = -ENOMEM;
  209. goto fail;
  210. }
  211. connector = &hdmi_connector->base;
  212. hdmi_connector->encoder = encoder;
  213. drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
  214. DRM_MODE_CONNECTOR_HDMIA);
  215. drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
  216. connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  217. DRM_CONNECTOR_POLL_DISCONNECT);
  218. connector->interlace_allowed = 0;
  219. connector->doublescan_allowed = 0;
  220. drm_mode_connector_attach_encoder(connector, encoder);
  221. return connector;
  222. fail:
  223. if (connector)
  224. vc4_hdmi_connector_destroy(connector);
  225. return ERR_PTR(ret);
  226. }
  227. static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
  228. {
  229. drm_encoder_cleanup(encoder);
  230. }
  231. static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
  232. .destroy = vc4_hdmi_encoder_destroy,
  233. };
  234. static void vc4_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  235. struct drm_display_mode *unadjusted_mode,
  236. struct drm_display_mode *mode)
  237. {
  238. struct drm_device *dev = encoder->dev;
  239. struct vc4_dev *vc4 = to_vc4_dev(dev);
  240. bool debug_dump_regs = false;
  241. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  242. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  243. u32 vactive = (mode->vdisplay >>
  244. ((mode->flags & DRM_MODE_FLAG_INTERLACE) ? 1 : 0));
  245. u32 verta = (VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  246. VC4_HDMI_VERTA_VSP) |
  247. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  248. VC4_HDMI_VERTA_VFP) |
  249. VC4_SET_FIELD(vactive, VC4_HDMI_VERTA_VAL));
  250. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  251. VC4_SET_FIELD(mode->vtotal - mode->vsync_end,
  252. VC4_HDMI_VERTB_VBP));
  253. if (debug_dump_regs) {
  254. DRM_INFO("HDMI regs before:\n");
  255. vc4_hdmi_dump_regs(dev);
  256. }
  257. HD_WRITE(VC4_HD_VID_CTL, 0);
  258. clk_set_rate(vc4->hdmi->pixel_clock, mode->clock * 1000);
  259. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  260. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  261. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  262. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  263. HDMI_WRITE(VC4_HDMI_HORZA,
  264. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  265. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  266. VC4_SET_FIELD(mode->hdisplay, VC4_HDMI_HORZA_HAP));
  267. HDMI_WRITE(VC4_HDMI_HORZB,
  268. VC4_SET_FIELD(mode->htotal - mode->hsync_end,
  269. VC4_HDMI_HORZB_HBP) |
  270. VC4_SET_FIELD(mode->hsync_end - mode->hsync_start,
  271. VC4_HDMI_HORZB_HSP) |
  272. VC4_SET_FIELD(mode->hsync_start - mode->hdisplay,
  273. VC4_HDMI_HORZB_HFP));
  274. HDMI_WRITE(VC4_HDMI_VERTA0, verta);
  275. HDMI_WRITE(VC4_HDMI_VERTA1, verta);
  276. HDMI_WRITE(VC4_HDMI_VERTB0, vertb);
  277. HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
  278. HD_WRITE(VC4_HD_VID_CTL,
  279. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  280. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  281. /* The RGB order applies even when CSC is disabled. */
  282. HD_WRITE(VC4_HD_CSC_CTL, VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  283. VC4_HD_CSC_CTL_ORDER));
  284. HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  285. if (debug_dump_regs) {
  286. DRM_INFO("HDMI regs after:\n");
  287. vc4_hdmi_dump_regs(dev);
  288. }
  289. }
  290. static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
  291. {
  292. struct drm_device *dev = encoder->dev;
  293. struct vc4_dev *vc4 = to_vc4_dev(dev);
  294. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  295. HD_WRITE(VC4_HD_VID_CTL,
  296. HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  297. }
  298. static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
  299. {
  300. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  301. struct drm_device *dev = encoder->dev;
  302. struct vc4_dev *vc4 = to_vc4_dev(dev);
  303. int ret;
  304. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
  305. HD_WRITE(VC4_HD_VID_CTL,
  306. HD_READ(VC4_HD_VID_CTL) |
  307. VC4_HD_VID_CTL_ENABLE |
  308. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  309. VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
  310. if (vc4_encoder->hdmi_monitor) {
  311. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  312. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  313. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  314. ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  315. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1);
  316. WARN_ONCE(ret, "Timeout waiting for "
  317. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  318. } else {
  319. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  320. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  321. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  322. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  323. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  324. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  325. ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  326. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1);
  327. WARN_ONCE(ret, "Timeout waiting for "
  328. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  329. }
  330. if (vc4_encoder->hdmi_monitor) {
  331. u32 drift;
  332. WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  333. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  334. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  335. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  336. VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
  337. /* XXX: Set HDMI_RAM_PACKET_CONFIG (1 << 16) and set
  338. * up the infoframe.
  339. */
  340. drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
  341. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  342. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  343. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  344. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  345. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  346. udelay(1000);
  347. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  348. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  349. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  350. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  351. ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
  352. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  353. WARN_ONCE(ret, "Timeout waiting for "
  354. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  355. }
  356. }
  357. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  358. .mode_set = vc4_hdmi_encoder_mode_set,
  359. .disable = vc4_hdmi_encoder_disable,
  360. .enable = vc4_hdmi_encoder_enable,
  361. };
  362. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  363. {
  364. struct platform_device *pdev = to_platform_device(dev);
  365. struct drm_device *drm = dev_get_drvdata(master);
  366. struct vc4_dev *vc4 = drm->dev_private;
  367. struct vc4_hdmi *hdmi;
  368. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  369. struct device_node *ddc_node;
  370. u32 value;
  371. int ret;
  372. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  373. if (!hdmi)
  374. return -ENOMEM;
  375. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  376. GFP_KERNEL);
  377. if (!vc4_hdmi_encoder)
  378. return -ENOMEM;
  379. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  380. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  381. hdmi->pdev = pdev;
  382. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  383. if (IS_ERR(hdmi->hdmicore_regs))
  384. return PTR_ERR(hdmi->hdmicore_regs);
  385. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  386. if (IS_ERR(hdmi->hd_regs))
  387. return PTR_ERR(hdmi->hd_regs);
  388. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  389. if (!ddc_node) {
  390. DRM_ERROR("Failed to find ddc node in device tree\n");
  391. return -ENODEV;
  392. }
  393. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  394. if (IS_ERR(hdmi->pixel_clock)) {
  395. DRM_ERROR("Failed to get pixel clock\n");
  396. return PTR_ERR(hdmi->pixel_clock);
  397. }
  398. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  399. if (IS_ERR(hdmi->hsm_clock)) {
  400. DRM_ERROR("Failed to get HDMI state machine clock\n");
  401. return PTR_ERR(hdmi->hsm_clock);
  402. }
  403. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  404. if (!hdmi->ddc) {
  405. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  406. return -EPROBE_DEFER;
  407. }
  408. /* Enable the clocks at startup. We can't quite recover from
  409. * turning off the pixel clock during disable/enables yet, so
  410. * it's always running.
  411. */
  412. ret = clk_prepare_enable(hdmi->pixel_clock);
  413. if (ret) {
  414. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  415. goto err_put_i2c;
  416. }
  417. ret = clk_prepare_enable(hdmi->hsm_clock);
  418. if (ret) {
  419. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  420. ret);
  421. goto err_unprepare_pix;
  422. }
  423. /* Only use the GPIO HPD pin if present in the DT, otherwise
  424. * we'll use the HDMI core's register.
  425. */
  426. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  427. hdmi->hpd_gpio = of_get_named_gpio(dev->of_node, "hpd-gpios", 0);
  428. if (hdmi->hpd_gpio < 0) {
  429. ret = hdmi->hpd_gpio;
  430. goto err_unprepare_hsm;
  431. }
  432. }
  433. vc4->hdmi = hdmi;
  434. /* HDMI core must be enabled. */
  435. WARN_ON_ONCE((HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE) == 0);
  436. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  437. DRM_MODE_ENCODER_TMDS);
  438. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  439. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  440. if (IS_ERR(hdmi->connector)) {
  441. ret = PTR_ERR(hdmi->connector);
  442. goto err_destroy_encoder;
  443. }
  444. return 0;
  445. err_destroy_encoder:
  446. vc4_hdmi_encoder_destroy(hdmi->encoder);
  447. err_unprepare_hsm:
  448. clk_disable_unprepare(hdmi->hsm_clock);
  449. err_unprepare_pix:
  450. clk_disable_unprepare(hdmi->pixel_clock);
  451. err_put_i2c:
  452. put_device(&vc4->hdmi->ddc->dev);
  453. return ret;
  454. }
  455. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  456. void *data)
  457. {
  458. struct drm_device *drm = dev_get_drvdata(master);
  459. struct vc4_dev *vc4 = drm->dev_private;
  460. struct vc4_hdmi *hdmi = vc4->hdmi;
  461. vc4_hdmi_connector_destroy(hdmi->connector);
  462. vc4_hdmi_encoder_destroy(hdmi->encoder);
  463. clk_disable_unprepare(hdmi->pixel_clock);
  464. clk_disable_unprepare(hdmi->hsm_clock);
  465. put_device(&hdmi->ddc->dev);
  466. vc4->hdmi = NULL;
  467. }
  468. static const struct component_ops vc4_hdmi_ops = {
  469. .bind = vc4_hdmi_bind,
  470. .unbind = vc4_hdmi_unbind,
  471. };
  472. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  473. {
  474. return component_add(&pdev->dev, &vc4_hdmi_ops);
  475. }
  476. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  477. {
  478. component_del(&pdev->dev, &vc4_hdmi_ops);
  479. return 0;
  480. }
  481. static const struct of_device_id vc4_hdmi_dt_match[] = {
  482. { .compatible = "brcm,bcm2835-hdmi" },
  483. {}
  484. };
  485. struct platform_driver vc4_hdmi_driver = {
  486. .probe = vc4_hdmi_dev_probe,
  487. .remove = vc4_hdmi_dev_remove,
  488. .driver = {
  489. .name = "vc4_hdmi",
  490. .of_match_table = vc4_hdmi_dt_match,
  491. },
  492. };