sun4i_hdmi_enc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Copyright (C) 2016 Maxime Ripard
  3. *
  4. * Maxime Ripard <maxime.ripard@free-electrons.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_crtc_helper.h>
  14. #include <drm/drm_edid.h>
  15. #include <drm/drm_encoder.h>
  16. #include <drm/drm_of.h>
  17. #include <drm/drm_panel.h>
  18. #include <linux/clk.h>
  19. #include <linux/component.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/regmap.h>
  25. #include <linux/reset.h>
  26. #include "sun4i_backend.h"
  27. #include "sun4i_crtc.h"
  28. #include "sun4i_drv.h"
  29. #include "sun4i_hdmi.h"
  30. #include "sun4i_tcon.h"
  31. static inline struct sun4i_hdmi *
  32. drm_encoder_to_sun4i_hdmi(struct drm_encoder *encoder)
  33. {
  34. return container_of(encoder, struct sun4i_hdmi,
  35. encoder);
  36. }
  37. static inline struct sun4i_hdmi *
  38. drm_connector_to_sun4i_hdmi(struct drm_connector *connector)
  39. {
  40. return container_of(connector, struct sun4i_hdmi,
  41. connector);
  42. }
  43. static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi,
  44. struct drm_display_mode *mode)
  45. {
  46. struct hdmi_avi_infoframe frame;
  47. u8 buffer[17];
  48. int i, ret;
  49. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
  50. if (ret < 0) {
  51. DRM_ERROR("Failed to get infoframes from mode\n");
  52. return ret;
  53. }
  54. ret = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  55. if (ret < 0) {
  56. DRM_ERROR("Failed to pack infoframes\n");
  57. return ret;
  58. }
  59. for (i = 0; i < sizeof(buffer); i++)
  60. writeb(buffer[i], hdmi->base + SUN4I_HDMI_AVI_INFOFRAME_REG(i));
  61. return 0;
  62. }
  63. static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder,
  64. struct drm_crtc_state *crtc_state,
  65. struct drm_connector_state *conn_state)
  66. {
  67. struct drm_display_mode *mode = &crtc_state->mode;
  68. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  69. return -EINVAL;
  70. return 0;
  71. }
  72. static void sun4i_hdmi_disable(struct drm_encoder *encoder)
  73. {
  74. struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
  75. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  76. struct sun4i_tcon *tcon = crtc->tcon;
  77. u32 val;
  78. DRM_DEBUG_DRIVER("Disabling the HDMI Output\n");
  79. val = readl(hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
  80. val &= ~SUN4I_HDMI_VID_CTRL_ENABLE;
  81. writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
  82. sun4i_tcon_channel_disable(tcon, 1);
  83. }
  84. static void sun4i_hdmi_enable(struct drm_encoder *encoder)
  85. {
  86. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  87. struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
  88. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  89. struct sun4i_tcon *tcon = crtc->tcon;
  90. u32 val = 0;
  91. DRM_DEBUG_DRIVER("Enabling the HDMI Output\n");
  92. sun4i_tcon_channel_enable(tcon, 1);
  93. sun4i_hdmi_setup_avi_infoframes(hdmi, mode);
  94. val |= SUN4I_HDMI_PKT_CTRL_TYPE(0, SUN4I_HDMI_PKT_AVI);
  95. val |= SUN4I_HDMI_PKT_CTRL_TYPE(1, SUN4I_HDMI_PKT_END);
  96. writel(val, hdmi->base + SUN4I_HDMI_PKT_CTRL_REG(0));
  97. val = SUN4I_HDMI_VID_CTRL_ENABLE;
  98. if (hdmi->hdmi_monitor)
  99. val |= SUN4I_HDMI_VID_CTRL_HDMI_MODE;
  100. writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
  101. }
  102. static void sun4i_hdmi_mode_set(struct drm_encoder *encoder,
  103. struct drm_display_mode *mode,
  104. struct drm_display_mode *adjusted_mode)
  105. {
  106. struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
  107. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  108. struct sun4i_tcon *tcon = crtc->tcon;
  109. unsigned int x, y;
  110. u32 val;
  111. sun4i_tcon1_mode_set(tcon, mode);
  112. sun4i_tcon_set_mux(tcon, 1, encoder);
  113. clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
  114. clk_set_rate(hdmi->mod_clk, mode->crtc_clock * 1000);
  115. clk_set_rate(hdmi->tmds_clk, mode->crtc_clock * 1000);
  116. /* Set input sync enable */
  117. writel(SUN4I_HDMI_UNKNOWN_INPUT_SYNC,
  118. hdmi->base + SUN4I_HDMI_UNKNOWN_REG);
  119. /* Setup timing registers */
  120. writel(SUN4I_HDMI_VID_TIMING_X(mode->hdisplay) |
  121. SUN4I_HDMI_VID_TIMING_Y(mode->vdisplay),
  122. hdmi->base + SUN4I_HDMI_VID_TIMING_ACT_REG);
  123. x = mode->htotal - mode->hsync_start;
  124. y = mode->vtotal - mode->vsync_start;
  125. writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
  126. hdmi->base + SUN4I_HDMI_VID_TIMING_BP_REG);
  127. x = mode->hsync_start - mode->hdisplay;
  128. y = mode->vsync_start - mode->vdisplay;
  129. writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
  130. hdmi->base + SUN4I_HDMI_VID_TIMING_FP_REG);
  131. x = mode->hsync_end - mode->hsync_start;
  132. y = mode->vsync_end - mode->vsync_start;
  133. writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
  134. hdmi->base + SUN4I_HDMI_VID_TIMING_SPW_REG);
  135. val = SUN4I_HDMI_VID_TIMING_POL_TX_CLK;
  136. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  137. val |= SUN4I_HDMI_VID_TIMING_POL_HSYNC;
  138. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  139. val |= SUN4I_HDMI_VID_TIMING_POL_VSYNC;
  140. writel(val, hdmi->base + SUN4I_HDMI_VID_TIMING_POL_REG);
  141. }
  142. static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = {
  143. .atomic_check = sun4i_hdmi_atomic_check,
  144. .disable = sun4i_hdmi_disable,
  145. .enable = sun4i_hdmi_enable,
  146. .mode_set = sun4i_hdmi_mode_set,
  147. };
  148. static const struct drm_encoder_funcs sun4i_hdmi_funcs = {
  149. .destroy = drm_encoder_cleanup,
  150. };
  151. static int sun4i_hdmi_get_modes(struct drm_connector *connector)
  152. {
  153. struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
  154. struct edid *edid;
  155. int ret;
  156. edid = drm_get_edid(connector, hdmi->i2c);
  157. if (!edid)
  158. return 0;
  159. hdmi->hdmi_monitor = drm_detect_hdmi_monitor(edid);
  160. DRM_DEBUG_DRIVER("Monitor is %s monitor\n",
  161. hdmi->hdmi_monitor ? "an HDMI" : "a DVI");
  162. drm_mode_connector_update_edid_property(connector, edid);
  163. cec_s_phys_addr_from_edid(hdmi->cec_adap, edid);
  164. ret = drm_add_edid_modes(connector, edid);
  165. kfree(edid);
  166. return ret;
  167. }
  168. static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = {
  169. .get_modes = sun4i_hdmi_get_modes,
  170. };
  171. static enum drm_connector_status
  172. sun4i_hdmi_connector_detect(struct drm_connector *connector, bool force)
  173. {
  174. struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
  175. unsigned long reg;
  176. if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_HPD_REG, reg,
  177. reg & SUN4I_HDMI_HPD_HIGH,
  178. 0, 500000)) {
  179. cec_phys_addr_invalidate(hdmi->cec_adap);
  180. return connector_status_disconnected;
  181. }
  182. return connector_status_connected;
  183. }
  184. static const struct drm_connector_funcs sun4i_hdmi_connector_funcs = {
  185. .detect = sun4i_hdmi_connector_detect,
  186. .fill_modes = drm_helper_probe_single_connector_modes,
  187. .destroy = drm_connector_cleanup,
  188. .reset = drm_atomic_helper_connector_reset,
  189. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  190. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  191. };
  192. #ifdef CONFIG_DRM_SUN4I_HDMI_CEC
  193. static bool sun4i_hdmi_cec_pin_read(struct cec_adapter *adap)
  194. {
  195. struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
  196. return readl(hdmi->base + SUN4I_HDMI_CEC) & SUN4I_HDMI_CEC_RX;
  197. }
  198. static void sun4i_hdmi_cec_pin_low(struct cec_adapter *adap)
  199. {
  200. struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
  201. /* Start driving the CEC pin low */
  202. writel(SUN4I_HDMI_CEC_ENABLE, hdmi->base + SUN4I_HDMI_CEC);
  203. }
  204. static void sun4i_hdmi_cec_pin_high(struct cec_adapter *adap)
  205. {
  206. struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
  207. /*
  208. * Stop driving the CEC pin, the pull up will take over
  209. * unless another CEC device is driving the pin low.
  210. */
  211. writel(0, hdmi->base + SUN4I_HDMI_CEC);
  212. }
  213. static const struct cec_pin_ops sun4i_hdmi_cec_pin_ops = {
  214. .read = sun4i_hdmi_cec_pin_read,
  215. .low = sun4i_hdmi_cec_pin_low,
  216. .high = sun4i_hdmi_cec_pin_high,
  217. };
  218. #endif
  219. #define SUN4I_HDMI_PAD_CTRL1_MASK (GENMASK(24, 7) | GENMASK(5, 0))
  220. #define SUN4I_HDMI_PLL_CTRL_MASK (GENMASK(31, 8) | GENMASK(3, 0))
  221. static const struct sun4i_hdmi_variant sun5i_variant = {
  222. .pad_ctrl0_init_val = SUN4I_HDMI_PAD_CTRL0_TXEN |
  223. SUN4I_HDMI_PAD_CTRL0_CKEN |
  224. SUN4I_HDMI_PAD_CTRL0_PWENG |
  225. SUN4I_HDMI_PAD_CTRL0_PWEND |
  226. SUN4I_HDMI_PAD_CTRL0_PWENC |
  227. SUN4I_HDMI_PAD_CTRL0_LDODEN |
  228. SUN4I_HDMI_PAD_CTRL0_LDOCEN |
  229. SUN4I_HDMI_PAD_CTRL0_BIASEN,
  230. .pad_ctrl1_init_val = SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
  231. SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) |
  232. SUN4I_HDMI_PAD_CTRL1_REG_DENCK |
  233. SUN4I_HDMI_PAD_CTRL1_REG_DEN |
  234. SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT |
  235. SUN4I_HDMI_PAD_CTRL1_EMP_OPT |
  236. SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT |
  237. SUN4I_HDMI_PAD_CTRL1_AMP_OPT,
  238. .pll_ctrl_init_val = SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
  239. SUN4I_HDMI_PLL_CTRL_CS(7) |
  240. SUN4I_HDMI_PLL_CTRL_CP_S(15) |
  241. SUN4I_HDMI_PLL_CTRL_S(7) |
  242. SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
  243. SUN4I_HDMI_PLL_CTRL_SDIV2 |
  244. SUN4I_HDMI_PLL_CTRL_LDO2_EN |
  245. SUN4I_HDMI_PLL_CTRL_LDO1_EN |
  246. SUN4I_HDMI_PLL_CTRL_HV_IS_33 |
  247. SUN4I_HDMI_PLL_CTRL_BWS |
  248. SUN4I_HDMI_PLL_CTRL_PLL_EN,
  249. .ddc_clk_reg = REG_FIELD(SUN4I_HDMI_DDC_CLK_REG, 0, 6),
  250. .ddc_clk_pre_divider = 2,
  251. .ddc_clk_m_offset = 1,
  252. .field_ddc_en = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 31, 31),
  253. .field_ddc_start = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 30, 30),
  254. .field_ddc_reset = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 0, 0),
  255. .field_ddc_addr_reg = REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG, 0, 31),
  256. .field_ddc_slave_addr = REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG, 0, 6),
  257. .field_ddc_int_status = REG_FIELD(SUN4I_HDMI_DDC_INT_STATUS_REG, 0, 8),
  258. .field_ddc_fifo_clear = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 31, 31),
  259. .field_ddc_fifo_rx_thres = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 4, 7),
  260. .field_ddc_fifo_tx_thres = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 0, 3),
  261. .field_ddc_byte_count = REG_FIELD(SUN4I_HDMI_DDC_BYTE_COUNT_REG, 0, 9),
  262. .field_ddc_cmd = REG_FIELD(SUN4I_HDMI_DDC_CMD_REG, 0, 2),
  263. .field_ddc_sda_en = REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG, 9, 9),
  264. .field_ddc_sck_en = REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG, 8, 8),
  265. .ddc_fifo_reg = SUN4I_HDMI_DDC_FIFO_DATA_REG,
  266. .ddc_fifo_has_dir = true,
  267. };
  268. static const struct sun4i_hdmi_variant sun6i_variant = {
  269. .has_ddc_parent_clk = true,
  270. .has_reset_control = true,
  271. .pad_ctrl0_init_val = 0xff |
  272. SUN4I_HDMI_PAD_CTRL0_TXEN |
  273. SUN4I_HDMI_PAD_CTRL0_CKEN |
  274. SUN4I_HDMI_PAD_CTRL0_PWENG |
  275. SUN4I_HDMI_PAD_CTRL0_PWEND |
  276. SUN4I_HDMI_PAD_CTRL0_PWENC |
  277. SUN4I_HDMI_PAD_CTRL0_LDODEN |
  278. SUN4I_HDMI_PAD_CTRL0_LDOCEN,
  279. .pad_ctrl1_init_val = SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
  280. SUN4I_HDMI_PAD_CTRL1_REG_EMP(4) |
  281. SUN4I_HDMI_PAD_CTRL1_REG_DENCK |
  282. SUN4I_HDMI_PAD_CTRL1_REG_DEN |
  283. SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT |
  284. SUN4I_HDMI_PAD_CTRL1_EMP_OPT |
  285. SUN4I_HDMI_PAD_CTRL1_PWSDT |
  286. SUN4I_HDMI_PAD_CTRL1_PWSCK |
  287. SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT |
  288. SUN4I_HDMI_PAD_CTRL1_AMP_OPT |
  289. SUN4I_HDMI_PAD_CTRL1_UNKNOWN,
  290. .pll_ctrl_init_val = SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
  291. SUN4I_HDMI_PLL_CTRL_CS(3) |
  292. SUN4I_HDMI_PLL_CTRL_CP_S(10) |
  293. SUN4I_HDMI_PLL_CTRL_S(4) |
  294. SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
  295. SUN4I_HDMI_PLL_CTRL_SDIV2 |
  296. SUN4I_HDMI_PLL_CTRL_LDO2_EN |
  297. SUN4I_HDMI_PLL_CTRL_LDO1_EN |
  298. SUN4I_HDMI_PLL_CTRL_HV_IS_33 |
  299. SUN4I_HDMI_PLL_CTRL_PLL_EN,
  300. .ddc_clk_reg = REG_FIELD(SUN6I_HDMI_DDC_CLK_REG, 0, 6),
  301. .ddc_clk_pre_divider = 1,
  302. .ddc_clk_m_offset = 2,
  303. .tmds_clk_div_offset = 1,
  304. .field_ddc_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 0, 0),
  305. .field_ddc_start = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 27, 27),
  306. .field_ddc_reset = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 31, 31),
  307. .field_ddc_addr_reg = REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG, 1, 31),
  308. .field_ddc_slave_addr = REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG, 1, 7),
  309. .field_ddc_int_status = REG_FIELD(SUN6I_HDMI_DDC_INT_STATUS_REG, 0, 8),
  310. .field_ddc_fifo_clear = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 18, 18),
  311. .field_ddc_fifo_rx_thres = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 4, 7),
  312. .field_ddc_fifo_tx_thres = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 0, 3),
  313. .field_ddc_byte_count = REG_FIELD(SUN6I_HDMI_DDC_CMD_REG, 16, 25),
  314. .field_ddc_cmd = REG_FIELD(SUN6I_HDMI_DDC_CMD_REG, 0, 2),
  315. .field_ddc_sda_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 6, 6),
  316. .field_ddc_sck_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 4, 4),
  317. .ddc_fifo_reg = SUN6I_HDMI_DDC_FIFO_DATA_REG,
  318. .ddc_fifo_thres_incl = true,
  319. };
  320. static const struct regmap_config sun4i_hdmi_regmap_config = {
  321. .reg_bits = 32,
  322. .val_bits = 32,
  323. .reg_stride = 4,
  324. .max_register = 0x580,
  325. };
  326. static int sun4i_hdmi_bind(struct device *dev, struct device *master,
  327. void *data)
  328. {
  329. struct platform_device *pdev = to_platform_device(dev);
  330. struct drm_device *drm = data;
  331. struct sun4i_drv *drv = drm->dev_private;
  332. struct sun4i_hdmi *hdmi;
  333. struct resource *res;
  334. u32 reg;
  335. int ret;
  336. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  337. if (!hdmi)
  338. return -ENOMEM;
  339. dev_set_drvdata(dev, hdmi);
  340. hdmi->dev = dev;
  341. hdmi->drv = drv;
  342. hdmi->variant = of_device_get_match_data(dev);
  343. if (!hdmi->variant)
  344. return -EINVAL;
  345. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  346. hdmi->base = devm_ioremap_resource(dev, res);
  347. if (IS_ERR(hdmi->base)) {
  348. dev_err(dev, "Couldn't map the HDMI encoder registers\n");
  349. return PTR_ERR(hdmi->base);
  350. }
  351. if (hdmi->variant->has_reset_control) {
  352. hdmi->reset = devm_reset_control_get(dev, NULL);
  353. if (IS_ERR(hdmi->reset)) {
  354. dev_err(dev, "Couldn't get the HDMI reset control\n");
  355. return PTR_ERR(hdmi->reset);
  356. }
  357. ret = reset_control_deassert(hdmi->reset);
  358. if (ret) {
  359. dev_err(dev, "Couldn't deassert HDMI reset\n");
  360. return ret;
  361. }
  362. }
  363. hdmi->bus_clk = devm_clk_get(dev, "ahb");
  364. if (IS_ERR(hdmi->bus_clk)) {
  365. dev_err(dev, "Couldn't get the HDMI bus clock\n");
  366. ret = PTR_ERR(hdmi->bus_clk);
  367. goto err_assert_reset;
  368. }
  369. clk_prepare_enable(hdmi->bus_clk);
  370. hdmi->mod_clk = devm_clk_get(dev, "mod");
  371. if (IS_ERR(hdmi->mod_clk)) {
  372. dev_err(dev, "Couldn't get the HDMI mod clock\n");
  373. ret = PTR_ERR(hdmi->mod_clk);
  374. goto err_disable_bus_clk;
  375. }
  376. clk_prepare_enable(hdmi->mod_clk);
  377. hdmi->pll0_clk = devm_clk_get(dev, "pll-0");
  378. if (IS_ERR(hdmi->pll0_clk)) {
  379. dev_err(dev, "Couldn't get the HDMI PLL 0 clock\n");
  380. ret = PTR_ERR(hdmi->pll0_clk);
  381. goto err_disable_mod_clk;
  382. }
  383. hdmi->pll1_clk = devm_clk_get(dev, "pll-1");
  384. if (IS_ERR(hdmi->pll1_clk)) {
  385. dev_err(dev, "Couldn't get the HDMI PLL 1 clock\n");
  386. ret = PTR_ERR(hdmi->pll1_clk);
  387. goto err_disable_mod_clk;
  388. }
  389. hdmi->regmap = devm_regmap_init_mmio(dev, hdmi->base,
  390. &sun4i_hdmi_regmap_config);
  391. if (IS_ERR(hdmi->regmap)) {
  392. dev_err(dev, "Couldn't create HDMI encoder regmap\n");
  393. return PTR_ERR(hdmi->regmap);
  394. }
  395. ret = sun4i_tmds_create(hdmi);
  396. if (ret) {
  397. dev_err(dev, "Couldn't create the TMDS clock\n");
  398. goto err_disable_mod_clk;
  399. }
  400. if (hdmi->variant->has_ddc_parent_clk) {
  401. hdmi->ddc_parent_clk = devm_clk_get(dev, "ddc");
  402. if (IS_ERR(hdmi->ddc_parent_clk)) {
  403. dev_err(dev, "Couldn't get the HDMI DDC clock\n");
  404. return PTR_ERR(hdmi->ddc_parent_clk);
  405. }
  406. } else {
  407. hdmi->ddc_parent_clk = hdmi->tmds_clk;
  408. }
  409. writel(SUN4I_HDMI_CTRL_ENABLE, hdmi->base + SUN4I_HDMI_CTRL_REG);
  410. writel(hdmi->variant->pad_ctrl0_init_val,
  411. hdmi->base + SUN4I_HDMI_PAD_CTRL0_REG);
  412. /*
  413. * We can't just initialize the register there, we need to
  414. * protect the clock bits that have already been read out and
  415. * cached by the clock framework.
  416. */
  417. reg = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  418. reg &= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
  419. reg |= hdmi->variant->pad_ctrl1_init_val;
  420. writel(reg, hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  421. reg = readl(hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  422. reg &= SUN4I_HDMI_PLL_CTRL_DIV_MASK;
  423. reg |= hdmi->variant->pll_ctrl_init_val;
  424. writel(reg, hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  425. ret = sun4i_hdmi_i2c_create(dev, hdmi);
  426. if (ret) {
  427. dev_err(dev, "Couldn't create the HDMI I2C adapter\n");
  428. goto err_disable_mod_clk;
  429. }
  430. drm_encoder_helper_add(&hdmi->encoder,
  431. &sun4i_hdmi_helper_funcs);
  432. ret = drm_encoder_init(drm,
  433. &hdmi->encoder,
  434. &sun4i_hdmi_funcs,
  435. DRM_MODE_ENCODER_TMDS,
  436. NULL);
  437. if (ret) {
  438. dev_err(dev, "Couldn't initialise the HDMI encoder\n");
  439. goto err_del_i2c_adapter;
  440. }
  441. hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm,
  442. dev->of_node);
  443. if (!hdmi->encoder.possible_crtcs) {
  444. ret = -EPROBE_DEFER;
  445. goto err_del_i2c_adapter;
  446. }
  447. #ifdef CONFIG_DRM_SUN4I_HDMI_CEC
  448. hdmi->cec_adap = cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops,
  449. hdmi, "sun4i", CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
  450. CEC_CAP_PASSTHROUGH | CEC_CAP_RC);
  451. ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
  452. if (ret < 0)
  453. goto err_cleanup_connector;
  454. writel(readl(hdmi->base + SUN4I_HDMI_CEC) & ~SUN4I_HDMI_CEC_TX,
  455. hdmi->base + SUN4I_HDMI_CEC);
  456. #endif
  457. drm_connector_helper_add(&hdmi->connector,
  458. &sun4i_hdmi_connector_helper_funcs);
  459. ret = drm_connector_init(drm, &hdmi->connector,
  460. &sun4i_hdmi_connector_funcs,
  461. DRM_MODE_CONNECTOR_HDMIA);
  462. if (ret) {
  463. dev_err(dev,
  464. "Couldn't initialise the HDMI connector\n");
  465. goto err_cleanup_connector;
  466. }
  467. /* There is no HPD interrupt, so we need to poll the controller */
  468. hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
  469. DRM_CONNECTOR_POLL_DISCONNECT;
  470. ret = cec_register_adapter(hdmi->cec_adap, dev);
  471. if (ret < 0)
  472. goto err_cleanup_connector;
  473. drm_mode_connector_attach_encoder(&hdmi->connector, &hdmi->encoder);
  474. return 0;
  475. err_cleanup_connector:
  476. cec_delete_adapter(hdmi->cec_adap);
  477. drm_encoder_cleanup(&hdmi->encoder);
  478. err_del_i2c_adapter:
  479. i2c_del_adapter(hdmi->i2c);
  480. err_disable_mod_clk:
  481. clk_disable_unprepare(hdmi->mod_clk);
  482. err_disable_bus_clk:
  483. clk_disable_unprepare(hdmi->bus_clk);
  484. err_assert_reset:
  485. reset_control_assert(hdmi->reset);
  486. return ret;
  487. }
  488. static void sun4i_hdmi_unbind(struct device *dev, struct device *master,
  489. void *data)
  490. {
  491. struct sun4i_hdmi *hdmi = dev_get_drvdata(dev);
  492. cec_unregister_adapter(hdmi->cec_adap);
  493. drm_connector_cleanup(&hdmi->connector);
  494. drm_encoder_cleanup(&hdmi->encoder);
  495. i2c_del_adapter(hdmi->i2c);
  496. clk_disable_unprepare(hdmi->mod_clk);
  497. clk_disable_unprepare(hdmi->bus_clk);
  498. }
  499. static const struct component_ops sun4i_hdmi_ops = {
  500. .bind = sun4i_hdmi_bind,
  501. .unbind = sun4i_hdmi_unbind,
  502. };
  503. static int sun4i_hdmi_probe(struct platform_device *pdev)
  504. {
  505. return component_add(&pdev->dev, &sun4i_hdmi_ops);
  506. }
  507. static int sun4i_hdmi_remove(struct platform_device *pdev)
  508. {
  509. component_del(&pdev->dev, &sun4i_hdmi_ops);
  510. return 0;
  511. }
  512. static const struct of_device_id sun4i_hdmi_of_table[] = {
  513. { .compatible = "allwinner,sun5i-a10s-hdmi", .data = &sun5i_variant, },
  514. { .compatible = "allwinner,sun6i-a31-hdmi", .data = &sun6i_variant, },
  515. { }
  516. };
  517. MODULE_DEVICE_TABLE(of, sun4i_hdmi_of_table);
  518. static struct platform_driver sun4i_hdmi_driver = {
  519. .probe = sun4i_hdmi_probe,
  520. .remove = sun4i_hdmi_remove,
  521. .driver = {
  522. .name = "sun4i-hdmi",
  523. .of_match_table = sun4i_hdmi_of_table,
  524. },
  525. };
  526. module_platform_driver(sun4i_hdmi_driver);
  527. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  528. MODULE_DESCRIPTION("Allwinner A10 HDMI Driver");
  529. MODULE_LICENSE("GPL");