1 // SPDX-License-Identifier: GPL-2.0-or-later
3 On Screen Display cx23415 Framebuffer driver
5 This module presents the cx23415 OSD (onscreen display) framebuffer memory
6 as a standard Linux /dev/fb style framebuffer device. The framebuffer has
7 support for 8, 16 & 32 bpp packed pixel formats with alpha channel. In 16bpp
8 mode, there is a choice of a three color depths (12, 15 or 16 bits), but no
9 local alpha. The colorspace is selectable between rgb & yuv.
10 Depending on the TV standard configured in the ivtv module at load time,
11 the initial resolution is either 640x400 (NTSC) or 640x480 (PAL) at 8bpp.
12 Video timings are locked to ensure a vertical refresh rate of 50Hz (PAL)
15 Copyright (c) 2003 Matt T. Yourst <yourst@yourst.com>
17 Derived from drivers/video/vesafb.c
18 Portions (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
21 Copyright (C) 2004 Matthias Badaire
23 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
25 Copyright (C) 2006 Ian Armstrong <ian@iarmst.demon.co.uk>
29 #include "ivtv-driver.h"
30 #include "ivtv-cards.h"
32 #include "ivtv-udma.h"
33 #include "ivtv-mailbox.h"
34 #include "ivtv-firmware.h"
37 #include <linux/ivtvfb.h>
39 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
40 #include <asm/memtype.h>
44 static int ivtvfb_card_id
= -1;
45 static int ivtvfb_debug
;
46 static bool ivtvfb_force_pat
= IS_ENABLED(CONFIG_VIDEO_FB_IVTV_FORCE_PAT
);
47 static bool osd_laced
;
51 static unsigned int osd_yres
;
52 static unsigned int osd_xres
;
54 module_param(ivtvfb_card_id
, int, 0444);
55 module_param_named(debug
,ivtvfb_debug
, int, 0644);
56 module_param_named(force_pat
, ivtvfb_force_pat
, bool, 0644);
57 module_param(osd_laced
, bool, 0444);
58 module_param(osd_depth
, int, 0444);
59 module_param(osd_upper
, int, 0444);
60 module_param(osd_left
, int, 0444);
61 module_param(osd_yres
, uint
, 0444);
62 module_param(osd_xres
, uint
, 0444);
64 MODULE_PARM_DESC(ivtvfb_card_id
,
65 "Only use framebuffer of the specified ivtv card (0-31)\n"
66 "\t\t\tdefault -1: initialize all available framebuffers");
68 MODULE_PARM_DESC(debug
,
69 "Debug level (bitmask). Default: errors only\n"
70 "\t\t\t(debug = 3 gives full debugging)");
72 MODULE_PARM_DESC(force_pat
,
73 "Force initialization on x86 PAT-enabled systems (bool).\n");
75 /* Why upper, left, xres, yres, depth, laced ? To match terminology used
77 Why start at 1 for left & upper coordinate ? Because X doesn't allow 0 */
79 MODULE_PARM_DESC(osd_laced
,
85 MODULE_PARM_DESC(osd_depth
,
86 "Bits per pixel - 8, 16, 32\n"
89 MODULE_PARM_DESC(osd_upper
,
90 "Vertical start position\n"
91 "\t\t\tdefault 0 (Centered)");
93 MODULE_PARM_DESC(osd_left
,
94 "Horizontal start position\n"
95 "\t\t\tdefault 0 (Centered)");
97 MODULE_PARM_DESC(osd_yres
,
99 "\t\t\tdefault 480 (PAL)\n"
100 "\t\t\t 400 (NTSC)");
102 MODULE_PARM_DESC(osd_xres
,
104 "\t\t\tdefault 640");
106 MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil, John Harvey, Ian Armstrong");
107 MODULE_DESCRIPTION("Conexant cx23415 framebuffer support");
108 MODULE_LICENSE("GPL");
110 /* --------------------------------------------------------------------- */
112 #define IVTVFB_DBGFLG_WARN (1 << 0)
113 #define IVTVFB_DBGFLG_INFO (1 << 1)
115 #define IVTVFB_DEBUG(x, type, fmt, args...) \
117 if ((x) & ivtvfb_debug) \
118 printk(KERN_INFO "ivtvfb%d " type ": " fmt, itv->instance , ## args); \
120 #define IVTVFB_DEBUG_WARN(fmt, args...) IVTVFB_DEBUG(IVTVFB_DBGFLG_WARN, "warning", fmt , ## args)
121 #define IVTVFB_DEBUG_INFO(fmt, args...) IVTVFB_DEBUG(IVTVFB_DBGFLG_INFO, "info", fmt , ## args)
123 /* Standard kernel messages */
124 #define IVTVFB_ERR(fmt, args...) printk(KERN_ERR "ivtvfb%d: " fmt, itv->instance , ## args)
125 #define IVTVFB_WARN(fmt, args...) printk(KERN_WARNING "ivtvfb%d: " fmt, itv->instance , ## args)
126 #define IVTVFB_INFO(fmt, args...) printk(KERN_INFO "ivtvfb%d: " fmt, itv->instance , ## args)
128 /* --------------------------------------------------------------------- */
130 #define IVTV_OSD_MAX_WIDTH 720
131 #define IVTV_OSD_MAX_HEIGHT 576
133 #define IVTV_OSD_BPP_8 0x00
134 #define IVTV_OSD_BPP_16_444 0x03
135 #define IVTV_OSD_BPP_16_555 0x02
136 #define IVTV_OSD_BPP_16_565 0x01
137 #define IVTV_OSD_BPP_32 0x04
140 /* Physical base address */
141 unsigned long video_pbase
;
142 /* Relative base address (relative to start of decoder memory) */
144 /* Mapped base address */
145 volatile char __iomem
*video_vbase
;
147 u32 video_buffer_size
;
149 /* video_base rounded down as required by hardware MTRRs */
150 unsigned long fb_start_aligned_physaddr
;
151 /* video_base rounded up as required by hardware MTRRs */
152 unsigned long fb_end_aligned_physaddr
;
155 /* Store the buffer offset */
156 int set_osd_coords_x
;
157 int set_osd_coords_y
;
159 /* Current dimensions (NOT VISIBLE SIZE!) */
162 int display_byte_stride
;
164 /* Current bits per pixel */
168 /* Frame buffer stuff */
169 struct fb_info ivtvfb_info
;
170 struct fb_var_screeninfo ivtvfb_defined
;
171 struct fb_fix_screeninfo ivtvfb_fix
;
173 /* Used for a warm start */
174 struct fb_var_screeninfo fbvar_cur
;
176 u32 palette_cur
[256];
180 struct ivtv_osd_coords
{
181 unsigned long offset
;
182 unsigned long max_offset
;
189 /* --------------------------------------------------------------------- */
191 /* ivtv API calls for framebuffer related support */
193 static int ivtvfb_get_framebuffer(struct ivtv
*itv
, u32
*fbbase
,
196 u32 data
[CX2341X_MBOX_MAX_DATA
];
199 ivtv_firmware_check(itv
, "ivtvfb_get_framebuffer");
200 rc
= ivtv_vapi_result(itv
, data
, CX2341X_OSD_GET_FRAMEBUFFER
, 0);
206 static int ivtvfb_get_osd_coords(struct ivtv
*itv
,
207 struct ivtv_osd_coords
*osd
)
209 struct osd_info
*oi
= itv
->osd_info
;
210 u32 data
[CX2341X_MBOX_MAX_DATA
];
212 ivtv_vapi_result(itv
, data
, CX2341X_OSD_GET_OSD_COORDS
, 0);
214 osd
->offset
= data
[0] - oi
->video_rbase
;
215 osd
->max_offset
= oi
->display_width
* oi
->display_height
* 4;
216 osd
->pixel_stride
= data
[1];
217 osd
->lines
= data
[2];
223 static int ivtvfb_set_osd_coords(struct ivtv
*itv
, const struct ivtv_osd_coords
*osd
)
225 struct osd_info
*oi
= itv
->osd_info
;
227 oi
->display_width
= osd
->pixel_stride
;
228 oi
->display_byte_stride
= osd
->pixel_stride
* oi
->bytes_per_pixel
;
229 oi
->set_osd_coords_x
+= osd
->x
;
230 oi
->set_osd_coords_y
= osd
->y
;
232 return ivtv_vapi(itv
, CX2341X_OSD_SET_OSD_COORDS
, 5,
233 osd
->offset
+ oi
->video_rbase
,
235 osd
->lines
, osd
->x
, osd
->y
);
238 static int ivtvfb_set_display_window(struct ivtv
*itv
, struct v4l2_rect
*ivtv_window
)
240 int osd_height_limit
= itv
->is_out_50hz
? 576 : 480;
242 /* Only fail if resolution too high, otherwise fudge the start coords. */
243 if ((ivtv_window
->height
> osd_height_limit
) || (ivtv_window
->width
> IVTV_OSD_MAX_WIDTH
))
246 /* Ensure we don't exceed display limits */
247 if (ivtv_window
->top
+ ivtv_window
->height
> osd_height_limit
) {
248 IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid height setting (%d, %d)\n",
249 ivtv_window
->top
, ivtv_window
->height
);
250 ivtv_window
->top
= osd_height_limit
- ivtv_window
->height
;
253 if (ivtv_window
->left
+ ivtv_window
->width
> IVTV_OSD_MAX_WIDTH
) {
254 IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid width setting (%d, %d)\n",
255 ivtv_window
->left
, ivtv_window
->width
);
256 ivtv_window
->left
= IVTV_OSD_MAX_WIDTH
- ivtv_window
->width
;
259 /* Set the OSD origin */
260 write_reg((ivtv_window
->top
<< 16) | ivtv_window
->left
, 0x02a04);
262 /* How much to display */
263 write_reg(((ivtv_window
->top
+ivtv_window
->height
) << 16) | (ivtv_window
->left
+ivtv_window
->width
), 0x02a08);
265 /* Pass this info back the yuv handler */
266 itv
->yuv_info
.osd_vis_w
= ivtv_window
->width
;
267 itv
->yuv_info
.osd_vis_h
= ivtv_window
->height
;
268 itv
->yuv_info
.osd_x_offset
= ivtv_window
->left
;
269 itv
->yuv_info
.osd_y_offset
= ivtv_window
->top
;
274 static int ivtvfb_prep_dec_dma_to_device(struct ivtv
*itv
,
275 unsigned long ivtv_dest_addr
, void __user
*userbuf
,
281 mutex_lock(&itv
->udma
.lock
);
283 if (ivtv_udma_setup(itv
, ivtv_dest_addr
, userbuf
, size_in_bytes
) <= 0) {
284 mutex_unlock(&itv
->udma
.lock
);
285 IVTVFB_WARN("%s, Error in ivtv_udma_setup: %d bytes, %d pages returned\n",
286 __func__
, size_in_bytes
, itv
->udma
.page_count
);
288 /* pin_user_pages or DMA must have failed completely */
292 IVTVFB_DEBUG_INFO("ivtvfb_prep_dec_dma_to_device, %d bytes, %d pages\n",
293 size_in_bytes
, itv
->udma
.page_count
);
295 ivtv_udma_prepare(itv
);
296 prepare_to_wait(&itv
->dma_waitq
, &wait
, TASK_INTERRUPTIBLE
);
297 /* if no UDMA is pending and no UDMA is in progress, then the DMA
299 while (test_bit(IVTV_F_I_UDMA_PENDING
, &itv
->i_flags
) ||
300 test_bit(IVTV_F_I_UDMA
, &itv
->i_flags
)) {
301 /* don't interrupt if the DMA is in progress but break off
302 a still pending DMA. */
303 got_sig
= signal_pending(current
);
304 if (got_sig
&& test_and_clear_bit(IVTV_F_I_UDMA_PENDING
, &itv
->i_flags
))
309 finish_wait(&itv
->dma_waitq
, &wait
);
311 /* Unmap Last DMA Xfer */
312 ivtv_udma_unmap(itv
);
313 mutex_unlock(&itv
->udma
.lock
);
315 IVTV_DEBUG_INFO("User stopped OSD\n");
322 static int ivtvfb_prep_frame(struct ivtv
*itv
, int cmd
, void __user
*source
,
323 unsigned long dest_offset
, int count
)
326 struct osd_info
*oi
= itv
->osd_info
;
330 IVTVFB_DEBUG_WARN("ivtvfb_prep_frame: Nothing to do. count = 0\n");
334 /* Check Total FB Size */
335 if ((dest_offset
+ count
) > oi
->video_buffer_size
) {
336 IVTVFB_WARN("ivtvfb_prep_frame: Overflowing the framebuffer %ld, only %d available\n",
337 dest_offset
+ count
, oi
->video_buffer_size
);
341 /* Not fatal, but will have undesirable results */
342 if ((unsigned long)source
& 3)
343 IVTVFB_WARN("ivtvfb_prep_frame: Source address not 32 bit aligned (%p)\n",
347 IVTVFB_WARN("ivtvfb_prep_frame: Dest offset not 32 bit aligned (%ld)\n", dest_offset
);
350 IVTVFB_WARN("ivtvfb_prep_frame: Count not a multiple of 4 (%d)\n", count
);
353 if (!access_ok(source
+ dest_offset
, count
)) {
354 IVTVFB_WARN("Invalid userspace pointer %p\n", source
);
356 IVTVFB_DEBUG_WARN("access_ok() failed for offset 0x%08lx source %p count %d\n",
357 dest_offset
, source
, count
);
361 /* OSD Address to send DMA to */
362 dest_offset
+= IVTV_DECODER_OFFSET
+ oi
->video_rbase
;
365 return ivtvfb_prep_dec_dma_to_device(itv
, dest_offset
, source
, count
);
368 static ssize_t
ivtvfb_write(struct fb_info
*info
, const char __user
*buf
,
369 size_t count
, loff_t
*ppos
)
371 unsigned long p
= *ppos
;
375 unsigned long total_size
;
376 struct ivtv
*itv
= (struct ivtv
*) info
->par
;
377 unsigned long dma_offset
=
378 IVTV_DECODER_OFFSET
+ itv
->osd_info
->video_rbase
;
379 unsigned long dma_size
;
380 u16 lead
= 0, tail
= 0;
382 if (!info
->screen_base
)
385 total_size
= info
->screen_size
;
388 total_size
= info
->fix
.smem_len
;
393 if (count
> total_size
) {
398 if (count
+ p
> total_size
) {
401 count
= total_size
- p
;
404 dst
= (void __force
*) (info
->screen_base
+ p
);
406 if (info
->fbops
->fb_sync
)
407 info
->fbops
->fb_sync(info
);
409 /* If transfer size > threshold and both src/dst
410 addresses are aligned, use DMA */
412 ((unsigned long)buf
& 3) == ((unsigned long)dst
& 3)) {
413 /* Odd address = can't DMA. Align */
414 if ((unsigned long)dst
& 3) {
415 lead
= 4 - ((unsigned long)dst
& 3);
416 if (copy_from_user(dst
, buf
, lead
))
421 /* DMA resolution is 32 bits */
422 if ((count
- lead
) & 3)
423 tail
= (count
- lead
) & 3;
425 dma_size
= count
- lead
- tail
;
426 dma_err
= ivtvfb_prep_dec_dma_to_device(itv
,
427 p
+ lead
+ dma_offset
, (void __user
*)buf
, dma_size
);
432 /* Copy any leftover data */
433 if (tail
&& copy_from_user(dst
, buf
, tail
))
435 } else if (copy_from_user(dst
, buf
, count
)) {
442 return (err
) ? err
: count
;
445 static int ivtvfb_ioctl(struct fb_info
*info
, unsigned int cmd
, unsigned long arg
)
448 struct ivtv
*itv
= (struct ivtv
*)info
->par
;
452 case FBIOGET_VBLANK
: {
453 struct fb_vblank vblank
;
456 memset(&vblank
, 0, sizeof(struct fb_vblank
));
458 vblank
.flags
= FB_VBLANK_HAVE_COUNT
|FB_VBLANK_HAVE_VCOUNT
|
459 FB_VBLANK_HAVE_VSYNC
;
460 trace
= read_reg(IVTV_REG_DEC_LINE_FIELD
) >> 16;
461 if (itv
->is_out_50hz
&& trace
> 312)
463 else if (itv
->is_out_60hz
&& trace
> 262)
466 vblank
.flags
|= FB_VBLANK_VSYNCING
;
467 vblank
.count
= itv
->last_vsync_field
;
468 vblank
.vcount
= trace
;
470 if (copy_to_user((void __user
*)arg
, &vblank
, sizeof(vblank
)))
475 case FBIO_WAITFORVSYNC
:
476 prepare_to_wait(&itv
->vsync_waitq
, &wait
, TASK_INTERRUPTIBLE
);
477 if (!schedule_timeout(msecs_to_jiffies(50)))
479 finish_wait(&itv
->vsync_waitq
, &wait
);
482 case IVTVFB_IOC_DMA_FRAME
: {
483 struct ivtvfb_dma_frame args
;
485 IVTVFB_DEBUG_INFO("IVTVFB_IOC_DMA_FRAME\n");
486 if (copy_from_user(&args
, (void __user
*)arg
, sizeof(args
)))
489 return ivtvfb_prep_frame(itv
, cmd
, args
.source
, args
.dest_offset
, args
.count
);
493 IVTVFB_DEBUG_INFO("Unknown ioctl %08x\n", cmd
);
499 /* Framebuffer device handling */
501 static int ivtvfb_set_var(struct ivtv
*itv
, struct fb_var_screeninfo
*var
)
503 struct osd_info
*oi
= itv
->osd_info
;
504 struct ivtv_osd_coords ivtv_osd
;
505 struct v4l2_rect ivtv_window
;
508 IVTVFB_DEBUG_INFO("ivtvfb_set_var\n");
510 /* Select color space */
511 if (var
->nonstd
) /* YUV */
512 write_reg(read_reg(0x02a00) | 0x0002000, 0x02a00);
514 write_reg(read_reg(0x02a00) & ~0x0002000, 0x02a00);
516 /* Set the color mode */
517 switch (var
->bits_per_pixel
) {
519 osd_mode
= IVTV_OSD_BPP_8
;
522 osd_mode
= IVTV_OSD_BPP_32
;
525 switch (var
->green
.length
) {
527 osd_mode
= IVTV_OSD_BPP_16_444
;
530 osd_mode
= IVTV_OSD_BPP_16_555
;
533 osd_mode
= IVTV_OSD_BPP_16_565
;
536 IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
540 IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
543 /* Set video mode. Although rare, the display can become scrambled even
544 if we don't change mode. Always 'bounce' to osd_mode via mode 0 */
545 if (osd_mode
!= -1) {
546 ivtv_vapi(itv
, CX2341X_OSD_SET_PIXEL_FORMAT
, 1, 0);
547 ivtv_vapi(itv
, CX2341X_OSD_SET_PIXEL_FORMAT
, 1, osd_mode
);
550 oi
->bits_per_pixel
= var
->bits_per_pixel
;
551 oi
->bytes_per_pixel
= var
->bits_per_pixel
/ 8;
553 /* Set the flicker filter */
554 switch (var
->vmode
& FB_VMODE_MASK
) {
555 case FB_VMODE_NONINTERLACED
: /* Filter on */
556 ivtv_vapi(itv
, CX2341X_OSD_SET_FLICKER_STATE
, 1, 1);
558 case FB_VMODE_INTERLACED
: /* Filter off */
559 ivtv_vapi(itv
, CX2341X_OSD_SET_FLICKER_STATE
, 1, 0);
562 IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid video mode\n");
565 /* Read the current osd info */
566 ivtvfb_get_osd_coords(itv
, &ivtv_osd
);
568 /* Now set the OSD to the size we want */
569 ivtv_osd
.pixel_stride
= var
->xres_virtual
;
570 ivtv_osd
.lines
= var
->yres_virtual
;
573 ivtvfb_set_osd_coords(itv
, &ivtv_osd
);
575 /* Can't seem to find the right API combo for this.
576 Use another function which does what we need through direct register access. */
577 ivtv_window
.width
= var
->xres
;
578 ivtv_window
.height
= var
->yres
;
580 /* Minimum margin cannot be 0, as X won't allow such a mode */
581 if (!var
->upper_margin
)
583 if (!var
->left_margin
)
585 ivtv_window
.top
= var
->upper_margin
- 1;
586 ivtv_window
.left
= var
->left_margin
- 1;
588 ivtvfb_set_display_window(itv
, &ivtv_window
);
590 /* Pass screen size back to yuv handler */
591 itv
->yuv_info
.osd_full_w
= ivtv_osd
.pixel_stride
;
592 itv
->yuv_info
.osd_full_h
= ivtv_osd
.lines
;
594 /* Force update of yuv registers */
595 itv
->yuv_info
.yuv_forced_update
= 1;
597 /* Keep a copy of these settings */
598 memcpy(&oi
->fbvar_cur
, var
, sizeof(oi
->fbvar_cur
));
600 IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
601 var
->xres
, var
->yres
,
602 var
->xres_virtual
, var
->yres_virtual
,
603 var
->bits_per_pixel
);
605 IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
606 var
->left_margin
, var
->upper_margin
);
608 IVTVFB_DEBUG_INFO("Display filter: %s\n",
609 (var
->vmode
& FB_VMODE_MASK
) == FB_VMODE_NONINTERLACED
? "on" : "off");
610 IVTVFB_DEBUG_INFO("Color space: %s\n", var
->nonstd
? "YUV" : "RGB");
615 static int ivtvfb_get_fix(struct ivtv
*itv
, struct fb_fix_screeninfo
*fix
)
617 struct osd_info
*oi
= itv
->osd_info
;
619 IVTVFB_DEBUG_INFO("ivtvfb_get_fix\n");
620 memset(fix
, 0, sizeof(struct fb_fix_screeninfo
));
621 strscpy(fix
->id
, "cx23415 TV out", sizeof(fix
->id
));
622 fix
->smem_start
= oi
->video_pbase
;
623 fix
->smem_len
= oi
->video_buffer_size
;
624 fix
->type
= FB_TYPE_PACKED_PIXELS
;
625 fix
->visual
= (oi
->bits_per_pixel
== 8) ? FB_VISUAL_PSEUDOCOLOR
: FB_VISUAL_TRUECOLOR
;
629 fix
->line_length
= oi
->display_byte_stride
;
630 fix
->accel
= FB_ACCEL_NONE
;
634 /* Check the requested display mode, returning -EINVAL if we can't
637 static int _ivtvfb_check_var(struct fb_var_screeninfo
*var
, struct ivtv
*itv
)
639 struct osd_info
*oi
= itv
->osd_info
;
640 int osd_height_limit
;
641 u32 pixclock
, hlimit
, vlimit
;
643 IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
645 /* Set base references for mode calcs. */
646 if (itv
->is_out_50hz
) {
650 osd_height_limit
= 576;
656 osd_height_limit
= 480;
659 if (var
->bits_per_pixel
== 8 || var
->bits_per_pixel
== 32) {
660 var
->transp
.offset
= 24;
661 var
->transp
.length
= 8;
662 var
->red
.offset
= 16;
664 var
->green
.offset
= 8;
665 var
->green
.length
= 8;
666 var
->blue
.offset
= 0;
667 var
->blue
.length
= 8;
669 else if (var
->bits_per_pixel
== 16) {
670 /* To find out the true mode, check green length */
671 switch (var
->green
.length
) {
675 var
->green
.offset
= 4;
676 var
->green
.length
= 4;
677 var
->blue
.offset
= 0;
678 var
->blue
.length
= 4;
679 var
->transp
.offset
= 12;
680 var
->transp
.length
= 1;
683 var
->red
.offset
= 10;
685 var
->green
.offset
= 5;
686 var
->green
.length
= 5;
687 var
->blue
.offset
= 0;
688 var
->blue
.length
= 5;
689 var
->transp
.offset
= 15;
690 var
->transp
.length
= 1;
693 var
->red
.offset
= 11;
695 var
->green
.offset
= 5;
696 var
->green
.length
= 6;
697 var
->blue
.offset
= 0;
698 var
->blue
.length
= 5;
699 var
->transp
.offset
= 0;
700 var
->transp
.length
= 0;
705 IVTVFB_DEBUG_WARN("Invalid colour mode: %d\n", var
->bits_per_pixel
);
709 /* Check the resolution */
710 if (var
->xres
> IVTV_OSD_MAX_WIDTH
|| var
->yres
> osd_height_limit
) {
711 IVTVFB_DEBUG_WARN("Invalid resolution: %dx%d\n",
712 var
->xres
, var
->yres
);
716 /* Max horizontal size is 1023 @ 32bpp, 2046 & 16bpp, 4092 @ 8bpp */
717 if (var
->xres_virtual
> 4095 / (var
->bits_per_pixel
/ 8) ||
718 var
->xres_virtual
* var
->yres_virtual
* (var
->bits_per_pixel
/ 8) > oi
->video_buffer_size
||
719 var
->xres_virtual
< var
->xres
||
720 var
->yres_virtual
< var
->yres
) {
721 IVTVFB_DEBUG_WARN("Invalid virtual resolution: %dx%d\n",
722 var
->xres_virtual
, var
->yres_virtual
);
726 /* Some extra checks if in 8 bit mode */
727 if (var
->bits_per_pixel
== 8) {
728 /* Width must be a multiple of 4 */
730 IVTVFB_DEBUG_WARN("Invalid resolution for 8bpp: %d\n", var
->xres
);
733 if (var
->xres_virtual
& 3) {
734 IVTVFB_DEBUG_WARN("Invalid virtual resolution for 8bpp: %d)\n", var
->xres_virtual
);
738 else if (var
->bits_per_pixel
== 16) {
739 /* Width must be a multiple of 2 */
741 IVTVFB_DEBUG_WARN("Invalid resolution for 16bpp: %d\n", var
->xres
);
744 if (var
->xres_virtual
& 1) {
745 IVTVFB_DEBUG_WARN("Invalid virtual resolution for 16bpp: %d)\n", var
->xres_virtual
);
750 /* Now check the offsets */
751 if (var
->xoffset
>= var
->xres_virtual
|| var
->yoffset
>= var
->yres_virtual
) {
752 IVTVFB_DEBUG_WARN("Invalid offset: %d (%d) %d (%d)\n",
753 var
->xoffset
, var
->xres_virtual
, var
->yoffset
, var
->yres_virtual
);
757 /* Check pixel format */
758 if (var
->nonstd
> 1) {
759 IVTVFB_DEBUG_WARN("Invalid nonstd % d\n", var
->nonstd
);
763 /* Check video mode */
764 if (((var
->vmode
& FB_VMODE_MASK
) != FB_VMODE_NONINTERLACED
) &&
765 ((var
->vmode
& FB_VMODE_MASK
) != FB_VMODE_INTERLACED
)) {
766 IVTVFB_DEBUG_WARN("Invalid video mode: %d\n", var
->vmode
& FB_VMODE_MASK
);
770 /* Check the left & upper margins
771 If the margins are too large, just center the screen
772 (enforcing margins causes too many problems) */
774 if (var
->left_margin
+ var
->xres
> IVTV_OSD_MAX_WIDTH
+ 1)
775 var
->left_margin
= 1 + ((IVTV_OSD_MAX_WIDTH
- var
->xres
) / 2);
777 if (var
->upper_margin
+ var
->yres
> (itv
->is_out_50hz
? 577 : 481))
778 var
->upper_margin
= 1 + (((itv
->is_out_50hz
? 576 : 480) -
781 /* Maintain overall 'size' for a constant refresh rate */
782 var
->right_margin
= hlimit
- var
->left_margin
- var
->xres
;
783 var
->lower_margin
= vlimit
- var
->upper_margin
- var
->yres
;
785 /* Fixed sync times */
789 /* Non-interlaced / interlaced mode is used to switch the OSD filter
790 on or off. Adjust the clock timings to maintain a constant
791 vertical refresh rate. */
792 if ((var
->vmode
& FB_VMODE_MASK
) == FB_VMODE_NONINTERLACED
)
793 var
->pixclock
= pixclock
/ 2;
795 var
->pixclock
= pixclock
;
797 itv
->osd_rect
.width
= var
->xres
;
798 itv
->osd_rect
.height
= var
->yres
;
800 IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
801 var
->xres
, var
->yres
,
802 var
->xres_virtual
, var
->yres_virtual
,
803 var
->bits_per_pixel
);
805 IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
806 var
->left_margin
, var
->upper_margin
);
808 IVTVFB_DEBUG_INFO("Display filter: %s\n",
809 (var
->vmode
& FB_VMODE_MASK
) == FB_VMODE_NONINTERLACED
? "on" : "off");
810 IVTVFB_DEBUG_INFO("Color space: %s\n", var
->nonstd
? "YUV" : "RGB");
814 static int ivtvfb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
816 struct ivtv
*itv
= (struct ivtv
*) info
->par
;
817 IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
818 return _ivtvfb_check_var(var
, itv
);
821 static int ivtvfb_pan_display(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
824 struct ivtv
*itv
= (struct ivtv
*) info
->par
;
826 if (var
->yoffset
+ info
->var
.yres
> info
->var
.yres_virtual
||
827 var
->xoffset
+ info
->var
.xres
> info
->var
.xres_virtual
)
830 osd_pan_index
= var
->yoffset
* info
->fix
.line_length
831 + var
->xoffset
* info
->var
.bits_per_pixel
/ 8;
832 write_reg(osd_pan_index
, 0x02A0C);
834 /* Pass this info back the yuv handler */
835 itv
->yuv_info
.osd_x_pan
= var
->xoffset
;
836 itv
->yuv_info
.osd_y_pan
= var
->yoffset
;
837 /* Force update of yuv registers */
838 itv
->yuv_info
.yuv_forced_update
= 1;
839 /* Remember this value */
840 itv
->osd_info
->pan_cur
= osd_pan_index
;
844 static int ivtvfb_set_par(struct fb_info
*info
)
847 struct ivtv
*itv
= (struct ivtv
*) info
->par
;
849 IVTVFB_DEBUG_INFO("ivtvfb_set_par\n");
851 rc
= ivtvfb_set_var(itv
, &info
->var
);
852 ivtvfb_pan_display(&info
->var
, info
);
853 ivtvfb_get_fix(itv
, &info
->fix
);
854 ivtv_firmware_check(itv
, "ivtvfb_set_par");
858 static int ivtvfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
859 unsigned blue
, unsigned transp
,
860 struct fb_info
*info
)
863 struct ivtv
*itv
= (struct ivtv
*)info
->par
;
865 if (regno
>= info
->cmap
.len
)
868 color
= ((transp
& 0xFF00) << 16) |((red
& 0xFF00) << 8) | (green
& 0xFF00) | ((blue
& 0xFF00) >> 8);
869 if (info
->var
.bits_per_pixel
<= 8) {
870 write_reg(regno
, 0x02a30);
871 write_reg(color
, 0x02a34);
872 itv
->osd_info
->palette_cur
[regno
] = color
;
878 palette
= info
->pseudo_palette
;
879 if (info
->var
.bits_per_pixel
== 16) {
880 switch (info
->var
.green
.length
) {
882 color
= ((red
& 0xf000) >> 4) |
883 ((green
& 0xf000) >> 8) |
884 ((blue
& 0xf000) >> 12);
887 color
= ((red
& 0xf800) >> 1) |
888 ((green
& 0xf800) >> 6) |
889 ((blue
& 0xf800) >> 11);
892 color
= (red
& 0xf800 ) |
893 ((green
& 0xfc00) >> 5) |
894 ((blue
& 0xf800) >> 11);
898 palette
[regno
] = color
;
902 /* We don't really support blanking. All this does is enable or
904 static int ivtvfb_blank(int blank_mode
, struct fb_info
*info
)
906 struct ivtv
*itv
= (struct ivtv
*)info
->par
;
908 IVTVFB_DEBUG_INFO("Set blanking mode : %d\n", blank_mode
);
909 switch (blank_mode
) {
910 case FB_BLANK_UNBLANK
:
911 ivtv_vapi(itv
, CX2341X_OSD_SET_STATE
, 1, 1);
912 ivtv_call_hw(itv
, IVTV_HW_SAA7127
, video
, s_stream
, 1);
914 case FB_BLANK_NORMAL
:
915 case FB_BLANK_HSYNC_SUSPEND
:
916 case FB_BLANK_VSYNC_SUSPEND
:
917 ivtv_vapi(itv
, CX2341X_OSD_SET_STATE
, 1, 0);
918 ivtv_call_hw(itv
, IVTV_HW_SAA7127
, video
, s_stream
, 1);
920 case FB_BLANK_POWERDOWN
:
921 ivtv_call_hw(itv
, IVTV_HW_SAA7127
, video
, s_stream
, 0);
922 ivtv_vapi(itv
, CX2341X_OSD_SET_STATE
, 1, 0);
925 itv
->osd_info
->blank_cur
= blank_mode
;
929 static const struct fb_ops ivtvfb_ops
= {
930 .owner
= THIS_MODULE
,
931 .fb_read
= fb_io_read
,
932 .fb_write
= ivtvfb_write
,
933 .fb_check_var
= ivtvfb_check_var
,
934 .fb_set_par
= ivtvfb_set_par
,
935 .fb_setcolreg
= ivtvfb_setcolreg
,
936 __FB_DEFAULT_IOMEM_OPS_DRAW
,
938 .fb_ioctl
= ivtvfb_ioctl
,
939 .fb_pan_display
= ivtvfb_pan_display
,
940 .fb_blank
= ivtvfb_blank
,
941 __FB_DEFAULT_IOMEM_OPS_MMAP
,
944 /* Restore hardware after firmware restart */
945 static void ivtvfb_restore(struct ivtv
*itv
)
947 struct osd_info
*oi
= itv
->osd_info
;
950 ivtvfb_set_var(itv
, &oi
->fbvar_cur
);
951 ivtvfb_blank(oi
->blank_cur
, &oi
->ivtvfb_info
);
952 for (i
= 0; i
< 256; i
++) {
953 write_reg(i
, 0x02a30);
954 write_reg(oi
->palette_cur
[i
], 0x02a34);
956 write_reg(oi
->pan_cur
, 0x02a0c);
962 /* Setup our initial video mode */
963 static int ivtvfb_init_vidmode(struct ivtv
*itv
)
965 struct osd_info
*oi
= itv
->osd_info
;
966 struct v4l2_rect start_window
;
971 if (osd_depth
!= 8 && osd_depth
!= 16 && osd_depth
!= 32)
973 oi
->bits_per_pixel
= osd_depth
;
974 oi
->bytes_per_pixel
= oi
->bits_per_pixel
/ 8;
976 /* Horizontal size & position */
981 /* Must be a multiple of 4 for 8bpp & 2 for 16bpp */
984 else if (osd_depth
== 16)
987 start_window
.width
= osd_xres
? osd_xres
: 640;
989 /* Check horizontal start (osd_left). */
990 if (osd_left
&& osd_left
+ start_window
.width
> 721) {
991 IVTVFB_ERR("Invalid osd_left - assuming default\n");
995 /* Hardware coords start at 0, user coords start at 1. */
998 start_window
.left
= osd_left
>= 0 ?
999 osd_left
: ((IVTV_OSD_MAX_WIDTH
- start_window
.width
) / 2);
1001 oi
->display_byte_stride
=
1002 start_window
.width
* oi
->bytes_per_pixel
;
1004 /* Vertical size & position */
1006 max_height
= itv
->is_out_50hz
? 576 : 480;
1008 if (osd_yres
> max_height
)
1009 osd_yres
= max_height
;
1011 start_window
.height
= osd_yres
?
1012 osd_yres
: itv
->is_out_50hz
? 480 : 400;
1014 /* Check vertical start (osd_upper). */
1015 if (osd_upper
+ start_window
.height
> max_height
+ 1) {
1016 IVTVFB_ERR("Invalid osd_upper - assuming default\n");
1020 /* Hardware coords start at 0, user coords start at 1. */
1023 start_window
.top
= osd_upper
>= 0 ? osd_upper
: ((max_height
- start_window
.height
) / 2);
1025 oi
->display_width
= start_window
.width
;
1026 oi
->display_height
= start_window
.height
;
1028 /* Generate a valid fb_var_screeninfo */
1030 oi
->ivtvfb_defined
.xres
= oi
->display_width
;
1031 oi
->ivtvfb_defined
.yres
= oi
->display_height
;
1032 oi
->ivtvfb_defined
.xres_virtual
= oi
->display_width
;
1033 oi
->ivtvfb_defined
.yres_virtual
= oi
->display_height
;
1034 oi
->ivtvfb_defined
.bits_per_pixel
= oi
->bits_per_pixel
;
1035 oi
->ivtvfb_defined
.vmode
= (osd_laced
? FB_VMODE_INTERLACED
: FB_VMODE_NONINTERLACED
);
1036 oi
->ivtvfb_defined
.left_margin
= start_window
.left
+ 1;
1037 oi
->ivtvfb_defined
.upper_margin
= start_window
.top
+ 1;
1038 oi
->ivtvfb_defined
.accel_flags
= FB_ACCEL_NONE
;
1039 oi
->ivtvfb_defined
.nonstd
= 0;
1041 /* We've filled in the most data, let the usual mode check
1042 routine fill in the rest. */
1043 _ivtvfb_check_var(&oi
->ivtvfb_defined
, itv
);
1045 /* Generate valid fb_fix_screeninfo */
1047 ivtvfb_get_fix(itv
, &oi
->ivtvfb_fix
);
1049 /* Generate valid fb_info */
1051 oi
->ivtvfb_info
.node
= -1;
1052 oi
->ivtvfb_info
.par
= itv
;
1053 oi
->ivtvfb_info
.var
= oi
->ivtvfb_defined
;
1054 oi
->ivtvfb_info
.fix
= oi
->ivtvfb_fix
;
1055 oi
->ivtvfb_info
.screen_base
= (u8 __iomem
*)oi
->video_vbase
;
1056 oi
->ivtvfb_info
.fbops
= &ivtvfb_ops
;
1058 /* Supply some monitor specs. Bogus values will do for now */
1059 oi
->ivtvfb_info
.monspecs
.hfmin
= 8000;
1060 oi
->ivtvfb_info
.monspecs
.hfmax
= 70000;
1061 oi
->ivtvfb_info
.monspecs
.vfmin
= 10;
1062 oi
->ivtvfb_info
.monspecs
.vfmax
= 100;
1064 /* Allocate color map */
1065 if (fb_alloc_cmap(&oi
->ivtvfb_info
.cmap
, 256, 1)) {
1066 IVTVFB_ERR("abort, unable to alloc cmap\n");
1070 /* Allocate the pseudo palette */
1071 oi
->ivtvfb_info
.pseudo_palette
=
1072 kmalloc_array(16, sizeof(u32
), GFP_KERNEL
|__GFP_NOWARN
);
1074 if (!oi
->ivtvfb_info
.pseudo_palette
) {
1075 IVTVFB_ERR("abort, unable to alloc pseudo palette\n");
1082 /* Find OSD buffer base & size. Add to mtrr. Zero osd buffer. */
1084 static int ivtvfb_init_io(struct ivtv
*itv
)
1086 struct osd_info
*oi
= itv
->osd_info
;
1087 /* Find the largest power of two that maps the whole buffer */
1088 int size_shift
= 31;
1090 mutex_lock(&itv
->serialize_lock
);
1091 if (ivtv_init_on_first_open(itv
)) {
1092 mutex_unlock(&itv
->serialize_lock
);
1093 IVTVFB_ERR("Failed to initialize ivtv\n");
1096 mutex_unlock(&itv
->serialize_lock
);
1098 if (ivtvfb_get_framebuffer(itv
, &oi
->video_rbase
,
1099 &oi
->video_buffer_size
) < 0) {
1100 IVTVFB_ERR("Firmware failed to respond\n");
1104 /* The osd buffer size depends on the number of video buffers allocated
1105 on the PVR350 itself. For now we'll hardcode the smallest osd buffer
1106 size to prevent any overlap. */
1107 oi
->video_buffer_size
= 1704960;
1109 oi
->video_pbase
= itv
->base_addr
+ IVTV_DECODER_OFFSET
+ oi
->video_rbase
;
1110 oi
->video_vbase
= itv
->dec_mem
+ oi
->video_rbase
;
1112 if (!oi
->video_vbase
) {
1113 IVTVFB_ERR("abort, video memory 0x%x @ 0x%lx isn't mapped!\n",
1114 oi
->video_buffer_size
, oi
->video_pbase
);
1118 IVTVFB_INFO("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
1119 oi
->video_pbase
, oi
->video_vbase
,
1120 oi
->video_buffer_size
/ 1024);
1122 while (!(oi
->video_buffer_size
& (1 << size_shift
)))
1125 oi
->fb_start_aligned_physaddr
= oi
->video_pbase
& ~((1 << size_shift
) - 1);
1126 oi
->fb_end_aligned_physaddr
= oi
->video_pbase
+ oi
->video_buffer_size
;
1127 oi
->fb_end_aligned_physaddr
+= (1 << size_shift
) - 1;
1128 oi
->fb_end_aligned_physaddr
&= ~((1 << size_shift
) - 1);
1129 oi
->wc_cookie
= arch_phys_wc_add(oi
->fb_start_aligned_physaddr
,
1130 oi
->fb_end_aligned_physaddr
-
1131 oi
->fb_start_aligned_physaddr
);
1132 /* Blank the entire osd. */
1133 memset_io(oi
->video_vbase
, 0, oi
->video_buffer_size
);
1138 /* Release any memory we've grabbed & remove mtrr entry */
1139 static void ivtvfb_release_buffers (struct ivtv
*itv
)
1141 struct osd_info
*oi
= itv
->osd_info
;
1144 if (oi
->ivtvfb_info
.cmap
.len
)
1145 fb_dealloc_cmap(&oi
->ivtvfb_info
.cmap
);
1147 /* Release pseudo palette */
1148 kfree(oi
->ivtvfb_info
.pseudo_palette
);
1149 arch_phys_wc_del(oi
->wc_cookie
);
1151 itv
->osd_info
= NULL
;
1154 /* Initialize the specified card */
1156 static int ivtvfb_init_card(struct ivtv
*itv
)
1160 #if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
1161 if (pat_enabled()) {
1162 if (ivtvfb_force_pat
) {
1163 pr_info("PAT is enabled. Write-combined framebuffer caching will be disabled.\n");
1164 pr_info("To enable caching, boot with nopat kernel parameter\n");
1166 pr_warn("ivtvfb needs PAT disabled for write-combined framebuffer caching.\n");
1167 pr_warn("Boot with nopat kernel parameter to use caching, or use the\n");
1168 pr_warn("force_pat module parameter to run with caching disabled\n");
1174 if (itv
->osd_info
) {
1175 IVTVFB_ERR("Card %d already initialised\n", ivtvfb_card_id
);
1179 itv
->osd_info
= kzalloc(sizeof(struct osd_info
),
1180 GFP_KERNEL
|__GFP_NOWARN
);
1181 if (itv
->osd_info
== NULL
) {
1182 IVTVFB_ERR("Failed to allocate memory for osd_info\n");
1186 /* Find & setup the OSD buffer */
1187 rc
= ivtvfb_init_io(itv
);
1189 ivtvfb_release_buffers(itv
);
1193 /* Set the startup video mode information */
1194 if ((rc
= ivtvfb_init_vidmode(itv
))) {
1195 ivtvfb_release_buffers(itv
);
1199 /* Register the framebuffer */
1200 if (register_framebuffer(&itv
->osd_info
->ivtvfb_info
) < 0) {
1201 ivtvfb_release_buffers(itv
);
1205 itv
->osd_video_pbase
= itv
->osd_info
->video_pbase
;
1207 /* Set the card to the requested mode */
1208 ivtvfb_set_par(&itv
->osd_info
->ivtvfb_info
);
1210 /* Set color 0 to black */
1211 write_reg(0, 0x02a30);
1212 write_reg(0, 0x02a34);
1214 /* Enable the osd */
1215 ivtvfb_blank(FB_BLANK_UNBLANK
, &itv
->osd_info
->ivtvfb_info
);
1217 /* Enable restart */
1218 itv
->ivtvfb_restore
= ivtvfb_restore
;
1221 ivtv_udma_alloc(itv
);
1222 itv
->streams
[IVTV_DEC_STREAM_TYPE_YUV
].vdev
.device_caps
|=
1223 V4L2_CAP_VIDEO_OUTPUT_OVERLAY
;
1224 itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
].vdev
.device_caps
|=
1225 V4L2_CAP_VIDEO_OUTPUT_OVERLAY
;
1226 itv
->v4l2_cap
|= V4L2_CAP_VIDEO_OUTPUT_OVERLAY
;
1231 static int __init
ivtvfb_callback_init(struct device
*dev
, void *p
)
1233 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
1234 struct ivtv
*itv
= container_of(v4l2_dev
, struct ivtv
, v4l2_dev
);
1236 if (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
) {
1237 if (ivtvfb_init_card(itv
) == 0) {
1238 IVTVFB_INFO("Framebuffer registered on %s\n",
1239 itv
->v4l2_dev
.name
);
1246 static int ivtvfb_callback_cleanup(struct device
*dev
, void *p
)
1248 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
1249 struct ivtv
*itv
= container_of(v4l2_dev
, struct ivtv
, v4l2_dev
);
1250 struct osd_info
*oi
= itv
->osd_info
;
1252 if (itv
->v4l2_cap
& V4L2_CAP_VIDEO_OUTPUT
) {
1253 itv
->streams
[IVTV_DEC_STREAM_TYPE_YUV
].vdev
.device_caps
&=
1254 ~V4L2_CAP_VIDEO_OUTPUT_OVERLAY
;
1255 itv
->streams
[IVTV_DEC_STREAM_TYPE_MPG
].vdev
.device_caps
&=
1256 ~V4L2_CAP_VIDEO_OUTPUT_OVERLAY
;
1257 itv
->v4l2_cap
&= ~V4L2_CAP_VIDEO_OUTPUT_OVERLAY
;
1258 unregister_framebuffer(&itv
->osd_info
->ivtvfb_info
);
1259 IVTVFB_INFO("Unregister framebuffer %d\n", itv
->instance
);
1260 itv
->ivtvfb_restore
= NULL
;
1261 ivtvfb_blank(FB_BLANK_VSYNC_SUSPEND
, &oi
->ivtvfb_info
);
1262 ivtvfb_release_buffers(itv
);
1263 itv
->osd_video_pbase
= 0;
1268 static int __init
ivtvfb_init(void)
1270 struct device_driver
*drv
;
1275 if (ivtvfb_card_id
< -1 || ivtvfb_card_id
>= IVTV_MAX_CARDS
) {
1276 pr_err("ivtvfb_card_id parameter is out of range (valid range: -1 - %d)\n",
1277 IVTV_MAX_CARDS
- 1);
1281 drv
= driver_find("ivtv", &pci_bus_type
);
1282 err
= driver_for_each_device(drv
, NULL
, ®istered
, ivtvfb_callback_init
);
1283 (void)err
; /* suppress compiler warning */
1285 pr_err("no cards found\n");
1291 static void ivtvfb_cleanup(void)
1293 struct device_driver
*drv
;
1296 pr_info("Unloading framebuffer module\n");
1298 drv
= driver_find("ivtv", &pci_bus_type
);
1299 err
= driver_for_each_device(drv
, NULL
, NULL
, ivtvfb_callback_cleanup
);
1300 (void)err
; /* suppress compiler warning */
1303 module_init(ivtvfb_init
);
1304 module_exit(ivtvfb_cleanup
);