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_domain.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/spinlock_types.h>
20 #include <linux/spinlock.h>
21 #include <media/media-entity.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-subdev.h>
25 #include "camss-vfe.h"
28 #define MSM_VFE_NAME "msm_vfe"
30 /* VFE reset timeout */
31 #define VFE_RESET_TIMEOUT_MS 50
33 #define SCALER_RATIO_MAX 16
35 static const struct camss_format_info formats_rdi_8x16
[] = {
36 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_UYVY
, 1,
37 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
38 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_VYUY
, 1,
39 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
40 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_YUYV
, 1,
41 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
42 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_YVYU
, 1,
43 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
44 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, V4L2_PIX_FMT_SBGGR8
, 1,
45 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
46 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, V4L2_PIX_FMT_SGBRG8
, 1,
47 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
48 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, V4L2_PIX_FMT_SGRBG8
, 1,
49 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
50 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, V4L2_PIX_FMT_SRGGB8
, 1,
51 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
52 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, V4L2_PIX_FMT_SBGGR10P
, 1,
53 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
54 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, V4L2_PIX_FMT_SGBRG10P
, 1,
55 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
56 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, V4L2_PIX_FMT_SGRBG10P
, 1,
57 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
58 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, V4L2_PIX_FMT_SRGGB10P
, 1,
59 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
60 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, V4L2_PIX_FMT_SBGGR12P
, 1,
61 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
62 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, V4L2_PIX_FMT_SGBRG12P
, 1,
63 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
64 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, V4L2_PIX_FMT_SGRBG12P
, 1,
65 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
66 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, V4L2_PIX_FMT_SRGGB12P
, 1,
67 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
68 { MEDIA_BUS_FMT_Y10_1X10
, 10, V4L2_PIX_FMT_Y10P
, 1,
69 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
72 static const struct camss_format_info formats_rdi_8x96
[] = {
73 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_UYVY
, 1,
74 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
75 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_VYUY
, 1,
76 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
77 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_YUYV
, 1,
78 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
79 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_YVYU
, 1,
80 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
81 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, V4L2_PIX_FMT_SBGGR8
, 1,
82 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
83 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, V4L2_PIX_FMT_SGBRG8
, 1,
84 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
85 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, V4L2_PIX_FMT_SGRBG8
, 1,
86 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
87 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, V4L2_PIX_FMT_SRGGB8
, 1,
88 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
89 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, V4L2_PIX_FMT_SBGGR10P
, 1,
90 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
91 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, V4L2_PIX_FMT_SGBRG10P
, 1,
92 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
93 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, V4L2_PIX_FMT_SGRBG10P
, 1,
94 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
95 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, V4L2_PIX_FMT_SRGGB10P
, 1,
96 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
97 { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE
, 16, V4L2_PIX_FMT_SBGGR10
, 1,
98 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
99 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, V4L2_PIX_FMT_SBGGR12P
, 1,
100 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
101 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, V4L2_PIX_FMT_SGBRG12P
, 1,
102 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
103 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, V4L2_PIX_FMT_SGRBG12P
, 1,
104 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
105 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, V4L2_PIX_FMT_SRGGB12P
, 1,
106 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
107 { MEDIA_BUS_FMT_SBGGR14_1X14
, 14, V4L2_PIX_FMT_SBGGR14P
, 1,
108 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
109 { MEDIA_BUS_FMT_SGBRG14_1X14
, 14, V4L2_PIX_FMT_SGBRG14P
, 1,
110 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
111 { MEDIA_BUS_FMT_SGRBG14_1X14
, 14, V4L2_PIX_FMT_SGRBG14P
, 1,
112 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
113 { MEDIA_BUS_FMT_SRGGB14_1X14
, 14, V4L2_PIX_FMT_SRGGB14P
, 1,
114 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
115 { MEDIA_BUS_FMT_Y10_1X10
, 10, V4L2_PIX_FMT_Y10P
, 1,
116 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
117 { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE
, 16, V4L2_PIX_FMT_Y10
, 1,
118 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
121 static const struct camss_format_info formats_rdi_845
[] = {
122 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_UYVY
, 1,
123 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
124 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_VYUY
, 1,
125 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
126 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_YUYV
, 1,
127 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
128 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_YVYU
, 1,
129 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
130 { MEDIA_BUS_FMT_SBGGR8_1X8
, 8, V4L2_PIX_FMT_SBGGR8
, 1,
131 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
132 { MEDIA_BUS_FMT_SGBRG8_1X8
, 8, V4L2_PIX_FMT_SGBRG8
, 1,
133 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
134 { MEDIA_BUS_FMT_SGRBG8_1X8
, 8, V4L2_PIX_FMT_SGRBG8
, 1,
135 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
136 { MEDIA_BUS_FMT_SRGGB8_1X8
, 8, V4L2_PIX_FMT_SRGGB8
, 1,
137 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
138 { MEDIA_BUS_FMT_SBGGR10_1X10
, 10, V4L2_PIX_FMT_SBGGR10P
, 1,
139 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
140 { MEDIA_BUS_FMT_SGBRG10_1X10
, 10, V4L2_PIX_FMT_SGBRG10P
, 1,
141 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
142 { MEDIA_BUS_FMT_SGRBG10_1X10
, 10, V4L2_PIX_FMT_SGRBG10P
, 1,
143 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
144 { MEDIA_BUS_FMT_SRGGB10_1X10
, 10, V4L2_PIX_FMT_SRGGB10P
, 1,
145 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
146 { MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE
, 16, V4L2_PIX_FMT_SBGGR10
, 1,
147 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
148 { MEDIA_BUS_FMT_SBGGR12_1X12
, 12, V4L2_PIX_FMT_SBGGR12P
, 1,
149 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
150 { MEDIA_BUS_FMT_SGBRG12_1X12
, 12, V4L2_PIX_FMT_SGBRG12P
, 1,
151 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
152 { MEDIA_BUS_FMT_SGRBG12_1X12
, 12, V4L2_PIX_FMT_SGRBG12P
, 1,
153 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
154 { MEDIA_BUS_FMT_SRGGB12_1X12
, 12, V4L2_PIX_FMT_SRGGB12P
, 1,
155 PER_PLANE_DATA(0, 1, 1, 1, 1, 12) },
156 { MEDIA_BUS_FMT_SBGGR14_1X14
, 14, V4L2_PIX_FMT_SBGGR14P
, 1,
157 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
158 { MEDIA_BUS_FMT_SGBRG14_1X14
, 14, V4L2_PIX_FMT_SGBRG14P
, 1,
159 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
160 { MEDIA_BUS_FMT_SGRBG14_1X14
, 14, V4L2_PIX_FMT_SGRBG14P
, 1,
161 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
162 { MEDIA_BUS_FMT_SRGGB14_1X14
, 14, V4L2_PIX_FMT_SRGGB14P
, 1,
163 PER_PLANE_DATA(0, 1, 1, 1, 1, 14) },
164 { MEDIA_BUS_FMT_Y8_1X8
, 8, V4L2_PIX_FMT_GREY
, 1,
165 PER_PLANE_DATA(0, 1, 1, 1, 1, 8) },
166 { MEDIA_BUS_FMT_Y10_1X10
, 10, V4L2_PIX_FMT_Y10P
, 1,
167 PER_PLANE_DATA(0, 1, 1, 1, 1, 10) },
168 { MEDIA_BUS_FMT_Y10_2X8_PADHI_LE
, 16, V4L2_PIX_FMT_Y10
, 1,
169 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
172 static const struct camss_format_info formats_pix_8x16
[] = {
173 { MEDIA_BUS_FMT_YUYV8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
174 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
175 { MEDIA_BUS_FMT_YVYU8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
176 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
177 { MEDIA_BUS_FMT_UYVY8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
178 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
179 { MEDIA_BUS_FMT_VYUY8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
180 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
181 { MEDIA_BUS_FMT_YUYV8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
182 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
183 { MEDIA_BUS_FMT_YVYU8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
184 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
185 { MEDIA_BUS_FMT_UYVY8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
186 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
187 { MEDIA_BUS_FMT_VYUY8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
188 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
189 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
190 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
191 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
192 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
193 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
194 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
195 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
196 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
197 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
198 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
199 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
200 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
201 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
202 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
203 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
204 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
207 static const struct camss_format_info formats_pix_8x96
[] = {
208 { MEDIA_BUS_FMT_YUYV8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
209 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
210 { MEDIA_BUS_FMT_YVYU8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
211 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
212 { MEDIA_BUS_FMT_UYVY8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
213 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
214 { MEDIA_BUS_FMT_VYUY8_1_5X8
, 8, V4L2_PIX_FMT_NV12
, 1,
215 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
216 { MEDIA_BUS_FMT_YUYV8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
217 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
218 { MEDIA_BUS_FMT_YVYU8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
219 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
220 { MEDIA_BUS_FMT_UYVY8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
221 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
222 { MEDIA_BUS_FMT_VYUY8_1_5X8
, 8, V4L2_PIX_FMT_NV21
, 1,
223 PER_PLANE_DATA(0, 1, 1, 2, 3, 8) },
224 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
225 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
226 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
227 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
228 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
229 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
230 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_NV16
, 1,
231 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
232 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
233 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
234 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
235 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
236 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
237 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
238 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_NV61
, 1,
239 PER_PLANE_DATA(0, 1, 1, 1, 2, 8) },
240 { MEDIA_BUS_FMT_UYVY8_1X16
, 8, V4L2_PIX_FMT_UYVY
, 1,
241 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
242 { MEDIA_BUS_FMT_VYUY8_1X16
, 8, V4L2_PIX_FMT_VYUY
, 1,
243 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
244 { MEDIA_BUS_FMT_YUYV8_1X16
, 8, V4L2_PIX_FMT_YUYV
, 1,
245 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
246 { MEDIA_BUS_FMT_YVYU8_1X16
, 8, V4L2_PIX_FMT_YVYU
, 1,
247 PER_PLANE_DATA(0, 1, 1, 1, 1, 16) },
250 const struct camss_formats vfe_formats_rdi_8x16
= {
251 .nformats
= ARRAY_SIZE(formats_rdi_8x16
),
252 .formats
= formats_rdi_8x16
255 const struct camss_formats vfe_formats_pix_8x16
= {
256 .nformats
= ARRAY_SIZE(formats_pix_8x16
),
257 .formats
= formats_pix_8x16
260 const struct camss_formats vfe_formats_rdi_8x96
= {
261 .nformats
= ARRAY_SIZE(formats_rdi_8x96
),
262 .formats
= formats_rdi_8x96
265 const struct camss_formats vfe_formats_pix_8x96
= {
266 .nformats
= ARRAY_SIZE(formats_pix_8x96
),
267 .formats
= formats_pix_8x96
270 const struct camss_formats vfe_formats_rdi_845
= {
271 .nformats
= ARRAY_SIZE(formats_rdi_845
),
272 .formats
= formats_rdi_845
275 /* TODO: Replace with pix formats */
276 const struct camss_formats vfe_formats_pix_845
= {
277 .nformats
= ARRAY_SIZE(formats_rdi_845
),
278 .formats
= formats_rdi_845
281 static u32
vfe_src_pad_code(struct vfe_line
*line
, u32 sink_code
,
282 unsigned int index
, u32 src_req_code
)
284 struct vfe_device
*vfe
= to_vfe(line
);
286 switch (vfe
->camss
->res
->version
) {
290 case MEDIA_BUS_FMT_YUYV8_1X16
:
293 MEDIA_BUS_FMT_YUYV8_1X16
,
294 MEDIA_BUS_FMT_YUYV8_1_5X8
,
297 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
298 index
, src_req_code
);
300 case MEDIA_BUS_FMT_YVYU8_1X16
:
303 MEDIA_BUS_FMT_YVYU8_1X16
,
304 MEDIA_BUS_FMT_YVYU8_1_5X8
,
307 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
308 index
, src_req_code
);
310 case MEDIA_BUS_FMT_UYVY8_1X16
:
313 MEDIA_BUS_FMT_UYVY8_1X16
,
314 MEDIA_BUS_FMT_UYVY8_1_5X8
,
317 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
318 index
, src_req_code
);
320 case MEDIA_BUS_FMT_VYUY8_1X16
:
323 MEDIA_BUS_FMT_VYUY8_1X16
,
324 MEDIA_BUS_FMT_VYUY8_1_5X8
,
327 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
328 index
, src_req_code
);
343 case MEDIA_BUS_FMT_YUYV8_1X16
:
346 MEDIA_BUS_FMT_YUYV8_1X16
,
347 MEDIA_BUS_FMT_YVYU8_1X16
,
348 MEDIA_BUS_FMT_UYVY8_1X16
,
349 MEDIA_BUS_FMT_VYUY8_1X16
,
350 MEDIA_BUS_FMT_YUYV8_1_5X8
,
353 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
354 index
, src_req_code
);
356 case MEDIA_BUS_FMT_YVYU8_1X16
:
359 MEDIA_BUS_FMT_YVYU8_1X16
,
360 MEDIA_BUS_FMT_YUYV8_1X16
,
361 MEDIA_BUS_FMT_UYVY8_1X16
,
362 MEDIA_BUS_FMT_VYUY8_1X16
,
363 MEDIA_BUS_FMT_YVYU8_1_5X8
,
366 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
367 index
, src_req_code
);
369 case MEDIA_BUS_FMT_UYVY8_1X16
:
372 MEDIA_BUS_FMT_UYVY8_1X16
,
373 MEDIA_BUS_FMT_YUYV8_1X16
,
374 MEDIA_BUS_FMT_YVYU8_1X16
,
375 MEDIA_BUS_FMT_VYUY8_1X16
,
376 MEDIA_BUS_FMT_UYVY8_1_5X8
,
379 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
380 index
, src_req_code
);
382 case MEDIA_BUS_FMT_VYUY8_1X16
:
385 MEDIA_BUS_FMT_VYUY8_1X16
,
386 MEDIA_BUS_FMT_YUYV8_1X16
,
387 MEDIA_BUS_FMT_YVYU8_1X16
,
388 MEDIA_BUS_FMT_UYVY8_1X16
,
389 MEDIA_BUS_FMT_VYUY8_1_5X8
,
392 return camss_format_find_code(src_code
, ARRAY_SIZE(src_code
),
393 index
, src_req_code
);
406 int vfe_reset(struct vfe_device
*vfe
)
410 reinit_completion(&vfe
->reset_complete
);
412 vfe
->res
->hw_ops
->global_reset(vfe
);
414 time
= wait_for_completion_timeout(&vfe
->reset_complete
,
415 msecs_to_jiffies(VFE_RESET_TIMEOUT_MS
));
417 dev_err(vfe
->camss
->dev
, "VFE reset timeout\n");
424 static void vfe_init_outputs(struct vfe_device
*vfe
)
428 for (i
= 0; i
< vfe
->res
->line_num
; i
++) {
429 struct vfe_output
*output
= &vfe
->line
[i
].output
;
431 output
->state
= VFE_OUTPUT_OFF
;
432 output
->buf
[0] = NULL
;
433 output
->buf
[1] = NULL
;
434 INIT_LIST_HEAD(&output
->pending_bufs
);
438 static void vfe_reset_output_maps(struct vfe_device
*vfe
)
442 for (i
= 0; i
< ARRAY_SIZE(vfe
->wm_output_map
); i
++)
443 vfe
->wm_output_map
[i
] = VFE_LINE_NONE
;
446 int vfe_reserve_wm(struct vfe_device
*vfe
, enum vfe_line_id line_id
)
451 for (i
= 0; i
< ARRAY_SIZE(vfe
->wm_output_map
); i
++) {
452 if (vfe
->wm_output_map
[i
] == VFE_LINE_NONE
) {
453 vfe
->wm_output_map
[i
] = line_id
;
462 int vfe_release_wm(struct vfe_device
*vfe
, u8 wm
)
464 if (wm
>= ARRAY_SIZE(vfe
->wm_output_map
))
467 vfe
->wm_output_map
[wm
] = VFE_LINE_NONE
;
472 struct camss_buffer
*vfe_buf_get_pending(struct vfe_output
*output
)
474 struct camss_buffer
*buffer
= NULL
;
476 if (!list_empty(&output
->pending_bufs
)) {
477 buffer
= list_first_entry(&output
->pending_bufs
,
480 list_del(&buffer
->queue
);
486 void vfe_buf_add_pending(struct vfe_output
*output
,
487 struct camss_buffer
*buffer
)
489 INIT_LIST_HEAD(&buffer
->queue
);
490 list_add_tail(&buffer
->queue
, &output
->pending_bufs
);
494 * vfe_buf_flush_pending - Flush all pending buffers.
495 * @output: VFE output
496 * @state: vb2 buffer state
498 static void vfe_buf_flush_pending(struct vfe_output
*output
,
499 enum vb2_buffer_state state
)
501 struct camss_buffer
*buf
;
502 struct camss_buffer
*t
;
504 list_for_each_entry_safe(buf
, t
, &output
->pending_bufs
, queue
) {
505 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
506 list_del(&buf
->queue
);
510 int vfe_put_output(struct vfe_line
*line
)
512 struct vfe_device
*vfe
= to_vfe(line
);
513 struct vfe_output
*output
= &line
->output
;
517 spin_lock_irqsave(&vfe
->output_lock
, flags
);
519 for (i
= 0; i
< output
->wm_num
; i
++)
520 vfe_release_wm(vfe
, output
->wm_idx
[i
]);
522 output
->state
= VFE_OUTPUT_OFF
;
524 spin_unlock_irqrestore(&vfe
->output_lock
, flags
);
528 static int vfe_disable_output(struct vfe_line
*line
)
530 struct vfe_device
*vfe
= to_vfe(line
);
531 struct vfe_output
*output
= &line
->output
;
535 spin_lock_irqsave(&vfe
->output_lock
, flags
);
536 for (i
= 0; i
< output
->wm_num
; i
++)
537 vfe
->res
->hw_ops
->vfe_wm_stop(vfe
, output
->wm_idx
[i
]);
538 output
->gen2
.active_num
= 0;
539 spin_unlock_irqrestore(&vfe
->output_lock
, flags
);
541 return vfe_reset(vfe
);
545 * vfe_disable - Disable streaming on VFE line
548 * Return 0 on success or a negative error code otherwise
550 int vfe_disable(struct vfe_line
*line
)
552 struct vfe_device
*vfe
= to_vfe(line
);
555 ret
= vfe_disable_output(line
);
559 vfe_put_output(line
);
561 mutex_lock(&vfe
->stream_lock
);
565 mutex_unlock(&vfe
->stream_lock
);
572 * vfe_isr_comp_done() - Process composite image done interrupt
574 * @comp: Composite image id
576 void vfe_isr_comp_done(struct vfe_device
*vfe
, u8 comp
)
580 for (i
= 0; i
< ARRAY_SIZE(vfe
->wm_output_map
); i
++)
581 if (vfe
->wm_output_map
[i
] == VFE_LINE_PIX
) {
582 vfe
->isr_ops
.wm_done(vfe
, i
);
587 void vfe_isr_reset_ack(struct vfe_device
*vfe
)
589 complete(&vfe
->reset_complete
);
593 * vfe_pm_domain_off - Disable power domains specific to this VFE.
596 void vfe_pm_domain_off(struct vfe_device
*vfe
)
601 device_link_del(vfe
->genpd_link
);
602 vfe
->genpd_link
= NULL
;
606 * vfe_pm_domain_on - Enable power domains specific to this VFE.
609 int vfe_pm_domain_on(struct vfe_device
*vfe
)
611 struct camss
*camss
= vfe
->camss
;
616 vfe
->genpd_link
= device_link_add(camss
->dev
, vfe
->genpd
,
620 if (!vfe
->genpd_link
)
626 static int vfe_match_clock_names(struct vfe_device
*vfe
,
627 struct camss_clock
*clock
)
629 char vfe_name
[7]; /* vfeXXX\0 */
630 char vfe_lite_name
[12]; /* vfe_liteXXX\0 */
632 snprintf(vfe_name
, sizeof(vfe_name
), "vfe%d", vfe
->id
);
633 snprintf(vfe_lite_name
, sizeof(vfe_lite_name
), "vfe_lite%d", vfe
->id
);
635 return (!strcmp(clock
->name
, vfe_name
) ||
636 !strcmp(clock
->name
, vfe_lite_name
) ||
637 !strcmp(clock
->name
, "vfe_lite"));
641 * vfe_set_clock_rates - Calculate and set clock rates on VFE module
644 * Return 0 on success or a negative error code otherwise
646 static int vfe_set_clock_rates(struct vfe_device
*vfe
)
648 struct device
*dev
= vfe
->camss
->dev
;
649 u64 pixel_clock
[VFE_LINE_NUM_MAX
];
653 for (i
= VFE_LINE_RDI0
; i
< vfe
->res
->line_num
; i
++) {
654 ret
= camss_get_pixel_clock(&vfe
->line
[i
].subdev
.entity
,
660 for (i
= 0; i
< vfe
->nclocks
; i
++) {
661 struct camss_clock
*clock
= &vfe
->clock
[i
];
663 if (vfe_match_clock_names(vfe
, clock
)) {
667 for (j
= VFE_LINE_RDI0
; j
< vfe
->res
->line_num
; j
++) {
671 if (j
== VFE_LINE_PIX
) {
672 tmp
= pixel_clock
[j
];
674 struct vfe_line
*l
= &vfe
->line
[j
];
676 bpp
= camss_format_get_bpp(l
->formats
,
678 l
->fmt
[MSM_VFE_PAD_SINK
].code
);
679 tmp
= pixel_clock
[j
] * bpp
/ 64;
686 camss_add_clock_margin(&min_rate
);
688 for (j
= 0; j
< clock
->nfreqs
; j
++)
689 if (min_rate
< clock
->freq
[j
])
692 if (j
== clock
->nfreqs
) {
694 "Pixel clock is too high for VFE");
698 /* if sensor pixel clock is not available */
699 /* set highest possible VFE clock rate */
701 j
= clock
->nfreqs
- 1;
703 rate
= clk_round_rate(clock
->clk
, clock
->freq
[j
]);
705 dev_err(dev
, "clk round rate failed: %ld\n",
710 ret
= clk_set_rate(clock
->clk
, rate
);
712 dev_err(dev
, "clk set rate failed: %d\n", ret
);
722 * vfe_check_clock_rates - Check current clock rates on VFE module
725 * Return 0 if current clock rates are suitable for a new pipeline
726 * or a negative error code otherwise
728 static int vfe_check_clock_rates(struct vfe_device
*vfe
)
730 u64 pixel_clock
[VFE_LINE_NUM_MAX
];
734 for (i
= VFE_LINE_RDI0
; i
< vfe
->res
->line_num
; i
++) {
735 ret
= camss_get_pixel_clock(&vfe
->line
[i
].subdev
.entity
,
741 for (i
= 0; i
< vfe
->nclocks
; i
++) {
742 struct camss_clock
*clock
= &vfe
->clock
[i
];
744 if (vfe_match_clock_names(vfe
, clock
)) {
748 for (j
= VFE_LINE_RDI0
; j
< vfe
->res
->line_num
; j
++) {
752 if (j
== VFE_LINE_PIX
) {
753 tmp
= pixel_clock
[j
];
755 struct vfe_line
*l
= &vfe
->line
[j
];
757 bpp
= camss_format_get_bpp(l
->formats
,
759 l
->fmt
[MSM_VFE_PAD_SINK
].code
);
760 tmp
= pixel_clock
[j
] * bpp
/ 64;
767 camss_add_clock_margin(&min_rate
);
769 rate
= clk_get_rate(clock
->clk
);
779 * vfe_get - Power up and reset VFE module
782 * Return 0 on success or a negative error code otherwise
784 int vfe_get(struct vfe_device
*vfe
)
788 mutex_lock(&vfe
->power_lock
);
790 if (vfe
->power_count
== 0) {
791 ret
= vfe
->res
->hw_ops
->pm_domain_on(vfe
);
793 goto error_pm_domain
;
795 ret
= pm_runtime_resume_and_get(vfe
->camss
->dev
);
797 goto error_domain_off
;
799 ret
= vfe_set_clock_rates(vfe
);
801 goto error_pm_runtime_get
;
803 ret
= camss_enable_clocks(vfe
->nclocks
, vfe
->clock
,
806 goto error_pm_runtime_get
;
808 ret
= vfe_reset(vfe
);
812 vfe_reset_output_maps(vfe
);
814 vfe_init_outputs(vfe
);
816 vfe
->res
->hw_ops
->hw_version(vfe
);
818 ret
= vfe_check_clock_rates(vfe
);
820 goto error_pm_domain
;
824 mutex_unlock(&vfe
->power_lock
);
829 camss_disable_clocks(vfe
->nclocks
, vfe
->clock
);
831 error_pm_runtime_get
:
832 pm_runtime_put_sync(vfe
->camss
->dev
);
834 vfe
->res
->hw_ops
->pm_domain_off(vfe
);
837 mutex_unlock(&vfe
->power_lock
);
843 * vfe_put - Power down VFE module
846 void vfe_put(struct vfe_device
*vfe
)
848 mutex_lock(&vfe
->power_lock
);
850 if (vfe
->power_count
== 0) {
851 dev_err(vfe
->camss
->dev
, "vfe power off on power_count == 0\n");
853 } else if (vfe
->power_count
== 1) {
854 if (vfe
->was_streaming
) {
855 vfe
->was_streaming
= 0;
856 vfe
->res
->hw_ops
->vfe_halt(vfe
);
858 camss_disable_clocks(vfe
->nclocks
, vfe
->clock
);
859 pm_runtime_put_sync(vfe
->camss
->dev
);
860 vfe
->res
->hw_ops
->pm_domain_off(vfe
);
866 mutex_unlock(&vfe
->power_lock
);
870 * vfe_flush_buffers - Return all vb2 buffers
871 * @vid: Video device structure
872 * @state: vb2 buffer state of the returned buffers
874 * Return all buffers to vb2. This includes queued pending buffers (still
875 * unused) and any buffers given to the hardware but again still not used.
877 * Return 0 on success or a negative error code otherwise
879 int vfe_flush_buffers(struct camss_video
*vid
,
880 enum vb2_buffer_state state
)
882 struct vfe_line
*line
= container_of(vid
, struct vfe_line
, video_out
);
883 struct vfe_device
*vfe
= to_vfe(line
);
884 struct vfe_output
*output
;
887 output
= &line
->output
;
889 spin_lock_irqsave(&vfe
->output_lock
, flags
);
891 vfe_buf_flush_pending(output
, state
);
894 vb2_buffer_done(&output
->buf
[0]->vb
.vb2_buf
, state
);
897 vb2_buffer_done(&output
->buf
[1]->vb
.vb2_buf
, state
);
899 if (output
->last_buffer
) {
900 vb2_buffer_done(&output
->last_buffer
->vb
.vb2_buf
, state
);
901 output
->last_buffer
= NULL
;
904 spin_unlock_irqrestore(&vfe
->output_lock
, flags
);
910 * vfe_set_power - Power on/off VFE module
911 * @sd: VFE V4L2 subdevice
912 * @on: Requested power state
914 * Return 0 on success or a negative error code otherwise
916 static int vfe_set_power(struct v4l2_subdev
*sd
, int on
)
918 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
919 struct vfe_device
*vfe
= to_vfe(line
);
934 * vfe_set_stream - Enable/disable streaming on VFE module
935 * @sd: VFE V4L2 subdevice
936 * @enable: Requested streaming state
938 * Main configuration of VFE module is triggered here.
940 * Return 0 on success or a negative error code otherwise
942 static int vfe_set_stream(struct v4l2_subdev
*sd
, int enable
)
944 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
945 struct vfe_device
*vfe
= to_vfe(line
);
949 line
->output
.state
= VFE_OUTPUT_RESERVED
;
950 ret
= vfe
->res
->hw_ops
->vfe_enable(line
);
952 dev_err(vfe
->camss
->dev
,
953 "Failed to enable vfe outputs\n");
955 ret
= vfe
->res
->hw_ops
->vfe_disable(line
);
957 dev_err(vfe
->camss
->dev
,
958 "Failed to disable vfe outputs\n");
965 * __vfe_get_format - Get pointer to format structure
967 * @sd_state: V4L2 subdev state
968 * @pad: pad from which format is requested
969 * @which: TRY or ACTIVE format
971 * Return pointer to TRY or ACTIVE format structure
973 static struct v4l2_mbus_framefmt
*
974 __vfe_get_format(struct vfe_line
*line
,
975 struct v4l2_subdev_state
*sd_state
,
977 enum v4l2_subdev_format_whence which
)
979 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
980 return v4l2_subdev_state_get_format(sd_state
, pad
);
982 return &line
->fmt
[pad
];
986 * __vfe_get_compose - Get pointer to compose selection structure
988 * @sd_state: V4L2 subdev state
989 * @which: TRY or ACTIVE format
991 * Return pointer to TRY or ACTIVE compose rectangle structure
993 static struct v4l2_rect
*
994 __vfe_get_compose(struct vfe_line
*line
,
995 struct v4l2_subdev_state
*sd_state
,
996 enum v4l2_subdev_format_whence which
)
998 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
999 return v4l2_subdev_state_get_compose(sd_state
,
1002 return &line
->compose
;
1006 * __vfe_get_crop - Get pointer to crop selection structure
1008 * @sd_state: V4L2 subdev state
1009 * @which: TRY or ACTIVE format
1011 * Return pointer to TRY or ACTIVE crop rectangle structure
1013 static struct v4l2_rect
*
1014 __vfe_get_crop(struct vfe_line
*line
,
1015 struct v4l2_subdev_state
*sd_state
,
1016 enum v4l2_subdev_format_whence which
)
1018 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
1019 return v4l2_subdev_state_get_crop(sd_state
, MSM_VFE_PAD_SRC
);
1025 * vfe_try_format - Handle try format by pad subdev method
1027 * @sd_state: V4L2 subdev state
1028 * @pad: pad on which format is requested
1029 * @fmt: pointer to v4l2 format structure
1030 * @which: wanted subdev format
1032 static void vfe_try_format(struct vfe_line
*line
,
1033 struct v4l2_subdev_state
*sd_state
,
1035 struct v4l2_mbus_framefmt
*fmt
,
1036 enum v4l2_subdev_format_whence which
)
1042 case MSM_VFE_PAD_SINK
:
1043 /* Set format on sink pad */
1045 for (i
= 0; i
< line
->nformats
; i
++)
1046 if (fmt
->code
== line
->formats
[i
].code
)
1049 /* If not found, use UYVY as default */
1050 if (i
>= line
->nformats
)
1051 fmt
->code
= MEDIA_BUS_FMT_UYVY8_1X16
;
1053 fmt
->width
= clamp_t(u32
, fmt
->width
, 1, 8191);
1054 fmt
->height
= clamp_t(u32
, fmt
->height
, 1, 8191);
1056 fmt
->field
= V4L2_FIELD_NONE
;
1057 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
1061 case MSM_VFE_PAD_SRC
:
1062 /* Set and return a format same as sink pad */
1065 *fmt
= *__vfe_get_format(line
, sd_state
, MSM_VFE_PAD_SINK
,
1068 fmt
->code
= vfe_src_pad_code(line
, fmt
->code
, 0, code
);
1070 if (line
->id
== VFE_LINE_PIX
) {
1071 struct v4l2_rect
*rect
;
1073 rect
= __vfe_get_crop(line
, sd_state
, which
);
1075 fmt
->width
= rect
->width
;
1076 fmt
->height
= rect
->height
;
1082 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
1086 * vfe_try_compose - Handle try compose selection by pad subdev method
1088 * @sd_state: V4L2 subdev state
1089 * @rect: pointer to v4l2 rect structure
1090 * @which: wanted subdev format
1092 static void vfe_try_compose(struct vfe_line
*line
,
1093 struct v4l2_subdev_state
*sd_state
,
1094 struct v4l2_rect
*rect
,
1095 enum v4l2_subdev_format_whence which
)
1097 struct v4l2_mbus_framefmt
*fmt
;
1099 fmt
= __vfe_get_format(line
, sd_state
, MSM_VFE_PAD_SINK
, which
);
1101 if (rect
->width
> fmt
->width
)
1102 rect
->width
= fmt
->width
;
1104 if (rect
->height
> fmt
->height
)
1105 rect
->height
= fmt
->height
;
1107 if (fmt
->width
> rect
->width
* SCALER_RATIO_MAX
)
1108 rect
->width
= (fmt
->width
+ SCALER_RATIO_MAX
- 1) /
1111 rect
->width
&= ~0x1;
1113 if (fmt
->height
> rect
->height
* SCALER_RATIO_MAX
)
1114 rect
->height
= (fmt
->height
+ SCALER_RATIO_MAX
- 1) /
1117 if (rect
->width
< 16)
1120 if (rect
->height
< 4)
1125 * vfe_try_crop - Handle try crop selection by pad subdev method
1127 * @sd_state: V4L2 subdev state
1128 * @rect: pointer to v4l2 rect structure
1129 * @which: wanted subdev format
1131 static void vfe_try_crop(struct vfe_line
*line
,
1132 struct v4l2_subdev_state
*sd_state
,
1133 struct v4l2_rect
*rect
,
1134 enum v4l2_subdev_format_whence which
)
1136 struct v4l2_rect
*compose
;
1138 compose
= __vfe_get_compose(line
, sd_state
, which
);
1140 if (rect
->width
> compose
->width
)
1141 rect
->width
= compose
->width
;
1143 if (rect
->width
+ rect
->left
> compose
->width
)
1144 rect
->left
= compose
->width
- rect
->width
;
1146 if (rect
->height
> compose
->height
)
1147 rect
->height
= compose
->height
;
1149 if (rect
->height
+ rect
->top
> compose
->height
)
1150 rect
->top
= compose
->height
- rect
->height
;
1152 /* wm in line based mode writes multiple of 16 horizontally */
1153 rect
->left
+= (rect
->width
& 0xf) >> 1;
1154 rect
->width
&= ~0xf;
1156 if (rect
->width
< 16) {
1161 if (rect
->height
< 4) {
1168 * vfe_enum_mbus_code - Handle pixel format enumeration
1169 * @sd: VFE V4L2 subdevice
1170 * @sd_state: V4L2 subdev state
1171 * @code: pointer to v4l2_subdev_mbus_code_enum structure
1173 * return -EINVAL or zero on success
1175 static int vfe_enum_mbus_code(struct v4l2_subdev
*sd
,
1176 struct v4l2_subdev_state
*sd_state
,
1177 struct v4l2_subdev_mbus_code_enum
*code
)
1179 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
1181 if (code
->pad
== MSM_VFE_PAD_SINK
) {
1182 if (code
->index
>= line
->nformats
)
1185 code
->code
= line
->formats
[code
->index
].code
;
1187 struct v4l2_mbus_framefmt
*sink_fmt
;
1189 sink_fmt
= __vfe_get_format(line
, sd_state
, MSM_VFE_PAD_SINK
,
1192 code
->code
= vfe_src_pad_code(line
, sink_fmt
->code
,
1202 * vfe_enum_frame_size - Handle frame size enumeration
1203 * @sd: VFE V4L2 subdevice
1204 * @sd_state: V4L2 subdev state
1205 * @fse: pointer to v4l2_subdev_frame_size_enum structure
1207 * Return -EINVAL or zero on success
1209 static int vfe_enum_frame_size(struct v4l2_subdev
*sd
,
1210 struct v4l2_subdev_state
*sd_state
,
1211 struct v4l2_subdev_frame_size_enum
*fse
)
1213 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
1214 struct v4l2_mbus_framefmt format
;
1216 if (fse
->index
!= 0)
1219 format
.code
= fse
->code
;
1222 vfe_try_format(line
, sd_state
, fse
->pad
, &format
, fse
->which
);
1223 fse
->min_width
= format
.width
;
1224 fse
->min_height
= format
.height
;
1226 if (format
.code
!= fse
->code
)
1229 format
.code
= fse
->code
;
1232 vfe_try_format(line
, sd_state
, fse
->pad
, &format
, fse
->which
);
1233 fse
->max_width
= format
.width
;
1234 fse
->max_height
= format
.height
;
1240 * vfe_get_format - Handle get format by pads subdev method
1241 * @sd: VFE V4L2 subdevice
1242 * @sd_state: V4L2 subdev state
1243 * @fmt: pointer to v4l2 subdev format structure
1245 * Return -EINVAL or zero on success
1247 static int vfe_get_format(struct v4l2_subdev
*sd
,
1248 struct v4l2_subdev_state
*sd_state
,
1249 struct v4l2_subdev_format
*fmt
)
1251 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
1252 struct v4l2_mbus_framefmt
*format
;
1254 format
= __vfe_get_format(line
, sd_state
, fmt
->pad
, fmt
->which
);
1258 fmt
->format
= *format
;
1263 static int vfe_set_selection(struct v4l2_subdev
*sd
,
1264 struct v4l2_subdev_state
*sd_state
,
1265 struct v4l2_subdev_selection
*sel
);
1268 * vfe_set_format - Handle set format by pads subdev method
1269 * @sd: VFE V4L2 subdevice
1270 * @sd_state: V4L2 subdev state
1271 * @fmt: pointer to v4l2 subdev format structure
1273 * Return -EINVAL or zero on success
1275 static int vfe_set_format(struct v4l2_subdev
*sd
,
1276 struct v4l2_subdev_state
*sd_state
,
1277 struct v4l2_subdev_format
*fmt
)
1279 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
1280 struct v4l2_mbus_framefmt
*format
;
1282 format
= __vfe_get_format(line
, sd_state
, fmt
->pad
, fmt
->which
);
1286 vfe_try_format(line
, sd_state
, fmt
->pad
, &fmt
->format
, fmt
->which
);
1287 *format
= fmt
->format
;
1289 if (fmt
->pad
== MSM_VFE_PAD_SINK
) {
1290 struct v4l2_subdev_selection sel
= { 0 };
1293 /* Propagate the format from sink to source */
1294 format
= __vfe_get_format(line
, sd_state
, MSM_VFE_PAD_SRC
,
1297 *format
= fmt
->format
;
1298 vfe_try_format(line
, sd_state
, MSM_VFE_PAD_SRC
, format
,
1301 if (line
->id
!= VFE_LINE_PIX
)
1304 /* Reset sink pad compose selection */
1305 sel
.which
= fmt
->which
;
1306 sel
.pad
= MSM_VFE_PAD_SINK
;
1307 sel
.target
= V4L2_SEL_TGT_COMPOSE
;
1308 sel
.r
.width
= fmt
->format
.width
;
1309 sel
.r
.height
= fmt
->format
.height
;
1310 ret
= vfe_set_selection(sd
, sd_state
, &sel
);
1319 * vfe_get_selection - Handle get selection by pads subdev method
1320 * @sd: VFE V4L2 subdevice
1321 * @sd_state: V4L2 subdev state
1322 * @sel: pointer to v4l2 subdev selection structure
1324 * Return -EINVAL or zero on success
1326 static int vfe_get_selection(struct v4l2_subdev
*sd
,
1327 struct v4l2_subdev_state
*sd_state
,
1328 struct v4l2_subdev_selection
*sel
)
1330 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
1331 struct v4l2_subdev_format fmt
= { 0 };
1332 struct v4l2_rect
*rect
;
1335 if (line
->id
!= VFE_LINE_PIX
)
1338 if (sel
->pad
== MSM_VFE_PAD_SINK
)
1339 switch (sel
->target
) {
1340 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1342 fmt
.which
= sel
->which
;
1343 ret
= vfe_get_format(sd
, sd_state
, &fmt
);
1349 sel
->r
.width
= fmt
.format
.width
;
1350 sel
->r
.height
= fmt
.format
.height
;
1352 case V4L2_SEL_TGT_COMPOSE
:
1353 rect
= __vfe_get_compose(line
, sd_state
, sel
->which
);
1362 else if (sel
->pad
== MSM_VFE_PAD_SRC
)
1363 switch (sel
->target
) {
1364 case V4L2_SEL_TGT_CROP_BOUNDS
:
1365 rect
= __vfe_get_compose(line
, sd_state
, sel
->which
);
1369 sel
->r
.left
= rect
->left
;
1370 sel
->r
.top
= rect
->top
;
1371 sel
->r
.width
= rect
->width
;
1372 sel
->r
.height
= rect
->height
;
1374 case V4L2_SEL_TGT_CROP
:
1375 rect
= __vfe_get_crop(line
, sd_state
, sel
->which
);
1389 * vfe_set_selection - Handle set selection by pads subdev method
1390 * @sd: VFE V4L2 subdevice
1391 * @sd_state: V4L2 subdev state
1392 * @sel: pointer to v4l2 subdev selection structure
1394 * Return -EINVAL or zero on success
1396 static int vfe_set_selection(struct v4l2_subdev
*sd
,
1397 struct v4l2_subdev_state
*sd_state
,
1398 struct v4l2_subdev_selection
*sel
)
1400 struct vfe_line
*line
= v4l2_get_subdevdata(sd
);
1401 struct v4l2_rect
*rect
;
1404 if (line
->id
!= VFE_LINE_PIX
)
1407 if (sel
->target
== V4L2_SEL_TGT_COMPOSE
&&
1408 sel
->pad
== MSM_VFE_PAD_SINK
) {
1409 struct v4l2_subdev_selection crop
= { 0 };
1411 rect
= __vfe_get_compose(line
, sd_state
, sel
->which
);
1415 vfe_try_compose(line
, sd_state
, &sel
->r
, sel
->which
);
1418 /* Reset source crop selection */
1419 crop
.which
= sel
->which
;
1420 crop
.pad
= MSM_VFE_PAD_SRC
;
1421 crop
.target
= V4L2_SEL_TGT_CROP
;
1423 ret
= vfe_set_selection(sd
, sd_state
, &crop
);
1424 } else if (sel
->target
== V4L2_SEL_TGT_CROP
&&
1425 sel
->pad
== MSM_VFE_PAD_SRC
) {
1426 struct v4l2_subdev_format fmt
= { 0 };
1428 rect
= __vfe_get_crop(line
, sd_state
, sel
->which
);
1432 vfe_try_crop(line
, sd_state
, &sel
->r
, sel
->which
);
1435 /* Reset source pad format width and height */
1436 fmt
.which
= sel
->which
;
1437 fmt
.pad
= MSM_VFE_PAD_SRC
;
1438 ret
= vfe_get_format(sd
, sd_state
, &fmt
);
1442 fmt
.format
.width
= rect
->width
;
1443 fmt
.format
.height
= rect
->height
;
1444 ret
= vfe_set_format(sd
, sd_state
, &fmt
);
1453 * vfe_init_formats - Initialize formats on all pads
1454 * @sd: VFE V4L2 subdevice
1455 * @fh: V4L2 subdev file handle
1457 * Initialize all pad formats with default values.
1459 * Return 0 on success or a negative error code otherwise
1461 static int vfe_init_formats(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1463 struct v4l2_subdev_format format
= {
1464 .pad
= MSM_VFE_PAD_SINK
,
1465 .which
= fh
? V4L2_SUBDEV_FORMAT_TRY
:
1466 V4L2_SUBDEV_FORMAT_ACTIVE
,
1468 .code
= MEDIA_BUS_FMT_UYVY8_1X16
,
1474 return vfe_set_format(sd
, fh
? fh
->state
: NULL
, &format
);
1478 * msm_vfe_subdev_init - Initialize VFE device structure and resources
1480 * @res: VFE module resources table
1482 * Return 0 on success or a negative error code otherwise
1484 int msm_vfe_subdev_init(struct camss
*camss
, struct vfe_device
*vfe
,
1485 const struct camss_subdev_resources
*res
, u8 id
)
1487 struct device
*dev
= camss
->dev
;
1488 struct platform_device
*pdev
= to_platform_device(dev
);
1492 if (!res
->vfe
.line_num
)
1495 vfe
->res
= &res
->vfe
;
1496 vfe
->res
->hw_ops
->subdev_init(dev
, vfe
);
1500 if (res
->vfe
.pd_name
) {
1501 vfe
->genpd
= dev_pm_domain_attach_by_name(camss
->dev
,
1503 if (IS_ERR(vfe
->genpd
)) {
1504 ret
= PTR_ERR(vfe
->genpd
);
1509 if (!vfe
->genpd
&& res
->vfe
.has_pd
) {
1511 * Legacy magic index.
1513 * power-domain = <VFE_X>,
1516 * id must correspondng to the index of the VFE which must
1517 * come before the TOP GDSC. VFE Lite has no individually
1518 * collapasible domain which is why id < vfe_num is a valid
1521 vfe
->genpd
= dev_pm_domain_attach_by_id(camss
->dev
, id
);
1522 if (IS_ERR(vfe
->genpd
))
1523 return PTR_ERR(vfe
->genpd
);
1528 vfe
->base
= devm_platform_ioremap_resource_byname(pdev
, res
->reg
[0]);
1529 if (IS_ERR(vfe
->base
)) {
1530 dev_err(dev
, "could not map memory\n");
1531 return PTR_ERR(vfe
->base
);
1536 ret
= platform_get_irq_byname(pdev
, res
->interrupt
[0]);
1541 snprintf(vfe
->irq_name
, sizeof(vfe
->irq_name
), "%s_%s%d",
1542 dev_name(dev
), MSM_VFE_NAME
, id
);
1543 ret
= devm_request_irq(dev
, vfe
->irq
, vfe
->res
->hw_ops
->isr
,
1544 IRQF_TRIGGER_RISING
, vfe
->irq_name
, vfe
);
1546 dev_err(dev
, "request_irq failed: %d\n", ret
);
1553 while (res
->clock
[vfe
->nclocks
])
1556 vfe
->clock
= devm_kcalloc(dev
, vfe
->nclocks
, sizeof(*vfe
->clock
),
1561 for (i
= 0; i
< vfe
->nclocks
; i
++) {
1562 struct camss_clock
*clock
= &vfe
->clock
[i
];
1564 clock
->clk
= devm_clk_get(dev
, res
->clock
[i
]);
1565 if (IS_ERR(clock
->clk
))
1566 return PTR_ERR(clock
->clk
);
1568 clock
->name
= res
->clock
[i
];
1571 while (res
->clock_rate
[i
][clock
->nfreqs
])
1574 if (!clock
->nfreqs
) {
1579 clock
->freq
= devm_kcalloc(dev
,
1581 sizeof(*clock
->freq
),
1586 for (j
= 0; j
< clock
->nfreqs
; j
++)
1587 clock
->freq
[j
] = res
->clock_rate
[i
][j
];
1590 mutex_init(&vfe
->power_lock
);
1591 vfe
->power_count
= 0;
1593 mutex_init(&vfe
->stream_lock
);
1594 vfe
->stream_count
= 0;
1596 spin_lock_init(&vfe
->output_lock
);
1600 vfe
->reg_update
= 0;
1602 for (i
= VFE_LINE_RDI0
; i
< vfe
->res
->line_num
; i
++) {
1603 struct vfe_line
*l
= &vfe
->line
[i
];
1605 l
->video_out
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1606 l
->video_out
.camss
= camss
;
1608 init_completion(&l
->output
.sof
);
1609 init_completion(&l
->output
.reg_update
);
1611 if (i
== VFE_LINE_PIX
) {
1612 l
->nformats
= res
->vfe
.formats_pix
->nformats
;
1613 l
->formats
= res
->vfe
.formats_pix
->formats
;
1615 l
->nformats
= res
->vfe
.formats_rdi
->nformats
;
1616 l
->formats
= res
->vfe
.formats_rdi
->formats
;
1620 init_completion(&vfe
->reset_complete
);
1621 init_completion(&vfe
->halt_complete
);
1627 * msm_vfe_genpd_cleanup - Cleanup VFE genpd linkages
1630 void msm_vfe_genpd_cleanup(struct vfe_device
*vfe
)
1632 if (vfe
->genpd_link
)
1633 device_link_del(vfe
->genpd_link
);
1636 dev_pm_domain_detach(vfe
->genpd
, true);
1640 * vfe_link_setup - Setup VFE connections
1641 * @entity: Pointer to media entity structure
1642 * @local: Pointer to local pad
1643 * @remote: Pointer to remote pad
1644 * @flags: Link flags
1646 * Return 0 on success
1648 static int vfe_link_setup(struct media_entity
*entity
,
1649 const struct media_pad
*local
,
1650 const struct media_pad
*remote
, u32 flags
)
1652 if (flags
& MEDIA_LNK_FL_ENABLED
)
1653 if (media_pad_remote_pad_first(local
))
1659 static const struct v4l2_subdev_core_ops vfe_core_ops
= {
1660 .s_power
= vfe_set_power
,
1663 static const struct v4l2_subdev_video_ops vfe_video_ops
= {
1664 .s_stream
= vfe_set_stream
,
1667 static const struct v4l2_subdev_pad_ops vfe_pad_ops
= {
1668 .enum_mbus_code
= vfe_enum_mbus_code
,
1669 .enum_frame_size
= vfe_enum_frame_size
,
1670 .get_fmt
= vfe_get_format
,
1671 .set_fmt
= vfe_set_format
,
1672 .get_selection
= vfe_get_selection
,
1673 .set_selection
= vfe_set_selection
,
1676 static const struct v4l2_subdev_ops vfe_v4l2_ops
= {
1677 .core
= &vfe_core_ops
,
1678 .video
= &vfe_video_ops
,
1679 .pad
= &vfe_pad_ops
,
1682 static const struct v4l2_subdev_internal_ops vfe_v4l2_internal_ops
= {
1683 .open
= vfe_init_formats
,
1686 static const struct media_entity_operations vfe_media_ops
= {
1687 .link_setup
= vfe_link_setup
,
1688 .link_validate
= v4l2_subdev_link_validate
,
1691 static int vfe_bpl_align(struct vfe_device
*vfe
)
1695 switch (vfe
->camss
->res
->version
) {
1709 * msm_vfe_register_entities - Register subdev node for VFE module
1711 * @v4l2_dev: V4L2 device
1713 * Initialize and register a subdev node for the VFE module. Then
1714 * call msm_video_register() to register the video device node which
1715 * will be connected to this subdev node. Then actually create the
1716 * media link between them.
1718 * Return 0 on success or a negative error code otherwise
1720 int msm_vfe_register_entities(struct vfe_device
*vfe
,
1721 struct v4l2_device
*v4l2_dev
)
1723 struct device
*dev
= vfe
->camss
->dev
;
1724 struct v4l2_subdev
*sd
;
1725 struct media_pad
*pads
;
1726 struct camss_video
*video_out
;
1730 for (i
= 0; i
< vfe
->res
->line_num
; i
++) {
1733 sd
= &vfe
->line
[i
].subdev
;
1734 pads
= vfe
->line
[i
].pads
;
1735 video_out
= &vfe
->line
[i
].video_out
;
1737 v4l2_subdev_init(sd
, &vfe_v4l2_ops
);
1738 sd
->internal_ops
= &vfe_v4l2_internal_ops
;
1739 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1740 if (i
== VFE_LINE_PIX
)
1741 snprintf(sd
->name
, ARRAY_SIZE(sd
->name
), "%s%d_%s",
1742 MSM_VFE_NAME
, vfe
->id
, "pix");
1744 snprintf(sd
->name
, ARRAY_SIZE(sd
->name
), "%s%d_%s%d",
1745 MSM_VFE_NAME
, vfe
->id
, "rdi", i
);
1747 v4l2_set_subdevdata(sd
, &vfe
->line
[i
]);
1749 ret
= vfe_init_formats(sd
, NULL
);
1751 dev_err(dev
, "Failed to init format: %d\n", ret
);
1755 pads
[MSM_VFE_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1756 pads
[MSM_VFE_PAD_SRC
].flags
= MEDIA_PAD_FL_SOURCE
;
1758 sd
->entity
.function
= MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER
;
1759 sd
->entity
.ops
= &vfe_media_ops
;
1760 ret
= media_entity_pads_init(&sd
->entity
, MSM_VFE_PADS_NUM
,
1763 dev_err(dev
, "Failed to init media entity: %d\n", ret
);
1767 ret
= v4l2_device_register_subdev(v4l2_dev
, sd
);
1769 dev_err(dev
, "Failed to register subdev: %d\n", ret
);
1770 goto error_reg_subdev
;
1773 video_out
->ops
= &vfe
->video_ops
;
1774 video_out
->bpl_alignment
= vfe_bpl_align(vfe
);
1775 video_out
->line_based
= 0;
1776 if (i
== VFE_LINE_PIX
) {
1777 video_out
->bpl_alignment
= 16;
1778 video_out
->line_based
= 1;
1781 video_out
->nformats
= vfe
->line
[i
].nformats
;
1782 video_out
->formats
= vfe
->line
[i
].formats
;
1784 snprintf(name
, ARRAY_SIZE(name
), "%s%d_%s%d",
1785 MSM_VFE_NAME
, vfe
->id
, "video", i
);
1786 ret
= msm_video_register(video_out
, v4l2_dev
, name
);
1788 dev_err(dev
, "Failed to register video node: %d\n",
1790 goto error_reg_video
;
1793 ret
= media_create_pad_link(
1794 &sd
->entity
, MSM_VFE_PAD_SRC
,
1795 &video_out
->vdev
.entity
, 0,
1796 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
1798 dev_err(dev
, "Failed to link %s->%s entities: %d\n",
1799 sd
->entity
.name
, video_out
->vdev
.entity
.name
,
1808 msm_video_unregister(video_out
);
1811 v4l2_device_unregister_subdev(sd
);
1814 media_entity_cleanup(&sd
->entity
);
1817 for (i
--; i
>= 0; i
--) {
1818 sd
= &vfe
->line
[i
].subdev
;
1819 video_out
= &vfe
->line
[i
].video_out
;
1821 msm_video_unregister(video_out
);
1822 v4l2_device_unregister_subdev(sd
);
1823 media_entity_cleanup(&sd
->entity
);
1830 * msm_vfe_unregister_entities - Unregister VFE module subdev node
1833 void msm_vfe_unregister_entities(struct vfe_device
*vfe
)
1837 mutex_destroy(&vfe
->power_lock
);
1838 mutex_destroy(&vfe
->stream_lock
);
1840 for (i
= 0; i
< vfe
->res
->line_num
; i
++) {
1841 struct v4l2_subdev
*sd
= &vfe
->line
[i
].subdev
;
1842 struct camss_video
*video_out
= &vfe
->line
[i
].video_out
;
1844 msm_video_unregister(video_out
);
1845 v4l2_device_unregister_subdev(sd
);
1846 media_entity_cleanup(&sd
->entity
);
1850 bool vfe_is_lite(struct vfe_device
*vfe
)
1852 return vfe
->camss
->res
->vfe_res
[vfe
->id
].vfe
.is_lite
;