Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Samsung S5P SoC series camera interface (camera capture) driver |
| 3 | * |
| 4 | * Copyright (c) 2010 Samsung Electronics Co., Ltd |
| 5 | * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/version.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/bug.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/list.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/clk.h> |
| 24 | #include <linux/i2c.h> |
| 25 | |
| 26 | #include <linux/videodev2.h> |
| 27 | #include <media/v4l2-device.h> |
| 28 | #include <media/v4l2-ioctl.h> |
| 29 | #include <media/v4l2-mem2mem.h> |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 30 | #include <media/videobuf2-core.h> |
| 31 | #include <media/videobuf2-dma-contig.h> |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 32 | |
| 33 | #include "fimc-core.h" |
| 34 | |
| 35 | static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc, |
Sylwester Nawrocki | df7e09a | 2010-12-27 14:42:15 -0300 | [diff] [blame] | 36 | struct s5p_fimc_isp_info *isp_info) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 37 | { |
| 38 | struct i2c_adapter *i2c_adap; |
| 39 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 40 | struct v4l2_subdev *sd = NULL; |
| 41 | |
| 42 | i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num); |
| 43 | if (!i2c_adap) |
| 44 | return ERR_PTR(-ENOMEM); |
| 45 | |
| 46 | sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 47 | isp_info->board_info, NULL); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 48 | if (!sd) { |
| 49 | v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n"); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n", |
| 54 | isp_info->board_info->type); |
| 55 | |
| 56 | return sd; |
| 57 | } |
| 58 | |
| 59 | static void fimc_subdev_unregister(struct fimc_dev *fimc) |
| 60 | { |
| 61 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 62 | struct i2c_client *client; |
| 63 | |
| 64 | if (vid_cap->input_index < 0) |
| 65 | return; /* Subdevice already released or not registered. */ |
| 66 | |
| 67 | if (vid_cap->sd) { |
| 68 | v4l2_device_unregister_subdev(vid_cap->sd); |
| 69 | client = v4l2_get_subdevdata(vid_cap->sd); |
| 70 | i2c_unregister_device(client); |
| 71 | i2c_put_adapter(client->adapter); |
| 72 | vid_cap->sd = NULL; |
| 73 | } |
| 74 | |
| 75 | vid_cap->input_index = -1; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * fimc_subdev_attach - attach v4l2_subdev to camera host interface |
| 80 | * |
| 81 | * @fimc: FIMC device information |
| 82 | * @index: index to the array of available subdevices, |
| 83 | * -1 for full array search or non negative value |
| 84 | * to select specific subdevice |
| 85 | */ |
| 86 | static int fimc_subdev_attach(struct fimc_dev *fimc, int index) |
| 87 | { |
| 88 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
Sylwester Nawrocki | df7e09a | 2010-12-27 14:42:15 -0300 | [diff] [blame] | 89 | struct s5p_platform_fimc *pdata = fimc->pdata; |
| 90 | struct s5p_fimc_isp_info *isp_info; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 91 | struct v4l2_subdev *sd; |
| 92 | int i; |
| 93 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 94 | for (i = 0; i < pdata->num_clients; ++i) { |
| 95 | isp_info = &pdata->isp_info[i]; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 96 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 97 | if (index >= 0 && i != index) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 98 | continue; |
| 99 | |
| 100 | sd = fimc_subdev_register(fimc, isp_info); |
Sylwester Nawrocki | a0f8cae | 2011-02-11 12:33:06 -0300 | [diff] [blame] | 101 | if (!IS_ERR_OR_NULL(sd)) { |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 102 | vid_cap->sd = sd; |
| 103 | vid_cap->input_index = i; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | vid_cap->input_index = -1; |
| 110 | vid_cap->sd = NULL; |
| 111 | v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n", |
| 112 | fimc->id); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 116 | static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 117 | { |
Sylwester Nawrocki | df7e09a | 2010-12-27 14:42:15 -0300 | [diff] [blame] | 118 | struct s5p_fimc_isp_info *isp_info; |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 119 | struct s5p_platform_fimc *pdata = fimc->pdata; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 120 | int ret; |
| 121 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 122 | if (index >= pdata->num_clients) |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 125 | isp_info = &pdata->isp_info[index]; |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 126 | |
| 127 | if (isp_info->clk_frequency) |
| 128 | clk_set_rate(fimc->clock[CLK_CAM], isp_info->clk_frequency); |
| 129 | |
| 130 | ret = clk_enable(fimc->clock[CLK_CAM]); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 134 | ret = fimc_subdev_attach(fimc, index); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 138 | ret = fimc_hw_set_camera_polarity(fimc, isp_info); |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 139 | if (ret) |
| 140 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 141 | |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 142 | ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1); |
| 143 | if (!ret) |
| 144 | return ret; |
| 145 | |
| 146 | /* enabling power failed so unregister subdev */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 147 | fimc_subdev_unregister(fimc); |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 148 | |
| 149 | v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n", |
| 150 | ret); |
| 151 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 155 | static int fimc_stop_capture(struct fimc_dev *fimc) |
| 156 | { |
| 157 | unsigned long flags; |
| 158 | struct fimc_vid_cap *cap; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 159 | struct fimc_vid_buffer *buf; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 160 | |
| 161 | cap = &fimc->vid_cap; |
| 162 | |
| 163 | if (!fimc_capture_active(fimc)) |
| 164 | return 0; |
| 165 | |
| 166 | spin_lock_irqsave(&fimc->slock, flags); |
| 167 | set_bit(ST_CAPT_SHUT, &fimc->state); |
| 168 | fimc_deactivate_capture(fimc); |
| 169 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 170 | |
| 171 | wait_event_timeout(fimc->irq_queue, |
Sungchun Kang | ba10795 | 2010-12-30 02:35:28 -0300 | [diff] [blame] | 172 | !test_bit(ST_CAPT_SHUT, &fimc->state), |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 173 | FIMC_SHUTDOWN_TIMEOUT); |
| 174 | |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 175 | v4l2_subdev_call(cap->sd, video, s_stream, 0); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 176 | |
| 177 | spin_lock_irqsave(&fimc->slock, flags); |
| 178 | fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 179 | 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 180 | |
| 181 | fimc->vid_cap.active_buf_cnt = 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 182 | |
| 183 | /* Release buffers that were enqueued in the driver by videobuf2. */ |
| 184 | while (!list_empty(&cap->pending_buf_q)) { |
| 185 | buf = pending_queue_pop(cap); |
| 186 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 187 | } |
| 188 | |
| 189 | while (!list_empty(&cap->active_buf_q)) { |
| 190 | buf = active_queue_pop(cap); |
| 191 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 192 | } |
| 193 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 194 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 195 | |
| 196 | dbg("state: 0x%lx", fimc->state); |
| 197 | return 0; |
| 198 | } |
| 199 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 200 | static int start_streaming(struct vb2_queue *q) |
| 201 | { |
| 202 | struct fimc_ctx *ctx = q->drv_priv; |
| 203 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | df7e09a | 2010-12-27 14:42:15 -0300 | [diff] [blame] | 204 | struct s5p_fimc_isp_info *isp_info; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 205 | int ret; |
| 206 | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 207 | fimc_hw_reset(fimc); |
| 208 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 209 | ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1); |
| 210 | if (ret && ret != -ENOIOCTLCMD) |
| 211 | return ret; |
| 212 | |
| 213 | ret = fimc_prepare_config(ctx, ctx->state); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 217 | isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index]; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 218 | fimc_hw_set_camera_type(fimc, isp_info); |
| 219 | fimc_hw_set_camera_source(fimc, isp_info); |
| 220 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); |
| 221 | |
| 222 | if (ctx->state & FIMC_PARAMS) { |
| 223 | ret = fimc_set_scaler_info(ctx); |
| 224 | if (ret) { |
| 225 | err("Scaler setup error"); |
| 226 | return ret; |
| 227 | } |
| 228 | fimc_hw_set_input_path(ctx); |
Hyunwoong Kim | b241c6d | 2010-12-28 11:27:13 -0300 | [diff] [blame] | 229 | fimc_hw_set_prescaler(ctx); |
Sylwester Nawrocki | 70f66ea | 2010-12-28 11:37:55 -0300 | [diff] [blame] | 230 | fimc_hw_set_mainscaler(ctx); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 231 | fimc_hw_set_target_format(ctx); |
| 232 | fimc_hw_set_rotation(ctx); |
| 233 | fimc_hw_set_effect(ctx); |
| 234 | } |
| 235 | |
| 236 | fimc_hw_set_output_path(ctx); |
| 237 | fimc_hw_set_out_dma(ctx); |
| 238 | |
| 239 | INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q); |
| 240 | INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q); |
| 241 | fimc->vid_cap.active_buf_cnt = 0; |
| 242 | fimc->vid_cap.frame_count = 0; |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 243 | fimc->vid_cap.buf_index = 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 244 | |
| 245 | set_bit(ST_CAPT_PEND, &fimc->state); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int stop_streaming(struct vb2_queue *q) |
| 251 | { |
| 252 | struct fimc_ctx *ctx = q->drv_priv; |
| 253 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 254 | |
Sylwester Nawrocki | 4ecbf5d | 2011-02-23 08:24:33 -0300 | [diff] [blame] | 255 | if (!fimc_capture_active(fimc)) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 256 | return -EINVAL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 257 | |
| 258 | return fimc_stop_capture(fimc); |
| 259 | } |
| 260 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 261 | static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 262 | { |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 263 | if (!fr || plane >= fr->fmt->memplanes) |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 264 | return 0; |
| 265 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 266 | dbg("%s: w: %d. h: %d. depth[%d]: %d", |
| 267 | __func__, fr->width, fr->height, plane, fr->fmt->depth[plane]); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 268 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 269 | return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8; |
| 270 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers, |
| 274 | unsigned int *num_planes, unsigned long sizes[], |
| 275 | void *allocators[]) |
| 276 | { |
| 277 | struct fimc_ctx *ctx = vq->drv_priv; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 278 | struct fimc_fmt *fmt = ctx->d_frame.fmt; |
| 279 | int i; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 280 | |
| 281 | if (!fmt) |
| 282 | return -EINVAL; |
| 283 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 284 | *num_planes = fmt->memplanes; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 285 | |
| 286 | dbg("%s, buffer count=%d, plane count=%d", |
| 287 | __func__, *num_buffers, *num_planes); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 288 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 289 | for (i = 0; i < fmt->memplanes; i++) { |
| 290 | sizes[i] = get_plane_size(&ctx->d_frame, i); |
| 291 | dbg("plane: %u, plane_size: %lu", i, sizes[i]); |
| 292 | allocators[i] = ctx->fimc_dev->alloc_ctx; |
| 293 | } |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 294 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 295 | return 0; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int buffer_init(struct vb2_buffer *vb) |
| 299 | { |
| 300 | /* TODO: */ |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int buffer_prepare(struct vb2_buffer *vb) |
| 305 | { |
| 306 | struct vb2_queue *vq = vb->vb2_queue; |
| 307 | struct fimc_ctx *ctx = vq->drv_priv; |
| 308 | struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 309 | int i; |
| 310 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 311 | if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 312 | return -EINVAL; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 313 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 314 | for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) { |
| 315 | unsigned long size = get_plane_size(&ctx->d_frame, i); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 316 | |
| 317 | if (vb2_plane_size(vb, i) < size) { |
| 318 | v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n", |
| 319 | vb2_plane_size(vb, i), size); |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | |
| 323 | vb2_set_plane_payload(vb, i, size); |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static void buffer_queue(struct vb2_buffer *vb) |
| 330 | { |
| 331 | struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 332 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 333 | struct fimc_vid_buffer *buf |
| 334 | = container_of(vb, struct fimc_vid_buffer, vb); |
| 335 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
| 336 | unsigned long flags; |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 337 | int min_bufs; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 338 | |
| 339 | spin_lock_irqsave(&fimc->slock, flags); |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 340 | fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 341 | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 342 | if (!test_bit(ST_CAPT_STREAM, &fimc->state) |
| 343 | && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) { |
| 344 | /* Setup the buffer directly for processing. */ |
| 345 | int buf_id = (vid_cap->reqbufs_count == 1) ? -1 : |
| 346 | vid_cap->buf_index; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 347 | |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 348 | fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id); |
| 349 | buf->index = vid_cap->buf_index; |
| 350 | active_queue_add(vid_cap, buf); |
| 351 | |
| 352 | if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS) |
| 353 | vid_cap->buf_index = 0; |
| 354 | } else { |
| 355 | fimc_pending_queue_add(vid_cap, buf); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 356 | } |
Sungchun Kang | 8ec737f | 2011-02-07 15:59:46 -0300 | [diff] [blame] | 357 | |
| 358 | min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1; |
| 359 | |
| 360 | if (vid_cap->active_buf_cnt >= min_bufs && |
| 361 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) |
| 362 | fimc_activate_capture(ctx); |
| 363 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 364 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 365 | } |
| 366 | |
| 367 | static void fimc_lock(struct vb2_queue *vq) |
| 368 | { |
| 369 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); |
| 370 | mutex_lock(&ctx->fimc_dev->lock); |
| 371 | } |
| 372 | |
| 373 | static void fimc_unlock(struct vb2_queue *vq) |
| 374 | { |
| 375 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); |
| 376 | mutex_unlock(&ctx->fimc_dev->lock); |
| 377 | } |
| 378 | |
| 379 | static struct vb2_ops fimc_capture_qops = { |
| 380 | .queue_setup = queue_setup, |
| 381 | .buf_prepare = buffer_prepare, |
| 382 | .buf_queue = buffer_queue, |
| 383 | .buf_init = buffer_init, |
| 384 | .wait_prepare = fimc_unlock, |
| 385 | .wait_finish = fimc_lock, |
| 386 | .start_streaming = start_streaming, |
| 387 | .stop_streaming = stop_streaming, |
| 388 | }; |
| 389 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 390 | static int fimc_capture_open(struct file *file) |
| 391 | { |
| 392 | struct fimc_dev *fimc = video_drvdata(file); |
| 393 | int ret = 0; |
| 394 | |
| 395 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 396 | |
| 397 | /* Return if the corresponding video mem2mem node is already opened. */ |
| 398 | if (fimc_m2m_active(fimc)) |
| 399 | return -EBUSY; |
| 400 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 401 | if (++fimc->vid_cap.refcnt == 1) { |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 402 | ret = fimc_isp_subdev_init(fimc, 0); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 403 | if (ret) { |
| 404 | fimc->vid_cap.refcnt--; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 405 | return -EIO; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | file->private_data = fimc->vid_cap.ctx; |
| 410 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 411 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static int fimc_capture_close(struct file *file) |
| 415 | { |
| 416 | struct fimc_dev *fimc = video_drvdata(file); |
| 417 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 418 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
| 419 | |
| 420 | if (--fimc->vid_cap.refcnt == 0) { |
| 421 | fimc_stop_capture(fimc); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 422 | vb2_queue_release(&fimc->vid_cap.vbq); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 423 | |
| 424 | v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n"); |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 425 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 426 | v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0); |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 427 | clk_disable(fimc->clock[CLK_CAM]); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 428 | fimc_subdev_unregister(fimc); |
| 429 | } |
| 430 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static unsigned int fimc_capture_poll(struct file *file, |
| 435 | struct poll_table_struct *wait) |
| 436 | { |
| 437 | struct fimc_ctx *ctx = file->private_data; |
| 438 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 439 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 440 | return vb2_poll(&fimc->vid_cap.vbq, file, wait); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma) |
| 444 | { |
| 445 | struct fimc_ctx *ctx = file->private_data; |
| 446 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 447 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 448 | return vb2_mmap(&fimc->vid_cap.vbq, vma); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* video device file operations */ |
| 452 | static const struct v4l2_file_operations fimc_capture_fops = { |
| 453 | .owner = THIS_MODULE, |
| 454 | .open = fimc_capture_open, |
| 455 | .release = fimc_capture_close, |
| 456 | .poll = fimc_capture_poll, |
| 457 | .unlocked_ioctl = video_ioctl2, |
| 458 | .mmap = fimc_capture_mmap, |
| 459 | }; |
| 460 | |
| 461 | static int fimc_vidioc_querycap_capture(struct file *file, void *priv, |
| 462 | struct v4l2_capability *cap) |
| 463 | { |
| 464 | struct fimc_ctx *ctx = file->private_data; |
| 465 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 466 | |
| 467 | strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1); |
| 468 | strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1); |
| 469 | cap->bus_info[0] = 0; |
| 470 | cap->version = KERNEL_VERSION(1, 0, 0); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 471 | cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE | |
| 472 | V4L2_CAP_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* Synchronize formats of the camera interface input and attached sensor. */ |
| 478 | static int sync_capture_fmt(struct fimc_ctx *ctx) |
| 479 | { |
| 480 | struct fimc_frame *frame = &ctx->s_frame; |
| 481 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 482 | struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt; |
| 483 | int ret; |
| 484 | |
| 485 | fmt->width = ctx->d_frame.o_width; |
| 486 | fmt->height = ctx->d_frame.o_height; |
| 487 | |
| 488 | ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt); |
| 489 | if (ret == -ENOIOCTLCMD) { |
| 490 | err("s_mbus_fmt failed"); |
| 491 | return ret; |
| 492 | } |
| 493 | dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code); |
| 494 | |
| 495 | frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM); |
| 496 | if (!frame->fmt) { |
| 497 | err("fimc source format not found\n"); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | |
| 501 | frame->f_width = fmt->width; |
| 502 | frame->f_height = fmt->height; |
| 503 | frame->width = fmt->width; |
| 504 | frame->height = fmt->height; |
| 505 | frame->o_width = fmt->width; |
| 506 | frame->o_height = fmt->height; |
| 507 | frame->offs_h = 0; |
| 508 | frame->offs_v = 0; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 513 | static int fimc_cap_s_fmt_mplane(struct file *file, void *priv, |
| 514 | struct v4l2_format *f) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 515 | { |
| 516 | struct fimc_ctx *ctx = priv; |
| 517 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 518 | struct fimc_frame *frame; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 519 | struct v4l2_pix_format_mplane *pix; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 520 | int ret; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 521 | int i; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 522 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 523 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 524 | return -EINVAL; |
| 525 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 526 | ret = fimc_vidioc_try_fmt_mplane(file, priv, f); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 527 | if (ret) |
| 528 | return ret; |
| 529 | |
Sylwester Nawrocki | c4a6273 | 2011-03-15 14:50:52 -0300 | [diff] [blame^] | 530 | if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc)) |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 531 | return -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 532 | |
| 533 | frame = &ctx->d_frame; |
| 534 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 535 | pix = &f->fmt.pix_mp; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 536 | frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM); |
| 537 | if (!frame->fmt) { |
| 538 | err("fimc target format not found\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 539 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 540 | } |
| 541 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 542 | for (i = 0; i < frame->fmt->colplanes; i++) |
| 543 | frame->payload[i] = pix->plane_fmt[i].bytesperline * pix->height; |
| 544 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 545 | /* Output DMA frame pixel size and offsets. */ |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 546 | frame->f_width = pix->plane_fmt[0].bytesperline * 8 |
| 547 | / frame->fmt->depth[0]; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 548 | frame->f_height = pix->height; |
| 549 | frame->width = pix->width; |
| 550 | frame->height = pix->height; |
| 551 | frame->o_width = pix->width; |
| 552 | frame->o_height = pix->height; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 553 | frame->offs_h = 0; |
| 554 | frame->offs_v = 0; |
| 555 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 556 | ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT); |
| 557 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 558 | ret = sync_capture_fmt(ctx); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | static int fimc_cap_enum_input(struct file *file, void *priv, |
| 563 | struct v4l2_input *i) |
| 564 | { |
| 565 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | df7e09a | 2010-12-27 14:42:15 -0300 | [diff] [blame] | 566 | struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata; |
| 567 | struct s5p_fimc_isp_info *isp_info; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 568 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 569 | if (i->index >= pldata->num_clients) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 570 | return -EINVAL; |
| 571 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 572 | isp_info = &pldata->isp_info[i->index]; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 573 | |
| 574 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 575 | strncpy(i->name, isp_info->board_info->type, 32); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static int fimc_cap_s_input(struct file *file, void *priv, |
| 580 | unsigned int i) |
| 581 | { |
| 582 | struct fimc_ctx *ctx = priv; |
| 583 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | df7e09a | 2010-12-27 14:42:15 -0300 | [diff] [blame] | 584 | struct s5p_platform_fimc *pdata = fimc->pdata; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 585 | |
| 586 | if (fimc_capture_active(ctx->fimc_dev)) |
| 587 | return -EBUSY; |
| 588 | |
Sylwester Nawrocki | 117182d | 2011-02-28 11:12:19 -0300 | [diff] [blame] | 589 | if (i >= pdata->num_clients) |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 590 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 591 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 592 | |
| 593 | if (fimc->vid_cap.sd) { |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 594 | int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 595 | if (ret) |
| 596 | err("s_power failed: %d", ret); |
Sylwester Nawrocki | a25be18 | 2010-12-27 15:34:43 -0300 | [diff] [blame] | 597 | |
| 598 | clk_disable(fimc->clock[CLK_CAM]); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /* Release the attached sensor subdevice. */ |
| 602 | fimc_subdev_unregister(fimc); |
| 603 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 604 | return fimc_isp_subdev_init(fimc, i); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static int fimc_cap_g_input(struct file *file, void *priv, |
| 608 | unsigned int *i) |
| 609 | { |
| 610 | struct fimc_ctx *ctx = priv; |
| 611 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
| 612 | |
| 613 | *i = cap->input_index; |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int fimc_cap_streamon(struct file *file, void *priv, |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 618 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 619 | { |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 620 | struct fimc_ctx *ctx = priv; |
| 621 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 622 | |
| 623 | if (fimc_capture_active(fimc) || !fimc->vid_cap.sd) |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 624 | return -EBUSY; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 625 | |
| 626 | if (!(ctx->state & FIMC_DST_FMT)) { |
| 627 | v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 628 | return -EINVAL; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 629 | } |
| 630 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 631 | return vb2_streamon(&fimc->vid_cap.vbq, type); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int fimc_cap_streamoff(struct file *file, void *priv, |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 635 | enum v4l2_buf_type type) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 636 | { |
| 637 | struct fimc_ctx *ctx = priv; |
| 638 | struct fimc_dev *fimc = ctx->fimc_dev; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 639 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 640 | return vb2_streamoff(&fimc->vid_cap.vbq, type); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static int fimc_cap_reqbufs(struct file *file, void *priv, |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 644 | struct v4l2_requestbuffers *reqbufs) |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 645 | { |
| 646 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 647 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 648 | int ret; |
| 649 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 650 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 651 | ret = vb2_reqbufs(&cap->vbq, reqbufs); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 652 | if (!ret) |
| 653 | cap->reqbufs_count = reqbufs->count; |
| 654 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | static int fimc_cap_querybuf(struct file *file, void *priv, |
| 659 | struct v4l2_buffer *buf) |
| 660 | { |
| 661 | struct fimc_ctx *ctx = priv; |
| 662 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
| 663 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 664 | return vb2_querybuf(&cap->vbq, buf); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static int fimc_cap_qbuf(struct file *file, void *priv, |
| 668 | struct v4l2_buffer *buf) |
| 669 | { |
| 670 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 671 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; |
| 672 | return vb2_qbuf(&cap->vbq, buf); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static int fimc_cap_dqbuf(struct file *file, void *priv, |
| 676 | struct v4l2_buffer *buf) |
| 677 | { |
| 678 | struct fimc_ctx *ctx = priv; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 679 | return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 680 | file->f_flags & O_NONBLOCK); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static int fimc_cap_s_ctrl(struct file *file, void *priv, |
| 684 | struct v4l2_control *ctrl) |
| 685 | { |
| 686 | struct fimc_ctx *ctx = priv; |
| 687 | int ret = -EINVAL; |
| 688 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 689 | /* Allow any controls but 90/270 rotation while streaming */ |
| 690 | if (!fimc_capture_active(ctx->fimc_dev) || |
| 691 | ctrl->id != V4L2_CID_ROTATE || |
| 692 | (ctrl->value != 90 && ctrl->value != 270)) { |
| 693 | ret = check_ctrl_val(ctx, ctrl); |
| 694 | if (!ret) { |
| 695 | ret = fimc_s_ctrl(ctx, ctrl); |
| 696 | if (!ret) |
| 697 | ctx->state |= FIMC_PARAMS; |
| 698 | } |
| 699 | } |
| 700 | if (ret == -EINVAL) |
| 701 | ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd, |
| 702 | core, s_ctrl, ctrl); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 703 | return ret; |
| 704 | } |
| 705 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 706 | static int fimc_cap_cropcap(struct file *file, void *fh, |
| 707 | struct v4l2_cropcap *cr) |
| 708 | { |
| 709 | struct fimc_frame *f; |
| 710 | struct fimc_ctx *ctx = fh; |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 711 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 712 | if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 713 | return -EINVAL; |
| 714 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 715 | f = &ctx->s_frame; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 716 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 717 | cr->bounds.left = 0; |
| 718 | cr->bounds.top = 0; |
| 719 | cr->bounds.width = f->o_width; |
| 720 | cr->bounds.height = f->o_height; |
| 721 | cr->defrect = cr->bounds; |
| 722 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr) |
| 727 | { |
| 728 | struct fimc_frame *f; |
| 729 | struct fimc_ctx *ctx = file->private_data; |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 730 | |
| 731 | f = &ctx->s_frame; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 732 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 733 | cr->c.left = f->offs_h; |
| 734 | cr->c.top = f->offs_v; |
| 735 | cr->c.width = f->width; |
| 736 | cr->c.height = f->height; |
| 737 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 741 | static int fimc_cap_s_crop(struct file *file, void *fh, |
| 742 | struct v4l2_crop *cr) |
| 743 | { |
| 744 | struct fimc_frame *f; |
| 745 | struct fimc_ctx *ctx = file->private_data; |
| 746 | struct fimc_dev *fimc = ctx->fimc_dev; |
| 747 | int ret = -EINVAL; |
| 748 | |
| 749 | if (fimc_capture_active(fimc)) |
| 750 | return -EBUSY; |
| 751 | |
| 752 | ret = fimc_try_crop(ctx, cr); |
| 753 | if (ret) |
| 754 | return ret; |
| 755 | |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 756 | if (!(ctx->state & FIMC_DST_FMT)) { |
| 757 | v4l2_err(&fimc->vid_cap.v4l2_dev, |
| 758 | "Capture color format not set\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 759 | return -EINVAL; /* TODO: make sure this is the right value */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | f = &ctx->s_frame; |
| 763 | /* Check for the pixel scaling ratio when cropping input image. */ |
Hyunwoong Kim | 1b09f29 | 2010-12-28 22:12:43 -0300 | [diff] [blame] | 764 | ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height, |
| 765 | ctx->d_frame.width, ctx->d_frame.height, |
| 766 | ctx->rotation); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 767 | if (ret) { |
Sylwester Nawrocki | 4ecbf5d | 2011-02-23 08:24:33 -0300 | [diff] [blame] | 768 | v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range\n"); |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 769 | return ret; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 770 | } |
| 771 | |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 772 | f->offs_h = cr->c.left; |
| 773 | f->offs_v = cr->c.top; |
| 774 | f->width = cr->c.width; |
| 775 | f->height = cr->c.height; |
| 776 | |
| 777 | return 0; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
| 781 | static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = { |
| 782 | .vidioc_querycap = fimc_vidioc_querycap_capture, |
| 783 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 784 | .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane, |
| 785 | .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane, |
| 786 | .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane, |
| 787 | .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 788 | |
| 789 | .vidioc_reqbufs = fimc_cap_reqbufs, |
| 790 | .vidioc_querybuf = fimc_cap_querybuf, |
| 791 | |
| 792 | .vidioc_qbuf = fimc_cap_qbuf, |
| 793 | .vidioc_dqbuf = fimc_cap_dqbuf, |
| 794 | |
| 795 | .vidioc_streamon = fimc_cap_streamon, |
| 796 | .vidioc_streamoff = fimc_cap_streamoff, |
| 797 | |
| 798 | .vidioc_queryctrl = fimc_vidioc_queryctrl, |
| 799 | .vidioc_g_ctrl = fimc_vidioc_g_ctrl, |
| 800 | .vidioc_s_ctrl = fimc_cap_s_ctrl, |
| 801 | |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 802 | .vidioc_g_crop = fimc_cap_g_crop, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 803 | .vidioc_s_crop = fimc_cap_s_crop, |
Sylwester Nawrocki | e004e02 | 2010-11-25 11:01:51 -0300 | [diff] [blame] | 804 | .vidioc_cropcap = fimc_cap_cropcap, |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 805 | |
| 806 | .vidioc_enum_input = fimc_cap_enum_input, |
| 807 | .vidioc_s_input = fimc_cap_s_input, |
| 808 | .vidioc_g_input = fimc_cap_g_input, |
| 809 | }; |
| 810 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 811 | /* fimc->lock must be already initialized */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 812 | int fimc_register_capture_device(struct fimc_dev *fimc) |
| 813 | { |
| 814 | struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev; |
| 815 | struct video_device *vfd; |
| 816 | struct fimc_vid_cap *vid_cap; |
| 817 | struct fimc_ctx *ctx; |
| 818 | struct v4l2_format f; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 819 | struct fimc_frame *fr; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 820 | struct vb2_queue *q; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 821 | int ret; |
| 822 | |
| 823 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); |
| 824 | if (!ctx) |
| 825 | return -ENOMEM; |
| 826 | |
| 827 | ctx->fimc_dev = fimc; |
| 828 | ctx->in_path = FIMC_CAMERA; |
| 829 | ctx->out_path = FIMC_DMA; |
| 830 | ctx->state = FIMC_CTX_CAP; |
| 831 | |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 832 | /* Default format of the output frames */ |
| 833 | f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32; |
| 834 | fr = &ctx->d_frame; |
| 835 | fr->fmt = find_format(&f, FMT_FLAGS_M2M); |
| 836 | fr->width = fr->f_width = fr->o_width = 640; |
| 837 | fr->height = fr->f_height = fr->o_height = 480; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 838 | |
| 839 | if (!v4l2_dev->name[0]) |
| 840 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), |
| 841 | "%s.capture", dev_name(&fimc->pdev->dev)); |
| 842 | |
| 843 | ret = v4l2_device_register(NULL, v4l2_dev); |
| 844 | if (ret) |
| 845 | goto err_info; |
| 846 | |
| 847 | vfd = video_device_alloc(); |
| 848 | if (!vfd) { |
| 849 | v4l2_err(v4l2_dev, "Failed to allocate video device\n"); |
| 850 | goto err_v4l2_reg; |
| 851 | } |
| 852 | |
| 853 | snprintf(vfd->name, sizeof(vfd->name), "%s:cap", |
| 854 | dev_name(&fimc->pdev->dev)); |
| 855 | |
| 856 | vfd->fops = &fimc_capture_fops; |
| 857 | vfd->ioctl_ops = &fimc_capture_ioctl_ops; |
| 858 | vfd->minor = -1; |
| 859 | vfd->release = video_device_release; |
Sylwester Nawrocki | 8293ebf | 2010-12-01 10:25:18 -0300 | [diff] [blame] | 860 | vfd->lock = &fimc->lock; |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 861 | video_set_drvdata(vfd, fimc); |
| 862 | |
| 863 | vid_cap = &fimc->vid_cap; |
| 864 | vid_cap->vfd = vfd; |
| 865 | vid_cap->active_buf_cnt = 0; |
| 866 | vid_cap->reqbufs_count = 0; |
| 867 | vid_cap->refcnt = 0; |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 868 | /* Default color format for image sensor */ |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 869 | vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8; |
| 870 | |
| 871 | INIT_LIST_HEAD(&vid_cap->pending_buf_q); |
| 872 | INIT_LIST_HEAD(&vid_cap->active_buf_q); |
| 873 | spin_lock_init(&ctx->slock); |
| 874 | vid_cap->ctx = ctx; |
| 875 | |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 876 | q = &fimc->vid_cap.vbq; |
| 877 | memset(q, 0, sizeof(*q)); |
Sylwester Nawrocki | ef7af59 | 2010-12-08 14:05:08 -0300 | [diff] [blame] | 878 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
Sylwester Nawrocki | 2dab38e | 2010-12-01 10:14:59 -0300 | [diff] [blame] | 879 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 880 | q->drv_priv = fimc->vid_cap.ctx; |
| 881 | q->ops = &fimc_capture_qops; |
| 882 | q->mem_ops = &vb2_dma_contig_memops; |
| 883 | q->buf_struct_size = sizeof(struct fimc_vid_buffer); |
| 884 | |
| 885 | vb2_queue_init(q); |
Sylwester Nawrocki | 5f3cc44 | 2010-10-07 10:06:16 -0300 | [diff] [blame] | 886 | |
| 887 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1); |
| 888 | if (ret) { |
| 889 | v4l2_err(v4l2_dev, "Failed to register video device\n"); |
| 890 | goto err_vd_reg; |
| 891 | } |
| 892 | |
| 893 | v4l2_info(v4l2_dev, |
| 894 | "FIMC capture driver registered as /dev/video%d\n", |
| 895 | vfd->num); |
| 896 | |
| 897 | return 0; |
| 898 | |
| 899 | err_vd_reg: |
| 900 | video_device_release(vfd); |
| 901 | err_v4l2_reg: |
| 902 | v4l2_device_unregister(v4l2_dev); |
| 903 | err_info: |
| 904 | dev_err(&fimc->pdev->dev, "failed to install\n"); |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | void fimc_unregister_capture_device(struct fimc_dev *fimc) |
| 909 | { |
| 910 | struct fimc_vid_cap *capture = &fimc->vid_cap; |
| 911 | |
| 912 | if (capture->vfd) |
| 913 | video_unregister_device(capture->vfd); |
| 914 | |
| 915 | kfree(capture->ctx); |
| 916 | } |