bfin_capture.c 26 KB

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