• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * camss-vfe.c
4  *
5  * Qualcomm MSM Camera Subsystem - VFE (Video Front End) Module
6  *
7  * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
8  * Copyright (C) 2015-2018 Linaro Ltd.
9  */
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>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/spinlock_types.h>
19 #include <linux/spinlock.h>
20 #include <media/media-entity.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-subdev.h>
23 
24 #include "camss-vfe.h"
25 #include "camss.h"
26 
27 #define MSM_VFE_NAME "msm_vfe"
28 
29 /* VFE reset timeout */
30 #define VFE_RESET_TIMEOUT_MS 50
31 
32 #define SCALER_RATIO_MAX 16
33 
34 struct vfe_format {
35 	u32 code;
36 	u8 bpp;
37 };
38 
39 static const struct vfe_format formats_rdi_8x16[] = {
40 	{ MEDIA_BUS_FMT_UYVY8_2X8, 8 },
41 	{ MEDIA_BUS_FMT_VYUY8_2X8, 8 },
42 	{ MEDIA_BUS_FMT_YUYV8_2X8, 8 },
43 	{ MEDIA_BUS_FMT_YVYU8_2X8, 8 },
44 	{ MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
45 	{ MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
46 	{ MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
47 	{ MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
48 	{ MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
49 	{ MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
50 	{ MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
51 	{ MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
52 	{ MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
53 	{ MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
54 	{ MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
55 	{ MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
56 	{ MEDIA_BUS_FMT_Y10_1X10, 10 },
57 };
58 
59 static const struct vfe_format formats_pix_8x16[] = {
60 	{ MEDIA_BUS_FMT_UYVY8_2X8, 8 },
61 	{ MEDIA_BUS_FMT_VYUY8_2X8, 8 },
62 	{ MEDIA_BUS_FMT_YUYV8_2X8, 8 },
63 	{ MEDIA_BUS_FMT_YVYU8_2X8, 8 },
64 };
65 
66 static const struct vfe_format formats_rdi_8x96[] = {
67 	{ MEDIA_BUS_FMT_UYVY8_2X8, 8 },
68 	{ MEDIA_BUS_FMT_VYUY8_2X8, 8 },
69 	{ MEDIA_BUS_FMT_YUYV8_2X8, 8 },
70 	{ MEDIA_BUS_FMT_YVYU8_2X8, 8 },
71 	{ MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
72 	{ MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
73 	{ MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
74 	{ MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
75 	{ MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
76 	{ MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
77 	{ MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
78 	{ MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
79 	{ MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 },
80 	{ MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
81 	{ MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
82 	{ MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
83 	{ MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
84 	{ MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
85 	{ MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
86 	{ MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
87 	{ MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
88 	{ MEDIA_BUS_FMT_Y10_1X10, 10 },
89 	{ MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 },
90 };
91 
92 static const struct vfe_format formats_pix_8x96[] = {
93 	{ MEDIA_BUS_FMT_UYVY8_2X8, 8 },
94 	{ MEDIA_BUS_FMT_VYUY8_2X8, 8 },
95 	{ MEDIA_BUS_FMT_YUYV8_2X8, 8 },
96 	{ MEDIA_BUS_FMT_YVYU8_2X8, 8 },
97 };
98 
99 static const struct vfe_format formats_rdi_845[] = {
100 	{ MEDIA_BUS_FMT_UYVY8_2X8, 8 },
101 	{ MEDIA_BUS_FMT_VYUY8_2X8, 8 },
102 	{ MEDIA_BUS_FMT_YUYV8_2X8, 8 },
103 	{ MEDIA_BUS_FMT_YVYU8_2X8, 8 },
104 	{ MEDIA_BUS_FMT_SBGGR8_1X8, 8 },
105 	{ MEDIA_BUS_FMT_SGBRG8_1X8, 8 },
106 	{ MEDIA_BUS_FMT_SGRBG8_1X8, 8 },
107 	{ MEDIA_BUS_FMT_SRGGB8_1X8, 8 },
108 	{ MEDIA_BUS_FMT_SBGGR10_1X10, 10 },
109 	{ MEDIA_BUS_FMT_SGBRG10_1X10, 10 },
110 	{ MEDIA_BUS_FMT_SGRBG10_1X10, 10 },
111 	{ MEDIA_BUS_FMT_SRGGB10_1X10, 10 },
112 	{ MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, 16 },
113 	{ MEDIA_BUS_FMT_SBGGR12_1X12, 12 },
114 	{ MEDIA_BUS_FMT_SGBRG12_1X12, 12 },
115 	{ MEDIA_BUS_FMT_SGRBG12_1X12, 12 },
116 	{ MEDIA_BUS_FMT_SRGGB12_1X12, 12 },
117 	{ MEDIA_BUS_FMT_SBGGR14_1X14, 14 },
118 	{ MEDIA_BUS_FMT_SGBRG14_1X14, 14 },
119 	{ MEDIA_BUS_FMT_SGRBG14_1X14, 14 },
120 	{ MEDIA_BUS_FMT_SRGGB14_1X14, 14 },
121 	{ MEDIA_BUS_FMT_Y10_1X10, 10 },
122 	{ MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, 16 },
123 };
124 
125 /*
126  * vfe_get_bpp - map media bus format to bits per pixel
127  * @formats: supported media bus formats array
128  * @nformats: size of @formats array
129  * @code: media bus format code
130  *
131  * Return number of bits per pixel
132  */
vfe_get_bpp(const struct vfe_format * formats,unsigned int nformats,u32 code)133 static u8 vfe_get_bpp(const struct vfe_format *formats,
134 		      unsigned int nformats, u32 code)
135 {
136 	unsigned int i;
137 
138 	for (i = 0; i < nformats; i++)
139 		if (code == formats[i].code)
140 			return formats[i].bpp;
141 
142 	WARN(1, "Unknown format\n");
143 
144 	return formats[0].bpp;
145 }
146 
vfe_find_code(u32 * code,unsigned int n_code,unsigned int index,u32 req_code)147 static u32 vfe_find_code(u32 *code, unsigned int n_code,
148 			 unsigned int index, u32 req_code)
149 {
150 	int i;
151 
152 	if (!req_code && (index >= n_code))
153 		return 0;
154 
155 	for (i = 0; i < n_code; i++)
156 		if (req_code) {
157 			if (req_code == code[i])
158 				return req_code;
159 		} else {
160 			if (i == index)
161 				return code[i];
162 		}
163 
164 	return code[0];
165 }
166 
vfe_src_pad_code(struct vfe_line * line,u32 sink_code,unsigned int index,u32 src_req_code)167 static u32 vfe_src_pad_code(struct vfe_line *line, u32 sink_code,
168 			    unsigned int index, u32 src_req_code)
169 {
170 	struct vfe_device *vfe = to_vfe(line);
171 
172 	if (vfe->camss->version == CAMSS_8x16)
173 		switch (sink_code) {
174 		case MEDIA_BUS_FMT_YUYV8_2X8:
175 		{
176 			u32 src_code[] = {
177 				MEDIA_BUS_FMT_YUYV8_2X8,
178 				MEDIA_BUS_FMT_YUYV8_1_5X8,
179 			};
180 
181 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
182 					     index, src_req_code);
183 		}
184 		case MEDIA_BUS_FMT_YVYU8_2X8:
185 		{
186 			u32 src_code[] = {
187 				MEDIA_BUS_FMT_YVYU8_2X8,
188 				MEDIA_BUS_FMT_YVYU8_1_5X8,
189 			};
190 
191 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
192 					     index, src_req_code);
193 		}
194 		case MEDIA_BUS_FMT_UYVY8_2X8:
195 		{
196 			u32 src_code[] = {
197 				MEDIA_BUS_FMT_UYVY8_2X8,
198 				MEDIA_BUS_FMT_UYVY8_1_5X8,
199 			};
200 
201 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
202 					     index, src_req_code);
203 		}
204 		case MEDIA_BUS_FMT_VYUY8_2X8:
205 		{
206 			u32 src_code[] = {
207 				MEDIA_BUS_FMT_VYUY8_2X8,
208 				MEDIA_BUS_FMT_VYUY8_1_5X8,
209 			};
210 
211 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
212 					     index, src_req_code);
213 		}
214 		default:
215 			if (index > 0)
216 				return 0;
217 
218 			return sink_code;
219 		}
220 	else if (vfe->camss->version == CAMSS_8x96 ||
221 		 vfe->camss->version == CAMSS_660 ||
222 		 vfe->camss->version == CAMSS_845)
223 		switch (sink_code) {
224 		case MEDIA_BUS_FMT_YUYV8_2X8:
225 		{
226 			u32 src_code[] = {
227 				MEDIA_BUS_FMT_YUYV8_2X8,
228 				MEDIA_BUS_FMT_YVYU8_2X8,
229 				MEDIA_BUS_FMT_UYVY8_2X8,
230 				MEDIA_BUS_FMT_VYUY8_2X8,
231 				MEDIA_BUS_FMT_YUYV8_1_5X8,
232 			};
233 
234 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
235 					     index, src_req_code);
236 		}
237 		case MEDIA_BUS_FMT_YVYU8_2X8:
238 		{
239 			u32 src_code[] = {
240 				MEDIA_BUS_FMT_YVYU8_2X8,
241 				MEDIA_BUS_FMT_YUYV8_2X8,
242 				MEDIA_BUS_FMT_UYVY8_2X8,
243 				MEDIA_BUS_FMT_VYUY8_2X8,
244 				MEDIA_BUS_FMT_YVYU8_1_5X8,
245 			};
246 
247 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
248 					     index, src_req_code);
249 		}
250 		case MEDIA_BUS_FMT_UYVY8_2X8:
251 		{
252 			u32 src_code[] = {
253 				MEDIA_BUS_FMT_UYVY8_2X8,
254 				MEDIA_BUS_FMT_YUYV8_2X8,
255 				MEDIA_BUS_FMT_YVYU8_2X8,
256 				MEDIA_BUS_FMT_VYUY8_2X8,
257 				MEDIA_BUS_FMT_UYVY8_1_5X8,
258 			};
259 
260 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
261 					     index, src_req_code);
262 		}
263 		case MEDIA_BUS_FMT_VYUY8_2X8:
264 		{
265 			u32 src_code[] = {
266 				MEDIA_BUS_FMT_VYUY8_2X8,
267 				MEDIA_BUS_FMT_YUYV8_2X8,
268 				MEDIA_BUS_FMT_YVYU8_2X8,
269 				MEDIA_BUS_FMT_UYVY8_2X8,
270 				MEDIA_BUS_FMT_VYUY8_1_5X8,
271 			};
272 
273 			return vfe_find_code(src_code, ARRAY_SIZE(src_code),
274 					     index, src_req_code);
275 		}
276 		default:
277 			if (index > 0)
278 				return 0;
279 
280 			return sink_code;
281 		}
282 	else
283 		return 0;
284 }
285 
vfe_reset(struct vfe_device * vfe)286 int vfe_reset(struct vfe_device *vfe)
287 {
288 	unsigned long time;
289 
290 	reinit_completion(&vfe->reset_complete);
291 
292 	vfe->ops->global_reset(vfe);
293 
294 	time = wait_for_completion_timeout(&vfe->reset_complete,
295 		msecs_to_jiffies(VFE_RESET_TIMEOUT_MS));
296 	if (!time) {
297 		dev_err(vfe->camss->dev, "VFE reset timeout\n");
298 		return -EIO;
299 	}
300 
301 	return 0;
302 }
303 
vfe_init_outputs(struct vfe_device * vfe)304 static void vfe_init_outputs(struct vfe_device *vfe)
305 {
306 	int i;
307 
308 	for (i = 0; i < vfe->line_num; i++) {
309 		struct vfe_output *output = &vfe->line[i].output;
310 
311 		output->state = VFE_OUTPUT_OFF;
312 		output->buf[0] = NULL;
313 		output->buf[1] = NULL;
314 		INIT_LIST_HEAD(&output->pending_bufs);
315 	}
316 }
317 
vfe_reset_output_maps(struct vfe_device * vfe)318 static void vfe_reset_output_maps(struct vfe_device *vfe)
319 {
320 	int i;
321 
322 	for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
323 		vfe->wm_output_map[i] = VFE_LINE_NONE;
324 }
325 
vfe_reserve_wm(struct vfe_device * vfe,enum vfe_line_id line_id)326 int vfe_reserve_wm(struct vfe_device *vfe, enum vfe_line_id line_id)
327 {
328 	int ret = -EBUSY;
329 	int i;
330 
331 	for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) {
332 		if (vfe->wm_output_map[i] == VFE_LINE_NONE) {
333 			vfe->wm_output_map[i] = line_id;
334 			ret = i;
335 			break;
336 		}
337 	}
338 
339 	return ret;
340 }
341 
vfe_release_wm(struct vfe_device * vfe,u8 wm)342 int vfe_release_wm(struct vfe_device *vfe, u8 wm)
343 {
344 	if (wm >= ARRAY_SIZE(vfe->wm_output_map))
345 		return -EINVAL;
346 
347 	vfe->wm_output_map[wm] = VFE_LINE_NONE;
348 
349 	return 0;
350 }
351 
vfe_buf_get_pending(struct vfe_output * output)352 struct camss_buffer *vfe_buf_get_pending(struct vfe_output *output)
353 {
354 	struct camss_buffer *buffer = NULL;
355 
356 	if (!list_empty(&output->pending_bufs)) {
357 		buffer = list_first_entry(&output->pending_bufs,
358 					  struct camss_buffer,
359 					  queue);
360 		list_del(&buffer->queue);
361 	}
362 
363 	return buffer;
364 }
365 
vfe_buf_add_pending(struct vfe_output * output,struct camss_buffer * buffer)366 void vfe_buf_add_pending(struct vfe_output *output,
367 			 struct camss_buffer *buffer)
368 {
369 	INIT_LIST_HEAD(&buffer->queue);
370 	list_add_tail(&buffer->queue, &output->pending_bufs);
371 }
372 
373 /*
374  * vfe_buf_flush_pending - Flush all pending buffers.
375  * @output: VFE output
376  * @state: vb2 buffer state
377  */
vfe_buf_flush_pending(struct vfe_output * output,enum vb2_buffer_state state)378 static void vfe_buf_flush_pending(struct vfe_output *output,
379 				  enum vb2_buffer_state state)
380 {
381 	struct camss_buffer *buf;
382 	struct camss_buffer *t;
383 
384 	list_for_each_entry_safe(buf, t, &output->pending_bufs, queue) {
385 		vb2_buffer_done(&buf->vb.vb2_buf, state);
386 		list_del(&buf->queue);
387 	}
388 }
389 
vfe_put_output(struct vfe_line * line)390 int vfe_put_output(struct vfe_line *line)
391 {
392 	struct vfe_device *vfe = to_vfe(line);
393 	struct vfe_output *output = &line->output;
394 	unsigned long flags;
395 	unsigned int i;
396 
397 	spin_lock_irqsave(&vfe->output_lock, flags);
398 
399 	for (i = 0; i < output->wm_num; i++)
400 		vfe_release_wm(vfe, output->wm_idx[i]);
401 
402 	output->state = VFE_OUTPUT_OFF;
403 
404 	spin_unlock_irqrestore(&vfe->output_lock, flags);
405 	return 0;
406 }
407 
408 /**
409  * vfe_isr_comp_done() - Process composite image done interrupt
410  * @vfe: VFE Device
411  * @comp: Composite image id
412  */
vfe_isr_comp_done(struct vfe_device * vfe,u8 comp)413 void vfe_isr_comp_done(struct vfe_device *vfe, u8 comp)
414 {
415 	unsigned int i;
416 
417 	for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++)
418 		if (vfe->wm_output_map[i] == VFE_LINE_PIX) {
419 			vfe->isr_ops.wm_done(vfe, i);
420 			break;
421 		}
422 }
423 
vfe_isr_reset_ack(struct vfe_device * vfe)424 void vfe_isr_reset_ack(struct vfe_device *vfe)
425 {
426 	complete(&vfe->reset_complete);
427 }
428 
429 /*
430  * vfe_set_clock_rates - Calculate and set clock rates on VFE module
431  * @vfe: VFE device
432  *
433  * Return 0 on success or a negative error code otherwise
434  */
vfe_set_clock_rates(struct vfe_device * vfe)435 static int vfe_set_clock_rates(struct vfe_device *vfe)
436 {
437 	struct device *dev = vfe->camss->dev;
438 	u64 pixel_clock[VFE_LINE_NUM_MAX];
439 	int i, j;
440 	int ret;
441 
442 	for (i = VFE_LINE_RDI0; i < vfe->line_num; i++) {
443 		ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
444 					    &pixel_clock[i]);
445 		if (ret)
446 			pixel_clock[i] = 0;
447 	}
448 
449 	for (i = 0; i < vfe->nclocks; i++) {
450 		struct camss_clock *clock = &vfe->clock[i];
451 
452 		if (!strcmp(clock->name, "vfe0") ||
453 		    !strcmp(clock->name, "vfe1") ||
454 		    !strcmp(clock->name, "vfe_lite")) {
455 			u64 min_rate = 0;
456 			long rate;
457 
458 			for (j = VFE_LINE_RDI0; j < vfe->line_num; j++) {
459 				u32 tmp;
460 				u8 bpp;
461 
462 				if (j == VFE_LINE_PIX) {
463 					tmp = pixel_clock[j];
464 				} else {
465 					struct vfe_line *l = &vfe->line[j];
466 
467 					bpp = vfe_get_bpp(l->formats,
468 						l->nformats,
469 						l->fmt[MSM_VFE_PAD_SINK].code);
470 					tmp = pixel_clock[j] * bpp / 64;
471 				}
472 
473 				if (min_rate < tmp)
474 					min_rate = tmp;
475 			}
476 
477 			camss_add_clock_margin(&min_rate);
478 
479 			for (j = 0; j < clock->nfreqs; j++)
480 				if (min_rate < clock->freq[j])
481 					break;
482 
483 			if (j == clock->nfreqs) {
484 				dev_err(dev,
485 					"Pixel clock is too high for VFE");
486 				return -EINVAL;
487 			}
488 
489 			/* if sensor pixel clock is not available */
490 			/* set highest possible VFE clock rate */
491 			if (min_rate == 0)
492 				j = clock->nfreqs - 1;
493 
494 			rate = clk_round_rate(clock->clk, clock->freq[j]);
495 			if (rate < 0) {
496 				dev_err(dev, "clk round rate failed: %ld\n",
497 					rate);
498 				return -EINVAL;
499 			}
500 
501 			ret = clk_set_rate(clock->clk, rate);
502 			if (ret < 0) {
503 				dev_err(dev, "clk set rate failed: %d\n", ret);
504 				return ret;
505 			}
506 		}
507 	}
508 
509 	return 0;
510 }
511 
512 /*
513  * vfe_check_clock_rates - Check current clock rates on VFE module
514  * @vfe: VFE device
515  *
516  * Return 0 if current clock rates are suitable for a new pipeline
517  * or a negative error code otherwise
518  */
vfe_check_clock_rates(struct vfe_device * vfe)519 static int vfe_check_clock_rates(struct vfe_device *vfe)
520 {
521 	u64 pixel_clock[VFE_LINE_NUM_MAX];
522 	int i, j;
523 	int ret;
524 
525 	for (i = VFE_LINE_RDI0; i < vfe->line_num; i++) {
526 		ret = camss_get_pixel_clock(&vfe->line[i].subdev.entity,
527 					    &pixel_clock[i]);
528 		if (ret)
529 			pixel_clock[i] = 0;
530 	}
531 
532 	for (i = 0; i < vfe->nclocks; i++) {
533 		struct camss_clock *clock = &vfe->clock[i];
534 
535 		if (!strcmp(clock->name, "vfe0") ||
536 		    !strcmp(clock->name, "vfe1") ||
537 		    !strcmp(clock->name, "vfe_lite")) {
538 			u64 min_rate = 0;
539 			unsigned long rate;
540 
541 			for (j = VFE_LINE_RDI0; j < vfe->line_num; j++) {
542 				u32 tmp;
543 				u8 bpp;
544 
545 				if (j == VFE_LINE_PIX) {
546 					tmp = pixel_clock[j];
547 				} else {
548 					struct vfe_line *l = &vfe->line[j];
549 
550 					bpp = vfe_get_bpp(l->formats,
551 						l->nformats,
552 						l->fmt[MSM_VFE_PAD_SINK].code);
553 					tmp = pixel_clock[j] * bpp / 64;
554 				}
555 
556 				if (min_rate < tmp)
557 					min_rate = tmp;
558 			}
559 
560 			camss_add_clock_margin(&min_rate);
561 
562 			rate = clk_get_rate(clock->clk);
563 			if (rate < min_rate)
564 				return -EBUSY;
565 		}
566 	}
567 
568 	return 0;
569 }
570 
571 /*
572  * vfe_get - Power up and reset VFE module
573  * @vfe: VFE Device
574  *
575  * Return 0 on success or a negative error code otherwise
576  */
vfe_get(struct vfe_device * vfe)577 static int vfe_get(struct vfe_device *vfe)
578 {
579 	int ret;
580 
581 	mutex_lock(&vfe->power_lock);
582 
583 	if (vfe->power_count == 0) {
584 		ret = vfe->ops->pm_domain_on(vfe);
585 		if (ret < 0)
586 			goto error_pm_domain;
587 
588 		ret = pm_runtime_resume_and_get(vfe->camss->dev);
589 		if (ret < 0)
590 			goto error_domain_off;
591 
592 		ret = vfe_set_clock_rates(vfe);
593 		if (ret < 0)
594 			goto error_pm_runtime_get;
595 
596 		ret = camss_enable_clocks(vfe->nclocks, vfe->clock,
597 					  vfe->camss->dev);
598 		if (ret < 0)
599 			goto error_pm_runtime_get;
600 
601 		ret = vfe_reset(vfe);
602 		if (ret < 0)
603 			goto error_reset;
604 
605 		vfe_reset_output_maps(vfe);
606 
607 		vfe_init_outputs(vfe);
608 	} else {
609 		ret = vfe_check_clock_rates(vfe);
610 		if (ret < 0)
611 			goto error_pm_domain;
612 	}
613 	vfe->power_count++;
614 
615 	mutex_unlock(&vfe->power_lock);
616 
617 	return 0;
618 
619 error_reset:
620 	camss_disable_clocks(vfe->nclocks, vfe->clock);
621 
622 error_pm_runtime_get:
623 	pm_runtime_put_sync(vfe->camss->dev);
624 error_domain_off:
625 	vfe->ops->pm_domain_off(vfe);
626 
627 error_pm_domain:
628 	mutex_unlock(&vfe->power_lock);
629 
630 	return ret;
631 }
632 
633 /*
634  * vfe_put - Power down VFE module
635  * @vfe: VFE Device
636  */
vfe_put(struct vfe_device * vfe)637 static void vfe_put(struct vfe_device *vfe)
638 {
639 	mutex_lock(&vfe->power_lock);
640 
641 	if (vfe->power_count == 0) {
642 		dev_err(vfe->camss->dev, "vfe power off on power_count == 0\n");
643 		goto exit;
644 	} else if (vfe->power_count == 1) {
645 		if (vfe->was_streaming) {
646 			vfe->was_streaming = 0;
647 			vfe->ops->vfe_halt(vfe);
648 		}
649 		camss_disable_clocks(vfe->nclocks, vfe->clock);
650 		pm_runtime_put_sync(vfe->camss->dev);
651 		vfe->ops->pm_domain_off(vfe);
652 	}
653 
654 	vfe->power_count--;
655 
656 exit:
657 	mutex_unlock(&vfe->power_lock);
658 }
659 
660 /*
661  * vfe_flush_buffers - Return all vb2 buffers
662  * @vid: Video device structure
663  * @state: vb2 buffer state of the returned buffers
664  *
665  * Return all buffers to vb2. This includes queued pending buffers (still
666  * unused) and any buffers given to the hardware but again still not used.
667  *
668  * Return 0 on success or a negative error code otherwise
669  */
vfe_flush_buffers(struct camss_video * vid,enum vb2_buffer_state state)670 int vfe_flush_buffers(struct camss_video *vid,
671 		      enum vb2_buffer_state state)
672 {
673 	struct vfe_line *line = container_of(vid, struct vfe_line, video_out);
674 	struct vfe_device *vfe = to_vfe(line);
675 	struct vfe_output *output;
676 	unsigned long flags;
677 
678 	output = &line->output;
679 
680 	spin_lock_irqsave(&vfe->output_lock, flags);
681 
682 	vfe_buf_flush_pending(output, state);
683 
684 	if (output->buf[0])
685 		vb2_buffer_done(&output->buf[0]->vb.vb2_buf, state);
686 
687 	if (output->buf[1])
688 		vb2_buffer_done(&output->buf[1]->vb.vb2_buf, state);
689 
690 	if (output->last_buffer) {
691 		vb2_buffer_done(&output->last_buffer->vb.vb2_buf, state);
692 		output->last_buffer = NULL;
693 	}
694 
695 	spin_unlock_irqrestore(&vfe->output_lock, flags);
696 
697 	return 0;
698 }
699 
700 /*
701  * vfe_set_power - Power on/off VFE module
702  * @sd: VFE V4L2 subdevice
703  * @on: Requested power state
704  *
705  * Return 0 on success or a negative error code otherwise
706  */
vfe_set_power(struct v4l2_subdev * sd,int on)707 static int vfe_set_power(struct v4l2_subdev *sd, int on)
708 {
709 	struct vfe_line *line = v4l2_get_subdevdata(sd);
710 	struct vfe_device *vfe = to_vfe(line);
711 	int ret;
712 
713 	if (on) {
714 		ret = vfe_get(vfe);
715 		if (ret < 0)
716 			return ret;
717 
718 		vfe->ops->hw_version_read(vfe, vfe->camss->dev);
719 	} else {
720 		vfe_put(vfe);
721 	}
722 
723 	return 0;
724 }
725 
726 /*
727  * vfe_set_stream - Enable/disable streaming on VFE module
728  * @sd: VFE V4L2 subdevice
729  * @enable: Requested streaming state
730  *
731  * Main configuration of VFE module is triggered here.
732  *
733  * Return 0 on success or a negative error code otherwise
734  */
vfe_set_stream(struct v4l2_subdev * sd,int enable)735 static int vfe_set_stream(struct v4l2_subdev *sd, int enable)
736 {
737 	struct vfe_line *line = v4l2_get_subdevdata(sd);
738 	struct vfe_device *vfe = to_vfe(line);
739 	int ret;
740 
741 	if (enable) {
742 		ret = vfe->ops->vfe_enable(line);
743 		if (ret < 0)
744 			dev_err(vfe->camss->dev,
745 				"Failed to enable vfe outputs\n");
746 	} else {
747 		ret = vfe->ops->vfe_disable(line);
748 		if (ret < 0)
749 			dev_err(vfe->camss->dev,
750 				"Failed to disable vfe outputs\n");
751 	}
752 
753 	return ret;
754 }
755 
756 /*
757  * __vfe_get_format - Get pointer to format structure
758  * @line: VFE line
759  * @cfg: V4L2 subdev pad configuration
760  * @pad: pad from which format is requested
761  * @which: TRY or ACTIVE format
762  *
763  * Return pointer to TRY or ACTIVE format structure
764  */
765 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)766 __vfe_get_format(struct vfe_line *line,
767 		 struct v4l2_subdev_state *sd_state,
768 		 unsigned int pad,
769 		 enum v4l2_subdev_format_whence which)
770 {
771 	if (which == V4L2_SUBDEV_FORMAT_TRY)
772 		return v4l2_subdev_get_try_format(&line->subdev, sd_state,
773 						  pad);
774 
775 	return &line->fmt[pad];
776 }
777 
778 /*
779  * __vfe_get_compose - Get pointer to compose selection structure
780  * @line: VFE line
781  * @cfg: V4L2 subdev pad configuration
782  * @which: TRY or ACTIVE format
783  *
784  * Return pointer to TRY or ACTIVE compose rectangle structure
785  */
786 static struct v4l2_rect *
__vfe_get_compose(struct vfe_line * line,struct v4l2_subdev_state * sd_state,enum v4l2_subdev_format_whence which)787 __vfe_get_compose(struct vfe_line *line,
788 		  struct v4l2_subdev_state *sd_state,
789 		  enum v4l2_subdev_format_whence which)
790 {
791 	if (which == V4L2_SUBDEV_FORMAT_TRY)
792 		return v4l2_subdev_get_try_compose(&line->subdev, sd_state,
793 						   MSM_VFE_PAD_SINK);
794 
795 	return &line->compose;
796 }
797 
798 /*
799  * __vfe_get_crop - Get pointer to crop selection structure
800  * @line: VFE line
801  * @cfg: V4L2 subdev pad configuration
802  * @which: TRY or ACTIVE format
803  *
804  * Return pointer to TRY or ACTIVE crop rectangle structure
805  */
806 static struct v4l2_rect *
__vfe_get_crop(struct vfe_line * line,struct v4l2_subdev_state * sd_state,enum v4l2_subdev_format_whence which)807 __vfe_get_crop(struct vfe_line *line,
808 	       struct v4l2_subdev_state *sd_state,
809 	       enum v4l2_subdev_format_whence which)
810 {
811 	if (which == V4L2_SUBDEV_FORMAT_TRY)
812 		return v4l2_subdev_get_try_crop(&line->subdev, sd_state,
813 						MSM_VFE_PAD_SRC);
814 
815 	return &line->crop;
816 }
817 
818 /*
819  * vfe_try_format - Handle try format by pad subdev method
820  * @line: VFE line
821  * @cfg: V4L2 subdev pad configuration
822  * @pad: pad on which format is requested
823  * @fmt: pointer to v4l2 format structure
824  * @which: wanted subdev format
825  */
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)826 static void vfe_try_format(struct vfe_line *line,
827 			   struct v4l2_subdev_state *sd_state,
828 			   unsigned int pad,
829 			   struct v4l2_mbus_framefmt *fmt,
830 			   enum v4l2_subdev_format_whence which)
831 {
832 	unsigned int i;
833 	u32 code;
834 
835 	switch (pad) {
836 	case MSM_VFE_PAD_SINK:
837 		/* Set format on sink pad */
838 
839 		for (i = 0; i < line->nformats; i++)
840 			if (fmt->code == line->formats[i].code)
841 				break;
842 
843 		/* If not found, use UYVY as default */
844 		if (i >= line->nformats)
845 			fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
846 
847 		fmt->width = clamp_t(u32, fmt->width, 1, 8191);
848 		fmt->height = clamp_t(u32, fmt->height, 1, 8191);
849 
850 		fmt->field = V4L2_FIELD_NONE;
851 		fmt->colorspace = V4L2_COLORSPACE_SRGB;
852 
853 		break;
854 
855 	case MSM_VFE_PAD_SRC:
856 		/* Set and return a format same as sink pad */
857 		code = fmt->code;
858 
859 		*fmt = *__vfe_get_format(line, sd_state, MSM_VFE_PAD_SINK,
860 					 which);
861 
862 		fmt->code = vfe_src_pad_code(line, fmt->code, 0, code);
863 
864 		if (line->id == VFE_LINE_PIX) {
865 			struct v4l2_rect *rect;
866 
867 			rect = __vfe_get_crop(line, sd_state, which);
868 
869 			fmt->width = rect->width;
870 			fmt->height = rect->height;
871 		}
872 
873 		break;
874 	}
875 
876 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
877 }
878 
879 /*
880  * vfe_try_compose - Handle try compose selection by pad subdev method
881  * @line: VFE line
882  * @cfg: V4L2 subdev pad configuration
883  * @rect: pointer to v4l2 rect structure
884  * @which: wanted subdev format
885  */
vfe_try_compose(struct vfe_line * line,struct v4l2_subdev_state * sd_state,struct v4l2_rect * rect,enum v4l2_subdev_format_whence which)886 static void vfe_try_compose(struct vfe_line *line,
887 			    struct v4l2_subdev_state *sd_state,
888 			    struct v4l2_rect *rect,
889 			    enum v4l2_subdev_format_whence which)
890 {
891 	struct v4l2_mbus_framefmt *fmt;
892 
893 	fmt = __vfe_get_format(line, sd_state, MSM_VFE_PAD_SINK, which);
894 
895 	if (rect->width > fmt->width)
896 		rect->width = fmt->width;
897 
898 	if (rect->height > fmt->height)
899 		rect->height = fmt->height;
900 
901 	if (fmt->width > rect->width * SCALER_RATIO_MAX)
902 		rect->width = (fmt->width + SCALER_RATIO_MAX - 1) /
903 							SCALER_RATIO_MAX;
904 
905 	rect->width &= ~0x1;
906 
907 	if (fmt->height > rect->height * SCALER_RATIO_MAX)
908 		rect->height = (fmt->height + SCALER_RATIO_MAX - 1) /
909 							SCALER_RATIO_MAX;
910 
911 	if (rect->width < 16)
912 		rect->width = 16;
913 
914 	if (rect->height < 4)
915 		rect->height = 4;
916 }
917 
918 /*
919  * vfe_try_crop - Handle try crop selection by pad subdev method
920  * @line: VFE line
921  * @cfg: V4L2 subdev pad configuration
922  * @rect: pointer to v4l2 rect structure
923  * @which: wanted subdev format
924  */
vfe_try_crop(struct vfe_line * line,struct v4l2_subdev_state * sd_state,struct v4l2_rect * rect,enum v4l2_subdev_format_whence which)925 static void vfe_try_crop(struct vfe_line *line,
926 			 struct v4l2_subdev_state *sd_state,
927 			 struct v4l2_rect *rect,
928 			 enum v4l2_subdev_format_whence which)
929 {
930 	struct v4l2_rect *compose;
931 
932 	compose = __vfe_get_compose(line, sd_state, which);
933 
934 	if (rect->width > compose->width)
935 		rect->width = compose->width;
936 
937 	if (rect->width + rect->left > compose->width)
938 		rect->left = compose->width - rect->width;
939 
940 	if (rect->height > compose->height)
941 		rect->height = compose->height;
942 
943 	if (rect->height + rect->top > compose->height)
944 		rect->top = compose->height - rect->height;
945 
946 	/* wm in line based mode writes multiple of 16 horizontally */
947 	rect->left += (rect->width & 0xf) >> 1;
948 	rect->width &= ~0xf;
949 
950 	if (rect->width < 16) {
951 		rect->left = 0;
952 		rect->width = 16;
953 	}
954 
955 	if (rect->height < 4) {
956 		rect->top = 0;
957 		rect->height = 4;
958 	}
959 }
960 
961 /*
962  * vfe_enum_mbus_code - Handle pixel format enumeration
963  * @sd: VFE V4L2 subdevice
964  * @cfg: V4L2 subdev pad configuration
965  * @code: pointer to v4l2_subdev_mbus_code_enum structure
966  *
967  * return -EINVAL or zero on success
968  */
vfe_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)969 static int vfe_enum_mbus_code(struct v4l2_subdev *sd,
970 			      struct v4l2_subdev_state *sd_state,
971 			      struct v4l2_subdev_mbus_code_enum *code)
972 {
973 	struct vfe_line *line = v4l2_get_subdevdata(sd);
974 
975 	if (code->pad == MSM_VFE_PAD_SINK) {
976 		if (code->index >= line->nformats)
977 			return -EINVAL;
978 
979 		code->code = line->formats[code->index].code;
980 	} else {
981 		struct v4l2_mbus_framefmt *sink_fmt;
982 
983 		sink_fmt = __vfe_get_format(line, sd_state, MSM_VFE_PAD_SINK,
984 					    code->which);
985 
986 		code->code = vfe_src_pad_code(line, sink_fmt->code,
987 					      code->index, 0);
988 		if (!code->code)
989 			return -EINVAL;
990 	}
991 
992 	return 0;
993 }
994 
995 /*
996  * vfe_enum_frame_size - Handle frame size enumeration
997  * @sd: VFE V4L2 subdevice
998  * @cfg: V4L2 subdev pad configuration
999  * @fse: pointer to v4l2_subdev_frame_size_enum structure
1000  *
1001  * Return -EINVAL or zero on success
1002  */
vfe_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)1003 static int vfe_enum_frame_size(struct v4l2_subdev *sd,
1004 			       struct v4l2_subdev_state *sd_state,
1005 			       struct v4l2_subdev_frame_size_enum *fse)
1006 {
1007 	struct vfe_line *line = v4l2_get_subdevdata(sd);
1008 	struct v4l2_mbus_framefmt format;
1009 
1010 	if (fse->index != 0)
1011 		return -EINVAL;
1012 
1013 	format.code = fse->code;
1014 	format.width = 1;
1015 	format.height = 1;
1016 	vfe_try_format(line, sd_state, fse->pad, &format, fse->which);
1017 	fse->min_width = format.width;
1018 	fse->min_height = format.height;
1019 
1020 	if (format.code != fse->code)
1021 		return -EINVAL;
1022 
1023 	format.code = fse->code;
1024 	format.width = -1;
1025 	format.height = -1;
1026 	vfe_try_format(line, sd_state, fse->pad, &format, fse->which);
1027 	fse->max_width = format.width;
1028 	fse->max_height = format.height;
1029 
1030 	return 0;
1031 }
1032 
1033 /*
1034  * vfe_get_format - Handle get format by pads subdev method
1035  * @sd: VFE V4L2 subdevice
1036  * @cfg: V4L2 subdev pad configuration
1037  * @fmt: pointer to v4l2 subdev format structure
1038  *
1039  * Return -EINVAL or zero on success
1040  */
vfe_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1041 static int vfe_get_format(struct v4l2_subdev *sd,
1042 			  struct v4l2_subdev_state *sd_state,
1043 			  struct v4l2_subdev_format *fmt)
1044 {
1045 	struct vfe_line *line = v4l2_get_subdevdata(sd);
1046 	struct v4l2_mbus_framefmt *format;
1047 
1048 	format = __vfe_get_format(line, sd_state, fmt->pad, fmt->which);
1049 	if (format == NULL)
1050 		return -EINVAL;
1051 
1052 	fmt->format = *format;
1053 
1054 	return 0;
1055 }
1056 
1057 static int vfe_set_selection(struct v4l2_subdev *sd,
1058 			     struct v4l2_subdev_state *sd_state,
1059 			     struct v4l2_subdev_selection *sel);
1060 
1061 /*
1062  * vfe_set_format - Handle set format by pads subdev method
1063  * @sd: VFE V4L2 subdevice
1064  * @cfg: V4L2 subdev pad configuration
1065  * @fmt: pointer to v4l2 subdev format structure
1066  *
1067  * Return -EINVAL or zero on success
1068  */
vfe_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1069 static int vfe_set_format(struct v4l2_subdev *sd,
1070 			  struct v4l2_subdev_state *sd_state,
1071 			  struct v4l2_subdev_format *fmt)
1072 {
1073 	struct vfe_line *line = v4l2_get_subdevdata(sd);
1074 	struct v4l2_mbus_framefmt *format;
1075 
1076 	format = __vfe_get_format(line, sd_state, fmt->pad, fmt->which);
1077 	if (format == NULL)
1078 		return -EINVAL;
1079 
1080 	vfe_try_format(line, sd_state, fmt->pad, &fmt->format, fmt->which);
1081 	*format = fmt->format;
1082 
1083 	if (fmt->pad == MSM_VFE_PAD_SINK) {
1084 		struct v4l2_subdev_selection sel = { 0 };
1085 		int ret;
1086 
1087 		/* Propagate the format from sink to source */
1088 		format = __vfe_get_format(line, sd_state, MSM_VFE_PAD_SRC,
1089 					  fmt->which);
1090 
1091 		*format = fmt->format;
1092 		vfe_try_format(line, sd_state, MSM_VFE_PAD_SRC, format,
1093 			       fmt->which);
1094 
1095 		if (line->id != VFE_LINE_PIX)
1096 			return 0;
1097 
1098 		/* Reset sink pad compose selection */
1099 		sel.which = fmt->which;
1100 		sel.pad = MSM_VFE_PAD_SINK;
1101 		sel.target = V4L2_SEL_TGT_COMPOSE;
1102 		sel.r.width = fmt->format.width;
1103 		sel.r.height = fmt->format.height;
1104 		ret = vfe_set_selection(sd, sd_state, &sel);
1105 		if (ret < 0)
1106 			return ret;
1107 	}
1108 
1109 	return 0;
1110 }
1111 
1112 /*
1113  * vfe_get_selection - Handle get selection by pads subdev method
1114  * @sd: VFE V4L2 subdevice
1115  * @cfg: V4L2 subdev pad configuration
1116  * @sel: pointer to v4l2 subdev selection structure
1117  *
1118  * Return -EINVAL or zero on success
1119  */
vfe_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1120 static int vfe_get_selection(struct v4l2_subdev *sd,
1121 			     struct v4l2_subdev_state *sd_state,
1122 			     struct v4l2_subdev_selection *sel)
1123 {
1124 	struct vfe_line *line = v4l2_get_subdevdata(sd);
1125 	struct v4l2_subdev_format fmt = { 0 };
1126 	struct v4l2_rect *rect;
1127 	int ret;
1128 
1129 	if (line->id != VFE_LINE_PIX)
1130 		return -EINVAL;
1131 
1132 	if (sel->pad == MSM_VFE_PAD_SINK)
1133 		switch (sel->target) {
1134 		case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1135 			fmt.pad = sel->pad;
1136 			fmt.which = sel->which;
1137 			ret = vfe_get_format(sd, sd_state, &fmt);
1138 			if (ret < 0)
1139 				return ret;
1140 
1141 			sel->r.left = 0;
1142 			sel->r.top = 0;
1143 			sel->r.width = fmt.format.width;
1144 			sel->r.height = fmt.format.height;
1145 			break;
1146 		case V4L2_SEL_TGT_COMPOSE:
1147 			rect = __vfe_get_compose(line, sd_state, sel->which);
1148 			if (rect == NULL)
1149 				return -EINVAL;
1150 
1151 			sel->r = *rect;
1152 			break;
1153 		default:
1154 			return -EINVAL;
1155 		}
1156 	else if (sel->pad == MSM_VFE_PAD_SRC)
1157 		switch (sel->target) {
1158 		case V4L2_SEL_TGT_CROP_BOUNDS:
1159 			rect = __vfe_get_compose(line, sd_state, sel->which);
1160 			if (rect == NULL)
1161 				return -EINVAL;
1162 
1163 			sel->r.left = rect->left;
1164 			sel->r.top = rect->top;
1165 			sel->r.width = rect->width;
1166 			sel->r.height = rect->height;
1167 			break;
1168 		case V4L2_SEL_TGT_CROP:
1169 			rect = __vfe_get_crop(line, sd_state, sel->which);
1170 			if (rect == NULL)
1171 				return -EINVAL;
1172 
1173 			sel->r = *rect;
1174 			break;
1175 		default:
1176 			return -EINVAL;
1177 		}
1178 
1179 	return 0;
1180 }
1181 
1182 /*
1183  * vfe_set_selection - Handle set selection by pads subdev method
1184  * @sd: VFE V4L2 subdevice
1185  * @cfg: V4L2 subdev pad configuration
1186  * @sel: pointer to v4l2 subdev selection structure
1187  *
1188  * Return -EINVAL or zero on success
1189  */
vfe_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)1190 static int vfe_set_selection(struct v4l2_subdev *sd,
1191 			     struct v4l2_subdev_state *sd_state,
1192 			     struct v4l2_subdev_selection *sel)
1193 {
1194 	struct vfe_line *line = v4l2_get_subdevdata(sd);
1195 	struct v4l2_rect *rect;
1196 	int ret;
1197 
1198 	if (line->id != VFE_LINE_PIX)
1199 		return -EINVAL;
1200 
1201 	if (sel->target == V4L2_SEL_TGT_COMPOSE &&
1202 		sel->pad == MSM_VFE_PAD_SINK) {
1203 		struct v4l2_subdev_selection crop = { 0 };
1204 
1205 		rect = __vfe_get_compose(line, sd_state, sel->which);
1206 		if (rect == NULL)
1207 			return -EINVAL;
1208 
1209 		vfe_try_compose(line, sd_state, &sel->r, sel->which);
1210 		*rect = sel->r;
1211 
1212 		/* Reset source crop selection */
1213 		crop.which = sel->which;
1214 		crop.pad = MSM_VFE_PAD_SRC;
1215 		crop.target = V4L2_SEL_TGT_CROP;
1216 		crop.r = *rect;
1217 		ret = vfe_set_selection(sd, sd_state, &crop);
1218 	} else if (sel->target == V4L2_SEL_TGT_CROP &&
1219 		sel->pad == MSM_VFE_PAD_SRC) {
1220 		struct v4l2_subdev_format fmt = { 0 };
1221 
1222 		rect = __vfe_get_crop(line, sd_state, sel->which);
1223 		if (rect == NULL)
1224 			return -EINVAL;
1225 
1226 		vfe_try_crop(line, sd_state, &sel->r, sel->which);
1227 		*rect = sel->r;
1228 
1229 		/* Reset source pad format width and height */
1230 		fmt.which = sel->which;
1231 		fmt.pad = MSM_VFE_PAD_SRC;
1232 		ret = vfe_get_format(sd, sd_state, &fmt);
1233 		if (ret < 0)
1234 			return ret;
1235 
1236 		fmt.format.width = rect->width;
1237 		fmt.format.height = rect->height;
1238 		ret = vfe_set_format(sd, sd_state, &fmt);
1239 	} else {
1240 		ret = -EINVAL;
1241 	}
1242 
1243 	return ret;
1244 }
1245 
1246 /*
1247  * vfe_init_formats - Initialize formats on all pads
1248  * @sd: VFE V4L2 subdevice
1249  * @fh: V4L2 subdev file handle
1250  *
1251  * Initialize all pad formats with default values.
1252  *
1253  * Return 0 on success or a negative error code otherwise
1254  */
vfe_init_formats(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1255 static int vfe_init_formats(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1256 {
1257 	struct v4l2_subdev_format format = {
1258 		.pad = MSM_VFE_PAD_SINK,
1259 		.which = fh ? V4L2_SUBDEV_FORMAT_TRY :
1260 			      V4L2_SUBDEV_FORMAT_ACTIVE,
1261 		.format = {
1262 			.code = MEDIA_BUS_FMT_UYVY8_2X8,
1263 			.width = 1920,
1264 			.height = 1080
1265 		}
1266 	};
1267 
1268 	return vfe_set_format(sd, fh ? fh->state : NULL, &format);
1269 }
1270 
1271 /*
1272  * msm_vfe_subdev_init - Initialize VFE device structure and resources
1273  * @vfe: VFE device
1274  * @res: VFE module resources table
1275  *
1276  * Return 0 on success or a negative error code otherwise
1277  */
msm_vfe_subdev_init(struct camss * camss,struct vfe_device * vfe,const struct resources * res,u8 id)1278 int msm_vfe_subdev_init(struct camss *camss, struct vfe_device *vfe,
1279 			const struct resources *res, u8 id)
1280 {
1281 	struct device *dev = camss->dev;
1282 	struct platform_device *pdev = to_platform_device(dev);
1283 	struct resource *r;
1284 	int i, j;
1285 	int ret;
1286 
1287 	switch (camss->version) {
1288 	case CAMSS_8x16:
1289 		vfe->ops = &vfe_ops_4_1;
1290 		break;
1291 	case CAMSS_8x96:
1292 		vfe->ops = &vfe_ops_4_7;
1293 		break;
1294 	case CAMSS_660:
1295 		vfe->ops = &vfe_ops_4_8;
1296 		break;
1297 
1298 	case CAMSS_845:
1299 		vfe->ops = &vfe_ops_170;
1300 		break;
1301 	default:
1302 		return -EINVAL;
1303 	}
1304 	vfe->ops->subdev_init(dev, vfe);
1305 
1306 	/* Memory */
1307 
1308 	vfe->base = devm_platform_ioremap_resource_byname(pdev, res->reg[0]);
1309 	if (IS_ERR(vfe->base)) {
1310 		dev_err(dev, "could not map memory\n");
1311 		return PTR_ERR(vfe->base);
1312 	}
1313 
1314 	/* Interrupt */
1315 
1316 	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1317 					 res->interrupt[0]);
1318 	if (!r) {
1319 		dev_err(dev, "missing IRQ\n");
1320 		return -EINVAL;
1321 	}
1322 
1323 	vfe->irq = r->start;
1324 	snprintf(vfe->irq_name, sizeof(vfe->irq_name), "%s_%s%d",
1325 		 dev_name(dev), MSM_VFE_NAME, vfe->id);
1326 	ret = devm_request_irq(dev, vfe->irq, vfe->ops->isr,
1327 			       IRQF_TRIGGER_RISING, vfe->irq_name, vfe);
1328 	if (ret < 0) {
1329 		dev_err(dev, "request_irq failed: %d\n", ret);
1330 		return ret;
1331 	}
1332 
1333 	/* Clocks */
1334 
1335 	vfe->nclocks = 0;
1336 	while (res->clock[vfe->nclocks])
1337 		vfe->nclocks++;
1338 
1339 	vfe->clock = devm_kcalloc(dev, vfe->nclocks, sizeof(*vfe->clock),
1340 				  GFP_KERNEL);
1341 	if (!vfe->clock)
1342 		return -ENOMEM;
1343 
1344 	for (i = 0; i < vfe->nclocks; i++) {
1345 		struct camss_clock *clock = &vfe->clock[i];
1346 
1347 		clock->clk = devm_clk_get(dev, res->clock[i]);
1348 		if (IS_ERR(clock->clk))
1349 			return PTR_ERR(clock->clk);
1350 
1351 		clock->name = res->clock[i];
1352 
1353 		clock->nfreqs = 0;
1354 		while (res->clock_rate[i][clock->nfreqs])
1355 			clock->nfreqs++;
1356 
1357 		if (!clock->nfreqs) {
1358 			clock->freq = NULL;
1359 			continue;
1360 		}
1361 
1362 		clock->freq = devm_kcalloc(dev,
1363 					   clock->nfreqs,
1364 					   sizeof(*clock->freq),
1365 					   GFP_KERNEL);
1366 		if (!clock->freq)
1367 			return -ENOMEM;
1368 
1369 		for (j = 0; j < clock->nfreqs; j++)
1370 			clock->freq[j] = res->clock_rate[i][j];
1371 	}
1372 
1373 	mutex_init(&vfe->power_lock);
1374 	vfe->power_count = 0;
1375 
1376 	mutex_init(&vfe->stream_lock);
1377 	vfe->stream_count = 0;
1378 
1379 	spin_lock_init(&vfe->output_lock);
1380 
1381 	vfe->camss = camss;
1382 	vfe->id = id;
1383 	vfe->reg_update = 0;
1384 
1385 	for (i = VFE_LINE_RDI0; i < vfe->line_num; i++) {
1386 		struct vfe_line *l = &vfe->line[i];
1387 
1388 		l->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1389 		l->video_out.camss = camss;
1390 		l->id = i;
1391 		init_completion(&l->output.sof);
1392 		init_completion(&l->output.reg_update);
1393 
1394 		if (camss->version == CAMSS_8x16) {
1395 			if (i == VFE_LINE_PIX) {
1396 				l->formats = formats_pix_8x16;
1397 				l->nformats = ARRAY_SIZE(formats_pix_8x16);
1398 			} else {
1399 				l->formats = formats_rdi_8x16;
1400 				l->nformats = ARRAY_SIZE(formats_rdi_8x16);
1401 			}
1402 		} else if (camss->version == CAMSS_8x96 ||
1403 			   camss->version == CAMSS_660) {
1404 			if (i == VFE_LINE_PIX) {
1405 				l->formats = formats_pix_8x96;
1406 				l->nformats = ARRAY_SIZE(formats_pix_8x96);
1407 			} else {
1408 				l->formats = formats_rdi_8x96;
1409 				l->nformats = ARRAY_SIZE(formats_rdi_8x96);
1410 			}
1411 		} else if (camss->version == CAMSS_845) {
1412 			l->formats = formats_rdi_845;
1413 			l->nformats = ARRAY_SIZE(formats_rdi_845);
1414 		} else {
1415 			return -EINVAL;
1416 		}
1417 	}
1418 
1419 	init_completion(&vfe->reset_complete);
1420 	init_completion(&vfe->halt_complete);
1421 
1422 	return 0;
1423 }
1424 
1425 /*
1426  * msm_vfe_get_vfe_id - Get VFE HW module id
1427  * @entity: Pointer to VFE media entity structure
1428  * @id: Return CSID HW module id here
1429  */
msm_vfe_get_vfe_id(struct media_entity * entity,u8 * id)1430 void msm_vfe_get_vfe_id(struct media_entity *entity, u8 *id)
1431 {
1432 	struct v4l2_subdev *sd;
1433 	struct vfe_line *line;
1434 	struct vfe_device *vfe;
1435 
1436 	sd = media_entity_to_v4l2_subdev(entity);
1437 	line = v4l2_get_subdevdata(sd);
1438 	vfe = to_vfe(line);
1439 
1440 	*id = vfe->id;
1441 }
1442 
1443 /*
1444  * msm_vfe_get_vfe_line_id - Get VFE line id by media entity
1445  * @entity: Pointer to VFE media entity structure
1446  * @id: Return VFE line id here
1447  */
msm_vfe_get_vfe_line_id(struct media_entity * entity,enum vfe_line_id * id)1448 void msm_vfe_get_vfe_line_id(struct media_entity *entity, enum vfe_line_id *id)
1449 {
1450 	struct v4l2_subdev *sd;
1451 	struct vfe_line *line;
1452 
1453 	sd = media_entity_to_v4l2_subdev(entity);
1454 	line = v4l2_get_subdevdata(sd);
1455 
1456 	*id = line->id;
1457 }
1458 
1459 /*
1460  * vfe_link_setup - Setup VFE connections
1461  * @entity: Pointer to media entity structure
1462  * @local: Pointer to local pad
1463  * @remote: Pointer to remote pad
1464  * @flags: Link flags
1465  *
1466  * Return 0 on success
1467  */
vfe_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)1468 static int vfe_link_setup(struct media_entity *entity,
1469 			  const struct media_pad *local,
1470 			  const struct media_pad *remote, u32 flags)
1471 {
1472 	if (flags & MEDIA_LNK_FL_ENABLED)
1473 		if (media_entity_remote_pad(local))
1474 			return -EBUSY;
1475 
1476 	return 0;
1477 }
1478 
1479 static const struct v4l2_subdev_core_ops vfe_core_ops = {
1480 	.s_power = vfe_set_power,
1481 };
1482 
1483 static const struct v4l2_subdev_video_ops vfe_video_ops = {
1484 	.s_stream = vfe_set_stream,
1485 };
1486 
1487 static const struct v4l2_subdev_pad_ops vfe_pad_ops = {
1488 	.enum_mbus_code = vfe_enum_mbus_code,
1489 	.enum_frame_size = vfe_enum_frame_size,
1490 	.get_fmt = vfe_get_format,
1491 	.set_fmt = vfe_set_format,
1492 	.get_selection = vfe_get_selection,
1493 	.set_selection = vfe_set_selection,
1494 };
1495 
1496 static const struct v4l2_subdev_ops vfe_v4l2_ops = {
1497 	.core = &vfe_core_ops,
1498 	.video = &vfe_video_ops,
1499 	.pad = &vfe_pad_ops,
1500 };
1501 
1502 static const struct v4l2_subdev_internal_ops vfe_v4l2_internal_ops = {
1503 	.open = vfe_init_formats,
1504 };
1505 
1506 static const struct media_entity_operations vfe_media_ops = {
1507 	.link_setup = vfe_link_setup,
1508 	.link_validate = v4l2_subdev_link_validate,
1509 };
1510 
1511 /*
1512  * msm_vfe_register_entities - Register subdev node for VFE module
1513  * @vfe: VFE device
1514  * @v4l2_dev: V4L2 device
1515  *
1516  * Initialize and register a subdev node for the VFE module. Then
1517  * call msm_video_register() to register the video device node which
1518  * will be connected to this subdev node. Then actually create the
1519  * media link between them.
1520  *
1521  * Return 0 on success or a negative error code otherwise
1522  */
msm_vfe_register_entities(struct vfe_device * vfe,struct v4l2_device * v4l2_dev)1523 int msm_vfe_register_entities(struct vfe_device *vfe,
1524 			      struct v4l2_device *v4l2_dev)
1525 {
1526 	struct device *dev = vfe->camss->dev;
1527 	struct v4l2_subdev *sd;
1528 	struct media_pad *pads;
1529 	struct camss_video *video_out;
1530 	int ret;
1531 	int i;
1532 
1533 	for (i = 0; i < vfe->line_num; i++) {
1534 		char name[32];
1535 
1536 		sd = &vfe->line[i].subdev;
1537 		pads = vfe->line[i].pads;
1538 		video_out = &vfe->line[i].video_out;
1539 
1540 		v4l2_subdev_init(sd, &vfe_v4l2_ops);
1541 		sd->internal_ops = &vfe_v4l2_internal_ops;
1542 		sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1543 		if (i == VFE_LINE_PIX)
1544 			snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s",
1545 				 MSM_VFE_NAME, vfe->id, "pix");
1546 		else
1547 			snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d_%s%d",
1548 				 MSM_VFE_NAME, vfe->id, "rdi", i);
1549 
1550 		v4l2_set_subdevdata(sd, &vfe->line[i]);
1551 
1552 		ret = vfe_init_formats(sd, NULL);
1553 		if (ret < 0) {
1554 			dev_err(dev, "Failed to init format: %d\n", ret);
1555 			goto error_init;
1556 		}
1557 
1558 		pads[MSM_VFE_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1559 		pads[MSM_VFE_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
1560 
1561 		sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
1562 		sd->entity.ops = &vfe_media_ops;
1563 		ret = media_entity_pads_init(&sd->entity, MSM_VFE_PADS_NUM,
1564 					     pads);
1565 		if (ret < 0) {
1566 			dev_err(dev, "Failed to init media entity: %d\n", ret);
1567 			goto error_init;
1568 		}
1569 
1570 		ret = v4l2_device_register_subdev(v4l2_dev, sd);
1571 		if (ret < 0) {
1572 			dev_err(dev, "Failed to register subdev: %d\n", ret);
1573 			goto error_reg_subdev;
1574 		}
1575 
1576 		video_out->ops = &vfe->video_ops;
1577 		video_out->bpl_alignment = 8;
1578 		video_out->line_based = 0;
1579 		if (i == VFE_LINE_PIX) {
1580 			video_out->bpl_alignment = 16;
1581 			video_out->line_based = 1;
1582 		}
1583 		snprintf(name, ARRAY_SIZE(name), "%s%d_%s%d",
1584 			 MSM_VFE_NAME, vfe->id, "video", i);
1585 		ret = msm_video_register(video_out, v4l2_dev, name,
1586 					 i == VFE_LINE_PIX ? 1 : 0);
1587 		if (ret < 0) {
1588 			dev_err(dev, "Failed to register video node: %d\n",
1589 				ret);
1590 			goto error_reg_video;
1591 		}
1592 
1593 		ret = media_create_pad_link(
1594 				&sd->entity, MSM_VFE_PAD_SRC,
1595 				&video_out->vdev.entity, 0,
1596 				MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1597 		if (ret < 0) {
1598 			dev_err(dev, "Failed to link %s->%s entities: %d\n",
1599 				sd->entity.name, video_out->vdev.entity.name,
1600 				ret);
1601 			goto error_link;
1602 		}
1603 	}
1604 
1605 	return 0;
1606 
1607 error_link:
1608 	msm_video_unregister(video_out);
1609 
1610 error_reg_video:
1611 	v4l2_device_unregister_subdev(sd);
1612 
1613 error_reg_subdev:
1614 	media_entity_cleanup(&sd->entity);
1615 
1616 error_init:
1617 	for (i--; i >= 0; i--) {
1618 		sd = &vfe->line[i].subdev;
1619 		video_out = &vfe->line[i].video_out;
1620 
1621 		msm_video_unregister(video_out);
1622 		v4l2_device_unregister_subdev(sd);
1623 		media_entity_cleanup(&sd->entity);
1624 	}
1625 
1626 	return ret;
1627 }
1628 
1629 /*
1630  * msm_vfe_unregister_entities - Unregister VFE module subdev node
1631  * @vfe: VFE device
1632  */
msm_vfe_unregister_entities(struct vfe_device * vfe)1633 void msm_vfe_unregister_entities(struct vfe_device *vfe)
1634 {
1635 	int i;
1636 
1637 	mutex_destroy(&vfe->power_lock);
1638 	mutex_destroy(&vfe->stream_lock);
1639 
1640 	for (i = 0; i < vfe->line_num; i++) {
1641 		struct v4l2_subdev *sd = &vfe->line[i].subdev;
1642 		struct camss_video *video_out = &vfe->line[i].video_out;
1643 
1644 		msm_video_unregister(video_out);
1645 		v4l2_device_unregister_subdev(sd);
1646 		media_entity_cleanup(&sd->entity);
1647 	}
1648 }
1649