s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / media / pci / saa7134 / saa7134-go7007.c
blob8a2abb34186b3dfa08b3ac6a22af298b757bc132
1 /*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "saa7134.h"
15 #include "saa7134-reg.h"
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/mm.h>
26 #include <linux/usb.h>
27 #include <linux/i2c.h>
28 #include <asm/byteorder.h>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-subdev.h>
33 #include "go7007-priv.h"
35 /*#define GO7007_HPI_DEBUG*/
37 enum hpi_address {
38 HPI_ADDR_VIDEO_BUFFER = 0xe4,
39 HPI_ADDR_INIT_BUFFER = 0xea,
40 HPI_ADDR_INTR_RET_VALUE = 0xee,
41 HPI_ADDR_INTR_RET_DATA = 0xec,
42 HPI_ADDR_INTR_STATUS = 0xf4,
43 HPI_ADDR_INTR_WR_PARAM = 0xf6,
44 HPI_ADDR_INTR_WR_INDEX = 0xf8,
47 enum gpio_command {
48 GPIO_COMMAND_RESET = 0x00, /* 000b */
49 GPIO_COMMAND_REQ1 = 0x04, /* 001b */
50 GPIO_COMMAND_WRITE = 0x20, /* 010b */
51 GPIO_COMMAND_REQ2 = 0x24, /* 011b */
52 GPIO_COMMAND_READ = 0x80, /* 100b */
53 GPIO_COMMAND_VIDEO = 0x84, /* 101b */
54 GPIO_COMMAND_IDLE = 0xA0, /* 110b */
55 GPIO_COMMAND_ADDR = 0xA4, /* 111b */
58 struct saa7134_go7007 {
59 struct v4l2_subdev sd;
60 struct saa7134_dev *dev;
61 u8 *top;
62 u8 *bottom;
63 dma_addr_t top_dma;
64 dma_addr_t bottom_dma;
67 static inline struct saa7134_go7007 *to_state(struct v4l2_subdev *sd)
69 return container_of(sd, struct saa7134_go7007, sd);
72 static const struct go7007_board_info board_voyager = {
73 .flags = 0,
74 .sensor_flags = GO7007_SENSOR_656 |
75 GO7007_SENSOR_VALID_ENABLE |
76 GO7007_SENSOR_TV |
77 GO7007_SENSOR_VBI,
78 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
79 GO7007_AUDIO_WORD_16,
80 .audio_rate = 48000,
81 .audio_bclk_div = 8,
82 .audio_main_div = 2,
83 .hpi_buffer_cap = 7,
84 .num_inputs = 1,
85 .inputs = {
87 .name = "SAA7134",
92 /********************* Driver for GPIO HPI interface *********************/
94 static int gpio_write(struct saa7134_dev *dev, u8 addr, u16 data)
96 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
98 /* Write HPI address */
99 saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
100 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
101 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
103 /* Write low byte */
104 saa_writeb(SAA7134_GPIO_GPSTATUS0, data & 0xff);
105 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
106 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
108 /* Write high byte */
109 saa_writeb(SAA7134_GPIO_GPSTATUS0, data >> 8);
110 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
111 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
113 return 0;
116 static int gpio_read(struct saa7134_dev *dev, u8 addr, u16 *data)
118 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
120 /* Write HPI address */
121 saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
122 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
123 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
125 saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
127 /* Read low byte */
128 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
129 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
130 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
131 *data = saa_readb(SAA7134_GPIO_GPSTATUS0);
132 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
134 /* Read high byte */
135 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
136 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
137 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
138 *data |= saa_readb(SAA7134_GPIO_GPSTATUS0) << 8;
139 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
141 return 0;
144 static int saa7134_go7007_interface_reset(struct go7007 *go)
146 struct saa7134_go7007 *saa = go->hpi_context;
147 struct saa7134_dev *dev = saa->dev;
148 u16 intr_val, intr_data;
149 int count = 20;
151 saa_clearb(SAA7134_TS_PARALLEL, 0x80); /* Disable TS interface */
152 saa_writeb(SAA7134_GPIO_GPMODE2, 0xa4);
153 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
155 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
156 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_RESET);
157 msleep(1);
158 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
159 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
160 msleep(10);
162 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
163 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
165 saa_readb(SAA7134_GPIO_GPSTATUS2);
166 /*pr_debug("status is %s\n", saa_readb(SAA7134_GPIO_GPSTATUS2) & 0x40 ? "OK" : "not OK"); */
168 /* enter command mode...(?) */
169 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
170 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
172 do {
173 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
174 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
175 saa_readb(SAA7134_GPIO_GPSTATUS2);
176 /*pr_info("gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
177 } while (--count > 0);
179 /* Wait for an interrupt to indicate successful hardware reset */
180 if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
181 (intr_val & ~0x1) != 0x55aa) {
182 pr_err("saa7134-go7007: unable to reset the GO7007\n");
183 return -1;
185 return 0;
188 static int saa7134_go7007_write_interrupt(struct go7007 *go, int addr, int data)
190 struct saa7134_go7007 *saa = go->hpi_context;
191 struct saa7134_dev *dev = saa->dev;
192 int i;
193 u16 status_reg;
195 #ifdef GO7007_HPI_DEBUG
196 pr_debug("saa7134-go7007: WriteInterrupt: %04x %04x\n", addr, data);
197 #endif
199 for (i = 0; i < 100; ++i) {
200 gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
201 if (!(status_reg & 0x0010))
202 break;
203 msleep(10);
205 if (i == 100) {
206 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
207 status_reg);
208 return -1;
210 gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
211 gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
213 return 0;
216 static int saa7134_go7007_read_interrupt(struct go7007 *go)
218 struct saa7134_go7007 *saa = go->hpi_context;
219 struct saa7134_dev *dev = saa->dev;
221 /* XXX we need to wait if there is no interrupt available */
222 go->interrupt_available = 1;
223 gpio_read(dev, HPI_ADDR_INTR_RET_VALUE, &go->interrupt_value);
224 gpio_read(dev, HPI_ADDR_INTR_RET_DATA, &go->interrupt_data);
225 #ifdef GO7007_HPI_DEBUG
226 pr_debug("saa7134-go7007: ReadInterrupt: %04x %04x\n",
227 go->interrupt_value, go->interrupt_data);
228 #endif
229 return 0;
232 static void saa7134_go7007_irq_ts_done(struct saa7134_dev *dev,
233 unsigned long status)
235 struct go7007 *go = video_get_drvdata(dev->empress_dev);
236 struct saa7134_go7007 *saa = go->hpi_context;
238 if (!vb2_is_streaming(&go->vidq))
239 return;
240 if (0 != (status & 0x000f0000))
241 pr_debug("saa7134-go7007: irq: lost %ld\n",
242 (status >> 16) & 0x0f);
243 if (status & 0x100000) {
244 dma_sync_single_for_cpu(&dev->pci->dev,
245 saa->bottom_dma, PAGE_SIZE, DMA_FROM_DEVICE);
246 go7007_parse_video_stream(go, saa->bottom, PAGE_SIZE);
247 saa_writel(SAA7134_RS_BA2(5), saa->bottom_dma);
248 } else {
249 dma_sync_single_for_cpu(&dev->pci->dev,
250 saa->top_dma, PAGE_SIZE, DMA_FROM_DEVICE);
251 go7007_parse_video_stream(go, saa->top, PAGE_SIZE);
252 saa_writel(SAA7134_RS_BA1(5), saa->top_dma);
256 static int saa7134_go7007_stream_start(struct go7007 *go)
258 struct saa7134_go7007 *saa = go->hpi_context;
259 struct saa7134_dev *dev = saa->dev;
261 saa->top_dma = dma_map_page(&dev->pci->dev, virt_to_page(saa->top),
262 0, PAGE_SIZE, DMA_FROM_DEVICE);
263 if (dma_mapping_error(&dev->pci->dev, saa->top_dma))
264 return -ENOMEM;
265 saa->bottom_dma = dma_map_page(&dev->pci->dev,
266 virt_to_page(saa->bottom),
267 0, PAGE_SIZE, DMA_FROM_DEVICE);
268 if (dma_mapping_error(&dev->pci->dev, saa->bottom_dma)) {
269 dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
270 DMA_FROM_DEVICE);
271 return -ENOMEM;
274 saa_writel(SAA7134_VIDEO_PORT_CTRL0 >> 2, 0xA300B000);
275 saa_writel(SAA7134_VIDEO_PORT_CTRL4 >> 2, 0x40000200);
277 /* Set HPI interface for video */
278 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
279 saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_VIDEO_BUFFER);
280 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
281 saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
283 /* Enable TS interface */
284 saa_writeb(SAA7134_TS_PARALLEL, 0xe6);
286 /* Reset TS interface */
287 saa_setb(SAA7134_TS_SERIAL1, 0x01);
288 saa_clearb(SAA7134_TS_SERIAL1, 0x01);
290 /* Set up transfer block size */
291 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 128 - 1);
292 saa_writeb(SAA7134_TS_DMA0, ((PAGE_SIZE >> 7) - 1) & 0xff);
293 saa_writeb(SAA7134_TS_DMA1, (PAGE_SIZE >> 15) & 0xff);
294 saa_writeb(SAA7134_TS_DMA2, (PAGE_SIZE >> 31) & 0x3f);
296 /* Enable video streaming mode */
297 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_VIDEO);
299 saa_writel(SAA7134_RS_BA1(5), saa->top_dma);
300 saa_writel(SAA7134_RS_BA2(5), saa->bottom_dma);
301 saa_writel(SAA7134_RS_PITCH(5), 128);
302 saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX);
304 /* Enable TS FIFO */
305 saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
307 /* Enable DMA IRQ */
308 saa_setl(SAA7134_IRQ1,
309 SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
311 return 0;
314 static int saa7134_go7007_stream_stop(struct go7007 *go)
316 struct saa7134_go7007 *saa = go->hpi_context;
317 struct saa7134_dev *dev;
319 if (!saa)
320 return -EINVAL;
321 dev = saa->dev;
322 if (!dev)
323 return -EINVAL;
325 /* Shut down TS FIFO */
326 saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
328 /* Disable DMA IRQ */
329 saa_clearl(SAA7134_IRQ1,
330 SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
332 /* Disable TS interface */
333 saa_clearb(SAA7134_TS_PARALLEL, 0x80);
335 dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
336 DMA_FROM_DEVICE);
337 dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
338 DMA_FROM_DEVICE);
340 return 0;
343 static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
345 struct saa7134_go7007 *saa = go->hpi_context;
346 struct saa7134_dev *dev = saa->dev;
347 u16 status_reg;
348 int i;
350 #ifdef GO7007_HPI_DEBUG
351 pr_debug("saa7134-go7007: DownloadBuffer sending %d bytes\n", len);
352 #endif
354 while (len > 0) {
355 i = len > 64 ? 64 : len;
356 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
357 saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_INIT_BUFFER);
358 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
359 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
360 while (i-- > 0) {
361 saa_writeb(SAA7134_GPIO_GPSTATUS0, *data);
362 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
363 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
364 ++data;
365 --len;
367 for (i = 0; i < 100; ++i) {
368 gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
369 if (!(status_reg & 0x0002))
370 break;
372 if (i == 100) {
373 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
374 status_reg);
375 return -1;
378 return 0;
381 static struct go7007_hpi_ops saa7134_go7007_hpi_ops = {
382 .interface_reset = saa7134_go7007_interface_reset,
383 .write_interrupt = saa7134_go7007_write_interrupt,
384 .read_interrupt = saa7134_go7007_read_interrupt,
385 .stream_start = saa7134_go7007_stream_start,
386 .stream_stop = saa7134_go7007_stream_stop,
387 .send_firmware = saa7134_go7007_send_firmware,
389 MODULE_FIRMWARE("go7007/go7007tv.bin");
391 /* --------------------------------------------------------------------------*/
393 static int saa7134_go7007_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
395 #if 0
396 struct saa7134_go7007 *saa = to_state(sd);
397 struct saa7134_dev *dev = saa->dev;
399 return saa7134_s_std_internal(dev, NULL, norm);
400 #else
401 return 0;
402 #endif
405 static const struct v4l2_subdev_video_ops saa7134_go7007_video_ops = {
406 .s_std = saa7134_go7007_s_std,
409 static const struct v4l2_subdev_ops saa7134_go7007_sd_ops = {
410 .video = &saa7134_go7007_video_ops,
413 /* --------------------------------------------------------------------------*/
416 /********************* Add/remove functions *********************/
418 static int saa7134_go7007_init(struct saa7134_dev *dev)
420 struct go7007 *go;
421 struct saa7134_go7007 *saa;
422 struct v4l2_subdev *sd;
424 pr_debug("saa7134-go7007: probing new SAA713X board\n");
426 go = go7007_alloc(&board_voyager, &dev->pci->dev);
427 if (go == NULL)
428 return -ENOMEM;
430 saa = kzalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
431 if (saa == NULL) {
432 kfree(go);
433 return -ENOMEM;
436 go->board_id = GO7007_BOARDID_PCI_VOYAGER;
437 snprintf(go->bus_info, sizeof(go->bus_info), "PCI:%s", pci_name(dev->pci));
438 strlcpy(go->name, saa7134_boards[dev->board].name, sizeof(go->name));
439 go->hpi_ops = &saa7134_go7007_hpi_ops;
440 go->hpi_context = saa;
441 saa->dev = dev;
443 /* Init the subdevice interface */
444 sd = &saa->sd;
445 v4l2_subdev_init(sd, &saa7134_go7007_sd_ops);
446 v4l2_set_subdevdata(sd, saa);
447 strncpy(sd->name, "saa7134-go7007", sizeof(sd->name));
449 /* Allocate a couple pages for receiving the compressed stream */
450 saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
451 if (!saa->top)
452 goto allocfail;
453 saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
454 if (!saa->bottom)
455 goto allocfail;
457 /* Boot the GO7007 */
458 if (go7007_boot_encoder(go, go->board_info->flags &
459 GO7007_BOARD_USE_ONBOARD_I2C) < 0)
460 goto allocfail;
462 /* Do any final GO7007 initialization, then register the
463 * V4L2 and ALSA interfaces */
464 if (go7007_register_encoder(go, go->board_info->num_i2c_devs) < 0)
465 goto allocfail;
467 /* Register the subdevice interface with the go7007 device */
468 if (v4l2_device_register_subdev(&go->v4l2_dev, sd) < 0)
469 pr_info("saa7134-go7007: register subdev failed\n");
471 dev->empress_dev = &go->vdev;
473 go->status = STATUS_ONLINE;
474 return 0;
476 allocfail:
477 if (saa->top)
478 free_page((unsigned long)saa->top);
479 if (saa->bottom)
480 free_page((unsigned long)saa->bottom);
481 kfree(saa);
482 kfree(go);
483 return -ENOMEM;
486 static int saa7134_go7007_fini(struct saa7134_dev *dev)
488 struct go7007 *go;
489 struct saa7134_go7007 *saa;
491 if (NULL == dev->empress_dev)
492 return 0;
494 go = video_get_drvdata(dev->empress_dev);
495 if (go->audio_enabled)
496 go7007_snd_remove(go);
498 saa = go->hpi_context;
499 go->status = STATUS_SHUTDOWN;
500 free_page((unsigned long)saa->top);
501 free_page((unsigned long)saa->bottom);
502 v4l2_device_unregister_subdev(&saa->sd);
503 kfree(saa);
504 video_unregister_device(&go->vdev);
506 v4l2_device_put(&go->v4l2_dev);
507 dev->empress_dev = NULL;
509 return 0;
512 static struct saa7134_mpeg_ops saa7134_go7007_ops = {
513 .type = SAA7134_MPEG_GO7007,
514 .init = saa7134_go7007_init,
515 .fini = saa7134_go7007_fini,
516 .irq_ts_done = saa7134_go7007_irq_ts_done,
519 static int __init saa7134_go7007_mod_init(void)
521 return saa7134_ts_register(&saa7134_go7007_ops);
524 static void __exit saa7134_go7007_mod_cleanup(void)
526 saa7134_ts_unregister(&saa7134_go7007_ops);
529 module_init(saa7134_go7007_mod_init);
530 module_exit(saa7134_go7007_mod_cleanup);
532 MODULE_LICENSE("GPL v2");