• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * vsp1_uds.c  --  R-Car VSP1 Up and Down Scaler
3  *
4  * Copyright (C) 2013-2014 Renesas Electronics Corporation
5  *
6  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <linux/device.h>
15 #include <linux/gfp.h>
16 
17 #include <media/v4l2-subdev.h>
18 
19 #include "vsp1.h"
20 #include "vsp1_uds.h"
21 
22 #define UDS_MIN_SIZE				4U
23 #define UDS_MAX_SIZE				8190U
24 
25 #define UDS_MIN_FACTOR				0x0100
26 #define UDS_MAX_FACTOR				0xffff
27 
28 /* -----------------------------------------------------------------------------
29  * Device Access
30  */
31 
vsp1_uds_read(struct vsp1_uds * uds,u32 reg)32 static inline u32 vsp1_uds_read(struct vsp1_uds *uds, u32 reg)
33 {
34 	return vsp1_read(uds->entity.vsp1,
35 			 reg + uds->entity.index * VI6_UDS_OFFSET);
36 }
37 
vsp1_uds_write(struct vsp1_uds * uds,u32 reg,u32 data)38 static inline void vsp1_uds_write(struct vsp1_uds *uds, u32 reg, u32 data)
39 {
40 	vsp1_write(uds->entity.vsp1,
41 		   reg + uds->entity.index * VI6_UDS_OFFSET, data);
42 }
43 
44 /* -----------------------------------------------------------------------------
45  * Scaling Computation
46  */
47 
vsp1_uds_set_alpha(struct vsp1_uds * uds,unsigned int alpha)48 void vsp1_uds_set_alpha(struct vsp1_uds *uds, unsigned int alpha)
49 {
50 	vsp1_uds_write(uds, VI6_UDS_ALPVAL, alpha << VI6_UDS_ALPVAL_VAL0_SHIFT);
51 }
52 
53 /*
54  * uds_output_size - Return the output size for an input size and scaling ratio
55  * @input: input size in pixels
56  * @ratio: scaling ratio in U4.12 fixed-point format
57  */
uds_output_size(unsigned int input,unsigned int ratio)58 static unsigned int uds_output_size(unsigned int input, unsigned int ratio)
59 {
60 	if (ratio > 4096) {
61 		/* Down-scaling */
62 		unsigned int mp;
63 
64 		mp = ratio / 4096;
65 		mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
66 
67 		return (input - 1) / mp * mp * 4096 / ratio + 1;
68 	} else {
69 		/* Up-scaling */
70 		return (input - 1) * 4096 / ratio + 1;
71 	}
72 }
73 
74 /*
75  * uds_output_limits - Return the min and max output sizes for an input size
76  * @input: input size in pixels
77  * @minimum: minimum output size (returned)
78  * @maximum: maximum output size (returned)
79  */
uds_output_limits(unsigned int input,unsigned int * minimum,unsigned int * maximum)80 static void uds_output_limits(unsigned int input,
81 			      unsigned int *minimum, unsigned int *maximum)
82 {
83 	*minimum = max(uds_output_size(input, UDS_MAX_FACTOR), UDS_MIN_SIZE);
84 	*maximum = min(uds_output_size(input, UDS_MIN_FACTOR), UDS_MAX_SIZE);
85 }
86 
87 /*
88  * uds_passband_width - Return the passband filter width for a scaling ratio
89  * @ratio: scaling ratio in U4.12 fixed-point format
90  */
uds_passband_width(unsigned int ratio)91 static unsigned int uds_passband_width(unsigned int ratio)
92 {
93 	if (ratio >= 4096) {
94 		/* Down-scaling */
95 		unsigned int mp;
96 
97 		mp = ratio / 4096;
98 		mp = mp < 4 ? 1 : (mp < 8 ? 2 : 4);
99 
100 		return 64 * 4096 * mp / ratio;
101 	} else {
102 		/* Up-scaling */
103 		return 64;
104 	}
105 }
106 
uds_compute_ratio(unsigned int input,unsigned int output)107 static unsigned int uds_compute_ratio(unsigned int input, unsigned int output)
108 {
109 	/* TODO: This is an approximation that will need to be refined. */
110 	return (input - 1) * 4096 / (output - 1);
111 }
112 
113 /* -----------------------------------------------------------------------------
114  * V4L2 Subdevice Core Operations
115  */
116 
uds_s_stream(struct v4l2_subdev * subdev,int enable)117 static int uds_s_stream(struct v4l2_subdev *subdev, int enable)
118 {
119 	struct vsp1_uds *uds = to_uds(subdev);
120 	const struct v4l2_mbus_framefmt *output;
121 	const struct v4l2_mbus_framefmt *input;
122 	unsigned int hscale;
123 	unsigned int vscale;
124 	bool multitap;
125 
126 	if (!enable)
127 		return 0;
128 
129 	input = &uds->entity.formats[UDS_PAD_SINK];
130 	output = &uds->entity.formats[UDS_PAD_SOURCE];
131 
132 	hscale = uds_compute_ratio(input->width, output->width);
133 	vscale = uds_compute_ratio(input->height, output->height);
134 
135 	dev_dbg(uds->entity.vsp1->dev, "hscale %u vscale %u\n", hscale, vscale);
136 
137 	/* Multi-tap scaling can't be enabled along with alpha scaling when
138 	 * scaling down with a factor lower than or equal to 1/2 in either
139 	 * direction.
140 	 */
141 	if (uds->scale_alpha && (hscale >= 8192 || vscale >= 8192))
142 		multitap = false;
143 	else
144 		multitap = true;
145 
146 	vsp1_uds_write(uds, VI6_UDS_CTRL,
147 		       (uds->scale_alpha ? VI6_UDS_CTRL_AON : 0) |
148 		       (multitap ? VI6_UDS_CTRL_BC : 0));
149 
150 	vsp1_uds_write(uds, VI6_UDS_PASS_BWIDTH,
151 		       (uds_passband_width(hscale)
152 				<< VI6_UDS_PASS_BWIDTH_H_SHIFT) |
153 		       (uds_passband_width(vscale)
154 				<< VI6_UDS_PASS_BWIDTH_V_SHIFT));
155 
156 	/* Set the scaling ratios and the output size. */
157 	vsp1_uds_write(uds, VI6_UDS_SCALE,
158 		       (hscale << VI6_UDS_SCALE_HFRAC_SHIFT) |
159 		       (vscale << VI6_UDS_SCALE_VFRAC_SHIFT));
160 	vsp1_uds_write(uds, VI6_UDS_CLIP_SIZE,
161 		       (output->width << VI6_UDS_CLIP_SIZE_HSIZE_SHIFT) |
162 		       (output->height << VI6_UDS_CLIP_SIZE_VSIZE_SHIFT));
163 
164 	return 0;
165 }
166 
167 /* -----------------------------------------------------------------------------
168  * V4L2 Subdevice Pad Operations
169  */
170 
uds_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_fh * fh,struct v4l2_subdev_mbus_code_enum * code)171 static int uds_enum_mbus_code(struct v4l2_subdev *subdev,
172 			      struct v4l2_subdev_fh *fh,
173 			      struct v4l2_subdev_mbus_code_enum *code)
174 {
175 	static const unsigned int codes[] = {
176 		V4L2_MBUS_FMT_ARGB8888_1X32,
177 		V4L2_MBUS_FMT_AYUV8_1X32,
178 	};
179 
180 	if (code->pad == UDS_PAD_SINK) {
181 		if (code->index >= ARRAY_SIZE(codes))
182 			return -EINVAL;
183 
184 		code->code = codes[code->index];
185 	} else {
186 		struct v4l2_mbus_framefmt *format;
187 
188 		/* The UDS can't perform format conversion, the sink format is
189 		 * always identical to the source format.
190 		 */
191 		if (code->index)
192 			return -EINVAL;
193 
194 		format = v4l2_subdev_get_try_format(fh, UDS_PAD_SINK);
195 		code->code = format->code;
196 	}
197 
198 	return 0;
199 }
200 
uds_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_fh * fh,struct v4l2_subdev_frame_size_enum * fse)201 static int uds_enum_frame_size(struct v4l2_subdev *subdev,
202 			       struct v4l2_subdev_fh *fh,
203 			       struct v4l2_subdev_frame_size_enum *fse)
204 {
205 	struct v4l2_mbus_framefmt *format;
206 
207 	format = v4l2_subdev_get_try_format(fh, UDS_PAD_SINK);
208 
209 	if (fse->index || fse->code != format->code)
210 		return -EINVAL;
211 
212 	if (fse->pad == UDS_PAD_SINK) {
213 		fse->min_width = UDS_MIN_SIZE;
214 		fse->max_width = UDS_MAX_SIZE;
215 		fse->min_height = UDS_MIN_SIZE;
216 		fse->max_height = UDS_MAX_SIZE;
217 	} else {
218 		uds_output_limits(format->width, &fse->min_width,
219 				  &fse->max_width);
220 		uds_output_limits(format->height, &fse->min_height,
221 				  &fse->max_height);
222 	}
223 
224 	return 0;
225 }
226 
uds_get_format(struct v4l2_subdev * subdev,struct v4l2_subdev_fh * fh,struct v4l2_subdev_format * fmt)227 static int uds_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
228 			  struct v4l2_subdev_format *fmt)
229 {
230 	struct vsp1_uds *uds = to_uds(subdev);
231 
232 	fmt->format = *vsp1_entity_get_pad_format(&uds->entity, fh, fmt->pad,
233 						  fmt->which);
234 
235 	return 0;
236 }
237 
uds_try_format(struct vsp1_uds * uds,struct v4l2_subdev_fh * fh,unsigned int pad,struct v4l2_mbus_framefmt * fmt,enum v4l2_subdev_format_whence which)238 static void uds_try_format(struct vsp1_uds *uds, struct v4l2_subdev_fh *fh,
239 			   unsigned int pad, struct v4l2_mbus_framefmt *fmt,
240 			   enum v4l2_subdev_format_whence which)
241 {
242 	struct v4l2_mbus_framefmt *format;
243 	unsigned int minimum;
244 	unsigned int maximum;
245 
246 	switch (pad) {
247 	case UDS_PAD_SINK:
248 		/* Default to YUV if the requested format is not supported. */
249 		if (fmt->code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
250 		    fmt->code != V4L2_MBUS_FMT_AYUV8_1X32)
251 			fmt->code = V4L2_MBUS_FMT_AYUV8_1X32;
252 
253 		fmt->width = clamp(fmt->width, UDS_MIN_SIZE, UDS_MAX_SIZE);
254 		fmt->height = clamp(fmt->height, UDS_MIN_SIZE, UDS_MAX_SIZE);
255 		break;
256 
257 	case UDS_PAD_SOURCE:
258 		/* The UDS scales but can't perform format conversion. */
259 		format = vsp1_entity_get_pad_format(&uds->entity, fh,
260 						    UDS_PAD_SINK, which);
261 		fmt->code = format->code;
262 
263 		uds_output_limits(format->width, &minimum, &maximum);
264 		fmt->width = clamp(fmt->width, minimum, maximum);
265 		uds_output_limits(format->height, &minimum, &maximum);
266 		fmt->height = clamp(fmt->height, minimum, maximum);
267 		break;
268 	}
269 
270 	fmt->field = V4L2_FIELD_NONE;
271 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
272 }
273 
uds_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_fh * fh,struct v4l2_subdev_format * fmt)274 static int uds_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
275 			  struct v4l2_subdev_format *fmt)
276 {
277 	struct vsp1_uds *uds = to_uds(subdev);
278 	struct v4l2_mbus_framefmt *format;
279 
280 	uds_try_format(uds, fh, fmt->pad, &fmt->format, fmt->which);
281 
282 	format = vsp1_entity_get_pad_format(&uds->entity, fh, fmt->pad,
283 					    fmt->which);
284 	*format = fmt->format;
285 
286 	if (fmt->pad == UDS_PAD_SINK) {
287 		/* Propagate the format to the source pad. */
288 		format = vsp1_entity_get_pad_format(&uds->entity, fh,
289 						    UDS_PAD_SOURCE, fmt->which);
290 		*format = fmt->format;
291 
292 		uds_try_format(uds, fh, UDS_PAD_SOURCE, format, fmt->which);
293 	}
294 
295 	return 0;
296 }
297 
298 /* -----------------------------------------------------------------------------
299  * V4L2 Subdevice Operations
300  */
301 
302 static struct v4l2_subdev_video_ops uds_video_ops = {
303 	.s_stream = uds_s_stream,
304 };
305 
306 static struct v4l2_subdev_pad_ops uds_pad_ops = {
307 	.enum_mbus_code = uds_enum_mbus_code,
308 	.enum_frame_size = uds_enum_frame_size,
309 	.get_fmt = uds_get_format,
310 	.set_fmt = uds_set_format,
311 };
312 
313 static struct v4l2_subdev_ops uds_ops = {
314 	.video	= &uds_video_ops,
315 	.pad    = &uds_pad_ops,
316 };
317 
318 /* -----------------------------------------------------------------------------
319  * Initialization and Cleanup
320  */
321 
vsp1_uds_create(struct vsp1_device * vsp1,unsigned int index)322 struct vsp1_uds *vsp1_uds_create(struct vsp1_device *vsp1, unsigned int index)
323 {
324 	struct v4l2_subdev *subdev;
325 	struct vsp1_uds *uds;
326 	int ret;
327 
328 	uds = devm_kzalloc(vsp1->dev, sizeof(*uds), GFP_KERNEL);
329 	if (uds == NULL)
330 		return ERR_PTR(-ENOMEM);
331 
332 	uds->entity.type = VSP1_ENTITY_UDS;
333 	uds->entity.index = index;
334 
335 	ret = vsp1_entity_init(vsp1, &uds->entity, 2);
336 	if (ret < 0)
337 		return ERR_PTR(ret);
338 
339 	/* Initialize the V4L2 subdev. */
340 	subdev = &uds->entity.subdev;
341 	v4l2_subdev_init(subdev, &uds_ops);
342 
343 	subdev->entity.ops = &vsp1_media_ops;
344 	subdev->internal_ops = &vsp1_subdev_internal_ops;
345 	snprintf(subdev->name, sizeof(subdev->name), "%s uds.%u",
346 		 dev_name(vsp1->dev), index);
347 	v4l2_set_subdevdata(subdev, uds);
348 	subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
349 
350 	vsp1_entity_init_formats(subdev, NULL);
351 
352 	return uds;
353 }
354