29 #include <linux/module.h>
31 #include <linux/slab.h>
34 #include <linux/device.h>
48 static unsigned int mpegbufs = 32;
52 static unsigned int debug;
56 #define dprintk(level,fmt, arg...) if (debug >= level) \
57 printk(KERN_DEBUG "%s/2-bb: " fmt, dev->core->name , ## arg)
62 #define BLACKBIRD_FIRM_IMAGE_SIZE 376836
66 #define IVTV_CMD_HW_BLOCKS_RST 0xFFFFFFFF
69 #define IVTV_API_STD_TIMEOUT 500
175 #define IVTV_REG_ENC_SDRAM_REFRESH (0x07F8 )
176 #define IVTV_REG_ENC_SDRAM_PRECHARGE (0x07FC )
177 #define IVTV_REG_SPU (0x9050 )
178 #define IVTV_REG_HW_BLOCKS (0x9054 )
179 #define IVTV_REG_VPU (0x9058 )
180 #define IVTV_REG_APU (0xA064 )
203 #define P1_MDATA0 0x390000
204 #define P1_MDATA1 0x390001
205 #define P1_MDATA2 0x390002
206 #define P1_MDATA3 0x390003
207 #define P1_MADDR2 0x390004
208 #define P1_MADDR1 0x390005
209 #define P1_MADDR0 0x390006
210 #define P1_RDATA0 0x390008
211 #define P1_RDATA1 0x390009
212 #define P1_RDATA2 0x39000A
213 #define P1_RDATA3 0x39000B
214 #define P1_RADDR0 0x39000C
215 #define P1_RADDR1 0x39000D
216 #define P1_RRDWR 0x39000E
223 need = state ? 2 : 0;
247 return wait_ready_gpio0_bit1(core,1);
250 static int memory_read(
struct cx88_core *core,
u32 address,
u32 *value)
261 retval = wait_ready_gpio0_bit1(core,1);
288 return wait_ready_gpio0_bit1(core,1);
292 static int register_read(
struct cx88_core *core,
u32 address,
u32 *value)
302 retval = wait_ready_gpio0_bit1(core,1);
321 dprintk(1,
"%s: 0x%X\n", __func__, command);
325 memory_read(dev->
core, dev->mailbox - 4, &value);
326 if (value != 0x12345678) {
327 dprintk(0,
"Firmware and/or mailbox pointer not initialized or corrupted\n");
331 memory_read(dev->
core, dev->mailbox, &flag);
333 dprintk(0,
"ERROR: Mailbox appears to be in use (%x)\n", flag);
338 memory_write(dev->
core, dev->mailbox, flag);
341 memory_write(dev->
core, dev->mailbox + 1, command);
343 for (i = 0; i <
in; i++) {
344 memory_write(dev->
core, dev->mailbox + 4 + i,
data[i]);
348 memory_write(dev->
core, dev->mailbox + 4 + i, 0);
351 memory_write(dev->
core, dev->mailbox, flag);
356 memory_read(dev->
core, dev->mailbox, &flag);
360 dprintk(0,
"ERROR: API Mailbox timeout\n");
367 for (i = 0; i <
out; i++) {
368 memory_read(dev->
core, dev->mailbox + 4 + i,
data + i);
372 memory_read(dev->
core, dev->mailbox + 2, &retval);
373 dprintk(1,
"API result = %d\n",retval);
376 memory_write(dev->
core, dev->mailbox, flag);
382 static int blackbird_api_cmd(
struct cx8802_dev *dev,
u32 command,
383 u32 inputcnt,
u32 outputcnt, ...)
391 for (i = 0; i < inputcnt; i++) {
394 err = blackbird_mbox_func(dev, command, inputcnt, outputcnt, data);
395 for (i = 0; i < outputcnt; i++) {
396 int *vptr =
va_arg(vargs,
int *);
403 static int blackbird_find_mailbox(
struct cx8802_dev *dev)
405 u32 signature[4]={0x12345678, 0x34567812, 0x56781234, 0x78123456};
411 memory_read(dev->
core, i, &value);
412 if (value == signature[signaturecnt])
416 if (4 == signaturecnt) {
417 dprintk(1,
"Mailbox signature found\n");
421 dprintk(0,
"Mailbox signature values not found!\n");
425 static int blackbird_load_firmware(
struct cx8802_dev *dev)
427 static const unsigned char magic[8] = {
428 0xa7, 0x0d, 0x00, 0x00, 0x66, 0xbb, 0x55, 0xaa
444 dprintk(0,
"Error with register_write\n");
451 dprintk(0,
"ERROR: Hotplug firmware request failed (%s).\n",
453 dprintk(0,
"Please fix your hotplug setup, the board will "
454 "not work without firmware loaded!\n");
458 if (firmware->
size != BLACKBIRD_FIRM_IMAGE_SIZE) {
459 dprintk(0,
"ERROR: Firmware size mismatch (have %zd, expected %d)\n",
460 firmware->
size, BLACKBIRD_FIRM_IMAGE_SIZE);
466 dprintk(0,
"ERROR: Firmware magic mismatch, wrong file?\n");
472 dprintk(1,
"Loading firmware ...\n");
473 dataptr = (
u32*)firmware->
data;
474 for (i = 0; i < (firmware->
size >> 2); i++) {
477 memory_write(dev->
core, i, value);
482 for (i--; i >= 0; i--) {
483 memory_read(dev->
core, i, &value);
487 dprintk(0,
"ERROR: Firmware load failed (checksum mismatch).\n");
492 dprintk(0,
"Firmware upload successful.\n");
503 dprintk(0,
"Error with register_write\n");
521 static void blackbird_codec_settings(
struct cx8802_dev *dev)
525 dev->height, dev->width);
527 dev->cxhdl.width = dev->width;
528 dev->cxhdl.height = dev->height;
533 static int blackbird_initialize_codec(
struct cx8802_dev *dev)
539 dprintk(1,
"Initialize codec\n");
543 dev->mpeg_active = 0;
548 retval = blackbird_load_firmware(dev);
552 retval = blackbird_find_mailbox(dev);
560 dprintk(0,
"ERROR: Firmware ping failed!\n");
566 dprintk(0,
"ERROR: Firmware get encoder version failed!\n");
569 dprintk(0,
"Firmware version is 0x%08x\n", version);
577 blackbird_codec_settings(dev);
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
591 static int blackbird_start_codec(
struct file *
file,
void *priv)
602 for (i = 0; (i < 10) && (i < (lastchange + 4)); i++) {
605 dprintk(1,
"AUD_STATUS:%dL: 0x%x\n", i, reg);
606 if ((reg & 0x0F) != lastval) {
607 lastval = reg & 0x0F;
629 dev->mpeg_active = 1;
633 static int blackbird_stop_codec(
struct cx8802_dev *dev)
643 dev->mpeg_active = 0;
654 fh->
dev->ts_packet_size = 188 * 4;
655 fh->
dev->ts_packet_count = mpegbufs;
657 *size = fh->
dev->ts_packet_size * fh->
dev->ts_packet_count;
658 *count = fh->
dev->ts_packet_count;
684 .buf_setup = bb_buf_setup,
685 .buf_prepare = bb_buf_prepare,
686 .buf_queue = bb_buf_queue,
687 .buf_release = bb_buf_release,
692 static int vidioc_querycap(
struct file *file,
void *priv,
704 static int vidioc_enum_fmt_vid_cap (
struct file *file,
void *priv,
716 static int vidioc_g_fmt_vid_cap (
struct file *file,
void *priv,
723 f->
fmt.
pix.bytesperline = 0;
724 f->
fmt.
pix.sizeimage = 188 * 4 * mpegbufs;
726 f->
fmt.
pix.width = dev->width;
727 f->
fmt.
pix.height = dev->height;
729 dprintk(1,
"VIDIOC_G_FMT: w: %d, h: %d, f: %d\n",
730 dev->width, dev->height, fh->
mpegq.field );
734 static int vidioc_try_fmt_vid_cap (
struct file *file,
void *priv,
741 f->
fmt.
pix.bytesperline = 0;
742 f->
fmt.
pix.sizeimage = 188 * 4 * mpegbufs;
744 dprintk(1,
"VIDIOC_TRY_FMT: w: %d, h: %d, f: %d\n",
745 dev->width, dev->height, fh->
mpegq.field );
749 static int vidioc_s_fmt_vid_cap (
struct file *file,
void *priv,
757 f->
fmt.
pix.bytesperline = 0;
758 f->
fmt.
pix.sizeimage = 188 * 4 * mpegbufs;
760 dev->width = f->
fmt.
pix.width;
761 dev->height = f->
fmt.
pix.height;
766 dprintk(1,
"VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
777 static int vidioc_querybuf (
struct file *file,
void *priv,
struct v4l2_buffer *p)
783 static int vidioc_qbuf (
struct file *file,
void *priv,
struct v4l2_buffer *p)
789 static int vidioc_dqbuf (
struct file *file,
void *priv,
struct v4l2_buffer *p)
796 static int vidioc_streamon(
struct file *file,
void *priv,
enum v4l2_buf_type i)
801 if (!dev->mpeg_active)
802 blackbird_start_codec(file, fh);
806 static int vidioc_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
811 if (dev->mpeg_active)
812 blackbird_stop_codec(dev);
816 static int vidioc_s_frequency (
struct file *file,
void *priv,
827 if (dev->mpeg_active)
828 blackbird_stop_codec(dev);
831 blackbird_initialize_codec(dev);
837 static int vidioc_log_status (
struct file *file,
void *priv)
849 static int vidioc_enum_input (
struct file *file,
void *priv,
856 static int vidioc_g_frequency (
struct file *file,
void *priv,
873 static int vidioc_g_input (
struct file *file,
void *priv,
unsigned int *i)
881 static int vidioc_s_input (
struct file *file,
void *priv,
unsigned int i)
887 if (0 ==
INPUT(i).type)
897 static int vidioc_g_tuner (
struct file *file,
void *priv,
915 t->
signal = (reg & (1<<5)) ? 0xffff : 0x0000;
919 static int vidioc_s_tuner (
struct file *file,
void *priv,
941 static int vidioc_s_std (
struct file *file,
void *priv,
v4l2_std_id *
id)
953 static int mpeg_open(
struct file *file)
968 dprintk(1,
"%s: blackbird driver is not loaded\n", __func__);
975 dprintk(1,
"%s: Unable to acquire hardware, %d\n", __func__, err);
980 if (!dev->
core->mpeg_users && blackbird_initialize_codec(dev) < 0) {
985 dprintk(1,
"open dev=%s\n", video_device_node_name(vdev));
1009 dev->
core->mpeg_users++;
1015 static int mpeg_release(
struct file *file)
1023 if (dev->mpeg_active && dev->
core->mpeg_users == 1)
1024 blackbird_stop_codec(dev);
1043 dev->
core->mpeg_users--;
1051 mpeg_read(
struct file *file,
char __user *data,
size_t count, loff_t *ppos)
1056 if (!dev->mpeg_active)
1057 blackbird_start_codec(file, fh);
1066 unsigned long req_events = poll_requested_events(wait);
1071 blackbird_start_codec(file, fh);
1088 .release = mpeg_release,
1124 .ioctl_ops = &mpeg_ioctl_ops,
1131 static int cx8802_blackbird_advise_acquire(
struct cx8802_driver *drv)
1161 static int cx8802_blackbird_advise_release(
struct cx8802_driver *drv)
1176 static void blackbird_unregister_video(
struct cx8802_dev *dev)
1178 if (dev->mpeg_dev) {
1179 if (video_is_registered(dev->mpeg_dev))
1183 dev->mpeg_dev =
NULL;
1187 static int blackbird_register_video(
struct cx8802_dev *dev)
1192 &cx8802_mpeg_template,
"mpeg");
1193 dev->mpeg_dev->ctrl_handler = &dev->cxhdl.hdl;
1194 video_set_drvdata(dev->mpeg_dev, dev);
1202 dev->
core->name, video_device_node_name(dev->mpeg_dev));
1208 static int cx8802_blackbird_probe(
struct cx8802_driver *drv)
1214 dprintk( 1,
"%s\n", __func__);
1215 dprintk( 1,
" ->being probed by Card=%d Name=%s, PCI %02x:%02x\n",
1232 dev->cxhdl.width = dev->width;
1233 dev->cxhdl.height = dev->height;
1234 dev->cxhdl.func = blackbird_mbox_func;
1235 dev->cxhdl.priv =
dev;
1242 printk(
"%s/2: cx23416 based mpeg encoder (blackbird reference design)\n",
1244 host_setup(dev->
core);
1246 blackbird_initialize_codec(dev);
1254 blackbird_register_video(dev);
1262 static int cx8802_blackbird_remove(
struct cx8802_driver *drv)
1268 blackbird_unregister_video(drv->
core->dvbdev);
1277 .probe = cx8802_blackbird_probe,
1278 .remove = cx8802_blackbird_remove,
1279 .advise_acquire = cx8802_blackbird_advise_acquire,
1280 .advise_release = cx8802_blackbird_advise_release,
1283 static int __init blackbird_init(
void)
1290 static void __exit blackbird_fini(
void)