Commit | Line | Data |
---|---|---|
704a84cc EG |
1 | /* |
2 | * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar | |
3 | * | |
4 | * Copyright (C) 2015 Industrial Research Institute for Automation | |
5 | * and Measurements PIAP | |
6 | * Written by Krzysztof Ha?asa | |
7 | * | |
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. | |
11 | */ | |
12 | ||
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> | |
23 | ||
24 | #include "tw686x-regs.h" | |
25 | ||
26 | #define TYPE_MAX_CHANNELS 0x0f | |
27 | #define TYPE_SECOND_GEN 0x10 | |
28 | #define TW686X_DEF_PHASE_REF 0x1518 | |
29 | ||
704a84cc EG |
30 | #define TW686X_AUDIO_PAGE_SZ 4096 |
31 | #define TW686X_AUDIO_PAGE_MAX 16 | |
32 | #define TW686X_AUDIO_PERIODS_MIN 2 | |
33 | #define TW686X_AUDIO_PERIODS_MAX TW686X_AUDIO_PAGE_MAX | |
34 | ||
f8afaa8d | 35 | #define TW686X_DMA_MODE_MEMCPY 0 |
11a16974 | 36 | #define TW686X_DMA_MODE_CONTIG 1 |
f8afaa8d | 37 | |
704a84cc EG |
38 | struct tw686x_format { |
39 | char *name; | |
40 | unsigned int fourcc; | |
41 | unsigned int depth; | |
42 | unsigned int mode; | |
43 | }; | |
44 | ||
45 | struct tw686x_dma_desc { | |
46 | dma_addr_t phys; | |
47 | void *virt; | |
48 | unsigned int size; | |
49 | }; | |
50 | ||
51 | struct tw686x_audio_buf { | |
52 | dma_addr_t dma; | |
53 | void *virt; | |
54 | struct list_head list; | |
55 | }; | |
56 | ||
57 | struct tw686x_v4l2_buf { | |
58 | struct vb2_v4l2_buffer vb; | |
59 | struct list_head list; | |
60 | }; | |
61 | ||
62 | struct tw686x_audio_channel { | |
63 | struct tw686x_dev *dev; | |
64 | struct snd_pcm_substream *ss; | |
65 | unsigned int ch; | |
66 | struct tw686x_audio_buf *curr_bufs[2]; | |
67 | struct tw686x_dma_desc dma_descs[2]; | |
68 | dma_addr_t ptr; | |
69 | ||
70 | struct tw686x_audio_buf buf[TW686X_AUDIO_PAGE_MAX]; | |
71 | struct list_head buf_list; | |
72 | spinlock_t lock; | |
73 | }; | |
74 | ||
75 | struct tw686x_video_channel { | |
76 | struct tw686x_dev *dev; | |
77 | ||
78 | struct vb2_queue vidq; | |
79 | struct list_head vidq_queued; | |
80 | struct video_device *device; | |
81 | struct tw686x_v4l2_buf *curr_bufs[2]; | |
82 | struct tw686x_dma_desc dma_descs[2]; | |
83 | ||
84 | struct v4l2_ctrl_handler ctrl_handler; | |
85 | const struct tw686x_format *format; | |
86 | struct mutex vb_mutex; | |
87 | spinlock_t qlock; | |
88 | v4l2_std_id video_standard; | |
89 | unsigned int width, height; | |
90 | unsigned int h_halve, v_halve; | |
91 | unsigned int ch; | |
92 | unsigned int num; | |
93 | unsigned int fps; | |
94 | unsigned int input; | |
95 | unsigned int sequence; | |
96 | unsigned int pb; | |
97 | bool no_signal; | |
98 | }; | |
99 | ||
f8afaa8d EG |
100 | struct tw686x_dma_ops { |
101 | int (*setup)(struct tw686x_dev *dev); | |
102 | void (*cleanup)(struct tw686x_dev *dev); | |
103 | int (*alloc)(struct tw686x_video_channel *vc, unsigned int pb); | |
104 | void (*free)(struct tw686x_video_channel *vc, unsigned int pb); | |
105 | void (*buf_refill)(struct tw686x_video_channel *vc, unsigned int pb); | |
106 | const struct vb2_mem_ops *mem_ops; | |
107 | enum v4l2_field field; | |
108 | u32 hw_dma_mode; | |
109 | }; | |
110 | ||
704a84cc EG |
111 | /** |
112 | * struct tw686x_dev - global device status | |
113 | * @lock: spinlock controlling access to the | |
114 | * shared device registers (DMA enable/disable). | |
115 | */ | |
116 | struct tw686x_dev { | |
117 | spinlock_t lock; | |
118 | ||
119 | struct v4l2_device v4l2_dev; | |
120 | struct snd_card *snd_card; | |
121 | ||
122 | char name[32]; | |
123 | unsigned int type; | |
f8afaa8d | 124 | unsigned int dma_mode; |
704a84cc EG |
125 | struct pci_dev *pci_dev; |
126 | __u32 __iomem *mmio; | |
127 | ||
128 | void *alloc_ctx; | |
129 | ||
f8afaa8d | 130 | const struct tw686x_dma_ops *dma_ops; |
704a84cc EG |
131 | struct tw686x_video_channel *video_channels; |
132 | struct tw686x_audio_channel *audio_channels; | |
133 | ||
134 | int audio_rate; /* per-device value */ | |
135 | ||
136 | struct timer_list dma_delay_timer; | |
137 | u32 pending_dma_en; /* must be protected by lock */ | |
138 | u32 pending_dma_cmd; /* must be protected by lock */ | |
139 | }; | |
140 | ||
141 | static inline uint32_t reg_read(struct tw686x_dev *dev, unsigned int reg) | |
142 | { | |
143 | return readl(dev->mmio + reg); | |
144 | } | |
145 | ||
146 | static inline void reg_write(struct tw686x_dev *dev, unsigned int reg, | |
147 | uint32_t value) | |
148 | { | |
149 | writel(value, dev->mmio + reg); | |
150 | } | |
151 | ||
152 | static inline unsigned int max_channels(struct tw686x_dev *dev) | |
153 | { | |
154 | return dev->type & TYPE_MAX_CHANNELS; /* 4 or 8 channels */ | |
155 | } | |
156 | ||
157 | void tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel); | |
158 | void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel); | |
159 | ||
160 | int tw686x_video_init(struct tw686x_dev *dev); | |
161 | void tw686x_video_free(struct tw686x_dev *dev); | |
162 | void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests, | |
163 | unsigned int pb_status, unsigned int fifo_status, | |
164 | unsigned int *reset_ch); | |
165 | ||
166 | int tw686x_audio_init(struct tw686x_dev *dev); | |
167 | void tw686x_audio_free(struct tw686x_dev *dev); | |
168 | void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests, | |
169 | unsigned int pb_status); |