vc4_crtc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /**
  9. * DOC: VC4 CRTC module
  10. *
  11. * In VC4, the Pixel Valve is what most closely corresponds to the
  12. * DRM's concept of a CRTC. The PV generates video timings from the
  13. * output's clock plus its configuration. It pulls scaled pixels from
  14. * the HVS at that timing, and feeds it to the encoder.
  15. *
  16. * However, the DRM CRTC also collects the configuration of all the
  17. * DRM planes attached to it. As a result, this file also manages
  18. * setup of the VC4 HVS's display elements on the CRTC.
  19. *
  20. * The 2835 has 3 different pixel valves. pv0 in the audio power
  21. * domain feeds DSI0 or DPI, while pv1 feeds DS1 or SMI. pv2 in the
  22. * image domain can feed either HDMI or the SDTV controller. The
  23. * pixel valve chooses from the CPRMAN clocks (HSM for HDMI, VEC for
  24. * SDTV, etc.) according to which output type is chosen in the mux.
  25. *
  26. * For power management, the pixel valve's registers are all clocked
  27. * by the AXI clock, while the timings and FIFOs make use of the
  28. * output-specific clock. Since the encoders also directly consume
  29. * the CPRMAN clocks, and know what timings they need, they are the
  30. * ones that set the clock.
  31. */
  32. #include "drm_atomic.h"
  33. #include "drm_atomic_helper.h"
  34. #include "drm_crtc_helper.h"
  35. #include "linux/clk.h"
  36. #include "linux/component.h"
  37. #include "linux/of_device.h"
  38. #include "vc4_drv.h"
  39. #include "vc4_regs.h"
  40. struct vc4_crtc {
  41. struct drm_crtc base;
  42. const struct vc4_crtc_data *data;
  43. void __iomem *regs;
  44. /* Which HVS channel we're using for our CRTC. */
  45. int channel;
  46. /* Pointer to the actual hardware display list memory for the
  47. * crtc.
  48. */
  49. u32 __iomem *dlist;
  50. u32 dlist_size; /* in dwords */
  51. struct drm_pending_vblank_event *event;
  52. };
  53. static inline struct vc4_crtc *
  54. to_vc4_crtc(struct drm_crtc *crtc)
  55. {
  56. return (struct vc4_crtc *)crtc;
  57. }
  58. struct vc4_crtc_data {
  59. /* Which channel of the HVS this pixelvalve sources from. */
  60. int hvs_channel;
  61. enum vc4_encoder_type encoder0_type;
  62. enum vc4_encoder_type encoder1_type;
  63. };
  64. #define CRTC_WRITE(offset, val) writel(val, vc4_crtc->regs + (offset))
  65. #define CRTC_READ(offset) readl(vc4_crtc->regs + (offset))
  66. #define CRTC_REG(reg) { reg, #reg }
  67. static const struct {
  68. u32 reg;
  69. const char *name;
  70. } crtc_regs[] = {
  71. CRTC_REG(PV_CONTROL),
  72. CRTC_REG(PV_V_CONTROL),
  73. CRTC_REG(PV_VSYNCD),
  74. CRTC_REG(PV_HORZA),
  75. CRTC_REG(PV_HORZB),
  76. CRTC_REG(PV_VERTA),
  77. CRTC_REG(PV_VERTB),
  78. CRTC_REG(PV_VERTA_EVEN),
  79. CRTC_REG(PV_VERTB_EVEN),
  80. CRTC_REG(PV_INTEN),
  81. CRTC_REG(PV_INTSTAT),
  82. CRTC_REG(PV_STAT),
  83. CRTC_REG(PV_HACT_ACT),
  84. };
  85. static void vc4_crtc_dump_regs(struct vc4_crtc *vc4_crtc)
  86. {
  87. int i;
  88. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  89. DRM_INFO("0x%04x (%s): 0x%08x\n",
  90. crtc_regs[i].reg, crtc_regs[i].name,
  91. CRTC_READ(crtc_regs[i].reg));
  92. }
  93. }
  94. #ifdef CONFIG_DEBUG_FS
  95. int vc4_crtc_debugfs_regs(struct seq_file *m, void *unused)
  96. {
  97. struct drm_info_node *node = (struct drm_info_node *)m->private;
  98. struct drm_device *dev = node->minor->dev;
  99. int crtc_index = (uintptr_t)node->info_ent->data;
  100. struct drm_crtc *crtc;
  101. struct vc4_crtc *vc4_crtc;
  102. int i;
  103. i = 0;
  104. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  105. if (i == crtc_index)
  106. break;
  107. i++;
  108. }
  109. if (!crtc)
  110. return 0;
  111. vc4_crtc = to_vc4_crtc(crtc);
  112. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  113. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  114. crtc_regs[i].name, crtc_regs[i].reg,
  115. CRTC_READ(crtc_regs[i].reg));
  116. }
  117. return 0;
  118. }
  119. #endif
  120. static void vc4_crtc_destroy(struct drm_crtc *crtc)
  121. {
  122. drm_crtc_cleanup(crtc);
  123. }
  124. static u32 vc4_get_fifo_full_level(u32 format)
  125. {
  126. static const u32 fifo_len_bytes = 64;
  127. static const u32 hvs_latency_pix = 6;
  128. switch (format) {
  129. case PV_CONTROL_FORMAT_DSIV_16:
  130. case PV_CONTROL_FORMAT_DSIC_16:
  131. return fifo_len_bytes - 2 * hvs_latency_pix;
  132. case PV_CONTROL_FORMAT_DSIV_18:
  133. return fifo_len_bytes - 14;
  134. case PV_CONTROL_FORMAT_24:
  135. case PV_CONTROL_FORMAT_DSIV_24:
  136. default:
  137. return fifo_len_bytes - 3 * hvs_latency_pix;
  138. }
  139. }
  140. /*
  141. * Returns the clock select bit for the connector attached to the
  142. * CRTC.
  143. */
  144. static int vc4_get_clock_select(struct drm_crtc *crtc)
  145. {
  146. struct drm_connector *connector;
  147. drm_for_each_connector(connector, crtc->dev) {
  148. if (connector->state->crtc == crtc) {
  149. struct drm_encoder *encoder = connector->encoder;
  150. struct vc4_encoder *vc4_encoder =
  151. to_vc4_encoder(encoder);
  152. return vc4_encoder->clock_select;
  153. }
  154. }
  155. return -1;
  156. }
  157. static void vc4_crtc_mode_set_nofb(struct drm_crtc *crtc)
  158. {
  159. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  160. struct drm_crtc_state *state = crtc->state;
  161. struct drm_display_mode *mode = &state->adjusted_mode;
  162. bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
  163. u32 vactive = (mode->vdisplay >> (interlace ? 1 : 0));
  164. u32 format = PV_CONTROL_FORMAT_24;
  165. bool debug_dump_regs = false;
  166. int clock_select = vc4_get_clock_select(crtc);
  167. if (debug_dump_regs) {
  168. DRM_INFO("CRTC %d regs before:\n", drm_crtc_index(crtc));
  169. vc4_crtc_dump_regs(vc4_crtc);
  170. }
  171. /* Reset the PV fifo. */
  172. CRTC_WRITE(PV_CONTROL, 0);
  173. CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR | PV_CONTROL_EN);
  174. CRTC_WRITE(PV_CONTROL, 0);
  175. CRTC_WRITE(PV_HORZA,
  176. VC4_SET_FIELD(mode->htotal - mode->hsync_end,
  177. PV_HORZA_HBP) |
  178. VC4_SET_FIELD(mode->hsync_end - mode->hsync_start,
  179. PV_HORZA_HSYNC));
  180. CRTC_WRITE(PV_HORZB,
  181. VC4_SET_FIELD(mode->hsync_start - mode->hdisplay,
  182. PV_HORZB_HFP) |
  183. VC4_SET_FIELD(mode->hdisplay, PV_HORZB_HACTIVE));
  184. if (interlace) {
  185. CRTC_WRITE(PV_VERTA_EVEN,
  186. VC4_SET_FIELD(mode->vtotal - mode->vsync_end - 1,
  187. PV_VERTA_VBP) |
  188. VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  189. PV_VERTA_VSYNC));
  190. CRTC_WRITE(PV_VERTB_EVEN,
  191. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  192. PV_VERTB_VFP) |
  193. VC4_SET_FIELD(vactive, PV_VERTB_VACTIVE));
  194. }
  195. CRTC_WRITE(PV_HACT_ACT, mode->hdisplay);
  196. CRTC_WRITE(PV_V_CONTROL,
  197. PV_VCONTROL_CONTINUOUS |
  198. (interlace ? PV_VCONTROL_INTERLACE : 0));
  199. CRTC_WRITE(PV_CONTROL,
  200. VC4_SET_FIELD(format, PV_CONTROL_FORMAT) |
  201. VC4_SET_FIELD(vc4_get_fifo_full_level(format),
  202. PV_CONTROL_FIFO_LEVEL) |
  203. PV_CONTROL_CLR_AT_START |
  204. PV_CONTROL_TRIGGER_UNDERFLOW |
  205. PV_CONTROL_WAIT_HSTART |
  206. VC4_SET_FIELD(clock_select, PV_CONTROL_CLK_SELECT) |
  207. PV_CONTROL_FIFO_CLR |
  208. PV_CONTROL_EN);
  209. if (debug_dump_regs) {
  210. DRM_INFO("CRTC %d regs after:\n", drm_crtc_index(crtc));
  211. vc4_crtc_dump_regs(vc4_crtc);
  212. }
  213. }
  214. static void require_hvs_enabled(struct drm_device *dev)
  215. {
  216. struct vc4_dev *vc4 = to_vc4_dev(dev);
  217. WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) !=
  218. SCALER_DISPCTRL_ENABLE);
  219. }
  220. static void vc4_crtc_disable(struct drm_crtc *crtc)
  221. {
  222. struct drm_device *dev = crtc->dev;
  223. struct vc4_dev *vc4 = to_vc4_dev(dev);
  224. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  225. u32 chan = vc4_crtc->channel;
  226. int ret;
  227. require_hvs_enabled(dev);
  228. CRTC_WRITE(PV_V_CONTROL,
  229. CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN);
  230. ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1);
  231. WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n");
  232. if (HVS_READ(SCALER_DISPCTRLX(chan)) &
  233. SCALER_DISPCTRLX_ENABLE) {
  234. HVS_WRITE(SCALER_DISPCTRLX(chan),
  235. SCALER_DISPCTRLX_RESET);
  236. /* While the docs say that reset is self-clearing, it
  237. * seems it doesn't actually.
  238. */
  239. HVS_WRITE(SCALER_DISPCTRLX(chan), 0);
  240. }
  241. /* Once we leave, the scaler should be disabled and its fifo empty. */
  242. WARN_ON_ONCE(HVS_READ(SCALER_DISPCTRLX(chan)) & SCALER_DISPCTRLX_RESET);
  243. WARN_ON_ONCE(VC4_GET_FIELD(HVS_READ(SCALER_DISPSTATX(chan)),
  244. SCALER_DISPSTATX_MODE) !=
  245. SCALER_DISPSTATX_MODE_DISABLED);
  246. WARN_ON_ONCE((HVS_READ(SCALER_DISPSTATX(chan)) &
  247. (SCALER_DISPSTATX_FULL | SCALER_DISPSTATX_EMPTY)) !=
  248. SCALER_DISPSTATX_EMPTY);
  249. }
  250. static void vc4_crtc_enable(struct drm_crtc *crtc)
  251. {
  252. struct drm_device *dev = crtc->dev;
  253. struct vc4_dev *vc4 = to_vc4_dev(dev);
  254. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  255. struct drm_crtc_state *state = crtc->state;
  256. struct drm_display_mode *mode = &state->adjusted_mode;
  257. require_hvs_enabled(dev);
  258. /* Turn on the scaler, which will wait for vstart to start
  259. * compositing.
  260. */
  261. HVS_WRITE(SCALER_DISPCTRLX(vc4_crtc->channel),
  262. VC4_SET_FIELD(mode->hdisplay, SCALER_DISPCTRLX_WIDTH) |
  263. VC4_SET_FIELD(mode->vdisplay, SCALER_DISPCTRLX_HEIGHT) |
  264. SCALER_DISPCTRLX_ENABLE);
  265. /* Turn on the pixel valve, which will emit the vstart signal. */
  266. CRTC_WRITE(PV_V_CONTROL,
  267. CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
  268. }
  269. static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
  270. struct drm_crtc_state *state)
  271. {
  272. struct drm_device *dev = crtc->dev;
  273. struct vc4_dev *vc4 = to_vc4_dev(dev);
  274. struct drm_plane *plane;
  275. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  276. u32 dlist_count = 0;
  277. /* The pixelvalve can only feed one encoder (and encoders are
  278. * 1:1 with connectors.)
  279. */
  280. if (drm_atomic_connectors_for_crtc(state->state, crtc) > 1)
  281. return -EINVAL;
  282. drm_atomic_crtc_state_for_each_plane(plane, state) {
  283. struct drm_plane_state *plane_state =
  284. state->state->plane_states[drm_plane_index(plane)];
  285. /* plane might not have changed, in which case take
  286. * current state:
  287. */
  288. if (!plane_state)
  289. plane_state = plane->state;
  290. dlist_count += vc4_plane_dlist_size(plane_state);
  291. }
  292. dlist_count++; /* Account for SCALER_CTL0_END. */
  293. if (!vc4_crtc->dlist || dlist_count > vc4_crtc->dlist_size) {
  294. vc4_crtc->dlist = ((u32 __iomem *)vc4->hvs->dlist +
  295. HVS_BOOTLOADER_DLIST_END);
  296. vc4_crtc->dlist_size = ((SCALER_DLIST_SIZE >> 2) -
  297. HVS_BOOTLOADER_DLIST_END);
  298. if (dlist_count > vc4_crtc->dlist_size) {
  299. DRM_DEBUG_KMS("dlist too large for CRTC (%d > %d).\n",
  300. dlist_count, vc4_crtc->dlist_size);
  301. return -EINVAL;
  302. }
  303. }
  304. return 0;
  305. }
  306. static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
  307. struct drm_crtc_state *old_state)
  308. {
  309. struct drm_device *dev = crtc->dev;
  310. struct vc4_dev *vc4 = to_vc4_dev(dev);
  311. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  312. struct drm_plane *plane;
  313. bool debug_dump_regs = false;
  314. u32 __iomem *dlist_next = vc4_crtc->dlist;
  315. if (debug_dump_regs) {
  316. DRM_INFO("CRTC %d HVS before:\n", drm_crtc_index(crtc));
  317. vc4_hvs_dump_state(dev);
  318. }
  319. /* Copy all the active planes' dlist contents to the hardware dlist.
  320. *
  321. * XXX: If the new display list was large enough that it
  322. * overlapped a currently-read display list, we need to do
  323. * something like disable scanout before putting in the new
  324. * list. For now, we're safe because we only have the two
  325. * planes.
  326. */
  327. drm_atomic_crtc_for_each_plane(plane, crtc) {
  328. dlist_next += vc4_plane_write_dlist(plane, dlist_next);
  329. }
  330. if (dlist_next == vc4_crtc->dlist) {
  331. /* If no planes were enabled, use the SCALER_CTL0_END
  332. * at the start of the display list memory (in the
  333. * bootloader section). We'll rewrite that
  334. * SCALER_CTL0_END, just in case, though.
  335. */
  336. writel(SCALER_CTL0_END, vc4->hvs->dlist);
  337. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel), 0);
  338. } else {
  339. writel(SCALER_CTL0_END, dlist_next);
  340. dlist_next++;
  341. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
  342. (u32 __iomem *)vc4_crtc->dlist -
  343. (u32 __iomem *)vc4->hvs->dlist);
  344. /* Make the next display list start after ours. */
  345. vc4_crtc->dlist_size -= (dlist_next - vc4_crtc->dlist);
  346. vc4_crtc->dlist = dlist_next;
  347. }
  348. if (debug_dump_regs) {
  349. DRM_INFO("CRTC %d HVS after:\n", drm_crtc_index(crtc));
  350. vc4_hvs_dump_state(dev);
  351. }
  352. if (crtc->state->event) {
  353. unsigned long flags;
  354. crtc->state->event->pipe = drm_crtc_index(crtc);
  355. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  356. spin_lock_irqsave(&dev->event_lock, flags);
  357. vc4_crtc->event = crtc->state->event;
  358. spin_unlock_irqrestore(&dev->event_lock, flags);
  359. crtc->state->event = NULL;
  360. }
  361. }
  362. int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id)
  363. {
  364. struct vc4_dev *vc4 = to_vc4_dev(dev);
  365. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  366. CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
  367. return 0;
  368. }
  369. void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id)
  370. {
  371. struct vc4_dev *vc4 = to_vc4_dev(dev);
  372. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  373. CRTC_WRITE(PV_INTEN, 0);
  374. }
  375. static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
  376. {
  377. struct drm_crtc *crtc = &vc4_crtc->base;
  378. struct drm_device *dev = crtc->dev;
  379. unsigned long flags;
  380. spin_lock_irqsave(&dev->event_lock, flags);
  381. if (vc4_crtc->event) {
  382. drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
  383. vc4_crtc->event = NULL;
  384. }
  385. spin_unlock_irqrestore(&dev->event_lock, flags);
  386. }
  387. static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
  388. {
  389. struct vc4_crtc *vc4_crtc = data;
  390. u32 stat = CRTC_READ(PV_INTSTAT);
  391. irqreturn_t ret = IRQ_NONE;
  392. if (stat & PV_INT_VFP_START) {
  393. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  394. drm_crtc_handle_vblank(&vc4_crtc->base);
  395. vc4_crtc_handle_page_flip(vc4_crtc);
  396. ret = IRQ_HANDLED;
  397. }
  398. return ret;
  399. }
  400. static const struct drm_crtc_funcs vc4_crtc_funcs = {
  401. .set_config = drm_atomic_helper_set_config,
  402. .destroy = vc4_crtc_destroy,
  403. .page_flip = drm_atomic_helper_page_flip,
  404. .set_property = NULL,
  405. .cursor_set = NULL, /* handled by drm_mode_cursor_universal */
  406. .cursor_move = NULL, /* handled by drm_mode_cursor_universal */
  407. .reset = drm_atomic_helper_crtc_reset,
  408. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  409. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  410. };
  411. static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
  412. .mode_set_nofb = vc4_crtc_mode_set_nofb,
  413. .disable = vc4_crtc_disable,
  414. .enable = vc4_crtc_enable,
  415. .atomic_check = vc4_crtc_atomic_check,
  416. .atomic_flush = vc4_crtc_atomic_flush,
  417. };
  418. /* Frees the page flip event when the DRM device is closed with the
  419. * event still outstanding.
  420. */
  421. void vc4_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  422. {
  423. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  424. struct drm_device *dev = crtc->dev;
  425. unsigned long flags;
  426. spin_lock_irqsave(&dev->event_lock, flags);
  427. if (vc4_crtc->event && vc4_crtc->event->base.file_priv == file) {
  428. vc4_crtc->event->base.destroy(&vc4_crtc->event->base);
  429. drm_crtc_vblank_put(crtc);
  430. vc4_crtc->event = NULL;
  431. }
  432. spin_unlock_irqrestore(&dev->event_lock, flags);
  433. }
  434. static const struct vc4_crtc_data pv0_data = {
  435. .hvs_channel = 0,
  436. .encoder0_type = VC4_ENCODER_TYPE_DSI0,
  437. .encoder1_type = VC4_ENCODER_TYPE_DPI,
  438. };
  439. static const struct vc4_crtc_data pv1_data = {
  440. .hvs_channel = 2,
  441. .encoder0_type = VC4_ENCODER_TYPE_DSI1,
  442. .encoder1_type = VC4_ENCODER_TYPE_SMI,
  443. };
  444. static const struct vc4_crtc_data pv2_data = {
  445. .hvs_channel = 1,
  446. .encoder0_type = VC4_ENCODER_TYPE_VEC,
  447. .encoder1_type = VC4_ENCODER_TYPE_HDMI,
  448. };
  449. static const struct of_device_id vc4_crtc_dt_match[] = {
  450. { .compatible = "brcm,bcm2835-pixelvalve0", .data = &pv0_data },
  451. { .compatible = "brcm,bcm2835-pixelvalve1", .data = &pv1_data },
  452. { .compatible = "brcm,bcm2835-pixelvalve2", .data = &pv2_data },
  453. {}
  454. };
  455. static void vc4_set_crtc_possible_masks(struct drm_device *drm,
  456. struct drm_crtc *crtc)
  457. {
  458. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  459. struct drm_encoder *encoder;
  460. drm_for_each_encoder(encoder, drm) {
  461. struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
  462. if (vc4_encoder->type == vc4_crtc->data->encoder0_type) {
  463. vc4_encoder->clock_select = 0;
  464. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  465. } else if (vc4_encoder->type == vc4_crtc->data->encoder1_type) {
  466. vc4_encoder->clock_select = 1;
  467. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  468. }
  469. }
  470. }
  471. static int vc4_crtc_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_crtc *vc4_crtc;
  477. struct drm_crtc *crtc;
  478. struct drm_plane *primary_plane, *cursor_plane;
  479. const struct of_device_id *match;
  480. int ret;
  481. vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL);
  482. if (!vc4_crtc)
  483. return -ENOMEM;
  484. crtc = &vc4_crtc->base;
  485. match = of_match_device(vc4_crtc_dt_match, dev);
  486. if (!match)
  487. return -ENODEV;
  488. vc4_crtc->data = match->data;
  489. vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
  490. if (IS_ERR(vc4_crtc->regs))
  491. return PTR_ERR(vc4_crtc->regs);
  492. /* For now, we create just the primary and the legacy cursor
  493. * planes. We should be able to stack more planes on easily,
  494. * but to do that we would need to compute the bandwidth
  495. * requirement of the plane configuration, and reject ones
  496. * that will take too much.
  497. */
  498. primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY);
  499. if (IS_ERR(primary_plane)) {
  500. dev_err(dev, "failed to construct primary plane\n");
  501. ret = PTR_ERR(primary_plane);
  502. goto err;
  503. }
  504. cursor_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_CURSOR);
  505. if (IS_ERR(cursor_plane)) {
  506. dev_err(dev, "failed to construct cursor plane\n");
  507. ret = PTR_ERR(cursor_plane);
  508. goto err_primary;
  509. }
  510. drm_crtc_init_with_planes(drm, crtc, primary_plane, cursor_plane,
  511. &vc4_crtc_funcs);
  512. drm_crtc_helper_add(crtc, &vc4_crtc_helper_funcs);
  513. primary_plane->crtc = crtc;
  514. cursor_plane->crtc = crtc;
  515. vc4->crtc[drm_crtc_index(crtc)] = vc4_crtc;
  516. vc4_crtc->channel = vc4_crtc->data->hvs_channel;
  517. CRTC_WRITE(PV_INTEN, 0);
  518. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  519. ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
  520. vc4_crtc_irq_handler, 0, "vc4 crtc", vc4_crtc);
  521. if (ret)
  522. goto err_cursor;
  523. vc4_set_crtc_possible_masks(drm, crtc);
  524. platform_set_drvdata(pdev, vc4_crtc);
  525. return 0;
  526. err_cursor:
  527. cursor_plane->funcs->destroy(cursor_plane);
  528. err_primary:
  529. primary_plane->funcs->destroy(primary_plane);
  530. err:
  531. return ret;
  532. }
  533. static void vc4_crtc_unbind(struct device *dev, struct device *master,
  534. void *data)
  535. {
  536. struct platform_device *pdev = to_platform_device(dev);
  537. struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
  538. vc4_crtc_destroy(&vc4_crtc->base);
  539. CRTC_WRITE(PV_INTEN, 0);
  540. platform_set_drvdata(pdev, NULL);
  541. }
  542. static const struct component_ops vc4_crtc_ops = {
  543. .bind = vc4_crtc_bind,
  544. .unbind = vc4_crtc_unbind,
  545. };
  546. static int vc4_crtc_dev_probe(struct platform_device *pdev)
  547. {
  548. return component_add(&pdev->dev, &vc4_crtc_ops);
  549. }
  550. static int vc4_crtc_dev_remove(struct platform_device *pdev)
  551. {
  552. component_del(&pdev->dev, &vc4_crtc_ops);
  553. return 0;
  554. }
  555. struct platform_driver vc4_crtc_driver = {
  556. .probe = vc4_crtc_dev_probe,
  557. .remove = vc4_crtc_dev_remove,
  558. .driver = {
  559. .name = "vc4_crtc",
  560. .of_match_table = vc4_crtc_dt_match,
  561. },
  562. };