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