cx25821.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. *
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #ifndef CX25821_H_
  24. #define CX25821_H_
  25. #include <linux/pci.h>
  26. #include <linux/i2c.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/sched.h>
  30. #include <linux/kdev_t.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/videobuf-dma-sg.h>
  35. #include "btcx-risc.h"
  36. #include "cx25821-reg.h"
  37. #include "cx25821-medusa-reg.h"
  38. #include "cx25821-sram.h"
  39. #include "cx25821-audio.h"
  40. #include <linux/version.h>
  41. #include <linux/mutex.h>
  42. #define CX25821_VERSION_CODE KERNEL_VERSION(0, 0, 106)
  43. #define UNSET (-1U)
  44. #define NO_SYNC_LINE (-1U)
  45. #define CX25821_MAXBOARDS 2
  46. #define LINE_SIZE_D1 1440
  47. /* Number of decoders and encoders */
  48. #define MAX_DECODERS 8
  49. #define MAX_ENCODERS 2
  50. #define QUAD_DECODERS 4
  51. #define MAX_CAMERAS 16
  52. /* Max number of inputs by card */
  53. #define MAX_CX25821_INPUT 8
  54. #define INPUT(nr) (&cx25821_boards[dev->board].input[nr])
  55. #define RESOURCE_VIDEO0 1
  56. #define RESOURCE_VIDEO1 2
  57. #define RESOURCE_VIDEO2 4
  58. #define RESOURCE_VIDEO3 8
  59. #define RESOURCE_VIDEO4 16
  60. #define RESOURCE_VIDEO5 32
  61. #define RESOURCE_VIDEO6 64
  62. #define RESOURCE_VIDEO7 128
  63. #define RESOURCE_VIDEO8 256
  64. #define RESOURCE_VIDEO9 512
  65. #define RESOURCE_VIDEO10 1024
  66. #define RESOURCE_VIDEO11 2048
  67. #define BUFFER_TIMEOUT (HZ) /* 0.5 seconds */
  68. #define UNKNOWN_BOARD 0
  69. #define CX25821_BOARD 1
  70. /* Currently supported by the driver */
  71. #define CX25821_NORMS (\
  72. V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR | \
  73. V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_I | \
  74. V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_H | \
  75. V4L2_STD_PAL_Nc)
  76. #define CX25821_BOARD_CONEXANT_ATHENA10 1
  77. #define MAX_VID_CHANNEL_NUM 12
  78. #define VID_CHANNEL_NUM 8
  79. #define CX25821_NR_INPUT 2
  80. struct cx25821_fmt {
  81. char *name;
  82. u32 fourcc; /* v4l2 format id */
  83. int depth;
  84. int flags;
  85. u32 cxformat;
  86. };
  87. struct cx25821_ctrl {
  88. struct v4l2_queryctrl v;
  89. u32 off;
  90. u32 reg;
  91. u32 mask;
  92. u32 shift;
  93. };
  94. struct cx25821_tvnorm {
  95. char *name;
  96. v4l2_std_id id;
  97. u32 cxiformat;
  98. u32 cxoformat;
  99. };
  100. struct cx25821_fh {
  101. struct cx25821_dev *dev;
  102. enum v4l2_buf_type type;
  103. u32 resources;
  104. enum v4l2_priority prio;
  105. /* video capture */
  106. const struct cx25821_fmt *fmt;
  107. unsigned int width, height;
  108. int channel_id;
  109. struct videobuf_queue vidq;
  110. };
  111. enum cx25821_itype {
  112. CX25821_VMUX_COMPOSITE = 1,
  113. CX25821_VMUX_SVIDEO,
  114. CX25821_VMUX_DEBUG,
  115. };
  116. enum cx25821_src_sel_type {
  117. CX25821_SRC_SEL_EXT_656_VIDEO = 0,
  118. CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
  119. };
  120. /* buffer for one video frame */
  121. struct cx25821_buffer {
  122. /* common v4l buffer stuff -- must be first */
  123. struct videobuf_buffer vb;
  124. /* cx25821 specific */
  125. unsigned int bpl;
  126. struct btcx_riscmem risc;
  127. const struct cx25821_fmt *fmt;
  128. u32 count;
  129. };
  130. struct cx25821_input {
  131. enum cx25821_itype type;
  132. unsigned int vmux;
  133. u32 gpio0, gpio1, gpio2, gpio3;
  134. };
  135. enum port {
  136. CX25821_UNDEFINED = 0,
  137. CX25821_RAW,
  138. CX25821_264
  139. };
  140. struct cx25821_board {
  141. const char *name;
  142. enum port porta;
  143. enum port portb;
  144. enum port portc;
  145. u32 clk_freq;
  146. struct cx25821_input input[CX25821_NR_INPUT];
  147. };
  148. struct cx25821_i2c {
  149. struct cx25821_dev *dev;
  150. int nr;
  151. /* i2c i/o */
  152. struct i2c_adapter i2c_adap;
  153. struct i2c_client i2c_client;
  154. u32 i2c_rc;
  155. /* cx25821 registers used for raw addess */
  156. u32 i2c_period;
  157. u32 reg_ctrl;
  158. u32 reg_stat;
  159. u32 reg_addr;
  160. u32 reg_rdata;
  161. u32 reg_wdata;
  162. };
  163. struct cx25821_dmaqueue {
  164. struct list_head active;
  165. struct list_head queued;
  166. struct timer_list timeout;
  167. struct btcx_riscmem stopper;
  168. u32 count;
  169. };
  170. struct cx25821_data {
  171. struct cx25821_dev *dev;
  172. const struct sram_channel *channel;
  173. };
  174. struct cx25821_dev;
  175. struct cx25821_channel {
  176. unsigned id;
  177. struct cx25821_dev *dev;
  178. struct v4l2_prio_state prio;
  179. struct v4l2_ctrl_handler hdl;
  180. struct cx25821_data timeout_data;
  181. struct video_device vdev;
  182. struct cx25821_dmaqueue vidq;
  183. const struct sram_channel *sram_channels;
  184. struct mutex lock;
  185. int resources;
  186. int pixel_formats;
  187. int use_cif_resolution;
  188. int cif_width;
  189. };
  190. struct snd_card;
  191. struct cx25821_dev {
  192. struct v4l2_device v4l2_dev;
  193. /* pci stuff */
  194. struct pci_dev *pci;
  195. unsigned char pci_rev, pci_lat;
  196. int pci_bus, pci_slot;
  197. u32 base_io_addr;
  198. u32 __iomem *lmmio;
  199. u8 __iomem *bmmio;
  200. int pci_irqmask;
  201. int hwrevision;
  202. /* used by cx25821-alsa */
  203. struct snd_card *card;
  204. u32 clk_freq;
  205. /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
  206. struct cx25821_i2c i2c_bus[3];
  207. int nr;
  208. struct mutex lock;
  209. struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
  210. /* board details */
  211. unsigned int board;
  212. char name[32];
  213. /* Analog video */
  214. u32 resources;
  215. unsigned int input;
  216. v4l2_std_id tvnorm;
  217. unsigned short _max_num_decoders;
  218. /* Analog Audio Upstream */
  219. int _audio_is_running;
  220. int _audiopixel_format;
  221. int _is_first_audio_frame;
  222. int _audiofile_status;
  223. int _audio_lines_count;
  224. int _audioframe_count;
  225. int _audio_upstream_channel;
  226. int _last_index_irq; /* The last interrupt index processed. */
  227. __le32 *_risc_audio_jmp_addr;
  228. __le32 *_risc_virt_start_addr;
  229. __le32 *_risc_virt_addr;
  230. dma_addr_t _risc_phys_addr;
  231. dma_addr_t _risc_phys_start_addr;
  232. unsigned int _audiorisc_size;
  233. unsigned int _audiodata_buf_size;
  234. __le32 *_audiodata_buf_virt_addr;
  235. dma_addr_t _audiodata_buf_phys_addr;
  236. char *_audiofilename;
  237. /* V4l */
  238. spinlock_t slock;
  239. /* Video Upstream */
  240. int _line_size;
  241. int _prog_cnt;
  242. int _pixel_format;
  243. int _is_first_frame;
  244. int _is_running;
  245. int _file_status;
  246. int _lines_count;
  247. int _frame_count;
  248. int _channel_upstream_select;
  249. unsigned int _risc_size;
  250. __le32 *_dma_virt_start_addr;
  251. __le32 *_dma_virt_addr;
  252. dma_addr_t _dma_phys_addr;
  253. dma_addr_t _dma_phys_start_addr;
  254. unsigned int _data_buf_size;
  255. __le32 *_data_buf_virt_addr;
  256. dma_addr_t _data_buf_phys_addr;
  257. char *_filename;
  258. char *_defaultname;
  259. int _line_size_ch2;
  260. int _prog_cnt_ch2;
  261. int _pixel_format_ch2;
  262. int _is_first_frame_ch2;
  263. int _is_running_ch2;
  264. int _file_status_ch2;
  265. int _lines_count_ch2;
  266. int _frame_count_ch2;
  267. int _channel2_upstream_select;
  268. unsigned int _risc_size_ch2;
  269. __le32 *_dma_virt_start_addr_ch2;
  270. __le32 *_dma_virt_addr_ch2;
  271. dma_addr_t _dma_phys_addr_ch2;
  272. dma_addr_t _dma_phys_start_addr_ch2;
  273. unsigned int _data_buf_size_ch2;
  274. __le32 *_data_buf_virt_addr_ch2;
  275. dma_addr_t _data_buf_phys_addr_ch2;
  276. char *_filename_ch2;
  277. char *_defaultname_ch2;
  278. u32 upstream_riscbuf_size;
  279. u32 upstream_databuf_size;
  280. u32 upstream_riscbuf_size_ch2;
  281. u32 upstream_databuf_size_ch2;
  282. u32 audio_upstream_riscbuf_size;
  283. u32 audio_upstream_databuf_size;
  284. int _isNTSC;
  285. int _frame_index;
  286. int _audioframe_index;
  287. struct workqueue_struct *_irq_queues;
  288. struct work_struct _irq_work_entry;
  289. struct workqueue_struct *_irq_queues_ch2;
  290. struct work_struct _irq_work_entry_ch2;
  291. struct workqueue_struct *_irq_audio_queues;
  292. struct work_struct _audio_work_entry;
  293. char *input_filename;
  294. char *input_filename_ch2;
  295. int _frame_index_ch2;
  296. int _isNTSC_ch2;
  297. char *vid_stdname_ch2;
  298. int pixel_format_ch2;
  299. int channel_select_ch2;
  300. int command_ch2;
  301. char *input_audiofilename;
  302. char *vid_stdname;
  303. int pixel_format;
  304. int channel_select;
  305. int command;
  306. int channel_opened;
  307. };
  308. struct upstream_user_struct {
  309. char *input_filename;
  310. char *vid_stdname;
  311. int pixel_format;
  312. int channel_select;
  313. int command;
  314. };
  315. struct downstream_user_struct {
  316. char *vid_stdname;
  317. int pixel_format;
  318. int cif_resolution_enable;
  319. int cif_width;
  320. int decoder_select;
  321. int command;
  322. int reg_address;
  323. int reg_data;
  324. };
  325. extern struct upstream_user_struct *up_data;
  326. static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
  327. {
  328. return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
  329. }
  330. #define cx25821_call_all(dev, o, f, args...) \
  331. v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
  332. extern struct cx25821_board cx25821_boards[];
  333. #define SRAM_CH00 0 /* Video A */
  334. #define SRAM_CH01 1 /* Video B */
  335. #define SRAM_CH02 2 /* Video C */
  336. #define SRAM_CH03 3 /* Video D */
  337. #define SRAM_CH04 4 /* Video E */
  338. #define SRAM_CH05 5 /* Video F */
  339. #define SRAM_CH06 6 /* Video G */
  340. #define SRAM_CH07 7 /* Video H */
  341. #define SRAM_CH08 8 /* Audio A */
  342. #define SRAM_CH09 9 /* Video Upstream I */
  343. #define SRAM_CH10 10 /* Video Upstream J */
  344. #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
  345. #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
  346. #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
  347. #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
  348. struct sram_channel {
  349. char *name;
  350. u32 i;
  351. u32 cmds_start;
  352. u32 ctrl_start;
  353. u32 cdt;
  354. u32 fifo_start;
  355. u32 fifo_size;
  356. u32 ptr1_reg;
  357. u32 ptr2_reg;
  358. u32 cnt1_reg;
  359. u32 cnt2_reg;
  360. u32 int_msk;
  361. u32 int_stat;
  362. u32 int_mstat;
  363. u32 dma_ctl;
  364. u32 gpcnt_ctl;
  365. u32 gpcnt;
  366. u32 aud_length;
  367. u32 aud_cfg;
  368. u32 fld_aud_fifo_en;
  369. u32 fld_aud_risc_en;
  370. /* For Upstream Video */
  371. u32 vid_fmt_ctl;
  372. u32 vid_active_ctl1;
  373. u32 vid_active_ctl2;
  374. u32 vid_cdt_size;
  375. u32 vip_ctl;
  376. u32 pix_frmt;
  377. u32 jumponly;
  378. u32 irq_bit;
  379. };
  380. extern const struct sram_channel cx25821_sram_channels[];
  381. #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
  382. #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
  383. #define cx_andor(reg, mask, value) \
  384. writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
  385. ((value) & (mask)), dev->lmmio+((reg)>>2))
  386. #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
  387. #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
  388. #define Set_GPIO_Bit(Bit) (1 << Bit)
  389. #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
  390. #define CX25821_ERR(fmt, args...) \
  391. pr_err("(%d): " fmt, dev->board, ##args)
  392. #define CX25821_WARN(fmt, args...) \
  393. pr_warn("(%d): " fmt, dev->board, ##args)
  394. #define CX25821_INFO(fmt, args...) \
  395. pr_info("(%d): " fmt, dev->board, ##args)
  396. extern int cx25821_i2c_register(struct cx25821_i2c *bus);
  397. extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
  398. extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
  399. extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
  400. extern void cx25821_gpio_init(struct cx25821_dev *dev);
  401. extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
  402. int pin_number, int pin_logic_value);
  403. extern int medusa_video_init(struct cx25821_dev *dev);
  404. extern int medusa_set_videostandard(struct cx25821_dev *dev);
  405. extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
  406. int decoder_select);
  407. extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
  408. int decoder);
  409. extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
  410. int decoder);
  411. extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
  412. extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
  413. int decoder);
  414. extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
  415. const struct sram_channel *ch, unsigned int bpl,
  416. u32 risc);
  417. extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
  418. struct scatterlist *sglist,
  419. unsigned int top_offset,
  420. unsigned int bottom_offset,
  421. unsigned int bpl,
  422. unsigned int padding, unsigned int lines);
  423. extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
  424. struct btcx_riscmem *risc,
  425. struct scatterlist *sglist,
  426. unsigned int bpl,
  427. unsigned int lines, unsigned int lpi);
  428. extern void cx25821_free_buffer(struct videobuf_queue *q,
  429. struct cx25821_buffer *buf);
  430. extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
  431. u32 reg, u32 mask, u32 value);
  432. extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
  433. const struct sram_channel *ch);
  434. extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
  435. const struct sram_channel *ch);
  436. extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
  437. extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
  438. int len, u32 bits, u32 mask);
  439. extern void cx25821_dev_unregister(struct cx25821_dev *dev);
  440. extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
  441. const struct sram_channel *ch,
  442. unsigned int bpl, u32 risc);
  443. extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
  444. int channel_select, int pixel_format);
  445. extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
  446. int channel_select, int pixel_format);
  447. extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
  448. int channel_select);
  449. extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
  450. extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
  451. extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
  452. extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
  453. struct upstream_user_struct
  454. *up_data);
  455. extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
  456. struct upstream_user_struct
  457. *up_data);
  458. extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
  459. struct upstream_user_struct *up_data);
  460. extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
  461. extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
  462. extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
  463. extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
  464. const struct sram_channel *ch,
  465. unsigned int bpl, u32 risc);
  466. extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
  467. u32 format);
  468. #endif