Skip to contentSkip to conversation

media: platform: Add Aspeed Video Engine Driver

PATCH Superseded Eddie James 3 participants 10 messages Oct 5, 2018 – Nov 9, 2018

Subsystems (3)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover media: platform: Add Aspeed Video Engine Driver

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting as a service processor, the Video Engine can capture the host processor graphics output.

This series adds a V4L2 driver for the VE, providing the usual V4L2 streaming interface by way of videobuf2. Each frame, the driver triggers the hardware to capture the host graphics output and compress it to JPEG format.

v4l-utils HEAD 3598047c70e395d8ee0da5519996bc0126f18262

v4l2-compliance SHA: not available, 32 bits

Compliance test for device /dev/video0:

Driver Info:

	Driver name      : aspeed-video
	Card type        : Aspeed Video Engine
	Bus info         : platform:aspeed-video
	Driver version   : 4.18.8
	Capabilities     : 0x85200001

Video Capture
Read/Write
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x05200001
Video Capture
Read/Write
Streaming
Extended Pix Format

Required ioctls:
test VIDIOC_QUERYCAP: OK

Allow for multiple opens:
test second /dev/video0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK

Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK
test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 1 Audio Inputs: 0 Tuners: 0

Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK test VIDIOC_DV_TIMINGS_CAP: OK
test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls (Input 0):
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK
warn: v4l2-test-controls.cpp(845): V4L2_CID_DV_RX_POWER_PRESENT not found for input 0
test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK
test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
Standard Controls: 3 Private Controls: 0

Format ioctls (Input 0):
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK
test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)

Codec ioctls (Input 0):
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls (Input 0):
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test VIDIOC_EXPBUF: OK (Not Supported)

Test input 0:

Streaming ioctls:
test read/write: OK
test blocking wait: OK
test MMAP: OK
test USERPTR: OK (Not Supported)
test DMABUF: Cannot test, specify --expbuf-device

Total: 47, Succeeded: 47, Failed: 0, Warnings: 1

Changes since v3:
- Switch update reg function to use u32 clear rather than unsigned long mask
- Add timing information from host VGA signal
- Fix binding documentation mispelling
- Fix upper case hex values
- Add wait_prepare and wait_finish
- Set buffer state to queued (rather than error) if streaming fails to start
- Switch engine busy print statement to error
- Removed a couple unecessary type assignments in v4l2 ioctls
- Added query_dv_timings, fixed get_dv_timings
- Corrected source change event to fire if and only if size actually changes
- Locked open and release

Changes since v2:
- Switch to streaming interface. This involved a lot of changes.
- Rework memory allocation due to using videobuf2 buffers, but also only allocate the necessary size of source buffer rather than the max size

Changes since v1:
- Removed le32_to_cpu calls for JPEG header data
- Reworked v4l2 ioctls to be compliant.
- Added JPEG controls
- Updated devicetree docs according to Rob's suggestions.
- Added myself to MAINTAINERS

Eddie James (2):
dt-bindings: media: Add Aspeed Video Engine binding documentation
media: platform: Add Aspeed Video Engine driver

 .../devicetree/bindings/media/aspeed-video.txt     |   26 +
 MAINTAINERS                                        |    8 +
 drivers/media/platform/Kconfig                     |    8 +
 drivers/media/platform/Makefile                    |    1 +
 drivers/media/platform/aspeed-video.c              | 1674 ++++++++++++++++++++
 5 files changed, 1717 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/aspeed-video.txt
 create mode 100644 drivers/media/platform/aspeed-video.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v4 1/2 dt-bindings: media: Add Aspeed Video Engine binding documentation

Document the bindings.

---
 .../devicetree/bindings/media/aspeed-video.txt     | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/aspeed-video.txt
Documentation/devicetree/bindings/media/aspeed-video.txt new file +26 -0
new file mode 100644
index 0000000..78b464a
@@ -0,0 +1,26 @@
+* Device tree bindings for Aspeed Video Engine
+
+The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can
+capture and compress video data from digital or analog sources.
+
+Required properties:
+ - compatible:		"aspeed,ast2400-video-engine" or
+			"aspeed,ast2500-video-engine"
+ - reg:			contains the offset and length of the VE memory region
+ - clocks:		clock specifiers for the syscon clocks associated with
+			the VE (ordering must match the clock-names property)
+ - clock-names:		"vclk" and "eclk"
+ - resets:		reset specifier for the syscon reset associated with
+			the VE
+ - interrupts:		the interrupt associated with the VE on this platform
+
+Example:
+
+video-engine at 1e700000 {
+    compatible = "aspeed,ast2500-video-engine";
+    reg = <0x1e700000 0x20000>;
+    clocks = <&syscon ASPEED_CLK_GATE_VCLK>, <&syscon ASPEED_CLK_GATE_ECLK>;
+    clock-names = "vclk", "eclk";
+    resets = <&syscon ASPEED_RESET_VIDEO>;
+    interrupts = <7>;
+};
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v4 2/2 media: platform: Add Aspeed Video Engine driver

In reply to Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting a service processor, the Video Engine can capture the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
 MAINTAINERS                           |    8 +
 drivers/media/platform/Kconfig        |    8 +
 drivers/media/platform/Makefile       |    1 +
 drivers/media/platform/aspeed-video.c | 1674 +++++++++++++++++++++++++++++++++
 4 files changed, 1691 insertions(+)
 create mode 100644 drivers/media/platform/aspeed-video.c
MAINTAINERS +8 -0
index 29c0810..9b53694 100644
@@ -2407,6 +2407,14 @@ S:	Maintained
 F:	Documentation/hwmon/asc7621
 F:	drivers/hwmon/asc7621.c
 
+ASPEED VIDEO ENGINE DRIVER
+M:	Eddie James <[email protected]>
+L:	linux-media at vger.kernel.org
+L:	openbmc at lists.ozlabs.org (moderated for non-subscribers)
+S:	Maintained
+F:	drivers/media/platform/aspeed-video.c
+F:	Documentation/devicetree/bindings/media/aspeed-video.txt
+
 ASUS NOTEBOOKS AND EEEPC ACPI/WMI EXTRAS DRIVERS
 M:	Corentin Chary <[email protected]>
 L:	acpi4asus-user at lists.sourceforge.net
drivers/media/platform/Kconfig +8 -0
index 936675d..f211253 100644
@@ -32,6 +32,14 @@ source "drivers/media/platform/davinci/Kconfig"
 
 source "drivers/media/platform/omap/Kconfig"
 
+config VIDEO_ASPEED
+	tristate "Aspeed AST2400 and AST2500 Video Engine driver"
+	depends on VIDEO_V4L2
+	help
+	  Support for the Aspeed Video Engine (VE) embedded in the Aspeed
+	  AST2400 and AST2500 SOCs. The VE can capture and compress video data
+	  from digital or analog sources.
+
 config VIDEO_SH_VOU
 	tristate "SuperH VOU video output driver"
 	depends on MEDIA_CAMERA_SUPPORT
drivers/media/platform/Makefile +1 -0
index 6ab6200..2973953 100644
@@ -3,6 +3,7 @@
 # Makefile for the video capture/playback device drivers.
 #
 
+obj-$(CONFIG_VIDEO_ASPEED)		+= aspeed-video.o
 obj-$(CONFIG_VIDEO_CADENCE)		+= cadence/
 obj-$(CONFIG_VIDEO_VIA_CAMERA) += via-camera.o
 obj-$(CONFIG_VIDEO_CAFE_CCIC) += marvell-ccic/
drivers/media/platform/aspeed-video.c new file +1674 -0

1,674 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

Quoted 17 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting a service processor, the Video Engine can capture the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
 MAINTAINERS                           |    8 +
 drivers/media/platform/Kconfig        |    8 +
 drivers/media/platform/Makefile       |    1 +
 drivers/media/platform/aspeed-video.c | 1674 +++++++++++++++++++++++++++++++++
 4 files changed, 1691 insertions(+)
 create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

drivers/media/platform/aspeed-video.c line 985

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+static int aspeed_video_enum_input(struct file *file, void *fh,
+				   struct v4l2_input *inp)
+{
+	if (inp->index)
+		return -EINVAL;
+
+	strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
+	inp->type = V4L2_INPUT_TYPE_CAMERA;
+	inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
+	inp->status = V4L2_IN_ST_NO_SIGNAL | V4L2_IN_ST_NO_SYNC;

This can't be right. If there is a valid signal, then status should be 0. And ideally you can tell the difference between no signal and no sync as well.

Quoted 44 lines
drivers/media/platform/aspeed-video.c new file +44 -0
+
+	return 0;
+}
+
+static int aspeed_video_get_input(struct file *file, void *fh, unsigned int *i)
+{
+	*i = 0;
+
+	return 0;
+}
+
+static int aspeed_video_set_input(struct file *file, void *fh, unsigned int i)
+{
+	if (i)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int aspeed_video_get_parm(struct file *file, void *fh,
+				 struct v4l2_streamparm *a)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+	a->parm.capture.readbuffers = 3;
+	a->parm.capture.timeperframe.numerator = 1;
+	if (!video->frame_rate)
+		a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;
+	else
+		a->parm.capture.timeperframe.denominator = video->frame_rate;
+
+	return 0;
+}
+
+static int aspeed_video_set_parm(struct file *file, void *fh,
+				 struct v4l2_streamparm *a)
+{
+	unsigned int frame_rate = 0;
+	struct aspeed_video *video = video_drvdata(file);
+
+	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+	a->parm.capture.readbuffers = 3;
+

drivers/media/platform/aspeed-video.c line 1039

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	if (a->parm.capture.timeperframe.numerator)
+		frame_rate = a->parm.capture.timeperframe.denominator /
+			a->parm.capture.timeperframe.numerator;
+
+	if (!frame_rate || frame_rate > MAX_FRAME_RATE) {
+		frame_rate = 0;
+
+		/*
+		 * Set to max + 1 to differentiate between max and 0, which
+		 * means "don't care".

But what does "don't care" mean in practice? It's still not clear to me how this is supposed to work.

drivers/media/platform/aspeed-video.c line 1041

Quoted 2 lines
drivers/media/platform/aspeed-video.c new file +2 -0
+		 */
+		a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;

And regardless of anything else this timeperframe is out of the range that aspeed_video_enum_frameintervals() returns.

Quoted 50 lines
drivers/media/platform/aspeed-video.c new file +50 -0
+		a->parm.capture.timeperframe.numerator = 1;
+	}
+
+	if (video->frame_rate != frame_rate) {
+		video->frame_rate = frame_rate;
+		aspeed_video_update(video, VE_CTRL, VE_CTRL_FRC,
+				    FIELD_PREP(VE_CTRL_FRC, frame_rate));
+	}
+
+	return 0;
+}
+
+static int aspeed_video_enum_framesizes(struct file *file, void *fh,
+					struct v4l2_frmsizeenum *fsize)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (fsize->index)
+		return -EINVAL;
+
+	if (fsize->pixel_format != V4L2_PIX_FMT_JPEG)
+		return -EINVAL;
+
+	fsize->discrete.width = video->pix_fmt.width;
+	fsize->discrete.height = video->pix_fmt.height;
+	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+
+	return 0;
+}
+
+static int aspeed_video_enum_frameintervals(struct file *file, void *fh,
+					    struct v4l2_frmivalenum *fival)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (fival->index)
+		return -EINVAL;
+
+	if (fival->width != video->width || fival->height != video->height)
+		return -EINVAL;
+
+	if (fival->pixel_format != V4L2_PIX_FMT_JPEG)
+		return -EINVAL;
+
+	fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
+
+	fival->stepwise.min.denominator = MAX_FRAME_RATE;
+	fival->stepwise.min.numerator = 1;
+	fival->stepwise.max.denominator = 1;
+	fival->stepwise.max.numerator = 1;

drivers/media/platform/aspeed-video.c line 1101

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	fival->stepwise.step = fival->stepwise.max;
+
+	return 0;
+}
+
+static int aspeed_video_set_dv_timings(struct file *file, void *fh,
+				       struct v4l2_dv_timings *timings)
+{
+	struct aspeed_video *video = video_drvdata(file);
+

If vb2_is_busy() returns true, then return -EBUSY here. It is not allowed to set the timings while vb2 is busy.

Quoted 61 lines
drivers/media/platform/aspeed-video.c new file +61 -0
+	if (video->width != timings->bt.width ||
+	    video->height != timings->bt.height)
+		return -EINVAL;
+
+	video->pix_fmt.width = timings->bt.width;
+	video->pix_fmt.height = timings->bt.height;
+	video->pix_fmt.sizeimage = video->max_compressed_size;
+	video->timings.width = timings->bt.width;
+	video->timings.height = timings->bt.height;
+
+	timings->type = V4L2_DV_BT_656_1120;
+
+	return 0;
+}
+
+static int aspeed_video_get_dv_timings(struct file *file, void *fh,
+				       struct v4l2_dv_timings *timings)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	timings->type = V4L2_DV_BT_656_1120;
+	timings->bt = video->timings;
+
+	return 0;
+}
+
+static int aspeed_video_query_dv_timings(struct file *file, void *fh,
+					 struct v4l2_dv_timings *timings)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (file->f_flags & O_NONBLOCK) {
+		if (test_bit(VIDEO_RES_CHANGE, &video->flags))
+			return -EAGAIN;
+	} else {
+		rc = wait_event_interruptible(video->wait,
+					      !test_bit(VIDEO_RES_CHANGE,
+							&video->flags));
+		if (rc)
+			return -EINTR;
+	}
+
+	timings->type = V4L2_DV_BT_656_1120;
+	timings->bt = video->timings;
+	timings->bt.width = video->width;
+	timings->bt.height = video->height;
+
+	return 0;
+}
+
+static int aspeed_video_enum_dv_timings(struct file *file, void *fh,
+					struct v4l2_enum_dv_timings *timings)
+{
+	if (timings->index)
+		return -EINVAL;
+
+	return aspeed_video_get_dv_timings(file, fh, &timings->timings);
+}
+
+static int aspeed_video_dv_timings_cap(struct file *file, void *fh,

drivers/media/platform/aspeed-video.c line 1172

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+				       struct v4l2_dv_timings_cap *cap)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	cap->type = V4L2_DV_BT_656_1120;
+	cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
+	cap->bt.min_width = video->width;
+	cap->bt.max_width = video->width;
+	cap->bt.min_height = video->height;
+	cap->bt.max_height = video->height;

This should return the capabilities of the aspeed. In this case I'd guess that the max width/height is 1920x1080 (or perhaps 1200).

The minimum is probably the VGA resolution.

