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