dw_hdmi-rockchip.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2014, Rockchip Electronics Co., Ltd.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/hw_bitfield.h>
  7. #include <linux/mfd/syscon.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <drm/bridge/dw_hdmi.h>
  14. #include <drm/drm_edid.h>
  15. #include <drm/drm_of.h>
  16. #include <drm/drm_probe_helper.h>
  17. #include <drm/drm_simple_kms_helper.h>
  18. #include "rockchip_drm_drv.h"
  19. #define RK3228_GRF_SOC_CON2 0x0408
  20. #define RK3228_HDMI_SDAIN_MSK BIT(14)
  21. #define RK3228_HDMI_SCLIN_MSK BIT(13)
  22. #define RK3228_GRF_SOC_CON6 0x0418
  23. #define RK3228_HDMI_HPD_VSEL BIT(6)
  24. #define RK3228_HDMI_SDA_VSEL BIT(5)
  25. #define RK3228_HDMI_SCL_VSEL BIT(4)
  26. #define RK3288_GRF_SOC_CON6 0x025C
  27. #define RK3288_HDMI_LCDC_SEL BIT(4)
  28. #define RK3328_GRF_SOC_CON2 0x0408
  29. #define RK3328_HDMI_SDAIN_MSK BIT(11)
  30. #define RK3328_HDMI_SCLIN_MSK BIT(10)
  31. #define RK3328_HDMI_HPD_IOE BIT(2)
  32. #define RK3328_GRF_SOC_CON3 0x040c
  33. /* need to be unset if hdmi or i2c should control voltage */
  34. #define RK3328_HDMI_SDA5V_GRF BIT(15)
  35. #define RK3328_HDMI_SCL5V_GRF BIT(14)
  36. #define RK3328_HDMI_HPD5V_GRF BIT(13)
  37. #define RK3328_HDMI_CEC5V_GRF BIT(12)
  38. #define RK3328_GRF_SOC_CON4 0x0410
  39. #define RK3328_HDMI_HPD_SARADC BIT(13)
  40. #define RK3328_HDMI_CEC_5V BIT(11)
  41. #define RK3328_HDMI_SDA_5V BIT(10)
  42. #define RK3328_HDMI_SCL_5V BIT(9)
  43. #define RK3328_HDMI_HPD_5V BIT(8)
  44. #define RK3399_GRF_SOC_CON20 0x6250
  45. #define RK3399_HDMI_LCDC_SEL BIT(6)
  46. #define RK3568_GRF_VO_CON1 0x0364
  47. #define RK3568_HDMI_SDAIN_MSK BIT(15)
  48. #define RK3568_HDMI_SCLIN_MSK BIT(14)
  49. /**
  50. * struct rockchip_hdmi_chip_data - splite the grf setting of kind of chips
  51. * @lcdsel_grf_reg: grf register offset of lcdc select
  52. * @lcdsel_big: reg value of selecting vop big for HDMI
  53. * @lcdsel_lit: reg value of selecting vop little for HDMI
  54. * @max_tmds_clock: maximum TMDS clock rate supported
  55. */
  56. struct rockchip_hdmi_chip_data {
  57. int lcdsel_grf_reg;
  58. u32 lcdsel_big;
  59. u32 lcdsel_lit;
  60. int max_tmds_clock;
  61. };
  62. struct rockchip_hdmi {
  63. struct device *dev;
  64. struct regmap *regmap;
  65. struct rockchip_encoder encoder;
  66. const struct rockchip_hdmi_chip_data *chip_data;
  67. const struct dw_hdmi_plat_data *plat_data;
  68. struct clk *hdmiphy_clk;
  69. struct clk *ref_clk;
  70. struct clk *grf_clk;
  71. struct dw_hdmi *hdmi;
  72. struct phy *phy;
  73. };
  74. static struct rockchip_hdmi *to_rockchip_hdmi(struct drm_encoder *encoder)
  75. {
  76. struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
  77. return container_of(rkencoder, struct rockchip_hdmi, encoder);
  78. }
  79. static const struct dw_hdmi_mpll_config rockchip_mpll_cfg[] = {
  80. {
  81. 30666000, {
  82. { 0x00b3, 0x0000 },
  83. { 0x2153, 0x0000 },
  84. { 0x40f3, 0x0000 },
  85. },
  86. }, {
  87. 36800000, {
  88. { 0x00b3, 0x0000 },
  89. { 0x2153, 0x0000 },
  90. { 0x40a2, 0x0001 },
  91. },
  92. }, {
  93. 46000000, {
  94. { 0x00b3, 0x0000 },
  95. { 0x2142, 0x0001 },
  96. { 0x40a2, 0x0001 },
  97. },
  98. }, {
  99. 61333000, {
  100. { 0x0072, 0x0001 },
  101. { 0x2142, 0x0001 },
  102. { 0x40a2, 0x0001 },
  103. },
  104. }, {
  105. 73600000, {
  106. { 0x0072, 0x0001 },
  107. { 0x2142, 0x0001 },
  108. { 0x4061, 0x0002 },
  109. },
  110. }, {
  111. 92000000, {
  112. { 0x0072, 0x0001 },
  113. { 0x2145, 0x0002 },
  114. { 0x4061, 0x0002 },
  115. },
  116. }, {
  117. 122666000, {
  118. { 0x0051, 0x0002 },
  119. { 0x2145, 0x0002 },
  120. { 0x4061, 0x0002 },
  121. },
  122. }, {
  123. 147200000, {
  124. { 0x0051, 0x0002 },
  125. { 0x2145, 0x0002 },
  126. { 0x4064, 0x0003 },
  127. },
  128. }, {
  129. 184000000, {
  130. { 0x0051, 0x0002 },
  131. { 0x214c, 0x0003 },
  132. { 0x4064, 0x0003 },
  133. },
  134. }, {
  135. 226666000, {
  136. { 0x0040, 0x0003 },
  137. { 0x214c, 0x0003 },
  138. { 0x4064, 0x0003 },
  139. },
  140. }, {
  141. 272000000, {
  142. { 0x0040, 0x0003 },
  143. { 0x214c, 0x0003 },
  144. { 0x5a64, 0x0003 },
  145. },
  146. }, {
  147. 340000000, {
  148. { 0x0040, 0x0003 },
  149. { 0x3b4c, 0x0003 },
  150. { 0x5a64, 0x0003 },
  151. },
  152. }, {
  153. 600000000, {
  154. { 0x1a40, 0x0003 },
  155. { 0x3b4c, 0x0003 },
  156. { 0x5a64, 0x0003 },
  157. },
  158. }, {
  159. ~0UL, {
  160. { 0x0000, 0x0000 },
  161. { 0x0000, 0x0000 },
  162. { 0x0000, 0x0000 },
  163. },
  164. }
  165. };
  166. static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr[] = {
  167. /* pixelclk bpp8 bpp10 bpp12 */
  168. {
  169. 600000000, { 0x0000, 0x0000, 0x0000 },
  170. }, {
  171. ~0UL, { 0x0000, 0x0000, 0x0000 },
  172. }
  173. };
  174. static const struct dw_hdmi_phy_config rockchip_phy_config[] = {
  175. /*pixelclk symbol term vlev*/
  176. { 74250000, 0x8009, 0x0004, 0x0272},
  177. { 165000000, 0x802b, 0x0004, 0x0209},
  178. { 297000000, 0x8039, 0x0005, 0x028d},
  179. { 594000000, 0x8039, 0x0000, 0x019d},
  180. { ~0UL, 0x0000, 0x0000, 0x0000}
  181. };
  182. static int rockchip_hdmi_parse_dt(struct rockchip_hdmi *hdmi)
  183. {
  184. struct device_node *np = hdmi->dev->of_node;
  185. int ret;
  186. hdmi->regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  187. if (IS_ERR(hdmi->regmap)) {
  188. dev_err(hdmi->dev, "Unable to get rockchip,grf\n");
  189. return PTR_ERR(hdmi->regmap);
  190. }
  191. hdmi->ref_clk = devm_clk_get_optional_enabled(hdmi->dev, "ref");
  192. if (!hdmi->ref_clk)
  193. hdmi->ref_clk = devm_clk_get_optional_enabled(hdmi->dev, "vpll");
  194. if (IS_ERR(hdmi->ref_clk)) {
  195. ret = PTR_ERR(hdmi->ref_clk);
  196. return dev_err_probe(hdmi->dev, ret, "failed to get reference clock\n");
  197. }
  198. hdmi->grf_clk = devm_clk_get_optional(hdmi->dev, "grf");
  199. if (IS_ERR(hdmi->grf_clk)) {
  200. ret = PTR_ERR(hdmi->grf_clk);
  201. return dev_err_probe(hdmi->dev, ret, "failed to get grf clock\n");
  202. }
  203. ret = devm_regulator_get_enable(hdmi->dev, "avdd-0v9");
  204. if (ret)
  205. return ret;
  206. ret = devm_regulator_get_enable(hdmi->dev, "avdd-1v8");
  207. return ret;
  208. }
  209. static enum drm_mode_status
  210. dw_hdmi_rockchip_mode_valid(struct dw_hdmi *dw_hdmi, void *data,
  211. const struct drm_display_info *info,
  212. const struct drm_display_mode *mode)
  213. {
  214. struct rockchip_hdmi *hdmi = data;
  215. int pclk = mode->clock * 1000;
  216. if (hdmi->chip_data->max_tmds_clock &&
  217. mode->clock > hdmi->chip_data->max_tmds_clock)
  218. return MODE_CLOCK_HIGH;
  219. if (hdmi->ref_clk) {
  220. int rpclk = clk_round_rate(hdmi->ref_clk, pclk);
  221. if (rpclk < 0 || abs(rpclk - pclk) > pclk / 1000)
  222. return MODE_NOCLOCK;
  223. }
  224. if (hdmi->hdmiphy_clk) {
  225. int rpclk = clk_round_rate(hdmi->hdmiphy_clk, pclk);
  226. if (rpclk < 0 || abs(rpclk - pclk) > pclk / 1000)
  227. return MODE_NOCLOCK;
  228. }
  229. return MODE_OK;
  230. }
  231. static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder *encoder)
  232. {
  233. }
  234. static bool
  235. dw_hdmi_rockchip_encoder_mode_fixup(struct drm_encoder *encoder,
  236. const struct drm_display_mode *mode,
  237. struct drm_display_mode *adj_mode)
  238. {
  239. return true;
  240. }
  241. static void dw_hdmi_rockchip_encoder_mode_set(struct drm_encoder *encoder,
  242. struct drm_display_mode *mode,
  243. struct drm_display_mode *adj_mode)
  244. {
  245. struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
  246. clk_set_rate(hdmi->ref_clk, adj_mode->clock * 1000);
  247. }
  248. static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder *encoder)
  249. {
  250. struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
  251. u32 val;
  252. int ret;
  253. if (hdmi->chip_data->lcdsel_grf_reg < 0)
  254. return;
  255. ret = drm_of_encoder_active_endpoint_id(hdmi->dev->of_node, encoder);
  256. if (ret)
  257. val = hdmi->chip_data->lcdsel_lit;
  258. else
  259. val = hdmi->chip_data->lcdsel_big;
  260. ret = clk_prepare_enable(hdmi->grf_clk);
  261. if (ret < 0) {
  262. dev_err(hdmi->dev, "failed to enable grfclk %d\n", ret);
  263. return;
  264. }
  265. ret = regmap_write(hdmi->regmap, hdmi->chip_data->lcdsel_grf_reg, val);
  266. if (ret != 0)
  267. dev_err(hdmi->dev, "Could not write to GRF: %d\n", ret);
  268. clk_disable_unprepare(hdmi->grf_clk);
  269. dev_dbg(hdmi->dev, "vop %s output to hdmi\n", ret ? "LIT" : "BIG");
  270. }
  271. static int
  272. dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder *encoder,
  273. struct drm_crtc_state *crtc_state,
  274. struct drm_connector_state *conn_state)
  275. {
  276. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  277. s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
  278. s->output_type = DRM_MODE_CONNECTOR_HDMIA;
  279. return 0;
  280. }
  281. static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs = {
  282. .mode_fixup = dw_hdmi_rockchip_encoder_mode_fixup,
  283. .mode_set = dw_hdmi_rockchip_encoder_mode_set,
  284. .enable = dw_hdmi_rockchip_encoder_enable,
  285. .disable = dw_hdmi_rockchip_encoder_disable,
  286. .atomic_check = dw_hdmi_rockchip_encoder_atomic_check,
  287. };
  288. static int dw_hdmi_rockchip_genphy_init(struct dw_hdmi *dw_hdmi, void *data,
  289. const struct drm_display_info *display,
  290. const struct drm_display_mode *mode)
  291. {
  292. struct rockchip_hdmi *hdmi = (struct rockchip_hdmi *)data;
  293. dw_hdmi_set_high_tmds_clock_ratio(dw_hdmi, display);
  294. return phy_power_on(hdmi->phy);
  295. }
  296. static void dw_hdmi_rockchip_genphy_disable(struct dw_hdmi *dw_hdmi, void *data)
  297. {
  298. struct rockchip_hdmi *hdmi = (struct rockchip_hdmi *)data;
  299. phy_power_off(hdmi->phy);
  300. }
  301. static void dw_hdmi_rk3228_setup_hpd(struct dw_hdmi *dw_hdmi, void *data)
  302. {
  303. struct rockchip_hdmi *hdmi = (struct rockchip_hdmi *)data;
  304. dw_hdmi_phy_setup_hpd(dw_hdmi, data);
  305. regmap_write(hdmi->regmap, RK3228_GRF_SOC_CON6,
  306. FIELD_PREP_WM16(RK3228_HDMI_HPD_VSEL, 1) |
  307. FIELD_PREP_WM16(RK3228_HDMI_SDA_VSEL, 1) |
  308. FIELD_PREP_WM16(RK3228_HDMI_SCL_VSEL, 1));
  309. regmap_write(hdmi->regmap, RK3228_GRF_SOC_CON2,
  310. FIELD_PREP_WM16(RK3228_HDMI_SDAIN_MSK, 1) |
  311. FIELD_PREP_WM16(RK3228_HDMI_SCLIN_MSK, 1));
  312. }
  313. static enum drm_connector_status
  314. dw_hdmi_rk3328_read_hpd(struct dw_hdmi *dw_hdmi, void *data)
  315. {
  316. struct rockchip_hdmi *hdmi = (struct rockchip_hdmi *)data;
  317. enum drm_connector_status status;
  318. status = dw_hdmi_phy_read_hpd(dw_hdmi, data);
  319. if (status == connector_status_connected)
  320. regmap_write(hdmi->regmap, RK3328_GRF_SOC_CON4,
  321. FIELD_PREP_WM16(RK3328_HDMI_SDA_5V, 1) |
  322. FIELD_PREP_WM16(RK3328_HDMI_SCL_5V, 1));
  323. else
  324. regmap_write(hdmi->regmap, RK3328_GRF_SOC_CON4,
  325. FIELD_PREP_WM16(RK3328_HDMI_SDA_5V, 0) |
  326. FIELD_PREP_WM16(RK3328_HDMI_SCL_5V, 0));
  327. return status;
  328. }
  329. static void dw_hdmi_rk3328_setup_hpd(struct dw_hdmi *dw_hdmi, void *data)
  330. {
  331. struct rockchip_hdmi *hdmi = (struct rockchip_hdmi *)data;
  332. dw_hdmi_phy_setup_hpd(dw_hdmi, data);
  333. /* Enable and map pins to 3V grf-controlled io-voltage */
  334. regmap_write(hdmi->regmap, RK3328_GRF_SOC_CON4,
  335. FIELD_PREP_WM16(RK3328_HDMI_HPD_SARADC, 0) |
  336. FIELD_PREP_WM16(RK3328_HDMI_CEC_5V, 0) |
  337. FIELD_PREP_WM16(RK3328_HDMI_SDA_5V, 0) |
  338. FIELD_PREP_WM16(RK3328_HDMI_SCL_5V, 0) |
  339. FIELD_PREP_WM16(RK3328_HDMI_HPD_5V, 0));
  340. regmap_write(hdmi->regmap, RK3328_GRF_SOC_CON3,
  341. FIELD_PREP_WM16(RK3328_HDMI_SDA5V_GRF, 0) |
  342. FIELD_PREP_WM16(RK3328_HDMI_SCL5V_GRF, 0) |
  343. FIELD_PREP_WM16(RK3328_HDMI_HPD5V_GRF, 0) |
  344. FIELD_PREP_WM16(RK3328_HDMI_CEC5V_GRF, 0));
  345. regmap_write(hdmi->regmap, RK3328_GRF_SOC_CON2,
  346. FIELD_PREP_WM16(RK3328_HDMI_SDAIN_MSK, 1) |
  347. FIELD_PREP_WM16(RK3328_HDMI_SCLIN_MSK, 1) |
  348. FIELD_PREP_WM16(RK3328_HDMI_HPD_IOE, 0));
  349. dw_hdmi_rk3328_read_hpd(dw_hdmi, data);
  350. }
  351. static const struct dw_hdmi_phy_ops rk3228_hdmi_phy_ops = {
  352. .init = dw_hdmi_rockchip_genphy_init,
  353. .disable = dw_hdmi_rockchip_genphy_disable,
  354. .read_hpd = dw_hdmi_phy_read_hpd,
  355. .update_hpd = dw_hdmi_phy_update_hpd,
  356. .setup_hpd = dw_hdmi_rk3228_setup_hpd,
  357. };
  358. static struct rockchip_hdmi_chip_data rk3228_chip_data = {
  359. .lcdsel_grf_reg = -1,
  360. .max_tmds_clock = 594000,
  361. };
  362. static const struct dw_hdmi_plat_data rk3228_hdmi_drv_data = {
  363. .mode_valid = dw_hdmi_rockchip_mode_valid,
  364. .phy_data = &rk3228_chip_data,
  365. .phy_ops = &rk3228_hdmi_phy_ops,
  366. .phy_name = "inno_dw_hdmi_phy2",
  367. .phy_force_vendor = true,
  368. };
  369. static struct rockchip_hdmi_chip_data rk3288_chip_data = {
  370. .lcdsel_grf_reg = RK3288_GRF_SOC_CON6,
  371. .lcdsel_big = FIELD_PREP_WM16_CONST(RK3288_HDMI_LCDC_SEL, 0),
  372. .lcdsel_lit = FIELD_PREP_WM16_CONST(RK3288_HDMI_LCDC_SEL, 1),
  373. .max_tmds_clock = 340000,
  374. };
  375. static const struct dw_hdmi_plat_data rk3288_hdmi_drv_data = {
  376. .mode_valid = dw_hdmi_rockchip_mode_valid,
  377. .mpll_cfg = rockchip_mpll_cfg,
  378. .cur_ctr = rockchip_cur_ctr,
  379. .phy_config = rockchip_phy_config,
  380. .phy_data = &rk3288_chip_data,
  381. };
  382. static const struct dw_hdmi_phy_ops rk3328_hdmi_phy_ops = {
  383. .init = dw_hdmi_rockchip_genphy_init,
  384. .disable = dw_hdmi_rockchip_genphy_disable,
  385. .read_hpd = dw_hdmi_rk3328_read_hpd,
  386. .update_hpd = dw_hdmi_phy_update_hpd,
  387. .setup_hpd = dw_hdmi_rk3328_setup_hpd,
  388. };
  389. static struct rockchip_hdmi_chip_data rk3328_chip_data = {
  390. .lcdsel_grf_reg = -1,
  391. .max_tmds_clock = 594000,
  392. };
  393. static const struct dw_hdmi_plat_data rk3328_hdmi_drv_data = {
  394. .mode_valid = dw_hdmi_rockchip_mode_valid,
  395. .phy_data = &rk3328_chip_data,
  396. .phy_ops = &rk3328_hdmi_phy_ops,
  397. .phy_name = "inno_dw_hdmi_phy2",
  398. .phy_force_vendor = true,
  399. .use_drm_infoframe = true,
  400. };
  401. static struct rockchip_hdmi_chip_data rk3368_chip_data = {
  402. .lcdsel_grf_reg = -1,
  403. };
  404. static const struct dw_hdmi_plat_data rk3368_hdmi_drv_data = {
  405. .mode_valid = dw_hdmi_rockchip_mode_valid,
  406. .mpll_cfg = rockchip_mpll_cfg,
  407. .cur_ctr = rockchip_cur_ctr,
  408. .phy_config = rockchip_phy_config,
  409. .phy_data = &rk3368_chip_data,
  410. .use_drm_infoframe = true,
  411. };
  412. static struct rockchip_hdmi_chip_data rk3399_chip_data = {
  413. .lcdsel_grf_reg = RK3399_GRF_SOC_CON20,
  414. .lcdsel_big = FIELD_PREP_WM16_CONST(RK3399_HDMI_LCDC_SEL, 0),
  415. .lcdsel_lit = FIELD_PREP_WM16_CONST(RK3399_HDMI_LCDC_SEL, 1),
  416. .max_tmds_clock = 594000,
  417. };
  418. static const struct dw_hdmi_plat_data rk3399_hdmi_drv_data = {
  419. .mode_valid = dw_hdmi_rockchip_mode_valid,
  420. .mpll_cfg = rockchip_mpll_cfg,
  421. .cur_ctr = rockchip_cur_ctr,
  422. .phy_config = rockchip_phy_config,
  423. .phy_data = &rk3399_chip_data,
  424. .use_drm_infoframe = true,
  425. };
  426. static struct rockchip_hdmi_chip_data rk3568_chip_data = {
  427. .lcdsel_grf_reg = -1,
  428. .max_tmds_clock = 594000,
  429. };
  430. static const struct dw_hdmi_plat_data rk3568_hdmi_drv_data = {
  431. .mode_valid = dw_hdmi_rockchip_mode_valid,
  432. .mpll_cfg = rockchip_mpll_cfg,
  433. .cur_ctr = rockchip_cur_ctr,
  434. .phy_config = rockchip_phy_config,
  435. .phy_data = &rk3568_chip_data,
  436. .use_drm_infoframe = true,
  437. };
  438. static const struct of_device_id dw_hdmi_rockchip_dt_ids[] = {
  439. { .compatible = "rockchip,rk3228-dw-hdmi",
  440. .data = &rk3228_hdmi_drv_data
  441. },
  442. { .compatible = "rockchip,rk3288-dw-hdmi",
  443. .data = &rk3288_hdmi_drv_data
  444. },
  445. { .compatible = "rockchip,rk3328-dw-hdmi",
  446. .data = &rk3328_hdmi_drv_data
  447. },
  448. { .compatible = "rockchip,rk3368-dw-hdmi",
  449. .data = &rk3368_hdmi_drv_data
  450. },
  451. { .compatible = "rockchip,rk3399-dw-hdmi",
  452. .data = &rk3399_hdmi_drv_data
  453. },
  454. { .compatible = "rockchip,rk3568-dw-hdmi",
  455. .data = &rk3568_hdmi_drv_data
  456. },
  457. {},
  458. };
  459. MODULE_DEVICE_TABLE(of, dw_hdmi_rockchip_dt_ids);
  460. static int dw_hdmi_rockchip_bind(struct device *dev, struct device *master,
  461. void *data)
  462. {
  463. struct platform_device *pdev = to_platform_device(dev);
  464. struct dw_hdmi_plat_data *plat_data;
  465. const struct of_device_id *match;
  466. struct drm_device *drm = data;
  467. struct drm_encoder *encoder;
  468. struct rockchip_hdmi *hdmi;
  469. int ret;
  470. if (!pdev->dev.of_node)
  471. return -ENODEV;
  472. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  473. if (!hdmi)
  474. return -ENOMEM;
  475. match = of_match_node(dw_hdmi_rockchip_dt_ids, pdev->dev.of_node);
  476. plat_data = devm_kmemdup(&pdev->dev, match->data,
  477. sizeof(*plat_data), GFP_KERNEL);
  478. if (!plat_data)
  479. return -ENOMEM;
  480. hdmi->dev = &pdev->dev;
  481. hdmi->plat_data = plat_data;
  482. hdmi->chip_data = plat_data->phy_data;
  483. plat_data->phy_data = hdmi;
  484. plat_data->priv_data = hdmi;
  485. encoder = &hdmi->encoder.encoder;
  486. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  487. rockchip_drm_encoder_set_crtc_endpoint_id(&hdmi->encoder,
  488. dev->of_node, 0, 0);
  489. /*
  490. * If we failed to find the CRTC(s) which this encoder is
  491. * supposed to be connected to, it's because the CRTC has
  492. * not been registered yet. Defer probing, and hope that
  493. * the required CRTC is added later.
  494. */
  495. if (encoder->possible_crtcs == 0)
  496. return -EPROBE_DEFER;
  497. ret = rockchip_hdmi_parse_dt(hdmi);
  498. if (ret) {
  499. return dev_err_probe(hdmi->dev, ret, "Unable to parse OF data\n");
  500. }
  501. hdmi->phy = devm_phy_optional_get(dev, "hdmi");
  502. if (IS_ERR(hdmi->phy)) {
  503. ret = PTR_ERR(hdmi->phy);
  504. return dev_err_probe(hdmi->dev, ret, "failed to get phy\n");
  505. }
  506. if (hdmi->phy) {
  507. struct of_phandle_args clkspec;
  508. clkspec.np = hdmi->phy->dev.of_node;
  509. hdmi->hdmiphy_clk = of_clk_get_from_provider(&clkspec);
  510. if (IS_ERR(hdmi->hdmiphy_clk))
  511. hdmi->hdmiphy_clk = NULL;
  512. }
  513. if (hdmi->chip_data == &rk3568_chip_data) {
  514. regmap_write(hdmi->regmap, RK3568_GRF_VO_CON1,
  515. FIELD_PREP_WM16(RK3568_HDMI_SDAIN_MSK, 1) |
  516. FIELD_PREP_WM16(RK3568_HDMI_SCLIN_MSK, 1));
  517. }
  518. drm_encoder_helper_add(encoder, &dw_hdmi_rockchip_encoder_helper_funcs);
  519. drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
  520. platform_set_drvdata(pdev, hdmi);
  521. hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
  522. /*
  523. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  524. * which would have called the encoder cleanup. Do it manually.
  525. */
  526. if (IS_ERR(hdmi->hdmi)) {
  527. ret = PTR_ERR(hdmi->hdmi);
  528. goto err_bind;
  529. }
  530. return 0;
  531. err_bind:
  532. drm_encoder_cleanup(encoder);
  533. return ret;
  534. }
  535. static void dw_hdmi_rockchip_unbind(struct device *dev, struct device *master,
  536. void *data)
  537. {
  538. struct rockchip_hdmi *hdmi = dev_get_drvdata(dev);
  539. dw_hdmi_unbind(hdmi->hdmi);
  540. drm_encoder_cleanup(&hdmi->encoder.encoder);
  541. }
  542. static const struct component_ops dw_hdmi_rockchip_ops = {
  543. .bind = dw_hdmi_rockchip_bind,
  544. .unbind = dw_hdmi_rockchip_unbind,
  545. };
  546. static int dw_hdmi_rockchip_probe(struct platform_device *pdev)
  547. {
  548. return component_add(&pdev->dev, &dw_hdmi_rockchip_ops);
  549. }
  550. static void dw_hdmi_rockchip_remove(struct platform_device *pdev)
  551. {
  552. component_del(&pdev->dev, &dw_hdmi_rockchip_ops);
  553. }
  554. static int __maybe_unused dw_hdmi_rockchip_resume(struct device *dev)
  555. {
  556. struct rockchip_hdmi *hdmi = dev_get_drvdata(dev);
  557. dw_hdmi_resume(hdmi->hdmi);
  558. return 0;
  559. }
  560. static const struct dev_pm_ops dw_hdmi_rockchip_pm = {
  561. SET_SYSTEM_SLEEP_PM_OPS(NULL, dw_hdmi_rockchip_resume)
  562. };
  563. struct platform_driver dw_hdmi_rockchip_pltfm_driver = {
  564. .probe = dw_hdmi_rockchip_probe,
  565. .remove = dw_hdmi_rockchip_remove,
  566. .driver = {
  567. .name = "dwhdmi-rockchip",
  568. .pm = &dw_hdmi_rockchip_pm,
  569. .of_match_table = dw_hdmi_rockchip_dt_ids,
  570. },
  571. };