bfin_capture.c 27 KB

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