1b873663bSTodor Tomov // SPDX-License-Identifier: GPL-2.0
2ec6859b2STodor Tomov /*
3ec6859b2STodor Tomov * camss-vfe.c
4ec6859b2STodor Tomov *
5ec6859b2STodor Tomov * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module
6ec6859b2STodor Tomov *
7ec6859b2STodor Tomov * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
8ec6859b2STodor Tomov * Copyright (C) 2015-2018 Linaro Ltd.
9ec6859b2STodor Tomov */
10ec6859b2STodor Tomov #include <linux/clk.h>
11ec6859b2STodor Tomov #include <linux/completion.h>
12ec6859b2STodor Tomov #include <linux/interrupt.h>
13ec6859b2STodor Tomov #include <linux/iommu.h>
14ec6859b2STodor Tomov #include <linux/mutex.h>
15ec6859b2STodor Tomov #include <linux/of.h>
16ec6859b2STodor Tomov #include <linux/platform_device.h>
1702afa816STodor Tomov #include <linux/pm_runtime.h>
18ec6859b2STodor Tomov #include <linux/spinlock_types.h>
19ec6859b2STodor Tomov #include <linux/spinlock.h>
20ec6859b2STodor Tomov #include <media/media-entity.h>
21ec6859b2STodor Tomov #include <media/v4l2-device.h>
22ec6859b2STodor Tomov #include <media/v4l2-subdev.h>
23ec6859b2STodor Tomov
24ec6859b2STodor Tomov #include "camss-vfe.h"
25ec6859b2STodor Tomov #include "camss.h"
26ec6859b2STodor Tomov
27ec6859b2STodor Tomov #define MSM_VFE_NAME "msm_vfe"
28ec6859b2STodor Tomov
29ec6859b2STodor Tomov /* VFE reset timeout */
30ec6859b2STodor Tomov #define VFE_RESET_TIMEOUT_MS 50
31ec6859b2STodor Tomov
32ec6859b2STodor Tomov #define SCALER_RATIO_MAX 16
33ec6859b2STodor Tomov
34cba3819dSTodor Tomov struct vfe_format {
35ec6859b2STodor Tomov u32 code;
36ec6859b2STodor Tomov u8 bpp;
37cba3819dSTodor Tomov };
38cba3819dSTodor Tomov
39cba3819dSTodor Tomov static const struct vfe_format formats_rdi_8x16[] = {
40cba3819dSTodor Tomov { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
41cba3819dSTodor Tomov { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
42cba3819dSTodor Tomov { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
43cba3819dSTodor Tomov { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
44cba3819dSTodor Tomov { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
45cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
46cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
47cba3819dSTodor Tomov { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
48cba3819dSTodor Tomov { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
49cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
50cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
51cba3819dSTodor Tomov { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
52cba3819dSTodor Tomov { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
53cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
54cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
55cba3819dSTodor Tomov { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
56cc8fe073STodor Tomov { MEDIA_BUS_FMT_Y10_1X10, 10 },
57cba3819dSTodor Tomov };
58cba3819dSTodor Tomov
59cba3819dSTodor Tomov static const struct vfe_format formats_pix_8x16[] = {
60cba3819dSTodor Tomov { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
61cba3819dSTodor Tomov { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
62cba3819dSTodor Tomov { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
63cba3819dSTodor Tomov { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
64cba3819dSTodor Tomov };
65cba3819dSTodor Tomov
66cba3819dSTodor Tomov static const struct vfe_format formats_rdi_8x96[] = {
67cba3819dSTodor Tomov { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
68cba3819dSTodor Tomov { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
69cba3819dSTodor Tomov { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
70cba3819dSTodor Tomov { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
71cba3819dSTodor Tomov { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
72cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
73cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
74cba3819dSTodor Tomov { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
75cba3819dSTodor Tomov { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
76cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
77cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
78cba3819dSTodor Tomov { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
795019d7c8STodor Tomov { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 },
80cba3819dSTodor Tomov { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
81cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
82cba3819dSTodor Tomov { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
83cba3819dSTodor Tomov { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
84f476fb56STodor Tomov { MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
85f476fb56STodor Tomov { MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
86f476fb56STodor Tomov { MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
87f476fb56STodor Tomov { MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
88cc8fe073STodor Tomov { MEDIA_BUS_FMT_Y10_1X10, 10 },
89cc8fe073STodor Tomov { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 },
90cba3819dSTodor Tomov };
91cba3819dSTodor Tomov
92cba3819dSTodor Tomov static const struct vfe_format formats_pix_8x96[] = {
93cba3819dSTodor Tomov { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
94cba3819dSTodor Tomov { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
95cba3819dSTodor Tomov { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
96cba3819dSTodor Tomov { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
97ec6859b2STodor Tomov };
98ec6859b2STodor Tomov
997319cdf1SRobert Foss static const struct vfe_format formats_rdi_845[] = {
1007319cdf1SRobert Foss { MEDIA_BUS_FMT_UYVY8_2X8, 8 },
1017319cdf1SRobert Foss { MEDIA_BUS_FMT_VYUY8_2X8, 8 },
1027319cdf1SRobert Foss { MEDIA_BUS_FMT_YUYV8_2X8, 8 },
1037319cdf1SRobert Foss { MEDIA_BUS_FMT_YVYU8_2X8, 8 },
1047319cdf1SRobert Foss { MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
1057319cdf1SRobert Foss { MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
1067319cdf1SRobert Foss { MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
1077319cdf1SRobert Foss { MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
1087319cdf1SRobert Foss { MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
1097319cdf1SRobert Foss { MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
1107319cdf1SRobert Foss { MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
1117319cdf1SRobert Foss { MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
1127319cdf1SRobert Foss { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 },
1137319cdf1SRobert Foss { MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
1147319cdf1SRobert Foss { MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
1157319cdf1SRobert Foss { MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
1167319cdf1SRobert Foss { MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
1177319cdf1SRobert Foss { MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
1187319cdf1SRobert Foss { MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
1197319cdf1SRobert Foss { MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
1207319cdf1SRobert Foss { MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
121e53d6608SJonathan Marek { MEDIA_BUS_FMT_Y8_1X8, 8 },
1227319cdf1SRobert Foss { MEDIA_BUS_FMT_Y10_1X10, 10 },
1237319cdf1SRobert Foss { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 },
1247319cdf1SRobert Foss };
1257319cdf1SRobert Foss
126ec6859b2STodor Tomov /*
127ec6859b2STodor Tomov * vfe_get_bpp - map media bus format to bits per pixel
128cba3819dSTodor Tomov * @formats: supported media bus formats array
129cba3819dSTodor Tomov * @nformats: size of @formats array
130ec6859b2STodor Tomov * @code: media bus format code
131ec6859b2STodor Tomov *
132ec6859b2STodor Tomov * Return number of bits per pixel
133ec6859b2STodor Tomov */
vfe_get_bpp(const struct vfe_format * formats,unsigned int nformats,u32 code)134cba3819dSTodor Tomov static u8 vfe_get_bpp(const struct vfe_format *formats,
135cba3819dSTodor Tomov unsigned int nformats, u32 code)
136ec6859b2STodor Tomov {
137ec6859b2STodor Tomov unsigned int i;
138ec6859b2STodor Tomov
139cba3819dSTodor Tomov for (i = 0; i < nformats; i++)
140cba3819dSTodor Tomov if (code == formats[i].code)
141cba3819dSTodor Tomov return formats[i].bpp;
142ec6859b2STodor Tomov
143ec6859b2STodor Tomov WARN(1, "Unknown format\n");
144ec6859b2STodor Tomov
145cba3819dSTodor Tomov return formats[0].bpp;
146ec6859b2STodor Tomov }
147ec6859b2STodor Tomov
vfe_find_code(u32 * code,unsigned int n_code,unsigned int index,u32 req_code)14807eeb342STodor Tomov static u32 vfe_find_code(u32 *code, unsigned int n_code,
14907eeb342STodor Tomov unsigned int index, u32 req_code)
15007eeb342STodor Tomov {
15107eeb342STodor Tomov int i;
15207eeb342STodor Tomov
15307eeb342STodor Tomov if (!req_code && (index >= n_code))
15407eeb342STodor Tomov return 0;
15507eeb342STodor Tomov
15607eeb342STodor Tomov for (i = 0; i < n_code; i++)
15707eeb342STodor Tomov if (req_code) {
15807eeb342STodor Tomov if (req_code == code[i])
15907eeb342STodor Tomov return req_code;
16007eeb342STodor Tomov } else {
16107eeb342STodor Tomov if (i == index)
16207eeb342STodor Tomov return code[i];
16307eeb342STodor Tomov }
16407eeb342STodor Tomov
16507eeb342STodor Tomov return code[0];
16607eeb342STodor Tomov }
16707eeb342STodor Tomov
vfe_src_pad_code(struct vfe_line * line,u32 sink_code,unsigned int index,u32 src_req_code)16807eeb342STodor Tomov static u32 vfe_src_pad_code(struct vfe_line *line, u32 sink_code,
16907eeb342STodor Tomov unsigned int index, u32 src_req_code)
17007eeb342STodor Tomov {
17107eeb342STodor Tomov struct vfe_device *vfe = to_vfe(line);
17207eeb342STodor Tomov
17307eeb342STodor Tomov if (vfe->camss->version == CAMSS_8x16)
17407eeb342STodor Tomov switch (sink_code) {
17507eeb342STodor Tomov case MEDIA_BUS_FMT_YUYV8_2X8:
17607eeb342STodor Tomov {
17707eeb342STodor Tomov u32 src_code[] = {
17807eeb342STodor Tomov MEDIA_BUS_FMT_YUYV8_2X8,
17907eeb342STodor Tomov MEDIA_BUS_FMT_YUYV8_1_5X8,
18007eeb342STodor Tomov };
18107eeb342STodor Tomov
18207eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
18307eeb342STodor Tomov index, src_req_code);
18407eeb342STodor Tomov }
18507eeb342STodor Tomov case MEDIA_BUS_FMT_YVYU8_2X8:
18607eeb342STodor Tomov {
18707eeb342STodor Tomov u32 src_code[] = {
18807eeb342STodor Tomov MEDIA_BUS_FMT_YVYU8_2X8,
18907eeb342STodor Tomov MEDIA_BUS_FMT_YVYU8_1_5X8,
19007eeb342STodor Tomov };
19107eeb342STodor Tomov
19207eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
19307eeb342STodor Tomov index, src_req_code);
19407eeb342STodor Tomov }
19507eeb342STodor Tomov case MEDIA_BUS_FMT_UYVY8_2X8:
19607eeb342STodor Tomov {
19707eeb342STodor Tomov u32 src_code[] = {
19807eeb342STodor Tomov MEDIA_BUS_FMT_UYVY8_2X8,
19907eeb342STodor Tomov MEDIA_BUS_FMT_UYVY8_1_5X8,
20007eeb342STodor Tomov };
20107eeb342STodor Tomov
20207eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
20307eeb342STodor Tomov index, src_req_code);
20407eeb342STodor Tomov }
20507eeb342STodor Tomov case MEDIA_BUS_FMT_VYUY8_2X8:
20607eeb342STodor Tomov {
20707eeb342STodor Tomov u32 src_code[] = {
20807eeb342STodor Tomov MEDIA_BUS_FMT_VYUY8_2X8,
20907eeb342STodor Tomov MEDIA_BUS_FMT_VYUY8_1_5X8,
21007eeb342STodor Tomov };
21107eeb342STodor Tomov
21207eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
21307eeb342STodor Tomov index, src_req_code);
21407eeb342STodor Tomov }
21507eeb342STodor Tomov default:
21607eeb342STodor Tomov if (index > 0)
21707eeb342STodor Tomov return 0;
21807eeb342STodor Tomov
21907eeb342STodor Tomov return sink_code;
22007eeb342STodor Tomov }
2219e5d1581SAngeloGioacchino Del Regno else if (vfe->camss->version == CAMSS_8x96 ||
2227319cdf1SRobert Foss vfe->camss->version == CAMSS_660 ||
223b4436a18SJonathan Marek vfe->camss->version == CAMSS_845 ||
224b4436a18SJonathan Marek vfe->camss->version == CAMSS_8250)
22507eeb342STodor Tomov switch (sink_code) {
22607eeb342STodor Tomov case MEDIA_BUS_FMT_YUYV8_2X8:
22707eeb342STodor Tomov {
22807eeb342STodor Tomov u32 src_code[] = {
22907eeb342STodor Tomov MEDIA_BUS_FMT_YUYV8_2X8,
230312e1c85STodor Tomov MEDIA_BUS_FMT_YVYU8_2X8,
231312e1c85STodor Tomov MEDIA_BUS_FMT_UYVY8_2X8,
232312e1c85STodor Tomov MEDIA_BUS_FMT_VYUY8_2X8,
23307eeb342STodor Tomov MEDIA_BUS_FMT_YUYV8_1_5X8,
23407eeb342STodor Tomov };
23507eeb342STodor Tomov
23607eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
23707eeb342STodor Tomov index, src_req_code);
23807eeb342STodor Tomov }
23907eeb342STodor Tomov case MEDIA_BUS_FMT_YVYU8_2X8:
24007eeb342STodor Tomov {
24107eeb342STodor Tomov u32 src_code[] = {
24207eeb342STodor Tomov MEDIA_BUS_FMT_YVYU8_2X8,
243312e1c85STodor Tomov MEDIA_BUS_FMT_YUYV8_2X8,
244312e1c85STodor Tomov MEDIA_BUS_FMT_UYVY8_2X8,
245312e1c85STodor Tomov MEDIA_BUS_FMT_VYUY8_2X8,
24607eeb342STodor Tomov MEDIA_BUS_FMT_YVYU8_1_5X8,
24707eeb342STodor Tomov };
24807eeb342STodor Tomov
24907eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
25007eeb342STodor Tomov index, src_req_code);
25107eeb342STodor Tomov }
25207eeb342STodor Tomov case MEDIA_BUS_FMT_UYVY8_2X8:
25307eeb342STodor Tomov {
25407eeb342STodor Tomov u32 src_code[] = {
25507eeb342STodor Tomov MEDIA_BUS_FMT_UYVY8_2X8,
256312e1c85STodor Tomov MEDIA_BUS_FMT_YUYV8_2X8,
257312e1c85STodor Tomov MEDIA_BUS_FMT_YVYU8_2X8,
258312e1c85STodor Tomov MEDIA_BUS_FMT_VYUY8_2X8,
25907eeb342STodor Tomov MEDIA_BUS_FMT_UYVY8_1_5X8,
26007eeb342STodor Tomov };
26107eeb342STodor Tomov
26207eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
26307eeb342STodor Tomov index, src_req_code);
26407eeb342STodor Tomov }
26507eeb342STodor Tomov case MEDIA_BUS_FMT_VYUY8_2X8:
26607eeb342STodor Tomov {
26707eeb342STodor Tomov u32 src_code[] = {
26807eeb342STodor Tomov MEDIA_BUS_FMT_VYUY8_2X8,
269312e1c85STodor Tomov MEDIA_BUS_FMT_YUYV8_2X8,
270312e1c85STodor Tomov MEDIA_BUS_FMT_YVYU8_2X8,
271312e1c85STodor Tomov MEDIA_BUS_FMT_UYVY8_2X8,
27207eeb342STodor Tomov MEDIA_BUS_FMT_VYUY8_1_5X8,
27307eeb342STodor Tomov };
27407eeb342STodor Tomov
27507eeb342STodor Tomov return vfe_find_code(src_code, ARRAY_SIZE(src_code),
27607eeb342STodor Tomov index, src_req_code);
27707eeb342STodor Tomov }
27807eeb342STodor Tomov default:
27907eeb342STodor Tomov if (index > 0)
28007eeb342STodor Tomov return 0;
28107eeb342STodor Tomov
28207eeb342STodor Tomov return sink_code;
28307eeb342STodor Tomov }
28407eeb342STodor Tomov else
28507eeb342STodor Tomov return 0;
28607eeb342STodor Tomov }
28707eeb342STodor Tomov
vfe_reset(struct vfe_device * vfe)2887319cdf1SRobert Foss int vfe_reset(struct vfe_device *vfe)
289ec6859b2STodor Tomov {
290ec6859b2STodor Tomov unsigned long time;
291ec6859b2STodor Tomov
292ec6859b2STodor Tomov reinit_completion(&vfe->reset_complete);
293ec6859b2STodor Tomov
294051a01acSTodor Tomov vfe->ops->global_reset(vfe);
295ec6859b2STodor Tomov
296ec6859b2STodor Tomov time = wait_for_completion_timeout(&vfe->reset_complete,
297ec6859b2STodor Tomov msecs_to_jiffies(VFE_RESET_TIMEOUT_MS));
298ec6859b2STodor Tomov if (!time) {
2999c3e59deSTodor Tomov dev_err(vfe->camss->dev, "VFE reset timeout\n");
300ec6859b2STodor Tomov return -EIO;
301ec6859b2STodor Tomov }
302ec6859b2STodor Tomov
303ec6859b2STodor Tomov return 0;
304ec6859b2STodor Tomov }
305ec6859b2STodor Tomov
vfe_init_outputs(struct vfe_device * vfe)306ec6859b2STodor Tomov static void vfe_init_outputs(struct vfe_device *vfe)
307ec6859b2STodor Tomov {
308ec6859b2STodor Tomov int i;
309ec6859b2STodor Tomov
310633b388fSRobert Foss for (i = 0; i < vfe->line_num; i++) {
311ec6859b2STodor Tomov struct vfe_output *output = &vfe->line[i].output;
312ec6859b2STodor Tomov
313ec6859b2STodor Tomov output->state = VFE_OUTPUT_OFF;
314ec6859b2STodor Tomov output->buf[0] = NULL;
315ec6859b2STodor Tomov output->buf[1] = NULL;
316ec6859b2STodor Tomov INIT_LIST_HEAD(&output->pending_bufs);
317ec6859b2STodor Tomov }
318ec6859b2STodor Tomov }
319ec6859b2STodor Tomov
vfe_reset_output_maps(struct vfe_device * vfe)320ec6859b2STodor Tomov static void vfe_reset_output_maps(struct vfe_device *vfe)
321ec6859b2STodor Tomov {
322ec6859b2STodor Tomov int i;
323ec6859b2STodor Tomov
324ec6859b2STodor Tomov for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
325ec6859b2STodor Tomov vfe->wm_output_map[i] = VFE_LINE_NONE;
326ec6859b2STodor Tomov }
327ec6859b2STodor Tomov
vfe_reserve_wm(struct vfe_device * vfe,enum vfe_line_id line_id)328633b388fSRobert Foss int vfe_reserve_wm(struct vfe_device *vfe, enum vfe_line_id line_id)
329ec6859b2STodor Tomov {
330ec6859b2STodor Tomov int ret = -EBUSY;
331ec6859b2STodor Tomov int i;
332ec6859b2STodor Tomov
333ec6859b2STodor Tomov for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) {
334ec6859b2STodor Tomov if (vfe->wm_output_map[i] == VFE_LINE_NONE) {
335ec6859b2STodor Tomov vfe->wm_output_map[i] = line_id;
336ec6859b2STodor Tomov ret = i;
337ec6859b2STodor Tomov break;
338ec6859b2STodor Tomov }
339ec6859b2STodor Tomov }
340ec6859b2STodor Tomov
341ec6859b2STodor Tomov return ret;
342ec6859b2STodor Tomov }
343ec6859b2STodor Tomov
vfe_release_wm(struct vfe_device * vfe,u8 wm)344633b388fSRobert Foss int vfe_release_wm(struct vfe_device *vfe, u8 wm)
345ec6859b2STodor Tomov {
346ec6859b2STodor Tomov if (wm >= ARRAY_SIZE(vfe->wm_output_map))
347ec6859b2STodor Tomov return -EINVAL;
348ec6859b2STodor Tomov
349ec6859b2STodor Tomov vfe->wm_output_map[wm] = VFE_LINE_NONE;
350ec6859b2STodor Tomov
351ec6859b2STodor Tomov return 0;
352ec6859b2STodor Tomov }
353ec6859b2STodor Tomov
vfe_buf_get_pending(struct vfe_output * output)354633b388fSRobert Foss struct camss_buffer *vfe_buf_get_pending(struct vfe_output *output)
355ec6859b2STodor Tomov {
356ec6859b2STodor Tomov struct camss_buffer *buffer = NULL;
357ec6859b2STodor Tomov
358ec6859b2STodor Tomov if (!list_empty(&output->pending_bufs)) {
359ec6859b2STodor Tomov buffer = list_first_entry(&output->pending_bufs,
360ec6859b2STodor Tomov struct camss_buffer,
361ec6859b2STodor Tomov queue);
362ec6859b2STodor Tomov list_del(&buffer->queue);
363ec6859b2STodor Tomov }
364ec6859b2STodor Tomov
365ec6859b2STodor Tomov return buffer;
366ec6859b2STodor Tomov }
367ec6859b2STodor Tomov
vfe_buf_add_pending(struct vfe_output * output,struct camss_buffer * buffer)368633b388fSRobert Foss void vfe_buf_add_pending(struct vfe_output *output,
369ec6859b2STodor Tomov struct camss_buffer *buffer)
370ec6859b2STodor Tomov {
371ec6859b2STodor Tomov INIT_LIST_HEAD(&buffer->queue);
372ec6859b2STodor Tomov list_add_tail(&buffer->queue, &output->pending_bufs);
373ec6859b2STodor Tomov }
374ec6859b2STodor Tomov
375ec6859b2STodor Tomov /*
376ec6859b2STodor Tomov * vfe_buf_flush_pending - Flush all pending buffers.
377ec6859b2STodor Tomov * @output: VFE output
378ec6859b2STodor Tomov * @state: vb2 buffer state
379ec6859b2STodor Tomov */
vfe_buf_flush_pending(struct vfe_output * output,enum vb2_buffer_state state)380ec6859b2STodor Tomov static void vfe_buf_flush_pending(struct vfe_output *output,
381ec6859b2STodor Tomov enum vb2_buffer_state state)
382ec6859b2STodor Tomov {
383ec6859b2STodor Tomov struct camss_buffer *buf;
384ec6859b2STodor Tomov struct camss_buffer *t;
385ec6859b2STodor Tomov
386ec6859b2STodor Tomov list_for_each_entry_safe(buf, t, &output->pending_bufs, queue) {
387ec6859b2STodor Tomov vb2_buffer_done(&buf->vb.vb2_buf, state);
388ec6859b2STodor Tomov list_del(&buf->queue);
389ec6859b2STodor Tomov }
390ec6859b2STodor Tomov }
391ec6859b2STodor Tomov
vfe_put_output(struct vfe_line * line)392633b388fSRobert Foss int vfe_put_output(struct vfe_line *line)
393ec6859b2STodor Tomov {
394ec6859b2STodor Tomov struct vfe_device *vfe = to_vfe(line);
395ec6859b2STodor Tomov struct vfe_output *output = &line->output;
396ec6859b2STodor Tomov unsigned long flags;
397ec6859b2STodor Tomov unsigned int i;
398ec6859b2STodor Tomov
399ec6859b2STodor Tomov spin_lock_irqsave(&vfe->output_lock, flags);
400ec6859b2STodor Tomov
401ec6859b2STodor Tomov for (i = 0; i < output->wm_num; i++)
402ec6859b2STodor Tomov vfe_release_wm(vfe, output->wm_idx[i]);
403ec6859b2STodor Tomov
404ec6859b2STodor Tomov output->state = VFE_OUTPUT_OFF;
405ec6859b2STodor Tomov
406ec6859b2STodor Tomov spin_unlock_irqrestore(&vfe->output_lock, flags);
407ec6859b2STodor Tomov return 0;
408ec6859b2STodor Tomov }
409ec6859b2STodor Tomov
410d8bdc3e4SRobert Foss /**
411d8bdc3e4SRobert Foss * vfe_isr_comp_done() - Process composite image done interrupt
412ec6859b2STodor Tomov * @vfe: VFE Device
413ec6859b2STodor Tomov * @comp: Composite image id
414ec6859b2STodor Tomov */
vfe_isr_comp_done(struct vfe_device * vfe,u8 comp)415633b388fSRobert Foss void vfe_isr_comp_done(struct vfe_device *vfe, u8 comp)
416ec6859b2STodor Tomov {
417ec6859b2STodor Tomov unsigned int i;
418ec6859b2STodor Tomov
419ec6859b2STodor Tomov for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
420ec6859b2STodor Tomov if (vfe->wm_output_map[i] == VFE_LINE_PIX) {
421633b388fSRobert Foss vfe->isr_ops.wm_done(vfe, i);
422ec6859b2STodor Tomov break;
423ec6859b2STodor Tomov }
424ec6859b2STodor Tomov }
425ec6859b2STodor Tomov
vfe_isr_reset_ack(struct vfe_device * vfe)426633b388fSRobert Foss void vfe_isr_reset_ack(struct vfe_device *vfe)
427ec6859b2STodor Tomov {
428ec6859b2STodor Tomov complete(&vfe->reset_complete);
429ec6859b2STodor Tomov }
430ec6859b2STodor Tomov
431ec6859b2STodor Tomov /*
432ec6859b2STodor Tomov * vfe_set_clock_rates - Calculate and set clock rates on VFE module
433ec6859b2STodor Tomov * @vfe: VFE device
434ec6859b2STodor Tomov *
435ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
436ec6859b2STodor Tomov */
vfe_set_clock_rates(struct vfe_device * vfe)437ec6859b2STodor Tomov static int vfe_set_clock_rates(struct vfe_device *vfe)
438ec6859b2STodor Tomov {
4399c3e59deSTodor Tomov struct device *dev = vfe->camss->dev;
440633b388fSRobert Foss u64 pixel_clock[VFE_LINE_NUM_MAX];
441ec6859b2STodor Tomov int i, j;
442ec6859b2STodor Tomov int ret;
443ec6859b2STodor Tomov
444633b388fSRobert Foss for (i = VFE_LINE_RDI0; i < vfe->line_num; i++) {
445ec6859b2STodor Tomov ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
446ec6859b2STodor Tomov &pixel_clock[i]);
447ec6859b2STodor Tomov if (ret)
448ec6859b2STodor Tomov pixel_clock[i] = 0;
449ec6859b2STodor Tomov }
450ec6859b2STodor Tomov
451ec6859b2STodor Tomov for (i = 0; i < vfe->nclocks; i++) {
452ec6859b2STodor Tomov struct camss_clock *clock = &vfe->clock[i];
453ec6859b2STodor Tomov
4549c3e59deSTodor Tomov if (!strcmp(clock->name, "vfe0") ||
4557319cdf1SRobert Foss !strcmp(clock->name, "vfe1") ||
4567319cdf1SRobert Foss !strcmp(clock->name, "vfe_lite")) {
457ec6859b2STodor Tomov u64 min_rate = 0;
458ec6859b2STodor Tomov long rate;
459ec6859b2STodor Tomov
460633b388fSRobert Foss for (j = VFE_LINE_RDI0; j < vfe->line_num; j++) {
461ec6859b2STodor Tomov u32 tmp;
462ec6859b2STodor Tomov u8 bpp;
463ec6859b2STodor Tomov
464ec6859b2STodor Tomov if (j == VFE_LINE_PIX) {
465ec6859b2STodor Tomov tmp = pixel_clock[j];
466ec6859b2STodor Tomov } else {
467cba3819dSTodor Tomov struct vfe_line *l = &vfe->line[j];
468cba3819dSTodor Tomov
469cba3819dSTodor Tomov bpp = vfe_get_bpp(l->formats,
470cba3819dSTodor Tomov l->nformats,
471cba3819dSTodor Tomov l->fmt[MSM_VFE_PAD_SINK].code);
472ec6859b2STodor Tomov tmp = pixel_clock[j] * bpp / 64;
473ec6859b2STodor Tomov }
474ec6859b2STodor Tomov
475ec6859b2STodor Tomov if (min_rate < tmp)
476ec6859b2STodor Tomov min_rate = tmp;
477ec6859b2STodor Tomov }
478ec6859b2STodor Tomov
479ec6859b2STodor Tomov camss_add_clock_margin(&min_rate);
480ec6859b2STodor Tomov
481ec6859b2STodor Tomov for (j = 0; j < clock->nfreqs; j++)
482ec6859b2STodor Tomov if (min_rate < clock->freq[j])
483ec6859b2STodor Tomov break;
484ec6859b2STodor Tomov
485ec6859b2STodor Tomov if (j == clock->nfreqs) {
486ec6859b2STodor Tomov dev_err(dev,
487ec6859b2STodor Tomov "Pixel clock is too high for VFE");
488ec6859b2STodor Tomov return -EINVAL;
489ec6859b2STodor Tomov }
490ec6859b2STodor Tomov
491ec6859b2STodor Tomov /* if sensor pixel clock is not available */
492ec6859b2STodor Tomov /* set highest possible VFE clock rate */
493ec6859b2STodor Tomov if (min_rate == 0)
494ec6859b2STodor Tomov j = clock->nfreqs - 1;
495ec6859b2STodor Tomov
496ec6859b2STodor Tomov rate = clk_round_rate(clock->clk, clock->freq[j]);
497ec6859b2STodor Tomov if (rate < 0) {
498ec6859b2STodor Tomov dev_err(dev, "clk round rate failed: %ld\n",
499ec6859b2STodor Tomov rate);
500ec6859b2STodor Tomov return -EINVAL;
501ec6859b2STodor Tomov }
502ec6859b2STodor Tomov
503ec6859b2STodor Tomov ret = clk_set_rate(clock->clk, rate);
504ec6859b2STodor Tomov if (ret < 0) {
505ec6859b2STodor Tomov dev_err(dev, "clk set rate failed: %d\n", ret);
506ec6859b2STodor Tomov return ret;
507ec6859b2STodor Tomov }
508ec6859b2STodor Tomov }
509ec6859b2STodor Tomov }
510ec6859b2STodor Tomov
511ec6859b2STodor Tomov return 0;
512ec6859b2STodor Tomov }
513ec6859b2STodor Tomov
514ec6859b2STodor Tomov /*
515ec6859b2STodor Tomov * vfe_check_clock_rates - Check current clock rates on VFE module
516ec6859b2STodor Tomov * @vfe: VFE device
517ec6859b2STodor Tomov *
518ec6859b2STodor Tomov * Return 0 if current clock rates are suitable for a new pipeline
519ec6859b2STodor Tomov * or a negative error code otherwise
520ec6859b2STodor Tomov */
vfe_check_clock_rates(struct vfe_device * vfe)521ec6859b2STodor Tomov static int vfe_check_clock_rates(struct vfe_device *vfe)
522ec6859b2STodor Tomov {
523633b388fSRobert Foss u64 pixel_clock[VFE_LINE_NUM_MAX];
524ec6859b2STodor Tomov int i, j;
525ec6859b2STodor Tomov int ret;
526ec6859b2STodor Tomov
527633b388fSRobert Foss for (i = VFE_LINE_RDI0; i < vfe->line_num; i++) {
528ec6859b2STodor Tomov ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
529ec6859b2STodor Tomov &pixel_clock[i]);
530ec6859b2STodor Tomov if (ret)
531ec6859b2STodor Tomov pixel_clock[i] = 0;
532ec6859b2STodor Tomov }
533ec6859b2STodor Tomov
534ec6859b2STodor Tomov for (i = 0; i < vfe->nclocks; i++) {
535ec6859b2STodor Tomov struct camss_clock *clock = &vfe->clock[i];
536ec6859b2STodor Tomov
5379c3e59deSTodor Tomov if (!strcmp(clock->name, "vfe0") ||
538*f6cc8265SBryan O'Donoghue !strcmp(clock->name, "vfe1") ||
539*f6cc8265SBryan O'Donoghue !strcmp(clock->name, "vfe_lite")) {
540ec6859b2STodor Tomov u64 min_rate = 0;
541ec6859b2STodor Tomov unsigned long rate;
542ec6859b2STodor Tomov
543633b388fSRobert Foss for (j = VFE_LINE_RDI0; j < vfe->line_num; j++) {
544ec6859b2STodor Tomov u32 tmp;
545ec6859b2STodor Tomov u8 bpp;
546ec6859b2STodor Tomov
547ec6859b2STodor Tomov if (j == VFE_LINE_PIX) {
548ec6859b2STodor Tomov tmp = pixel_clock[j];
549ec6859b2STodor Tomov } else {
550cba3819dSTodor Tomov struct vfe_line *l = &vfe->line[j];
551cba3819dSTodor Tomov
552cba3819dSTodor Tomov bpp = vfe_get_bpp(l->formats,
553cba3819dSTodor Tomov l->nformats,
554cba3819dSTodor Tomov l->fmt[MSM_VFE_PAD_SINK].code);
555ec6859b2STodor Tomov tmp = pixel_clock[j] * bpp / 64;
556ec6859b2STodor Tomov }
557ec6859b2STodor Tomov
558ec6859b2STodor Tomov if (min_rate < tmp)
559ec6859b2STodor Tomov min_rate = tmp;
560ec6859b2STodor Tomov }
561ec6859b2STodor Tomov
562ec6859b2STodor Tomov camss_add_clock_margin(&min_rate);
563ec6859b2STodor Tomov
564ec6859b2STodor Tomov rate = clk_get_rate(clock->clk);
565ec6859b2STodor Tomov if (rate < min_rate)
566ec6859b2STodor Tomov return -EBUSY;
567ec6859b2STodor Tomov }
568ec6859b2STodor Tomov }
569ec6859b2STodor Tomov
570ec6859b2STodor Tomov return 0;
571ec6859b2STodor Tomov }
572ec6859b2STodor Tomov
573ec6859b2STodor Tomov /*
574ec6859b2STodor Tomov * vfe_get - Power up and reset VFE module
575ec6859b2STodor Tomov * @vfe: VFE Device
576ec6859b2STodor Tomov *
577ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
578ec6859b2STodor Tomov */
vfe_get(struct vfe_device * vfe)579c5af8db8SBryan O'Donoghue int vfe_get(struct vfe_device *vfe)
580ec6859b2STodor Tomov {
581ec6859b2STodor Tomov int ret;
582ec6859b2STodor Tomov
583ec6859b2STodor Tomov mutex_lock(&vfe->power_lock);
584ec6859b2STodor Tomov
585ec6859b2STodor Tomov if (vfe->power_count == 0) {
5862f6f8af6SRobert Foss ret = vfe->ops->pm_domain_on(vfe);
58702afa816STodor Tomov if (ret < 0)
58802afa816STodor Tomov goto error_pm_domain;
58902afa816STodor Tomov
59009dfb36cSMauro Carvalho Chehab ret = pm_runtime_resume_and_get(vfe->camss->dev);
59102afa816STodor Tomov if (ret < 0)
59209dfb36cSMauro Carvalho Chehab goto error_domain_off;
59302afa816STodor Tomov
594ec6859b2STodor Tomov ret = vfe_set_clock_rates(vfe);
595ec6859b2STodor Tomov if (ret < 0)
59677909691SDinghao Liu goto error_pm_runtime_get;
597ec6859b2STodor Tomov
598ec6859b2STodor Tomov ret = camss_enable_clocks(vfe->nclocks, vfe->clock,
5999c3e59deSTodor Tomov vfe->camss->dev);
600ec6859b2STodor Tomov if (ret < 0)
60177909691SDinghao Liu goto error_pm_runtime_get;
602ec6859b2STodor Tomov
603ec6859b2STodor Tomov ret = vfe_reset(vfe);
604ec6859b2STodor Tomov if (ret < 0)
605ec6859b2STodor Tomov goto error_reset;
606ec6859b2STodor Tomov
607ec6859b2STodor Tomov vfe_reset_output_maps(vfe);
608ec6859b2STodor Tomov
609ec6859b2STodor Tomov vfe_init_outputs(vfe);
610745b475eSRobert Foss
611745b475eSRobert Foss vfe->ops->hw_version(vfe);
612ec6859b2STodor Tomov } else {
613ec6859b2STodor Tomov ret = vfe_check_clock_rates(vfe);
614ec6859b2STodor Tomov if (ret < 0)
615816a4070SBryan O'Donoghue goto error_pm_domain;
616ec6859b2STodor Tomov }
617ec6859b2STodor Tomov vfe->power_count++;
618ec6859b2STodor Tomov
619ec6859b2STodor Tomov mutex_unlock(&vfe->power_lock);
620ec6859b2STodor Tomov
621ec6859b2STodor Tomov return 0;
622ec6859b2STodor Tomov
623ec6859b2STodor Tomov error_reset:
624ec6859b2STodor Tomov camss_disable_clocks(vfe->nclocks, vfe->clock);
625ec6859b2STodor Tomov
62602afa816STodor Tomov error_pm_runtime_get:
62777909691SDinghao Liu pm_runtime_put_sync(vfe->camss->dev);
62809dfb36cSMauro Carvalho Chehab error_domain_off:
6292f6f8af6SRobert Foss vfe->ops->pm_domain_off(vfe);
63002afa816STodor Tomov
63102afa816STodor Tomov error_pm_domain:
632ec6859b2STodor Tomov mutex_unlock(&vfe->power_lock);
633ec6859b2STodor Tomov
634ec6859b2STodor Tomov return ret;
635ec6859b2STodor Tomov }
636ec6859b2STodor Tomov
637ec6859b2STodor Tomov /*
638ec6859b2STodor Tomov * vfe_put - Power down VFE module
639ec6859b2STodor Tomov * @vfe: VFE Device
640ec6859b2STodor Tomov */
vfe_put(struct vfe_device * vfe)641c5af8db8SBryan O'Donoghue void vfe_put(struct vfe_device *vfe)
642ec6859b2STodor Tomov {
643ec6859b2STodor Tomov mutex_lock(&vfe->power_lock);
644ec6859b2STodor Tomov
645ec6859b2STodor Tomov if (vfe->power_count == 0) {
6469c3e59deSTodor Tomov dev_err(vfe->camss->dev, "vfe power off on power_count == 0\n");
647ec6859b2STodor Tomov goto exit;
648ec6859b2STodor Tomov } else if (vfe->power_count == 1) {
649ec6859b2STodor Tomov if (vfe->was_streaming) {
650ec6859b2STodor Tomov vfe->was_streaming = 0;
651633b388fSRobert Foss vfe->ops->vfe_halt(vfe);
652ec6859b2STodor Tomov }
653ec6859b2STodor Tomov camss_disable_clocks(vfe->nclocks, vfe->clock);
65402afa816STodor Tomov pm_runtime_put_sync(vfe->camss->dev);
6552f6f8af6SRobert Foss vfe->ops->pm_domain_off(vfe);
656ec6859b2STodor Tomov }
657ec6859b2STodor Tomov
658ec6859b2STodor Tomov vfe->power_count--;
659ec6859b2STodor Tomov
660ec6859b2STodor Tomov exit:
661ec6859b2STodor Tomov mutex_unlock(&vfe->power_lock);
662ec6859b2STodor Tomov }
663ec6859b2STodor Tomov
664ec6859b2STodor Tomov /*
665ec6859b2STodor Tomov * vfe_flush_buffers - Return all vb2 buffers
666ec6859b2STodor Tomov * @vid: Video device structure
667ec6859b2STodor Tomov * @state: vb2 buffer state of the returned buffers
668ec6859b2STodor Tomov *
669ec6859b2STodor Tomov * Return all buffers to vb2. This includes queued pending buffers (still
670ec6859b2STodor Tomov * unused) and any buffers given to the hardware but again still not used.
671ec6859b2STodor Tomov *
672ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
673ec6859b2STodor Tomov */
vfe_flush_buffers(struct camss_video * vid,enum vb2_buffer_state state)674633b388fSRobert Foss int vfe_flush_buffers(struct camss_video *vid,
675ec6859b2STodor Tomov enum vb2_buffer_state state)
676ec6859b2STodor Tomov {
677a93e5f4fSTodor Tomov struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
678a93e5f4fSTodor Tomov struct vfe_device *vfe = to_vfe(line);
679ec6859b2STodor Tomov struct vfe_output *output;
680ec6859b2STodor Tomov unsigned long flags;
681ec6859b2STodor Tomov
682ec6859b2STodor Tomov output = &line->output;
683ec6859b2STodor Tomov
684ec6859b2STodor Tomov spin_lock_irqsave(&vfe->output_lock, flags);
685ec6859b2STodor Tomov
686ec6859b2STodor Tomov vfe_buf_flush_pending(output, state);
687ec6859b2STodor Tomov
688ec6859b2STodor Tomov if (output->buf[0])
689ec6859b2STodor Tomov vb2_buffer_done(&output->buf[0]->vb.vb2_buf, state);
690ec6859b2STodor Tomov
691ec6859b2STodor Tomov if (output->buf[1])
692ec6859b2STodor Tomov vb2_buffer_done(&output->buf[1]->vb.vb2_buf, state);
693ec6859b2STodor Tomov
694ec6859b2STodor Tomov if (output->last_buffer) {
695ec6859b2STodor Tomov vb2_buffer_done(&output->last_buffer->vb.vb2_buf, state);
696ec6859b2STodor Tomov output->last_buffer = NULL;
697ec6859b2STodor Tomov }
698ec6859b2STodor Tomov
699ec6859b2STodor Tomov spin_unlock_irqrestore(&vfe->output_lock, flags);
700ec6859b2STodor Tomov
701ec6859b2STodor Tomov return 0;
702ec6859b2STodor Tomov }
703ec6859b2STodor Tomov
704ec6859b2STodor Tomov /*
705ec6859b2STodor Tomov * vfe_set_power - Power on/off VFE module
706ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
707ec6859b2STodor Tomov * @on: Requested power state
708ec6859b2STodor Tomov *
709ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
710ec6859b2STodor Tomov */
vfe_set_power(struct v4l2_subdev * sd,int on)711ec6859b2STodor Tomov static int vfe_set_power(struct v4l2_subdev *sd, int on)
712ec6859b2STodor Tomov {
713ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
714ec6859b2STodor Tomov struct vfe_device *vfe = to_vfe(line);
715ec6859b2STodor Tomov int ret;
716ec6859b2STodor Tomov
717ec6859b2STodor Tomov if (on) {
718ec6859b2STodor Tomov ret = vfe_get(vfe);
719ec6859b2STodor Tomov if (ret < 0)
720ec6859b2STodor Tomov return ret;
721ec6859b2STodor Tomov } else {
722ec6859b2STodor Tomov vfe_put(vfe);
723ec6859b2STodor Tomov }
724ec6859b2STodor Tomov
725ec6859b2STodor Tomov return 0;
726ec6859b2STodor Tomov }
727ec6859b2STodor Tomov
728ec6859b2STodor Tomov /*
729ec6859b2STodor Tomov * vfe_set_stream - Enable/disable streaming on VFE module
730ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
731ec6859b2STodor Tomov * @enable: Requested streaming state
732ec6859b2STodor Tomov *
733ec6859b2STodor Tomov * Main configuration of VFE module is triggered here.
734ec6859b2STodor Tomov *
735ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
736ec6859b2STodor Tomov */
vfe_set_stream(struct v4l2_subdev * sd,int enable)737ec6859b2STodor Tomov static int vfe_set_stream(struct v4l2_subdev *sd, int enable)
738ec6859b2STodor Tomov {
739ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
740ec6859b2STodor Tomov struct vfe_device *vfe = to_vfe(line);
741ec6859b2STodor Tomov int ret;
742ec6859b2STodor Tomov
743ec6859b2STodor Tomov if (enable) {
7448ce158c1SMilen Mitkov line->output.state = VFE_OUTPUT_RESERVED;
745633b388fSRobert Foss ret = vfe->ops->vfe_enable(line);
746ec6859b2STodor Tomov if (ret < 0)
7479c3e59deSTodor Tomov dev_err(vfe->camss->dev,
748ec6859b2STodor Tomov "Failed to enable vfe outputs\n");
749ec6859b2STodor Tomov } else {
750633b388fSRobert Foss ret = vfe->ops->vfe_disable(line);
751ec6859b2STodor Tomov if (ret < 0)
7529c3e59deSTodor Tomov dev_err(vfe->camss->dev,
753ec6859b2STodor Tomov "Failed to disable vfe outputs\n");
754ec6859b2STodor Tomov }
755ec6859b2STodor Tomov
756ec6859b2STodor Tomov return ret;
757ec6859b2STodor Tomov }
758ec6859b2STodor Tomov
759ec6859b2STodor Tomov /*
760ec6859b2STodor Tomov * __vfe_get_format - Get pointer to format structure
761ec6859b2STodor Tomov * @line: VFE line
762ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
763ec6859b2STodor Tomov * @pad: pad from which format is requested
764ec6859b2STodor Tomov * @which: TRY or ACTIVE format
765ec6859b2STodor Tomov *
766ec6859b2STodor Tomov * Return pointer to TRY or ACTIVE format structure
767ec6859b2STodor Tomov */
768ec6859b2STodor Tomov static struct v4l2_mbus_framefmt *
__vfe_get_format(struct vfe_line * line,struct v4l2_subdev_state * sd_state,unsigned int pad,enum v4l2_subdev_format_whence which)769ec6859b2STodor Tomov __vfe_get_format(struct vfe_line *line,
7700d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
771ec6859b2STodor Tomov unsigned int pad,
772ec6859b2STodor Tomov enum v4l2_subdev_format_whence which)
773ec6859b2STodor Tomov {
774ec6859b2STodor Tomov if (which == V4L2_SUBDEV_FORMAT_TRY)
7750d346d2aSTomi Valkeinen return v4l2_subdev_get_try_format(&line->subdev, sd_state,
7760d346d2aSTomi Valkeinen pad);
777ec6859b2STodor Tomov
778ec6859b2STodor Tomov return &line->fmt[pad];
779ec6859b2STodor Tomov }
780ec6859b2STodor Tomov
781ec6859b2STodor Tomov /*
782ec6859b2STodor Tomov * __vfe_get_compose - Get pointer to compose selection structure
783ec6859b2STodor Tomov * @line: VFE line
784ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
785ec6859b2STodor Tomov * @which: TRY or ACTIVE format
786ec6859b2STodor Tomov *
787ec6859b2STodor Tomov * Return pointer to TRY or ACTIVE compose rectangle structure
788ec6859b2STodor Tomov */
789ec6859b2STodor Tomov static struct v4l2_rect *
__vfe_get_compose(struct vfe_line * line,struct v4l2_subdev_state * sd_state,enum v4l2_subdev_format_whence which)790ec6859b2STodor Tomov __vfe_get_compose(struct vfe_line *line,
7910d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
792ec6859b2STodor Tomov enum v4l2_subdev_format_whence which)
793ec6859b2STodor Tomov {
794ec6859b2STodor Tomov if (which == V4L2_SUBDEV_FORMAT_TRY)
7950d346d2aSTomi Valkeinen return v4l2_subdev_get_try_compose(&line->subdev, sd_state,
796ec6859b2STodor Tomov MSM_VFE_PAD_SINK);
797ec6859b2STodor Tomov
798ec6859b2STodor Tomov return &line->compose;
799ec6859b2STodor Tomov }
800ec6859b2STodor Tomov
801ec6859b2STodor Tomov /*
802ec6859b2STodor Tomov * __vfe_get_crop - Get pointer to crop selection structure
803ec6859b2STodor Tomov * @line: VFE line
804ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
805ec6859b2STodor Tomov * @which: TRY or ACTIVE format
806ec6859b2STodor Tomov *
807ec6859b2STodor Tomov * Return pointer to TRY or ACTIVE crop rectangle structure
808ec6859b2STodor Tomov */
809ec6859b2STodor Tomov static struct v4l2_rect *
__vfe_get_crop(struct vfe_line * line,struct v4l2_subdev_state * sd_state,enum v4l2_subdev_format_whence which)810ec6859b2STodor Tomov __vfe_get_crop(struct vfe_line *line,
8110d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
812ec6859b2STodor Tomov enum v4l2_subdev_format_whence which)
813ec6859b2STodor Tomov {
814ec6859b2STodor Tomov if (which == V4L2_SUBDEV_FORMAT_TRY)
8150d346d2aSTomi Valkeinen return v4l2_subdev_get_try_crop(&line->subdev, sd_state,
816ec6859b2STodor Tomov MSM_VFE_PAD_SRC);
817ec6859b2STodor Tomov
818ec6859b2STodor Tomov return &line->crop;
819ec6859b2STodor Tomov }
820ec6859b2STodor Tomov
821ec6859b2STodor Tomov /*
822ec6859b2STodor Tomov * vfe_try_format - Handle try format by pad subdev method
823ec6859b2STodor Tomov * @line: VFE line
824ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
825ec6859b2STodor Tomov * @pad: pad on which format is requested
826ec6859b2STodor Tomov * @fmt: pointer to v4l2 format structure
827ec6859b2STodor Tomov * @which: wanted subdev format
828ec6859b2STodor Tomov */
vfe_try_format(struct vfe_line * line,struct v4l2_subdev_state * sd_state,unsigned int pad,struct v4l2_mbus_framefmt * fmt,enum v4l2_subdev_format_whence which)829ec6859b2STodor Tomov static void vfe_try_format(struct vfe_line *line,
8300d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
831ec6859b2STodor Tomov unsigned int pad,
832ec6859b2STodor Tomov struct v4l2_mbus_framefmt *fmt,
833ec6859b2STodor Tomov enum v4l2_subdev_format_whence which)
834ec6859b2STodor Tomov {
835ec6859b2STodor Tomov unsigned int i;
836ec6859b2STodor Tomov u32 code;
837ec6859b2STodor Tomov
838ec6859b2STodor Tomov switch (pad) {
839ec6859b2STodor Tomov case MSM_VFE_PAD_SINK:
840ec6859b2STodor Tomov /* Set format on sink pad */
841ec6859b2STodor Tomov
842cba3819dSTodor Tomov for (i = 0; i < line->nformats; i++)
843cba3819dSTodor Tomov if (fmt->code == line->formats[i].code)
844ec6859b2STodor Tomov break;
845ec6859b2STodor Tomov
846ec6859b2STodor Tomov /* If not found, use UYVY as default */
847cba3819dSTodor Tomov if (i >= line->nformats)
848ec6859b2STodor Tomov fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
849ec6859b2STodor Tomov
850ec6859b2STodor Tomov fmt->width = clamp_t(u32, fmt->width, 1, 8191);
851ec6859b2STodor Tomov fmt->height = clamp_t(u32, fmt->height, 1, 8191);
852ec6859b2STodor Tomov
853ec6859b2STodor Tomov fmt->field = V4L2_FIELD_NONE;
854ec6859b2STodor Tomov fmt->colorspace = V4L2_COLORSPACE_SRGB;
855ec6859b2STodor Tomov
856ec6859b2STodor Tomov break;
857ec6859b2STodor Tomov
858ec6859b2STodor Tomov case MSM_VFE_PAD_SRC:
859ec6859b2STodor Tomov /* Set and return a format same as sink pad */
860ec6859b2STodor Tomov code = fmt->code;
861ec6859b2STodor Tomov
8620d346d2aSTomi Valkeinen *fmt = *__vfe_get_format(line, sd_state, MSM_VFE_PAD_SINK,
8630d346d2aSTomi Valkeinen which);
86407eeb342STodor Tomov
86507eeb342STodor Tomov fmt->code = vfe_src_pad_code(line, fmt->code, 0, code);
866ec6859b2STodor Tomov
867ec6859b2STodor Tomov if (line->id == VFE_LINE_PIX) {
868ec6859b2STodor Tomov struct v4l2_rect *rect;
869ec6859b2STodor Tomov
8700d346d2aSTomi Valkeinen rect = __vfe_get_crop(line, sd_state, which);
871ec6859b2STodor Tomov
872ec6859b2STodor Tomov fmt->width = rect->width;
873ec6859b2STodor Tomov fmt->height = rect->height;
874ec6859b2STodor Tomov }
875ec6859b2STodor Tomov
876ec6859b2STodor Tomov break;
877ec6859b2STodor Tomov }
878ec6859b2STodor Tomov
879ec6859b2STodor Tomov fmt->colorspace = V4L2_COLORSPACE_SRGB;
880ec6859b2STodor Tomov }
881ec6859b2STodor Tomov
882ec6859b2STodor Tomov /*
883ec6859b2STodor Tomov * vfe_try_compose - Handle try compose selection by pad subdev method
884ec6859b2STodor Tomov * @line: VFE line
885ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
886ec6859b2STodor Tomov * @rect: pointer to v4l2 rect structure
887ec6859b2STodor Tomov * @which: wanted subdev format
888ec6859b2STodor Tomov */
vfe_try_compose(struct vfe_line * line,struct v4l2_subdev_state * sd_state,struct v4l2_rect * rect,enum v4l2_subdev_format_whence which)889ec6859b2STodor Tomov static void vfe_try_compose(struct vfe_line *line,
8900d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
891ec6859b2STodor Tomov struct v4l2_rect *rect,
892ec6859b2STodor Tomov enum v4l2_subdev_format_whence which)
893ec6859b2STodor Tomov {
894ec6859b2STodor Tomov struct v4l2_mbus_framefmt *fmt;
895ec6859b2STodor Tomov
8960d346d2aSTomi Valkeinen fmt = __vfe_get_format(line, sd_state, MSM_VFE_PAD_SINK, which);
897ec6859b2STodor Tomov
898ec6859b2STodor Tomov if (rect->width > fmt->width)
899ec6859b2STodor Tomov rect->width = fmt->width;
900ec6859b2STodor Tomov
901ec6859b2STodor Tomov if (rect->height > fmt->height)
902ec6859b2STodor Tomov rect->height = fmt->height;
903ec6859b2STodor Tomov
904ec6859b2STodor Tomov if (fmt->width > rect->width * SCALER_RATIO_MAX)
905ec6859b2STodor Tomov rect->width = (fmt->width + SCALER_RATIO_MAX - 1) /
906ec6859b2STodor Tomov SCALER_RATIO_MAX;
907ec6859b2STodor Tomov
908ec6859b2STodor Tomov rect->width &= ~0x1;
909ec6859b2STodor Tomov
910ec6859b2STodor Tomov if (fmt->height > rect->height * SCALER_RATIO_MAX)
911ec6859b2STodor Tomov rect->height = (fmt->height + SCALER_RATIO_MAX - 1) /
912ec6859b2STodor Tomov SCALER_RATIO_MAX;
913ec6859b2STodor Tomov
914ec6859b2STodor Tomov if (rect->width < 16)
915ec6859b2STodor Tomov rect->width = 16;
916ec6859b2STodor Tomov
917ec6859b2STodor Tomov if (rect->height < 4)
918ec6859b2STodor Tomov rect->height = 4;
919ec6859b2STodor Tomov }
920ec6859b2STodor Tomov
921ec6859b2STodor Tomov /*
922ec6859b2STodor Tomov * vfe_try_crop - Handle try crop selection by pad subdev method
923ec6859b2STodor Tomov * @line: VFE line
924ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
925ec6859b2STodor Tomov * @rect: pointer to v4l2 rect structure
926ec6859b2STodor Tomov * @which: wanted subdev format
927ec6859b2STodor Tomov */
vfe_try_crop(struct vfe_line * line,struct v4l2_subdev_state * sd_state,struct v4l2_rect * rect,enum v4l2_subdev_format_whence which)928ec6859b2STodor Tomov static void vfe_try_crop(struct vfe_line *line,
9290d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
930ec6859b2STodor Tomov struct v4l2_rect *rect,
931ec6859b2STodor Tomov enum v4l2_subdev_format_whence which)
932ec6859b2STodor Tomov {
933ec6859b2STodor Tomov struct v4l2_rect *compose;
934ec6859b2STodor Tomov
9350d346d2aSTomi Valkeinen compose = __vfe_get_compose(line, sd_state, which);
936ec6859b2STodor Tomov
937ec6859b2STodor Tomov if (rect->width > compose->width)
938ec6859b2STodor Tomov rect->width = compose->width;
939ec6859b2STodor Tomov
940ec6859b2STodor Tomov if (rect->width + rect->left > compose->width)
941ec6859b2STodor Tomov rect->left = compose->width - rect->width;
942ec6859b2STodor Tomov
943ec6859b2STodor Tomov if (rect->height > compose->height)
944ec6859b2STodor Tomov rect->height = compose->height;
945ec6859b2STodor Tomov
946ec6859b2STodor Tomov if (rect->height + rect->top > compose->height)
947ec6859b2STodor Tomov rect->top = compose->height - rect->height;
948ec6859b2STodor Tomov
949ec6859b2STodor Tomov /* wm in line based mode writes multiple of 16 horizontally */
950ec6859b2STodor Tomov rect->left += (rect->width & 0xf) >> 1;
951ec6859b2STodor Tomov rect->width &= ~0xf;
952ec6859b2STodor Tomov
953ec6859b2STodor Tomov if (rect->width < 16) {
954ec6859b2STodor Tomov rect->left = 0;
955ec6859b2STodor Tomov rect->width = 16;
956ec6859b2STodor Tomov }
957ec6859b2STodor Tomov
958ec6859b2STodor Tomov if (rect->height < 4) {
959ec6859b2STodor Tomov rect->top = 0;
960ec6859b2STodor Tomov rect->height = 4;
961ec6859b2STodor Tomov }
962ec6859b2STodor Tomov }
963ec6859b2STodor Tomov
964ec6859b2STodor Tomov /*
965ec6859b2STodor Tomov * vfe_enum_mbus_code - Handle pixel format enumeration
966ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
967ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
968ec6859b2STodor Tomov * @code: pointer to v4l2_subdev_mbus_code_enum structure
969ec6859b2STodor Tomov *
970ec6859b2STodor Tomov * return -EINVAL or zero on success
971ec6859b2STodor Tomov */
vfe_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)972ec6859b2STodor Tomov static int vfe_enum_mbus_code(struct v4l2_subdev *sd,
9730d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
974ec6859b2STodor Tomov struct v4l2_subdev_mbus_code_enum *code)
975ec6859b2STodor Tomov {
976ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
977ec6859b2STodor Tomov
978ec6859b2STodor Tomov if (code->pad == MSM_VFE_PAD_SINK) {
979cba3819dSTodor Tomov if (code->index >= line->nformats)
980ec6859b2STodor Tomov return -EINVAL;
981ec6859b2STodor Tomov
982cba3819dSTodor Tomov code->code = line->formats[code->index].code;
983ec6859b2STodor Tomov } else {
98407eeb342STodor Tomov struct v4l2_mbus_framefmt *sink_fmt;
985ec6859b2STodor Tomov
9860d346d2aSTomi Valkeinen sink_fmt = __vfe_get_format(line, sd_state, MSM_VFE_PAD_SINK,
987ec6859b2STodor Tomov code->which);
988ec6859b2STodor Tomov
98907eeb342STodor Tomov code->code = vfe_src_pad_code(line, sink_fmt->code,
99007eeb342STodor Tomov code->index, 0);
99107eeb342STodor Tomov if (!code->code)
99207eeb342STodor Tomov return -EINVAL;
993ec6859b2STodor Tomov }
994ec6859b2STodor Tomov
995ec6859b2STodor Tomov return 0;
996ec6859b2STodor Tomov }
997ec6859b2STodor Tomov
998ec6859b2STodor Tomov /*
999ec6859b2STodor Tomov * vfe_enum_frame_size - Handle frame size enumeration
1000ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
1001ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
1002ec6859b2STodor Tomov * @fse: pointer to v4l2_subdev_frame_size_enum structure
1003ec6859b2STodor Tomov *
1004ec6859b2STodor Tomov * Return -EINVAL or zero on success
1005ec6859b2STodor Tomov */
vfe_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)1006ec6859b2STodor Tomov static int vfe_enum_frame_size(struct v4l2_subdev *sd,
10070d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1008ec6859b2STodor Tomov struct v4l2_subdev_frame_size_enum *fse)
1009ec6859b2STodor Tomov {
1010ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
1011ec6859b2STodor Tomov struct v4l2_mbus_framefmt format;
1012ec6859b2STodor Tomov
1013ec6859b2STodor Tomov if (fse->index != 0)
1014ec6859b2STodor Tomov return -EINVAL;
1015ec6859b2STodor Tomov
1016ec6859b2STodor Tomov format.code = fse->code;
1017ec6859b2STodor Tomov format.width = 1;
1018ec6859b2STodor Tomov format.height = 1;
10190d346d2aSTomi Valkeinen vfe_try_format(line, sd_state, fse->pad, &format, fse->which);
1020ec6859b2STodor Tomov fse->min_width = format.width;
1021ec6859b2STodor Tomov fse->min_height = format.height;
1022ec6859b2STodor Tomov
1023ec6859b2STodor Tomov if (format.code != fse->code)
1024ec6859b2STodor Tomov return -EINVAL;
1025ec6859b2STodor Tomov
1026ec6859b2STodor Tomov format.code = fse->code;
1027ec6859b2STodor Tomov format.width = -1;
1028ec6859b2STodor Tomov format.height = -1;
10290d346d2aSTomi Valkeinen vfe_try_format(line, sd_state, fse->pad, &format, fse->which);
1030ec6859b2STodor Tomov fse->max_width = format.width;
1031ec6859b2STodor Tomov fse->max_height = format.height;
1032ec6859b2STodor Tomov
1033ec6859b2STodor Tomov return 0;
1034ec6859b2STodor Tomov }
1035ec6859b2STodor Tomov
1036ec6859b2STodor Tomov /*
1037ec6859b2STodor Tomov * vfe_get_format - Handle get format by pads subdev method
1038ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
1039ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
1040ec6859b2STodor Tomov * @fmt: pointer to v4l2 subdev format structure
1041ec6859b2STodor Tomov *
1042ec6859b2STodor Tomov * Return -EINVAL or zero on success
1043ec6859b2STodor Tomov */
vfe_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1044ec6859b2STodor Tomov static int vfe_get_format(struct v4l2_subdev *sd,
10450d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1046ec6859b2STodor Tomov struct v4l2_subdev_format *fmt)
1047ec6859b2STodor Tomov {
1048ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
1049ec6859b2STodor Tomov struct v4l2_mbus_framefmt *format;
1050ec6859b2STodor Tomov
10510d346d2aSTomi Valkeinen format = __vfe_get_format(line, sd_state, fmt->pad, fmt->which);
1052ec6859b2STodor Tomov if (format == NULL)
1053ec6859b2STodor Tomov return -EINVAL;
1054ec6859b2STodor Tomov
1055ec6859b2STodor Tomov fmt->format = *format;
1056ec6859b2STodor Tomov
1057ec6859b2STodor Tomov return 0;
1058ec6859b2STodor Tomov }
1059ec6859b2STodor Tomov
1060ec6859b2STodor Tomov static int vfe_set_selection(struct v4l2_subdev *sd,
10610d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1062ec6859b2STodor Tomov struct v4l2_subdev_selection *sel);
1063ec6859b2STodor Tomov
1064ec6859b2STodor Tomov /*
1065ec6859b2STodor Tomov * vfe_set_format - Handle set format by pads subdev method
1066ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
1067ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
1068ec6859b2STodor Tomov * @fmt: pointer to v4l2 subdev format structure
1069ec6859b2STodor Tomov *
1070ec6859b2STodor Tomov * Return -EINVAL or zero on success
1071ec6859b2STodor Tomov */
vfe_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1072ec6859b2STodor Tomov static int vfe_set_format(struct v4l2_subdev *sd,
10730d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1074ec6859b2STodor Tomov struct v4l2_subdev_format *fmt)
1075ec6859b2STodor Tomov {
1076ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
1077ec6859b2STodor Tomov struct v4l2_mbus_framefmt *format;
1078ec6859b2STodor Tomov
10790d346d2aSTomi Valkeinen format = __vfe_get_format(line, sd_state, fmt->pad, fmt->which);
1080ec6859b2STodor Tomov if (format == NULL)
1081ec6859b2STodor Tomov return -EINVAL;
1082ec6859b2STodor Tomov
10830d346d2aSTomi Valkeinen vfe_try_format(line, sd_state, fmt->pad, &fmt->format, fmt->which);
1084ec6859b2STodor Tomov *format = fmt->format;
1085ec6859b2STodor Tomov
1086ec6859b2STodor Tomov if (fmt->pad == MSM_VFE_PAD_SINK) {
1087ec6859b2STodor Tomov struct v4l2_subdev_selection sel = { 0 };
1088ec6859b2STodor Tomov int ret;
1089ec6859b2STodor Tomov
1090ec6859b2STodor Tomov /* Propagate the format from sink to source */
10910d346d2aSTomi Valkeinen format = __vfe_get_format(line, sd_state, MSM_VFE_PAD_SRC,
1092ec6859b2STodor Tomov fmt->which);
1093ec6859b2STodor Tomov
1094ec6859b2STodor Tomov *format = fmt->format;
10950d346d2aSTomi Valkeinen vfe_try_format(line, sd_state, MSM_VFE_PAD_SRC, format,
1096ec6859b2STodor Tomov fmt->which);
1097ec6859b2STodor Tomov
1098ec6859b2STodor Tomov if (line->id != VFE_LINE_PIX)
1099ec6859b2STodor Tomov return 0;
1100ec6859b2STodor Tomov
1101ec6859b2STodor Tomov /* Reset sink pad compose selection */
1102ec6859b2STodor Tomov sel.which = fmt->which;
1103ec6859b2STodor Tomov sel.pad = MSM_VFE_PAD_SINK;
1104ec6859b2STodor Tomov sel.target = V4L2_SEL_TGT_COMPOSE;
1105ec6859b2STodor Tomov sel.r.width = fmt->format.width;
1106ec6859b2STodor Tomov sel.r.height = fmt->format.height;
11070d346d2aSTomi Valkeinen ret = vfe_set_selection(sd, sd_state, &sel);
1108ec6859b2STodor Tomov if (ret < 0)
1109ec6859b2STodor Tomov return ret;
1110ec6859b2STodor Tomov }
1111ec6859b2STodor Tomov
1112ec6859b2STodor Tomov return 0;
1113ec6859b2STodor Tomov }
1114ec6859b2STodor Tomov
1115ec6859b2STodor Tomov /*
1116ec6859b2STodor Tomov * vfe_get_selection - Handle get selection by pads subdev method
1117ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
1118ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
1119ec6859b2STodor Tomov * @sel: pointer to v4l2 subdev selection structure
1120ec6859b2STodor Tomov *
1121ec6859b2STodor Tomov * Return -EINVAL or zero on success
1122ec6859b2STodor Tomov */
vfe_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1123ec6859b2STodor Tomov static int vfe_get_selection(struct v4l2_subdev *sd,
11240d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1125ec6859b2STodor Tomov struct v4l2_subdev_selection *sel)
1126ec6859b2STodor Tomov {
1127ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
1128ec6859b2STodor Tomov struct v4l2_subdev_format fmt = { 0 };
1129ec6859b2STodor Tomov struct v4l2_rect *rect;
1130ec6859b2STodor Tomov int ret;
1131ec6859b2STodor Tomov
1132ec6859b2STodor Tomov if (line->id != VFE_LINE_PIX)
1133ec6859b2STodor Tomov return -EINVAL;
1134ec6859b2STodor Tomov
1135ec6859b2STodor Tomov if (sel->pad == MSM_VFE_PAD_SINK)
1136ec6859b2STodor Tomov switch (sel->target) {
1137ec6859b2STodor Tomov case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1138ec6859b2STodor Tomov fmt.pad = sel->pad;
1139ec6859b2STodor Tomov fmt.which = sel->which;
11400d346d2aSTomi Valkeinen ret = vfe_get_format(sd, sd_state, &fmt);
1141ec6859b2STodor Tomov if (ret < 0)
1142ec6859b2STodor Tomov return ret;
1143ec6859b2STodor Tomov
1144ec6859b2STodor Tomov sel->r.left = 0;
1145ec6859b2STodor Tomov sel->r.top = 0;
1146ec6859b2STodor Tomov sel->r.width = fmt.format.width;
1147ec6859b2STodor Tomov sel->r.height = fmt.format.height;
1148ec6859b2STodor Tomov break;
1149ec6859b2STodor Tomov case V4L2_SEL_TGT_COMPOSE:
11500d346d2aSTomi Valkeinen rect = __vfe_get_compose(line, sd_state, sel->which);
1151ec6859b2STodor Tomov if (rect == NULL)
1152ec6859b2STodor Tomov return -EINVAL;
1153ec6859b2STodor Tomov
1154ec6859b2STodor Tomov sel->r = *rect;
1155ec6859b2STodor Tomov break;
1156ec6859b2STodor Tomov default:
1157ec6859b2STodor Tomov return -EINVAL;
1158ec6859b2STodor Tomov }
1159ec6859b2STodor Tomov else if (sel->pad == MSM_VFE_PAD_SRC)
1160ec6859b2STodor Tomov switch (sel->target) {
1161ec6859b2STodor Tomov case V4L2_SEL_TGT_CROP_BOUNDS:
11620d346d2aSTomi Valkeinen rect = __vfe_get_compose(line, sd_state, sel->which);
1163ec6859b2STodor Tomov if (rect == NULL)
1164ec6859b2STodor Tomov return -EINVAL;
1165ec6859b2STodor Tomov
1166ec6859b2STodor Tomov sel->r.left = rect->left;
1167ec6859b2STodor Tomov sel->r.top = rect->top;
1168ec6859b2STodor Tomov sel->r.width = rect->width;
1169ec6859b2STodor Tomov sel->r.height = rect->height;
1170ec6859b2STodor Tomov break;
1171ec6859b2STodor Tomov case V4L2_SEL_TGT_CROP:
11720d346d2aSTomi Valkeinen rect = __vfe_get_crop(line, sd_state, sel->which);
1173ec6859b2STodor Tomov if (rect == NULL)
1174ec6859b2STodor Tomov return -EINVAL;
1175ec6859b2STodor Tomov
1176ec6859b2STodor Tomov sel->r = *rect;
1177ec6859b2STodor Tomov break;
1178ec6859b2STodor Tomov default:
1179ec6859b2STodor Tomov return -EINVAL;
1180ec6859b2STodor Tomov }
1181ec6859b2STodor Tomov
1182ec6859b2STodor Tomov return 0;
1183ec6859b2STodor Tomov }
1184ec6859b2STodor Tomov
1185ec6859b2STodor Tomov /*
1186ec6859b2STodor Tomov * vfe_set_selection - Handle set selection by pads subdev method
1187ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
1188ec6859b2STodor Tomov * @cfg: V4L2 subdev pad configuration
1189ec6859b2STodor Tomov * @sel: pointer to v4l2 subdev selection structure
1190ec6859b2STodor Tomov *
1191ec6859b2STodor Tomov * Return -EINVAL or zero on success
1192ec6859b2STodor Tomov */
vfe_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1193ec6859b2STodor Tomov static int vfe_set_selection(struct v4l2_subdev *sd,
11940d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1195ec6859b2STodor Tomov struct v4l2_subdev_selection *sel)
1196ec6859b2STodor Tomov {
1197ec6859b2STodor Tomov struct vfe_line *line = v4l2_get_subdevdata(sd);
1198ec6859b2STodor Tomov struct v4l2_rect *rect;
1199ec6859b2STodor Tomov int ret;
1200ec6859b2STodor Tomov
1201ec6859b2STodor Tomov if (line->id != VFE_LINE_PIX)
1202ec6859b2STodor Tomov return -EINVAL;
1203ec6859b2STodor Tomov
1204ec6859b2STodor Tomov if (sel->target == V4L2_SEL_TGT_COMPOSE &&
1205ec6859b2STodor Tomov sel->pad == MSM_VFE_PAD_SINK) {
1206ec6859b2STodor Tomov struct v4l2_subdev_selection crop = { 0 };
1207ec6859b2STodor Tomov
12080d346d2aSTomi Valkeinen rect = __vfe_get_compose(line, sd_state, sel->which);
1209ec6859b2STodor Tomov if (rect == NULL)
1210ec6859b2STodor Tomov return -EINVAL;
1211ec6859b2STodor Tomov
12120d346d2aSTomi Valkeinen vfe_try_compose(line, sd_state, &sel->r, sel->which);
1213ec6859b2STodor Tomov *rect = sel->r;
1214ec6859b2STodor Tomov
1215ec6859b2STodor Tomov /* Reset source crop selection */
1216ec6859b2STodor Tomov crop.which = sel->which;
1217ec6859b2STodor Tomov crop.pad = MSM_VFE_PAD_SRC;
1218ec6859b2STodor Tomov crop.target = V4L2_SEL_TGT_CROP;
1219ec6859b2STodor Tomov crop.r = *rect;
12200d346d2aSTomi Valkeinen ret = vfe_set_selection(sd, sd_state, &crop);
1221ec6859b2STodor Tomov } else if (sel->target == V4L2_SEL_TGT_CROP &&
1222ec6859b2STodor Tomov sel->pad == MSM_VFE_PAD_SRC) {
1223ec6859b2STodor Tomov struct v4l2_subdev_format fmt = { 0 };
1224ec6859b2STodor Tomov
12250d346d2aSTomi Valkeinen rect = __vfe_get_crop(line, sd_state, sel->which);
1226ec6859b2STodor Tomov if (rect == NULL)
1227ec6859b2STodor Tomov return -EINVAL;
1228ec6859b2STodor Tomov
12290d346d2aSTomi Valkeinen vfe_try_crop(line, sd_state, &sel->r, sel->which);
1230ec6859b2STodor Tomov *rect = sel->r;
1231ec6859b2STodor Tomov
1232ec6859b2STodor Tomov /* Reset source pad format width and height */
1233ec6859b2STodor Tomov fmt.which = sel->which;
1234ec6859b2STodor Tomov fmt.pad = MSM_VFE_PAD_SRC;
12350d346d2aSTomi Valkeinen ret = vfe_get_format(sd, sd_state, &fmt);
1236ec6859b2STodor Tomov if (ret < 0)
1237ec6859b2STodor Tomov return ret;
1238ec6859b2STodor Tomov
1239ec6859b2STodor Tomov fmt.format.width = rect->width;
1240ec6859b2STodor Tomov fmt.format.height = rect->height;
12410d346d2aSTomi Valkeinen ret = vfe_set_format(sd, sd_state, &fmt);
1242ec6859b2STodor Tomov } else {
1243ec6859b2STodor Tomov ret = -EINVAL;
1244ec6859b2STodor Tomov }
1245ec6859b2STodor Tomov
1246ec6859b2STodor Tomov return ret;
1247ec6859b2STodor Tomov }
1248ec6859b2STodor Tomov
1249ec6859b2STodor Tomov /*
1250ec6859b2STodor Tomov * vfe_init_formats - Initialize formats on all pads
1251ec6859b2STodor Tomov * @sd: VFE V4L2 subdevice
1252ec6859b2STodor Tomov * @fh: V4L2 subdev file handle
1253ec6859b2STodor Tomov *
1254ec6859b2STodor Tomov * Initialize all pad formats with default values.
1255ec6859b2STodor Tomov *
1256ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
1257ec6859b2STodor Tomov */
vfe_init_formats(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1258ec6859b2STodor Tomov static int vfe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1259ec6859b2STodor Tomov {
1260ec6859b2STodor Tomov struct v4l2_subdev_format format = {
1261ec6859b2STodor Tomov .pad = MSM_VFE_PAD_SINK,
1262ec6859b2STodor Tomov .which = fh ? V4L2_SUBDEV_FORMAT_TRY :
1263ec6859b2STodor Tomov V4L2_SUBDEV_FORMAT_ACTIVE,
1264ec6859b2STodor Tomov .format = {
1265ec6859b2STodor Tomov .code = MEDIA_BUS_FMT_UYVY8_2X8,
1266ec6859b2STodor Tomov .width = 1920,
1267ec6859b2STodor Tomov .height = 1080
1268ec6859b2STodor Tomov }
1269ec6859b2STodor Tomov };
1270ec6859b2STodor Tomov
12710d346d2aSTomi Valkeinen return vfe_set_format(sd, fh ? fh->state : NULL, &format);
1272ec6859b2STodor Tomov }
1273ec6859b2STodor Tomov
1274ec6859b2STodor Tomov /*
1275ec6859b2STodor Tomov * msm_vfe_subdev_init - Initialize VFE device structure and resources
1276ec6859b2STodor Tomov * @vfe: VFE device
1277ec6859b2STodor Tomov * @res: VFE module resources table
1278ec6859b2STodor Tomov *
1279ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
1280ec6859b2STodor Tomov */
msm_vfe_subdev_init(struct camss * camss,struct vfe_device * vfe,const struct resources * res,u8 id)12819c3e59deSTodor Tomov int msm_vfe_subdev_init(struct camss *camss, struct vfe_device *vfe,
12829c3e59deSTodor Tomov const struct resources *res, u8 id)
1283ec6859b2STodor Tomov {
12849c3e59deSTodor Tomov struct device *dev = camss->dev;
1285ec6859b2STodor Tomov struct platform_device *pdev = to_platform_device(dev);
1286ec6859b2STodor Tomov int i, j;
1287ec6859b2STodor Tomov int ret;
1288ec6859b2STodor Tomov
12899e5d1581SAngeloGioacchino Del Regno switch (camss->version) {
12909e5d1581SAngeloGioacchino Del Regno case CAMSS_8x16:
1291051a01acSTodor Tomov vfe->ops = &vfe_ops_4_1;
12929e5d1581SAngeloGioacchino Del Regno break;
12939e5d1581SAngeloGioacchino Del Regno case CAMSS_8x96:
12944e1abf66STodor Tomov vfe->ops = &vfe_ops_4_7;
12959e5d1581SAngeloGioacchino Del Regno break;
12969e5d1581SAngeloGioacchino Del Regno case CAMSS_660:
12979e5d1581SAngeloGioacchino Del Regno vfe->ops = &vfe_ops_4_8;
12989e5d1581SAngeloGioacchino Del Regno break;
12997319cdf1SRobert Foss case CAMSS_845:
13007319cdf1SRobert Foss vfe->ops = &vfe_ops_170;
13017319cdf1SRobert Foss break;
1302b4436a18SJonathan Marek case CAMSS_8250:
1303b4436a18SJonathan Marek vfe->ops = &vfe_ops_480;
1304b4436a18SJonathan Marek break;
13059e5d1581SAngeloGioacchino Del Regno default:
1306051a01acSTodor Tomov return -EINVAL;
13079e5d1581SAngeloGioacchino Del Regno }
1308633b388fSRobert Foss vfe->ops->subdev_init(dev, vfe);
1309051a01acSTodor Tomov
1310ec6859b2STodor Tomov /* Memory */
1311ec6859b2STodor Tomov
1312414e0a64Sdingsenjie vfe->base = devm_platform_ioremap_resource_byname(pdev, res->reg[0]);
1313ec6859b2STodor Tomov if (IS_ERR(vfe->base)) {
1314ec6859b2STodor Tomov dev_err(dev, "could not map memory\n");
1315ec6859b2STodor Tomov return PTR_ERR(vfe->base);
1316ec6859b2STodor Tomov }
1317ec6859b2STodor Tomov
1318ec6859b2STodor Tomov /* Interrupt */
1319ec6859b2STodor Tomov
1320b416be3aSLad Prabhakar ret = platform_get_irq_byname(pdev, res->interrupt[0]);
1321b416be3aSLad Prabhakar if (ret < 0)
1322b416be3aSLad Prabhakar return ret;
1323ec6859b2STodor Tomov
1324b416be3aSLad Prabhakar vfe->irq = ret;
1325ec6859b2STodor Tomov snprintf(vfe->irq_name, sizeof(vfe->irq_name), "%s_%s%d",
1326ed38a146SJonathan Marek dev_name(dev), MSM_VFE_NAME, id);
1327051a01acSTodor Tomov ret = devm_request_irq(dev, vfe->irq, vfe->ops->isr,
1328ec6859b2STodor Tomov IRQF_TRIGGER_RISING, vfe->irq_name, vfe);
1329ec6859b2STodor Tomov if (ret < 0) {
1330ec6859b2STodor Tomov dev_err(dev, "request_irq failed: %d\n", ret);
1331ec6859b2STodor Tomov return ret;
1332ec6859b2STodor Tomov }
1333ec6859b2STodor Tomov
1334ec6859b2STodor Tomov /* Clocks */
1335ec6859b2STodor Tomov
1336ec6859b2STodor Tomov vfe->nclocks = 0;
1337ec6859b2STodor Tomov while (res->clock[vfe->nclocks])
1338ec6859b2STodor Tomov vfe->nclocks++;
1339ec6859b2STodor Tomov
1340ec6859b2STodor Tomov vfe->clock = devm_kcalloc(dev, vfe->nclocks, sizeof(*vfe->clock),
1341ec6859b2STodor Tomov GFP_KERNEL);
1342ec6859b2STodor Tomov if (!vfe->clock)
1343ec6859b2STodor Tomov return -ENOMEM;
1344ec6859b2STodor Tomov
1345ec6859b2STodor Tomov for (i = 0; i < vfe->nclocks; i++) {
1346ec6859b2STodor Tomov struct camss_clock *clock = &vfe->clock[i];
1347ec6859b2STodor Tomov
1348ec6859b2STodor Tomov clock->clk = devm_clk_get(dev, res->clock[i]);
1349ec6859b2STodor Tomov if (IS_ERR(clock->clk))
1350ec6859b2STodor Tomov return PTR_ERR(clock->clk);
1351ec6859b2STodor Tomov
1352ec6859b2STodor Tomov clock->name = res->clock[i];
1353ec6859b2STodor Tomov
1354ec6859b2STodor Tomov clock->nfreqs = 0;
1355ec6859b2STodor Tomov while (res->clock_rate[i][clock->nfreqs])
1356ec6859b2STodor Tomov clock->nfreqs++;
1357ec6859b2STodor Tomov
1358ec6859b2STodor Tomov if (!clock->nfreqs) {
1359ec6859b2STodor Tomov clock->freq = NULL;
1360ec6859b2STodor Tomov continue;
1361ec6859b2STodor Tomov }
1362ec6859b2STodor Tomov
1363ec6859b2STodor Tomov clock->freq = devm_kcalloc(dev,
1364ec6859b2STodor Tomov clock->nfreqs,
1365ec6859b2STodor Tomov sizeof(*clock->freq),
1366ec6859b2STodor Tomov GFP_KERNEL);
1367ec6859b2STodor Tomov if (!clock->freq)
1368ec6859b2STodor Tomov return -ENOMEM;
1369ec6859b2STodor Tomov
1370ec6859b2STodor Tomov for (j = 0; j < clock->nfreqs; j++)
1371ec6859b2STodor Tomov clock->freq[j] = res->clock_rate[i][j];
1372ec6859b2STodor Tomov }
1373ec6859b2STodor Tomov
1374ec6859b2STodor Tomov mutex_init(&vfe->power_lock);
1375ec6859b2STodor Tomov vfe->power_count = 0;
1376ec6859b2STodor Tomov
1377ec6859b2STodor Tomov mutex_init(&vfe->stream_lock);
1378ec6859b2STodor Tomov vfe->stream_count = 0;
1379ec6859b2STodor Tomov
1380ec6859b2STodor Tomov spin_lock_init(&vfe->output_lock);
1381ec6859b2STodor Tomov
13829c3e59deSTodor Tomov vfe->camss = camss;
13839c3e59deSTodor Tomov vfe->id = id;
1384ec6859b2STodor Tomov vfe->reg_update = 0;
1385ec6859b2STodor Tomov
1386633b388fSRobert Foss for (i = VFE_LINE_RDI0; i < vfe->line_num; i++) {
1387cba3819dSTodor Tomov struct vfe_line *l = &vfe->line[i];
1388cba3819dSTodor Tomov
1389cba3819dSTodor Tomov l->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1390cba3819dSTodor Tomov l->video_out.camss = camss;
1391cba3819dSTodor Tomov l->id = i;
1392cba3819dSTodor Tomov init_completion(&l->output.sof);
1393cba3819dSTodor Tomov init_completion(&l->output.reg_update);
1394cba3819dSTodor Tomov
1395cba3819dSTodor Tomov if (camss->version == CAMSS_8x16) {
1396cba3819dSTodor Tomov if (i == VFE_LINE_PIX) {
1397cba3819dSTodor Tomov l->formats = formats_pix_8x16;
1398cba3819dSTodor Tomov l->nformats = ARRAY_SIZE(formats_pix_8x16);
1399cba3819dSTodor Tomov } else {
1400cba3819dSTodor Tomov l->formats = formats_rdi_8x16;
1401cba3819dSTodor Tomov l->nformats = ARRAY_SIZE(formats_rdi_8x16);
1402cba3819dSTodor Tomov }
14039e5d1581SAngeloGioacchino Del Regno } else if (camss->version == CAMSS_8x96 ||
14049e5d1581SAngeloGioacchino Del Regno camss->version == CAMSS_660) {
1405cba3819dSTodor Tomov if (i == VFE_LINE_PIX) {
1406cba3819dSTodor Tomov l->formats = formats_pix_8x96;
1407cba3819dSTodor Tomov l->nformats = ARRAY_SIZE(formats_pix_8x96);
1408cba3819dSTodor Tomov } else {
1409cba3819dSTodor Tomov l->formats = formats_rdi_8x96;
1410cba3819dSTodor Tomov l->nformats = ARRAY_SIZE(formats_rdi_8x96);
1411cba3819dSTodor Tomov }
1412b4436a18SJonathan Marek } else if (camss->version == CAMSS_845 ||
1413b4436a18SJonathan Marek camss->version == CAMSS_8250) {
14147319cdf1SRobert Foss l->formats = formats_rdi_845;
14157319cdf1SRobert Foss l->nformats = ARRAY_SIZE(formats_rdi_845);
1416cba3819dSTodor Tomov } else {
1417cba3819dSTodor Tomov return -EINVAL;
1418cba3819dSTodor Tomov }
1419ec6859b2STodor Tomov }
1420ec6859b2STodor Tomov
1421ec6859b2STodor Tomov init_completion(&vfe->reset_complete);
1422ec6859b2STodor Tomov init_completion(&vfe->halt_complete);
1423ec6859b2STodor Tomov
1424ec6859b2STodor Tomov return 0;
1425ec6859b2STodor Tomov }
1426ec6859b2STodor Tomov
1427ec6859b2STodor Tomov /*
1428ec6859b2STodor Tomov * vfe_link_setup - Setup VFE connections
1429ec6859b2STodor Tomov * @entity: Pointer to media entity structure
1430ec6859b2STodor Tomov * @local: Pointer to local pad
1431ec6859b2STodor Tomov * @remote: Pointer to remote pad
1432ec6859b2STodor Tomov * @flags: Link flags
1433ec6859b2STodor Tomov *
1434ec6859b2STodor Tomov * Return 0 on success
1435ec6859b2STodor Tomov */
vfe_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)1436ec6859b2STodor Tomov static int vfe_link_setup(struct media_entity *entity,
1437ec6859b2STodor Tomov const struct media_pad *local,
1438ec6859b2STodor Tomov const struct media_pad *remote, u32 flags)
1439ec6859b2STodor Tomov {
1440ec6859b2STodor Tomov if (flags & MEDIA_LNK_FL_ENABLED)
1441b2e44430SLaurent Pinchart if (media_pad_remote_pad_first(local))
1442ec6859b2STodor Tomov return -EBUSY;
1443ec6859b2STodor Tomov
1444ec6859b2STodor Tomov return 0;
1445ec6859b2STodor Tomov }
1446ec6859b2STodor Tomov
1447ec6859b2STodor Tomov static const struct v4l2_subdev_core_ops vfe_core_ops = {
1448ec6859b2STodor Tomov .s_power = vfe_set_power,
1449ec6859b2STodor Tomov };
1450ec6859b2STodor Tomov
1451ec6859b2STodor Tomov static const struct v4l2_subdev_video_ops vfe_video_ops = {
1452ec6859b2STodor Tomov .s_stream = vfe_set_stream,
1453ec6859b2STodor Tomov };
1454ec6859b2STodor Tomov
1455ec6859b2STodor Tomov static const struct v4l2_subdev_pad_ops vfe_pad_ops = {
1456ec6859b2STodor Tomov .enum_mbus_code = vfe_enum_mbus_code,
1457ec6859b2STodor Tomov .enum_frame_size = vfe_enum_frame_size,
1458ec6859b2STodor Tomov .get_fmt = vfe_get_format,
1459ec6859b2STodor Tomov .set_fmt = vfe_set_format,
1460ec6859b2STodor Tomov .get_selection = vfe_get_selection,
1461ec6859b2STodor Tomov .set_selection = vfe_set_selection,
1462ec6859b2STodor Tomov };
1463ec6859b2STodor Tomov
1464ec6859b2STodor Tomov static const struct v4l2_subdev_ops vfe_v4l2_ops = {
1465ec6859b2STodor Tomov .core = &vfe_core_ops,
1466ec6859b2STodor Tomov .video = &vfe_video_ops,
1467ec6859b2STodor Tomov .pad = &vfe_pad_ops,
1468ec6859b2STodor Tomov };
1469ec6859b2STodor Tomov
1470ec6859b2STodor Tomov static const struct v4l2_subdev_internal_ops vfe_v4l2_internal_ops = {
1471ec6859b2STodor Tomov .open = vfe_init_formats,
1472ec6859b2STodor Tomov };
1473ec6859b2STodor Tomov
1474ec6859b2STodor Tomov static const struct media_entity_operations vfe_media_ops = {
1475ec6859b2STodor Tomov .link_setup = vfe_link_setup,
1476ec6859b2STodor Tomov .link_validate = v4l2_subdev_link_validate,
1477ec6859b2STodor Tomov };
1478ec6859b2STodor Tomov
1479ec6859b2STodor Tomov /*
1480ec6859b2STodor Tomov * msm_vfe_register_entities - Register subdev node for VFE module
1481ec6859b2STodor Tomov * @vfe: VFE device
1482ec6859b2STodor Tomov * @v4l2_dev: V4L2 device
1483ec6859b2STodor Tomov *
1484ec6859b2STodor Tomov * Initialize and register a subdev node for the VFE module. Then
1485ec6859b2STodor Tomov * call msm_video_register() to register the video device node which
1486ec6859b2STodor Tomov * will be connected to this subdev node. Then actually create the
1487ec6859b2STodor Tomov * media link between them.
1488ec6859b2STodor Tomov *
1489ec6859b2STodor Tomov * Return 0 on success or a negative error code otherwise
1490ec6859b2STodor Tomov */
msm_vfe_register_entities(struct vfe_device * vfe,struct v4l2_device * v4l2_dev)1491ec6859b2STodor Tomov int msm_vfe_register_entities(struct vfe_device *vfe,
1492ec6859b2STodor Tomov struct v4l2_device *v4l2_dev)
1493ec6859b2STodor Tomov {
14949c3e59deSTodor Tomov struct device *dev = vfe->camss->dev;
1495ec6859b2STodor Tomov struct v4l2_subdev *sd;
1496ec6859b2STodor Tomov struct media_pad *pads;
1497ec6859b2STodor Tomov struct camss_video *video_out;
1498ec6859b2STodor Tomov int ret;
1499ec6859b2STodor Tomov int i;
1500ec6859b2STodor Tomov
1501633b388fSRobert Foss for (i = 0; i < vfe->line_num; i++) {
1502ec6859b2STodor Tomov char name[32];
1503ec6859b2STodor Tomov
1504ec6859b2STodor Tomov sd = &vfe->line[i].subdev;
1505ec6859b2STodor Tomov pads = vfe->line[i].pads;
1506ec6859b2STodor Tomov video_out = &vfe->line[i].video_out;
1507ec6859b2STodor Tomov
1508ec6859b2STodor Tomov v4l2_subdev_init(sd, &vfe_v4l2_ops);
1509ec6859b2STodor Tomov sd->internal_ops = &vfe_v4l2_internal_ops;
1510ec6859b2STodor Tomov sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1511ec6859b2STodor Tomov if (i == VFE_LINE_PIX)
1512ec6859b2STodor Tomov snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s",
1513ec6859b2STodor Tomov MSM_VFE_NAME, vfe->id, "pix");
1514ec6859b2STodor Tomov else
1515ec6859b2STodor Tomov snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s%d",
1516ec6859b2STodor Tomov MSM_VFE_NAME, vfe->id, "rdi", i);
1517ec6859b2STodor Tomov
1518ec6859b2STodor Tomov v4l2_set_subdevdata(sd, &vfe->line[i]);
1519ec6859b2STodor Tomov
1520ec6859b2STodor Tomov ret = vfe_init_formats(sd, NULL);
1521ec6859b2STodor Tomov if (ret < 0) {
1522ec6859b2STodor Tomov dev_err(dev, "Failed to init format: %d\n", ret);
1523ec6859b2STodor Tomov goto error_init;
1524ec6859b2STodor Tomov }
1525ec6859b2STodor Tomov
1526ec6859b2STodor Tomov pads[MSM_VFE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1527ec6859b2STodor Tomov pads[MSM_VFE_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
1528ec6859b2STodor Tomov
1529ec6859b2STodor Tomov sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1530ec6859b2STodor Tomov sd->entity.ops = &vfe_media_ops;
1531ec6859b2STodor Tomov ret = media_entity_pads_init(&sd->entity, MSM_VFE_PADS_NUM,
1532ec6859b2STodor Tomov pads);
1533ec6859b2STodor Tomov if (ret < 0) {
1534ec6859b2STodor Tomov dev_err(dev, "Failed to init media entity: %d\n", ret);
1535ec6859b2STodor Tomov goto error_init;
1536ec6859b2STodor Tomov }
1537ec6859b2STodor Tomov
1538ec6859b2STodor Tomov ret = v4l2_device_register_subdev(v4l2_dev, sd);
1539ec6859b2STodor Tomov if (ret < 0) {
1540ec6859b2STodor Tomov dev_err(dev, "Failed to register subdev: %d\n", ret);
1541ec6859b2STodor Tomov goto error_reg_subdev;
1542ec6859b2STodor Tomov }
1543ec6859b2STodor Tomov
1544633b388fSRobert Foss video_out->ops = &vfe->video_ops;
1545d5b7eb47SAndrey Konovalov if (vfe->camss->version == CAMSS_845 ||
1546d5b7eb47SAndrey Konovalov vfe->camss->version == CAMSS_8250)
1547d5b7eb47SAndrey Konovalov video_out->bpl_alignment = 16;
1548d5b7eb47SAndrey Konovalov else
1549ec6859b2STodor Tomov video_out->bpl_alignment = 8;
1550ec6859b2STodor Tomov video_out->line_based = 0;
1551ec6859b2STodor Tomov if (i == VFE_LINE_PIX) {
1552ec6859b2STodor Tomov video_out->bpl_alignment = 16;
1553ec6859b2STodor Tomov video_out->line_based = 1;
1554ec6859b2STodor Tomov }
1555ec6859b2STodor Tomov snprintf(name, ARRAY_SIZE(name), "%s%d_%s%d",
1556ec6859b2STodor Tomov MSM_VFE_NAME, vfe->id, "video", i);
1557ec6859b2STodor Tomov ret = msm_video_register(video_out, v4l2_dev, name,
1558ec6859b2STodor Tomov i == VFE_LINE_PIX ? 1 : 0);
1559ec6859b2STodor Tomov if (ret < 0) {
1560ec6859b2STodor Tomov dev_err(dev, "Failed to register video node: %d\n",
1561ec6859b2STodor Tomov ret);
1562ec6859b2STodor Tomov goto error_reg_video;
1563ec6859b2STodor Tomov }
1564ec6859b2STodor Tomov
1565ec6859b2STodor Tomov ret = media_create_pad_link(
1566ec6859b2STodor Tomov &sd->entity, MSM_VFE_PAD_SRC,
1567ec6859b2STodor Tomov &video_out->vdev.entity, 0,
1568ec6859b2STodor Tomov MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1569ec6859b2STodor Tomov if (ret < 0) {
1570ec6859b2STodor Tomov dev_err(dev, "Failed to link %s->%s entities: %d\n",
1571ec6859b2STodor Tomov sd->entity.name, video_out->vdev.entity.name,
1572ec6859b2STodor Tomov ret);
1573ec6859b2STodor Tomov goto error_link;
1574ec6859b2STodor Tomov }
1575ec6859b2STodor Tomov }
1576ec6859b2STodor Tomov
1577ec6859b2STodor Tomov return 0;
1578ec6859b2STodor Tomov
1579ec6859b2STodor Tomov error_link:
1580ec6859b2STodor Tomov msm_video_unregister(video_out);
1581ec6859b2STodor Tomov
1582ec6859b2STodor Tomov error_reg_video:
1583ec6859b2STodor Tomov v4l2_device_unregister_subdev(sd);
1584ec6859b2STodor Tomov
1585ec6859b2STodor Tomov error_reg_subdev:
1586ec6859b2STodor Tomov media_entity_cleanup(&sd->entity);
1587ec6859b2STodor Tomov
1588ec6859b2STodor Tomov error_init:
1589ec6859b2STodor Tomov for (i--; i >= 0; i--) {
1590ec6859b2STodor Tomov sd = &vfe->line[i].subdev;
1591ec6859b2STodor Tomov video_out = &vfe->line[i].video_out;
1592ec6859b2STodor Tomov
1593ec6859b2STodor Tomov msm_video_unregister(video_out);
1594ec6859b2STodor Tomov v4l2_device_unregister_subdev(sd);
1595ec6859b2STodor Tomov media_entity_cleanup(&sd->entity);
1596ec6859b2STodor Tomov }
1597ec6859b2STodor Tomov
1598ec6859b2STodor Tomov return ret;
1599ec6859b2STodor Tomov }
1600ec6859b2STodor Tomov
1601ec6859b2STodor Tomov /*
1602ec6859b2STodor Tomov * msm_vfe_unregister_entities - Unregister VFE module subdev node
1603ec6859b2STodor Tomov * @vfe: VFE device
1604ec6859b2STodor Tomov */
msm_vfe_unregister_entities(struct vfe_device * vfe)1605ec6859b2STodor Tomov void msm_vfe_unregister_entities(struct vfe_device *vfe)
1606ec6859b2STodor Tomov {
1607ec6859b2STodor Tomov int i;
1608ec6859b2STodor Tomov
1609ec6859b2STodor Tomov mutex_destroy(&vfe->power_lock);
1610ec6859b2STodor Tomov mutex_destroy(&vfe->stream_lock);
1611ec6859b2STodor Tomov
1612633b388fSRobert Foss for (i = 0; i < vfe->line_num; i++) {
1613ec6859b2STodor Tomov struct v4l2_subdev *sd = &vfe->line[i].subdev;
1614ec6859b2STodor Tomov struct camss_video *video_out = &vfe->line[i].video_out;
1615ec6859b2STodor Tomov
1616ec6859b2STodor Tomov msm_video_unregister(video_out);
1617ec6859b2STodor Tomov v4l2_device_unregister_subdev(sd);
1618ec6859b2STodor Tomov media_entity_cleanup(&sd->entity);
1619ec6859b2STodor Tomov }
1620ec6859b2STodor Tomov }
1621