1 // SPDX-License-Identifier: GPL-2.0
5 * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module
7 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
8 * Copyright (C) 2015-2018 Linaro Ltd.
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/interrupt.h>
13 #include <linux/iommu.h>
14 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/spinlock_types.h>
19 #include <linux/spinlock.h>
20 #include <media/media-entity.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-subdev.h>
24 #include "camss-vfe.h"
27 #define MSM_VFE_NAME "msm_vfe"
29 #define vfe_line_array(ptr_line) \
30 ((const struct vfe_line (*)[]) &(ptr_line[-(ptr_line->id)]))
32 #define to_vfe(ptr_line) \
33 container_of(vfe_line_array(ptr_line), struct vfe_device, line)
35 /* VFE reset timeout */
36 #define VFE_RESET_TIMEOUT_MS 50
37 /* VFE halt timeout */
38 #define VFE_HALT_TIMEOUT_MS 100
39 /* Max number of frame drop updates per frame */
40 #define VFE_FRAME_DROP_UPDATES 5
41 /* Frame drop value. NOTE: VAL + UPDATES should not exceed 31 */
42 #define VFE_FRAME_DROP_VAL 20
44 #define VFE_NEXT_SOF_MS 500
46 #define SCALER_RATIO_MAX 16
53 static const struct vfe_format formats_rdi_8x16[] = {
54 { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
55 { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
56 { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
57 { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
58 { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
59 { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
60 { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
61 { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
62 { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
63 { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
64 { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
65 { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
66 { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
67 { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
68 { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
69 { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
70 { MEDIA_BUS_FMT_Y10_1X10, 10 },
73 static const struct vfe_format formats_pix_8x16[] = {
74 { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
75 { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
76 { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
77 { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
80 static const struct vfe_format formats_rdi_8x96[] = {
81 { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
82 { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
83 { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
84 { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
85 { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
86 { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
87 { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
88 { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
89 { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
90 { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
91 { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
92 { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
93 { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 },
94 { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
95 { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
96 { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
97 { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
98 { MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
99 { MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
100 { MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
101 { MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
102 { MEDIA_BUS_FMT_Y10_1X10, 10 },
103 { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 },
106 static const struct vfe_format formats_pix_8x96[] = {
107 { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
108 { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
109 { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
110 { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
114 * vfe_get_bpp - map media bus format to bits per pixel
115 * @formats: supported media bus formats array
116 * @nformats: size of @formats array
117 * @code: media bus format code
119 * Return number of bits per pixel
121 static u8 vfe_get_bpp(const struct vfe_format *formats,
122 unsigned int nformats, u32 code)
126 for (i = 0; i < nformats; i++)
127 if (code == formats[i].code)
128 return formats[i].bpp;
130 WARN(1, "Unknown format\n");
132 return formats[0].bpp;
135 static u32 vfe_find_code(u32 *code, unsigned int n_code,
136 unsigned int index, u32 req_code)
140 if (!req_code && (index >= n_code))
143 for (i = 0; i < n_code; i++)
145 if (req_code == code[i])
155 static u32 vfe_src_pad_code(struct vfe_line *line, u32 sink_code,
156 unsigned int index, u32 src_req_code)
158 struct vfe_device *vfe = to_vfe(line);
160 if (vfe->camss->version == CAMSS_8x16)
162 case MEDIA_BUS_FMT_YUYV8_2X8:
165 MEDIA_BUS_FMT_YUYV8_2X8,
166 MEDIA_BUS_FMT_YUYV8_1_5X8,
169 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
170 index, src_req_code);
172 case MEDIA_BUS_FMT_YVYU8_2X8:
175 MEDIA_BUS_FMT_YVYU8_2X8,
176 MEDIA_BUS_FMT_YVYU8_1_5X8,
179 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
180 index, src_req_code);
182 case MEDIA_BUS_FMT_UYVY8_2X8:
185 MEDIA_BUS_FMT_UYVY8_2X8,
186 MEDIA_BUS_FMT_UYVY8_1_5X8,
189 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
190 index, src_req_code);
192 case MEDIA_BUS_FMT_VYUY8_2X8:
195 MEDIA_BUS_FMT_VYUY8_2X8,
196 MEDIA_BUS_FMT_VYUY8_1_5X8,
199 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
200 index, src_req_code);
208 else if (vfe->camss->version == CAMSS_8x96)
210 case MEDIA_BUS_FMT_YUYV8_2X8:
213 MEDIA_BUS_FMT_YUYV8_2X8,
214 MEDIA_BUS_FMT_YVYU8_2X8,
215 MEDIA_BUS_FMT_UYVY8_2X8,
216 MEDIA_BUS_FMT_VYUY8_2X8,
217 MEDIA_BUS_FMT_YUYV8_1_5X8,
220 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
221 index, src_req_code);
223 case MEDIA_BUS_FMT_YVYU8_2X8:
226 MEDIA_BUS_FMT_YVYU8_2X8,
227 MEDIA_BUS_FMT_YUYV8_2X8,
228 MEDIA_BUS_FMT_UYVY8_2X8,
229 MEDIA_BUS_FMT_VYUY8_2X8,
230 MEDIA_BUS_FMT_YVYU8_1_5X8,
233 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
234 index, src_req_code);
236 case MEDIA_BUS_FMT_UYVY8_2X8:
239 MEDIA_BUS_FMT_UYVY8_2X8,
240 MEDIA_BUS_FMT_YUYV8_2X8,
241 MEDIA_BUS_FMT_YVYU8_2X8,
242 MEDIA_BUS_FMT_VYUY8_2X8,
243 MEDIA_BUS_FMT_UYVY8_1_5X8,
246 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
247 index, src_req_code);
249 case MEDIA_BUS_FMT_VYUY8_2X8:
252 MEDIA_BUS_FMT_VYUY8_2X8,
253 MEDIA_BUS_FMT_YUYV8_2X8,
254 MEDIA_BUS_FMT_YVYU8_2X8,
255 MEDIA_BUS_FMT_UYVY8_2X8,
256 MEDIA_BUS_FMT_VYUY8_1_5X8,
259 return vfe_find_code(src_code, ARRAY_SIZE(src_code),
260 index, src_req_code);
273 * vfe_reset - Trigger reset on VFE module and wait to complete
276 * Return 0 on success or a negative error code otherwise
278 static int vfe_reset(struct vfe_device *vfe)
282 reinit_completion(&vfe->reset_complete);
284 vfe->ops->global_reset(vfe);
286 time = wait_for_completion_timeout(&vfe->reset_complete,
287 msecs_to_jiffies(VFE_RESET_TIMEOUT_MS));
289 dev_err(vfe->camss->dev, "VFE reset timeout\n");
297 * vfe_halt - Trigger halt on VFE module and wait to complete
300 * Return 0 on success or a negative error code otherwise
302 static int vfe_halt(struct vfe_device *vfe)
306 reinit_completion(&vfe->halt_complete);
308 vfe->ops->halt_request(vfe);
310 time = wait_for_completion_timeout(&vfe->halt_complete,
311 msecs_to_jiffies(VFE_HALT_TIMEOUT_MS));
313 dev_err(vfe->camss->dev, "VFE halt timeout\n");
320 static void vfe_init_outputs(struct vfe_device *vfe)
324 for (i = 0; i < ARRAY_SIZE(vfe->line); i++) {
325 struct vfe_output *output = &vfe->line[i].output;
327 output->state = VFE_OUTPUT_OFF;
328 output->buf[0] = NULL;
329 output->buf[1] = NULL;
330 INIT_LIST_HEAD(&output->pending_bufs);
334 static void vfe_reset_output_maps(struct vfe_device *vfe)
338 for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
339 vfe->wm_output_map[i] = VFE_LINE_NONE;
342 static void vfe_output_init_addrs(struct vfe_device *vfe,
343 struct vfe_output *output, u8 sync)
349 output->active_buf = 0;
351 for (i = 0; i < output->wm_num; i++) {
353 ping_addr = output->buf[0]->addr[i];
358 pong_addr = output->buf[1]->addr[i];
360 pong_addr = ping_addr;
362 vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], ping_addr);
363 vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], pong_addr);
365 vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]);
369 static void vfe_output_update_ping_addr(struct vfe_device *vfe,
370 struct vfe_output *output, u8 sync)
375 for (i = 0; i < output->wm_num; i++) {
377 addr = output->buf[0]->addr[i];
381 vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i], addr);
383 vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]);
387 static void vfe_output_update_pong_addr(struct vfe_device *vfe,
388 struct vfe_output *output, u8 sync)
393 for (i = 0; i < output->wm_num; i++) {
395 addr = output->buf[1]->addr[i];
399 vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i], addr);
401 vfe->ops->bus_reload_wm(vfe, output->wm_idx[i]);
406 static int vfe_reserve_wm(struct vfe_device *vfe, enum vfe_line_id line_id)
411 for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) {
412 if (vfe->wm_output_map[i] == VFE_LINE_NONE) {
413 vfe->wm_output_map[i] = line_id;
422 static int vfe_release_wm(struct vfe_device *vfe, u8 wm)
424 if (wm >= ARRAY_SIZE(vfe->wm_output_map))
427 vfe->wm_output_map[wm] = VFE_LINE_NONE;
432 static void vfe_output_frame_drop(struct vfe_device *vfe,
433 struct vfe_output *output,
439 /* We need to toggle update period to be valid on next frame */
440 output->drop_update_idx++;
441 output->drop_update_idx %= VFE_FRAME_DROP_UPDATES;
442 drop_period = VFE_FRAME_DROP_VAL + output->drop_update_idx;
444 for (i = 0; i < output->wm_num; i++) {
445 vfe->ops->wm_set_framedrop_period(vfe, output->wm_idx[i],
447 vfe->ops->wm_set_framedrop_pattern(vfe, output->wm_idx[i],
450 vfe->ops->reg_update(vfe,
451 container_of(output, struct vfe_line, output)->id);
454 static struct camss_buffer *vfe_buf_get_pending(struct vfe_output *output)
456 struct camss_buffer *buffer = NULL;
458 if (!list_empty(&output->pending_bufs)) {
459 buffer = list_first_entry(&output->pending_bufs,
462 list_del(&buffer->queue);
469 * vfe_buf_add_pending - Add output buffer to list of pending
470 * @output: VFE output
471 * @buffer: Video buffer
473 static void vfe_buf_add_pending(struct vfe_output *output,
474 struct camss_buffer *buffer)
476 INIT_LIST_HEAD(&buffer->queue);
477 list_add_tail(&buffer->queue, &output->pending_bufs);
481 * vfe_buf_flush_pending - Flush all pending buffers.
482 * @output: VFE output
483 * @state: vb2 buffer state
485 static void vfe_buf_flush_pending(struct vfe_output *output,
486 enum vb2_buffer_state state)
488 struct camss_buffer *buf;
489 struct camss_buffer *t;
491 list_for_each_entry_safe(buf, t, &output->pending_bufs, queue) {
492 vb2_buffer_done(&buf->vb.vb2_buf, state);
493 list_del(&buf->queue);
497 static void vfe_buf_update_wm_on_next(struct vfe_device *vfe,
498 struct vfe_output *output)
500 switch (output->state) {
501 case VFE_OUTPUT_CONTINUOUS:
502 vfe_output_frame_drop(vfe, output, 3);
504 case VFE_OUTPUT_SINGLE:
506 dev_err_ratelimited(vfe->camss->dev,
507 "Next buf in wrong state! %d\n",
513 static void vfe_buf_update_wm_on_last(struct vfe_device *vfe,
514 struct vfe_output *output)
516 switch (output->state) {
517 case VFE_OUTPUT_CONTINUOUS:
518 output->state = VFE_OUTPUT_SINGLE;
519 vfe_output_frame_drop(vfe, output, 1);
521 case VFE_OUTPUT_SINGLE:
522 output->state = VFE_OUTPUT_STOPPING;
523 vfe_output_frame_drop(vfe, output, 0);
526 dev_err_ratelimited(vfe->camss->dev,
527 "Last buff in wrong state! %d\n",
533 static void vfe_buf_update_wm_on_new(struct vfe_device *vfe,
534 struct vfe_output *output,
535 struct camss_buffer *new_buf)
539 switch (output->state) {
540 case VFE_OUTPUT_SINGLE:
541 inactive_idx = !output->active_buf;
543 if (!output->buf[inactive_idx]) {
544 output->buf[inactive_idx] = new_buf;
547 vfe_output_update_pong_addr(vfe, output, 0);
549 vfe_output_update_ping_addr(vfe, output, 0);
551 vfe_output_frame_drop(vfe, output, 3);
552 output->state = VFE_OUTPUT_CONTINUOUS;
554 vfe_buf_add_pending(output, new_buf);
555 dev_err_ratelimited(vfe->camss->dev,
556 "Inactive buffer is busy\n");
560 case VFE_OUTPUT_IDLE:
561 if (!output->buf[0]) {
562 output->buf[0] = new_buf;
564 vfe_output_init_addrs(vfe, output, 1);
566 vfe_output_frame_drop(vfe, output, 1);
567 output->state = VFE_OUTPUT_SINGLE;
569 vfe_buf_add_pending(output, new_buf);
570 dev_err_ratelimited(vfe->camss->dev,
571 "Output idle with buffer set!\n");
575 case VFE_OUTPUT_CONTINUOUS:
577 vfe_buf_add_pending(output, new_buf);
582 static int vfe_get_output(struct vfe_line *line)
584 struct vfe_device *vfe = to_vfe(line);
585 struct vfe_output *output;
586 struct v4l2_format *f = &line->video_out.active_fmt;
591 spin_lock_irqsave(&vfe->output_lock, flags);
593 output = &line->output;
594 if (output->state != VFE_OUTPUT_OFF) {
595 dev_err(vfe->camss->dev, "Output is running\n");
598 output->state = VFE_OUTPUT_RESERVED;
600 output->active_buf = 0;
602 switch (f->fmt.pix_mp.pixelformat) {
603 case V4L2_PIX_FMT_NV12:
604 case V4L2_PIX_FMT_NV21:
605 case V4L2_PIX_FMT_NV16:
606 case V4L2_PIX_FMT_NV61:
614 for (i = 0; i < output->wm_num; i++) {
615 wm_idx = vfe_reserve_wm(vfe, line->id);
617 dev_err(vfe->camss->dev, "Can not reserve wm\n");
620 output->wm_idx[i] = wm_idx;
623 output->drop_update_idx = 0;
625 spin_unlock_irqrestore(&vfe->output_lock, flags);
630 for (i--; i >= 0; i--)
631 vfe_release_wm(vfe, output->wm_idx[i]);
632 output->state = VFE_OUTPUT_OFF;
634 spin_unlock_irqrestore(&vfe->output_lock, flags);
639 static int vfe_put_output(struct vfe_line *line)
641 struct vfe_device *vfe = to_vfe(line);
642 struct vfe_output *output = &line->output;
646 spin_lock_irqsave(&vfe->output_lock, flags);
648 for (i = 0; i < output->wm_num; i++)
649 vfe_release_wm(vfe, output->wm_idx[i]);
651 output->state = VFE_OUTPUT_OFF;
653 spin_unlock_irqrestore(&vfe->output_lock, flags);
657 static int vfe_enable_output(struct vfe_line *line)
659 struct vfe_device *vfe = to_vfe(line);
660 struct vfe_output *output = &line->output;
661 const struct vfe_hw_ops *ops = vfe->ops;
666 ub_size = ops->get_ub_size(vfe->id);
670 spin_lock_irqsave(&vfe->output_lock, flags);
672 ops->reg_update_clear(vfe, line->id);
674 if (output->state != VFE_OUTPUT_RESERVED) {
675 dev_err(vfe->camss->dev, "Output is not in reserved state %d\n",
677 spin_unlock_irqrestore(&vfe->output_lock, flags);
680 output->state = VFE_OUTPUT_IDLE;
682 output->buf[0] = vfe_buf_get_pending(output);
683 output->buf[1] = vfe_buf_get_pending(output);
685 if (!output->buf[0] && output->buf[1]) {
686 output->buf[0] = output->buf[1];
687 output->buf[1] = NULL;
691 output->state = VFE_OUTPUT_SINGLE;
694 output->state = VFE_OUTPUT_CONTINUOUS;
696 switch (output->state) {
697 case VFE_OUTPUT_SINGLE:
698 vfe_output_frame_drop(vfe, output, 1);
700 case VFE_OUTPUT_CONTINUOUS:
701 vfe_output_frame_drop(vfe, output, 3);
704 vfe_output_frame_drop(vfe, output, 0);
708 output->sequence = 0;
709 output->wait_sof = 0;
710 output->wait_reg_update = 0;
711 reinit_completion(&output->sof);
712 reinit_completion(&output->reg_update);
714 vfe_output_init_addrs(vfe, output, 0);
716 if (line->id != VFE_LINE_PIX) {
717 ops->set_cgc_override(vfe, output->wm_idx[0], 1);
718 ops->enable_irq_wm_line(vfe, output->wm_idx[0], line->id, 1);
719 ops->bus_connect_wm_to_rdi(vfe, output->wm_idx[0], line->id);
720 ops->wm_set_subsample(vfe, output->wm_idx[0]);
721 ops->set_rdi_cid(vfe, line->id, 0);
722 ops->wm_set_ub_cfg(vfe, output->wm_idx[0],
723 (ub_size + 1) * output->wm_idx[0], ub_size);
724 ops->wm_frame_based(vfe, output->wm_idx[0], 1);
725 ops->wm_enable(vfe, output->wm_idx[0], 1);
726 ops->bus_reload_wm(vfe, output->wm_idx[0]);
728 ub_size /= output->wm_num;
729 for (i = 0; i < output->wm_num; i++) {
730 ops->set_cgc_override(vfe, output->wm_idx[i], 1);
731 ops->wm_set_subsample(vfe, output->wm_idx[i]);
732 ops->wm_set_ub_cfg(vfe, output->wm_idx[i],
733 (ub_size + 1) * output->wm_idx[i],
735 ops->wm_line_based(vfe, output->wm_idx[i],
736 &line->video_out.active_fmt.fmt.pix_mp,
738 ops->wm_enable(vfe, output->wm_idx[i], 1);
739 ops->bus_reload_wm(vfe, output->wm_idx[i]);
741 ops->enable_irq_pix_line(vfe, 0, line->id, 1);
742 ops->set_module_cfg(vfe, 1);
743 ops->set_camif_cfg(vfe, line);
744 ops->set_realign_cfg(vfe, line, 1);
745 ops->set_xbar_cfg(vfe, output, 1);
746 ops->set_demux_cfg(vfe, line);
747 ops->set_scale_cfg(vfe, line);
748 ops->set_crop_cfg(vfe, line);
749 ops->set_clamp_cfg(vfe);
750 ops->set_camif_cmd(vfe, 1);
753 ops->reg_update(vfe, line->id);
755 spin_unlock_irqrestore(&vfe->output_lock, flags);
760 static int vfe_disable_output(struct vfe_line *line)
762 struct vfe_device *vfe = to_vfe(line);
763 struct vfe_output *output = &line->output;
764 const struct vfe_hw_ops *ops = vfe->ops;
769 spin_lock_irqsave(&vfe->output_lock, flags);
771 output->wait_sof = 1;
772 spin_unlock_irqrestore(&vfe->output_lock, flags);
774 time = wait_for_completion_timeout(&output->sof,
775 msecs_to_jiffies(VFE_NEXT_SOF_MS));
777 dev_err(vfe->camss->dev, "VFE sof timeout\n");
779 spin_lock_irqsave(&vfe->output_lock, flags);
780 for (i = 0; i < output->wm_num; i++)
781 ops->wm_enable(vfe, output->wm_idx[i], 0);
783 ops->reg_update(vfe, line->id);
784 output->wait_reg_update = 1;
785 spin_unlock_irqrestore(&vfe->output_lock, flags);
787 time = wait_for_completion_timeout(&output->reg_update,
788 msecs_to_jiffies(VFE_NEXT_SOF_MS));
790 dev_err(vfe->camss->dev, "VFE reg update timeout\n");
792 spin_lock_irqsave(&vfe->output_lock, flags);
794 if (line->id != VFE_LINE_PIX) {
795 ops->wm_frame_based(vfe, output->wm_idx[0], 0);
796 ops->bus_disconnect_wm_from_rdi(vfe, output->wm_idx[0],
798 ops->enable_irq_wm_line(vfe, output->wm_idx[0], line->id, 0);
799 ops->set_cgc_override(vfe, output->wm_idx[0], 0);
800 spin_unlock_irqrestore(&vfe->output_lock, flags);
802 for (i = 0; i < output->wm_num; i++) {
803 ops->wm_line_based(vfe, output->wm_idx[i], NULL, i, 0);
804 ops->set_cgc_override(vfe, output->wm_idx[i], 0);
807 ops->enable_irq_pix_line(vfe, 0, line->id, 0);
808 ops->set_module_cfg(vfe, 0);
809 ops->set_realign_cfg(vfe, line, 0);
810 ops->set_xbar_cfg(vfe, output, 0);
812 ops->set_camif_cmd(vfe, 0);
813 spin_unlock_irqrestore(&vfe->output_lock, flags);
815 ops->camif_wait_for_stop(vfe, vfe->camss->dev);
822 * vfe_enable - Enable streaming on VFE line
825 * Return 0 on success or a negative error code otherwise
827 static int vfe_enable(struct vfe_line *line)
829 struct vfe_device *vfe = to_vfe(line);
832 mutex_lock(&vfe->stream_lock);
834 if (!vfe->stream_count) {
835 vfe->ops->enable_irq_common(vfe);
837 vfe->ops->bus_enable_wr_if(vfe, 1);
839 vfe->ops->set_qos(vfe);
841 vfe->ops->set_ds(vfe);
846 mutex_unlock(&vfe->stream_lock);
848 ret = vfe_get_output(line);
850 goto error_get_output;
852 ret = vfe_enable_output(line);
854 goto error_enable_output;
856 vfe->was_streaming = 1;
862 vfe_put_output(line);
865 mutex_lock(&vfe->stream_lock);
867 if (vfe->stream_count == 1)
868 vfe->ops->bus_enable_wr_if(vfe, 0);
872 mutex_unlock(&vfe->stream_lock);
878 * vfe_disable - Disable streaming on VFE line
881 * Return 0 on success or a negative error code otherwise
883 static int vfe_disable(struct vfe_line *line)
885 struct vfe_device *vfe = to_vfe(line);
887 vfe_disable_output(line);
889 vfe_put_output(line);
891 mutex_lock(&vfe->stream_lock);
893 if (vfe->stream_count == 1)
894 vfe->ops->bus_enable_wr_if(vfe, 0);
898 mutex_unlock(&vfe->stream_lock);
904 * vfe_isr_sof - Process start of frame interrupt
908 static void vfe_isr_sof(struct vfe_device *vfe, enum vfe_line_id line_id)
910 struct vfe_output *output;
913 spin_lock_irqsave(&vfe->output_lock, flags);
914 output = &vfe->line[line_id].output;
915 if (output->wait_sof) {
916 output->wait_sof = 0;
917 complete(&output->sof);
919 spin_unlock_irqrestore(&vfe->output_lock, flags);
923 * vfe_isr_reg_update - Process reg update interrupt
927 static void vfe_isr_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
929 struct vfe_output *output;
932 spin_lock_irqsave(&vfe->output_lock, flags);
933 vfe->ops->reg_update_clear(vfe, line_id);
935 output = &vfe->line[line_id].output;
937 if (output->wait_reg_update) {
938 output->wait_reg_update = 0;
939 complete(&output->reg_update);
940 spin_unlock_irqrestore(&vfe->output_lock, flags);
944 if (output->state == VFE_OUTPUT_STOPPING) {
945 /* Release last buffer when hw is idle */
946 if (output->last_buffer) {
947 vb2_buffer_done(&output->last_buffer->vb.vb2_buf,
949 output->last_buffer = NULL;
951 output->state = VFE_OUTPUT_IDLE;
953 /* Buffers received in stopping state are queued in */
954 /* dma pending queue, start next capture here */
956 output->buf[0] = vfe_buf_get_pending(output);
957 output->buf[1] = vfe_buf_get_pending(output);
959 if (!output->buf[0] && output->buf[1]) {
960 output->buf[0] = output->buf[1];
961 output->buf[1] = NULL;
965 output->state = VFE_OUTPUT_SINGLE;
968 output->state = VFE_OUTPUT_CONTINUOUS;
970 switch (output->state) {
971 case VFE_OUTPUT_SINGLE:
972 vfe_output_frame_drop(vfe, output, 2);
974 case VFE_OUTPUT_CONTINUOUS:
975 vfe_output_frame_drop(vfe, output, 3);
978 vfe_output_frame_drop(vfe, output, 0);
982 vfe_output_init_addrs(vfe, output, 1);
985 spin_unlock_irqrestore(&vfe->output_lock, flags);
989 * vfe_isr_wm_done - Process write master done interrupt
991 * @wm: Write master id
993 static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
995 struct camss_buffer *ready_buf;
996 struct vfe_output *output;
997 dma_addr_t *new_addr;
1000 u64 ts = ktime_get_ns();
1003 active_index = vfe->ops->wm_get_ping_pong_status(vfe, wm);
1005 spin_lock_irqsave(&vfe->output_lock, flags);
1007 if (vfe->wm_output_map[wm] == VFE_LINE_NONE) {
1008 dev_err_ratelimited(vfe->camss->dev,
1009 "Received wm done for unmapped index\n");
1012 output = &vfe->line[vfe->wm_output_map[wm]].output;
1014 if (output->active_buf == active_index) {
1015 dev_err_ratelimited(vfe->camss->dev,
1016 "Active buffer mismatch!\n");
1019 output->active_buf = active_index;
1021 ready_buf = output->buf[!active_index];
1023 dev_err_ratelimited(vfe->camss->dev,
1024 "Missing ready buf %d %d!\n",
1025 !active_index, output->state);
1029 ready_buf->vb.vb2_buf.timestamp = ts;
1030 ready_buf->vb.sequence = output->sequence++;
1032 /* Get next buffer */
1033 output->buf[!active_index] = vfe_buf_get_pending(output);
1034 if (!output->buf[!active_index]) {
1035 /* No next buffer - set same address */
1036 new_addr = ready_buf->addr;
1037 vfe_buf_update_wm_on_last(vfe, output);
1039 new_addr = output->buf[!active_index]->addr;
1040 vfe_buf_update_wm_on_next(vfe, output);
1044 for (i = 0; i < output->wm_num; i++)
1045 vfe->ops->wm_set_ping_addr(vfe, output->wm_idx[i],
1048 for (i = 0; i < output->wm_num; i++)
1049 vfe->ops->wm_set_pong_addr(vfe, output->wm_idx[i],
1052 spin_unlock_irqrestore(&vfe->output_lock, flags);
1054 if (output->state == VFE_OUTPUT_STOPPING)
1055 output->last_buffer = ready_buf;
1057 vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
1062 spin_unlock_irqrestore(&vfe->output_lock, flags);
1066 * vfe_isr_wm_done - Process composite image done interrupt
1068 * @comp: Composite image id
1070 static void vfe_isr_comp_done(struct vfe_device *vfe, u8 comp)
1074 for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
1075 if (vfe->wm_output_map[i] == VFE_LINE_PIX) {
1076 vfe_isr_wm_done(vfe, i);
1081 static inline void vfe_isr_reset_ack(struct vfe_device *vfe)
1083 complete(&vfe->reset_complete);
1086 static inline void vfe_isr_halt_ack(struct vfe_device *vfe)
1088 complete(&vfe->halt_complete);
1089 vfe->ops->halt_clear(vfe);
1093 * vfe_set_clock_rates - Calculate and set clock rates on VFE module
1096 * Return 0 on success or a negative error code otherwise
1098 static int vfe_set_clock_rates(struct vfe_device *vfe)
1100 struct device *dev = vfe->camss->dev;
1101 u32 pixel_clock[MSM_VFE_LINE_NUM];
1105 for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) {
1106 ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
1112 for (i = 0; i < vfe->nclocks; i++) {
1113 struct camss_clock *clock = &vfe->clock[i];
1115 if (!strcmp(clock->name, "vfe0") ||
1116 !strcmp(clock->name, "vfe1")) {
1120 for (j = VFE_LINE_RDI0; j <= VFE_LINE_PIX; j++) {
1124 if (j == VFE_LINE_PIX) {
1125 tmp = pixel_clock[j];
1127 struct vfe_line *l = &vfe->line[j];
1129 bpp = vfe_get_bpp(l->formats,
1131 l->fmt[MSM_VFE_PAD_SINK].code);
1132 tmp = pixel_clock[j] * bpp / 64;
1139 camss_add_clock_margin(&min_rate);
1141 for (j = 0; j < clock->nfreqs; j++)
1142 if (min_rate < clock->freq[j])
1145 if (j == clock->nfreqs) {
1147 "Pixel clock is too high for VFE");
1151 /* if sensor pixel clock is not available */
1152 /* set highest possible VFE clock rate */
1154 j = clock->nfreqs - 1;
1156 rate = clk_round_rate(clock->clk, clock->freq[j]);
1158 dev_err(dev, "clk round rate failed: %ld\n",
1163 ret = clk_set_rate(clock->clk, rate);
1165 dev_err(dev, "clk set rate failed: %d\n", ret);
1175 * vfe_check_clock_rates - Check current clock rates on VFE module
1178 * Return 0 if current clock rates are suitable for a new pipeline
1179 * or a negative error code otherwise
1181 static int vfe_check_clock_rates(struct vfe_device *vfe)
1183 u32 pixel_clock[MSM_VFE_LINE_NUM];
1187 for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) {
1188 ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
1194 for (i = 0; i < vfe->nclocks; i++) {
1195 struct camss_clock *clock = &vfe->clock[i];
1197 if (!strcmp(clock->name, "vfe0") ||
1198 !strcmp(clock->name, "vfe1")) {
1202 for (j = VFE_LINE_RDI0; j <= VFE_LINE_PIX; j++) {
1206 if (j == VFE_LINE_PIX) {
1207 tmp = pixel_clock[j];
1209 struct vfe_line *l = &vfe->line[j];
1211 bpp = vfe_get_bpp(l->formats,
1213 l->fmt[MSM_VFE_PAD_SINK].code);
1214 tmp = pixel_clock[j] * bpp / 64;
1221 camss_add_clock_margin(&min_rate);
1223 rate = clk_get_rate(clock->clk);
1224 if (rate < min_rate)
1233 * vfe_get - Power up and reset VFE module
1236 * Return 0 on success or a negative error code otherwise
1238 static int vfe_get(struct vfe_device *vfe)
1242 mutex_lock(&vfe->power_lock);
1244 if (vfe->power_count == 0) {
1245 ret = camss_pm_domain_on(vfe->camss, vfe->id);
1247 goto error_pm_domain;
1249 ret = pm_runtime_get_sync(vfe->camss->dev);
1251 goto error_pm_runtime_get;
1253 ret = vfe_set_clock_rates(vfe);
1257 ret = camss_enable_clocks(vfe->nclocks, vfe->clock,
1262 ret = vfe_reset(vfe);
1266 vfe_reset_output_maps(vfe);
1268 vfe_init_outputs(vfe);
1270 ret = vfe_check_clock_rates(vfe);
1276 mutex_unlock(&vfe->power_lock);
1281 camss_disable_clocks(vfe->nclocks, vfe->clock);
1284 pm_runtime_put_sync(vfe->camss->dev);
1286 error_pm_runtime_get:
1287 camss_pm_domain_off(vfe->camss, vfe->id);
1290 mutex_unlock(&vfe->power_lock);
1296 * vfe_put - Power down VFE module
1299 static void vfe_put(struct vfe_device *vfe)
1301 mutex_lock(&vfe->power_lock);
1303 if (vfe->power_count == 0) {
1304 dev_err(vfe->camss->dev, "vfe power off on power_count == 0\n");
1306 } else if (vfe->power_count == 1) {
1307 if (vfe->was_streaming) {
1308 vfe->was_streaming = 0;
1311 camss_disable_clocks(vfe->nclocks, vfe->clock);
1312 pm_runtime_put_sync(vfe->camss->dev);
1313 camss_pm_domain_off(vfe->camss, vfe->id);
1319 mutex_unlock(&vfe->power_lock);
1323 * vfe_queue_buffer - Add empty buffer
1324 * @vid: Video device structure
1325 * @buf: Buffer to be enqueued
1327 * Add an empty buffer - depending on the current number of buffers it will be
1328 * put in pending buffer queue or directly given to the hardware to be filled.
1330 * Return 0 on success or a negative error code otherwise
1332 static int vfe_queue_buffer(struct camss_video *vid,
1333 struct camss_buffer *buf)
1335 struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
1336 struct vfe_device *vfe = to_vfe(line);
1337 struct vfe_output *output;
1338 unsigned long flags;
1340 output = &line->output;
1342 spin_lock_irqsave(&vfe->output_lock, flags);
1344 vfe_buf_update_wm_on_new(vfe, output, buf);
1346 spin_unlock_irqrestore(&vfe->output_lock, flags);
1352 * vfe_flush_buffers - Return all vb2 buffers
1353 * @vid: Video device structure
1354 * @state: vb2 buffer state of the returned buffers
1356 * Return all buffers to vb2. This includes queued pending buffers (still
1357 * unused) and any buffers given to the hardware but again still not used.
1359 * Return 0 on success or a negative error code otherwise
1361 static int vfe_flush_buffers(struct camss_video *vid,
1362 enum vb2_buffer_state state)
1364 struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
1365 struct vfe_device *vfe = to_vfe(line);
1366 struct vfe_output *output;
1367 unsigned long flags;
1369 output = &line->output;
1371 spin_lock_irqsave(&vfe->output_lock, flags);
1373 vfe_buf_flush_pending(output, state);
1376 vb2_buffer_done(&output->buf[0]->vb.vb2_buf, state);
1379 vb2_buffer_done(&output->buf[1]->vb.vb2_buf, state);
1381 if (output->last_buffer) {
1382 vb2_buffer_done(&output->last_buffer->vb.vb2_buf, state);
1383 output->last_buffer = NULL;
1386 spin_unlock_irqrestore(&vfe->output_lock, flags);
1392 * vfe_set_power - Power on/off VFE module
1393 * @sd: VFE V4L2 subdevice
1394 * @on: Requested power state
1396 * Return 0 on success or a negative error code otherwise
1398 static int vfe_set_power(struct v4l2_subdev *sd, int on)
1400 struct vfe_line *line = v4l2_get_subdevdata(sd);
1401 struct vfe_device *vfe = to_vfe(line);
1409 vfe->ops->hw_version_read(vfe, vfe->camss->dev);
1418 * vfe_set_stream - Enable/disable streaming on VFE module
1419 * @sd: VFE V4L2 subdevice
1420 * @enable: Requested streaming state
1422 * Main configuration of VFE module is triggered here.
1424 * Return 0 on success or a negative error code otherwise
1426 static int vfe_set_stream(struct v4l2_subdev *sd, int enable)
1428 struct vfe_line *line = v4l2_get_subdevdata(sd);
1429 struct vfe_device *vfe = to_vfe(line);
1433 ret = vfe_enable(line);
1435 dev_err(vfe->camss->dev,
1436 "Failed to enable vfe outputs\n");
1438 ret = vfe_disable(line);
1440 dev_err(vfe->camss->dev,
1441 "Failed to disable vfe outputs\n");
1448 * __vfe_get_format - Get pointer to format structure
1450 * @cfg: V4L2 subdev pad configuration
1451 * @pad: pad from which format is requested
1452 * @which: TRY or ACTIVE format
1454 * Return pointer to TRY or ACTIVE format structure
1456 static struct v4l2_mbus_framefmt *
1457 __vfe_get_format(struct vfe_line *line,
1458 struct v4l2_subdev_pad_config *cfg,
1460 enum v4l2_subdev_format_whence which)
1462 if (which == V4L2_SUBDEV_FORMAT_TRY)
1463 return v4l2_subdev_get_try_format(&line->subdev, cfg, pad);
1465 return &line->fmt[pad];
1469 * __vfe_get_compose - Get pointer to compose selection structure
1471 * @cfg: V4L2 subdev pad configuration
1472 * @which: TRY or ACTIVE format
1474 * Return pointer to TRY or ACTIVE compose rectangle structure
1476 static struct v4l2_rect *
1477 __vfe_get_compose(struct vfe_line *line,
1478 struct v4l2_subdev_pad_config *cfg,
1479 enum v4l2_subdev_format_whence which)
1481 if (which == V4L2_SUBDEV_FORMAT_TRY)
1482 return v4l2_subdev_get_try_compose(&line->subdev, cfg,
1485 return &line->compose;
1489 * __vfe_get_crop - Get pointer to crop selection structure
1491 * @cfg: V4L2 subdev pad configuration
1492 * @which: TRY or ACTIVE format
1494 * Return pointer to TRY or ACTIVE crop rectangle structure
1496 static struct v4l2_rect *
1497 __vfe_get_crop(struct vfe_line *line,
1498 struct v4l2_subdev_pad_config *cfg,
1499 enum v4l2_subdev_format_whence which)
1501 if (which == V4L2_SUBDEV_FORMAT_TRY)
1502 return v4l2_subdev_get_try_crop(&line->subdev, cfg,
1509 * vfe_try_format - Handle try format by pad subdev method
1511 * @cfg: V4L2 subdev pad configuration
1512 * @pad: pad on which format is requested
1513 * @fmt: pointer to v4l2 format structure
1514 * @which: wanted subdev format
1516 static void vfe_try_format(struct vfe_line *line,
1517 struct v4l2_subdev_pad_config *cfg,
1519 struct v4l2_mbus_framefmt *fmt,
1520 enum v4l2_subdev_format_whence which)
1526 case MSM_VFE_PAD_SINK:
1527 /* Set format on sink pad */
1529 for (i = 0; i < line->nformats; i++)
1530 if (fmt->code == line->formats[i].code)
1533 /* If not found, use UYVY as default */
1534 if (i >= line->nformats)
1535 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
1537 fmt->width = clamp_t(u32, fmt->width, 1, 8191);
1538 fmt->height = clamp_t(u32, fmt->height, 1, 8191);
1540 fmt->field = V4L2_FIELD_NONE;
1541 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1545 case MSM_VFE_PAD_SRC:
1546 /* Set and return a format same as sink pad */
1549 *fmt = *__vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, which);
1551 fmt->code = vfe_src_pad_code(line, fmt->code, 0, code);
1553 if (line->id == VFE_LINE_PIX) {
1554 struct v4l2_rect *rect;
1556 rect = __vfe_get_crop(line, cfg, which);
1558 fmt->width = rect->width;
1559 fmt->height = rect->height;
1565 fmt->colorspace = V4L2_COLORSPACE_SRGB;
1569 * vfe_try_compose - Handle try compose selection by pad subdev method
1571 * @cfg: V4L2 subdev pad configuration
1572 * @rect: pointer to v4l2 rect structure
1573 * @which: wanted subdev format
1575 static void vfe_try_compose(struct vfe_line *line,
1576 struct v4l2_subdev_pad_config *cfg,
1577 struct v4l2_rect *rect,
1578 enum v4l2_subdev_format_whence which)
1580 struct v4l2_mbus_framefmt *fmt;
1582 fmt = __vfe_get_format(line, cfg, MSM_VFE_PAD_SINK, which);
1584 if (rect->width > fmt->width)
1585 rect->width = fmt->width;
1587 if (rect->height > fmt->height)
1588 rect->height = fmt->height;
1590 if (fmt->width > rect->width * SCALER_RATIO_MAX)
1591 rect->width = (fmt->width + SCALER_RATIO_MAX - 1) /
1594 rect->width &= ~0x1;
1596 if (fmt->height > rect->height * SCALER_RATIO_MAX)
1597 rect->height = (fmt->height + SCALER_RATIO_MAX - 1) /
1600 if (rect->width < 16)
1603 if (rect->height < 4)
1608 * vfe_try_crop - Handle try crop selection by pad subdev method
1610 * @cfg: V4L2 subdev pad configuration
1611 * @rect: pointer to v4l2 rect structure
1612 * @which: wanted subdev format
1614 static void vfe_try_crop(struct vfe_line *line,
1615 struct v4l2_subdev_pad_config *cfg,
1616 struct v4l2_rect *rect,
1617 enum v4l2_subdev_format_whence which)
1619 struct v4l2_rect *compose;
1621 compose = __vfe_get_compose(line, cfg, which);
1623 if (rect->width > compose->width)
1624 rect->width = compose->width;
1626 if (rect->width + rect->left > compose->width)
1627 rect->left = compose->width - rect->width;
1629 if (rect->height > compose->height)
1630 rect->height = compose->height;
1632 if (rect->height + rect->top > compose->height)
1633 rect->top = compose->height - rect->height;
1635 /* wm in line based mode writes multiple of 16 horizontally */
1636 rect->left += (rect->width & 0xf) >> 1;
1637 rect->width &= ~0xf;
1639 if (rect->width < 16) {
1644 if (rect->height < 4) {
1651 * vfe_enum_mbus_code - Handle pixel format enumeration
1652 * @sd: VFE V4L2 subdevice
1653 * @cfg: V4L2 subdev pad configuration
1654 * @code: pointer to v4l2_subdev_mbus_code_enum structure
1656 * return -EINVAL or zero on success
1658 static int vfe_enum_mbus_code(struct v4l2_subdev *sd,
1659 struct v4l2_subdev_pad_config *cfg,
1660 struct v4l2_subdev_mbus_code_enum *code)
1662 struct vfe_line *line = v4l2_get_subdevdata(sd);
1664 if (code->pad == MSM_VFE_PAD_SINK) {
1665 if (code->index >= line->nformats)
1668 code->code = line->formats[code->index].code;
1670 struct v4l2_mbus_framefmt *sink_fmt;
1672 sink_fmt = __vfe_get_format(line, cfg, MSM_VFE_PAD_SINK,
1675 code->code = vfe_src_pad_code(line, sink_fmt->code,
1685 * vfe_enum_frame_size - Handle frame size enumeration
1686 * @sd: VFE V4L2 subdevice
1687 * @cfg: V4L2 subdev pad configuration
1688 * @fse: pointer to v4l2_subdev_frame_size_enum structure
1690 * Return -EINVAL or zero on success
1692 static int vfe_enum_frame_size(struct v4l2_subdev *sd,
1693 struct v4l2_subdev_pad_config *cfg,
1694 struct v4l2_subdev_frame_size_enum *fse)
1696 struct vfe_line *line = v4l2_get_subdevdata(sd);
1697 struct v4l2_mbus_framefmt format;
1699 if (fse->index != 0)
1702 format.code = fse->code;
1705 vfe_try_format(line, cfg, fse->pad, &format, fse->which);
1706 fse->min_width = format.width;
1707 fse->min_height = format.height;
1709 if (format.code != fse->code)
1712 format.code = fse->code;
1715 vfe_try_format(line, cfg, fse->pad, &format, fse->which);
1716 fse->max_width = format.width;
1717 fse->max_height = format.height;
1723 * vfe_get_format - Handle get format by pads subdev method
1724 * @sd: VFE V4L2 subdevice
1725 * @cfg: V4L2 subdev pad configuration
1726 * @fmt: pointer to v4l2 subdev format structure
1728 * Return -EINVAL or zero on success
1730 static int vfe_get_format(struct v4l2_subdev *sd,
1731 struct v4l2_subdev_pad_config *cfg,
1732 struct v4l2_subdev_format *fmt)
1734 struct vfe_line *line = v4l2_get_subdevdata(sd);
1735 struct v4l2_mbus_framefmt *format;
1737 format = __vfe_get_format(line, cfg, fmt->pad, fmt->which);
1741 fmt->format = *format;
1746 static int vfe_set_selection(struct v4l2_subdev *sd,
1747 struct v4l2_subdev_pad_config *cfg,
1748 struct v4l2_subdev_selection *sel);
1751 * vfe_set_format - Handle set format by pads subdev method
1752 * @sd: VFE V4L2 subdevice
1753 * @cfg: V4L2 subdev pad configuration
1754 * @fmt: pointer to v4l2 subdev format structure
1756 * Return -EINVAL or zero on success
1758 static int vfe_set_format(struct v4l2_subdev *sd,
1759 struct v4l2_subdev_pad_config *cfg,
1760 struct v4l2_subdev_format *fmt)
1762 struct vfe_line *line = v4l2_get_subdevdata(sd);
1763 struct v4l2_mbus_framefmt *format;
1765 format = __vfe_get_format(line, cfg, fmt->pad, fmt->which);
1769 vfe_try_format(line, cfg, fmt->pad, &fmt->format, fmt->which);
1770 *format = fmt->format;
1772 if (fmt->pad == MSM_VFE_PAD_SINK) {
1773 struct v4l2_subdev_selection sel = { 0 };
1776 /* Propagate the format from sink to source */
1777 format = __vfe_get_format(line, cfg, MSM_VFE_PAD_SRC,
1780 *format = fmt->format;
1781 vfe_try_format(line, cfg, MSM_VFE_PAD_SRC, format,
1784 if (line->id != VFE_LINE_PIX)
1787 /* Reset sink pad compose selection */
1788 sel.which = fmt->which;
1789 sel.pad = MSM_VFE_PAD_SINK;
1790 sel.target = V4L2_SEL_TGT_COMPOSE;
1791 sel.r.width = fmt->format.width;
1792 sel.r.height = fmt->format.height;
1793 ret = vfe_set_selection(sd, cfg, &sel);
1802 * vfe_get_selection - Handle get selection by pads subdev method
1803 * @sd: VFE V4L2 subdevice
1804 * @cfg: V4L2 subdev pad configuration
1805 * @sel: pointer to v4l2 subdev selection structure
1807 * Return -EINVAL or zero on success
1809 static int vfe_get_selection(struct v4l2_subdev *sd,
1810 struct v4l2_subdev_pad_config *cfg,
1811 struct v4l2_subdev_selection *sel)
1813 struct vfe_line *line = v4l2_get_subdevdata(sd);
1814 struct v4l2_subdev_format fmt = { 0 };
1815 struct v4l2_rect *rect;
1818 if (line->id != VFE_LINE_PIX)
1821 if (sel->pad == MSM_VFE_PAD_SINK)
1822 switch (sel->target) {
1823 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1825 fmt.which = sel->which;
1826 ret = vfe_get_format(sd, cfg, &fmt);
1832 sel->r.width = fmt.format.width;
1833 sel->r.height = fmt.format.height;
1835 case V4L2_SEL_TGT_COMPOSE:
1836 rect = __vfe_get_compose(line, cfg, sel->which);
1845 else if (sel->pad == MSM_VFE_PAD_SRC)
1846 switch (sel->target) {
1847 case V4L2_SEL_TGT_CROP_BOUNDS:
1848 rect = __vfe_get_compose(line, cfg, sel->which);
1852 sel->r.left = rect->left;
1853 sel->r.top = rect->top;
1854 sel->r.width = rect->width;
1855 sel->r.height = rect->height;
1857 case V4L2_SEL_TGT_CROP:
1858 rect = __vfe_get_crop(line, cfg, sel->which);
1872 * vfe_set_selection - Handle set selection by pads subdev method
1873 * @sd: VFE V4L2 subdevice
1874 * @cfg: V4L2 subdev pad configuration
1875 * @sel: pointer to v4l2 subdev selection structure
1877 * Return -EINVAL or zero on success
1879 static int vfe_set_selection(struct v4l2_subdev *sd,
1880 struct v4l2_subdev_pad_config *cfg,
1881 struct v4l2_subdev_selection *sel)
1883 struct vfe_line *line = v4l2_get_subdevdata(sd);
1884 struct v4l2_rect *rect;
1887 if (line->id != VFE_LINE_PIX)
1890 if (sel->target == V4L2_SEL_TGT_COMPOSE &&
1891 sel->pad == MSM_VFE_PAD_SINK) {
1892 struct v4l2_subdev_selection crop = { 0 };
1894 rect = __vfe_get_compose(line, cfg, sel->which);
1898 vfe_try_compose(line, cfg, &sel->r, sel->which);
1901 /* Reset source crop selection */
1902 crop.which = sel->which;
1903 crop.pad = MSM_VFE_PAD_SRC;
1904 crop.target = V4L2_SEL_TGT_CROP;
1906 ret = vfe_set_selection(sd, cfg, &crop);
1907 } else if (sel->target == V4L2_SEL_TGT_CROP &&
1908 sel->pad == MSM_VFE_PAD_SRC) {
1909 struct v4l2_subdev_format fmt = { 0 };
1911 rect = __vfe_get_crop(line, cfg, sel->which);
1915 vfe_try_crop(line, cfg, &sel->r, sel->which);
1918 /* Reset source pad format width and height */
1919 fmt.which = sel->which;
1920 fmt.pad = MSM_VFE_PAD_SRC;
1921 ret = vfe_get_format(sd, cfg, &fmt);
1925 fmt.format.width = rect->width;
1926 fmt.format.height = rect->height;
1927 ret = vfe_set_format(sd, cfg, &fmt);
1936 * vfe_init_formats - Initialize formats on all pads
1937 * @sd: VFE V4L2 subdevice
1938 * @fh: V4L2 subdev file handle
1940 * Initialize all pad formats with default values.
1942 * Return 0 on success or a negative error code otherwise
1944 static int vfe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1946 struct v4l2_subdev_format format = {
1947 .pad = MSM_VFE_PAD_SINK,
1948 .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
1949 V4L2_SUBDEV_FORMAT_ACTIVE,
1951 .code = MEDIA_BUS_FMT_UYVY8_2X8,
1957 return vfe_set_format(sd, fh ? fh->pad : NULL, &format);
1961 * msm_vfe_subdev_init - Initialize VFE device structure and resources
1963 * @res: VFE module resources table
1965 * Return 0 on success or a negative error code otherwise
1967 int msm_vfe_subdev_init(struct camss *camss, struct vfe_device *vfe,
1968 const struct resources *res, u8 id)
1970 struct device *dev = camss->dev;
1971 struct platform_device *pdev = to_platform_device(dev);
1976 vfe->isr_ops.reset_ack = vfe_isr_reset_ack;
1977 vfe->isr_ops.halt_ack = vfe_isr_halt_ack;
1978 vfe->isr_ops.reg_update = vfe_isr_reg_update;
1979 vfe->isr_ops.sof = vfe_isr_sof;
1980 vfe->isr_ops.comp_done = vfe_isr_comp_done;
1981 vfe->isr_ops.wm_done = vfe_isr_wm_done;
1983 if (camss->version == CAMSS_8x16)
1984 vfe->ops = &vfe_ops_4_1;
1985 else if (camss->version == CAMSS_8x96)
1986 vfe->ops = &vfe_ops_4_7;
1992 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res->reg[0]);
1993 vfe->base = devm_ioremap_resource(dev, r);
1994 if (IS_ERR(vfe->base)) {
1995 dev_err(dev, "could not map memory\n");
1996 return PTR_ERR(vfe->base);
2001 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2004 dev_err(dev, "missing IRQ\n");
2008 vfe->irq = r->start;
2009 snprintf(vfe->irq_name, sizeof(vfe->irq_name), "%s_%s%d",
2010 dev_name(dev), MSM_VFE_NAME, vfe->id);
2011 ret = devm_request_irq(dev, vfe->irq, vfe->ops->isr,
2012 IRQF_TRIGGER_RISING, vfe->irq_name, vfe);
2014 dev_err(dev, "request_irq failed: %d\n", ret);
2021 while (res->clock[vfe->nclocks])
2024 vfe->clock = devm_kcalloc(dev, vfe->nclocks, sizeof(*vfe->clock),
2029 for (i = 0; i < vfe->nclocks; i++) {
2030 struct camss_clock *clock = &vfe->clock[i];
2032 clock->clk = devm_clk_get(dev, res->clock[i]);
2033 if (IS_ERR(clock->clk))
2034 return PTR_ERR(clock->clk);
2036 clock->name = res->clock[i];
2039 while (res->clock_rate[i][clock->nfreqs])
2042 if (!clock->nfreqs) {
2047 clock->freq = devm_kcalloc(dev,
2049 sizeof(*clock->freq),
2054 for (j = 0; j < clock->nfreqs; j++)
2055 clock->freq[j] = res->clock_rate[i][j];
2058 mutex_init(&vfe->power_lock);
2059 vfe->power_count = 0;
2061 mutex_init(&vfe->stream_lock);
2062 vfe->stream_count = 0;
2064 spin_lock_init(&vfe->output_lock);
2068 vfe->reg_update = 0;
2070 for (i = VFE_LINE_RDI0; i <= VFE_LINE_PIX; i++) {
2071 struct vfe_line *l = &vfe->line[i];
2073 l->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
2074 l->video_out.camss = camss;
2076 init_completion(&l->output.sof);
2077 init_completion(&l->output.reg_update);
2079 if (camss->version == CAMSS_8x16) {
2080 if (i == VFE_LINE_PIX) {
2081 l->formats = formats_pix_8x16;
2082 l->nformats = ARRAY_SIZE(formats_pix_8x16);
2084 l->formats = formats_rdi_8x16;
2085 l->nformats = ARRAY_SIZE(formats_rdi_8x16);
2087 } else if (camss->version == CAMSS_8x96) {
2088 if (i == VFE_LINE_PIX) {
2089 l->formats = formats_pix_8x96;
2090 l->nformats = ARRAY_SIZE(formats_pix_8x96);
2092 l->formats = formats_rdi_8x96;
2093 l->nformats = ARRAY_SIZE(formats_rdi_8x96);
2100 init_completion(&vfe->reset_complete);
2101 init_completion(&vfe->halt_complete);
2107 * msm_vfe_get_vfe_id - Get VFE HW module id
2108 * @entity: Pointer to VFE media entity structure
2109 * @id: Return CSID HW module id here
2111 void msm_vfe_get_vfe_id(struct media_entity *entity, u8 *id)
2113 struct v4l2_subdev *sd;
2114 struct vfe_line *line;
2115 struct vfe_device *vfe;
2117 sd = media_entity_to_v4l2_subdev(entity);
2118 line = v4l2_get_subdevdata(sd);
2125 * msm_vfe_get_vfe_line_id - Get VFE line id by media entity
2126 * @entity: Pointer to VFE media entity structure
2127 * @id: Return VFE line id here
2129 void msm_vfe_get_vfe_line_id(struct media_entity *entity, enum vfe_line_id *id)
2131 struct v4l2_subdev *sd;
2132 struct vfe_line *line;
2134 sd = media_entity_to_v4l2_subdev(entity);
2135 line = v4l2_get_subdevdata(sd);
2141 * vfe_link_setup - Setup VFE connections
2142 * @entity: Pointer to media entity structure
2143 * @local: Pointer to local pad
2144 * @remote: Pointer to remote pad
2145 * @flags: Link flags
2147 * Return 0 on success
2149 static int vfe_link_setup(struct media_entity *entity,
2150 const struct media_pad *local,
2151 const struct media_pad *remote, u32 flags)
2153 if (flags & MEDIA_LNK_FL_ENABLED)
2154 if (media_entity_remote_pad(local))
2160 static const struct v4l2_subdev_core_ops vfe_core_ops = {
2161 .s_power = vfe_set_power,
2164 static const struct v4l2_subdev_video_ops vfe_video_ops = {
2165 .s_stream = vfe_set_stream,
2168 static const struct v4l2_subdev_pad_ops vfe_pad_ops = {
2169 .enum_mbus_code = vfe_enum_mbus_code,
2170 .enum_frame_size = vfe_enum_frame_size,
2171 .get_fmt = vfe_get_format,
2172 .set_fmt = vfe_set_format,
2173 .get_selection = vfe_get_selection,
2174 .set_selection = vfe_set_selection,
2177 static const struct v4l2_subdev_ops vfe_v4l2_ops = {
2178 .core = &vfe_core_ops,
2179 .video = &vfe_video_ops,
2180 .pad = &vfe_pad_ops,
2183 static const struct v4l2_subdev_internal_ops vfe_v4l2_internal_ops = {
2184 .open = vfe_init_formats,
2187 static const struct media_entity_operations vfe_media_ops = {
2188 .link_setup = vfe_link_setup,
2189 .link_validate = v4l2_subdev_link_validate,
2192 static const struct camss_video_ops camss_vfe_video_ops = {
2193 .queue_buffer = vfe_queue_buffer,
2194 .flush_buffers = vfe_flush_buffers,
2197 void msm_vfe_stop_streaming(struct vfe_device *vfe)
2201 for (i = 0; i < ARRAY_SIZE(vfe->line); i++)
2202 msm_video_stop_streaming(&vfe->line[i].video_out);
2206 * msm_vfe_register_entities - Register subdev node for VFE module
2208 * @v4l2_dev: V4L2 device
2210 * Initialize and register a subdev node for the VFE module. Then
2211 * call msm_video_register() to register the video device node which
2212 * will be connected to this subdev node. Then actually create the
2213 * media link between them.
2215 * Return 0 on success or a negative error code otherwise
2217 int msm_vfe_register_entities(struct vfe_device *vfe,
2218 struct v4l2_device *v4l2_dev)
2220 struct device *dev = vfe->camss->dev;
2221 struct v4l2_subdev *sd;
2222 struct media_pad *pads;
2223 struct camss_video *video_out;
2227 for (i = 0; i < ARRAY_SIZE(vfe->line); i++) {
2230 sd = &vfe->line[i].subdev;
2231 pads = vfe->line[i].pads;
2232 video_out = &vfe->line[i].video_out;
2234 v4l2_subdev_init(sd, &vfe_v4l2_ops);
2235 sd->internal_ops = &vfe_v4l2_internal_ops;
2236 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2237 if (i == VFE_LINE_PIX)
2238 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s",
2239 MSM_VFE_NAME, vfe->id, "pix");
2241 snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s%d",
2242 MSM_VFE_NAME, vfe->id, "rdi", i);
2244 v4l2_set_subdevdata(sd, &vfe->line[i]);
2246 ret = vfe_init_formats(sd, NULL);
2248 dev_err(dev, "Failed to init format: %d\n", ret);
2252 pads[MSM_VFE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
2253 pads[MSM_VFE_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
2255 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
2256 sd->entity.ops = &vfe_media_ops;
2257 ret = media_entity_pads_init(&sd->entity, MSM_VFE_PADS_NUM,
2260 dev_err(dev, "Failed to init media entity: %d\n", ret);
2264 ret = v4l2_device_register_subdev(v4l2_dev, sd);
2266 dev_err(dev, "Failed to register subdev: %d\n", ret);
2267 goto error_reg_subdev;
2270 video_out->ops = &camss_vfe_video_ops;
2271 video_out->bpl_alignment = 8;
2272 video_out->line_based = 0;
2273 if (i == VFE_LINE_PIX) {
2274 video_out->bpl_alignment = 16;
2275 video_out->line_based = 1;
2277 snprintf(name, ARRAY_SIZE(name), "%s%d_%s%d",
2278 MSM_VFE_NAME, vfe->id, "video", i);
2279 ret = msm_video_register(video_out, v4l2_dev, name,
2280 i == VFE_LINE_PIX ? 1 : 0);
2282 dev_err(dev, "Failed to register video node: %d\n",
2284 goto error_reg_video;
2287 ret = media_create_pad_link(
2288 &sd->entity, MSM_VFE_PAD_SRC,
2289 &video_out->vdev.entity, 0,
2290 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
2292 dev_err(dev, "Failed to link %s->%s entities: %d\n",
2293 sd->entity.name, video_out->vdev.entity.name,
2302 msm_video_unregister(video_out);
2305 v4l2_device_unregister_subdev(sd);
2308 media_entity_cleanup(&sd->entity);
2311 for (i--; i >= 0; i--) {
2312 sd = &vfe->line[i].subdev;
2313 video_out = &vfe->line[i].video_out;
2315 msm_video_unregister(video_out);
2316 v4l2_device_unregister_subdev(sd);
2317 media_entity_cleanup(&sd->entity);
2324 * msm_vfe_unregister_entities - Unregister VFE module subdev node
2327 void msm_vfe_unregister_entities(struct vfe_device *vfe)
2331 mutex_destroy(&vfe->power_lock);
2332 mutex_destroy(&vfe->stream_lock);
2334 for (i = 0; i < ARRAY_SIZE(vfe->line); i++) {
2335 struct v4l2_subdev *sd = &vfe->line[i].subdev;
2336 struct camss_video *video_out = &vfe->line[i].video_out;
2338 msm_video_unregister(video_out);
2339 v4l2_device_unregister_subdev(sd);
2340 media_entity_cleanup(&sd->entity);