• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * camss-vfe-170.c
4  *
5  * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module v170
6  *
7  * Copyright (C) 2020-2021 Linaro Ltd.
8  */
9 
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/iopoll.h>
13 
14 #include "camss.h"
15 #include "camss-vfe.h"
16 
17 #define VFE_HW_VERSION				(0x000)
18 
19 #define VFE_GLOBAL_RESET_CMD			(0x018)
20 #define		GLOBAL_RESET_CMD_CORE		BIT(0)
21 #define		GLOBAL_RESET_CMD_CAMIF		BIT(1)
22 #define		GLOBAL_RESET_CMD_BUS		BIT(2)
23 #define		GLOBAL_RESET_CMD_BUS_BDG	BIT(3)
24 #define		GLOBAL_RESET_CMD_REGISTER	BIT(4)
25 #define		GLOBAL_RESET_CMD_PM		BIT(5)
26 #define		GLOBAL_RESET_CMD_BUS_MISR	BIT(6)
27 #define		GLOBAL_RESET_CMD_TESTGEN	BIT(7)
28 #define		GLOBAL_RESET_CMD_DSP		BIT(8)
29 #define		GLOBAL_RESET_CMD_IDLE_CGC	BIT(9)
30 #define		GLOBAL_RESET_CMD_RDI0		BIT(10)
31 #define		GLOBAL_RESET_CMD_RDI1		BIT(11)
32 #define		GLOBAL_RESET_CMD_RDI2		BIT(12)
33 #define		GLOBAL_RESET_CMD_RDI3		BIT(13)
34 #define		GLOBAL_RESET_CMD_VFE_DOMAIN	BIT(30)
35 #define		GLOBAL_RESET_CMD_RESET_BYPASS	BIT(31)
36 
37 #define VFE_CORE_CFG				(0x050)
38 #define		CFG_PIXEL_PATTERN_YCBYCR	(0x4)
39 #define		CFG_PIXEL_PATTERN_YCRYCB	(0x5)
40 #define		CFG_PIXEL_PATTERN_CBYCRY	(0x6)
41 #define		CFG_PIXEL_PATTERN_CRYCBY	(0x7)
42 #define		CFG_COMPOSITE_REG_UPDATE_EN	BIT(4)
43 
44 #define VFE_IRQ_CMD				(0x058)
45 #define		CMD_GLOBAL_CLEAR		BIT(0)
46 
47 #define VFE_IRQ_MASK_0					(0x05c)
48 #define		MASK_0_CAMIF_SOF			BIT(0)
49 #define		MASK_0_CAMIF_EOF			BIT(1)
50 #define		MASK_0_RDI_REG_UPDATE(n)		BIT((n) + 5)
51 #define		MASK_0_IMAGE_MASTER_n_PING_PONG(n)	BIT((n) + 8)
52 #define		MASK_0_IMAGE_COMPOSITE_DONE_n(n)	BIT((n) + 25)
53 #define		MASK_0_RESET_ACK			BIT(31)
54 
55 #define VFE_IRQ_MASK_1					(0x060)
56 #define		MASK_1_CAMIF_ERROR			BIT(0)
57 #define		MASK_1_VIOLATION			BIT(7)
58 #define		MASK_1_BUS_BDG_HALT_ACK			BIT(8)
59 #define		MASK_1_IMAGE_MASTER_n_BUS_OVERFLOW(n)	BIT((n) + 9)
60 #define		MASK_1_RDI_SOF(n)			BIT((n) + 29)
61 
62 #define VFE_IRQ_CLEAR_0					(0x064)
63 #define VFE_IRQ_CLEAR_1					(0x068)
64 
65 #define VFE_IRQ_STATUS_0				(0x06c)
66 #define		STATUS_0_CAMIF_SOF			BIT(0)
67 #define		STATUS_0_RDI_REG_UPDATE(n)		BIT((n) + 5)
68 #define		STATUS_0_IMAGE_MASTER_PING_PONG(n)	BIT((n) + 8)
69 #define		STATUS_0_IMAGE_COMPOSITE_DONE(n)	BIT((n) + 25)
70 #define		STATUS_0_RESET_ACK			BIT(31)
71 
72 #define VFE_IRQ_STATUS_1				(0x070)
73 #define		STATUS_1_VIOLATION			BIT(7)
74 #define		STATUS_1_BUS_BDG_HALT_ACK		BIT(8)
75 #define		STATUS_1_RDI_SOF(n)			BIT((n) + 27)
76 
77 #define VFE_VIOLATION_STATUS			(0x07c)
78 
79 #define VFE_CAMIF_CMD				(0x478)
80 #define		CMD_CLEAR_CAMIF_STATUS		BIT(2)
81 
82 #define VFE_CAMIF_CFG				(0x47c)
83 #define		CFG_VSYNC_SYNC_EDGE		(0)
84 #define			VSYNC_ACTIVE_HIGH	(0)
85 #define			VSYNC_ACTIVE_LOW	(1)
86 #define		CFG_HSYNC_SYNC_EDGE		(1)
87 #define			HSYNC_ACTIVE_HIGH	(0)
88 #define			HSYNC_ACTIVE_LOW	(1)
89 #define		CFG_VFE_SUBSAMPLE_ENABLE	BIT(4)
90 #define		CFG_BUS_SUBSAMPLE_ENABLE	BIT(5)
91 #define		CFG_VFE_OUTPUT_EN		BIT(6)
92 #define		CFG_BUS_OUTPUT_EN		BIT(7)
93 #define		CFG_BINNING_EN			BIT(9)
94 #define		CFG_FRAME_BASED_EN		BIT(10)
95 #define		CFG_RAW_CROP_EN			BIT(22)
96 
97 #define VFE_REG_UPDATE_CMD			(0x4ac)
98 #define		REG_UPDATE_RDI(n)		BIT(1 + (n))
99 
100 #define VFE_BUS_IRQ_MASK(n)		(0x2044 + (n) * 4)
101 #define VFE_BUS_IRQ_CLEAR(n)		(0x2050 + (n) * 4)
102 #define VFE_BUS_IRQ_STATUS(n)		(0x205c + (n) * 4)
103 #define		STATUS0_COMP_RESET_DONE		BIT(0)
104 #define		STATUS0_COMP_REG_UPDATE0_DONE	BIT(1)
105 #define		STATUS0_COMP_REG_UPDATE1_DONE	BIT(2)
106 #define		STATUS0_COMP_REG_UPDATE2_DONE	BIT(3)
107 #define		STATUS0_COMP_REG_UPDATE3_DONE	BIT(4)
108 #define		STATUS0_COMP_REG_UPDATE_DONE(n)	BIT((n) + 1)
109 #define		STATUS0_COMP0_BUF_DONE		BIT(5)
110 #define		STATUS0_COMP1_BUF_DONE		BIT(6)
111 #define		STATUS0_COMP2_BUF_DONE		BIT(7)
112 #define		STATUS0_COMP3_BUF_DONE		BIT(8)
113 #define		STATUS0_COMP4_BUF_DONE		BIT(9)
114 #define		STATUS0_COMP5_BUF_DONE		BIT(10)
115 #define		STATUS0_COMP_BUF_DONE(n)	BIT((n) + 5)
116 #define		STATUS0_COMP_ERROR		BIT(11)
117 #define		STATUS0_COMP_OVERWRITE		BIT(12)
118 #define		STATUS0_OVERFLOW		BIT(13)
119 #define		STATUS0_VIOLATION		BIT(14)
120 /* WM_CLIENT_BUF_DONE defined for buffers 0:19 */
121 #define		STATUS1_WM_CLIENT_BUF_DONE(n)		BIT(n)
122 #define		STATUS1_EARLY_DONE			BIT(24)
123 #define		STATUS2_DUAL_COMP0_BUF_DONE		BIT(0)
124 #define		STATUS2_DUAL_COMP1_BUF_DONE		BIT(1)
125 #define		STATUS2_DUAL_COMP2_BUF_DONE		BIT(2)
126 #define		STATUS2_DUAL_COMP3_BUF_DONE		BIT(3)
127 #define		STATUS2_DUAL_COMP4_BUF_DONE		BIT(4)
128 #define		STATUS2_DUAL_COMP5_BUF_DONE		BIT(5)
129 #define		STATUS2_DUAL_COMP_BUF_DONE(n)		BIT(n)
130 #define		STATUS2_DUAL_COMP_ERROR			BIT(6)
131 #define		STATUS2_DUAL_COMP_OVERWRITE		BIT(7)
132 
133 #define VFE_BUS_IRQ_CLEAR_GLOBAL		(0x2068)
134 
135 #define VFE_BUS_WM_DEBUG_STATUS_CFG		(0x226c)
136 #define		DEBUG_STATUS_CFG_STATUS0(n)	BIT(n)
137 #define		DEBUG_STATUS_CFG_STATUS1(n)	BIT(8 + (n))
138 
139 #define VFE_BUS_WM_ADDR_SYNC_FRAME_HEADER	(0x2080)
140 
141 #define VFE_BUS_WM_ADDR_SYNC_NO_SYNC		(0x2084)
142 #define		BUS_VER2_MAX_CLIENTS (24)
143 #define		WM_ADDR_NO_SYNC_DEFAULT_VAL \
144 				((1 << BUS_VER2_MAX_CLIENTS) - 1)
145 
146 #define VFE_BUS_WM_CGC_OVERRIDE			(0x200c)
147 #define		WM_CGC_OVERRIDE_ALL		(0xFFFFF)
148 
149 #define VFE_BUS_WM_TEST_BUS_CTRL		(0x211c)
150 
151 #define VFE_BUS_WM_STATUS0(n)			(0x2200 + (n) * 0x100)
152 #define VFE_BUS_WM_STATUS1(n)			(0x2204 + (n) * 0x100)
153 #define VFE_BUS_WM_CFG(n)			(0x2208 + (n) * 0x100)
154 #define		WM_CFG_EN			(0)
155 #define		WM_CFG_MODE			(1)
156 #define			MODE_QCOM_PLAIN	(0)
157 #define			MODE_MIPI_RAW	(1)
158 #define		WM_CFG_VIRTUALFRAME		(2)
159 #define VFE_BUS_WM_HEADER_ADDR(n)		(0x220c + (n) * 0x100)
160 #define VFE_BUS_WM_HEADER_CFG(n)		(0x2210 + (n) * 0x100)
161 #define VFE_BUS_WM_IMAGE_ADDR(n)		(0x2214 + (n) * 0x100)
162 #define VFE_BUS_WM_IMAGE_ADDR_OFFSET(n)		(0x2218 + (n) * 0x100)
163 #define VFE_BUS_WM_BUFFER_WIDTH_CFG(n)		(0x221c + (n) * 0x100)
164 #define		WM_BUFFER_DEFAULT_WIDTH		(0xFF01)
165 
166 #define VFE_BUS_WM_BUFFER_HEIGHT_CFG(n)		(0x2220 + (n) * 0x100)
167 #define VFE_BUS_WM_PACKER_CFG(n)		(0x2224 + (n) * 0x100)
168 
169 #define VFE_BUS_WM_STRIDE(n)			(0x2228 + (n) * 0x100)
170 #define		WM_STRIDE_DEFAULT_STRIDE	(0xFF01)
171 
172 #define VFE_BUS_WM_IRQ_SUBSAMPLE_PERIOD(n)	(0x2248 + (n) * 0x100)
173 #define VFE_BUS_WM_IRQ_SUBSAMPLE_PATTERN(n)	(0x224c + (n) * 0x100)
174 #define VFE_BUS_WM_FRAMEDROP_PERIOD(n)		(0x2250 + (n) * 0x100)
175 #define VFE_BUS_WM_FRAMEDROP_PATTERN(n)		(0x2254 + (n) * 0x100)
176 #define VFE_BUS_WM_FRAME_INC(n)			(0x2258 + (n) * 0x100)
177 #define VFE_BUS_WM_BURST_LIMIT(n)		(0x225c + (n) * 0x100)
178 
vfe_hw_version_read(struct vfe_device * vfe,struct device * dev)179 static void vfe_hw_version_read(struct vfe_device *vfe, struct device *dev)
180 {
181 	u32 hw_version = readl_relaxed(vfe->base + VFE_HW_VERSION);
182 
183 	u32 gen = (hw_version >> 28) & 0xF;
184 	u32 rev = (hw_version >> 16) & 0xFFF;
185 	u32 step = hw_version & 0xFFFF;
186 
187 	dev_err(dev, "VFE HW Version = %u.%u.%u\n", gen, rev, step);
188 }
189 
vfe_reg_clr(struct vfe_device * vfe,u32 reg,u32 clr_bits)190 static inline void vfe_reg_clr(struct vfe_device *vfe, u32 reg, u32 clr_bits)
191 {
192 	u32 bits = readl_relaxed(vfe->base + reg);
193 
194 	writel_relaxed(bits & ~clr_bits, vfe->base + reg);
195 }
196 
vfe_reg_set(struct vfe_device * vfe,u32 reg,u32 set_bits)197 static inline void vfe_reg_set(struct vfe_device *vfe, u32 reg, u32 set_bits)
198 {
199 	u32 bits = readl_relaxed(vfe->base + reg);
200 
201 	writel_relaxed(bits | set_bits, vfe->base + reg);
202 }
203 
vfe_global_reset(struct vfe_device * vfe)204 static void vfe_global_reset(struct vfe_device *vfe)
205 {
206 	u32 reset_bits = GLOBAL_RESET_CMD_CORE		|
207 			 GLOBAL_RESET_CMD_CAMIF		|
208 			 GLOBAL_RESET_CMD_BUS		|
209 			 GLOBAL_RESET_CMD_BUS_BDG	|
210 			 GLOBAL_RESET_CMD_REGISTER	|
211 			 GLOBAL_RESET_CMD_TESTGEN	|
212 			 GLOBAL_RESET_CMD_DSP		|
213 			 GLOBAL_RESET_CMD_IDLE_CGC	|
214 			 GLOBAL_RESET_CMD_RDI0		|
215 			 GLOBAL_RESET_CMD_RDI1		|
216 			 GLOBAL_RESET_CMD_RDI2;
217 
218 	writel_relaxed(BIT(31), vfe->base + VFE_IRQ_MASK_0);
219 
220 	/* Make sure IRQ mask has been written before resetting */
221 	wmb();
222 
223 	writel_relaxed(reset_bits, vfe->base + VFE_GLOBAL_RESET_CMD);
224 }
225 
vfe_wm_start(struct vfe_device * vfe,u8 wm,struct vfe_line * line)226 static void vfe_wm_start(struct vfe_device *vfe, u8 wm, struct vfe_line *line)
227 {
228 	u32 val;
229 
230 	/*Set Debug Registers*/
231 	val = DEBUG_STATUS_CFG_STATUS0(1) |
232 	      DEBUG_STATUS_CFG_STATUS0(7);
233 	writel_relaxed(val, vfe->base + VFE_BUS_WM_DEBUG_STATUS_CFG);
234 
235 	/* BUS_WM_INPUT_IF_ADDR_SYNC_FRAME_HEADER */
236 	writel_relaxed(0, vfe->base + VFE_BUS_WM_ADDR_SYNC_FRAME_HEADER);
237 
238 	/* no clock gating at bus input */
239 	val = WM_CGC_OVERRIDE_ALL;
240 	writel_relaxed(val, vfe->base + VFE_BUS_WM_CGC_OVERRIDE);
241 
242 	writel_relaxed(0x0, vfe->base + VFE_BUS_WM_TEST_BUS_CTRL);
243 
244 	/* if addr_no_sync has default value then config the addr no sync reg */
245 	val = WM_ADDR_NO_SYNC_DEFAULT_VAL;
246 	writel_relaxed(val, vfe->base + VFE_BUS_WM_ADDR_SYNC_NO_SYNC);
247 
248 	writel_relaxed(0xf, vfe->base + VFE_BUS_WM_BURST_LIMIT(wm));
249 
250 	val = WM_BUFFER_DEFAULT_WIDTH;
251 	writel_relaxed(val, vfe->base + VFE_BUS_WM_BUFFER_WIDTH_CFG(wm));
252 
253 	val = 0;
254 	writel_relaxed(val, vfe->base + VFE_BUS_WM_BUFFER_HEIGHT_CFG(wm));
255 
256 	val = 0;
257 	writel_relaxed(val, vfe->base + VFE_BUS_WM_PACKER_CFG(wm)); // XXX 1 for PLAIN8?
258 
259 	/* Configure stride for RDIs */
260 	val = WM_STRIDE_DEFAULT_STRIDE;
261 	writel_relaxed(val, vfe->base + VFE_BUS_WM_STRIDE(wm));
262 
263 	/* Enable WM */
264 	val = 1 << WM_CFG_EN |
265 	      MODE_MIPI_RAW << WM_CFG_MODE;
266 	writel_relaxed(val, vfe->base + VFE_BUS_WM_CFG(wm));
267 }
268 
vfe_wm_stop(struct vfe_device * vfe,u8 wm)269 static void vfe_wm_stop(struct vfe_device *vfe, u8 wm)
270 {
271 	/* Disable WM */
272 	writel_relaxed(0, vfe->base + VFE_BUS_WM_CFG(wm));
273 }
274 
vfe_wm_update(struct vfe_device * vfe,u8 wm,u32 addr,struct vfe_line * line)275 static void vfe_wm_update(struct vfe_device *vfe, u8 wm, u32 addr,
276 			  struct vfe_line *line)
277 {
278 	struct v4l2_pix_format_mplane *pix =
279 		&line->video_out.active_fmt.fmt.pix_mp;
280 	u32 stride = pix->plane_fmt[0].bytesperline;
281 
282 	writel_relaxed(addr, vfe->base + VFE_BUS_WM_IMAGE_ADDR(wm));
283 	writel_relaxed(stride * pix->height, vfe->base + VFE_BUS_WM_FRAME_INC(wm));
284 }
285 
vfe_reg_update(struct vfe_device * vfe,enum vfe_line_id line_id)286 static void vfe_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
287 {
288 	vfe->reg_update |= REG_UPDATE_RDI(line_id);
289 
290 	/* Enforce ordering between previous reg writes and reg update */
291 	wmb();
292 
293 	writel_relaxed(vfe->reg_update, vfe->base + VFE_REG_UPDATE_CMD);
294 
295 	/* Enforce ordering between reg update and subsequent reg writes */
296 	wmb();
297 }
298 
vfe_reg_update_clear(struct vfe_device * vfe,enum vfe_line_id line_id)299 static inline void vfe_reg_update_clear(struct vfe_device *vfe,
300 					enum vfe_line_id line_id)
301 {
302 	vfe->reg_update &= ~REG_UPDATE_RDI(line_id);
303 }
304 
vfe_enable_irq_common(struct vfe_device * vfe)305 static void vfe_enable_irq_common(struct vfe_device *vfe)
306 {
307 	vfe_reg_set(vfe, VFE_IRQ_MASK_0, ~0u);
308 	vfe_reg_set(vfe, VFE_IRQ_MASK_1, ~0u);
309 
310 	writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(0));
311 	writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(1));
312 	writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(2));
313 }
314 
vfe_isr_halt_ack(struct vfe_device * vfe)315 static void vfe_isr_halt_ack(struct vfe_device *vfe)
316 {
317 	complete(&vfe->halt_complete);
318 }
319 
vfe_isr_read(struct vfe_device * vfe,u32 * status0,u32 * status1)320 static void vfe_isr_read(struct vfe_device *vfe, u32 *status0, u32 *status1)
321 {
322 	*status0 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_0);
323 	*status1 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_1);
324 
325 	writel_relaxed(*status0, vfe->base + VFE_IRQ_CLEAR_0);
326 	writel_relaxed(*status1, vfe->base + VFE_IRQ_CLEAR_1);
327 
328 	/* Enforce ordering between IRQ Clear and Global IRQ Clear */
329 	wmb();
330 	writel_relaxed(CMD_GLOBAL_CLEAR, vfe->base + VFE_IRQ_CMD);
331 }
332 
vfe_violation_read(struct vfe_device * vfe)333 static void vfe_violation_read(struct vfe_device *vfe)
334 {
335 	u32 violation = readl_relaxed(vfe->base + VFE_VIOLATION_STATUS);
336 
337 	pr_err_ratelimited("VFE: violation = 0x%08x\n", violation);
338 }
339 
340 /*
341  * vfe_isr - VFE module interrupt handler
342  * @irq: Interrupt line
343  * @dev: VFE device
344  *
345  * Return IRQ_HANDLED on success
346  */
vfe_isr(int irq,void * dev)347 static irqreturn_t vfe_isr(int irq, void *dev)
348 {
349 	struct vfe_device *vfe = dev;
350 	u32 status0, status1, vfe_bus_status[3];
351 	int i, wm;
352 
353 	status0 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_0);
354 	status1 = readl_relaxed(vfe->base + VFE_IRQ_STATUS_1);
355 
356 	writel_relaxed(status0, vfe->base + VFE_IRQ_CLEAR_0);
357 	writel_relaxed(status1, vfe->base + VFE_IRQ_CLEAR_1);
358 
359 	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++) {
360 		vfe_bus_status[i] = readl_relaxed(vfe->base + VFE_BUS_IRQ_STATUS(i));
361 		writel_relaxed(vfe_bus_status[i], vfe->base + VFE_BUS_IRQ_CLEAR(i));
362 	}
363 
364 	/* Enforce ordering between IRQ reading and interpretation */
365 	wmb();
366 
367 	writel_relaxed(CMD_GLOBAL_CLEAR, vfe->base + VFE_IRQ_CMD);
368 	writel_relaxed(1, vfe->base + VFE_BUS_IRQ_CLEAR_GLOBAL);
369 
370 	if (status0 & STATUS_0_RESET_ACK)
371 		vfe->isr_ops.reset_ack(vfe);
372 
373 	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++)
374 		if (status0 & STATUS_0_RDI_REG_UPDATE(i))
375 			vfe->isr_ops.reg_update(vfe, i);
376 
377 	for (i = VFE_LINE_RDI0; i <= VFE_LINE_RDI2; i++)
378 		if (status0 & STATUS_1_RDI_SOF(i))
379 			vfe->isr_ops.sof(vfe, i);
380 
381 	for (i = 0; i < MSM_VFE_COMPOSITE_IRQ_NUM; i++)
382 		if (vfe_bus_status[0] & STATUS0_COMP_BUF_DONE(i))
383 			vfe->isr_ops.comp_done(vfe, i);
384 
385 	for (wm = 0; wm < MSM_VFE_IMAGE_MASTERS_NUM; wm++)
386 		if (status0 & BIT(9))
387 			if (vfe_bus_status[1] & STATUS1_WM_CLIENT_BUF_DONE(wm))
388 				vfe->isr_ops.wm_done(vfe, wm);
389 
390 	return IRQ_HANDLED;
391 }
392 
393 /*
394  * vfe_halt - Trigger halt on VFE module and wait to complete
395  * @vfe: VFE device
396  *
397  * Return 0 on success or a negative error code otherwise
398  */
vfe_halt(struct vfe_device * vfe)399 static int vfe_halt(struct vfe_device *vfe)
400 {
401 	/* rely on vfe_disable_output() to stop the VFE */
402 	return 0;
403 }
404 
vfe_get_output(struct vfe_line * line)405 static int vfe_get_output(struct vfe_line *line)
406 {
407 	struct vfe_device *vfe = to_vfe(line);
408 	struct vfe_output *output;
409 	unsigned long flags;
410 	int wm_idx;
411 
412 	spin_lock_irqsave(&vfe->output_lock, flags);
413 
414 	output = &line->output;
415 	if (output->state != VFE_OUTPUT_OFF) {
416 		dev_err(vfe->camss->dev, "Output is running\n");
417 		goto error;
418 	}
419 
420 	output->wm_num = 1;
421 
422 	wm_idx = vfe_reserve_wm(vfe, line->id);
423 	if (wm_idx < 0) {
424 		dev_err(vfe->camss->dev, "Can not reserve wm\n");
425 		goto error_get_wm;
426 	}
427 	output->wm_idx[0] = wm_idx;
428 
429 	output->drop_update_idx = 0;
430 
431 	spin_unlock_irqrestore(&vfe->output_lock, flags);
432 
433 	return 0;
434 
435 error_get_wm:
436 	vfe_release_wm(vfe, output->wm_idx[0]);
437 	output->state = VFE_OUTPUT_OFF;
438 error:
439 	spin_unlock_irqrestore(&vfe->output_lock, flags);
440 
441 	return -EINVAL;
442 }
443 
vfe_enable_output(struct vfe_line * line)444 static int vfe_enable_output(struct vfe_line *line)
445 {
446 	struct vfe_device *vfe = to_vfe(line);
447 	struct vfe_output *output = &line->output;
448 	const struct vfe_hw_ops *ops = vfe->ops;
449 	struct media_entity *sensor;
450 	unsigned long flags;
451 	unsigned int frame_skip = 0;
452 	unsigned int i;
453 
454 	sensor = camss_find_sensor(&line->subdev.entity);
455 	if (sensor) {
456 		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
457 
458 		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
459 		/* Max frame skip is 29 frames */
460 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
461 			frame_skip = VFE_FRAME_DROP_VAL - 1;
462 	}
463 
464 	spin_lock_irqsave(&vfe->output_lock, flags);
465 
466 	ops->reg_update_clear(vfe, line->id);
467 
468 	if (output->state != VFE_OUTPUT_OFF) {
469 		dev_err(vfe->camss->dev, "Output is not in reserved state %d\n",
470 			output->state);
471 		spin_unlock_irqrestore(&vfe->output_lock, flags);
472 		return -EINVAL;
473 	}
474 
475 	WARN_ON(output->gen2.active_num);
476 
477 	output->state = VFE_OUTPUT_ON;
478 
479 	output->sequence = 0;
480 	output->wait_reg_update = 0;
481 	reinit_completion(&output->reg_update);
482 
483 	vfe_wm_start(vfe, output->wm_idx[0], line);
484 
485 	for (i = 0; i < 2; i++) {
486 		output->buf[i] = vfe_buf_get_pending(output);
487 		if (!output->buf[i])
488 			break;
489 		output->gen2.active_num++;
490 		vfe_wm_update(vfe, output->wm_idx[0], output->buf[i]->addr[0], line);
491 	}
492 
493 	ops->reg_update(vfe, line->id);
494 
495 	spin_unlock_irqrestore(&vfe->output_lock, flags);
496 
497 	return 0;
498 }
499 
vfe_disable_output(struct vfe_line * line)500 static void vfe_disable_output(struct vfe_line *line)
501 {
502 	struct vfe_device *vfe = to_vfe(line);
503 	struct vfe_output *output = &line->output;
504 	unsigned long flags;
505 	unsigned int i;
506 
507 	spin_lock_irqsave(&vfe->output_lock, flags);
508 	for (i = 0; i < output->wm_num; i++)
509 		vfe_wm_stop(vfe, output->wm_idx[i]);
510 	output->gen2.active_num = 0;
511 	spin_unlock_irqrestore(&vfe->output_lock, flags);
512 
513 	vfe_reset(vfe);
514 }
515 
516 /*
517  * vfe_enable - Enable streaming on VFE line
518  * @line: VFE line
519  *
520  * Return 0 on success or a negative error code otherwise
521  */
vfe_enable(struct vfe_line * line)522 static int vfe_enable(struct vfe_line *line)
523 {
524 	struct vfe_device *vfe = to_vfe(line);
525 	int ret;
526 
527 	mutex_lock(&vfe->stream_lock);
528 
529 	if (!vfe->stream_count)
530 		vfe_enable_irq_common(vfe);
531 
532 	vfe->stream_count++;
533 
534 	mutex_unlock(&vfe->stream_lock);
535 
536 	ret = vfe_get_output(line);
537 	if (ret < 0)
538 		goto error_get_output;
539 
540 	ret = vfe_enable_output(line);
541 	if (ret < 0)
542 		goto error_enable_output;
543 
544 	vfe->was_streaming = 1;
545 
546 	return 0;
547 
548 error_enable_output:
549 	vfe_put_output(line);
550 
551 error_get_output:
552 	mutex_lock(&vfe->stream_lock);
553 
554 	vfe->stream_count--;
555 
556 	mutex_unlock(&vfe->stream_lock);
557 
558 	return ret;
559 }
560 
561 /*
562  * vfe_disable - Disable streaming on VFE line
563  * @line: VFE line
564  *
565  * Return 0 on success or a negative error code otherwise
566  */
vfe_disable(struct vfe_line * line)567 static int vfe_disable(struct vfe_line *line)
568 {
569 	struct vfe_device *vfe = to_vfe(line);
570 
571 	vfe_disable_output(line);
572 
573 	vfe_put_output(line);
574 
575 	mutex_lock(&vfe->stream_lock);
576 
577 	vfe->stream_count--;
578 
579 	mutex_unlock(&vfe->stream_lock);
580 
581 	return 0;
582 }
583 
584 /*
585  * vfe_isr_sof - Process start of frame interrupt
586  * @vfe: VFE Device
587  * @line_id: VFE line
588  */
vfe_isr_sof(struct vfe_device * vfe,enum vfe_line_id line_id)589 static void vfe_isr_sof(struct vfe_device *vfe, enum vfe_line_id line_id)
590 {
591 	/* nop */
592 }
593 
594 /*
595  * vfe_isr_reg_update - Process reg update interrupt
596  * @vfe: VFE Device
597  * @line_id: VFE line
598  */
vfe_isr_reg_update(struct vfe_device * vfe,enum vfe_line_id line_id)599 static void vfe_isr_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id)
600 {
601 	struct vfe_output *output;
602 	unsigned long flags;
603 
604 	spin_lock_irqsave(&vfe->output_lock, flags);
605 	vfe->ops->reg_update_clear(vfe, line_id);
606 
607 	output = &vfe->line[line_id].output;
608 
609 	if (output->wait_reg_update) {
610 		output->wait_reg_update = 0;
611 		complete(&output->reg_update);
612 	}
613 
614 	spin_unlock_irqrestore(&vfe->output_lock, flags);
615 }
616 
617 /*
618  * vfe_isr_wm_done - Process write master done interrupt
619  * @vfe: VFE Device
620  * @wm: Write master id
621  */
vfe_isr_wm_done(struct vfe_device * vfe,u8 wm)622 static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
623 {
624 	struct vfe_line *line = &vfe->line[vfe->wm_output_map[wm]];
625 	struct camss_buffer *ready_buf;
626 	struct vfe_output *output;
627 	unsigned long flags;
628 	u32 index;
629 	u64 ts = ktime_get_ns();
630 
631 	spin_lock_irqsave(&vfe->output_lock, flags);
632 
633 	if (vfe->wm_output_map[wm] == VFE_LINE_NONE) {
634 		dev_err_ratelimited(vfe->camss->dev,
635 				    "Received wm done for unmapped index\n");
636 		goto out_unlock;
637 	}
638 	output = &vfe->line[vfe->wm_output_map[wm]].output;
639 
640 	ready_buf = output->buf[0];
641 	if (!ready_buf) {
642 		dev_err_ratelimited(vfe->camss->dev,
643 				    "Missing ready buf %d!\n", output->state);
644 		goto out_unlock;
645 	}
646 
647 	ready_buf->vb.vb2_buf.timestamp = ts;
648 	ready_buf->vb.sequence = output->sequence++;
649 
650 	index = 0;
651 	output->buf[0] = output->buf[1];
652 	if (output->buf[0])
653 		index = 1;
654 
655 	output->buf[index] = vfe_buf_get_pending(output);
656 
657 	if (output->buf[index])
658 		vfe_wm_update(vfe, output->wm_idx[0], output->buf[index]->addr[0], line);
659 	else
660 		output->gen2.active_num--;
661 
662 	spin_unlock_irqrestore(&vfe->output_lock, flags);
663 
664 	vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
665 
666 	return;
667 
668 out_unlock:
669 	spin_unlock_irqrestore(&vfe->output_lock, flags);
670 }
671 
672 /*
673  * vfe_pm_domain_off - Disable power domains specific to this VFE.
674  * @vfe: VFE Device
675  */
vfe_pm_domain_off(struct vfe_device * vfe)676 static void vfe_pm_domain_off(struct vfe_device *vfe)
677 {
678 	/* nop */
679 }
680 
681 /*
682  * vfe_pm_domain_on - Enable power domains specific to this VFE.
683  * @vfe: VFE Device
684  */
vfe_pm_domain_on(struct vfe_device * vfe)685 static int vfe_pm_domain_on(struct vfe_device *vfe)
686 {
687 	return 0;
688 }
689 
690 /*
691  * vfe_queue_buffer - Add empty buffer
692  * @vid: Video device structure
693  * @buf: Buffer to be enqueued
694  *
695  * Add an empty buffer - depending on the current number of buffers it will be
696  * put in pending buffer queue or directly given to the hardware to be filled.
697  *
698  * Return 0 on success or a negative error code otherwise
699  */
vfe_queue_buffer(struct camss_video * vid,struct camss_buffer * buf)700 static int vfe_queue_buffer(struct camss_video *vid,
701 			    struct camss_buffer *buf)
702 {
703 	struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
704 	struct vfe_device *vfe = to_vfe(line);
705 	struct vfe_output *output;
706 	unsigned long flags;
707 
708 	output = &line->output;
709 
710 	spin_lock_irqsave(&vfe->output_lock, flags);
711 
712 	if (output->state == VFE_OUTPUT_ON && output->gen2.active_num < 2) {
713 		output->buf[output->gen2.active_num++] = buf;
714 		vfe_wm_update(vfe, output->wm_idx[0], buf->addr[0], line);
715 	} else {
716 		vfe_buf_add_pending(output, buf);
717 	}
718 
719 	spin_unlock_irqrestore(&vfe->output_lock, flags);
720 
721 	return 0;
722 }
723 
724 static const struct vfe_isr_ops vfe_isr_ops_170 = {
725 	.reset_ack = vfe_isr_reset_ack,
726 	.halt_ack = vfe_isr_halt_ack,
727 	.reg_update = vfe_isr_reg_update,
728 	.sof = vfe_isr_sof,
729 	.comp_done = vfe_isr_comp_done,
730 	.wm_done = vfe_isr_wm_done,
731 };
732 
733 static const struct camss_video_ops vfe_video_ops_170 = {
734 	.queue_buffer = vfe_queue_buffer,
735 	.flush_buffers = vfe_flush_buffers,
736 };
737 
vfe_subdev_init(struct device * dev,struct vfe_device * vfe)738 static void vfe_subdev_init(struct device *dev, struct vfe_device *vfe)
739 {
740 	vfe->isr_ops = vfe_isr_ops_170;
741 	vfe->video_ops = vfe_video_ops_170;
742 
743 	vfe->line_num = VFE_LINE_NUM_GEN2;
744 }
745 
746 const struct vfe_hw_ops vfe_ops_170 = {
747 	.global_reset = vfe_global_reset,
748 	.hw_version_read = vfe_hw_version_read,
749 	.isr_read = vfe_isr_read,
750 	.isr = vfe_isr,
751 	.pm_domain_off = vfe_pm_domain_off,
752 	.pm_domain_on = vfe_pm_domain_on,
753 	.reg_update_clear = vfe_reg_update_clear,
754 	.reg_update = vfe_reg_update,
755 	.subdev_init = vfe_subdev_init,
756 	.vfe_disable = vfe_disable,
757 	.vfe_enable = vfe_enable,
758 	.vfe_halt = vfe_halt,
759 	.violation_read = vfe_violation_read,
760 };
761