vc4_hdmi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 struct drm_encoder *
  183. vc4_hdmi_connector_best_encoder(struct drm_connector *connector)
  184. {
  185. struct vc4_hdmi_connector *hdmi_connector =
  186. to_vc4_hdmi_connector(connector);
  187. return hdmi_connector->encoder;
  188. }
  189. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  190. .dpms = drm_atomic_helper_connector_dpms,
  191. .detect = vc4_hdmi_connector_detect,
  192. .fill_modes = drm_helper_probe_single_connector_modes,
  193. .destroy = vc4_hdmi_connector_destroy,
  194. .reset = drm_atomic_helper_connector_reset,
  195. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  196. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  197. };
  198. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  199. .get_modes = vc4_hdmi_connector_get_modes,
  200. .best_encoder = vc4_hdmi_connector_best_encoder,
  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 = 0;
  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 void vc4_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  238. struct drm_display_mode *unadjusted_mode,
  239. struct drm_display_mode *mode)
  240. {
  241. struct drm_device *dev = encoder->dev;
  242. struct vc4_dev *vc4 = to_vc4_dev(dev);
  243. bool debug_dump_regs = false;
  244. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  245. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  246. u32 vactive = (mode->vdisplay >>
  247. ((mode->flags & DRM_MODE_FLAG_INTERLACE) ? 1 : 0));
  248. u32 verta = (VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  249. VC4_HDMI_VERTA_VSP) |
  250. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  251. VC4_HDMI_VERTA_VFP) |
  252. VC4_SET_FIELD(vactive, VC4_HDMI_VERTA_VAL));
  253. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  254. VC4_SET_FIELD(mode->vtotal - mode->vsync_end,
  255. VC4_HDMI_VERTB_VBP));
  256. if (debug_dump_regs) {
  257. DRM_INFO("HDMI regs before:\n");
  258. vc4_hdmi_dump_regs(dev);
  259. }
  260. HD_WRITE(VC4_HD_VID_CTL, 0);
  261. clk_set_rate(vc4->hdmi->pixel_clock, mode->clock * 1000);
  262. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  263. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  264. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  265. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  266. HDMI_WRITE(VC4_HDMI_HORZA,
  267. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  268. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  269. VC4_SET_FIELD(mode->hdisplay, VC4_HDMI_HORZA_HAP));
  270. HDMI_WRITE(VC4_HDMI_HORZB,
  271. VC4_SET_FIELD(mode->htotal - mode->hsync_end,
  272. VC4_HDMI_HORZB_HBP) |
  273. VC4_SET_FIELD(mode->hsync_end - mode->hsync_start,
  274. VC4_HDMI_HORZB_HSP) |
  275. VC4_SET_FIELD(mode->hsync_start - mode->hdisplay,
  276. VC4_HDMI_HORZB_HFP));
  277. HDMI_WRITE(VC4_HDMI_VERTA0, verta);
  278. HDMI_WRITE(VC4_HDMI_VERTA1, verta);
  279. HDMI_WRITE(VC4_HDMI_VERTB0, vertb);
  280. HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
  281. HD_WRITE(VC4_HD_VID_CTL,
  282. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  283. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  284. /* The RGB order applies even when CSC is disabled. */
  285. HD_WRITE(VC4_HD_CSC_CTL, VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  286. VC4_HD_CSC_CTL_ORDER));
  287. HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  288. if (debug_dump_regs) {
  289. DRM_INFO("HDMI regs after:\n");
  290. vc4_hdmi_dump_regs(dev);
  291. }
  292. }
  293. static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
  294. {
  295. struct drm_device *dev = encoder->dev;
  296. struct vc4_dev *vc4 = to_vc4_dev(dev);
  297. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  298. HD_WRITE(VC4_HD_VID_CTL,
  299. HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  300. }
  301. static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
  302. {
  303. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  304. struct drm_device *dev = encoder->dev;
  305. struct vc4_dev *vc4 = to_vc4_dev(dev);
  306. int ret;
  307. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
  308. HD_WRITE(VC4_HD_VID_CTL,
  309. HD_READ(VC4_HD_VID_CTL) |
  310. VC4_HD_VID_CTL_ENABLE |
  311. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  312. VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
  313. if (vc4_encoder->hdmi_monitor) {
  314. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  315. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  316. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  317. ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  318. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1);
  319. WARN_ONCE(ret, "Timeout waiting for "
  320. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  321. } else {
  322. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  323. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  324. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  325. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  326. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  327. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  328. ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  329. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1);
  330. WARN_ONCE(ret, "Timeout waiting for "
  331. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  332. }
  333. if (vc4_encoder->hdmi_monitor) {
  334. u32 drift;
  335. WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  336. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  337. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  338. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  339. VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
  340. /* XXX: Set HDMI_RAM_PACKET_CONFIG (1 << 16) and set
  341. * up the infoframe.
  342. */
  343. drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
  344. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  345. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  346. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  347. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  348. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  349. udelay(1000);
  350. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  351. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  352. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  353. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  354. ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
  355. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  356. WARN_ONCE(ret, "Timeout waiting for "
  357. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  358. }
  359. }
  360. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  361. .mode_set = vc4_hdmi_encoder_mode_set,
  362. .disable = vc4_hdmi_encoder_disable,
  363. .enable = vc4_hdmi_encoder_enable,
  364. };
  365. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  366. {
  367. struct platform_device *pdev = to_platform_device(dev);
  368. struct drm_device *drm = dev_get_drvdata(master);
  369. struct vc4_dev *vc4 = drm->dev_private;
  370. struct vc4_hdmi *hdmi;
  371. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  372. struct device_node *ddc_node;
  373. u32 value;
  374. int ret;
  375. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  376. if (!hdmi)
  377. return -ENOMEM;
  378. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  379. GFP_KERNEL);
  380. if (!vc4_hdmi_encoder)
  381. return -ENOMEM;
  382. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  383. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  384. hdmi->pdev = pdev;
  385. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  386. if (IS_ERR(hdmi->hdmicore_regs))
  387. return PTR_ERR(hdmi->hdmicore_regs);
  388. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  389. if (IS_ERR(hdmi->hd_regs))
  390. return PTR_ERR(hdmi->hd_regs);
  391. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  392. if (IS_ERR(hdmi->pixel_clock)) {
  393. DRM_ERROR("Failed to get pixel clock\n");
  394. return PTR_ERR(hdmi->pixel_clock);
  395. }
  396. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  397. if (IS_ERR(hdmi->hsm_clock)) {
  398. DRM_ERROR("Failed to get HDMI state machine clock\n");
  399. return PTR_ERR(hdmi->hsm_clock);
  400. }
  401. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  402. if (!ddc_node) {
  403. DRM_ERROR("Failed to find ddc node in device tree\n");
  404. return -ENODEV;
  405. }
  406. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  407. of_node_put(ddc_node);
  408. if (!hdmi->ddc) {
  409. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  410. return -EPROBE_DEFER;
  411. }
  412. /* Enable the clocks at startup. We can't quite recover from
  413. * turning off the pixel clock during disable/enables yet, so
  414. * it's always running.
  415. */
  416. ret = clk_prepare_enable(hdmi->pixel_clock);
  417. if (ret) {
  418. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  419. goto err_put_i2c;
  420. }
  421. /* This is the rate that is set by the firmware. The number
  422. * needs to be a bit higher than the pixel clock rate
  423. * (generally 148.5Mhz).
  424. */
  425. ret = clk_set_rate(hdmi->hsm_clock, 163682864);
  426. if (ret) {
  427. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  428. goto err_unprepare_pix;
  429. }
  430. ret = clk_prepare_enable(hdmi->hsm_clock);
  431. if (ret) {
  432. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  433. ret);
  434. goto err_unprepare_pix;
  435. }
  436. /* Only use the GPIO HPD pin if present in the DT, otherwise
  437. * we'll use the HDMI core's register.
  438. */
  439. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  440. enum of_gpio_flags hpd_gpio_flags;
  441. hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
  442. "hpd-gpios", 0,
  443. &hpd_gpio_flags);
  444. if (hdmi->hpd_gpio < 0) {
  445. ret = hdmi->hpd_gpio;
  446. goto err_unprepare_hsm;
  447. }
  448. hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
  449. }
  450. vc4->hdmi = hdmi;
  451. /* HDMI core must be enabled. */
  452. if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
  453. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
  454. udelay(1);
  455. HD_WRITE(VC4_HD_M_CTL, 0);
  456. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
  457. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
  458. VC4_HDMI_SW_RESET_HDMI |
  459. VC4_HDMI_SW_RESET_FORMAT_DETECT);
  460. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
  461. /* PHY should be in reset, like
  462. * vc4_hdmi_encoder_disable() does.
  463. */
  464. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  465. }
  466. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  467. DRM_MODE_ENCODER_TMDS, NULL);
  468. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  469. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  470. if (IS_ERR(hdmi->connector)) {
  471. ret = PTR_ERR(hdmi->connector);
  472. goto err_destroy_encoder;
  473. }
  474. return 0;
  475. err_destroy_encoder:
  476. vc4_hdmi_encoder_destroy(hdmi->encoder);
  477. err_unprepare_hsm:
  478. clk_disable_unprepare(hdmi->hsm_clock);
  479. err_unprepare_pix:
  480. clk_disable_unprepare(hdmi->pixel_clock);
  481. err_put_i2c:
  482. put_device(&hdmi->ddc->dev);
  483. return ret;
  484. }
  485. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  486. void *data)
  487. {
  488. struct drm_device *drm = dev_get_drvdata(master);
  489. struct vc4_dev *vc4 = drm->dev_private;
  490. struct vc4_hdmi *hdmi = vc4->hdmi;
  491. vc4_hdmi_connector_destroy(hdmi->connector);
  492. vc4_hdmi_encoder_destroy(hdmi->encoder);
  493. clk_disable_unprepare(hdmi->pixel_clock);
  494. clk_disable_unprepare(hdmi->hsm_clock);
  495. put_device(&hdmi->ddc->dev);
  496. vc4->hdmi = NULL;
  497. }
  498. static const struct component_ops vc4_hdmi_ops = {
  499. .bind = vc4_hdmi_bind,
  500. .unbind = vc4_hdmi_unbind,
  501. };
  502. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  503. {
  504. return component_add(&pdev->dev, &vc4_hdmi_ops);
  505. }
  506. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  507. {
  508. component_del(&pdev->dev, &vc4_hdmi_ops);
  509. return 0;
  510. }
  511. static const struct of_device_id vc4_hdmi_dt_match[] = {
  512. { .compatible = "brcm,bcm2835-hdmi" },
  513. {}
  514. };
  515. struct platform_driver vc4_hdmi_driver = {
  516. .probe = vc4_hdmi_dev_probe,
  517. .remove = vc4_hdmi_dev_remove,
  518. .driver = {
  519. .name = "vc4_hdmi",
  520. .of_match_table = vc4_hdmi_dt_match,
  521. },
  522. };