vsp1_entity.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. void vsp1_entity_route_setup(struct vsp1_entity *entity,
  24. struct vsp1_pipeline *pipe,
  25. struct vsp1_dl_list *dl)
  26. {
  27. struct vsp1_entity *source;
  28. u32 route;
  29. if (entity->type == VSP1_ENTITY_HGO) {
  30. u32 smppt;
  31. /*
  32. * The HGO is a special case, its routing is configured on the
  33. * sink pad.
  34. */
  35. source = entity->sources[0];
  36. smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
  37. | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
  38. vsp1_dl_list_write(dl, VI6_DPR_HGO_SMPPT, smppt);
  39. return;
  40. } else if (entity->type == VSP1_ENTITY_HGT) {
  41. u32 smppt;
  42. /*
  43. * The HGT is a special case, its routing is configured on the
  44. * sink pad.
  45. */
  46. source = entity->sources[0];
  47. smppt = (pipe->output->entity.index << VI6_DPR_SMPPT_TGW_SHIFT)
  48. | (source->route->output << VI6_DPR_SMPPT_PT_SHIFT);
  49. vsp1_dl_list_write(dl, VI6_DPR_HGT_SMPPT, smppt);
  50. return;
  51. }
  52. source = entity;
  53. if (source->route->reg == 0)
  54. return;
  55. route = source->sink->route->inputs[source->sink_pad];
  56. /*
  57. * The ILV and BRS share the same data path route. The extra BRSSEL bit
  58. * selects between the ILV and BRS.
  59. */
  60. if (source->type == VSP1_ENTITY_BRS)
  61. route |= VI6_DPR_ROUTE_BRSSEL;
  62. vsp1_dl_list_write(dl, source->route->reg, route);
  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 inline struct vsp1_entity *
  279. media_entity_to_vsp1_entity(struct media_entity *entity)
  280. {
  281. return container_of(entity, struct vsp1_entity, subdev.entity);
  282. }
  283. static int vsp1_entity_link_setup_source(const struct media_pad *source_pad,
  284. const struct media_pad *sink_pad,
  285. u32 flags)
  286. {
  287. struct vsp1_entity *source;
  288. source = media_entity_to_vsp1_entity(source_pad->entity);
  289. if (!source->route)
  290. return 0;
  291. if (flags & MEDIA_LNK_FL_ENABLED) {
  292. struct vsp1_entity *sink
  293. = media_entity_to_vsp1_entity(sink_pad->entity);
  294. /*
  295. * Fan-out is limited to one for the normal data path plus
  296. * optional HGO and HGT. We ignore the HGO and HGT here.
  297. */
  298. if (sink->type != VSP1_ENTITY_HGO &&
  299. sink->type != VSP1_ENTITY_HGT) {
  300. if (source->sink)
  301. return -EBUSY;
  302. source->sink = sink;
  303. source->sink_pad = sink_pad->index;
  304. }
  305. } else {
  306. source->sink = NULL;
  307. source->sink_pad = 0;
  308. }
  309. return 0;
  310. }
  311. static int vsp1_entity_link_setup_sink(const struct media_pad *source_pad,
  312. const struct media_pad *sink_pad,
  313. u32 flags)
  314. {
  315. struct vsp1_entity *sink;
  316. struct vsp1_entity *source;
  317. sink = media_entity_to_vsp1_entity(sink_pad->entity);
  318. source = media_entity_to_vsp1_entity(source_pad->entity);
  319. if (flags & MEDIA_LNK_FL_ENABLED) {
  320. /* Fan-in is limited to one. */
  321. if (sink->sources[sink_pad->index])
  322. return -EBUSY;
  323. sink->sources[sink_pad->index] = source;
  324. } else {
  325. sink->sources[sink_pad->index] = NULL;
  326. }
  327. return 0;
  328. }
  329. int vsp1_entity_link_setup(struct media_entity *entity,
  330. const struct media_pad *local,
  331. const struct media_pad *remote, u32 flags)
  332. {
  333. if (local->flags & MEDIA_PAD_FL_SOURCE)
  334. return vsp1_entity_link_setup_source(local, remote, flags);
  335. else
  336. return vsp1_entity_link_setup_sink(remote, local, flags);
  337. }
  338. /**
  339. * vsp1_entity_remote_pad - Find the pad at the remote end of a link
  340. * @pad: Pad at the local end of the link
  341. *
  342. * Search for a remote pad connected to the given pad by iterating over all
  343. * links originating or terminating at that pad until an enabled link is found.
  344. *
  345. * Our link setup implementation guarantees that the output fan-out will not be
  346. * higher than one for the data pipelines, except for the links to the HGO and
  347. * HGT that can be enabled in addition to a regular data link. When traversing
  348. * outgoing links this function ignores HGO and HGT entities and should thus be
  349. * used in place of the generic media_entity_remote_pad() function to traverse
  350. * data pipelines.
  351. *
  352. * Return a pointer to the pad at the remote end of the first found enabled
  353. * link, or NULL if no enabled link has been found.
  354. */
  355. struct media_pad *vsp1_entity_remote_pad(struct media_pad *pad)
  356. {
  357. struct media_link *link;
  358. list_for_each_entry(link, &pad->entity->links, list) {
  359. struct vsp1_entity *entity;
  360. if (!(link->flags & MEDIA_LNK_FL_ENABLED))
  361. continue;
  362. /* If we're the sink the source will never be an HGO or HGT. */
  363. if (link->sink == pad)
  364. return link->source;
  365. if (link->source != pad)
  366. continue;
  367. /* If the sink isn't a subdevice it can't be an HGO or HGT. */
  368. if (!is_media_entity_v4l2_subdev(link->sink->entity))
  369. return link->sink;
  370. entity = media_entity_to_vsp1_entity(link->sink->entity);
  371. if (entity->type != VSP1_ENTITY_HGO &&
  372. entity->type != VSP1_ENTITY_HGT)
  373. return link->sink;
  374. }
  375. return NULL;
  376. }
  377. /* -----------------------------------------------------------------------------
  378. * Initialization
  379. */
  380. #define VSP1_ENTITY_ROUTE(ent) \
  381. { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
  382. { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
  383. #define VSP1_ENTITY_ROUTE_RPF(idx) \
  384. { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
  385. { 0, }, VI6_DPR_NODE_RPF(idx) }
  386. #define VSP1_ENTITY_ROUTE_UDS(idx) \
  387. { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
  388. { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
  389. #define VSP1_ENTITY_ROUTE_WPF(idx) \
  390. { VSP1_ENTITY_WPF, idx, 0, \
  391. { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
  392. static const struct vsp1_route vsp1_routes[] = {
  393. { VSP1_ENTITY_BRS, 0, VI6_DPR_ILV_BRS_ROUTE,
  394. { VI6_DPR_NODE_BRS_IN(0), VI6_DPR_NODE_BRS_IN(1) }, 0 },
  395. { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
  396. { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
  397. VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
  398. VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
  399. VSP1_ENTITY_ROUTE(CLU),
  400. { VSP1_ENTITY_HGO, 0, 0, { 0, }, 0 },
  401. { VSP1_ENTITY_HGT, 0, 0, { 0, }, 0 },
  402. VSP1_ENTITY_ROUTE(HSI),
  403. VSP1_ENTITY_ROUTE(HST),
  404. { VSP1_ENTITY_LIF, 0, 0, { 0, }, 0 },
  405. { VSP1_ENTITY_LIF, 1, 0, { 0, }, 0 },
  406. VSP1_ENTITY_ROUTE(LUT),
  407. VSP1_ENTITY_ROUTE_RPF(0),
  408. VSP1_ENTITY_ROUTE_RPF(1),
  409. VSP1_ENTITY_ROUTE_RPF(2),
  410. VSP1_ENTITY_ROUTE_RPF(3),
  411. VSP1_ENTITY_ROUTE_RPF(4),
  412. VSP1_ENTITY_ROUTE(SRU),
  413. VSP1_ENTITY_ROUTE_UDS(0),
  414. VSP1_ENTITY_ROUTE_UDS(1),
  415. VSP1_ENTITY_ROUTE_UDS(2),
  416. VSP1_ENTITY_ROUTE_WPF(0),
  417. VSP1_ENTITY_ROUTE_WPF(1),
  418. VSP1_ENTITY_ROUTE_WPF(2),
  419. VSP1_ENTITY_ROUTE_WPF(3),
  420. };
  421. int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
  422. const char *name, unsigned int num_pads,
  423. const struct v4l2_subdev_ops *ops, u32 function)
  424. {
  425. struct v4l2_subdev *subdev;
  426. unsigned int i;
  427. int ret;
  428. for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
  429. if (vsp1_routes[i].type == entity->type &&
  430. vsp1_routes[i].index == entity->index) {
  431. entity->route = &vsp1_routes[i];
  432. break;
  433. }
  434. }
  435. if (i == ARRAY_SIZE(vsp1_routes))
  436. return -EINVAL;
  437. mutex_init(&entity->lock);
  438. entity->vsp1 = vsp1;
  439. entity->source_pad = num_pads - 1;
  440. /* Allocate and initialize pads. */
  441. entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
  442. GFP_KERNEL);
  443. if (entity->pads == NULL)
  444. return -ENOMEM;
  445. for (i = 0; i < num_pads - 1; ++i)
  446. entity->pads[i].flags = MEDIA_PAD_FL_SINK;
  447. entity->sources = devm_kcalloc(vsp1->dev, max(num_pads - 1, 1U),
  448. sizeof(*entity->sources), GFP_KERNEL);
  449. if (entity->sources == NULL)
  450. return -ENOMEM;
  451. /* Single-pad entities only have a sink. */
  452. entity->pads[num_pads - 1].flags = num_pads > 1 ? MEDIA_PAD_FL_SOURCE
  453. : MEDIA_PAD_FL_SINK;
  454. /* Initialize the media entity. */
  455. ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
  456. entity->pads);
  457. if (ret < 0)
  458. return ret;
  459. /* Initialize the V4L2 subdev. */
  460. subdev = &entity->subdev;
  461. v4l2_subdev_init(subdev, ops);
  462. subdev->entity.function = function;
  463. subdev->entity.ops = &vsp1->media_ops;
  464. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  465. snprintf(subdev->name, sizeof(subdev->name), "%s %s",
  466. dev_name(vsp1->dev), name);
  467. vsp1_entity_init_cfg(subdev, NULL);
  468. /*
  469. * Allocate the pad configuration to store formats and selection
  470. * rectangles.
  471. */
  472. entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
  473. if (entity->config == NULL) {
  474. media_entity_cleanup(&entity->subdev.entity);
  475. return -ENOMEM;
  476. }
  477. return 0;
  478. }
  479. void vsp1_entity_destroy(struct vsp1_entity *entity)
  480. {
  481. if (entity->ops && entity->ops->destroy)
  482. entity->ops->destroy(entity);
  483. if (entity->subdev.ctrl_handler)
  484. v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
  485. v4l2_subdev_free_pad_config(entity->config);
  486. media_entity_cleanup(&entity->subdev.entity);
  487. }