bfin_capture.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Analog Devices video capture driver
  3. *
  4. * Copyright (c) 2011 Analog Devices Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  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. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/time.h>
  32. #include <linux/types.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ctrls.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/videobuf2-dma-contig.h>
  39. #include <asm/dma.h>
  40. #include <media/blackfin/bfin_capture.h>
  41. #include <media/blackfin/ppi.h>
  42. #define CAPTURE_DRV_NAME "bfin_capture"
  43. #define BCAP_MIN_NUM_BUF 2
  44. struct bcap_format {
  45. char *desc;
  46. u32 pixelformat;
  47. enum v4l2_mbus_pixelcode mbus_code;
  48. int bpp; /* bits per pixel */
  49. };
  50. struct bcap_buffer {
  51. struct vb2_buffer vb;
  52. struct list_head list;
  53. };
  54. struct bcap_device {
  55. /* capture device instance */
  56. struct v4l2_device v4l2_dev;
  57. /* v4l2 control handler */
  58. struct v4l2_ctrl_handler ctrl_handler;
  59. /* device node data */
  60. struct video_device *video_dev;
  61. /* sub device instance */
  62. struct v4l2_subdev *sd;
  63. /* capture config */
  64. struct bfin_capture_config *cfg;
  65. /* ppi interface */
  66. struct ppi_if *ppi;
  67. /* current input */
  68. unsigned int cur_input;
  69. /* current selected standard */
  70. v4l2_std_id std;
  71. /* used to store pixel format */
  72. struct v4l2_pix_format fmt;
  73. /* bits per pixel*/
  74. int bpp;
  75. /* used to store sensor supported format */
  76. struct bcap_format *sensor_formats;
  77. /* number of sensor formats array */
  78. int num_sensor_formats;
  79. /* pointing to current video buffer */
  80. struct bcap_buffer *cur_frm;
  81. /* pointing to next video buffer */
  82. struct bcap_buffer *next_frm;
  83. /* buffer queue used in videobuf2 */
  84. struct vb2_queue buffer_queue;
  85. /* allocator-specific contexts for each plane */
  86. struct vb2_alloc_ctx *alloc_ctx;
  87. /* queue of filled frames */
  88. struct list_head dma_queue;
  89. /* used in videobuf2 callback */
  90. spinlock_t lock;
  91. /* used to access capture device */
  92. struct mutex mutex;
  93. /* used to wait ppi to complete one transfer */
  94. struct completion comp;
  95. /* prepare to stop */
  96. bool stop;
  97. };
  98. struct bcap_fh {
  99. struct v4l2_fh fh;
  100. /* indicates whether this file handle is doing IO */
  101. bool io_allowed;
  102. };
  103. static const struct bcap_format bcap_formats[] = {
  104. {
  105. .desc = "YCbCr 4:2:2 Interleaved UYVY",
  106. .pixelformat = V4L2_PIX_FMT_UYVY,
  107. .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
  108. .bpp = 16,
  109. },
  110. {
  111. .desc = "YCbCr 4:2:2 Interleaved YUYV",
  112. .pixelformat = V4L2_PIX_FMT_YUYV,
  113. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  114. .bpp = 16,
  115. },
  116. {
  117. .desc = "RGB 565",
  118. .pixelformat = V4L2_PIX_FMT_RGB565,
  119. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  120. .bpp = 16,
  121. },
  122. {
  123. .desc = "RGB 444",
  124. .pixelformat = V4L2_PIX_FMT_RGB444,
  125. .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
  126. .bpp = 16,
  127. },
  128. };
  129. #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
  130. static irqreturn_t bcap_isr(int irq, void *dev_id);
  131. static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
  132. {
  133. return container_of(vb, struct bcap_buffer, vb);
  134. }
  135. static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
  136. {
  137. enum v4l2_mbus_pixelcode code;
  138. struct bcap_format *sf;
  139. unsigned int num_formats = 0;
  140. int i, j;
  141. while (!v4l2_subdev_call(bcap_dev->sd, video,
  142. enum_mbus_fmt, num_formats, &code))
  143. num_formats++;
  144. if (!num_formats)
  145. return -ENXIO;
  146. sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
  147. if (!sf)
  148. return -ENOMEM;
  149. for (i = 0; i < num_formats; i++) {
  150. v4l2_subdev_call(bcap_dev->sd, video,
  151. enum_mbus_fmt, i, &code);
  152. for (j = 0; j < BCAP_MAX_FMTS; j++)
  153. if (code == bcap_formats[j].mbus_code)
  154. break;
  155. if (j == BCAP_MAX_FMTS) {
  156. /* we don't allow this sensor working with our bridge */
  157. kfree(sf);
  158. return -EINVAL;
  159. }
  160. sf[i] = bcap_formats[j];
  161. }
  162. bcap_dev->sensor_formats = sf;
  163. bcap_dev->num_sensor_formats = num_formats;
  164. return 0;
  165. }
  166. static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
  167. {
  168. bcap_dev->num_sensor_formats = 0;
  169. kfree(bcap_dev->sensor_formats);
  170. bcap_dev->sensor_formats = NULL;
  171. }
  172. static int bcap_open(struct file *file)
  173. {
  174. struct bcap_device *bcap_dev = video_drvdata(file);
  175. struct video_device *vfd = bcap_dev->video_dev;
  176. struct bcap_fh *bcap_fh;
  177. if (!bcap_dev->sd) {
  178. v4l2_err(&bcap_dev->v4l2_dev, "No sub device registered\n");
  179. return -ENODEV;
  180. }
  181. bcap_fh = kzalloc(sizeof(*bcap_fh), GFP_KERNEL);
  182. if (!bcap_fh) {
  183. v4l2_err(&bcap_dev->v4l2_dev,
  184. "unable to allocate memory for file handle object\n");
  185. return -ENOMEM;
  186. }
  187. v4l2_fh_init(&bcap_fh->fh, vfd);
  188. /* store pointer to v4l2_fh in private_data member of file */
  189. file->private_data = &bcap_fh->fh;
  190. v4l2_fh_add(&bcap_fh->fh);
  191. bcap_fh->io_allowed = false;
  192. return 0;
  193. }
  194. static int bcap_release(struct file *file)
  195. {
  196. struct bcap_device *bcap_dev = video_drvdata(file);
  197. struct v4l2_fh *fh = file->private_data;
  198. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  199. /* if this instance is doing IO */
  200. if (bcap_fh->io_allowed)
  201. vb2_queue_release(&bcap_dev->buffer_queue);
  202. file->private_data = NULL;
  203. v4l2_fh_del(&bcap_fh->fh);
  204. v4l2_fh_exit(&bcap_fh->fh);
  205. kfree(bcap_fh);
  206. return 0;
  207. }
  208. static int bcap_mmap(struct file *file, struct vm_area_struct *vma)
  209. {
  210. struct bcap_device *bcap_dev = video_drvdata(file);
  211. int ret;
  212. if (mutex_lock_interruptible(&bcap_dev->mutex))
  213. return -ERESTARTSYS;
  214. ret = vb2_mmap(&bcap_dev->buffer_queue, vma);
  215. mutex_unlock(&bcap_dev->mutex);
  216. return ret;
  217. }
  218. #ifndef CONFIG_MMU
  219. static unsigned long bcap_get_unmapped_area(struct file *file,
  220. unsigned long addr,
  221. unsigned long len,
  222. unsigned long pgoff,
  223. unsigned long flags)
  224. {
  225. struct bcap_device *bcap_dev = video_drvdata(file);
  226. return vb2_get_unmapped_area(&bcap_dev->buffer_queue,
  227. addr,
  228. len,
  229. pgoff,
  230. flags);
  231. }
  232. #endif
  233. static unsigned int bcap_poll(struct file *file, poll_table *wait)
  234. {
  235. struct bcap_device *bcap_dev = video_drvdata(file);
  236. unsigned int res;
  237. mutex_lock(&bcap_dev->mutex);
  238. res = vb2_poll(&bcap_dev->buffer_queue, file, wait);
  239. mutex_unlock(&bcap_dev->mutex);
  240. return res;
  241. }
  242. static int bcap_queue_setup(struct vb2_queue *vq,
  243. const struct v4l2_format *fmt,
  244. unsigned int *nbuffers, unsigned int *nplanes,
  245. unsigned int sizes[], void *alloc_ctxs[])
  246. {
  247. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  248. if (*nbuffers < BCAP_MIN_NUM_BUF)
  249. *nbuffers = BCAP_MIN_NUM_BUF;
  250. *nplanes = 1;
  251. sizes[0] = bcap_dev->fmt.sizeimage;
  252. alloc_ctxs[0] = bcap_dev->alloc_ctx;
  253. return 0;
  254. }
  255. static int bcap_buffer_init(struct vb2_buffer *vb)
  256. {
  257. struct bcap_buffer *buf = to_bcap_vb(vb);
  258. INIT_LIST_HEAD(&buf->list);
  259. return 0;
  260. }
  261. static int bcap_buffer_prepare(struct vb2_buffer *vb)
  262. {
  263. struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
  264. struct bcap_buffer *buf = to_bcap_vb(vb);
  265. unsigned long size;
  266. size = bcap_dev->fmt.sizeimage;
  267. if (vb2_plane_size(vb, 0) < size) {
  268. v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
  269. vb2_plane_size(vb, 0), size);
  270. return -EINVAL;
  271. }
  272. vb2_set_plane_payload(&buf->vb, 0, size);
  273. return 0;
  274. }
  275. static void bcap_buffer_queue(struct vb2_buffer *vb)
  276. {
  277. struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
  278. struct bcap_buffer *buf = to_bcap_vb(vb);
  279. unsigned long flags;
  280. spin_lock_irqsave(&bcap_dev->lock, flags);
  281. list_add_tail(&buf->list, &bcap_dev->dma_queue);
  282. spin_unlock_irqrestore(&bcap_dev->lock, flags);
  283. }
  284. static void bcap_buffer_cleanup(struct vb2_buffer *vb)
  285. {
  286. struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
  287. struct bcap_buffer *buf = to_bcap_vb(vb);
  288. unsigned long flags;
  289. spin_lock_irqsave(&bcap_dev->lock, flags);
  290. list_del_init(&buf->list);
  291. spin_unlock_irqrestore(&bcap_dev->lock, flags);
  292. }
  293. static void bcap_lock(struct vb2_queue *vq)
  294. {
  295. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  296. mutex_lock(&bcap_dev->mutex);
  297. }
  298. static void bcap_unlock(struct vb2_queue *vq)
  299. {
  300. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  301. mutex_unlock(&bcap_dev->mutex);
  302. }
  303. static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
  304. {
  305. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  306. struct ppi_if *ppi = bcap_dev->ppi;
  307. struct ppi_params params;
  308. int ret;
  309. /* enable streamon on the sub device */
  310. ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
  311. if (ret && (ret != -ENOIOCTLCMD)) {
  312. v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
  313. return ret;
  314. }
  315. /* set ppi params */
  316. params.width = bcap_dev->fmt.width;
  317. params.height = bcap_dev->fmt.height;
  318. params.bpp = bcap_dev->bpp;
  319. params.ppi_control = bcap_dev->cfg->ppi_control;
  320. params.int_mask = bcap_dev->cfg->int_mask;
  321. params.blank_clocks = bcap_dev->cfg->blank_clocks;
  322. ret = ppi->ops->set_params(ppi, &params);
  323. if (ret < 0) {
  324. v4l2_err(&bcap_dev->v4l2_dev,
  325. "Error in setting ppi params\n");
  326. return ret;
  327. }
  328. /* attach ppi DMA irq handler */
  329. ret = ppi->ops->attach_irq(ppi, bcap_isr);
  330. if (ret < 0) {
  331. v4l2_err(&bcap_dev->v4l2_dev,
  332. "Error in attaching interrupt handler\n");
  333. return ret;
  334. }
  335. INIT_COMPLETION(bcap_dev->comp);
  336. bcap_dev->stop = false;
  337. return 0;
  338. }
  339. static int bcap_stop_streaming(struct vb2_queue *vq)
  340. {
  341. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  342. struct ppi_if *ppi = bcap_dev->ppi;
  343. int ret;
  344. if (!vb2_is_streaming(vq))
  345. return 0;
  346. bcap_dev->stop = true;
  347. wait_for_completion(&bcap_dev->comp);
  348. ppi->ops->stop(ppi);
  349. ppi->ops->detach_irq(ppi);
  350. ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
  351. if (ret && (ret != -ENOIOCTLCMD))
  352. v4l2_err(&bcap_dev->v4l2_dev,
  353. "stream off failed in subdev\n");
  354. /* release all active buffers */
  355. while (!list_empty(&bcap_dev->dma_queue)) {
  356. bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
  357. struct bcap_buffer, list);
  358. list_del(&bcap_dev->next_frm->list);
  359. vb2_buffer_done(&bcap_dev->next_frm->vb, VB2_BUF_STATE_ERROR);
  360. }
  361. return 0;
  362. }
  363. static struct vb2_ops bcap_video_qops = {
  364. .queue_setup = bcap_queue_setup,
  365. .buf_init = bcap_buffer_init,
  366. .buf_prepare = bcap_buffer_prepare,
  367. .buf_cleanup = bcap_buffer_cleanup,
  368. .buf_queue = bcap_buffer_queue,
  369. .wait_prepare = bcap_unlock,
  370. .wait_finish = bcap_lock,
  371. .start_streaming = bcap_start_streaming,
  372. .stop_streaming = bcap_stop_streaming,
  373. };
  374. static int bcap_reqbufs(struct file *file, void *priv,
  375. struct v4l2_requestbuffers *req_buf)
  376. {
  377. struct bcap_device *bcap_dev = video_drvdata(file);
  378. struct vb2_queue *vq = &bcap_dev->buffer_queue;
  379. struct v4l2_fh *fh = file->private_data;
  380. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  381. if (vb2_is_busy(vq))
  382. return -EBUSY;
  383. bcap_fh->io_allowed = true;
  384. return vb2_reqbufs(vq, req_buf);
  385. }
  386. static int bcap_querybuf(struct file *file, void *priv,
  387. struct v4l2_buffer *buf)
  388. {
  389. struct bcap_device *bcap_dev = video_drvdata(file);
  390. return vb2_querybuf(&bcap_dev->buffer_queue, buf);
  391. }
  392. static int bcap_qbuf(struct file *file, void *priv,
  393. struct v4l2_buffer *buf)
  394. {
  395. struct bcap_device *bcap_dev = video_drvdata(file);
  396. struct v4l2_fh *fh = file->private_data;
  397. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  398. if (!bcap_fh->io_allowed)
  399. return -EBUSY;
  400. return vb2_qbuf(&bcap_dev->buffer_queue, buf);
  401. }
  402. static int bcap_dqbuf(struct file *file, void *priv,
  403. struct v4l2_buffer *buf)
  404. {
  405. struct bcap_device *bcap_dev = video_drvdata(file);
  406. struct v4l2_fh *fh = file->private_data;
  407. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  408. if (!bcap_fh->io_allowed)
  409. return -EBUSY;
  410. return vb2_dqbuf(&bcap_dev->buffer_queue,
  411. buf, file->f_flags & O_NONBLOCK);
  412. }
  413. static irqreturn_t bcap_isr(int irq, void *dev_id)
  414. {
  415. struct ppi_if *ppi = dev_id;
  416. struct bcap_device *bcap_dev = ppi->priv;
  417. struct timeval timevalue;
  418. struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
  419. dma_addr_t addr;
  420. spin_lock(&bcap_dev->lock);
  421. if (bcap_dev->cur_frm != bcap_dev->next_frm) {
  422. do_gettimeofday(&timevalue);
  423. vb->v4l2_buf.timestamp = timevalue;
  424. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  425. bcap_dev->cur_frm = bcap_dev->next_frm;
  426. }
  427. ppi->ops->stop(ppi);
  428. if (bcap_dev->stop) {
  429. complete(&bcap_dev->comp);
  430. } else {
  431. if (!list_empty(&bcap_dev->dma_queue)) {
  432. bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
  433. struct bcap_buffer, list);
  434. list_del(&bcap_dev->next_frm->list);
  435. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->next_frm->vb, 0);
  436. ppi->ops->update_addr(ppi, (unsigned long)addr);
  437. }
  438. ppi->ops->start(ppi);
  439. }
  440. spin_unlock(&bcap_dev->lock);
  441. return IRQ_HANDLED;
  442. }
  443. static int bcap_streamon(struct file *file, void *priv,
  444. enum v4l2_buf_type buf_type)
  445. {
  446. struct bcap_device *bcap_dev = video_drvdata(file);
  447. struct bcap_fh *fh = file->private_data;
  448. struct ppi_if *ppi = bcap_dev->ppi;
  449. dma_addr_t addr;
  450. int ret;
  451. if (!fh->io_allowed)
  452. return -EBUSY;
  453. /* call streamon to start streaming in videobuf */
  454. ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
  455. if (ret)
  456. return ret;
  457. /* if dma queue is empty, return error */
  458. if (list_empty(&bcap_dev->dma_queue)) {
  459. v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
  460. ret = -EINVAL;
  461. goto err;
  462. }
  463. /* get the next frame from the dma queue */
  464. bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
  465. struct bcap_buffer, list);
  466. bcap_dev->cur_frm = bcap_dev->next_frm;
  467. /* remove buffer from the dma queue */
  468. list_del(&bcap_dev->cur_frm->list);
  469. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
  470. /* update DMA address */
  471. ppi->ops->update_addr(ppi, (unsigned long)addr);
  472. /* enable ppi */
  473. ppi->ops->start(ppi);
  474. return 0;
  475. err:
  476. vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  477. return ret;
  478. }
  479. static int bcap_streamoff(struct file *file, void *priv,
  480. enum v4l2_buf_type buf_type)
  481. {
  482. struct bcap_device *bcap_dev = video_drvdata(file);
  483. struct bcap_fh *fh = file->private_data;
  484. if (!fh->io_allowed)
  485. return -EBUSY;
  486. return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  487. }
  488. static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
  489. {
  490. struct bcap_device *bcap_dev = video_drvdata(file);
  491. return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
  492. }
  493. static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
  494. {
  495. struct bcap_device *bcap_dev = video_drvdata(file);
  496. *std = bcap_dev->std;
  497. return 0;
  498. }
  499. static int bcap_s_std(struct file *file, void *priv, v4l2_std_id *std)
  500. {
  501. struct bcap_device *bcap_dev = video_drvdata(file);
  502. int ret;
  503. if (vb2_is_busy(&bcap_dev->buffer_queue))
  504. return -EBUSY;
  505. ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, *std);
  506. if (ret < 0)
  507. return ret;
  508. bcap_dev->std = *std;
  509. return 0;
  510. }
  511. static int bcap_enum_input(struct file *file, void *priv,
  512. struct v4l2_input *input)
  513. {
  514. struct bcap_device *bcap_dev = video_drvdata(file);
  515. struct bfin_capture_config *config = bcap_dev->cfg;
  516. int ret;
  517. u32 status;
  518. if (input->index >= config->num_inputs)
  519. return -EINVAL;
  520. *input = config->inputs[input->index];
  521. /* get input status */
  522. ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
  523. if (!ret)
  524. input->status = status;
  525. return 0;
  526. }
  527. static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
  528. {
  529. struct bcap_device *bcap_dev = video_drvdata(file);
  530. *index = bcap_dev->cur_input;
  531. return 0;
  532. }
  533. static int bcap_s_input(struct file *file, void *priv, unsigned int index)
  534. {
  535. struct bcap_device *bcap_dev = video_drvdata(file);
  536. struct bfin_capture_config *config = bcap_dev->cfg;
  537. struct bcap_route *route;
  538. int ret;
  539. if (vb2_is_busy(&bcap_dev->buffer_queue))
  540. return -EBUSY;
  541. if (index >= config->num_inputs)
  542. return -EINVAL;
  543. route = &config->routes[index];
  544. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  545. route->input, route->output, 0);
  546. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  547. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  548. return ret;
  549. }
  550. bcap_dev->cur_input = index;
  551. return 0;
  552. }
  553. static int bcap_try_format(struct bcap_device *bcap,
  554. struct v4l2_pix_format *pixfmt,
  555. enum v4l2_mbus_pixelcode *mbus_code,
  556. int *bpp)
  557. {
  558. struct bcap_format *sf = bcap->sensor_formats;
  559. struct bcap_format *fmt = NULL;
  560. struct v4l2_mbus_framefmt mbus_fmt;
  561. int ret, i;
  562. for (i = 0; i < bcap->num_sensor_formats; i++) {
  563. fmt = &sf[i];
  564. if (pixfmt->pixelformat == fmt->pixelformat)
  565. break;
  566. }
  567. if (i == bcap->num_sensor_formats)
  568. fmt = &sf[0];
  569. if (mbus_code)
  570. *mbus_code = fmt->mbus_code;
  571. if (bpp)
  572. *bpp = fmt->bpp;
  573. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
  574. ret = v4l2_subdev_call(bcap->sd, video,
  575. try_mbus_fmt, &mbus_fmt);
  576. if (ret < 0)
  577. return ret;
  578. v4l2_fill_pix_format(pixfmt, &mbus_fmt);
  579. pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
  580. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  581. return 0;
  582. }
  583. static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
  584. struct v4l2_fmtdesc *fmt)
  585. {
  586. struct bcap_device *bcap_dev = video_drvdata(file);
  587. struct bcap_format *sf = bcap_dev->sensor_formats;
  588. if (fmt->index >= bcap_dev->num_sensor_formats)
  589. return -EINVAL;
  590. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  591. strlcpy(fmt->description,
  592. sf[fmt->index].desc,
  593. sizeof(fmt->description));
  594. fmt->pixelformat = sf[fmt->index].pixelformat;
  595. return 0;
  596. }
  597. static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
  598. struct v4l2_format *fmt)
  599. {
  600. struct bcap_device *bcap_dev = video_drvdata(file);
  601. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  602. return bcap_try_format(bcap_dev, pixfmt, NULL, NULL);
  603. }
  604. static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
  605. struct v4l2_format *fmt)
  606. {
  607. struct bcap_device *bcap_dev = video_drvdata(file);
  608. fmt->fmt.pix = bcap_dev->fmt;
  609. return 0;
  610. }
  611. static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
  612. struct v4l2_format *fmt)
  613. {
  614. struct bcap_device *bcap_dev = video_drvdata(file);
  615. struct v4l2_mbus_framefmt mbus_fmt;
  616. enum v4l2_mbus_pixelcode mbus_code;
  617. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  618. int ret, bpp;
  619. if (vb2_is_busy(&bcap_dev->buffer_queue))
  620. return -EBUSY;
  621. /* see if format works */
  622. ret = bcap_try_format(bcap_dev, pixfmt, &mbus_code, &bpp);
  623. if (ret < 0)
  624. return ret;
  625. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, mbus_code);
  626. ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
  627. if (ret < 0)
  628. return ret;
  629. bcap_dev->fmt = *pixfmt;
  630. bcap_dev->bpp = bpp;
  631. return 0;
  632. }
  633. static int bcap_querycap(struct file *file, void *priv,
  634. struct v4l2_capability *cap)
  635. {
  636. struct bcap_device *bcap_dev = video_drvdata(file);
  637. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  638. strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
  639. strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
  640. strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
  641. return 0;
  642. }
  643. static int bcap_g_parm(struct file *file, void *fh,
  644. struct v4l2_streamparm *a)
  645. {
  646. struct bcap_device *bcap_dev = video_drvdata(file);
  647. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  648. return -EINVAL;
  649. return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
  650. }
  651. static int bcap_s_parm(struct file *file, void *fh,
  652. struct v4l2_streamparm *a)
  653. {
  654. struct bcap_device *bcap_dev = video_drvdata(file);
  655. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  656. return -EINVAL;
  657. return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
  658. }
  659. static int bcap_g_chip_ident(struct file *file, void *priv,
  660. struct v4l2_dbg_chip_ident *chip)
  661. {
  662. struct bcap_device *bcap_dev = video_drvdata(file);
  663. chip->ident = V4L2_IDENT_NONE;
  664. chip->revision = 0;
  665. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  666. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  667. return -EINVAL;
  668. return v4l2_subdev_call(bcap_dev->sd, core,
  669. g_chip_ident, chip);
  670. }
  671. #ifdef CONFIG_VIDEO_ADV_DEBUG
  672. static int bcap_dbg_g_register(struct file *file, void *priv,
  673. struct v4l2_dbg_register *reg)
  674. {
  675. struct bcap_device *bcap_dev = video_drvdata(file);
  676. return v4l2_subdev_call(bcap_dev->sd, core,
  677. g_register, reg);
  678. }
  679. static int bcap_dbg_s_register(struct file *file, void *priv,
  680. struct v4l2_dbg_register *reg)
  681. {
  682. struct bcap_device *bcap_dev = video_drvdata(file);
  683. return v4l2_subdev_call(bcap_dev->sd, core,
  684. s_register, reg);
  685. }
  686. #endif
  687. static int bcap_log_status(struct file *file, void *priv)
  688. {
  689. struct bcap_device *bcap_dev = video_drvdata(file);
  690. /* status for sub devices */
  691. v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
  692. return 0;
  693. }
  694. static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
  695. .vidioc_querycap = bcap_querycap,
  696. .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
  697. .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
  698. .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
  699. .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
  700. .vidioc_enum_input = bcap_enum_input,
  701. .vidioc_g_input = bcap_g_input,
  702. .vidioc_s_input = bcap_s_input,
  703. .vidioc_querystd = bcap_querystd,
  704. .vidioc_s_std = bcap_s_std,
  705. .vidioc_g_std = bcap_g_std,
  706. .vidioc_reqbufs = bcap_reqbufs,
  707. .vidioc_querybuf = bcap_querybuf,
  708. .vidioc_qbuf = bcap_qbuf,
  709. .vidioc_dqbuf = bcap_dqbuf,
  710. .vidioc_streamon = bcap_streamon,
  711. .vidioc_streamoff = bcap_streamoff,
  712. .vidioc_g_parm = bcap_g_parm,
  713. .vidioc_s_parm = bcap_s_parm,
  714. .vidioc_g_chip_ident = bcap_g_chip_ident,
  715. #ifdef CONFIG_VIDEO_ADV_DEBUG
  716. .vidioc_g_register = bcap_dbg_g_register,
  717. .vidioc_s_register = bcap_dbg_s_register,
  718. #endif
  719. .vidioc_log_status = bcap_log_status,
  720. };
  721. static struct v4l2_file_operations bcap_fops = {
  722. .owner = THIS_MODULE,
  723. .open = bcap_open,
  724. .release = bcap_release,
  725. .unlocked_ioctl = video_ioctl2,
  726. .mmap = bcap_mmap,
  727. #ifndef CONFIG_MMU
  728. .get_unmapped_area = bcap_get_unmapped_area,
  729. #endif
  730. .poll = bcap_poll
  731. };
  732. static int bcap_probe(struct platform_device *pdev)
  733. {
  734. struct bcap_device *bcap_dev;
  735. struct video_device *vfd;
  736. struct i2c_adapter *i2c_adap;
  737. struct bfin_capture_config *config;
  738. struct vb2_queue *q;
  739. int ret;
  740. config = pdev->dev.platform_data;
  741. if (!config) {
  742. v4l2_err(pdev->dev.driver, "Unable to get board config\n");
  743. return -ENODEV;
  744. }
  745. bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
  746. if (!bcap_dev) {
  747. v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
  748. return -ENOMEM;
  749. }
  750. bcap_dev->cfg = config;
  751. bcap_dev->ppi = ppi_create_instance(config->ppi_info);
  752. if (!bcap_dev->ppi) {
  753. v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
  754. ret = -ENODEV;
  755. goto err_free_dev;
  756. }
  757. bcap_dev->ppi->priv = bcap_dev;
  758. bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  759. if (IS_ERR(bcap_dev->alloc_ctx)) {
  760. ret = PTR_ERR(bcap_dev->alloc_ctx);
  761. goto err_free_ppi;
  762. }
  763. vfd = video_device_alloc();
  764. if (!vfd) {
  765. ret = -ENOMEM;
  766. v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
  767. goto err_cleanup_ctx;
  768. }
  769. /* initialize field of video device */
  770. vfd->release = video_device_release;
  771. vfd->fops = &bcap_fops;
  772. vfd->ioctl_ops = &bcap_ioctl_ops;
  773. vfd->tvnorms = 0;
  774. vfd->v4l2_dev = &bcap_dev->v4l2_dev;
  775. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  776. strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
  777. bcap_dev->video_dev = vfd;
  778. ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
  779. if (ret) {
  780. v4l2_err(pdev->dev.driver,
  781. "Unable to register v4l2 device\n");
  782. goto err_release_vdev;
  783. }
  784. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
  785. bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
  786. ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
  787. if (ret) {
  788. v4l2_err(&bcap_dev->v4l2_dev,
  789. "Unable to init control handler\n");
  790. goto err_unreg_v4l2;
  791. }
  792. spin_lock_init(&bcap_dev->lock);
  793. /* initialize queue */
  794. q = &bcap_dev->buffer_queue;
  795. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  796. q->io_modes = VB2_MMAP;
  797. q->drv_priv = bcap_dev;
  798. q->buf_struct_size = sizeof(struct bcap_buffer);
  799. q->ops = &bcap_video_qops;
  800. q->mem_ops = &vb2_dma_contig_memops;
  801. vb2_queue_init(q);
  802. mutex_init(&bcap_dev->mutex);
  803. init_completion(&bcap_dev->comp);
  804. /* init video dma queues */
  805. INIT_LIST_HEAD(&bcap_dev->dma_queue);
  806. vfd->lock = &bcap_dev->mutex;
  807. /* register video device */
  808. ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
  809. if (ret) {
  810. v4l2_err(&bcap_dev->v4l2_dev,
  811. "Unable to register video device\n");
  812. goto err_free_handler;
  813. }
  814. video_set_drvdata(bcap_dev->video_dev, bcap_dev);
  815. v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
  816. video_device_node_name(vfd));
  817. /* load up the subdevice */
  818. i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
  819. if (!i2c_adap) {
  820. v4l2_err(&bcap_dev->v4l2_dev,
  821. "Unable to find i2c adapter\n");
  822. ret = -ENODEV;
  823. goto err_unreg_vdev;
  824. }
  825. bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
  826. i2c_adap,
  827. &config->board_info,
  828. NULL);
  829. if (bcap_dev->sd) {
  830. int i;
  831. /* update tvnorms from the sub devices */
  832. for (i = 0; i < config->num_inputs; i++)
  833. vfd->tvnorms |= config->inputs[i].std;
  834. } else {
  835. v4l2_err(&bcap_dev->v4l2_dev,
  836. "Unable to register sub device\n");
  837. goto err_unreg_vdev;
  838. }
  839. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
  840. /* now we can probe the default state */
  841. if (vfd->tvnorms) {
  842. v4l2_std_id std;
  843. ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std);
  844. if (ret) {
  845. v4l2_err(&bcap_dev->v4l2_dev,
  846. "Unable to get std\n");
  847. goto err_unreg_vdev;
  848. }
  849. bcap_dev->std = std;
  850. }
  851. ret = bcap_init_sensor_formats(bcap_dev);
  852. if (ret) {
  853. v4l2_err(&bcap_dev->v4l2_dev,
  854. "Unable to create sensor formats table\n");
  855. goto err_unreg_vdev;
  856. }
  857. return 0;
  858. err_unreg_vdev:
  859. video_unregister_device(bcap_dev->video_dev);
  860. bcap_dev->video_dev = NULL;
  861. err_free_handler:
  862. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  863. err_unreg_v4l2:
  864. v4l2_device_unregister(&bcap_dev->v4l2_dev);
  865. err_release_vdev:
  866. if (bcap_dev->video_dev)
  867. video_device_release(bcap_dev->video_dev);
  868. err_cleanup_ctx:
  869. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  870. err_free_ppi:
  871. ppi_delete_instance(bcap_dev->ppi);
  872. err_free_dev:
  873. kfree(bcap_dev);
  874. return ret;
  875. }
  876. static int bcap_remove(struct platform_device *pdev)
  877. {
  878. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  879. struct bcap_device *bcap_dev = container_of(v4l2_dev,
  880. struct bcap_device, v4l2_dev);
  881. bcap_free_sensor_formats(bcap_dev);
  882. video_unregister_device(bcap_dev->video_dev);
  883. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  884. v4l2_device_unregister(v4l2_dev);
  885. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  886. ppi_delete_instance(bcap_dev->ppi);
  887. kfree(bcap_dev);
  888. return 0;
  889. }
  890. static struct platform_driver bcap_driver = {
  891. .driver = {
  892. .name = CAPTURE_DRV_NAME,
  893. .owner = THIS_MODULE,
  894. },
  895. .probe = bcap_probe,
  896. .remove = bcap_remove,
  897. };
  898. module_platform_driver(bcap_driver);
  899. MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
  900. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  901. MODULE_LICENSE("GPL v2");