sun4i_hdmi_enc.c 21 KB

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