Quoted 330 lines
drivers/media/platform/aspeed-video.c new file +330 -0
+
+	return 0;
+}
+
+static int aspeed_video_sub_event(struct v4l2_fh *fh,
+				  const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_src_change_event_subscribe(fh, sub);
+	}
+
+	return v4l2_ctrl_subscribe_event(fh, sub);
+}
+
+static const struct v4l2_ioctl_ops aspeed_video_ioctl_ops = {
+	.vidioc_querycap = aspeed_video_querycap,
+
+	.vidioc_enum_fmt_vid_cap = aspeed_video_enum_format,
+	.vidioc_g_fmt_vid_cap = aspeed_video_get_format,
+	.vidioc_s_fmt_vid_cap = aspeed_video_get_format,
+	.vidioc_try_fmt_vid_cap = aspeed_video_get_format,
+
+	.vidioc_reqbufs = vb2_ioctl_reqbufs,
+	.vidioc_querybuf = vb2_ioctl_querybuf,
+	.vidioc_qbuf = vb2_ioctl_qbuf,
+	.vidioc_dqbuf = vb2_ioctl_dqbuf,
+	.vidioc_create_bufs = vb2_ioctl_create_bufs,
+	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
+	.vidioc_streamon = vb2_ioctl_streamon,
+	.vidioc_streamoff = vb2_ioctl_streamoff,
+
+	.vidioc_enum_input = aspeed_video_enum_input,
+	.vidioc_g_input = aspeed_video_get_input,
+	.vidioc_s_input = aspeed_video_set_input,
+
+	.vidioc_g_parm = aspeed_video_get_parm,
+	.vidioc_s_parm = aspeed_video_set_parm,
+	.vidioc_enum_framesizes = aspeed_video_enum_framesizes,
+	.vidioc_enum_frameintervals = aspeed_video_enum_frameintervals,
+
+	.vidioc_s_dv_timings = aspeed_video_set_dv_timings,
+	.vidioc_g_dv_timings = aspeed_video_get_dv_timings,
+	.vidioc_query_dv_timings = aspeed_video_query_dv_timings,
+	.vidioc_enum_dv_timings = aspeed_video_enum_dv_timings,
+	.vidioc_dv_timings_cap = aspeed_video_dv_timings_cap,
+
+	.vidioc_subscribe_event = aspeed_video_sub_event,
+	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+};
+
+static void aspeed_video_update_jpeg_quality(struct aspeed_video *video)
+{
+	u32 comp_ctrl = FIELD_PREP(VE_COMP_CTRL_DCT_LUM, video->jpeg_quality) |
+		FIELD_PREP(VE_COMP_CTRL_DCT_CHR, video->jpeg_quality | 0x10);
+
+	aspeed_video_update(video, VE_COMP_CTRL,
+			    VE_COMP_CTRL_DCT_LUM | VE_COMP_CTRL_DCT_CHR,
+			    comp_ctrl);
+}
+
+static void aspeed_video_update_subsampling(struct aspeed_video *video)
+{
+	if (video->jpeg.virt)
+		aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+	if (video->yuv420)
+		aspeed_video_update(video, VE_SEQ_CTRL, 0, VE_SEQ_CTRL_YUV420);
+	else
+		aspeed_video_update(video, VE_SEQ_CTRL, VE_SEQ_CTRL_YUV420, 0);
+}
+
+static int aspeed_video_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct aspeed_video *video = container_of(ctrl->handler,
+						  struct aspeed_video,
+						  ctrl_handler);
+
+	switch (ctrl->id) {
+	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
+		video->jpeg_quality = ctrl->val;
+		aspeed_video_update_jpeg_quality(video);
+		break;
+	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
+		if (ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
+			video->yuv420 = true;
+			aspeed_video_update_subsampling(video);
+		} else {
+			video->yuv420 = false;
+			aspeed_video_update_subsampling(video);
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops aspeed_video_ctrl_ops = {
+	.s_ctrl = aspeed_video_set_ctrl,
+};
+
+static void aspeed_video_resolution_work(struct work_struct *work)
+{
+	int rc;
+	struct delayed_work *dwork = to_delayed_work(work);
+	struct aspeed_video *video = container_of(dwork, struct aspeed_video,
+						  res_work);
+
+	/* No clients remaining after delay */
+	if (atomic_read(&video->clients) == 0)
+		goto done;
+
+	aspeed_video_on(video);
+
+	aspeed_video_init_regs(video);
+
+	rc = aspeed_video_get_resolution(video);
+	if (rc)
+		dev_err(video->dev,
+			"resolution changed; couldn't get new resolution\n");
+	else if (test_bit(VIDEO_STREAMING, &video->flags))
+		aspeed_video_start_frame(video);
+
+	if (video->width != video->pix_fmt.width ||
+	    video->height != video->pix_fmt.height) {
+		static const struct v4l2_event ev = {
+			.type = V4L2_EVENT_SOURCE_CHANGE,
+			.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
+		};
+
+		v4l2_event_queue(&video->vdev, &ev);
+	}
+
+done:
+	clear_bit(VIDEO_RES_CHANGE, &video->flags);
+	wake_up_interruptible_all(&video->wait);
+}
+
+static int aspeed_video_open(struct file *file)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	mutex_lock(&video->video_lock);
+
+	if (atomic_inc_return(&video->clients) == 1) {
+		rc = aspeed_video_start(video);
+		if (rc) {
+			dev_err(video->dev, "Failed to start video engine\n");
+			atomic_dec(&video->clients);
+			mutex_unlock(&video->video_lock);
+			return rc;
+		}
+	}
+
+	mutex_unlock(&video->video_lock);
+
+	return v4l2_fh_open(file);
+}
+
+static int aspeed_video_release(struct file *file)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	rc = vb2_fop_release(file);
+
+	mutex_lock(&video->video_lock);
+
+	if (atomic_dec_return(&video->clients) == 0)
+		aspeed_video_stop(video);
+
+	mutex_unlock(&video->video_lock);
+
+	return rc;
+}
+
+static const struct v4l2_file_operations aspeed_video_v4l2_fops = {
+	.owner = THIS_MODULE,
+	.read = vb2_fop_read,
+	.poll = vb2_fop_poll,
+	.unlocked_ioctl = video_ioctl2,
+	.mmap = vb2_fop_mmap,
+	.open = aspeed_video_open,
+	.release = aspeed_video_release,
+};
+
+static int aspeed_video_queue_setup(struct vb2_queue *q,
+				    unsigned int *num_buffers,
+				    unsigned int *num_planes,
+				    unsigned int sizes[],
+				    struct device *alloc_devs[])
+{
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	if (*num_planes) {
+		if (sizes[0] < video->max_compressed_size)
+			return -EINVAL;
+
+		return 0;
+	}
+
+	*num_planes = 1;
+	sizes[0] = video->max_compressed_size;
+
+	return 0;
+}
+
+static int aspeed_video_buf_prepare(struct vb2_buffer *vb)
+{
+	struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+
+	if (vb2_plane_size(vb, 0) < video->max_compressed_size)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int aspeed_video_start_streaming(struct vb2_queue *q,
+					unsigned int count)
+{
+	int rc;
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	rc = aspeed_video_start_frame(video);
+	if (rc) {
+		aspeed_video_bufs_done(video, VB2_BUF_STATE_QUEUED);
+		return rc;
+	}
+
+	video->sequence = 0;
+	set_bit(VIDEO_STREAMING, &video->flags);
+	return 0;
+}
+
+static void aspeed_video_stop_streaming(struct vb2_queue *q)
+{
+	int rc;
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	clear_bit(VIDEO_STREAMING, &video->flags);
+
+	rc = wait_event_timeout(video->wait,
+				!test_bit(VIDEO_FRAME_INPRG, &video->flags),
+				STOP_TIMEOUT);
+	if (!rc) {
+		dev_err(video->dev, "Timed out when stopping streaming\n");
+		aspeed_video_stop(video);
+	}
+
+	aspeed_video_bufs_done(video, VB2_BUF_STATE_ERROR);
+}
+
+static void aspeed_video_buf_queue(struct vb2_buffer *vb)
+{
+	struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct aspeed_video_buffer *avb = to_aspeed_video_buffer(vbuf);
+	unsigned long flags;
+
+	spin_lock_irqsave(&video->lock, flags);
+	list_add_tail(&avb->link, &video->buffers);
+	spin_unlock_irqrestore(&video->lock, flags);
+}
+
+static const struct vb2_ops aspeed_video_vb2_ops = {
+	.queue_setup = aspeed_video_queue_setup,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+	.buf_prepare = aspeed_video_buf_prepare,
+	.start_streaming = aspeed_video_start_streaming,
+	.stop_streaming = aspeed_video_stop_streaming,
+	.buf_queue =  aspeed_video_buf_queue,
+};
+
+static int aspeed_video_setup_video(struct aspeed_video *video)
+{
+	const u64 mask = ~(BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_444) |
+			   BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_420));
+	struct v4l2_device *v4l2_dev = &video->v4l2_dev;
+	struct vb2_queue *vbq = &video->queue;
+	struct video_device *vdev = &video->vdev;
+	int rc;
+
+	video->pix_fmt.pixelformat = V4L2_PIX_FMT_JPEG;
+	video->pix_fmt.field = V4L2_FIELD_NONE;
+	video->pix_fmt.colorspace = V4L2_COLORSPACE_SRGB;
+	video->pix_fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
+
+	rc = v4l2_device_register(video->dev, v4l2_dev);
+	if (rc) {
+		dev_err(video->dev, "Failed to register v4l2 device\n");
+		return rc;
+	}
+
+	v4l2_ctrl_handler_init(&video->ctrl_handler, 2);
+	v4l2_ctrl_new_std(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
+			  ASPEED_VIDEO_JPEG_NUM_QUALITIES - 1, 1, 0);
+	v4l2_ctrl_new_std_menu(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+			       V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
+			       V4L2_JPEG_CHROMA_SUBSAMPLING_420, mask,
+			       V4L2_JPEG_CHROMA_SUBSAMPLING_444);
+
+	if (video->ctrl_handler.error) {
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to init controls: %d\n",
+			video->ctrl_handler.error);
+		return rc;
+	}
+
+	v4l2_dev->ctrl_handler = &video->ctrl_handler;
+
+	vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	vbq->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
+	vbq->dev = v4l2_dev->dev;
+	vbq->lock = &video->video_lock;
+	vbq->ops = &aspeed_video_vb2_ops;
+	vbq->mem_ops = &vb2_dma_contig_memops;
+	vbq->drv_priv = video;
+	vbq->buf_struct_size = sizeof(struct aspeed_video_buffer);
+	vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	vbq->min_buffers_needed = 3;
+
+	rc = vb2_queue_init(vbq);
+	if (rc) {

drivers/media/platform/aspeed-video.c line 1512

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to init vb2 queue\n");
+		return rc;
+	}
+
+	vdev->queue = vbq;
+	vdev->fops = &aspeed_video_v4l2_fops;
+	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |

READWRITE doesn't work for JPEG since there is no clear end of a frame. Just drop this and the read op in aspeed_video_v4l2_fops.

Quoted 153 lines
drivers/media/platform/aspeed-video.c new file +152 -0
+		V4L2_CAP_STREAMING;
+	vdev->v4l2_dev = v4l2_dev;
+	strscpy(vdev->name, DEVICE_NAME, sizeof(vdev->name));
+	vdev->vfl_type = VFL_TYPE_GRABBER;
+	vdev->vfl_dir = VFL_DIR_RX;
+	vdev->release = video_device_release_empty;
+	vdev->ioctl_ops = &aspeed_video_ioctl_ops;
+	vdev->lock = &video->video_lock;
+
+	video_set_drvdata(vdev, video);
+	rc = video_register_device(vdev, VFL_TYPE_GRABBER, 0);
+	if (rc) {
+		vb2_queue_release(vbq);
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to register video device\n");
+		return rc;
+	}
+
+	return 0;
+}
+
+static int aspeed_video_init(struct aspeed_video *video)
+{
+	int irq;
+	int rc;
+	struct device *dev = video->dev;
+
+	irq = irq_of_parse_and_map(dev->of_node, 0);
+	if (!irq) {
+		dev_err(dev, "Unable to find IRQ\n");
+		return -ENODEV;
+	}
+
+	rc = devm_request_irq(dev, irq, aspeed_video_irq, IRQF_SHARED,
+			      DEVICE_NAME, video);
+	if (rc < 0) {
+		dev_err(dev, "Unable to request IRQ %d\n", irq);
+		return rc;
+	}
+
+	video->eclk = devm_clk_get(dev, "eclk");
+	if (IS_ERR(video->eclk)) {
+		dev_err(dev, "Unable to get ECLK\n");
+		return PTR_ERR(video->eclk);
+	}
+
+	video->vclk = devm_clk_get(dev, "vclk");
+	if (IS_ERR(video->vclk)) {
+		dev_err(dev, "Unable to get VCLK\n");
+		return PTR_ERR(video->vclk);
+	}
+
+	video->rst = devm_reset_control_get_exclusive(dev, NULL);
+	if (IS_ERR(video->rst)) {
+		dev_err(dev, "Unable to get VE reset\n");
+		return PTR_ERR(video->rst);
+	}
+
+	rc = of_reserved_mem_device_init(dev);
+	if (rc) {
+		dev_err(dev, "Unable to reserve memory\n");
+		return rc;
+	}
+
+	rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+	if (rc) {
+		dev_err(dev, "Failed to set DMA mask\n");
+		of_reserved_mem_device_release(dev);
+		return rc;
+	}
+
+	if (!aspeed_video_alloc_buf(video, &video->jpeg,
+				    VE_JPEG_HEADER_SIZE)) {
+		dev_err(dev, "Failed to allocate DMA for JPEG header\n");
+		of_reserved_mem_device_release(dev);
+		return rc;
+	}
+
+	aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+	return 0;
+}
+
+static int aspeed_video_probe(struct platform_device *pdev)
+{
+	int rc;
+	struct resource *res;
+	struct aspeed_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
+
+	if (!video)
+		return -ENOMEM;
+
+	video->frame_rate = 30;
+	video->dev = &pdev->dev;
+	mutex_init(&video->video_lock);
+	init_waitqueue_head(&video->wait);
+	INIT_DELAYED_WORK(&video->res_work, aspeed_video_resolution_work);
+	INIT_LIST_HEAD(&video->buffers);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+
+	video->base = devm_ioremap_resource(video->dev, res);
+
+	if (IS_ERR(video->base))
+		return PTR_ERR(video->base);
+
+	rc = aspeed_video_init(video);
+	if (rc)
+		return rc;
+
+	rc = aspeed_video_setup_video(video);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+
+static int aspeed_video_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
+	struct aspeed_video *video = to_aspeed_video(v4l2_dev);
+
+	video_unregister_device(&video->vdev);
+
+	vb2_queue_release(&video->queue);
+
+	v4l2_ctrl_handler_free(&video->ctrl_handler);
+
+	v4l2_device_unregister(v4l2_dev);
+
+	dma_free_coherent(video->dev, VE_JPEG_HEADER_SIZE, video->jpeg.virt,
+			  video->jpeg.dma);
+
+	of_reserved_mem_device_release(dev);
+
+	return 0;
+}
+
+static const struct of_device_id aspeed_video_of_match[] = {
+	{ .compatible = "aspeed,ast2400-video-engine" },
+	{ .compatible = "aspeed,ast2500-video-engine" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, aspeed_video_of_match);
+
+static struct platform_driver aspeed_video_driver = {
+	.driver = {
+		.name = DEVICE_NAME,
+		.of_match_table = aspeed_video_of_match,

drivers/media/platform/aspeed-video.c line 1674

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	},
+	.probe = aspeed_video_probe,
+	.remove = aspeed_video_remove,
+};
+
+module_platform_driver(aspeed_video_driver);
+
+MODULE_DESCRIPTION("ASPEED Video Engine Driver");
+MODULE_AUTHOR("Eddie James");
+MODULE_LICENSE("GPL v2");

Regards,

Hans

#4 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

Quoted 20 lines · Hans Verkuil
Quoted 17 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting a service processor, the Video Engine can capture the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
  MAINTAINERS                           |    8 +
  drivers/media/platform/Kconfig        |    8 +
  drivers/media/platform/Makefile       |    1 +
  drivers/media/platform/aspeed-video.c | 1674 +++++++++++++++++++++++++++++++++
  4 files changed, 1691 insertions(+)

create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

Quoted 13 lines

drivers/media/platform/aspeed-video.c line 985

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+static int aspeed_video_enum_input(struct file *file, void *fh,
+				   struct v4l2_input *inp)
+{
+	if (inp->index)
+		return -EINVAL;
+
+	strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
+	inp->type = V4L2_INPUT_TYPE_CAMERA;
+	inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
+	inp->status = V4L2_IN_ST_NO_SIGNAL | V4L2_IN_ST_NO_SYNC;

This can't be right. If there is a valid signal, then status should be 0. And ideally you can tell the difference between no signal and no sync as well.

Ah, yes I misunderstood the status, thanks.

Quoted 200 lines
Quoted 45 lines
drivers/media/platform/aspeed-video.c new file +44 -0
+
+	return 0;
+}
+
+static int aspeed_video_get_input(struct file *file, void *fh, unsigned int *i)
+{
+	*i = 0;
+
+	return 0;
+}
+
+static int aspeed_video_set_input(struct file *file, void *fh, unsigned int i)
+{
+	if (i)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int aspeed_video_get_parm(struct file *file, void *fh,
+				 struct v4l2_streamparm *a)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+	a->parm.capture.readbuffers = 3;
+	a->parm.capture.timeperframe.numerator = 1;
+	if (!video->frame_rate)
+		a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;
+	else
+		a->parm.capture.timeperframe.denominator = video->frame_rate;
+
+	return 0;
+}
+
+static int aspeed_video_set_parm(struct file *file, void *fh,
+				 struct v4l2_streamparm *a)
+{
+	unsigned int frame_rate = 0;
+	struct aspeed_video *video = video_drvdata(file);
+
+	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+	a->parm.capture.readbuffers = 3;
+

drivers/media/platform/aspeed-video.c line 1039

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	if (a->parm.capture.timeperframe.numerator)
+		frame_rate = a->parm.capture.timeperframe.denominator /
+			a->parm.capture.timeperframe.numerator;
+
+	if (!frame_rate || frame_rate > MAX_FRAME_RATE) {
+		frame_rate = 0;
+
+		/*
+		 * Set to max + 1 to differentiate between max and 0, which
+		 * means "don't care".

But what does "don't care" mean in practice? It's still not clear to me how this is supposed to work.

drivers/media/platform/aspeed-video.c line 1041

Quoted 2 lines
drivers/media/platform/aspeed-video.c new file +2 -0
+		 */
+		a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;

And regardless of anything else this timeperframe is out of the range that aspeed_video_enum_frameintervals() returns.

Quoted 50 lines
drivers/media/platform/aspeed-video.c new file +50 -0
+		a->parm.capture.timeperframe.numerator = 1;
+	}
+
+	if (video->frame_rate != frame_rate) {
+		video->frame_rate = frame_rate;
+		aspeed_video_update(video, VE_CTRL, VE_CTRL_FRC,
+				    FIELD_PREP(VE_CTRL_FRC, frame_rate));
+	}
+
+	return 0;
+}
+
+static int aspeed_video_enum_framesizes(struct file *file, void *fh,
+					struct v4l2_frmsizeenum *fsize)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (fsize->index)
+		return -EINVAL;
+
+	if (fsize->pixel_format != V4L2_PIX_FMT_JPEG)
+		return -EINVAL;
+
+	fsize->discrete.width = video->pix_fmt.width;
+	fsize->discrete.height = video->pix_fmt.height;
+	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+
+	return 0;
+}
+
+static int aspeed_video_enum_frameintervals(struct file *file, void *fh,
+					    struct v4l2_frmivalenum *fival)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (fival->index)
+		return -EINVAL;
+
+	if (fival->width != video->width || fival->height != video->height)
+		return -EINVAL;
+
+	if (fival->pixel_format != V4L2_PIX_FMT_JPEG)
+		return -EINVAL;
+
+	fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
+
+	fival->stepwise.min.denominator = MAX_FRAME_RATE;
+	fival->stepwise.min.numerator = 1;
+	fival->stepwise.max.denominator = 1;
+	fival->stepwise.max.numerator = 1;

