bfin_capture.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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 = V4L2_DV_BT_FRAME_WIDTH(bt);
  341. params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
  342. } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
  343. & V4L2_IN_CAP_STD) {
  344. params.hdelay = 0;
  345. params.vdelay = 0;
  346. if (bcap_dev->std & V4L2_STD_525_60) {
  347. params.line = 858;
  348. params.frame = 525;
  349. } else {
  350. params.line = 864;
  351. params.frame = 625;
  352. }
  353. } else {
  354. params.hdelay = 0;
  355. params.vdelay = 0;
  356. params.line = params.width + bcap_dev->cfg->blank_pixels;
  357. params.frame = params.height;
  358. }
  359. ret = ppi->ops->set_params(ppi, &params);
  360. if (ret < 0) {
  361. v4l2_err(&bcap_dev->v4l2_dev,
  362. "Error in setting ppi params\n");
  363. return ret;
  364. }
  365. /* attach ppi DMA irq handler */
  366. ret = ppi->ops->attach_irq(ppi, bcap_isr);
  367. if (ret < 0) {
  368. v4l2_err(&bcap_dev->v4l2_dev,
  369. "Error in attaching interrupt handler\n");
  370. return ret;
  371. }
  372. reinit_completion(&bcap_dev->comp);
  373. bcap_dev->stop = false;
  374. return 0;
  375. }
  376. static void bcap_stop_streaming(struct vb2_queue *vq)
  377. {
  378. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  379. struct ppi_if *ppi = bcap_dev->ppi;
  380. int ret;
  381. bcap_dev->stop = true;
  382. wait_for_completion(&bcap_dev->comp);
  383. ppi->ops->stop(ppi);
  384. ppi->ops->detach_irq(ppi);
  385. ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
  386. if (ret && (ret != -ENOIOCTLCMD))
  387. v4l2_err(&bcap_dev->v4l2_dev,
  388. "stream off failed in subdev\n");
  389. /* release all active buffers */
  390. while (!list_empty(&bcap_dev->dma_queue)) {
  391. bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
  392. struct bcap_buffer, list);
  393. list_del(&bcap_dev->cur_frm->list);
  394. vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
  395. }
  396. }
  397. static struct vb2_ops bcap_video_qops = {
  398. .queue_setup = bcap_queue_setup,
  399. .buf_init = bcap_buffer_init,
  400. .buf_prepare = bcap_buffer_prepare,
  401. .buf_cleanup = bcap_buffer_cleanup,
  402. .buf_queue = bcap_buffer_queue,
  403. .wait_prepare = bcap_unlock,
  404. .wait_finish = bcap_lock,
  405. .start_streaming = bcap_start_streaming,
  406. .stop_streaming = bcap_stop_streaming,
  407. };
  408. static int bcap_reqbufs(struct file *file, void *priv,
  409. struct v4l2_requestbuffers *req_buf)
  410. {
  411. struct bcap_device *bcap_dev = video_drvdata(file);
  412. struct vb2_queue *vq = &bcap_dev->buffer_queue;
  413. struct v4l2_fh *fh = file->private_data;
  414. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  415. if (vb2_is_busy(vq))
  416. return -EBUSY;
  417. bcap_fh->io_allowed = true;
  418. return vb2_reqbufs(vq, req_buf);
  419. }
  420. static int bcap_querybuf(struct file *file, void *priv,
  421. struct v4l2_buffer *buf)
  422. {
  423. struct bcap_device *bcap_dev = video_drvdata(file);
  424. return vb2_querybuf(&bcap_dev->buffer_queue, buf);
  425. }
  426. static int bcap_qbuf(struct file *file, void *priv,
  427. struct v4l2_buffer *buf)
  428. {
  429. struct bcap_device *bcap_dev = video_drvdata(file);
  430. struct v4l2_fh *fh = file->private_data;
  431. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  432. if (!bcap_fh->io_allowed)
  433. return -EBUSY;
  434. return vb2_qbuf(&bcap_dev->buffer_queue, buf);
  435. }
  436. static int bcap_dqbuf(struct file *file, void *priv,
  437. struct v4l2_buffer *buf)
  438. {
  439. struct bcap_device *bcap_dev = video_drvdata(file);
  440. struct v4l2_fh *fh = file->private_data;
  441. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  442. if (!bcap_fh->io_allowed)
  443. return -EBUSY;
  444. return vb2_dqbuf(&bcap_dev->buffer_queue,
  445. buf, file->f_flags & O_NONBLOCK);
  446. }
  447. static irqreturn_t bcap_isr(int irq, void *dev_id)
  448. {
  449. struct ppi_if *ppi = dev_id;
  450. struct bcap_device *bcap_dev = ppi->priv;
  451. struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
  452. dma_addr_t addr;
  453. spin_lock(&bcap_dev->lock);
  454. if (!list_empty(&bcap_dev->dma_queue)) {
  455. v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
  456. if (ppi->err) {
  457. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  458. ppi->err = false;
  459. } else {
  460. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  461. }
  462. bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
  463. struct bcap_buffer, list);
  464. list_del(&bcap_dev->cur_frm->list);
  465. } else {
  466. /* clear error flag, we will get a new frame */
  467. if (ppi->err)
  468. ppi->err = false;
  469. }
  470. ppi->ops->stop(ppi);
  471. if (bcap_dev->stop) {
  472. complete(&bcap_dev->comp);
  473. } else {
  474. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
  475. ppi->ops->update_addr(ppi, (unsigned long)addr);
  476. ppi->ops->start(ppi);
  477. }
  478. spin_unlock(&bcap_dev->lock);
  479. return IRQ_HANDLED;
  480. }
  481. static int bcap_streamon(struct file *file, void *priv,
  482. enum v4l2_buf_type buf_type)
  483. {
  484. struct bcap_device *bcap_dev = video_drvdata(file);
  485. struct bcap_fh *fh = file->private_data;
  486. struct ppi_if *ppi = bcap_dev->ppi;
  487. dma_addr_t addr;
  488. int ret;
  489. if (!fh->io_allowed)
  490. return -EBUSY;
  491. /* call streamon to start streaming in videobuf */
  492. ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
  493. if (ret)
  494. return ret;
  495. /* if dma queue is empty, return error */
  496. if (list_empty(&bcap_dev->dma_queue)) {
  497. v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
  498. ret = -EINVAL;
  499. goto err;
  500. }
  501. /* get the next frame from the dma queue */
  502. bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
  503. struct bcap_buffer, list);
  504. /* remove buffer from the dma queue */
  505. list_del(&bcap_dev->cur_frm->list);
  506. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
  507. /* update DMA address */
  508. ppi->ops->update_addr(ppi, (unsigned long)addr);
  509. /* enable ppi */
  510. ppi->ops->start(ppi);
  511. return 0;
  512. err:
  513. vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  514. return ret;
  515. }
  516. static int bcap_streamoff(struct file *file, void *priv,
  517. enum v4l2_buf_type buf_type)
  518. {
  519. struct bcap_device *bcap_dev = video_drvdata(file);
  520. struct bcap_fh *fh = file->private_data;
  521. if (!fh->io_allowed)
  522. return -EBUSY;
  523. return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  524. }
  525. static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
  526. {
  527. struct bcap_device *bcap_dev = video_drvdata(file);
  528. return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
  529. }
  530. static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
  531. {
  532. struct bcap_device *bcap_dev = video_drvdata(file);
  533. *std = bcap_dev->std;
  534. return 0;
  535. }
  536. static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
  537. {
  538. struct bcap_device *bcap_dev = video_drvdata(file);
  539. int ret;
  540. if (vb2_is_busy(&bcap_dev->buffer_queue))
  541. return -EBUSY;
  542. ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
  543. if (ret < 0)
  544. return ret;
  545. bcap_dev->std = std;
  546. return 0;
  547. }
  548. static int bcap_enum_dv_timings(struct file *file, void *priv,
  549. struct v4l2_enum_dv_timings *timings)
  550. {
  551. struct bcap_device *bcap_dev = video_drvdata(file);
  552. timings->pad = 0;
  553. return v4l2_subdev_call(bcap_dev->sd, pad,
  554. enum_dv_timings, timings);
  555. }
  556. static int bcap_query_dv_timings(struct file *file, void *priv,
  557. struct v4l2_dv_timings *timings)
  558. {
  559. struct bcap_device *bcap_dev = video_drvdata(file);
  560. return v4l2_subdev_call(bcap_dev->sd, video,
  561. query_dv_timings, timings);
  562. }
  563. static int bcap_g_dv_timings(struct file *file, void *priv,
  564. struct v4l2_dv_timings *timings)
  565. {
  566. struct bcap_device *bcap_dev = video_drvdata(file);
  567. *timings = bcap_dev->dv_timings;
  568. return 0;
  569. }
  570. static int bcap_s_dv_timings(struct file *file, void *priv,
  571. struct v4l2_dv_timings *timings)
  572. {
  573. struct bcap_device *bcap_dev = video_drvdata(file);
  574. int ret;
  575. if (vb2_is_busy(&bcap_dev->buffer_queue))
  576. return -EBUSY;
  577. ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
  578. if (ret < 0)
  579. return ret;
  580. bcap_dev->dv_timings = *timings;
  581. return 0;
  582. }
  583. static int bcap_enum_input(struct file *file, void *priv,
  584. struct v4l2_input *input)
  585. {
  586. struct bcap_device *bcap_dev = video_drvdata(file);
  587. struct bfin_capture_config *config = bcap_dev->cfg;
  588. int ret;
  589. u32 status;
  590. if (input->index >= config->num_inputs)
  591. return -EINVAL;
  592. *input = config->inputs[input->index];
  593. /* get input status */
  594. ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
  595. if (!ret)
  596. input->status = status;
  597. return 0;
  598. }
  599. static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
  600. {
  601. struct bcap_device *bcap_dev = video_drvdata(file);
  602. *index = bcap_dev->cur_input;
  603. return 0;
  604. }
  605. static int bcap_s_input(struct file *file, void *priv, unsigned int index)
  606. {
  607. struct bcap_device *bcap_dev = video_drvdata(file);
  608. struct bfin_capture_config *config = bcap_dev->cfg;
  609. struct bcap_route *route;
  610. int ret;
  611. if (vb2_is_busy(&bcap_dev->buffer_queue))
  612. return -EBUSY;
  613. if (index >= config->num_inputs)
  614. return -EINVAL;
  615. route = &config->routes[index];
  616. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  617. route->input, route->output, 0);
  618. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  619. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  620. return ret;
  621. }
  622. bcap_dev->cur_input = index;
  623. /* if this route has specific config, update ppi control */
  624. if (route->ppi_control)
  625. config->ppi_control = route->ppi_control;
  626. return 0;
  627. }
  628. static int bcap_try_format(struct bcap_device *bcap,
  629. struct v4l2_pix_format *pixfmt,
  630. struct bcap_format *bcap_fmt)
  631. {
  632. struct bcap_format *sf = bcap->sensor_formats;
  633. struct bcap_format *fmt = NULL;
  634. struct v4l2_mbus_framefmt mbus_fmt;
  635. int ret, i;
  636. for (i = 0; i < bcap->num_sensor_formats; i++) {
  637. fmt = &sf[i];
  638. if (pixfmt->pixelformat == fmt->pixelformat)
  639. break;
  640. }
  641. if (i == bcap->num_sensor_formats)
  642. fmt = &sf[0];
  643. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
  644. ret = v4l2_subdev_call(bcap->sd, video,
  645. try_mbus_fmt, &mbus_fmt);
  646. if (ret < 0)
  647. return ret;
  648. v4l2_fill_pix_format(pixfmt, &mbus_fmt);
  649. if (bcap_fmt) {
  650. for (i = 0; i < bcap->num_sensor_formats; i++) {
  651. fmt = &sf[i];
  652. if (mbus_fmt.code == fmt->mbus_code)
  653. break;
  654. }
  655. *bcap_fmt = *fmt;
  656. }
  657. pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
  658. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  659. return 0;
  660. }
  661. static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
  662. struct v4l2_fmtdesc *fmt)
  663. {
  664. struct bcap_device *bcap_dev = video_drvdata(file);
  665. struct bcap_format *sf = bcap_dev->sensor_formats;
  666. if (fmt->index >= bcap_dev->num_sensor_formats)
  667. return -EINVAL;
  668. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  669. strlcpy(fmt->description,
  670. sf[fmt->index].desc,
  671. sizeof(fmt->description));
  672. fmt->pixelformat = sf[fmt->index].pixelformat;
  673. return 0;
  674. }
  675. static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
  676. struct v4l2_format *fmt)
  677. {
  678. struct bcap_device *bcap_dev = video_drvdata(file);
  679. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  680. return bcap_try_format(bcap_dev, pixfmt, NULL);
  681. }
  682. static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
  683. struct v4l2_format *fmt)
  684. {
  685. struct bcap_device *bcap_dev = video_drvdata(file);
  686. fmt->fmt.pix = bcap_dev->fmt;
  687. return 0;
  688. }
  689. static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
  690. struct v4l2_format *fmt)
  691. {
  692. struct bcap_device *bcap_dev = video_drvdata(file);
  693. struct v4l2_mbus_framefmt mbus_fmt;
  694. struct bcap_format bcap_fmt;
  695. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  696. int ret;
  697. if (vb2_is_busy(&bcap_dev->buffer_queue))
  698. return -EBUSY;
  699. /* see if format works */
  700. ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
  701. if (ret < 0)
  702. return ret;
  703. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
  704. ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
  705. if (ret < 0)
  706. return ret;
  707. bcap_dev->fmt = *pixfmt;
  708. bcap_dev->bpp = bcap_fmt.bpp;
  709. bcap_dev->dlen = bcap_fmt.dlen;
  710. return 0;
  711. }
  712. static int bcap_querycap(struct file *file, void *priv,
  713. struct v4l2_capability *cap)
  714. {
  715. struct bcap_device *bcap_dev = video_drvdata(file);
  716. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  717. strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
  718. strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
  719. strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
  720. return 0;
  721. }
  722. static int bcap_g_parm(struct file *file, void *fh,
  723. struct v4l2_streamparm *a)
  724. {
  725. struct bcap_device *bcap_dev = video_drvdata(file);
  726. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  727. return -EINVAL;
  728. return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
  729. }
  730. static int bcap_s_parm(struct file *file, void *fh,
  731. struct v4l2_streamparm *a)
  732. {
  733. struct bcap_device *bcap_dev = video_drvdata(file);
  734. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  735. return -EINVAL;
  736. return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
  737. }
  738. static int bcap_log_status(struct file *file, void *priv)
  739. {
  740. struct bcap_device *bcap_dev = video_drvdata(file);
  741. /* status for sub devices */
  742. v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
  743. return 0;
  744. }
  745. static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
  746. .vidioc_querycap = bcap_querycap,
  747. .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
  748. .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
  749. .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
  750. .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
  751. .vidioc_enum_input = bcap_enum_input,
  752. .vidioc_g_input = bcap_g_input,
  753. .vidioc_s_input = bcap_s_input,
  754. .vidioc_querystd = bcap_querystd,
  755. .vidioc_s_std = bcap_s_std,
  756. .vidioc_g_std = bcap_g_std,
  757. .vidioc_s_dv_timings = bcap_s_dv_timings,
  758. .vidioc_g_dv_timings = bcap_g_dv_timings,
  759. .vidioc_query_dv_timings = bcap_query_dv_timings,
  760. .vidioc_enum_dv_timings = bcap_enum_dv_timings,
  761. .vidioc_reqbufs = bcap_reqbufs,
  762. .vidioc_querybuf = bcap_querybuf,
  763. .vidioc_qbuf = bcap_qbuf,
  764. .vidioc_dqbuf = bcap_dqbuf,
  765. .vidioc_streamon = bcap_streamon,
  766. .vidioc_streamoff = bcap_streamoff,
  767. .vidioc_g_parm = bcap_g_parm,
  768. .vidioc_s_parm = bcap_s_parm,
  769. .vidioc_log_status = bcap_log_status,
  770. };
  771. static struct v4l2_file_operations bcap_fops = {
  772. .owner = THIS_MODULE,
  773. .open = bcap_open,
  774. .release = bcap_release,
  775. .unlocked_ioctl = video_ioctl2,
  776. .mmap = bcap_mmap,
  777. #ifndef CONFIG_MMU
  778. .get_unmapped_area = bcap_get_unmapped_area,
  779. #endif
  780. .poll = bcap_poll
  781. };
  782. static int bcap_probe(struct platform_device *pdev)
  783. {
  784. struct bcap_device *bcap_dev;
  785. struct video_device *vfd;
  786. struct i2c_adapter *i2c_adap;
  787. struct bfin_capture_config *config;
  788. struct vb2_queue *q;
  789. struct bcap_route *route;
  790. int ret;
  791. config = pdev->dev.platform_data;
  792. if (!config || !config->num_inputs) {
  793. v4l2_err(pdev->dev.driver, "Unable to get board config\n");
  794. return -ENODEV;
  795. }
  796. bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
  797. if (!bcap_dev) {
  798. v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
  799. return -ENOMEM;
  800. }
  801. bcap_dev->cfg = config;
  802. bcap_dev->ppi = ppi_create_instance(config->ppi_info);
  803. if (!bcap_dev->ppi) {
  804. v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
  805. ret = -ENODEV;
  806. goto err_free_dev;
  807. }
  808. bcap_dev->ppi->priv = bcap_dev;
  809. bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  810. if (IS_ERR(bcap_dev->alloc_ctx)) {
  811. ret = PTR_ERR(bcap_dev->alloc_ctx);
  812. goto err_free_ppi;
  813. }
  814. vfd = video_device_alloc();
  815. if (!vfd) {
  816. ret = -ENOMEM;
  817. v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
  818. goto err_cleanup_ctx;
  819. }
  820. /* initialize field of video device */
  821. vfd->release = video_device_release;
  822. vfd->fops = &bcap_fops;
  823. vfd->ioctl_ops = &bcap_ioctl_ops;
  824. vfd->tvnorms = 0;
  825. vfd->v4l2_dev = &bcap_dev->v4l2_dev;
  826. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  827. strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
  828. bcap_dev->video_dev = vfd;
  829. ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
  830. if (ret) {
  831. v4l2_err(pdev->dev.driver,
  832. "Unable to register v4l2 device\n");
  833. goto err_release_vdev;
  834. }
  835. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
  836. bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
  837. ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
  838. if (ret) {
  839. v4l2_err(&bcap_dev->v4l2_dev,
  840. "Unable to init control handler\n");
  841. goto err_unreg_v4l2;
  842. }
  843. spin_lock_init(&bcap_dev->lock);
  844. /* initialize queue */
  845. q = &bcap_dev->buffer_queue;
  846. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  847. q->io_modes = VB2_MMAP;
  848. q->drv_priv = bcap_dev;
  849. q->buf_struct_size = sizeof(struct bcap_buffer);
  850. q->ops = &bcap_video_qops;
  851. q->mem_ops = &vb2_dma_contig_memops;
  852. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  853. ret = vb2_queue_init(q);
  854. if (ret)
  855. goto err_free_handler;
  856. mutex_init(&bcap_dev->mutex);
  857. init_completion(&bcap_dev->comp);
  858. /* init video dma queues */
  859. INIT_LIST_HEAD(&bcap_dev->dma_queue);
  860. vfd->lock = &bcap_dev->mutex;
  861. /* register video device */
  862. ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
  863. if (ret) {
  864. v4l2_err(&bcap_dev->v4l2_dev,
  865. "Unable to register video device\n");
  866. goto err_free_handler;
  867. }
  868. video_set_drvdata(bcap_dev->video_dev, bcap_dev);
  869. v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
  870. video_device_node_name(vfd));
  871. /* load up the subdevice */
  872. i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
  873. if (!i2c_adap) {
  874. v4l2_err(&bcap_dev->v4l2_dev,
  875. "Unable to find i2c adapter\n");
  876. ret = -ENODEV;
  877. goto err_unreg_vdev;
  878. }
  879. bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
  880. i2c_adap,
  881. &config->board_info,
  882. NULL);
  883. if (bcap_dev->sd) {
  884. int i;
  885. /* update tvnorms from the sub devices */
  886. for (i = 0; i < config->num_inputs; i++)
  887. vfd->tvnorms |= config->inputs[i].std;
  888. } else {
  889. v4l2_err(&bcap_dev->v4l2_dev,
  890. "Unable to register sub device\n");
  891. ret = -ENODEV;
  892. goto err_unreg_vdev;
  893. }
  894. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
  895. /*
  896. * explicitly set input, otherwise some boards
  897. * may not work at the state as we expected
  898. */
  899. route = &config->routes[0];
  900. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  901. route->input, route->output, 0);
  902. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  903. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  904. goto err_unreg_vdev;
  905. }
  906. bcap_dev->cur_input = 0;
  907. /* if this route has specific config, update ppi control */
  908. if (route->ppi_control)
  909. config->ppi_control = route->ppi_control;
  910. /* now we can probe the default state */
  911. if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
  912. v4l2_std_id std;
  913. ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
  914. if (ret) {
  915. v4l2_err(&bcap_dev->v4l2_dev,
  916. "Unable to get std\n");
  917. goto err_unreg_vdev;
  918. }
  919. bcap_dev->std = std;
  920. }
  921. if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
  922. struct v4l2_dv_timings dv_timings;
  923. ret = v4l2_subdev_call(bcap_dev->sd, video,
  924. g_dv_timings, &dv_timings);
  925. if (ret) {
  926. v4l2_err(&bcap_dev->v4l2_dev,
  927. "Unable to get dv timings\n");
  928. goto err_unreg_vdev;
  929. }
  930. bcap_dev->dv_timings = dv_timings;
  931. }
  932. ret = bcap_init_sensor_formats(bcap_dev);
  933. if (ret) {
  934. v4l2_err(&bcap_dev->v4l2_dev,
  935. "Unable to create sensor formats table\n");
  936. goto err_unreg_vdev;
  937. }
  938. return 0;
  939. err_unreg_vdev:
  940. video_unregister_device(bcap_dev->video_dev);
  941. bcap_dev->video_dev = NULL;
  942. err_free_handler:
  943. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  944. err_unreg_v4l2:
  945. v4l2_device_unregister(&bcap_dev->v4l2_dev);
  946. err_release_vdev:
  947. if (bcap_dev->video_dev)
  948. video_device_release(bcap_dev->video_dev);
  949. err_cleanup_ctx:
  950. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  951. err_free_ppi:
  952. ppi_delete_instance(bcap_dev->ppi);
  953. err_free_dev:
  954. kfree(bcap_dev);
  955. return ret;
  956. }
  957. static int bcap_remove(struct platform_device *pdev)
  958. {
  959. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  960. struct bcap_device *bcap_dev = container_of(v4l2_dev,
  961. struct bcap_device, v4l2_dev);
  962. bcap_free_sensor_formats(bcap_dev);
  963. video_unregister_device(bcap_dev->video_dev);
  964. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  965. v4l2_device_unregister(v4l2_dev);
  966. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  967. ppi_delete_instance(bcap_dev->ppi);
  968. kfree(bcap_dev);
  969. return 0;
  970. }
  971. static struct platform_driver bcap_driver = {
  972. .driver = {
  973. .name = CAPTURE_DRV_NAME,
  974. .owner = THIS_MODULE,
  975. },
  976. .probe = bcap_probe,
  977. .remove = bcap_remove,
  978. };
  979. module_platform_driver(bcap_driver);
  980. MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
  981. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  982. MODULE_LICENSE("GPL v2");