vc4_vec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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.h>
  23. #include <linux/platform_device.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_SECAM_STD BIT(21)
  42. #define VEC_CONFIG0_PBPR_FIL BIT(18)
  43. #define VEC_CONFIG0_CHROMA_GAIN_MASK GENMASK(17, 16)
  44. #define VEC_CONFIG0_CHROMA_GAIN_UNITY (0 << 16)
  45. #define VEC_CONFIG0_CHROMA_GAIN_1_32 (1 << 16)
  46. #define VEC_CONFIG0_CHROMA_GAIN_1_16 (2 << 16)
  47. #define VEC_CONFIG0_CHROMA_GAIN_1_8 (3 << 16)
  48. #define VEC_CONFIG0_CBURST_GAIN_MASK GENMASK(14, 13)
  49. #define VEC_CONFIG0_CBURST_GAIN_UNITY (0 << 13)
  50. #define VEC_CONFIG0_CBURST_GAIN_1_128 (1 << 13)
  51. #define VEC_CONFIG0_CBURST_GAIN_1_64 (2 << 13)
  52. #define VEC_CONFIG0_CBURST_GAIN_1_32 (3 << 13)
  53. #define VEC_CONFIG0_CHRBW1 BIT(11)
  54. #define VEC_CONFIG0_CHRBW0 BIT(10)
  55. #define VEC_CONFIG0_SYNCDIS BIT(9)
  56. #define VEC_CONFIG0_BURDIS BIT(8)
  57. #define VEC_CONFIG0_CHRDIS BIT(7)
  58. #define VEC_CONFIG0_PDEN BIT(6)
  59. #define VEC_CONFIG0_YCDELAY BIT(4)
  60. #define VEC_CONFIG0_RAMPEN BIT(2)
  61. #define VEC_CONFIG0_YCDIS BIT(2)
  62. #define VEC_CONFIG0_STD_MASK GENMASK(1, 0)
  63. #define VEC_CONFIG0_NTSC_STD 0
  64. #define VEC_CONFIG0_PAL_BDGHI_STD 1
  65. #define VEC_CONFIG0_PAL_M_STD 2
  66. #define VEC_CONFIG0_PAL_N_STD 3
  67. #define VEC_SCHPH 0x108
  68. #define VEC_SOFT_RESET 0x10c
  69. #define VEC_CLMP0_START 0x144
  70. #define VEC_CLMP0_END 0x148
  71. /*
  72. * These set the color subcarrier frequency
  73. * if VEC_CONFIG1_CUSTOM_FREQ is enabled.
  74. *
  75. * VEC_FREQ1_0 contains the most significant 16-bit half-word,
  76. * VEC_FREQ3_2 contains the least significant 16-bit half-word.
  77. * 0x80000000 seems to be equivalent to the pixel clock
  78. * (which itself is the VEC clock divided by 8).
  79. *
  80. * Reference values (with the default pixel clock of 13.5 MHz):
  81. *
  82. * NTSC (3579545.[45] Hz) - 0x21F07C1F
  83. * PAL (4433618.75 Hz) - 0x2A098ACB
  84. * PAL-M (3575611.[888111] Hz) - 0x21E6EFE3
  85. * PAL-N (3582056.25 Hz) - 0x21F69446
  86. *
  87. * NOTE: For SECAM, it is used as the Dr center frequency,
  88. * regardless of whether VEC_CONFIG1_CUSTOM_FREQ is enabled or not;
  89. * that is specified as 4406250 Hz, which corresponds to 0x29C71C72.
  90. */
  91. #define VEC_FREQ3_2 0x180
  92. #define VEC_FREQ1_0 0x184
  93. #define VEC_CONFIG1 0x188
  94. #define VEC_CONFIG_VEC_RESYNC_OFF BIT(18)
  95. #define VEC_CONFIG_RGB219 BIT(17)
  96. #define VEC_CONFIG_CBAR_EN BIT(16)
  97. #define VEC_CONFIG_TC_OBB BIT(15)
  98. #define VEC_CONFIG1_OUTPUT_MODE_MASK GENMASK(12, 10)
  99. #define VEC_CONFIG1_C_Y_CVBS (0 << 10)
  100. #define VEC_CONFIG1_CVBS_Y_C (1 << 10)
  101. #define VEC_CONFIG1_PR_Y_PB (2 << 10)
  102. #define VEC_CONFIG1_RGB (4 << 10)
  103. #define VEC_CONFIG1_Y_C_CVBS (5 << 10)
  104. #define VEC_CONFIG1_C_CVBS_Y (6 << 10)
  105. #define VEC_CONFIG1_C_CVBS_CVBS (7 << 10)
  106. #define VEC_CONFIG1_DIS_CHR BIT(9)
  107. #define VEC_CONFIG1_DIS_LUMA BIT(8)
  108. #define VEC_CONFIG1_YCBCR_IN BIT(6)
  109. #define VEC_CONFIG1_DITHER_TYPE_LFSR 0
  110. #define VEC_CONFIG1_DITHER_TYPE_COUNTER BIT(5)
  111. #define VEC_CONFIG1_DITHER_EN BIT(4)
  112. #define VEC_CONFIG1_CYDELAY BIT(3)
  113. #define VEC_CONFIG1_LUMADIS BIT(2)
  114. #define VEC_CONFIG1_COMPDIS BIT(1)
  115. #define VEC_CONFIG1_CUSTOM_FREQ BIT(0)
  116. #define VEC_CONFIG2 0x18c
  117. #define VEC_CONFIG2_PROG_SCAN BIT(15)
  118. #define VEC_CONFIG2_SYNC_ADJ_MASK GENMASK(14, 12)
  119. #define VEC_CONFIG2_SYNC_ADJ(x) (((x) / 2) << 12)
  120. #define VEC_CONFIG2_PBPR_EN BIT(10)
  121. #define VEC_CONFIG2_UV_DIG_DIS BIT(6)
  122. #define VEC_CONFIG2_RGB_DIG_DIS BIT(5)
  123. #define VEC_CONFIG2_TMUX_MASK GENMASK(3, 2)
  124. #define VEC_CONFIG2_TMUX_DRIVE0 (0 << 2)
  125. #define VEC_CONFIG2_TMUX_RG_COMP (1 << 2)
  126. #define VEC_CONFIG2_TMUX_UV_YC (2 << 2)
  127. #define VEC_CONFIG2_TMUX_SYNC_YC (3 << 2)
  128. #define VEC_INTERRUPT_CONTROL 0x190
  129. #define VEC_INTERRUPT_STATUS 0x194
  130. /*
  131. * Db center frequency for SECAM; the clock for this is the same as for
  132. * VEC_FREQ3_2/VEC_FREQ1_0, which is used for Dr center frequency.
  133. *
  134. * This is specified as 4250000 Hz, which corresponds to 0x284BDA13.
  135. * That is also the default value, so no need to set it explicitly.
  136. */
  137. #define VEC_FCW_SECAM_B 0x198
  138. #define VEC_SECAM_GAIN_VAL 0x19c
  139. #define VEC_CONFIG3 0x1a0
  140. #define VEC_CONFIG3_HORIZ_LEN_STD (0 << 0)
  141. #define VEC_CONFIG3_HORIZ_LEN_MPEG1_SIF (1 << 0)
  142. #define VEC_CONFIG3_SHAPE_NON_LINEAR BIT(1)
  143. #define VEC_STATUS0 0x200
  144. #define VEC_MASK0 0x204
  145. #define VEC_CFG 0x208
  146. #define VEC_CFG_SG_MODE_MASK GENMASK(6, 5)
  147. #define VEC_CFG_SG_MODE(x) ((x) << 5)
  148. #define VEC_CFG_SG_EN BIT(4)
  149. #define VEC_CFG_VEC_EN BIT(3)
  150. #define VEC_CFG_MB_EN BIT(2)
  151. #define VEC_CFG_ENABLE BIT(1)
  152. #define VEC_CFG_TB_EN BIT(0)
  153. #define VEC_DAC_TEST 0x20c
  154. #define VEC_DAC_CONFIG 0x210
  155. #define VEC_DAC_CONFIG_LDO_BIAS_CTRL(x) ((x) << 24)
  156. #define VEC_DAC_CONFIG_DRIVER_CTRL(x) ((x) << 16)
  157. #define VEC_DAC_CONFIG_DAC_CTRL(x) (x)
  158. #define VEC_DAC_MISC 0x214
  159. #define VEC_DAC_MISC_VCD_CTRL_MASK GENMASK(31, 16)
  160. #define VEC_DAC_MISC_VCD_CTRL(x) ((x) << 16)
  161. #define VEC_DAC_MISC_VID_ACT BIT(8)
  162. #define VEC_DAC_MISC_VCD_PWRDN BIT(6)
  163. #define VEC_DAC_MISC_BIAS_PWRDN BIT(5)
  164. #define VEC_DAC_MISC_DAC_PWRDN BIT(2)
  165. #define VEC_DAC_MISC_LDO_PWRDN BIT(1)
  166. #define VEC_DAC_MISC_DAC_RST_N BIT(0)
  167. struct vc4_vec_variant {
  168. u32 dac_config;
  169. };
  170. /* General VEC hardware state. */
  171. struct vc4_vec {
  172. struct vc4_encoder encoder;
  173. struct drm_connector connector;
  174. struct platform_device *pdev;
  175. const struct vc4_vec_variant *variant;
  176. void __iomem *regs;
  177. struct clk *clock;
  178. struct drm_property *legacy_tv_mode_property;
  179. struct debugfs_regset32 regset;
  180. };
  181. #define VEC_READ(offset) \
  182. ({ \
  183. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  184. readl(vec->regs + (offset)); \
  185. })
  186. #define VEC_WRITE(offset, val) \
  187. do { \
  188. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  189. writel(val, vec->regs + (offset)); \
  190. } while (0)
  191. #define encoder_to_vc4_vec(_encoder) \
  192. container_of_const(_encoder, struct vc4_vec, encoder.base)
  193. #define connector_to_vc4_vec(_connector) \
  194. container_of_const(_connector, struct vc4_vec, connector)
  195. enum vc4_vec_tv_mode_id {
  196. VC4_VEC_TV_MODE_NTSC,
  197. VC4_VEC_TV_MODE_NTSC_J,
  198. VC4_VEC_TV_MODE_PAL,
  199. VC4_VEC_TV_MODE_PAL_M,
  200. VC4_VEC_TV_MODE_NTSC_443,
  201. VC4_VEC_TV_MODE_PAL_60,
  202. VC4_VEC_TV_MODE_PAL_N,
  203. VC4_VEC_TV_MODE_SECAM,
  204. VC4_VEC_TV_MODE_MONOCHROME,
  205. };
  206. struct vc4_vec_tv_mode {
  207. unsigned int mode;
  208. u16 expected_htotal;
  209. u32 config0;
  210. u32 config1;
  211. u32 custom_freq;
  212. };
  213. static const struct debugfs_reg32 vec_regs[] = {
  214. VC4_REG32(VEC_WSE_CONTROL),
  215. VC4_REG32(VEC_WSE_WSS_DATA),
  216. VC4_REG32(VEC_WSE_VPS_DATA1),
  217. VC4_REG32(VEC_WSE_VPS_CONTROL),
  218. VC4_REG32(VEC_REVID),
  219. VC4_REG32(VEC_CONFIG0),
  220. VC4_REG32(VEC_SCHPH),
  221. VC4_REG32(VEC_CLMP0_START),
  222. VC4_REG32(VEC_CLMP0_END),
  223. VC4_REG32(VEC_FREQ3_2),
  224. VC4_REG32(VEC_FREQ1_0),
  225. VC4_REG32(VEC_CONFIG1),
  226. VC4_REG32(VEC_CONFIG2),
  227. VC4_REG32(VEC_INTERRUPT_CONTROL),
  228. VC4_REG32(VEC_INTERRUPT_STATUS),
  229. VC4_REG32(VEC_FCW_SECAM_B),
  230. VC4_REG32(VEC_SECAM_GAIN_VAL),
  231. VC4_REG32(VEC_CONFIG3),
  232. VC4_REG32(VEC_STATUS0),
  233. VC4_REG32(VEC_MASK0),
  234. VC4_REG32(VEC_CFG),
  235. VC4_REG32(VEC_DAC_TEST),
  236. VC4_REG32(VEC_DAC_CONFIG),
  237. VC4_REG32(VEC_DAC_MISC),
  238. };
  239. static const struct vc4_vec_tv_mode vc4_vec_tv_modes[] = {
  240. {
  241. .mode = DRM_MODE_TV_MODE_NTSC,
  242. .expected_htotal = 858,
  243. .config0 = VEC_CONFIG0_NTSC_STD | VEC_CONFIG0_PDEN,
  244. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  245. },
  246. {
  247. .mode = DRM_MODE_TV_MODE_NTSC_443,
  248. .expected_htotal = 858,
  249. .config0 = VEC_CONFIG0_NTSC_STD,
  250. .config1 = VEC_CONFIG1_C_CVBS_CVBS | VEC_CONFIG1_CUSTOM_FREQ,
  251. .custom_freq = 0x2a098acb,
  252. },
  253. {
  254. .mode = DRM_MODE_TV_MODE_NTSC_J,
  255. .expected_htotal = 858,
  256. .config0 = VEC_CONFIG0_NTSC_STD,
  257. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  258. },
  259. {
  260. .mode = DRM_MODE_TV_MODE_PAL,
  261. .expected_htotal = 864,
  262. .config0 = VEC_CONFIG0_PAL_BDGHI_STD,
  263. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  264. },
  265. {
  266. /* PAL-60 */
  267. .mode = DRM_MODE_TV_MODE_PAL,
  268. .expected_htotal = 858,
  269. .config0 = VEC_CONFIG0_PAL_M_STD,
  270. .config1 = VEC_CONFIG1_C_CVBS_CVBS | VEC_CONFIG1_CUSTOM_FREQ,
  271. .custom_freq = 0x2a098acb,
  272. },
  273. {
  274. .mode = DRM_MODE_TV_MODE_PAL_M,
  275. .expected_htotal = 858,
  276. .config0 = VEC_CONFIG0_PAL_M_STD,
  277. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  278. },
  279. {
  280. .mode = DRM_MODE_TV_MODE_PAL_N,
  281. .expected_htotal = 864,
  282. .config0 = VEC_CONFIG0_PAL_N_STD,
  283. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  284. },
  285. {
  286. .mode = DRM_MODE_TV_MODE_SECAM,
  287. .expected_htotal = 864,
  288. .config0 = VEC_CONFIG0_SECAM_STD,
  289. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  290. .custom_freq = 0x29c71c72,
  291. },
  292. {
  293. /* 50Hz mono */
  294. .mode = DRM_MODE_TV_MODE_MONOCHROME,
  295. .expected_htotal = 864,
  296. .config0 = VEC_CONFIG0_PAL_BDGHI_STD | VEC_CONFIG0_BURDIS |
  297. VEC_CONFIG0_CHRDIS,
  298. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  299. },
  300. {
  301. /* 60Hz mono */
  302. .mode = DRM_MODE_TV_MODE_MONOCHROME,
  303. .expected_htotal = 858,
  304. .config0 = VEC_CONFIG0_PAL_M_STD | VEC_CONFIG0_BURDIS |
  305. VEC_CONFIG0_CHRDIS,
  306. .config1 = VEC_CONFIG1_C_CVBS_CVBS,
  307. },
  308. };
  309. static inline const struct vc4_vec_tv_mode *
  310. vc4_vec_tv_mode_lookup(unsigned int mode, u16 htotal)
  311. {
  312. unsigned int i;
  313. for (i = 0; i < ARRAY_SIZE(vc4_vec_tv_modes); i++) {
  314. const struct vc4_vec_tv_mode *tv_mode = &vc4_vec_tv_modes[i];
  315. if (tv_mode->mode == mode &&
  316. tv_mode->expected_htotal == htotal)
  317. return tv_mode;
  318. }
  319. return NULL;
  320. }
  321. static const struct drm_prop_enum_list legacy_tv_mode_names[] = {
  322. { VC4_VEC_TV_MODE_NTSC, "NTSC", },
  323. { VC4_VEC_TV_MODE_NTSC_443, "NTSC-443", },
  324. { VC4_VEC_TV_MODE_NTSC_J, "NTSC-J", },
  325. { VC4_VEC_TV_MODE_PAL, "PAL", },
  326. { VC4_VEC_TV_MODE_PAL_60, "PAL-60", },
  327. { VC4_VEC_TV_MODE_PAL_M, "PAL-M", },
  328. { VC4_VEC_TV_MODE_PAL_N, "PAL-N", },
  329. { VC4_VEC_TV_MODE_SECAM, "SECAM", },
  330. { VC4_VEC_TV_MODE_MONOCHROME, "Mono", },
  331. };
  332. static enum drm_connector_status
  333. vc4_vec_connector_detect(struct drm_connector *connector, bool force)
  334. {
  335. return connector_status_unknown;
  336. }
  337. static void vc4_vec_connector_reset(struct drm_connector *connector)
  338. {
  339. drm_atomic_helper_connector_reset(connector);
  340. drm_atomic_helper_connector_tv_reset(connector);
  341. }
  342. static int
  343. vc4_vec_connector_set_property(struct drm_connector *connector,
  344. struct drm_connector_state *state,
  345. struct drm_property *property,
  346. uint64_t val)
  347. {
  348. struct vc4_vec *vec = connector_to_vc4_vec(connector);
  349. if (property != vec->legacy_tv_mode_property)
  350. return -EINVAL;
  351. switch (val) {
  352. case VC4_VEC_TV_MODE_NTSC:
  353. state->tv.mode = DRM_MODE_TV_MODE_NTSC;
  354. break;
  355. case VC4_VEC_TV_MODE_NTSC_443:
  356. state->tv.mode = DRM_MODE_TV_MODE_NTSC_443;
  357. break;
  358. case VC4_VEC_TV_MODE_NTSC_J:
  359. state->tv.mode = DRM_MODE_TV_MODE_NTSC_J;
  360. break;
  361. case VC4_VEC_TV_MODE_PAL:
  362. case VC4_VEC_TV_MODE_PAL_60:
  363. state->tv.mode = DRM_MODE_TV_MODE_PAL;
  364. break;
  365. case VC4_VEC_TV_MODE_PAL_M:
  366. state->tv.mode = DRM_MODE_TV_MODE_PAL_M;
  367. break;
  368. case VC4_VEC_TV_MODE_PAL_N:
  369. state->tv.mode = DRM_MODE_TV_MODE_PAL_N;
  370. break;
  371. case VC4_VEC_TV_MODE_SECAM:
  372. state->tv.mode = DRM_MODE_TV_MODE_SECAM;
  373. break;
  374. case VC4_VEC_TV_MODE_MONOCHROME:
  375. state->tv.mode = DRM_MODE_TV_MODE_MONOCHROME;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. return 0;
  381. }
  382. static int
  383. vc4_vec_connector_get_property(struct drm_connector *connector,
  384. const struct drm_connector_state *state,
  385. struct drm_property *property,
  386. uint64_t *val)
  387. {
  388. struct vc4_vec *vec = connector_to_vc4_vec(connector);
  389. if (property != vec->legacy_tv_mode_property)
  390. return -EINVAL;
  391. switch (state->tv.mode) {
  392. case DRM_MODE_TV_MODE_NTSC:
  393. *val = VC4_VEC_TV_MODE_NTSC;
  394. break;
  395. case DRM_MODE_TV_MODE_NTSC_443:
  396. *val = VC4_VEC_TV_MODE_NTSC_443;
  397. break;
  398. case DRM_MODE_TV_MODE_NTSC_J:
  399. *val = VC4_VEC_TV_MODE_NTSC_J;
  400. break;
  401. case DRM_MODE_TV_MODE_PAL:
  402. *val = VC4_VEC_TV_MODE_PAL;
  403. break;
  404. case DRM_MODE_TV_MODE_PAL_M:
  405. *val = VC4_VEC_TV_MODE_PAL_M;
  406. break;
  407. case DRM_MODE_TV_MODE_PAL_N:
  408. *val = VC4_VEC_TV_MODE_PAL_N;
  409. break;
  410. case DRM_MODE_TV_MODE_SECAM:
  411. *val = VC4_VEC_TV_MODE_SECAM;
  412. break;
  413. case DRM_MODE_TV_MODE_MONOCHROME:
  414. *val = VC4_VEC_TV_MODE_MONOCHROME;
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }
  421. static const struct drm_connector_funcs vc4_vec_connector_funcs = {
  422. .detect = vc4_vec_connector_detect,
  423. .fill_modes = drm_helper_probe_single_connector_modes,
  424. .reset = vc4_vec_connector_reset,
  425. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  426. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  427. .atomic_get_property = vc4_vec_connector_get_property,
  428. .atomic_set_property = vc4_vec_connector_set_property,
  429. };
  430. static const struct drm_connector_helper_funcs vc4_vec_connector_helper_funcs = {
  431. .atomic_check = drm_atomic_helper_connector_tv_check,
  432. .get_modes = drm_connector_helper_tv_get_modes,
  433. };
  434. static int vc4_vec_connector_init(struct drm_device *dev, struct vc4_vec *vec)
  435. {
  436. struct drm_connector *connector = &vec->connector;
  437. struct drm_property *prop;
  438. int ret;
  439. connector->interlace_allowed = true;
  440. ret = drmm_connector_init(dev, connector, &vc4_vec_connector_funcs,
  441. DRM_MODE_CONNECTOR_Composite, NULL);
  442. if (ret)
  443. return ret;
  444. drm_connector_helper_add(connector, &vc4_vec_connector_helper_funcs);
  445. drm_object_attach_property(&connector->base,
  446. dev->mode_config.tv_mode_property,
  447. DRM_MODE_TV_MODE_NTSC);
  448. prop = drm_property_create_enum(dev, 0, "mode",
  449. legacy_tv_mode_names,
  450. ARRAY_SIZE(legacy_tv_mode_names));
  451. if (!prop)
  452. return -ENOMEM;
  453. vec->legacy_tv_mode_property = prop;
  454. drm_object_attach_property(&connector->base, prop, VC4_VEC_TV_MODE_NTSC);
  455. drm_connector_attach_tv_margin_properties(connector);
  456. drm_connector_attach_encoder(connector, &vec->encoder.base);
  457. return 0;
  458. }
  459. static void vc4_vec_encoder_disable(struct drm_encoder *encoder,
  460. struct drm_atomic_state *state)
  461. {
  462. struct drm_device *drm = encoder->dev;
  463. struct vc4_vec *vec = encoder_to_vc4_vec(encoder);
  464. int idx, ret;
  465. if (!drm_dev_enter(drm, &idx))
  466. return;
  467. VEC_WRITE(VEC_CFG, 0);
  468. VEC_WRITE(VEC_DAC_MISC,
  469. VEC_DAC_MISC_VCD_PWRDN |
  470. VEC_DAC_MISC_BIAS_PWRDN |
  471. VEC_DAC_MISC_DAC_PWRDN |
  472. VEC_DAC_MISC_LDO_PWRDN);
  473. clk_disable_unprepare(vec->clock);
  474. ret = pm_runtime_put(&vec->pdev->dev);
  475. if (ret < 0) {
  476. drm_err(drm, "Failed to release power domain: %d\n", ret);
  477. goto err_dev_exit;
  478. }
  479. drm_dev_exit(idx);
  480. return;
  481. err_dev_exit:
  482. drm_dev_exit(idx);
  483. }
  484. static void vc4_vec_encoder_enable(struct drm_encoder *encoder,
  485. struct drm_atomic_state *state)
  486. {
  487. struct drm_device *drm = encoder->dev;
  488. struct vc4_vec *vec = encoder_to_vc4_vec(encoder);
  489. struct drm_connector *connector = &vec->connector;
  490. struct drm_connector_state *conn_state =
  491. drm_atomic_get_new_connector_state(state, connector);
  492. struct drm_display_mode *adjusted_mode =
  493. &encoder->crtc->state->adjusted_mode;
  494. const struct vc4_vec_tv_mode *tv_mode;
  495. int idx, ret;
  496. if (!drm_dev_enter(drm, &idx))
  497. return;
  498. tv_mode = vc4_vec_tv_mode_lookup(conn_state->tv.mode,
  499. adjusted_mode->htotal);
  500. if (!tv_mode)
  501. goto err_dev_exit;
  502. ret = pm_runtime_resume_and_get(&vec->pdev->dev);
  503. if (ret < 0) {
  504. drm_err(drm, "Failed to retain power domain: %d\n", ret);
  505. goto err_dev_exit;
  506. }
  507. /*
  508. * We need to set the clock rate each time we enable the encoder
  509. * because there's a chance we share the same parent with the HDMI
  510. * clock, and both drivers are requesting different rates.
  511. * The good news is, these 2 encoders cannot be enabled at the same
  512. * time, thus preventing incompatible rate requests.
  513. */
  514. ret = clk_set_rate(vec->clock, 108000000);
  515. if (ret) {
  516. drm_err(drm, "Failed to set clock rate: %d\n", ret);
  517. goto err_put_runtime_pm;
  518. }
  519. ret = clk_prepare_enable(vec->clock);
  520. if (ret) {
  521. drm_err(drm, "Failed to turn on core clock: %d\n", ret);
  522. goto err_put_runtime_pm;
  523. }
  524. /* Reset the different blocks */
  525. VEC_WRITE(VEC_WSE_RESET, 1);
  526. VEC_WRITE(VEC_SOFT_RESET, 1);
  527. /* Disable the CGSM-A and WSE blocks */
  528. VEC_WRITE(VEC_WSE_CONTROL, 0);
  529. /* Write config common to all modes. */
  530. /*
  531. * Color subcarrier phase: phase = 360 * SCHPH / 256.
  532. * 0x28 <=> 39.375 deg.
  533. */
  534. VEC_WRITE(VEC_SCHPH, 0x28);
  535. /*
  536. * Reset to default values.
  537. */
  538. VEC_WRITE(VEC_CLMP0_START, 0xac);
  539. VEC_WRITE(VEC_CLMP0_END, 0xec);
  540. VEC_WRITE(VEC_CONFIG2,
  541. VEC_CONFIG2_UV_DIG_DIS |
  542. VEC_CONFIG2_RGB_DIG_DIS |
  543. ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ? 0 : VEC_CONFIG2_PROG_SCAN));
  544. VEC_WRITE(VEC_CONFIG3, VEC_CONFIG3_HORIZ_LEN_STD);
  545. VEC_WRITE(VEC_DAC_CONFIG, vec->variant->dac_config);
  546. /* Mask all interrupts. */
  547. VEC_WRITE(VEC_MASK0, 0);
  548. VEC_WRITE(VEC_CONFIG0, tv_mode->config0);
  549. VEC_WRITE(VEC_CONFIG1, tv_mode->config1);
  550. if (tv_mode->custom_freq) {
  551. VEC_WRITE(VEC_FREQ3_2,
  552. (tv_mode->custom_freq >> 16) & 0xffff);
  553. VEC_WRITE(VEC_FREQ1_0,
  554. tv_mode->custom_freq & 0xffff);
  555. }
  556. VEC_WRITE(VEC_DAC_MISC,
  557. VEC_DAC_MISC_VID_ACT | VEC_DAC_MISC_DAC_RST_N);
  558. VEC_WRITE(VEC_CFG, VEC_CFG_VEC_EN);
  559. drm_dev_exit(idx);
  560. return;
  561. err_put_runtime_pm:
  562. pm_runtime_put(&vec->pdev->dev);
  563. err_dev_exit:
  564. drm_dev_exit(idx);
  565. }
  566. static int vc4_vec_encoder_atomic_check(struct drm_encoder *encoder,
  567. struct drm_crtc_state *crtc_state,
  568. struct drm_connector_state *conn_state)
  569. {
  570. const struct drm_display_mode *mode = &crtc_state->adjusted_mode;
  571. const struct vc4_vec_tv_mode *tv_mode;
  572. tv_mode = vc4_vec_tv_mode_lookup(conn_state->tv.mode, mode->htotal);
  573. if (!tv_mode)
  574. return -EINVAL;
  575. if (mode->crtc_hdisplay % 4)
  576. return -EINVAL;
  577. if (!(mode->crtc_hsync_end - mode->crtc_hsync_start))
  578. return -EINVAL;
  579. switch (mode->htotal) {
  580. /* NTSC */
  581. case 858:
  582. if (mode->crtc_vtotal > 262)
  583. return -EINVAL;
  584. if (mode->crtc_vdisplay < 1 || mode->crtc_vdisplay > 253)
  585. return -EINVAL;
  586. if (!(mode->crtc_vsync_start - mode->crtc_vdisplay))
  587. return -EINVAL;
  588. if ((mode->crtc_vsync_end - mode->crtc_vsync_start) != 3)
  589. return -EINVAL;
  590. if ((mode->crtc_vtotal - mode->crtc_vsync_end) < 4)
  591. return -EINVAL;
  592. break;
  593. /* PAL/SECAM */
  594. case 864:
  595. if (mode->crtc_vtotal > 312)
  596. return -EINVAL;
  597. if (mode->crtc_vdisplay < 1 || mode->crtc_vdisplay > 305)
  598. return -EINVAL;
  599. if (!(mode->crtc_vsync_start - mode->crtc_vdisplay))
  600. return -EINVAL;
  601. if ((mode->crtc_vsync_end - mode->crtc_vsync_start) != 3)
  602. return -EINVAL;
  603. if ((mode->crtc_vtotal - mode->crtc_vsync_end) < 2)
  604. return -EINVAL;
  605. break;
  606. default:
  607. return -EINVAL;
  608. }
  609. return 0;
  610. }
  611. static const struct drm_encoder_helper_funcs vc4_vec_encoder_helper_funcs = {
  612. .atomic_check = vc4_vec_encoder_atomic_check,
  613. .atomic_disable = vc4_vec_encoder_disable,
  614. .atomic_enable = vc4_vec_encoder_enable,
  615. };
  616. static int vc4_vec_late_register(struct drm_encoder *encoder)
  617. {
  618. struct drm_device *drm = encoder->dev;
  619. struct vc4_vec *vec = encoder_to_vc4_vec(encoder);
  620. vc4_debugfs_add_regset32(drm, "vec_regs", &vec->regset);
  621. return 0;
  622. }
  623. static const struct drm_encoder_funcs vc4_vec_encoder_funcs = {
  624. .late_register = vc4_vec_late_register,
  625. };
  626. static const struct vc4_vec_variant bcm2835_vec_variant = {
  627. .dac_config = VEC_DAC_CONFIG_DAC_CTRL(0xc) |
  628. VEC_DAC_CONFIG_DRIVER_CTRL(0xc) |
  629. VEC_DAC_CONFIG_LDO_BIAS_CTRL(0x46)
  630. };
  631. static const struct vc4_vec_variant bcm2711_vec_variant = {
  632. .dac_config = VEC_DAC_CONFIG_DAC_CTRL(0x0) |
  633. VEC_DAC_CONFIG_DRIVER_CTRL(0x80) |
  634. VEC_DAC_CONFIG_LDO_BIAS_CTRL(0x61)
  635. };
  636. static const struct of_device_id vc4_vec_dt_match[] = {
  637. { .compatible = "brcm,bcm2835-vec", .data = &bcm2835_vec_variant },
  638. { .compatible = "brcm,bcm2711-vec", .data = &bcm2711_vec_variant },
  639. { /* sentinel */ },
  640. };
  641. static int vc4_vec_bind(struct device *dev, struct device *master, void *data)
  642. {
  643. struct platform_device *pdev = to_platform_device(dev);
  644. struct drm_device *drm = dev_get_drvdata(master);
  645. struct vc4_vec *vec;
  646. int ret;
  647. ret = drm_mode_create_tv_properties(drm,
  648. BIT(DRM_MODE_TV_MODE_NTSC) |
  649. BIT(DRM_MODE_TV_MODE_NTSC_443) |
  650. BIT(DRM_MODE_TV_MODE_NTSC_J) |
  651. BIT(DRM_MODE_TV_MODE_PAL) |
  652. BIT(DRM_MODE_TV_MODE_PAL_M) |
  653. BIT(DRM_MODE_TV_MODE_PAL_N) |
  654. BIT(DRM_MODE_TV_MODE_SECAM) |
  655. BIT(DRM_MODE_TV_MODE_MONOCHROME));
  656. if (ret)
  657. return ret;
  658. vec = drmm_kzalloc(drm, sizeof(*vec), GFP_KERNEL);
  659. if (!vec)
  660. return -ENOMEM;
  661. vec->encoder.type = VC4_ENCODER_TYPE_VEC;
  662. vec->pdev = pdev;
  663. vec->variant = (const struct vc4_vec_variant *)
  664. of_device_get_match_data(dev);
  665. vec->regs = vc4_ioremap_regs(pdev, 0);
  666. if (IS_ERR(vec->regs))
  667. return PTR_ERR(vec->regs);
  668. vec->regset.base = vec->regs;
  669. vec->regset.regs = vec_regs;
  670. vec->regset.nregs = ARRAY_SIZE(vec_regs);
  671. vec->clock = devm_clk_get(dev, NULL);
  672. if (IS_ERR(vec->clock)) {
  673. ret = PTR_ERR(vec->clock);
  674. if (ret != -EPROBE_DEFER)
  675. drm_err(drm, "Failed to get clock: %d\n", ret);
  676. return ret;
  677. }
  678. ret = devm_pm_runtime_enable(dev);
  679. if (ret)
  680. return ret;
  681. ret = drmm_encoder_init(drm, &vec->encoder.base,
  682. &vc4_vec_encoder_funcs,
  683. DRM_MODE_ENCODER_TVDAC,
  684. NULL);
  685. if (ret)
  686. return ret;
  687. drm_encoder_helper_add(&vec->encoder.base, &vc4_vec_encoder_helper_funcs);
  688. ret = vc4_vec_connector_init(drm, vec);
  689. if (ret)
  690. return ret;
  691. dev_set_drvdata(dev, vec);
  692. return 0;
  693. }
  694. static const struct component_ops vc4_vec_ops = {
  695. .bind = vc4_vec_bind,
  696. };
  697. static int vc4_vec_dev_probe(struct platform_device *pdev)
  698. {
  699. return component_add(&pdev->dev, &vc4_vec_ops);
  700. }
  701. static void vc4_vec_dev_remove(struct platform_device *pdev)
  702. {
  703. component_del(&pdev->dev, &vc4_vec_ops);
  704. }
  705. struct platform_driver vc4_vec_driver = {
  706. .probe = vc4_vec_dev_probe,
  707. .remove_new = vc4_vec_dev_remove,
  708. .driver = {
  709. .name = "vc4_vec",
  710. .of_match_table = vc4_vec_dt_match,
  711. },
  712. };