drivers/media/platform/aspeed-video.c line 1101

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	fival->stepwise.step = fival->stepwise.max;
+
+	return 0;
+}
+
+static int aspeed_video_set_dv_timings(struct file *file, void *fh,
+				       struct v4l2_dv_timings *timings)
+{
+	struct aspeed_video *video = video_drvdata(file);
+

If vb2_is_busy() returns true, then return -EBUSY here. It is not allowed to set the timings while vb2 is busy.

Quoted 61 lines
drivers/media/platform/aspeed-video.c new file +61 -0
+	if (video->width != timings->bt.width ||
+	    video->height != timings->bt.height)
+		return -EINVAL;
+
+	video->pix_fmt.width = timings->bt.width;
+	video->pix_fmt.height = timings->bt.height;
+	video->pix_fmt.sizeimage = video->max_compressed_size;
+	video->timings.width = timings->bt.width;
+	video->timings.height = timings->bt.height;
+
+	timings->type = V4L2_DV_BT_656_1120;
+
+	return 0;
+}
+
+static int aspeed_video_get_dv_timings(struct file *file, void *fh,
+				       struct v4l2_dv_timings *timings)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	timings->type = V4L2_DV_BT_656_1120;
+	timings->bt = video->timings;
+
+	return 0;
+}
+
+static int aspeed_video_query_dv_timings(struct file *file, void *fh,
+					 struct v4l2_dv_timings *timings)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (file->f_flags & O_NONBLOCK) {
+		if (test_bit(VIDEO_RES_CHANGE, &video->flags))
+			return -EAGAIN;
+	} else {
+		rc = wait_event_interruptible(video->wait,
+					      !test_bit(VIDEO_RES_CHANGE,
+							&video->flags));
+		if (rc)
+			return -EINTR;
+	}
+
+	timings->type = V4L2_DV_BT_656_1120;
+	timings->bt = video->timings;
+	timings->bt.width = video->width;
+	timings->bt.height = video->height;
+
+	return 0;
+}
+
+static int aspeed_video_enum_dv_timings(struct file *file, void *fh,
+					struct v4l2_enum_dv_timings *timings)
+{
+	if (timings->index)
+		return -EINVAL;
+
+	return aspeed_video_get_dv_timings(file, fh, &timings->timings);
+}
+
+static int aspeed_video_dv_timings_cap(struct file *file, void *fh,

drivers/media/platform/aspeed-video.c line 1172

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+				       struct v4l2_dv_timings_cap *cap)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	cap->type = V4L2_DV_BT_656_1120;
+	cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
+	cap->bt.min_width = video->width;
+	cap->bt.max_width = video->width;
+	cap->bt.min_height = video->height;
+	cap->bt.max_height = video->height;

This should return the capabilities of the aspeed. In this case I'd guess that the max width/height is 1920x1080 (or perhaps 1200).

Quoted 1 line

The minimum is probably the VGA resolution.

OK that works, but keep in mind a call to s_dv_timings must return EINVAL if the timings resolution doesn't match the detected resolution... driver can't change the capture resolution.

Quoted 335 lines
Quoted 335 lines
drivers/media/platform/aspeed-video.c new file +334 -0
+
+	return 0;
+}
+
+static int aspeed_video_sub_event(struct v4l2_fh *fh,
+				  const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_src_change_event_subscribe(fh, sub);
+	}
+
+	return v4l2_ctrl_subscribe_event(fh, sub);
+}
+
+static const struct v4l2_ioctl_ops aspeed_video_ioctl_ops = {
+	.vidioc_querycap = aspeed_video_querycap,
+
+	.vidioc_enum_fmt_vid_cap = aspeed_video_enum_format,
+	.vidioc_g_fmt_vid_cap = aspeed_video_get_format,
+	.vidioc_s_fmt_vid_cap = aspeed_video_get_format,
+	.vidioc_try_fmt_vid_cap = aspeed_video_get_format,
+
+	.vidioc_reqbufs = vb2_ioctl_reqbufs,
+	.vidioc_querybuf = vb2_ioctl_querybuf,
+	.vidioc_qbuf = vb2_ioctl_qbuf,
+	.vidioc_dqbuf = vb2_ioctl_dqbuf,
+	.vidioc_create_bufs = vb2_ioctl_create_bufs,
+	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
+	.vidioc_streamon = vb2_ioctl_streamon,
+	.vidioc_streamoff = vb2_ioctl_streamoff,
+
+	.vidioc_enum_input = aspeed_video_enum_input,
+	.vidioc_g_input = aspeed_video_get_input,
+	.vidioc_s_input = aspeed_video_set_input,
+
+	.vidioc_g_parm = aspeed_video_get_parm,
+	.vidioc_s_parm = aspeed_video_set_parm,
+	.vidioc_enum_framesizes = aspeed_video_enum_framesizes,
+	.vidioc_enum_frameintervals = aspeed_video_enum_frameintervals,
+
+	.vidioc_s_dv_timings = aspeed_video_set_dv_timings,
+	.vidioc_g_dv_timings = aspeed_video_get_dv_timings,
+	.vidioc_query_dv_timings = aspeed_video_query_dv_timings,
+	.vidioc_enum_dv_timings = aspeed_video_enum_dv_timings,
+	.vidioc_dv_timings_cap = aspeed_video_dv_timings_cap,
+
+	.vidioc_subscribe_event = aspeed_video_sub_event,
+	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+};
+
+static void aspeed_video_update_jpeg_quality(struct aspeed_video *video)
+{
+	u32 comp_ctrl = FIELD_PREP(VE_COMP_CTRL_DCT_LUM, video->jpeg_quality) |
+		FIELD_PREP(VE_COMP_CTRL_DCT_CHR, video->jpeg_quality | 0x10);
+
+	aspeed_video_update(video, VE_COMP_CTRL,
+			    VE_COMP_CTRL_DCT_LUM | VE_COMP_CTRL_DCT_CHR,
+			    comp_ctrl);
+}
+
+static void aspeed_video_update_subsampling(struct aspeed_video *video)
+{
+	if (video->jpeg.virt)
+		aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+	if (video->yuv420)
+		aspeed_video_update(video, VE_SEQ_CTRL, 0, VE_SEQ_CTRL_YUV420);
+	else
+		aspeed_video_update(video, VE_SEQ_CTRL, VE_SEQ_CTRL_YUV420, 0);
+}
+
+static int aspeed_video_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct aspeed_video *video = container_of(ctrl->handler,
+						  struct aspeed_video,
+						  ctrl_handler);
+
+	switch (ctrl->id) {
+	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
+		video->jpeg_quality = ctrl->val;
+		aspeed_video_update_jpeg_quality(video);
+		break;
+	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
+		if (ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
+			video->yuv420 = true;
+			aspeed_video_update_subsampling(video);
+		} else {
+			video->yuv420 = false;
+			aspeed_video_update_subsampling(video);
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops aspeed_video_ctrl_ops = {
+	.s_ctrl = aspeed_video_set_ctrl,
+};
+
+static void aspeed_video_resolution_work(struct work_struct *work)
+{
+	int rc;
+	struct delayed_work *dwork = to_delayed_work(work);
+	struct aspeed_video *video = container_of(dwork, struct aspeed_video,
+						  res_work);
+
+	/* No clients remaining after delay */
+	if (atomic_read(&video->clients) == 0)
+		goto done;
+
+	aspeed_video_on(video);
+
+	aspeed_video_init_regs(video);
+
+	rc = aspeed_video_get_resolution(video);
+	if (rc)
+		dev_err(video->dev,
+			"resolution changed; couldn't get new resolution\n");
+	else if (test_bit(VIDEO_STREAMING, &video->flags))
+		aspeed_video_start_frame(video);
+
+	if (video->width != video->pix_fmt.width ||
+	    video->height != video->pix_fmt.height) {
+		static const struct v4l2_event ev = {
+			.type = V4L2_EVENT_SOURCE_CHANGE,
+			.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
+		};
+
+		v4l2_event_queue(&video->vdev, &ev);
+	}
+
+done:
+	clear_bit(VIDEO_RES_CHANGE, &video->flags);
+	wake_up_interruptible_all(&video->wait);
+}
+
+static int aspeed_video_open(struct file *file)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	mutex_lock(&video->video_lock);
+
+	if (atomic_inc_return(&video->clients) == 1) {
+		rc = aspeed_video_start(video);
+		if (rc) {
+			dev_err(video->dev, "Failed to start video engine\n");
+			atomic_dec(&video->clients);
+			mutex_unlock(&video->video_lock);
+			return rc;
+		}
+	}
+
+	mutex_unlock(&video->video_lock);
+
+	return v4l2_fh_open(file);
+}
+
+static int aspeed_video_release(struct file *file)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	rc = vb2_fop_release(file);
+
+	mutex_lock(&video->video_lock);
+
+	if (atomic_dec_return(&video->clients) == 0)
+		aspeed_video_stop(video);
+
+	mutex_unlock(&video->video_lock);
+
+	return rc;
+}
+
+static const struct v4l2_file_operations aspeed_video_v4l2_fops = {
+	.owner = THIS_MODULE,
+	.read = vb2_fop_read,
+	.poll = vb2_fop_poll,
+	.unlocked_ioctl = video_ioctl2,
+	.mmap = vb2_fop_mmap,
+	.open = aspeed_video_open,
+	.release = aspeed_video_release,
+};
+
+static int aspeed_video_queue_setup(struct vb2_queue *q,
+				    unsigned int *num_buffers,
+				    unsigned int *num_planes,
+				    unsigned int sizes[],
+				    struct device *alloc_devs[])
+{
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	if (*num_planes) {
+		if (sizes[0] < video->max_compressed_size)
+			return -EINVAL;
+
+		return 0;
+	}
+
+	*num_planes = 1;
+	sizes[0] = video->max_compressed_size;
+
+	return 0;
+}
+
+static int aspeed_video_buf_prepare(struct vb2_buffer *vb)
+{
+	struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+
+	if (vb2_plane_size(vb, 0) < video->max_compressed_size)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int aspeed_video_start_streaming(struct vb2_queue *q,
+					unsigned int count)
+{
+	int rc;
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	rc = aspeed_video_start_frame(video);
+	if (rc) {
+		aspeed_video_bufs_done(video, VB2_BUF_STATE_QUEUED);
+		return rc;
+	}
+
+	video->sequence = 0;
+	set_bit(VIDEO_STREAMING, &video->flags);
+	return 0;
+}
+
+static void aspeed_video_stop_streaming(struct vb2_queue *q)
+{
+	int rc;
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	clear_bit(VIDEO_STREAMING, &video->flags);
+
+	rc = wait_event_timeout(video->wait,
+				!test_bit(VIDEO_FRAME_INPRG, &video->flags),
+				STOP_TIMEOUT);
+	if (!rc) {
+		dev_err(video->dev, "Timed out when stopping streaming\n");
+		aspeed_video_stop(video);
+	}
+
+	aspeed_video_bufs_done(video, VB2_BUF_STATE_ERROR);
+}
+
+static void aspeed_video_buf_queue(struct vb2_buffer *vb)
+{
+	struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct aspeed_video_buffer *avb = to_aspeed_video_buffer(vbuf);
+	unsigned long flags;
+
+	spin_lock_irqsave(&video->lock, flags);
+	list_add_tail(&avb->link, &video->buffers);
+	spin_unlock_irqrestore(&video->lock, flags);
+}
+
+static const struct vb2_ops aspeed_video_vb2_ops = {
+	.queue_setup = aspeed_video_queue_setup,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+	.buf_prepare = aspeed_video_buf_prepare,
+	.start_streaming = aspeed_video_start_streaming,
+	.stop_streaming = aspeed_video_stop_streaming,
+	.buf_queue =  aspeed_video_buf_queue,
+};
+
+static int aspeed_video_setup_video(struct aspeed_video *video)
+{
+	const u64 mask = ~(BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_444) |
+			   BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_420));
+	struct v4l2_device *v4l2_dev = &video->v4l2_dev;
+	struct vb2_queue *vbq = &video->queue;
+	struct video_device *vdev = &video->vdev;
+	int rc;
+
+	video->pix_fmt.pixelformat = V4L2_PIX_FMT_JPEG;
+	video->pix_fmt.field = V4L2_FIELD_NONE;
+	video->pix_fmt.colorspace = V4L2_COLORSPACE_SRGB;
+	video->pix_fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
+
+	rc = v4l2_device_register(video->dev, v4l2_dev);
+	if (rc) {
+		dev_err(video->dev, "Failed to register v4l2 device\n");
+		return rc;
+	}
+
+	v4l2_ctrl_handler_init(&video->ctrl_handler, 2);
+	v4l2_ctrl_new_std(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
+			  ASPEED_VIDEO_JPEG_NUM_QUALITIES - 1, 1, 0);
+	v4l2_ctrl_new_std_menu(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+			       V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
+			       V4L2_JPEG_CHROMA_SUBSAMPLING_420, mask,
+			       V4L2_JPEG_CHROMA_SUBSAMPLING_444);
+
+	if (video->ctrl_handler.error) {
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to init controls: %d\n",
+			video->ctrl_handler.error);
+		return rc;
+	}
+
+	v4l2_dev->ctrl_handler = &video->ctrl_handler;
+
+	vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	vbq->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
+	vbq->dev = v4l2_dev->dev;
+	vbq->lock = &video->video_lock;
+	vbq->ops = &aspeed_video_vb2_ops;
+	vbq->mem_ops = &vb2_dma_contig_memops;
+	vbq->drv_priv = video;
+	vbq->buf_struct_size = sizeof(struct aspeed_video_buffer);
+	vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	vbq->min_buffers_needed = 3;
+
+	rc = vb2_queue_init(vbq);
+	if (rc) {
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to init vb2 queue\n");
Quoted 8 lines

drivers/media/platform/aspeed-video.c line 1512

Quoted 6 lines
drivers/media/platform/aspeed-video.c new file +6 -0
+		return rc;
+	}
+
+	vdev->queue = vbq;
+	vdev->fops = &aspeed_video_v4l2_fops;
+	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |

READWRITE doesn't work for JPEG since there is no clear end of a frame. Just drop this and the read op in aspeed_video_v4l2_fops.

I don't follow. The buffer will have the payload set correctly, defining the end of the frame, right? I haven't tested the read interface now but looking at the vb2_fop_read I think it would work?

Thanks,
Eddie

