cx25821.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. u32 resources;
  103. enum v4l2_priority prio;
  104. /* video capture */
  105. int channel_id;
  106. };
  107. enum cx25821_itype {
  108. CX25821_VMUX_COMPOSITE = 1,
  109. CX25821_VMUX_SVIDEO,
  110. CX25821_VMUX_DEBUG,
  111. };
  112. enum cx25821_src_sel_type {
  113. CX25821_SRC_SEL_EXT_656_VIDEO = 0,
  114. CX25821_SRC_SEL_PARALLEL_MPEG_VIDEO
  115. };
  116. /* buffer for one video frame */
  117. struct cx25821_buffer {
  118. /* common v4l buffer stuff -- must be first */
  119. struct videobuf_buffer vb;
  120. /* cx25821 specific */
  121. unsigned int bpl;
  122. struct btcx_riscmem risc;
  123. const struct cx25821_fmt *fmt;
  124. u32 count;
  125. };
  126. struct cx25821_input {
  127. enum cx25821_itype type;
  128. unsigned int vmux;
  129. u32 gpio0, gpio1, gpio2, gpio3;
  130. };
  131. enum port {
  132. CX25821_UNDEFINED = 0,
  133. CX25821_RAW,
  134. CX25821_264
  135. };
  136. struct cx25821_board {
  137. const char *name;
  138. enum port porta;
  139. enum port portb;
  140. enum port portc;
  141. u32 clk_freq;
  142. struct cx25821_input input[CX25821_NR_INPUT];
  143. };
  144. struct cx25821_i2c {
  145. struct cx25821_dev *dev;
  146. int nr;
  147. /* i2c i/o */
  148. struct i2c_adapter i2c_adap;
  149. struct i2c_client i2c_client;
  150. u32 i2c_rc;
  151. /* cx25821 registers used for raw addess */
  152. u32 i2c_period;
  153. u32 reg_ctrl;
  154. u32 reg_stat;
  155. u32 reg_addr;
  156. u32 reg_rdata;
  157. u32 reg_wdata;
  158. };
  159. struct cx25821_dmaqueue {
  160. struct list_head active;
  161. struct list_head queued;
  162. struct timer_list timeout;
  163. struct btcx_riscmem stopper;
  164. u32 count;
  165. };
  166. struct cx25821_data {
  167. struct cx25821_dev *dev;
  168. const struct sram_channel *channel;
  169. };
  170. struct cx25821_dev;
  171. struct cx25821_channel {
  172. unsigned id;
  173. struct cx25821_dev *dev;
  174. struct v4l2_prio_state prio;
  175. struct v4l2_ctrl_handler hdl;
  176. struct cx25821_data timeout_data;
  177. struct video_device vdev;
  178. struct cx25821_dmaqueue dma_vidq;
  179. struct videobuf_queue vidq;
  180. const struct sram_channel *sram_channels;
  181. int resources;
  182. const struct cx25821_fmt *fmt;
  183. unsigned int width, height;
  184. int pixel_formats;
  185. int use_cif_resolution;
  186. int cif_width;
  187. };
  188. struct snd_card;
  189. struct cx25821_dev {
  190. struct v4l2_device v4l2_dev;
  191. /* pci stuff */
  192. struct pci_dev *pci;
  193. unsigned char pci_rev, pci_lat;
  194. int pci_bus, pci_slot;
  195. u32 base_io_addr;
  196. u32 __iomem *lmmio;
  197. u8 __iomem *bmmio;
  198. int pci_irqmask;
  199. int hwrevision;
  200. /* used by cx25821-alsa */
  201. struct snd_card *card;
  202. u32 clk_freq;
  203. /* I2C adapters: Master 1 & 2 (External) & Master 3 (Internal only) */
  204. struct cx25821_i2c i2c_bus[3];
  205. int nr;
  206. struct mutex lock;
  207. struct cx25821_channel channels[MAX_VID_CHANNEL_NUM];
  208. /* board details */
  209. unsigned int board;
  210. char name[32];
  211. /* Analog video */
  212. u32 resources;
  213. unsigned int input;
  214. v4l2_std_id tvnorm;
  215. unsigned short _max_num_decoders;
  216. /* Analog Audio Upstream */
  217. int _audio_is_running;
  218. int _audiopixel_format;
  219. int _is_first_audio_frame;
  220. int _audiofile_status;
  221. int _audio_lines_count;
  222. int _audioframe_count;
  223. int _audio_upstream_channel;
  224. int _last_index_irq; /* The last interrupt index processed. */
  225. __le32 *_risc_audio_jmp_addr;
  226. __le32 *_risc_virt_start_addr;
  227. __le32 *_risc_virt_addr;
  228. dma_addr_t _risc_phys_addr;
  229. dma_addr_t _risc_phys_start_addr;
  230. unsigned int _audiorisc_size;
  231. unsigned int _audiodata_buf_size;
  232. __le32 *_audiodata_buf_virt_addr;
  233. dma_addr_t _audiodata_buf_phys_addr;
  234. char *_audiofilename;
  235. /* V4l */
  236. spinlock_t slock;
  237. /* Video Upstream */
  238. int _line_size;
  239. int _prog_cnt;
  240. int _pixel_format;
  241. int _is_first_frame;
  242. int _is_running;
  243. int _file_status;
  244. int _lines_count;
  245. int _frame_count;
  246. int _channel_upstream_select;
  247. unsigned int _risc_size;
  248. __le32 *_dma_virt_start_addr;
  249. __le32 *_dma_virt_addr;
  250. dma_addr_t _dma_phys_addr;
  251. dma_addr_t _dma_phys_start_addr;
  252. unsigned int _data_buf_size;
  253. __le32 *_data_buf_virt_addr;
  254. dma_addr_t _data_buf_phys_addr;
  255. char *_filename;
  256. char *_defaultname;
  257. int _line_size_ch2;
  258. int _prog_cnt_ch2;
  259. int _pixel_format_ch2;
  260. int _is_first_frame_ch2;
  261. int _is_running_ch2;
  262. int _file_status_ch2;
  263. int _lines_count_ch2;
  264. int _frame_count_ch2;
  265. int _channel2_upstream_select;
  266. unsigned int _risc_size_ch2;
  267. __le32 *_dma_virt_start_addr_ch2;
  268. __le32 *_dma_virt_addr_ch2;
  269. dma_addr_t _dma_phys_addr_ch2;
  270. dma_addr_t _dma_phys_start_addr_ch2;
  271. unsigned int _data_buf_size_ch2;
  272. __le32 *_data_buf_virt_addr_ch2;
  273. dma_addr_t _data_buf_phys_addr_ch2;
  274. char *_filename_ch2;
  275. char *_defaultname_ch2;
  276. u32 upstream_riscbuf_size;
  277. u32 upstream_databuf_size;
  278. u32 upstream_riscbuf_size_ch2;
  279. u32 upstream_databuf_size_ch2;
  280. u32 audio_upstream_riscbuf_size;
  281. u32 audio_upstream_databuf_size;
  282. int _isNTSC;
  283. int _frame_index;
  284. int _audioframe_index;
  285. struct workqueue_struct *_irq_queues;
  286. struct work_struct _irq_work_entry;
  287. struct workqueue_struct *_irq_queues_ch2;
  288. struct work_struct _irq_work_entry_ch2;
  289. struct workqueue_struct *_irq_audio_queues;
  290. struct work_struct _audio_work_entry;
  291. char *input_filename;
  292. char *input_filename_ch2;
  293. int _frame_index_ch2;
  294. int _isNTSC_ch2;
  295. char *vid_stdname_ch2;
  296. int pixel_format_ch2;
  297. int channel_select_ch2;
  298. int command_ch2;
  299. char *input_audiofilename;
  300. char *vid_stdname;
  301. int pixel_format;
  302. int channel_select;
  303. int command;
  304. int channel_opened;
  305. };
  306. struct upstream_user_struct {
  307. char *input_filename;
  308. char *vid_stdname;
  309. int pixel_format;
  310. int channel_select;
  311. int command;
  312. };
  313. struct downstream_user_struct {
  314. char *vid_stdname;
  315. int pixel_format;
  316. int cif_resolution_enable;
  317. int cif_width;
  318. int decoder_select;
  319. int command;
  320. int reg_address;
  321. int reg_data;
  322. };
  323. extern struct upstream_user_struct *up_data;
  324. static inline struct cx25821_dev *get_cx25821(struct v4l2_device *v4l2_dev)
  325. {
  326. return container_of(v4l2_dev, struct cx25821_dev, v4l2_dev);
  327. }
  328. #define cx25821_call_all(dev, o, f, args...) \
  329. v4l2_device_call_all(&dev->v4l2_dev, 0, o, f, ##args)
  330. extern struct cx25821_board cx25821_boards[];
  331. #define SRAM_CH00 0 /* Video A */
  332. #define SRAM_CH01 1 /* Video B */
  333. #define SRAM_CH02 2 /* Video C */
  334. #define SRAM_CH03 3 /* Video D */
  335. #define SRAM_CH04 4 /* Video E */
  336. #define SRAM_CH05 5 /* Video F */
  337. #define SRAM_CH06 6 /* Video G */
  338. #define SRAM_CH07 7 /* Video H */
  339. #define SRAM_CH08 8 /* Audio A */
  340. #define SRAM_CH09 9 /* Video Upstream I */
  341. #define SRAM_CH10 10 /* Video Upstream J */
  342. #define SRAM_CH11 11 /* Audio Upstream AUD_CHANNEL_B */
  343. #define VID_UPSTREAM_SRAM_CHANNEL_I SRAM_CH09
  344. #define VID_UPSTREAM_SRAM_CHANNEL_J SRAM_CH10
  345. #define AUDIO_UPSTREAM_SRAM_CHANNEL_B SRAM_CH11
  346. struct sram_channel {
  347. char *name;
  348. u32 i;
  349. u32 cmds_start;
  350. u32 ctrl_start;
  351. u32 cdt;
  352. u32 fifo_start;
  353. u32 fifo_size;
  354. u32 ptr1_reg;
  355. u32 ptr2_reg;
  356. u32 cnt1_reg;
  357. u32 cnt2_reg;
  358. u32 int_msk;
  359. u32 int_stat;
  360. u32 int_mstat;
  361. u32 dma_ctl;
  362. u32 gpcnt_ctl;
  363. u32 gpcnt;
  364. u32 aud_length;
  365. u32 aud_cfg;
  366. u32 fld_aud_fifo_en;
  367. u32 fld_aud_risc_en;
  368. /* For Upstream Video */
  369. u32 vid_fmt_ctl;
  370. u32 vid_active_ctl1;
  371. u32 vid_active_ctl2;
  372. u32 vid_cdt_size;
  373. u32 vip_ctl;
  374. u32 pix_frmt;
  375. u32 jumponly;
  376. u32 irq_bit;
  377. };
  378. extern const struct sram_channel cx25821_sram_channels[];
  379. #define cx_read(reg) readl(dev->lmmio + ((reg)>>2))
  380. #define cx_write(reg, value) writel((value), dev->lmmio + ((reg)>>2))
  381. #define cx_andor(reg, mask, value) \
  382. writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
  383. ((value) & (mask)), dev->lmmio+((reg)>>2))
  384. #define cx_set(reg, bit) cx_andor((reg), (bit), (bit))
  385. #define cx_clear(reg, bit) cx_andor((reg), (bit), 0)
  386. #define Set_GPIO_Bit(Bit) (1 << Bit)
  387. #define Clear_GPIO_Bit(Bit) (~(1 << Bit))
  388. #define CX25821_ERR(fmt, args...) \
  389. pr_err("(%d): " fmt, dev->board, ##args)
  390. #define CX25821_WARN(fmt, args...) \
  391. pr_warn("(%d): " fmt, dev->board, ##args)
  392. #define CX25821_INFO(fmt, args...) \
  393. pr_info("(%d): " fmt, dev->board, ##args)
  394. extern int cx25821_i2c_register(struct cx25821_i2c *bus);
  395. extern int cx25821_i2c_read(struct cx25821_i2c *bus, u16 reg_addr, int *value);
  396. extern int cx25821_i2c_write(struct cx25821_i2c *bus, u16 reg_addr, int value);
  397. extern int cx25821_i2c_unregister(struct cx25821_i2c *bus);
  398. extern void cx25821_gpio_init(struct cx25821_dev *dev);
  399. extern void cx25821_set_gpiopin_direction(struct cx25821_dev *dev,
  400. int pin_number, int pin_logic_value);
  401. extern int medusa_video_init(struct cx25821_dev *dev);
  402. extern int medusa_set_videostandard(struct cx25821_dev *dev);
  403. extern void medusa_set_resolution(struct cx25821_dev *dev, int width,
  404. int decoder_select);
  405. extern int medusa_set_brightness(struct cx25821_dev *dev, int brightness,
  406. int decoder);
  407. extern int medusa_set_contrast(struct cx25821_dev *dev, int contrast,
  408. int decoder);
  409. extern int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder);
  410. extern int medusa_set_saturation(struct cx25821_dev *dev, int saturation,
  411. int decoder);
  412. extern int cx25821_sram_channel_setup(struct cx25821_dev *dev,
  413. const struct sram_channel *ch, unsigned int bpl,
  414. u32 risc);
  415. extern int cx25821_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
  416. struct scatterlist *sglist,
  417. unsigned int top_offset,
  418. unsigned int bottom_offset,
  419. unsigned int bpl,
  420. unsigned int padding, unsigned int lines);
  421. extern int cx25821_risc_databuffer_audio(struct pci_dev *pci,
  422. struct btcx_riscmem *risc,
  423. struct scatterlist *sglist,
  424. unsigned int bpl,
  425. unsigned int lines, unsigned int lpi);
  426. extern void cx25821_free_buffer(struct videobuf_queue *q,
  427. struct cx25821_buffer *buf);
  428. extern int cx25821_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
  429. u32 reg, u32 mask, u32 value);
  430. extern void cx25821_sram_channel_dump(struct cx25821_dev *dev,
  431. const struct sram_channel *ch);
  432. extern void cx25821_sram_channel_dump_audio(struct cx25821_dev *dev,
  433. const struct sram_channel *ch);
  434. extern struct cx25821_dev *cx25821_dev_get(struct pci_dev *pci);
  435. extern void cx25821_print_irqbits(char *name, char *tag, char **strings,
  436. int len, u32 bits, u32 mask);
  437. extern void cx25821_dev_unregister(struct cx25821_dev *dev);
  438. extern int cx25821_sram_channel_setup_audio(struct cx25821_dev *dev,
  439. const struct sram_channel *ch,
  440. unsigned int bpl, u32 risc);
  441. extern int cx25821_vidupstream_init_ch1(struct cx25821_dev *dev,
  442. int channel_select, int pixel_format);
  443. extern int cx25821_vidupstream_init_ch2(struct cx25821_dev *dev,
  444. int channel_select, int pixel_format);
  445. extern int cx25821_audio_upstream_init(struct cx25821_dev *dev,
  446. int channel_select);
  447. extern void cx25821_free_mem_upstream_ch1(struct cx25821_dev *dev);
  448. extern void cx25821_free_mem_upstream_ch2(struct cx25821_dev *dev);
  449. extern void cx25821_free_mem_upstream_audio(struct cx25821_dev *dev);
  450. extern void cx25821_start_upstream_video_ch1(struct cx25821_dev *dev,
  451. struct upstream_user_struct
  452. *up_data);
  453. extern void cx25821_start_upstream_video_ch2(struct cx25821_dev *dev,
  454. struct upstream_user_struct
  455. *up_data);
  456. extern void cx25821_start_upstream_audio(struct cx25821_dev *dev,
  457. struct upstream_user_struct *up_data);
  458. extern void cx25821_stop_upstream_video_ch1(struct cx25821_dev *dev);
  459. extern void cx25821_stop_upstream_video_ch2(struct cx25821_dev *dev);
  460. extern void cx25821_stop_upstream_audio(struct cx25821_dev *dev);
  461. extern int cx25821_sram_channel_setup_upstream(struct cx25821_dev *dev,
  462. const struct sram_channel *ch,
  463. unsigned int bpl, u32 risc);
  464. extern void cx25821_set_pixel_format(struct cx25821_dev *dev, int channel,
  465. u32 format);
  466. #endif