fimc-capture.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. MODULE_NAME, 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. set_bit(ST_CAPT_PEND, &fimc->state);
  422. ret = videobuf_streamon(&fimc->vid_cap.vbq);
  423. s_unlock:
  424. mutex_unlock(&fimc->lock);
  425. return ret;
  426. }
  427. static int fimc_cap_streamoff(struct file *file, void *priv,
  428. enum v4l2_buf_type type)
  429. {
  430. struct fimc_ctx *ctx = priv;
  431. struct fimc_dev *fimc = ctx->fimc_dev;
  432. struct fimc_vid_cap *cap = &fimc->vid_cap;
  433. unsigned long flags;
  434. int ret;
  435. spin_lock_irqsave(&fimc->slock, flags);
  436. if (!fimc_capture_running(fimc) && !fimc_capture_pending(fimc)) {
  437. spin_unlock_irqrestore(&fimc->slock, flags);
  438. dbg("state: 0x%lx", fimc->state);
  439. return -EINVAL;
  440. }
  441. spin_unlock_irqrestore(&fimc->slock, flags);
  442. if (mutex_lock_interruptible(&fimc->lock))
  443. return -ERESTARTSYS;
  444. fimc_stop_capture(fimc);
  445. ret = videobuf_streamoff(&cap->vbq);
  446. mutex_unlock(&fimc->lock);
  447. return ret;
  448. }
  449. static int fimc_cap_reqbufs(struct file *file, void *priv,
  450. struct v4l2_requestbuffers *reqbufs)
  451. {
  452. struct fimc_ctx *ctx = priv;
  453. struct fimc_dev *fimc = ctx->fimc_dev;
  454. struct fimc_vid_cap *cap = &fimc->vid_cap;
  455. int ret;
  456. if (fimc_capture_active(ctx->fimc_dev))
  457. return -EBUSY;
  458. if (mutex_lock_interruptible(&fimc->lock))
  459. return -ERESTARTSYS;
  460. ret = videobuf_reqbufs(&cap->vbq, reqbufs);
  461. if (!ret)
  462. cap->reqbufs_count = reqbufs->count;
  463. mutex_unlock(&fimc->lock);
  464. return ret;
  465. }
  466. static int fimc_cap_querybuf(struct file *file, void *priv,
  467. struct v4l2_buffer *buf)
  468. {
  469. struct fimc_ctx *ctx = priv;
  470. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  471. if (fimc_capture_active(ctx->fimc_dev))
  472. return -EBUSY;
  473. return videobuf_querybuf(&cap->vbq, buf);
  474. }
  475. static int fimc_cap_qbuf(struct file *file, void *priv,
  476. struct v4l2_buffer *buf)
  477. {
  478. struct fimc_ctx *ctx = priv;
  479. struct fimc_dev *fimc = ctx->fimc_dev;
  480. struct fimc_vid_cap *cap = &fimc->vid_cap;
  481. int ret;
  482. if (mutex_lock_interruptible(&fimc->lock))
  483. return -ERESTARTSYS;
  484. ret = videobuf_qbuf(&cap->vbq, buf);
  485. mutex_unlock(&fimc->lock);
  486. return ret;
  487. }
  488. static int fimc_cap_dqbuf(struct file *file, void *priv,
  489. struct v4l2_buffer *buf)
  490. {
  491. struct fimc_ctx *ctx = priv;
  492. int ret;
  493. if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
  494. return -ERESTARTSYS;
  495. ret = videobuf_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
  496. file->f_flags & O_NONBLOCK);
  497. mutex_unlock(&ctx->fimc_dev->lock);
  498. return ret;
  499. }
  500. static int fimc_cap_s_ctrl(struct file *file, void *priv,
  501. struct v4l2_control *ctrl)
  502. {
  503. struct fimc_ctx *ctx = priv;
  504. int ret = -EINVAL;
  505. if (mutex_lock_interruptible(&ctx->fimc_dev->lock))
  506. return -ERESTARTSYS;
  507. /* Allow any controls but 90/270 rotation while streaming */
  508. if (!fimc_capture_active(ctx->fimc_dev) ||
  509. ctrl->id != V4L2_CID_ROTATE ||
  510. (ctrl->value != 90 && ctrl->value != 270)) {
  511. ret = check_ctrl_val(ctx, ctrl);
  512. if (!ret) {
  513. ret = fimc_s_ctrl(ctx, ctrl);
  514. if (!ret)
  515. ctx->state |= FIMC_PARAMS;
  516. }
  517. }
  518. if (ret == -EINVAL)
  519. ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
  520. core, s_ctrl, ctrl);
  521. mutex_unlock(&ctx->fimc_dev->lock);
  522. return ret;
  523. }
  524. static int fimc_cap_s_crop(struct file *file, void *fh,
  525. struct v4l2_crop *cr)
  526. {
  527. struct fimc_frame *f;
  528. struct fimc_ctx *ctx = file->private_data;
  529. struct fimc_dev *fimc = ctx->fimc_dev;
  530. int ret = -EINVAL;
  531. if (fimc_capture_active(fimc))
  532. return -EBUSY;
  533. ret = fimc_try_crop(ctx, cr);
  534. if (ret)
  535. return ret;
  536. if (mutex_lock_interruptible(&fimc->lock))
  537. return -ERESTARTSYS;
  538. if (!(ctx->state & FIMC_DST_FMT)) {
  539. v4l2_err(&fimc->vid_cap.v4l2_dev,
  540. "Capture color format not set\n");
  541. goto sc_unlock;
  542. }
  543. f = &ctx->s_frame;
  544. /* Check for the pixel scaling ratio when cropping input image. */
  545. ret = fimc_check_scaler_ratio(&cr->c, &ctx->d_frame);
  546. if (ret) {
  547. v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range");
  548. } else {
  549. ret = 0;
  550. f->offs_h = cr->c.left;
  551. f->offs_v = cr->c.top;
  552. f->width = cr->c.width;
  553. f->height = cr->c.height;
  554. }
  555. sc_unlock:
  556. mutex_unlock(&fimc->lock);
  557. return ret;
  558. }
  559. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  560. .vidioc_querycap = fimc_vidioc_querycap_capture,
  561. .vidioc_enum_fmt_vid_cap = fimc_vidioc_enum_fmt,
  562. .vidioc_try_fmt_vid_cap = fimc_vidioc_try_fmt,
  563. .vidioc_s_fmt_vid_cap = fimc_cap_s_fmt,
  564. .vidioc_g_fmt_vid_cap = fimc_vidioc_g_fmt,
  565. .vidioc_reqbufs = fimc_cap_reqbufs,
  566. .vidioc_querybuf = fimc_cap_querybuf,
  567. .vidioc_qbuf = fimc_cap_qbuf,
  568. .vidioc_dqbuf = fimc_cap_dqbuf,
  569. .vidioc_streamon = fimc_cap_streamon,
  570. .vidioc_streamoff = fimc_cap_streamoff,
  571. .vidioc_queryctrl = fimc_vidioc_queryctrl,
  572. .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
  573. .vidioc_s_ctrl = fimc_cap_s_ctrl,
  574. .vidioc_g_crop = fimc_vidioc_g_crop,
  575. .vidioc_s_crop = fimc_cap_s_crop,
  576. .vidioc_cropcap = fimc_vidioc_cropcap,
  577. .vidioc_enum_input = fimc_cap_enum_input,
  578. .vidioc_s_input = fimc_cap_s_input,
  579. .vidioc_g_input = fimc_cap_g_input,
  580. };
  581. int fimc_register_capture_device(struct fimc_dev *fimc)
  582. {
  583. struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
  584. struct video_device *vfd;
  585. struct fimc_vid_cap *vid_cap;
  586. struct fimc_ctx *ctx;
  587. struct v4l2_format f;
  588. int ret;
  589. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  590. if (!ctx)
  591. return -ENOMEM;
  592. ctx->fimc_dev = fimc;
  593. ctx->in_path = FIMC_CAMERA;
  594. ctx->out_path = FIMC_DMA;
  595. ctx->state = FIMC_CTX_CAP;
  596. f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
  597. ctx->d_frame.fmt = find_format(&f, FMT_FLAGS_M2M);
  598. if (!v4l2_dev->name[0])
  599. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  600. "%s.capture", dev_name(&fimc->pdev->dev));
  601. ret = v4l2_device_register(NULL, v4l2_dev);
  602. if (ret)
  603. goto err_info;
  604. vfd = video_device_alloc();
  605. if (!vfd) {
  606. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  607. goto err_v4l2_reg;
  608. }
  609. snprintf(vfd->name, sizeof(vfd->name), "%s:cap",
  610. dev_name(&fimc->pdev->dev));
  611. vfd->fops = &fimc_capture_fops;
  612. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  613. vfd->minor = -1;
  614. vfd->release = video_device_release;
  615. video_set_drvdata(vfd, fimc);
  616. vid_cap = &fimc->vid_cap;
  617. vid_cap->vfd = vfd;
  618. vid_cap->active_buf_cnt = 0;
  619. vid_cap->reqbufs_count = 0;
  620. vid_cap->refcnt = 0;
  621. /* The default color format for image sensor. */
  622. vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
  623. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  624. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  625. spin_lock_init(&ctx->slock);
  626. vid_cap->ctx = ctx;
  627. videobuf_queue_dma_contig_init(&vid_cap->vbq, &fimc_qops,
  628. vid_cap->v4l2_dev.dev, &fimc->irqlock,
  629. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  630. sizeof(struct fimc_vid_buffer), (void *)ctx);
  631. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  632. if (ret) {
  633. v4l2_err(v4l2_dev, "Failed to register video device\n");
  634. goto err_vd_reg;
  635. }
  636. v4l2_info(v4l2_dev,
  637. "FIMC capture driver registered as /dev/video%d\n",
  638. vfd->num);
  639. return 0;
  640. err_vd_reg:
  641. video_device_release(vfd);
  642. err_v4l2_reg:
  643. v4l2_device_unregister(v4l2_dev);
  644. err_info:
  645. dev_err(&fimc->pdev->dev, "failed to install\n");
  646. return ret;
  647. }
  648. void fimc_unregister_capture_device(struct fimc_dev *fimc)
  649. {
  650. struct fimc_vid_cap *capture = &fimc->vid_cap;
  651. if (capture->vfd)
  652. video_unregister_device(capture->vfd);
  653. kfree(capture->ctx);
  654. }