Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 2 | /* |
| 3 | * ispvideo.c |
| 4 | * |
| 5 | * TI OMAP3 ISP - Generic video node |
| 6 | * |
| 7 | * Copyright (C) 2009-2010 Nokia Corporation |
| 8 | * |
| 9 | * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 10 | * Sakari Ailus <sakari.ailus@iki.fi> |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <asm/cacheflush.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/mm.h> |
Dmitry Artamonow | 025521f | 2011-11-20 12:54:26 -0300 | [diff] [blame] | 16 | #include <linux/module.h> |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/scatterlist.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/vmalloc.h> |
Sakari Ailus | 506a47e | 2016-02-21 13:25:10 -0300 | [diff] [blame] | 22 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 23 | #include <media/v4l2-dev.h> |
| 24 | #include <media/v4l2-ioctl.h> |
Sakari Ailus | 506a47e | 2016-02-21 13:25:10 -0300 | [diff] [blame] | 25 | #include <media/v4l2-mc.h> |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 26 | #include <media/videobuf2-dma-contig.h> |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 27 | |
| 28 | #include "ispvideo.h" |
| 29 | #include "isp.h" |
| 30 | |
| 31 | |
| 32 | /* ----------------------------------------------------------------------------- |
| 33 | * Helper functions |
| 34 | */ |
| 35 | |
Sakari Ailus | 8f4f298 | 2011-12-28 06:28:41 -0300 | [diff] [blame] | 36 | /* |
| 37 | * NOTE: When adding new media bus codes, always remember to add |
| 38 | * corresponding in-memory formats to the table below!!! |
| 39 | */ |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 40 | static struct isp_format_info formats[] = { |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 41 | { MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8, |
| 42 | MEDIA_BUS_FMT_Y8_1X8, MEDIA_BUS_FMT_Y8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 43 | V4L2_PIX_FMT_GREY, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 44 | { MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y10_1X10, |
| 45 | MEDIA_BUS_FMT_Y10_1X10, MEDIA_BUS_FMT_Y8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 46 | V4L2_PIX_FMT_Y10, 10, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 47 | { MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y10_1X10, |
| 48 | MEDIA_BUS_FMT_Y12_1X12, MEDIA_BUS_FMT_Y8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 49 | V4L2_PIX_FMT_Y12, 12, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 50 | { MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8, |
| 51 | MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SBGGR8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 52 | V4L2_PIX_FMT_SBGGR8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 53 | { MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8, |
| 54 | MEDIA_BUS_FMT_SGBRG8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 55 | V4L2_PIX_FMT_SGBRG8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 56 | { MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8, |
| 57 | MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SGRBG8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 58 | V4L2_PIX_FMT_SGRBG8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 59 | { MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8, |
| 60 | MEDIA_BUS_FMT_SRGGB8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 61 | V4L2_PIX_FMT_SRGGB8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 62 | { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, |
| 63 | MEDIA_BUS_FMT_SBGGR10_1X10, 0, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 64 | V4L2_PIX_FMT_SBGGR10DPCM8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 65 | { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, |
| 66 | MEDIA_BUS_FMT_SGBRG10_1X10, 0, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 67 | V4L2_PIX_FMT_SGBRG10DPCM8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 68 | { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, |
| 69 | MEDIA_BUS_FMT_SGRBG10_1X10, 0, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 70 | V4L2_PIX_FMT_SGRBG10DPCM8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 71 | { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, |
| 72 | MEDIA_BUS_FMT_SRGGB10_1X10, 0, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 73 | V4L2_PIX_FMT_SRGGB10DPCM8, 8, 1, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 74 | { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, |
| 75 | MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SBGGR8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 76 | V4L2_PIX_FMT_SBGGR10, 10, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 77 | { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10, |
| 78 | MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SGBRG8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 79 | V4L2_PIX_FMT_SGBRG10, 10, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 80 | { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, |
| 81 | MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SGRBG8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 82 | V4L2_PIX_FMT_SGRBG10, 10, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 83 | { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10, |
| 84 | MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SRGGB8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 85 | V4L2_PIX_FMT_SRGGB10, 10, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 86 | { MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR10_1X10, |
| 87 | MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SBGGR8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 88 | V4L2_PIX_FMT_SBGGR12, 12, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 89 | { MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG10_1X10, |
| 90 | MEDIA_BUS_FMT_SGBRG12_1X12, MEDIA_BUS_FMT_SGBRG8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 91 | V4L2_PIX_FMT_SGBRG12, 12, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 92 | { MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG10_1X10, |
| 93 | MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SGRBG8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 94 | V4L2_PIX_FMT_SGRBG12, 12, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 95 | { MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB10_1X10, |
| 96 | MEDIA_BUS_FMT_SRGGB12_1X12, MEDIA_BUS_FMT_SRGGB8_1X8, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 97 | V4L2_PIX_FMT_SRGGB12, 12, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 98 | { MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_UYVY8_1X16, |
| 99 | MEDIA_BUS_FMT_UYVY8_1X16, 0, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 100 | V4L2_PIX_FMT_UYVY, 16, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 101 | { MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YUYV8_1X16, |
| 102 | MEDIA_BUS_FMT_YUYV8_1X16, 0, |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 103 | V4L2_PIX_FMT_YUYV, 16, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 104 | { MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_UYVY8_2X8, |
| 105 | MEDIA_BUS_FMT_UYVY8_2X8, 0, |
Laurent Pinchart | 0810fd9 | 2012-05-24 11:55:00 -0300 | [diff] [blame] | 106 | V4L2_PIX_FMT_UYVY, 8, 2, }, |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 107 | { MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YUYV8_2X8, |
| 108 | MEDIA_BUS_FMT_YUYV8_2X8, 0, |
Laurent Pinchart | 0810fd9 | 2012-05-24 11:55:00 -0300 | [diff] [blame] | 109 | V4L2_PIX_FMT_YUYV, 8, 2, }, |
Laurent Pinchart | c51364c | 2011-08-31 11:03:53 -0300 | [diff] [blame] | 110 | /* Empty entry to catch the unsupported pixel code (0) used by the CCDC |
| 111 | * module and avoid NULL pointer dereferences. |
| 112 | */ |
| 113 | { 0, } |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 114 | }; |
| 115 | |
Boris BREZILLON | 27ffaeb | 2014-11-10 14:28:31 -0300 | [diff] [blame] | 116 | const struct isp_format_info *omap3isp_video_format_info(u32 code) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 117 | { |
| 118 | unsigned int i; |
| 119 | |
| 120 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { |
| 121 | if (formats[i].code == code) |
| 122 | return &formats[i]; |
| 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format |
| 130 | * @video: ISP video instance |
| 131 | * @mbus: v4l2_mbus_framefmt format (input) |
| 132 | * @pix: v4l2_pix_format format (output) |
| 133 | * |
| 134 | * Fill the output pix structure with information from the input mbus format. |
| 135 | * The bytesperline and sizeimage fields are computed from the requested bytes |
| 136 | * per line value in the pix format and information from the video instance. |
| 137 | * |
| 138 | * Return the number of padding bytes at end of line. |
| 139 | */ |
| 140 | static unsigned int isp_video_mbus_to_pix(const struct isp_video *video, |
| 141 | const struct v4l2_mbus_framefmt *mbus, |
| 142 | struct v4l2_pix_format *pix) |
| 143 | { |
| 144 | unsigned int bpl = pix->bytesperline; |
| 145 | unsigned int min_bpl; |
| 146 | unsigned int i; |
| 147 | |
| 148 | memset(pix, 0, sizeof(*pix)); |
| 149 | pix->width = mbus->width; |
| 150 | pix->height = mbus->height; |
| 151 | |
| 152 | for (i = 0; i < ARRAY_SIZE(formats); ++i) { |
| 153 | if (formats[i].code == mbus->code) |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | if (WARN_ON(i == ARRAY_SIZE(formats))) |
| 158 | return 0; |
| 159 | |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 160 | min_bpl = pix->width * formats[i].bpp; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 161 | |
| 162 | /* Clamp the requested bytes per line value. If the maximum bytes per |
| 163 | * line value is zero, the module doesn't support user configurable line |
| 164 | * sizes. Override the requested value with the minimum in that case. |
| 165 | */ |
| 166 | if (video->bpl_max) |
| 167 | bpl = clamp(bpl, min_bpl, video->bpl_max); |
| 168 | else |
| 169 | bpl = min_bpl; |
| 170 | |
| 171 | if (!video->bpl_zero_padding || bpl != min_bpl) |
| 172 | bpl = ALIGN(bpl, video->bpl_alignment); |
| 173 | |
| 174 | pix->pixelformat = formats[i].pixelformat; |
| 175 | pix->bytesperline = bpl; |
| 176 | pix->sizeimage = pix->bytesperline * pix->height; |
| 177 | pix->colorspace = mbus->colorspace; |
| 178 | pix->field = mbus->field; |
| 179 | |
| 180 | return bpl - min_bpl; |
| 181 | } |
| 182 | |
| 183 | static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix, |
| 184 | struct v4l2_mbus_framefmt *mbus) |
| 185 | { |
| 186 | unsigned int i; |
| 187 | |
| 188 | memset(mbus, 0, sizeof(*mbus)); |
| 189 | mbus->width = pix->width; |
| 190 | mbus->height = pix->height; |
| 191 | |
Laurent Pinchart | c3cd257 | 2011-11-28 08:25:30 -0300 | [diff] [blame] | 192 | /* Skip the last format in the loop so that it will be selected if no |
| 193 | * match is found. |
| 194 | */ |
| 195 | for (i = 0; i < ARRAY_SIZE(formats) - 1; ++i) { |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 196 | if (formats[i].pixelformat == pix->pixelformat) |
| 197 | break; |
| 198 | } |
| 199 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 200 | mbus->code = formats[i].code; |
| 201 | mbus->colorspace = pix->colorspace; |
| 202 | mbus->field = pix->field; |
| 203 | } |
| 204 | |
| 205 | static struct v4l2_subdev * |
| 206 | isp_video_remote_subdev(struct isp_video *video, u32 *pad) |
| 207 | { |
| 208 | struct media_pad *remote; |
| 209 | |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 210 | remote = media_entity_remote_pad(&video->pad); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 211 | |
Mauro Carvalho Chehab | 3efdf62 | 2015-05-07 22:12:32 -0300 | [diff] [blame] | 212 | if (!remote || !is_media_entity_v4l2_subdev(remote->entity)) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 213 | return NULL; |
| 214 | |
| 215 | if (pad) |
| 216 | *pad = remote->index; |
| 217 | |
| 218 | return media_entity_to_v4l2_subdev(remote->entity); |
| 219 | } |
| 220 | |
| 221 | /* Return a pointer to the ISP video instance at the far end of the pipeline. */ |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 222 | static int isp_video_get_graph_data(struct isp_video *video, |
| 223 | struct isp_pipeline *pipe) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 224 | { |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 225 | struct media_graph graph; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 226 | struct media_entity *entity = &video->video.entity; |
Javier Martinez Canillas | d10c989 | 2015-08-19 12:35:21 -0300 | [diff] [blame] | 227 | struct media_device *mdev = entity->graph_obj.mdev; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 228 | struct isp_video *far_end = NULL; |
Sakari Ailus | 2846145 | 2015-12-16 11:32:24 -0200 | [diff] [blame] | 229 | int ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 230 | |
| 231 | mutex_lock(&mdev->graph_mutex); |
Sakari Ailus | 79ac44c | 2016-11-28 08:42:44 -0200 | [diff] [blame] | 232 | ret = media_graph_walk_init(&graph, mdev); |
Sakari Ailus | 2846145 | 2015-12-16 11:32:24 -0200 | [diff] [blame] | 233 | if (ret) { |
| 234 | mutex_unlock(&mdev->graph_mutex); |
| 235 | return ret; |
| 236 | } |
| 237 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 238 | media_graph_walk_start(&graph, entity); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 239 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 240 | while ((entity = media_graph_walk_next(&graph))) { |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 241 | struct isp_video *__video; |
| 242 | |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 243 | media_entity_enum_set(&pipe->ent_enum, entity); |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 244 | |
| 245 | if (far_end != NULL) |
| 246 | continue; |
| 247 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 248 | if (entity == &video->video.entity) |
| 249 | continue; |
| 250 | |
Laurent Pinchart | 45b4687 | 2016-02-29 08:45:45 -0300 | [diff] [blame] | 251 | if (!is_media_entity_v4l2_video_device(entity)) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 252 | continue; |
| 253 | |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 254 | __video = to_isp_video(media_entity_to_video_device(entity)); |
| 255 | if (__video->type != video->type) |
| 256 | far_end = __video; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | mutex_unlock(&mdev->graph_mutex); |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 260 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 261 | media_graph_walk_cleanup(&graph); |
Sakari Ailus | 2846145 | 2015-12-16 11:32:24 -0200 | [diff] [blame] | 262 | |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 263 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 264 | pipe->input = far_end; |
| 265 | pipe->output = video; |
| 266 | } else { |
| 267 | if (far_end == NULL) |
| 268 | return -EPIPE; |
| 269 | |
| 270 | pipe->input = video; |
| 271 | pipe->output = far_end; |
| 272 | } |
| 273 | |
| 274 | return 0; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 275 | } |
| 276 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 277 | static int |
| 278 | __isp_video_get_format(struct isp_video *video, struct v4l2_format *format) |
| 279 | { |
| 280 | struct v4l2_subdev_format fmt; |
| 281 | struct v4l2_subdev *subdev; |
| 282 | u32 pad; |
| 283 | int ret; |
| 284 | |
| 285 | subdev = isp_video_remote_subdev(video, &pad); |
| 286 | if (subdev == NULL) |
| 287 | return -EINVAL; |
| 288 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 289 | fmt.pad = pad; |
| 290 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 291 | |
Laurent Pinchart | 5264682 | 2013-11-04 06:52:10 -0300 | [diff] [blame] | 292 | mutex_lock(&video->mutex); |
| 293 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 294 | mutex_unlock(&video->mutex); |
| 295 | |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | |
| 299 | format->type = video->type; |
| 300 | return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix); |
| 301 | } |
| 302 | |
| 303 | static int |
| 304 | isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh) |
| 305 | { |
| 306 | struct v4l2_format format; |
| 307 | int ret; |
| 308 | |
| 309 | memcpy(&format, &vfh->format, sizeof(format)); |
| 310 | ret = __isp_video_get_format(video, &format); |
| 311 | if (ret < 0) |
| 312 | return ret; |
| 313 | |
| 314 | if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat || |
| 315 | vfh->format.fmt.pix.height != format.fmt.pix.height || |
| 316 | vfh->format.fmt.pix.width != format.fmt.pix.width || |
| 317 | vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline || |
Laurent Pinchart | f7abbe9 | 2014-05-19 12:14:42 -0300 | [diff] [blame] | 318 | vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage || |
| 319 | vfh->format.fmt.pix.field != format.fmt.pix.field) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 320 | return -EINVAL; |
| 321 | |
Laurent Pinchart | f7abbe9 | 2014-05-19 12:14:42 -0300 | [diff] [blame] | 322 | return 0; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* ----------------------------------------------------------------------------- |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 326 | * Video queue operations |
| 327 | */ |
| 328 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 329 | static int isp_video_queue_setup(struct vb2_queue *queue, |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 330 | unsigned int *count, unsigned int *num_planes, |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 331 | unsigned int sizes[], struct device *alloc_devs[]) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 332 | { |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 333 | struct isp_video_fh *vfh = vb2_get_drv_priv(queue); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 334 | struct isp_video *video = vfh->video; |
| 335 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 336 | *num_planes = 1; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 337 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 338 | sizes[0] = vfh->format.fmt.pix.sizeimage; |
| 339 | if (sizes[0] == 0) |
| 340 | return -EINVAL; |
| 341 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 342 | *count = min(*count, video->capture_mem / PAGE_ALIGN(sizes[0])); |
| 343 | |
| 344 | return 0; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 345 | } |
| 346 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 347 | static int isp_video_buffer_prepare(struct vb2_buffer *buf) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 348 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 349 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 350 | struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 351 | struct isp_buffer *buffer = to_isp_buffer(vbuf); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 352 | struct isp_video *video = vfh->video; |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 353 | dma_addr_t addr; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 354 | |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 355 | /* Refuse to prepare the buffer is the video node has registered an |
| 356 | * error. We don't need to take any lock here as the operation is |
| 357 | * inherently racy. The authoritative check will be performed in the |
| 358 | * queue handler, which can't return an error, this check is just a best |
| 359 | * effort to notify userspace as early as possible. |
| 360 | */ |
| 361 | if (unlikely(video->error)) |
| 362 | return -EIO; |
| 363 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 364 | addr = vb2_dma_contig_plane_dma_addr(buf, 0); |
| 365 | if (!IS_ALIGNED(addr, 32)) { |
| 366 | dev_dbg(video->isp->dev, |
| 367 | "Buffer address must be aligned to 32 bytes boundary.\n"); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 371 | vb2_set_plane_payload(&buffer->vb.vb2_buf, 0, |
| 372 | vfh->format.fmt.pix.sizeimage); |
Laurent Pinchart | 21d8582 | 2014-03-09 20:17:12 -0300 | [diff] [blame] | 373 | buffer->dma = addr; |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 374 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * isp_video_buffer_queue - Add buffer to streaming queue |
| 380 | * @buf: Video buffer |
| 381 | * |
| 382 | * In memory-to-memory mode, start streaming on the pipeline if buffers are |
| 383 | * queued on both the input and the output, if the pipeline isn't already busy. |
| 384 | * If the pipeline is busy, it will be restarted in the output module interrupt |
| 385 | * handler. |
| 386 | */ |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 387 | static void isp_video_buffer_queue(struct vb2_buffer *buf) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 388 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 389 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(buf); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 390 | struct isp_video_fh *vfh = vb2_get_drv_priv(buf->vb2_queue); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 391 | struct isp_buffer *buffer = to_isp_buffer(vbuf); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 392 | struct isp_video *video = vfh->video; |
| 393 | struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity); |
| 394 | enum isp_pipeline_state state; |
| 395 | unsigned long flags; |
| 396 | unsigned int empty; |
| 397 | unsigned int start; |
| 398 | |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 399 | spin_lock_irqsave(&video->irqlock, flags); |
| 400 | |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 401 | if (unlikely(video->error)) { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 402 | vb2_buffer_done(&buffer->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 403 | spin_unlock_irqrestore(&video->irqlock, flags); |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 407 | empty = list_empty(&video->dmaqueue); |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 408 | list_add_tail(&buffer->irqlist, &video->dmaqueue); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 409 | |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 410 | spin_unlock_irqrestore(&video->irqlock, flags); |
| 411 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 412 | if (empty) { |
| 413 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 414 | state = ISP_PIPELINE_QUEUE_OUTPUT; |
| 415 | else |
| 416 | state = ISP_PIPELINE_QUEUE_INPUT; |
| 417 | |
| 418 | spin_lock_irqsave(&pipe->lock, flags); |
| 419 | pipe->state |= state; |
| 420 | video->ops->queue(video, buffer); |
| 421 | video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED; |
| 422 | |
| 423 | start = isp_pipeline_ready(pipe); |
| 424 | if (start) |
| 425 | pipe->state |= ISP_PIPELINE_STREAM; |
| 426 | spin_unlock_irqrestore(&pipe->lock, flags); |
| 427 | |
| 428 | if (start) |
| 429 | omap3isp_pipeline_set_stream(pipe, |
| 430 | ISP_PIPELINE_STREAM_SINGLESHOT); |
| 431 | } |
| 432 | } |
| 433 | |
Sakari Ailus | 87e062d | 2014-09-18 18:57:49 -0300 | [diff] [blame] | 434 | /* |
| 435 | * omap3isp_video_return_buffers - Return all queued buffers to videobuf2 |
| 436 | * @video: ISP video object |
| 437 | * @state: new state for the returned buffers |
| 438 | * |
| 439 | * Return all buffers queued on the video node to videobuf2 in the given state. |
| 440 | * The buffer state should be VB2_BUF_STATE_QUEUED if called due to an error |
| 441 | * when starting the stream, or VB2_BUF_STATE_ERROR otherwise. |
| 442 | * |
| 443 | * The function must be called with the video irqlock held. |
| 444 | */ |
| 445 | static void omap3isp_video_return_buffers(struct isp_video *video, |
| 446 | enum vb2_buffer_state state) |
| 447 | { |
| 448 | while (!list_empty(&video->dmaqueue)) { |
| 449 | struct isp_buffer *buf; |
| 450 | |
| 451 | buf = list_first_entry(&video->dmaqueue, |
| 452 | struct isp_buffer, irqlist); |
| 453 | list_del(&buf->irqlist); |
| 454 | vb2_buffer_done(&buf->vb.vb2_buf, state); |
| 455 | } |
| 456 | } |
| 457 | |
Sakari Ailus | 35c5f63 | 2014-09-18 18:57:48 -0300 | [diff] [blame] | 458 | static int isp_video_start_streaming(struct vb2_queue *queue, |
| 459 | unsigned int count) |
| 460 | { |
| 461 | struct isp_video_fh *vfh = vb2_get_drv_priv(queue); |
| 462 | struct isp_video *video = vfh->video; |
| 463 | struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity); |
| 464 | unsigned long flags; |
| 465 | int ret; |
| 466 | |
| 467 | /* In sensor-to-memory mode, the stream can be started synchronously |
| 468 | * to the stream on command. In memory-to-memory mode, it will be |
| 469 | * started when buffers are queued on both the input and output. |
| 470 | */ |
| 471 | if (pipe->input) |
| 472 | return 0; |
| 473 | |
| 474 | ret = omap3isp_pipeline_set_stream(pipe, |
| 475 | ISP_PIPELINE_STREAM_CONTINUOUS); |
Sakari Ailus | 87e062d | 2014-09-18 18:57:49 -0300 | [diff] [blame] | 476 | if (ret < 0) { |
| 477 | spin_lock_irqsave(&video->irqlock, flags); |
| 478 | omap3isp_video_return_buffers(video, VB2_BUF_STATE_QUEUED); |
| 479 | spin_unlock_irqrestore(&video->irqlock, flags); |
Sakari Ailus | 35c5f63 | 2014-09-18 18:57:48 -0300 | [diff] [blame] | 480 | return ret; |
Sakari Ailus | 87e062d | 2014-09-18 18:57:49 -0300 | [diff] [blame] | 481 | } |
Sakari Ailus | 35c5f63 | 2014-09-18 18:57:48 -0300 | [diff] [blame] | 482 | |
| 483 | spin_lock_irqsave(&video->irqlock, flags); |
| 484 | if (list_empty(&video->dmaqueue)) |
| 485 | video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN; |
| 486 | spin_unlock_irqrestore(&video->irqlock, flags); |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 491 | static const struct vb2_ops isp_video_queue_ops = { |
| 492 | .queue_setup = isp_video_queue_setup, |
| 493 | .buf_prepare = isp_video_buffer_prepare, |
| 494 | .buf_queue = isp_video_buffer_queue, |
Sakari Ailus | 35c5f63 | 2014-09-18 18:57:48 -0300 | [diff] [blame] | 495 | .start_streaming = isp_video_start_streaming, |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | /* |
| 499 | * omap3isp_video_buffer_next - Complete the current buffer and return the next |
| 500 | * @video: ISP video object |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 501 | * |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 502 | * Remove the current video buffer from the DMA queue and fill its timestamp and |
| 503 | * field count before handing it back to videobuf2. |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 504 | * |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 505 | * For capture video nodes the buffer state is set to VB2_BUF_STATE_DONE if no |
| 506 | * error has been flagged in the pipeline, or to VB2_BUF_STATE_ERROR otherwise. |
| 507 | * For video output nodes the buffer state is always set to VB2_BUF_STATE_DONE. |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 508 | * |
| 509 | * The DMA queue is expected to contain at least one buffer. |
| 510 | * |
| 511 | * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is |
| 512 | * empty. |
| 513 | */ |
Laurent Pinchart | 875e2e3 | 2011-12-07 08:34:50 -0300 | [diff] [blame] | 514 | struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 515 | { |
| 516 | struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity); |
Lad, Prabhakar | 18ba53f | 2015-02-25 12:58:14 -0300 | [diff] [blame] | 517 | enum vb2_buffer_state vb_state; |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 518 | struct isp_buffer *buf; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 519 | unsigned long flags; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 520 | |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 521 | spin_lock_irqsave(&video->irqlock, flags); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 522 | if (WARN_ON(list_empty(&video->dmaqueue))) { |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 523 | spin_unlock_irqrestore(&video->irqlock, flags); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 524 | return NULL; |
| 525 | } |
| 526 | |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 527 | buf = list_first_entry(&video->dmaqueue, struct isp_buffer, |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 528 | irqlist); |
| 529 | list_del(&buf->irqlist); |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 530 | spin_unlock_irqrestore(&video->irqlock, flags); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 531 | |
Junghak Sung | d6dd645 | 2015-11-03 08:16:37 -0200 | [diff] [blame] | 532 | buf->vb.vb2_buf.timestamp = ktime_get_ns(); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 533 | |
| 534 | /* Do frame number propagation only if this is the output video node. |
| 535 | * Frame number either comes from the CSI receivers or it gets |
| 536 | * incremented here if H3A is not active. |
| 537 | * Note: There is no guarantee that the output buffer will finish |
| 538 | * first, so the input number might lag behind by 1 in some cases. |
| 539 | */ |
| 540 | if (video == pipe->output && !pipe->do_propagation) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 541 | buf->vb.sequence = |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 542 | atomic_inc_return(&pipe->frame_number); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 543 | else |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 544 | buf->vb.sequence = atomic_read(&pipe->frame_number); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 545 | |
Laurent Pinchart | 9a36d8e | 2014-05-19 16:37:38 -0300 | [diff] [blame] | 546 | if (pipe->field != V4L2_FIELD_NONE) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 547 | buf->vb.sequence /= 2; |
Laurent Pinchart | 9a36d8e | 2014-05-19 16:37:38 -0300 | [diff] [blame] | 548 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 549 | buf->vb.field = pipe->field; |
Laurent Pinchart | 9a36d8e | 2014-05-19 16:37:38 -0300 | [diff] [blame] | 550 | |
Laurent Pinchart | 875e2e3 | 2011-12-07 08:34:50 -0300 | [diff] [blame] | 551 | /* Report pipeline errors to userspace on the capture device side. */ |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 552 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->error) { |
Lad, Prabhakar | 18ba53f | 2015-02-25 12:58:14 -0300 | [diff] [blame] | 553 | vb_state = VB2_BUF_STATE_ERROR; |
Laurent Pinchart | 875e2e3 | 2011-12-07 08:34:50 -0300 | [diff] [blame] | 554 | pipe->error = false; |
| 555 | } else { |
Lad, Prabhakar | 18ba53f | 2015-02-25 12:58:14 -0300 | [diff] [blame] | 556 | vb_state = VB2_BUF_STATE_DONE; |
Laurent Pinchart | 875e2e3 | 2011-12-07 08:34:50 -0300 | [diff] [blame] | 557 | } |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 558 | |
Lad, Prabhakar | 18ba53f | 2015-02-25 12:58:14 -0300 | [diff] [blame] | 559 | vb2_buffer_done(&buf->vb.vb2_buf, vb_state); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 560 | |
| 561 | spin_lock_irqsave(&video->irqlock, flags); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 562 | |
| 563 | if (list_empty(&video->dmaqueue)) { |
Lad, Prabhakar | 18ba53f | 2015-02-25 12:58:14 -0300 | [diff] [blame] | 564 | enum isp_pipeline_state state; |
| 565 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 566 | spin_unlock_irqrestore(&video->irqlock, flags); |
| 567 | |
| 568 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 569 | state = ISP_PIPELINE_QUEUE_OUTPUT |
| 570 | | ISP_PIPELINE_STREAM; |
| 571 | else |
| 572 | state = ISP_PIPELINE_QUEUE_INPUT |
| 573 | | ISP_PIPELINE_STREAM; |
| 574 | |
| 575 | spin_lock_irqsave(&pipe->lock, flags); |
| 576 | pipe->state &= ~state; |
| 577 | if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS) |
| 578 | video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN; |
| 579 | spin_unlock_irqrestore(&pipe->lock, flags); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 583 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) { |
| 584 | spin_lock(&pipe->lock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 585 | pipe->state &= ~ISP_PIPELINE_STREAM; |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 586 | spin_unlock(&pipe->lock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 587 | } |
| 588 | |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 589 | buf = list_first_entry(&video->dmaqueue, struct isp_buffer, |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 590 | irqlist); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 591 | |
| 592 | spin_unlock_irqrestore(&video->irqlock, flags); |
| 593 | |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 594 | return buf; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 598 | * omap3isp_video_cancel_stream - Cancel stream on a video node |
| 599 | * @video: ISP video object |
| 600 | * |
Sakari Ailus | 87e062d | 2014-09-18 18:57:49 -0300 | [diff] [blame] | 601 | * Cancelling a stream returns all buffers queued on the video node to videobuf2 |
| 602 | * in the erroneous state and makes sure no new buffer can be queued. |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 603 | */ |
| 604 | void omap3isp_video_cancel_stream(struct isp_video *video) |
| 605 | { |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 606 | unsigned long flags; |
| 607 | |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 608 | spin_lock_irqsave(&video->irqlock, flags); |
Sakari Ailus | 87e062d | 2014-09-18 18:57:49 -0300 | [diff] [blame] | 609 | omap3isp_video_return_buffers(video, VB2_BUF_STATE_ERROR); |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 610 | video->error = true; |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 611 | spin_unlock_irqrestore(&video->irqlock, flags); |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 615 | * omap3isp_video_resume - Perform resume operation on the buffers |
| 616 | * @video: ISP video object |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 617 | * @continuous: Pipeline is in single shot mode if 0 or continuous mode otherwise |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 618 | * |
| 619 | * This function is intended to be used on suspend/resume scenario. It |
| 620 | * requests video queue layer to discard buffers marked as DONE if it's in |
| 621 | * continuous mode and requests ISP modules to queue again the ACTIVE buffer |
| 622 | * if there's any. |
| 623 | */ |
| 624 | void omap3isp_video_resume(struct isp_video *video, int continuous) |
| 625 | { |
| 626 | struct isp_buffer *buf = NULL; |
| 627 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 628 | if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 629 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 630 | vb2_discard_done(video->queue); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 631 | mutex_unlock(&video->queue_lock); |
| 632 | } |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 633 | |
| 634 | if (!list_empty(&video->dmaqueue)) { |
| 635 | buf = list_first_entry(&video->dmaqueue, |
Laurent Pinchart | eb2c00d | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 636 | struct isp_buffer, irqlist); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 637 | video->ops->queue(video, buf); |
| 638 | video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED; |
| 639 | } else { |
| 640 | if (continuous) |
| 641 | video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /* ----------------------------------------------------------------------------- |
| 646 | * V4L2 ioctls |
| 647 | */ |
| 648 | |
| 649 | static int |
| 650 | isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap) |
| 651 | { |
| 652 | struct isp_video *video = video_drvdata(file); |
| 653 | |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 654 | strscpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver)); |
| 655 | strscpy(cap->card, video->video.name, sizeof(cap->card)); |
| 656 | strscpy(cap->bus_info, "media", sizeof(cap->bus_info)); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 657 | |
Sakari Ailus | 2c0108e | 2015-01-01 18:13:54 -0300 | [diff] [blame] | 658 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
| 659 | | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS; |
| 660 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 661 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Sakari Ailus | 2c0108e | 2015-01-01 18:13:54 -0300 | [diff] [blame] | 662 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 663 | else |
Sakari Ailus | 2c0108e | 2015-01-01 18:13:54 -0300 | [diff] [blame] | 664 | cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int |
| 670 | isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format) |
| 671 | { |
| 672 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
| 673 | struct isp_video *video = video_drvdata(file); |
| 674 | |
| 675 | if (format->type != video->type) |
| 676 | return -EINVAL; |
| 677 | |
| 678 | mutex_lock(&video->mutex); |
| 679 | *format = vfh->format; |
| 680 | mutex_unlock(&video->mutex); |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static int |
| 686 | isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format) |
| 687 | { |
| 688 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
| 689 | struct isp_video *video = video_drvdata(file); |
| 690 | struct v4l2_mbus_framefmt fmt; |
| 691 | |
| 692 | if (format->type != video->type) |
| 693 | return -EINVAL; |
| 694 | |
Laurent Pinchart | bcb4e0e | 2014-05-19 19:40:04 -0300 | [diff] [blame] | 695 | /* Replace unsupported field orders with sane defaults. */ |
| 696 | switch (format->fmt.pix.field) { |
| 697 | case V4L2_FIELD_NONE: |
| 698 | /* Progressive is supported everywhere. */ |
| 699 | break; |
| 700 | case V4L2_FIELD_ALTERNATE: |
| 701 | /* ALTERNATE is not supported on output nodes. */ |
| 702 | if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 703 | format->fmt.pix.field = V4L2_FIELD_NONE; |
| 704 | break; |
| 705 | case V4L2_FIELD_INTERLACED: |
| 706 | /* The ISP has no concept of video standard, select the |
| 707 | * top-bottom order when the unqualified interlaced order is |
| 708 | * requested. |
| 709 | */ |
| 710 | format->fmt.pix.field = V4L2_FIELD_INTERLACED_TB; |
| 711 | /* Fall-through */ |
| 712 | case V4L2_FIELD_INTERLACED_TB: |
| 713 | case V4L2_FIELD_INTERLACED_BT: |
| 714 | /* Interlaced orders are only supported at the CCDC output. */ |
| 715 | if (video != &video->isp->isp_ccdc.video_out) |
| 716 | format->fmt.pix.field = V4L2_FIELD_NONE; |
| 717 | break; |
| 718 | case V4L2_FIELD_TOP: |
| 719 | case V4L2_FIELD_BOTTOM: |
| 720 | case V4L2_FIELD_SEQ_TB: |
| 721 | case V4L2_FIELD_SEQ_BT: |
| 722 | default: |
| 723 | /* All other field orders are currently unsupported, default to |
| 724 | * progressive. |
| 725 | */ |
Laurent Pinchart | 48a8a0c | 2014-05-19 10:49:58 -0300 | [diff] [blame] | 726 | format->fmt.pix.field = V4L2_FIELD_NONE; |
Laurent Pinchart | bcb4e0e | 2014-05-19 19:40:04 -0300 | [diff] [blame] | 727 | break; |
| 728 | } |
Laurent Pinchart | 48a8a0c | 2014-05-19 10:49:58 -0300 | [diff] [blame] | 729 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 730 | /* Fill the bytesperline and sizeimage fields by converting to media bus |
| 731 | * format and back to pixel format. |
| 732 | */ |
| 733 | isp_video_pix_to_mbus(&format->fmt.pix, &fmt); |
| 734 | isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix); |
| 735 | |
Laurent Pinchart | 25c5cc9 | 2014-05-19 10:33:15 -0300 | [diff] [blame] | 736 | mutex_lock(&video->mutex); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 737 | vfh->format = *format; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 738 | mutex_unlock(&video->mutex); |
Laurent Pinchart | 25c5cc9 | 2014-05-19 10:33:15 -0300 | [diff] [blame] | 739 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format) |
| 745 | { |
| 746 | struct isp_video *video = video_drvdata(file); |
| 747 | struct v4l2_subdev_format fmt; |
| 748 | struct v4l2_subdev *subdev; |
| 749 | u32 pad; |
| 750 | int ret; |
| 751 | |
| 752 | if (format->type != video->type) |
| 753 | return -EINVAL; |
| 754 | |
| 755 | subdev = isp_video_remote_subdev(video, &pad); |
| 756 | if (subdev == NULL) |
| 757 | return -EINVAL; |
| 758 | |
| 759 | isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format); |
| 760 | |
| 761 | fmt.pad = pad; |
| 762 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 763 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); |
| 764 | if (ret) |
Wanlong Gao | a694f1b | 2012-08-27 04:23:15 -0300 | [diff] [blame] | 765 | return ret == -ENOIOCTLCMD ? -ENOTTY : ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 766 | |
| 767 | isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static int |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 772 | isp_video_get_selection(struct file *file, void *fh, struct v4l2_selection *sel) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 773 | { |
| 774 | struct isp_video *video = video_drvdata(file); |
| 775 | struct v4l2_subdev_format format; |
| 776 | struct v4l2_subdev *subdev; |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 777 | struct v4l2_subdev_selection sdsel = { |
| 778 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, |
| 779 | .target = sel->target, |
| 780 | }; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 781 | u32 pad; |
| 782 | int ret; |
| 783 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 784 | switch (sel->target) { |
| 785 | case V4L2_SEL_TGT_CROP: |
| 786 | case V4L2_SEL_TGT_CROP_BOUNDS: |
| 787 | case V4L2_SEL_TGT_CROP_DEFAULT: |
| 788 | if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 789 | return -EINVAL; |
| 790 | break; |
| 791 | case V4L2_SEL_TGT_COMPOSE: |
| 792 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 793 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 794 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 795 | return -EINVAL; |
| 796 | break; |
| 797 | default: |
| 798 | return -EINVAL; |
| 799 | } |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 800 | subdev = isp_video_remote_subdev(video, &pad); |
| 801 | if (subdev == NULL) |
| 802 | return -EINVAL; |
| 803 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 804 | /* Try the get selection operation first and fallback to get format if not |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 805 | * implemented. |
| 806 | */ |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 807 | sdsel.pad = pad; |
| 808 | ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel); |
| 809 | if (!ret) |
| 810 | sel->r = sdsel.r; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 811 | if (ret != -ENOIOCTLCMD) |
| 812 | return ret; |
| 813 | |
| 814 | format.pad = pad; |
| 815 | format.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 816 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format); |
| 817 | if (ret < 0) |
Wanlong Gao | a694f1b | 2012-08-27 04:23:15 -0300 | [diff] [blame] | 818 | return ret == -ENOIOCTLCMD ? -ENOTTY : ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 819 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 820 | sel->r.left = 0; |
| 821 | sel->r.top = 0; |
| 822 | sel->r.width = format.format.width; |
| 823 | sel->r.height = format.format.height; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 829 | isp_video_set_selection(struct file *file, void *fh, struct v4l2_selection *sel) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 830 | { |
| 831 | struct isp_video *video = video_drvdata(file); |
| 832 | struct v4l2_subdev *subdev; |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 833 | struct v4l2_subdev_selection sdsel = { |
| 834 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, |
| 835 | .target = sel->target, |
| 836 | .flags = sel->flags, |
| 837 | .r = sel->r, |
| 838 | }; |
| 839 | u32 pad; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 840 | int ret; |
| 841 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 842 | switch (sel->target) { |
| 843 | case V4L2_SEL_TGT_CROP: |
| 844 | if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 845 | return -EINVAL; |
| 846 | break; |
| 847 | case V4L2_SEL_TGT_COMPOSE: |
| 848 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 849 | return -EINVAL; |
| 850 | break; |
| 851 | default: |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | subdev = isp_video_remote_subdev(video, &pad); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 855 | if (subdev == NULL) |
| 856 | return -EINVAL; |
| 857 | |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 858 | sdsel.pad = pad; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 859 | mutex_lock(&video->mutex); |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 860 | ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 861 | mutex_unlock(&video->mutex); |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 862 | if (!ret) |
| 863 | sel->r = sdsel.r; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 864 | |
Wanlong Gao | a694f1b | 2012-08-27 04:23:15 -0300 | [diff] [blame] | 865 | return ret == -ENOIOCTLCMD ? -ENOTTY : ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static int |
| 869 | isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a) |
| 870 | { |
| 871 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
| 872 | struct isp_video *video = video_drvdata(file); |
| 873 | |
| 874 | if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || |
| 875 | video->type != a->type) |
| 876 | return -EINVAL; |
| 877 | |
| 878 | memset(a, 0, sizeof(*a)); |
| 879 | a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
| 880 | a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; |
| 881 | a->parm.output.timeperframe = vfh->timeperframe; |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static int |
| 887 | isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a) |
| 888 | { |
| 889 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
| 890 | struct isp_video *video = video_drvdata(file); |
| 891 | |
| 892 | if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT || |
| 893 | video->type != a->type) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | if (a->parm.output.timeperframe.denominator == 0) |
| 897 | a->parm.output.timeperframe.denominator = 1; |
| 898 | |
| 899 | vfh->timeperframe = a->parm.output.timeperframe; |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static int |
| 905 | isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb) |
| 906 | { |
| 907 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 908 | struct isp_video *video = video_drvdata(file); |
| 909 | int ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 910 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 911 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 912 | ret = vb2_reqbufs(&vfh->queue, rb); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 913 | mutex_unlock(&video->queue_lock); |
| 914 | |
| 915 | return ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static int |
| 919 | isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b) |
| 920 | { |
| 921 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 922 | struct isp_video *video = video_drvdata(file); |
| 923 | int ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 924 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 925 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 926 | ret = vb2_querybuf(&vfh->queue, b); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 927 | mutex_unlock(&video->queue_lock); |
| 928 | |
| 929 | return ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static int |
| 933 | isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b) |
| 934 | { |
| 935 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 936 | struct isp_video *video = video_drvdata(file); |
| 937 | int ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 938 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 939 | mutex_lock(&video->queue_lock); |
Hans Verkuil | 394dc58 | 2018-05-30 02:46:22 -0400 | [diff] [blame] | 940 | ret = vb2_qbuf(&vfh->queue, video->video.v4l2_dev->mdev, b); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 941 | mutex_unlock(&video->queue_lock); |
| 942 | |
| 943 | return ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static int |
| 947 | isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b) |
| 948 | { |
| 949 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 950 | struct isp_video *video = video_drvdata(file); |
| 951 | int ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 952 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 953 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 954 | ret = vb2_dqbuf(&vfh->queue, b, file->f_flags & O_NONBLOCK); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 955 | mutex_unlock(&video->queue_lock); |
| 956 | |
| 957 | return ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 958 | } |
| 959 | |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 960 | static int isp_video_check_external_subdevs(struct isp_video *video, |
| 961 | struct isp_pipeline *pipe) |
| 962 | { |
| 963 | struct isp_device *isp = video->isp; |
| 964 | struct media_entity *ents[] = { |
| 965 | &isp->isp_csi2a.subdev.entity, |
| 966 | &isp->isp_csi2c.subdev.entity, |
| 967 | &isp->isp_ccp2.subdev.entity, |
| 968 | &isp->isp_ccdc.subdev.entity |
| 969 | }; |
| 970 | struct media_pad *source_pad; |
| 971 | struct media_entity *source = NULL; |
| 972 | struct media_entity *sink; |
| 973 | struct v4l2_subdev_format fmt; |
| 974 | struct v4l2_ext_controls ctrls; |
| 975 | struct v4l2_ext_control ctrl; |
| 976 | unsigned int i; |
Laurent Pinchart | 7c486c0 | 2014-02-14 21:45:50 -0300 | [diff] [blame] | 977 | int ret; |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 978 | |
Laurent Pinchart | 4b2f1ed | 2014-02-14 21:40:48 -0300 | [diff] [blame] | 979 | /* Memory-to-memory pipelines have no external subdev. */ |
| 980 | if (pipe->input != NULL) |
| 981 | return 0; |
| 982 | |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 983 | for (i = 0; i < ARRAY_SIZE(ents); i++) { |
| 984 | /* Is the entity part of the pipeline? */ |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 985 | if (!media_entity_enum_test(&pipe->ent_enum, ents[i])) |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 986 | continue; |
| 987 | |
| 988 | /* ISP entities have always sink pad == 0. Find source. */ |
Andrzej Hajda | 1bddf1b | 2013-06-03 05:16:13 -0300 | [diff] [blame] | 989 | source_pad = media_entity_remote_pad(&ents[i]->pads[0]); |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 990 | if (source_pad == NULL) |
| 991 | continue; |
| 992 | |
| 993 | source = source_pad->entity; |
| 994 | sink = ents[i]; |
| 995 | break; |
| 996 | } |
| 997 | |
| 998 | if (!source) { |
| 999 | dev_warn(isp->dev, "can't find source, failing now\n"); |
Laurent Pinchart | 7c486c0 | 2014-02-14 21:45:50 -0300 | [diff] [blame] | 1000 | return -EINVAL; |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 1001 | } |
| 1002 | |
Mauro Carvalho Chehab | 3efdf62 | 2015-05-07 22:12:32 -0300 | [diff] [blame] | 1003 | if (!is_media_entity_v4l2_subdev(source)) |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 1004 | return 0; |
| 1005 | |
| 1006 | pipe->external = media_entity_to_v4l2_subdev(source); |
| 1007 | |
| 1008 | fmt.pad = source_pad->index; |
| 1009 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 1010 | ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(sink), |
| 1011 | pad, get_fmt, NULL, &fmt); |
| 1012 | if (unlikely(ret < 0)) { |
| 1013 | dev_warn(isp->dev, "get_fmt returned null!\n"); |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Laurent Pinchart | 1697e49 | 2011-08-31 11:57:12 -0300 | [diff] [blame] | 1017 | pipe->external_width = |
| 1018 | omap3isp_video_format_info(fmt.format.code)->width; |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 1019 | |
| 1020 | memset(&ctrls, 0, sizeof(ctrls)); |
| 1021 | memset(&ctrl, 0, sizeof(ctrl)); |
| 1022 | |
| 1023 | ctrl.id = V4L2_CID_PIXEL_RATE; |
| 1024 | |
| 1025 | ctrls.count = 1; |
| 1026 | ctrls.controls = &ctrl; |
| 1027 | |
Hans Verkuil | c41e9cf | 2018-05-21 04:54:42 -0400 | [diff] [blame] | 1028 | ret = v4l2_g_ext_ctrls(pipe->external->ctrl_handler, NULL, &ctrls); |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 1029 | if (ret < 0) { |
| 1030 | dev_warn(isp->dev, "no pixel rate control in subdev %s\n", |
| 1031 | pipe->external->name); |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | pipe->external_rate = ctrl.value64; |
| 1036 | |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 1037 | if (media_entity_enum_test(&pipe->ent_enum, |
| 1038 | &isp->isp_ccdc.subdev.entity)) { |
Sakari Ailus | a6d7a62 | 2012-02-25 20:42:07 -0300 | [diff] [blame] | 1039 | unsigned int rate = UINT_MAX; |
| 1040 | /* |
| 1041 | * Check that maximum allowed CCDC pixel rate isn't |
| 1042 | * exceeded by the pixel rate. |
| 1043 | */ |
| 1044 | omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate); |
| 1045 | if (pipe->external_rate > rate) |
| 1046 | return -ENOSPC; |
| 1047 | } |
| 1048 | |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1052 | /* |
| 1053 | * Stream management |
| 1054 | * |
| 1055 | * Every ISP pipeline has a single input and a single output. The input can be |
| 1056 | * either a sensor or a video node. The output is always a video node. |
| 1057 | * |
| 1058 | * As every pipeline has an output video node, the ISP video objects at the |
| 1059 | * pipeline output stores the pipeline state. It tracks the streaming state of |
| 1060 | * both the input and output, as well as the availability of buffers. |
| 1061 | * |
| 1062 | * In sensor-to-memory mode, frames are always available at the pipeline input. |
| 1063 | * Starting the sensor usually requires I2C transfers and must be done in |
| 1064 | * interruptible context. The pipeline is started and stopped synchronously |
| 1065 | * to the stream on/off commands. All modules in the pipeline will get their |
| 1066 | * subdev set stream handler called. The module at the end of the pipeline must |
| 1067 | * delay starting the hardware until buffers are available at its output. |
| 1068 | * |
| 1069 | * In memory-to-memory mode, starting/stopping the stream requires |
| 1070 | * synchronization between the input and output. ISP modules can't be stopped |
| 1071 | * in the middle of a frame, and at least some of the modules seem to become |
| 1072 | * busy as soon as they're started, even if they don't receive a frame start |
| 1073 | * event. For that reason frames need to be processed in single-shot mode. The |
| 1074 | * driver needs to wait until a frame is completely processed and written to |
| 1075 | * memory before restarting the pipeline for the next frame. Pipelined |
| 1076 | * processing might be possible but requires more testing. |
| 1077 | * |
| 1078 | * Stream start must be delayed until buffers are available at both the input |
| 1079 | * and output. The pipeline must be started in the videobuf queue callback with |
| 1080 | * the buffers queue spinlock held. The modules subdev set stream operation must |
| 1081 | * not sleep. |
| 1082 | */ |
| 1083 | static int |
| 1084 | isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) |
| 1085 | { |
| 1086 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
| 1087 | struct isp_video *video = video_drvdata(file); |
| 1088 | enum isp_pipeline_state state; |
| 1089 | struct isp_pipeline *pipe; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1090 | unsigned long flags; |
| 1091 | int ret; |
| 1092 | |
| 1093 | if (type != video->type) |
| 1094 | return -EINVAL; |
| 1095 | |
| 1096 | mutex_lock(&video->stream_lock); |
| 1097 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1098 | /* Start streaming on the pipeline. No link touching an entity in the |
| 1099 | * pipeline can be activated or deactivated once streaming is started. |
| 1100 | */ |
| 1101 | pipe = video->video.entity.pipe |
| 1102 | ? to_isp_pipeline(&video->video.entity) : &video->pipe; |
Sakari Ailus | b0cd79e | 2012-01-16 18:59:02 -0300 | [diff] [blame] | 1103 | |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 1104 | ret = media_entity_enum_init(&pipe->ent_enum, &video->isp->media_dev); |
| 1105 | if (ret) |
| 1106 | goto err_enum_init; |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 1107 | |
Laurent Pinchart | 78c66fb | 2015-05-20 04:08:30 -0300 | [diff] [blame] | 1108 | /* TODO: Implement PM QoS */ |
Sakari Ailus | b0cd79e | 2012-01-16 18:59:02 -0300 | [diff] [blame] | 1109 | pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]); |
| 1110 | pipe->max_rate = pipe->l3_ick; |
| 1111 | |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 1112 | ret = media_pipeline_start(&video->video.entity, &pipe->pipe); |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1113 | if (ret < 0) |
| 1114 | goto err_pipeline_start; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1115 | |
| 1116 | /* Verify that the currently configured format matches the output of |
| 1117 | * the connected subdev. |
| 1118 | */ |
| 1119 | ret = isp_video_check_format(video, vfh); |
| 1120 | if (ret < 0) |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1121 | goto err_check_format; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1122 | |
| 1123 | video->bpl_padding = ret; |
| 1124 | video->bpl_value = vfh->format.fmt.pix.bytesperline; |
| 1125 | |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 1126 | ret = isp_video_get_graph_data(video, pipe); |
| 1127 | if (ret < 0) |
| 1128 | goto err_check_format; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1129 | |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 1130 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1131 | state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT; |
Sakari Ailus | ae5df81 | 2012-03-05 20:22:41 -0300 | [diff] [blame] | 1132 | else |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1133 | state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1134 | |
Sakari Ailus | ccddd91 | 2012-01-17 12:29:38 -0300 | [diff] [blame] | 1135 | ret = isp_video_check_external_subdevs(video, pipe); |
| 1136 | if (ret < 0) |
| 1137 | goto err_check_format; |
| 1138 | |
Laurent Pinchart | 875e2e3 | 2011-12-07 08:34:50 -0300 | [diff] [blame] | 1139 | pipe->error = false; |
| 1140 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1141 | spin_lock_irqsave(&pipe->lock, flags); |
| 1142 | pipe->state &= ~ISP_PIPELINE_STREAM; |
| 1143 | pipe->state |= state; |
| 1144 | spin_unlock_irqrestore(&pipe->lock, flags); |
| 1145 | |
| 1146 | /* Set the maximum time per frame as the value requested by userspace. |
| 1147 | * This is a soft limit that can be overridden if the hardware doesn't |
| 1148 | * support the request limit. |
| 1149 | */ |
| 1150 | if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) |
| 1151 | pipe->max_timeperframe = vfh->timeperframe; |
| 1152 | |
| 1153 | video->queue = &vfh->queue; |
| 1154 | INIT_LIST_HEAD(&video->dmaqueue); |
| 1155 | atomic_set(&pipe->frame_number, -1); |
Laurent Pinchart | 9a36d8e | 2014-05-19 16:37:38 -0300 | [diff] [blame] | 1156 | pipe->field = vfh->format.fmt.pix.field; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1157 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1158 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1159 | ret = vb2_streamon(&vfh->queue, type); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1160 | mutex_unlock(&video->queue_lock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1161 | if (ret < 0) |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1162 | goto err_check_format; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1163 | |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1164 | mutex_unlock(&video->stream_lock); |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 1165 | |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1166 | return 0; |
| 1167 | |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1168 | err_check_format: |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 1169 | media_pipeline_stop(&video->video.entity); |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1170 | err_pipeline_start: |
Laurent Pinchart | 78c66fb | 2015-05-20 04:08:30 -0300 | [diff] [blame] | 1171 | /* TODO: Implement PM QoS */ |
Sakari Ailus | da39257 | 2011-10-10 17:05:24 -0300 | [diff] [blame] | 1172 | /* The DMA queue must be emptied here, otherwise CCDC interrupts that |
| 1173 | * will get triggered the next time the CCDC is powered up will try to |
| 1174 | * access buffers that might have been freed but still present in the |
| 1175 | * DMA queue. This can easily get triggered if the above |
| 1176 | * omap3isp_pipeline_set_stream() call fails on a system with a |
| 1177 | * free-running sensor. |
| 1178 | */ |
| 1179 | INIT_LIST_HEAD(&video->dmaqueue); |
| 1180 | video->queue = NULL; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1181 | |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 1182 | media_entity_enum_cleanup(&pipe->ent_enum); |
| 1183 | |
| 1184 | err_enum_init: |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1185 | mutex_unlock(&video->stream_lock); |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 1186 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | static int |
| 1191 | isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) |
| 1192 | { |
| 1193 | struct isp_video_fh *vfh = to_isp_video_fh(fh); |
| 1194 | struct isp_video *video = video_drvdata(file); |
| 1195 | struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity); |
| 1196 | enum isp_pipeline_state state; |
| 1197 | unsigned int streaming; |
| 1198 | unsigned long flags; |
| 1199 | |
| 1200 | if (type != video->type) |
| 1201 | return -EINVAL; |
| 1202 | |
| 1203 | mutex_lock(&video->stream_lock); |
| 1204 | |
| 1205 | /* Make sure we're not streaming yet. */ |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1206 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1207 | streaming = vb2_is_streaming(&vfh->queue); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1208 | mutex_unlock(&video->queue_lock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1209 | |
| 1210 | if (!streaming) |
| 1211 | goto done; |
| 1212 | |
| 1213 | /* Update the pipeline state. */ |
| 1214 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1215 | state = ISP_PIPELINE_STREAM_OUTPUT |
| 1216 | | ISP_PIPELINE_QUEUE_OUTPUT; |
| 1217 | else |
| 1218 | state = ISP_PIPELINE_STREAM_INPUT |
| 1219 | | ISP_PIPELINE_QUEUE_INPUT; |
| 1220 | |
| 1221 | spin_lock_irqsave(&pipe->lock, flags); |
| 1222 | pipe->state &= ~state; |
| 1223 | spin_unlock_irqrestore(&pipe->lock, flags); |
| 1224 | |
| 1225 | /* Stop the stream. */ |
| 1226 | omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED); |
Laurent Pinchart | 0834449 | 2014-04-20 20:59:03 -0300 | [diff] [blame] | 1227 | omap3isp_video_cancel_stream(video); |
| 1228 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1229 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1230 | vb2_streamoff(&vfh->queue, type); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1231 | mutex_unlock(&video->queue_lock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1232 | video->queue = NULL; |
Laurent Pinchart | 661112c | 2013-12-09 11:36:51 -0300 | [diff] [blame] | 1233 | video->error = false; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1234 | |
Laurent Pinchart | 78c66fb | 2015-05-20 04:08:30 -0300 | [diff] [blame] | 1235 | /* TODO: Implement PM QoS */ |
Sakari Ailus | 20b8522 | 2016-11-21 14:48:30 -0200 | [diff] [blame] | 1236 | media_pipeline_stop(&video->video.entity); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1237 | |
Sakari Ailus | 17d3d40 | 2015-12-16 11:32:30 -0200 | [diff] [blame] | 1238 | media_entity_enum_cleanup(&pipe->ent_enum); |
| 1239 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1240 | done: |
| 1241 | mutex_unlock(&video->stream_lock); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static int |
| 1246 | isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input) |
| 1247 | { |
| 1248 | if (input->index > 0) |
| 1249 | return -EINVAL; |
| 1250 | |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 1251 | strscpy(input->name, "camera", sizeof(input->name)); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1252 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static int |
| 1258 | isp_video_g_input(struct file *file, void *fh, unsigned int *input) |
| 1259 | { |
| 1260 | *input = 0; |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static int |
| 1266 | isp_video_s_input(struct file *file, void *fh, unsigned int input) |
| 1267 | { |
| 1268 | return input == 0 ? 0 : -EINVAL; |
| 1269 | } |
| 1270 | |
| 1271 | static const struct v4l2_ioctl_ops isp_video_ioctl_ops = { |
| 1272 | .vidioc_querycap = isp_video_querycap, |
| 1273 | .vidioc_g_fmt_vid_cap = isp_video_get_format, |
| 1274 | .vidioc_s_fmt_vid_cap = isp_video_set_format, |
| 1275 | .vidioc_try_fmt_vid_cap = isp_video_try_format, |
| 1276 | .vidioc_g_fmt_vid_out = isp_video_get_format, |
| 1277 | .vidioc_s_fmt_vid_out = isp_video_set_format, |
| 1278 | .vidioc_try_fmt_vid_out = isp_video_try_format, |
Hans Verkuil | 10d5509c | 2015-12-14 08:25:32 -0200 | [diff] [blame] | 1279 | .vidioc_g_selection = isp_video_get_selection, |
| 1280 | .vidioc_s_selection = isp_video_set_selection, |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1281 | .vidioc_g_parm = isp_video_get_param, |
| 1282 | .vidioc_s_parm = isp_video_set_param, |
| 1283 | .vidioc_reqbufs = isp_video_reqbufs, |
| 1284 | .vidioc_querybuf = isp_video_querybuf, |
| 1285 | .vidioc_qbuf = isp_video_qbuf, |
| 1286 | .vidioc_dqbuf = isp_video_dqbuf, |
| 1287 | .vidioc_streamon = isp_video_streamon, |
| 1288 | .vidioc_streamoff = isp_video_streamoff, |
| 1289 | .vidioc_enum_input = isp_video_enum_input, |
| 1290 | .vidioc_g_input = isp_video_g_input, |
| 1291 | .vidioc_s_input = isp_video_s_input, |
| 1292 | }; |
| 1293 | |
| 1294 | /* ----------------------------------------------------------------------------- |
| 1295 | * V4L2 file operations |
| 1296 | */ |
| 1297 | |
| 1298 | static int isp_video_open(struct file *file) |
| 1299 | { |
| 1300 | struct isp_video *video = video_drvdata(file); |
| 1301 | struct isp_video_fh *handle; |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1302 | struct vb2_queue *queue; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1303 | int ret = 0; |
| 1304 | |
| 1305 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
| 1306 | if (handle == NULL) |
| 1307 | return -ENOMEM; |
| 1308 | |
| 1309 | v4l2_fh_init(&handle->vfh, &video->video); |
| 1310 | v4l2_fh_add(&handle->vfh); |
| 1311 | |
| 1312 | /* If this is the first user, initialise the pipeline. */ |
| 1313 | if (omap3isp_get(video->isp) == NULL) { |
| 1314 | ret = -EBUSY; |
| 1315 | goto done; |
| 1316 | } |
| 1317 | |
Sakari Ailus | 506a47e | 2016-02-21 13:25:10 -0300 | [diff] [blame] | 1318 | ret = v4l2_pipeline_pm_use(&video->video.entity, 1); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1319 | if (ret < 0) { |
| 1320 | omap3isp_put(video->isp); |
| 1321 | goto done; |
| 1322 | } |
| 1323 | |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1324 | queue = &handle->queue; |
| 1325 | queue->type = video->type; |
| 1326 | queue->io_modes = VB2_MMAP | VB2_USERPTR; |
| 1327 | queue->drv_priv = handle; |
| 1328 | queue->ops = &isp_video_queue_ops; |
| 1329 | queue->mem_ops = &vb2_dma_contig_memops; |
| 1330 | queue->buf_struct_size = sizeof(struct isp_buffer); |
| 1331 | queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Hans Verkuil | 1ad70ce | 2016-02-15 13:41:51 -0200 | [diff] [blame] | 1332 | queue->dev = video->isp->dev; |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1333 | |
| 1334 | ret = vb2_queue_init(&handle->queue); |
| 1335 | if (ret < 0) { |
| 1336 | omap3isp_put(video->isp); |
| 1337 | goto done; |
| 1338 | } |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1339 | |
| 1340 | memset(&handle->format, 0, sizeof(handle->format)); |
| 1341 | handle->format.type = video->type; |
| 1342 | handle->timeperframe.denominator = 1; |
| 1343 | |
| 1344 | handle->video = video; |
| 1345 | file->private_data = &handle->vfh; |
| 1346 | |
| 1347 | done: |
| 1348 | if (ret < 0) { |
| 1349 | v4l2_fh_del(&handle->vfh); |
Shailendra Verma | 1deb54d | 2016-11-25 02:44:32 -0200 | [diff] [blame] | 1350 | v4l2_fh_exit(&handle->vfh); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1351 | kfree(handle); |
| 1352 | } |
| 1353 | |
| 1354 | return ret; |
| 1355 | } |
| 1356 | |
| 1357 | static int isp_video_release(struct file *file) |
| 1358 | { |
| 1359 | struct isp_video *video = video_drvdata(file); |
| 1360 | struct v4l2_fh *vfh = file->private_data; |
| 1361 | struct isp_video_fh *handle = to_isp_video_fh(vfh); |
| 1362 | |
| 1363 | /* Disable streaming and free the buffers queue resources. */ |
| 1364 | isp_video_streamoff(file, vfh, video->type); |
| 1365 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1366 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1367 | vb2_queue_release(&handle->queue); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1368 | mutex_unlock(&video->queue_lock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1369 | |
Sakari Ailus | 506a47e | 2016-02-21 13:25:10 -0300 | [diff] [blame] | 1370 | v4l2_pipeline_pm_use(&video->video.entity, 0); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1371 | |
| 1372 | /* Release the file handle. */ |
| 1373 | v4l2_fh_del(vfh); |
Shailendra Verma | 1deb54d | 2016-11-25 02:44:32 -0200 | [diff] [blame] | 1374 | v4l2_fh_exit(vfh); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1375 | kfree(handle); |
| 1376 | file->private_data = NULL; |
| 1377 | |
| 1378 | omap3isp_put(video->isp); |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 1383 | static __poll_t isp_video_poll(struct file *file, poll_table *wait) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1384 | { |
| 1385 | struct isp_video_fh *vfh = to_isp_video_fh(file->private_data); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1386 | struct isp_video *video = video_drvdata(file); |
Al Viro | c23e0cb | 2017-07-03 03:02:56 -0400 | [diff] [blame] | 1387 | __poll_t ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1388 | |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1389 | mutex_lock(&video->queue_lock); |
Laurent Pinchart | fbac140 | 2014-03-09 20:36:15 -0300 | [diff] [blame] | 1390 | ret = vb2_poll(&vfh->queue, file, wait); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1391 | mutex_unlock(&video->queue_lock); |
| 1392 | |
| 1393 | return ret; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | static int isp_video_mmap(struct file *file, struct vm_area_struct *vma) |
| 1397 | { |
| 1398 | struct isp_video_fh *vfh = to_isp_video_fh(file->private_data); |
| 1399 | |
Laurent Pinchart | 9be29369 | 2015-02-24 07:39:49 -0300 | [diff] [blame] | 1400 | return vb2_mmap(&vfh->queue, vma); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1401 | } |
| 1402 | |
Bhumika Goyal | cdc9c74 | 2017-08-26 02:12:07 -0400 | [diff] [blame] | 1403 | static const struct v4l2_file_operations isp_video_fops = { |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1404 | .owner = THIS_MODULE, |
| 1405 | .unlocked_ioctl = video_ioctl2, |
| 1406 | .open = isp_video_open, |
| 1407 | .release = isp_video_release, |
| 1408 | .poll = isp_video_poll, |
| 1409 | .mmap = isp_video_mmap, |
| 1410 | }; |
| 1411 | |
| 1412 | /* ----------------------------------------------------------------------------- |
| 1413 | * ISP video core |
| 1414 | */ |
| 1415 | |
| 1416 | static const struct isp_video_operations isp_video_dummy_ops = { |
| 1417 | }; |
| 1418 | |
| 1419 | int omap3isp_video_init(struct isp_video *video, const char *name) |
| 1420 | { |
| 1421 | const char *direction; |
| 1422 | int ret; |
| 1423 | |
| 1424 | switch (video->type) { |
| 1425 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 1426 | direction = "output"; |
Sakari Ailus | 8dad936 | 2013-10-02 20:17:52 -0300 | [diff] [blame] | 1427 | video->pad.flags = MEDIA_PAD_FL_SINK |
| 1428 | | MEDIA_PAD_FL_MUST_CONNECT; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1429 | break; |
| 1430 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 1431 | direction = "input"; |
Sakari Ailus | 8dad936 | 2013-10-02 20:17:52 -0300 | [diff] [blame] | 1432 | video->pad.flags = MEDIA_PAD_FL_SOURCE |
| 1433 | | MEDIA_PAD_FL_MUST_CONNECT; |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 1434 | video->video.vfl_dir = VFL_DIR_TX; |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1435 | break; |
| 1436 | |
| 1437 | default: |
| 1438 | return -EINVAL; |
| 1439 | } |
| 1440 | |
Mauro Carvalho Chehab | ab22e77 | 2015-12-11 07:44:40 -0200 | [diff] [blame] | 1441 | ret = media_entity_pads_init(&video->video.entity, 1, &video->pad); |
Hans Verkuil | 1ad70ce | 2016-02-15 13:41:51 -0200 | [diff] [blame] | 1442 | if (ret < 0) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1443 | return ret; |
| 1444 | |
| 1445 | mutex_init(&video->mutex); |
| 1446 | atomic_set(&video->active, 0); |
| 1447 | |
| 1448 | spin_lock_init(&video->pipe.lock); |
| 1449 | mutex_init(&video->stream_lock); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1450 | mutex_init(&video->queue_lock); |
Laurent Pinchart | e8feb87 | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1451 | spin_lock_init(&video->irqlock); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1452 | |
| 1453 | /* Initialize the video device. */ |
| 1454 | if (video->ops == NULL) |
| 1455 | video->ops = &isp_video_dummy_ops; |
| 1456 | |
| 1457 | video->video.fops = &isp_video_fops; |
| 1458 | snprintf(video->video.name, sizeof(video->video.name), |
| 1459 | "OMAP3 ISP %s %s", name, direction); |
| 1460 | video->video.vfl_type = VFL_TYPE_GRABBER; |
| 1461 | video->video.release = video_device_release_empty; |
| 1462 | video->video.ioctl_ops = &isp_video_ioctl_ops; |
| 1463 | video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED; |
| 1464 | |
| 1465 | video_set_drvdata(&video->video, video); |
| 1466 | |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
Laurent Pinchart | 63b4ca2 | 2011-09-22 16:54:34 -0300 | [diff] [blame] | 1470 | void omap3isp_video_cleanup(struct isp_video *video) |
| 1471 | { |
| 1472 | media_entity_cleanup(&video->video.entity); |
Laurent Pinchart | 988d54c | 2014-03-09 20:57:53 -0300 | [diff] [blame] | 1473 | mutex_destroy(&video->queue_lock); |
Laurent Pinchart | ed33ac8 | 2011-09-22 17:09:26 -0300 | [diff] [blame] | 1474 | mutex_destroy(&video->stream_lock); |
| 1475 | mutex_destroy(&video->mutex); |
Laurent Pinchart | 63b4ca2 | 2011-09-22 16:54:34 -0300 | [diff] [blame] | 1476 | } |
| 1477 | |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1478 | int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev) |
| 1479 | { |
| 1480 | int ret; |
| 1481 | |
| 1482 | video->video.v4l2_dev = vdev; |
| 1483 | |
| 1484 | ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1); |
| 1485 | if (ret < 0) |
Laurent Pinchart | 4feca39 | 2012-10-22 08:28:51 -0300 | [diff] [blame] | 1486 | dev_err(video->isp->dev, |
| 1487 | "%s: could not register video device (%d)\n", |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1488 | __func__, ret); |
| 1489 | |
| 1490 | return ret; |
| 1491 | } |
| 1492 | |
| 1493 | void omap3isp_video_unregister(struct isp_video *video) |
| 1494 | { |
Laurent Pinchart | 63b4ca2 | 2011-09-22 16:54:34 -0300 | [diff] [blame] | 1495 | if (video_is_registered(&video->video)) |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1496 | video_unregister_device(&video->video); |
Laurent Pinchart | ad614ac | 2011-02-12 18:05:06 -0300 | [diff] [blame] | 1497 | } |