xref: /linux/drivers/media/platform/renesas/vsp1/vsp1_uif.c (revision b83deaa741558babf4b8d51d34f6637ccfff1b26)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_uif.c  --  R-Car VSP1 User Logic Interface
4  *
5  * Copyright (C) 2017-2018 Laurent Pinchart
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/device.h>
11 #include <linux/gfp.h>
12 #include <linux/sys_soc.h>
13 
14 #include <media/media-entity.h>
15 #include <media/v4l2-subdev.h>
16 
17 #include "vsp1.h"
18 #include "vsp1_dl.h"
19 #include "vsp1_entity.h"
20 #include "vsp1_uif.h"
21 
22 #define UIF_MIN_SIZE				4U
23 #define UIF_MAX_SIZE				8190U
24 
25 /* -----------------------------------------------------------------------------
26  * Device Access
27  */
28 
29 static inline u32 vsp1_uif_read(struct vsp1_uif *uif, u32 reg)
30 {
31 	return vsp1_read(uif->entity.vsp1,
32 			 uif->entity.index * VI6_UIF_OFFSET + reg);
33 }
34 
35 static inline void vsp1_uif_write(struct vsp1_uif *uif,
36 				  struct vsp1_dl_body *dlb, u32 reg, u32 data)
37 {
38 	vsp1_dl_body_write(dlb, reg + uif->entity.index * VI6_UIF_OFFSET, data);
39 }
40 
41 u32 vsp1_uif_get_crc(struct vsp1_uif *uif)
42 {
43 	return vsp1_uif_read(uif, VI6_UIF_DISCOM_DOCMCCRCR);
44 }
45 
46 /* -----------------------------------------------------------------------------
47  * V4L2 Subdevice Pad Operations
48  */
49 
50 static const unsigned int uif_codes[] = {
51 	MEDIA_BUS_FMT_ARGB8888_1X32,
52 	MEDIA_BUS_FMT_AHSV8888_1X32,
53 	MEDIA_BUS_FMT_AYUV8_1X32,
54 };
55 
56 static int uif_enum_mbus_code(struct v4l2_subdev *subdev,
57 			      struct v4l2_subdev_state *sd_state,
58 			      struct v4l2_subdev_mbus_code_enum *code)
59 {
60 	return vsp1_subdev_enum_mbus_code(subdev, sd_state, code, uif_codes,
61 					  ARRAY_SIZE(uif_codes));
62 }
63 
64 static int uif_enum_frame_size(struct v4l2_subdev *subdev,
65 			       struct v4l2_subdev_state *sd_state,
66 			       struct v4l2_subdev_frame_size_enum *fse)
67 {
68 	return vsp1_subdev_enum_frame_size(subdev, sd_state, fse,
69 					   UIF_MIN_SIZE,
70 					   UIF_MIN_SIZE, UIF_MAX_SIZE,
71 					   UIF_MAX_SIZE);
72 }
73 
74 static int uif_set_format(struct v4l2_subdev *subdev,
75 			    struct v4l2_subdev_state *sd_state,
76 			    struct v4l2_subdev_format *fmt)
77 {
78 	return vsp1_subdev_set_pad_format(subdev, sd_state, fmt, uif_codes,
79 					  ARRAY_SIZE(uif_codes),
80 					  UIF_MIN_SIZE, UIF_MIN_SIZE,
81 					  UIF_MAX_SIZE, UIF_MAX_SIZE);
82 }
83 
84 static int uif_get_selection(struct v4l2_subdev *subdev,
85 			     struct v4l2_subdev_state *sd_state,
86 			     struct v4l2_subdev_selection *sel)
87 {
88 	struct vsp1_uif *uif = to_uif(subdev);
89 	struct v4l2_subdev_state *config;
90 	struct v4l2_mbus_framefmt *format;
91 	int ret = 0;
92 
93 	if (sel->pad != UIF_PAD_SINK)
94 		return -EINVAL;
95 
96 	mutex_lock(&uif->entity.lock);
97 
98 	config = vsp1_entity_get_pad_config(&uif->entity, sd_state,
99 					    sel->which);
100 	if (!config) {
101 		ret = -EINVAL;
102 		goto done;
103 	}
104 
105 	switch (sel->target) {
106 	case V4L2_SEL_TGT_CROP_BOUNDS:
107 	case V4L2_SEL_TGT_CROP_DEFAULT:
108 		format = vsp1_entity_get_pad_format(&uif->entity, config,
109 						    UIF_PAD_SINK);
110 		sel->r.left = 0;
111 		sel->r.top = 0;
112 		sel->r.width = format->width;
113 		sel->r.height = format->height;
114 		break;
115 
116 	case V4L2_SEL_TGT_CROP:
117 		sel->r = *vsp1_entity_get_pad_selection(&uif->entity, config,
118 							sel->pad, sel->target);
119 		break;
120 
121 	default:
122 		ret = -EINVAL;
123 		break;
124 	}
125 
126 done:
127 	mutex_unlock(&uif->entity.lock);
128 	return ret;
129 }
130 
131 static int uif_set_selection(struct v4l2_subdev *subdev,
132 			     struct v4l2_subdev_state *sd_state,
133 			     struct v4l2_subdev_selection *sel)
134 {
135 	struct vsp1_uif *uif = to_uif(subdev);
136 	struct v4l2_subdev_state *config;
137 	struct v4l2_mbus_framefmt *format;
138 	struct v4l2_rect *selection;
139 	int ret = 0;
140 
141 	if (sel->pad != UIF_PAD_SINK ||
142 	    sel->target != V4L2_SEL_TGT_CROP)
143 		return -EINVAL;
144 
145 	mutex_lock(&uif->entity.lock);
146 
147 	config = vsp1_entity_get_pad_config(&uif->entity, sd_state,
148 					    sel->which);
149 	if (!config) {
150 		ret = -EINVAL;
151 		goto done;
152 	}
153 
154 	/* The crop rectangle must be inside the input frame. */
155 	format = vsp1_entity_get_pad_format(&uif->entity, config, UIF_PAD_SINK);
156 
157 	sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
158 	sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
159 	sel->r.width = clamp_t(unsigned int, sel->r.width, UIF_MIN_SIZE,
160 			       format->width - sel->r.left);
161 	sel->r.height = clamp_t(unsigned int, sel->r.height, UIF_MIN_SIZE,
162 				format->height - sel->r.top);
163 
164 	/* Store the crop rectangle. */
165 	selection = vsp1_entity_get_pad_selection(&uif->entity, config,
166 						  sel->pad, V4L2_SEL_TGT_CROP);
167 	*selection = sel->r;
168 
169 done:
170 	mutex_unlock(&uif->entity.lock);
171 	return ret;
172 }
173 
174 /* -----------------------------------------------------------------------------
175  * V4L2 Subdevice Operations
176  */
177 
178 static const struct v4l2_subdev_pad_ops uif_pad_ops = {
179 	.init_cfg = vsp1_entity_init_cfg,
180 	.enum_mbus_code = uif_enum_mbus_code,
181 	.enum_frame_size = uif_enum_frame_size,
182 	.get_fmt = vsp1_subdev_get_pad_format,
183 	.set_fmt = uif_set_format,
184 	.get_selection = uif_get_selection,
185 	.set_selection = uif_set_selection,
186 };
187 
188 static const struct v4l2_subdev_ops uif_ops = {
189 	.pad    = &uif_pad_ops,
190 };
191 
192 /* -----------------------------------------------------------------------------
193  * VSP1 Entity Operations
194  */
195 
196 static void uif_configure_stream(struct vsp1_entity *entity,
197 				 struct vsp1_pipeline *pipe,
198 				 struct vsp1_dl_list *dl,
199 				 struct vsp1_dl_body *dlb)
200 {
201 	struct vsp1_uif *uif = to_uif(&entity->subdev);
202 	const struct v4l2_rect *crop;
203 	unsigned int left;
204 	unsigned int width;
205 
206 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMPMR,
207 		       VI6_UIF_DISCOM_DOCMPMR_SEL(9));
208 
209 	crop = vsp1_entity_get_pad_selection(entity, entity->config,
210 					     UIF_PAD_SINK, V4L2_SEL_TGT_CROP);
211 
212 	left = crop->left;
213 	width = crop->width;
214 
215 	/* On M3-W the horizontal coordinates are twice the register value. */
216 	if (uif->m3w_quirk) {
217 		left /= 2;
218 		width /= 2;
219 	}
220 
221 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPXR, left);
222 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPYR, crop->top);
223 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZXR, width);
224 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZYR, crop->height);
225 
226 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMCR,
227 		       VI6_UIF_DISCOM_DOCMCR_CMPR);
228 }
229 
230 static const struct vsp1_entity_operations uif_entity_ops = {
231 	.configure_stream = uif_configure_stream,
232 };
233 
234 /* -----------------------------------------------------------------------------
235  * Initialization and Cleanup
236  */
237 
238 static const struct soc_device_attribute vsp1_r8a7796[] = {
239 	{ .soc_id = "r8a7796" },
240 	{ /* sentinel */ }
241 };
242 
243 struct vsp1_uif *vsp1_uif_create(struct vsp1_device *vsp1, unsigned int index)
244 {
245 	struct vsp1_uif *uif;
246 	char name[6];
247 	int ret;
248 
249 	uif = devm_kzalloc(vsp1->dev, sizeof(*uif), GFP_KERNEL);
250 	if (!uif)
251 		return ERR_PTR(-ENOMEM);
252 
253 	if (soc_device_match(vsp1_r8a7796))
254 		uif->m3w_quirk = true;
255 
256 	uif->entity.ops = &uif_entity_ops;
257 	uif->entity.type = VSP1_ENTITY_UIF;
258 	uif->entity.index = index;
259 
260 	/* The datasheet names the two UIF instances UIF4 and UIF5. */
261 	sprintf(name, "uif.%u", index + 4);
262 	ret = vsp1_entity_init(vsp1, &uif->entity, name, 2, &uif_ops,
263 			       MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
264 	if (ret < 0)
265 		return ERR_PTR(ret);
266 
267 	return uif;
268 }
269