sun4i_hdmi_enc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include "sun4i_backend.h"
  24. #include "sun4i_crtc.h"
  25. #include "sun4i_drv.h"
  26. #include "sun4i_hdmi.h"
  27. #include "sun4i_tcon.h"
  28. static inline struct sun4i_hdmi *
  29. drm_encoder_to_sun4i_hdmi(struct drm_encoder *encoder)
  30. {
  31. return container_of(encoder, struct sun4i_hdmi,
  32. encoder);
  33. }
  34. static inline struct sun4i_hdmi *
  35. drm_connector_to_sun4i_hdmi(struct drm_connector *connector)
  36. {
  37. return container_of(connector, struct sun4i_hdmi,
  38. connector);
  39. }
  40. static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi,
  41. struct drm_display_mode *mode)
  42. {
  43. struct hdmi_avi_infoframe frame;
  44. u8 buffer[17];
  45. int i, ret;
  46. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
  47. if (ret < 0) {
  48. DRM_ERROR("Failed to get infoframes from mode\n");
  49. return ret;
  50. }
  51. ret = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  52. if (ret < 0) {
  53. DRM_ERROR("Failed to pack infoframes\n");
  54. return ret;
  55. }
  56. for (i = 0; i < sizeof(buffer); i++)
  57. writeb(buffer[i], hdmi->base + SUN4I_HDMI_AVI_INFOFRAME_REG(i));
  58. return 0;
  59. }
  60. static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder,
  61. struct drm_crtc_state *crtc_state,
  62. struct drm_connector_state *conn_state)
  63. {
  64. struct drm_display_mode *mode = &crtc_state->mode;
  65. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  66. return -EINVAL;
  67. return 0;
  68. }
  69. static void sun4i_hdmi_disable(struct drm_encoder *encoder)
  70. {
  71. struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
  72. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  73. struct sun4i_tcon *tcon = crtc->tcon;
  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. sun4i_tcon_channel_disable(tcon, 1);
  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. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  86. struct sun4i_tcon *tcon = crtc->tcon;
  87. u32 val = 0;
  88. DRM_DEBUG_DRIVER("Enabling the HDMI Output\n");
  89. sun4i_tcon_channel_enable(tcon, 1);
  90. sun4i_hdmi_setup_avi_infoframes(hdmi, mode);
  91. val |= SUN4I_HDMI_PKT_CTRL_TYPE(0, SUN4I_HDMI_PKT_AVI);
  92. val |= SUN4I_HDMI_PKT_CTRL_TYPE(1, SUN4I_HDMI_PKT_END);
  93. writel(val, hdmi->base + SUN4I_HDMI_PKT_CTRL_REG(0));
  94. val = SUN4I_HDMI_VID_CTRL_ENABLE;
  95. if (hdmi->hdmi_monitor)
  96. val |= SUN4I_HDMI_VID_CTRL_HDMI_MODE;
  97. writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
  98. }
  99. static void sun4i_hdmi_mode_set(struct drm_encoder *encoder,
  100. struct drm_display_mode *mode,
  101. struct drm_display_mode *adjusted_mode)
  102. {
  103. struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
  104. struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
  105. struct sun4i_tcon *tcon = crtc->tcon;
  106. unsigned int x, y;
  107. u32 val;
  108. sun4i_tcon1_mode_set(tcon, mode);
  109. sun4i_tcon_set_mux(tcon, 1, encoder);
  110. clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
  111. clk_set_rate(hdmi->mod_clk, mode->crtc_clock * 1000);
  112. clk_set_rate(hdmi->tmds_clk, mode->crtc_clock * 1000);
  113. /* Set input sync enable */
  114. writel(SUN4I_HDMI_UNKNOWN_INPUT_SYNC,
  115. hdmi->base + SUN4I_HDMI_UNKNOWN_REG);
  116. /* Setup timing registers */
  117. writel(SUN4I_HDMI_VID_TIMING_X(mode->hdisplay) |
  118. SUN4I_HDMI_VID_TIMING_Y(mode->vdisplay),
  119. hdmi->base + SUN4I_HDMI_VID_TIMING_ACT_REG);
  120. x = mode->htotal - mode->hsync_start;
  121. y = mode->vtotal - mode->vsync_start;
  122. writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
  123. hdmi->base + SUN4I_HDMI_VID_TIMING_BP_REG);
  124. x = mode->hsync_start - mode->hdisplay;
  125. y = mode->vsync_start - mode->vdisplay;
  126. writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
  127. hdmi->base + SUN4I_HDMI_VID_TIMING_FP_REG);
  128. x = mode->hsync_end - mode->hsync_start;
  129. y = mode->vsync_end - mode->vsync_start;
  130. writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
  131. hdmi->base + SUN4I_HDMI_VID_TIMING_SPW_REG);
  132. val = SUN4I_HDMI_VID_TIMING_POL_TX_CLK;
  133. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  134. val |= SUN4I_HDMI_VID_TIMING_POL_HSYNC;
  135. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  136. val |= SUN4I_HDMI_VID_TIMING_POL_VSYNC;
  137. writel(val, hdmi->base + SUN4I_HDMI_VID_TIMING_POL_REG);
  138. }
  139. static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = {
  140. .atomic_check = sun4i_hdmi_atomic_check,
  141. .disable = sun4i_hdmi_disable,
  142. .enable = sun4i_hdmi_enable,
  143. .mode_set = sun4i_hdmi_mode_set,
  144. };
  145. static const struct drm_encoder_funcs sun4i_hdmi_funcs = {
  146. .destroy = drm_encoder_cleanup,
  147. };
  148. static int sun4i_hdmi_get_modes(struct drm_connector *connector)
  149. {
  150. struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
  151. struct edid *edid;
  152. int ret;
  153. edid = drm_get_edid(connector, hdmi->i2c);
  154. if (!edid)
  155. return 0;
  156. hdmi->hdmi_monitor = drm_detect_hdmi_monitor(edid);
  157. DRM_DEBUG_DRIVER("Monitor is %s monitor\n",
  158. hdmi->hdmi_monitor ? "an HDMI" : "a DVI");
  159. drm_mode_connector_update_edid_property(connector, edid);
  160. cec_s_phys_addr_from_edid(hdmi->cec_adap, edid);
  161. ret = drm_add_edid_modes(connector, edid);
  162. kfree(edid);
  163. return ret;
  164. }
  165. static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = {
  166. .get_modes = sun4i_hdmi_get_modes,
  167. };
  168. static enum drm_connector_status
  169. sun4i_hdmi_connector_detect(struct drm_connector *connector, bool force)
  170. {
  171. struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
  172. unsigned long reg;
  173. if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_HPD_REG, reg,
  174. reg & SUN4I_HDMI_HPD_HIGH,
  175. 0, 500000)) {
  176. cec_phys_addr_invalidate(hdmi->cec_adap);
  177. return connector_status_disconnected;
  178. }
  179. return connector_status_connected;
  180. }
  181. static const struct drm_connector_funcs sun4i_hdmi_connector_funcs = {
  182. .detect = sun4i_hdmi_connector_detect,
  183. .fill_modes = drm_helper_probe_single_connector_modes,
  184. .destroy = drm_connector_cleanup,
  185. .reset = drm_atomic_helper_connector_reset,
  186. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  187. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  188. };
  189. #ifdef CONFIG_DRM_SUN4I_HDMI_CEC
  190. static bool sun4i_hdmi_cec_pin_read(struct cec_adapter *adap)
  191. {
  192. struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
  193. return readl(hdmi->base + SUN4I_HDMI_CEC) & SUN4I_HDMI_CEC_RX;
  194. }
  195. static void sun4i_hdmi_cec_pin_low(struct cec_adapter *adap)
  196. {
  197. struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
  198. /* Start driving the CEC pin low */
  199. writel(SUN4I_HDMI_CEC_ENABLE, hdmi->base + SUN4I_HDMI_CEC);
  200. }
  201. static void sun4i_hdmi_cec_pin_high(struct cec_adapter *adap)
  202. {
  203. struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
  204. /*
  205. * Stop driving the CEC pin, the pull up will take over
  206. * unless another CEC device is driving the pin low.
  207. */
  208. writel(0, hdmi->base + SUN4I_HDMI_CEC);
  209. }
  210. static const struct cec_pin_ops sun4i_hdmi_cec_pin_ops = {
  211. .read = sun4i_hdmi_cec_pin_read,
  212. .low = sun4i_hdmi_cec_pin_low,
  213. .high = sun4i_hdmi_cec_pin_high,
  214. };
  215. #endif
  216. static int sun4i_hdmi_bind(struct device *dev, struct device *master,
  217. void *data)
  218. {
  219. struct platform_device *pdev = to_platform_device(dev);
  220. struct drm_device *drm = data;
  221. struct sun4i_drv *drv = drm->dev_private;
  222. struct sun4i_hdmi *hdmi;
  223. struct resource *res;
  224. u32 reg;
  225. int ret;
  226. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  227. if (!hdmi)
  228. return -ENOMEM;
  229. dev_set_drvdata(dev, hdmi);
  230. hdmi->dev = dev;
  231. hdmi->drv = drv;
  232. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  233. hdmi->base = devm_ioremap_resource(dev, res);
  234. if (IS_ERR(hdmi->base)) {
  235. dev_err(dev, "Couldn't map the HDMI encoder registers\n");
  236. return PTR_ERR(hdmi->base);
  237. }
  238. hdmi->bus_clk = devm_clk_get(dev, "ahb");
  239. if (IS_ERR(hdmi->bus_clk)) {
  240. dev_err(dev, "Couldn't get the HDMI bus clock\n");
  241. return PTR_ERR(hdmi->bus_clk);
  242. }
  243. clk_prepare_enable(hdmi->bus_clk);
  244. hdmi->mod_clk = devm_clk_get(dev, "mod");
  245. if (IS_ERR(hdmi->mod_clk)) {
  246. dev_err(dev, "Couldn't get the HDMI mod clock\n");
  247. ret = PTR_ERR(hdmi->mod_clk);
  248. goto err_disable_bus_clk;
  249. }
  250. clk_prepare_enable(hdmi->mod_clk);
  251. hdmi->pll0_clk = devm_clk_get(dev, "pll-0");
  252. if (IS_ERR(hdmi->pll0_clk)) {
  253. dev_err(dev, "Couldn't get the HDMI PLL 0 clock\n");
  254. ret = PTR_ERR(hdmi->pll0_clk);
  255. goto err_disable_mod_clk;
  256. }
  257. hdmi->pll1_clk = devm_clk_get(dev, "pll-1");
  258. if (IS_ERR(hdmi->pll1_clk)) {
  259. dev_err(dev, "Couldn't get the HDMI PLL 1 clock\n");
  260. ret = PTR_ERR(hdmi->pll1_clk);
  261. goto err_disable_mod_clk;
  262. }
  263. ret = sun4i_tmds_create(hdmi);
  264. if (ret) {
  265. dev_err(dev, "Couldn't create the TMDS clock\n");
  266. goto err_disable_mod_clk;
  267. }
  268. writel(SUN4I_HDMI_CTRL_ENABLE, hdmi->base + SUN4I_HDMI_CTRL_REG);
  269. writel(SUN4I_HDMI_PAD_CTRL0_TXEN | SUN4I_HDMI_PAD_CTRL0_CKEN |
  270. SUN4I_HDMI_PAD_CTRL0_PWENG | SUN4I_HDMI_PAD_CTRL0_PWEND |
  271. SUN4I_HDMI_PAD_CTRL0_PWENC | SUN4I_HDMI_PAD_CTRL0_LDODEN |
  272. SUN4I_HDMI_PAD_CTRL0_LDOCEN | SUN4I_HDMI_PAD_CTRL0_BIASEN,
  273. hdmi->base + SUN4I_HDMI_PAD_CTRL0_REG);
  274. /*
  275. * We can't just initialize the register there, we need to
  276. * protect the clock bits that have already been read out and
  277. * cached by the clock framework.
  278. */
  279. reg = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  280. reg &= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
  281. reg |= SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
  282. SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) |
  283. SUN4I_HDMI_PAD_CTRL1_REG_DENCK |
  284. SUN4I_HDMI_PAD_CTRL1_REG_DEN |
  285. SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT |
  286. SUN4I_HDMI_PAD_CTRL1_EMP_OPT |
  287. SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT |
  288. SUN4I_HDMI_PAD_CTRL1_AMP_OPT;
  289. writel(reg, hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  290. reg = readl(hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  291. reg &= SUN4I_HDMI_PLL_CTRL_DIV_MASK;
  292. reg |= SUN4I_HDMI_PLL_CTRL_VCO_S(8) | SUN4I_HDMI_PLL_CTRL_CS(7) |
  293. SUN4I_HDMI_PLL_CTRL_CP_S(15) | SUN4I_HDMI_PLL_CTRL_S(7) |
  294. SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) | SUN4I_HDMI_PLL_CTRL_SDIV2 |
  295. SUN4I_HDMI_PLL_CTRL_LDO2_EN | SUN4I_HDMI_PLL_CTRL_LDO1_EN |
  296. SUN4I_HDMI_PLL_CTRL_HV_IS_33 | SUN4I_HDMI_PLL_CTRL_BWS |
  297. SUN4I_HDMI_PLL_CTRL_PLL_EN;
  298. writel(reg, hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  299. ret = sun4i_hdmi_i2c_create(dev, hdmi);
  300. if (ret) {
  301. dev_err(dev, "Couldn't create the HDMI I2C adapter\n");
  302. goto err_disable_mod_clk;
  303. }
  304. drm_encoder_helper_add(&hdmi->encoder,
  305. &sun4i_hdmi_helper_funcs);
  306. ret = drm_encoder_init(drm,
  307. &hdmi->encoder,
  308. &sun4i_hdmi_funcs,
  309. DRM_MODE_ENCODER_TMDS,
  310. NULL);
  311. if (ret) {
  312. dev_err(dev, "Couldn't initialise the HDMI encoder\n");
  313. goto err_del_i2c_adapter;
  314. }
  315. hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm,
  316. dev->of_node);
  317. if (!hdmi->encoder.possible_crtcs) {
  318. ret = -EPROBE_DEFER;
  319. goto err_del_i2c_adapter;
  320. }
  321. #ifdef CONFIG_DRM_SUN4I_HDMI_CEC
  322. hdmi->cec_adap = cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops,
  323. hdmi, "sun4i", CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
  324. CEC_CAP_PASSTHROUGH | CEC_CAP_RC);
  325. ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
  326. if (ret < 0)
  327. goto err_cleanup_connector;
  328. writel(readl(hdmi->base + SUN4I_HDMI_CEC) & ~SUN4I_HDMI_CEC_TX,
  329. hdmi->base + SUN4I_HDMI_CEC);
  330. #endif
  331. drm_connector_helper_add(&hdmi->connector,
  332. &sun4i_hdmi_connector_helper_funcs);
  333. ret = drm_connector_init(drm, &hdmi->connector,
  334. &sun4i_hdmi_connector_funcs,
  335. DRM_MODE_CONNECTOR_HDMIA);
  336. if (ret) {
  337. dev_err(dev,
  338. "Couldn't initialise the HDMI connector\n");
  339. goto err_cleanup_connector;
  340. }
  341. /* There is no HPD interrupt, so we need to poll the controller */
  342. hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
  343. DRM_CONNECTOR_POLL_DISCONNECT;
  344. ret = cec_register_adapter(hdmi->cec_adap, dev);
  345. if (ret < 0)
  346. goto err_cleanup_connector;
  347. drm_mode_connector_attach_encoder(&hdmi->connector, &hdmi->encoder);
  348. return 0;
  349. err_cleanup_connector:
  350. cec_delete_adapter(hdmi->cec_adap);
  351. drm_encoder_cleanup(&hdmi->encoder);
  352. err_del_i2c_adapter:
  353. i2c_del_adapter(hdmi->i2c);
  354. err_disable_mod_clk:
  355. clk_disable_unprepare(hdmi->mod_clk);
  356. err_disable_bus_clk:
  357. clk_disable_unprepare(hdmi->bus_clk);
  358. return ret;
  359. }
  360. static void sun4i_hdmi_unbind(struct device *dev, struct device *master,
  361. void *data)
  362. {
  363. struct sun4i_hdmi *hdmi = dev_get_drvdata(dev);
  364. cec_unregister_adapter(hdmi->cec_adap);
  365. drm_connector_cleanup(&hdmi->connector);
  366. drm_encoder_cleanup(&hdmi->encoder);
  367. i2c_del_adapter(hdmi->i2c);
  368. clk_disable_unprepare(hdmi->mod_clk);
  369. clk_disable_unprepare(hdmi->bus_clk);
  370. }
  371. static const struct component_ops sun4i_hdmi_ops = {
  372. .bind = sun4i_hdmi_bind,
  373. .unbind = sun4i_hdmi_unbind,
  374. };
  375. static int sun4i_hdmi_probe(struct platform_device *pdev)
  376. {
  377. return component_add(&pdev->dev, &sun4i_hdmi_ops);
  378. }
  379. static int sun4i_hdmi_remove(struct platform_device *pdev)
  380. {
  381. component_del(&pdev->dev, &sun4i_hdmi_ops);
  382. return 0;
  383. }
  384. static const struct of_device_id sun4i_hdmi_of_table[] = {
  385. { .compatible = "allwinner,sun5i-a10s-hdmi" },
  386. { }
  387. };
  388. MODULE_DEVICE_TABLE(of, sun4i_hdmi_of_table);
  389. static struct platform_driver sun4i_hdmi_driver = {
  390. .probe = sun4i_hdmi_probe,
  391. .remove = sun4i_hdmi_remove,
  392. .driver = {
  393. .name = "sun4i-hdmi",
  394. .of_match_table = sun4i_hdmi_of_table,
  395. },
  396. };
  397. module_platform_driver(sun4i_hdmi_driver);
  398. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  399. MODULE_DESCRIPTION("Allwinner A10 HDMI Driver");
  400. MODULE_LICENSE("GPL");