1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Video Capture/Differentiation Engine (VCD) and Encoding
4 * Compression Engine (ECE) present on Nuvoton NPCM SoCs.
6 * Copyright (C) 2022 Nuvoton Technologies
9 #include <linux/atomic.h>
10 #include <linux/bitfield.h>
11 #include <linux/bitmap.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/interrupt.h>
17 #include <linux/jiffies.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_reserved_mem.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/reset.h>
28 #include <linux/sched.h>
29 #include <linux/spinlock.h>
30 #include <linux/string.h>
31 #include <linux/v4l2-controls.h>
32 #include <linux/videodev2.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-dev.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-dv-timings.h>
37 #include <media/v4l2-event.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/videobuf2-dma-contig.h>
40 #include <uapi/linux/npcm-video.h>
41 #include "npcm-regs.h"
43 #define DEVICE_NAME "npcm-video"
44 #define MAX_WIDTH 1920
45 #define MAX_HEIGHT 1200
47 #define MIN_HEIGHT 240
52 #define BITMAP_SIZE 32
54 struct npcm_video_addr {
60 struct npcm_video_buffer {
61 struct vb2_v4l2_buffer vb;
62 struct list_head link;
65 #define to_npcm_video_buffer(x) \
66 container_of((x), struct npcm_video_buffer, vb)
69 * VIDEO_STREAMING: a flag indicating if the video has started streaming
70 * VIDEO_CAPTURING: a flag indicating if the VCD is capturing a frame
71 * VIDEO_RES_CHANGING: a flag indicating if the resolution is changing
72 * VIDEO_STOPPED: a flag indicating if the video has stopped streaming
82 struct v4l2_clip clip;
83 struct list_head list;
86 struct rect_list_info {
87 struct rect_list *list;
88 struct rect_list *first;
89 struct list_head *head;
91 unsigned int tile_perline;
92 unsigned int tile_perrow;
93 unsigned int offset_perline;
94 unsigned int tile_size;
95 unsigned int tile_cnt;
99 struct regmap *regmap;
101 struct reset_control *reset;
106 struct regmap *gcr_regmap;
107 struct regmap *gfx_regmap;
108 struct regmap *vcd_regmap;
111 struct v4l2_ctrl_handler ctrl_handler;
112 struct v4l2_ctrl *rect_cnt_ctrl;
113 struct v4l2_device v4l2_dev;
114 struct v4l2_pix_format pix_fmt;
115 struct v4l2_bt_timings active_timings;
116 struct v4l2_bt_timings detected_timings;
117 unsigned int v4l2_input_status;
118 struct vb2_queue queue;
119 struct video_device vdev;
120 struct mutex video_lock; /* v4l2 and videobuf2 lock */
122 struct list_head buffers;
123 spinlock_t lock; /* buffer list lock */
125 unsigned int sequence;
127 struct npcm_video_addr src;
128 struct reset_control *reset;
131 unsigned int bytesperline;
132 unsigned int bytesperpixel;
133 unsigned int rect_cnt;
134 struct list_head list[VIDEO_MAX_FRAME];
135 unsigned int rect[VIDEO_MAX_FRAME];
136 unsigned int ctrl_cmd;
140 #define to_npcm_video(x) container_of((x), struct npcm_video, v4l2_dev)
144 unsigned int bpp; /* bytes per pixel */
147 static const struct npcm_fmt npcm_fmt_list[] = {
149 .fourcc = V4L2_PIX_FMT_RGB565,
153 .fourcc = V4L2_PIX_FMT_HEXTILE,
158 #define NUM_FORMATS ARRAY_SIZE(npcm_fmt_list)
160 static const struct v4l2_dv_timings_cap npcm_video_timings_cap = {
161 .type = V4L2_DV_BT_656_1120,
163 .min_width = MIN_WIDTH,
164 .max_width = MAX_WIDTH,
165 .min_height = MIN_HEIGHT,
166 .max_height = MAX_HEIGHT,
167 .min_pixelclock = 6574080, /* 640 x 480 x 24Hz */
168 .max_pixelclock = 138240000, /* 1920 x 1200 x 60Hz */
169 .standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
170 V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
171 .capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
172 V4L2_DV_BT_CAP_REDUCED_BLANKING |
173 V4L2_DV_BT_CAP_CUSTOM,
177 static DECLARE_BITMAP(bitmap, BITMAP_SIZE);
179 static const struct npcm_fmt *npcm_video_find_format(struct v4l2_format *f)
181 const struct npcm_fmt *fmt;
184 for (k = 0; k < NUM_FORMATS; k++) {
185 fmt = &npcm_fmt_list[k];
186 if (fmt->fourcc == f->fmt.pix.pixelformat)
190 if (k == NUM_FORMATS)
193 return &npcm_fmt_list[k];
196 static void npcm_video_ece_prepend_rect_header(void *addr, u16 x, u16 y, u16 w, u16 h)
198 __be16 x_pos = cpu_to_be16(x);
199 __be16 y_pos = cpu_to_be16(y);
200 __be16 width = cpu_to_be16(w);
201 __be16 height = cpu_to_be16(h);
202 __be32 encoding = cpu_to_be32(5); /* Hextile encoding */
204 memcpy(addr, &x_pos, 2);
205 memcpy(addr + 2, &y_pos, 2);
206 memcpy(addr + 4, &width, 2);
207 memcpy(addr + 6, &height, 2);
208 memcpy(addr + 8, &encoding, 4);
211 static unsigned int npcm_video_ece_get_ed_size(struct npcm_video *video,
212 unsigned int offset, void *addr)
214 struct regmap *ece = video->ece.regmap;
215 unsigned int size, gap, val;
218 ret = regmap_read_poll_timeout(ece, ECE_DDA_STS, val,
219 (val & ECE_DDA_STS_CDREADY), 0,
220 ECE_POLL_TIMEOUT_US);
223 dev_warn(video->dev, "Wait for ECE_DDA_STS_CDREADY timeout\n");
227 size = readl((void __iomem *)addr + offset);
228 regmap_read(ece, ECE_HEX_CTRL, &val);
229 gap = FIELD_GET(ECE_HEX_CTRL_ENC_GAP, val);
231 dev_dbg(video->dev, "offset = %u, ed_size = %u, gap = %u\n", offset,
237 static void npcm_video_ece_enc_rect(struct npcm_video *video,
238 unsigned int r_off_x, unsigned int r_off_y,
239 unsigned int r_w, unsigned int r_h)
241 struct regmap *ece = video->ece.regmap;
242 unsigned int rect_offset = (r_off_y * video->bytesperline) + (r_off_x * 2);
243 unsigned int w_size = ECE_TILE_W, h_size = ECE_TILE_H;
244 unsigned int temp, w_tile, h_tile;
246 regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, 0);
247 regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, ECE_DDA_CTRL_ECEEN);
248 regmap_write(ece, ECE_DDA_STS, ECE_DDA_STS_CDREADY | ECE_DDA_STS_ACDRDY);
249 regmap_write(ece, ECE_RECT_XY, rect_offset);
251 w_tile = r_w / ECE_TILE_W;
252 h_tile = r_h / ECE_TILE_H;
254 if (r_w % ECE_TILE_W) {
256 w_size = r_w % ECE_TILE_W;
258 if (r_h % ECE_TILE_H || !h_tile) {
260 h_size = r_h % ECE_TILE_H;
263 temp = FIELD_PREP(ECE_RECT_DIMEN_WLTR, w_size - 1) |
264 FIELD_PREP(ECE_RECT_DIMEN_HLTR, h_size - 1) |
265 FIELD_PREP(ECE_RECT_DIMEN_WR, w_tile - 1) |
266 FIELD_PREP(ECE_RECT_DIMEN_HR, h_tile - 1);
268 regmap_write(ece, ECE_RECT_DIMEN, temp);
271 static unsigned int npcm_video_ece_read_rect_offset(struct npcm_video *video)
273 struct regmap *ece = video->ece.regmap;
276 regmap_read(ece, ECE_HEX_RECT_OFFSET, &offset);
277 return FIELD_GET(ECE_HEX_RECT_OFFSET_MASK, offset);
281 * Set the line pitch (in bytes) for the frame buffers.
282 * Can be on of those values: 512, 1024, 2048, 2560 or 4096 bytes.
284 static void npcm_video_ece_set_lp(struct npcm_video *video, unsigned int pitch)
286 struct regmap *ece = video->ece.regmap;
291 lp = ECE_RESOL_FB_LP_512;
294 lp = ECE_RESOL_FB_LP_1024;
297 lp = ECE_RESOL_FB_LP_2048;
300 lp = ECE_RESOL_FB_LP_2560;
303 lp = ECE_RESOL_FB_LP_4096;
309 regmap_write(ece, ECE_RESOL, lp);
312 static inline void npcm_video_ece_set_fb_addr(struct npcm_video *video,
315 struct regmap *ece = video->ece.regmap;
317 regmap_write(ece, ECE_FBR_BA, buffer);
320 static inline void npcm_video_ece_set_enc_dba(struct npcm_video *video,
323 struct regmap *ece = video->ece.regmap;
325 regmap_write(ece, ECE_ED_BA, addr);
328 static inline void npcm_video_ece_clear_rect_offset(struct npcm_video *video)
330 struct regmap *ece = video->ece.regmap;
332 regmap_write(ece, ECE_HEX_RECT_OFFSET, 0);
335 static void npcm_video_ece_ctrl_reset(struct npcm_video *video)
337 struct regmap *ece = video->ece.regmap;
339 regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, 0);
340 regmap_update_bits(ece, ECE_HEX_CTRL, ECE_HEX_CTRL_ENCDIS, ECE_HEX_CTRL_ENCDIS);
341 regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, ECE_DDA_CTRL_ECEEN);
342 regmap_update_bits(ece, ECE_HEX_CTRL, ECE_HEX_CTRL_ENCDIS, 0);
344 npcm_video_ece_clear_rect_offset(video);
347 static void npcm_video_ece_ip_reset(struct npcm_video *video)
350 * After resetting a module and clearing the reset bit, it should wait
351 * at least 10 us before accessing the module.
353 reset_control_assert(video->ece.reset);
354 usleep_range(10, 20);
355 reset_control_deassert(video->ece.reset);
356 usleep_range(10, 20);
359 static void npcm_video_ece_stop(struct npcm_video *video)
361 struct regmap *ece = video->ece.regmap;
363 regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_ECEEN, 0);
364 regmap_update_bits(ece, ECE_DDA_CTRL, ECE_DDA_CTRL_INTEN, 0);
365 regmap_update_bits(ece, ECE_HEX_CTRL, ECE_HEX_CTRL_ENCDIS, ECE_HEX_CTRL_ENCDIS);
366 npcm_video_ece_clear_rect_offset(video);
369 static bool npcm_video_alloc_fb(struct npcm_video *video,
370 struct npcm_video_addr *addr)
372 addr->virt = dma_alloc_coherent(video->dev, VCD_FB_SIZE, &addr->dma,
377 addr->size = VCD_FB_SIZE;
381 static void npcm_video_free_fb(struct npcm_video *video,
382 struct npcm_video_addr *addr)
384 dma_free_coherent(video->dev, addr->size, addr->virt, addr->dma);
390 static void npcm_video_free_diff_table(struct npcm_video *video)
392 struct list_head *head, *pos, *nx;
393 struct rect_list *tmp;
396 for (i = 0; i < video->queue.num_buffers; i++) {
397 head = &video->list[i];
398 list_for_each_safe(pos, nx, head) {
399 tmp = list_entry(pos, struct rect_list, list);
400 list_del(&tmp->list);
406 static unsigned int npcm_video_add_rect(struct npcm_video *video,
408 unsigned int x, unsigned int y,
409 unsigned int w, unsigned int h)
411 struct list_head *head = &video->list[index];
412 struct rect_list *list = NULL;
415 list = kzalloc(sizeof(*list), GFP_KERNEL);
425 list_add_tail(&list->list, head);
429 static void npcm_video_merge_rect(struct npcm_video *video,
430 struct rect_list_info *info)
432 struct list_head *head = info->head;
433 struct rect_list *list = info->list, *first = info->first;
434 struct v4l2_rect *r = &list->clip.c, *f = &first->clip.c;
439 list_add_tail(&list->list, head);
442 if ((r->left == (f->left + f->width)) && r->top == f->top) {
443 f->width += r->width;
445 } else if ((r->top == (f->top + f->height)) &&
446 (r->left == f->left)) {
447 f->height += r->height;
449 } else if (((r->top > f->top) &&
450 (r->top < (f->top + f->height))) &&
451 ((r->left > f->left) &&
452 (r->left < (f->left + f->width)))) {
455 list_add_tail(&list->list, head);
462 static struct rect_list *npcm_video_new_rect(struct npcm_video *video,
466 struct v4l2_bt_timings *act = &video->active_timings;
467 struct rect_list *list = NULL;
470 list = kzalloc(sizeof(*list), GFP_KERNEL);
476 r->left = (offset << 4);
477 r->top = (index >> 2);
480 if ((r->left + RECT_W) > act->width)
481 r->width = act->width - r->left;
482 if ((r->top + RECT_H) > act->height)
483 r->height = act->height - r->top;
488 static int npcm_video_find_rect(struct npcm_video *video,
489 struct rect_list_info *info,
492 if (offset < info->tile_perline) {
493 info->list = npcm_video_new_rect(video, offset, info->index);
495 dev_err(video->dev, "Failed to allocate rect_list\n");
499 npcm_video_merge_rect(video, info);
504 static int npcm_video_build_table(struct npcm_video *video,
505 struct rect_list_info *info)
507 struct regmap *vcd = video->vcd_regmap;
508 unsigned int j, bit, value;
511 for (j = 0; j < info->offset_perline; j += 4) {
512 regmap_read(vcd, VCD_DIFF_TBL + (j + info->index), &value);
514 bitmap_from_arr32(bitmap, &value, BITMAP_SIZE);
516 for_each_set_bit(bit, bitmap, BITMAP_SIZE) {
517 ret = npcm_video_find_rect(video, info, bit + (j << 3));
523 return info->tile_perline;
526 static void npcm_video_get_rect_list(struct npcm_video *video, unsigned int index)
528 struct v4l2_bt_timings *act = &video->active_timings;
529 struct rect_list_info info;
530 unsigned int tile_cnt = 0, mod;
533 memset(&info, 0, sizeof(struct rect_list_info));
534 info.head = &video->list[index];
536 info.tile_perline = act->width >> 4;
537 mod = act->width % RECT_W;
539 info.tile_perline += 1;
541 info.tile_perrow = act->height >> 4;
542 mod = act->height % RECT_H;
544 info.tile_perrow += 1;
546 info.tile_size = info.tile_perrow * info.tile_perline;
548 info.offset_perline = info.tile_perline >> 5;
549 mod = info.tile_perline % 32;
551 info.offset_perline += 1;
553 info.offset_perline *= 4;
556 ret = npcm_video_build_table(video, &info);
561 } while (tile_cnt < info.tile_size);
564 static unsigned int npcm_video_is_mga(struct npcm_video *video)
566 struct regmap *gfxi = video->gfx_regmap;
569 regmap_read(gfxi, DISPST, &dispst);
570 return ((dispst & DISPST_MGAMODE) == DISPST_MGAMODE);
573 static unsigned int npcm_video_hres(struct npcm_video *video)
575 struct regmap *gfxi = video->gfx_regmap;
576 unsigned int hvcnth, hvcntl, apb_hor_res;
578 regmap_read(gfxi, HVCNTH, &hvcnth);
579 regmap_read(gfxi, HVCNTL, &hvcntl);
580 apb_hor_res = (((hvcnth & HVCNTH_MASK) << 8) + (hvcntl & HVCNTL_MASK) + 1);
585 static unsigned int npcm_video_vres(struct npcm_video *video)
587 struct regmap *gfxi = video->gfx_regmap;
588 unsigned int vvcnth, vvcntl, apb_ver_res;
590 regmap_read(gfxi, VVCNTH, &vvcnth);
591 regmap_read(gfxi, VVCNTL, &vvcntl);
593 apb_ver_res = (((vvcnth & VVCNTH_MASK) << 8) + (vvcntl & VVCNTL_MASK));
598 static int npcm_video_capres(struct npcm_video *video, unsigned int hor_res,
599 unsigned int vert_res)
601 struct regmap *vcd = video->vcd_regmap;
602 unsigned int res, cap_res;
604 if (hor_res > MAX_WIDTH || vert_res > MAX_HEIGHT)
607 res = FIELD_PREP(VCD_CAP_RES_VERT_RES, vert_res) |
608 FIELD_PREP(VCD_CAP_RES_HOR_RES, hor_res);
610 regmap_write(vcd, VCD_CAP_RES, res);
611 regmap_read(vcd, VCD_CAP_RES, &cap_res);
619 static void npcm_video_vcd_ip_reset(struct npcm_video *video)
622 * After resetting a module and clearing the reset bit, it should wait
623 * at least 10 us before accessing the module.
625 reset_control_assert(video->reset);
626 usleep_range(10, 20);
627 reset_control_deassert(video->reset);
628 usleep_range(10, 20);
631 static void npcm_video_vcd_state_machine_reset(struct npcm_video *video)
633 struct regmap *vcd = video->vcd_regmap;
635 regmap_update_bits(vcd, VCD_MODE, VCD_MODE_VCDE, 0);
636 regmap_update_bits(vcd, VCD_MODE, VCD_MODE_IDBC, 0);
637 regmap_update_bits(vcd, VCD_CMD, VCD_CMD_RST, VCD_CMD_RST);
640 * VCD_CMD_RST will reset VCD internal state machines and clear FIFOs,
641 * it should wait at least 800 us for the reset operations completed.
643 usleep_range(800, 1000);
645 regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
646 regmap_update_bits(vcd, VCD_MODE, VCD_MODE_VCDE, VCD_MODE_VCDE);
647 regmap_update_bits(vcd, VCD_MODE, VCD_MODE_IDBC, VCD_MODE_IDBC);
650 static void npcm_video_gfx_reset(struct npcm_video *video)
652 struct regmap *gcr = video->gcr_regmap;
654 regmap_update_bits(gcr, INTCR2, INTCR2_GIRST2, INTCR2_GIRST2);
655 npcm_video_vcd_state_machine_reset(video);
656 regmap_update_bits(gcr, INTCR2, INTCR2_GIRST2, 0);
659 static void npcm_video_kvm_bw(struct npcm_video *video, bool set_bw)
661 struct regmap *vcd = video->vcd_regmap;
663 if (set_bw || !npcm_video_is_mga(video))
664 regmap_update_bits(vcd, VCD_MODE, VCD_MODE_KVM_BW_SET,
665 VCD_MODE_KVM_BW_SET);
667 regmap_update_bits(vcd, VCD_MODE, VCD_MODE_KVM_BW_SET, 0);
670 static unsigned int npcm_video_pclk(struct npcm_video *video)
672 struct regmap *gfxi = video->gfx_regmap;
673 unsigned int tmp, pllfbdiv, pllinotdiv, gpllfbdiv;
674 unsigned int gpllfbdv109, gpllfbdv8, gpllindiv;
675 unsigned int gpllst_pllotdiv1, gpllst_pllotdiv2;
677 regmap_read(gfxi, GPLLST, &tmp);
678 gpllfbdv109 = FIELD_GET(GPLLST_GPLLFBDV109, tmp);
679 gpllst_pllotdiv1 = FIELD_GET(GPLLST_PLLOTDIV1, tmp);
680 gpllst_pllotdiv2 = FIELD_GET(GPLLST_PLLOTDIV2, tmp);
682 regmap_read(gfxi, GPLLINDIV, &tmp);
683 gpllfbdv8 = FIELD_GET(GPLLINDIV_GPLLFBDV8, tmp);
684 gpllindiv = FIELD_GET(GPLLINDIV_MASK, tmp);
686 regmap_read(gfxi, GPLLFBDIV, &tmp);
687 gpllfbdiv = FIELD_GET(GPLLFBDIV_MASK, tmp);
689 pllfbdiv = (512 * gpllfbdv109 + 256 * gpllfbdv8 + gpllfbdiv);
690 pllinotdiv = (gpllindiv * gpllst_pllotdiv1 * gpllst_pllotdiv2);
691 if (pllfbdiv == 0 || pllinotdiv == 0)
694 return ((pllfbdiv * 25000) / pllinotdiv) * 1000;
697 static unsigned int npcm_video_get_bpp(struct npcm_video *video)
699 const struct npcm_fmt *fmt;
702 for (k = 0; k < NUM_FORMATS; k++) {
703 fmt = &npcm_fmt_list[k];
704 if (fmt->fourcc == video->pix_fmt.pixelformat)
712 * Pitch must be a power of 2, >= linebytes,
713 * at least 512, and no more than 4096.
715 static void npcm_video_set_linepitch(struct npcm_video *video,
716 unsigned int linebytes)
718 struct regmap *vcd = video->vcd_regmap;
719 unsigned int pitch = MIN_LP;
721 while ((pitch < linebytes) && (pitch < MAX_LP))
724 regmap_write(vcd, VCD_FB_LP, FIELD_PREP(VCD_FBA_LP, pitch) |
725 FIELD_PREP(VCD_FBB_LP, pitch));
728 static unsigned int npcm_video_get_linepitch(struct npcm_video *video)
730 struct regmap *vcd = video->vcd_regmap;
731 unsigned int linepitch;
733 regmap_read(vcd, VCD_FB_LP, &linepitch);
734 return FIELD_GET(VCD_FBA_LP, linepitch);
737 static void npcm_video_command(struct npcm_video *video, unsigned int value)
739 struct regmap *vcd = video->vcd_regmap;
742 regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
743 regmap_read(vcd, VCD_CMD, &cmd);
744 cmd |= FIELD_PREP(VCD_CMD_OPERATION, value);
746 regmap_write(vcd, VCD_CMD, cmd);
747 regmap_update_bits(vcd, VCD_CMD, VCD_CMD_GO, VCD_CMD_GO);
748 video->op_cmd = value;
751 static void npcm_video_init_reg(struct npcm_video *video)
753 struct regmap *gcr = video->gcr_regmap, *vcd = video->vcd_regmap;
755 /* Selects Data Enable */
756 regmap_update_bits(gcr, INTCR, INTCR_DEHS, 0);
758 /* Enable display of KVM GFX and access to memory */
759 regmap_update_bits(gcr, INTCR, INTCR_GFXIFDIS, 0);
761 /* Active Vertical/Horizontal Counters Reset */
762 regmap_update_bits(gcr, INTCR2, INTCR2_GIHCRST | INTCR2_GIVCRST,
763 INTCR2_GIHCRST | INTCR2_GIVCRST);
765 /* Reset video modules */
766 npcm_video_vcd_ip_reset(video);
767 npcm_video_gfx_reset(video);
769 /* Set the FIFO thresholds */
770 regmap_write(vcd, VCD_FIFO, VCD_FIFO_TH);
773 regmap_write(vcd, VCD_RCHG, FIELD_PREP(VCD_RCHG_TIM_PRSCL, 0xf) |
774 FIELD_PREP(VCD_RCHG_IG_CHG0, 0x3));
777 regmap_write(vcd, VCD_MODE, VCD_MODE_VCDE | VCD_MODE_CM565 |
778 VCD_MODE_IDBC | VCD_MODE_KVM_BW_SET);
781 static int npcm_video_start_frame(struct npcm_video *video)
783 struct npcm_video_buffer *buf;
784 struct regmap *vcd = video->vcd_regmap;
789 if (video->v4l2_input_status) {
790 dev_dbg(video->dev, "No video signal; skip capture frame\n");
794 ret = regmap_read_poll_timeout(vcd, VCD_STAT, val, !(val & VCD_STAT_BUSY),
795 1000, VCD_TIMEOUT_US);
797 dev_err(video->dev, "Wait for VCD_STAT_BUSY timeout\n");
801 spin_lock_irqsave(&video->lock, flags);
802 buf = list_first_entry_or_null(&video->buffers,
803 struct npcm_video_buffer, link);
805 spin_unlock_irqrestore(&video->lock, flags);
806 dev_dbg(video->dev, "No empty buffers; skip capture frame\n");
810 set_bit(VIDEO_CAPTURING, &video->flags);
811 spin_unlock_irqrestore(&video->lock, flags);
813 npcm_video_vcd_state_machine_reset(video);
815 regmap_read(vcd, VCD_HOR_AC_TIM, &val);
816 regmap_update_bits(vcd, VCD_HOR_AC_LST, VCD_HOR_AC_LAST,
817 FIELD_GET(VCD_HOR_AC_TIME, val));
819 regmap_read(vcd, VCD_VER_HI_TIM, &val);
820 regmap_update_bits(vcd, VCD_VER_HI_LST, VCD_VER_HI_LAST,
821 FIELD_GET(VCD_VER_HI_TIME, val));
823 regmap_update_bits(vcd, VCD_INTE, VCD_INTE_DONE_IE | VCD_INTE_IFOT_IE |
824 VCD_INTE_IFOR_IE | VCD_INTE_HAC_IE | VCD_INTE_VHT_IE,
825 VCD_INTE_DONE_IE | VCD_INTE_IFOT_IE | VCD_INTE_IFOR_IE |
826 VCD_INTE_HAC_IE | VCD_INTE_VHT_IE);
828 npcm_video_command(video, video->ctrl_cmd);
833 static void npcm_video_bufs_done(struct npcm_video *video,
834 enum vb2_buffer_state state)
836 struct npcm_video_buffer *buf;
839 spin_lock_irqsave(&video->lock, flags);
840 list_for_each_entry(buf, &video->buffers, link)
841 vb2_buffer_done(&buf->vb.vb2_buf, state);
843 INIT_LIST_HEAD(&video->buffers);
844 spin_unlock_irqrestore(&video->lock, flags);
847 static void npcm_video_get_diff_rect(struct npcm_video *video, unsigned int index)
849 unsigned int width = video->active_timings.width;
850 unsigned int height = video->active_timings.height;
852 if (video->op_cmd != VCD_CMD_OPERATION_CAPTURE) {
854 npcm_video_get_rect_list(video, index);
855 video->rect[index] = video->rect_cnt;
857 video->rect[index] = npcm_video_add_rect(video, index, 0, 0,
862 static void npcm_video_detect_resolution(struct npcm_video *video)
864 struct v4l2_bt_timings *act = &video->active_timings;
865 struct v4l2_bt_timings *det = &video->detected_timings;
866 struct regmap *gfxi = video->gfx_regmap;
869 video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
870 det->width = npcm_video_hres(video);
871 det->height = npcm_video_vres(video);
873 if (act->width != det->width || act->height != det->height) {
874 dev_dbg(video->dev, "Resolution changed\n");
876 if (npcm_video_hres(video) > 0 && npcm_video_vres(video) > 0) {
877 if (test_bit(VIDEO_STREAMING, &video->flags)) {
879 * Wait for resolution is available,
880 * and it is also captured by host.
884 regmap_read(gfxi, DISPST, &dispst);
885 } while (npcm_video_vres(video) < 100 ||
886 npcm_video_pclk(video) == 0 ||
887 (dispst & DISPST_HSCROFF));
890 det->width = npcm_video_hres(video);
891 det->height = npcm_video_vres(video);
892 det->pixelclock = npcm_video_pclk(video);
895 clear_bit(VIDEO_RES_CHANGING, &video->flags);
898 if (det->width && det->height)
899 video->v4l2_input_status = 0;
901 dev_dbg(video->dev, "Got resolution[%dx%d] -> [%dx%d], status %d\n",
902 act->width, act->height, det->width, det->height,
903 video->v4l2_input_status);
906 static int npcm_video_set_resolution(struct npcm_video *video,
907 struct v4l2_bt_timings *timing)
909 struct regmap *vcd = video->vcd_regmap;
912 if (npcm_video_capres(video, timing->width, timing->height)) {
913 dev_err(video->dev, "Failed to set VCD_CAP_RES\n");
917 video->active_timings = *timing;
918 video->bytesperpixel = npcm_video_get_bpp(video);
919 npcm_video_set_linepitch(video, timing->width * video->bytesperpixel);
920 video->bytesperline = npcm_video_get_linepitch(video);
921 video->pix_fmt.width = timing->width ? timing->width : MIN_WIDTH;
922 video->pix_fmt.height = timing->height ? timing->height : MIN_HEIGHT;
923 video->pix_fmt.sizeimage = video->pix_fmt.width * video->pix_fmt.height *
924 video->bytesperpixel;
925 video->pix_fmt.bytesperline = video->bytesperline;
927 npcm_video_kvm_bw(video, timing->pixelclock > VCD_KVM_BW_PCLK);
928 npcm_video_gfx_reset(video);
929 regmap_read(vcd, VCD_MODE, &mode);
931 dev_dbg(video->dev, "VCD mode = 0x%x, %s mode\n", mode,
932 npcm_video_is_mga(video) ? "Hi Res" : "VGA");
935 "Digital mode: %d x %d x %d, pixelclock %lld, bytesperline %d\n",
936 timing->width, timing->height, video->bytesperpixel,
937 timing->pixelclock, video->bytesperline);
942 static void npcm_video_start(struct npcm_video *video)
944 npcm_video_init_reg(video);
946 if (!npcm_video_alloc_fb(video, &video->src)) {
947 dev_err(video->dev, "Failed to allocate VCD frame buffer\n");
951 npcm_video_detect_resolution(video);
952 if (npcm_video_set_resolution(video, &video->detected_timings)) {
953 dev_err(video->dev, "Failed to set resolution\n");
957 /* Set frame buffer physical address */
958 regmap_write(video->vcd_regmap, VCD_FBA_ADR, video->src.dma);
959 regmap_write(video->vcd_regmap, VCD_FBB_ADR, video->src.dma);
961 if (video->ece.enable && atomic_inc_return(&video->ece.clients) == 1) {
962 npcm_video_ece_ip_reset(video);
963 npcm_video_ece_ctrl_reset(video);
964 npcm_video_ece_set_fb_addr(video, video->src.dma);
965 npcm_video_ece_set_lp(video, video->bytesperline);
967 dev_dbg(video->dev, "ECE open: client %d\n",
968 atomic_read(&video->ece.clients));
972 static void npcm_video_stop(struct npcm_video *video)
974 struct regmap *vcd = video->vcd_regmap;
976 set_bit(VIDEO_STOPPED, &video->flags);
978 regmap_write(vcd, VCD_INTE, 0);
979 regmap_write(vcd, VCD_MODE, 0);
980 regmap_write(vcd, VCD_RCHG, 0);
981 regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
984 npcm_video_free_fb(video, &video->src);
986 npcm_video_free_diff_table(video);
987 video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
989 video->ctrl_cmd = VCD_CMD_OPERATION_CAPTURE;
991 if (video->ece.enable && atomic_dec_return(&video->ece.clients) == 0) {
992 npcm_video_ece_stop(video);
993 dev_dbg(video->dev, "ECE close: client %d\n",
994 atomic_read(&video->ece.clients));
998 static unsigned int npcm_video_raw(struct npcm_video *video, int index, void *addr)
1000 unsigned int width = video->active_timings.width;
1001 unsigned int height = video->active_timings.height;
1002 unsigned int i, len, offset, bytes = 0;
1004 video->rect[index] = npcm_video_add_rect(video, index, 0, 0, width, height);
1006 for (i = 0; i < height; i++) {
1007 len = width * video->bytesperpixel;
1008 offset = i * video->bytesperline;
1010 memcpy(addr + bytes, video->src.virt + offset, len);
1017 static unsigned int npcm_video_hextile(struct npcm_video *video, unsigned int index,
1018 unsigned int dma_addr, void *vaddr)
1020 struct rect_list *rect_list;
1021 struct v4l2_rect *rect;
1022 unsigned int offset, len, bytes = 0;
1024 npcm_video_ece_ctrl_reset(video);
1025 npcm_video_ece_clear_rect_offset(video);
1026 npcm_video_ece_set_fb_addr(video, video->src.dma);
1028 /* Set base address of encoded data to video buffer */
1029 npcm_video_ece_set_enc_dba(video, dma_addr);
1031 npcm_video_ece_set_lp(video, video->bytesperline);
1032 npcm_video_get_diff_rect(video, index);
1034 list_for_each_entry(rect_list, &video->list[index], list) {
1035 rect = &rect_list->clip.c;
1036 offset = npcm_video_ece_read_rect_offset(video);
1037 npcm_video_ece_enc_rect(video, rect->left, rect->top,
1038 rect->width, rect->height);
1040 len = npcm_video_ece_get_ed_size(video, offset, vaddr);
1041 npcm_video_ece_prepend_rect_header(vaddr + offset,
1042 rect->left, rect->top,
1043 rect->width, rect->height);
1050 static irqreturn_t npcm_video_irq(int irq, void *arg)
1052 struct npcm_video *video = arg;
1053 struct regmap *vcd = video->vcd_regmap;
1054 struct npcm_video_buffer *buf;
1055 unsigned int index, size, status, fmt;
1056 dma_addr_t dma_addr;
1058 static const struct v4l2_event ev = {
1059 .type = V4L2_EVENT_SOURCE_CHANGE,
1060 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1063 regmap_read(vcd, VCD_STAT, &status);
1064 dev_dbg(video->dev, "VCD irq status 0x%x\n", status);
1066 regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
1068 if (test_bit(VIDEO_STOPPED, &video->flags) ||
1069 !test_bit(VIDEO_STREAMING, &video->flags))
1072 if (status & VCD_STAT_DONE) {
1073 regmap_write(vcd, VCD_INTE, 0);
1074 spin_lock(&video->lock);
1075 clear_bit(VIDEO_CAPTURING, &video->flags);
1076 buf = list_first_entry_or_null(&video->buffers,
1077 struct npcm_video_buffer, link);
1079 spin_unlock(&video->lock);
1083 addr = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1084 index = buf->vb.vb2_buf.index;
1085 fmt = video->pix_fmt.pixelformat;
1088 case V4L2_PIX_FMT_RGB565:
1089 size = npcm_video_raw(video, index, addr);
1091 case V4L2_PIX_FMT_HEXTILE:
1092 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
1093 size = npcm_video_hextile(video, index, dma_addr, addr);
1096 spin_unlock(&video->lock);
1100 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1101 buf->vb.vb2_buf.timestamp = ktime_get_ns();
1102 buf->vb.sequence = video->sequence++;
1103 buf->vb.field = V4L2_FIELD_NONE;
1105 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
1106 list_del(&buf->link);
1107 spin_unlock(&video->lock);
1109 if (npcm_video_start_frame(video))
1110 dev_err(video->dev, "Failed to capture next frame\n");
1113 /* Resolution changed */
1114 if (status & VCD_STAT_VHT_CHG || status & VCD_STAT_HAC_CHG) {
1115 if (!test_bit(VIDEO_RES_CHANGING, &video->flags)) {
1116 set_bit(VIDEO_RES_CHANGING, &video->flags);
1118 vb2_queue_error(&video->queue);
1119 v4l2_event_queue(&video->vdev, &ev);
1123 if (status & VCD_STAT_IFOR || status & VCD_STAT_IFOT) {
1124 dev_warn(video->dev, "VCD FIFO overrun or over thresholds\n");
1125 if (npcm_video_start_frame(video))
1126 dev_err(video->dev, "Failed to recover from FIFO overrun\n");
1132 static int npcm_video_querycap(struct file *file, void *fh,
1133 struct v4l2_capability *cap)
1135 strscpy(cap->driver, DEVICE_NAME, sizeof(cap->driver));
1136 strscpy(cap->card, "NPCM Video Engine", sizeof(cap->card));
1141 static int npcm_video_enum_format(struct file *file, void *fh,
1142 struct v4l2_fmtdesc *f)
1144 struct npcm_video *video = video_drvdata(file);
1145 const struct npcm_fmt *fmt;
1147 if (f->index >= NUM_FORMATS)
1150 fmt = &npcm_fmt_list[f->index];
1151 if (fmt->fourcc == V4L2_PIX_FMT_HEXTILE && !video->ece.enable)
1154 f->pixelformat = fmt->fourcc;
1158 static int npcm_video_try_format(struct file *file, void *fh,
1159 struct v4l2_format *f)
1161 struct npcm_video *video = video_drvdata(file);
1162 const struct npcm_fmt *fmt;
1164 fmt = npcm_video_find_format(f);
1166 /* If format not found or HEXTILE not supported, use RGB565 as default */
1167 if (!fmt || (fmt->fourcc == V4L2_PIX_FMT_HEXTILE && !video->ece.enable))
1168 f->fmt.pix.pixelformat = npcm_fmt_list[0].fourcc;
1170 f->fmt.pix.field = V4L2_FIELD_NONE;
1171 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1172 f->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1173 f->fmt.pix.width = video->pix_fmt.width;
1174 f->fmt.pix.height = video->pix_fmt.height;
1175 f->fmt.pix.bytesperline = video->bytesperline;
1176 f->fmt.pix.sizeimage = video->pix_fmt.sizeimage;
1181 static int npcm_video_get_format(struct file *file, void *fh,
1182 struct v4l2_format *f)
1184 struct npcm_video *video = video_drvdata(file);
1186 f->fmt.pix = video->pix_fmt;
1190 static int npcm_video_set_format(struct file *file, void *fh,
1191 struct v4l2_format *f)
1193 struct npcm_video *video = video_drvdata(file);
1196 ret = npcm_video_try_format(file, fh, f);
1200 if (vb2_is_busy(&video->queue)) {
1201 dev_err(video->dev, "%s device busy\n", __func__);
1205 video->pix_fmt.pixelformat = f->fmt.pix.pixelformat;
1209 static int npcm_video_enum_input(struct file *file, void *fh,
1210 struct v4l2_input *inp)
1212 struct npcm_video *video = video_drvdata(file);
1217 strscpy(inp->name, "Host VGA capture", sizeof(inp->name));
1218 inp->type = V4L2_INPUT_TYPE_CAMERA;
1219 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1220 inp->status = video->v4l2_input_status;
1225 static int npcm_video_get_input(struct file *file, void *fh, unsigned int *i)
1232 static int npcm_video_set_input(struct file *file, void *fh, unsigned int i)
1240 static int npcm_video_set_dv_timings(struct file *file, void *fh,
1241 struct v4l2_dv_timings *timings)
1243 struct npcm_video *video = video_drvdata(file);
1246 if (timings->bt.width == video->active_timings.width &&
1247 timings->bt.height == video->active_timings.height)
1250 if (vb2_is_busy(&video->queue)) {
1251 dev_err(video->dev, "%s device busy\n", __func__);
1255 rc = npcm_video_set_resolution(video, &timings->bt);
1259 timings->type = V4L2_DV_BT_656_1120;
1264 static int npcm_video_get_dv_timings(struct file *file, void *fh,
1265 struct v4l2_dv_timings *timings)
1267 struct npcm_video *video = video_drvdata(file);
1269 timings->type = V4L2_DV_BT_656_1120;
1270 timings->bt = video->active_timings;
1275 static int npcm_video_query_dv_timings(struct file *file, void *fh,
1276 struct v4l2_dv_timings *timings)
1278 struct npcm_video *video = video_drvdata(file);
1280 npcm_video_detect_resolution(video);
1281 timings->type = V4L2_DV_BT_656_1120;
1282 timings->bt = video->detected_timings;
1284 return video->v4l2_input_status ? -ENOLINK : 0;
1287 static int npcm_video_enum_dv_timings(struct file *file, void *fh,
1288 struct v4l2_enum_dv_timings *timings)
1290 return v4l2_enum_dv_timings_cap(timings, &npcm_video_timings_cap,
1294 static int npcm_video_dv_timings_cap(struct file *file, void *fh,
1295 struct v4l2_dv_timings_cap *cap)
1297 *cap = npcm_video_timings_cap;
1302 static int npcm_video_sub_event(struct v4l2_fh *fh,
1303 const struct v4l2_event_subscription *sub)
1305 switch (sub->type) {
1306 case V4L2_EVENT_SOURCE_CHANGE:
1307 return v4l2_src_change_event_subscribe(fh, sub);
1310 return v4l2_ctrl_subscribe_event(fh, sub);
1313 static const struct v4l2_ioctl_ops npcm_video_ioctls = {
1314 .vidioc_querycap = npcm_video_querycap,
1316 .vidioc_enum_fmt_vid_cap = npcm_video_enum_format,
1317 .vidioc_g_fmt_vid_cap = npcm_video_get_format,
1318 .vidioc_s_fmt_vid_cap = npcm_video_set_format,
1319 .vidioc_try_fmt_vid_cap = npcm_video_try_format,
1321 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1322 .vidioc_querybuf = vb2_ioctl_querybuf,
1323 .vidioc_qbuf = vb2_ioctl_qbuf,
1324 .vidioc_expbuf = vb2_ioctl_expbuf,
1325 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1326 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1327 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1328 .vidioc_streamon = vb2_ioctl_streamon,
1329 .vidioc_streamoff = vb2_ioctl_streamoff,
1331 .vidioc_enum_input = npcm_video_enum_input,
1332 .vidioc_g_input = npcm_video_get_input,
1333 .vidioc_s_input = npcm_video_set_input,
1335 .vidioc_s_dv_timings = npcm_video_set_dv_timings,
1336 .vidioc_g_dv_timings = npcm_video_get_dv_timings,
1337 .vidioc_query_dv_timings = npcm_video_query_dv_timings,
1338 .vidioc_enum_dv_timings = npcm_video_enum_dv_timings,
1339 .vidioc_dv_timings_cap = npcm_video_dv_timings_cap,
1341 .vidioc_subscribe_event = npcm_video_sub_event,
1342 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1345 static int npcm_video_set_ctrl(struct v4l2_ctrl *ctrl)
1347 struct npcm_video *video = container_of(ctrl->handler, struct npcm_video,
1351 case V4L2_CID_NPCM_CAPTURE_MODE:
1352 if (ctrl->val == V4L2_NPCM_CAPTURE_MODE_COMPLETE)
1353 video->ctrl_cmd = VCD_CMD_OPERATION_CAPTURE;
1354 else if (ctrl->val == V4L2_NPCM_CAPTURE_MODE_DIFF)
1355 video->ctrl_cmd = VCD_CMD_OPERATION_COMPARE;
1364 static const struct v4l2_ctrl_ops npcm_video_ctrl_ops = {
1365 .s_ctrl = npcm_video_set_ctrl,
1368 static const char * const npcm_ctrl_capture_mode_menu[] = {
1374 static const struct v4l2_ctrl_config npcm_ctrl_capture_mode = {
1375 .ops = &npcm_video_ctrl_ops,
1376 .id = V4L2_CID_NPCM_CAPTURE_MODE,
1377 .name = "NPCM Video Capture Mode",
1378 .type = V4L2_CTRL_TYPE_MENU,
1380 .max = V4L2_NPCM_CAPTURE_MODE_DIFF,
1382 .qmenu = npcm_ctrl_capture_mode_menu,
1386 * This control value is set when a buffer is dequeued by userspace, i.e. in
1387 * npcm_video_buf_finish function.
1389 static const struct v4l2_ctrl_config npcm_ctrl_rect_count = {
1390 .id = V4L2_CID_NPCM_RECT_COUNT,
1391 .name = "NPCM Hextile Rectangle Count",
1392 .type = V4L2_CTRL_TYPE_INTEGER,
1394 .max = (MAX_WIDTH / RECT_W) * (MAX_HEIGHT / RECT_H),
1399 static int npcm_video_open(struct file *file)
1401 struct npcm_video *video = video_drvdata(file);
1404 mutex_lock(&video->video_lock);
1405 rc = v4l2_fh_open(file);
1407 mutex_unlock(&video->video_lock);
1411 if (v4l2_fh_is_singular_file(file))
1412 npcm_video_start(video);
1414 mutex_unlock(&video->video_lock);
1418 static int npcm_video_release(struct file *file)
1420 struct npcm_video *video = video_drvdata(file);
1423 mutex_lock(&video->video_lock);
1424 if (v4l2_fh_is_singular_file(file))
1425 npcm_video_stop(video);
1427 rc = _vb2_fop_release(file, NULL);
1429 mutex_unlock(&video->video_lock);
1433 static const struct v4l2_file_operations npcm_video_v4l2_fops = {
1434 .owner = THIS_MODULE,
1435 .read = vb2_fop_read,
1436 .poll = vb2_fop_poll,
1437 .unlocked_ioctl = video_ioctl2,
1438 .mmap = vb2_fop_mmap,
1439 .open = npcm_video_open,
1440 .release = npcm_video_release,
1443 static int npcm_video_queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
1444 unsigned int *num_planes, unsigned int sizes[],
1445 struct device *alloc_devs[])
1447 struct npcm_video *video = vb2_get_drv_priv(q);
1451 if (sizes[0] < video->pix_fmt.sizeimage)
1458 sizes[0] = video->pix_fmt.sizeimage;
1460 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1461 INIT_LIST_HEAD(&video->list[i]);
1466 static int npcm_video_buf_prepare(struct vb2_buffer *vb)
1468 struct npcm_video *video = vb2_get_drv_priv(vb->vb2_queue);
1470 if (vb2_plane_size(vb, 0) < video->pix_fmt.sizeimage)
1476 static int npcm_video_start_streaming(struct vb2_queue *q, unsigned int count)
1478 struct npcm_video *video = vb2_get_drv_priv(q);
1481 video->sequence = 0;
1482 rc = npcm_video_start_frame(video);
1484 npcm_video_bufs_done(video, VB2_BUF_STATE_QUEUED);
1488 set_bit(VIDEO_STREAMING, &video->flags);
1492 static void npcm_video_stop_streaming(struct vb2_queue *q)
1494 struct npcm_video *video = vb2_get_drv_priv(q);
1495 struct regmap *vcd = video->vcd_regmap;
1497 clear_bit(VIDEO_STREAMING, &video->flags);
1498 regmap_write(vcd, VCD_INTE, 0);
1499 regmap_write(vcd, VCD_STAT, VCD_STAT_CLEAR);
1500 npcm_video_gfx_reset(video);
1501 npcm_video_bufs_done(video, VB2_BUF_STATE_ERROR);
1502 video->ctrl_cmd = VCD_CMD_OPERATION_CAPTURE;
1503 v4l2_ctrl_s_ctrl(video->rect_cnt_ctrl, 0);
1506 static void npcm_video_buf_queue(struct vb2_buffer *vb)
1508 struct npcm_video *video = vb2_get_drv_priv(vb->vb2_queue);
1509 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1510 struct npcm_video_buffer *nvb = to_npcm_video_buffer(vbuf);
1511 unsigned long flags;
1514 spin_lock_irqsave(&video->lock, flags);
1515 empty = list_empty(&video->buffers);
1516 list_add_tail(&nvb->link, &video->buffers);
1517 spin_unlock_irqrestore(&video->lock, flags);
1519 if (test_bit(VIDEO_STREAMING, &video->flags) &&
1520 !test_bit(VIDEO_CAPTURING, &video->flags) && empty) {
1521 if (npcm_video_start_frame(video))
1522 dev_err(video->dev, "Failed to capture next frame\n");
1526 static void npcm_video_buf_finish(struct vb2_buffer *vb)
1528 struct npcm_video *video = vb2_get_drv_priv(vb->vb2_queue);
1529 struct list_head *head, *pos, *nx;
1530 struct rect_list *tmp;
1533 * This callback is called when the buffer is dequeued, so update
1534 * V4L2_CID_NPCM_RECT_COUNT control value with the number of rectangles
1535 * in this buffer and free associated rect_list.
1537 if (test_bit(VIDEO_STREAMING, &video->flags)) {
1538 v4l2_ctrl_s_ctrl(video->rect_cnt_ctrl, video->rect[vb->index]);
1540 head = &video->list[vb->index];
1541 list_for_each_safe(pos, nx, head) {
1542 tmp = list_entry(pos, struct rect_list, list);
1543 list_del(&tmp->list);
1549 static const struct regmap_config npcm_video_regmap_cfg = {
1553 .max_register = VCD_FIFO,
1556 static const struct regmap_config npcm_video_ece_regmap_cfg = {
1560 .max_register = ECE_HEX_RECT_OFFSET,
1563 static const struct vb2_ops npcm_video_vb2_ops = {
1564 .queue_setup = npcm_video_queue_setup,
1565 .wait_prepare = vb2_ops_wait_prepare,
1566 .wait_finish = vb2_ops_wait_finish,
1567 .buf_prepare = npcm_video_buf_prepare,
1568 .buf_finish = npcm_video_buf_finish,
1569 .start_streaming = npcm_video_start_streaming,
1570 .stop_streaming = npcm_video_stop_streaming,
1571 .buf_queue = npcm_video_buf_queue,
1574 static int npcm_video_setup_video(struct npcm_video *video)
1576 struct v4l2_device *v4l2_dev = &video->v4l2_dev;
1577 struct video_device *vdev = &video->vdev;
1578 struct vb2_queue *vbq = &video->queue;
1581 if (video->ece.enable)
1582 video->pix_fmt.pixelformat = V4L2_PIX_FMT_HEXTILE;
1584 video->pix_fmt.pixelformat = V4L2_PIX_FMT_RGB565;
1586 video->pix_fmt.field = V4L2_FIELD_NONE;
1587 video->pix_fmt.colorspace = V4L2_COLORSPACE_SRGB;
1588 video->pix_fmt.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1589 video->v4l2_input_status = V4L2_IN_ST_NO_SIGNAL;
1591 rc = v4l2_device_register(video->dev, v4l2_dev);
1593 dev_err(video->dev, "Failed to register v4l2 device\n");
1597 v4l2_ctrl_handler_init(&video->ctrl_handler, 2);
1598 v4l2_ctrl_new_custom(&video->ctrl_handler, &npcm_ctrl_capture_mode, NULL);
1599 video->rect_cnt_ctrl = v4l2_ctrl_new_custom(&video->ctrl_handler,
1600 &npcm_ctrl_rect_count, NULL);
1601 if (video->ctrl_handler.error) {
1602 dev_err(video->dev, "Failed to init controls: %d\n",
1603 video->ctrl_handler.error);
1605 rc = video->ctrl_handler.error;
1606 goto rel_ctrl_handler;
1608 v4l2_dev->ctrl_handler = &video->ctrl_handler;
1610 vbq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1611 vbq->io_modes = VB2_MMAP | VB2_DMABUF;
1612 vbq->dev = v4l2_dev->dev;
1613 vbq->lock = &video->video_lock;
1614 vbq->ops = &npcm_video_vb2_ops;
1615 vbq->mem_ops = &vb2_dma_contig_memops;
1616 vbq->drv_priv = video;
1617 vbq->buf_struct_size = sizeof(struct npcm_video_buffer);
1618 vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1619 vbq->min_buffers_needed = 3;
1621 rc = vb2_queue_init(vbq);
1623 dev_err(video->dev, "Failed to init vb2 queue\n");
1624 goto rel_ctrl_handler;
1627 vdev->fops = &npcm_video_v4l2_fops;
1628 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1629 vdev->v4l2_dev = v4l2_dev;
1630 strscpy(vdev->name, DEVICE_NAME, sizeof(vdev->name));
1631 vdev->vfl_type = VFL_TYPE_VIDEO;
1632 vdev->vfl_dir = VFL_DIR_RX;
1633 vdev->release = video_device_release_empty;
1634 vdev->ioctl_ops = &npcm_video_ioctls;
1635 vdev->lock = &video->video_lock;
1637 video_set_drvdata(vdev, video);
1638 rc = video_register_device(vdev, VFL_TYPE_VIDEO, 0);
1640 dev_err(video->dev, "Failed to register video device\n");
1647 vb2_queue_release(vbq);
1649 v4l2_ctrl_handler_free(&video->ctrl_handler);
1650 v4l2_device_unregister(v4l2_dev);
1655 static int npcm_video_ece_init(struct npcm_video *video)
1657 struct device *dev = video->dev;
1658 struct device_node *ece_node;
1659 struct platform_device *ece_pdev;
1662 ece_node = of_parse_phandle(video->dev->of_node, "nuvoton,ece", 0);
1664 dev_err(dev, "Failed to get ECE phandle in DTS\n");
1668 video->ece.enable = of_device_is_available(ece_node);
1670 if (video->ece.enable) {
1671 dev_info(dev, "Support HEXTILE pixel format\n");
1673 ece_pdev = of_find_device_by_node(ece_node);
1674 if (IS_ERR(ece_pdev)) {
1675 dev_err(dev, "Failed to find ECE device\n");
1676 return PTR_ERR(ece_pdev);
1678 of_node_put(ece_node);
1680 regs = devm_platform_ioremap_resource(ece_pdev, 0);
1682 dev_err(dev, "Failed to parse ECE reg in DTS\n");
1683 return PTR_ERR(regs);
1686 video->ece.regmap = devm_regmap_init_mmio(dev, regs,
1687 &npcm_video_ece_regmap_cfg);
1688 if (IS_ERR(video->ece.regmap)) {
1689 dev_err(dev, "Failed to initialize ECE regmap\n");
1690 return PTR_ERR(video->ece.regmap);
1693 video->ece.reset = devm_reset_control_get(&ece_pdev->dev, NULL);
1694 if (IS_ERR(video->ece.reset)) {
1695 dev_err(dev, "Failed to get ECE reset control in DTS\n");
1696 return PTR_ERR(video->ece.reset);
1703 static int npcm_video_init(struct npcm_video *video)
1705 struct device *dev = video->dev;
1708 irq = irq_of_parse_and_map(dev->of_node, 0);
1710 dev_err(dev, "Failed to find VCD IRQ\n");
1714 rc = devm_request_threaded_irq(dev, irq, NULL, npcm_video_irq,
1715 IRQF_ONESHOT, DEVICE_NAME, video);
1717 dev_err(dev, "Failed to request IRQ %d\n", irq);
1721 of_reserved_mem_device_init(dev);
1722 rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
1724 dev_err(dev, "Failed to set DMA mask\n");
1725 of_reserved_mem_device_release(dev);
1728 rc = npcm_video_ece_init(video);
1730 dev_err(dev, "Failed to initialize ECE\n");
1737 static int npcm_video_probe(struct platform_device *pdev)
1739 struct npcm_video *video = kzalloc(sizeof(*video), GFP_KERNEL);
1746 video->dev = &pdev->dev;
1747 spin_lock_init(&video->lock);
1748 mutex_init(&video->video_lock);
1749 INIT_LIST_HEAD(&video->buffers);
1751 regs = devm_platform_ioremap_resource(pdev, 0);
1753 dev_err(&pdev->dev, "Failed to parse VCD reg in DTS\n");
1754 return PTR_ERR(regs);
1757 video->vcd_regmap = devm_regmap_init_mmio(&pdev->dev, regs,
1758 &npcm_video_regmap_cfg);
1759 if (IS_ERR(video->vcd_regmap)) {
1760 dev_err(&pdev->dev, "Failed to initialize VCD regmap\n");
1761 return PTR_ERR(video->vcd_regmap);
1764 video->reset = devm_reset_control_get(&pdev->dev, NULL);
1765 if (IS_ERR(video->reset)) {
1766 dev_err(&pdev->dev, "Failed to get VCD reset control in DTS\n");
1767 return PTR_ERR(video->reset);
1770 video->gcr_regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1772 if (IS_ERR(video->gcr_regmap))
1773 return PTR_ERR(video->gcr_regmap);
1775 video->gfx_regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1777 if (IS_ERR(video->gfx_regmap))
1778 return PTR_ERR(video->gfx_regmap);
1780 rc = npcm_video_init(video);
1784 rc = npcm_video_setup_video(video);
1788 dev_info(video->dev, "NPCM video driver probed\n");
1792 static int npcm_video_remove(struct platform_device *pdev)
1794 struct device *dev = &pdev->dev;
1795 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
1796 struct npcm_video *video = to_npcm_video(v4l2_dev);
1798 video_unregister_device(&video->vdev);
1799 vb2_queue_release(&video->queue);
1800 v4l2_ctrl_handler_free(&video->ctrl_handler);
1801 v4l2_device_unregister(v4l2_dev);
1802 if (video->ece.enable)
1803 npcm_video_ece_stop(video);
1804 of_reserved_mem_device_release(dev);
1809 static const struct of_device_id npcm_video_match[] = {
1810 { .compatible = "nuvoton,npcm750-vcd" },
1811 { .compatible = "nuvoton,npcm845-vcd" },
1815 MODULE_DEVICE_TABLE(of, npcm_video_match);
1817 static struct platform_driver npcm_video_driver = {
1819 .name = DEVICE_NAME,
1820 .of_match_table = npcm_video_match,
1822 .probe = npcm_video_probe,
1823 .remove = npcm_video_remove,
1826 module_platform_driver(npcm_video_driver);
1828 MODULE_AUTHOR("Joseph Liu <kwliu@nuvoton.com>");
1829 MODULE_AUTHOR("Marvin Lin <kflin@nuvoton.com>");
1830 MODULE_DESCRIPTION("Driver for Nuvoton NPCM Video Capture/Encode Engine");
1831 MODULE_LICENSE("GPL v2");