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