1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
6 * Ben Collins <bcollins@ubuntu.com>
9 * John Brooks <john.brooks@bluecherry.net>
15 #include <linux/pci.h>
16 #include <linux/i2c.h>
17 #include <linux/mutex.h>
18 #include <linux/list.h>
19 #include <linux/wait.h>
20 #include <linux/stringify.h>
22 #include <linux/atomic.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
25 #include <linux/gpio/driver.h>
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/videobuf2-v4l2.h>
32 #include "solo6x10-regs.h"
34 #ifndef PCI_VENDOR_ID_SOFTLOGIC
35 #define PCI_VENDOR_ID_SOFTLOGIC 0x9413
36 #define PCI_DEVICE_ID_SOLO6010 0x6010
37 #define PCI_DEVICE_ID_SOLO6110 0x6110
40 #ifndef PCI_VENDOR_ID_BLUECHERRY
41 #define PCI_VENDOR_ID_BLUECHERRY 0x1BB3
42 /* Neugent Softlogic 6010 based cards */
43 #define PCI_DEVICE_ID_NEUSOLO_4 0x4304
44 #define PCI_DEVICE_ID_NEUSOLO_9 0x4309
45 #define PCI_DEVICE_ID_NEUSOLO_16 0x4310
46 /* Bluecherry Softlogic 6010 based cards */
47 #define PCI_DEVICE_ID_BC_SOLO_4 0x4E04
48 #define PCI_DEVICE_ID_BC_SOLO_9 0x4E09
49 #define PCI_DEVICE_ID_BC_SOLO_16 0x4E10
50 /* Bluecherry Softlogic 6110 based cards */
51 #define PCI_DEVICE_ID_BC_6110_4 0x5304
52 #define PCI_DEVICE_ID_BC_6110_8 0x5308
53 #define PCI_DEVICE_ID_BC_6110_16 0x5310
54 #endif /* Bluecherry */
56 /* Used in pci_device_id, and solo_dev->type */
57 #define SOLO_DEV_6010 0
58 #define SOLO_DEV_6110 1
60 #define SOLO6X10_NAME "solo6x10"
62 #define SOLO_MAX_CHANNELS 16
64 #define SOLO6X10_VERSION "3.0.0"
67 * The SOLO6x10 actually has 8 i2c channels, but we only use 2.
68 * 0 - Techwell chip(s)
71 #define SOLO_I2C_ADAPTERS 2
73 #define SOLO_I2C_SAA 1
75 /* DMA Engine setup */
77 #define SOLO_NR_P2M_DESC 256
78 #define SOLO_P2M_DESC_SIZE (SOLO_NR_P2M_DESC * 16)
80 /* Encoder standard modes */
81 #define SOLO_ENC_MODE_CIF 2
82 #define SOLO_ENC_MODE_HD1 1
83 #define SOLO_ENC_MODE_D1 9
85 #define SOLO_DEFAULT_QP 3
87 #define SOLO_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
88 #define V4L2_CID_MOTION_TRACE (SOLO_CID_CUSTOM_BASE+2)
89 #define V4L2_CID_OSD_TEXT (SOLO_CID_CUSTOM_BASE+3)
92 * Motion thresholds are in a table of 64x64 samples, with
93 * each sample representing 16x16 pixels of the source. In
94 * effect, 44x30 samples are used for NTSC, and 44x36 for PAL.
95 * The 5th sample on the 10th row is (10*64)+5 = 645.
97 * Internally it is stored as a 45x45 array (45*16 = 720, which is the
98 * maximum PAL/NTSC width).
100 #define SOLO_MOTION_SZ (45)
102 enum SOLO_I2C_STATE
{
110 /* Defined in Table 4-16, Page 68-69 of the 6010 Datasheet */
111 struct solo_p2m_desc
{
118 struct solo_p2m_dev
{
120 struct completion completion
;
123 struct solo_p2m_desc
*descs
;
127 #define OSD_TEXT_MAX 44
129 struct solo_vb2_buf
{
130 struct vb2_v4l2_buffer vb
;
131 struct list_head list
;
134 enum solo_enc_types
{
139 struct solo_enc_dev
{
140 struct solo_dev
*solo_dev
;
142 struct v4l2_ctrl_handler hdl
;
143 struct v4l2_ctrl
*md_thresholds
;
144 struct video_device
*vfd
;
145 /* General accounting */
147 spinlock_t motion_lock
;
149 u8 mode
, gop
, qp
, interlaced
, interval
;
158 char osd_text
[OSD_TEXT_MAX
+ 1];
159 u8 osd_buf
[SOLO_EOSD_EXT_SIZE_MAX
]
165 u8 jpeg_header
[1024];
169 enum solo_enc_types type
;
171 struct vb2_queue vidq
;
172 struct list_head vidq_active
;
175 struct solo_p2m_desc
*desc_items
;
180 /* The SOLO6x10 PCI Device */
183 struct pci_dev
*pdev
;
185 unsigned int time_sync
;
186 unsigned int usec_lsb
;
187 unsigned int clock_mhz
;
188 u8 __iomem
*reg_base
;
193 struct v4l2_device v4l2_dev
;
194 #ifdef CONFIG_GPIOLIB
196 struct gpio_chip gpio_dev
;
199 /* tw28xx accounting */
200 u8 tw2865
, tw2864
, tw2815
;
203 /* i2c related items */
204 struct i2c_adapter i2c_adap
[SOLO_I2C_ADAPTERS
];
205 enum SOLO_I2C_STATE i2c_state
;
206 struct mutex i2c_mutex
;
208 wait_queue_head_t i2c_wait
;
209 struct i2c_msg
*i2c_msg
;
210 unsigned int i2c_msg_num
;
211 unsigned int i2c_msg_ptr
;
214 struct solo_p2m_dev p2m_dev
[SOLO_NR_P2M
];
217 unsigned int p2m_timeouts
;
219 /* V4L2 Display items */
220 struct video_device
*vfd
;
221 unsigned int erasing
;
222 unsigned int frame_blank
;
224 wait_queue_head_t disp_thread_wait
;
225 struct v4l2_ctrl_handler disp_hdl
;
227 /* V4L2 Encoder items */
228 struct solo_enc_dev
*v4l2_enc
[SOLO_MAX_CHANNELS
];
230 /* IDX into hw mp4 encoder */
233 /* Current video settings */
235 u16 video_hsize
, video_vsize
;
236 u16 vout_hstart
, vout_vstart
;
237 u16 vin_hstart
, vin_vstart
;
240 /* JPEG Qp setting */
241 spinlock_t jpeg_qp_lock
;
244 /* Audio components */
245 struct snd_card
*snd_card
;
246 struct snd_pcm
*snd_pcm
;
253 struct bin_attribute sdram_attr
;
254 unsigned int sys_config
;
257 struct task_struct
*ring_thread
;
258 wait_queue_head_t ring_thread_wait
;
260 /* VOP_HEADER handling */
265 /* Buffer handling */
266 struct vb2_queue vidq
;
268 struct task_struct
*kthread
;
272 struct list_head vidq_active
;
275 static inline u32
solo_reg_read(struct solo_dev
*solo_dev
, int reg
)
277 return readl(solo_dev
->reg_base
+ reg
);
280 static inline void solo_reg_write(struct solo_dev
*solo_dev
, int reg
,
285 writel(data
, solo_dev
->reg_base
+ reg
);
286 pci_read_config_word(solo_dev
->pdev
, PCI_STATUS
, &val
);
289 static inline void solo_irq_on(struct solo_dev
*dev
, u32 mask
)
291 dev
->irq_mask
|= mask
;
292 solo_reg_write(dev
, SOLO_IRQ_MASK
, dev
->irq_mask
);
295 static inline void solo_irq_off(struct solo_dev
*dev
, u32 mask
)
297 dev
->irq_mask
&= ~mask
;
298 solo_reg_write(dev
, SOLO_IRQ_MASK
, dev
->irq_mask
);
301 /* Init/exit routines for subsystems */
302 int solo_disp_init(struct solo_dev
*solo_dev
);
303 void solo_disp_exit(struct solo_dev
*solo_dev
);
305 int solo_gpio_init(struct solo_dev
*solo_dev
);
306 void solo_gpio_exit(struct solo_dev
*solo_dev
);
308 int solo_i2c_init(struct solo_dev
*solo_dev
);
309 void solo_i2c_exit(struct solo_dev
*solo_dev
);
311 int solo_p2m_init(struct solo_dev
*solo_dev
);
312 void solo_p2m_exit(struct solo_dev
*solo_dev
);
314 int solo_v4l2_init(struct solo_dev
*solo_dev
, unsigned nr
);
315 void solo_v4l2_exit(struct solo_dev
*solo_dev
);
317 int solo_enc_init(struct solo_dev
*solo_dev
);
318 void solo_enc_exit(struct solo_dev
*solo_dev
);
320 int solo_enc_v4l2_init(struct solo_dev
*solo_dev
, unsigned nr
);
321 void solo_enc_v4l2_exit(struct solo_dev
*solo_dev
);
323 int solo_g723_init(struct solo_dev
*solo_dev
);
324 void solo_g723_exit(struct solo_dev
*solo_dev
);
327 int solo_i2c_isr(struct solo_dev
*solo_dev
);
328 void solo_p2m_isr(struct solo_dev
*solo_dev
, int id
);
329 void solo_p2m_error_isr(struct solo_dev
*solo_dev
);
330 void solo_enc_v4l2_isr(struct solo_dev
*solo_dev
);
331 void solo_g723_isr(struct solo_dev
*solo_dev
);
332 void solo_motion_isr(struct solo_dev
*solo_dev
);
333 void solo_video_in_isr(struct solo_dev
*solo_dev
);
336 u8
solo_i2c_readbyte(struct solo_dev
*solo_dev
, int id
, u8 addr
, u8 off
);
337 void solo_i2c_writebyte(struct solo_dev
*solo_dev
, int id
, u8 addr
, u8 off
,
341 int solo_p2m_dma_t(struct solo_dev
*solo_dev
, int wr
,
342 dma_addr_t dma_addr
, u32 ext_addr
, u32 size
,
343 int repeat
, u32 ext_size
);
344 int solo_p2m_dma(struct solo_dev
*solo_dev
, int wr
,
345 void *sys_addr
, u32 ext_addr
, u32 size
,
346 int repeat
, u32 ext_size
);
347 void solo_p2m_fill_desc(struct solo_p2m_desc
*desc
, int wr
,
348 dma_addr_t dma_addr
, u32 ext_addr
, u32 size
,
349 int repeat
, u32 ext_size
);
350 int solo_p2m_dma_desc(struct solo_dev
*solo_dev
,
351 struct solo_p2m_desc
*desc
, dma_addr_t desc_dma
,
354 /* Global s_std ioctl */
355 int solo_set_video_type(struct solo_dev
*solo_dev
, bool is_50hz
);
356 void solo_update_mode(struct solo_enc_dev
*solo_enc
);
358 /* Set the threshold for motion detection */
359 int solo_set_motion_threshold(struct solo_dev
*solo_dev
, u8 ch
, u16 val
);
360 int solo_set_motion_block(struct solo_dev
*solo_dev
, u8 ch
,
361 const u16
*thresholds
);
362 #define SOLO_DEF_MOT_THRESH 0x0300
364 /* Write text on OSD */
365 int solo_osd_print(struct solo_enc_dev
*solo_enc
);
367 /* EEPROM commands */
368 unsigned int solo_eeprom_ewen(struct solo_dev
*solo_dev
, int w_en
);
369 __be16
solo_eeprom_read(struct solo_dev
*solo_dev
, int loc
);
370 int solo_eeprom_write(struct solo_dev
*solo_dev
, int loc
,
373 /* JPEG Qp functions */
374 void solo_s_jpeg_qp(struct solo_dev
*solo_dev
, unsigned int ch
,
376 int solo_g_jpeg_qp(struct solo_dev
*solo_dev
, unsigned int ch
);
378 #define CHK_FLAGS(v, flags) (((v) & (flags)) == (flags))
380 #endif /* __SOLO6X10_H */