timblogiw.c 21 KB

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