2 * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
4 * Copyright (C) 2015 Industrial Research Institute for Automation
5 * and Measurements PIAP
6 * Written by Krzysztof Ha?asa
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License
10 * as published by the Free Software Foundation.
13 #include <linux/mutex.h>
14 #include <linux/pci.h>
15 #include <linux/timer.h>
16 #include <linux/videodev2.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <sound/pcm.h>
24 #include "tw686x-regs.h"
26 #define TYPE_MAX_CHANNELS 0x0f
27 #define TYPE_SECOND_GEN 0x10
28 #define TW686X_DEF_PHASE_REF 0x1518
30 #define TW686X_AUDIO_PAGE_MAX 16
31 #define TW686X_AUDIO_PERIODS_MIN 2
32 #define TW686X_AUDIO_PERIODS_MAX TW686X_AUDIO_PAGE_MAX
34 #define TW686X_DMA_MODE_MEMCPY 0
35 #define TW686X_DMA_MODE_CONTIG 1
36 #define TW686X_DMA_MODE_SG 2
38 struct tw686x_format
{
45 struct tw686x_dma_desc
{
51 struct tw686x_sg_desc
{
52 /* 3 MSBits for flags, 13 LSBits for length */
57 struct tw686x_audio_buf
{
60 struct list_head list
;
63 struct tw686x_v4l2_buf
{
64 struct vb2_v4l2_buffer vb
;
65 struct list_head list
;
68 struct tw686x_audio_channel
{
69 struct tw686x_dev
*dev
;
70 struct snd_pcm_substream
*ss
;
72 struct tw686x_audio_buf
*curr_bufs
[2];
73 struct tw686x_dma_desc dma_descs
[2];
76 struct tw686x_audio_buf buf
[TW686X_AUDIO_PAGE_MAX
];
77 struct list_head buf_list
;
81 struct tw686x_video_channel
{
82 struct tw686x_dev
*dev
;
84 struct vb2_queue vidq
;
85 struct list_head vidq_queued
;
86 struct video_device
*device
;
87 struct tw686x_v4l2_buf
*curr_bufs
[2];
88 struct tw686x_dma_desc dma_descs
[2];
89 struct tw686x_sg_desc
*sg_descs
[2];
91 struct v4l2_ctrl_handler ctrl_handler
;
92 const struct tw686x_format
*format
;
93 struct mutex vb_mutex
;
95 v4l2_std_id video_standard
;
96 unsigned int width
, height
;
97 unsigned int h_halve
, v_halve
;
102 unsigned int sequence
;
107 struct tw686x_dma_ops
{
108 int (*setup
)(struct tw686x_dev
*dev
);
109 int (*alloc
)(struct tw686x_video_channel
*vc
, unsigned int pb
);
110 void (*free
)(struct tw686x_video_channel
*vc
, unsigned int pb
);
111 void (*buf_refill
)(struct tw686x_video_channel
*vc
, unsigned int pb
);
112 const struct vb2_mem_ops
*mem_ops
;
113 enum v4l2_field field
;
118 * struct tw686x_dev - global device status
119 * @lock: spinlock controlling access to the
120 * shared device registers (DMA enable/disable).
125 struct v4l2_device v4l2_dev
;
126 struct snd_card
*snd_card
;
130 unsigned int dma_mode
;
131 struct pci_dev
*pci_dev
;
134 const struct tw686x_dma_ops
*dma_ops
;
135 struct tw686x_video_channel
*video_channels
;
136 struct tw686x_audio_channel
*audio_channels
;
138 /* Per-device audio parameters */
143 struct timer_list dma_delay_timer
;
144 u32 pending_dma_en
; /* must be protected by lock */
145 u32 pending_dma_cmd
; /* must be protected by lock */
148 static inline uint32_t reg_read(struct tw686x_dev
*dev
, unsigned int reg
)
150 return readl(dev
->mmio
+ reg
);
153 static inline void reg_write(struct tw686x_dev
*dev
, unsigned int reg
,
156 writel(value
, dev
->mmio
+ reg
);
159 static inline unsigned int max_channels(struct tw686x_dev
*dev
)
161 return dev
->type
& TYPE_MAX_CHANNELS
; /* 4 or 8 channels */
164 static inline unsigned is_second_gen(struct tw686x_dev
*dev
)
166 /* each channel has its own DMA SG table */
167 return dev
->type
& TYPE_SECOND_GEN
;
170 void tw686x_enable_channel(struct tw686x_dev
*dev
, unsigned int channel
);
171 void tw686x_disable_channel(struct tw686x_dev
*dev
, unsigned int channel
);
173 int tw686x_video_init(struct tw686x_dev
*dev
);
174 void tw686x_video_free(struct tw686x_dev
*dev
);
175 void tw686x_video_irq(struct tw686x_dev
*dev
, unsigned long requests
,
176 unsigned int pb_status
, unsigned int fifo_status
,
177 unsigned int *reset_ch
);
179 int tw686x_audio_init(struct tw686x_dev
*dev
);
180 void tw686x_audio_free(struct tw686x_dev
*dev
);
181 void tw686x_audio_irq(struct tw686x_dev
*dev
, unsigned long requests
,
182 unsigned int pb_status
);