vc4_hdmi.c 17 KB

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