2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
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.
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.
24 #include <linux/pci.h>
25 #include <linux/i2c.h>
26 #include <linux/mutex.h>
27 #include <linux/list.h>
28 #include <linux/wait.h>
29 #include <linux/stringify.h>
31 #include <linux/atomic.h>
32 #include <linux/slab.h>
33 #include <linux/videodev2.h>
34 #include <linux/gpio/driver.h>
36 #include <media/v4l2-dev.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ctrls.h>
39 #include <media/videobuf2-v4l2.h>
41 #include "solo6x10-regs.h"
43 #ifndef PCI_VENDOR_ID_SOFTLOGIC
44 #define PCI_VENDOR_ID_SOFTLOGIC 0x9413
45 #define PCI_DEVICE_ID_SOLO6010 0x6010
46 #define PCI_DEVICE_ID_SOLO6110 0x6110
49 #ifndef PCI_VENDOR_ID_BLUECHERRY
50 #define PCI_VENDOR_ID_BLUECHERRY 0x1BB3
51 /* Neugent Softlogic 6010 based cards */
52 #define PCI_DEVICE_ID_NEUSOLO_4 0x4304
53 #define PCI_DEVICE_ID_NEUSOLO_9 0x4309
54 #define PCI_DEVICE_ID_NEUSOLO_16 0x4310
55 /* Bluecherry Softlogic 6010 based cards */
56 #define PCI_DEVICE_ID_BC_SOLO_4 0x4E04
57 #define PCI_DEVICE_ID_BC_SOLO_9 0x4E09
58 #define PCI_DEVICE_ID_BC_SOLO_16 0x4E10
59 /* Bluecherry Softlogic 6110 based cards */
60 #define PCI_DEVICE_ID_BC_6110_4 0x5304
61 #define PCI_DEVICE_ID_BC_6110_8 0x5308
62 #define PCI_DEVICE_ID_BC_6110_16 0x5310
63 #endif /* Bluecherry */
65 /* Used in pci_device_id, and solo_dev->type */
66 #define SOLO_DEV_6010 0
67 #define SOLO_DEV_6110 1
69 #define SOLO6X10_NAME "solo6x10"
71 #define SOLO_MAX_CHANNELS 16
73 #define SOLO6X10_VERSION "3.0.0"
76 * The SOLO6x10 actually has 8 i2c channels, but we only use 2.
77 * 0 - Techwell chip(s)
80 #define SOLO_I2C_ADAPTERS 2
82 #define SOLO_I2C_SAA 1
84 /* DMA Engine setup */
86 #define SOLO_NR_P2M_DESC 256
87 #define SOLO_P2M_DESC_SIZE (SOLO_NR_P2M_DESC * 16)
89 /* Encoder standard modes */
90 #define SOLO_ENC_MODE_CIF 2
91 #define SOLO_ENC_MODE_HD1 1
92 #define SOLO_ENC_MODE_D1 9
94 #define SOLO_DEFAULT_QP 3
96 #define SOLO_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
97 #define V4L2_CID_MOTION_TRACE (SOLO_CID_CUSTOM_BASE+2)
98 #define V4L2_CID_OSD_TEXT (SOLO_CID_CUSTOM_BASE+3)
101 * Motion thresholds are in a table of 64x64 samples, with
102 * each sample representing 16x16 pixels of the source. In
103 * effect, 44x30 samples are used for NTSC, and 44x36 for PAL.
104 * The 5th sample on the 10th row is (10*64)+5 = 645.
106 * Internally it is stored as a 45x45 array (45*16 = 720, which is the
107 * maximum PAL/NTSC width).
109 #define SOLO_MOTION_SZ (45)
111 enum SOLO_I2C_STATE {
119 /* Defined in Table 4-16, Page 68-69 of the 6010 Datasheet */
120 struct solo_p2m_desc {
127 struct solo_p2m_dev {
129 struct completion completion;
132 struct solo_p2m_desc *descs;
136 #define OSD_TEXT_MAX 44
138 struct solo_vb2_buf {
139 struct vb2_v4l2_buffer vb;
140 struct list_head list;
143 enum solo_enc_types {
148 struct solo_enc_dev {
149 struct solo_dev *solo_dev;
151 struct v4l2_ctrl_handler hdl;
152 struct v4l2_ctrl *md_thresholds;
153 struct video_device *vfd;
154 /* General accounting */
156 spinlock_t motion_lock;
158 u8 mode, gop, qp, interlaced, interval;
167 char osd_text[OSD_TEXT_MAX + 1];
168 u8 osd_buf[SOLO_EOSD_EXT_SIZE_MAX]
174 u8 jpeg_header[1024];
178 enum solo_enc_types type;
180 struct vb2_queue vidq;
181 struct list_head vidq_active;
184 struct solo_p2m_desc *desc_items;
189 /* The SOLO6x10 PCI Device */
192 struct pci_dev *pdev;
194 unsigned int time_sync;
195 unsigned int usec_lsb;
196 unsigned int clock_mhz;
197 u8 __iomem *reg_base;
202 struct v4l2_device v4l2_dev;
203 #ifdef CONFIG_GPIOLIB
205 struct gpio_chip gpio_dev;
208 /* tw28xx accounting */
209 u8 tw2865, tw2864, tw2815;
212 /* i2c related items */
213 struct i2c_adapter i2c_adap[SOLO_I2C_ADAPTERS];
214 enum SOLO_I2C_STATE i2c_state;
215 struct mutex i2c_mutex;
217 wait_queue_head_t i2c_wait;
218 struct i2c_msg *i2c_msg;
219 unsigned int i2c_msg_num;
220 unsigned int i2c_msg_ptr;
223 struct solo_p2m_dev p2m_dev[SOLO_NR_P2M];
226 unsigned int p2m_timeouts;
228 /* V4L2 Display items */
229 struct video_device *vfd;
230 unsigned int erasing;
231 unsigned int frame_blank;
233 wait_queue_head_t disp_thread_wait;
234 struct v4l2_ctrl_handler disp_hdl;
236 /* V4L2 Encoder items */
237 struct solo_enc_dev *v4l2_enc[SOLO_MAX_CHANNELS];
239 /* IDX into hw mp4 encoder */
242 /* Current video settings */
244 u16 video_hsize, video_vsize;
245 u16 vout_hstart, vout_vstart;
246 u16 vin_hstart, vin_vstart;
249 /* JPEG Qp setting */
250 spinlock_t jpeg_qp_lock;
253 /* Audio components */
254 struct snd_card *snd_card;
255 struct snd_pcm *snd_pcm;
262 struct bin_attribute sdram_attr;
263 unsigned int sys_config;
266 struct task_struct *ring_thread;
267 wait_queue_head_t ring_thread_wait;
269 /* VOP_HEADER handling */
274 /* Buffer handling */
275 struct vb2_queue vidq;
277 struct task_struct *kthread;
281 struct list_head vidq_active;
284 static inline u32 solo_reg_read(struct solo_dev *solo_dev, int reg)
286 return readl(solo_dev->reg_base + reg);
289 static inline void solo_reg_write(struct solo_dev *solo_dev, int reg,
294 writel(data, solo_dev->reg_base + reg);
295 pci_read_config_word(solo_dev->pdev, PCI_STATUS, &val);
298 static inline void solo_irq_on(struct solo_dev *dev, u32 mask)
300 dev->irq_mask |= mask;
301 solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask);
304 static inline void solo_irq_off(struct solo_dev *dev, u32 mask)
306 dev->irq_mask &= ~mask;
307 solo_reg_write(dev, SOLO_IRQ_MASK, dev->irq_mask);
310 /* Init/exit routines for subsystems */
311 int solo_disp_init(struct solo_dev *solo_dev);
312 void solo_disp_exit(struct solo_dev *solo_dev);
314 int solo_gpio_init(struct solo_dev *solo_dev);
315 void solo_gpio_exit(struct solo_dev *solo_dev);
317 int solo_i2c_init(struct solo_dev *solo_dev);
318 void solo_i2c_exit(struct solo_dev *solo_dev);
320 int solo_p2m_init(struct solo_dev *solo_dev);
321 void solo_p2m_exit(struct solo_dev *solo_dev);
323 int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr);
324 void solo_v4l2_exit(struct solo_dev *solo_dev);
326 int solo_enc_init(struct solo_dev *solo_dev);
327 void solo_enc_exit(struct solo_dev *solo_dev);
329 int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr);
330 void solo_enc_v4l2_exit(struct solo_dev *solo_dev);
332 int solo_g723_init(struct solo_dev *solo_dev);
333 void solo_g723_exit(struct solo_dev *solo_dev);
336 int solo_i2c_isr(struct solo_dev *solo_dev);
337 void solo_p2m_isr(struct solo_dev *solo_dev, int id);
338 void solo_p2m_error_isr(struct solo_dev *solo_dev);
339 void solo_enc_v4l2_isr(struct solo_dev *solo_dev);
340 void solo_g723_isr(struct solo_dev *solo_dev);
341 void solo_motion_isr(struct solo_dev *solo_dev);
342 void solo_video_in_isr(struct solo_dev *solo_dev);
345 u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off);
346 void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off,
350 int solo_p2m_dma_t(struct solo_dev *solo_dev, int wr,
351 dma_addr_t dma_addr, u32 ext_addr, u32 size,
352 int repeat, u32 ext_size);
353 int solo_p2m_dma(struct solo_dev *solo_dev, int wr,
354 void *sys_addr, u32 ext_addr, u32 size,
355 int repeat, u32 ext_size);
356 void solo_p2m_fill_desc(struct solo_p2m_desc *desc, int wr,
357 dma_addr_t dma_addr, u32 ext_addr, u32 size,
358 int repeat, u32 ext_size);
359 int solo_p2m_dma_desc(struct solo_dev *solo_dev,
360 struct solo_p2m_desc *desc, dma_addr_t desc_dma,
363 /* Global s_std ioctl */
364 int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz);
365 void solo_update_mode(struct solo_enc_dev *solo_enc);
367 /* Set the threshold for motion detection */
368 int solo_set_motion_threshold(struct solo_dev *solo_dev, u8 ch, u16 val);
369 int solo_set_motion_block(struct solo_dev *solo_dev, u8 ch,
370 const u16 *thresholds);
371 #define SOLO_DEF_MOT_THRESH 0x0300
373 /* Write text on OSD */
374 int solo_osd_print(struct solo_enc_dev *solo_enc);
376 /* EEPROM commands */
377 unsigned int solo_eeprom_ewen(struct solo_dev *solo_dev, int w_en);
378 __be16 solo_eeprom_read(struct solo_dev *solo_dev, int loc);
379 int solo_eeprom_write(struct solo_dev *solo_dev, int loc,
382 /* JPEG Qp functions */
383 void solo_s_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch,
385 int solo_g_jpeg_qp(struct solo_dev *solo_dev, unsigned int ch);
387 #define CHK_FLAGS(v, flags) (((v) & (flags)) == (flags))
389 #endif /* __SOLO6X10_H */