vsp1_entity.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * vsp1_entity.c -- R-Car VSP1 Base Entity
  3. *
  4. * Copyright (C) 2013-2014 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/gfp.h>
  15. #include <media/media-entity.h>
  16. #include <media/v4l2-ctrls.h>
  17. #include <media/v4l2-subdev.h>
  18. #include "vsp1.h"
  19. #include "vsp1_dl.h"
  20. #include "vsp1_entity.h"
  21. #include "vsp1_pipe.h"
  22. #include "vsp1_rwpf.h"
  23. static inline struct vsp1_entity *
  24. media_entity_to_vsp1_entity(struct media_entity *entity)
  25. {
  26. return container_of(entity, struct vsp1_entity, subdev.entity);
  27. }
  28. void vsp1_entity_route_setup(struct vsp1_entity *entity,
  29. struct vsp1_pipeline *pipe,
  30. struct vsp1_dl_list *dl)
  31. {
  32. struct vsp1_entity *source;
  33. struct vsp1_entity *sink;
  34. if (entity->type == VSP1_ENTITY_HGO) {
  35. u32 smppt;
  36. /*
  37. * The HGO is a special case, its routing is configured on the
  38. * sink pad.
  39. */
  40. source = media_entity_to_vsp1_entity(entity->sources[0]);
  41. smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
  42. | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
  43. vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt);
  44. return;
  45. } else if (entity->type == VSP1_ENTITY_HGT) {
  46. u32 smppt;
  47. /*
  48. * The HGT is a special case, its routing is configured on the
  49. * sink pad.
  50. */
  51. source = media_entity_to_vsp1_entity(entity->sources[0]);
  52. smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
  53. | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
  54. vsp1_dl_list_write(dl, VI6_DPR_HGT_SMPPT, smppt);
  55. return;
  56. }
  57. source = entity;
  58. if (source->route->reg == 0)
  59. return;
  60. sink = media_entity_to_vsp1_entity(source->sink);
  61. vsp1_dl_list_write(dl, source->route->reg,
  62. sink->route->inputs[source->sink_pad]);
  63. }
  64. /* -----------------------------------------------------------------------------
  65. * V4L2 Subdevice Operations
  66. */
  67. /**
  68. * vsp1_entity_get_pad_config - Get the pad configuration for an entity
  69. * @entity: the entity
  70. * @cfg: the TRY pad configuration
  71. * @which: configuration selector (ACTIVE or TRY)
  72. *
  73. * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
  74. * the entity lock to access the returned configuration.
  75. *
  76. * Return the pad configuration requested by the which argument. The TRY
  77. * configuration is passed explicitly to the function through the cfg argument
  78. * and simply returned when requested. The ACTIVE configuration comes from the
  79. * entity structure.
  80. */
  81. struct v4l2_subdev_pad_config *
  82. vsp1_entity_get_pad_config(struct vsp1_entity *entity,
  83. struct v4l2_subdev_pad_config *cfg,
  84. enum v4l2_subdev_format_whence which)
  85. {
  86. switch (which) {
  87. case V4L2_SUBDEV_FORMAT_ACTIVE:
  88. return entity->config;
  89. case V4L2_SUBDEV_FORMAT_TRY:
  90. default:
  91. return cfg;
  92. }
  93. }
  94. /**
  95. * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
  96. * @entity: the entity
  97. * @cfg: the configuration storage
  98. * @pad: the pad number
  99. *
  100. * Return the format stored in the given configuration for an entity's pad. The
  101. * configuration can be an ACTIVE or TRY configuration.
  102. */
  103. struct v4l2_mbus_framefmt *
  104. vsp1_entity_get_pad_format(struct vsp1_entity *entity,
  105. struct v4l2_subdev_pad_config *cfg,
  106. unsigned int pad)
  107. {
  108. return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
  109. }
  110. /**
  111. * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
  112. * @entity: the entity
  113. * @cfg: the configuration storage
  114. * @pad: the pad number
  115. * @target: the selection target
  116. *
  117. * Return the selection rectangle stored in the given configuration for an
  118. * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
  119. * selection target can be COMPOSE or CROP.
  120. */
  121. struct v4l2_rect *
  122. vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
  123. struct v4l2_subdev_pad_config *cfg,
  124. unsigned int pad, unsigned int target)
  125. {
  126. switch (target) {
  127. case V4L2_SEL_TGT_COMPOSE:
  128. return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
  129. case V4L2_SEL_TGT_CROP:
  130. return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
  131. default:
  132. return NULL;
  133. }
  134. }
  135. /*
  136. * vsp1_entity_init_cfg - Initialize formats on all pads
  137. * @subdev: V4L2 subdevice
  138. * @cfg: V4L2 subdev pad configuration
  139. *
  140. * Initialize all pad formats with default values in the given pad config. This
  141. * function can be used as a handler for the subdev pad::init_cfg operation.
  142. */
  143. int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
  144. struct v4l2_subdev_pad_config *cfg)
  145. {
  146. struct v4l2_subdev_format format;
  147. unsigned int pad;
  148. for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
  149. memset(&format, 0, sizeof(format));
  150. format.pad = pad;
  151. format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
  152. : V4L2_SUBDEV_FORMAT_ACTIVE;
  153. v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
  154. }
  155. return 0;
  156. }
  157. /*
  158. * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
  159. * @subdev: V4L2 subdevice
  160. * @cfg: V4L2 subdev pad configuration
  161. * @fmt: V4L2 subdev format
  162. *
  163. * This function implements the subdev get_fmt pad operation. It can be used as
  164. * a direct drop-in for the operation handler.
  165. */
  166. int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
  167. struct v4l2_subdev_pad_config *cfg,
  168. struct v4l2_subdev_format *fmt)
  169. {
  170. struct vsp1_entity *entity = to_vsp1_entity(subdev);
  171. struct v4l2_subdev_pad_config *config;
  172. config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
  173. if (!config)
  174. return -EINVAL;
  175. mutex_lock(&entity->lock);
  176. fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
  177. mutex_unlock(&entity->lock);
  178. return 0;
  179. }
  180. /*
  181. * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
  182. * @subdev: V4L2 subdevice
  183. * @cfg: V4L2 subdev pad configuration
  184. * @code: Media bus code enumeration
  185. * @codes: Array of supported media bus codes
  186. * @ncodes: Number of supported media bus codes
  187. *
  188. * This function implements the subdev enum_mbus_code pad operation for entities
  189. * that do not support format conversion. It enumerates the given supported
  190. * media bus codes on the sink pad and reports a source pad format identical to
  191. * the sink pad.
  192. */
  193. int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
  194. struct v4l2_subdev_pad_config *cfg,
  195. struct v4l2_subdev_mbus_code_enum *code,
  196. const unsigned int *codes, unsigned int ncodes)
  197. {
  198. struct vsp1_entity *entity = to_vsp1_entity(subdev);
  199. if (code->pad == 0) {
  200. if (code->index >= ncodes)
  201. return -EINVAL;
  202. code->code = codes[code->index];
  203. } else {
  204. struct v4l2_subdev_pad_config *config;
  205. struct v4l2_mbus_framefmt *format;
  206. /*
  207. * The entity can't perform format conversion, the sink format
  208. * is always identical to the source format.
  209. */
  210. if (code->index)
  211. return -EINVAL;
  212. config = vsp1_entity_get_pad_config(entity, cfg, code->which);
  213. if (!config)
  214. return -EINVAL;
  215. mutex_lock(&entity->lock);
  216. format = vsp1_entity_get_pad_format(entity, config, 0);
  217. code->code = format->code;
  218. mutex_unlock(&entity->lock);
  219. }
  220. return 0;
  221. }
  222. /*
  223. * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
  224. * @subdev: V4L2 subdevice
  225. * @cfg: V4L2 subdev pad configuration
  226. * @fse: Frame size enumeration
  227. * @min_width: Minimum image width
  228. * @min_height: Minimum image height
  229. * @max_width: Maximum image width
  230. * @max_height: Maximum image height
  231. *
  232. * This function implements the subdev enum_frame_size pad operation for
  233. * entities that do not support scaling or cropping. It reports the given
  234. * minimum and maximum frame width and height on the sink pad, and a fixed
  235. * source pad size identical to the sink pad.
  236. */
  237. int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
  238. struct v4l2_subdev_pad_config *cfg,
  239. struct v4l2_subdev_frame_size_enum *fse,
  240. unsigned int min_width, unsigned int min_height,
  241. unsigned int max_width, unsigned int max_height)
  242. {
  243. struct vsp1_entity *entity = to_vsp1_entity(subdev);
  244. struct v4l2_subdev_pad_config *config;
  245. struct v4l2_mbus_framefmt *format;
  246. int ret = 0;
  247. config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
  248. if (!config)
  249. return -EINVAL;
  250. format = vsp1_entity_get_pad_format(entity, config, fse->pad);
  251. mutex_lock(&entity->lock);
  252. if (fse->index || fse->code != format->code) {
  253. ret = -EINVAL;
  254. goto done;
  255. }
  256. if (fse->pad == 0) {
  257. fse->min_width = min_width;
  258. fse->max_width = max_width;
  259. fse->min_height = min_height;
  260. fse->max_height = max_height;
  261. } else {
  262. /*
  263. * The size on the source pad are fixed and always identical to
  264. * the size on the sink pad.
  265. */
  266. fse->min_width = format->width;
  267. fse->max_width = format->width;
  268. fse->min_height = format->height;
  269. fse->max_height = format->height;
  270. }
  271. done:
  272. mutex_unlock(&entity->lock);
  273. return ret;
  274. }
  275. /* -----------------------------------------------------------------------------
  276. * Media Operations
  277. */
  278. static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
  279. const struct media_pad *sink_pad,
  280. u32 flags)
  281. {
  282. struct vsp1_entity *source;
  283. source = media_entity_to_vsp1_entity(source_pad->entity);
  284. if (!source->route)
  285. return 0;
  286. if (flags & MEDIA_LNK_FL_ENABLED) {
  287. struct vsp1_entity *sink
  288. = media_entity_to_vsp1_entity(sink_pad->entity);
  289. /*
  290. * Fan-out is limited to one for the normal data path plus
  291. * optional HGO and HGT. We ignore the HGO and HGT here.
  292. */
  293. if (sink->type != VSP1_ENTITY_HGO &&
  294. sink->type != VSP1_ENTITY_HGT) {
  295. if (source->sink)
  296. return -EBUSY;
  297. source->sink = sink_pad->entity;
  298. source->sink_pad = sink_pad->index;
  299. }
  300. } else {
  301. source->sink = NULL;
  302. source->sink_pad = 0;
  303. }
  304. return 0;
  305. }
  306. static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
  307. const struct media_pad *sink_pad,
  308. u32 flags)
  309. {
  310. struct vsp1_entity *sink;
  311. sink = media_entity_to_vsp1_entity(sink_pad->entity);
  312. if (flags & MEDIA_LNK_FL_ENABLED) {
  313. /* Fan-in is limited to one. */
  314. if (sink->sources[sink_pad->index])
  315. return -EBUSY;
  316. sink->sources[sink_pad->index] = source_pad->entity;
  317. } else {
  318. sink->sources[sink_pad->index] = NULL;
  319. }
  320. return 0;
  321. }
  322. int vsp1_entity_link_setup(struct media_entity *entity,
  323. const struct media_pad *local,
  324. const struct media_pad *remote, u32 flags)
  325. {
  326. if (local->flags & MEDIA_PAD_FL_SOURCE)
  327. return vsp1_entity_link_setup_source(local, remote, flags);
  328. else
  329. return vsp1_entity_link_setup_sink(remote, local, flags);
  330. }
  331. /**
  332. * vsp1_entity_remote_pad - Find the pad at the remote end of a link
  333. * @pad: Pad at the local end of the link
  334. *
  335. * Search for a remote pad connected to the given pad by iterating over all
  336. * links originating or terminating at that pad until an enabled link is found.
  337. *
  338. * Our link setup implementation guarantees that the output fan-out will not be
  339. * higher than one for the data pipelines, except for the links to the HGO and
  340. * HGT that can be enabled in addition to a regular data link. When traversing
  341. * outgoing links this function ignores HGO and HGT entities and should thus be
  342. * used in place of the generic media_entity_remote_pad() function to traverse
  343. * data pipelines.
  344. *
  345. * Return a pointer to the pad at the remote end of the first found enabled
  346. * link, or NULL if no enabled link has been found.
  347. */
  348. struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
  349. {
  350. struct media_link *link;
  351. list_for_each_entry(link, &pad->entity->links, list) {
  352. struct vsp1_entity *entity;
  353. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  354. continue;
  355. /* If we're the sink the source will never be an HGO or HGT. */
  356. if (link->sink == pad)
  357. return link->source;
  358. if (link->source != pad)
  359. continue;
  360. /* If the sink isn't a subdevice it can't be an HGO or HGT. */
  361. if (!is_media_entity_v4l2_subdev(link->sink->entity))
  362. return link->sink;
  363. entity = media_entity_to_vsp1_entity(link->sink->entity);
  364. if (entity->type != VSP1_ENTITY_HGO &&
  365. entity->type != VSP1_ENTITY_HGT)
  366. return link->sink;
  367. }
  368. return NULL;
  369. }
  370. /* -----------------------------------------------------------------------------
  371. * Initialization
  372. */
  373. #define VSP1_ENTITY_ROUTE(ent) \
  374. { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
  375. { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
  376. #define VSP1_ENTITY_ROUTE_RPF(idx) \
  377. { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
  378. { 0, }, VI6_DPR_NODE_RPF(idx) }
  379. #define VSP1_ENTITY_ROUTE_UDS(idx) \
  380. { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
  381. { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
  382. #define VSP1_ENTITY_ROUTE_WPF(idx) \
  383. { VSP1_ENTITY_WPF, idx, 0, \
  384. { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
  385. static const struct vsp1_route vsp1_routes[] = {
  386. { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
  387. { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
  388. VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
  389. VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
  390. VSP1_ENTITY_ROUTE(CLU),
  391. { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
  392. { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
  393. VSP1_ENTITY_ROUTE(HSI),
  394. VSP1_ENTITY_ROUTE(HST),
  395. { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
  396. VSP1_ENTITY_ROUTE(LUT),
  397. VSP1_ENTITY_ROUTE_RPF(0),
  398. VSP1_ENTITY_ROUTE_RPF(1),
  399. VSP1_ENTITY_ROUTE_RPF(2),
  400. VSP1_ENTITY_ROUTE_RPF(3),
  401. VSP1_ENTITY_ROUTE_RPF(4),
  402. VSP1_ENTITY_ROUTE(SRU),
  403. VSP1_ENTITY_ROUTE_UDS(0),
  404. VSP1_ENTITY_ROUTE_UDS(1),
  405. VSP1_ENTITY_ROUTE_UDS(2),
  406. VSP1_ENTITY_ROUTE_WPF(0),
  407. VSP1_ENTITY_ROUTE_WPF(1),
  408. VSP1_ENTITY_ROUTE_WPF(2),
  409. VSP1_ENTITY_ROUTE_WPF(3),
  410. };
  411. int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
  412. const char *name, unsigned int num_pads,
  413. const struct v4l2_subdev_ops *ops, u32 function)
  414. {
  415. struct v4l2_subdev *subdev;
  416. unsigned int i;
  417. int ret;
  418. for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
  419. if (vsp1_routes[i].type == entity->type &&
  420. vsp1_routes[i].index == entity->index) {
  421. entity->route = &vsp1_routes[i];
  422. break;
  423. }
  424. }
  425. if (i == ARRAY_SIZE(vsp1_routes))
  426. return -EINVAL;
  427. mutex_init(&entity->lock);
  428. entity->vsp1 = vsp1;
  429. entity->source_pad = num_pads - 1;
  430. /* Allocate and initialize pads. */
  431. entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
  432. GFP_KERNEL);
  433. if (entity->pads == NULL)
  434. return -ENOMEM;
  435. for (i = 0; i < num_pads - 1; ++i)
  436. entity->pads[i].flags = MEDIA_PAD_FL_SINK;
  437. entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
  438. sizeof(*entity->sources), GFP_KERNEL);
  439. if (entity->sources == NULL)
  440. return -ENOMEM;
  441. /* Single-pad entities only have a sink. */
  442. entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
  443. : MEDIA_PAD_FL_SINK;
  444. /* Initialize the media entity. */
  445. ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
  446. entity->pads);
  447. if (ret < 0)
  448. return ret;
  449. /* Initialize the V4L2 subdev. */
  450. subdev = &entity->subdev;
  451. v4l2_subdev_init(subdev, ops);
  452. subdev->entity.function = function;
  453. subdev->entity.ops = &vsp1->media_ops;
  454. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  455. snprintf(subdev->name, sizeof(subdev->name), "%s %s",
  456. dev_name(vsp1->dev), name);
  457. vsp1_entity_init_cfg(subdev, NULL);
  458. /*
  459. * Allocate the pad configuration to store formats and selection
  460. * rectangles.
  461. */
  462. entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
  463. if (entity->config == NULL) {
  464. media_entity_cleanup(&entity->subdev.entity);
  465. return -ENOMEM;
  466. }
  467. return 0;
  468. }
  469. void vsp1_entity_destroy(struct vsp1_entity *entity)
  470. {
  471. if (entity->ops && entity->ops->destroy)
  472. entity->ops->destroy(entity);
  473. if (entity->subdev.ctrl_handler)
  474. v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
  475. v4l2_subdev_free_pad_config(entity->config);
  476. media_entity_cleanup(&entity->subdev.entity);
  477. }