Quoted 168 lines
Quoted 154 lines
drivers/media/platform/aspeed-video.c new file +152 -0
+		V4L2_CAP_STREAMING;
+	vdev->v4l2_dev = v4l2_dev;
+	strscpy(vdev->name, DEVICE_NAME, sizeof(vdev->name));
+	vdev->vfl_type = VFL_TYPE_GRABBER;
+	vdev->vfl_dir = VFL_DIR_RX;
+	vdev->release = video_device_release_empty;
+	vdev->ioctl_ops = &aspeed_video_ioctl_ops;
+	vdev->lock = &video->video_lock;
+
+	video_set_drvdata(vdev, video);
+	rc = video_register_device(vdev, VFL_TYPE_GRABBER, 0);
+	if (rc) {
+		vb2_queue_release(vbq);
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to register video device\n");
+		return rc;
+	}
+
+	return 0;
+}
+
+static int aspeed_video_init(struct aspeed_video *video)
+{
+	int irq;
+	int rc;
+	struct device *dev = video->dev;
+
+	irq = irq_of_parse_and_map(dev->of_node, 0);
+	if (!irq) {
+		dev_err(dev, "Unable to find IRQ\n");
+		return -ENODEV;
+	}
+
+	rc = devm_request_irq(dev, irq, aspeed_video_irq, IRQF_SHARED,
+			      DEVICE_NAME, video);
+	if (rc < 0) {
+		dev_err(dev, "Unable to request IRQ %d\n", irq);
+		return rc;
+	}
+
+	video->eclk = devm_clk_get(dev, "eclk");
+	if (IS_ERR(video->eclk)) {
+		dev_err(dev, "Unable to get ECLK\n");
+		return PTR_ERR(video->eclk);
+	}
+
+	video->vclk = devm_clk_get(dev, "vclk");
+	if (IS_ERR(video->vclk)) {
+		dev_err(dev, "Unable to get VCLK\n");
+		return PTR_ERR(video->vclk);
+	}
+
+	video->rst = devm_reset_control_get_exclusive(dev, NULL);
+	if (IS_ERR(video->rst)) {
+		dev_err(dev, "Unable to get VE reset\n");
+		return PTR_ERR(video->rst);
+	}
+
+	rc = of_reserved_mem_device_init(dev);
+	if (rc) {
+		dev_err(dev, "Unable to reserve memory\n");
+		return rc;
+	}
+
+	rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+	if (rc) {
+		dev_err(dev, "Failed to set DMA mask\n");
+		of_reserved_mem_device_release(dev);
+		return rc;
+	}
+
+	if (!aspeed_video_alloc_buf(video, &video->jpeg,
+				    VE_JPEG_HEADER_SIZE)) {
+		dev_err(dev, "Failed to allocate DMA for JPEG header\n");
+		of_reserved_mem_device_release(dev);
+		return rc;
+	}
+
+	aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+	return 0;
+}
+
+static int aspeed_video_probe(struct platform_device *pdev)
+{
+	int rc;
+	struct resource *res;
+	struct aspeed_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
+
+	if (!video)
+		return -ENOMEM;
+
+	video->frame_rate = 30;
+	video->dev = &pdev->dev;
+	mutex_init(&video->video_lock);
+	init_waitqueue_head(&video->wait);
+	INIT_DELAYED_WORK(&video->res_work, aspeed_video_resolution_work);
+	INIT_LIST_HEAD(&video->buffers);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+
+	video->base = devm_ioremap_resource(video->dev, res);
+
+	if (IS_ERR(video->base))
+		return PTR_ERR(video->base);
+
+	rc = aspeed_video_init(video);
+	if (rc)
+		return rc;
+
+	rc = aspeed_video_setup_video(video);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+
+static int aspeed_video_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
+	struct aspeed_video *video = to_aspeed_video(v4l2_dev);
+
+	video_unregister_device(&video->vdev);
+
+	vb2_queue_release(&video->queue);
+
+	v4l2_ctrl_handler_free(&video->ctrl_handler);
+
+	v4l2_device_unregister(v4l2_dev);
+
+	dma_free_coherent(video->dev, VE_JPEG_HEADER_SIZE, video->jpeg.virt,
+			  video->jpeg.dma);
+
+	of_reserved_mem_device_release(dev);
+
+	return 0;
+}
+
+static const struct of_device_id aspeed_video_of_match[] = {
+	{ .compatible = "aspeed,ast2400-video-engine" },
+	{ .compatible = "aspeed,ast2500-video-engine" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, aspeed_video_of_match);
+
+static struct platform_driver aspeed_video_driver = {
+	.driver = {
+		.name = DEVICE_NAME,
+		.of_match_table = aspeed_video_of_match,

drivers/media/platform/aspeed-video.c line 1674

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	},
+	.probe = aspeed_video_probe,
+	.remove = aspeed_video_remove,
+};
+
+module_platform_driver(aspeed_video_driver);
+
+MODULE_DESCRIPTION("ASPEED Video Engine Driver");
+MODULE_AUTHOR("Eddie James");
+MODULE_LICENSE("GPL v2");

Regards,

Hans

#5 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

In reply to Hans Verkuil
Quoted 150 lines · Hans Verkuil
Quoted 17 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting a service processor, the Video Engine can capture the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
  MAINTAINERS                           |    8 +
  drivers/media/platform/Kconfig        |    8 +
  drivers/media/platform/Makefile       |    1 +
  drivers/media/platform/aspeed-video.c | 1674 +++++++++++++++++++++++++++++++++
  4 files changed, 1691 insertions(+)

create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

drivers/media/platform/aspeed-video.c line 985

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+static int aspeed_video_enum_input(struct file *file, void *fh,
+				   struct v4l2_input *inp)
+{
+	if (inp->index)
+		return -EINVAL;
+
+	strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
+	inp->type = V4L2_INPUT_TYPE_CAMERA;
+	inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
+	inp->status = V4L2_IN_ST_NO_SIGNAL | V4L2_IN_ST_NO_SYNC;

This can't be right. If there is a valid signal, then status should be 0. And ideally you can tell the difference between no signal and no sync as well.

Quoted 44 lines
drivers/media/platform/aspeed-video.c new file +44 -0
+
+	return 0;
+}
+
+static int aspeed_video_get_input(struct file *file, void *fh, unsigned int *i)
+{
+	*i = 0;
+
+	return 0;
+}
+
+static int aspeed_video_set_input(struct file *file, void *fh, unsigned int i)
+{
+	if (i)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int aspeed_video_get_parm(struct file *file, void *fh,
+				 struct v4l2_streamparm *a)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+	a->parm.capture.readbuffers = 3;
+	a->parm.capture.timeperframe.numerator = 1;
+	if (!video->frame_rate)
+		a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;
+	else
+		a->parm.capture.timeperframe.denominator = video->frame_rate;
+
+	return 0;
+}
+
+static int aspeed_video_set_parm(struct file *file, void *fh,
+				 struct v4l2_streamparm *a)
+{
+	unsigned int frame_rate = 0;
+	struct aspeed_video *video = video_drvdata(file);
+
+	a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+	a->parm.capture.readbuffers = 3;
+

drivers/media/platform/aspeed-video.c line 1039

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	if (a->parm.capture.timeperframe.numerator)
+		frame_rate = a->parm.capture.timeperframe.denominator /
+			a->parm.capture.timeperframe.numerator;
+
+	if (!frame_rate || frame_rate > MAX_FRAME_RATE) {
+		frame_rate = 0;
+
+		/*
+		 * Set to max + 1 to differentiate between max and 0, which
+		 * means "don't care".

But what does "don't care" mean in practice? It's still not clear to me how this is supposed to work.

drivers/media/platform/aspeed-video.c line 1041

Quoted 2 lines
drivers/media/platform/aspeed-video.c new file +2 -0
+		 */
+		a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;

And regardless of anything else this timeperframe is out of the range that aspeed_video_enum_frameintervals() returns.

Quoted 54 lines
drivers/media/platform/aspeed-video.c new file +54 -0
+		a->parm.capture.timeperframe.numerator = 1;
+	}
+
+	if (video->frame_rate != frame_rate) {
+		video->frame_rate = frame_rate;
+		aspeed_video_update(video, VE_CTRL, VE_CTRL_FRC,
+				    FIELD_PREP(VE_CTRL_FRC, frame_rate));
+	}
+
+	return 0;
+}
+
+static int aspeed_video_enum_framesizes(struct file *file, void *fh,
+					struct v4l2_frmsizeenum *fsize)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (fsize->index)
+		return -EINVAL;
+
+	if (fsize->pixel_format != V4L2_PIX_FMT_JPEG)
+		return -EINVAL;
+
+	fsize->discrete.width = video->pix_fmt.width;
+	fsize->discrete.height = video->pix_fmt.height;
+	fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+
+	return 0;
+}
+
+static int aspeed_video_enum_frameintervals(struct file *file, void *fh,
+					    struct v4l2_frmivalenum *fival)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (fival->index)
+		return -EINVAL;
+
+	if (fival->width != video->width || fival->height != video->height)
+		return -EINVAL;
+
+	if (fival->pixel_format != V4L2_PIX_FMT_JPEG)
+		return -EINVAL;
+
+	fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
+
+	fival->stepwise.min.denominator = MAX_FRAME_RATE;
+	fival->stepwise.min.numerator = 1;
+	fival->stepwise.max.denominator = 1;
+	fival->stepwise.max.numerator = 1;
+	fival->stepwise.step = fival->stepwise.max;
+
+	return 0;
+}
Quoted 8 lines

drivers/media/platform/aspeed-video.c line 1101

Quoted 6 lines
drivers/media/platform/aspeed-video.c new file +6 -0
+
+static int aspeed_video_set_dv_timings(struct file *file, void *fh,
+				       struct v4l2_dv_timings *timings)
+{
+	struct aspeed_video *video = video_drvdata(file);
+

If vb2_is_busy() returns true, then return -EBUSY here. It is not allowed to set the timings while vb2 is busy.

Buffer ioctls (Input 0):
??? ??? fail: v4l2-test-buffers.cpp(344): node->s_dv_timings(timings) ??? ??? fail: v4l2-test-buffers.cpp(452): testCanSetSameTimings(node) ??? test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: FAIL

Streaming ioctls:
??? test read/write: OK
??? test blocking wait: OK
??? ??? fail: v4l2-test-buffers.cpp(344): node->s_dv_timings(timings) ??? ??? fail: v4l2-test-buffers.cpp(637): testCanSetSameTimings(node) ??? ??? fail: v4l2-test-buffers.cpp(952): captureBufs(node, q, m2m_q,
frame_count, false)
??? test MMAP: FAIL

Built from v4l-utils c36dbbdfa8b30b2badd4f893b59d0bd4f0bd12aa

Adding this causes some of the v4l2-compliance streaming tests to fail, and prevents my own application from being able to call S_DV_TIMINGS after detecting a resolution change, despite calling streamoff and unmapping all the buffers first.

Thanks,
Eddie

Quoted 587 lines
Quoted 62 lines
drivers/media/platform/aspeed-video.c new file +61 -0
+	if (video->width != timings->bt.width ||
+	    video->height != timings->bt.height)
+		return -EINVAL;
+
+	video->pix_fmt.width = timings->bt.width;
+	video->pix_fmt.height = timings->bt.height;
+	video->pix_fmt.sizeimage = video->max_compressed_size;
+	video->timings.width = timings->bt.width;
+	video->timings.height = timings->bt.height;
+
+	timings->type = V4L2_DV_BT_656_1120;
+
+	return 0;
+}
+
+static int aspeed_video_get_dv_timings(struct file *file, void *fh,
+				       struct v4l2_dv_timings *timings)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	timings->type = V4L2_DV_BT_656_1120;
+	timings->bt = video->timings;
+
+	return 0;
+}
+
+static int aspeed_video_query_dv_timings(struct file *file, void *fh,
+					 struct v4l2_dv_timings *timings)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	if (file->f_flags & O_NONBLOCK) {
+		if (test_bit(VIDEO_RES_CHANGE, &video->flags))
+			return -EAGAIN;
+	} else {
+		rc = wait_event_interruptible(video->wait,
+					      !test_bit(VIDEO_RES_CHANGE,
+							&video->flags));
+		if (rc)
+			return -EINTR;
+	}
+
+	timings->type = V4L2_DV_BT_656_1120;
+	timings->bt = video->timings;
+	timings->bt.width = video->width;
+	timings->bt.height = video->height;
+
+	return 0;
+}
+
+static int aspeed_video_enum_dv_timings(struct file *file, void *fh,
+					struct v4l2_enum_dv_timings *timings)
+{
+	if (timings->index)
+		return -EINVAL;
+
+	return aspeed_video_get_dv_timings(file, fh, &timings->timings);
+}
+
+static int aspeed_video_dv_timings_cap(struct file *file, void *fh,

drivers/media/platform/aspeed-video.c line 1172

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+				       struct v4l2_dv_timings_cap *cap)
+{
+	struct aspeed_video *video = video_drvdata(file);
+
+	cap->type = V4L2_DV_BT_656_1120;
+	cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
+	cap->bt.min_width = video->width;
+	cap->bt.max_width = video->width;
+	cap->bt.min_height = video->height;
+	cap->bt.max_height = video->height;

This should return the capabilities of the aspeed. In this case I'd guess that the max width/height is 1920x1080 (or perhaps 1200).

The minimum is probably the VGA resolution.

Quoted 330 lines
drivers/media/platform/aspeed-video.c new file +330 -0
+
+	return 0;
+}
+
+static int aspeed_video_sub_event(struct v4l2_fh *fh,
+				  const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_SOURCE_CHANGE:
+		return v4l2_src_change_event_subscribe(fh, sub);
+	}
+
+	return v4l2_ctrl_subscribe_event(fh, sub);
+}
+
+static const struct v4l2_ioctl_ops aspeed_video_ioctl_ops = {
+	.vidioc_querycap = aspeed_video_querycap,
+
+	.vidioc_enum_fmt_vid_cap = aspeed_video_enum_format,
+	.vidioc_g_fmt_vid_cap = aspeed_video_get_format,
+	.vidioc_s_fmt_vid_cap = aspeed_video_get_format,
+	.vidioc_try_fmt_vid_cap = aspeed_video_get_format,
+
+	.vidioc_reqbufs = vb2_ioctl_reqbufs,
+	.vidioc_querybuf = vb2_ioctl_querybuf,
+	.vidioc_qbuf = vb2_ioctl_qbuf,
+	.vidioc_dqbuf = vb2_ioctl_dqbuf,
+	.vidioc_create_bufs = vb2_ioctl_create_bufs,
+	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
+	.vidioc_streamon = vb2_ioctl_streamon,
+	.vidioc_streamoff = vb2_ioctl_streamoff,
+
+	.vidioc_enum_input = aspeed_video_enum_input,
+	.vidioc_g_input = aspeed_video_get_input,
+	.vidioc_s_input = aspeed_video_set_input,
+
+	.vidioc_g_parm = aspeed_video_get_parm,
+	.vidioc_s_parm = aspeed_video_set_parm,
+	.vidioc_enum_framesizes = aspeed_video_enum_framesizes,
+	.vidioc_enum_frameintervals = aspeed_video_enum_frameintervals,
+
+	.vidioc_s_dv_timings = aspeed_video_set_dv_timings,
+	.vidioc_g_dv_timings = aspeed_video_get_dv_timings,
+	.vidioc_query_dv_timings = aspeed_video_query_dv_timings,
+	.vidioc_enum_dv_timings = aspeed_video_enum_dv_timings,
+	.vidioc_dv_timings_cap = aspeed_video_dv_timings_cap,
+
+	.vidioc_subscribe_event = aspeed_video_sub_event,
+	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+};
+
+static void aspeed_video_update_jpeg_quality(struct aspeed_video *video)
+{
+	u32 comp_ctrl = FIELD_PREP(VE_COMP_CTRL_DCT_LUM, video->jpeg_quality) |
+		FIELD_PREP(VE_COMP_CTRL_DCT_CHR, video->jpeg_quality | 0x10);
+
+	aspeed_video_update(video, VE_COMP_CTRL,
+			    VE_COMP_CTRL_DCT_LUM | VE_COMP_CTRL_DCT_CHR,
+			    comp_ctrl);
+}
+
+static void aspeed_video_update_subsampling(struct aspeed_video *video)
+{
+	if (video->jpeg.virt)
+		aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+	if (video->yuv420)
+		aspeed_video_update(video, VE_SEQ_CTRL, 0, VE_SEQ_CTRL_YUV420);
+	else
+		aspeed_video_update(video, VE_SEQ_CTRL, VE_SEQ_CTRL_YUV420, 0);
+}
+
+static int aspeed_video_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+	struct aspeed_video *video = container_of(ctrl->handler,
+						  struct aspeed_video,
+						  ctrl_handler);
+
+	switch (ctrl->id) {
+	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
+		video->jpeg_quality = ctrl->val;
+		aspeed_video_update_jpeg_quality(video);
+		break;
+	case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
+		if (ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
+			video->yuv420 = true;
+			aspeed_video_update_subsampling(video);
+		} else {
+			video->yuv420 = false;
+			aspeed_video_update_subsampling(video);
+		}
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static const struct v4l2_ctrl_ops aspeed_video_ctrl_ops = {
+	.s_ctrl = aspeed_video_set_ctrl,
+};
+
+static void aspeed_video_resolution_work(struct work_struct *work)
+{
+	int rc;
+	struct delayed_work *dwork = to_delayed_work(work);
+	struct aspeed_video *video = container_of(dwork, struct aspeed_video,
+						  res_work);
+
+	/* No clients remaining after delay */
+	if (atomic_read(&video->clients) == 0)
+		goto done;
+
+	aspeed_video_on(video);
+
+	aspeed_video_init_regs(video);
+
+	rc = aspeed_video_get_resolution(video);
+	if (rc)
+		dev_err(video->dev,
+			"resolution changed; couldn't get new resolution\n");
+	else if (test_bit(VIDEO_STREAMING, &video->flags))
+		aspeed_video_start_frame(video);
+
+	if (video->width != video->pix_fmt.width ||
+	    video->height != video->pix_fmt.height) {
+		static const struct v4l2_event ev = {
+			.type = V4L2_EVENT_SOURCE_CHANGE,
+			.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
+		};
+
+		v4l2_event_queue(&video->vdev, &ev);
+	}
+
+done:
+	clear_bit(VIDEO_RES_CHANGE, &video->flags);
+	wake_up_interruptible_all(&video->wait);
+}
+
+static int aspeed_video_open(struct file *file)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	mutex_lock(&video->video_lock);
+
+	if (atomic_inc_return(&video->clients) == 1) {
+		rc = aspeed_video_start(video);
+		if (rc) {
+			dev_err(video->dev, "Failed to start video engine\n");
+			atomic_dec(&video->clients);
+			mutex_unlock(&video->video_lock);
+			return rc;
+		}
+	}
+
+	mutex_unlock(&video->video_lock);
+
+	return v4l2_fh_open(file);
+}
+
+static int aspeed_video_release(struct file *file)
+{
+	int rc;
+	struct aspeed_video *video = video_drvdata(file);
+
+	rc = vb2_fop_release(file);
+
+	mutex_lock(&video->video_lock);
+
+	if (atomic_dec_return(&video->clients) == 0)
+		aspeed_video_stop(video);
+
+	mutex_unlock(&video->video_lock);
+
+	return rc;
+}
+
+static const struct v4l2_file_operations aspeed_video_v4l2_fops = {
+	.owner = THIS_MODULE,
+	.read = vb2_fop_read,
+	.poll = vb2_fop_poll,
+	.unlocked_ioctl = video_ioctl2,
+	.mmap = vb2_fop_mmap,
+	.open = aspeed_video_open,
+	.release = aspeed_video_release,
+};
+
+static int aspeed_video_queue_setup(struct vb2_queue *q,
+				    unsigned int *num_buffers,
+				    unsigned int *num_planes,
+				    unsigned int sizes[],
+				    struct device *alloc_devs[])
+{
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	if (*num_planes) {
+		if (sizes[0] < video->max_compressed_size)
+			return -EINVAL;
+
+		return 0;
+	}
+
+	*num_planes = 1;
+	sizes[0] = video->max_compressed_size;
+
+	return 0;
+}
+
+static int aspeed_video_buf_prepare(struct vb2_buffer *vb)
+{
+	struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+
+	if (vb2_plane_size(vb, 0) < video->max_compressed_size)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int aspeed_video_start_streaming(struct vb2_queue *q,
+					unsigned int count)
+{
+	int rc;
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	rc = aspeed_video_start_frame(video);
+	if (rc) {
+		aspeed_video_bufs_done(video, VB2_BUF_STATE_QUEUED);
+		return rc;
+	}
+
+	video->sequence = 0;
+	set_bit(VIDEO_STREAMING, &video->flags);
+	return 0;
+}
+
+static void aspeed_video_stop_streaming(struct vb2_queue *q)
+{
+	int rc;
+	struct aspeed_video *video = vb2_get_drv_priv(q);
+
+	clear_bit(VIDEO_STREAMING, &video->flags);
+
+	rc = wait_event_timeout(video->wait,
+				!test_bit(VIDEO_FRAME_INPRG, &video->flags),
+				STOP_TIMEOUT);
+	if (!rc) {
+		dev_err(video->dev, "Timed out when stopping streaming\n");
+		aspeed_video_stop(video);
+	}
+
+	aspeed_video_bufs_done(video, VB2_BUF_STATE_ERROR);
+}
+
+static void aspeed_video_buf_queue(struct vb2_buffer *vb)
+{
+	struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+	struct aspeed_video_buffer *avb = to_aspeed_video_buffer(vbuf);
+	unsigned long flags;
+
+	spin_lock_irqsave(&video->lock, flags);
+	list_add_tail(&avb->link, &video->buffers);
+	spin_unlock_irqrestore(&video->lock, flags);
+}
+
+static const struct vb2_ops aspeed_video_vb2_ops = {
+	.queue_setup = aspeed_video_queue_setup,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+	.buf_prepare = aspeed_video_buf_prepare,
+	.start_streaming = aspeed_video_start_streaming,
+	.stop_streaming = aspeed_video_stop_streaming,
+	.buf_queue =  aspeed_video_buf_queue,
+};
+
+static int aspeed_video_setup_video(struct aspeed_video *video)
+{
+	const u64 mask = ~(BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_444) |
+			   BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_420));
+	struct v4l2_device *v4l2_dev = &video->v4l2_dev;
+	struct vb2_queue *vbq = &video->queue;
+	struct video_device *vdev = &video->vdev;
+	int rc;
+
+	video->pix_fmt.pixelformat = V4L2_PIX_FMT_JPEG;
+	video->pix_fmt.field = V4L2_FIELD_NONE;
+	video->pix_fmt.colorspace = V4L2_COLORSPACE_SRGB;
+	video->pix_fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
+
+	rc = v4l2_device_register(video->dev, v4l2_dev);
+	if (rc) {
+		dev_err(video->dev, "Failed to register v4l2 device\n");
+		return rc;
+	}
+
+	v4l2_ctrl_handler_init(&video->ctrl_handler, 2);
+	v4l2_ctrl_new_std(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
+			  ASPEED_VIDEO_JPEG_NUM_QUALITIES - 1, 1, 0);
+	v4l2_ctrl_new_std_menu(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+			       V4L2_CID_JPEG_CHROMA_SUBSAMPLING,
+			       V4L2_JPEG_CHROMA_SUBSAMPLING_420, mask,
+			       V4L2_JPEG_CHROMA_SUBSAMPLING_444);
+
+	if (video->ctrl_handler.error) {
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to init controls: %d\n",
+			video->ctrl_handler.error);
+		return rc;
+	}
+
+	v4l2_dev->ctrl_handler = &video->ctrl_handler;
+
+	vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	vbq->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
+	vbq->dev = v4l2_dev->dev;
+	vbq->lock = &video->video_lock;
+	vbq->ops = &aspeed_video_vb2_ops;
+	vbq->mem_ops = &vb2_dma_contig_memops;
+	vbq->drv_priv = video;
+	vbq->buf_struct_size = sizeof(struct aspeed_video_buffer);
+	vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	vbq->min_buffers_needed = 3;
+
+	rc = vb2_queue_init(vbq);
+	if (rc) {

drivers/media/platform/aspeed-video.c line 1512

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to init vb2 queue\n");
+		return rc;
+	}
+
+	vdev->queue = vbq;
+	vdev->fops = &aspeed_video_v4l2_fops;
+	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |

