Commit | Line | Data |
---|---|---|
faa4fd2a | 1 | /* |
dcae5dac HV |
2 | * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com> |
3 | * | |
4 | * Original author: | |
5 | * Ben Collins <bcollins@ubuntu.com> | |
6 | * | |
7 | * Additional work by: | |
8 | * John Brooks <john.brooks@bluecherry.net> | |
faa4fd2a BC |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | */ | |
24 | ||
decebabf KH |
25 | #ifndef __SOLO6X10_H |
26 | #define __SOLO6X10_H | |
faa4fd2a | 27 | |
faa4fd2a BC |
28 | #include <linux/pci.h> |
29 | #include <linux/i2c.h> | |
faa4fd2a BC |
30 | #include <linux/mutex.h> |
31 | #include <linux/list.h> | |
32 | #include <linux/wait.h> | |
dcae5dac HV |
33 | #include <linux/stringify.h> |
34 | #include <linux/io.h> | |
60063497 | 35 | #include <linux/atomic.h> |
dcae5dac | 36 | #include <linux/slab.h> |
faa4fd2a | 37 | #include <linux/videodev2.h> |
dcae5dac | 38 | |
faa4fd2a | 39 | #include <media/v4l2-dev.h> |
d9ebd623 | 40 | #include <media/v4l2-device.h> |
c813bd3c | 41 | #include <media/v4l2-ctrls.h> |
faa4fd2a | 42 | #include <media/videobuf-core.h> |
dcae5dac | 43 | |
ae69b22c | 44 | #include "registers.h" |
faa4fd2a BC |
45 | |
46 | #ifndef PCI_VENDOR_ID_SOFTLOGIC | |
47 | #define PCI_VENDOR_ID_SOFTLOGIC 0x9413 | |
48 | #define PCI_DEVICE_ID_SOLO6010 0x6010 | |
908113d8 | 49 | #define PCI_DEVICE_ID_SOLO6110 0x6110 |
faa4fd2a BC |
50 | #endif |
51 | ||
52 | #ifndef PCI_VENDOR_ID_BLUECHERRY | |
53 | #define PCI_VENDOR_ID_BLUECHERRY 0x1BB3 | |
54 | /* Neugent Softlogic 6010 based cards */ | |
55 | #define PCI_DEVICE_ID_NEUSOLO_4 0x4304 | |
56 | #define PCI_DEVICE_ID_NEUSOLO_9 0x4309 | |
57 | #define PCI_DEVICE_ID_NEUSOLO_16 0x4310 | |
f62de9be BC |
58 | /* Bluecherry Softlogic 6010 based cards */ |
59 | #define PCI_DEVICE_ID_BC_SOLO_4 0x4E04 | |
60 | #define PCI_DEVICE_ID_BC_SOLO_9 0x4E09 | |
61 | #define PCI_DEVICE_ID_BC_SOLO_16 0x4E10 | |
62 | /* Bluecherry Softlogic 6110 based cards */ | |
63 | #define PCI_DEVICE_ID_BC_6110_4 0x5304 | |
64 | #define PCI_DEVICE_ID_BC_6110_8 0x5308 | |
65 | #define PCI_DEVICE_ID_BC_6110_16 0x5310 | |
faa4fd2a BC |
66 | #endif /* Bluecherry */ |
67 | ||
dcae5dac HV |
68 | /* Used in pci_device_id, and solo_dev->type */ |
69 | #define SOLO_DEV_6010 0 | |
70 | #define SOLO_DEV_6110 1 | |
71 | ||
decebabf | 72 | #define SOLO6X10_NAME "solo6x10" |
faa4fd2a BC |
73 | |
74 | #define SOLO_MAX_CHANNELS 16 | |
75 | ||
20c5f492 | 76 | #define SOLO6X10_VERSION "3.0.0" |
908113d8 | 77 | |
faa4fd2a | 78 | /* |
decebabf | 79 | * The SOLO6x10 actually has 8 i2c channels, but we only use 2. |
faa4fd2a BC |
80 | * 0 - Techwell chip(s) |
81 | * 1 - SAA7128 | |
82 | */ | |
83 | #define SOLO_I2C_ADAPTERS 2 | |
84 | #define SOLO_I2C_TW 0 | |
85 | #define SOLO_I2C_SAA 1 | |
86 | ||
87 | /* DMA Engine setup */ | |
88 | #define SOLO_NR_P2M 4 | |
89 | #define SOLO_NR_P2M_DESC 256 | |
dcae5dac | 90 | #define SOLO_P2M_DESC_SIZE (SOLO_NR_P2M_DESC * 16) |
faa4fd2a BC |
91 | |
92 | /* Encoder standard modes */ | |
93 | #define SOLO_ENC_MODE_CIF 2 | |
94 | #define SOLO_ENC_MODE_HD1 1 | |
95 | #define SOLO_ENC_MODE_D1 9 | |
96 | ||
97 | #define SOLO_DEFAULT_GOP 30 | |
98 | #define SOLO_DEFAULT_QP 3 | |
99 | ||
faa4fd2a BC |
100 | #ifndef V4L2_BUF_FLAG_MOTION_ON |
101 | #define V4L2_BUF_FLAG_MOTION_ON 0x0400 | |
102 | #define V4L2_BUF_FLAG_MOTION_DETECTED 0x0800 | |
103 | #endif | |
c813bd3c HV |
104 | |
105 | #define SOLO_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000) | |
106 | #define V4L2_CID_MOTION_ENABLE (SOLO_CID_CUSTOM_BASE+0) | |
107 | #define V4L2_CID_MOTION_THRESHOLD (SOLO_CID_CUSTOM_BASE+1) | |
108 | #define V4L2_CID_MOTION_TRACE (SOLO_CID_CUSTOM_BASE+2) | |
109 | #define V4L2_CID_OSD_TEXT (SOLO_CID_CUSTOM_BASE+3) | |
faa4fd2a BC |
110 | |
111 | enum SOLO_I2C_STATE { | |
112 | IIC_STATE_IDLE, | |
113 | IIC_STATE_START, | |
114 | IIC_STATE_READ, | |
115 | IIC_STATE_WRITE, | |
116 | IIC_STATE_STOP | |
117 | }; | |
118 | ||
dcae5dac HV |
119 | /* Defined in Table 4-16, Page 68-69 of the 6010 Datasheet */ |
120 | struct solo_p2m_desc { | |
121 | u32 ctrl; | |
122 | u32 cfg; | |
123 | u32 dma_addr; | |
124 | u32 ext_addr; | |
f62de9be BC |
125 | }; |
126 | ||
faa4fd2a | 127 | struct solo_p2m_dev { |
f62de9be | 128 | struct mutex mutex; |
faa4fd2a | 129 | struct completion completion; |
dcae5dac HV |
130 | int desc_count; |
131 | int desc_idx; | |
132 | struct solo_p2m_desc *descs; | |
faa4fd2a | 133 | int error; |
faa4fd2a BC |
134 | }; |
135 | ||
dcae5dac | 136 | #define OSD_TEXT_MAX 44 |
faa4fd2a BC |
137 | |
138 | struct solo_enc_dev { | |
dcae5dac | 139 | struct solo_dev *solo_dev; |
faa4fd2a | 140 | /* V4L2 Items */ |
c813bd3c | 141 | struct v4l2_ctrl_handler hdl; |
faa4fd2a BC |
142 | struct video_device *vfd; |
143 | /* General accounting */ | |
dcae5dac HV |
144 | struct mutex enable_lock; |
145 | spinlock_t motion_lock; | |
faa4fd2a | 146 | atomic_t readers; |
dcae5dac | 147 | atomic_t mpeg_readers; |
faa4fd2a BC |
148 | u8 ch; |
149 | u8 mode, gop, qp, interlaced, interval; | |
faa4fd2a | 150 | u8 bw_weight; |
faa4fd2a BC |
151 | u16 motion_thresh; |
152 | u16 width; | |
153 | u16 height; | |
dcae5dac HV |
154 | |
155 | /* OSD buffers */ | |
faa4fd2a | 156 | char osd_text[OSD_TEXT_MAX + 1]; |
dcae5dac HV |
157 | u8 osd_buf[SOLO_EOSD_EXT_SIZE_MAX] |
158 | __aligned(4); | |
faa4fd2a | 159 | |
dcae5dac HV |
160 | /* VOP stuff */ |
161 | unsigned char vop[64]; | |
162 | int vop_len; | |
163 | unsigned char jpeg_header[1024]; | |
164 | int jpeg_len; | |
165 | ||
166 | /* File handles that are listening for buffers */ | |
167 | struct list_head listeners; | |
faa4fd2a BC |
168 | }; |
169 | ||
decebabf KH |
170 | /* The SOLO6x10 PCI Device */ |
171 | struct solo_dev { | |
faa4fd2a BC |
172 | /* General stuff */ |
173 | struct pci_dev *pdev; | |
dcae5dac HV |
174 | int type; |
175 | unsigned int time_sync; | |
176 | unsigned int usec_lsb; | |
177 | unsigned int clock_mhz; | |
faa4fd2a BC |
178 | u8 __iomem *reg_base; |
179 | int nr_chans; | |
180 | int nr_ext; | |
181 | u32 irq_mask; | |
182 | u32 motion_mask; | |
183 | spinlock_t reg_io_lock; | |
d9ebd623 | 184 | struct v4l2_device v4l2_dev; |
faa4fd2a BC |
185 | |
186 | /* tw28xx accounting */ | |
ee6351f5 | 187 | u8 tw2865, tw2864, tw2815; |
faa4fd2a BC |
188 | u8 tw28_cnt; |
189 | ||
190 | /* i2c related items */ | |
191 | struct i2c_adapter i2c_adap[SOLO_I2C_ADAPTERS]; | |
192 | enum SOLO_I2C_STATE i2c_state; | |
f62de9be | 193 | struct mutex i2c_mutex; |
faa4fd2a BC |
194 | int i2c_id; |
195 | wait_queue_head_t i2c_wait; | |
196 | struct i2c_msg *i2c_msg; | |
197 | unsigned int i2c_msg_num; | |
198 | unsigned int i2c_msg_ptr; | |
199 | ||
200 | /* P2M DMA Engine */ | |
201 | struct solo_p2m_dev p2m_dev[SOLO_NR_P2M]; | |
dcae5dac HV |
202 | atomic_t p2m_count; |
203 | int p2m_jiffies; | |
204 | unsigned int p2m_timeouts; | |
faa4fd2a BC |
205 | |
206 | /* V4L2 Display items */ | |
207 | struct video_device *vfd; | |
208 | unsigned int erasing; | |
209 | unsigned int frame_blank; | |
210 | u8 cur_disp_ch; | |
211 | wait_queue_head_t disp_thread_wait; | |
c813bd3c | 212 | struct v4l2_ctrl_handler disp_hdl; |
faa4fd2a BC |
213 | |
214 | /* V4L2 Encoder items */ | |
215 | struct solo_enc_dev *v4l2_enc[SOLO_MAX_CHANNELS]; | |
216 | u16 enc_bw_remain; | |
217 | /* IDX into hw mp4 encoder */ | |
218 | u8 enc_idx; | |
faa4fd2a BC |
219 | |
220 | /* Current video settings */ | |
afabbe6d | 221 | u32 video_type; |
faa4fd2a BC |
222 | u16 video_hsize, video_vsize; |
223 | u16 vout_hstart, vout_vstart; | |
224 | u16 vin_hstart, vin_vstart; | |
225 | u8 fps; | |
226 | ||
dcae5dac HV |
227 | /* JPEG Qp setting */ |
228 | spinlock_t jpeg_qp_lock; | |
229 | u32 jpeg_qp[2]; | |
230 | ||
faa4fd2a BC |
231 | /* Audio components */ |
232 | struct snd_card *snd_card; | |
233 | struct snd_pcm *snd_pcm; | |
234 | atomic_t snd_users; | |
235 | int g723_hw_idx; | |
dcae5dac HV |
236 | |
237 | /* sysfs stuffs */ | |
238 | struct device dev; | |
239 | int sdram_size; | |
240 | struct bin_attribute sdram_attr; | |
241 | unsigned int sys_config; | |
242 | ||
243 | /* Ring thread */ | |
244 | struct task_struct *ring_thread; | |
245 | wait_queue_head_t ring_thread_wait; | |
246 | atomic_t enc_users; | |
247 | atomic_t disp_users; | |
248 | ||
249 | /* VOP_HEADER handling */ | |
250 | void *vh_buf; | |
251 | dma_addr_t vh_dma; | |
252 | int vh_size; | |
6a2e65d5 HV |
253 | |
254 | /* Buffer handling */ | |
255 | struct videobuf_queue vidq; | |
256 | struct task_struct *kthread; | |
257 | spinlock_t slock; | |
258 | int old_write; | |
259 | struct list_head vidq_active; | |
faa4fd2a BC |
260 | }; |
261 | ||
decebabf | 262 | static inline u32 solo_reg_read(struct solo_dev *solo_dev, int reg) |
faa4fd2a BC |
263 | { |
264 | unsigned long flags; | |
265 | u32 ret; | |
266 | u16 val; | |
267 | ||
268 | spin_lock_irqsave(&solo_dev->reg_io_lock, flags); | |
269 | ||
270 | ret = readl(solo_dev->reg_base + reg); | |
271 | rmb(); | |
272 | pci_read_config_word(solo_dev->pdev, PCI_STATUS, &val); | |
273 | rmb(); | |
274 | ||
275 | spin_unlock_irqrestore(&solo_dev->reg_io_lock, flags); | |
276 | ||
277 | return ret; | |
278 | } | |
279 | ||
dcae5dac HV |
280 | static inline void solo_reg_write(struct solo_dev *solo_dev, int reg, |
281 | u32 data) | |
faa4fd2a BC |
282 | { |
283 | unsigned long flags; | |
284 | u16 val; | |
285 | ||
286 | spin_lock_irqsave(&solo_dev->reg_io_lock, flags); | |
287 | ||
288 | writel(data, solo_dev->reg_base + reg); | |
289 | wmb(); | |
290 | pci_read_config_word(solo_dev->pdev, PCI_STATUS, &val); | |
291 | rmb(); | |
292 | ||
293 | spin_unlock_irqrestore(&solo_dev->reg_io_lock, flags); | |
294 | } | |
295 | ||
dcae5dac HV |
296 | static inline void solo_irq_on(struct solo_dev *dev, u32 mask) |
297 | { | |
298 | dev->irq_mask |= mask; | |
299 | solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask); | |
300 | } | |
301 | ||
302 | static inline void solo_irq_off(struct solo_dev *dev, u32 mask) | |
303 | { | |
304 | dev->irq_mask &= ~mask; | |
305 | solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask); | |
306 | } | |
faa4fd2a BC |
307 | |
308 | /* Init/exit routeines for subsystems */ | |
decebabf KH |
309 | int solo_disp_init(struct solo_dev *solo_dev); |
310 | void solo_disp_exit(struct solo_dev *solo_dev); | |
faa4fd2a | 311 | |
decebabf KH |
312 | int solo_gpio_init(struct solo_dev *solo_dev); |
313 | void solo_gpio_exit(struct solo_dev *solo_dev); | |
faa4fd2a | 314 | |
decebabf KH |
315 | int solo_i2c_init(struct solo_dev *solo_dev); |
316 | void solo_i2c_exit(struct solo_dev *solo_dev); | |
faa4fd2a | 317 | |
decebabf KH |
318 | int solo_p2m_init(struct solo_dev *solo_dev); |
319 | void solo_p2m_exit(struct solo_dev *solo_dev); | |
faa4fd2a | 320 | |
dcae5dac | 321 | int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr); |
decebabf | 322 | void solo_v4l2_exit(struct solo_dev *solo_dev); |
faa4fd2a | 323 | |
decebabf KH |
324 | int solo_enc_init(struct solo_dev *solo_dev); |
325 | void solo_enc_exit(struct solo_dev *solo_dev); | |
faa4fd2a | 326 | |
dcae5dac | 327 | int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr); |
decebabf | 328 | void solo_enc_v4l2_exit(struct solo_dev *solo_dev); |
faa4fd2a | 329 | |
decebabf KH |
330 | int solo_g723_init(struct solo_dev *solo_dev); |
331 | void solo_g723_exit(struct solo_dev *solo_dev); | |
faa4fd2a BC |
332 | |
333 | /* ISR's */ | |
decebabf KH |
334 | int solo_i2c_isr(struct solo_dev *solo_dev); |
335 | void solo_p2m_isr(struct solo_dev *solo_dev, int id); | |
dcae5dac | 336 | void solo_p2m_error_isr(struct solo_dev *solo_dev); |
decebabf KH |
337 | void solo_enc_v4l2_isr(struct solo_dev *solo_dev); |
338 | void solo_g723_isr(struct solo_dev *solo_dev); | |
339 | void solo_motion_isr(struct solo_dev *solo_dev); | |
340 | void solo_video_in_isr(struct solo_dev *solo_dev); | |
faa4fd2a BC |
341 | |
342 | /* i2c read/write */ | |
decebabf KH |
343 | u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off); |
344 | void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off, | |
faa4fd2a BC |
345 | u8 data); |
346 | ||
347 | /* P2M DMA */ | |
dcae5dac HV |
348 | int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr, |
349 | dma_addr_t dma_addr, u32 ext_addr, u32 size, | |
350 | int repeat, u32 ext_size); | |
351 | int solo_p2m_dma(struct solo_dev *solo_dev, int wr, | |
352 | void *sys_addr, u32 ext_addr, u32 size, | |
353 | int repeat, u32 ext_size); | |
354 | void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr, | |
355 | dma_addr_t dma_addr, u32 ext_addr, u32 size, | |
356 | int repeat, u32 ext_size); | |
357 | int solo_p2m_dma_desc(struct solo_dev *solo_dev, | |
358 | struct solo_p2m_desc *desc, dma_addr_t desc_dma, | |
359 | int desc_cnt); | |
faa4fd2a BC |
360 | |
361 | /* Set the threshold for motion detection */ | |
dcae5dac HV |
362 | int solo_set_motion_threshold(struct solo_dev *solo_dev, u8 ch, u16 val); |
363 | int solo_set_motion_block(struct solo_dev *solo_dev, u8 ch, u16 val, | |
364 | u16 block); | |
faa4fd2a BC |
365 | #define SOLO_DEF_MOT_THRESH 0x0300 |
366 | ||
367 | /* Write text on OSD */ | |
368 | int solo_osd_print(struct solo_enc_dev *solo_enc); | |
369 | ||
dcae5dac HV |
370 | /* EEPROM commands */ |
371 | unsigned int solo_eeprom_ewen(struct solo_dev *solo_dev, int w_en); | |
372 | unsigned short solo_eeprom_read(struct solo_dev *solo_dev, int loc); | |
373 | int solo_eeprom_write(struct solo_dev *solo_dev, int loc, | |
374 | unsigned short data); | |
375 | ||
376 | /* JPEG Qp functions */ | |
377 | void solo_s_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch, | |
378 | unsigned int qp); | |
379 | int solo_g_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch); | |
380 | ||
381 | #define CHK_FLAGS(v, flags) (((v) & (flags)) == (flags)) | |
382 | ||
decebabf | 383 | #endif /* __SOLO6X10_H */ |