timblogiw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. static 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. v4l2_get_timestamp(&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. strncpy(cap->card, TIMBLOGIWIN_NAME, sizeof(cap->card)-1);
  195. strncpy(cap->driver, DRIVER_NAME, sizeof(cap->driver) - 1);
  196. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", vdev->name);
  197. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  198. V4L2_CAP_READWRITE;
  199. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  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, video, 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, enum v4l2_buf_type 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. enum v4l2_buf_type 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 = dmaengine_prep_slave_sg(fh->chan,
  437. buf->sg, sg_elems, DMA_DEV_TO_MEM,
  438. DMA_PREP_INTERRUPT);
  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 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 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 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 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 = devm_kzalloc(&pdev->dev, 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;
  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. v4l2_device_unregister(&lw->v4l2_dev);
  653. err:
  654. dev_err(&pdev->dev, "Failed to register: %d\n", err);
  655. return err;
  656. }
  657. static int timblogiw_remove(struct platform_device *pdev)
  658. {
  659. struct timblogiw *lw = platform_get_drvdata(pdev);
  660. video_unregister_device(&lw->video_dev);
  661. v4l2_device_unregister(&lw->v4l2_dev);
  662. return 0;
  663. }
  664. static struct platform_driver timblogiw_platform_driver = {
  665. .driver = {
  666. .name = DRIVER_NAME,
  667. },
  668. .probe = timblogiw_probe,
  669. .remove = timblogiw_remove,
  670. };
  671. module_platform_driver(timblogiw_platform_driver);
  672. MODULE_DESCRIPTION(TIMBLOGIWIN_NAME);
  673. MODULE_AUTHOR("Pelagicore AB <info@pelagicore.com>");
  674. MODULE_LICENSE("GPL v2");
  675. MODULE_ALIAS("platform:"DRIVER_NAME);