READWRITE doesn't work for JPEG since there is no clear end of a frame. Just drop this and the read op in aspeed_video_v4l2_fops.

Quoted 153 lines
drivers/media/platform/aspeed-video.c new file +152 -0
+		V4L2_CAP_STREAMING;
+	vdev->v4l2_dev = v4l2_dev;
+	strscpy(vdev->name, DEVICE_NAME, sizeof(vdev->name));
+	vdev->vfl_type = VFL_TYPE_GRABBER;
+	vdev->vfl_dir = VFL_DIR_RX;
+	vdev->release = video_device_release_empty;
+	vdev->ioctl_ops = &aspeed_video_ioctl_ops;
+	vdev->lock = &video->video_lock;
+
+	video_set_drvdata(vdev, video);
+	rc = video_register_device(vdev, VFL_TYPE_GRABBER, 0);
+	if (rc) {
+		vb2_queue_release(vbq);
+		v4l2_ctrl_handler_free(&video->ctrl_handler);
+		v4l2_device_unregister(v4l2_dev);
+
+		dev_err(video->dev, "Failed to register video device\n");
+		return rc;
+	}
+
+	return 0;
+}
+
+static int aspeed_video_init(struct aspeed_video *video)
+{
+	int irq;
+	int rc;
+	struct device *dev = video->dev;
+
+	irq = irq_of_parse_and_map(dev->of_node, 0);
+	if (!irq) {
+		dev_err(dev, "Unable to find IRQ\n");
+		return -ENODEV;
+	}
+
+	rc = devm_request_irq(dev, irq, aspeed_video_irq, IRQF_SHARED,
+			      DEVICE_NAME, video);
+	if (rc < 0) {
+		dev_err(dev, "Unable to request IRQ %d\n", irq);
+		return rc;
+	}
+
+	video->eclk = devm_clk_get(dev, "eclk");
+	if (IS_ERR(video->eclk)) {
+		dev_err(dev, "Unable to get ECLK\n");
+		return PTR_ERR(video->eclk);
+	}
+
+	video->vclk = devm_clk_get(dev, "vclk");
+	if (IS_ERR(video->vclk)) {
+		dev_err(dev, "Unable to get VCLK\n");
+		return PTR_ERR(video->vclk);
+	}
+
+	video->rst = devm_reset_control_get_exclusive(dev, NULL);
+	if (IS_ERR(video->rst)) {
+		dev_err(dev, "Unable to get VE reset\n");
+		return PTR_ERR(video->rst);
+	}
+
+	rc = of_reserved_mem_device_init(dev);
+	if (rc) {
+		dev_err(dev, "Unable to reserve memory\n");
+		return rc;
+	}
+
+	rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+	if (rc) {
+		dev_err(dev, "Failed to set DMA mask\n");
+		of_reserved_mem_device_release(dev);
+		return rc;
+	}
+
+	if (!aspeed_video_alloc_buf(video, &video->jpeg,
+				    VE_JPEG_HEADER_SIZE)) {
+		dev_err(dev, "Failed to allocate DMA for JPEG header\n");
+		of_reserved_mem_device_release(dev);
+		return rc;
+	}
+
+	aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+	return 0;
+}
+
+static int aspeed_video_probe(struct platform_device *pdev)
+{
+	int rc;
+	struct resource *res;
+	struct aspeed_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
+
+	if (!video)
+		return -ENOMEM;
+
+	video->frame_rate = 30;
+	video->dev = &pdev->dev;
+	mutex_init(&video->video_lock);
+	init_waitqueue_head(&video->wait);
+	INIT_DELAYED_WORK(&video->res_work, aspeed_video_resolution_work);
+	INIT_LIST_HEAD(&video->buffers);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+
+	video->base = devm_ioremap_resource(video->dev, res);
+
+	if (IS_ERR(video->base))
+		return PTR_ERR(video->base);
+
+	rc = aspeed_video_init(video);
+	if (rc)
+		return rc;
+
+	rc = aspeed_video_setup_video(video);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+
+static int aspeed_video_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
+	struct aspeed_video *video = to_aspeed_video(v4l2_dev);
+
+	video_unregister_device(&video->vdev);
+
+	vb2_queue_release(&video->queue);
+
+	v4l2_ctrl_handler_free(&video->ctrl_handler);
+
+	v4l2_device_unregister(v4l2_dev);
+
+	dma_free_coherent(video->dev, VE_JPEG_HEADER_SIZE, video->jpeg.virt,
+			  video->jpeg.dma);
+
+	of_reserved_mem_device_release(dev);
+
+	return 0;
+}
+
+static const struct of_device_id aspeed_video_of_match[] = {
+	{ .compatible = "aspeed,ast2400-video-engine" },
+	{ .compatible = "aspeed,ast2500-video-engine" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, aspeed_video_of_match);
+
+static struct platform_driver aspeed_video_driver = {
+	.driver = {
+		.name = DEVICE_NAME,
+		.of_match_table = aspeed_video_of_match,

drivers/media/platform/aspeed-video.c line 1674

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	},
+	.probe = aspeed_video_probe,
+	.remove = aspeed_video_remove,
+};
+
+module_platform_driver(aspeed_video_driver);
+
+MODULE_DESCRIPTION("ASPEED Video Engine Driver");
+MODULE_AUTHOR("Eddie James");
+MODULE_LICENSE("GPL v2");

Regards,

Hans

#6 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

Quoted 187 lines · Eddie James
Quoted 161 lines · Hans Verkuil
Quoted 20 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With
the Aspeed chip acting a service processor, the Video Engine can capture
the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---

? MAINTAINERS?????????????????????????? |??? 8 +
? drivers/media/platform/Kconfig??????? |??? 8 +
? drivers/media/platform/Makefile?????? |??? 1 +
? drivers/media/platform/aspeed-video.c | 1674 

+++++++++++++++++++++++++++++++++
? 4 files changed, 1691 insertions(+)
? create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

Review

Quoted 10 lines
(quoted diff) +10 -0
+static int aspeed_video_enum_input(struct file *file, void *fh,
+?????????????????? struct v4l2_input *inp)
+{
+??? if (inp->index)
+??????? return -EINVAL;
+
+??? strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
+??? inp->type = V4L2_INPUT_TYPE_CAMERA;
+??? inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
+??? inp->status = V4L2_IN_ST_NO_SIGNAL | V4L2_IN_ST_NO_SYNC;

This can't be right. If there is a valid signal, then status should be 0.
And ideally you can tell the difference between no signal and no sync as well.

Quoted 46 lines
drivers/media/platform/aspeed-video.c new file +5 -0
+
+??? return 0;
+}
+
+static int aspeed_video_get_input(struct file *file, void *fh, 

unsigned int *i)

drivers/media/platform/aspeed-video.c new file +7 -0
+{
+??? *i = 0;
+
+??? return 0;
+}
+
+static int aspeed_video_set_input(struct file *file, void *fh, 

unsigned int i)

(quoted diff) +32 -0
+{
+??? if (i)
+??????? return -EINVAL;
+
+??? return 0;
+}
+
+static int aspeed_video_get_parm(struct file *file, void *fh,
+???????????????? struct v4l2_streamparm *a)
+{
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+??? a->parm.capture.readbuffers = 3;
+??? a->parm.capture.timeperframe.numerator = 1;
+??? if (!video->frame_rate)
+??????? a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;
+??? else
+??????? a->parm.capture.timeperframe.denominator = video->frame_rate;
+
+??? return 0;
+}
+
+static int aspeed_video_set_parm(struct file *file, void *fh,
+???????????????? struct v4l2_streamparm *a)
+{
+??? unsigned int frame_rate = 0;
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+??? a->parm.capture.readbuffers = 3;
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+??? if (a->parm.capture.timeperframe.numerator)
+??????? frame_rate = a->parm.capture.timeperframe.denominator /
+??????????? a->parm.capture.timeperframe.numerator;
+
+??? if (!frame_rate || frame_rate > MAX_FRAME_RATE) {
+??????? frame_rate = 0;
+
+??????? /*
+???????? * Set to max + 1 to differentiate between max and 0, which
+???????? * means "don't care".

But what does "don't care" mean in practice? It's still not clear to me how this
is supposed to work.

Quoted 2 lines

+???????? */
+??????? a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;

And regardless of anything else this timeperframe is out of the range that
aspeed_video_enum_frameintervals() returns.

Quoted 57 lines

+ a->parm.capture.timeperframe.numerator = 1;
+??? }

(quoted diff) +29 -0
+
+??? if (video->frame_rate != frame_rate) {
+??????? video->frame_rate = frame_rate;
+??????? aspeed_video_update(video, VE_CTRL, VE_CTRL_FRC,
+??????????????????? FIELD_PREP(VE_CTRL_FRC, frame_rate));
+??? }
+
+??? return 0;
+}
+
+static int aspeed_video_enum_framesizes(struct file *file, void *fh,
+??????????????????? struct v4l2_frmsizeenum *fsize)
+{
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? if (fsize->index)
+??????? return -EINVAL;
+
+??? if (fsize->pixel_format != V4L2_PIX_FMT_JPEG)
+??????? return -EINVAL;
+
+??? fsize->discrete.width = video->pix_fmt.width;
+??? fsize->discrete.height = video->pix_fmt.height;
+??? fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+
+??? return 0;
+}
+
+static int aspeed_video_enum_frameintervals(struct file *file, void 

*fh,
+??????????????????????? struct v4l2_frmivalenum *fival)

(quoted diff) +7 -0
+{
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? if (fival->index)
+??????? return -EINVAL;
+
+??? if (fival->width != video->width || fival->height != 

video->height)
+??????? return -EINVAL;

(quoted diff) +15 -0
+
+??? if (fival->pixel_format != V4L2_PIX_FMT_JPEG)
+??????? return -EINVAL;
+
+??? fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
+
+??? fival->stepwise.min.denominator = MAX_FRAME_RATE;
+??? fival->stepwise.min.numerator = 1;
+??? fival->stepwise.max.denominator = 1;
+??? fival->stepwise.max.numerator = 1;
+??? fival->stepwise.step = fival->stepwise.max;
+
+??? return 0;
+}
+
Quoted 8 lines

drivers/media/platform/aspeed-video.c line 1101

Quoted 5 lines
drivers/media/platform/aspeed-video.c new file +5 -0
+static int aspeed_video_set_dv_timings(struct file *file, void *fh,
+?????????????????????? struct v4l2_dv_timings *timings)
+{
+??? struct aspeed_video *video = video_drvdata(file);
+

If vb2_is_busy() returns true, then return -EBUSY here. It is not allowed to
set the timings while vb2 is busy.

Buffer ioctls (Input 0):
??? ??? fail: v4l2-test-buffers.cpp(344): node->s_dv_timings(timings) ??? ??? fail: v4l2-test-buffers.cpp(452): testCanSetSameTimings(node) ??? test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: FAIL

Streaming ioctls:
??? test read/write: OK
??? test blocking wait: OK
??? ??? fail: v4l2-test-buffers.cpp(344): node->s_dv_timings(timings) ??? ??? fail: v4l2-test-buffers.cpp(637): testCanSetSameTimings(node) ??? ??? fail: v4l2-test-buffers.cpp(952): captureBufs(node, q, m2m_q, frame_count, false)
??? test MMAP: FAIL

Quoted 6 lines

Built from v4l-utils c36dbbdfa8b30b2badd4f893b59d0bd4f0bd12aa

Adding this causes some of the v4l2-compliance streaming tests to fail, and prevents my own application from being able to call S_DV_TIMINGS after detecting a resolution change, despite calling streamoff and unmapping all the buffers first.

Any thoughts on this Hans?

Thanks,
Eddie

Quoted 601 lines

Thanks,
Eddie

Quoted 596 lines
Quoted 62 lines

+??? if (video->width != timings->bt.width ||
+??????? video->height != timings->bt.height)
+??????? return -EINVAL;

(quoted diff) +58 -0
+
+??? video->pix_fmt.width = timings->bt.width;
+??? video->pix_fmt.height = timings->bt.height;
+??? video->pix_fmt.sizeimage = video->max_compressed_size;
+??? video->timings.width = timings->bt.width;
+??? video->timings.height = timings->bt.height;
+
+??? timings->type = V4L2_DV_BT_656_1120;
+
+??? return 0;
+}
+
+static int aspeed_video_get_dv_timings(struct file *file, void *fh,
+?????????????????????? struct v4l2_dv_timings *timings)
+{
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? timings->type = V4L2_DV_BT_656_1120;
+??? timings->bt = video->timings;
+
+??? return 0;
+}
+
+static int aspeed_video_query_dv_timings(struct file *file, void *fh,
+???????????????????? struct v4l2_dv_timings *timings)
+{
+??? int rc;
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? if (file->f_flags & O_NONBLOCK) {
+??????? if (test_bit(VIDEO_RES_CHANGE, &video->flags))
+??????????? return -EAGAIN;
+??? } else {
+??????? rc = wait_event_interruptible(video->wait,
+????????????????????????? !test_bit(VIDEO_RES_CHANGE,
+??????????????????????????? &video->flags));
+??????? if (rc)
+??????????? return -EINTR;
+??? }
+
+??? timings->type = V4L2_DV_BT_656_1120;
+??? timings->bt = video->timings;
+??? timings->bt.width = video->width;
+??? timings->bt.height = video->height;
+
+??? return 0;
+}
+
+static int aspeed_video_enum_dv_timings(struct file *file, void *fh,
+??????????????????? struct v4l2_enum_dv_timings *timings)
+{
+??? if (timings->index)
+??????? return -EINVAL;
+
+??? return aspeed_video_get_dv_timings(file, fh, &timings->timings);
+}
+
+static int aspeed_video_dv_timings_cap(struct file *file, void *fh,

Review

Quoted 10 lines
(quoted diff) +10 -0
+?????????????????????? struct v4l2_dv_timings_cap *cap)
+{
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? cap->type = V4L2_DV_BT_656_1120;
+??? cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
+??? cap->bt.min_width = video->width;
+??? cap->bt.max_width = video->width;
+??? cap->bt.min_height = video->height;
+??? cap->bt.max_height = video->height;

This should return the capabilities of the aspeed. In this case I'd guess that the max width/height is 1920x1080 (or perhaps 1200).

The minimum is probably the VGA resolution.

Quoted 337 lines
(quoted diff) +52 -0
+
+??? return 0;
+}
+
+static int aspeed_video_sub_event(struct v4l2_fh *fh,
+????????????????? const struct v4l2_event_subscription *sub)
+{
+??? switch (sub->type) {
+??? case V4L2_EVENT_SOURCE_CHANGE:
+??????? return v4l2_src_change_event_subscribe(fh, sub);
+??? }
+
+??? return v4l2_ctrl_subscribe_event(fh, sub);
+}
+
+static const struct v4l2_ioctl_ops aspeed_video_ioctl_ops = {
+??? .vidioc_querycap = aspeed_video_querycap,
+
+??? .vidioc_enum_fmt_vid_cap = aspeed_video_enum_format,
+??? .vidioc_g_fmt_vid_cap = aspeed_video_get_format,
+??? .vidioc_s_fmt_vid_cap = aspeed_video_get_format,
+??? .vidioc_try_fmt_vid_cap = aspeed_video_get_format,
+
+??? .vidioc_reqbufs = vb2_ioctl_reqbufs,
+??? .vidioc_querybuf = vb2_ioctl_querybuf,
+??? .vidioc_qbuf = vb2_ioctl_qbuf,
+??? .vidioc_dqbuf = vb2_ioctl_dqbuf,
+??? .vidioc_create_bufs = vb2_ioctl_create_bufs,
+??? .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
+??? .vidioc_streamon = vb2_ioctl_streamon,
+??? .vidioc_streamoff = vb2_ioctl_streamoff,
+
+??? .vidioc_enum_input = aspeed_video_enum_input,
+??? .vidioc_g_input = aspeed_video_get_input,
+??? .vidioc_s_input = aspeed_video_set_input,
+
+??? .vidioc_g_parm = aspeed_video_get_parm,
+??? .vidioc_s_parm = aspeed_video_set_parm,
+??? .vidioc_enum_framesizes = aspeed_video_enum_framesizes,
+??? .vidioc_enum_frameintervals = aspeed_video_enum_frameintervals,
+
+??? .vidioc_s_dv_timings = aspeed_video_set_dv_timings,
+??? .vidioc_g_dv_timings = aspeed_video_get_dv_timings,
+??? .vidioc_query_dv_timings = aspeed_video_query_dv_timings,
+??? .vidioc_enum_dv_timings = aspeed_video_enum_dv_timings,
+??? .vidioc_dv_timings_cap = aspeed_video_dv_timings_cap,
+
+??? .vidioc_subscribe_event = aspeed_video_sub_event,
+??? .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+};
+
+static void aspeed_video_update_jpeg_quality(struct aspeed_video 

*video)
+{
+??? u32 comp_ctrl = FIELD_PREP(VE_COMP_CTRL_DCT_LUM, video->jpeg_quality) |
+??????? FIELD_PREP(VE_COMP_CTRL_DCT_CHR, video->jpeg_quality | 0x10);

(quoted diff) +7 -0
+
+??? aspeed_video_update(video, VE_COMP_CTRL,
+??????????????? VE_COMP_CTRL_DCT_LUM | VE_COMP_CTRL_DCT_CHR,
+??????????????? comp_ctrl);
+}
+
+static void aspeed_video_update_subsampling(struct aspeed_video 

*video)

(quoted diff) +6 -0
+{
+??? if (video->jpeg.virt)
+??????? aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+??? if (video->yuv420)
+??????? aspeed_video_update(video, VE_SEQ_CTRL, 0, 

VE_SEQ_CTRL_YUV420);
+??? else
+??????? aspeed_video_update(video, VE_SEQ_CTRL, VE_SEQ_CTRL_YUV420, 0);

(quoted diff) +38 -0
+}
+
+static int aspeed_video_set_ctrl(struct v4l2_ctrl *ctrl)
+{
+??? struct aspeed_video *video = container_of(ctrl->handler,
+????????????????????????? struct aspeed_video,
+????????????????????????? ctrl_handler);
+
+??? switch (ctrl->id) {
+??? case V4L2_CID_JPEG_COMPRESSION_QUALITY:
+??????? video->jpeg_quality = ctrl->val;
+??????? aspeed_video_update_jpeg_quality(video);
+??????? break;
+??? case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
+??????? if (ctrl->val == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
+??????????? video->yuv420 = true;
+??????????? aspeed_video_update_subsampling(video);
+??????? } else {
+??????????? video->yuv420 = false;
+??????????? aspeed_video_update_subsampling(video);
+??????? }
+??????? break;
+??? default:
+??????? return -EINVAL;
+??? }
+
+??? return 0;
+}
+
+static const struct v4l2_ctrl_ops aspeed_video_ctrl_ops = {
+??? .s_ctrl = aspeed_video_set_ctrl,
+};
+
+static void aspeed_video_resolution_work(struct work_struct *work)
+{
+??? int rc;
+??? struct delayed_work *dwork = to_delayed_work(work);
+??? struct aspeed_video *video = container_of(dwork, struct 

aspeed_video,
+????????????????????????? res_work);

(quoted diff) +193 -0
+
+??? /* No clients remaining after delay */
+??? if (atomic_read(&video->clients) == 0)
+??????? goto done;
+
+??? aspeed_video_on(video);
+
+??? aspeed_video_init_regs(video);
+
+??? rc = aspeed_video_get_resolution(video);
+??? if (rc)
+??????? dev_err(video->dev,
+??????????? "resolution changed; couldn't get new resolution\n");
+??? else if (test_bit(VIDEO_STREAMING, &video->flags))
+??????? aspeed_video_start_frame(video);
+
+??? if (video->width != video->pix_fmt.width ||
+??????? video->height != video->pix_fmt.height) {
+??????? static const struct v4l2_event ev = {
+??????????? .type = V4L2_EVENT_SOURCE_CHANGE,
+??????????? .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
+??????? };
+
+??????? v4l2_event_queue(&video->vdev, &ev);
+??? }
+
+done:
+??? clear_bit(VIDEO_RES_CHANGE, &video->flags);
+??? wake_up_interruptible_all(&video->wait);
+}
+
+static int aspeed_video_open(struct file *file)
+{
+??? int rc;
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? mutex_lock(&video->video_lock);
+
+??? if (atomic_inc_return(&video->clients) == 1) {
+??????? rc = aspeed_video_start(video);
+??????? if (rc) {
+??????????? dev_err(video->dev, "Failed to start video engine\n");
+??????????? atomic_dec(&video->clients);
+??????????? mutex_unlock(&video->video_lock);
+??????????? return rc;
+??????? }
+??? }
+
+??? mutex_unlock(&video->video_lock);
+
+??? return v4l2_fh_open(file);
+}
+
+static int aspeed_video_release(struct file *file)
+{
+??? int rc;
+??? struct aspeed_video *video = video_drvdata(file);
+
+??? rc = vb2_fop_release(file);
+
+??? mutex_lock(&video->video_lock);
+
+??? if (atomic_dec_return(&video->clients) == 0)
+??????? aspeed_video_stop(video);
+
+??? mutex_unlock(&video->video_lock);
+
+??? return rc;
+}
+
+static const struct v4l2_file_operations aspeed_video_v4l2_fops = {
+??? .owner = THIS_MODULE,
+??? .read = vb2_fop_read,
+??? .poll = vb2_fop_poll,
+??? .unlocked_ioctl = video_ioctl2,
+??? .mmap = vb2_fop_mmap,
+??? .open = aspeed_video_open,
+??? .release = aspeed_video_release,
+};
+
+static int aspeed_video_queue_setup(struct vb2_queue *q,
+??????????????????? unsigned int *num_buffers,
+??????????????????? unsigned int *num_planes,
+??????????????????? unsigned int sizes[],
+??????????????????? struct device *alloc_devs[])
+{
+??? struct aspeed_video *video = vb2_get_drv_priv(q);
+
+??? if (*num_planes) {
+??????? if (sizes[0] < video->max_compressed_size)
+??????????? return -EINVAL;
+
+??????? return 0;
+??? }
+
+??? *num_planes = 1;
+??? sizes[0] = video->max_compressed_size;
+
+??? return 0;
+}
+
+static int aspeed_video_buf_prepare(struct vb2_buffer *vb)
+{
+??? struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+
+??? if (vb2_plane_size(vb, 0) < video->max_compressed_size)
+??????? return -EINVAL;
+
+??? return 0;
+}
+
+static int aspeed_video_start_streaming(struct vb2_queue *q,
+??????????????????? unsigned int count)
+{
+??? int rc;
+??? struct aspeed_video *video = vb2_get_drv_priv(q);
+
+??? rc = aspeed_video_start_frame(video);
+??? if (rc) {
+??????? aspeed_video_bufs_done(video, VB2_BUF_STATE_QUEUED);
+??????? return rc;
+??? }
+
+??? video->sequence = 0;
+??? set_bit(VIDEO_STREAMING, &video->flags);
+??? return 0;
+}
+
+static void aspeed_video_stop_streaming(struct vb2_queue *q)
+{
+??? int rc;
+??? struct aspeed_video *video = vb2_get_drv_priv(q);
+
+??? clear_bit(VIDEO_STREAMING, &video->flags);
+
+??? rc = wait_event_timeout(video->wait,
+??????????????? !test_bit(VIDEO_FRAME_INPRG, &video->flags),
+??????????????? STOP_TIMEOUT);
+??? if (!rc) {
+??????? dev_err(video->dev, "Timed out when stopping streaming\n");
+??????? aspeed_video_stop(video);
+??? }
+
+??? aspeed_video_bufs_done(video, VB2_BUF_STATE_ERROR);
+}
+
+static void aspeed_video_buf_queue(struct vb2_buffer *vb)
+{
+??? struct aspeed_video *video = vb2_get_drv_priv(vb->vb2_queue);
+??? struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+??? struct aspeed_video_buffer *avb = to_aspeed_video_buffer(vbuf);
+??? unsigned long flags;
+
+??? spin_lock_irqsave(&video->lock, flags);
+??? list_add_tail(&avb->link, &video->buffers);
+??? spin_unlock_irqrestore(&video->lock, flags);
+}
+
+static const struct vb2_ops aspeed_video_vb2_ops = {
+??? .queue_setup = aspeed_video_queue_setup,
+??? .wait_prepare = vb2_ops_wait_prepare,
+??? .wait_finish = vb2_ops_wait_finish,
+??? .buf_prepare = aspeed_video_buf_prepare,
+??? .start_streaming = aspeed_video_start_streaming,
+??? .stop_streaming = aspeed_video_stop_streaming,
+??? .buf_queue =? aspeed_video_buf_queue,
+};
+
+static int aspeed_video_setup_video(struct aspeed_video *video)
+{
+??? const u64 mask = ~(BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_444) |
+?????????????? BIT(V4L2_JPEG_CHROMA_SUBSAMPLING_420));
+??? struct v4l2_device *v4l2_dev = &video->v4l2_dev;
+??? struct vb2_queue *vbq = &video->queue;
+??? struct video_device *vdev = &video->vdev;
+??? int rc;
+
+??? video->pix_fmt.pixelformat = V4L2_PIX_FMT_JPEG;
+??? video->pix_fmt.field = V4L2_FIELD_NONE;
+??? video->pix_fmt.colorspace = V4L2_COLORSPACE_SRGB;
+??? video->pix_fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
+
+??? rc = v4l2_device_register(video->dev, v4l2_dev);
+??? if (rc) {
+??????? dev_err(video->dev, "Failed to register v4l2 device\n");
+??????? return rc;
+??? }
+
+??? v4l2_ctrl_handler_init(&video->ctrl_handler, 2);
+??? v4l2_ctrl_new_std(&video->ctrl_handler, &aspeed_video_ctrl_ops,
+????????????? V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
+????????????? ASPEED_VIDEO_JPEG_NUM_QUALITIES - 1, 1, 0);
+??? v4l2_ctrl_new_std_menu(&video->ctrl_handler, 

&aspeed_video_ctrl_ops,
+?????????????????? V4L2_CID_JPEG_CHROMA_SUBSAMPLING, +?????????????????? V4L2_JPEG_CHROMA_SUBSAMPLING_420, mask, +?????????????????? V4L2_JPEG_CHROMA_SUBSAMPLING_444);

(quoted diff) +25 -0
+
+??? if (video->ctrl_handler.error) {
+??????? v4l2_ctrl_handler_free(&video->ctrl_handler);
+??????? v4l2_device_unregister(v4l2_dev);
+
+??????? dev_err(video->dev, "Failed to init controls: %d\n",
+??????????? video->ctrl_handler.error);
+??????? return rc;
+??? }
+
+??? v4l2_dev->ctrl_handler = &video->ctrl_handler;
+
+??? vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+??? vbq->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
+??? vbq->dev = v4l2_dev->dev;
+??? vbq->lock = &video->video_lock;
+??? vbq->ops = &aspeed_video_vb2_ops;
+??? vbq->mem_ops = &vb2_dma_contig_memops;
+??? vbq->drv_priv = video;
+??? vbq->buf_struct_size = sizeof(struct aspeed_video_buffer);
+??? vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+??? vbq->min_buffers_needed = 3;
+
+??? rc = vb2_queue_init(vbq);
+??? if (rc) {

Review

Quoted 10 lines
(quoted diff) +10 -0
+??????? v4l2_ctrl_handler_free(&video->ctrl_handler);
+??????? v4l2_device_unregister(v4l2_dev);
+
+??????? dev_err(video->dev, "Failed to init vb2 queue\n");
+??????? return rc;
+??? }
+
+??? vdev->queue = vbq;
+??? vdev->fops = &aspeed_video_v4l2_fops;
+??? vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |

READWRITE doesn't work for JPEG since there is no clear end of a frame. Just drop
this and the read op in aspeed_video_v4l2_fops.

Quoted 154 lines

+??????? V4L2_CAP_STREAMING;
+??? vdev->v4l2_dev = v4l2_dev;
+??? strscpy(vdev->name, DEVICE_NAME, sizeof(vdev->name)); +??? vdev->vfl_type = VFL_TYPE_GRABBER;
+??? vdev->vfl_dir = VFL_DIR_RX;
+??? vdev->release = video_device_release_empty; +??? vdev->ioctl_ops = &aspeed_video_ioctl_ops; +??? vdev->lock = &video->video_lock;

(quoted diff) +126 -0
+
+??? video_set_drvdata(vdev, video);
+??? rc = video_register_device(vdev, VFL_TYPE_GRABBER, 0);
+??? if (rc) {
+??????? vb2_queue_release(vbq);
+??????? v4l2_ctrl_handler_free(&video->ctrl_handler);
+??????? v4l2_device_unregister(v4l2_dev);
+
+??????? dev_err(video->dev, "Failed to register video device\n");
+??????? return rc;
+??? }
+
+??? return 0;
+}
+
+static int aspeed_video_init(struct aspeed_video *video)
+{
+??? int irq;
+??? int rc;
+??? struct device *dev = video->dev;
+
+??? irq = irq_of_parse_and_map(dev->of_node, 0);
+??? if (!irq) {
+??????? dev_err(dev, "Unable to find IRQ\n");
+??????? return -ENODEV;
+??? }
+
+??? rc = devm_request_irq(dev, irq, aspeed_video_irq, IRQF_SHARED,
+????????????????? DEVICE_NAME, video);
+??? if (rc < 0) {
+??????? dev_err(dev, "Unable to request IRQ %d\n", irq);
+??????? return rc;
+??? }
+
+??? video->eclk = devm_clk_get(dev, "eclk");
+??? if (IS_ERR(video->eclk)) {
+??????? dev_err(dev, "Unable to get ECLK\n");
+??????? return PTR_ERR(video->eclk);
+??? }
+
+??? video->vclk = devm_clk_get(dev, "vclk");
+??? if (IS_ERR(video->vclk)) {
+??????? dev_err(dev, "Unable to get VCLK\n");
+??????? return PTR_ERR(video->vclk);
+??? }
+
+??? video->rst = devm_reset_control_get_exclusive(dev, NULL);
+??? if (IS_ERR(video->rst)) {
+??????? dev_err(dev, "Unable to get VE reset\n");
+??????? return PTR_ERR(video->rst);
+??? }
+
+??? rc = of_reserved_mem_device_init(dev);
+??? if (rc) {
+??????? dev_err(dev, "Unable to reserve memory\n");
+??????? return rc;
+??? }
+
+??? rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+??? if (rc) {
+??????? dev_err(dev, "Failed to set DMA mask\n");
+??????? of_reserved_mem_device_release(dev);
+??????? return rc;
+??? }
+
+??? if (!aspeed_video_alloc_buf(video, &video->jpeg,
+??????????????????? VE_JPEG_HEADER_SIZE)) {
+??????? dev_err(dev, "Failed to allocate DMA for JPEG header\n");
+??????? of_reserved_mem_device_release(dev);
+??????? return rc;
+??? }
+
+??? aspeed_video_init_jpeg_table(video->jpeg.virt, video->yuv420);
+
+??? return 0;
+}
+
+static int aspeed_video_probe(struct platform_device *pdev)
+{
+??? int rc;
+??? struct resource *res;
+??? struct aspeed_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
+
+??? if (!video)
+??????? return -ENOMEM;
+
+??? video->frame_rate = 30;
+??? video->dev = &pdev->dev;
+??? mutex_init(&video->video_lock);
+??? init_waitqueue_head(&video->wait);
+??? INIT_DELAYED_WORK(&video->res_work, aspeed_video_resolution_work);
+??? INIT_LIST_HEAD(&video->buffers);
+
+??? res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+
+??? video->base = devm_ioremap_resource(video->dev, res);
+
+??? if (IS_ERR(video->base))
+??????? return PTR_ERR(video->base);
+
+??? rc = aspeed_video_init(video);
+??? if (rc)
+??????? return rc;
+
+??? rc = aspeed_video_setup_video(video);
+??? if (rc)
+??????? return rc;
+
+??? return 0;
+}
+
+static int aspeed_video_remove(struct platform_device *pdev)
+{
+??? struct device *dev = &pdev->dev;
+??? struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
+??? struct aspeed_video *video = to_aspeed_video(v4l2_dev);
+
+??? video_unregister_device(&video->vdev);
+
+??? vb2_queue_release(&video->queue);
+
+??? v4l2_ctrl_handler_free(&video->ctrl_handler);
+
+??? v4l2_device_unregister(v4l2_dev);
+
+??? dma_free_coherent(video->dev, VE_JPEG_HEADER_SIZE, 

video->jpeg.virt,
+????????????? video->jpeg.dma);

drivers/media/platform/aspeed-video.c new file +17 -0
+
+??? of_reserved_mem_device_release(dev);
+
+??? return 0;
+}
+
+static const struct of_device_id aspeed_video_of_match[] = {
+??? { .compatible = "aspeed,ast2400-video-engine" },
+??? { .compatible = "aspeed,ast2500-video-engine" },
+??? {}
+};
+MODULE_DEVICE_TABLE(of, aspeed_video_of_match);
+
+static struct platform_driver aspeed_video_driver = {
+??? .driver = {
+??????? .name = DEVICE_NAME,
+??????? .of_match_table = aspeed_video_of_match,

drivers/media/platform/aspeed-video.c line 1674

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+??? },
+??? .probe = aspeed_video_probe,
+??? .remove = aspeed_video_remove,
+};
+
+module_platform_driver(aspeed_video_driver);
+
+MODULE_DESCRIPTION("ASPEED Video Engine Driver");
+MODULE_AUTHOR("Eddie James");
+MODULE_LICENSE("GPL v2");

