bfin_capture.c 27 KB

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