vsp1_entity.c 19 KB

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