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