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