1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_uds.c  --  R-Car VSP1 Up and Down Scaler
4  *
5  * Copyright (C) 2013-2014 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/device.h>
11 #include <linux/gfp.h>
12 
13 #include <media/v4l2-subdev.h>
14 
15 #include "vsp1.h"
16 #include "vsp1_dl.h"
17 #include "vsp1_pipe.h"
18 #include "vsp1_uds.h"
19 
20 #define UDS_MIN_SIZE				4U
21 #define UDS_MAX_SIZE				8190U
22 
23 #define UDS_MIN_FACTOR				0x0100
24 #define UDS_MAX_FACTOR				0xffff
25 
26 /* -----------------------------------------------------------------------------
27  * Device Access
28  */
29 
vsp1_uds_write(struct vsp1_uds * uds,struct vsp1_dl_body * dlb,u32 reg,u32 data)30 static inline void vsp1_uds_write(struct vsp1_uds *uds,
31 				  struct vsp1_dl_body *dlb, u32 reg, u32 data)
32 {
33 	vsp1_dl_body_write(dlb, reg + uds->entity.index * VI6_UDS_OFFSET, data);
34 }
35 
36 /* -----------------------------------------------------------------------------
37  * Scaling Computation
38  */
39 
vsp1_uds_set_alpha(struct vsp1_entity * entity,struct vsp1_dl_body * dlb,unsigned int alpha)40 void vsp1_uds_set_alpha(struct vsp1_entity *entity, struct vsp1_dl_body *dlb,
41 			unsigned int alpha)
42 {
43 	struct vsp1_uds *uds = to_uds(&entity->subdev);
44 
45 	vsp1_uds_write(uds, dlb, VI6_UDS_ALPVAL,
46 		       alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
47 }
48 
49 /*
50  * uds_output_size - Return the output size for an input size and scaling ratio
51  * @input: input size in pixels
52  * @ratio: scaling ratio in U4.12 fixed-point format
53  */
uds_output_size(unsigned int input,unsigned int ratio)54 static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
55 {
56 	if (ratio > 4096) {
57 		/* Down-scaling */
58 		unsigned int mp;
59 
60 		mp = ratio / 4096;
61 		mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
62 
63 		return (input - 1) / mp * mp * 4096 / ratio + 1;
64 	} else {
65 		/* Up-scaling */
66 		return (input - 1) * 4096 / ratio + 1;
67 	}
68 }
69 
70 /*
71  * uds_output_limits - Return the min and max output sizes for an input size
72  * @input: input size in pixels
73  * @minimum: minimum output size (returned)
74  * @maximum: maximum output size (returned)
75  */
uds_output_limits(unsigned int input,unsigned int * minimum,unsigned int * maximum)76 static void uds_output_limits(unsigned int input,
77 			      unsigned int *minimum, unsigned int *maximum)
78 {
79 	*minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
80 	*maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
81 }
82 
83 /*
84  * uds_passband_width - Return the passband filter width for a scaling ratio
85  * @ratio: scaling ratio in U4.12 fixed-point format
86  */
uds_passband_width(unsigned int ratio)87 static unsigned int uds_passband_width(unsigned int ratio)
88 {
89 	if (ratio >= 4096) {
90 		/* Down-scaling */
91 		unsigned int mp;
92 
93 		mp = ratio / 4096;
94 		mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
95 
96 		return 64 * 4096 * mp / ratio;
97 	} else {
98 		/* Up-scaling */
99 		return 64;
100 	}
101 }
102 
uds_compute_ratio(unsigned int input,unsigned int output)103 static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
104 {
105 	/* TODO: This is an approximation that will need to be refined. */
106 	return (input - 1) * 4096 / (output - 1);
107 }
108 
109 /* -----------------------------------------------------------------------------
110  * V4L2 Subdevice Pad Operations
111  */
112 
uds_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)113 static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
114 			      struct v4l2_subdev_state *sd_state,
115 			      struct v4l2_subdev_mbus_code_enum *code)
116 {
117 	static const unsigned int codes[] = {
118 		MEDIA_BUS_FMT_ARGB8888_1X32,
119 		MEDIA_BUS_FMT_AYUV8_1X32,
120 	};
121 
122 	return vsp1_subdev_enum_mbus_code(subdev, sd_state, code, codes,
123 					  ARRAY_SIZE(codes));
124 }
125 
uds_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)126 static int uds_enum_frame_size(struct v4l2_subdev *subdev,
127 			       struct v4l2_subdev_state *sd_state,
128 			       struct v4l2_subdev_frame_size_enum *fse)
129 {
130 	struct vsp1_uds *uds = to_uds(subdev);
131 	struct v4l2_subdev_state *config;
132 	struct v4l2_mbus_framefmt *format;
133 	int ret = 0;
134 
135 	config = vsp1_entity_get_pad_config(&uds->entity, sd_state,
136 					    fse->which);
137 	if (!config)
138 		return -EINVAL;
139 
140 	format = vsp1_entity_get_pad_format(&uds->entity, config,
141 					    UDS_PAD_SINK);
142 
143 	mutex_lock(&uds->entity.lock);
144 
145 	if (fse->index || fse->code != format->code) {
146 		ret = -EINVAL;
147 		goto done;
148 	}
149 
150 	if (fse->pad == UDS_PAD_SINK) {
151 		fse->min_width = UDS_MIN_SIZE;
152 		fse->max_width = UDS_MAX_SIZE;
153 		fse->min_height = UDS_MIN_SIZE;
154 		fse->max_height = UDS_MAX_SIZE;
155 	} else {
156 		uds_output_limits(format->width, &fse->min_width,
157 				  &fse->max_width);
158 		uds_output_limits(format->height, &fse->min_height,
159 				  &fse->max_height);
160 	}
161 
162 done:
163 	mutex_unlock(&uds->entity.lock);
164 	return ret;
165 }
166 
uds_try_format(struct vsp1_uds * uds,struct v4l2_subdev_state * sd_state,unsigned int pad,struct v4l2_mbus_framefmt * fmt)167 static void uds_try_format(struct vsp1_uds *uds,
168 			   struct v4l2_subdev_state *sd_state,
169 			   unsigned int pad, struct v4l2_mbus_framefmt *fmt)
170 {
171 	struct v4l2_mbus_framefmt *format;
172 	unsigned int minimum;
173 	unsigned int maximum;
174 
175 	switch (pad) {
176 	case UDS_PAD_SINK:
177 		/* Default to YUV if the requested format is not supported. */
178 		if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
179 		    fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
180 			fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
181 
182 		fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
183 		fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
184 		break;
185 
186 	case UDS_PAD_SOURCE:
187 		/* The UDS scales but can't perform format conversion. */
188 		format = vsp1_entity_get_pad_format(&uds->entity, sd_state,
189 						    UDS_PAD_SINK);
190 		fmt->code = format->code;
191 
192 		uds_output_limits(format->width, &minimum, &maximum);
193 		fmt->width = clamp(fmt->width, minimum, maximum);
194 		uds_output_limits(format->height, &minimum, &maximum);
195 		fmt->height = clamp(fmt->height, minimum, maximum);
196 		break;
197 	}
198 
199 	fmt->field = V4L2_FIELD_NONE;
200 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
201 }
202 
uds_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)203 static int uds_set_format(struct v4l2_subdev *subdev,
204 			  struct v4l2_subdev_state *sd_state,
205 			  struct v4l2_subdev_format *fmt)
206 {
207 	struct vsp1_uds *uds = to_uds(subdev);
208 	struct v4l2_subdev_state *config;
209 	struct v4l2_mbus_framefmt *format;
210 	int ret = 0;
211 
212 	mutex_lock(&uds->entity.lock);
213 
214 	config = vsp1_entity_get_pad_config(&uds->entity, sd_state,
215 					    fmt->which);
216 	if (!config) {
217 		ret = -EINVAL;
218 		goto done;
219 	}
220 
221 	uds_try_format(uds, config, fmt->pad, &fmt->format);
222 
223 	format = vsp1_entity_get_pad_format(&uds->entity, config, fmt->pad);
224 	*format = fmt->format;
225 
226 	if (fmt->pad == UDS_PAD_SINK) {
227 		/* Propagate the format to the source pad. */
228 		format = vsp1_entity_get_pad_format(&uds->entity, config,
229 						    UDS_PAD_SOURCE);
230 		*format = fmt->format;
231 
232 		uds_try_format(uds, config, UDS_PAD_SOURCE, format);
233 	}
234 
235 done:
236 	mutex_unlock(&uds->entity.lock);
237 	return ret;
238 }
239 
240 /* -----------------------------------------------------------------------------
241  * V4L2 Subdevice Operations
242  */
243 
244 static const struct v4l2_subdev_pad_ops uds_pad_ops = {
245 	.init_cfg = vsp1_entity_init_cfg,
246 	.enum_mbus_code = uds_enum_mbus_code,
247 	.enum_frame_size = uds_enum_frame_size,
248 	.get_fmt = vsp1_subdev_get_pad_format,
249 	.set_fmt = uds_set_format,
250 };
251 
252 static const struct v4l2_subdev_ops uds_ops = {
253 	.pad    = &uds_pad_ops,
254 };
255 
256 /* -----------------------------------------------------------------------------
257  * VSP1 Entity Operations
258  */
259 
uds_configure_stream(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)260 static void uds_configure_stream(struct vsp1_entity *entity,
261 				 struct vsp1_pipeline *pipe,
262 				 struct vsp1_dl_list *dl,
263 				 struct vsp1_dl_body *dlb)
264 {
265 	struct vsp1_uds *uds = to_uds(&entity->subdev);
266 	const struct v4l2_mbus_framefmt *output;
267 	const struct v4l2_mbus_framefmt *input;
268 	unsigned int hscale;
269 	unsigned int vscale;
270 	bool multitap;
271 
272 	input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
273 					   UDS_PAD_SINK);
274 	output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
275 					    UDS_PAD_SOURCE);
276 
277 	hscale = uds_compute_ratio(input->width, output->width);
278 	vscale = uds_compute_ratio(input->height, output->height);
279 
280 	dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
281 
282 	/*
283 	 * Multi-tap scaling can't be enabled along with alpha scaling when
284 	 * scaling down with a factor lower than or equal to 1/2 in either
285 	 * direction.
286 	 */
287 	if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
288 		multitap = false;
289 	else
290 		multitap = true;
291 
292 	vsp1_uds_write(uds, dlb, VI6_UDS_CTRL,
293 		       (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
294 		       (multitap ? VI6_UDS_CTRL_BC : 0));
295 
296 	vsp1_uds_write(uds, dlb, VI6_UDS_PASS_BWIDTH,
297 		       (uds_passband_width(hscale)
298 				<< VI6_UDS_PASS_BWIDTH_H_SHIFT) |
299 		       (uds_passband_width(vscale)
300 				<< VI6_UDS_PASS_BWIDTH_V_SHIFT));
301 
302 	/* Set the scaling ratios. */
303 	vsp1_uds_write(uds, dlb, VI6_UDS_SCALE,
304 		       (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
305 		       (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
306 }
307 
uds_configure_partition(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)308 static void uds_configure_partition(struct vsp1_entity *entity,
309 				    struct vsp1_pipeline *pipe,
310 				    struct vsp1_dl_list *dl,
311 				    struct vsp1_dl_body *dlb)
312 {
313 	struct vsp1_uds *uds = to_uds(&entity->subdev);
314 	struct vsp1_partition *partition = pipe->partition;
315 	const struct v4l2_mbus_framefmt *output;
316 
317 	output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
318 					    UDS_PAD_SOURCE);
319 
320 	/* Input size clipping. */
321 	vsp1_uds_write(uds, dlb, VI6_UDS_HSZCLIP, VI6_UDS_HSZCLIP_HCEN |
322 		       (0 << VI6_UDS_HSZCLIP_HCL_OFST_SHIFT) |
323 		       (partition->uds_sink.width
324 				<< VI6_UDS_HSZCLIP_HCL_SIZE_SHIFT));
325 
326 	/* Output size clipping. */
327 	vsp1_uds_write(uds, dlb, VI6_UDS_CLIP_SIZE,
328 		       (partition->uds_source.width
329 				<< VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
330 		       (output->height
331 				<< VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
332 }
333 
uds_max_width(struct vsp1_entity * entity,struct vsp1_pipeline * pipe)334 static unsigned int uds_max_width(struct vsp1_entity *entity,
335 				  struct vsp1_pipeline *pipe)
336 {
337 	struct vsp1_uds *uds = to_uds(&entity->subdev);
338 	const struct v4l2_mbus_framefmt *output;
339 	const struct v4l2_mbus_framefmt *input;
340 	unsigned int hscale;
341 
342 	input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
343 					   UDS_PAD_SINK);
344 	output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
345 					    UDS_PAD_SOURCE);
346 	hscale = output->width / input->width;
347 
348 	/*
349 	 * The maximum width of the UDS is 304 pixels. These are input pixels
350 	 * in the event of up-scaling, and output pixels in the event of
351 	 * downscaling.
352 	 *
353 	 * To support overlapping partition windows we clamp at units of 256 and
354 	 * the remaining pixels are reserved.
355 	 */
356 	if (hscale <= 2)
357 		return 256;
358 	else if (hscale <= 4)
359 		return 512;
360 	else if (hscale <= 8)
361 		return 1024;
362 	else
363 		return 2048;
364 }
365 
366 /* -----------------------------------------------------------------------------
367  * Partition Algorithm Support
368  */
369 
uds_partition(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_partition * partition,unsigned int partition_idx,struct vsp1_partition_window * window)370 static void uds_partition(struct vsp1_entity *entity,
371 			  struct vsp1_pipeline *pipe,
372 			  struct vsp1_partition *partition,
373 			  unsigned int partition_idx,
374 			  struct vsp1_partition_window *window)
375 {
376 	struct vsp1_uds *uds = to_uds(&entity->subdev);
377 	const struct v4l2_mbus_framefmt *output;
378 	const struct v4l2_mbus_framefmt *input;
379 
380 	/* Initialise the partition state. */
381 	partition->uds_sink = *window;
382 	partition->uds_source = *window;
383 
384 	input = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
385 					   UDS_PAD_SINK);
386 	output = vsp1_entity_get_pad_format(&uds->entity, uds->entity.config,
387 					    UDS_PAD_SOURCE);
388 
389 	partition->uds_sink.width = window->width * input->width
390 				  / output->width;
391 	partition->uds_sink.left = window->left * input->width
392 				 / output->width;
393 
394 	*window = partition->uds_sink;
395 }
396 
397 static const struct vsp1_entity_operations uds_entity_ops = {
398 	.configure_stream = uds_configure_stream,
399 	.configure_partition = uds_configure_partition,
400 	.max_width = uds_max_width,
401 	.partition = uds_partition,
402 };
403 
404 /* -----------------------------------------------------------------------------
405  * Initialization and Cleanup
406  */
407 
vsp1_uds_create(struct vsp1_device * vsp1,unsigned int index)408 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
409 {
410 	struct vsp1_uds *uds;
411 	char name[6];
412 	int ret;
413 
414 	uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
415 	if (uds == NULL)
416 		return ERR_PTR(-ENOMEM);
417 
418 	uds->entity.ops = &uds_entity_ops;
419 	uds->entity.type = VSP1_ENTITY_UDS;
420 	uds->entity.index = index;
421 
422 	sprintf(name, "uds.%u", index);
423 	ret = vsp1_entity_init(vsp1, &uds->entity, name, 2, &uds_ops,
424 			       MEDIA_ENT_F_PROC_VIDEO_SCALER);
425 	if (ret < 0)
426 		return ERR_PTR(ret);
427 
428 	return uds;
429 }
430