1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/delay.h>
24#include <linux/err.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/module.h>
29#include <linux/of_graph.h>
30#include <linux/pinctrl/consumer.h>
31#include <linux/platform_device.h>
32#include <linux/pm_runtime.h>
33#include <linux/slab.h>
34#include <linux/uaccess.h>
35#include <linux/videodev2.h>
36
37#include <media/v4l2-common.h>
38#include <media/v4l2-ctrls.h>
39#include <media/v4l2-event.h>
40#include <media/v4l2-fwnode.h>
41
42#include "am437x-vpfe.h"
43
44#define VPFE_MODULE_NAME "vpfe"
45#define VPFE_VERSION "0.1.0"
46
47static int debug;
48module_param(debug, int, 0644);
49MODULE_PARM_DESC(debug, "Debug level 0-8");
50
51#define vpfe_dbg(level, dev, fmt, arg...) \
52 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
53#define vpfe_info(dev, fmt, arg...) \
54 v4l2_info(&dev->v4l2_dev, fmt, ##arg)
55#define vpfe_err(dev, fmt, arg...) \
56 v4l2_err(&dev->v4l2_dev, fmt, ##arg)
57
58
59struct vpfe_standard {
60 v4l2_std_id std_id;
61 unsigned int width;
62 unsigned int height;
63 struct v4l2_fract pixelaspect;
64 int frame_format;
65};
66
67static const struct vpfe_standard vpfe_standards[] = {
68 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
69 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
70};
71
72struct bus_format {
73 unsigned int width;
74 unsigned int bpp;
75};
76
77
78
79
80
81
82
83
84
85
86
87
88struct vpfe_fmt {
89 const char *name;
90 u32 fourcc;
91 u32 code;
92 struct bus_format l;
93 struct bus_format s;
94 bool supported;
95 u32 index;
96};
97
98static struct vpfe_fmt formats[] = {
99 {
100 .name = "YUV 4:2:2 packed, YCbYCr",
101 .fourcc = V4L2_PIX_FMT_YUYV,
102 .code = MEDIA_BUS_FMT_YUYV8_2X8,
103 .l.width = 10,
104 .l.bpp = 4,
105 .s.width = 8,
106 .s.bpp = 2,
107 .supported = false,
108 }, {
109 .name = "YUV 4:2:2 packed, CbYCrY",
110 .fourcc = V4L2_PIX_FMT_UYVY,
111 .code = MEDIA_BUS_FMT_UYVY8_2X8,
112 .l.width = 10,
113 .l.bpp = 4,
114 .s.width = 8,
115 .s.bpp = 2,
116 .supported = false,
117 }, {
118 .name = "YUV 4:2:2 packed, YCrYCb",
119 .fourcc = V4L2_PIX_FMT_YVYU,
120 .code = MEDIA_BUS_FMT_YVYU8_2X8,
121 .l.width = 10,
122 .l.bpp = 4,
123 .s.width = 8,
124 .s.bpp = 2,
125 .supported = false,
126 }, {
127 .name = "YUV 4:2:2 packed, CrYCbY",
128 .fourcc = V4L2_PIX_FMT_VYUY,
129 .code = MEDIA_BUS_FMT_VYUY8_2X8,
130 .l.width = 10,
131 .l.bpp = 4,
132 .s.width = 8,
133 .s.bpp = 2,
134 .supported = false,
135 }, {
136 .name = "RAW8 BGGR",
137 .fourcc = V4L2_PIX_FMT_SBGGR8,
138 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
139 .l.width = 10,
140 .l.bpp = 2,
141 .s.width = 8,
142 .s.bpp = 1,
143 .supported = false,
144 }, {
145 .name = "RAW8 GBRG",
146 .fourcc = V4L2_PIX_FMT_SGBRG8,
147 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
148 .l.width = 10,
149 .l.bpp = 2,
150 .s.width = 8,
151 .s.bpp = 1,
152 .supported = false,
153 }, {
154 .name = "RAW8 GRBG",
155 .fourcc = V4L2_PIX_FMT_SGRBG8,
156 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
157 .l.width = 10,
158 .l.bpp = 2,
159 .s.width = 8,
160 .s.bpp = 1,
161 .supported = false,
162 }, {
163 .name = "RAW8 RGGB",
164 .fourcc = V4L2_PIX_FMT_SRGGB8,
165 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
166 .l.width = 10,
167 .l.bpp = 2,
168 .s.width = 8,
169 .s.bpp = 1,
170 .supported = false,
171 }, {
172 .name = "RGB565 (LE)",
173 .fourcc = V4L2_PIX_FMT_RGB565,
174 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
175 .l.width = 10,
176 .l.bpp = 4,
177 .s.width = 8,
178 .s.bpp = 2,
179 .supported = false,
180 }, {
181 .name = "RGB565 (BE)",
182 .fourcc = V4L2_PIX_FMT_RGB565X,
183 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
184 .l.width = 10,
185 .l.bpp = 4,
186 .s.width = 8,
187 .s.bpp = 2,
188 .supported = false,
189 },
190};
191
192static int
193__vpfe_get_format(struct vpfe_device *vpfe,
194 struct v4l2_format *format, unsigned int *bpp);
195
196static struct vpfe_fmt *find_format_by_code(unsigned int code)
197{
198 struct vpfe_fmt *fmt;
199 unsigned int k;
200
201 for (k = 0; k < ARRAY_SIZE(formats); k++) {
202 fmt = &formats[k];
203 if (fmt->code == code)
204 return fmt;
205 }
206
207 return NULL;
208}
209
210static struct vpfe_fmt *find_format_by_pix(unsigned int pixelformat)
211{
212 struct vpfe_fmt *fmt;
213 unsigned int k;
214
215 for (k = 0; k < ARRAY_SIZE(formats); k++) {
216 fmt = &formats[k];
217 if (fmt->fourcc == pixelformat)
218 return fmt;
219 }
220
221 return NULL;
222}
223
224static void
225mbus_to_pix(struct vpfe_device *vpfe,
226 const struct v4l2_mbus_framefmt *mbus,
227 struct v4l2_pix_format *pix, unsigned int *bpp)
228{
229 struct vpfe_subdev_info *sdinfo = vpfe->current_subdev;
230 unsigned int bus_width = sdinfo->vpfe_param.bus_width;
231 struct vpfe_fmt *fmt;
232
233 fmt = find_format_by_code(mbus->code);
234 if (WARN_ON(fmt == NULL)) {
235 pr_err("Invalid mbus code set\n");
236 *bpp = 1;
237 return;
238 }
239
240 memset(pix, 0, sizeof(*pix));
241 v4l2_fill_pix_format(pix, mbus);
242 pix->pixelformat = fmt->fourcc;
243 *bpp = (bus_width == 10) ? fmt->l.bpp : fmt->s.bpp;
244
245
246 pix->bytesperline = ALIGN(pix->width * *bpp, 32);
247 pix->sizeimage = pix->bytesperline * pix->height;
248}
249
250static void pix_to_mbus(struct vpfe_device *vpfe,
251 struct v4l2_pix_format *pix_fmt,
252 struct v4l2_mbus_framefmt *mbus_fmt)
253{
254 struct vpfe_fmt *fmt;
255
256 fmt = find_format_by_pix(pix_fmt->pixelformat);
257 if (!fmt) {
258
259 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
260 pix_fmt->pixelformat);
261 fmt = &formats[0];
262 }
263
264 memset(mbus_fmt, 0, sizeof(*mbus_fmt));
265 v4l2_fill_mbus_format(mbus_fmt, pix_fmt, fmt->code);
266}
267
268
269static char *print_fourcc(u32 fmt)
270{
271 static char code[5];
272
273 code[0] = (unsigned char)(fmt & 0xff);
274 code[1] = (unsigned char)((fmt >> 8) & 0xff);
275 code[2] = (unsigned char)((fmt >> 16) & 0xff);
276 code[3] = (unsigned char)((fmt >> 24) & 0xff);
277 code[4] = '\0';
278
279 return code;
280}
281
282static int
283cmp_v4l2_format(const struct v4l2_format *lhs, const struct v4l2_format *rhs)
284{
285 return lhs->type == rhs->type &&
286 lhs->fmt.pix.width == rhs->fmt.pix.width &&
287 lhs->fmt.pix.height == rhs->fmt.pix.height &&
288 lhs->fmt.pix.pixelformat == rhs->fmt.pix.pixelformat &&
289 lhs->fmt.pix.field == rhs->fmt.pix.field &&
290 lhs->fmt.pix.colorspace == rhs->fmt.pix.colorspace &&
291 lhs->fmt.pix.ycbcr_enc == rhs->fmt.pix.ycbcr_enc &&
292 lhs->fmt.pix.quantization == rhs->fmt.pix.quantization &&
293 lhs->fmt.pix.xfer_func == rhs->fmt.pix.xfer_func;
294}
295
296static inline u32 vpfe_reg_read(struct vpfe_ccdc *ccdc, u32 offset)
297{
298 return ioread32(ccdc->ccdc_cfg.base_addr + offset);
299}
300
301static inline void vpfe_reg_write(struct vpfe_ccdc *ccdc, u32 val, u32 offset)
302{
303 iowrite32(val, ccdc->ccdc_cfg.base_addr + offset);
304}
305
306static inline struct vpfe_device *to_vpfe(struct vpfe_ccdc *ccdc)
307{
308 return container_of(ccdc, struct vpfe_device, ccdc);
309}
310
311static inline
312struct vpfe_cap_buffer *to_vpfe_buffer(struct vb2_v4l2_buffer *vb)
313{
314 return container_of(vb, struct vpfe_cap_buffer, vb);
315}
316
317static inline void vpfe_pcr_enable(struct vpfe_ccdc *ccdc, int flag)
318{
319 vpfe_reg_write(ccdc, !!flag, VPFE_PCR);
320}
321
322static void vpfe_config_enable(struct vpfe_ccdc *ccdc, int flag)
323{
324 unsigned int cfg;
325
326 if (!flag) {
327 cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
328 cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
329 } else {
330 cfg = VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT;
331 }
332
333 vpfe_reg_write(ccdc, cfg, VPFE_CONFIG);
334}
335
336static void vpfe_ccdc_setwin(struct vpfe_ccdc *ccdc,
337 struct v4l2_rect *image_win,
338 enum ccdc_frmfmt frm_fmt,
339 int bpp)
340{
341 int horz_start, horz_nr_pixels;
342 int vert_start, vert_nr_lines;
343 int val, mid_img;
344
345
346
347
348
349
350 horz_start = image_win->left * bpp;
351 horz_nr_pixels = (image_win->width * bpp) - 1;
352 vpfe_reg_write(ccdc, (horz_start << VPFE_HORZ_INFO_SPH_SHIFT) |
353 horz_nr_pixels, VPFE_HORZ_INFO);
354
355 vert_start = image_win->top;
356
357 if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
358 vert_nr_lines = (image_win->height >> 1) - 1;
359 vert_start >>= 1;
360
361 vert_start += 1;
362
363 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
364 } else {
365
366 vert_start += 1;
367 vert_nr_lines = image_win->height - 1;
368
369
370
371
372 mid_img = vert_start + (image_win->height / 2);
373 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT) |
374 (mid_img & VPFE_VDINT_VDINT1_MASK);
375 }
376
377 vpfe_reg_write(ccdc, val, VPFE_VDINT);
378
379 vpfe_reg_write(ccdc, (vert_start << VPFE_VERT_START_SLV0_SHIFT) |
380 vert_start, VPFE_VERT_START);
381 vpfe_reg_write(ccdc, vert_nr_lines, VPFE_VERT_LINES);
382}
383
384static void vpfe_reg_dump(struct vpfe_ccdc *ccdc)
385{
386 struct vpfe_device *vpfe = to_vpfe(ccdc);
387
388 vpfe_dbg(3, vpfe, "ALAW: 0x%x\n", vpfe_reg_read(ccdc, VPFE_ALAW));
389 vpfe_dbg(3, vpfe, "CLAMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_CLAMP));
390 vpfe_dbg(3, vpfe, "DCSUB: 0x%x\n", vpfe_reg_read(ccdc, VPFE_DCSUB));
391 vpfe_dbg(3, vpfe, "BLKCMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_BLKCMP));
392 vpfe_dbg(3, vpfe, "COLPTN: 0x%x\n", vpfe_reg_read(ccdc, VPFE_COLPTN));
393 vpfe_dbg(3, vpfe, "SDOFST: 0x%x\n", vpfe_reg_read(ccdc, VPFE_SDOFST));
394 vpfe_dbg(3, vpfe, "SYN_MODE: 0x%x\n",
395 vpfe_reg_read(ccdc, VPFE_SYNMODE));
396 vpfe_dbg(3, vpfe, "HSIZE_OFF: 0x%x\n",
397 vpfe_reg_read(ccdc, VPFE_HSIZE_OFF));
398 vpfe_dbg(3, vpfe, "HORZ_INFO: 0x%x\n",
399 vpfe_reg_read(ccdc, VPFE_HORZ_INFO));
400 vpfe_dbg(3, vpfe, "VERT_START: 0x%x\n",
401 vpfe_reg_read(ccdc, VPFE_VERT_START));
402 vpfe_dbg(3, vpfe, "VERT_LINES: 0x%x\n",
403 vpfe_reg_read(ccdc, VPFE_VERT_LINES));
404}
405
406static int
407vpfe_ccdc_validate_param(struct vpfe_ccdc *ccdc,
408 struct vpfe_ccdc_config_params_raw *ccdcparam)
409{
410 struct vpfe_device *vpfe = to_vpfe(ccdc);
411 u8 max_gamma, max_data;
412
413 if (!ccdcparam->alaw.enable)
414 return 0;
415
416 max_gamma = ccdc_gamma_width_max_bit(ccdcparam->alaw.gamma_wd);
417 max_data = ccdc_data_size_max_bit(ccdcparam->data_sz);
418
419 if (ccdcparam->alaw.gamma_wd > VPFE_CCDC_GAMMA_BITS_09_0 ||
420 ccdcparam->alaw.gamma_wd < VPFE_CCDC_GAMMA_BITS_15_6 ||
421 max_gamma > max_data) {
422 vpfe_dbg(1, vpfe, "Invalid data line select\n");
423 return -EINVAL;
424 }
425
426 return 0;
427}
428
429static void
430vpfe_ccdc_update_raw_params(struct vpfe_ccdc *ccdc,
431 struct vpfe_ccdc_config_params_raw *raw_params)
432{
433 struct vpfe_ccdc_config_params_raw *config_params =
434 &ccdc->ccdc_cfg.bayer.config_params;
435
436 *config_params = *raw_params;
437}
438
439
440
441
442
443static void vpfe_ccdc_restore_defaults(struct vpfe_ccdc *ccdc)
444{
445 int i;
446
447
448 vpfe_pcr_enable(ccdc, 0);
449
450
451 for (i = 4; i <= 0x94; i += 4)
452 vpfe_reg_write(ccdc, 0, i);
453
454 vpfe_reg_write(ccdc, VPFE_NO_CULLING, VPFE_CULLING);
455 vpfe_reg_write(ccdc, VPFE_CCDC_GAMMA_BITS_11_2, VPFE_ALAW);
456}
457
458static int vpfe_ccdc_close(struct vpfe_ccdc *ccdc, struct device *dev)
459{
460 int dma_cntl, i, pcr;
461
462
463 for (i = 0; i < 10; i++) {
464 usleep_range(5000, 6000);
465 pcr = vpfe_reg_read(ccdc, VPFE_PCR);
466 if (!pcr)
467 break;
468
469
470 vpfe_pcr_enable(ccdc, 0);
471 }
472
473
474 vpfe_ccdc_restore_defaults(ccdc);
475
476
477
478
479 for (i = 0; i < 10; i++) {
480 dma_cntl = vpfe_reg_read(ccdc, VPFE_DMA_CNTL);
481 if (!(dma_cntl & VPFE_DMA_CNTL_OVERFLOW))
482 break;
483
484
485 vpfe_reg_write(ccdc, dma_cntl, VPFE_DMA_CNTL);
486 usleep_range(5000, 6000);
487 }
488
489
490 vpfe_config_enable(ccdc, 0);
491
492 pm_runtime_put_sync(dev);
493
494 return 0;
495}
496
497static int vpfe_ccdc_set_params(struct vpfe_ccdc *ccdc, void __user *params)
498{
499 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
500 struct vpfe_ccdc_config_params_raw raw_params;
501 int x;
502
503 if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
504 return -EINVAL;
505
506 x = copy_from_user(&raw_params, params, sizeof(raw_params));
507 if (x) {
508 vpfe_dbg(1, vpfe,
509 "vpfe_ccdc_set_params: error in copying ccdc params, %d\n",
510 x);
511 return -EFAULT;
512 }
513
514 if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
515 vpfe_ccdc_update_raw_params(ccdc, &raw_params);
516 return 0;
517 }
518
519 return -EINVAL;
520}
521
522
523
524
525
526static void vpfe_ccdc_config_ycbcr(struct vpfe_ccdc *ccdc)
527{
528 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
529 struct ccdc_params_ycbcr *params = &ccdc->ccdc_cfg.ycbcr;
530 u32 syn_mode;
531
532 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_ycbcr:\n");
533
534
535
536
537
538 vpfe_ccdc_restore_defaults(ccdc);
539
540
541
542
543
544
545 syn_mode = (((params->pix_fmt & VPFE_SYN_MODE_INPMOD_MASK) <<
546 VPFE_SYN_MODE_INPMOD_SHIFT) |
547 ((params->frm_fmt & VPFE_SYN_FLDMODE_MASK) <<
548 VPFE_SYN_FLDMODE_SHIFT) | VPFE_VDHDEN_ENABLE |
549 VPFE_WEN_ENABLE | VPFE_DATA_PACK_ENABLE);
550
551
552 if (params->bt656_enable) {
553 vpfe_reg_write(ccdc, VPFE_REC656IF_BT656_EN, VPFE_REC656IF);
554
555
556
557
558
559 syn_mode |= VPFE_SYN_MODE_VD_POL_NEGATIVE;
560 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
561 syn_mode |= VPFE_SYN_MODE_10BITS;
562 else
563 syn_mode |= VPFE_SYN_MODE_8BITS;
564 } else {
565
566 syn_mode |= (((params->fid_pol & VPFE_FID_POL_MASK) <<
567 VPFE_FID_POL_SHIFT) |
568 ((params->hd_pol & VPFE_HD_POL_MASK) <<
569 VPFE_HD_POL_SHIFT) |
570 ((params->vd_pol & VPFE_VD_POL_MASK) <<
571 VPFE_VD_POL_SHIFT));
572 }
573 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
574
575
576 vpfe_ccdc_setwin(ccdc, ¶ms->win,
577 params->frm_fmt, params->bytesperpixel);
578
579
580
581
582
583 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
584 vpfe_reg_write(ccdc,
585 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
586 VPFE_LATCH_ON_VSYNC_DISABLE |
587 VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
588 else
589 vpfe_reg_write(ccdc,
590 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
591 VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
592
593
594
595
596
597 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
598
599
600 if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
601
602 vpfe_reg_write(ccdc, VPFE_SDOFST_FIELD_INTERLEAVED,
603 VPFE_SDOFST);
604}
605
606static void
607vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
608 struct vpfe_ccdc_black_clamp *bclamp)
609{
610 u32 val;
611
612 if (!bclamp->enable) {
613
614 val = (bclamp->dc_sub) & VPFE_BLK_DC_SUB_MASK;
615 vpfe_reg_write(ccdc, val, VPFE_DCSUB);
616 vpfe_reg_write(ccdc, VPFE_CLAMP_DEFAULT_VAL, VPFE_CLAMP);
617 return;
618 }
619
620
621
622
623 val = ((bclamp->sgain & VPFE_BLK_SGAIN_MASK) |
624 ((bclamp->start_pixel & VPFE_BLK_ST_PXL_MASK) <<
625 VPFE_BLK_ST_PXL_SHIFT) |
626 ((bclamp->sample_ln & VPFE_BLK_SAMPLE_LINE_MASK) <<
627 VPFE_BLK_SAMPLE_LINE_SHIFT) |
628 ((bclamp->sample_pixel & VPFE_BLK_SAMPLE_LN_MASK) <<
629 VPFE_BLK_SAMPLE_LN_SHIFT) | VPFE_BLK_CLAMP_ENABLE);
630 vpfe_reg_write(ccdc, val, VPFE_CLAMP);
631
632 vpfe_reg_write(ccdc, VPFE_DCSUB_DEFAULT_VAL, VPFE_DCSUB);
633}
634
635static void
636vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
637 struct vpfe_ccdc_black_compensation *bcomp)
638{
639 u32 val;
640
641 val = ((bcomp->b & VPFE_BLK_COMP_MASK) |
642 ((bcomp->gb & VPFE_BLK_COMP_MASK) <<
643 VPFE_BLK_COMP_GB_COMP_SHIFT) |
644 ((bcomp->gr & VPFE_BLK_COMP_MASK) <<
645 VPFE_BLK_COMP_GR_COMP_SHIFT) |
646 ((bcomp->r & VPFE_BLK_COMP_MASK) <<
647 VPFE_BLK_COMP_R_COMP_SHIFT));
648 vpfe_reg_write(ccdc, val, VPFE_BLKCMP);
649}
650
651
652
653
654
655static void vpfe_ccdc_config_raw(struct vpfe_ccdc *ccdc)
656{
657 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
658 struct vpfe_ccdc_config_params_raw *config_params =
659 &ccdc->ccdc_cfg.bayer.config_params;
660 struct ccdc_params_raw *params = &ccdc->ccdc_cfg.bayer;
661 unsigned int syn_mode;
662 unsigned int val;
663
664 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_raw:\n");
665
666
667 vpfe_ccdc_restore_defaults(ccdc);
668
669
670 vpfe_reg_write(ccdc, VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
671
672
673
674
675
676
677
678
679 syn_mode = (((params->vd_pol & VPFE_VD_POL_MASK) << VPFE_VD_POL_SHIFT) |
680 ((params->hd_pol & VPFE_HD_POL_MASK) << VPFE_HD_POL_SHIFT) |
681 ((params->fid_pol & VPFE_FID_POL_MASK) <<
682 VPFE_FID_POL_SHIFT) | ((params->frm_fmt &
683 VPFE_FRM_FMT_MASK) << VPFE_FRM_FMT_SHIFT) |
684 ((config_params->data_sz & VPFE_DATA_SZ_MASK) <<
685 VPFE_DATA_SZ_SHIFT) | ((params->pix_fmt &
686 VPFE_PIX_FMT_MASK) << VPFE_PIX_FMT_SHIFT) |
687 VPFE_WEN_ENABLE | VPFE_VDHDEN_ENABLE);
688
689
690 if (config_params->alaw.enable) {
691 val = ((config_params->alaw.gamma_wd &
692 VPFE_ALAW_GAMMA_WD_MASK) | VPFE_ALAW_ENABLE);
693 vpfe_reg_write(ccdc, val, VPFE_ALAW);
694 vpfe_dbg(3, vpfe, "\nWriting 0x%x to ALAW...\n", val);
695 }
696
697
698 vpfe_ccdc_setwin(ccdc, ¶ms->win, params->frm_fmt,
699 params->bytesperpixel);
700
701
702 vpfe_ccdc_config_black_clamp(ccdc, &config_params->blk_clamp);
703
704
705 vpfe_ccdc_config_black_compense(ccdc, &config_params->blk_comp);
706
707
708 if ((config_params->data_sz == VPFE_CCDC_DATA_8BITS) ||
709 config_params->alaw.enable)
710 syn_mode |= VPFE_DATA_PACK_ENABLE;
711
712
713
714
715
716 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
717
718 vpfe_dbg(3, vpfe, "Writing %d (%x) to HSIZE_OFF\n",
719 params->bytesperline, params->bytesperline);
720
721
722 if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
723 if (params->image_invert_enable) {
724
725 vpfe_reg_write(ccdc, VPFE_INTERLACED_IMAGE_INVERT,
726 VPFE_SDOFST);
727 } else {
728
729 vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
730 VPFE_SDOFST);
731 }
732 } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
733 vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
734 VPFE_SDOFST);
735 }
736
737 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
738
739 vpfe_reg_dump(ccdc);
740}
741
742static inline int
743vpfe_ccdc_set_buftype(struct vpfe_ccdc *ccdc,
744 enum ccdc_buftype buf_type)
745{
746 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
747 ccdc->ccdc_cfg.bayer.buf_type = buf_type;
748 else
749 ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
750
751 return 0;
752}
753
754static inline enum ccdc_buftype vpfe_ccdc_get_buftype(struct vpfe_ccdc *ccdc)
755{
756 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
757 return ccdc->ccdc_cfg.bayer.buf_type;
758
759 return ccdc->ccdc_cfg.ycbcr.buf_type;
760}
761
762static int vpfe_ccdc_set_pixel_format(struct vpfe_ccdc *ccdc, u32 pixfmt)
763{
764 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
765
766 vpfe_dbg(1, vpfe, "vpfe_ccdc_set_pixel_format: if_type: %d, pixfmt:%s\n",
767 ccdc->ccdc_cfg.if_type, print_fourcc(pixfmt));
768
769 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
770 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
771
772
773
774
775 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 0;
776
777 switch (pixfmt) {
778 case V4L2_PIX_FMT_SBGGR8:
779 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
780 break;
781
782 case V4L2_PIX_FMT_YUYV:
783 case V4L2_PIX_FMT_UYVY:
784 case V4L2_PIX_FMT_YUV420:
785 case V4L2_PIX_FMT_NV12:
786 case V4L2_PIX_FMT_RGB565X:
787 break;
788
789 case V4L2_PIX_FMT_SBGGR16:
790 default:
791 return -EINVAL;
792 }
793 } else {
794 switch (pixfmt) {
795 case V4L2_PIX_FMT_YUYV:
796 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
797 break;
798
799 case V4L2_PIX_FMT_UYVY:
800 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
801 break;
802
803 default:
804 return -EINVAL;
805 }
806 }
807
808 return 0;
809}
810
811static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
812{
813 u32 pixfmt;
814
815 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
816 pixfmt = V4L2_PIX_FMT_YUYV;
817 } else {
818 if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
819 pixfmt = V4L2_PIX_FMT_YUYV;
820 else
821 pixfmt = V4L2_PIX_FMT_UYVY;
822 }
823
824 return pixfmt;
825}
826
827static int
828vpfe_ccdc_set_image_window(struct vpfe_ccdc *ccdc,
829 struct v4l2_rect *win, unsigned int bpp)
830{
831 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
832 ccdc->ccdc_cfg.bayer.win = *win;
833 ccdc->ccdc_cfg.bayer.bytesperpixel = bpp;
834 ccdc->ccdc_cfg.bayer.bytesperline = ALIGN(win->width * bpp, 32);
835 } else {
836 ccdc->ccdc_cfg.ycbcr.win = *win;
837 ccdc->ccdc_cfg.ycbcr.bytesperpixel = bpp;
838 ccdc->ccdc_cfg.ycbcr.bytesperline = ALIGN(win->width * bpp, 32);
839 }
840
841 return 0;
842}
843
844static inline void
845vpfe_ccdc_get_image_window(struct vpfe_ccdc *ccdc,
846 struct v4l2_rect *win)
847{
848 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
849 *win = ccdc->ccdc_cfg.bayer.win;
850 else
851 *win = ccdc->ccdc_cfg.ycbcr.win;
852}
853
854static inline unsigned int vpfe_ccdc_get_line_length(struct vpfe_ccdc *ccdc)
855{
856 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
857 return ccdc->ccdc_cfg.bayer.bytesperline;
858
859 return ccdc->ccdc_cfg.ycbcr.bytesperline;
860}
861
862static inline int
863vpfe_ccdc_set_frame_format(struct vpfe_ccdc *ccdc,
864 enum ccdc_frmfmt frm_fmt)
865{
866 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
867 ccdc->ccdc_cfg.bayer.frm_fmt = frm_fmt;
868 else
869 ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
870
871 return 0;
872}
873
874static inline enum ccdc_frmfmt
875vpfe_ccdc_get_frame_format(struct vpfe_ccdc *ccdc)
876{
877 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
878 return ccdc->ccdc_cfg.bayer.frm_fmt;
879
880 return ccdc->ccdc_cfg.ycbcr.frm_fmt;
881}
882
883static inline int vpfe_ccdc_getfid(struct vpfe_ccdc *ccdc)
884{
885 return (vpfe_reg_read(ccdc, VPFE_SYNMODE) >> 15) & 1;
886}
887
888static inline void vpfe_set_sdr_addr(struct vpfe_ccdc *ccdc, unsigned long addr)
889{
890 vpfe_reg_write(ccdc, addr & 0xffffffe0, VPFE_SDR_ADDR);
891}
892
893static int vpfe_ccdc_set_hw_if_params(struct vpfe_ccdc *ccdc,
894 struct vpfe_hw_if_param *params)
895{
896 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
897
898 ccdc->ccdc_cfg.if_type = params->if_type;
899
900 switch (params->if_type) {
901 case VPFE_BT656:
902 case VPFE_YCBCR_SYNC_16:
903 case VPFE_YCBCR_SYNC_8:
904 case VPFE_BT656_10BIT:
905 ccdc->ccdc_cfg.ycbcr.vd_pol = params->vdpol;
906 ccdc->ccdc_cfg.ycbcr.hd_pol = params->hdpol;
907 break;
908
909 case VPFE_RAW_BAYER:
910 ccdc->ccdc_cfg.bayer.vd_pol = params->vdpol;
911 ccdc->ccdc_cfg.bayer.hd_pol = params->hdpol;
912 if (params->bus_width == 10)
913 ccdc->ccdc_cfg.bayer.config_params.data_sz =
914 VPFE_CCDC_DATA_10BITS;
915 else
916 ccdc->ccdc_cfg.bayer.config_params.data_sz =
917 VPFE_CCDC_DATA_8BITS;
918 vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
919 params->bus_width);
920 vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
921 ccdc->ccdc_cfg.bayer.config_params.data_sz);
922 break;
923
924 default:
925 return -EINVAL;
926 }
927
928 return 0;
929}
930
931static void vpfe_clear_intr(struct vpfe_ccdc *ccdc, int vdint)
932{
933 unsigned int vpfe_int_status;
934
935 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
936
937 switch (vdint) {
938
939 case VPFE_VDINT0:
940 vpfe_int_status &= ~VPFE_VDINT0;
941 vpfe_int_status |= VPFE_VDINT0;
942 break;
943
944
945 case VPFE_VDINT1:
946 vpfe_int_status &= ~VPFE_VDINT1;
947 vpfe_int_status |= VPFE_VDINT1;
948 break;
949
950
951 case VPFE_VDINT2:
952 vpfe_int_status &= ~VPFE_VDINT2;
953 vpfe_int_status |= VPFE_VDINT2;
954 break;
955
956
957 default:
958 vpfe_int_status &= ~(VPFE_VDINT0 |
959 VPFE_VDINT1 |
960 VPFE_VDINT2);
961 vpfe_int_status |= (VPFE_VDINT0 |
962 VPFE_VDINT1 |
963 VPFE_VDINT2);
964 break;
965 }
966
967 vpfe_reg_write(ccdc, vpfe_int_status, VPFE_IRQ_STS);
968
969 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
970
971
972 vpfe_reg_write(ccdc, 1, VPFE_IRQ_EOI);
973}
974
975static void vpfe_ccdc_config_defaults(struct vpfe_ccdc *ccdc)
976{
977 ccdc->ccdc_cfg.if_type = VPFE_RAW_BAYER;
978
979 ccdc->ccdc_cfg.ycbcr.pix_fmt = CCDC_PIXFMT_YCBCR_8BIT;
980 ccdc->ccdc_cfg.ycbcr.frm_fmt = CCDC_FRMFMT_INTERLACED;
981 ccdc->ccdc_cfg.ycbcr.fid_pol = VPFE_PINPOL_POSITIVE;
982 ccdc->ccdc_cfg.ycbcr.vd_pol = VPFE_PINPOL_POSITIVE;
983 ccdc->ccdc_cfg.ycbcr.hd_pol = VPFE_PINPOL_POSITIVE;
984 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
985 ccdc->ccdc_cfg.ycbcr.buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED;
986
987 ccdc->ccdc_cfg.ycbcr.win.left = 0;
988 ccdc->ccdc_cfg.ycbcr.win.top = 0;
989 ccdc->ccdc_cfg.ycbcr.win.width = 720;
990 ccdc->ccdc_cfg.ycbcr.win.height = 576;
991 ccdc->ccdc_cfg.ycbcr.bt656_enable = 1;
992
993 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
994 ccdc->ccdc_cfg.bayer.frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
995 ccdc->ccdc_cfg.bayer.fid_pol = VPFE_PINPOL_POSITIVE;
996 ccdc->ccdc_cfg.bayer.vd_pol = VPFE_PINPOL_POSITIVE;
997 ccdc->ccdc_cfg.bayer.hd_pol = VPFE_PINPOL_POSITIVE;
998
999 ccdc->ccdc_cfg.bayer.win.left = 0;
1000 ccdc->ccdc_cfg.bayer.win.top = 0;
1001 ccdc->ccdc_cfg.bayer.win.width = 800;
1002 ccdc->ccdc_cfg.bayer.win.height = 600;
1003 ccdc->ccdc_cfg.bayer.config_params.data_sz = VPFE_CCDC_DATA_8BITS;
1004 ccdc->ccdc_cfg.bayer.config_params.alaw.gamma_wd =
1005 VPFE_CCDC_GAMMA_BITS_09_0;
1006}
1007
1008
1009
1010
1011static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe,
1012 struct v4l2_format *f)
1013{
1014 struct v4l2_rect image_win;
1015 enum ccdc_buftype buf_type;
1016 enum ccdc_frmfmt frm_fmt;
1017
1018 memset(f, 0, sizeof(*f));
1019 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1020 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1021 f->fmt.pix.width = image_win.width;
1022 f->fmt.pix.height = image_win.height;
1023 f->fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
1024 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
1025 f->fmt.pix.height;
1026 buf_type = vpfe_ccdc_get_buftype(&vpfe->ccdc);
1027 f->fmt.pix.pixelformat = vpfe_ccdc_get_pixel_format(&vpfe->ccdc);
1028 frm_fmt = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1029
1030 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
1031 f->fmt.pix.field = V4L2_FIELD_NONE;
1032 } else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
1033 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED) {
1034 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1035 } else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED) {
1036 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
1037 } else {
1038 vpfe_err(vpfe, "Invalid buf_type\n");
1039 return -EINVAL;
1040 }
1041 } else {
1042 vpfe_err(vpfe, "Invalid frm_fmt\n");
1043 return -EINVAL;
1044 }
1045 return 0;
1046}
1047
1048static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
1049{
1050 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
1051 int ret = 0;
1052
1053 vpfe_dbg(2, vpfe, "vpfe_config_ccdc_image_format\n");
1054
1055 vpfe_dbg(1, vpfe, "pixelformat: %s\n",
1056 print_fourcc(vpfe->fmt.fmt.pix.pixelformat));
1057
1058 if (vpfe_ccdc_set_pixel_format(&vpfe->ccdc,
1059 vpfe->fmt.fmt.pix.pixelformat) < 0) {
1060 vpfe_err(vpfe, "couldn't set pix format in ccdc\n");
1061 return -EINVAL;
1062 }
1063
1064
1065 vpfe_ccdc_set_image_window(&vpfe->ccdc, &vpfe->crop, vpfe->bpp);
1066
1067 switch (vpfe->fmt.fmt.pix.field) {
1068 case V4L2_FIELD_INTERLACED:
1069
1070 ret = vpfe_ccdc_set_buftype(
1071 &vpfe->ccdc,
1072 CCDC_BUFTYPE_FLD_INTERLEAVED);
1073 break;
1074
1075 case V4L2_FIELD_NONE:
1076 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
1077
1078 break;
1079
1080 case V4L2_FIELD_SEQ_TB:
1081 ret = vpfe_ccdc_set_buftype(
1082 &vpfe->ccdc,
1083 CCDC_BUFTYPE_FLD_SEPARATED);
1084 break;
1085
1086 default:
1087 return -EINVAL;
1088 }
1089
1090 if (ret)
1091 return ret;
1092
1093 return vpfe_ccdc_set_frame_format(&vpfe->ccdc, frm_fmt);
1094}
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106static int vpfe_config_image_format(struct vpfe_device *vpfe,
1107 v4l2_std_id std_id)
1108{
1109 struct v4l2_pix_format *pix = &vpfe->fmt.fmt.pix;
1110 int i, ret;
1111
1112 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
1113 if (vpfe_standards[i].std_id & std_id) {
1114 vpfe->std_info.active_pixels =
1115 vpfe_standards[i].width;
1116 vpfe->std_info.active_lines =
1117 vpfe_standards[i].height;
1118 vpfe->std_info.frame_format =
1119 vpfe_standards[i].frame_format;
1120 vpfe->std_index = i;
1121
1122 break;
1123 }
1124 }
1125
1126 if (i == ARRAY_SIZE(vpfe_standards)) {
1127 vpfe_err(vpfe, "standard not supported\n");
1128 return -EINVAL;
1129 }
1130
1131 vpfe->crop.top = vpfe->crop.left = 0;
1132 vpfe->crop.width = vpfe->std_info.active_pixels;
1133 vpfe->crop.height = vpfe->std_info.active_lines;
1134 pix->width = vpfe->crop.width;
1135 pix->height = vpfe->crop.height;
1136 pix->pixelformat = V4L2_PIX_FMT_YUYV;
1137
1138
1139 if (vpfe->std_info.frame_format)
1140 pix->field = V4L2_FIELD_INTERLACED;
1141 else
1142 pix->field = V4L2_FIELD_NONE;
1143
1144 ret = __vpfe_get_format(vpfe, &vpfe->fmt, &vpfe->bpp);
1145 if (ret)
1146 return ret;
1147
1148
1149 vpfe->crop.width = pix->width;
1150 vpfe->crop.height = pix->height;
1151
1152 return vpfe_config_ccdc_image_format(vpfe);
1153}
1154
1155static int vpfe_initialize_device(struct vpfe_device *vpfe)
1156{
1157 struct vpfe_subdev_info *sdinfo;
1158 int ret;
1159
1160 sdinfo = &vpfe->cfg->sub_devs[0];
1161 sdinfo->sd = vpfe->sd[0];
1162 vpfe->current_input = 0;
1163 vpfe->std_index = 0;
1164
1165 ret = vpfe_config_image_format(vpfe,
1166 vpfe_standards[vpfe->std_index].std_id);
1167 if (ret)
1168 return ret;
1169
1170 pm_runtime_get_sync(vpfe->pdev);
1171
1172 vpfe_config_enable(&vpfe->ccdc, 1);
1173
1174 vpfe_ccdc_restore_defaults(&vpfe->ccdc);
1175
1176
1177 vpfe_clear_intr(&vpfe->ccdc, -1);
1178
1179 return ret;
1180}
1181
1182
1183
1184
1185
1186
1187
1188static int vpfe_release(struct file *file)
1189{
1190 struct vpfe_device *vpfe = video_drvdata(file);
1191 bool fh_singular;
1192 int ret;
1193
1194 mutex_lock(&vpfe->lock);
1195
1196
1197 fh_singular = v4l2_fh_is_singular_file(file);
1198
1199
1200 ret = _vb2_fop_release(file, NULL);
1201
1202
1203
1204
1205
1206 if (fh_singular)
1207 vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1208
1209 mutex_unlock(&vpfe->lock);
1210
1211 return ret;
1212}
1213
1214
1215
1216
1217
1218
1219static int vpfe_open(struct file *file)
1220{
1221 struct vpfe_device *vpfe = video_drvdata(file);
1222 int ret;
1223
1224 mutex_lock(&vpfe->lock);
1225
1226 ret = v4l2_fh_open(file);
1227 if (ret) {
1228 vpfe_err(vpfe, "v4l2_fh_open failed\n");
1229 goto unlock;
1230 }
1231
1232 if (!v4l2_fh_is_singular_file(file))
1233 goto unlock;
1234
1235 if (vpfe_initialize_device(vpfe)) {
1236 v4l2_fh_release(file);
1237 ret = -ENODEV;
1238 }
1239
1240unlock:
1241 mutex_unlock(&vpfe->lock);
1242 return ret;
1243}
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255static inline void vpfe_schedule_next_buffer(struct vpfe_device *vpfe)
1256{
1257 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1258 struct vpfe_cap_buffer, list);
1259 list_del(&vpfe->next_frm->list);
1260
1261 vpfe_set_sdr_addr(&vpfe->ccdc,
1262 vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0));
1263}
1264
1265static inline void vpfe_schedule_bottom_field(struct vpfe_device *vpfe)
1266{
1267 unsigned long addr;
1268
1269 addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1270 vpfe->field_off;
1271
1272 vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283static inline void vpfe_process_buffer_complete(struct vpfe_device *vpfe)
1284{
1285 vpfe->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
1286 vpfe->cur_frm->vb.field = vpfe->fmt.fmt.pix.field;
1287 vpfe->cur_frm->vb.sequence = vpfe->sequence++;
1288 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
1289 vpfe->cur_frm = vpfe->next_frm;
1290}
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300static irqreturn_t vpfe_isr(int irq, void *dev)
1301{
1302 struct vpfe_device *vpfe = (struct vpfe_device *)dev;
1303 enum v4l2_field field;
1304 int intr_status;
1305 int fid;
1306
1307 intr_status = vpfe_reg_read(&vpfe->ccdc, VPFE_IRQ_STS);
1308
1309 if (intr_status & VPFE_VDINT0) {
1310 field = vpfe->fmt.fmt.pix.field;
1311
1312 if (field == V4L2_FIELD_NONE) {
1313
1314 if (vpfe->cur_frm != vpfe->next_frm)
1315 vpfe_process_buffer_complete(vpfe);
1316 goto next_intr;
1317 }
1318
1319
1320
1321 fid = vpfe_ccdc_getfid(&vpfe->ccdc);
1322
1323
1324 vpfe->field ^= 1;
1325 if (fid == vpfe->field) {
1326
1327 if (fid == 0) {
1328
1329
1330
1331
1332
1333 if (vpfe->cur_frm != vpfe->next_frm)
1334 vpfe_process_buffer_complete(vpfe);
1335
1336
1337
1338
1339
1340 if (field == V4L2_FIELD_SEQ_TB)
1341 vpfe_schedule_bottom_field(vpfe);
1342
1343 goto next_intr;
1344 }
1345
1346
1347
1348
1349
1350
1351 spin_lock(&vpfe->dma_queue_lock);
1352 if (!list_empty(&vpfe->dma_queue) &&
1353 vpfe->cur_frm == vpfe->next_frm)
1354 vpfe_schedule_next_buffer(vpfe);
1355 spin_unlock(&vpfe->dma_queue_lock);
1356 } else if (fid == 0) {
1357
1358
1359
1360
1361 vpfe->field = fid;
1362 }
1363 }
1364
1365next_intr:
1366 if (intr_status & VPFE_VDINT1) {
1367 spin_lock(&vpfe->dma_queue_lock);
1368 if (vpfe->fmt.fmt.pix.field == V4L2_FIELD_NONE &&
1369 !list_empty(&vpfe->dma_queue) &&
1370 vpfe->cur_frm == vpfe->next_frm)
1371 vpfe_schedule_next_buffer(vpfe);
1372 spin_unlock(&vpfe->dma_queue_lock);
1373 }
1374
1375 vpfe_clear_intr(&vpfe->ccdc, intr_status);
1376
1377 return IRQ_HANDLED;
1378}
1379
1380static inline void vpfe_detach_irq(struct vpfe_device *vpfe)
1381{
1382 unsigned int intr = VPFE_VDINT0;
1383 enum ccdc_frmfmt frame_format;
1384
1385 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1386 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1387 intr |= VPFE_VDINT1;
1388
1389 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_CLR);
1390}
1391
1392static inline void vpfe_attach_irq(struct vpfe_device *vpfe)
1393{
1394 unsigned int intr = VPFE_VDINT0;
1395 enum ccdc_frmfmt frame_format;
1396
1397 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1398 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1399 intr |= VPFE_VDINT1;
1400
1401 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_SET);
1402}
1403
1404static int vpfe_querycap(struct file *file, void *priv,
1405 struct v4l2_capability *cap)
1406{
1407 struct vpfe_device *vpfe = video_drvdata(file);
1408
1409 vpfe_dbg(2, vpfe, "vpfe_querycap\n");
1410
1411 strscpy(cap->driver, VPFE_MODULE_NAME, sizeof(cap->driver));
1412 strscpy(cap->card, "TI AM437x VPFE", sizeof(cap->card));
1413 snprintf(cap->bus_info, sizeof(cap->bus_info),
1414 "platform:%s", vpfe->v4l2_dev.name);
1415 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1416 V4L2_CAP_READWRITE;
1417 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1418
1419 return 0;
1420}
1421
1422
1423static int __vpfe_get_format(struct vpfe_device *vpfe,
1424 struct v4l2_format *format, unsigned int *bpp)
1425{
1426 struct v4l2_mbus_framefmt mbus_fmt;
1427 struct vpfe_subdev_info *sdinfo;
1428 struct v4l2_subdev_format fmt;
1429 int ret;
1430
1431 sdinfo = vpfe->current_subdev;
1432 if (!sdinfo->sd)
1433 return -EINVAL;
1434
1435 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1436 fmt.pad = 0;
1437
1438 ret = v4l2_subdev_call(sdinfo->sd, pad, get_fmt, NULL, &fmt);
1439 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1440 return ret;
1441
1442 if (!ret) {
1443 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1444 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1445 } else {
1446 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev,
1447 sdinfo->grp_id,
1448 pad, get_fmt,
1449 NULL, &fmt);
1450 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1451 return ret;
1452 v4l2_fill_pix_format(&format->fmt.pix, &mbus_fmt);
1453 mbus_to_pix(vpfe, &mbus_fmt, &format->fmt.pix, bpp);
1454 }
1455
1456 format->type = vpfe->fmt.type;
1457
1458 vpfe_dbg(1, vpfe,
1459 "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1460 __func__, format->fmt.pix.width, format->fmt.pix.height,
1461 print_fourcc(format->fmt.pix.pixelformat),
1462 format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1463
1464 return 0;
1465}
1466
1467
1468static int __vpfe_set_format(struct vpfe_device *vpfe,
1469 struct v4l2_format *format, unsigned int *bpp)
1470{
1471 struct vpfe_subdev_info *sdinfo;
1472 struct v4l2_subdev_format fmt;
1473 int ret;
1474
1475 vpfe_dbg(2, vpfe, "__vpfe_set_format\n");
1476
1477 sdinfo = vpfe->current_subdev;
1478 if (!sdinfo->sd)
1479 return -EINVAL;
1480
1481 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1482 fmt.pad = 0;
1483
1484 pix_to_mbus(vpfe, &format->fmt.pix, &fmt.format);
1485
1486 ret = v4l2_subdev_call(sdinfo->sd, pad, set_fmt, NULL, &fmt);
1487 if (ret)
1488 return ret;
1489
1490 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1491 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1492
1493 format->type = vpfe->fmt.type;
1494
1495 vpfe_dbg(1, vpfe,
1496 "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1497 __func__, format->fmt.pix.width, format->fmt.pix.height,
1498 print_fourcc(format->fmt.pix.pixelformat),
1499 format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1500
1501 return 0;
1502}
1503
1504static int vpfe_g_fmt(struct file *file, void *priv,
1505 struct v4l2_format *fmt)
1506{
1507 struct vpfe_device *vpfe = video_drvdata(file);
1508
1509 vpfe_dbg(2, vpfe, "vpfe_g_fmt\n");
1510
1511 *fmt = vpfe->fmt;
1512
1513 return 0;
1514}
1515
1516static int vpfe_enum_fmt(struct file *file, void *priv,
1517 struct v4l2_fmtdesc *f)
1518{
1519 struct vpfe_device *vpfe = video_drvdata(file);
1520 struct vpfe_subdev_info *sdinfo;
1521 struct vpfe_fmt *fmt = NULL;
1522 unsigned int k;
1523
1524 vpfe_dbg(2, vpfe, "vpfe_enum_format index:%d\n",
1525 f->index);
1526
1527 sdinfo = vpfe->current_subdev;
1528 if (!sdinfo->sd)
1529 return -EINVAL;
1530
1531 if (f->index > ARRAY_SIZE(formats))
1532 return -EINVAL;
1533
1534 for (k = 0; k < ARRAY_SIZE(formats); k++) {
1535 if (formats[k].index == f->index) {
1536 fmt = &formats[k];
1537 break;
1538 }
1539 }
1540 if (!fmt)
1541 return -EINVAL;
1542
1543 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1544 f->pixelformat = fmt->fourcc;
1545 f->type = vpfe->fmt.type;
1546
1547 vpfe_dbg(1, vpfe, "vpfe_enum_format: mbus index: %d code: %x pixelformat: %s [%s]\n",
1548 f->index, fmt->code, print_fourcc(fmt->fourcc), fmt->name);
1549
1550 return 0;
1551}
1552
1553static int vpfe_try_fmt(struct file *file, void *priv,
1554 struct v4l2_format *fmt)
1555{
1556 struct vpfe_device *vpfe = video_drvdata(file);
1557 unsigned int bpp;
1558
1559 vpfe_dbg(2, vpfe, "vpfe_try_fmt\n");
1560
1561 return __vpfe_get_format(vpfe, fmt, &bpp);
1562}
1563
1564static int vpfe_s_fmt(struct file *file, void *priv,
1565 struct v4l2_format *fmt)
1566{
1567 struct vpfe_device *vpfe = video_drvdata(file);
1568 struct v4l2_format format;
1569 unsigned int bpp;
1570 int ret;
1571
1572 vpfe_dbg(2, vpfe, "vpfe_s_fmt\n");
1573
1574
1575 if (vb2_is_busy(&vpfe->buffer_queue)) {
1576 vpfe_err(vpfe, "%s device busy\n", __func__);
1577 return -EBUSY;
1578 }
1579
1580 ret = __vpfe_get_format(vpfe, &format, &bpp);
1581 if (ret)
1582 return ret;
1583
1584
1585 if (!cmp_v4l2_format(fmt, &format)) {
1586
1587
1588
1589 ret = __vpfe_set_format(vpfe, fmt, &bpp);
1590 if (ret)
1591 return ret;
1592 } else
1593 *fmt = format;
1594
1595
1596 vpfe_detach_irq(vpfe);
1597 vpfe->fmt = *fmt;
1598 vpfe->bpp = bpp;
1599
1600
1601 vpfe->crop.width = fmt->fmt.pix.width;
1602 vpfe->crop.height = fmt->fmt.pix.height;
1603
1604
1605 return vpfe_config_ccdc_image_format(vpfe);
1606}
1607
1608static int vpfe_enum_size(struct file *file, void *priv,
1609 struct v4l2_frmsizeenum *fsize)
1610{
1611 struct vpfe_device *vpfe = video_drvdata(file);
1612 struct v4l2_subdev_frame_size_enum fse;
1613 struct vpfe_subdev_info *sdinfo;
1614 struct v4l2_mbus_framefmt mbus;
1615 struct v4l2_pix_format pix;
1616 struct vpfe_fmt *fmt;
1617 int ret;
1618
1619 vpfe_dbg(2, vpfe, "vpfe_enum_size\n");
1620
1621
1622 fmt = find_format_by_pix(fsize->pixel_format);
1623 if (!fmt) {
1624 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1625 fsize->pixel_format);
1626 return -EINVAL;
1627 }
1628
1629 memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1630
1631 sdinfo = vpfe->current_subdev;
1632 if (!sdinfo->sd)
1633 return -EINVAL;
1634
1635 memset(&pix, 0x0, sizeof(pix));
1636
1637 pix.pixelformat = fsize->pixel_format;
1638 pix.width = 640;
1639 pix.height = 480;
1640 pix.colorspace = V4L2_COLORSPACE_SRGB;
1641 pix.field = V4L2_FIELD_NONE;
1642 pix_to_mbus(vpfe, &pix, &mbus);
1643
1644 memset(&fse, 0x0, sizeof(fse));
1645 fse.index = fsize->index;
1646 fse.pad = 0;
1647 fse.code = mbus.code;
1648 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1649 ret = v4l2_subdev_call(sdinfo->sd, pad, enum_frame_size, NULL, &fse);
1650 if (ret)
1651 return -EINVAL;
1652
1653 vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1654 fse.index, fse.code, fse.min_width, fse.max_width,
1655 fse.min_height, fse.max_height);
1656
1657 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1658 fsize->discrete.width = fse.max_width;
1659 fsize->discrete.height = fse.max_height;
1660
1661 vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d pixformat: %s size: %dx%d\n",
1662 fsize->index, print_fourcc(fsize->pixel_format),
1663 fsize->discrete.width, fsize->discrete.height);
1664
1665 return 0;
1666}
1667
1668
1669
1670
1671
1672static int
1673vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1674 int *subdev_index,
1675 int *subdev_input_index,
1676 int app_input_index)
1677{
1678 int i, j = 0;
1679
1680 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1681 if (app_input_index < (j + 1)) {
1682 *subdev_index = i;
1683 *subdev_input_index = app_input_index - j;
1684 return 0;
1685 }
1686 j++;
1687 }
1688 return -EINVAL;
1689}
1690
1691
1692
1693
1694
1695
1696static int vpfe_get_app_input_index(struct vpfe_device *vpfe,
1697 int *app_input_index)
1698{
1699 struct vpfe_config *cfg = vpfe->cfg;
1700 struct vpfe_subdev_info *sdinfo;
1701 struct i2c_client *client;
1702 struct i2c_client *curr_client;
1703 int i, j = 0;
1704
1705 curr_client = v4l2_get_subdevdata(vpfe->current_subdev->sd);
1706 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1707 sdinfo = &cfg->sub_devs[i];
1708 client = v4l2_get_subdevdata(sdinfo->sd);
1709 if (client->addr == curr_client->addr &&
1710 client->adapter->nr == curr_client->adapter->nr) {
1711 if (vpfe->current_input >= 1)
1712 return -1;
1713 *app_input_index = j + vpfe->current_input;
1714 return 0;
1715 }
1716 j++;
1717 }
1718 return -EINVAL;
1719}
1720
1721static int vpfe_enum_input(struct file *file, void *priv,
1722 struct v4l2_input *inp)
1723{
1724 struct vpfe_device *vpfe = video_drvdata(file);
1725 struct vpfe_subdev_info *sdinfo;
1726 int subdev, index;
1727
1728 vpfe_dbg(2, vpfe, "vpfe_enum_input\n");
1729
1730 if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1731 inp->index) < 0) {
1732 vpfe_dbg(1, vpfe,
1733 "input information not found for the subdev\n");
1734 return -EINVAL;
1735 }
1736 sdinfo = &vpfe->cfg->sub_devs[subdev];
1737 *inp = sdinfo->inputs[index];
1738
1739 return 0;
1740}
1741
1742static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1743{
1744 struct vpfe_device *vpfe = video_drvdata(file);
1745
1746 vpfe_dbg(2, vpfe, "vpfe_g_input\n");
1747
1748 return vpfe_get_app_input_index(vpfe, index);
1749}
1750
1751
1752static int vpfe_set_input(struct vpfe_device *vpfe, unsigned int index)
1753{
1754 int subdev_index = 0, inp_index = 0;
1755 struct vpfe_subdev_info *sdinfo;
1756 struct vpfe_route *route;
1757 u32 input, output;
1758 int ret;
1759
1760 vpfe_dbg(2, vpfe, "vpfe_set_input: index: %d\n", index);
1761
1762
1763 if (vb2_is_busy(&vpfe->buffer_queue)) {
1764 vpfe_err(vpfe, "%s device busy\n", __func__);
1765 return -EBUSY;
1766 }
1767 ret = vpfe_get_subdev_input_index(vpfe,
1768 &subdev_index,
1769 &inp_index,
1770 index);
1771 if (ret < 0) {
1772 vpfe_err(vpfe, "invalid input index: %d\n", index);
1773 goto get_out;
1774 }
1775
1776 sdinfo = &vpfe->cfg->sub_devs[subdev_index];
1777 sdinfo->sd = vpfe->sd[subdev_index];
1778 route = &sdinfo->routes[inp_index];
1779 if (route && sdinfo->can_route) {
1780 input = route->input;
1781 output = route->output;
1782 if (sdinfo->sd) {
1783 ret = v4l2_subdev_call(sdinfo->sd, video,
1784 s_routing, input, output, 0);
1785 if (ret) {
1786 vpfe_err(vpfe, "s_routing failed\n");
1787 ret = -EINVAL;
1788 goto get_out;
1789 }
1790 }
1791
1792 }
1793
1794 vpfe->current_subdev = sdinfo;
1795 if (sdinfo->sd)
1796 vpfe->v4l2_dev.ctrl_handler = sdinfo->sd->ctrl_handler;
1797 vpfe->current_input = index;
1798 vpfe->std_index = 0;
1799
1800
1801 ret = vpfe_ccdc_set_hw_if_params(&vpfe->ccdc, &sdinfo->vpfe_param);
1802 if (ret)
1803 return ret;
1804
1805
1806 return vpfe_config_image_format(vpfe,
1807 vpfe_standards[vpfe->std_index].std_id);
1808
1809get_out:
1810 return ret;
1811}
1812
1813static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1814{
1815 struct vpfe_device *vpfe = video_drvdata(file);
1816
1817 vpfe_dbg(2, vpfe,
1818 "vpfe_s_input: index: %d\n", index);
1819
1820 return vpfe_set_input(vpfe, index);
1821}
1822
1823static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1824{
1825 struct vpfe_device *vpfe = video_drvdata(file);
1826 struct vpfe_subdev_info *sdinfo;
1827
1828 vpfe_dbg(2, vpfe, "vpfe_querystd\n");
1829
1830 sdinfo = vpfe->current_subdev;
1831 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1832 return -ENODATA;
1833
1834
1835 return v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1836 video, querystd, std_id);
1837}
1838
1839static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1840{
1841 struct vpfe_device *vpfe = video_drvdata(file);
1842 struct vpfe_subdev_info *sdinfo;
1843 int ret;
1844
1845 vpfe_dbg(2, vpfe, "vpfe_s_std\n");
1846
1847 sdinfo = vpfe->current_subdev;
1848 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1849 return -ENODATA;
1850
1851
1852 if (vb2_is_busy(&vpfe->buffer_queue)) {
1853 vpfe_err(vpfe, "%s device busy\n", __func__);
1854 ret = -EBUSY;
1855 return ret;
1856 }
1857
1858 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1859 video, s_std, std_id);
1860 if (ret < 0) {
1861 vpfe_err(vpfe, "Failed to set standard\n");
1862 return ret;
1863 }
1864 ret = vpfe_config_image_format(vpfe, std_id);
1865
1866 return ret;
1867}
1868
1869static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1870{
1871 struct vpfe_device *vpfe = video_drvdata(file);
1872 struct vpfe_subdev_info *sdinfo;
1873
1874 vpfe_dbg(2, vpfe, "vpfe_g_std\n");
1875
1876 sdinfo = vpfe->current_subdev;
1877 if (sdinfo->inputs[0].capabilities != V4L2_IN_CAP_STD)
1878 return -ENODATA;
1879
1880 *std_id = vpfe_standards[vpfe->std_index].std_id;
1881
1882 return 0;
1883}
1884
1885
1886
1887
1888
1889static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1890{
1891 struct v4l2_rect image_win;
1892
1893 vpfe_dbg(2, vpfe, "vpfe_calculate_offsets\n");
1894
1895 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1896 vpfe->field_off = image_win.height * image_win.width;
1897}
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910static int vpfe_queue_setup(struct vb2_queue *vq,
1911 unsigned int *nbuffers, unsigned int *nplanes,
1912 unsigned int sizes[], struct device *alloc_devs[])
1913{
1914 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1915 unsigned size = vpfe->fmt.fmt.pix.sizeimage;
1916
1917 if (vq->num_buffers + *nbuffers < 3)
1918 *nbuffers = 3 - vq->num_buffers;
1919
1920 if (*nplanes) {
1921 if (sizes[0] < size)
1922 return -EINVAL;
1923 size = sizes[0];
1924 }
1925
1926 *nplanes = 1;
1927 sizes[0] = size;
1928
1929 vpfe_dbg(1, vpfe,
1930 "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1931
1932
1933 vpfe_calculate_offsets(vpfe);
1934
1935 return 0;
1936}
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1947{
1948 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1949 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1950
1951 vb2_set_plane_payload(vb, 0, vpfe->fmt.fmt.pix.sizeimage);
1952
1953 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1954 return -EINVAL;
1955
1956 vbuf->field = vpfe->fmt.fmt.pix.field;
1957
1958 return 0;
1959}
1960
1961
1962
1963
1964
1965static void vpfe_buffer_queue(struct vb2_buffer *vb)
1966{
1967 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1968 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1969 struct vpfe_cap_buffer *buf = to_vpfe_buffer(vbuf);
1970 unsigned long flags = 0;
1971
1972
1973 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1974 list_add_tail(&buf->list, &vpfe->dma_queue);
1975 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1976}
1977
1978
1979
1980
1981
1982
1983static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1984{
1985 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1986 struct vpfe_cap_buffer *buf, *tmp;
1987 struct vpfe_subdev_info *sdinfo;
1988 unsigned long flags;
1989 unsigned long addr;
1990 int ret;
1991
1992 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1993
1994 vpfe->field = 0;
1995 vpfe->sequence = 0;
1996
1997 sdinfo = vpfe->current_subdev;
1998
1999 vpfe_attach_irq(vpfe);
2000
2001 if (vpfe->ccdc.ccdc_cfg.if_type == VPFE_RAW_BAYER)
2002 vpfe_ccdc_config_raw(&vpfe->ccdc);
2003 else
2004 vpfe_ccdc_config_ycbcr(&vpfe->ccdc);
2005
2006
2007 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2008 struct vpfe_cap_buffer, list);
2009 vpfe->cur_frm = vpfe->next_frm;
2010
2011 list_del(&vpfe->cur_frm->list);
2012 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2013
2014 addr = vb2_dma_contig_plane_dma_addr(&vpfe->cur_frm->vb.vb2_buf, 0);
2015
2016 vpfe_set_sdr_addr(&vpfe->ccdc, (unsigned long)(addr));
2017
2018 vpfe_pcr_enable(&vpfe->ccdc, 1);
2019
2020 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 1);
2021 if (ret < 0) {
2022 vpfe_err(vpfe, "Error in attaching interrupt handle\n");
2023 goto err;
2024 }
2025
2026 return 0;
2027
2028err:
2029 list_for_each_entry_safe(buf, tmp, &vpfe->dma_queue, list) {
2030 list_del(&buf->list);
2031 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
2032 }
2033
2034 return ret;
2035}
2036
2037
2038
2039
2040
2041
2042
2043
2044static void vpfe_stop_streaming(struct vb2_queue *vq)
2045{
2046 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
2047 struct vpfe_subdev_info *sdinfo;
2048 unsigned long flags;
2049 int ret;
2050
2051 vpfe_pcr_enable(&vpfe->ccdc, 0);
2052
2053 vpfe_detach_irq(vpfe);
2054
2055 sdinfo = vpfe->current_subdev;
2056 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 0);
2057 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
2058 vpfe_dbg(1, vpfe, "stream off failed in subdev\n");
2059
2060
2061 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
2062 if (vpfe->cur_frm == vpfe->next_frm) {
2063 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2064 VB2_BUF_STATE_ERROR);
2065 } else {
2066 if (vpfe->cur_frm != NULL)
2067 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2068 VB2_BUF_STATE_ERROR);
2069 if (vpfe->next_frm != NULL)
2070 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2071 VB2_BUF_STATE_ERROR);
2072 }
2073
2074 while (!list_empty(&vpfe->dma_queue)) {
2075 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2076 struct vpfe_cap_buffer, list);
2077 list_del(&vpfe->next_frm->list);
2078 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2079 VB2_BUF_STATE_ERROR);
2080 }
2081 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2082}
2083
2084static int vpfe_cropcap(struct file *file, void *priv,
2085 struct v4l2_cropcap *crop)
2086{
2087 struct vpfe_device *vpfe = video_drvdata(file);
2088
2089 vpfe_dbg(2, vpfe, "vpfe_cropcap\n");
2090
2091 if (vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2092 return -EINVAL;
2093
2094 crop->pixelaspect = vpfe_standards[vpfe->std_index].pixelaspect;
2095
2096 return 0;
2097}
2098
2099static int
2100vpfe_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2101{
2102 struct vpfe_device *vpfe = video_drvdata(file);
2103
2104 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2105 vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2106 return -EINVAL;
2107
2108 switch (s->target) {
2109 case V4L2_SEL_TGT_CROP_BOUNDS:
2110 case V4L2_SEL_TGT_CROP_DEFAULT:
2111 s->r.left = 0;
2112 s->r.top = 0;
2113 s->r.width = vpfe_standards[vpfe->std_index].width;
2114 s->r.height = vpfe_standards[vpfe->std_index].height;
2115 break;
2116
2117 case V4L2_SEL_TGT_CROP:
2118 s->r = vpfe->crop;
2119 break;
2120
2121 default:
2122 return -EINVAL;
2123 }
2124
2125 return 0;
2126}
2127
2128static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2129{
2130 if (a->left < b->left || a->top < b->top)
2131 return 0;
2132
2133 if (a->left + a->width > b->left + b->width)
2134 return 0;
2135
2136 if (a->top + a->height > b->top + b->height)
2137 return 0;
2138
2139 return 1;
2140}
2141
2142static int
2143vpfe_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2144{
2145 struct vpfe_device *vpfe = video_drvdata(file);
2146 struct v4l2_rect cr = vpfe->crop;
2147 struct v4l2_rect r = s->r;
2148
2149
2150 if (vb2_is_busy(&vpfe->buffer_queue)) {
2151 vpfe_err(vpfe, "%s device busy\n", __func__);
2152 return -EBUSY;
2153 }
2154
2155 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2156 s->target != V4L2_SEL_TGT_CROP)
2157 return -EINVAL;
2158
2159 v4l_bound_align_image(&r.width, 0, cr.width, 0,
2160 &r.height, 0, cr.height, 0, 0);
2161
2162 r.left = clamp_t(unsigned int, r.left, 0, cr.width - r.width);
2163 r.top = clamp_t(unsigned int, r.top, 0, cr.height - r.height);
2164
2165 if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2166 return -ERANGE;
2167
2168 if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2169 return -ERANGE;
2170
2171 s->r = vpfe->crop = r;
2172
2173 vpfe_ccdc_set_image_window(&vpfe->ccdc, &r, vpfe->bpp);
2174 vpfe->fmt.fmt.pix.width = r.width;
2175 vpfe->fmt.fmt.pix.height = r.height;
2176 vpfe->fmt.fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
2177 vpfe->fmt.fmt.pix.sizeimage = vpfe->fmt.fmt.pix.bytesperline *
2178 vpfe->fmt.fmt.pix.height;
2179
2180 vpfe_dbg(1, vpfe, "cropped (%d,%d)/%dx%d of %dx%d\n",
2181 r.left, r.top, r.width, r.height, cr.width, cr.height);
2182
2183 return 0;
2184}
2185
2186static long vpfe_ioctl_default(struct file *file, void *priv,
2187 bool valid_prio, unsigned int cmd, void *param)
2188{
2189 struct vpfe_device *vpfe = video_drvdata(file);
2190 int ret;
2191
2192 vpfe_dbg(2, vpfe, "vpfe_ioctl_default\n");
2193
2194 if (!valid_prio) {
2195 vpfe_err(vpfe, "%s device busy\n", __func__);
2196 return -EBUSY;
2197 }
2198
2199
2200 if (vb2_is_busy(&vpfe->buffer_queue)) {
2201 vpfe_err(vpfe, "%s device busy\n", __func__);
2202 return -EBUSY;
2203 }
2204
2205 switch (cmd) {
2206 case VIDIOC_AM437X_CCDC_CFG:
2207 ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2208 if (ret) {
2209 vpfe_dbg(2, vpfe,
2210 "Error setting parameters in CCDC\n");
2211 return ret;
2212 }
2213 ret = vpfe_get_ccdc_image_format(vpfe,
2214 &vpfe->fmt);
2215 if (ret < 0) {
2216 vpfe_dbg(2, vpfe,
2217 "Invalid image format at CCDC\n");
2218 return ret;
2219 }
2220 break;
2221
2222 default:
2223 ret = -ENOTTY;
2224 break;
2225 }
2226
2227 return ret;
2228}
2229
2230static const struct vb2_ops vpfe_video_qops = {
2231 .wait_prepare = vb2_ops_wait_prepare,
2232 .wait_finish = vb2_ops_wait_finish,
2233 .queue_setup = vpfe_queue_setup,
2234 .buf_prepare = vpfe_buffer_prepare,
2235 .buf_queue = vpfe_buffer_queue,
2236 .start_streaming = vpfe_start_streaming,
2237 .stop_streaming = vpfe_stop_streaming,
2238};
2239
2240
2241static const struct v4l2_file_operations vpfe_fops = {
2242 .owner = THIS_MODULE,
2243 .open = vpfe_open,
2244 .release = vpfe_release,
2245 .read = vb2_fop_read,
2246 .poll = vb2_fop_poll,
2247 .unlocked_ioctl = video_ioctl2,
2248 .mmap = vb2_fop_mmap,
2249};
2250
2251
2252static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
2253 .vidioc_querycap = vpfe_querycap,
2254 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
2255 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
2256 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
2257 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
2258
2259 .vidioc_enum_framesizes = vpfe_enum_size,
2260
2261 .vidioc_enum_input = vpfe_enum_input,
2262 .vidioc_g_input = vpfe_g_input,
2263 .vidioc_s_input = vpfe_s_input,
2264
2265 .vidioc_querystd = vpfe_querystd,
2266 .vidioc_s_std = vpfe_s_std,
2267 .vidioc_g_std = vpfe_g_std,
2268
2269 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2270 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2271 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2272 .vidioc_querybuf = vb2_ioctl_querybuf,
2273 .vidioc_qbuf = vb2_ioctl_qbuf,
2274 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2275 .vidioc_expbuf = vb2_ioctl_expbuf,
2276 .vidioc_streamon = vb2_ioctl_streamon,
2277 .vidioc_streamoff = vb2_ioctl_streamoff,
2278
2279 .vidioc_log_status = v4l2_ctrl_log_status,
2280 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2281 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2282
2283 .vidioc_cropcap = vpfe_cropcap,
2284 .vidioc_g_selection = vpfe_g_selection,
2285 .vidioc_s_selection = vpfe_s_selection,
2286
2287 .vidioc_default = vpfe_ioctl_default,
2288};
2289
2290static int
2291vpfe_async_bound(struct v4l2_async_notifier *notifier,
2292 struct v4l2_subdev *subdev,
2293 struct v4l2_async_subdev *asd)
2294{
2295 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2296 struct vpfe_device, v4l2_dev);
2297 struct v4l2_subdev_mbus_code_enum mbus_code;
2298 struct vpfe_subdev_info *sdinfo;
2299 bool found = false;
2300 int i, j;
2301
2302 vpfe_dbg(1, vpfe, "vpfe_async_bound\n");
2303
2304 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
2305 if (vpfe->cfg->asd[i]->match.fwnode ==
2306 asd[i].match.fwnode) {
2307 sdinfo = &vpfe->cfg->sub_devs[i];
2308 vpfe->sd[i] = subdev;
2309 vpfe->sd[i]->grp_id = sdinfo->grp_id;
2310 found = true;
2311 break;
2312 }
2313 }
2314
2315 if (!found) {
2316 vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2317 return -EINVAL;
2318 }
2319
2320 vpfe->video_dev.tvnorms |= sdinfo->inputs[0].std;
2321
2322
2323 for (j = 0, i = 0; ; ++j) {
2324 struct vpfe_fmt *fmt;
2325 int ret;
2326
2327 memset(&mbus_code, 0, sizeof(mbus_code));
2328 mbus_code.index = j;
2329 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2330 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
2331 NULL, &mbus_code);
2332 if (ret)
2333 break;
2334
2335 fmt = find_format_by_code(mbus_code.code);
2336 if (!fmt)
2337 continue;
2338
2339 fmt->supported = true;
2340 fmt->index = i++;
2341 }
2342
2343 return 0;
2344}
2345
2346static int vpfe_probe_complete(struct vpfe_device *vpfe)
2347{
2348 struct video_device *vdev;
2349 struct vb2_queue *q;
2350 int err;
2351
2352 spin_lock_init(&vpfe->dma_queue_lock);
2353 mutex_init(&vpfe->lock);
2354
2355 vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2356
2357
2358 vpfe->current_subdev = &vpfe->cfg->sub_devs[0];
2359 vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
2360
2361 err = vpfe_set_input(vpfe, 0);
2362 if (err)
2363 goto probe_out;
2364
2365
2366 q = &vpfe->buffer_queue;
2367 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2368 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2369 q->drv_priv = vpfe;
2370 q->ops = &vpfe_video_qops;
2371 q->mem_ops = &vb2_dma_contig_memops;
2372 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
2373 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2374 q->lock = &vpfe->lock;
2375 q->min_buffers_needed = 1;
2376 q->dev = vpfe->pdev;
2377
2378 err = vb2_queue_init(q);
2379 if (err) {
2380 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2381 goto probe_out;
2382 }
2383
2384 INIT_LIST_HEAD(&vpfe->dma_queue);
2385
2386 vdev = &vpfe->video_dev;
2387 strscpy(vdev->name, VPFE_MODULE_NAME, sizeof(vdev->name));
2388 vdev->release = video_device_release_empty;
2389 vdev->fops = &vpfe_fops;
2390 vdev->ioctl_ops = &vpfe_ioctl_ops;
2391 vdev->v4l2_dev = &vpfe->v4l2_dev;
2392 vdev->vfl_dir = VFL_DIR_RX;
2393 vdev->queue = q;
2394 vdev->lock = &vpfe->lock;
2395 video_set_drvdata(vdev, vpfe);
2396 err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2397 if (err) {
2398 vpfe_err(vpfe,
2399 "Unable to register video device.\n");
2400 goto probe_out;
2401 }
2402
2403 return 0;
2404
2405probe_out:
2406 v4l2_device_unregister(&vpfe->v4l2_dev);
2407 return err;
2408}
2409
2410static int vpfe_async_complete(struct v4l2_async_notifier *notifier)
2411{
2412 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2413 struct vpfe_device, v4l2_dev);
2414
2415 return vpfe_probe_complete(vpfe);
2416}
2417
2418static const struct v4l2_async_notifier_operations vpfe_async_ops = {
2419 .bound = vpfe_async_bound,
2420 .complete = vpfe_async_complete,
2421};
2422
2423static struct vpfe_config *
2424vpfe_get_pdata(struct platform_device *pdev)
2425{
2426 struct device_node *endpoint = NULL;
2427 struct v4l2_fwnode_endpoint bus_cfg;
2428 struct vpfe_subdev_info *sdinfo;
2429 struct vpfe_config *pdata;
2430 unsigned int flags;
2431 unsigned int i;
2432 int err;
2433
2434 dev_dbg(&pdev->dev, "vpfe_get_pdata\n");
2435
2436 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
2437 return pdev->dev.platform_data;
2438
2439 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2440 if (!pdata)
2441 return NULL;
2442
2443 for (i = 0; ; i++) {
2444 struct device_node *rem;
2445
2446 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
2447 endpoint);
2448 if (!endpoint)
2449 break;
2450
2451 sdinfo = &pdata->sub_devs[i];
2452 sdinfo->grp_id = 0;
2453
2454
2455 sdinfo->inputs[0].index = i;
2456 strcpy(sdinfo->inputs[0].name, "Camera");
2457 sdinfo->inputs[0].type = V4L2_INPUT_TYPE_CAMERA;
2458 sdinfo->inputs[0].std = V4L2_STD_ALL;
2459 sdinfo->inputs[0].capabilities = V4L2_IN_CAP_STD;
2460
2461 sdinfo->can_route = 0;
2462 sdinfo->routes = NULL;
2463
2464 of_property_read_u32(endpoint, "ti,am437x-vpfe-interface",
2465 &sdinfo->vpfe_param.if_type);
2466 if (sdinfo->vpfe_param.if_type < 0 ||
2467 sdinfo->vpfe_param.if_type > 4) {
2468 sdinfo->vpfe_param.if_type = VPFE_RAW_BAYER;
2469 }
2470
2471 err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
2472 &bus_cfg);
2473 if (err) {
2474 dev_err(&pdev->dev, "Could not parse the endpoint\n");
2475 goto done;
2476 }
2477
2478 sdinfo->vpfe_param.bus_width = bus_cfg.bus.parallel.bus_width;
2479
2480 if (sdinfo->vpfe_param.bus_width < 8 ||
2481 sdinfo->vpfe_param.bus_width > 16) {
2482 dev_err(&pdev->dev, "Invalid bus width.\n");
2483 goto done;
2484 }
2485
2486 flags = bus_cfg.bus.parallel.flags;
2487
2488 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2489 sdinfo->vpfe_param.hdpol = 1;
2490
2491 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2492 sdinfo->vpfe_param.vdpol = 1;
2493
2494 rem = of_graph_get_remote_port_parent(endpoint);
2495 if (!rem) {
2496 dev_err(&pdev->dev, "Remote device at %pOF not found\n",
2497 endpoint);
2498 goto done;
2499 }
2500
2501 pdata->asd[i] = devm_kzalloc(&pdev->dev,
2502 sizeof(struct v4l2_async_subdev),
2503 GFP_KERNEL);
2504 if (!pdata->asd[i]) {
2505 of_node_put(rem);
2506 pdata = NULL;
2507 goto done;
2508 }
2509
2510 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_FWNODE;
2511 pdata->asd[i]->match.fwnode = of_fwnode_handle(rem);
2512 of_node_put(rem);
2513 }
2514
2515 of_node_put(endpoint);
2516 return pdata;
2517
2518done:
2519 of_node_put(endpoint);
2520 return NULL;
2521}
2522
2523
2524
2525
2526
2527
2528static int vpfe_probe(struct platform_device *pdev)
2529{
2530 struct vpfe_config *vpfe_cfg = vpfe_get_pdata(pdev);
2531 struct vpfe_device *vpfe;
2532 struct vpfe_ccdc *ccdc;
2533 struct resource *res;
2534 int ret;
2535
2536 if (!vpfe_cfg) {
2537 dev_err(&pdev->dev, "No platform data\n");
2538 return -EINVAL;
2539 }
2540
2541 vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2542 if (!vpfe)
2543 return -ENOMEM;
2544
2545 vpfe->pdev = &pdev->dev;
2546 vpfe->cfg = vpfe_cfg;
2547 ccdc = &vpfe->ccdc;
2548
2549 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2550 ccdc->ccdc_cfg.base_addr = devm_ioremap_resource(&pdev->dev, res);
2551 if (IS_ERR(ccdc->ccdc_cfg.base_addr))
2552 return PTR_ERR(ccdc->ccdc_cfg.base_addr);
2553
2554 ret = platform_get_irq(pdev, 0);
2555 if (ret <= 0) {
2556 dev_err(&pdev->dev, "No IRQ resource\n");
2557 return -ENODEV;
2558 }
2559 vpfe->irq = ret;
2560
2561 ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2562 "vpfe_capture0", vpfe);
2563 if (ret) {
2564 dev_err(&pdev->dev, "Unable to request interrupt\n");
2565 return -EINVAL;
2566 }
2567
2568 ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2569 if (ret) {
2570 vpfe_err(vpfe,
2571 "Unable to register v4l2 device.\n");
2572 return ret;
2573 }
2574
2575
2576 platform_set_drvdata(pdev, vpfe);
2577
2578 pm_runtime_enable(&pdev->dev);
2579
2580
2581 pm_runtime_get_sync(&pdev->dev);
2582
2583 vpfe_ccdc_config_defaults(ccdc);
2584
2585 pm_runtime_put_sync(&pdev->dev);
2586
2587 vpfe->sd = devm_kcalloc(&pdev->dev,
2588 ARRAY_SIZE(vpfe->cfg->asd),
2589 sizeof(struct v4l2_subdev *),
2590 GFP_KERNEL);
2591 if (!vpfe->sd) {
2592 ret = -ENOMEM;
2593 goto probe_out_v4l2_unregister;
2594 }
2595
2596 vpfe->notifier.subdevs = vpfe->cfg->asd;
2597 vpfe->notifier.num_subdevs = ARRAY_SIZE(vpfe->cfg->asd);
2598 vpfe->notifier.ops = &vpfe_async_ops;
2599 ret = v4l2_async_notifier_register(&vpfe->v4l2_dev,
2600 &vpfe->notifier);
2601 if (ret) {
2602 vpfe_err(vpfe, "Error registering async notifier\n");
2603 ret = -EINVAL;
2604 goto probe_out_v4l2_unregister;
2605 }
2606
2607 return 0;
2608
2609probe_out_v4l2_unregister:
2610 v4l2_device_unregister(&vpfe->v4l2_dev);
2611 return ret;
2612}
2613
2614
2615
2616
2617static int vpfe_remove(struct platform_device *pdev)
2618{
2619 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2620
2621 vpfe_dbg(2, vpfe, "vpfe_remove\n");
2622
2623 pm_runtime_disable(&pdev->dev);
2624
2625 v4l2_async_notifier_unregister(&vpfe->notifier);
2626 v4l2_device_unregister(&vpfe->v4l2_dev);
2627 video_unregister_device(&vpfe->video_dev);
2628
2629 return 0;
2630}
2631
2632#ifdef CONFIG_PM_SLEEP
2633
2634static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2635{
2636 ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2637 ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2638 ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2639 ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2640 ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2641 ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2642 ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2643 ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2644 ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2645 ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2646 ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2647 ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2648 ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2649 ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2650 VPFE_HD_VD_WID);
2651 ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2652 VPFE_PIX_LINES);
2653 ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2654 VPFE_HORZ_INFO);
2655 ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2656 VPFE_VERT_START);
2657 ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2658 VPFE_VERT_LINES);
2659 ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2660 VPFE_HSIZE_OFF);
2661}
2662
2663static int vpfe_suspend(struct device *dev)
2664{
2665 struct vpfe_device *vpfe = dev_get_drvdata(dev);
2666 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2667
2668
2669 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2670 return 0;
2671
2672 pm_runtime_get_sync(dev);
2673 vpfe_config_enable(ccdc, 1);
2674
2675
2676 vpfe_save_context(ccdc);
2677
2678
2679 vpfe_pcr_enable(ccdc, 0);
2680 vpfe_config_enable(ccdc, 0);
2681
2682
2683 pm_runtime_put_sync(dev);
2684
2685
2686 pinctrl_pm_select_sleep_state(dev);
2687
2688 return 0;
2689}
2690
2691static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
2692{
2693 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SYNMODE >> 2], VPFE_SYNMODE);
2694 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CULLING >> 2], VPFE_CULLING);
2695 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDOFST >> 2], VPFE_SDOFST);
2696 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2], VPFE_SDR_ADDR);
2697 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CLAMP >> 2], VPFE_CLAMP);
2698 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_DCSUB >> 2], VPFE_DCSUB);
2699 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_COLPTN >> 2], VPFE_COLPTN);
2700 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_BLKCMP >> 2], VPFE_BLKCMP);
2701 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VDINT >> 2], VPFE_VDINT);
2702 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_ALAW >> 2], VPFE_ALAW);
2703 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_REC656IF >> 2], VPFE_REC656IF);
2704 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CCDCFG >> 2], VPFE_CCDCFG);
2705 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PCR >> 2], VPFE_PCR);
2706 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2],
2707 VPFE_HD_VD_WID);
2708 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2709 VPFE_PIX_LINES);
2710 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2711 VPFE_HORZ_INFO);
2712 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2713 VPFE_VERT_START);
2714 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2715 VPFE_VERT_LINES);
2716 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2717 VPFE_HSIZE_OFF);
2718}
2719
2720static int vpfe_resume(struct device *dev)
2721{
2722 struct vpfe_device *vpfe = dev_get_drvdata(dev);
2723 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2724
2725
2726 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2727 return 0;
2728
2729
2730 pm_runtime_get_sync(dev);
2731 vpfe_config_enable(ccdc, 1);
2732
2733
2734 vpfe_restore_context(ccdc);
2735
2736 vpfe_config_enable(ccdc, 0);
2737 pm_runtime_put_sync(dev);
2738
2739
2740 pinctrl_pm_select_default_state(dev);
2741
2742 return 0;
2743}
2744
2745#endif
2746
2747static SIMPLE_DEV_PM_OPS(vpfe_pm_ops, vpfe_suspend, vpfe_resume);
2748
2749static const struct of_device_id vpfe_of_match[] = {
2750 { .compatible = "ti,am437x-vpfe", },
2751 { },
2752};
2753MODULE_DEVICE_TABLE(of, vpfe_of_match);
2754
2755static struct platform_driver vpfe_driver = {
2756 .probe = vpfe_probe,
2757 .remove = vpfe_remove,
2758 .driver = {
2759 .name = VPFE_MODULE_NAME,
2760 .pm = &vpfe_pm_ops,
2761 .of_match_table = of_match_ptr(vpfe_of_match),
2762 },
2763};
2764
2765module_platform_driver(vpfe_driver);
2766
2767MODULE_AUTHOR("Texas Instruments");
2768MODULE_DESCRIPTION("TI AM437x VPFE driver");
2769MODULE_LICENSE("GPL");
2770MODULE_VERSION(VPFE_VERSION);
2771