dt3155.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /***************************************************************************
  2. * Copyright (C) 2006-2010 by Marin Mitov *
  3. * mitov@issp.bas.bg *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. ***************************************************************************/
  16. #include <linux/module.h>
  17. #include <linux/stringify.h>
  18. #include <linux/delay.h>
  19. #include <linux/kthread.h>
  20. #include <linux/slab.h>
  21. #include <media/v4l2-dev.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include <media/v4l2-common.h>
  24. #include <media/videobuf2-dma-contig.h>
  25. #include "dt3155.h"
  26. #define DT3155_DEVICE_ID 0x1223
  27. /**
  28. * read_i2c_reg - reads an internal i2c register
  29. *
  30. * @addr: dt3155 mmio base address
  31. * @index: index (internal address) of register to read
  32. * @data: pointer to byte the read data will be placed in
  33. *
  34. * returns: zero on success or error code
  35. *
  36. * This function starts reading the specified (by index) register
  37. * and busy waits for the process to finish. The result is placed
  38. * in a byte pointed by data.
  39. */
  40. static int read_i2c_reg(void __iomem *addr, u8 index, u8 *data)
  41. {
  42. u32 tmp = index;
  43. iowrite32((tmp << 17) | IIC_READ, addr + IIC_CSR2);
  44. mmiowb();
  45. udelay(45); /* wait at least 43 usec for NEW_CYCLE to clear */
  46. if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
  47. return -EIO; /* error: NEW_CYCLE not cleared */
  48. tmp = ioread32(addr + IIC_CSR1);
  49. if (tmp & DIRECT_ABORT) {
  50. /* reset DIRECT_ABORT bit */
  51. iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
  52. return -EIO; /* error: DIRECT_ABORT set */
  53. }
  54. *data = tmp >> 24;
  55. return 0;
  56. }
  57. /**
  58. * write_i2c_reg - writes to an internal i2c register
  59. *
  60. * @addr: dt3155 mmio base address
  61. * @index: index (internal address) of register to read
  62. * @data: data to be written
  63. *
  64. * returns: zero on success or error code
  65. *
  66. * This function starts writing the specified (by index) register
  67. * and busy waits for the process to finish.
  68. */
  69. static int write_i2c_reg(void __iomem *addr, u8 index, u8 data)
  70. {
  71. u32 tmp = index;
  72. iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2);
  73. mmiowb();
  74. udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
  75. if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
  76. return -EIO; /* error: NEW_CYCLE not cleared */
  77. if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
  78. /* reset DIRECT_ABORT bit */
  79. iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
  80. return -EIO; /* error: DIRECT_ABORT set */
  81. }
  82. return 0;
  83. }
  84. /**
  85. * write_i2c_reg_nowait - writes to an internal i2c register
  86. *
  87. * @addr: dt3155 mmio base address
  88. * @index: index (internal address) of register to read
  89. * @data: data to be written
  90. *
  91. * This function starts writing the specified (by index) register
  92. * and then returns.
  93. */
  94. static void write_i2c_reg_nowait(void __iomem *addr, u8 index, u8 data)
  95. {
  96. u32 tmp = index;
  97. iowrite32((tmp << 17) | IIC_WRITE | data, addr + IIC_CSR2);
  98. mmiowb();
  99. }
  100. /**
  101. * wait_i2c_reg - waits the read/write to finish
  102. *
  103. * @addr: dt3155 mmio base address
  104. *
  105. * returns: zero on success or error code
  106. *
  107. * This function waits reading/writing to finish.
  108. */
  109. static int wait_i2c_reg(void __iomem *addr)
  110. {
  111. if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
  112. udelay(65); /* wait at least 63 usec for NEW_CYCLE to clear */
  113. if (ioread32(addr + IIC_CSR2) & NEW_CYCLE)
  114. return -EIO; /* error: NEW_CYCLE not cleared */
  115. if (ioread32(addr + IIC_CSR1) & DIRECT_ABORT) {
  116. /* reset DIRECT_ABORT bit */
  117. iowrite32(DIRECT_ABORT, addr + IIC_CSR1);
  118. return -EIO; /* error: DIRECT_ABORT set */
  119. }
  120. return 0;
  121. }
  122. static int
  123. dt3155_queue_setup(struct vb2_queue *vq,
  124. unsigned int *nbuffers, unsigned int *num_planes,
  125. unsigned int sizes[], struct device *alloc_devs[])
  126. {
  127. struct dt3155_priv *pd = vb2_get_drv_priv(vq);
  128. unsigned size = pd->width * pd->height;
  129. if (vq->num_buffers + *nbuffers < 2)
  130. *nbuffers = 2 - vq->num_buffers;
  131. if (*num_planes)
  132. return sizes[0] < size ? -EINVAL : 0;
  133. *num_planes = 1;
  134. sizes[0] = size;
  135. return 0;
  136. }
  137. static int dt3155_buf_prepare(struct vb2_buffer *vb)
  138. {
  139. struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
  140. vb2_set_plane_payload(vb, 0, pd->width * pd->height);
  141. return 0;
  142. }
  143. static int dt3155_start_streaming(struct vb2_queue *q, unsigned count)
  144. {
  145. struct dt3155_priv *pd = vb2_get_drv_priv(q);
  146. struct vb2_buffer *vb = &pd->curr_buf->vb2_buf;
  147. dma_addr_t dma_addr;
  148. pd->sequence = 0;
  149. dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  150. iowrite32(dma_addr, pd->regs + EVEN_DMA_START);
  151. iowrite32(dma_addr + pd->width, pd->regs + ODD_DMA_START);
  152. iowrite32(pd->width, pd->regs + EVEN_DMA_STRIDE);
  153. iowrite32(pd->width, pd->regs + ODD_DMA_STRIDE);
  154. /* enable interrupts, clear all irq flags */
  155. iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
  156. FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
  157. iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
  158. FLD_DN_ODD | FLD_DN_EVEN | CAP_CONT_EVEN | CAP_CONT_ODD,
  159. pd->regs + CSR1);
  160. wait_i2c_reg(pd->regs);
  161. write_i2c_reg(pd->regs, CONFIG, pd->config);
  162. write_i2c_reg(pd->regs, EVEN_CSR, CSR_ERROR | CSR_DONE);
  163. write_i2c_reg(pd->regs, ODD_CSR, CSR_ERROR | CSR_DONE);
  164. /* start the board */
  165. write_i2c_reg(pd->regs, CSR2, pd->csr2 | BUSY_EVEN | BUSY_ODD);
  166. return 0;
  167. }
  168. static void dt3155_stop_streaming(struct vb2_queue *q)
  169. {
  170. struct dt3155_priv *pd = vb2_get_drv_priv(q);
  171. struct vb2_buffer *vb;
  172. spin_lock_irq(&pd->lock);
  173. /* stop the board */
  174. write_i2c_reg_nowait(pd->regs, CSR2, pd->csr2);
  175. iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
  176. FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1);
  177. /* disable interrupts, clear all irq flags */
  178. iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD, pd->regs + INT_CSR);
  179. spin_unlock_irq(&pd->lock);
  180. /*
  181. * It is not clear whether the DMA stops at once or whether it
  182. * will finish the current frame or field first. To be on the
  183. * safe side we wait a bit.
  184. */
  185. msleep(45);
  186. spin_lock_irq(&pd->lock);
  187. if (pd->curr_buf) {
  188. vb2_buffer_done(&pd->curr_buf->vb2_buf, VB2_BUF_STATE_ERROR);
  189. pd->curr_buf = NULL;
  190. }
  191. while (!list_empty(&pd->dmaq)) {
  192. vb = list_first_entry(&pd->dmaq, typeof(*vb), done_entry);
  193. list_del(&vb->done_entry);
  194. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  195. }
  196. spin_unlock_irq(&pd->lock);
  197. }
  198. static void dt3155_buf_queue(struct vb2_buffer *vb)
  199. {
  200. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  201. struct dt3155_priv *pd = vb2_get_drv_priv(vb->vb2_queue);
  202. /* pd->vidq.streaming = 1 when dt3155_buf_queue() is invoked */
  203. spin_lock_irq(&pd->lock);
  204. if (pd->curr_buf)
  205. list_add_tail(&vb->done_entry, &pd->dmaq);
  206. else
  207. pd->curr_buf = vbuf;
  208. spin_unlock_irq(&pd->lock);
  209. }
  210. static const struct vb2_ops q_ops = {
  211. .queue_setup = dt3155_queue_setup,
  212. .wait_prepare = vb2_ops_wait_prepare,
  213. .wait_finish = vb2_ops_wait_finish,
  214. .buf_prepare = dt3155_buf_prepare,
  215. .start_streaming = dt3155_start_streaming,
  216. .stop_streaming = dt3155_stop_streaming,
  217. .buf_queue = dt3155_buf_queue,
  218. };
  219. static irqreturn_t dt3155_irq_handler_even(int irq, void *dev_id)
  220. {
  221. struct dt3155_priv *ipd = dev_id;
  222. struct vb2_buffer *ivb;
  223. dma_addr_t dma_addr;
  224. u32 tmp;
  225. tmp = ioread32(ipd->regs + INT_CSR) & (FLD_START | FLD_END_ODD);
  226. if (!tmp)
  227. return IRQ_NONE; /* not our irq */
  228. if ((tmp & FLD_START) && !(tmp & FLD_END_ODD)) {
  229. iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START,
  230. ipd->regs + INT_CSR);
  231. return IRQ_HANDLED; /* start of field irq */
  232. }
  233. tmp = ioread32(ipd->regs + CSR1) & (FLD_CRPT_EVEN | FLD_CRPT_ODD);
  234. if (tmp) {
  235. iowrite32(FIFO_EN | SRST | FLD_CRPT_ODD | FLD_CRPT_EVEN |
  236. FLD_DN_ODD | FLD_DN_EVEN |
  237. CAP_CONT_EVEN | CAP_CONT_ODD,
  238. ipd->regs + CSR1);
  239. mmiowb();
  240. }
  241. spin_lock(&ipd->lock);
  242. if (ipd->curr_buf && !list_empty(&ipd->dmaq)) {
  243. ipd->curr_buf->vb2_buf.timestamp = ktime_get_ns();
  244. ipd->curr_buf->sequence = ipd->sequence++;
  245. ipd->curr_buf->field = V4L2_FIELD_NONE;
  246. vb2_buffer_done(&ipd->curr_buf->vb2_buf, VB2_BUF_STATE_DONE);
  247. ivb = list_first_entry(&ipd->dmaq, typeof(*ivb), done_entry);
  248. list_del(&ivb->done_entry);
  249. ipd->curr_buf = to_vb2_v4l2_buffer(ivb);
  250. dma_addr = vb2_dma_contig_plane_dma_addr(ivb, 0);
  251. iowrite32(dma_addr, ipd->regs + EVEN_DMA_START);
  252. iowrite32(dma_addr + ipd->width, ipd->regs + ODD_DMA_START);
  253. iowrite32(ipd->width, ipd->regs + EVEN_DMA_STRIDE);
  254. iowrite32(ipd->width, ipd->regs + ODD_DMA_STRIDE);
  255. mmiowb();
  256. }
  257. /* enable interrupts, clear all irq flags */
  258. iowrite32(FLD_START_EN | FLD_END_ODD_EN | FLD_START |
  259. FLD_END_EVEN | FLD_END_ODD, ipd->regs + INT_CSR);
  260. spin_unlock(&ipd->lock);
  261. return IRQ_HANDLED;
  262. }
  263. static const struct v4l2_file_operations dt3155_fops = {
  264. .owner = THIS_MODULE,
  265. .open = v4l2_fh_open,
  266. .release = vb2_fop_release,
  267. .unlocked_ioctl = video_ioctl2,
  268. .read = vb2_fop_read,
  269. .mmap = vb2_fop_mmap,
  270. .poll = vb2_fop_poll
  271. };
  272. static int dt3155_querycap(struct file *filp, void *p,
  273. struct v4l2_capability *cap)
  274. {
  275. struct dt3155_priv *pd = video_drvdata(filp);
  276. strcpy(cap->driver, DT3155_NAME);
  277. strcpy(cap->card, DT3155_NAME " frame grabber");
  278. sprintf(cap->bus_info, "PCI:%s", pci_name(pd->pdev));
  279. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  280. V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  281. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  282. return 0;
  283. }
  284. static int dt3155_enum_fmt_vid_cap(struct file *filp,
  285. void *p, struct v4l2_fmtdesc *f)
  286. {
  287. if (f->index)
  288. return -EINVAL;
  289. f->pixelformat = V4L2_PIX_FMT_GREY;
  290. strcpy(f->description, "8-bit Greyscale");
  291. return 0;
  292. }
  293. static int dt3155_fmt_vid_cap(struct file *filp, void *p, struct v4l2_format *f)
  294. {
  295. struct dt3155_priv *pd = video_drvdata(filp);
  296. f->fmt.pix.width = pd->width;
  297. f->fmt.pix.height = pd->height;
  298. f->fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
  299. f->fmt.pix.field = V4L2_FIELD_NONE;
  300. f->fmt.pix.bytesperline = f->fmt.pix.width;
  301. f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height;
  302. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  303. return 0;
  304. }
  305. static int dt3155_g_std(struct file *filp, void *p, v4l2_std_id *norm)
  306. {
  307. struct dt3155_priv *pd = video_drvdata(filp);
  308. *norm = pd->std;
  309. return 0;
  310. }
  311. static int dt3155_s_std(struct file *filp, void *p, v4l2_std_id norm)
  312. {
  313. struct dt3155_priv *pd = video_drvdata(filp);
  314. if (pd->std == norm)
  315. return 0;
  316. if (vb2_is_busy(&pd->vidq))
  317. return -EBUSY;
  318. pd->std = norm;
  319. if (pd->std & V4L2_STD_525_60) {
  320. pd->csr2 = VT_60HZ;
  321. pd->width = 640;
  322. pd->height = 480;
  323. } else {
  324. pd->csr2 = VT_50HZ;
  325. pd->width = 768;
  326. pd->height = 576;
  327. }
  328. return 0;
  329. }
  330. static int dt3155_enum_input(struct file *filp, void *p,
  331. struct v4l2_input *input)
  332. {
  333. if (input->index > 3)
  334. return -EINVAL;
  335. if (input->index)
  336. snprintf(input->name, sizeof(input->name), "VID%d",
  337. input->index);
  338. else
  339. strlcpy(input->name, "J2/VID0", sizeof(input->name));
  340. input->type = V4L2_INPUT_TYPE_CAMERA;
  341. input->std = V4L2_STD_ALL;
  342. input->status = 0;
  343. return 0;
  344. }
  345. static int dt3155_g_input(struct file *filp, void *p, unsigned int *i)
  346. {
  347. struct dt3155_priv *pd = video_drvdata(filp);
  348. *i = pd->input;
  349. return 0;
  350. }
  351. static int dt3155_s_input(struct file *filp, void *p, unsigned int i)
  352. {
  353. struct dt3155_priv *pd = video_drvdata(filp);
  354. if (i > 3)
  355. return -EINVAL;
  356. pd->input = i;
  357. write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
  358. write_i2c_reg(pd->regs, AD_CMD, (i << 6) | (i << 4) | SYNC_LVL_3);
  359. return 0;
  360. }
  361. static const struct v4l2_ioctl_ops dt3155_ioctl_ops = {
  362. .vidioc_querycap = dt3155_querycap,
  363. .vidioc_enum_fmt_vid_cap = dt3155_enum_fmt_vid_cap,
  364. .vidioc_try_fmt_vid_cap = dt3155_fmt_vid_cap,
  365. .vidioc_g_fmt_vid_cap = dt3155_fmt_vid_cap,
  366. .vidioc_s_fmt_vid_cap = dt3155_fmt_vid_cap,
  367. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  368. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  369. .vidioc_querybuf = vb2_ioctl_querybuf,
  370. .vidioc_expbuf = vb2_ioctl_expbuf,
  371. .vidioc_qbuf = vb2_ioctl_qbuf,
  372. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  373. .vidioc_streamon = vb2_ioctl_streamon,
  374. .vidioc_streamoff = vb2_ioctl_streamoff,
  375. .vidioc_g_std = dt3155_g_std,
  376. .vidioc_s_std = dt3155_s_std,
  377. .vidioc_enum_input = dt3155_enum_input,
  378. .vidioc_g_input = dt3155_g_input,
  379. .vidioc_s_input = dt3155_s_input,
  380. };
  381. static int dt3155_init_board(struct dt3155_priv *pd)
  382. {
  383. struct pci_dev *pdev = pd->pdev;
  384. int i;
  385. u8 tmp = 0;
  386. pci_set_master(pdev); /* dt3155 needs it */
  387. /* resetting the adapter */
  388. iowrite32(ADDR_ERR_ODD | ADDR_ERR_EVEN | FLD_CRPT_ODD | FLD_CRPT_EVEN |
  389. FLD_DN_ODD | FLD_DN_EVEN, pd->regs + CSR1);
  390. mmiowb();
  391. msleep(20);
  392. /* initializing adapter registers */
  393. iowrite32(FIFO_EN | SRST, pd->regs + CSR1);
  394. mmiowb();
  395. iowrite32(0xEEEEEE01, pd->regs + EVEN_PIXEL_FMT);
  396. iowrite32(0xEEEEEE01, pd->regs + ODD_PIXEL_FMT);
  397. iowrite32(0x00000020, pd->regs + FIFO_TRIGER);
  398. iowrite32(0x00000103, pd->regs + XFER_MODE);
  399. iowrite32(0, pd->regs + RETRY_WAIT_CNT);
  400. iowrite32(0, pd->regs + INT_CSR);
  401. iowrite32(1, pd->regs + EVEN_FLD_MASK);
  402. iowrite32(1, pd->regs + ODD_FLD_MASK);
  403. iowrite32(0, pd->regs + MASK_LENGTH);
  404. iowrite32(0x0005007C, pd->regs + FIFO_FLAG_CNT);
  405. iowrite32(0x01010101, pd->regs + IIC_CLK_DUR);
  406. mmiowb();
  407. /* verifying that we have a DT3155 board (not just a SAA7116 chip) */
  408. read_i2c_reg(pd->regs, DT_ID, &tmp);
  409. if (tmp != DT3155_ID)
  410. return -ENODEV;
  411. /* initialize AD LUT */
  412. write_i2c_reg(pd->regs, AD_ADDR, 0);
  413. for (i = 0; i < 256; i++)
  414. write_i2c_reg(pd->regs, AD_LUT, i);
  415. /* initialize ADC references */
  416. /* FIXME: pos_ref & neg_ref depend on VT_50HZ */
  417. write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
  418. write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
  419. write_i2c_reg(pd->regs, AD_ADDR, AD_POS_REF);
  420. write_i2c_reg(pd->regs, AD_CMD, 34);
  421. write_i2c_reg(pd->regs, AD_ADDR, AD_NEG_REF);
  422. write_i2c_reg(pd->regs, AD_CMD, 0);
  423. /* initialize PM LUT */
  424. write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM);
  425. for (i = 0; i < 256; i++) {
  426. write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
  427. write_i2c_reg(pd->regs, PM_LUT_DATA, i);
  428. }
  429. write_i2c_reg(pd->regs, CONFIG, pd->config | PM_LUT_PGM | PM_LUT_SEL);
  430. for (i = 0; i < 256; i++) {
  431. write_i2c_reg(pd->regs, PM_LUT_ADDR, i);
  432. write_i2c_reg(pd->regs, PM_LUT_DATA, i);
  433. }
  434. write_i2c_reg(pd->regs, CONFIG, pd->config); /* ACQ_MODE_EVEN */
  435. /* select channel 1 for input and set sync level */
  436. write_i2c_reg(pd->regs, AD_ADDR, AD_CMD_REG);
  437. write_i2c_reg(pd->regs, AD_CMD, VIDEO_CNL_1 | SYNC_CNL_1 | SYNC_LVL_3);
  438. /* disable all irqs, clear all irq flags */
  439. iowrite32(FLD_START | FLD_END_EVEN | FLD_END_ODD,
  440. pd->regs + INT_CSR);
  441. return 0;
  442. }
  443. static const struct video_device dt3155_vdev = {
  444. .name = DT3155_NAME,
  445. .fops = &dt3155_fops,
  446. .ioctl_ops = &dt3155_ioctl_ops,
  447. .minor = -1,
  448. .release = video_device_release_empty,
  449. .tvnorms = V4L2_STD_ALL,
  450. };
  451. static int dt3155_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  452. {
  453. int err;
  454. struct dt3155_priv *pd;
  455. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  456. if (err)
  457. return -ENODEV;
  458. pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
  459. if (!pd)
  460. return -ENOMEM;
  461. err = v4l2_device_register(&pdev->dev, &pd->v4l2_dev);
  462. if (err)
  463. return err;
  464. pd->vdev = dt3155_vdev;
  465. pd->vdev.v4l2_dev = &pd->v4l2_dev;
  466. video_set_drvdata(&pd->vdev, pd); /* for use in video_fops */
  467. pd->pdev = pdev;
  468. pd->std = V4L2_STD_625_50;
  469. pd->csr2 = VT_50HZ;
  470. pd->width = 768;
  471. pd->height = 576;
  472. INIT_LIST_HEAD(&pd->dmaq);
  473. mutex_init(&pd->mux);
  474. pd->vdev.lock = &pd->mux; /* for locking v4l2_file_operations */
  475. pd->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  476. pd->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  477. pd->vidq.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  478. pd->vidq.ops = &q_ops;
  479. pd->vidq.mem_ops = &vb2_dma_contig_memops;
  480. pd->vidq.drv_priv = pd;
  481. pd->vidq.min_buffers_needed = 2;
  482. pd->vidq.gfp_flags = GFP_DMA32;
  483. pd->vidq.lock = &pd->mux; /* for locking v4l2_file_operations */
  484. pd->vidq.dev = &pdev->dev;
  485. pd->vdev.queue = &pd->vidq;
  486. err = vb2_queue_init(&pd->vidq);
  487. if (err < 0)
  488. goto err_v4l2_dev_unreg;
  489. spin_lock_init(&pd->lock);
  490. pd->config = ACQ_MODE_EVEN;
  491. err = pci_enable_device(pdev);
  492. if (err)
  493. goto err_v4l2_dev_unreg;
  494. err = pci_request_region(pdev, 0, pci_name(pdev));
  495. if (err)
  496. goto err_pci_disable;
  497. pd->regs = pci_iomap(pdev, 0, pci_resource_len(pd->pdev, 0));
  498. if (!pd->regs) {
  499. err = -ENOMEM;
  500. goto err_free_reg;
  501. }
  502. err = dt3155_init_board(pd);
  503. if (err)
  504. goto err_iounmap;
  505. err = request_irq(pd->pdev->irq, dt3155_irq_handler_even,
  506. IRQF_SHARED, DT3155_NAME, pd);
  507. if (err)
  508. goto err_iounmap;
  509. err = video_register_device(&pd->vdev, VFL_TYPE_GRABBER, -1);
  510. if (err)
  511. goto err_free_irq;
  512. dev_info(&pdev->dev, "/dev/video%i is ready\n", pd->vdev.minor);
  513. return 0; /* success */
  514. err_free_irq:
  515. free_irq(pd->pdev->irq, pd);
  516. err_iounmap:
  517. pci_iounmap(pdev, pd->regs);
  518. err_free_reg:
  519. pci_release_region(pdev, 0);
  520. err_pci_disable:
  521. pci_disable_device(pdev);
  522. err_v4l2_dev_unreg:
  523. v4l2_device_unregister(&pd->v4l2_dev);
  524. return err;
  525. }
  526. static void dt3155_remove(struct pci_dev *pdev)
  527. {
  528. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  529. struct dt3155_priv *pd = container_of(v4l2_dev, struct dt3155_priv,
  530. v4l2_dev);
  531. video_unregister_device(&pd->vdev);
  532. free_irq(pd->pdev->irq, pd);
  533. vb2_queue_release(&pd->vidq);
  534. v4l2_device_unregister(&pd->v4l2_dev);
  535. pci_iounmap(pdev, pd->regs);
  536. pci_release_region(pdev, 0);
  537. pci_disable_device(pdev);
  538. }
  539. static const struct pci_device_id pci_ids[] = {
  540. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, DT3155_DEVICE_ID) },
  541. { 0, /* zero marks the end */ },
  542. };
  543. MODULE_DEVICE_TABLE(pci, pci_ids);
  544. static struct pci_driver pci_driver = {
  545. .name = DT3155_NAME,
  546. .id_table = pci_ids,
  547. .probe = dt3155_probe,
  548. .remove = dt3155_remove,
  549. };
  550. module_pci_driver(pci_driver);
  551. MODULE_DESCRIPTION("video4linux pci-driver for dt3155 frame grabber");
  552. MODULE_AUTHOR("Marin Mitov <mitov@issp.bas.bg>");
  553. MODULE_VERSION(DT3155_VERSION);
  554. MODULE_LICENSE("GPL");