vsp1_drm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * vsp1_drm.c -- R-Car VSP1 DRM API
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <media/media-entity.h>
  16. #include <media/v4l2-subdev.h>
  17. #include <media/vsp1.h>
  18. #include "vsp1.h"
  19. #include "vsp1_bru.h"
  20. #include "vsp1_dl.h"
  21. #include "vsp1_drm.h"
  22. #include "vsp1_lif.h"
  23. #include "vsp1_pipe.h"
  24. #include "vsp1_rwpf.h"
  25. /* -----------------------------------------------------------------------------
  26. * Interrupt Handling
  27. */
  28. void vsp1_drm_display_start(struct vsp1_device *vsp1)
  29. {
  30. vsp1_dlm_irq_display_start(vsp1->drm->pipe.output->dlm);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * DU Driver API
  34. */
  35. int vsp1_du_init(struct device *dev)
  36. {
  37. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  38. if (!vsp1)
  39. return -EPROBE_DEFER;
  40. return 0;
  41. }
  42. EXPORT_SYMBOL_GPL(vsp1_du_init);
  43. /**
  44. * vsp1_du_setup_lif - Setup the output part of the VSP pipeline
  45. * @dev: the VSP device
  46. * @width: output frame width in pixels
  47. * @height: output frame height in pixels
  48. *
  49. * Configure the output part of VSP DRM pipeline for the given frame @width and
  50. * @height. This sets up formats on the BRU source pad, the WPF0 sink and source
  51. * pads, and the LIF sink pad.
  52. *
  53. * As the media bus code on the BRU source pad is conditioned by the
  54. * configuration of the BRU sink 0 pad, we also set up the formats on all BRU
  55. * sinks, even if the configuration will be overwritten later by
  56. * vsp1_du_setup_rpf(). This ensures that the BRU configuration is set to a well
  57. * defined state.
  58. *
  59. * Return 0 on success or a negative error code on failure.
  60. */
  61. int vsp1_du_setup_lif(struct device *dev, unsigned int width,
  62. unsigned int height)
  63. {
  64. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  65. struct vsp1_pipeline *pipe = &vsp1->drm->pipe;
  66. struct vsp1_bru *bru = vsp1->bru;
  67. struct v4l2_subdev_format format;
  68. unsigned int i;
  69. int ret;
  70. dev_dbg(vsp1->dev, "%s: configuring LIF with format %ux%u\n",
  71. __func__, width, height);
  72. if (width == 0 || height == 0) {
  73. /* Zero width or height means the CRTC is being disabled, stop
  74. * the pipeline and turn the light off.
  75. */
  76. ret = vsp1_pipeline_stop(pipe);
  77. if (ret == -ETIMEDOUT)
  78. dev_err(vsp1->dev, "DRM pipeline stop timeout\n");
  79. media_entity_pipeline_stop(&pipe->output->entity.subdev.entity);
  80. for (i = 0; i < bru->entity.source_pad; ++i) {
  81. bru->inputs[i].rpf = NULL;
  82. pipe->inputs[i] = NULL;
  83. }
  84. pipe->num_inputs = 0;
  85. vsp1_dlm_reset(pipe->output->dlm);
  86. vsp1_device_put(vsp1);
  87. dev_dbg(vsp1->dev, "%s: pipeline disabled\n", __func__);
  88. return 0;
  89. }
  90. /* Configure the format at the BRU sinks and propagate it through the
  91. * pipeline.
  92. */
  93. memset(&format, 0, sizeof(format));
  94. format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  95. for (i = 0; i < bru->entity.source_pad; ++i) {
  96. format.pad = i;
  97. format.format.width = width;
  98. format.format.height = height;
  99. format.format.code = MEDIA_BUS_FMT_ARGB8888_1X32;
  100. format.format.field = V4L2_FIELD_NONE;
  101. ret = v4l2_subdev_call(&bru->entity.subdev, pad,
  102. set_fmt, NULL, &format);
  103. if (ret < 0)
  104. return ret;
  105. dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on BRU pad %u\n",
  106. __func__, format.format.width, format.format.height,
  107. format.format.code, i);
  108. }
  109. format.pad = bru->entity.source_pad;
  110. format.format.width = width;
  111. format.format.height = height;
  112. format.format.code = MEDIA_BUS_FMT_ARGB8888_1X32;
  113. format.format.field = V4L2_FIELD_NONE;
  114. ret = v4l2_subdev_call(&bru->entity.subdev, pad, set_fmt, NULL,
  115. &format);
  116. if (ret < 0)
  117. return ret;
  118. dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on BRU pad %u\n",
  119. __func__, format.format.width, format.format.height,
  120. format.format.code, i);
  121. format.pad = RWPF_PAD_SINK;
  122. ret = v4l2_subdev_call(&vsp1->wpf[0]->entity.subdev, pad, set_fmt, NULL,
  123. &format);
  124. if (ret < 0)
  125. return ret;
  126. dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on WPF0 sink\n",
  127. __func__, format.format.width, format.format.height,
  128. format.format.code);
  129. format.pad = RWPF_PAD_SOURCE;
  130. ret = v4l2_subdev_call(&vsp1->wpf[0]->entity.subdev, pad, get_fmt, NULL,
  131. &format);
  132. if (ret < 0)
  133. return ret;
  134. dev_dbg(vsp1->dev, "%s: got format %ux%u (%x) on WPF0 source\n",
  135. __func__, format.format.width, format.format.height,
  136. format.format.code);
  137. format.pad = LIF_PAD_SINK;
  138. ret = v4l2_subdev_call(&vsp1->lif->entity.subdev, pad, set_fmt, NULL,
  139. &format);
  140. if (ret < 0)
  141. return ret;
  142. dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on LIF sink\n",
  143. __func__, format.format.width, format.format.height,
  144. format.format.code);
  145. /* Verify that the format at the output of the pipeline matches the
  146. * requested frame size and media bus code.
  147. */
  148. if (format.format.width != width || format.format.height != height ||
  149. format.format.code != MEDIA_BUS_FMT_ARGB8888_1X32) {
  150. dev_dbg(vsp1->dev, "%s: format mismatch\n", __func__);
  151. return -EPIPE;
  152. }
  153. /* Mark the pipeline as streaming and enable the VSP1. This will store
  154. * the pipeline pointer in all entities, which the s_stream handlers
  155. * will need. We don't start the entities themselves right at this point
  156. * as there's no plane configured yet, so we can't start processing
  157. * buffers.
  158. */
  159. ret = vsp1_device_get(vsp1);
  160. if (ret < 0)
  161. return ret;
  162. ret = media_entity_pipeline_start(&pipe->output->entity.subdev.entity,
  163. &pipe->pipe);
  164. if (ret < 0) {
  165. dev_dbg(vsp1->dev, "%s: pipeline start failed\n", __func__);
  166. vsp1_device_put(vsp1);
  167. return ret;
  168. }
  169. dev_dbg(vsp1->dev, "%s: pipeline enabled\n", __func__);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL_GPL(vsp1_du_setup_lif);
  173. /**
  174. * vsp1_du_atomic_begin - Prepare for an atomic update
  175. * @dev: the VSP device
  176. */
  177. void vsp1_du_atomic_begin(struct device *dev)
  178. {
  179. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  180. struct vsp1_pipeline *pipe = &vsp1->drm->pipe;
  181. unsigned long flags;
  182. spin_lock_irqsave(&pipe->irqlock, flags);
  183. vsp1->drm->num_inputs = pipe->num_inputs;
  184. spin_unlock_irqrestore(&pipe->irqlock, flags);
  185. /* Prepare the display list. */
  186. pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
  187. }
  188. EXPORT_SYMBOL_GPL(vsp1_du_atomic_begin);
  189. /**
  190. * vsp1_du_atomic_update - Setup one RPF input of the VSP pipeline
  191. * @dev: the VSP device
  192. * @rpf_index: index of the RPF to setup (0-based)
  193. * @pixelformat: V4L2 pixel format for the RPF memory input
  194. * @pitch: number of bytes per line in the image stored in memory
  195. * @mem: DMA addresses of the memory buffers (one per plane)
  196. * @src: the source crop rectangle for the RPF
  197. * @dst: the destination compose rectangle for the BRU input
  198. *
  199. * Configure the VSP to perform composition of the image referenced by @mem
  200. * through RPF @rpf_index, using the @src crop rectangle and the @dst
  201. * composition rectangle. The Z-order is fixed with RPF 0 at the bottom.
  202. *
  203. * Image format as stored in memory is expressed as a V4L2 @pixelformat value.
  204. * As a special case, setting the pixel format to 0 will disable the RPF. The
  205. * @pitch, @mem, @src and @dst parameters are ignored in that case. Calling the
  206. * function on a disabled RPF is allowed.
  207. *
  208. * The memory pitch is configurable to allow for padding at end of lines, or
  209. * simple for images that extend beyond the crop rectangle boundaries. The
  210. * @pitch value is expressed in bytes and applies to all planes for multiplanar
  211. * formats.
  212. *
  213. * The source memory buffer is referenced by the DMA address of its planes in
  214. * the @mem array. Up to two planes are supported. The second plane DMA address
  215. * is ignored for formats using a single plane.
  216. *
  217. * This function isn't reentrant, the caller needs to serialize calls.
  218. *
  219. * TODO: Implement Z-order control by decoupling the RPF index from the BRU
  220. * input index.
  221. *
  222. * Return 0 on success or a negative error code on failure.
  223. */
  224. int vsp1_du_atomic_update(struct device *dev, unsigned int rpf_index,
  225. u32 pixelformat, unsigned int pitch,
  226. dma_addr_t mem[2], const struct v4l2_rect *src,
  227. const struct v4l2_rect *dst)
  228. {
  229. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  230. struct vsp1_pipeline *pipe = &vsp1->drm->pipe;
  231. const struct vsp1_format_info *fmtinfo;
  232. struct v4l2_subdev_selection sel;
  233. struct v4l2_subdev_format format;
  234. struct vsp1_rwpf *rpf;
  235. unsigned long flags;
  236. int ret;
  237. if (rpf_index >= vsp1->info->rpf_count)
  238. return -EINVAL;
  239. rpf = vsp1->rpf[rpf_index];
  240. if (pixelformat == 0) {
  241. dev_dbg(vsp1->dev, "%s: RPF%u: disable requested\n", __func__,
  242. rpf_index);
  243. spin_lock_irqsave(&pipe->irqlock, flags);
  244. if (pipe->inputs[rpf_index]) {
  245. /* Remove the RPF from the pipeline if it was previously
  246. * enabled.
  247. */
  248. vsp1->bru->inputs[rpf_index].rpf = NULL;
  249. pipe->inputs[rpf_index] = NULL;
  250. pipe->num_inputs--;
  251. }
  252. spin_unlock_irqrestore(&pipe->irqlock, flags);
  253. return 0;
  254. }
  255. dev_dbg(vsp1->dev,
  256. "%s: RPF%u: (%u,%u)/%ux%u -> (%u,%u)/%ux%u (%08x), pitch %u dma { %pad, %pad }\n",
  257. __func__, rpf_index,
  258. src->left, src->top, src->width, src->height,
  259. dst->left, dst->top, dst->width, dst->height,
  260. pixelformat, pitch, &mem[0], &mem[1]);
  261. /* Set the stride at the RPF input. */
  262. fmtinfo = vsp1_get_format_info(pixelformat);
  263. if (!fmtinfo) {
  264. dev_dbg(vsp1->dev, "Unsupport pixel format %08x for RPF\n",
  265. pixelformat);
  266. return -EINVAL;
  267. }
  268. rpf->fmtinfo = fmtinfo;
  269. rpf->format.num_planes = fmtinfo->planes;
  270. rpf->format.plane_fmt[0].bytesperline = pitch;
  271. rpf->format.plane_fmt[1].bytesperline = pitch;
  272. /* Configure the format on the RPF sink pad and propagate it up to the
  273. * BRU sink pad.
  274. */
  275. memset(&format, 0, sizeof(format));
  276. format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  277. format.pad = RWPF_PAD_SINK;
  278. format.format.width = src->width + src->left;
  279. format.format.height = src->height + src->top;
  280. format.format.code = fmtinfo->mbus;
  281. format.format.field = V4L2_FIELD_NONE;
  282. ret = v4l2_subdev_call(&rpf->entity.subdev, pad, set_fmt, NULL,
  283. &format);
  284. if (ret < 0)
  285. return ret;
  286. dev_dbg(vsp1->dev,
  287. "%s: set format %ux%u (%x) on RPF%u sink\n",
  288. __func__, format.format.width, format.format.height,
  289. format.format.code, rpf->entity.index);
  290. memset(&sel, 0, sizeof(sel));
  291. sel.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  292. sel.pad = RWPF_PAD_SINK;
  293. sel.target = V4L2_SEL_TGT_CROP;
  294. sel.r = *src;
  295. ret = v4l2_subdev_call(&rpf->entity.subdev, pad, set_selection, NULL,
  296. &sel);
  297. if (ret < 0)
  298. return ret;
  299. dev_dbg(vsp1->dev,
  300. "%s: set selection (%u,%u)/%ux%u on RPF%u sink\n",
  301. __func__, sel.r.left, sel.r.top, sel.r.width, sel.r.height,
  302. rpf->entity.index);
  303. /* RPF source, hardcode the format to ARGB8888 to turn on format
  304. * conversion if needed.
  305. */
  306. format.pad = RWPF_PAD_SOURCE;
  307. ret = v4l2_subdev_call(&rpf->entity.subdev, pad, get_fmt, NULL,
  308. &format);
  309. if (ret < 0)
  310. return ret;
  311. dev_dbg(vsp1->dev,
  312. "%s: got format %ux%u (%x) on RPF%u source\n",
  313. __func__, format.format.width, format.format.height,
  314. format.format.code, rpf->entity.index);
  315. format.format.code = MEDIA_BUS_FMT_ARGB8888_1X32;
  316. ret = v4l2_subdev_call(&rpf->entity.subdev, pad, set_fmt, NULL,
  317. &format);
  318. if (ret < 0)
  319. return ret;
  320. /* BRU sink, propagate the format from the RPF source. */
  321. format.pad = rpf->entity.index;
  322. ret = v4l2_subdev_call(&vsp1->bru->entity.subdev, pad, set_fmt, NULL,
  323. &format);
  324. if (ret < 0)
  325. return ret;
  326. dev_dbg(vsp1->dev, "%s: set format %ux%u (%x) on BRU pad %u\n",
  327. __func__, format.format.width, format.format.height,
  328. format.format.code, format.pad);
  329. sel.pad = rpf->entity.index;
  330. sel.target = V4L2_SEL_TGT_COMPOSE;
  331. sel.r = *dst;
  332. ret = v4l2_subdev_call(&vsp1->bru->entity.subdev, pad, set_selection,
  333. NULL, &sel);
  334. if (ret < 0)
  335. return ret;
  336. dev_dbg(vsp1->dev,
  337. "%s: set selection (%u,%u)/%ux%u on BRU pad %u\n",
  338. __func__, sel.r.left, sel.r.top, sel.r.width, sel.r.height,
  339. sel.pad);
  340. /* Store the BRU input pad number in the RPF. */
  341. rpf->bru_input = rpf->entity.index;
  342. /* Cache the memory buffer address but don't apply the values to the
  343. * hardware as the crop offsets haven't been computed yet.
  344. */
  345. rpf->mem.addr[0] = mem[0];
  346. rpf->mem.addr[1] = mem[1];
  347. rpf->mem.addr[2] = 0;
  348. spin_lock_irqsave(&pipe->irqlock, flags);
  349. /* If the RPF was previously stopped set the BRU input to the RPF and
  350. * store the RPF in the pipeline inputs array.
  351. */
  352. if (!pipe->inputs[rpf->entity.index]) {
  353. vsp1->bru->inputs[rpf_index].rpf = rpf;
  354. pipe->inputs[rpf->entity.index] = rpf;
  355. pipe->num_inputs++;
  356. }
  357. spin_unlock_irqrestore(&pipe->irqlock, flags);
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(vsp1_du_atomic_update);
  361. /**
  362. * vsp1_du_atomic_flush - Commit an atomic update
  363. * @dev: the VSP device
  364. */
  365. void vsp1_du_atomic_flush(struct device *dev)
  366. {
  367. struct vsp1_device *vsp1 = dev_get_drvdata(dev);
  368. struct vsp1_pipeline *pipe = &vsp1->drm->pipe;
  369. struct vsp1_entity *entity;
  370. unsigned long flags;
  371. bool stop = false;
  372. int ret;
  373. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  374. /* Disconnect unused RPFs from the pipeline. */
  375. if (entity->type == VSP1_ENTITY_RPF) {
  376. struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
  377. if (!pipe->inputs[rpf->entity.index]) {
  378. vsp1_mod_write(entity, entity->route->reg,
  379. VI6_DPR_NODE_UNUSED);
  380. continue;
  381. }
  382. }
  383. vsp1_entity_route_setup(entity);
  384. ret = v4l2_subdev_call(&entity->subdev, video,
  385. s_stream, 1);
  386. if (ret < 0) {
  387. dev_err(vsp1->dev,
  388. "DRM pipeline start failure on entity %s\n",
  389. entity->subdev.name);
  390. return;
  391. }
  392. if (entity->type == VSP1_ENTITY_RPF)
  393. vsp1_rwpf_set_memory(to_rwpf(&entity->subdev));
  394. }
  395. vsp1_dl_list_commit(pipe->dl);
  396. pipe->dl = NULL;
  397. /* Start or stop the pipeline if needed. */
  398. spin_lock_irqsave(&pipe->irqlock, flags);
  399. if (!vsp1->drm->num_inputs && pipe->num_inputs) {
  400. vsp1_write(vsp1, VI6_DISP_IRQ_STA, 0);
  401. vsp1_write(vsp1, VI6_DISP_IRQ_ENB, VI6_DISP_IRQ_ENB_DSTE);
  402. vsp1_pipeline_run(pipe);
  403. } else if (vsp1->drm->num_inputs && !pipe->num_inputs) {
  404. stop = true;
  405. }
  406. spin_unlock_irqrestore(&pipe->irqlock, flags);
  407. if (stop) {
  408. vsp1_write(vsp1, VI6_DISP_IRQ_ENB, 0);
  409. vsp1_pipeline_stop(pipe);
  410. }
  411. }
  412. EXPORT_SYMBOL_GPL(vsp1_du_atomic_flush);
  413. /* -----------------------------------------------------------------------------
  414. * Initialization
  415. */
  416. int vsp1_drm_create_links(struct vsp1_device *vsp1)
  417. {
  418. const u32 flags = MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
  419. unsigned int i;
  420. int ret;
  421. /* VSPD instances require a BRU to perform composition and a LIF to
  422. * output to the DU.
  423. */
  424. if (!vsp1->bru || !vsp1->lif)
  425. return -ENXIO;
  426. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  427. struct vsp1_rwpf *rpf = vsp1->rpf[i];
  428. ret = media_create_pad_link(&rpf->entity.subdev.entity,
  429. RWPF_PAD_SOURCE,
  430. &vsp1->bru->entity.subdev.entity,
  431. i, flags);
  432. if (ret < 0)
  433. return ret;
  434. rpf->entity.sink = &vsp1->bru->entity.subdev.entity;
  435. rpf->entity.sink_pad = i;
  436. }
  437. ret = media_create_pad_link(&vsp1->bru->entity.subdev.entity,
  438. vsp1->bru->entity.source_pad,
  439. &vsp1->wpf[0]->entity.subdev.entity,
  440. RWPF_PAD_SINK, flags);
  441. if (ret < 0)
  442. return ret;
  443. vsp1->bru->entity.sink = &vsp1->wpf[0]->entity.subdev.entity;
  444. vsp1->bru->entity.sink_pad = RWPF_PAD_SINK;
  445. ret = media_create_pad_link(&vsp1->wpf[0]->entity.subdev.entity,
  446. RWPF_PAD_SOURCE,
  447. &vsp1->lif->entity.subdev.entity,
  448. LIF_PAD_SINK, flags);
  449. if (ret < 0)
  450. return ret;
  451. return 0;
  452. }
  453. int vsp1_drm_init(struct vsp1_device *vsp1)
  454. {
  455. struct vsp1_pipeline *pipe;
  456. unsigned int i;
  457. vsp1->drm = devm_kzalloc(vsp1->dev, sizeof(*vsp1->drm), GFP_KERNEL);
  458. if (!vsp1->drm)
  459. return -ENOMEM;
  460. pipe = &vsp1->drm->pipe;
  461. vsp1_pipeline_init(pipe);
  462. /* The DRM pipeline is static, add entities manually. */
  463. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  464. struct vsp1_rwpf *input = vsp1->rpf[i];
  465. list_add_tail(&input->entity.list_pipe, &pipe->entities);
  466. }
  467. list_add_tail(&vsp1->bru->entity.list_pipe, &pipe->entities);
  468. list_add_tail(&vsp1->wpf[0]->entity.list_pipe, &pipe->entities);
  469. list_add_tail(&vsp1->lif->entity.list_pipe, &pipe->entities);
  470. pipe->bru = &vsp1->bru->entity;
  471. pipe->lif = &vsp1->lif->entity;
  472. pipe->output = vsp1->wpf[0];
  473. return 0;
  474. }
  475. void vsp1_drm_cleanup(struct vsp1_device *vsp1)
  476. {
  477. }