18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * vsp1_uif.c -- R-Car VSP1 User Logic Interface 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2017-2018 Laurent Pinchart 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/device.h> 118c2ecf20Sopenharmony_ci#include <linux/gfp.h> 128c2ecf20Sopenharmony_ci#include <linux/sys_soc.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <media/media-entity.h> 158c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include "vsp1.h" 188c2ecf20Sopenharmony_ci#include "vsp1_dl.h" 198c2ecf20Sopenharmony_ci#include "vsp1_entity.h" 208c2ecf20Sopenharmony_ci#include "vsp1_uif.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define UIF_MIN_SIZE 4U 238c2ecf20Sopenharmony_ci#define UIF_MAX_SIZE 8190U 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 268c2ecf20Sopenharmony_ci * Device Access 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic inline u32 vsp1_uif_read(struct vsp1_uif *uif, u32 reg) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci return vsp1_read(uif->entity.vsp1, 328c2ecf20Sopenharmony_ci uif->entity.index * VI6_UIF_OFFSET + reg); 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic inline void vsp1_uif_write(struct vsp1_uif *uif, 368c2ecf20Sopenharmony_ci struct vsp1_dl_body *dlb, u32 reg, u32 data) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci vsp1_dl_body_write(dlb, reg + uif->entity.index * VI6_UIF_OFFSET, data); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciu32 vsp1_uif_get_crc(struct vsp1_uif *uif) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci return vsp1_uif_read(uif, VI6_UIF_DISCOM_DOCMCCRCR); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 478c2ecf20Sopenharmony_ci * V4L2 Subdevice Pad Operations 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic const unsigned int uif_codes[] = { 518c2ecf20Sopenharmony_ci MEDIA_BUS_FMT_ARGB8888_1X32, 528c2ecf20Sopenharmony_ci MEDIA_BUS_FMT_AHSV8888_1X32, 538c2ecf20Sopenharmony_ci MEDIA_BUS_FMT_AYUV8_1X32, 548c2ecf20Sopenharmony_ci}; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic int uif_enum_mbus_code(struct v4l2_subdev *subdev, 578c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 588c2ecf20Sopenharmony_ci struct v4l2_subdev_mbus_code_enum *code) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci return vsp1_subdev_enum_mbus_code(subdev, cfg, code, uif_codes, 618c2ecf20Sopenharmony_ci ARRAY_SIZE(uif_codes)); 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic int uif_enum_frame_size(struct v4l2_subdev *subdev, 658c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 668c2ecf20Sopenharmony_ci struct v4l2_subdev_frame_size_enum *fse) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci return vsp1_subdev_enum_frame_size(subdev, cfg, fse, UIF_MIN_SIZE, 698c2ecf20Sopenharmony_ci UIF_MIN_SIZE, UIF_MAX_SIZE, 708c2ecf20Sopenharmony_ci UIF_MAX_SIZE); 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic int uif_set_format(struct v4l2_subdev *subdev, 748c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 758c2ecf20Sopenharmony_ci struct v4l2_subdev_format *fmt) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci return vsp1_subdev_set_pad_format(subdev, cfg, fmt, uif_codes, 788c2ecf20Sopenharmony_ci ARRAY_SIZE(uif_codes), 798c2ecf20Sopenharmony_ci UIF_MIN_SIZE, UIF_MIN_SIZE, 808c2ecf20Sopenharmony_ci UIF_MAX_SIZE, UIF_MAX_SIZE); 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic int uif_get_selection(struct v4l2_subdev *subdev, 848c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 858c2ecf20Sopenharmony_ci struct v4l2_subdev_selection *sel) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci struct vsp1_uif *uif = to_uif(subdev); 888c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *config; 898c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt *format; 908c2ecf20Sopenharmony_ci int ret = 0; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (sel->pad != UIF_PAD_SINK) 938c2ecf20Sopenharmony_ci return -EINVAL; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci mutex_lock(&uif->entity.lock); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci config = vsp1_entity_get_pad_config(&uif->entity, cfg, sel->which); 988c2ecf20Sopenharmony_ci if (!config) { 998c2ecf20Sopenharmony_ci ret = -EINVAL; 1008c2ecf20Sopenharmony_ci goto done; 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci switch (sel->target) { 1048c2ecf20Sopenharmony_ci case V4L2_SEL_TGT_CROP_BOUNDS: 1058c2ecf20Sopenharmony_ci case V4L2_SEL_TGT_CROP_DEFAULT: 1068c2ecf20Sopenharmony_ci format = vsp1_entity_get_pad_format(&uif->entity, config, 1078c2ecf20Sopenharmony_ci UIF_PAD_SINK); 1088c2ecf20Sopenharmony_ci sel->r.left = 0; 1098c2ecf20Sopenharmony_ci sel->r.top = 0; 1108c2ecf20Sopenharmony_ci sel->r.width = format->width; 1118c2ecf20Sopenharmony_ci sel->r.height = format->height; 1128c2ecf20Sopenharmony_ci break; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci case V4L2_SEL_TGT_CROP: 1158c2ecf20Sopenharmony_ci sel->r = *vsp1_entity_get_pad_selection(&uif->entity, config, 1168c2ecf20Sopenharmony_ci sel->pad, sel->target); 1178c2ecf20Sopenharmony_ci break; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci default: 1208c2ecf20Sopenharmony_ci ret = -EINVAL; 1218c2ecf20Sopenharmony_ci break; 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cidone: 1258c2ecf20Sopenharmony_ci mutex_unlock(&uif->entity.lock); 1268c2ecf20Sopenharmony_ci return ret; 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic int uif_set_selection(struct v4l2_subdev *subdev, 1308c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 1318c2ecf20Sopenharmony_ci struct v4l2_subdev_selection *sel) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci struct vsp1_uif *uif = to_uif(subdev); 1348c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *config; 1358c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt *format; 1368c2ecf20Sopenharmony_ci struct v4l2_rect *selection; 1378c2ecf20Sopenharmony_ci int ret = 0; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (sel->pad != UIF_PAD_SINK || 1408c2ecf20Sopenharmony_ci sel->target != V4L2_SEL_TGT_CROP) 1418c2ecf20Sopenharmony_ci return -EINVAL; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci mutex_lock(&uif->entity.lock); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci config = vsp1_entity_get_pad_config(&uif->entity, cfg, sel->which); 1468c2ecf20Sopenharmony_ci if (!config) { 1478c2ecf20Sopenharmony_ci ret = -EINVAL; 1488c2ecf20Sopenharmony_ci goto done; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* The crop rectangle must be inside the input frame. */ 1528c2ecf20Sopenharmony_ci format = vsp1_entity_get_pad_format(&uif->entity, config, UIF_PAD_SINK); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1); 1558c2ecf20Sopenharmony_ci sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1); 1568c2ecf20Sopenharmony_ci sel->r.width = clamp_t(unsigned int, sel->r.width, UIF_MIN_SIZE, 1578c2ecf20Sopenharmony_ci format->width - sel->r.left); 1588c2ecf20Sopenharmony_ci sel->r.height = clamp_t(unsigned int, sel->r.height, UIF_MIN_SIZE, 1598c2ecf20Sopenharmony_ci format->height - sel->r.top); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci /* Store the crop rectangle. */ 1628c2ecf20Sopenharmony_ci selection = vsp1_entity_get_pad_selection(&uif->entity, config, 1638c2ecf20Sopenharmony_ci sel->pad, V4L2_SEL_TGT_CROP); 1648c2ecf20Sopenharmony_ci *selection = sel->r; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_cidone: 1678c2ecf20Sopenharmony_ci mutex_unlock(&uif->entity.lock); 1688c2ecf20Sopenharmony_ci return ret; 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 1728c2ecf20Sopenharmony_ci * V4L2 Subdevice Operations 1738c2ecf20Sopenharmony_ci */ 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops uif_pad_ops = { 1768c2ecf20Sopenharmony_ci .init_cfg = vsp1_entity_init_cfg, 1778c2ecf20Sopenharmony_ci .enum_mbus_code = uif_enum_mbus_code, 1788c2ecf20Sopenharmony_ci .enum_frame_size = uif_enum_frame_size, 1798c2ecf20Sopenharmony_ci .get_fmt = vsp1_subdev_get_pad_format, 1808c2ecf20Sopenharmony_ci .set_fmt = uif_set_format, 1818c2ecf20Sopenharmony_ci .get_selection = uif_get_selection, 1828c2ecf20Sopenharmony_ci .set_selection = uif_set_selection, 1838c2ecf20Sopenharmony_ci}; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops uif_ops = { 1868c2ecf20Sopenharmony_ci .pad = &uif_pad_ops, 1878c2ecf20Sopenharmony_ci}; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 1908c2ecf20Sopenharmony_ci * VSP1 Entity Operations 1918c2ecf20Sopenharmony_ci */ 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic void uif_configure_stream(struct vsp1_entity *entity, 1948c2ecf20Sopenharmony_ci struct vsp1_pipeline *pipe, 1958c2ecf20Sopenharmony_ci struct vsp1_dl_list *dl, 1968c2ecf20Sopenharmony_ci struct vsp1_dl_body *dlb) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci struct vsp1_uif *uif = to_uif(&entity->subdev); 1998c2ecf20Sopenharmony_ci const struct v4l2_rect *crop; 2008c2ecf20Sopenharmony_ci unsigned int left; 2018c2ecf20Sopenharmony_ci unsigned int width; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMPMR, 2048c2ecf20Sopenharmony_ci VI6_UIF_DISCOM_DOCMPMR_SEL(9)); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci crop = vsp1_entity_get_pad_selection(entity, entity->config, 2078c2ecf20Sopenharmony_ci UIF_PAD_SINK, V4L2_SEL_TGT_CROP); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci left = crop->left; 2108c2ecf20Sopenharmony_ci width = crop->width; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* On M3-W the horizontal coordinates are twice the register value. */ 2138c2ecf20Sopenharmony_ci if (uif->m3w_quirk) { 2148c2ecf20Sopenharmony_ci left /= 2; 2158c2ecf20Sopenharmony_ci width /= 2; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPXR, left); 2198c2ecf20Sopenharmony_ci vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPYR, crop->top); 2208c2ecf20Sopenharmony_ci vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZXR, width); 2218c2ecf20Sopenharmony_ci vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZYR, crop->height); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMCR, 2248c2ecf20Sopenharmony_ci VI6_UIF_DISCOM_DOCMCR_CMPR); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic const struct vsp1_entity_operations uif_entity_ops = { 2288c2ecf20Sopenharmony_ci .configure_stream = uif_configure_stream, 2298c2ecf20Sopenharmony_ci}; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 2328c2ecf20Sopenharmony_ci * Initialization and Cleanup 2338c2ecf20Sopenharmony_ci */ 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_cistatic const struct soc_device_attribute vsp1_r8a7796[] = { 2368c2ecf20Sopenharmony_ci { .soc_id = "r8a7796" }, 2378c2ecf20Sopenharmony_ci { /* sentinel */ } 2388c2ecf20Sopenharmony_ci}; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistruct vsp1_uif *vsp1_uif_create(struct vsp1_device *vsp1, unsigned int index) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci struct vsp1_uif *uif; 2438c2ecf20Sopenharmony_ci char name[6]; 2448c2ecf20Sopenharmony_ci int ret; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci uif = devm_kzalloc(vsp1->dev, sizeof(*uif), GFP_KERNEL); 2478c2ecf20Sopenharmony_ci if (!uif) 2488c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci if (soc_device_match(vsp1_r8a7796)) 2518c2ecf20Sopenharmony_ci uif->m3w_quirk = true; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci uif->entity.ops = &uif_entity_ops; 2548c2ecf20Sopenharmony_ci uif->entity.type = VSP1_ENTITY_UIF; 2558c2ecf20Sopenharmony_ci uif->entity.index = index; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /* The datasheet names the two UIF instances UIF4 and UIF5. */ 2588c2ecf20Sopenharmony_ci sprintf(name, "uif.%u", index + 4); 2598c2ecf20Sopenharmony_ci ret = vsp1_entity_init(vsp1, &uif->entity, name, 2, &uif_ops, 2608c2ecf20Sopenharmony_ci MEDIA_ENT_F_PROC_VIDEO_STATISTICS); 2618c2ecf20Sopenharmony_ci if (ret < 0) 2628c2ecf20Sopenharmony_ci return ERR_PTR(ret); 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci return uif; 2658c2ecf20Sopenharmony_ci} 266