Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Samsung EXYNOS FIMC-LITE (camera host interface) driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics Co., Ltd. |
| 5 | * 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 | #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__ |
| 12 | |
| 13 | #include <linux/bug.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/pm_runtime.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/videodev2.h> |
| 25 | |
| 26 | #include <media/v4l2-device.h> |
| 27 | #include <media/v4l2-ioctl.h> |
| 28 | #include <media/v4l2-mem2mem.h> |
| 29 | #include <media/videobuf2-core.h> |
| 30 | #include <media/videobuf2-dma-contig.h> |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 31 | #include <media/s5p_fimc.h> |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 32 | |
| 33 | #include "fimc-mdevice.h" |
| 34 | #include "fimc-core.h" |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 35 | #include "fimc-lite.h" |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 36 | #include "fimc-lite-reg.h" |
| 37 | |
| 38 | static int debug; |
| 39 | module_param(debug, int, 0644); |
| 40 | |
| 41 | static const struct fimc_fmt fimc_lite_formats[] = { |
| 42 | { |
| 43 | .name = "YUV 4:2:2 packed, YCbYCr", |
| 44 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 45 | .depth = { 16 }, |
| 46 | .color = FIMC_FMT_YCBYCR422, |
| 47 | .memplanes = 1, |
| 48 | .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8, |
| 49 | }, { |
| 50 | .name = "YUV 4:2:2 packed, CbYCrY", |
| 51 | .fourcc = V4L2_PIX_FMT_UYVY, |
| 52 | .depth = { 16 }, |
| 53 | .color = FIMC_FMT_CBYCRY422, |
| 54 | .memplanes = 1, |
| 55 | .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8, |
| 56 | }, { |
| 57 | .name = "YUV 4:2:2 packed, CrYCbY", |
| 58 | .fourcc = V4L2_PIX_FMT_VYUY, |
| 59 | .depth = { 16 }, |
| 60 | .color = FIMC_FMT_CRYCBY422, |
| 61 | .memplanes = 1, |
| 62 | .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8, |
| 63 | }, { |
| 64 | .name = "YUV 4:2:2 packed, YCrYCb", |
| 65 | .fourcc = V4L2_PIX_FMT_YVYU, |
| 66 | .depth = { 16 }, |
| 67 | .color = FIMC_FMT_YCRYCB422, |
| 68 | .memplanes = 1, |
| 69 | .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8, |
| 70 | }, { |
| 71 | .name = "RAW8 (GRBG)", |
| 72 | .fourcc = V4L2_PIX_FMT_SGRBG8, |
| 73 | .depth = { 8 }, |
| 74 | .color = FIMC_FMT_RAW8, |
| 75 | .memplanes = 1, |
| 76 | .mbus_code = V4L2_MBUS_FMT_SGRBG8_1X8, |
| 77 | }, { |
| 78 | .name = "RAW10 (GRBG)", |
| 79 | .fourcc = V4L2_PIX_FMT_SGRBG10, |
| 80 | .depth = { 10 }, |
| 81 | .color = FIMC_FMT_RAW10, |
| 82 | .memplanes = 1, |
| 83 | .mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10, |
| 84 | }, { |
| 85 | .name = "RAW12 (GRBG)", |
| 86 | .fourcc = V4L2_PIX_FMT_SGRBG12, |
| 87 | .depth = { 12 }, |
| 88 | .color = FIMC_FMT_RAW12, |
| 89 | .memplanes = 1, |
| 90 | .mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12, |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code |
| 96 | * @pixelformat: fourcc to match, ignored if null |
| 97 | * @mbus_code: media bus code to match, ignored if null |
| 98 | * @index: index to the fimc_lite_formats array, ignored if negative |
| 99 | */ |
| 100 | static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat, |
| 101 | const u32 *mbus_code, int index) |
| 102 | { |
| 103 | const struct fimc_fmt *fmt, *def_fmt = NULL; |
| 104 | unsigned int i; |
| 105 | int id = 0; |
| 106 | |
| 107 | if (index >= (int)ARRAY_SIZE(fimc_lite_formats)) |
| 108 | return NULL; |
| 109 | |
| 110 | for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) { |
| 111 | fmt = &fimc_lite_formats[i]; |
| 112 | if (pixelformat && fmt->fourcc == *pixelformat) |
| 113 | return fmt; |
| 114 | if (mbus_code && fmt->mbus_code == *mbus_code) |
| 115 | return fmt; |
| 116 | if (index == id) |
| 117 | def_fmt = fmt; |
| 118 | id++; |
| 119 | } |
| 120 | return def_fmt; |
| 121 | } |
| 122 | |
| 123 | static int fimc_lite_hw_init(struct fimc_lite *fimc) |
| 124 | { |
| 125 | struct fimc_pipeline *pipeline = &fimc->pipeline; |
| 126 | struct fimc_sensor_info *sensor; |
| 127 | unsigned long flags; |
| 128 | |
| 129 | if (pipeline->subdevs[IDX_SENSOR] == NULL) |
| 130 | return -ENXIO; |
| 131 | |
| 132 | if (fimc->fmt == NULL) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | sensor = v4l2_get_subdev_hostdata(pipeline->subdevs[IDX_SENSOR]); |
| 136 | spin_lock_irqsave(&fimc->slock, flags); |
| 137 | |
| 138 | flite_hw_set_camera_bus(fimc, sensor->pdata); |
| 139 | flite_hw_set_source_format(fimc, &fimc->inp_frame); |
| 140 | flite_hw_set_window_offset(fimc, &fimc->inp_frame); |
| 141 | flite_hw_set_output_dma(fimc, &fimc->out_frame, true); |
| 142 | flite_hw_set_interrupt_mask(fimc); |
| 143 | flite_hw_set_test_pattern(fimc, fimc->test_pattern->val); |
| 144 | |
| 145 | if (debug > 0) |
| 146 | flite_hw_dump_regs(fimc, __func__); |
| 147 | |
| 148 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Reinitialize the driver so it is ready to start the streaming again. |
| 154 | * Set fimc->state to indicate stream off and the hardware shut down state. |
| 155 | * If not suspending (@suspend is false), return any buffers to videobuf2. |
| 156 | * Otherwise put any owned buffers onto the pending buffers queue, so they |
| 157 | * can be re-spun when the device is being resumed. Also perform FIMC |
| 158 | * software reset and disable streaming on the whole pipeline if required. |
| 159 | */ |
| 160 | static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend) |
| 161 | { |
| 162 | struct flite_buffer *buf; |
| 163 | unsigned long flags; |
| 164 | bool streaming; |
| 165 | |
| 166 | spin_lock_irqsave(&fimc->slock, flags); |
| 167 | streaming = fimc->state & (1 << ST_SENSOR_STREAM); |
| 168 | |
| 169 | fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF | |
| 170 | 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM); |
| 171 | if (suspend) |
| 172 | fimc->state |= (1 << ST_FLITE_SUSPENDED); |
| 173 | else |
| 174 | fimc->state &= ~(1 << ST_FLITE_PENDING | |
| 175 | 1 << ST_FLITE_SUSPENDED); |
| 176 | |
| 177 | /* Release unused buffers */ |
| 178 | while (!suspend && !list_empty(&fimc->pending_buf_q)) { |
| 179 | buf = fimc_lite_pending_queue_pop(fimc); |
| 180 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 181 | } |
| 182 | /* If suspending put unused buffers onto pending queue */ |
| 183 | while (!list_empty(&fimc->active_buf_q)) { |
| 184 | buf = fimc_lite_active_queue_pop(fimc); |
| 185 | if (suspend) |
| 186 | fimc_lite_pending_queue_add(fimc, buf); |
| 187 | else |
| 188 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 189 | } |
| 190 | |
| 191 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 192 | |
| 193 | flite_hw_reset(fimc); |
| 194 | |
| 195 | if (!streaming) |
| 196 | return 0; |
| 197 | |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 198 | return fimc_pipeline_call(fimc, set_stream, &fimc->pipeline, 0); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend) |
| 202 | { |
| 203 | unsigned long flags; |
| 204 | |
| 205 | if (!fimc_lite_active(fimc)) |
| 206 | return 0; |
| 207 | |
| 208 | spin_lock_irqsave(&fimc->slock, flags); |
| 209 | set_bit(ST_FLITE_OFF, &fimc->state); |
| 210 | flite_hw_capture_stop(fimc); |
| 211 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 212 | |
| 213 | wait_event_timeout(fimc->irq_queue, |
| 214 | !test_bit(ST_FLITE_OFF, &fimc->state), |
| 215 | (2*HZ/10)); /* 200 ms */ |
| 216 | |
| 217 | return fimc_lite_reinit(fimc, suspend); |
| 218 | } |
| 219 | |
| 220 | /* Must be called with fimc.slock spinlock held. */ |
| 221 | static void fimc_lite_config_update(struct fimc_lite *fimc) |
| 222 | { |
| 223 | flite_hw_set_window_offset(fimc, &fimc->inp_frame); |
| 224 | flite_hw_set_dma_window(fimc, &fimc->out_frame); |
| 225 | flite_hw_set_test_pattern(fimc, fimc->test_pattern->val); |
| 226 | clear_bit(ST_FLITE_CONFIG, &fimc->state); |
| 227 | } |
| 228 | |
| 229 | static irqreturn_t flite_irq_handler(int irq, void *priv) |
| 230 | { |
| 231 | struct fimc_lite *fimc = priv; |
| 232 | struct flite_buffer *vbuf; |
| 233 | unsigned long flags; |
| 234 | struct timeval *tv; |
| 235 | struct timespec ts; |
| 236 | u32 intsrc; |
| 237 | |
| 238 | spin_lock_irqsave(&fimc->slock, flags); |
| 239 | |
| 240 | intsrc = flite_hw_get_interrupt_source(fimc); |
| 241 | flite_hw_clear_pending_irq(fimc); |
| 242 | |
| 243 | if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) { |
| 244 | wake_up(&fimc->irq_queue); |
| 245 | goto done; |
| 246 | } |
| 247 | |
| 248 | if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) { |
| 249 | clear_bit(ST_FLITE_RUN, &fimc->state); |
| 250 | fimc->events.data_overflow++; |
| 251 | } |
| 252 | |
| 253 | if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) { |
| 254 | flite_hw_clear_last_capture_end(fimc); |
| 255 | clear_bit(ST_FLITE_STREAM, &fimc->state); |
| 256 | wake_up(&fimc->irq_queue); |
| 257 | } |
| 258 | |
| 259 | if (fimc->out_path != FIMC_IO_DMA) |
| 260 | goto done; |
| 261 | |
| 262 | if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) && |
| 263 | test_bit(ST_FLITE_RUN, &fimc->state) && |
| 264 | !list_empty(&fimc->active_buf_q) && |
| 265 | !list_empty(&fimc->pending_buf_q)) { |
| 266 | vbuf = fimc_lite_active_queue_pop(fimc); |
| 267 | ktime_get_ts(&ts); |
| 268 | tv = &vbuf->vb.v4l2_buf.timestamp; |
| 269 | tv->tv_sec = ts.tv_sec; |
| 270 | tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 271 | vbuf->vb.v4l2_buf.sequence = fimc->frame_count++; |
| 272 | vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE); |
| 273 | |
| 274 | vbuf = fimc_lite_pending_queue_pop(fimc); |
| 275 | flite_hw_set_output_addr(fimc, vbuf->paddr); |
| 276 | fimc_lite_active_queue_add(fimc, vbuf); |
| 277 | } |
| 278 | |
| 279 | if (test_bit(ST_FLITE_CONFIG, &fimc->state)) |
| 280 | fimc_lite_config_update(fimc); |
| 281 | |
| 282 | if (list_empty(&fimc->pending_buf_q)) { |
| 283 | flite_hw_capture_stop(fimc); |
| 284 | clear_bit(ST_FLITE_STREAM, &fimc->state); |
| 285 | } |
| 286 | done: |
| 287 | set_bit(ST_FLITE_RUN, &fimc->state); |
| 288 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 289 | return IRQ_HANDLED; |
| 290 | } |
| 291 | |
| 292 | static int start_streaming(struct vb2_queue *q, unsigned int count) |
| 293 | { |
| 294 | struct fimc_lite *fimc = q->drv_priv; |
| 295 | int ret; |
| 296 | |
| 297 | fimc->frame_count = 0; |
| 298 | |
| 299 | ret = fimc_lite_hw_init(fimc); |
| 300 | if (ret) { |
| 301 | fimc_lite_reinit(fimc, false); |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | set_bit(ST_FLITE_PENDING, &fimc->state); |
| 306 | |
| 307 | if (!list_empty(&fimc->active_buf_q) && |
| 308 | !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) { |
| 309 | flite_hw_capture_start(fimc); |
| 310 | |
| 311 | if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state)) |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 312 | fimc_pipeline_call(fimc, set_stream, |
| 313 | &fimc->pipeline, 1); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 314 | } |
| 315 | if (debug > 0) |
| 316 | flite_hw_dump_regs(fimc, __func__); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int stop_streaming(struct vb2_queue *q) |
| 322 | { |
| 323 | struct fimc_lite *fimc = q->drv_priv; |
| 324 | |
| 325 | if (!fimc_lite_active(fimc)) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | return fimc_lite_stop_capture(fimc, false); |
| 329 | } |
| 330 | |
| 331 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt, |
| 332 | unsigned int *num_buffers, unsigned int *num_planes, |
| 333 | unsigned int sizes[], void *allocators[]) |
| 334 | { |
| 335 | const struct v4l2_pix_format_mplane *pixm = NULL; |
| 336 | struct fimc_lite *fimc = vq->drv_priv; |
| 337 | struct flite_frame *frame = &fimc->out_frame; |
| 338 | const struct fimc_fmt *fmt = fimc->fmt; |
| 339 | unsigned long wh; |
| 340 | int i; |
| 341 | |
| 342 | if (pfmt) { |
| 343 | pixm = &pfmt->fmt.pix_mp; |
| 344 | fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, -1); |
| 345 | wh = pixm->width * pixm->height; |
| 346 | } else { |
| 347 | wh = frame->f_width * frame->f_height; |
| 348 | } |
| 349 | |
| 350 | if (fmt == NULL) |
| 351 | return -EINVAL; |
| 352 | |
| 353 | *num_planes = fmt->memplanes; |
| 354 | |
| 355 | for (i = 0; i < fmt->memplanes; i++) { |
| 356 | unsigned int size = (wh * fmt->depth[i]) / 8; |
| 357 | if (pixm) |
| 358 | sizes[i] = max(size, pixm->plane_fmt[i].sizeimage); |
| 359 | else |
| 360 | sizes[i] = size; |
| 361 | allocators[i] = fimc->alloc_ctx; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int buffer_prepare(struct vb2_buffer *vb) |
| 368 | { |
| 369 | struct vb2_queue *vq = vb->vb2_queue; |
| 370 | struct fimc_lite *fimc = vq->drv_priv; |
| 371 | int i; |
| 372 | |
| 373 | if (fimc->fmt == NULL) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | for (i = 0; i < fimc->fmt->memplanes; i++) { |
| 377 | unsigned long size = fimc->payload[i]; |
| 378 | |
| 379 | if (vb2_plane_size(vb, i) < size) { |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 380 | v4l2_err(&fimc->vfd, |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 381 | "User buffer too small (%ld < %ld)\n", |
| 382 | vb2_plane_size(vb, i), size); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | vb2_set_plane_payload(vb, i, size); |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static void buffer_queue(struct vb2_buffer *vb) |
| 392 | { |
| 393 | struct flite_buffer *buf |
| 394 | = container_of(vb, struct flite_buffer, vb); |
| 395 | struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue); |
| 396 | unsigned long flags; |
| 397 | |
| 398 | spin_lock_irqsave(&fimc->slock, flags); |
| 399 | buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0); |
| 400 | |
| 401 | if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) && |
| 402 | !test_bit(ST_FLITE_STREAM, &fimc->state) && |
| 403 | list_empty(&fimc->active_buf_q)) { |
| 404 | flite_hw_set_output_addr(fimc, buf->paddr); |
| 405 | fimc_lite_active_queue_add(fimc, buf); |
| 406 | } else { |
| 407 | fimc_lite_pending_queue_add(fimc, buf); |
| 408 | } |
| 409 | |
| 410 | if (vb2_is_streaming(&fimc->vb_queue) && |
| 411 | !list_empty(&fimc->pending_buf_q) && |
| 412 | !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) { |
| 413 | flite_hw_capture_start(fimc); |
| 414 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 415 | |
| 416 | if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state)) |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 417 | fimc_pipeline_call(fimc, set_stream, |
| 418 | &fimc->pipeline, 1); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 422 | } |
| 423 | |
| 424 | static void fimc_lock(struct vb2_queue *vq) |
| 425 | { |
| 426 | struct fimc_lite *fimc = vb2_get_drv_priv(vq); |
| 427 | mutex_lock(&fimc->lock); |
| 428 | } |
| 429 | |
| 430 | static void fimc_unlock(struct vb2_queue *vq) |
| 431 | { |
| 432 | struct fimc_lite *fimc = vb2_get_drv_priv(vq); |
| 433 | mutex_unlock(&fimc->lock); |
| 434 | } |
| 435 | |
| 436 | static const struct vb2_ops fimc_lite_qops = { |
| 437 | .queue_setup = queue_setup, |
| 438 | .buf_prepare = buffer_prepare, |
| 439 | .buf_queue = buffer_queue, |
| 440 | .wait_prepare = fimc_unlock, |
| 441 | .wait_finish = fimc_lock, |
| 442 | .start_streaming = start_streaming, |
| 443 | .stop_streaming = stop_streaming, |
| 444 | }; |
| 445 | |
| 446 | static void fimc_lite_clear_event_counters(struct fimc_lite *fimc) |
| 447 | { |
| 448 | unsigned long flags; |
| 449 | |
| 450 | spin_lock_irqsave(&fimc->slock, flags); |
| 451 | memset(&fimc->events, 0, sizeof(fimc->events)); |
| 452 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 453 | } |
| 454 | |
| 455 | static int fimc_lite_open(struct file *file) |
| 456 | { |
| 457 | struct fimc_lite *fimc = video_drvdata(file); |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 458 | int ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 459 | |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 460 | if (mutex_lock_interruptible(&fimc->lock)) |
| 461 | return -ERESTARTSYS; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 462 | |
| 463 | set_bit(ST_FLITE_IN_USE, &fimc->state); |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 464 | ret = pm_runtime_get_sync(&fimc->pdev->dev); |
| 465 | if (ret < 0) |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 466 | goto done; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 467 | |
Sylwester Nawrocki | e3fc82e | 2012-05-17 14:22:10 -0300 | [diff] [blame] | 468 | ret = v4l2_fh_open(file); |
| 469 | if (ret < 0) |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 470 | goto done; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 471 | |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 472 | if (++fimc->ref_count == 1 && fimc->out_path == FIMC_IO_DMA) { |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 473 | ret = fimc_pipeline_call(fimc, open, &fimc->pipeline, |
| 474 | &fimc->vfd.entity, true); |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 475 | if (ret < 0) { |
| 476 | pm_runtime_put_sync(&fimc->pdev->dev); |
| 477 | fimc->ref_count--; |
| 478 | v4l2_fh_release(file); |
| 479 | clear_bit(ST_FLITE_IN_USE, &fimc->state); |
| 480 | } |
| 481 | |
| 482 | fimc_lite_clear_event_counters(fimc); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 483 | } |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 484 | done: |
| 485 | mutex_unlock(&fimc->lock); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static int fimc_lite_close(struct file *file) |
| 490 | { |
| 491 | struct fimc_lite *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 492 | int ret; |
| 493 | |
| 494 | if (mutex_lock_interruptible(&fimc->lock)) |
| 495 | return -ERESTARTSYS; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 496 | |
| 497 | if (--fimc->ref_count == 0 && fimc->out_path == FIMC_IO_DMA) { |
| 498 | clear_bit(ST_FLITE_IN_USE, &fimc->state); |
| 499 | fimc_lite_stop_capture(fimc, false); |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 500 | fimc_pipeline_call(fimc, close, &fimc->pipeline); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 501 | clear_bit(ST_FLITE_SUSPENDED, &fimc->state); |
| 502 | } |
| 503 | |
| 504 | pm_runtime_put(&fimc->pdev->dev); |
| 505 | |
| 506 | if (fimc->ref_count == 0) |
| 507 | vb2_queue_release(&fimc->vb_queue); |
| 508 | |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 509 | ret = v4l2_fh_release(file); |
| 510 | |
| 511 | mutex_unlock(&fimc->lock); |
| 512 | return ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static unsigned int fimc_lite_poll(struct file *file, |
| 516 | struct poll_table_struct *wait) |
| 517 | { |
| 518 | struct fimc_lite *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 519 | int ret; |
| 520 | |
| 521 | if (mutex_lock_interruptible(&fimc->lock)) |
| 522 | return POLL_ERR; |
| 523 | |
| 524 | ret = vb2_poll(&fimc->vb_queue, file, wait); |
| 525 | mutex_unlock(&fimc->lock); |
| 526 | |
| 527 | return ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static int fimc_lite_mmap(struct file *file, struct vm_area_struct *vma) |
| 531 | { |
| 532 | struct fimc_lite *fimc = video_drvdata(file); |
Sylwester Nawrocki | 4e39da0 | 2012-06-04 13:15:56 -0300 | [diff] [blame] | 533 | int ret; |
| 534 | |
| 535 | if (mutex_lock_interruptible(&fimc->lock)) |
| 536 | return -ERESTARTSYS; |
| 537 | |
| 538 | ret = vb2_mmap(&fimc->vb_queue, vma); |
| 539 | mutex_unlock(&fimc->lock); |
| 540 | |
| 541 | return ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static const struct v4l2_file_operations fimc_lite_fops = { |
| 545 | .owner = THIS_MODULE, |
| 546 | .open = fimc_lite_open, |
| 547 | .release = fimc_lite_close, |
| 548 | .poll = fimc_lite_poll, |
| 549 | .unlocked_ioctl = video_ioctl2, |
| 550 | .mmap = fimc_lite_mmap, |
| 551 | }; |
| 552 | |
| 553 | /* |
| 554 | * Format and crop negotiation helpers |
| 555 | */ |
| 556 | |
| 557 | static const struct fimc_fmt *fimc_lite_try_format(struct fimc_lite *fimc, |
| 558 | u32 *width, u32 *height, |
| 559 | u32 *code, u32 *fourcc, int pad) |
| 560 | { |
| 561 | struct flite_variant *variant = fimc->variant; |
| 562 | const struct fimc_fmt *fmt; |
| 563 | |
| 564 | fmt = fimc_lite_find_format(fourcc, code, 0); |
| 565 | if (WARN_ON(!fmt)) |
| 566 | return NULL; |
| 567 | |
| 568 | if (code) |
| 569 | *code = fmt->mbus_code; |
| 570 | if (fourcc) |
| 571 | *fourcc = fmt->fourcc; |
| 572 | |
| 573 | if (pad == FLITE_SD_PAD_SINK) { |
| 574 | v4l_bound_align_image(width, 8, variant->max_width, |
| 575 | ffs(variant->out_width_align) - 1, |
| 576 | height, 0, variant->max_height, 0, 0); |
| 577 | } else { |
| 578 | v4l_bound_align_image(width, 8, fimc->inp_frame.rect.width, |
| 579 | ffs(variant->out_width_align) - 1, |
| 580 | height, 0, fimc->inp_frame.rect.height, |
| 581 | 0, 0); |
| 582 | } |
| 583 | |
| 584 | v4l2_dbg(1, debug, &fimc->subdev, "code: 0x%x, %dx%d\n", |
| 585 | code ? *code : 0, *width, *height); |
| 586 | |
| 587 | return fmt; |
| 588 | } |
| 589 | |
| 590 | static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r) |
| 591 | { |
| 592 | struct flite_frame *frame = &fimc->inp_frame; |
| 593 | |
| 594 | v4l_bound_align_image(&r->width, 0, frame->f_width, 0, |
| 595 | &r->height, 0, frame->f_height, 0, 0); |
| 596 | |
| 597 | /* Adjust left/top if cropping rectangle got out of bounds */ |
| 598 | r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width); |
| 599 | r->left = round_down(r->left, fimc->variant->win_hor_offs_align); |
| 600 | r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height); |
| 601 | |
| 602 | v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d", |
| 603 | r->left, r->top, r->width, r->height, |
| 604 | frame->f_width, frame->f_height); |
| 605 | } |
| 606 | |
| 607 | static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r) |
| 608 | { |
| 609 | struct flite_frame *frame = &fimc->out_frame; |
| 610 | struct v4l2_rect *crop_rect = &fimc->inp_frame.rect; |
| 611 | |
| 612 | /* Scaling is not supported so we enforce compose rectangle size |
| 613 | same as size of the sink crop rectangle. */ |
| 614 | r->width = crop_rect->width; |
| 615 | r->height = crop_rect->height; |
| 616 | |
| 617 | /* Adjust left/top if the composing rectangle got out of bounds */ |
| 618 | r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width); |
| 619 | r->left = round_down(r->left, fimc->variant->out_hor_offs_align); |
| 620 | r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height); |
| 621 | |
| 622 | v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d", |
| 623 | r->left, r->top, r->width, r->height, |
| 624 | frame->f_width, frame->f_height); |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Video node ioctl operations |
| 629 | */ |
| 630 | static int fimc_vidioc_querycap_capture(struct file *file, void *priv, |
| 631 | struct v4l2_capability *cap) |
| 632 | { |
| 633 | strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver)); |
| 634 | cap->bus_info[0] = 0; |
| 635 | cap->card[0] = 0; |
| 636 | cap->capabilities = V4L2_CAP_STREAMING; |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv, |
| 641 | struct v4l2_fmtdesc *f) |
| 642 | { |
| 643 | const struct fimc_fmt *fmt; |
| 644 | |
| 645 | if (f->index >= ARRAY_SIZE(fimc_lite_formats)) |
| 646 | return -EINVAL; |
| 647 | |
| 648 | fmt = &fimc_lite_formats[f->index]; |
| 649 | strlcpy(f->description, fmt->name, sizeof(f->description)); |
| 650 | f->pixelformat = fmt->fourcc; |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int fimc_lite_g_fmt_mplane(struct file *file, void *fh, |
| 656 | struct v4l2_format *f) |
| 657 | { |
| 658 | struct fimc_lite *fimc = video_drvdata(file); |
| 659 | struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp; |
| 660 | struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0]; |
| 661 | struct flite_frame *frame = &fimc->out_frame; |
| 662 | const struct fimc_fmt *fmt = fimc->fmt; |
| 663 | |
| 664 | plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8; |
| 665 | plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height; |
| 666 | |
| 667 | pixm->num_planes = fmt->memplanes; |
| 668 | pixm->pixelformat = fmt->fourcc; |
| 669 | pixm->width = frame->f_width; |
| 670 | pixm->height = frame->f_height; |
| 671 | pixm->field = V4L2_FIELD_NONE; |
| 672 | pixm->colorspace = V4L2_COLORSPACE_JPEG; |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | static int fimc_lite_try_fmt(struct fimc_lite *fimc, |
| 677 | struct v4l2_pix_format_mplane *pixm, |
| 678 | const struct fimc_fmt **ffmt) |
| 679 | { |
| 680 | struct flite_variant *variant = fimc->variant; |
| 681 | u32 bpl = pixm->plane_fmt[0].bytesperline; |
| 682 | const struct fimc_fmt *fmt; |
| 683 | |
| 684 | fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 0); |
| 685 | if (WARN_ON(fmt == NULL)) |
| 686 | return -EINVAL; |
| 687 | if (ffmt) |
| 688 | *ffmt = fmt; |
| 689 | v4l_bound_align_image(&pixm->width, 8, variant->max_width, |
| 690 | ffs(variant->out_width_align) - 1, |
| 691 | &pixm->height, 0, variant->max_height, 0, 0); |
| 692 | |
| 693 | if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width)) |
| 694 | pixm->plane_fmt[0].bytesperline = (pixm->width * |
| 695 | fmt->depth[0]) / 8; |
| 696 | |
| 697 | if (pixm->plane_fmt[0].sizeimage == 0) |
| 698 | pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height * |
| 699 | fmt->depth[0]) / 8; |
| 700 | pixm->num_planes = fmt->memplanes; |
| 701 | pixm->pixelformat = fmt->fourcc; |
| 702 | pixm->colorspace = V4L2_COLORSPACE_JPEG; |
| 703 | pixm->field = V4L2_FIELD_NONE; |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int fimc_lite_try_fmt_mplane(struct file *file, void *fh, |
| 708 | struct v4l2_format *f) |
| 709 | { |
| 710 | struct fimc_lite *fimc = video_drvdata(file); |
| 711 | |
| 712 | return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL); |
| 713 | } |
| 714 | |
| 715 | static int fimc_lite_s_fmt_mplane(struct file *file, void *priv, |
| 716 | struct v4l2_format *f) |
| 717 | { |
| 718 | struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp; |
| 719 | struct fimc_lite *fimc = video_drvdata(file); |
| 720 | struct flite_frame *frame = &fimc->out_frame; |
| 721 | const struct fimc_fmt *fmt = NULL; |
| 722 | int ret; |
| 723 | |
| 724 | if (vb2_is_busy(&fimc->vb_queue)) |
| 725 | return -EBUSY; |
| 726 | |
| 727 | ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt); |
| 728 | if (ret < 0) |
| 729 | return ret; |
| 730 | |
| 731 | fimc->fmt = fmt; |
| 732 | fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8, |
| 733 | pixm->plane_fmt[0].sizeimage); |
| 734 | frame->f_width = pixm->width; |
| 735 | frame->f_height = pixm->height; |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int fimc_pipeline_validate(struct fimc_lite *fimc) |
| 741 | { |
| 742 | struct v4l2_subdev *sd = &fimc->subdev; |
| 743 | struct v4l2_subdev_format sink_fmt, src_fmt; |
| 744 | struct media_pad *pad; |
| 745 | int ret; |
| 746 | |
| 747 | while (1) { |
| 748 | /* Retrieve format at the sink pad */ |
| 749 | pad = &sd->entity.pads[0]; |
| 750 | if (!(pad->flags & MEDIA_PAD_FL_SINK)) |
| 751 | break; |
| 752 | /* Don't call FIMC subdev operation to avoid nested locking */ |
| 753 | if (sd == &fimc->subdev) { |
| 754 | struct flite_frame *ff = &fimc->out_frame; |
| 755 | sink_fmt.format.width = ff->f_width; |
| 756 | sink_fmt.format.height = ff->f_height; |
| 757 | sink_fmt.format.code = fimc->fmt->mbus_code; |
| 758 | } else { |
| 759 | sink_fmt.pad = pad->index; |
| 760 | sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 761 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, |
| 762 | &sink_fmt); |
| 763 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 764 | return -EPIPE; |
| 765 | } |
| 766 | /* Retrieve format at the source pad */ |
| 767 | pad = media_entity_remote_source(pad); |
| 768 | if (pad == NULL || |
| 769 | media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) |
| 770 | break; |
| 771 | |
| 772 | sd = media_entity_to_v4l2_subdev(pad->entity); |
| 773 | src_fmt.pad = pad->index; |
| 774 | src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; |
| 775 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt); |
| 776 | if (ret < 0 && ret != -ENOIOCTLCMD) |
| 777 | return -EPIPE; |
| 778 | |
| 779 | if (src_fmt.format.width != sink_fmt.format.width || |
| 780 | src_fmt.format.height != sink_fmt.format.height || |
| 781 | src_fmt.format.code != sink_fmt.format.code) |
| 782 | return -EPIPE; |
| 783 | } |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static int fimc_lite_streamon(struct file *file, void *priv, |
| 788 | enum v4l2_buf_type type) |
| 789 | { |
| 790 | struct fimc_lite *fimc = video_drvdata(file); |
| 791 | struct v4l2_subdev *sensor = fimc->pipeline.subdevs[IDX_SENSOR]; |
| 792 | struct fimc_pipeline *p = &fimc->pipeline; |
| 793 | int ret; |
| 794 | |
| 795 | if (fimc_lite_active(fimc)) |
| 796 | return -EBUSY; |
| 797 | |
Sachin Kamat | a1a5861 | 2012-05-25 03:29:38 -0300 | [diff] [blame] | 798 | ret = media_entity_pipeline_start(&sensor->entity, p->m_pipeline); |
| 799 | if (ret < 0) |
| 800 | return ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 801 | |
| 802 | ret = fimc_pipeline_validate(fimc); |
| 803 | if (ret) { |
| 804 | media_entity_pipeline_stop(&sensor->entity); |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | return vb2_streamon(&fimc->vb_queue, type); |
| 809 | } |
| 810 | |
| 811 | static int fimc_lite_streamoff(struct file *file, void *priv, |
| 812 | enum v4l2_buf_type type) |
| 813 | { |
| 814 | struct fimc_lite *fimc = video_drvdata(file); |
| 815 | struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR]; |
| 816 | int ret; |
| 817 | |
| 818 | ret = vb2_streamoff(&fimc->vb_queue, type); |
| 819 | if (ret == 0) |
| 820 | media_entity_pipeline_stop(&sd->entity); |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | static int fimc_lite_reqbufs(struct file *file, void *priv, |
| 825 | struct v4l2_requestbuffers *reqbufs) |
| 826 | { |
| 827 | struct fimc_lite *fimc = video_drvdata(file); |
| 828 | int ret; |
| 829 | |
| 830 | reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count); |
| 831 | ret = vb2_reqbufs(&fimc->vb_queue, reqbufs); |
| 832 | if (!ret < 0) |
| 833 | fimc->reqbufs_count = reqbufs->count; |
| 834 | |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | static int fimc_lite_querybuf(struct file *file, void *priv, |
| 839 | struct v4l2_buffer *buf) |
| 840 | { |
| 841 | struct fimc_lite *fimc = video_drvdata(file); |
| 842 | |
| 843 | return vb2_querybuf(&fimc->vb_queue, buf); |
| 844 | } |
| 845 | |
| 846 | static int fimc_lite_qbuf(struct file *file, void *priv, |
| 847 | struct v4l2_buffer *buf) |
| 848 | { |
| 849 | struct fimc_lite *fimc = video_drvdata(file); |
| 850 | |
| 851 | return vb2_qbuf(&fimc->vb_queue, buf); |
| 852 | } |
| 853 | |
| 854 | static int fimc_lite_dqbuf(struct file *file, void *priv, |
| 855 | struct v4l2_buffer *buf) |
| 856 | { |
| 857 | struct fimc_lite *fimc = video_drvdata(file); |
| 858 | |
| 859 | return vb2_dqbuf(&fimc->vb_queue, buf, file->f_flags & O_NONBLOCK); |
| 860 | } |
| 861 | |
| 862 | static int fimc_lite_create_bufs(struct file *file, void *priv, |
| 863 | struct v4l2_create_buffers *create) |
| 864 | { |
| 865 | struct fimc_lite *fimc = video_drvdata(file); |
| 866 | |
| 867 | return vb2_create_bufs(&fimc->vb_queue, create); |
| 868 | } |
| 869 | |
| 870 | static int fimc_lite_prepare_buf(struct file *file, void *priv, |
| 871 | struct v4l2_buffer *b) |
| 872 | { |
| 873 | struct fimc_lite *fimc = video_drvdata(file); |
| 874 | |
| 875 | return vb2_prepare_buf(&fimc->vb_queue, b); |
| 876 | } |
| 877 | |
| 878 | /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */ |
| 879 | static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b) |
| 880 | { |
| 881 | if (a->left < b->left || a->top < b->top) |
| 882 | return 0; |
| 883 | if (a->left + a->width > b->left + b->width) |
| 884 | return 0; |
| 885 | if (a->top + a->height > b->top + b->height) |
| 886 | return 0; |
| 887 | |
| 888 | return 1; |
| 889 | } |
| 890 | |
| 891 | static int fimc_lite_g_selection(struct file *file, void *fh, |
| 892 | struct v4l2_selection *sel) |
| 893 | { |
| 894 | struct fimc_lite *fimc = video_drvdata(file); |
| 895 | struct flite_frame *f = &fimc->out_frame; |
| 896 | |
| 897 | if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
| 898 | return -EINVAL; |
| 899 | |
| 900 | switch (sel->target) { |
| 901 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 902 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
| 903 | sel->r.left = 0; |
| 904 | sel->r.top = 0; |
| 905 | sel->r.width = f->f_width; |
| 906 | sel->r.height = f->f_height; |
| 907 | return 0; |
| 908 | |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 909 | case V4L2_SEL_TGT_COMPOSE: |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 910 | sel->r = f->rect; |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | static int fimc_lite_s_selection(struct file *file, void *fh, |
| 918 | struct v4l2_selection *sel) |
| 919 | { |
| 920 | struct fimc_lite *fimc = video_drvdata(file); |
| 921 | struct flite_frame *f = &fimc->out_frame; |
| 922 | struct v4l2_rect rect = sel->r; |
| 923 | unsigned long flags; |
| 924 | |
| 925 | if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || |
Sylwester Nawrocki | c133482 | 2012-05-20 11:17:12 -0300 | [diff] [blame] | 926 | sel->target != V4L2_SEL_TGT_COMPOSE) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 927 | return -EINVAL; |
| 928 | |
| 929 | fimc_lite_try_compose(fimc, &rect); |
| 930 | |
| 931 | if ((sel->flags & V4L2_SEL_FLAG_LE) && |
| 932 | !enclosed_rectangle(&rect, &sel->r)) |
| 933 | return -ERANGE; |
| 934 | |
| 935 | if ((sel->flags & V4L2_SEL_FLAG_GE) && |
| 936 | !enclosed_rectangle(&sel->r, &rect)) |
| 937 | return -ERANGE; |
| 938 | |
| 939 | sel->r = rect; |
| 940 | spin_lock_irqsave(&fimc->slock, flags); |
| 941 | f->rect = rect; |
| 942 | set_bit(ST_FLITE_CONFIG, &fimc->state); |
| 943 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = { |
| 949 | .vidioc_querycap = fimc_vidioc_querycap_capture, |
| 950 | .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane, |
| 951 | .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane, |
| 952 | .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane, |
| 953 | .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane, |
| 954 | .vidioc_g_selection = fimc_lite_g_selection, |
| 955 | .vidioc_s_selection = fimc_lite_s_selection, |
| 956 | .vidioc_reqbufs = fimc_lite_reqbufs, |
| 957 | .vidioc_querybuf = fimc_lite_querybuf, |
| 958 | .vidioc_prepare_buf = fimc_lite_prepare_buf, |
| 959 | .vidioc_create_bufs = fimc_lite_create_bufs, |
| 960 | .vidioc_qbuf = fimc_lite_qbuf, |
| 961 | .vidioc_dqbuf = fimc_lite_dqbuf, |
| 962 | .vidioc_streamon = fimc_lite_streamon, |
| 963 | .vidioc_streamoff = fimc_lite_streamoff, |
| 964 | }; |
| 965 | |
| 966 | /* Capture subdev media entity operations */ |
| 967 | static int fimc_lite_link_setup(struct media_entity *entity, |
| 968 | const struct media_pad *local, |
| 969 | const struct media_pad *remote, u32 flags) |
| 970 | { |
| 971 | struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); |
| 972 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 973 | unsigned int remote_ent_type = media_entity_type(remote->entity); |
| 974 | |
| 975 | if (WARN_ON(fimc == NULL)) |
| 976 | return 0; |
| 977 | |
| 978 | v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x", |
| 979 | __func__, local->entity->name, remote->entity->name, |
| 980 | flags, fimc->source_subdev_grp_id); |
| 981 | |
| 982 | switch (local->index) { |
| 983 | case FIMC_SD_PAD_SINK: |
| 984 | if (remote_ent_type != MEDIA_ENT_T_V4L2_SUBDEV) |
| 985 | return -EINVAL; |
| 986 | |
| 987 | if (flags & MEDIA_LNK_FL_ENABLED) { |
| 988 | if (fimc->source_subdev_grp_id != 0) |
| 989 | return -EBUSY; |
| 990 | fimc->source_subdev_grp_id = sd->grp_id; |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | fimc->source_subdev_grp_id = 0; |
| 995 | break; |
| 996 | |
| 997 | case FIMC_SD_PAD_SOURCE: |
| 998 | if (!(flags & MEDIA_LNK_FL_ENABLED)) { |
| 999 | fimc->out_path = FIMC_IO_NONE; |
| 1000 | return 0; |
| 1001 | } |
| 1002 | if (remote_ent_type == MEDIA_ENT_T_V4L2_SUBDEV) |
| 1003 | fimc->out_path = FIMC_IO_ISP; |
| 1004 | else |
| 1005 | fimc->out_path = FIMC_IO_DMA; |
| 1006 | break; |
| 1007 | |
| 1008 | default: |
| 1009 | v4l2_err(sd, "Invalid pad index\n"); |
| 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static const struct media_entity_operations fimc_lite_subdev_media_ops = { |
| 1017 | .link_setup = fimc_lite_link_setup, |
| 1018 | }; |
| 1019 | |
| 1020 | static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd, |
| 1021 | struct v4l2_subdev_fh *fh, |
| 1022 | struct v4l2_subdev_mbus_code_enum *code) |
| 1023 | { |
| 1024 | const struct fimc_fmt *fmt; |
| 1025 | |
| 1026 | fmt = fimc_lite_find_format(NULL, NULL, code->index); |
| 1027 | if (!fmt) |
| 1028 | return -EINVAL; |
| 1029 | code->code = fmt->mbus_code; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd, |
| 1034 | struct v4l2_subdev_fh *fh, |
| 1035 | struct v4l2_subdev_format *fmt) |
| 1036 | { |
| 1037 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1038 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
| 1039 | struct flite_frame *f = &fimc->out_frame; |
| 1040 | |
| 1041 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1042 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1043 | fmt->format = *mf; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1047 | |
| 1048 | mutex_lock(&fimc->lock); |
| 1049 | mf->code = fimc->fmt->mbus_code; |
| 1050 | |
| 1051 | if (fmt->pad == FLITE_SD_PAD_SINK) { |
| 1052 | /* full camera input frame size */ |
| 1053 | mf->width = f->f_width; |
| 1054 | mf->height = f->f_height; |
| 1055 | } else { |
| 1056 | /* crop size */ |
| 1057 | mf->width = f->rect.width; |
| 1058 | mf->height = f->rect.height; |
| 1059 | } |
| 1060 | mutex_unlock(&fimc->lock); |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd, |
| 1065 | struct v4l2_subdev_fh *fh, |
| 1066 | struct v4l2_subdev_format *fmt) |
| 1067 | { |
| 1068 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1069 | struct v4l2_mbus_framefmt *mf = &fmt->format; |
| 1070 | struct flite_frame *sink = &fimc->inp_frame; |
Sylwester Nawrocki | 9356ac7 | 2012-08-31 12:50:45 -0300 | [diff] [blame] | 1071 | struct flite_frame *source = &fimc->out_frame; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1072 | const struct fimc_fmt *ffmt; |
| 1073 | |
| 1074 | v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d", |
| 1075 | fmt->pad, mf->code, mf->width, mf->height); |
| 1076 | |
| 1077 | mf->colorspace = V4L2_COLORSPACE_JPEG; |
| 1078 | mutex_lock(&fimc->lock); |
| 1079 | |
| 1080 | if ((fimc->out_path == FIMC_IO_ISP && sd->entity.stream_count > 0) || |
| 1081 | (fimc->out_path == FIMC_IO_DMA && vb2_is_busy(&fimc->vb_queue))) { |
| 1082 | mutex_unlock(&fimc->lock); |
| 1083 | return -EBUSY; |
| 1084 | } |
| 1085 | |
| 1086 | ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height, |
| 1087 | &mf->code, NULL, fmt->pad); |
| 1088 | |
| 1089 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1090 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); |
| 1091 | *mf = fmt->format; |
| 1092 | mutex_unlock(&fimc->lock); |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | if (fmt->pad == FLITE_SD_PAD_SINK) { |
| 1097 | sink->f_width = mf->width; |
| 1098 | sink->f_height = mf->height; |
| 1099 | fimc->fmt = ffmt; |
| 1100 | /* Set sink crop rectangle */ |
| 1101 | sink->rect.width = mf->width; |
| 1102 | sink->rect.height = mf->height; |
| 1103 | sink->rect.left = 0; |
| 1104 | sink->rect.top = 0; |
Sylwester Nawrocki | 9356ac7 | 2012-08-31 12:50:45 -0300 | [diff] [blame] | 1105 | /* Reset source format and crop rectangle */ |
| 1106 | source->rect = sink->rect; |
| 1107 | source->f_width = mf->width; |
| 1108 | source->f_height = mf->height; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1109 | } else { |
| 1110 | /* Allow changing format only on sink pad */ |
| 1111 | mf->code = fimc->fmt->mbus_code; |
| 1112 | mf->width = sink->rect.width; |
| 1113 | mf->height = sink->rect.height; |
| 1114 | } |
| 1115 | |
| 1116 | mutex_unlock(&fimc->lock); |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd, |
| 1121 | struct v4l2_subdev_fh *fh, |
| 1122 | struct v4l2_subdev_selection *sel) |
| 1123 | { |
| 1124 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1125 | struct flite_frame *f = &fimc->inp_frame; |
| 1126 | |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1127 | if ((sel->target != V4L2_SEL_TGT_CROP && |
| 1128 | sel->target != V4L2_SEL_TGT_CROP_BOUNDS) || |
| 1129 | sel->pad != FLITE_SD_PAD_SINK) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1130 | return -EINVAL; |
| 1131 | |
| 1132 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1133 | sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad); |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | mutex_lock(&fimc->lock); |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1138 | if (sel->target == V4L2_SEL_TGT_CROP) { |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1139 | sel->r = f->rect; |
| 1140 | } else { |
| 1141 | sel->r.left = 0; |
| 1142 | sel->r.top = 0; |
| 1143 | sel->r.width = f->f_width; |
| 1144 | sel->r.height = f->f_height; |
| 1145 | } |
| 1146 | mutex_unlock(&fimc->lock); |
| 1147 | |
| 1148 | v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d", |
| 1149 | __func__, f->rect.left, f->rect.top, f->rect.width, |
| 1150 | f->rect.height, f->f_width, f->f_height); |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd, |
| 1156 | struct v4l2_subdev_fh *fh, |
| 1157 | struct v4l2_subdev_selection *sel) |
| 1158 | { |
| 1159 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1160 | struct flite_frame *f = &fimc->inp_frame; |
| 1161 | int ret = 0; |
| 1162 | |
Sakari Ailus | 5689b28 | 2012-05-18 09:31:18 -0300 | [diff] [blame] | 1163 | if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1164 | return -EINVAL; |
| 1165 | |
| 1166 | mutex_lock(&fimc->lock); |
| 1167 | fimc_lite_try_crop(fimc, &sel->r); |
| 1168 | |
| 1169 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1170 | *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r; |
| 1171 | } else { |
| 1172 | unsigned long flags; |
| 1173 | spin_lock_irqsave(&fimc->slock, flags); |
| 1174 | f->rect = sel->r; |
| 1175 | /* Same crop rectangle on the source pad */ |
| 1176 | fimc->out_frame.rect = sel->r; |
| 1177 | set_bit(ST_FLITE_CONFIG, &fimc->state); |
| 1178 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 1179 | } |
| 1180 | mutex_unlock(&fimc->lock); |
| 1181 | |
| 1182 | v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d", |
| 1183 | __func__, f->rect.left, f->rect.top, f->rect.width, |
| 1184 | f->rect.height, f->f_width, f->f_height); |
| 1185 | |
| 1186 | return ret; |
| 1187 | } |
| 1188 | |
| 1189 | static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on) |
| 1190 | { |
| 1191 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1192 | |
| 1193 | if (fimc->out_path == FIMC_IO_DMA) |
| 1194 | return -ENOIOCTLCMD; |
| 1195 | |
| 1196 | /* TODO: */ |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | static int fimc_lite_subdev_s_power(struct v4l2_subdev *sd, int on) |
| 1202 | { |
| 1203 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1204 | |
| 1205 | if (fimc->out_path == FIMC_IO_DMA) |
| 1206 | return -ENOIOCTLCMD; |
| 1207 | |
| 1208 | /* TODO: */ |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static int fimc_lite_log_status(struct v4l2_subdev *sd) |
| 1214 | { |
| 1215 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1216 | |
| 1217 | flite_hw_dump_regs(fimc, __func__); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static int fimc_lite_subdev_registered(struct v4l2_subdev *sd) |
| 1222 | { |
| 1223 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1224 | struct vb2_queue *q = &fimc->vb_queue; |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 1225 | struct video_device *vfd = &fimc->vfd; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1226 | int ret; |
| 1227 | |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 1228 | memset(vfd, 0, sizeof(*vfd)); |
| 1229 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1230 | fimc->fmt = &fimc_lite_formats[0]; |
| 1231 | fimc->out_path = FIMC_IO_DMA; |
| 1232 | |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1233 | snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture", |
| 1234 | fimc->index); |
| 1235 | |
| 1236 | vfd->fops = &fimc_lite_fops; |
| 1237 | vfd->ioctl_ops = &fimc_lite_ioctl_ops; |
| 1238 | vfd->v4l2_dev = sd->v4l2_dev; |
| 1239 | vfd->minor = -1; |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 1240 | vfd->release = video_device_release_empty; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1241 | vfd->lock = &fimc->lock; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1242 | fimc->ref_count = 0; |
| 1243 | fimc->reqbufs_count = 0; |
| 1244 | |
| 1245 | INIT_LIST_HEAD(&fimc->pending_buf_q); |
| 1246 | INIT_LIST_HEAD(&fimc->active_buf_q); |
| 1247 | |
| 1248 | memset(q, 0, sizeof(*q)); |
| 1249 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 1250 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
| 1251 | q->ops = &fimc_lite_qops; |
| 1252 | q->mem_ops = &vb2_dma_contig_memops; |
| 1253 | q->buf_struct_size = sizeof(struct flite_buffer); |
| 1254 | q->drv_priv = fimc; |
| 1255 | |
| 1256 | vb2_queue_init(q); |
| 1257 | |
| 1258 | fimc->vd_pad.flags = MEDIA_PAD_FL_SINK; |
| 1259 | ret = media_entity_init(&vfd->entity, 1, &fimc->vd_pad, 0); |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 1260 | if (ret < 0) |
| 1261 | return ret; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1262 | |
| 1263 | video_set_drvdata(vfd, fimc); |
| 1264 | |
| 1265 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1); |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 1266 | if (ret < 0) { |
| 1267 | media_entity_cleanup(&vfd->entity); |
| 1268 | return ret; |
| 1269 | } |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1270 | |
| 1271 | v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n", |
| 1272 | vfd->name, video_device_node_name(vfd)); |
| 1273 | return 0; |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd) |
| 1277 | { |
| 1278 | struct fimc_lite *fimc = v4l2_get_subdevdata(sd); |
| 1279 | |
| 1280 | if (fimc == NULL) |
| 1281 | return; |
| 1282 | |
Sylwester Nawrocki | 1bcd704 | 2012-07-26 07:13:08 -0300 | [diff] [blame] | 1283 | if (video_is_registered(&fimc->vfd)) { |
| 1284 | video_unregister_device(&fimc->vfd); |
| 1285 | media_entity_cleanup(&fimc->vfd.entity); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = { |
| 1290 | .registered = fimc_lite_subdev_registered, |
| 1291 | .unregistered = fimc_lite_subdev_unregistered, |
| 1292 | }; |
| 1293 | |
| 1294 | static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = { |
| 1295 | .enum_mbus_code = fimc_lite_subdev_enum_mbus_code, |
| 1296 | .get_selection = fimc_lite_subdev_get_selection, |
| 1297 | .set_selection = fimc_lite_subdev_set_selection, |
| 1298 | .get_fmt = fimc_lite_subdev_get_fmt, |
| 1299 | .set_fmt = fimc_lite_subdev_set_fmt, |
| 1300 | }; |
| 1301 | |
| 1302 | static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = { |
| 1303 | .s_stream = fimc_lite_subdev_s_stream, |
| 1304 | }; |
| 1305 | |
| 1306 | static const struct v4l2_subdev_core_ops fimc_lite_core_ops = { |
| 1307 | .s_power = fimc_lite_subdev_s_power, |
| 1308 | .log_status = fimc_lite_log_status, |
| 1309 | }; |
| 1310 | |
| 1311 | static struct v4l2_subdev_ops fimc_lite_subdev_ops = { |
| 1312 | .core = &fimc_lite_core_ops, |
| 1313 | .video = &fimc_lite_subdev_video_ops, |
| 1314 | .pad = &fimc_lite_subdev_pad_ops, |
| 1315 | }; |
| 1316 | |
| 1317 | static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1318 | { |
| 1319 | struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite, |
| 1320 | ctrl_handler); |
| 1321 | set_bit(ST_FLITE_CONFIG, &fimc->state); |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = { |
| 1326 | .s_ctrl = fimc_lite_s_ctrl, |
| 1327 | }; |
| 1328 | |
| 1329 | static const struct v4l2_ctrl_config fimc_lite_ctrl = { |
| 1330 | .ops = &fimc_lite_ctrl_ops, |
| 1331 | .id = V4L2_CTRL_CLASS_USER | 0x1001, |
| 1332 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 1333 | .name = "Test Pattern 640x480", |
| 1334 | }; |
| 1335 | |
| 1336 | static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc) |
| 1337 | { |
| 1338 | struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler; |
| 1339 | struct v4l2_subdev *sd = &fimc->subdev; |
| 1340 | int ret; |
| 1341 | |
| 1342 | v4l2_subdev_init(sd, &fimc_lite_subdev_ops); |
| 1343 | sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE; |
| 1344 | snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index); |
| 1345 | |
| 1346 | fimc->subdev_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK; |
| 1347 | fimc->subdev_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE; |
| 1348 | ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM, |
| 1349 | fimc->subdev_pads, 0); |
| 1350 | if (ret) |
| 1351 | return ret; |
| 1352 | |
| 1353 | v4l2_ctrl_handler_init(handler, 1); |
| 1354 | fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl, |
| 1355 | NULL); |
| 1356 | if (handler->error) { |
| 1357 | media_entity_cleanup(&sd->entity); |
| 1358 | return handler->error; |
| 1359 | } |
| 1360 | |
| 1361 | sd->ctrl_handler = handler; |
| 1362 | sd->internal_ops = &fimc_lite_subdev_internal_ops; |
| 1363 | sd->entity.ops = &fimc_lite_subdev_media_ops; |
| 1364 | v4l2_set_subdevdata(sd, fimc); |
| 1365 | |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc) |
| 1370 | { |
| 1371 | struct v4l2_subdev *sd = &fimc->subdev; |
| 1372 | |
| 1373 | v4l2_device_unregister_subdev(sd); |
| 1374 | media_entity_cleanup(&sd->entity); |
| 1375 | v4l2_ctrl_handler_free(&fimc->ctrl_handler); |
| 1376 | v4l2_set_subdevdata(sd, NULL); |
| 1377 | } |
| 1378 | |
| 1379 | static void fimc_lite_clk_put(struct fimc_lite *fimc) |
| 1380 | { |
| 1381 | if (IS_ERR_OR_NULL(fimc->clock)) |
| 1382 | return; |
| 1383 | |
| 1384 | clk_unprepare(fimc->clock); |
| 1385 | clk_put(fimc->clock); |
| 1386 | fimc->clock = NULL; |
| 1387 | } |
| 1388 | |
| 1389 | static int fimc_lite_clk_get(struct fimc_lite *fimc) |
| 1390 | { |
| 1391 | int ret; |
| 1392 | |
| 1393 | fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME); |
| 1394 | if (IS_ERR(fimc->clock)) |
| 1395 | return PTR_ERR(fimc->clock); |
| 1396 | |
| 1397 | ret = clk_prepare(fimc->clock); |
| 1398 | if (ret < 0) { |
| 1399 | clk_put(fimc->clock); |
| 1400 | fimc->clock = NULL; |
| 1401 | } |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
| 1405 | static int __devinit fimc_lite_probe(struct platform_device *pdev) |
| 1406 | { |
| 1407 | struct flite_drvdata *drv_data = fimc_lite_get_drvdata(pdev); |
| 1408 | struct fimc_lite *fimc; |
| 1409 | struct resource *res; |
| 1410 | int ret; |
| 1411 | |
| 1412 | fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL); |
| 1413 | if (!fimc) |
| 1414 | return -ENOMEM; |
| 1415 | |
| 1416 | fimc->index = pdev->id; |
| 1417 | fimc->variant = drv_data->variant[fimc->index]; |
| 1418 | fimc->pdev = pdev; |
| 1419 | |
| 1420 | init_waitqueue_head(&fimc->irq_queue); |
| 1421 | spin_lock_init(&fimc->slock); |
| 1422 | mutex_init(&fimc->lock); |
| 1423 | |
| 1424 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1425 | fimc->regs = devm_request_and_ioremap(&pdev->dev, res); |
| 1426 | if (fimc->regs == NULL) { |
| 1427 | dev_err(&pdev->dev, "Failed to obtain io memory\n"); |
| 1428 | return -ENOENT; |
| 1429 | } |
| 1430 | |
| 1431 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1432 | if (res == NULL) { |
| 1433 | dev_err(&pdev->dev, "Failed to get IRQ resource\n"); |
| 1434 | return -ENXIO; |
| 1435 | } |
| 1436 | |
| 1437 | ret = fimc_lite_clk_get(fimc); |
| 1438 | if (ret) |
| 1439 | return ret; |
| 1440 | |
| 1441 | ret = devm_request_irq(&pdev->dev, res->start, flite_irq_handler, |
| 1442 | 0, dev_name(&pdev->dev), fimc); |
| 1443 | if (ret) { |
| 1444 | dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret); |
| 1445 | goto err_clk; |
| 1446 | } |
| 1447 | |
| 1448 | /* The video node will be created within the subdev's registered() op */ |
| 1449 | ret = fimc_lite_create_capture_subdev(fimc); |
| 1450 | if (ret) |
| 1451 | goto err_clk; |
| 1452 | |
| 1453 | platform_set_drvdata(pdev, fimc); |
| 1454 | pm_runtime_enable(&pdev->dev); |
| 1455 | ret = pm_runtime_get_sync(&pdev->dev); |
| 1456 | if (ret < 0) |
| 1457 | goto err_sd; |
| 1458 | |
| 1459 | fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); |
| 1460 | if (IS_ERR(fimc->alloc_ctx)) { |
| 1461 | ret = PTR_ERR(fimc->alloc_ctx); |
| 1462 | goto err_pm; |
| 1463 | } |
| 1464 | pm_runtime_put(&pdev->dev); |
| 1465 | |
| 1466 | dev_dbg(&pdev->dev, "FIMC-LITE.%d registered successfully\n", |
| 1467 | fimc->index); |
| 1468 | return 0; |
| 1469 | err_pm: |
| 1470 | pm_runtime_put(&pdev->dev); |
| 1471 | err_sd: |
| 1472 | fimc_lite_unregister_capture_subdev(fimc); |
| 1473 | err_clk: |
| 1474 | fimc_lite_clk_put(fimc); |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
| 1478 | static int fimc_lite_runtime_resume(struct device *dev) |
| 1479 | { |
| 1480 | struct fimc_lite *fimc = dev_get_drvdata(dev); |
| 1481 | |
| 1482 | clk_enable(fimc->clock); |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static int fimc_lite_runtime_suspend(struct device *dev) |
| 1487 | { |
| 1488 | struct fimc_lite *fimc = dev_get_drvdata(dev); |
| 1489 | |
| 1490 | clk_disable(fimc->clock); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | #ifdef CONFIG_PM_SLEEP |
| 1495 | static int fimc_lite_resume(struct device *dev) |
| 1496 | { |
| 1497 | struct fimc_lite *fimc = dev_get_drvdata(dev); |
| 1498 | struct flite_buffer *buf; |
| 1499 | unsigned long flags; |
| 1500 | int i; |
| 1501 | |
| 1502 | spin_lock_irqsave(&fimc->slock, flags); |
| 1503 | if (!test_and_clear_bit(ST_LPM, &fimc->state) || |
| 1504 | !test_bit(ST_FLITE_IN_USE, &fimc->state)) { |
| 1505 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 1506 | return 0; |
| 1507 | } |
| 1508 | flite_hw_reset(fimc); |
| 1509 | spin_unlock_irqrestore(&fimc->slock, flags); |
| 1510 | |
| 1511 | if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state)) |
| 1512 | return 0; |
| 1513 | |
| 1514 | INIT_LIST_HEAD(&fimc->active_buf_q); |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 1515 | fimc_pipeline_call(fimc, open, &fimc->pipeline, |
| 1516 | &fimc->vfd.entity, false); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1517 | fimc_lite_hw_init(fimc); |
| 1518 | clear_bit(ST_FLITE_SUSPENDED, &fimc->state); |
| 1519 | |
| 1520 | for (i = 0; i < fimc->reqbufs_count; i++) { |
| 1521 | if (list_empty(&fimc->pending_buf_q)) |
| 1522 | break; |
| 1523 | buf = fimc_lite_pending_queue_pop(fimc); |
| 1524 | buffer_queue(&buf->vb); |
| 1525 | } |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | static int fimc_lite_suspend(struct device *dev) |
| 1530 | { |
| 1531 | struct fimc_lite *fimc = dev_get_drvdata(dev); |
| 1532 | bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state); |
| 1533 | int ret; |
| 1534 | |
| 1535 | if (test_and_set_bit(ST_LPM, &fimc->state)) |
| 1536 | return 0; |
| 1537 | |
| 1538 | ret = fimc_lite_stop_capture(fimc, suspend); |
Sylwester Nawrocki | 316efab | 2012-05-18 13:31:28 -0300 | [diff] [blame] | 1539 | if (ret < 0 || !fimc_lite_active(fimc)) |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1540 | return ret; |
| 1541 | |
Sylwester Nawrocki | b9ee31e6 | 2012-08-14 10:46:58 -0300 | [diff] [blame^] | 1542 | return fimc_pipeline_call(fimc, close, &fimc->pipeline); |
Sylwester Nawrocki | 4af8131 | 2012-04-27 05:29:05 -0300 | [diff] [blame] | 1543 | } |
| 1544 | #endif /* CONFIG_PM_SLEEP */ |
| 1545 | |
| 1546 | static int __devexit fimc_lite_remove(struct platform_device *pdev) |
| 1547 | { |
| 1548 | struct fimc_lite *fimc = platform_get_drvdata(pdev); |
| 1549 | struct device *dev = &pdev->dev; |
| 1550 | |
| 1551 | pm_runtime_disable(dev); |
| 1552 | pm_runtime_set_suspended(dev); |
| 1553 | fimc_lite_unregister_capture_subdev(fimc); |
| 1554 | vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx); |
| 1555 | fimc_lite_clk_put(fimc); |
| 1556 | |
| 1557 | dev_info(dev, "Driver unloaded\n"); |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | static struct flite_variant fimc_lite0_variant_exynos4 = { |
| 1562 | .max_width = 8192, |
| 1563 | .max_height = 8192, |
| 1564 | .out_width_align = 8, |
| 1565 | .win_hor_offs_align = 2, |
| 1566 | .out_hor_offs_align = 8, |
| 1567 | }; |
| 1568 | |
| 1569 | /* EXYNOS4212, EXYNOS4412 */ |
| 1570 | static struct flite_drvdata fimc_lite_drvdata_exynos4 = { |
| 1571 | .variant = { |
| 1572 | [0] = &fimc_lite0_variant_exynos4, |
| 1573 | [1] = &fimc_lite0_variant_exynos4, |
| 1574 | }, |
| 1575 | }; |
| 1576 | |
| 1577 | static struct platform_device_id fimc_lite_driver_ids[] = { |
| 1578 | { |
| 1579 | .name = "exynos-fimc-lite", |
| 1580 | .driver_data = (unsigned long)&fimc_lite_drvdata_exynos4, |
| 1581 | }, |
| 1582 | { /* sentinel */ }, |
| 1583 | }; |
| 1584 | MODULE_DEVICE_TABLE(platform, fimc_lite_driver_ids); |
| 1585 | |
| 1586 | static const struct dev_pm_ops fimc_lite_pm_ops = { |
| 1587 | SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume) |
| 1588 | SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume, |
| 1589 | NULL) |
| 1590 | }; |
| 1591 | |
| 1592 | static struct platform_driver fimc_lite_driver = { |
| 1593 | .probe = fimc_lite_probe, |
| 1594 | .remove = __devexit_p(fimc_lite_remove), |
| 1595 | .id_table = fimc_lite_driver_ids, |
| 1596 | .driver = { |
| 1597 | .name = FIMC_LITE_DRV_NAME, |
| 1598 | .owner = THIS_MODULE, |
| 1599 | .pm = &fimc_lite_pm_ops, |
| 1600 | } |
| 1601 | }; |
| 1602 | module_platform_driver(fimc_lite_driver); |
| 1603 | MODULE_LICENSE("GPL"); |
| 1604 | MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME); |