Regards,

????Hans

#7 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

Hi Eddie,

Sorry for the delay, I've been away for the past two weeks and I've been catching up ever since I came back.

Quoted 190 lines · Eddie James
Quoted 187 lines · Eddie James
Quoted 161 lines · Hans Verkuil
Quoted 20 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With
the Aspeed chip acting a service processor, the Video Engine can capture
the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
  MAINTAINERS                           |    8 +
  drivers/media/platform/Kconfig        |    8 +
  drivers/media/platform/Makefile       |    1 +
  drivers/media/platform/aspeed-video.c | 1674 
+++++++++++++++++++++++++++++++++
  4 files changed, 1691 insertions(+)

create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

drivers/media/platform/aspeed-video.c line 985

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+static int aspeed_video_enum_input(struct file *file, void *fh,
+                   struct v4l2_input *inp)
+{
+    if (inp->index)
+        return -EINVAL;
+
+    strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
+    inp->type = V4L2_INPUT_TYPE_CAMERA;
+    inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
+    inp->status = V4L2_IN_ST_NO_SIGNAL | V4L2_IN_ST_NO_SYNC;

This can't be right. If there is a valid signal, then status should be 0.
And ideally you can tell the difference between no signal and no sync as well.

Quoted 46 lines
drivers/media/platform/aspeed-video.c new file +5 -0
+
+    return 0;
+}
+
+static int aspeed_video_get_input(struct file *file, void *fh, 

unsigned int *i)

