bfin_capture.c 30 KB

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