sun4i_hdmi_enc.c 21 KB

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