drivers/media/platform/aspeed-video.c new file +7 -0
+{
+    *i = 0;
+
+    return 0;
+}
+
+static int aspeed_video_set_input(struct file *file, void *fh, 

unsigned int i)

drivers/media/platform/aspeed-video.c new file +32 -0
+{
+    if (i)
+        return -EINVAL;
+
+    return 0;
+}
+
+static int aspeed_video_get_parm(struct file *file, void *fh,
+                 struct v4l2_streamparm *a)
+{
+    struct aspeed_video *video = video_drvdata(file);
+
+    a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+    a->parm.capture.readbuffers = 3;
+    a->parm.capture.timeperframe.numerator = 1;
+    if (!video->frame_rate)
+        a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;
+    else
+        a->parm.capture.timeperframe.denominator = video->frame_rate;
+
+    return 0;
+}
+
+static int aspeed_video_set_parm(struct file *file, void *fh,
+                 struct v4l2_streamparm *a)
+{
+    unsigned int frame_rate = 0;
+    struct aspeed_video *video = video_drvdata(file);
+
+    a->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+    a->parm.capture.readbuffers = 3;
+

drivers/media/platform/aspeed-video.c line 1039

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+    if (a->parm.capture.timeperframe.numerator)
+        frame_rate = a->parm.capture.timeperframe.denominator /
+            a->parm.capture.timeperframe.numerator;
+
+    if (!frame_rate || frame_rate > MAX_FRAME_RATE) {
+        frame_rate = 0;
+
+        /*
+         * Set to max + 1 to differentiate between max and 0, which
+         * means "don't care".

But what does "don't care" mean in practice? It's still not clear to me how this
is supposed to work.

drivers/media/platform/aspeed-video.c line 1041

Quoted 2 lines
drivers/media/platform/aspeed-video.c new file +2 -0
+         */
+        a->parm.capture.timeperframe.denominator = MAX_FRAME_RATE + 1;

And regardless of anything else this timeperframe is out of the range that
aspeed_video_enum_frameintervals() returns.

Quoted 57 lines

+ a->parm.capture.timeperframe.numerator = 1;

drivers/media/platform/aspeed-video.c new file +30 -0
+    }
+
+    if (video->frame_rate != frame_rate) {
+        video->frame_rate = frame_rate;
+        aspeed_video_update(video, VE_CTRL, VE_CTRL_FRC,
+                    FIELD_PREP(VE_CTRL_FRC, frame_rate));
+    }
+
+    return 0;
+}
+
+static int aspeed_video_enum_framesizes(struct file *file, void *fh,
+                    struct v4l2_frmsizeenum *fsize)
+{
+    struct aspeed_video *video = video_drvdata(file);
+
+    if (fsize->index)
+        return -EINVAL;
+
+    if (fsize->pixel_format != V4L2_PIX_FMT_JPEG)
+        return -EINVAL;
+
+    fsize->discrete.width = video->pix_fmt.width;
+    fsize->discrete.height = video->pix_fmt.height;
+    fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+
+    return 0;
+}
+
+static int aspeed_video_enum_frameintervals(struct file *file, void 

*fh,

drivers/media/platform/aspeed-video.c new file +8 -0
+                        struct v4l2_frmivalenum *fival)
+{
+    struct aspeed_video *video = video_drvdata(file);
+
+    if (fival->index)
+        return -EINVAL;
+
+    if (fival->width != video->width || fival->height != 

video->height)

drivers/media/platform/aspeed-video.c new file +16 -0
+        return -EINVAL;
+
+    if (fival->pixel_format != V4L2_PIX_FMT_JPEG)
+        return -EINVAL;
+
+    fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
+
+    fival->stepwise.min.denominator = MAX_FRAME_RATE;
+    fival->stepwise.min.numerator = 1;
+    fival->stepwise.max.denominator = 1;
+    fival->stepwise.max.numerator = 1;
+    fival->stepwise.step = fival->stepwise.max;
+
+    return 0;
+}
+
Quoted 8 lines

drivers/media/platform/aspeed-video.c line 1101

Quoted 5 lines
drivers/media/platform/aspeed-video.c new file +5 -0
+static int aspeed_video_set_dv_timings(struct file *file, void *fh,
+                       struct v4l2_dv_timings *timings)
+{
+    struct aspeed_video *video = video_drvdata(file);
+

If vb2_is_busy() returns true, then return -EBUSY here. It is not allowed to
set the timings while vb2 is busy.

Buffer ioctls (Input 0):
fail: v4l2-test-buffers.cpp(344): node->s_dv_timings(timings)
fail: v4l2-test-buffers.cpp(452): testCanSetSameTimings(node)
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: FAIL

Streaming ioctls:
test read/write: OK
test blocking wait: OK
fail: v4l2-test-buffers.cpp(344): node->s_dv_timings(timings)
fail: v4l2-test-buffers.cpp(637): testCanSetSameTimings(node)
fail: v4l2-test-buffers.cpp(952): captureBufs(node, q, m2m_q,
frame_count, false)
test MMAP: FAIL

Quoted 8 lines
Quoted 6 lines

Built from v4l-utils c36dbbdfa8b30b2badd4f893b59d0bd4f0bd12aa

Adding this causes some of the v4l2-compliance streaming tests to fail, and prevents my own application from being able to call S_DV_TIMINGS after detecting a resolution change, despite calling streamoff and unmapping all the buffers first.

Any thoughts on this Hans?

The compliance fail is because there is a requirement that if you set new timings that are equal to the existing timings, then that should work (and is effectively a NOP operation).

So in the code you can do something similar to what vivid does:

        if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
                return 0;
        if (vb2_is_busy(&dev->vb_vid_cap_q))
                return -EBUSY;

You can probably get away with just checking width, height and interlaced in this driver, rather than calling v4l2_match_dv_timings.

If the resolution changes, however, then you need to completely free all buffers (i.e. close the fd or call VIDIOC_REQBUFS with count = 0) before you can set new timings. So streamoff + unmap is not enough.

Streamoff only returns the buffers to userspace, it doesn't free them.

Regards,

Hans

#8 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

In reply to Eddie James
Quoted 17 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting a service processor, the Video Engine can capture the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
 MAINTAINERS                           |    8 +
 drivers/media/platform/Kconfig        |    8 +
 drivers/media/platform/Makefile       |    1 +
 drivers/media/platform/aspeed-video.c | 1674 +++++++++++++++++++++++++++++++++
 4 files changed, 1691 insertions(+)
 create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

Quoted 183 lines
drivers/media/platform/aspeed-video.c new file +183 -0
+static void aspeed_video_check_polarity(struct aspeed_video *video)
+{
+	int i;
+	int hsync_counter = 0;
+	int vsync_counter = 0;
+	u32 sts;
+
+	for (i = 0; i < NUM_POLARITY_CHECKS; ++i) {
+		sts = aspeed_video_read(video, VE_MODE_DETECT_STATUS);
+		if (sts & VE_MODE_DETECT_STATUS_VSYNC)
+			vsync_counter--;
+		else
+			vsync_counter++;
+
+		if (sts & VE_MODE_DETECT_STATUS_HSYNC)
+			hsync_counter--;
+		else
+			hsync_counter++;
+	}
+
+	if (hsync_counter < 0 || vsync_counter < 0) {
+		u32 ctrl;
+
+		if (hsync_counter < 0)
+			ctrl = VE_CTRL_HSYNC_POL;
+		else
+			video->timings.polarities |= V4L2_DV_HSYNC_POS_POL;
+
+		if (vsync_counter < 0)
+			ctrl = VE_CTRL_VSYNC_POL;
+		else
+			video->timings.polarities |= V4L2_DV_VSYNC_POS_POL;
+
+		aspeed_video_update(video, VE_CTRL, 0, ctrl);
+	}
+}
+
+static bool aspeed_video_alloc_buf(struct aspeed_video *video,
+				   struct aspeed_video_addr *addr,
+				   unsigned int size)
+{
+	addr->virt = dma_alloc_coherent(video->dev, size, &addr->dma,
+					GFP_KERNEL);
+	if (!addr->virt)
+		return false;
+
+	addr->size = size;
+	return true;
+}
+
+static void aspeed_video_free_buf(struct aspeed_video *video,
+				  struct aspeed_video_addr *addr)
+{
+	dma_free_coherent(video->dev, addr->size, addr->virt, addr->dma);
+	addr->size = 0;
+	addr->dma = 0ULL;
+	addr->virt = NULL;
+}
+
+/*
+ * Get the minimum HW-supported compression buffer size for the frame size.
+ * Assume worst-case JPEG compression size is 1/8 raw size. This should be
+ * plenty even for maximum quality; any worse and the engine will simply return
+ * incomplete JPEGs.
+ */
+static void aspeed_video_calc_compressed_size(struct aspeed_video *video)
+{
+	int i, j;
+	u32 compression_buffer_size_reg = 0;
+	unsigned int size;
+	const unsigned int num_compression_packets = 4;
+	const unsigned int compression_packet_size = 1024;
+	const unsigned int max_compressed_size =
+		video->width * video->height / 2;	/* 4 Bpp / 8 */
+
+	video->max_compressed_size = UINT_MAX;
+
+	for (i = 0; i < 6; ++i) {
+		for (j = 0; j < 8; ++j) {
+			size = (num_compression_packets << i) *
+				(compression_packet_size << j);
+			if (size < max_compressed_size)
+				continue;
+
+			if (size < video->max_compressed_size) {
+				compression_buffer_size_reg = (i << 3) | j;
+				video->max_compressed_size = size;
+			}
+		}
+	}
+
+	aspeed_video_write(video, VE_STREAM_BUF_SIZE,
+			   compression_buffer_size_reg);
+
+	dev_dbg(video->dev, "max compressed size: %x\n",
+		video->max_compressed_size);
+}
+
+#define res_check(v) test_and_clear_bit(VIDEO_MODE_DETECT_DONE, &(v)->flags)
+
+static int aspeed_video_get_resolution(struct aspeed_video *video)
+{
+	bool invalid_resolution = true;
+	int rc;
+	int tries = 0;
+	unsigned int bottom;
+	unsigned int left;
+	unsigned int right;
+	unsigned int size;
+	unsigned int top;
+	u32 mds;
+	u32 src_lr_edge;
+	u32 src_tb_edge;
+	u32 sync;
+	struct aspeed_video_addr src;
+
+	if (video->srcs[1].size)
+		aspeed_video_free_buf(video, &video->srcs[1]);
+
+	if (video->srcs[0].size >= VE_MAX_SRC_BUFFER_SIZE) {
+		src = video->srcs[0];
+	} else {
+		if (video->srcs[0].size)
+			aspeed_video_free_buf(video, &video->srcs[0]);
+
+		if (!aspeed_video_alloc_buf(video, &src,
+					    VE_MAX_SRC_BUFFER_SIZE))
+			goto err_mem;
+	}
+
+	aspeed_video_write(video, VE_SRC0_ADDR, src.dma);
+
+	video->width = 0;
+	video->height = 0;
+
+	do {
+		if (tries) {
+			set_current_state(TASK_INTERRUPTIBLE);
+			if (schedule_timeout(INVALID_RESOLUTION_DELAY))
+				return -EINTR;
+		}
+
+		aspeed_video_start_mode_detect(video);
+
+		rc = wait_event_interruptible_timeout(video->wait,
+						      res_check(video),
+						      MODE_DETECT_TIMEOUT);
+		if (!rc) {
+			dev_err(video->dev, "timed out on 1st mode detect\n");
+			aspeed_video_disable_mode_detect(video);
+			return -ETIMEDOUT;
+		}
+
+		/* Disable mode detect in order to re-trigger */
+		aspeed_video_update(video, VE_SEQ_CTRL,
+				    VE_SEQ_CTRL_TRIG_MODE_DET, 0);
+
+		aspeed_video_check_polarity(video);
+
+		aspeed_video_start_mode_detect(video);
+
+		rc = wait_event_interruptible_timeout(video->wait,
+						      res_check(video),
+						      MODE_DETECT_TIMEOUT);
+		if (!rc) {
+			dev_err(video->dev, "timed out on 2nd mode detect\n");
+			aspeed_video_disable_mode_detect(video);
+			return -ETIMEDOUT;
+		}
+
+		src_lr_edge = aspeed_video_read(video, VE_SRC_LR_EDGE_DET);
+		src_tb_edge = aspeed_video_read(video, VE_SRC_TB_EDGE_DET);
+		mds = aspeed_video_read(video, VE_MODE_DETECT_STATUS);
+		sync = aspeed_video_read(video, VE_SYNC_STATUS);
+
+		bottom = (src_tb_edge & VE_SRC_TB_EDGE_DET_BOT) >>
+			VE_SRC_TB_EDGE_DET_BOT_SHF;
+		top = src_tb_edge & VE_SRC_TB_EDGE_DET_TOP;
+		video->timings.vfrontporch = top;
+		video->timings.vbackporch = ((mds & VE_MODE_DETECT_V_LINES) >>
+			VE_MODE_DETECT_V_LINES_SHF) - bottom;
+		video->timings.vsync = (sync & VE_SYNC_STATUS_VSYNC) >>
+			VE_SYNC_STATUS_VSYNC_SHF;

drivers/media/platform/aspeed-video.c line 785

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+		if (top > bottom)
+			continue;
+
+		right = (src_lr_edge & VE_SRC_LR_EDGE_DET_RT) >>
+			VE_SRC_LR_EDGE_DET_RT_SHF;
+		left = src_lr_edge & VE_SRC_LR_EDGE_DET_LEFT;
+		video->timings.hfrontporch = left;
+		video->timings.hbackporch = (mds & VE_MODE_DETECT_H_PIXELS) -
+			right;
+		video->timings.hsync = sync & VE_SYNC_STATUS_HSYNC;

It's a bit odd that timings.width/height isn't set here.

drivers/media/platform/aspeed-video.c line 795

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+		if (left > right)
+			continue;
+
+		invalid_resolution = false;
+	} while (invalid_resolution && (tries++ < INVALID_RESOLUTION_RETRIES));
+
+	if (invalid_resolution) {
+		dev_err(video->dev, "invalid resolution detected\n");
+		return -ERANGE;
+	}

So far, so good. You now have detected the new resolution, but...

drivers/media/platform/aspeed-video.c line 799

Quoted 4 lines
drivers/media/platform/aspeed-video.c new file +4 -0
+
+	video->height = (bottom - top) + 1;
+	video->width = (right - left) + 1;
+	size = video->height * video->width;

here and below you appear to actually switch the capture timings to the new resolution as well.

That should only happen when s_dv_timings is called.

It is always very dangerous to automatically switch resolution since a new resolution might mean larger buffers, and userspace has to prepare for that.

The normal sequence is:

1) the driver detects a change in resolution (or the video signal disappears completely). It will send V4L2_EVENT_SOURCE_CHANGE to let userspace know.

2) Userspace stops streaming, frees all buffers, calls QUERY_DV_TIMINGS and, if valid, follows with S_DV_TIMINGS.

