timblogiw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * timblogiw.c timberdale FPGA LogiWin Video In driver
  3. * Copyright (c) 2009-2010 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * Timberdale FPGA LogiWin Video In
  20. */
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/list.h>
  27. #include <linux/i2c.h>
  28. #include <linux/module.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/videobuf-dma-contig.h>
  32. #include <media/timb_video.h>
  33. #define DRIVER_NAME "timb-video"
  34. #define TIMBLOGIWIN_NAME "Timberdale Video-In"
  35. #define TIMBLOGIW_VERSION_CODE 0x04
  36. #define TIMBLOGIW_LINES_PER_DESC 44
  37. #define TIMBLOGIW_MAX_VIDEO_MEM 16
  38. #define TIMBLOGIW_HAS_DECODER(lw) (lw->pdata.encoder.module_name)
  39. struct timblogiw {
  40. struct video_device video_dev;
  41. struct v4l2_device v4l2_dev; /* mutual exclusion */
  42. struct mutex lock;
  43. struct device *dev;
  44. struct timb_video_platform_data pdata;
  45. struct v4l2_subdev *sd_enc; /* encoder */
  46. bool opened;
  47. };
  48. struct timblogiw_tvnorm {
  49. v4l2_std_id std;
  50. u16 width;
  51. u16 height;
  52. u8 fps;
  53. };
  54. struct timblogiw_fh {
  55. struct videobuf_queue vb_vidq;
  56. struct timblogiw_tvnorm const *cur_norm;
  57. struct list_head capture;
  58. struct dma_chan *chan;
  59. spinlock_t queue_lock; /* mutual exclusion */
  60. unsigned int frame_count;
  61. };
  62. struct timblogiw_buffer {
  63. /* common v4l buffer stuff -- must be first */
  64. struct videobuf_buffer vb;
  65. struct scatterlist sg[16];
  66. dma_cookie_t cookie;
  67. struct timblogiw_fh *fh;
  68. };
  69. const struct timblogiw_tvnorm timblogiw_tvnorms[] = {
  70. {
  71. .std = V4L2_STD_PAL,
  72. .width = 720,
  73. .height = 576,
  74. .fps = 25
  75. },
  76. {
  77. .std = V4L2_STD_NTSC,
  78. .width = 720,
  79. .height = 480,
  80. .fps = 30
  81. }
  82. };
  83. static int timblogiw_bytes_per_line(const struct timblogiw_tvnorm *norm)
  84. {
  85. return norm->width * 2;
  86. }
  87. static int timblogiw_frame_size(const struct timblogiw_tvnorm *norm)
  88. {
  89. return norm->height * timblogiw_bytes_per_line(norm);
  90. }
  91. static const struct timblogiw_tvnorm *timblogiw_get_norm(const v4l2_std_id std)
  92. {
  93. int i;
  94. for (i = 0; i < ARRAY_SIZE(timblogiw_tvnorms); i++)
  95. if (timblogiw_tvnorms[i].std & std)
  96. return timblogiw_tvnorms + i;
  97. /* default to first element */
  98. return timblogiw_tvnorms;
  99. }
  100. static void timblogiw_dma_cb(void *data)
  101. {
  102. struct timblogiw_buffer *buf = data;
  103. struct timblogiw_fh *fh = buf->fh;
  104. struct videobuf_buffer *vb = &buf->vb;
  105. spin_lock(&fh->queue_lock);
  106. /* mark the transfer done */
  107. buf->cookie = -1;
  108. fh->frame_count++;
  109. if (vb->state != VIDEOBUF_ERROR) {
  110. list_del(&vb->queue);
  111. do_gettimeofday(&vb->ts);
  112. vb->field_count = fh->frame_count * 2;
  113. vb->state = VIDEOBUF_DONE;
  114. wake_up(&vb->done);
  115. }
  116. if (!list_empty(&fh->capture)) {
  117. vb = list_entry(fh->capture.next, struct videobuf_buffer,
  118. queue);
  119. vb->state = VIDEOBUF_ACTIVE;
  120. }
  121. spin_unlock(&fh->queue_lock);
  122. }
  123. static bool timblogiw_dma_filter_fn(struct dma_chan *chan, void *filter_param)
  124. {
  125. return chan->chan_id == (uintptr_t)filter_param;
  126. }
  127. /* IOCTL functions */
  128. static int timblogiw_g_fmt(struct file *file, void *priv,
  129. struct v4l2_format *format)
  130. {
  131. struct video_device *vdev = video_devdata(file);
  132. struct timblogiw *lw = video_get_drvdata(vdev);
  133. struct timblogiw_fh *fh = priv;
  134. dev_dbg(&vdev->dev, "%s entry\n", __func__);
  135. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  136. return -EINVAL;
  137. mutex_lock(&lw->lock);
  138. format->fmt.pix.width = fh->cur_norm->width;
  139. format->fmt.pix.height = fh->cur_norm->height;
  140. format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  141. format->fmt.pix.bytesperline = timblogiw_bytes_per_line(fh->cur_norm);
  142. format->fmt.pix.sizeimage = timblogiw_frame_size(fh->cur_norm);
  143. format->fmt.pix.field = V4L2_FIELD_NONE;
  144. mutex_unlock(&lw->lock);
  145. return 0;
  146. }
  147. static int timblogiw_try_fmt(struct file *file, void *priv,
  148. struct v4l2_format *format)
  149. {
  150. struct video_device *vdev = video_devdata(file);
  151. struct v4l2_pix_format *pix = &format->fmt.pix;
  152. dev_dbg(&vdev->dev,
  153. "%s - width=%d, height=%d, pixelformat=%d, field=%d\n"
  154. "bytes per line %d, size image: %d, colorspace: %d\n",
  155. __func__,
  156. pix->width, pix->height, pix->pixelformat, pix->field,
  157. pix->bytesperline, pix->sizeimage, pix->colorspace);
  158. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  159. return -EINVAL;
  160. if (pix->field != V4L2_FIELD_NONE)
  161. return -EINVAL;
  162. if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
  163. return -EINVAL;
  164. return 0;
  165. }
  166. static int timblogiw_s_fmt(struct file *file, void *priv,
  167. struct v4l2_format *format)
  168. {
  169. struct video_device *vdev = video_devdata(file);
  170. struct timblogiw *lw = video_get_drvdata(vdev);
  171. struct timblogiw_fh *fh = priv;
  172. struct v4l2_pix_format *pix = &format->fmt.pix;
  173. int err;
  174. mutex_lock(&lw->lock);
  175. err = timblogiw_try_fmt(file, priv, format);
  176. if (err)
  177. goto out;
  178. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  179. dev_err(&vdev->dev, "%s queue busy\n", __func__);
  180. err = -EBUSY;
  181. goto out;
  182. }
  183. pix->width = fh->cur_norm->width;
  184. pix->height = fh->cur_norm->height;
  185. out:
  186. mutex_unlock(&lw->lock);
  187. return err;
  188. }
  189. static int timblogiw_querycap(struct file *file, void *priv,
  190. struct v4l2_capability *cap)
  191. {
  192. struct video_device *vdev = video_devdata(file);
  193. dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
  194. memset(cap, 0, sizeof(*cap));
  195. strncpy(cap->card, TIMBLOGIWIN_NAME, sizeof(cap->card)-1);
  196. strncpy(cap->driver, DRIVER_NAME, sizeof(cap->driver) - 1);
  197. strlcpy(cap->bus_info, vdev->name, sizeof(cap->bus_info));
  198. cap->version = TIMBLOGIW_VERSION_CODE;
  199. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  200. V4L2_CAP_READWRITE;
  201. return 0;
  202. }
  203. static int timblogiw_enum_fmt(struct file *file, void *priv,
  204. struct v4l2_fmtdesc *fmt)
  205. {
  206. struct video_device *vdev = video_devdata(file);
  207. dev_dbg(&vdev->dev, "%s, index: %d\n", __func__, fmt->index);
  208. if (fmt->index != 0)
  209. return -EINVAL;
  210. memset(fmt, 0, sizeof(*fmt));
  211. fmt->index = 0;
  212. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  213. strncpy(fmt->description, "4:2:2, packed, YUYV",
  214. sizeof(fmt->description)-1);
  215. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  216. return 0;
  217. }
  218. static int timblogiw_g_parm(struct file *file, void *priv,
  219. struct v4l2_streamparm *sp)
  220. {
  221. struct timblogiw_fh *fh = priv;
  222. struct v4l2_captureparm *cp = &sp->parm.capture;
  223. cp->capability = V4L2_CAP_TIMEPERFRAME;
  224. cp->timeperframe.numerator = 1;
  225. cp->timeperframe.denominator = fh->cur_norm->fps;
  226. return 0;
  227. }
  228. static int timblogiw_reqbufs(struct file *file, void *priv,
  229. struct v4l2_requestbuffers *rb)
  230. {
  231. struct video_device *vdev = video_devdata(file);
  232. struct timblogiw_fh *fh = priv;
  233. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  234. return videobuf_reqbufs(&fh->vb_vidq, rb);
  235. }
  236. static int timblogiw_querybuf(struct file *file, void *priv,
  237. struct v4l2_buffer *b)
  238. {
  239. struct video_device *vdev = video_devdata(file);
  240. struct timblogiw_fh *fh = priv;
  241. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  242. return videobuf_querybuf(&fh->vb_vidq, b);
  243. }
  244. static int timblogiw_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  245. {
  246. struct video_device *vdev = video_devdata(file);
  247. struct timblogiw_fh *fh = priv;
  248. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  249. return videobuf_qbuf(&fh->vb_vidq, b);
  250. }
  251. static int timblogiw_dqbuf(struct file *file, void *priv,
  252. struct v4l2_buffer *b)
  253. {
  254. struct video_device *vdev = video_devdata(file);
  255. struct timblogiw_fh *fh = priv;
  256. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  257. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  258. }
  259. static int timblogiw_g_std(struct file *file, void *priv, v4l2_std_id *std)
  260. {
  261. struct video_device *vdev = video_devdata(file);
  262. struct timblogiw_fh *fh = priv;
  263. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  264. *std = fh->cur_norm->std;
  265. return 0;
  266. }
  267. static int timblogiw_s_std(struct file *file, void *priv, v4l2_std_id *std)
  268. {
  269. struct video_device *vdev = video_devdata(file);
  270. struct timblogiw *lw = video_get_drvdata(vdev);
  271. struct timblogiw_fh *fh = priv;
  272. int err = 0;
  273. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  274. mutex_lock(&lw->lock);
  275. if (TIMBLOGIW_HAS_DECODER(lw))
  276. err = v4l2_subdev_call(lw->sd_enc, core, s_std, *std);
  277. if (!err)
  278. fh->cur_norm = timblogiw_get_norm(*std);
  279. mutex_unlock(&lw->lock);
  280. return err;
  281. }
  282. static int timblogiw_enuminput(struct file *file, void *priv,
  283. struct v4l2_input *inp)
  284. {
  285. struct video_device *vdev = video_devdata(file);
  286. int i;
  287. dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
  288. if (inp->index != 0)
  289. return -EINVAL;
  290. inp->index = 0;
  291. strncpy(inp->name, "Timb input 1", sizeof(inp->name) - 1);
  292. inp->type = V4L2_INPUT_TYPE_CAMERA;
  293. inp->std = 0;
  294. for (i = 0; i < ARRAY_SIZE(timblogiw_tvnorms); i++)
  295. inp->std |= timblogiw_tvnorms[i].std;
  296. return 0;
  297. }
  298. static int timblogiw_g_input(struct file *file, void *priv,
  299. unsigned int *input)
  300. {
  301. struct video_device *vdev = video_devdata(file);
  302. dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
  303. *input = 0;
  304. return 0;
  305. }
  306. static int timblogiw_s_input(struct file *file, void *priv, unsigned int input)
  307. {
  308. struct video_device *vdev = video_devdata(file);
  309. dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
  310. if (input != 0)
  311. return -EINVAL;
  312. return 0;
  313. }
  314. static int timblogiw_streamon(struct file *file, void *priv, unsigned int type)
  315. {
  316. struct video_device *vdev = video_devdata(file);
  317. struct timblogiw_fh *fh = priv;
  318. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  319. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  320. dev_dbg(&vdev->dev, "%s - No capture device\n", __func__);
  321. return -EINVAL;
  322. }
  323. fh->frame_count = 0;
  324. return videobuf_streamon(&fh->vb_vidq);
  325. }
  326. static int timblogiw_streamoff(struct file *file, void *priv,
  327. unsigned int type)
  328. {
  329. struct video_device *vdev = video_devdata(file);
  330. struct timblogiw_fh *fh = priv;
  331. dev_dbg(&vdev->dev, "%s entry\n", __func__);
  332. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  333. return -EINVAL;
  334. return videobuf_streamoff(&fh->vb_vidq);
  335. }
  336. static int timblogiw_querystd(struct file *file, void *priv, v4l2_std_id *std)
  337. {
  338. struct video_device *vdev = video_devdata(file);
  339. struct timblogiw *lw = video_get_drvdata(vdev);
  340. struct timblogiw_fh *fh = priv;
  341. dev_dbg(&vdev->dev, "%s entry\n", __func__);
  342. if (TIMBLOGIW_HAS_DECODER(lw))
  343. return v4l2_subdev_call(lw->sd_enc, video, querystd, std);
  344. else {
  345. *std = fh->cur_norm->std;
  346. return 0;
  347. }
  348. }
  349. static int timblogiw_enum_framesizes(struct file *file, void *priv,
  350. struct v4l2_frmsizeenum *fsize)
  351. {
  352. struct video_device *vdev = video_devdata(file);
  353. struct timblogiw_fh *fh = priv;
  354. dev_dbg(&vdev->dev, "%s - index: %d, format: %d\n", __func__,
  355. fsize->index, fsize->pixel_format);
  356. if ((fsize->index != 0) ||
  357. (fsize->pixel_format != V4L2_PIX_FMT_UYVY))
  358. return -EINVAL;
  359. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  360. fsize->discrete.width = fh->cur_norm->width;
  361. fsize->discrete.height = fh->cur_norm->height;
  362. return 0;
  363. }
  364. /* Video buffer functions */
  365. static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  366. unsigned int *size)
  367. {
  368. struct timblogiw_fh *fh = vq->priv_data;
  369. *size = timblogiw_frame_size(fh->cur_norm);
  370. if (!*count)
  371. *count = 32;
  372. while (*size * *count > TIMBLOGIW_MAX_VIDEO_MEM * 1024 * 1024)
  373. (*count)--;
  374. return 0;
  375. }
  376. static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  377. enum v4l2_field field)
  378. {
  379. struct timblogiw_fh *fh = vq->priv_data;
  380. struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
  381. vb);
  382. unsigned int data_size = timblogiw_frame_size(fh->cur_norm);
  383. int err = 0;
  384. if (vb->baddr && vb->bsize < data_size)
  385. /* User provided buffer, but it is too small */
  386. return -ENOMEM;
  387. vb->size = data_size;
  388. vb->width = fh->cur_norm->width;
  389. vb->height = fh->cur_norm->height;
  390. vb->field = field;
  391. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  392. int i;
  393. unsigned int size;
  394. unsigned int bytes_per_desc = TIMBLOGIW_LINES_PER_DESC *
  395. timblogiw_bytes_per_line(fh->cur_norm);
  396. dma_addr_t addr;
  397. sg_init_table(buf->sg, ARRAY_SIZE(buf->sg));
  398. err = videobuf_iolock(vq, vb, NULL);
  399. if (err)
  400. goto err;
  401. addr = videobuf_to_dma_contig(vb);
  402. for (i = 0, size = 0; size < data_size; i++) {
  403. sg_dma_address(buf->sg + i) = addr + size;
  404. size += bytes_per_desc;
  405. sg_dma_len(buf->sg + i) = (size > data_size) ?
  406. (bytes_per_desc - (size - data_size)) :
  407. bytes_per_desc;
  408. }
  409. vb->state = VIDEOBUF_PREPARED;
  410. buf->cookie = -1;
  411. buf->fh = fh;
  412. }
  413. return 0;
  414. err:
  415. videobuf_dma_contig_free(vq, vb);
  416. vb->state = VIDEOBUF_NEEDS_INIT;
  417. return err;
  418. }
  419. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  420. {
  421. struct timblogiw_fh *fh = vq->priv_data;
  422. struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
  423. vb);
  424. struct dma_async_tx_descriptor *desc;
  425. int sg_elems;
  426. int bytes_per_desc = TIMBLOGIW_LINES_PER_DESC *
  427. timblogiw_bytes_per_line(fh->cur_norm);
  428. sg_elems = timblogiw_frame_size(fh->cur_norm) / bytes_per_desc;
  429. sg_elems +=
  430. (timblogiw_frame_size(fh->cur_norm) % bytes_per_desc) ? 1 : 0;
  431. if (list_empty(&fh->capture))
  432. vb->state = VIDEOBUF_ACTIVE;
  433. else
  434. vb->state = VIDEOBUF_QUEUED;
  435. list_add_tail(&vb->queue, &fh->capture);
  436. spin_unlock_irq(&fh->queue_lock);
  437. desc = dmaengine_prep_slave_sg(fh->chan,
  438. buf->sg, sg_elems, DMA_DEV_TO_MEM,
  439. DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP);
  440. if (!desc) {
  441. spin_lock_irq(&fh->queue_lock);
  442. list_del_init(&vb->queue);
  443. vb->state = VIDEOBUF_PREPARED;
  444. return;
  445. }
  446. desc->callback_param = buf;
  447. desc->callback = timblogiw_dma_cb;
  448. buf->cookie = desc->tx_submit(desc);
  449. spin_lock_irq(&fh->queue_lock);
  450. }
  451. static void buffer_release(struct videobuf_queue *vq,
  452. struct videobuf_buffer *vb)
  453. {
  454. struct timblogiw_fh *fh = vq->priv_data;
  455. struct timblogiw_buffer *buf = container_of(vb, struct timblogiw_buffer,
  456. vb);
  457. videobuf_waiton(vq, vb, 0, 0);
  458. if (buf->cookie >= 0)
  459. dma_sync_wait(fh->chan, buf->cookie);
  460. videobuf_dma_contig_free(vq, vb);
  461. vb->state = VIDEOBUF_NEEDS_INIT;
  462. }
  463. static struct videobuf_queue_ops timblogiw_video_qops = {
  464. .buf_setup = buffer_setup,
  465. .buf_prepare = buffer_prepare,
  466. .buf_queue = buffer_queue,
  467. .buf_release = buffer_release,
  468. };
  469. /* Device Operations functions */
  470. static int timblogiw_open(struct file *file)
  471. {
  472. struct video_device *vdev = video_devdata(file);
  473. struct timblogiw *lw = video_get_drvdata(vdev);
  474. struct timblogiw_fh *fh;
  475. v4l2_std_id std;
  476. dma_cap_mask_t mask;
  477. int err = 0;
  478. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  479. mutex_lock(&lw->lock);
  480. if (lw->opened) {
  481. err = -EBUSY;
  482. goto out;
  483. }
  484. if (TIMBLOGIW_HAS_DECODER(lw) && !lw->sd_enc) {
  485. struct i2c_adapter *adapt;
  486. /* find the video decoder */
  487. adapt = i2c_get_adapter(lw->pdata.i2c_adapter);
  488. if (!adapt) {
  489. dev_err(&vdev->dev, "No I2C bus #%d\n",
  490. lw->pdata.i2c_adapter);
  491. err = -ENODEV;
  492. goto out;
  493. }
  494. /* now find the encoder */
  495. lw->sd_enc = v4l2_i2c_new_subdev_board(&lw->v4l2_dev, adapt,
  496. lw->pdata.encoder.info, NULL);
  497. i2c_put_adapter(adapt);
  498. if (!lw->sd_enc) {
  499. dev_err(&vdev->dev, "Failed to get encoder: %s\n",
  500. lw->pdata.encoder.module_name);
  501. err = -ENODEV;
  502. goto out;
  503. }
  504. }
  505. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  506. if (!fh) {
  507. err = -ENOMEM;
  508. goto out;
  509. }
  510. fh->cur_norm = timblogiw_tvnorms;
  511. timblogiw_querystd(file, fh, &std);
  512. fh->cur_norm = timblogiw_get_norm(std);
  513. INIT_LIST_HEAD(&fh->capture);
  514. spin_lock_init(&fh->queue_lock);
  515. dma_cap_zero(mask);
  516. dma_cap_set(DMA_SLAVE, mask);
  517. dma_cap_set(DMA_PRIVATE, mask);
  518. /* find the DMA channel */
  519. fh->chan = dma_request_channel(mask, timblogiw_dma_filter_fn,
  520. (void *)(uintptr_t)lw->pdata.dma_channel);
  521. if (!fh->chan) {
  522. dev_err(&vdev->dev, "Failed to get DMA channel\n");
  523. kfree(fh);
  524. err = -ENODEV;
  525. goto out;
  526. }
  527. file->private_data = fh;
  528. videobuf_queue_dma_contig_init(&fh->vb_vidq,
  529. &timblogiw_video_qops, lw->dev, &fh->queue_lock,
  530. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  531. sizeof(struct timblogiw_buffer), fh, NULL);
  532. lw->opened = true;
  533. out:
  534. mutex_unlock(&lw->lock);
  535. return err;
  536. }
  537. static int timblogiw_close(struct file *file)
  538. {
  539. struct video_device *vdev = video_devdata(file);
  540. struct timblogiw *lw = video_get_drvdata(vdev);
  541. struct timblogiw_fh *fh = file->private_data;
  542. dev_dbg(&vdev->dev, "%s: Entry\n", __func__);
  543. videobuf_stop(&fh->vb_vidq);
  544. videobuf_mmap_free(&fh->vb_vidq);
  545. dma_release_channel(fh->chan);
  546. kfree(fh);
  547. mutex_lock(&lw->lock);
  548. lw->opened = false;
  549. mutex_unlock(&lw->lock);
  550. return 0;
  551. }
  552. static ssize_t timblogiw_read(struct file *file, char __user *data,
  553. size_t count, loff_t *ppos)
  554. {
  555. struct video_device *vdev = video_devdata(file);
  556. struct timblogiw_fh *fh = file->private_data;
  557. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  558. return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
  559. file->f_flags & O_NONBLOCK);
  560. }
  561. static unsigned int timblogiw_poll(struct file *file,
  562. struct poll_table_struct *wait)
  563. {
  564. struct video_device *vdev = video_devdata(file);
  565. struct timblogiw_fh *fh = file->private_data;
  566. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  567. return videobuf_poll_stream(file, &fh->vb_vidq, wait);
  568. }
  569. static int timblogiw_mmap(struct file *file, struct vm_area_struct *vma)
  570. {
  571. struct video_device *vdev = video_devdata(file);
  572. struct timblogiw_fh *fh = file->private_data;
  573. dev_dbg(&vdev->dev, "%s: entry\n", __func__);
  574. return videobuf_mmap_mapper(&fh->vb_vidq, vma);
  575. }
  576. /* Platform device functions */
  577. static __devinitconst struct v4l2_ioctl_ops timblogiw_ioctl_ops = {
  578. .vidioc_querycap = timblogiw_querycap,
  579. .vidioc_enum_fmt_vid_cap = timblogiw_enum_fmt,
  580. .vidioc_g_fmt_vid_cap = timblogiw_g_fmt,
  581. .vidioc_try_fmt_vid_cap = timblogiw_try_fmt,
  582. .vidioc_s_fmt_vid_cap = timblogiw_s_fmt,
  583. .vidioc_g_parm = timblogiw_g_parm,
  584. .vidioc_reqbufs = timblogiw_reqbufs,
  585. .vidioc_querybuf = timblogiw_querybuf,
  586. .vidioc_qbuf = timblogiw_qbuf,
  587. .vidioc_dqbuf = timblogiw_dqbuf,
  588. .vidioc_g_std = timblogiw_g_std,
  589. .vidioc_s_std = timblogiw_s_std,
  590. .vidioc_enum_input = timblogiw_enuminput,
  591. .vidioc_g_input = timblogiw_g_input,
  592. .vidioc_s_input = timblogiw_s_input,
  593. .vidioc_streamon = timblogiw_streamon,
  594. .vidioc_streamoff = timblogiw_streamoff,
  595. .vidioc_querystd = timblogiw_querystd,
  596. .vidioc_enum_framesizes = timblogiw_enum_framesizes,
  597. };
  598. static __devinitconst struct v4l2_file_operations timblogiw_fops = {
  599. .owner = THIS_MODULE,
  600. .open = timblogiw_open,
  601. .release = timblogiw_close,
  602. .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  603. .mmap = timblogiw_mmap,
  604. .read = timblogiw_read,
  605. .poll = timblogiw_poll,
  606. };
  607. static __devinitconst struct video_device timblogiw_template = {
  608. .name = TIMBLOGIWIN_NAME,
  609. .fops = &timblogiw_fops,
  610. .ioctl_ops = &timblogiw_ioctl_ops,
  611. .release = video_device_release_empty,
  612. .minor = -1,
  613. .tvnorms = V4L2_STD_PAL | V4L2_STD_NTSC
  614. };
  615. static int __devinit timblogiw_probe(struct platform_device *pdev)
  616. {
  617. int err;
  618. struct timblogiw *lw = NULL;
  619. struct timb_video_platform_data *pdata = pdev->dev.platform_data;
  620. if (!pdata) {
  621. dev_err(&pdev->dev, "No platform data\n");
  622. err = -EINVAL;
  623. goto err;
  624. }
  625. if (!pdata->encoder.module_name)
  626. dev_info(&pdev->dev, "Running without decoder\n");
  627. lw = kzalloc(sizeof(*lw), GFP_KERNEL);
  628. if (!lw) {
  629. err = -ENOMEM;
  630. goto err;
  631. }
  632. if (pdev->dev.parent)
  633. lw->dev = pdev->dev.parent;
  634. else
  635. lw->dev = &pdev->dev;
  636. memcpy(&lw->pdata, pdata, sizeof(lw->pdata));
  637. mutex_init(&lw->lock);
  638. lw->video_dev = timblogiw_template;
  639. strlcpy(lw->v4l2_dev.name, DRIVER_NAME, sizeof(lw->v4l2_dev.name));
  640. err = v4l2_device_register(NULL, &lw->v4l2_dev);
  641. if (err)
  642. goto err_register;
  643. lw->video_dev.v4l2_dev = &lw->v4l2_dev;
  644. platform_set_drvdata(pdev, lw);
  645. video_set_drvdata(&lw->video_dev, lw);
  646. err = video_register_device(&lw->video_dev, VFL_TYPE_GRABBER, 0);
  647. if (err) {
  648. dev_err(&pdev->dev, "Error reg video: %d\n", err);
  649. goto err_request;
  650. }
  651. return 0;
  652. err_request:
  653. platform_set_drvdata(pdev, NULL);
  654. v4l2_device_unregister(&lw->v4l2_dev);
  655. err_register:
  656. kfree(lw);
  657. err:
  658. dev_err(&pdev->dev, "Failed to register: %d\n", err);
  659. return err;
  660. }
  661. static int __devexit timblogiw_remove(struct platform_device *pdev)
  662. {
  663. struct timblogiw *lw = platform_get_drvdata(pdev);
  664. video_unregister_device(&lw->video_dev);
  665. v4l2_device_unregister(&lw->v4l2_dev);
  666. kfree(lw);
  667. platform_set_drvdata(pdev, NULL);
  668. return 0;
  669. }
  670. static struct platform_driver timblogiw_platform_driver = {
  671. .driver = {
  672. .name = DRIVER_NAME,
  673. .owner = THIS_MODULE,
  674. },
  675. .probe = timblogiw_probe,
  676. .remove = __devexit_p(timblogiw_remove),
  677. };
  678. module_platform_driver(timblogiw_platform_driver);
  679. MODULE_DESCRIPTION(TIMBLOGIWIN_NAME);
  680. MODULE_AUTHOR("Pelagicore AB <info@pelagicore.com>");
  681. MODULE_LICENSE("GPL v2");
  682. MODULE_ALIAS("platform:"DRIVER_NAME);