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