3) Userspace calls REQBUFS to allocate the new buffers sized for the new timings and starts streaming again.

That way everything remains under control of the application.

Quoted 54 lines
drivers/media/platform/aspeed-video.c new file +54 -0
+
+	/* Don't use direct mode below 1024 x 768 (irqs don't fire) */
+	if (size < DIRECT_FETCH_THRESHOLD) {
+		aspeed_video_write(video, VE_TGS_0,
+				   FIELD_PREP(VE_TGS_FIRST, left - 1) |
+				   FIELD_PREP(VE_TGS_LAST, right));
+		aspeed_video_write(video, VE_TGS_1,
+				   FIELD_PREP(VE_TGS_FIRST, top) |
+				   FIELD_PREP(VE_TGS_LAST, bottom + 1));
+		aspeed_video_update(video, VE_CTRL, 0, VE_CTRL_INT_DE);
+	} else {
+		aspeed_video_update(video, VE_CTRL, 0, VE_CTRL_DIRECT_FETCH);
+	}
+
+	aspeed_video_write(video, VE_CAP_WINDOW,
+			   video->width << 16 | video->height);
+	aspeed_video_write(video, VE_COMP_WINDOW,
+			   video->width << 16 | video->height);
+	aspeed_video_write(video, VE_SRC_SCANLINE_OFFSET, video->width * 4);
+
+	aspeed_video_update(video, VE_INTERRUPT_CTRL, 0,
+			    VE_INTERRUPT_MODE_DETECT_WD);
+	aspeed_video_update(video, VE_SEQ_CTRL, 0,
+			    VE_SEQ_CTRL_AUTO_COMP | VE_SEQ_CTRL_EN_WATCHDOG);
+
+	dev_dbg(video->dev, "got resolution[%dx%d]\n", video->width,
+		video->height);
+
+	size *= 4;
+	if (size == src.size / 2) {
+		aspeed_video_write(video, VE_SRC1_ADDR, src.dma + size);
+		video->srcs[0] = src;
+	} else if (size == src.size) {
+		video->srcs[0] = src;
+
+		if (!aspeed_video_alloc_buf(video, &video->srcs[1], size))
+			goto err_mem;
+
+		aspeed_video_write(video, VE_SRC1_ADDR, video->srcs[1].dma);
+	} else {
+		aspeed_video_free_buf(video, &src);
+
+		if (!aspeed_video_alloc_buf(video, &video->srcs[0], size))
+			goto err_mem;
+
+		if (!aspeed_video_alloc_buf(video, &video->srcs[1], size))
+			goto err_mem;
+
+		aspeed_video_write(video, VE_SRC0_ADDR, video->srcs[0].dma);
+		aspeed_video_write(video, VE_SRC1_ADDR, video->srcs[1].dma);
+	}
+
+	aspeed_video_calc_compressed_size(video);
+

drivers/media/platform/aspeed-video.c line 863

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	return 0;
+
+err_mem:
+	dev_err(video->dev, "failed to allocate source buffers\n");
+
+	if (video->srcs[0].size)
+		aspeed_video_free_buf(video, &video->srcs[0]);
+
+	return -ENOMEM;
+}

Regards,

Hans

#9 lore raw

Re: [PATCH v4 2/2] media: platform: Add Aspeed Video Engine driver

Quoted 227 lines · Hans Verkuil
Quoted 17 lines · Eddie James

The Video Engine (VE) embedded in the Aspeed AST2400 and AST2500 SOCs can capture and compress video data from digital or analog sources. With the Aspeed chip acting a service processor, the Video Engine can capture the host processor graphics output.

Add a V4L2 driver to capture video data and compress it to JPEG images. Make the video frames available through the V4L2 streaming interface.

---
  MAINTAINERS                           |    8 +
  drivers/media/platform/Kconfig        |    8 +
  drivers/media/platform/Makefile       |    1 +
  drivers/media/platform/aspeed-video.c | 1674 +++++++++++++++++++++++++++++++++
  4 files changed, 1691 insertions(+)

create mode 100644 drivers/media/platform/aspeed-video.c

<snip>

Quoted 183 lines
drivers/media/platform/aspeed-video.c new file +183 -0
+static void aspeed_video_check_polarity(struct aspeed_video *video)
+{
+	int i;
+	int hsync_counter = 0;
+	int vsync_counter = 0;
+	u32 sts;
+
+	for (i = 0; i < NUM_POLARITY_CHECKS; ++i) {
+		sts = aspeed_video_read(video, VE_MODE_DETECT_STATUS);
+		if (sts & VE_MODE_DETECT_STATUS_VSYNC)
+			vsync_counter--;
+		else
+			vsync_counter++;
+
+		if (sts & VE_MODE_DETECT_STATUS_HSYNC)
+			hsync_counter--;
+		else
+			hsync_counter++;
+	}
+
+	if (hsync_counter < 0 || vsync_counter < 0) {
+		u32 ctrl;
+
+		if (hsync_counter < 0)
+			ctrl = VE_CTRL_HSYNC_POL;
+		else
+			video->timings.polarities |= V4L2_DV_HSYNC_POS_POL;
+
+		if (vsync_counter < 0)
+			ctrl = VE_CTRL_VSYNC_POL;
+		else
+			video->timings.polarities |= V4L2_DV_VSYNC_POS_POL;
+
+		aspeed_video_update(video, VE_CTRL, 0, ctrl);
+	}
+}
+
+static bool aspeed_video_alloc_buf(struct aspeed_video *video,
+				   struct aspeed_video_addr *addr,
+				   unsigned int size)
+{
+	addr->virt = dma_alloc_coherent(video->dev, size, &addr->dma,
+					GFP_KERNEL);
+	if (!addr->virt)
+		return false;
+
+	addr->size = size;
+	return true;
+}
+
+static void aspeed_video_free_buf(struct aspeed_video *video,
+				  struct aspeed_video_addr *addr)
+{
+	dma_free_coherent(video->dev, addr->size, addr->virt, addr->dma);
+	addr->size = 0;
+	addr->dma = 0ULL;
+	addr->virt = NULL;
+}
+
+/*
+ * Get the minimum HW-supported compression buffer size for the frame size.
+ * Assume worst-case JPEG compression size is 1/8 raw size. This should be
+ * plenty even for maximum quality; any worse and the engine will simply return
+ * incomplete JPEGs.
+ */
+static void aspeed_video_calc_compressed_size(struct aspeed_video *video)
+{
+	int i, j;
+	u32 compression_buffer_size_reg = 0;
+	unsigned int size;
+	const unsigned int num_compression_packets = 4;
+	const unsigned int compression_packet_size = 1024;
+	const unsigned int max_compressed_size =
+		video->width * video->height / 2;	/* 4 Bpp / 8 */
+
+	video->max_compressed_size = UINT_MAX;
+
+	for (i = 0; i < 6; ++i) {
+		for (j = 0; j < 8; ++j) {
+			size = (num_compression_packets << i) *
+				(compression_packet_size << j);
+			if (size < max_compressed_size)
+				continue;
+
+			if (size < video->max_compressed_size) {
+				compression_buffer_size_reg = (i << 3) | j;
+				video->max_compressed_size = size;
+			}
+		}
+	}
+
+	aspeed_video_write(video, VE_STREAM_BUF_SIZE,
+			   compression_buffer_size_reg);
+
+	dev_dbg(video->dev, "max compressed size: %x\n",
+		video->max_compressed_size);
+}
+
+#define res_check(v) test_and_clear_bit(VIDEO_MODE_DETECT_DONE, &(v)->flags)
+
+static int aspeed_video_get_resolution(struct aspeed_video *video)
+{
+	bool invalid_resolution = true;
+	int rc;
+	int tries = 0;
+	unsigned int bottom;
+	unsigned int left;
+	unsigned int right;
+	unsigned int size;
+	unsigned int top;
+	u32 mds;
+	u32 src_lr_edge;
+	u32 src_tb_edge;
+	u32 sync;
+	struct aspeed_video_addr src;
+
+	if (video->srcs[1].size)
+		aspeed_video_free_buf(video, &video->srcs[1]);
+
+	if (video->srcs[0].size >= VE_MAX_SRC_BUFFER_SIZE) {
+		src = video->srcs[0];
+	} else {
+		if (video->srcs[0].size)
+			aspeed_video_free_buf(video, &video->srcs[0]);
+
+		if (!aspeed_video_alloc_buf(video, &src,
+					    VE_MAX_SRC_BUFFER_SIZE))
+			goto err_mem;
+	}
+
+	aspeed_video_write(video, VE_SRC0_ADDR, src.dma);
+
+	video->width = 0;
+	video->height = 0;
+
+	do {
+		if (tries) {
+			set_current_state(TASK_INTERRUPTIBLE);
+			if (schedule_timeout(INVALID_RESOLUTION_DELAY))
+				return -EINTR;
+		}
+
+		aspeed_video_start_mode_detect(video);
+
+		rc = wait_event_interruptible_timeout(video->wait,
+						      res_check(video),
+						      MODE_DETECT_TIMEOUT);
+		if (!rc) {
+			dev_err(video->dev, "timed out on 1st mode detect\n");
+			aspeed_video_disable_mode_detect(video);
+			return -ETIMEDOUT;
+		}
+
+		/* Disable mode detect in order to re-trigger */
+		aspeed_video_update(video, VE_SEQ_CTRL,
+				    VE_SEQ_CTRL_TRIG_MODE_DET, 0);
+
+		aspeed_video_check_polarity(video);
+
+		aspeed_video_start_mode_detect(video);
+
+		rc = wait_event_interruptible_timeout(video->wait,
+						      res_check(video),
+						      MODE_DETECT_TIMEOUT);
+		if (!rc) {
+			dev_err(video->dev, "timed out on 2nd mode detect\n");
+			aspeed_video_disable_mode_detect(video);
+			return -ETIMEDOUT;
+		}
+
+		src_lr_edge = aspeed_video_read(video, VE_SRC_LR_EDGE_DET);
+		src_tb_edge = aspeed_video_read(video, VE_SRC_TB_EDGE_DET);
+		mds = aspeed_video_read(video, VE_MODE_DETECT_STATUS);
+		sync = aspeed_video_read(video, VE_SYNC_STATUS);
+
+		bottom = (src_tb_edge & VE_SRC_TB_EDGE_DET_BOT) >>
+			VE_SRC_TB_EDGE_DET_BOT_SHF;
+		top = src_tb_edge & VE_SRC_TB_EDGE_DET_TOP;
+		video->timings.vfrontporch = top;
+		video->timings.vbackporch = ((mds & VE_MODE_DETECT_V_LINES) >>
+			VE_MODE_DETECT_V_LINES_SHF) - bottom;
+		video->timings.vsync = (sync & VE_SYNC_STATUS_VSYNC) >>
+			VE_SYNC_STATUS_VSYNC_SHF;

drivers/media/platform/aspeed-video.c line 785

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+		if (top > bottom)
+			continue;
+
+		right = (src_lr_edge & VE_SRC_LR_EDGE_DET_RT) >>
+			VE_SRC_LR_EDGE_DET_RT_SHF;
+		left = src_lr_edge & VE_SRC_LR_EDGE_DET_LEFT;
+		video->timings.hfrontporch = left;
+		video->timings.hbackporch = (mds & VE_MODE_DETECT_H_PIXELS) -
+			right;
+		video->timings.hsync = sync & VE_SYNC_STATUS_HSYNC;

It's a bit odd that timings.width/height isn't set here.

drivers/media/platform/aspeed-video.c line 795

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+		if (left > right)
+			continue;
+
+		invalid_resolution = false;
+	} while (invalid_resolution && (tries++ < INVALID_RESOLUTION_RETRIES));
+
+	if (invalid_resolution) {
+		dev_err(video->dev, "invalid resolution detected\n");
+		return -ERANGE;
+	}

So far, so good. You now have detected the new resolution, but...

Quoted 6 lines

drivers/media/platform/aspeed-video.c line 799

Quoted 4 lines
drivers/media/platform/aspeed-video.c new file +4 -0
+
+	video->height = (bottom - top) + 1;
+	video->width = (right - left) + 1;
+	size = video->height * video->width;

here and below you appear to actually switch the capture timings to the new resolution as well.

Hmm. I'm not sure I understand, the video->height and width represent the actual detected resolution. When a resolution change occurs, the entire engine is switched off and all buffers are set to error state (see the irq handler). Then (after a delay to wait for VGA stability) the engine is switched back on and the new resolution detected.

Regardless of how I keep track of the resolution, the video engine will return frames of the new size. The driver cannot control the actual resolution.

If userspace is determined to ignore the source change event, probably there will be bad memory access regardless of how everything is set up I would think.

Quoted 11 lines

That should only happen when s_dv_timings is called.

It is always very dangerous to automatically switch resolution since a new resolution might mean larger buffers, and userspace has to prepare for that.

The normal sequence is:

1) the driver detects a change in resolution (or the video signal disappears completely). It will send V4L2_EVENT_SOURCE_CHANGE to let userspace know.

Quoted 7 lines

2) Userspace stops streaming, frees all buffers, calls QUERY_DV_TIMINGS and, if valid, follows with S_DV_TIMINGS.

3) Userspace calls REQBUFS to allocate the new buffers sized for the new timings and starts streaming again.

That way everything remains under control of the application.

Yep, that's exactly how it works now, its just that the driver stops streaming internally before detecting new resolution.

Thanks,
Eddie

Quoted 69 lines
Quoted 55 lines
drivers/media/platform/aspeed-video.c new file +54 -0
+
+	/* Don't use direct mode below 1024 x 768 (irqs don't fire) */
+	if (size < DIRECT_FETCH_THRESHOLD) {
+		aspeed_video_write(video, VE_TGS_0,
+				   FIELD_PREP(VE_TGS_FIRST, left - 1) |
+				   FIELD_PREP(VE_TGS_LAST, right));
+		aspeed_video_write(video, VE_TGS_1,
+				   FIELD_PREP(VE_TGS_FIRST, top) |
+				   FIELD_PREP(VE_TGS_LAST, bottom + 1));
+		aspeed_video_update(video, VE_CTRL, 0, VE_CTRL_INT_DE);
+	} else {
+		aspeed_video_update(video, VE_CTRL, 0, VE_CTRL_DIRECT_FETCH);
+	}
+
+	aspeed_video_write(video, VE_CAP_WINDOW,
+			   video->width << 16 | video->height);
+	aspeed_video_write(video, VE_COMP_WINDOW,
+			   video->width << 16 | video->height);
+	aspeed_video_write(video, VE_SRC_SCANLINE_OFFSET, video->width * 4);
+
+	aspeed_video_update(video, VE_INTERRUPT_CTRL, 0,
+			    VE_INTERRUPT_MODE_DETECT_WD);
+	aspeed_video_update(video, VE_SEQ_CTRL, 0,
+			    VE_SEQ_CTRL_AUTO_COMP | VE_SEQ_CTRL_EN_WATCHDOG);
+
+	dev_dbg(video->dev, "got resolution[%dx%d]\n", video->width,
+		video->height);
+
+	size *= 4;
+	if (size == src.size / 2) {
+		aspeed_video_write(video, VE_SRC1_ADDR, src.dma + size);
+		video->srcs[0] = src;
+	} else if (size == src.size) {
+		video->srcs[0] = src;
+
+		if (!aspeed_video_alloc_buf(video, &video->srcs[1], size))
+			goto err_mem;
+
+		aspeed_video_write(video, VE_SRC1_ADDR, video->srcs[1].dma);
+	} else {
+		aspeed_video_free_buf(video, &src);
+
+		if (!aspeed_video_alloc_buf(video, &video->srcs[0], size))
+			goto err_mem;
+
+		if (!aspeed_video_alloc_buf(video, &video->srcs[1], size))
+			goto err_mem;
+
+		aspeed_video_write(video, VE_SRC0_ADDR, video->srcs[0].dma);
+		aspeed_video_write(video, VE_SRC1_ADDR, video->srcs[1].dma);
+	}
+
+	aspeed_video_calc_compressed_size(video);
+

drivers/media/platform/aspeed-video.c line 863

Quoted 10 lines
drivers/media/platform/aspeed-video.c new file +10 -0
+	return 0;
+
+err_mem:
+	dev_err(video->dev, "failed to allocate source buffers\n");
+
+	if (video->srcs[0].size)
+		aspeed_video_free_buf(video, &video->srcs[0]);
+
+	return -ENOMEM;
+}

Regards,

Hans