bfin_capture.c 27 KB

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