fimc-capture.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Samsung S5P SoC series camera interface (camera capture) driver
  3. *
  4. * Copyright (c) 2010 Samsung Electronics Co., Ltd
  5. * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/version.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/bug.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/list.h>
  21. #include <linux/slab.h>
  22. #include <linux/clk.h>
  23. #include <linux/i2c.h>
  24. #include <linux/videodev2.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-mem2mem.h>
  28. #include <media/videobuf-core.h>
  29. #include <media/videobuf-dma-contig.h>
  30. #include "fimc-core.h"
  31. static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc,
  32. struct s3c_fimc_isp_info *isp_info)
  33. {
  34. struct i2c_adapter *i2c_adap;
  35. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  36. struct v4l2_subdev *sd = NULL;
  37. i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num);
  38. if (!i2c_adap)
  39. return ERR_PTR(-ENOMEM);
  40. sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap,
  41. isp_info->board_info, NULL);
  42. if (!sd) {
  43. v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n");
  44. return NULL;
  45. }
  46. v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n",
  47. isp_info->board_info->type);
  48. return sd;
  49. }
  50. static void fimc_subdev_unregister(struct fimc_dev *fimc)
  51. {
  52. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  53. struct i2c_client *client;
  54. if (vid_cap->input_index < 0)
  55. return; /* Subdevice already released or not registered. */
  56. if (vid_cap->sd) {
  57. v4l2_device_unregister_subdev(vid_cap->sd);
  58. client = v4l2_get_subdevdata(vid_cap->sd);
  59. i2c_unregister_device(client);
  60. i2c_put_adapter(client->adapter);
  61. vid_cap->sd = NULL;
  62. }
  63. vid_cap->input_index = -1;
  64. }
  65. /**
  66. * fimc_subdev_attach - attach v4l2_subdev to camera host interface
  67. *
  68. * @fimc: FIMC device information
  69. * @index: index to the array of available subdevices,
  70. * -1 for full array search or non negative value
  71. * to select specific subdevice
  72. */
  73. static int fimc_subdev_attach(struct fimc_dev *fimc, int index)
  74. {
  75. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  76. struct s3c_platform_fimc *pdata = fimc->pdata;
  77. struct s3c_fimc_isp_info *isp_info;
  78. struct v4l2_subdev *sd;
  79. int i;
  80. for (i = 0; i < FIMC_MAX_CAMIF_CLIENTS; ++i) {
  81. isp_info = pdata->isp_info[i];
  82. if (!isp_info || (index >= 0 && i != index))
  83. continue;
  84. sd = fimc_subdev_register(fimc, isp_info);
  85. if (sd) {
  86. vid_cap->sd = sd;
  87. vid_cap->input_index = i;
  88. return 0;
  89. }
  90. }
  91. vid_cap->input_index = -1;
  92. vid_cap->sd = NULL;
  93. v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n",
  94. fimc->id);
  95. return -ENODEV;
  96. }
  97. static int fimc_isp_subdev_init(struct fimc_dev *fimc, int index)
  98. {
  99. struct s3c_fimc_isp_info *isp_info;
  100. int ret;
  101. ret = fimc_subdev_attach(fimc, index);
  102. if (ret)
  103. return ret;
  104. isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
  105. ret = fimc_hw_set_camera_polarity(fimc, isp_info);
  106. if (!ret) {
  107. ret = v4l2_subdev_call(fimc->vid_cap.sd, core,
  108. s_power, 1);
  109. if (!ret)
  110. return ret;
  111. }
  112. fimc_subdev_unregister(fimc);
  113. err("ISP initialization failed: %d", ret);
  114. return ret;
  115. }
  116. /*
  117. * At least one buffer on the pending_buf_q queue is required.
  118. * Locking: The caller holds fimc->slock spinlock.
  119. */
  120. int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
  121. struct fimc_vid_buffer *fimc_vb)
  122. {
  123. struct fimc_vid_cap *cap = &fimc->vid_cap;
  124. struct fimc_ctx *ctx = cap->ctx;
  125. int ret = 0;
  126. BUG_ON(!fimc || !fimc_vb);
  127. ret = fimc_prepare_addr(ctx, fimc_vb, &ctx->d_frame,
  128. &fimc_vb->paddr);
  129. if (ret)
  130. return ret;
  131. if (test_bit(ST_CAPT_STREAM, &fimc->state)) {
  132. fimc_pending_queue_add(cap, fimc_vb);
  133. } else {
  134. /* Setup the buffer directly for processing. */
  135. int buf_id = (cap->reqbufs_count == 1) ? -1 : cap->buf_index;
  136. fimc_hw_set_output_addr(fimc, &fimc_vb->paddr, buf_id);
  137. fimc_vb->index = cap->buf_index;
  138. active_queue_add(cap, fimc_vb);
  139. if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
  140. cap->buf_index = 0;
  141. }
  142. return ret;
  143. }
  144. static int fimc_stop_capture(struct fimc_dev *fimc)
  145. {
  146. unsigned long flags;
  147. struct fimc_vid_cap *cap;
  148. int ret;
  149. cap = &fimc->vid_cap;
  150. if (!fimc_capture_active(fimc))
  151. return 0;
  152. spin_lock_irqsave(&fimc->slock, flags);
  153. set_bit(ST_CAPT_SHUT, &fimc->state);
  154. fimc_deactivate_capture(fimc);
  155. spin_unlock_irqrestore(&fimc->slock, flags);
  156. wait_event_timeout(fimc->irq_queue,
  157. test_bit(ST_CAPT_SHUT, &fimc->state),
  158. FIMC_SHUTDOWN_TIMEOUT);
  159. ret = v4l2_subdev_call(cap->sd, video, s_stream, 0);
  160. if (ret)
  161. v4l2_err(&fimc->vid_cap.v4l2_dev, "s_stream(0) failed\n");
  162. spin_lock_irqsave(&fimc->slock, flags);
  163. fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
  164. 1 << ST_CAPT_STREAM);
  165. fimc->vid_cap.active_buf_cnt = 0;
  166. spin_unlock_irqrestore(&fimc->slock, flags);
  167. dbg("state: 0x%lx", fimc->state);
  168. return 0;
  169. }
  170. static int fimc_capture_open(struct file *file)
  171. {
  172. struct fimc_dev *fimc = video_drvdata(file);
  173. int ret = 0;
  174. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  175. /* Return if the corresponding video mem2mem node is already opened. */
  176. if (fimc_m2m_active(fimc))
  177. return -EBUSY;
  178. if (mutex_lock_interruptible(&fimc->lock))
  179. return -ERESTARTSYS;
  180. if (++fimc->vid_cap.refcnt == 1) {
  181. ret = fimc_isp_subdev_init(fimc, -1);
  182. if (ret) {
  183. fimc->vid_cap.refcnt--;
  184. ret = -EIO;
  185. }
  186. }
  187. file->private_data = fimc->vid_cap.ctx;
  188. mutex_unlock(&fimc->lock);
  189. return ret;
  190. }
  191. static int fimc_capture_close(struct file *file)
  192. {
  193. struct fimc_dev *fimc = video_drvdata(file);
  194. if (mutex_lock_interruptible(&fimc->lock))
  195. return -ERESTARTSYS;
  196. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  197. if (--fimc->vid_cap.refcnt == 0) {
  198. fimc_stop_capture(fimc);
  199. videobuf_stop(&fimc->vid_cap.vbq);
  200. videobuf_mmap_free(&fimc->vid_cap.vbq);
  201. v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
  202. v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
  203. fimc_subdev_unregister(fimc);
  204. }
  205. mutex_unlock(&fimc->lock);
  206. return 0;
  207. }
  208. static unsigned int fimc_capture_poll(struct file *file,
  209. struct poll_table_struct *wait)
  210. {
  211. struct fimc_ctx *ctx = file->private_data;
  212. struct fimc_dev *fimc = ctx->fimc_dev;
  213. struct fimc_vid_cap *cap = &fimc->vid_cap;
  214. int ret;
  215. if (mutex_lock_interruptible(&fimc->lock))
  216. return POLLERR;
  217. ret = videobuf_poll_stream(file, &cap->vbq, wait);
  218. mutex_unlock(&fimc->lock);
  219. return ret;
  220. }
  221. static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
  222. {
  223. struct fimc_ctx *ctx = file->private_data;
  224. struct fimc_dev *fimc = ctx->fimc_dev;
  225. struct fimc_vid_cap *cap = &fimc->vid_cap;
  226. int ret;
  227. if (mutex_lock_interruptible(&fimc->lock))
  228. return -ERESTARTSYS;
  229. ret = videobuf_mmap_mapper(&cap->vbq, vma);
  230. mutex_unlock(&fimc->lock);
  231. return ret;
  232. }
  233. /* video device file operations */
  234. static const struct v4l2_file_operations fimc_capture_fops = {
  235. .owner = THIS_MODULE,
  236. .open = fimc_capture_open,
  237. .release = fimc_capture_close,
  238. .poll = fimc_capture_poll,
  239. .unlocked_ioctl = video_ioctl2,
  240. .mmap = fimc_capture_mmap,
  241. };
  242. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  243. struct v4l2_capability *cap)
  244. {
  245. struct fimc_ctx *ctx = file->private_data;
  246. struct fimc_dev *fimc = ctx->fimc_dev;
  247. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  248. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  249. cap->bus_info[0] = 0;
  250. cap->version = KERNEL_VERSION(1, 0, 0);
  251. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
  252. return 0;
  253. }
  254. /* Synchronize formats of the camera interface input and attached sensor. */
  255. static int sync_capture_fmt(struct fimc_ctx *ctx)
  256. {
  257. struct fimc_frame *frame = &ctx->s_frame;
  258. struct fimc_dev *fimc = ctx->fimc_dev;
  259. struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
  260. int ret;
  261. fmt->width = ctx->d_frame.o_width;
  262. fmt->height = ctx->d_frame.o_height;
  263. ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
  264. if (ret == -ENOIOCTLCMD) {
  265. err("s_mbus_fmt failed");
  266. return ret;
  267. }
  268. dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
  269. frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
  270. if (!frame->fmt) {
  271. err("fimc source format not found\n");
  272. return -EINVAL;
  273. }
  274. frame->f_width = fmt->width;
  275. frame->f_height = fmt->height;
  276. frame->width = fmt->width;
  277. frame->height = fmt->height;
  278. frame->o_width = fmt->width;
  279. frame->o_height = fmt->height;
  280. frame->offs_h = 0;
  281. frame->offs_v = 0;
  282. return 0;
  283. }
  284. static int fimc_cap_s_fmt(struct file *file, void *priv,
  285. struct v4l2_format *f)
  286. {
  287. struct fimc_ctx *ctx = priv;
  288. struct fimc_dev *fimc = ctx->fimc_dev;
  289. struct fimc_frame *frame;
  290. struct v4l2_pix_format *pix;
  291. int ret;
  292. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  293. return -EINVAL;
  294. ret = fimc_vidioc_try_fmt(file, priv, f);
  295. if (ret)
  296. return ret;
  297. if (mutex_lock_interruptible(&fimc->lock))
  298. return -ERESTARTSYS;
  299. if (fimc_capture_active(fimc)) {
  300. ret = -EBUSY;
  301. goto sf_unlock;
  302. }
  303. frame = &ctx->d_frame;
  304. pix = &f->fmt.pix;
  305. frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
  306. if (!frame->fmt) {
  307. err("fimc target format not found\n");
  308. ret = -EINVAL;
  309. goto sf_unlock;
  310. }
  311. /* Output DMA frame pixel size and offsets. */
  312. frame->f_width = pix->bytesperline * 8 / frame->fmt->depth;
  313. frame->f_height = pix->height;
  314. frame->width = pix->width;
  315. frame->height = pix->height;
  316. frame->o_width = pix->width;
  317. frame->o_height = pix->height;
  318. frame->size = (pix->width * pix->height * frame->fmt->depth) >> 3;
  319. frame->offs_h = 0;
  320. frame->offs_v = 0;
  321. ret = sync_capture_fmt(ctx);
  322. ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
  323. sf_unlock:
  324. mutex_unlock(&fimc->lock);
  325. return ret;
  326. }
  327. static int fimc_cap_enum_input(struct file *file, void *priv,
  328. struct v4l2_input *i)
  329. {
  330. struct fimc_ctx *ctx = priv;
  331. struct s3c_platform_fimc *pldata = ctx->fimc_dev->pdata;
  332. struct s3c_fimc_isp_info *isp_info;
  333. if (i->index >= FIMC_MAX_CAMIF_CLIENTS)
  334. return -EINVAL;
  335. isp_info = pldata->isp_info[i->index];
  336. if (isp_info == NULL)
  337. return -EINVAL;
  338. i->type = V4L2_INPUT_TYPE_CAMERA;
  339. strncpy(i->name, isp_info->board_info->type, 32);
  340. return 0;
  341. }
  342. static int fimc_cap_s_input(struct file *file, void *priv,
  343. unsigned int i)
  344. {
  345. struct fimc_ctx *ctx = priv;
  346. struct fimc_dev *fimc = ctx->fimc_dev;
  347. struct s3c_platform_fimc *pdata = fimc->pdata;
  348. int ret;
  349. if (fimc_capture_active(ctx->fimc_dev))
  350. return -EBUSY;
  351. if (mutex_lock_interruptible(&fimc->lock))
  352. return -ERESTARTSYS;
  353. if (i >= FIMC_MAX_CAMIF_CLIENTS || !pdata->isp_info[i]) {
  354. ret = -EINVAL;
  355. goto si_unlock;
  356. }
  357. if (fimc->vid_cap.sd) {
  358. ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
  359. if (ret)
  360. err("s_power failed: %d", ret);
  361. }
  362. /* Release the attached sensor subdevice. */
  363. fimc_subdev_unregister(fimc);
  364. ret = fimc_isp_subdev_init(fimc, i);
  365. si_unlock:
  366. mutex_unlock(&fimc->lock);
  367. return ret;
  368. }
  369. static int fimc_cap_g_input(struct file *file, void *priv,
  370. unsigned int *i)
  371. {
  372. struct fimc_ctx *ctx = priv;
  373. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  374. *i = cap->input_index;
  375. return 0;
  376. }
  377. static int fimc_cap_streamon(struct file *file, void *priv,
  378. enum v4l2_buf_type type)
  379. {
  380. struct s3c_fimc_isp_info *isp_info;
  381. struct fimc_ctx *ctx = priv;
  382. struct fimc_dev *fimc = ctx->fimc_dev;
  383. int ret = -EBUSY;
  384. if (mutex_lock_interruptible(&fimc->lock))
  385. return -ERESTARTSYS;
  386. if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
  387. goto s_unlock;
  388. if (!(ctx->state & FIMC_DST_FMT)) {
  389. v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
  390. ret = -EINVAL;
  391. goto s_unlock;
  392. }
  393. ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
  394. if (ret && ret != -ENOIOCTLCMD)
  395. goto s_unlock;
  396. ret = fimc_prepare_config(ctx, ctx->state);
  397. if (ret)
  398. goto s_unlock;
  399. isp_info = fimc->pdata->isp_info[fimc->vid_cap.input_index];
  400. fimc_hw_set_camera_type(fimc, isp_info);
  401. fimc_hw_set_camera_source(fimc, isp_info);
  402. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  403. if (ctx->state & FIMC_PARAMS) {
  404. ret = fimc_set_scaler_info(ctx);
  405. if (ret) {
  406. err("Scaler setup error");
  407. goto s_unlock;
  408. }
  409. fimc_hw_set_input_path(ctx);
  410. fimc_hw_set_scaler(ctx);
  411. fimc_hw_set_target_format(ctx);
  412. fimc_hw_set_rotation(ctx);
  413. fimc_hw_set_effect(ctx);
  414. }
  415. fimc_hw_set_output_path(ctx);
  416. fimc_hw_set_out_dma(ctx);
  417. INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
  418. INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
  419. fimc->vid_cap.active_buf_cnt = 0;
  420. fimc->vid_cap.frame_count = 0;
  421. fimc->vid_cap.buf_index = fimc_hw_get_frame_index(fimc);
  422. set_bit(ST_CAPT_PEND, &fimc->state);
  423. ret = videobuf_streamon(&fimc->vid_cap.vbq);
  424. s_unlock:
  425. mutex_unlock(&fimc->lock);
  426. return ret;
  427. }
  428. static int fimc_cap_streamoff(struct file *file, void *priv,
  429. enum v4l2_buf_type type)
  430. {
  431. struct fimc_ctx *ctx = priv;
  432. struct fimc_dev *fimc = ctx->fimc_dev;
  433. struct fimc_vid_cap *cap = &fimc->vid_cap;
  434. unsigned long flags;
  435. int ret;
  436. spin_lock_irqsave(&fimc->slock, flags);
  437. if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
  438. spin_unlock_irqrestore(&fimc->slock, flags);
  439. dbg("state: 0x%lx", fimc->state);
  440. return -EINVAL;
  441. }
  442. spin_unlock_irqrestore(&fimc->slock, flags);
  443. if (mutex_lock_interruptible(&fimc->lock))
  444. return -ERESTARTSYS;
  445. fimc_stop_capture(fimc);
  446. ret = videobuf_streamoff(&cap->vbq);
  447. mutex_unlock(&fimc->lock);
  448. return ret;
  449. }
  450. static int fimc_cap_reqbufs(struct file *file, void *priv,
  451. struct v4l2_requestbuffers *reqbufs)
  452. {
  453. struct fimc_ctx *ctx = priv;
  454. struct fimc_dev *fimc = ctx->fimc_dev;
  455. struct fimc_vid_cap *cap = &fimc->vid_cap;
  456. int ret;
  457. if (fimc_capture_active(ctx->fimc_dev))
  458. return -EBUSY;
  459. if (mutex_lock_interruptible(&fimc->lock))
  460. return -ERESTARTSYS;
  461. ret = videobuf_reqbufs(&cap->vbq, reqbufs);
  462. if (!ret)
  463. cap->reqbufs_count = reqbufs->count;
  464. mutex_unlock(&fimc->lock);
  465. return ret;
  466. }
  467. static int fimc_cap_querybuf(struct file *file, void *priv,
  468. struct v4l2_buffer *buf)
  469. {
  470. struct fimc_ctx *ctx = priv;
  471. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  472. if (fimc_capture_active(ctx->fimc_dev))
  473. return -EBUSY;
  474. return videobuf_querybuf(&cap->vbq, buf);
  475. }
  476. static int fimc_cap_qbuf(struct file *file, void *priv,
  477. struct v4l2_buffer *buf)
  478. {
  479. struct fimc_ctx *ctx = priv;
  480. struct fimc_dev *fimc = ctx->fimc_dev;
  481. struct fimc_vid_cap *cap = &fimc->vid_cap;
  482. int ret;
  483. if (mutex_lock_interruptible(&fimc->lock))
  484. return -ERESTARTSYS;
  485. ret = videobuf_qbuf(&cap->vbq, buf);
  486. mutex_unlock(&fimc->lock);
  487. return ret;
  488. }
  489. static int fimc_cap_dqbuf(struct file *file, void *priv,
  490. struct v4l2_buffer *buf)
  491. {
  492. struct fimc_ctx *ctx = priv;
  493. int ret;
  494. if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
  495. return -ERESTARTSYS;
  496. ret = videobuf_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
  497. file->f_flags & O_NONBLOCK);
  498. mutex_unlock(&ctx->fimc_dev->lock);
  499. return ret;
  500. }
  501. static int fimc_cap_s_ctrl(struct file *file, void *priv,
  502. struct v4l2_control *ctrl)
  503. {
  504. struct fimc_ctx *ctx = priv;
  505. int ret = -EINVAL;
  506. if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
  507. return -ERESTARTSYS;
  508. /* Allow any controls but 90/270 rotation while streaming */
  509. if (!fimc_capture_active(ctx->fimc_dev) ||
  510. ctrl->id != V4L2_CID_ROTATE ||
  511. (ctrl->value != 90 && ctrl->value != 270)) {
  512. ret = check_ctrl_val(ctx, ctrl);
  513. if (!ret) {
  514. ret = fimc_s_ctrl(ctx, ctrl);
  515. if (!ret)
  516. ctx->state |= FIMC_PARAMS;
  517. }
  518. }
  519. if (ret == -EINVAL)
  520. ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
  521. core, s_ctrl, ctrl);
  522. mutex_unlock(&ctx->fimc_dev->lock);
  523. return ret;
  524. }
  525. static int fimc_cap_cropcap(struct file *file, void *fh,
  526. struct v4l2_cropcap *cr)
  527. {
  528. struct fimc_frame *f;
  529. struct fimc_ctx *ctx = fh;
  530. struct fimc_dev *fimc = ctx->fimc_dev;
  531. if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  532. return -EINVAL;
  533. if (mutex_lock_interruptible(&fimc->lock))
  534. return -ERESTARTSYS;
  535. f = &ctx->s_frame;
  536. cr->bounds.left = 0;
  537. cr->bounds.top = 0;
  538. cr->bounds.width = f->o_width;
  539. cr->bounds.height = f->o_height;
  540. cr->defrect = cr->bounds;
  541. mutex_unlock(&fimc->lock);
  542. return 0;
  543. }
  544. static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  545. {
  546. struct fimc_frame *f;
  547. struct fimc_ctx *ctx = file->private_data;
  548. struct fimc_dev *fimc = ctx->fimc_dev;
  549. if (mutex_lock_interruptible(&fimc->lock))
  550. return -ERESTARTSYS;
  551. f = &ctx->s_frame;
  552. cr->c.left = f->offs_h;
  553. cr->c.top = f->offs_v;
  554. cr->c.width = f->width;
  555. cr->c.height = f->height;
  556. mutex_unlock(&fimc->lock);
  557. return 0;
  558. }
  559. static int fimc_cap_s_crop(struct file *file, void *fh,
  560. struct v4l2_crop *cr)
  561. {
  562. struct fimc_frame *f;
  563. struct fimc_ctx *ctx = file->private_data;
  564. struct fimc_dev *fimc = ctx->fimc_dev;
  565. int ret = -EINVAL;
  566. if (fimc_capture_active(fimc))
  567. return -EBUSY;
  568. ret = fimc_try_crop(ctx, cr);
  569. if (ret)
  570. return ret;
  571. if (mutex_lock_interruptible(&fimc->lock))
  572. return -ERESTARTSYS;
  573. if (!(ctx->state & FIMC_DST_FMT)) {
  574. v4l2_err(&fimc->vid_cap.v4l2_dev,
  575. "Capture color format not set\n");
  576. goto sc_unlock;
  577. }
  578. f = &ctx->s_frame;
  579. /* Check for the pixel scaling ratio when cropping input image. */
  580. ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
  581. if (ret) {
  582. v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
  583. } else {
  584. ret = 0;
  585. f->offs_h = cr->c.left;
  586. f->offs_v = cr->c.top;
  587. f->width = cr->c.width;
  588. f->height = cr->c.height;
  589. }
  590. sc_unlock:
  591. mutex_unlock(&fimc->lock);
  592. return ret;
  593. }
  594. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  595. .vidioc_querycap = fimc_vidioc_querycap_capture,
  596. .vidioc_enum_fmt_vid_cap = fimc_vidioc_enum_fmt,
  597. .vidioc_try_fmt_vid_cap = fimc_vidioc_try_fmt,
  598. .vidioc_s_fmt_vid_cap = fimc_cap_s_fmt,
  599. .vidioc_g_fmt_vid_cap = fimc_vidioc_g_fmt,
  600. .vidioc_reqbufs = fimc_cap_reqbufs,
  601. .vidioc_querybuf = fimc_cap_querybuf,
  602. .vidioc_qbuf = fimc_cap_qbuf,
  603. .vidioc_dqbuf = fimc_cap_dqbuf,
  604. .vidioc_streamon = fimc_cap_streamon,
  605. .vidioc_streamoff = fimc_cap_streamoff,
  606. .vidioc_queryctrl = fimc_vidioc_queryctrl,
  607. .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
  608. .vidioc_s_ctrl = fimc_cap_s_ctrl,
  609. .vidioc_g_crop = fimc_cap_g_crop,
  610. .vidioc_s_crop = fimc_cap_s_crop,
  611. .vidioc_cropcap = fimc_cap_cropcap,
  612. .vidioc_enum_input = fimc_cap_enum_input,
  613. .vidioc_s_input = fimc_cap_s_input,
  614. .vidioc_g_input = fimc_cap_g_input,
  615. };
  616. int fimc_register_capture_device(struct fimc_dev *fimc)
  617. {
  618. struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
  619. struct video_device *vfd;
  620. struct fimc_vid_cap *vid_cap;
  621. struct fimc_ctx *ctx;
  622. struct v4l2_format f;
  623. int ret;
  624. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  625. if (!ctx)
  626. return -ENOMEM;
  627. ctx->fimc_dev = fimc;
  628. ctx->in_path = FIMC_CAMERA;
  629. ctx->out_path = FIMC_DMA;
  630. ctx->state = FIMC_CTX_CAP;
  631. f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
  632. ctx->d_frame.fmt = find_format(&f, FMT_FLAGS_M2M);
  633. if (!v4l2_dev->name[0])
  634. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  635. "%s.capture", dev_name(&fimc->pdev->dev));
  636. ret = v4l2_device_register(NULL, v4l2_dev);
  637. if (ret)
  638. goto err_info;
  639. vfd = video_device_alloc();
  640. if (!vfd) {
  641. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  642. goto err_v4l2_reg;
  643. }
  644. snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
  645. dev_name(&fimc->pdev->dev));
  646. vfd->fops = &fimc_capture_fops;
  647. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  648. vfd->minor = -1;
  649. vfd->release = video_device_release;
  650. video_set_drvdata(vfd, fimc);
  651. vid_cap = &fimc->vid_cap;
  652. vid_cap->vfd = vfd;
  653. vid_cap->active_buf_cnt = 0;
  654. vid_cap->reqbufs_count = 0;
  655. vid_cap->refcnt = 0;
  656. /* The default color format for image sensor. */
  657. vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
  658. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  659. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  660. spin_lock_init(&ctx->slock);
  661. vid_cap->ctx = ctx;
  662. videobuf_queue_dma_contig_init(&vid_cap->vbq, &fimc_qops,
  663. vid_cap->v4l2_dev.dev, &fimc->irqlock,
  664. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  665. sizeof(struct fimc_vid_buffer), (void *)ctx, NULL);
  666. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  667. if (ret) {
  668. v4l2_err(v4l2_dev, "Failed to register video device\n");
  669. goto err_vd_reg;
  670. }
  671. v4l2_info(v4l2_dev,
  672. "FIMC capture driver registered as /dev/video%d\n",
  673. vfd->num);
  674. return 0;
  675. err_vd_reg:
  676. video_device_release(vfd);
  677. err_v4l2_reg:
  678. v4l2_device_unregister(v4l2_dev);
  679. err_info:
  680. dev_err(&fimc->pdev->dev, "failed to install\n");
  681. return ret;
  682. }
  683. void fimc_unregister_capture_device(struct fimc_dev *fimc)
  684. {
  685. struct fimc_vid_cap *capture = &fimc->vid_cap;
  686. if (capture->vfd)
  687. video_unregister_device(capture->vfd);
  688. kfree(capture->ctx);
  689. }