vc4_txp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright © 2018 Broadcom
  4. *
  5. * Authors:
  6. * Eric Anholt <eric@anholt.net>
  7. * Boris Brezillon <boris.brezillon@bootlin.com>
  8. */
  9. #include <drm/drm_atomic_helper.h>
  10. #include <drm/drm_fb_cma_helper.h>
  11. #include <drm/drm_crtc_helper.h>
  12. #include <drm/drm_edid.h>
  13. #include <drm/drm_panel.h>
  14. #include <drm/drm_writeback.h>
  15. #include <linux/clk.h>
  16. #include <linux/component.h>
  17. #include <linux/of_graph.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/pm_runtime.h>
  20. #include "vc4_drv.h"
  21. #include "vc4_regs.h"
  22. /* Base address of the output. Raster formats must be 4-byte aligned,
  23. * T and LT must be 16-byte aligned or maybe utile-aligned (docs are
  24. * inconsistent, but probably utile).
  25. */
  26. #define TXP_DST_PTR 0x00
  27. /* Pitch in bytes for raster images, 16-byte aligned. For tiled, it's
  28. * the width in tiles.
  29. */
  30. #define TXP_DST_PITCH 0x04
  31. /* For T-tiled imgaes, DST_PITCH should be the number of tiles wide,
  32. * shifted up.
  33. */
  34. # define TXP_T_TILE_WIDTH_SHIFT 7
  35. /* For LT-tiled images, DST_PITCH should be the number of utiles wide,
  36. * shifted up.
  37. */
  38. # define TXP_LT_TILE_WIDTH_SHIFT 4
  39. /* Pre-rotation width/height of the image. Must match HVS config.
  40. *
  41. * If TFORMAT and 32-bit, limit is 1920 for 32-bit and 3840 to 16-bit
  42. * and width/height must be tile or utile-aligned as appropriate. If
  43. * transposing (rotating), width is limited to 1920.
  44. *
  45. * Height is limited to various numbers between 4088 and 4095. I'd
  46. * just use 4088 to be safe.
  47. */
  48. #define TXP_DIM 0x08
  49. # define TXP_HEIGHT_SHIFT 16
  50. # define TXP_HEIGHT_MASK GENMASK(31, 16)
  51. # define TXP_WIDTH_SHIFT 0
  52. # define TXP_WIDTH_MASK GENMASK(15, 0)
  53. #define TXP_DST_CTRL 0x0c
  54. /* These bits are set to 0x54 */
  55. #define TXP_PILOT_SHIFT 24
  56. #define TXP_PILOT_MASK GENMASK(31, 24)
  57. /* Bits 22-23 are set to 0x01 */
  58. #define TXP_VERSION_SHIFT 22
  59. #define TXP_VERSION_MASK GENMASK(23, 22)
  60. /* Powers down the internal memory. */
  61. # define TXP_POWERDOWN BIT(21)
  62. /* Enables storing the alpha component in 8888/4444, instead of
  63. * filling with ~ALPHA_INVERT.
  64. */
  65. # define TXP_ALPHA_ENABLE BIT(20)
  66. /* 4 bits, each enables stores for a channel in each set of 4 bytes.
  67. * Set to 0xf for normal operation.
  68. */
  69. # define TXP_BYTE_ENABLE_SHIFT 16
  70. # define TXP_BYTE_ENABLE_MASK GENMASK(19, 16)
  71. /* Debug: Generate VSTART again at EOF. */
  72. # define TXP_VSTART_AT_EOF BIT(15)
  73. /* Debug: Terminate the current frame immediately. Stops AXI
  74. * writes.
  75. */
  76. # define TXP_ABORT BIT(14)
  77. # define TXP_DITHER BIT(13)
  78. /* Inverts alpha if TXP_ALPHA_ENABLE, chooses fill value for
  79. * !TXP_ALPHA_ENABLE.
  80. */
  81. # define TXP_ALPHA_INVERT BIT(12)
  82. /* Note: I've listed the channels here in high bit (in byte 3/2/1) to
  83. * low bit (in byte 0) order.
  84. */
  85. # define TXP_FORMAT_SHIFT 8
  86. # define TXP_FORMAT_MASK GENMASK(11, 8)
  87. # define TXP_FORMAT_ABGR4444 0
  88. # define TXP_FORMAT_ARGB4444 1
  89. # define TXP_FORMAT_BGRA4444 2
  90. # define TXP_FORMAT_RGBA4444 3
  91. # define TXP_FORMAT_BGR565 6
  92. # define TXP_FORMAT_RGB565 7
  93. /* 888s are non-rotated, raster-only */
  94. # define TXP_FORMAT_BGR888 8
  95. # define TXP_FORMAT_RGB888 9
  96. # define TXP_FORMAT_ABGR8888 12
  97. # define TXP_FORMAT_ARGB8888 13
  98. # define TXP_FORMAT_BGRA8888 14
  99. # define TXP_FORMAT_RGBA8888 15
  100. /* If TFORMAT is set, generates LT instead of T format. */
  101. # define TXP_LINEAR_UTILE BIT(7)
  102. /* Rotate output by 90 degrees. */
  103. # define TXP_TRANSPOSE BIT(6)
  104. /* Generate a tiled format for V3D. */
  105. # define TXP_TFORMAT BIT(5)
  106. /* Generates some undefined test mode output. */
  107. # define TXP_TEST_MODE BIT(4)
  108. /* Request odd field from HVS. */
  109. # define TXP_FIELD BIT(3)
  110. /* Raise interrupt when idle. */
  111. # define TXP_EI BIT(2)
  112. /* Set when generating a frame, clears when idle. */
  113. # define TXP_BUSY BIT(1)
  114. /* Starts a frame. Self-clearing. */
  115. # define TXP_GO BIT(0)
  116. /* Number of lines received and committed to memory. */
  117. #define TXP_PROGRESS 0x10
  118. #define TXP_READ(offset) readl(txp->regs + (offset))
  119. #define TXP_WRITE(offset, val) writel(val, txp->regs + (offset))
  120. struct vc4_txp {
  121. struct platform_device *pdev;
  122. struct drm_writeback_connector connector;
  123. void __iomem *regs;
  124. };
  125. static inline struct vc4_txp *encoder_to_vc4_txp(struct drm_encoder *encoder)
  126. {
  127. return container_of(encoder, struct vc4_txp, connector.encoder);
  128. }
  129. static inline struct vc4_txp *connector_to_vc4_txp(struct drm_connector *conn)
  130. {
  131. return container_of(conn, struct vc4_txp, connector.base);
  132. }
  133. #define TXP_REG(reg) { reg, #reg }
  134. static const struct {
  135. u32 reg;
  136. const char *name;
  137. } txp_regs[] = {
  138. TXP_REG(TXP_DST_PTR),
  139. TXP_REG(TXP_DST_PITCH),
  140. TXP_REG(TXP_DIM),
  141. TXP_REG(TXP_DST_CTRL),
  142. TXP_REG(TXP_PROGRESS),
  143. };
  144. #ifdef CONFIG_DEBUG_FS
  145. int vc4_txp_debugfs_regs(struct seq_file *m, void *unused)
  146. {
  147. struct drm_info_node *node = (struct drm_info_node *)m->private;
  148. struct drm_device *dev = node->minor->dev;
  149. struct vc4_dev *vc4 = to_vc4_dev(dev);
  150. struct vc4_txp *txp = vc4->txp;
  151. int i;
  152. if (!txp)
  153. return 0;
  154. for (i = 0; i < ARRAY_SIZE(txp_regs); i++) {
  155. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  156. txp_regs[i].name, txp_regs[i].reg,
  157. TXP_READ(txp_regs[i].reg));
  158. }
  159. return 0;
  160. }
  161. #endif
  162. static int vc4_txp_connector_get_modes(struct drm_connector *connector)
  163. {
  164. struct drm_device *dev = connector->dev;
  165. return drm_add_modes_noedid(connector, dev->mode_config.max_width,
  166. dev->mode_config.max_height);
  167. }
  168. static enum drm_mode_status
  169. vc4_txp_connector_mode_valid(struct drm_connector *connector,
  170. struct drm_display_mode *mode)
  171. {
  172. struct drm_device *dev = connector->dev;
  173. struct drm_mode_config *mode_config = &dev->mode_config;
  174. int w = mode->hdisplay, h = mode->vdisplay;
  175. if (w < mode_config->min_width || w > mode_config->max_width)
  176. return MODE_BAD_HVALUE;
  177. if (h < mode_config->min_height || h > mode_config->max_height)
  178. return MODE_BAD_VVALUE;
  179. return MODE_OK;
  180. }
  181. static const u32 drm_fmts[] = {
  182. DRM_FORMAT_RGB888,
  183. DRM_FORMAT_BGR888,
  184. DRM_FORMAT_XRGB8888,
  185. DRM_FORMAT_XBGR8888,
  186. DRM_FORMAT_ARGB8888,
  187. DRM_FORMAT_ABGR8888,
  188. DRM_FORMAT_RGBX8888,
  189. DRM_FORMAT_BGRX8888,
  190. DRM_FORMAT_RGBA8888,
  191. DRM_FORMAT_BGRA8888,
  192. };
  193. static const u32 txp_fmts[] = {
  194. TXP_FORMAT_RGB888,
  195. TXP_FORMAT_BGR888,
  196. TXP_FORMAT_ARGB8888,
  197. TXP_FORMAT_ABGR8888,
  198. TXP_FORMAT_ARGB8888,
  199. TXP_FORMAT_ABGR8888,
  200. TXP_FORMAT_RGBA8888,
  201. TXP_FORMAT_BGRA8888,
  202. TXP_FORMAT_RGBA8888,
  203. TXP_FORMAT_BGRA8888,
  204. };
  205. static int vc4_txp_connector_atomic_check(struct drm_connector *conn,
  206. struct drm_connector_state *conn_state)
  207. {
  208. struct drm_crtc_state *crtc_state;
  209. struct drm_gem_cma_object *gem;
  210. struct drm_framebuffer *fb;
  211. int i;
  212. if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
  213. return 0;
  214. crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
  215. conn_state->crtc);
  216. fb = conn_state->writeback_job->fb;
  217. if (fb->width != crtc_state->mode.hdisplay ||
  218. fb->height != crtc_state->mode.vdisplay) {
  219. DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
  220. fb->width, fb->height);
  221. return -EINVAL;
  222. }
  223. for (i = 0; i < ARRAY_SIZE(drm_fmts); i++) {
  224. if (fb->format->format == drm_fmts[i])
  225. break;
  226. }
  227. if (i == ARRAY_SIZE(drm_fmts))
  228. return -EINVAL;
  229. gem = drm_fb_cma_get_gem_obj(fb, 0);
  230. /* Pitch must be aligned on 16 bytes. */
  231. if (fb->pitches[0] & GENMASK(3, 0))
  232. return -EINVAL;
  233. vc4_crtc_txp_armed(crtc_state);
  234. return 0;
  235. }
  236. static void vc4_txp_connector_atomic_commit(struct drm_connector *conn,
  237. struct drm_connector_state *conn_state)
  238. {
  239. struct vc4_txp *txp = connector_to_vc4_txp(conn);
  240. struct drm_gem_cma_object *gem;
  241. struct drm_display_mode *mode;
  242. struct drm_framebuffer *fb;
  243. u32 ctrl;
  244. int i;
  245. if (WARN_ON(!conn_state->writeback_job ||
  246. !conn_state->writeback_job->fb))
  247. return;
  248. mode = &conn_state->crtc->state->adjusted_mode;
  249. fb = conn_state->writeback_job->fb;
  250. for (i = 0; i < ARRAY_SIZE(drm_fmts); i++) {
  251. if (fb->format->format == drm_fmts[i])
  252. break;
  253. }
  254. if (WARN_ON(i == ARRAY_SIZE(drm_fmts)))
  255. return;
  256. ctrl = TXP_GO | TXP_VSTART_AT_EOF | TXP_EI |
  257. VC4_SET_FIELD(0xf, TXP_BYTE_ENABLE) |
  258. VC4_SET_FIELD(txp_fmts[i], TXP_FORMAT);
  259. if (fb->format->has_alpha)
  260. ctrl |= TXP_ALPHA_ENABLE;
  261. gem = drm_fb_cma_get_gem_obj(fb, 0);
  262. TXP_WRITE(TXP_DST_PTR, gem->paddr + fb->offsets[0]);
  263. TXP_WRITE(TXP_DST_PITCH, fb->pitches[0]);
  264. TXP_WRITE(TXP_DIM,
  265. VC4_SET_FIELD(mode->hdisplay, TXP_WIDTH) |
  266. VC4_SET_FIELD(mode->vdisplay, TXP_HEIGHT));
  267. TXP_WRITE(TXP_DST_CTRL, ctrl);
  268. drm_writeback_queue_job(&txp->connector, conn_state->writeback_job);
  269. }
  270. static const struct drm_connector_helper_funcs vc4_txp_connector_helper_funcs = {
  271. .get_modes = vc4_txp_connector_get_modes,
  272. .mode_valid = vc4_txp_connector_mode_valid,
  273. .atomic_check = vc4_txp_connector_atomic_check,
  274. .atomic_commit = vc4_txp_connector_atomic_commit,
  275. };
  276. static enum drm_connector_status
  277. vc4_txp_connector_detect(struct drm_connector *connector, bool force)
  278. {
  279. return connector_status_connected;
  280. }
  281. static void vc4_txp_connector_destroy(struct drm_connector *connector)
  282. {
  283. drm_connector_unregister(connector);
  284. drm_connector_cleanup(connector);
  285. }
  286. static const struct drm_connector_funcs vc4_txp_connector_funcs = {
  287. .detect = vc4_txp_connector_detect,
  288. .fill_modes = drm_helper_probe_single_connector_modes,
  289. .destroy = vc4_txp_connector_destroy,
  290. .reset = drm_atomic_helper_connector_reset,
  291. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  292. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  293. };
  294. static void vc4_txp_encoder_disable(struct drm_encoder *encoder)
  295. {
  296. struct vc4_txp *txp = encoder_to_vc4_txp(encoder);
  297. if (TXP_READ(TXP_DST_CTRL) & TXP_BUSY) {
  298. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  299. TXP_WRITE(TXP_DST_CTRL, TXP_ABORT);
  300. while (TXP_READ(TXP_DST_CTRL) & TXP_BUSY &&
  301. time_before(jiffies, timeout))
  302. ;
  303. WARN_ON(TXP_READ(TXP_DST_CTRL) & TXP_BUSY);
  304. }
  305. TXP_WRITE(TXP_DST_CTRL, TXP_POWERDOWN);
  306. }
  307. static const struct drm_encoder_helper_funcs vc4_txp_encoder_helper_funcs = {
  308. .disable = vc4_txp_encoder_disable,
  309. };
  310. static irqreturn_t vc4_txp_interrupt(int irq, void *data)
  311. {
  312. struct vc4_txp *txp = data;
  313. TXP_WRITE(TXP_DST_CTRL, TXP_READ(TXP_DST_CTRL) & ~TXP_EI);
  314. vc4_crtc_handle_vblank(to_vc4_crtc(txp->connector.base.state->crtc));
  315. drm_writeback_signal_completion(&txp->connector, 0);
  316. return IRQ_HANDLED;
  317. }
  318. static int vc4_txp_bind(struct device *dev, struct device *master, void *data)
  319. {
  320. struct platform_device *pdev = to_platform_device(dev);
  321. struct drm_device *drm = dev_get_drvdata(master);
  322. struct vc4_dev *vc4 = to_vc4_dev(drm);
  323. struct vc4_txp *txp;
  324. int ret, irq;
  325. irq = platform_get_irq(pdev, 0);
  326. if (irq < 0)
  327. return irq;
  328. txp = devm_kzalloc(dev, sizeof(*txp), GFP_KERNEL);
  329. if (!txp)
  330. return -ENOMEM;
  331. txp->pdev = pdev;
  332. txp->regs = vc4_ioremap_regs(pdev, 0);
  333. if (IS_ERR(txp->regs))
  334. return PTR_ERR(txp->regs);
  335. drm_connector_helper_add(&txp->connector.base,
  336. &vc4_txp_connector_helper_funcs);
  337. ret = drm_writeback_connector_init(drm, &txp->connector,
  338. &vc4_txp_connector_funcs,
  339. &vc4_txp_encoder_helper_funcs,
  340. drm_fmts, ARRAY_SIZE(drm_fmts));
  341. if (ret)
  342. return ret;
  343. ret = devm_request_irq(dev, irq, vc4_txp_interrupt, 0,
  344. dev_name(dev), txp);
  345. if (ret)
  346. return ret;
  347. dev_set_drvdata(dev, txp);
  348. vc4->txp = txp;
  349. return 0;
  350. }
  351. static void vc4_txp_unbind(struct device *dev, struct device *master,
  352. void *data)
  353. {
  354. struct drm_device *drm = dev_get_drvdata(master);
  355. struct vc4_dev *vc4 = to_vc4_dev(drm);
  356. struct vc4_txp *txp = dev_get_drvdata(dev);
  357. vc4_txp_connector_destroy(&txp->connector.base);
  358. vc4->txp = NULL;
  359. }
  360. static const struct component_ops vc4_txp_ops = {
  361. .bind = vc4_txp_bind,
  362. .unbind = vc4_txp_unbind,
  363. };
  364. static int vc4_txp_probe(struct platform_device *pdev)
  365. {
  366. return component_add(&pdev->dev, &vc4_txp_ops);
  367. }
  368. static int vc4_txp_remove(struct platform_device *pdev)
  369. {
  370. component_del(&pdev->dev, &vc4_txp_ops);
  371. return 0;
  372. }
  373. static const struct of_device_id vc4_txp_dt_match[] = {
  374. { .compatible = "brcm,bcm2835-txp" },
  375. { /* sentinel */ },
  376. };
  377. struct platform_driver vc4_txp_driver = {
  378. .probe = vc4_txp_probe,
  379. .remove = vc4_txp_remove,
  380. .driver = {
  381. .name = "vc4_txp",
  382. .of_match_table = vc4_txp_dt_match,
  383. },
  384. };