vc4_vec.c 18 KB

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