bfin_capture.c 26 KB

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