bfin_capture.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 = V4L2_DV_BT_FRAME_WIDTH(bt);
  341. params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
  342. } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
  343. & V4L2_IN_CAP_STD) {
  344. params.hdelay = 0;
  345. params.vdelay = 0;
  346. if (bcap_dev->std & V4L2_STD_525_60) {
  347. params.line = 858;
  348. params.frame = 525;
  349. } else {
  350. params.line = 864;
  351. params.frame = 625;
  352. }
  353. } else {
  354. params.hdelay = 0;
  355. params.vdelay = 0;
  356. params.line = params.width + bcap_dev->cfg->blank_pixels;
  357. params.frame = params.height;
  358. }
  359. ret = ppi->ops->set_params(ppi, &params);
  360. if (ret < 0) {
  361. v4l2_err(&bcap_dev->v4l2_dev,
  362. "Error in setting ppi params\n");
  363. return ret;
  364. }
  365. /* attach ppi DMA irq handler */
  366. ret = ppi->ops->attach_irq(ppi, bcap_isr);
  367. if (ret < 0) {
  368. v4l2_err(&bcap_dev->v4l2_dev,
  369. "Error in attaching interrupt handler\n");
  370. return ret;
  371. }
  372. INIT_COMPLETION(bcap_dev->comp);
  373. bcap_dev->stop = false;
  374. return 0;
  375. }
  376. static int bcap_stop_streaming(struct vb2_queue *vq)
  377. {
  378. struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
  379. struct ppi_if *ppi = bcap_dev->ppi;
  380. int ret;
  381. if (!vb2_is_streaming(vq))
  382. return 0;
  383. bcap_dev->stop = true;
  384. wait_for_completion(&bcap_dev->comp);
  385. ppi->ops->stop(ppi);
  386. ppi->ops->detach_irq(ppi);
  387. ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
  388. if (ret && (ret != -ENOIOCTLCMD))
  389. v4l2_err(&bcap_dev->v4l2_dev,
  390. "stream off failed in subdev\n");
  391. /* release all active buffers */
  392. while (!list_empty(&bcap_dev->dma_queue)) {
  393. bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
  394. struct bcap_buffer, list);
  395. list_del(&bcap_dev->cur_frm->list);
  396. vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
  397. }
  398. return 0;
  399. }
  400. static struct vb2_ops bcap_video_qops = {
  401. .queue_setup = bcap_queue_setup,
  402. .buf_init = bcap_buffer_init,
  403. .buf_prepare = bcap_buffer_prepare,
  404. .buf_cleanup = bcap_buffer_cleanup,
  405. .buf_queue = bcap_buffer_queue,
  406. .wait_prepare = bcap_unlock,
  407. .wait_finish = bcap_lock,
  408. .start_streaming = bcap_start_streaming,
  409. .stop_streaming = bcap_stop_streaming,
  410. };
  411. static int bcap_reqbufs(struct file *file, void *priv,
  412. struct v4l2_requestbuffers *req_buf)
  413. {
  414. struct bcap_device *bcap_dev = video_drvdata(file);
  415. struct vb2_queue *vq = &bcap_dev->buffer_queue;
  416. struct v4l2_fh *fh = file->private_data;
  417. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  418. if (vb2_is_busy(vq))
  419. return -EBUSY;
  420. bcap_fh->io_allowed = true;
  421. return vb2_reqbufs(vq, req_buf);
  422. }
  423. static int bcap_querybuf(struct file *file, void *priv,
  424. struct v4l2_buffer *buf)
  425. {
  426. struct bcap_device *bcap_dev = video_drvdata(file);
  427. return vb2_querybuf(&bcap_dev->buffer_queue, buf);
  428. }
  429. static int bcap_qbuf(struct file *file, void *priv,
  430. struct v4l2_buffer *buf)
  431. {
  432. struct bcap_device *bcap_dev = video_drvdata(file);
  433. struct v4l2_fh *fh = file->private_data;
  434. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  435. if (!bcap_fh->io_allowed)
  436. return -EBUSY;
  437. return vb2_qbuf(&bcap_dev->buffer_queue, buf);
  438. }
  439. static int bcap_dqbuf(struct file *file, void *priv,
  440. struct v4l2_buffer *buf)
  441. {
  442. struct bcap_device *bcap_dev = video_drvdata(file);
  443. struct v4l2_fh *fh = file->private_data;
  444. struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
  445. if (!bcap_fh->io_allowed)
  446. return -EBUSY;
  447. return vb2_dqbuf(&bcap_dev->buffer_queue,
  448. buf, file->f_flags & O_NONBLOCK);
  449. }
  450. static irqreturn_t bcap_isr(int irq, void *dev_id)
  451. {
  452. struct ppi_if *ppi = dev_id;
  453. struct bcap_device *bcap_dev = ppi->priv;
  454. struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
  455. dma_addr_t addr;
  456. spin_lock(&bcap_dev->lock);
  457. if (!list_empty(&bcap_dev->dma_queue)) {
  458. v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
  459. if (ppi->err) {
  460. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  461. ppi->err = false;
  462. } else {
  463. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  464. }
  465. bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
  466. struct bcap_buffer, list);
  467. list_del(&bcap_dev->cur_frm->list);
  468. } else {
  469. /* clear error flag, we will get a new frame */
  470. if (ppi->err)
  471. ppi->err = false;
  472. }
  473. ppi->ops->stop(ppi);
  474. if (bcap_dev->stop) {
  475. complete(&bcap_dev->comp);
  476. } else {
  477. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
  478. ppi->ops->update_addr(ppi, (unsigned long)addr);
  479. ppi->ops->start(ppi);
  480. }
  481. spin_unlock(&bcap_dev->lock);
  482. return IRQ_HANDLED;
  483. }
  484. static int bcap_streamon(struct file *file, void *priv,
  485. enum v4l2_buf_type buf_type)
  486. {
  487. struct bcap_device *bcap_dev = video_drvdata(file);
  488. struct bcap_fh *fh = file->private_data;
  489. struct ppi_if *ppi = bcap_dev->ppi;
  490. dma_addr_t addr;
  491. int ret;
  492. if (!fh->io_allowed)
  493. return -EBUSY;
  494. /* call streamon to start streaming in videobuf */
  495. ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
  496. if (ret)
  497. return ret;
  498. /* if dma queue is empty, return error */
  499. if (list_empty(&bcap_dev->dma_queue)) {
  500. v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
  501. ret = -EINVAL;
  502. goto err;
  503. }
  504. /* get the next frame from the dma queue */
  505. bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
  506. struct bcap_buffer, list);
  507. /* remove buffer from the dma queue */
  508. list_del(&bcap_dev->cur_frm->list);
  509. addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
  510. /* update DMA address */
  511. ppi->ops->update_addr(ppi, (unsigned long)addr);
  512. /* enable ppi */
  513. ppi->ops->start(ppi);
  514. return 0;
  515. err:
  516. vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  517. return ret;
  518. }
  519. static int bcap_streamoff(struct file *file, void *priv,
  520. enum v4l2_buf_type buf_type)
  521. {
  522. struct bcap_device *bcap_dev = video_drvdata(file);
  523. struct bcap_fh *fh = file->private_data;
  524. if (!fh->io_allowed)
  525. return -EBUSY;
  526. return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
  527. }
  528. static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
  529. {
  530. struct bcap_device *bcap_dev = video_drvdata(file);
  531. return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
  532. }
  533. static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
  534. {
  535. struct bcap_device *bcap_dev = video_drvdata(file);
  536. *std = bcap_dev->std;
  537. return 0;
  538. }
  539. static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
  540. {
  541. struct bcap_device *bcap_dev = video_drvdata(file);
  542. int ret;
  543. if (vb2_is_busy(&bcap_dev->buffer_queue))
  544. return -EBUSY;
  545. ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, std);
  546. if (ret < 0)
  547. return ret;
  548. bcap_dev->std = std;
  549. return 0;
  550. }
  551. static int bcap_enum_dv_timings(struct file *file, void *priv,
  552. struct v4l2_enum_dv_timings *timings)
  553. {
  554. struct bcap_device *bcap_dev = video_drvdata(file);
  555. return v4l2_subdev_call(bcap_dev->sd, video,
  556. enum_dv_timings, timings);
  557. }
  558. static int bcap_query_dv_timings(struct file *file, void *priv,
  559. struct v4l2_dv_timings *timings)
  560. {
  561. struct bcap_device *bcap_dev = video_drvdata(file);
  562. return v4l2_subdev_call(bcap_dev->sd, video,
  563. query_dv_timings, timings);
  564. }
  565. static int bcap_g_dv_timings(struct file *file, void *priv,
  566. struct v4l2_dv_timings *timings)
  567. {
  568. struct bcap_device *bcap_dev = video_drvdata(file);
  569. *timings = bcap_dev->dv_timings;
  570. return 0;
  571. }
  572. static int bcap_s_dv_timings(struct file *file, void *priv,
  573. struct v4l2_dv_timings *timings)
  574. {
  575. struct bcap_device *bcap_dev = video_drvdata(file);
  576. int ret;
  577. if (vb2_is_busy(&bcap_dev->buffer_queue))
  578. return -EBUSY;
  579. ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
  580. if (ret < 0)
  581. return ret;
  582. bcap_dev->dv_timings = *timings;
  583. return 0;
  584. }
  585. static int bcap_enum_input(struct file *file, void *priv,
  586. struct v4l2_input *input)
  587. {
  588. struct bcap_device *bcap_dev = video_drvdata(file);
  589. struct bfin_capture_config *config = bcap_dev->cfg;
  590. int ret;
  591. u32 status;
  592. if (input->index >= config->num_inputs)
  593. return -EINVAL;
  594. *input = config->inputs[input->index];
  595. /* get input status */
  596. ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
  597. if (!ret)
  598. input->status = status;
  599. return 0;
  600. }
  601. static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
  602. {
  603. struct bcap_device *bcap_dev = video_drvdata(file);
  604. *index = bcap_dev->cur_input;
  605. return 0;
  606. }
  607. static int bcap_s_input(struct file *file, void *priv, unsigned int index)
  608. {
  609. struct bcap_device *bcap_dev = video_drvdata(file);
  610. struct bfin_capture_config *config = bcap_dev->cfg;
  611. struct bcap_route *route;
  612. int ret;
  613. if (vb2_is_busy(&bcap_dev->buffer_queue))
  614. return -EBUSY;
  615. if (index >= config->num_inputs)
  616. return -EINVAL;
  617. route = &config->routes[index];
  618. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  619. route->input, route->output, 0);
  620. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  621. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  622. return ret;
  623. }
  624. bcap_dev->cur_input = index;
  625. /* if this route has specific config, update ppi control */
  626. if (route->ppi_control)
  627. config->ppi_control = route->ppi_control;
  628. return 0;
  629. }
  630. static int bcap_try_format(struct bcap_device *bcap,
  631. struct v4l2_pix_format *pixfmt,
  632. struct bcap_format *bcap_fmt)
  633. {
  634. struct bcap_format *sf = bcap->sensor_formats;
  635. struct bcap_format *fmt = NULL;
  636. struct v4l2_mbus_framefmt mbus_fmt;
  637. int ret, i;
  638. for (i = 0; i < bcap->num_sensor_formats; i++) {
  639. fmt = &sf[i];
  640. if (pixfmt->pixelformat == fmt->pixelformat)
  641. break;
  642. }
  643. if (i == bcap->num_sensor_formats)
  644. fmt = &sf[0];
  645. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
  646. ret = v4l2_subdev_call(bcap->sd, video,
  647. try_mbus_fmt, &mbus_fmt);
  648. if (ret < 0)
  649. return ret;
  650. v4l2_fill_pix_format(pixfmt, &mbus_fmt);
  651. if (bcap_fmt) {
  652. for (i = 0; i < bcap->num_sensor_formats; i++) {
  653. fmt = &sf[i];
  654. if (mbus_fmt.code == fmt->mbus_code)
  655. break;
  656. }
  657. *bcap_fmt = *fmt;
  658. }
  659. pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
  660. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  661. return 0;
  662. }
  663. static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
  664. struct v4l2_fmtdesc *fmt)
  665. {
  666. struct bcap_device *bcap_dev = video_drvdata(file);
  667. struct bcap_format *sf = bcap_dev->sensor_formats;
  668. if (fmt->index >= bcap_dev->num_sensor_formats)
  669. return -EINVAL;
  670. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  671. strlcpy(fmt->description,
  672. sf[fmt->index].desc,
  673. sizeof(fmt->description));
  674. fmt->pixelformat = sf[fmt->index].pixelformat;
  675. return 0;
  676. }
  677. static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
  678. struct v4l2_format *fmt)
  679. {
  680. struct bcap_device *bcap_dev = video_drvdata(file);
  681. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  682. return bcap_try_format(bcap_dev, pixfmt, NULL);
  683. }
  684. static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
  685. struct v4l2_format *fmt)
  686. {
  687. struct bcap_device *bcap_dev = video_drvdata(file);
  688. fmt->fmt.pix = bcap_dev->fmt;
  689. return 0;
  690. }
  691. static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
  692. struct v4l2_format *fmt)
  693. {
  694. struct bcap_device *bcap_dev = video_drvdata(file);
  695. struct v4l2_mbus_framefmt mbus_fmt;
  696. struct bcap_format bcap_fmt;
  697. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  698. int ret;
  699. if (vb2_is_busy(&bcap_dev->buffer_queue))
  700. return -EBUSY;
  701. /* see if format works */
  702. ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
  703. if (ret < 0)
  704. return ret;
  705. v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
  706. ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
  707. if (ret < 0)
  708. return ret;
  709. bcap_dev->fmt = *pixfmt;
  710. bcap_dev->bpp = bcap_fmt.bpp;
  711. bcap_dev->dlen = bcap_fmt.dlen;
  712. return 0;
  713. }
  714. static int bcap_querycap(struct file *file, void *priv,
  715. struct v4l2_capability *cap)
  716. {
  717. struct bcap_device *bcap_dev = video_drvdata(file);
  718. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  719. strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
  720. strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
  721. strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
  722. return 0;
  723. }
  724. static int bcap_g_parm(struct file *file, void *fh,
  725. struct v4l2_streamparm *a)
  726. {
  727. struct bcap_device *bcap_dev = video_drvdata(file);
  728. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  729. return -EINVAL;
  730. return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
  731. }
  732. static int bcap_s_parm(struct file *file, void *fh,
  733. struct v4l2_streamparm *a)
  734. {
  735. struct bcap_device *bcap_dev = video_drvdata(file);
  736. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  737. return -EINVAL;
  738. return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
  739. }
  740. static int bcap_log_status(struct file *file, void *priv)
  741. {
  742. struct bcap_device *bcap_dev = video_drvdata(file);
  743. /* status for sub devices */
  744. v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
  745. return 0;
  746. }
  747. static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
  748. .vidioc_querycap = bcap_querycap,
  749. .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
  750. .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
  751. .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
  752. .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
  753. .vidioc_enum_input = bcap_enum_input,
  754. .vidioc_g_input = bcap_g_input,
  755. .vidioc_s_input = bcap_s_input,
  756. .vidioc_querystd = bcap_querystd,
  757. .vidioc_s_std = bcap_s_std,
  758. .vidioc_g_std = bcap_g_std,
  759. .vidioc_s_dv_timings = bcap_s_dv_timings,
  760. .vidioc_g_dv_timings = bcap_g_dv_timings,
  761. .vidioc_query_dv_timings = bcap_query_dv_timings,
  762. .vidioc_enum_dv_timings = bcap_enum_dv_timings,
  763. .vidioc_reqbufs = bcap_reqbufs,
  764. .vidioc_querybuf = bcap_querybuf,
  765. .vidioc_qbuf = bcap_qbuf,
  766. .vidioc_dqbuf = bcap_dqbuf,
  767. .vidioc_streamon = bcap_streamon,
  768. .vidioc_streamoff = bcap_streamoff,
  769. .vidioc_g_parm = bcap_g_parm,
  770. .vidioc_s_parm = bcap_s_parm,
  771. .vidioc_log_status = bcap_log_status,
  772. };
  773. static struct v4l2_file_operations bcap_fops = {
  774. .owner = THIS_MODULE,
  775. .open = bcap_open,
  776. .release = bcap_release,
  777. .unlocked_ioctl = video_ioctl2,
  778. .mmap = bcap_mmap,
  779. #ifndef CONFIG_MMU
  780. .get_unmapped_area = bcap_get_unmapped_area,
  781. #endif
  782. .poll = bcap_poll
  783. };
  784. static int bcap_probe(struct platform_device *pdev)
  785. {
  786. struct bcap_device *bcap_dev;
  787. struct video_device *vfd;
  788. struct i2c_adapter *i2c_adap;
  789. struct bfin_capture_config *config;
  790. struct vb2_queue *q;
  791. struct bcap_route *route;
  792. int ret;
  793. config = pdev->dev.platform_data;
  794. if (!config || !config->num_inputs) {
  795. v4l2_err(pdev->dev.driver, "Unable to get board config\n");
  796. return -ENODEV;
  797. }
  798. bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
  799. if (!bcap_dev) {
  800. v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
  801. return -ENOMEM;
  802. }
  803. bcap_dev->cfg = config;
  804. bcap_dev->ppi = ppi_create_instance(config->ppi_info);
  805. if (!bcap_dev->ppi) {
  806. v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
  807. ret = -ENODEV;
  808. goto err_free_dev;
  809. }
  810. bcap_dev->ppi->priv = bcap_dev;
  811. bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  812. if (IS_ERR(bcap_dev->alloc_ctx)) {
  813. ret = PTR_ERR(bcap_dev->alloc_ctx);
  814. goto err_free_ppi;
  815. }
  816. vfd = video_device_alloc();
  817. if (!vfd) {
  818. ret = -ENOMEM;
  819. v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
  820. goto err_cleanup_ctx;
  821. }
  822. /* initialize field of video device */
  823. vfd->release = video_device_release;
  824. vfd->fops = &bcap_fops;
  825. vfd->ioctl_ops = &bcap_ioctl_ops;
  826. vfd->tvnorms = 0;
  827. vfd->v4l2_dev = &bcap_dev->v4l2_dev;
  828. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  829. strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
  830. bcap_dev->video_dev = vfd;
  831. ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
  832. if (ret) {
  833. v4l2_err(pdev->dev.driver,
  834. "Unable to register v4l2 device\n");
  835. goto err_release_vdev;
  836. }
  837. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
  838. bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
  839. ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
  840. if (ret) {
  841. v4l2_err(&bcap_dev->v4l2_dev,
  842. "Unable to init control handler\n");
  843. goto err_unreg_v4l2;
  844. }
  845. spin_lock_init(&bcap_dev->lock);
  846. /* initialize queue */
  847. q = &bcap_dev->buffer_queue;
  848. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  849. q->io_modes = VB2_MMAP;
  850. q->drv_priv = bcap_dev;
  851. q->buf_struct_size = sizeof(struct bcap_buffer);
  852. q->ops = &bcap_video_qops;
  853. q->mem_ops = &vb2_dma_contig_memops;
  854. q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  855. ret = vb2_queue_init(q);
  856. if (ret)
  857. goto err_free_handler;
  858. mutex_init(&bcap_dev->mutex);
  859. init_completion(&bcap_dev->comp);
  860. /* init video dma queues */
  861. INIT_LIST_HEAD(&bcap_dev->dma_queue);
  862. vfd->lock = &bcap_dev->mutex;
  863. /* register video device */
  864. ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
  865. if (ret) {
  866. v4l2_err(&bcap_dev->v4l2_dev,
  867. "Unable to register video device\n");
  868. goto err_free_handler;
  869. }
  870. video_set_drvdata(bcap_dev->video_dev, bcap_dev);
  871. v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
  872. video_device_node_name(vfd));
  873. /* load up the subdevice */
  874. i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
  875. if (!i2c_adap) {
  876. v4l2_err(&bcap_dev->v4l2_dev,
  877. "Unable to find i2c adapter\n");
  878. ret = -ENODEV;
  879. goto err_unreg_vdev;
  880. }
  881. bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
  882. i2c_adap,
  883. &config->board_info,
  884. NULL);
  885. if (bcap_dev->sd) {
  886. int i;
  887. /* update tvnorms from the sub devices */
  888. for (i = 0; i < config->num_inputs; i++)
  889. vfd->tvnorms |= config->inputs[i].std;
  890. } else {
  891. v4l2_err(&bcap_dev->v4l2_dev,
  892. "Unable to register sub device\n");
  893. ret = -ENODEV;
  894. goto err_unreg_vdev;
  895. }
  896. v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
  897. /*
  898. * explicitly set input, otherwise some boards
  899. * may not work at the state as we expected
  900. */
  901. route = &config->routes[0];
  902. ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
  903. route->input, route->output, 0);
  904. if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
  905. v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
  906. goto err_unreg_vdev;
  907. }
  908. bcap_dev->cur_input = 0;
  909. /* if this route has specific config, update ppi control */
  910. if (route->ppi_control)
  911. config->ppi_control = route->ppi_control;
  912. /* now we can probe the default state */
  913. if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
  914. v4l2_std_id std;
  915. ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std);
  916. if (ret) {
  917. v4l2_err(&bcap_dev->v4l2_dev,
  918. "Unable to get std\n");
  919. goto err_unreg_vdev;
  920. }
  921. bcap_dev->std = std;
  922. }
  923. if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
  924. struct v4l2_dv_timings dv_timings;
  925. ret = v4l2_subdev_call(bcap_dev->sd, video,
  926. g_dv_timings, &dv_timings);
  927. if (ret) {
  928. v4l2_err(&bcap_dev->v4l2_dev,
  929. "Unable to get dv timings\n");
  930. goto err_unreg_vdev;
  931. }
  932. bcap_dev->dv_timings = dv_timings;
  933. }
  934. ret = bcap_init_sensor_formats(bcap_dev);
  935. if (ret) {
  936. v4l2_err(&bcap_dev->v4l2_dev,
  937. "Unable to create sensor formats table\n");
  938. goto err_unreg_vdev;
  939. }
  940. return 0;
  941. err_unreg_vdev:
  942. video_unregister_device(bcap_dev->video_dev);
  943. bcap_dev->video_dev = NULL;
  944. err_free_handler:
  945. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  946. err_unreg_v4l2:
  947. v4l2_device_unregister(&bcap_dev->v4l2_dev);
  948. err_release_vdev:
  949. if (bcap_dev->video_dev)
  950. video_device_release(bcap_dev->video_dev);
  951. err_cleanup_ctx:
  952. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  953. err_free_ppi:
  954. ppi_delete_instance(bcap_dev->ppi);
  955. err_free_dev:
  956. kfree(bcap_dev);
  957. return ret;
  958. }
  959. static int bcap_remove(struct platform_device *pdev)
  960. {
  961. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  962. struct bcap_device *bcap_dev = container_of(v4l2_dev,
  963. struct bcap_device, v4l2_dev);
  964. bcap_free_sensor_formats(bcap_dev);
  965. video_unregister_device(bcap_dev->video_dev);
  966. v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
  967. v4l2_device_unregister(v4l2_dev);
  968. vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
  969. ppi_delete_instance(bcap_dev->ppi);
  970. kfree(bcap_dev);
  971. return 0;
  972. }
  973. static struct platform_driver bcap_driver = {
  974. .driver = {
  975. .name = CAPTURE_DRV_NAME,
  976. .owner = THIS_MODULE,
  977. },
  978. .probe = bcap_probe,
  979. .remove = bcap_remove,
  980. };
  981. module_platform_driver(bcap_driver);
  982. MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
  983. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  984. MODULE_LICENSE("GPL v2");