linux/drivers/media/pci/saa7134/saa7134-go7007.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2005-2006 Micronas USA Inc.
   4 */
   5
   6#include "saa7134.h"
   7#include "saa7134-reg.h"
   8
   9#include <linux/module.h>
  10#include <linux/kernel.h>
  11#include <linux/init.h>
  12#include <linux/spinlock.h>
  13#include <linux/wait.h>
  14#include <linux/list.h>
  15#include <linux/slab.h>
  16#include <linux/time.h>
  17#include <linux/mm.h>
  18#include <linux/usb.h>
  19#include <linux/i2c.h>
  20#include <asm/byteorder.h>
  21#include <media/v4l2-common.h>
  22#include <media/v4l2-device.h>
  23#include <media/v4l2-subdev.h>
  24
  25#include "go7007-priv.h"
  26
  27/*#define GO7007_HPI_DEBUG*/
  28
  29enum hpi_address {
  30        HPI_ADDR_VIDEO_BUFFER = 0xe4,
  31        HPI_ADDR_INIT_BUFFER = 0xea,
  32        HPI_ADDR_INTR_RET_VALUE = 0xee,
  33        HPI_ADDR_INTR_RET_DATA = 0xec,
  34        HPI_ADDR_INTR_STATUS = 0xf4,
  35        HPI_ADDR_INTR_WR_PARAM = 0xf6,
  36        HPI_ADDR_INTR_WR_INDEX = 0xf8,
  37};
  38
  39enum gpio_command {
  40        GPIO_COMMAND_RESET = 0x00, /* 000b */
  41        GPIO_COMMAND_REQ1  = 0x04, /* 001b */
  42        GPIO_COMMAND_WRITE = 0x20, /* 010b */
  43        GPIO_COMMAND_REQ2  = 0x24, /* 011b */
  44        GPIO_COMMAND_READ  = 0x80, /* 100b */
  45        GPIO_COMMAND_VIDEO = 0x84, /* 101b */
  46        GPIO_COMMAND_IDLE  = 0xA0, /* 110b */
  47        GPIO_COMMAND_ADDR  = 0xA4, /* 111b */
  48};
  49
  50struct saa7134_go7007 {
  51        struct v4l2_subdev sd;
  52        struct saa7134_dev *dev;
  53        u8 *top;
  54        u8 *bottom;
  55        dma_addr_t top_dma;
  56        dma_addr_t bottom_dma;
  57};
  58
  59static inline struct saa7134_go7007 *to_state(struct v4l2_subdev *sd)
  60{
  61        return container_of(sd, struct saa7134_go7007, sd);
  62}
  63
  64static const struct go7007_board_info board_voyager = {
  65        .flags           = 0,
  66        .sensor_flags    = GO7007_SENSOR_656 |
  67                                GO7007_SENSOR_VALID_ENABLE |
  68                                GO7007_SENSOR_TV |
  69                                GO7007_SENSOR_VBI,
  70        .audio_flags    = GO7007_AUDIO_I2S_MODE_1 |
  71                                GO7007_AUDIO_WORD_16,
  72        .audio_rate      = 48000,
  73        .audio_bclk_div  = 8,
  74        .audio_main_div  = 2,
  75        .hpi_buffer_cap  = 7,
  76        .num_inputs      = 1,
  77        .inputs          = {
  78                {
  79                        .name           = "SAA7134",
  80                },
  81        },
  82};
  83
  84/********************* Driver for GPIO HPI interface *********************/
  85
  86static int gpio_write(struct saa7134_dev *dev, u8 addr, u16 data)
  87{
  88        saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
  89
  90        /* Write HPI address */
  91        saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
  92        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
  93        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
  94
  95        /* Write low byte */
  96        saa_writeb(SAA7134_GPIO_GPSTATUS0, data & 0xff);
  97        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
  98        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
  99
 100        /* Write high byte */
 101        saa_writeb(SAA7134_GPIO_GPSTATUS0, data >> 8);
 102        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
 103        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
 104
 105        return 0;
 106}
 107
 108static int gpio_read(struct saa7134_dev *dev, u8 addr, u16 *data)
 109{
 110        saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
 111
 112        /* Write HPI address */
 113        saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
 114        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
 115        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
 116
 117        saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
 118
 119        /* Read low byte */
 120        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
 121        saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 122        saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 123        *data = saa_readb(SAA7134_GPIO_GPSTATUS0);
 124        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
 125
 126        /* Read high byte */
 127        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
 128        saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 129        saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 130        *data |= saa_readb(SAA7134_GPIO_GPSTATUS0) << 8;
 131        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
 132
 133        return 0;
 134}
 135
 136static int saa7134_go7007_interface_reset(struct go7007 *go)
 137{
 138        struct saa7134_go7007 *saa = go->hpi_context;
 139        struct saa7134_dev *dev = saa->dev;
 140        u16 intr_val, intr_data;
 141        int count = 20;
 142
 143        saa_clearb(SAA7134_TS_PARALLEL, 0x80); /* Disable TS interface */
 144        saa_writeb(SAA7134_GPIO_GPMODE2, 0xa4);
 145        saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
 146
 147        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
 148        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_RESET);
 149        msleep(1);
 150        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
 151        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
 152        msleep(10);
 153
 154        saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 155        saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 156
 157        saa_readb(SAA7134_GPIO_GPSTATUS2);
 158        /*pr_debug("status is %s\n", saa_readb(SAA7134_GPIO_GPSTATUS2) & 0x40 ? "OK" : "not OK"); */
 159
 160        /* enter command mode...(?) */
 161        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
 162        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
 163
 164        do {
 165                saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 166                saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
 167                saa_readb(SAA7134_GPIO_GPSTATUS2);
 168                /*pr_info("gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
 169        } while (--count > 0);
 170
 171        /* Wait for an interrupt to indicate successful hardware reset */
 172        if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
 173                        (intr_val & ~0x1) != 0x55aa) {
 174                pr_err("saa7134-go7007: unable to reset the GO7007\n");
 175                return -1;
 176        }
 177        return 0;
 178}
 179
 180static int saa7134_go7007_write_interrupt(struct go7007 *go, int addr, int data)
 181{
 182        struct saa7134_go7007 *saa = go->hpi_context;
 183        struct saa7134_dev *dev = saa->dev;
 184        int i;
 185        u16 status_reg;
 186
 187#ifdef GO7007_HPI_DEBUG
 188        pr_debug("saa7134-go7007: WriteInterrupt: %04x %04x\n", addr, data);
 189#endif
 190
 191        for (i = 0; i < 100; ++i) {
 192                gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
 193                if (!(status_reg & 0x0010))
 194                        break;
 195                msleep(10);
 196        }
 197        if (i == 100) {
 198                pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
 199                        status_reg);
 200                return -1;
 201        }
 202        gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
 203        gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
 204
 205        return 0;
 206}
 207
 208static int saa7134_go7007_read_interrupt(struct go7007 *go)
 209{
 210        struct saa7134_go7007 *saa = go->hpi_context;
 211        struct saa7134_dev *dev = saa->dev;
 212
 213        /* XXX we need to wait if there is no interrupt available */
 214        go->interrupt_available = 1;
 215        gpio_read(dev, HPI_ADDR_INTR_RET_VALUE, &go->interrupt_value);
 216        gpio_read(dev, HPI_ADDR_INTR_RET_DATA, &go->interrupt_data);
 217#ifdef GO7007_HPI_DEBUG
 218        pr_debug("saa7134-go7007: ReadInterrupt: %04x %04x\n",
 219                        go->interrupt_value, go->interrupt_data);
 220#endif
 221        return 0;
 222}
 223
 224static void saa7134_go7007_irq_ts_done(struct saa7134_dev *dev,
 225                                                unsigned long status)
 226{
 227        struct go7007 *go = video_get_drvdata(dev->empress_dev);
 228        struct saa7134_go7007 *saa = go->hpi_context;
 229
 230        if (!vb2_is_streaming(&go->vidq))
 231                return;
 232        if (0 != (status & 0x000f0000))
 233                pr_debug("saa7134-go7007: irq: lost %ld\n",
 234                                (status >> 16) & 0x0f);
 235        if (status & 0x100000) {
 236                dma_sync_single_for_cpu(&dev->pci->dev,
 237                                        saa->bottom_dma, PAGE_SIZE, DMA_FROM_DEVICE);
 238                go7007_parse_video_stream(go, saa->bottom, PAGE_SIZE);
 239                saa_writel(SAA7134_RS_BA2(5), saa->bottom_dma);
 240        } else {
 241                dma_sync_single_for_cpu(&dev->pci->dev,
 242                                        saa->top_dma, PAGE_SIZE, DMA_FROM_DEVICE);
 243                go7007_parse_video_stream(go, saa->top, PAGE_SIZE);
 244                saa_writel(SAA7134_RS_BA1(5), saa->top_dma);
 245        }
 246}
 247
 248static int saa7134_go7007_stream_start(struct go7007 *go)
 249{
 250        struct saa7134_go7007 *saa = go->hpi_context;
 251        struct saa7134_dev *dev = saa->dev;
 252
 253        saa->top_dma = dma_map_page(&dev->pci->dev, virt_to_page(saa->top),
 254                        0, PAGE_SIZE, DMA_FROM_DEVICE);
 255        if (dma_mapping_error(&dev->pci->dev, saa->top_dma))
 256                return -ENOMEM;
 257        saa->bottom_dma = dma_map_page(&dev->pci->dev,
 258                        virt_to_page(saa->bottom),
 259                        0, PAGE_SIZE, DMA_FROM_DEVICE);
 260        if (dma_mapping_error(&dev->pci->dev, saa->bottom_dma)) {
 261                dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
 262                                DMA_FROM_DEVICE);
 263                return -ENOMEM;
 264        }
 265
 266        saa_writel(SAA7134_VIDEO_PORT_CTRL0 >> 2, 0xA300B000);
 267        saa_writel(SAA7134_VIDEO_PORT_CTRL4 >> 2, 0x40000200);
 268
 269        /* Set HPI interface for video */
 270        saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
 271        saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_VIDEO_BUFFER);
 272        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
 273        saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
 274
 275        /* Enable TS interface */
 276        saa_writeb(SAA7134_TS_PARALLEL, 0xe6);
 277
 278        /* Reset TS interface */
 279        saa_setb(SAA7134_TS_SERIAL1, 0x01);
 280        saa_clearb(SAA7134_TS_SERIAL1, 0x01);
 281
 282        /* Set up transfer block size */
 283        saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 128 - 1);
 284        saa_writeb(SAA7134_TS_DMA0, ((PAGE_SIZE >> 7) - 1) & 0xff);
 285        saa_writeb(SAA7134_TS_DMA1, (PAGE_SIZE >> 15) & 0xff);
 286        saa_writeb(SAA7134_TS_DMA2, (PAGE_SIZE >> 31) & 0x3f);
 287
 288        /* Enable video streaming mode */
 289        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_VIDEO);
 290
 291        saa_writel(SAA7134_RS_BA1(5), saa->top_dma);
 292        saa_writel(SAA7134_RS_BA2(5), saa->bottom_dma);
 293        saa_writel(SAA7134_RS_PITCH(5), 128);
 294        saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX);
 295
 296        /* Enable TS FIFO */
 297        saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
 298
 299        /* Enable DMA IRQ */
 300        saa_setl(SAA7134_IRQ1,
 301                        SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
 302
 303        return 0;
 304}
 305
 306static int saa7134_go7007_stream_stop(struct go7007 *go)
 307{
 308        struct saa7134_go7007 *saa = go->hpi_context;
 309        struct saa7134_dev *dev;
 310
 311        if (!saa)
 312                return -EINVAL;
 313        dev = saa->dev;
 314        if (!dev)
 315                return -EINVAL;
 316
 317        /* Shut down TS FIFO */
 318        saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
 319
 320        /* Disable DMA IRQ */
 321        saa_clearl(SAA7134_IRQ1,
 322                        SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
 323
 324        /* Disable TS interface */
 325        saa_clearb(SAA7134_TS_PARALLEL, 0x80);
 326
 327        dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
 328                        DMA_FROM_DEVICE);
 329        dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
 330                        DMA_FROM_DEVICE);
 331
 332        return 0;
 333}
 334
 335static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
 336{
 337        struct saa7134_go7007 *saa = go->hpi_context;
 338        struct saa7134_dev *dev = saa->dev;
 339        u16 status_reg;
 340        int i;
 341
 342#ifdef GO7007_HPI_DEBUG
 343        pr_debug("saa7134-go7007: DownloadBuffer sending %d bytes\n", len);
 344#endif
 345
 346        while (len > 0) {
 347                i = len > 64 ? 64 : len;
 348                saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
 349                saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_INIT_BUFFER);
 350                saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
 351                saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
 352                while (i-- > 0) {
 353                        saa_writeb(SAA7134_GPIO_GPSTATUS0, *data);
 354                        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
 355                        saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
 356                        ++data;
 357                        --len;
 358                }
 359                for (i = 0; i < 100; ++i) {
 360                        gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
 361                        if (!(status_reg & 0x0002))
 362                                break;
 363                }
 364                if (i == 100) {
 365                        pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
 366                               status_reg);
 367                        return -1;
 368                }
 369        }
 370        return 0;
 371}
 372
 373static const struct go7007_hpi_ops saa7134_go7007_hpi_ops = {
 374        .interface_reset        = saa7134_go7007_interface_reset,
 375        .write_interrupt        = saa7134_go7007_write_interrupt,
 376        .read_interrupt         = saa7134_go7007_read_interrupt,
 377        .stream_start           = saa7134_go7007_stream_start,
 378        .stream_stop            = saa7134_go7007_stream_stop,
 379        .send_firmware          = saa7134_go7007_send_firmware,
 380};
 381MODULE_FIRMWARE("go7007/go7007tv.bin");
 382
 383/* --------------------------------------------------------------------------*/
 384
 385static int saa7134_go7007_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
 386{
 387#if 0
 388        struct saa7134_go7007 *saa = to_state(sd);
 389        struct saa7134_dev *dev = saa->dev;
 390
 391        return saa7134_s_std_internal(dev, NULL, norm);
 392#else
 393        return 0;
 394#endif
 395}
 396
 397static const struct v4l2_subdev_video_ops saa7134_go7007_video_ops = {
 398        .s_std = saa7134_go7007_s_std,
 399};
 400
 401static const struct v4l2_subdev_ops saa7134_go7007_sd_ops = {
 402        .video = &saa7134_go7007_video_ops,
 403};
 404
 405/* --------------------------------------------------------------------------*/
 406
 407
 408/********************* Add/remove functions *********************/
 409
 410static int saa7134_go7007_init(struct saa7134_dev *dev)
 411{
 412        struct go7007 *go;
 413        struct saa7134_go7007 *saa;
 414        struct v4l2_subdev *sd;
 415
 416        pr_debug("saa7134-go7007: probing new SAA713X board\n");
 417
 418        go = go7007_alloc(&board_voyager, &dev->pci->dev);
 419        if (go == NULL)
 420                return -ENOMEM;
 421
 422        saa = kzalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
 423        if (saa == NULL) {
 424                kfree(go);
 425                return -ENOMEM;
 426        }
 427
 428        go->board_id = GO7007_BOARDID_PCI_VOYAGER;
 429        snprintf(go->bus_info, sizeof(go->bus_info), "PCI:%s", pci_name(dev->pci));
 430        strscpy(go->name, saa7134_boards[dev->board].name, sizeof(go->name));
 431        go->hpi_ops = &saa7134_go7007_hpi_ops;
 432        go->hpi_context = saa;
 433        saa->dev = dev;
 434
 435        /* Init the subdevice interface */
 436        sd = &saa->sd;
 437        v4l2_subdev_init(sd, &saa7134_go7007_sd_ops);
 438        v4l2_set_subdevdata(sd, saa);
 439        strscpy(sd->name, "saa7134-go7007", sizeof(sd->name));
 440
 441        /* Allocate a couple pages for receiving the compressed stream */
 442        saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
 443        if (!saa->top)
 444                goto allocfail;
 445        saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
 446        if (!saa->bottom)
 447                goto allocfail;
 448
 449        /* Boot the GO7007 */
 450        if (go7007_boot_encoder(go, go->board_info->flags &
 451                                        GO7007_BOARD_USE_ONBOARD_I2C) < 0)
 452                goto allocfail;
 453
 454        /* Do any final GO7007 initialization, then register the
 455         * V4L2 and ALSA interfaces */
 456        if (go7007_register_encoder(go, go->board_info->num_i2c_devs) < 0)
 457                goto allocfail;
 458
 459        /* Register the subdevice interface with the go7007 device */
 460        if (v4l2_device_register_subdev(&go->v4l2_dev, sd) < 0)
 461                pr_info("saa7134-go7007: register subdev failed\n");
 462
 463        dev->empress_dev = &go->vdev;
 464
 465        go->status = STATUS_ONLINE;
 466        return 0;
 467
 468allocfail:
 469        if (saa->top)
 470                free_page((unsigned long)saa->top);
 471        if (saa->bottom)
 472                free_page((unsigned long)saa->bottom);
 473        kfree(saa);
 474        kfree(go);
 475        return -ENOMEM;
 476}
 477
 478static int saa7134_go7007_fini(struct saa7134_dev *dev)
 479{
 480        struct go7007 *go;
 481        struct saa7134_go7007 *saa;
 482
 483        if (NULL == dev->empress_dev)
 484                return 0;
 485
 486        go = video_get_drvdata(dev->empress_dev);
 487        if (go->audio_enabled)
 488                go7007_snd_remove(go);
 489
 490        saa = go->hpi_context;
 491        go->status = STATUS_SHUTDOWN;
 492        free_page((unsigned long)saa->top);
 493        free_page((unsigned long)saa->bottom);
 494        v4l2_device_unregister_subdev(&saa->sd);
 495        kfree(saa);
 496        vb2_video_unregister_device(&go->vdev);
 497
 498        v4l2_device_put(&go->v4l2_dev);
 499        dev->empress_dev = NULL;
 500
 501        return 0;
 502}
 503
 504static struct saa7134_mpeg_ops saa7134_go7007_ops = {
 505        .type          = SAA7134_MPEG_GO7007,
 506        .init          = saa7134_go7007_init,
 507        .fini          = saa7134_go7007_fini,
 508        .irq_ts_done   = saa7134_go7007_irq_ts_done,
 509};
 510
 511static int __init saa7134_go7007_mod_init(void)
 512{
 513        return saa7134_ts_register(&saa7134_go7007_ops);
 514}
 515
 516static void __exit saa7134_go7007_mod_cleanup(void)
 517{
 518        saa7134_ts_unregister(&saa7134_go7007_ops);
 519}
 520
 521module_init(saa7134_go7007_mod_init);
 522module_exit(saa7134_go7007_mod_cleanup);
 523
 524MODULE_LICENSE("GPL v2");
 525