• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
vsp1_uif_read(struct vsp1_uif * uif,u32 reg)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 
vsp1_uif_write(struct vsp1_uif * uif,struct vsp1_dl_body * dlb,u32 reg,u32 data)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 
vsp1_uif_get_crc(struct vsp1_uif * uif)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 
uif_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)56 static int uif_enum_mbus_code(struct v4l2_subdev *subdev,
57 			      struct v4l2_subdev_pad_config *cfg,
58 			      struct v4l2_subdev_mbus_code_enum *code)
59 {
60 	return vsp1_subdev_enum_mbus_code(subdev, cfg, code, uif_codes,
61 					  ARRAY_SIZE(uif_codes));
62 }
63 
uif_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)64 static int uif_enum_frame_size(struct v4l2_subdev *subdev,
65 			       struct v4l2_subdev_pad_config *cfg,
66 			       struct v4l2_subdev_frame_size_enum *fse)
67 {
68 	return vsp1_subdev_enum_frame_size(subdev, cfg, fse, UIF_MIN_SIZE,
69 					   UIF_MIN_SIZE, UIF_MAX_SIZE,
70 					   UIF_MAX_SIZE);
71 }
72 
uif_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)73 static int uif_set_format(struct v4l2_subdev *subdev,
74 			    struct v4l2_subdev_pad_config *cfg,
75 			    struct v4l2_subdev_format *fmt)
76 {
77 	return vsp1_subdev_set_pad_format(subdev, cfg, fmt, uif_codes,
78 					  ARRAY_SIZE(uif_codes),
79 					  UIF_MIN_SIZE, UIF_MIN_SIZE,
80 					  UIF_MAX_SIZE, UIF_MAX_SIZE);
81 }
82 
uif_get_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)83 static int uif_get_selection(struct v4l2_subdev *subdev,
84 			     struct v4l2_subdev_pad_config *cfg,
85 			     struct v4l2_subdev_selection *sel)
86 {
87 	struct vsp1_uif *uif = to_uif(subdev);
88 	struct v4l2_subdev_pad_config *config;
89 	struct v4l2_mbus_framefmt *format;
90 	int ret = 0;
91 
92 	if (sel->pad != UIF_PAD_SINK)
93 		return -EINVAL;
94 
95 	mutex_lock(&uif->entity.lock);
96 
97 	config = vsp1_entity_get_pad_config(&uif->entity, cfg, sel->which);
98 	if (!config) {
99 		ret = -EINVAL;
100 		goto done;
101 	}
102 
103 	switch (sel->target) {
104 	case V4L2_SEL_TGT_CROP_BOUNDS:
105 	case V4L2_SEL_TGT_CROP_DEFAULT:
106 		format = vsp1_entity_get_pad_format(&uif->entity, config,
107 						    UIF_PAD_SINK);
108 		sel->r.left = 0;
109 		sel->r.top = 0;
110 		sel->r.width = format->width;
111 		sel->r.height = format->height;
112 		break;
113 
114 	case V4L2_SEL_TGT_CROP:
115 		sel->r = *vsp1_entity_get_pad_selection(&uif->entity, config,
116 							sel->pad, sel->target);
117 		break;
118 
119 	default:
120 		ret = -EINVAL;
121 		break;
122 	}
123 
124 done:
125 	mutex_unlock(&uif->entity.lock);
126 	return ret;
127 }
128 
uif_set_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)129 static int uif_set_selection(struct v4l2_subdev *subdev,
130 			     struct v4l2_subdev_pad_config *cfg,
131 			     struct v4l2_subdev_selection *sel)
132 {
133 	struct vsp1_uif *uif = to_uif(subdev);
134 	struct v4l2_subdev_pad_config *config;
135 	struct v4l2_mbus_framefmt *format;
136 	struct v4l2_rect *selection;
137 	int ret = 0;
138 
139 	if (sel->pad != UIF_PAD_SINK ||
140 	    sel->target != V4L2_SEL_TGT_CROP)
141 		return -EINVAL;
142 
143 	mutex_lock(&uif->entity.lock);
144 
145 	config = vsp1_entity_get_pad_config(&uif->entity, cfg, sel->which);
146 	if (!config) {
147 		ret = -EINVAL;
148 		goto done;
149 	}
150 
151 	/* The crop rectangle must be inside the input frame. */
152 	format = vsp1_entity_get_pad_format(&uif->entity, config, UIF_PAD_SINK);
153 
154 	sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
155 	sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
156 	sel->r.width = clamp_t(unsigned int, sel->r.width, UIF_MIN_SIZE,
157 			       format->width - sel->r.left);
158 	sel->r.height = clamp_t(unsigned int, sel->r.height, UIF_MIN_SIZE,
159 				format->height - sel->r.top);
160 
161 	/* Store the crop rectangle. */
162 	selection = vsp1_entity_get_pad_selection(&uif->entity, config,
163 						  sel->pad, V4L2_SEL_TGT_CROP);
164 	*selection = sel->r;
165 
166 done:
167 	mutex_unlock(&uif->entity.lock);
168 	return ret;
169 }
170 
171 /* -----------------------------------------------------------------------------
172  * V4L2 Subdevice Operations
173  */
174 
175 static const struct v4l2_subdev_pad_ops uif_pad_ops = {
176 	.init_cfg = vsp1_entity_init_cfg,
177 	.enum_mbus_code = uif_enum_mbus_code,
178 	.enum_frame_size = uif_enum_frame_size,
179 	.get_fmt = vsp1_subdev_get_pad_format,
180 	.set_fmt = uif_set_format,
181 	.get_selection = uif_get_selection,
182 	.set_selection = uif_set_selection,
183 };
184 
185 static const struct v4l2_subdev_ops uif_ops = {
186 	.pad    = &uif_pad_ops,
187 };
188 
189 /* -----------------------------------------------------------------------------
190  * VSP1 Entity Operations
191  */
192 
uif_configure_stream(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)193 static void uif_configure_stream(struct vsp1_entity *entity,
194 				 struct vsp1_pipeline *pipe,
195 				 struct vsp1_dl_list *dl,
196 				 struct vsp1_dl_body *dlb)
197 {
198 	struct vsp1_uif *uif = to_uif(&entity->subdev);
199 	const struct v4l2_rect *crop;
200 	unsigned int left;
201 	unsigned int width;
202 
203 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMPMR,
204 		       VI6_UIF_DISCOM_DOCMPMR_SEL(9));
205 
206 	crop = vsp1_entity_get_pad_selection(entity, entity->config,
207 					     UIF_PAD_SINK, V4L2_SEL_TGT_CROP);
208 
209 	left = crop->left;
210 	width = crop->width;
211 
212 	/* On M3-W the horizontal coordinates are twice the register value. */
213 	if (uif->m3w_quirk) {
214 		left /= 2;
215 		width /= 2;
216 	}
217 
218 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPXR, left);
219 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSPYR, crop->top);
220 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZXR, width);
221 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMSZYR, crop->height);
222 
223 	vsp1_uif_write(uif, dlb, VI6_UIF_DISCOM_DOCMCR,
224 		       VI6_UIF_DISCOM_DOCMCR_CMPR);
225 }
226 
227 static const struct vsp1_entity_operations uif_entity_ops = {
228 	.configure_stream = uif_configure_stream,
229 };
230 
231 /* -----------------------------------------------------------------------------
232  * Initialization and Cleanup
233  */
234 
235 static const struct soc_device_attribute vsp1_r8a7796[] = {
236 	{ .soc_id = "r8a7796" },
237 	{ /* sentinel */ }
238 };
239 
vsp1_uif_create(struct vsp1_device * vsp1,unsigned int index)240 struct vsp1_uif *vsp1_uif_create(struct vsp1_device *vsp1, unsigned int index)
241 {
242 	struct vsp1_uif *uif;
243 	char name[6];
244 	int ret;
245 
246 	uif = devm_kzalloc(vsp1->dev, sizeof(*uif), GFP_KERNEL);
247 	if (!uif)
248 		return ERR_PTR(-ENOMEM);
249 
250 	if (soc_device_match(vsp1_r8a7796))
251 		uif->m3w_quirk = true;
252 
253 	uif->entity.ops = &uif_entity_ops;
254 	uif->entity.type = VSP1_ENTITY_UIF;
255 	uif->entity.index = index;
256 
257 	/* The datasheet names the two UIF instances UIF4 and UIF5. */
258 	sprintf(name, "uif.%u", index + 4);
259 	ret = vsp1_entity_init(vsp1, &uif->entity, name, 2, &uif_ops,
260 			       MEDIA_ENT_F_PROC_VIDEO_STATISTICS);
261 	if (ret < 0)
262 		return ERR_PTR(ret);
263 
264 	return uif;
265 }
266