bfin_capture.c 30 KB

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