vc4_vec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Broadcom
  4. */
  5. /**
  6. * DOC: VC4 SDTV module
  7. *
  8. * The VEC encoder generates PAL or NTSC composite video output.
  9. *
  10. * TV mode selection is done by an atomic property on the encoder,
  11. * because a drm_mode_modeinfo is insufficient to distinguish between
  12. * PAL and PAL-M or NTSC and NTSC-J.
  13. */
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_drv.h>
  16. #include <drm/drm_edid.h>
  17. #include <drm/drm_panel.h>
  18. #include <drm/drm_probe_helper.h>
  19. #include <drm/drm_simple_kms_helper.h>
  20. #include <linux/clk.h>
  21. #include <linux/component.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/pm_runtime.h>
  25. #include "vc4_drv.h"
  26. #include "vc4_regs.h"
  27. /* WSE Registers */
  28. #define VEC_WSE_RESET 0xc0
  29. #define VEC_WSE_CONTROL 0xc4
  30. #define VEC_WSE_WSS_ENABLE BIT(7)
  31. #define VEC_WSE_WSS_DATA 0xc8
  32. #define VEC_WSE_VPS_DATA1 0xcc
  33. #define VEC_WSE_VPS_CONTROL 0xd0
  34. /* VEC Registers */
  35. #define VEC_REVID 0x100
  36. #define VEC_CONFIG0 0x104
  37. #define VEC_CONFIG0_YDEL_MASK GENMASK(28, 26)
  38. #define VEC_CONFIG0_YDEL(x) ((x) << 26)
  39. #define VEC_CONFIG0_CDEL_MASK GENMASK(25, 24)
  40. #define VEC_CONFIG0_CDEL(x) ((x) << 24)
  41. #define VEC_CONFIG0_PBPR_FIL BIT(18)
  42. #define VEC_CONFIG0_CHROMA_GAIN_MASK GENMASK(17, 16)
  43. #define VEC_CONFIG0_CHROMA_GAIN_UNITY (0 << 16)
  44. #define VEC_CONFIG0_CHROMA_GAIN_1_32 (1 << 16)
  45. #define VEC_CONFIG0_CHROMA_GAIN_1_16 (2 << 16)
  46. #define VEC_CONFIG0_CHROMA_GAIN_1_8 (3 << 16)
  47. #define VEC_CONFIG0_CBURST_GAIN_MASK GENMASK(14, 13)
  48. #define VEC_CONFIG0_CBURST_GAIN_UNITY (0 << 13)
  49. #define VEC_CONFIG0_CBURST_GAIN_1_128 (1 << 13)
  50. #define VEC_CONFIG0_CBURST_GAIN_1_64 (2 << 13)
  51. #define VEC_CONFIG0_CBURST_GAIN_1_32 (3 << 13)
  52. #define VEC_CONFIG0_CHRBW1 BIT(11)
  53. #define VEC_CONFIG0_CHRBW0 BIT(10)
  54. #define VEC_CONFIG0_SYNCDIS BIT(9)
  55. #define VEC_CONFIG0_BURDIS BIT(8)
  56. #define VEC_CONFIG0_CHRDIS BIT(7)
  57. #define VEC_CONFIG0_PDEN BIT(6)
  58. #define VEC_CONFIG0_YCDELAY BIT(4)
  59. #define VEC_CONFIG0_RAMPEN BIT(2)
  60. #define VEC_CONFIG0_YCDIS BIT(2)
  61. #define VEC_CONFIG0_STD_MASK GENMASK(1, 0)
  62. #define VEC_CONFIG0_NTSC_STD 0
  63. #define VEC_CONFIG0_PAL_BDGHI_STD 1
  64. #define VEC_CONFIG0_PAL_N_STD 3
  65. #define VEC_SCHPH 0x108
  66. #define VEC_SOFT_RESET 0x10c
  67. #define VEC_CLMP0_START 0x144
  68. #define VEC_CLMP0_END 0x148
  69. #define VEC_FREQ3_2 0x180
  70. #define VEC_FREQ1_0 0x184
  71. #define VEC_CONFIG1 0x188
  72. #define VEC_CONFIG_VEC_RESYNC_OFF BIT(18)
  73. #define VEC_CONFIG_RGB219 BIT(17)
  74. #define VEC_CONFIG_CBAR_EN BIT(16)
  75. #define VEC_CONFIG_TC_OBB BIT(15)
  76. #define VEC_CONFIG1_OUTPUT_MODE_MASK GENMASK(12, 10)
  77. #define VEC_CONFIG1_C_Y_CVBS (0 << 10)
  78. #define VEC_CONFIG1_CVBS_Y_C (1 << 10)
  79. #define VEC_CONFIG1_PR_Y_PB (2 << 10)
  80. #define VEC_CONFIG1_RGB (4 << 10)
  81. #define VEC_CONFIG1_Y_C_CVBS (5 << 10)
  82. #define VEC_CONFIG1_C_CVBS_Y (6 << 10)
  83. #define VEC_CONFIG1_C_CVBS_CVBS (7 << 10)
  84. #define VEC_CONFIG1_DIS_CHR BIT(9)
  85. #define VEC_CONFIG1_DIS_LUMA BIT(8)
  86. #define VEC_CONFIG1_YCBCR_IN BIT(6)
  87. #define VEC_CONFIG1_DITHER_TYPE_LFSR 0
  88. #define VEC_CONFIG1_DITHER_TYPE_COUNTER BIT(5)
  89. #define VEC_CONFIG1_DITHER_EN BIT(4)
  90. #define VEC_CONFIG1_CYDELAY BIT(3)
  91. #define VEC_CONFIG1_LUMADIS BIT(2)
  92. #define VEC_CONFIG1_COMPDIS BIT(1)
  93. #define VEC_CONFIG1_CUSTOM_FREQ BIT(0)
  94. #define VEC_CONFIG2 0x18c
  95. #define VEC_CONFIG2_PROG_SCAN BIT(15)
  96. #define VEC_CONFIG2_SYNC_ADJ_MASK GENMASK(14, 12)
  97. #define VEC_CONFIG2_SYNC_ADJ(x) (((x) / 2) << 12)
  98. #define VEC_CONFIG2_PBPR_EN BIT(10)
  99. #define VEC_CONFIG2_UV_DIG_DIS BIT(6)
  100. #define VEC_CONFIG2_RGB_DIG_DIS BIT(5)
  101. #define VEC_CONFIG2_TMUX_MASK GENMASK(3, 2)
  102. #define VEC_CONFIG2_TMUX_DRIVE0 (0 << 2)
  103. #define VEC_CONFIG2_TMUX_RG_COMP (1 << 2)
  104. #define VEC_CONFIG2_TMUX_UV_YC (2 << 2)
  105. #define VEC_CONFIG2_TMUX_SYNC_YC (3 << 2)
  106. #define VEC_INTERRUPT_CONTROL 0x190
  107. #define VEC_INTERRUPT_STATUS 0x194
  108. #define VEC_FCW_SECAM_B 0x198
  109. #define VEC_SECAM_GAIN_VAL 0x19c
  110. #define VEC_CONFIG3 0x1a0
  111. #define VEC_CONFIG3_HORIZ_LEN_STD (0 << 0)
  112. #define VEC_CONFIG3_HORIZ_LEN_MPEG1_SIF (1 << 0)
  113. #define VEC_CONFIG3_SHAPE_NON_LINEAR BIT(1)
  114. #define VEC_STATUS0 0x200
  115. #define VEC_MASK0 0x204
  116. #define VEC_CFG 0x208
  117. #define VEC_CFG_SG_MODE_MASK GENMASK(6, 5)
  118. #define VEC_CFG_SG_MODE(x) ((x) << 5)
  119. #define VEC_CFG_SG_EN BIT(4)
  120. #define VEC_CFG_VEC_EN BIT(3)
  121. #define VEC_CFG_MB_EN BIT(2)
  122. #define VEC_CFG_ENABLE BIT(1)
  123. #define VEC_CFG_TB_EN BIT(0)
  124. #define VEC_DAC_TEST 0x20c
  125. #define VEC_DAC_CONFIG 0x210
  126. #define VEC_DAC_CONFIG_LDO_BIAS_CTRL(x) ((x) << 24)
  127. #define VEC_DAC_CONFIG_DRIVER_CTRL(x) ((x) << 16)
  128. #define VEC_DAC_CONFIG_DAC_CTRL(x) (x)
  129. #define VEC_DAC_MISC 0x214
  130. #define VEC_DAC_MISC_VCD_CTRL_MASK GENMASK(31, 16)
  131. #define VEC_DAC_MISC_VCD_CTRL(x) ((x) << 16)
  132. #define VEC_DAC_MISC_VID_ACT BIT(8)
  133. #define VEC_DAC_MISC_VCD_PWRDN BIT(6)
  134. #define VEC_DAC_MISC_BIAS_PWRDN BIT(5)
  135. #define VEC_DAC_MISC_DAC_PWRDN BIT(2)
  136. #define VEC_DAC_MISC_LDO_PWRDN BIT(1)
  137. #define VEC_DAC_MISC_DAC_RST_N BIT(0)
  138. struct vc4_vec_variant {
  139. u32 dac_config;
  140. };
  141. /* General VEC hardware state. */
  142. struct vc4_vec {
  143. struct vc4_encoder encoder;
  144. struct drm_connector connector;
  145. struct platform_device *pdev;
  146. const struct vc4_vec_variant *variant;
  147. void __iomem *regs;
  148. struct clk *clock;
  149. struct debugfs_regset32 regset;
  150. };
  151. #define VEC_READ(offset) readl(vec->regs + (offset))
  152. #define VEC_WRITE(offset, val) writel(val, vec->regs + (offset))
  153. static inline struct vc4_vec *
  154. encoder_to_vc4_vec(struct drm_encoder *encoder)
  155. {
  156. return container_of(encoder, struct vc4_vec, encoder.base);
  157. }
  158. enum vc4_vec_tv_mode_id {
  159. VC4_VEC_TV_MODE_NTSC,
  160. VC4_VEC_TV_MODE_NTSC_J,
  161. VC4_VEC_TV_MODE_PAL,
  162. VC4_VEC_TV_MODE_PAL_M,
  163. };
  164. struct vc4_vec_tv_mode {
  165. const struct drm_display_mode *mode;
  166. u32 config0;
  167. u32 config1;
  168. u32 custom_freq;
  169. };
  170. static const struct debugfs_reg32 vec_regs[] = {
  171. VC4_REG32(VEC_WSE_CONTROL),
  172. VC4_REG32(VEC_WSE_WSS_DATA),
  173. VC4_REG32(VEC_WSE_VPS_DATA1),
  174. VC4_REG32(VEC_WSE_VPS_CONTROL),
  175. VC4_REG32(VEC_REVID),
  176. VC4_REG32(VEC_CONFIG0),
  177. VC4_REG32(VEC_SCHPH),
  178. VC4_REG32(VEC_CLMP0_START),
  179. VC4_REG32(VEC_CLMP0_END),
  180. VC4_REG32(VEC_FREQ3_2),
  181. VC4_REG32(VEC_FREQ1_0),
  182. VC4_REG32(VEC_CONFIG1),
  183. VC4_REG32(VEC_CONFIG2),
  184. VC4_REG32(VEC_INTERRUPT_CONTROL),
  185. VC4_REG32(VEC_INTERRUPT_STATUS),
  186. VC4_REG32(VEC_FCW_SECAM_B),
  187. VC4_REG32(VEC_SECAM_GAIN_VAL),
  188. VC4_REG32(VEC_CONFIG3),
  189. VC4_REG32(VEC_STATUS0),
  190. VC4_REG32(VEC_MASK0),
  191. VC4_REG32(VEC_CFG),
  192. VC4_REG32(VEC_DAC_TEST),
  193. VC4_REG32(VEC_DAC_CONFIG),
  194. VC4_REG32(VEC_DAC_MISC),
  195. };
  196. static const struct drm_display_mode ntsc_mode = {
  197. DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 13500,
  198. 720, 720 + 14, 720 + 14 + 64, 720 + 14 + 64 + 60, 0,
  199. 480, 480 + 7, 480 + 7 + 6, 525, 0,
  200. DRM_MODE_FLAG_INTERLACE)
  201. };
  202. static const struct drm_display_mode pal_mode = {
  203. DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 13500,
  204. 720, 720 + 20, 720 + 20 + 64, 720 + 20 + 64 + 60, 0,
  205. 576, 576 + 4, 576 + 4 + 6, 625, 0,
  206. DRM_MODE_FLAG_INTERLACE)
  207. };
  208. static const struct vc4_vec_tv_mode vc4_vec_tv_modes[] = {
  209. [VC4_VEC_TV_MODE_NTSC] = {
  210. .mode = &ntsc_mode,
  211. .config0 = VEC_CONFIG0_NTSC_STD | VEC_CONFIG0_PDEN,
  212. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  213. },
  214. [VC4_VEC_TV_MODE_NTSC_J] = {
  215. .mode = &ntsc_mode,
  216. .config0 = VEC_CONFIG0_NTSC_STD,
  217. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  218. },
  219. [VC4_VEC_TV_MODE_PAL] = {
  220. .mode = &pal_mode,
  221. .config0 = VEC_CONFIG0_PAL_BDGHI_STD,
  222. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  223. },
  224. [VC4_VEC_TV_MODE_PAL_M] = {
  225. .mode = &pal_mode,
  226. .config0 = VEC_CONFIG0_PAL_BDGHI_STD,
  227. .config1 = VEC_CONFIG1_C_CVBS_CVBS | VEC_CONFIG1_CUSTOM_FREQ,
  228. .custom_freq = 0x223b61d1,
  229. },
  230. };
  231. static enum drm_connector_status
  232. vc4_vec_connector_detect(struct drm_connector *connector, bool force)
  233. {
  234. return connector_status_unknown;
  235. }
  236. static int vc4_vec_connector_get_modes(struct drm_connector *connector)
  237. {
  238. struct drm_connector_state *state = connector->state;
  239. struct drm_display_mode *mode;
  240. mode = drm_mode_duplicate(connector->dev,
  241. vc4_vec_tv_modes[state->tv.mode].mode);
  242. if (!mode) {
  243. DRM_ERROR("Failed to create a new display mode\n");
  244. return -ENOMEM;
  245. }
  246. drm_mode_probed_add(connector, mode);
  247. return 1;
  248. }
  249. static const struct drm_connector_funcs vc4_vec_connector_funcs = {
  250. .detect = vc4_vec_connector_detect,
  251. .fill_modes = drm_helper_probe_single_connector_modes,
  252. .reset = drm_atomic_helper_connector_reset,
  253. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  254. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  255. };
  256. static const struct drm_connector_helper_funcs vc4_vec_connector_helper_funcs = {
  257. .get_modes = vc4_vec_connector_get_modes,
  258. };
  259. static int vc4_vec_connector_init(struct drm_device *dev, struct vc4_vec *vec)
  260. {
  261. struct drm_connector *connector = &vec->connector;
  262. int ret;
  263. connector->interlace_allowed = true;
  264. ret = drmm_connector_init(dev, connector, &vc4_vec_connector_funcs,
  265. DRM_MODE_CONNECTOR_Composite, NULL);
  266. if (ret)
  267. return ret;
  268. drm_connector_helper_add(connector, &vc4_vec_connector_helper_funcs);
  269. drm_object_attach_property(&connector->base,
  270. dev->mode_config.tv_mode_property,
  271. VC4_VEC_TV_MODE_NTSC);
  272. drm_connector_attach_encoder(connector, &vec->encoder.base);
  273. return 0;
  274. }
  275. static void vc4_vec_encoder_disable(struct drm_encoder *encoder,
  276. struct drm_atomic_state *state)
  277. {
  278. struct drm_device *drm = encoder->dev;
  279. struct vc4_vec *vec = encoder_to_vc4_vec(encoder);
  280. int idx, ret;
  281. if (!drm_dev_enter(drm, &idx))
  282. return;
  283. VEC_WRITE(VEC_CFG, 0);
  284. VEC_WRITE(VEC_DAC_MISC,
  285. VEC_DAC_MISC_VCD_PWRDN |
  286. VEC_DAC_MISC_BIAS_PWRDN |
  287. VEC_DAC_MISC_DAC_PWRDN |
  288. VEC_DAC_MISC_LDO_PWRDN);
  289. clk_disable_unprepare(vec->clock);
  290. ret = pm_runtime_put(&vec->pdev->dev);
  291. if (ret < 0) {
  292. DRM_ERROR("Failed to release power domain: %d\n", ret);
  293. goto err_dev_exit;
  294. }
  295. drm_dev_exit(idx);
  296. return;
  297. err_dev_exit:
  298. drm_dev_exit(idx);
  299. }
  300. static void vc4_vec_encoder_enable(struct drm_encoder *encoder,
  301. struct drm_atomic_state *state)
  302. {
  303. struct drm_device *drm = encoder->dev;
  304. struct vc4_vec *vec = encoder_to_vc4_vec(encoder);
  305. struct drm_connector *connector = &vec->connector;
  306. struct drm_connector_state *conn_state =
  307. drm_atomic_get_new_connector_state(state, connector);
  308. const struct vc4_vec_tv_mode *tv_mode =
  309. &vc4_vec_tv_modes[conn_state->tv.mode];
  310. int idx, ret;
  311. if (!drm_dev_enter(drm, &idx))
  312. return;
  313. ret = pm_runtime_get_sync(&vec->pdev->dev);
  314. if (ret < 0) {
  315. DRM_ERROR("Failed to retain power domain: %d\n", ret);
  316. goto err_dev_exit;
  317. }
  318. /*
  319. * We need to set the clock rate each time we enable the encoder
  320. * because there's a chance we share the same parent with the HDMI
  321. * clock, and both drivers are requesting different rates.
  322. * The good news is, these 2 encoders cannot be enabled at the same
  323. * time, thus preventing incompatible rate requests.
  324. */
  325. ret = clk_set_rate(vec->clock, 108000000);
  326. if (ret) {
  327. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  328. goto err_put_runtime_pm;
  329. }
  330. ret = clk_prepare_enable(vec->clock);
  331. if (ret) {
  332. DRM_ERROR("Failed to turn on core clock: %d\n", ret);
  333. goto err_put_runtime_pm;
  334. }
  335. /* Reset the different blocks */
  336. VEC_WRITE(VEC_WSE_RESET, 1);
  337. VEC_WRITE(VEC_SOFT_RESET, 1);
  338. /* Disable the CGSM-A and WSE blocks */
  339. VEC_WRITE(VEC_WSE_CONTROL, 0);
  340. /* Write config common to all modes. */
  341. /*
  342. * Color subcarrier phase: phase = 360 * SCHPH / 256.
  343. * 0x28 <=> 39.375 deg.
  344. */
  345. VEC_WRITE(VEC_SCHPH, 0x28);
  346. /*
  347. * Reset to default values.
  348. */
  349. VEC_WRITE(VEC_CLMP0_START, 0xac);
  350. VEC_WRITE(VEC_CLMP0_END, 0xec);
  351. VEC_WRITE(VEC_CONFIG2,
  352. VEC_CONFIG2_UV_DIG_DIS | VEC_CONFIG2_RGB_DIG_DIS);
  353. VEC_WRITE(VEC_CONFIG3, VEC_CONFIG3_HORIZ_LEN_STD);
  354. VEC_WRITE(VEC_DAC_CONFIG, vec->variant->dac_config);
  355. /* Mask all interrupts. */
  356. VEC_WRITE(VEC_MASK0, 0);
  357. VEC_WRITE(VEC_CONFIG0, tv_mode->config0);
  358. VEC_WRITE(VEC_CONFIG1, tv_mode->config1);
  359. if (tv_mode->custom_freq) {
  360. VEC_WRITE(VEC_FREQ3_2,
  361. (tv_mode->custom_freq >> 16) & 0xffff);
  362. VEC_WRITE(VEC_FREQ1_0,
  363. tv_mode->custom_freq & 0xffff);
  364. }
  365. VEC_WRITE(VEC_DAC_MISC,
  366. VEC_DAC_MISC_VID_ACT | VEC_DAC_MISC_DAC_RST_N);
  367. VEC_WRITE(VEC_CFG, VEC_CFG_VEC_EN);
  368. drm_dev_exit(idx);
  369. return;
  370. err_put_runtime_pm:
  371. pm_runtime_put(&vec->pdev->dev);
  372. err_dev_exit:
  373. drm_dev_exit(idx);
  374. }
  375. static int vc4_vec_encoder_atomic_check(struct drm_encoder *encoder,
  376. struct drm_crtc_state *crtc_state,
  377. struct drm_connector_state *conn_state)
  378. {
  379. const struct vc4_vec_tv_mode *vec_mode;
  380. vec_mode = &vc4_vec_tv_modes[conn_state->tv.mode];
  381. if (conn_state->crtc &&
  382. !drm_mode_equal(vec_mode->mode, &crtc_state->adjusted_mode))
  383. return -EINVAL;
  384. return 0;
  385. }
  386. static const struct drm_encoder_helper_funcs vc4_vec_encoder_helper_funcs = {
  387. .atomic_check = vc4_vec_encoder_atomic_check,
  388. .atomic_disable = vc4_vec_encoder_disable,
  389. .atomic_enable = vc4_vec_encoder_enable,
  390. };
  391. static int vc4_vec_late_register(struct drm_encoder *encoder)
  392. {
  393. struct drm_device *drm = encoder->dev;
  394. struct vc4_vec *vec = encoder_to_vc4_vec(encoder);
  395. int ret;
  396. ret = vc4_debugfs_add_regset32(drm->primary, "vec_regs",
  397. &vec->regset);
  398. if (ret)
  399. return ret;
  400. return 0;
  401. }
  402. static const struct drm_encoder_funcs vc4_vec_encoder_funcs = {
  403. .late_register = vc4_vec_late_register,
  404. };
  405. static const struct vc4_vec_variant bcm2835_vec_variant = {
  406. .dac_config = VEC_DAC_CONFIG_DAC_CTRL(0xc) |
  407. VEC_DAC_CONFIG_DRIVER_CTRL(0xc) |
  408. VEC_DAC_CONFIG_LDO_BIAS_CTRL(0x46)
  409. };
  410. static const struct vc4_vec_variant bcm2711_vec_variant = {
  411. .dac_config = VEC_DAC_CONFIG_DAC_CTRL(0x0) |
  412. VEC_DAC_CONFIG_DRIVER_CTRL(0x80) |
  413. VEC_DAC_CONFIG_LDO_BIAS_CTRL(0x61)
  414. };
  415. static const struct of_device_id vc4_vec_dt_match[] = {
  416. { .compatible = "brcm,bcm2835-vec", .data = &bcm2835_vec_variant },
  417. { .compatible = "brcm,bcm2711-vec", .data = &bcm2711_vec_variant },
  418. { /* sentinel */ },
  419. };
  420. static const char * const tv_mode_names[] = {
  421. [VC4_VEC_TV_MODE_NTSC] = "NTSC",
  422. [VC4_VEC_TV_MODE_NTSC_J] = "NTSC-J",
  423. [VC4_VEC_TV_MODE_PAL] = "PAL",
  424. [VC4_VEC_TV_MODE_PAL_M] = "PAL-M",
  425. };
  426. static int vc4_vec_bind(struct device *dev, struct device *master, void *data)
  427. {
  428. struct platform_device *pdev = to_platform_device(dev);
  429. struct drm_device *drm = dev_get_drvdata(master);
  430. struct vc4_vec *vec;
  431. int ret;
  432. ret = drm_mode_create_tv_properties(drm, ARRAY_SIZE(tv_mode_names),
  433. tv_mode_names);
  434. if (ret)
  435. return ret;
  436. vec = drmm_kzalloc(drm, sizeof(*vec), GFP_KERNEL);
  437. if (!vec)
  438. return -ENOMEM;
  439. vec->encoder.type = VC4_ENCODER_TYPE_VEC;
  440. vec->pdev = pdev;
  441. vec->variant = (const struct vc4_vec_variant *)
  442. of_device_get_match_data(dev);
  443. vec->regs = vc4_ioremap_regs(pdev, 0);
  444. if (IS_ERR(vec->regs))
  445. return PTR_ERR(vec->regs);
  446. vec->regset.base = vec->regs;
  447. vec->regset.regs = vec_regs;
  448. vec->regset.nregs = ARRAY_SIZE(vec_regs);
  449. vec->clock = devm_clk_get(dev, NULL);
  450. if (IS_ERR(vec->clock)) {
  451. ret = PTR_ERR(vec->clock);
  452. if (ret != -EPROBE_DEFER)
  453. DRM_ERROR("Failed to get clock: %d\n", ret);
  454. return ret;
  455. }
  456. ret = devm_pm_runtime_enable(dev);
  457. if (ret)
  458. return ret;
  459. ret = drmm_encoder_init(drm, &vec->encoder.base,
  460. &vc4_vec_encoder_funcs,
  461. DRM_MODE_ENCODER_TVDAC,
  462. NULL);
  463. if (ret)
  464. return ret;
  465. drm_encoder_helper_add(&vec->encoder.base, &vc4_vec_encoder_helper_funcs);
  466. ret = vc4_vec_connector_init(drm, vec);
  467. if (ret)
  468. return ret;
  469. dev_set_drvdata(dev, vec);
  470. return 0;
  471. }
  472. static const struct component_ops vc4_vec_ops = {
  473. .bind = vc4_vec_bind,
  474. };
  475. static int vc4_vec_dev_probe(struct platform_device *pdev)
  476. {
  477. return component_add(&pdev->dev, &vc4_vec_ops);
  478. }
  479. static int vc4_vec_dev_remove(struct platform_device *pdev)
  480. {
  481. component_del(&pdev->dev, &vc4_vec_ops);
  482. return 0;
  483. }
  484. struct platform_driver vc4_vec_driver = {
  485. .probe = vc4_vec_dev_probe,
  486. .remove = vc4_vec_dev_remove,
  487. .driver = {
  488. .name = "vc4_vec",
  489. .of_match_table = vc4_vec_dt_match,
  490. },
  491. };