]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/media/solo6x10/solo6x10.h
[media] solo6x10: implement the motion detection event
[karo-tx-linux.git] / drivers / staging / media / solo6x10 / solo6x10.h
1 /*
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>
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
25 #ifndef __SOLO6X10_H
26 #define __SOLO6X10_H
27
28 #include <linux/pci.h>
29 #include <linux/i2c.h>
30 #include <linux/mutex.h>
31 #include <linux/list.h>
32 #include <linux/wait.h>
33 #include <linux/stringify.h>
34 #include <linux/io.h>
35 #include <linux/atomic.h>
36 #include <linux/slab.h>
37 #include <linux/videodev2.h>
38
39 #include <media/v4l2-dev.h>
40 #include <media/v4l2-device.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/videobuf2-core.h>
43
44 #include "solo6x10-regs.h"
45
46 #ifndef PCI_VENDOR_ID_SOFTLOGIC
47 #define PCI_VENDOR_ID_SOFTLOGIC         0x9413
48 #define PCI_DEVICE_ID_SOLO6010          0x6010
49 #define PCI_DEVICE_ID_SOLO6110          0x6110
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
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
66 #endif /* Bluecherry */
67
68 /* Used in pci_device_id, and solo_dev->type */
69 #define SOLO_DEV_6010                   0
70 #define SOLO_DEV_6110                   1
71
72 #define SOLO6X10_NAME                   "solo6x10"
73
74 #define SOLO_MAX_CHANNELS               16
75
76 #define SOLO6X10_VERSION                "3.0.0"
77
78 /*
79  * The SOLO6x10 actually has 8 i2c channels, but we only use 2.
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
90 #define SOLO_P2M_DESC_SIZE              (SOLO_NR_P2M_DESC * 16)
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_QP                 3
98
99 #define SOLO_CID_CUSTOM_BASE            (V4L2_CID_USER_BASE | 0xf000)
100 #define V4L2_CID_MOTION_TRACE           (SOLO_CID_CUSTOM_BASE+2)
101 #define V4L2_CID_OSD_TEXT               (SOLO_CID_CUSTOM_BASE+3)
102
103 /*
104  * Motion thresholds are in a table of 64x64 samples, with
105  * each sample representing 16x16 pixels of the source. In
106  * effect, 44x30 samples are used for NTSC, and 44x36 for PAL.
107  * The 5th sample on the 10th row is (10*64)+5 = 645.
108  *
109  * Internally it is stored as a 45x45 array (45*16 = 720, which is the
110  * maximum PAL/NTSC width).
111  */
112 #define SOLO_MOTION_SZ (45)
113
114 enum SOLO_I2C_STATE {
115         IIC_STATE_IDLE,
116         IIC_STATE_START,
117         IIC_STATE_READ,
118         IIC_STATE_WRITE,
119         IIC_STATE_STOP
120 };
121
122 /* Defined in Table 4-16, Page 68-69 of the 6010 Datasheet */
123 struct solo_p2m_desc {
124         u32     ctrl;
125         u32     cfg;
126         u32     dma_addr;
127         u32     ext_addr;
128 };
129
130 struct solo_p2m_dev {
131         struct mutex            mutex;
132         struct completion       completion;
133         int                     desc_count;
134         int                     desc_idx;
135         struct solo_p2m_desc    *descs;
136         int                     error;
137 };
138
139 #define OSD_TEXT_MAX            44
140
141 struct solo_vb2_buf {
142         struct vb2_buffer vb;
143         struct list_head list;
144 };
145
146 enum solo_enc_types {
147         SOLO_ENC_TYPE_STD,
148         SOLO_ENC_TYPE_EXT,
149 };
150
151 struct solo_enc_dev {
152         struct solo_dev *solo_dev;
153         /* V4L2 Items */
154         struct v4l2_ctrl_handler hdl;
155         struct v4l2_ctrl *md_thresholds;
156         struct video_device     *vfd;
157         /* General accounting */
158         struct mutex            lock;
159         spinlock_t              motion_lock;
160         u8                      ch;
161         u8                      mode, gop, qp, interlaced, interval;
162         u8                      bw_weight;
163         u16                     motion_thresh;
164         bool                    motion_global;
165         bool                    motion_enabled;
166         bool                    motion_last_state;
167         u8                      frames_since_last_motion;
168         u16                     width;
169         u16                     height;
170
171         /* OSD buffers */
172         char                    osd_text[OSD_TEXT_MAX + 1];
173         u8                      osd_buf[SOLO_EOSD_EXT_SIZE_MAX]
174                                         __aligned(4);
175
176         /* VOP stuff */
177         unsigned char           vop[64];
178         int                     vop_len;
179         unsigned char           jpeg_header[1024];
180         int                     jpeg_len;
181
182         u32                     fmt;
183         enum solo_enc_types     type;
184         u32                     sequence;
185         struct vb2_queue        vidq;
186         struct list_head        vidq_active;
187         int                     desc_count;
188         int                     desc_nelts;
189         struct solo_p2m_desc    *desc_items;
190         dma_addr_t              desc_dma;
191         spinlock_t              av_lock;
192 };
193
194 /* The SOLO6x10 PCI Device */
195 struct solo_dev {
196         /* General stuff */
197         struct pci_dev          *pdev;
198         int                     type;
199         unsigned int            time_sync;
200         unsigned int            usec_lsb;
201         unsigned int            clock_mhz;
202         u8 __iomem              *reg_base;
203         int                     nr_chans;
204         int                     nr_ext;
205         u32                     irq_mask;
206         u32                     motion_mask;
207         spinlock_t              reg_io_lock;
208         struct v4l2_device      v4l2_dev;
209
210         /* tw28xx accounting */
211         u8                      tw2865, tw2864, tw2815;
212         u8                      tw28_cnt;
213
214         /* i2c related items */
215         struct i2c_adapter      i2c_adap[SOLO_I2C_ADAPTERS];
216         enum SOLO_I2C_STATE     i2c_state;
217         struct mutex            i2c_mutex;
218         int                     i2c_id;
219         wait_queue_head_t       i2c_wait;
220         struct i2c_msg          *i2c_msg;
221         unsigned int            i2c_msg_num;
222         unsigned int            i2c_msg_ptr;
223
224         /* P2M DMA Engine */
225         struct solo_p2m_dev     p2m_dev[SOLO_NR_P2M];
226         atomic_t                p2m_count;
227         int                     p2m_jiffies;
228         unsigned int            p2m_timeouts;
229
230         /* V4L2 Display items */
231         struct video_device     *vfd;
232         unsigned int            erasing;
233         unsigned int            frame_blank;
234         u8                      cur_disp_ch;
235         wait_queue_head_t       disp_thread_wait;
236         struct v4l2_ctrl_handler disp_hdl;
237
238         /* V4L2 Encoder items */
239         struct solo_enc_dev     *v4l2_enc[SOLO_MAX_CHANNELS];
240         u16                     enc_bw_remain;
241         /* IDX into hw mp4 encoder */
242         u8                      enc_idx;
243
244         /* Current video settings */
245         u32                     video_type;
246         u16                     video_hsize, video_vsize;
247         u16                     vout_hstart, vout_vstart;
248         u16                     vin_hstart, vin_vstart;
249         u8                      fps;
250
251         /* JPEG Qp setting */
252         spinlock_t      jpeg_qp_lock;
253         u32             jpeg_qp[2];
254
255         /* Audio components */
256         struct snd_card         *snd_card;
257         struct snd_pcm          *snd_pcm;
258         atomic_t                snd_users;
259         int                     g723_hw_idx;
260
261         /* sysfs stuffs */
262         struct device           dev;
263         int                     sdram_size;
264         struct bin_attribute    sdram_attr;
265         unsigned int            sys_config;
266
267         /* Ring thread */
268         struct task_struct      *ring_thread;
269         wait_queue_head_t       ring_thread_wait;
270
271         /* VOP_HEADER handling */
272         void                    *vh_buf;
273         dma_addr_t              vh_dma;
274         int                     vh_size;
275
276         /* Buffer handling */
277         struct vb2_queue        vidq;
278         struct vb2_alloc_ctx    *alloc_ctx;
279         u32                     sequence;
280         struct task_struct      *kthread;
281         struct mutex            lock;
282         spinlock_t              slock;
283         int                     old_write;
284         struct list_head        vidq_active;
285 };
286
287 static inline u32 solo_reg_read(struct solo_dev *solo_dev, int reg)
288 {
289         unsigned long flags;
290         u32 ret;
291         u16 val;
292
293         spin_lock_irqsave(&solo_dev->reg_io_lock, flags);
294
295         ret = readl(solo_dev->reg_base + reg);
296         rmb();
297         pci_read_config_word(solo_dev->pdev, PCI_STATUS, &val);
298         rmb();
299
300         spin_unlock_irqrestore(&solo_dev->reg_io_lock, flags);
301
302         return ret;
303 }
304
305 static inline void solo_reg_write(struct solo_dev *solo_dev, int reg,
306                                   u32 data)
307 {
308         unsigned long flags;
309         u16 val;
310
311         spin_lock_irqsave(&solo_dev->reg_io_lock, flags);
312
313         writel(data, solo_dev->reg_base + reg);
314         wmb();
315         pci_read_config_word(solo_dev->pdev, PCI_STATUS, &val);
316         rmb();
317
318         spin_unlock_irqrestore(&solo_dev->reg_io_lock, flags);
319 }
320
321 static inline void solo_irq_on(struct solo_dev *dev, u32 mask)
322 {
323         dev->irq_mask |= mask;
324         solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask);
325 }
326
327 static inline void solo_irq_off(struct solo_dev *dev, u32 mask)
328 {
329         dev->irq_mask &= ~mask;
330         solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask);
331 }
332
333 /* Init/exit routines for subsystems */
334 int solo_disp_init(struct solo_dev *solo_dev);
335 void solo_disp_exit(struct solo_dev *solo_dev);
336
337 int solo_gpio_init(struct solo_dev *solo_dev);
338 void solo_gpio_exit(struct solo_dev *solo_dev);
339
340 int solo_i2c_init(struct solo_dev *solo_dev);
341 void solo_i2c_exit(struct solo_dev *solo_dev);
342
343 int solo_p2m_init(struct solo_dev *solo_dev);
344 void solo_p2m_exit(struct solo_dev *solo_dev);
345
346 int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr);
347 void solo_v4l2_exit(struct solo_dev *solo_dev);
348
349 int solo_enc_init(struct solo_dev *solo_dev);
350 void solo_enc_exit(struct solo_dev *solo_dev);
351
352 int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr);
353 void solo_enc_v4l2_exit(struct solo_dev *solo_dev);
354
355 int solo_g723_init(struct solo_dev *solo_dev);
356 void solo_g723_exit(struct solo_dev *solo_dev);
357
358 /* ISR's */
359 int solo_i2c_isr(struct solo_dev *solo_dev);
360 void solo_p2m_isr(struct solo_dev *solo_dev, int id);
361 void solo_p2m_error_isr(struct solo_dev *solo_dev);
362 void solo_enc_v4l2_isr(struct solo_dev *solo_dev);
363 void solo_g723_isr(struct solo_dev *solo_dev);
364 void solo_motion_isr(struct solo_dev *solo_dev);
365 void solo_video_in_isr(struct solo_dev *solo_dev);
366
367 /* i2c read/write */
368 u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off);
369 void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off,
370                         u8 data);
371
372 /* P2M DMA */
373 int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr,
374                    dma_addr_t dma_addr, u32 ext_addr, u32 size,
375                    int repeat, u32 ext_size);
376 int solo_p2m_dma(struct solo_dev *solo_dev, int wr,
377                  void *sys_addr, u32 ext_addr, u32 size,
378                  int repeat, u32 ext_size);
379 void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr,
380                         dma_addr_t dma_addr, u32 ext_addr, u32 size,
381                         int repeat, u32 ext_size);
382 int solo_p2m_dma_desc(struct solo_dev *solo_dev,
383                       struct solo_p2m_desc *desc, dma_addr_t desc_dma,
384                       int desc_cnt);
385
386 /* Global s_std ioctl */
387 int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz);
388 void solo_update_mode(struct solo_enc_dev *solo_enc);
389
390 /* Set the threshold for motion detection */
391 int solo_set_motion_threshold(struct solo_dev *solo_dev, u8 ch, u16 val);
392 int solo_set_motion_block(struct solo_dev *solo_dev, u8 ch,
393                 const u16 *thresholds);
394 #define SOLO_DEF_MOT_THRESH             0x0300
395
396 /* Write text on OSD */
397 int solo_osd_print(struct solo_enc_dev *solo_enc);
398
399 /* EEPROM commands */
400 unsigned int solo_eeprom_ewen(struct solo_dev *solo_dev, int w_en);
401 unsigned short solo_eeprom_read(struct solo_dev *solo_dev, int loc);
402 int solo_eeprom_write(struct solo_dev *solo_dev, int loc,
403                       unsigned short data);
404
405 /* JPEG Qp functions */
406 void solo_s_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch,
407                     unsigned int qp);
408 int solo_g_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch);
409
410 #define CHK_FLAGS(v, flags) (((v) & (flags)) == (flags))
411
412 #endif /* __SOLO6X10_H */