vsp1_entity.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static inline struct vsp1_entity *
  22. media_entity_to_vsp1_entity(struct media_entity *entity)
  23. {
  24. return container_of(entity, struct vsp1_entity, subdev.entity);
  25. }
  26. void vsp1_entity_route_setup(struct vsp1_entity *source,
  27. struct vsp1_dl_list *dl)
  28. {
  29. struct vsp1_entity *sink;
  30. if (source->route->reg == 0)
  31. return;
  32. sink = media_entity_to_vsp1_entity(source->sink);
  33. vsp1_dl_list_write(dl, source->route->reg,
  34. sink->route->inputs[source->sink_pad]);
  35. }
  36. /* -----------------------------------------------------------------------------
  37. * V4L2 Subdevice Operations
  38. */
  39. /**
  40. * vsp1_entity_get_pad_config - Get the pad configuration for an entity
  41. * @entity: the entity
  42. * @cfg: the TRY pad configuration
  43. * @which: configuration selector (ACTIVE or TRY)
  44. *
  45. * When called with which set to V4L2_SUBDEV_FORMAT_ACTIVE the caller must hold
  46. * the entity lock to access the returned configuration.
  47. *
  48. * Return the pad configuration requested by the which argument. The TRY
  49. * configuration is passed explicitly to the function through the cfg argument
  50. * and simply returned when requested. The ACTIVE configuration comes from the
  51. * entity structure.
  52. */
  53. struct v4l2_subdev_pad_config *
  54. vsp1_entity_get_pad_config(struct vsp1_entity *entity,
  55. struct v4l2_subdev_pad_config *cfg,
  56. enum v4l2_subdev_format_whence which)
  57. {
  58. switch (which) {
  59. case V4L2_SUBDEV_FORMAT_ACTIVE:
  60. return entity->config;
  61. case V4L2_SUBDEV_FORMAT_TRY:
  62. default:
  63. return cfg;
  64. }
  65. }
  66. /**
  67. * vsp1_entity_get_pad_format - Get a pad format from storage for an entity
  68. * @entity: the entity
  69. * @cfg: the configuration storage
  70. * @pad: the pad number
  71. *
  72. * Return the format stored in the given configuration for an entity's pad. The
  73. * configuration can be an ACTIVE or TRY configuration.
  74. */
  75. struct v4l2_mbus_framefmt *
  76. vsp1_entity_get_pad_format(struct vsp1_entity *entity,
  77. struct v4l2_subdev_pad_config *cfg,
  78. unsigned int pad)
  79. {
  80. return v4l2_subdev_get_try_format(&entity->subdev, cfg, pad);
  81. }
  82. /**
  83. * vsp1_entity_get_pad_selection - Get a pad selection from storage for entity
  84. * @entity: the entity
  85. * @cfg: the configuration storage
  86. * @pad: the pad number
  87. * @target: the selection target
  88. *
  89. * Return the selection rectangle stored in the given configuration for an
  90. * entity's pad. The configuration can be an ACTIVE or TRY configuration. The
  91. * selection target can be COMPOSE or CROP.
  92. */
  93. struct v4l2_rect *
  94. vsp1_entity_get_pad_selection(struct vsp1_entity *entity,
  95. struct v4l2_subdev_pad_config *cfg,
  96. unsigned int pad, unsigned int target)
  97. {
  98. switch (target) {
  99. case V4L2_SEL_TGT_COMPOSE:
  100. return v4l2_subdev_get_try_compose(&entity->subdev, cfg, pad);
  101. case V4L2_SEL_TGT_CROP:
  102. return v4l2_subdev_get_try_crop(&entity->subdev, cfg, pad);
  103. default:
  104. return NULL;
  105. }
  106. }
  107. /*
  108. * vsp1_entity_init_cfg - Initialize formats on all pads
  109. * @subdev: V4L2 subdevice
  110. * @cfg: V4L2 subdev pad configuration
  111. *
  112. * Initialize all pad formats with default values in the given pad config. This
  113. * function can be used as a handler for the subdev pad::init_cfg operation.
  114. */
  115. int vsp1_entity_init_cfg(struct v4l2_subdev *subdev,
  116. struct v4l2_subdev_pad_config *cfg)
  117. {
  118. struct v4l2_subdev_format format;
  119. unsigned int pad;
  120. for (pad = 0; pad < subdev->entity.num_pads - 1; ++pad) {
  121. memset(&format, 0, sizeof(format));
  122. format.pad = pad;
  123. format.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
  124. : V4L2_SUBDEV_FORMAT_ACTIVE;
  125. v4l2_subdev_call(subdev, pad, set_fmt, cfg, &format);
  126. }
  127. return 0;
  128. }
  129. /*
  130. * vsp1_subdev_get_pad_format - Subdev pad get_fmt handler
  131. * @subdev: V4L2 subdevice
  132. * @cfg: V4L2 subdev pad configuration
  133. * @fmt: V4L2 subdev format
  134. *
  135. * This function implements the subdev get_fmt pad operation. It can be used as
  136. * a direct drop-in for the operation handler.
  137. */
  138. int vsp1_subdev_get_pad_format(struct v4l2_subdev *subdev,
  139. struct v4l2_subdev_pad_config *cfg,
  140. struct v4l2_subdev_format *fmt)
  141. {
  142. struct vsp1_entity *entity = to_vsp1_entity(subdev);
  143. struct v4l2_subdev_pad_config *config;
  144. config = vsp1_entity_get_pad_config(entity, cfg, fmt->which);
  145. if (!config)
  146. return -EINVAL;
  147. mutex_lock(&entity->lock);
  148. fmt->format = *vsp1_entity_get_pad_format(entity, config, fmt->pad);
  149. mutex_unlock(&entity->lock);
  150. return 0;
  151. }
  152. /*
  153. * vsp1_subdev_enum_mbus_code - Subdev pad enum_mbus_code handler
  154. * @subdev: V4L2 subdevice
  155. * @cfg: V4L2 subdev pad configuration
  156. * @code: Media bus code enumeration
  157. * @codes: Array of supported media bus codes
  158. * @ncodes: Number of supported media bus codes
  159. *
  160. * This function implements the subdev enum_mbus_code pad operation for entities
  161. * that do not support format conversion. It enumerates the given supported
  162. * media bus codes on the sink pad and reports a source pad format identical to
  163. * the sink pad.
  164. */
  165. int vsp1_subdev_enum_mbus_code(struct v4l2_subdev *subdev,
  166. struct v4l2_subdev_pad_config *cfg,
  167. struct v4l2_subdev_mbus_code_enum *code,
  168. const unsigned int *codes, unsigned int ncodes)
  169. {
  170. struct vsp1_entity *entity = to_vsp1_entity(subdev);
  171. if (code->pad == 0) {
  172. if (code->index >= ncodes)
  173. return -EINVAL;
  174. code->code = codes[code->index];
  175. } else {
  176. struct v4l2_subdev_pad_config *config;
  177. struct v4l2_mbus_framefmt *format;
  178. /* The entity can't perform format conversion, the sink format
  179. * is always identical to the source format.
  180. */
  181. if (code->index)
  182. return -EINVAL;
  183. config = vsp1_entity_get_pad_config(entity, cfg, code->which);
  184. if (!config)
  185. return -EINVAL;
  186. mutex_lock(&entity->lock);
  187. format = vsp1_entity_get_pad_format(entity, config, 0);
  188. code->code = format->code;
  189. mutex_unlock(&entity->lock);
  190. }
  191. return 0;
  192. }
  193. /*
  194. * vsp1_subdev_enum_frame_size - Subdev pad enum_frame_size handler
  195. * @subdev: V4L2 subdevice
  196. * @cfg: V4L2 subdev pad configuration
  197. * @fse: Frame size enumeration
  198. * @min_width: Minimum image width
  199. * @min_height: Minimum image height
  200. * @max_width: Maximum image width
  201. * @max_height: Maximum image height
  202. *
  203. * This function implements the subdev enum_frame_size pad operation for
  204. * entities that do not support scaling or cropping. It reports the given
  205. * minimum and maximum frame width and height on the sink pad, and a fixed
  206. * source pad size identical to the sink pad.
  207. */
  208. int vsp1_subdev_enum_frame_size(struct v4l2_subdev *subdev,
  209. struct v4l2_subdev_pad_config *cfg,
  210. struct v4l2_subdev_frame_size_enum *fse,
  211. unsigned int min_width, unsigned int min_height,
  212. unsigned int max_width, unsigned int max_height)
  213. {
  214. struct vsp1_entity *entity = to_vsp1_entity(subdev);
  215. struct v4l2_subdev_pad_config *config;
  216. struct v4l2_mbus_framefmt *format;
  217. int ret = 0;
  218. config = vsp1_entity_get_pad_config(entity, cfg, fse->which);
  219. if (!config)
  220. return -EINVAL;
  221. format = vsp1_entity_get_pad_format(entity, config, fse->pad);
  222. mutex_lock(&entity->lock);
  223. if (fse->index || fse->code != format->code) {
  224. ret = -EINVAL;
  225. goto done;
  226. }
  227. if (fse->pad == 0) {
  228. fse->min_width = min_width;
  229. fse->max_width = max_width;
  230. fse->min_height = min_height;
  231. fse->max_height = max_height;
  232. } else {
  233. /* The size on the source pad are fixed and always identical to
  234. * the size on the sink pad.
  235. */
  236. fse->min_width = format->width;
  237. fse->max_width = format->width;
  238. fse->min_height = format->height;
  239. fse->max_height = format->height;
  240. }
  241. done:
  242. mutex_unlock(&entity->lock);
  243. return ret;
  244. }
  245. /* -----------------------------------------------------------------------------
  246. * Media Operations
  247. */
  248. int vsp1_entity_link_setup(struct media_entity *entity,
  249. const struct media_pad *local,
  250. const struct media_pad *remote, u32 flags)
  251. {
  252. struct vsp1_entity *source;
  253. if (!(local->flags & MEDIA_PAD_FL_SOURCE))
  254. return 0;
  255. source = media_entity_to_vsp1_entity(local->entity);
  256. if (!source->route)
  257. return 0;
  258. if (flags & MEDIA_LNK_FL_ENABLED) {
  259. if (source->sink)
  260. return -EBUSY;
  261. source->sink = remote->entity;
  262. source->sink_pad = remote->index;
  263. } else {
  264. source->sink = NULL;
  265. source->sink_pad = 0;
  266. }
  267. return 0;
  268. }
  269. /* -----------------------------------------------------------------------------
  270. * Initialization
  271. */
  272. #define VSP1_ENTITY_ROUTE(ent) \
  273. { VSP1_ENTITY_##ent, 0, VI6_DPR_##ent##_ROUTE, \
  274. { VI6_DPR_NODE_##ent }, VI6_DPR_NODE_##ent }
  275. #define VSP1_ENTITY_ROUTE_RPF(idx) \
  276. { VSP1_ENTITY_RPF, idx, VI6_DPR_RPF_ROUTE(idx), \
  277. { 0, }, VI6_DPR_NODE_RPF(idx) }
  278. #define VSP1_ENTITY_ROUTE_UDS(idx) \
  279. { VSP1_ENTITY_UDS, idx, VI6_DPR_UDS_ROUTE(idx), \
  280. { VI6_DPR_NODE_UDS(idx) }, VI6_DPR_NODE_UDS(idx) }
  281. #define VSP1_ENTITY_ROUTE_WPF(idx) \
  282. { VSP1_ENTITY_WPF, idx, 0, \
  283. { VI6_DPR_NODE_WPF(idx) }, VI6_DPR_NODE_WPF(idx) }
  284. static const struct vsp1_route vsp1_routes[] = {
  285. { VSP1_ENTITY_BRU, 0, VI6_DPR_BRU_ROUTE,
  286. { VI6_DPR_NODE_BRU_IN(0), VI6_DPR_NODE_BRU_IN(1),
  287. VI6_DPR_NODE_BRU_IN(2), VI6_DPR_NODE_BRU_IN(3),
  288. VI6_DPR_NODE_BRU_IN(4) }, VI6_DPR_NODE_BRU_OUT },
  289. VSP1_ENTITY_ROUTE(CLU),
  290. VSP1_ENTITY_ROUTE(HSI),
  291. VSP1_ENTITY_ROUTE(HST),
  292. { VSP1_ENTITY_LIF, 0, 0, { VI6_DPR_NODE_LIF, }, VI6_DPR_NODE_LIF },
  293. VSP1_ENTITY_ROUTE(LUT),
  294. VSP1_ENTITY_ROUTE_RPF(0),
  295. VSP1_ENTITY_ROUTE_RPF(1),
  296. VSP1_ENTITY_ROUTE_RPF(2),
  297. VSP1_ENTITY_ROUTE_RPF(3),
  298. VSP1_ENTITY_ROUTE_RPF(4),
  299. VSP1_ENTITY_ROUTE(SRU),
  300. VSP1_ENTITY_ROUTE_UDS(0),
  301. VSP1_ENTITY_ROUTE_UDS(1),
  302. VSP1_ENTITY_ROUTE_UDS(2),
  303. VSP1_ENTITY_ROUTE_WPF(0),
  304. VSP1_ENTITY_ROUTE_WPF(1),
  305. VSP1_ENTITY_ROUTE_WPF(2),
  306. VSP1_ENTITY_ROUTE_WPF(3),
  307. };
  308. int vsp1_entity_init(struct vsp1_device *vsp1, struct vsp1_entity *entity,
  309. const char *name, unsigned int num_pads,
  310. const struct v4l2_subdev_ops *ops, u32 function)
  311. {
  312. struct v4l2_subdev *subdev;
  313. unsigned int i;
  314. int ret;
  315. for (i = 0; i < ARRAY_SIZE(vsp1_routes); ++i) {
  316. if (vsp1_routes[i].type == entity->type &&
  317. vsp1_routes[i].index == entity->index) {
  318. entity->route = &vsp1_routes[i];
  319. break;
  320. }
  321. }
  322. if (i == ARRAY_SIZE(vsp1_routes))
  323. return -EINVAL;
  324. mutex_init(&entity->lock);
  325. entity->vsp1 = vsp1;
  326. entity->source_pad = num_pads - 1;
  327. /* Allocate and initialize pads. */
  328. entity->pads = devm_kzalloc(vsp1->dev, num_pads * sizeof(*entity->pads),
  329. GFP_KERNEL);
  330. if (entity->pads == NULL)
  331. return -ENOMEM;
  332. for (i = 0; i < num_pads - 1; ++i)
  333. entity->pads[i].flags = MEDIA_PAD_FL_SINK;
  334. entity->pads[num_pads - 1].flags = MEDIA_PAD_FL_SOURCE;
  335. /* Initialize the media entity. */
  336. ret = media_entity_pads_init(&entity->subdev.entity, num_pads,
  337. entity->pads);
  338. if (ret < 0)
  339. return ret;
  340. /* Initialize the V4L2 subdev. */
  341. subdev = &entity->subdev;
  342. v4l2_subdev_init(subdev, ops);
  343. subdev->entity.function = function;
  344. subdev->entity.ops = &vsp1->media_ops;
  345. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  346. snprintf(subdev->name, sizeof(subdev->name), "%s %s",
  347. dev_name(vsp1->dev), name);
  348. vsp1_entity_init_cfg(subdev, NULL);
  349. /* Allocate the pad configuration to store formats and selection
  350. * rectangles.
  351. */
  352. entity->config = v4l2_subdev_alloc_pad_config(&entity->subdev);
  353. if (entity->config == NULL) {
  354. media_entity_cleanup(&entity->subdev.entity);
  355. return -ENOMEM;
  356. }
  357. return 0;
  358. }
  359. void vsp1_entity_destroy(struct vsp1_entity *entity)
  360. {
  361. if (entity->ops && entity->ops->destroy)
  362. entity->ops->destroy(entity);
  363. if (entity->subdev.ctrl_handler)
  364. v4l2_ctrl_handler_free(entity->subdev.ctrl_handler);
  365. v4l2_subdev_free_pad_config(entity->config);
  366. media_entity_cleanup(&entity->subdev.entity);
  367. }