fimc-capture.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
  3. *
  4. * Copyright (C) 2010 - 2011 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/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/bug.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.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/videobuf2-core.h>
  29. #include <media/videobuf2-dma-contig.h>
  30. #include "fimc-core.h"
  31. static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc,
  32. struct s5p_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 s5p_platform_fimc *pdata = fimc->pdata;
  77. struct s5p_fimc_isp_info *isp_info;
  78. struct v4l2_subdev *sd;
  79. int i;
  80. for (i = 0; i < pdata->num_clients; ++i) {
  81. isp_info = &pdata->isp_info[i];
  82. if (index >= 0 && i != index)
  83. continue;
  84. sd = fimc_subdev_register(fimc, isp_info);
  85. if (!IS_ERR_OR_NULL(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, unsigned int index)
  98. {
  99. struct s5p_fimc_isp_info *isp_info;
  100. struct s5p_platform_fimc *pdata = fimc->pdata;
  101. int ret;
  102. if (index >= pdata->num_clients)
  103. return -EINVAL;
  104. isp_info = &pdata->isp_info[index];
  105. if (isp_info->clk_frequency)
  106. clk_set_rate(fimc->clock[CLK_CAM], isp_info->clk_frequency);
  107. ret = clk_enable(fimc->clock[CLK_CAM]);
  108. if (ret)
  109. return ret;
  110. ret = fimc_subdev_attach(fimc, index);
  111. if (ret)
  112. return ret;
  113. ret = fimc_hw_set_camera_polarity(fimc, isp_info);
  114. if (ret)
  115. return ret;
  116. ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1);
  117. if (!ret)
  118. return ret;
  119. /* enabling power failed so unregister subdev */
  120. fimc_subdev_unregister(fimc);
  121. v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n",
  122. ret);
  123. return ret;
  124. }
  125. static void fimc_capture_state_cleanup(struct fimc_dev *fimc)
  126. {
  127. struct fimc_vid_cap *cap = &fimc->vid_cap;
  128. struct fimc_vid_buffer *buf;
  129. unsigned long flags;
  130. spin_lock_irqsave(&fimc->slock, flags);
  131. fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND |
  132. 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM);
  133. fimc->vid_cap.active_buf_cnt = 0;
  134. /* Release buffers that were enqueued in the driver by videobuf2. */
  135. while (!list_empty(&cap->pending_buf_q)) {
  136. buf = pending_queue_pop(cap);
  137. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  138. }
  139. while (!list_empty(&cap->active_buf_q)) {
  140. buf = active_queue_pop(cap);
  141. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  142. }
  143. spin_unlock_irqrestore(&fimc->slock, flags);
  144. }
  145. static int fimc_stop_capture(struct fimc_dev *fimc)
  146. {
  147. struct fimc_vid_cap *cap = &fimc->vid_cap;
  148. unsigned long flags;
  149. if (!fimc_capture_active(fimc))
  150. return 0;
  151. spin_lock_irqsave(&fimc->slock, flags);
  152. set_bit(ST_CAPT_SHUT, &fimc->state);
  153. fimc_deactivate_capture(fimc);
  154. spin_unlock_irqrestore(&fimc->slock, flags);
  155. wait_event_timeout(fimc->irq_queue,
  156. !test_bit(ST_CAPT_SHUT, &fimc->state),
  157. FIMC_SHUTDOWN_TIMEOUT);
  158. v4l2_subdev_call(cap->sd, video, s_stream, 0);
  159. fimc_capture_state_cleanup(fimc);
  160. dbg("state: 0x%lx", fimc->state);
  161. return 0;
  162. }
  163. static int start_streaming(struct vb2_queue *q, unsigned int count)
  164. {
  165. struct fimc_ctx *ctx = q->drv_priv;
  166. struct fimc_dev *fimc = ctx->fimc_dev;
  167. struct s5p_fimc_isp_info *isp_info;
  168. int min_bufs;
  169. int ret;
  170. fimc_hw_reset(fimc);
  171. ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1);
  172. if (ret && ret != -ENOIOCTLCMD)
  173. goto error;
  174. ret = fimc_prepare_config(ctx, ctx->state);
  175. if (ret)
  176. goto error;
  177. isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index];
  178. fimc_hw_set_camera_type(fimc, isp_info);
  179. fimc_hw_set_camera_source(fimc, isp_info);
  180. fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
  181. if (ctx->state & FIMC_PARAMS) {
  182. ret = fimc_set_scaler_info(ctx);
  183. if (ret) {
  184. err("Scaler setup error");
  185. goto error;
  186. }
  187. fimc_hw_set_input_path(ctx);
  188. fimc_hw_set_prescaler(ctx);
  189. fimc_hw_set_mainscaler(ctx);
  190. fimc_hw_set_target_format(ctx);
  191. fimc_hw_set_rotation(ctx);
  192. fimc_hw_set_effect(ctx);
  193. }
  194. fimc_hw_set_output_path(ctx);
  195. fimc_hw_set_out_dma(ctx);
  196. INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q);
  197. INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
  198. fimc->vid_cap.frame_count = 0;
  199. fimc->vid_cap.buf_index = 0;
  200. set_bit(ST_CAPT_PEND, &fimc->state);
  201. min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
  202. if (fimc->vid_cap.active_buf_cnt >= min_bufs)
  203. fimc_activate_capture(ctx);
  204. return 0;
  205. error:
  206. fimc_capture_state_cleanup(fimc);
  207. return ret;
  208. }
  209. static int stop_streaming(struct vb2_queue *q)
  210. {
  211. struct fimc_ctx *ctx = q->drv_priv;
  212. struct fimc_dev *fimc = ctx->fimc_dev;
  213. if (!fimc_capture_active(fimc))
  214. return -EINVAL;
  215. return fimc_stop_capture(fimc);
  216. }
  217. int fimc_capture_suspend(struct fimc_dev *fimc)
  218. {
  219. return -EBUSY;
  220. }
  221. int fimc_capture_resume(struct fimc_dev *fimc)
  222. {
  223. return 0;
  224. }
  225. static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
  226. {
  227. if (!fr || plane >= fr->fmt->memplanes)
  228. return 0;
  229. return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
  230. }
  231. static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
  232. unsigned int *num_planes, unsigned int sizes[],
  233. void *allocators[])
  234. {
  235. struct fimc_ctx *ctx = vq->drv_priv;
  236. struct fimc_fmt *fmt = ctx->d_frame.fmt;
  237. int i;
  238. if (!fmt)
  239. return -EINVAL;
  240. *num_planes = fmt->memplanes;
  241. for (i = 0; i < fmt->memplanes; i++) {
  242. sizes[i] = get_plane_size(&ctx->d_frame, i);
  243. allocators[i] = ctx->fimc_dev->alloc_ctx;
  244. }
  245. return 0;
  246. }
  247. static int buffer_prepare(struct vb2_buffer *vb)
  248. {
  249. struct vb2_queue *vq = vb->vb2_queue;
  250. struct fimc_ctx *ctx = vq->drv_priv;
  251. struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev;
  252. int i;
  253. if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  254. return -EINVAL;
  255. for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
  256. unsigned long size = get_plane_size(&ctx->d_frame, i);
  257. if (vb2_plane_size(vb, i) < size) {
  258. v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n",
  259. vb2_plane_size(vb, i), size);
  260. return -EINVAL;
  261. }
  262. vb2_set_plane_payload(vb, i, size);
  263. }
  264. return 0;
  265. }
  266. static void buffer_queue(struct vb2_buffer *vb)
  267. {
  268. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  269. struct fimc_dev *fimc = ctx->fimc_dev;
  270. struct fimc_vid_buffer *buf
  271. = container_of(vb, struct fimc_vid_buffer, vb);
  272. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  273. unsigned long flags;
  274. int min_bufs;
  275. spin_lock_irqsave(&fimc->slock, flags);
  276. fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
  277. if (!test_bit(ST_CAPT_STREAM, &fimc->state)
  278. && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
  279. /* Setup the buffer directly for processing. */
  280. int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
  281. vid_cap->buf_index;
  282. fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
  283. buf->index = vid_cap->buf_index;
  284. active_queue_add(vid_cap, buf);
  285. if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
  286. vid_cap->buf_index = 0;
  287. } else {
  288. fimc_pending_queue_add(vid_cap, buf);
  289. }
  290. min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
  291. if (vb2_is_streaming(&vid_cap->vbq) &&
  292. vid_cap->active_buf_cnt >= min_bufs &&
  293. !test_and_set_bit(ST_CAPT_STREAM, &fimc->state))
  294. fimc_activate_capture(ctx);
  295. spin_unlock_irqrestore(&fimc->slock, flags);
  296. }
  297. static void fimc_lock(struct vb2_queue *vq)
  298. {
  299. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  300. mutex_lock(&ctx->fimc_dev->lock);
  301. }
  302. static void fimc_unlock(struct vb2_queue *vq)
  303. {
  304. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  305. mutex_unlock(&ctx->fimc_dev->lock);
  306. }
  307. static struct vb2_ops fimc_capture_qops = {
  308. .queue_setup = queue_setup,
  309. .buf_prepare = buffer_prepare,
  310. .buf_queue = buffer_queue,
  311. .wait_prepare = fimc_unlock,
  312. .wait_finish = fimc_lock,
  313. .start_streaming = start_streaming,
  314. .stop_streaming = stop_streaming,
  315. };
  316. static int fimc_capture_open(struct file *file)
  317. {
  318. struct fimc_dev *fimc = video_drvdata(file);
  319. int ret = 0;
  320. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  321. /* Return if the corresponding video mem2mem node is already opened. */
  322. if (fimc_m2m_active(fimc))
  323. return -EBUSY;
  324. ret = pm_runtime_get_sync(&fimc->pdev->dev);
  325. if (ret)
  326. return ret;
  327. if (++fimc->vid_cap.refcnt == 1) {
  328. ret = fimc_isp_subdev_init(fimc, 0);
  329. if (ret) {
  330. pm_runtime_put_sync(&fimc->pdev->dev);
  331. fimc->vid_cap.refcnt--;
  332. return -EIO;
  333. }
  334. }
  335. file->private_data = fimc->vid_cap.ctx;
  336. return 0;
  337. }
  338. static int fimc_capture_close(struct file *file)
  339. {
  340. struct fimc_dev *fimc = video_drvdata(file);
  341. dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
  342. if (--fimc->vid_cap.refcnt == 0) {
  343. fimc_stop_capture(fimc);
  344. vb2_queue_release(&fimc->vid_cap.vbq);
  345. v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n");
  346. v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
  347. clk_disable(fimc->clock[CLK_CAM]);
  348. fimc_subdev_unregister(fimc);
  349. }
  350. pm_runtime_put(&fimc->pdev->dev);
  351. return 0;
  352. }
  353. static unsigned int fimc_capture_poll(struct file *file,
  354. struct poll_table_struct *wait)
  355. {
  356. struct fimc_ctx *ctx = file->private_data;
  357. struct fimc_dev *fimc = ctx->fimc_dev;
  358. return vb2_poll(&fimc->vid_cap.vbq, file, wait);
  359. }
  360. static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
  361. {
  362. struct fimc_ctx *ctx = file->private_data;
  363. struct fimc_dev *fimc = ctx->fimc_dev;
  364. return vb2_mmap(&fimc->vid_cap.vbq, vma);
  365. }
  366. /* video device file operations */
  367. static const struct v4l2_file_operations fimc_capture_fops = {
  368. .owner = THIS_MODULE,
  369. .open = fimc_capture_open,
  370. .release = fimc_capture_close,
  371. .poll = fimc_capture_poll,
  372. .unlocked_ioctl = video_ioctl2,
  373. .mmap = fimc_capture_mmap,
  374. };
  375. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  376. struct v4l2_capability *cap)
  377. {
  378. struct fimc_ctx *ctx = file->private_data;
  379. struct fimc_dev *fimc = ctx->fimc_dev;
  380. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  381. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  382. cap->bus_info[0] = 0;
  383. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
  384. V4L2_CAP_VIDEO_CAPTURE_MPLANE;
  385. return 0;
  386. }
  387. /* Synchronize formats of the camera interface input and attached sensor. */
  388. static int sync_capture_fmt(struct fimc_ctx *ctx)
  389. {
  390. struct fimc_frame *frame = &ctx->s_frame;
  391. struct fimc_dev *fimc = ctx->fimc_dev;
  392. struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt;
  393. int ret;
  394. fmt->width = ctx->d_frame.o_width;
  395. fmt->height = ctx->d_frame.o_height;
  396. ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt);
  397. if (ret == -ENOIOCTLCMD) {
  398. err("s_mbus_fmt failed");
  399. return ret;
  400. }
  401. dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code);
  402. frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM);
  403. if (!frame->fmt) {
  404. err("fimc source format not found\n");
  405. return -EINVAL;
  406. }
  407. frame->f_width = fmt->width;
  408. frame->f_height = fmt->height;
  409. frame->width = fmt->width;
  410. frame->height = fmt->height;
  411. frame->o_width = fmt->width;
  412. frame->o_height = fmt->height;
  413. frame->offs_h = 0;
  414. frame->offs_v = 0;
  415. return 0;
  416. }
  417. static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
  418. struct v4l2_format *f)
  419. {
  420. struct fimc_ctx *ctx = priv;
  421. struct fimc_dev *fimc = ctx->fimc_dev;
  422. struct fimc_frame *frame;
  423. struct v4l2_pix_format_mplane *pix;
  424. int ret;
  425. int i;
  426. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  427. return -EINVAL;
  428. ret = fimc_vidioc_try_fmt_mplane(file, priv, f);
  429. if (ret)
  430. return ret;
  431. if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc))
  432. return -EBUSY;
  433. frame = &ctx->d_frame;
  434. pix = &f->fmt.pix_mp;
  435. frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM);
  436. if (!frame->fmt) {
  437. err("fimc target format not found\n");
  438. return -EINVAL;
  439. }
  440. for (i = 0; i < frame->fmt->colplanes; i++) {
  441. frame->payload[i] =
  442. (pix->width * pix->height * frame->fmt->depth[i]) >> 3;
  443. }
  444. /* Output DMA frame pixel size and offsets. */
  445. frame->f_width = pix->plane_fmt[0].bytesperline * 8
  446. / frame->fmt->depth[0];
  447. frame->f_height = pix->height;
  448. frame->width = pix->width;
  449. frame->height = pix->height;
  450. frame->o_width = pix->width;
  451. frame->o_height = pix->height;
  452. frame->offs_h = 0;
  453. frame->offs_v = 0;
  454. ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT);
  455. ret = sync_capture_fmt(ctx);
  456. return ret;
  457. }
  458. static int fimc_cap_enum_input(struct file *file, void *priv,
  459. struct v4l2_input *i)
  460. {
  461. struct fimc_ctx *ctx = priv;
  462. struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata;
  463. struct s5p_fimc_isp_info *isp_info;
  464. if (i->index >= pldata->num_clients)
  465. return -EINVAL;
  466. isp_info = &pldata->isp_info[i->index];
  467. i->type = V4L2_INPUT_TYPE_CAMERA;
  468. strncpy(i->name, isp_info->board_info->type, 32);
  469. return 0;
  470. }
  471. static int fimc_cap_s_input(struct file *file, void *priv,
  472. unsigned int i)
  473. {
  474. struct fimc_ctx *ctx = priv;
  475. struct fimc_dev *fimc = ctx->fimc_dev;
  476. struct s5p_platform_fimc *pdata = fimc->pdata;
  477. if (fimc_capture_active(ctx->fimc_dev))
  478. return -EBUSY;
  479. if (i >= pdata->num_clients)
  480. return -EINVAL;
  481. if (fimc->vid_cap.sd) {
  482. int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0);
  483. if (ret)
  484. err("s_power failed: %d", ret);
  485. clk_disable(fimc->clock[CLK_CAM]);
  486. }
  487. /* Release the attached sensor subdevice. */
  488. fimc_subdev_unregister(fimc);
  489. return fimc_isp_subdev_init(fimc, i);
  490. }
  491. static int fimc_cap_g_input(struct file *file, void *priv,
  492. unsigned int *i)
  493. {
  494. struct fimc_ctx *ctx = priv;
  495. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  496. *i = cap->input_index;
  497. return 0;
  498. }
  499. static int fimc_cap_streamon(struct file *file, void *priv,
  500. enum v4l2_buf_type type)
  501. {
  502. struct fimc_ctx *ctx = priv;
  503. struct fimc_dev *fimc = ctx->fimc_dev;
  504. if (fimc_capture_active(fimc) || !fimc->vid_cap.sd)
  505. return -EBUSY;
  506. if (!(ctx->state & FIMC_DST_FMT)) {
  507. v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n");
  508. return -EINVAL;
  509. }
  510. return vb2_streamon(&fimc->vid_cap.vbq, type);
  511. }
  512. static int fimc_cap_streamoff(struct file *file, void *priv,
  513. enum v4l2_buf_type type)
  514. {
  515. struct fimc_ctx *ctx = priv;
  516. struct fimc_dev *fimc = ctx->fimc_dev;
  517. return vb2_streamoff(&fimc->vid_cap.vbq, type);
  518. }
  519. static int fimc_cap_reqbufs(struct file *file, void *priv,
  520. struct v4l2_requestbuffers *reqbufs)
  521. {
  522. struct fimc_ctx *ctx = priv;
  523. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  524. int ret;
  525. ret = vb2_reqbufs(&cap->vbq, reqbufs);
  526. if (!ret)
  527. cap->reqbufs_count = reqbufs->count;
  528. return ret;
  529. }
  530. static int fimc_cap_querybuf(struct file *file, void *priv,
  531. struct v4l2_buffer *buf)
  532. {
  533. struct fimc_ctx *ctx = priv;
  534. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  535. return vb2_querybuf(&cap->vbq, buf);
  536. }
  537. static int fimc_cap_qbuf(struct file *file, void *priv,
  538. struct v4l2_buffer *buf)
  539. {
  540. struct fimc_ctx *ctx = priv;
  541. struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap;
  542. return vb2_qbuf(&cap->vbq, buf);
  543. }
  544. static int fimc_cap_dqbuf(struct file *file, void *priv,
  545. struct v4l2_buffer *buf)
  546. {
  547. struct fimc_ctx *ctx = priv;
  548. return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf,
  549. file->f_flags & O_NONBLOCK);
  550. }
  551. static int fimc_cap_s_ctrl(struct file *file, void *priv,
  552. struct v4l2_control *ctrl)
  553. {
  554. struct fimc_ctx *ctx = priv;
  555. int ret = -EINVAL;
  556. /* Allow any controls but 90/270 rotation while streaming */
  557. if (!fimc_capture_active(ctx->fimc_dev) ||
  558. ctrl->id != V4L2_CID_ROTATE ||
  559. (ctrl->value != 90 && ctrl->value != 270)) {
  560. ret = check_ctrl_val(ctx, ctrl);
  561. if (!ret) {
  562. ret = fimc_s_ctrl(ctx, ctrl);
  563. if (!ret)
  564. ctx->state |= FIMC_PARAMS;
  565. }
  566. }
  567. if (ret == -EINVAL)
  568. ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd,
  569. core, s_ctrl, ctrl);
  570. return ret;
  571. }
  572. static int fimc_cap_cropcap(struct file *file, void *fh,
  573. struct v4l2_cropcap *cr)
  574. {
  575. struct fimc_frame *f;
  576. struct fimc_ctx *ctx = fh;
  577. if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  578. return -EINVAL;
  579. f = &ctx->s_frame;
  580. cr->bounds.left = 0;
  581. cr->bounds.top = 0;
  582. cr->bounds.width = f->o_width;
  583. cr->bounds.height = f->o_height;
  584. cr->defrect = cr->bounds;
  585. return 0;
  586. }
  587. static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  588. {
  589. struct fimc_frame *f;
  590. struct fimc_ctx *ctx = file->private_data;
  591. f = &ctx->s_frame;
  592. cr->c.left = f->offs_h;
  593. cr->c.top = f->offs_v;
  594. cr->c.width = f->width;
  595. cr->c.height = f->height;
  596. return 0;
  597. }
  598. static int fimc_cap_s_crop(struct file *file, void *fh,
  599. struct v4l2_crop *cr)
  600. {
  601. struct fimc_frame *f;
  602. struct fimc_ctx *ctx = file->private_data;
  603. struct fimc_dev *fimc = ctx->fimc_dev;
  604. int ret = -EINVAL;
  605. if (fimc_capture_active(fimc))
  606. return -EBUSY;
  607. ret = fimc_try_crop(ctx, cr);
  608. if (ret)
  609. return ret;
  610. if (!(ctx->state & FIMC_DST_FMT)) {
  611. v4l2_err(&fimc->vid_cap.v4l2_dev,
  612. "Capture color format not set\n");
  613. return -EINVAL; /* TODO: make sure this is the right value */
  614. }
  615. f = &ctx->s_frame;
  616. /* Check for the pixel scaling ratio when cropping input image. */
  617. ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height,
  618. ctx->d_frame.width, ctx->d_frame.height,
  619. ctx->rotation);
  620. if (ret) {
  621. v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range\n");
  622. return ret;
  623. }
  624. f->offs_h = cr->c.left;
  625. f->offs_v = cr->c.top;
  626. f->width = cr->c.width;
  627. f->height = cr->c.height;
  628. return 0;
  629. }
  630. static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
  631. .vidioc_querycap = fimc_vidioc_querycap_capture,
  632. .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane,
  633. .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane,
  634. .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
  635. .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane,
  636. .vidioc_reqbufs = fimc_cap_reqbufs,
  637. .vidioc_querybuf = fimc_cap_querybuf,
  638. .vidioc_qbuf = fimc_cap_qbuf,
  639. .vidioc_dqbuf = fimc_cap_dqbuf,
  640. .vidioc_streamon = fimc_cap_streamon,
  641. .vidioc_streamoff = fimc_cap_streamoff,
  642. .vidioc_queryctrl = fimc_vidioc_queryctrl,
  643. .vidioc_g_ctrl = fimc_vidioc_g_ctrl,
  644. .vidioc_s_ctrl = fimc_cap_s_ctrl,
  645. .vidioc_g_crop = fimc_cap_g_crop,
  646. .vidioc_s_crop = fimc_cap_s_crop,
  647. .vidioc_cropcap = fimc_cap_cropcap,
  648. .vidioc_enum_input = fimc_cap_enum_input,
  649. .vidioc_s_input = fimc_cap_s_input,
  650. .vidioc_g_input = fimc_cap_g_input,
  651. };
  652. /* fimc->lock must be already initialized */
  653. int fimc_register_capture_device(struct fimc_dev *fimc)
  654. {
  655. struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev;
  656. struct video_device *vfd;
  657. struct fimc_vid_cap *vid_cap;
  658. struct fimc_ctx *ctx;
  659. struct v4l2_format f;
  660. struct fimc_frame *fr;
  661. struct vb2_queue *q;
  662. int ret;
  663. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  664. if (!ctx)
  665. return -ENOMEM;
  666. ctx->fimc_dev = fimc;
  667. ctx->in_path = FIMC_CAMERA;
  668. ctx->out_path = FIMC_DMA;
  669. ctx->state = FIMC_CTX_CAP;
  670. /* Default format of the output frames */
  671. f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
  672. fr = &ctx->d_frame;
  673. fr->fmt = find_format(&f, FMT_FLAGS_M2M);
  674. fr->width = fr->f_width = fr->o_width = 640;
  675. fr->height = fr->f_height = fr->o_height = 480;
  676. snprintf(v4l2_dev->name, sizeof(v4l2_dev->name),
  677. "%s.capture", dev_name(&fimc->pdev->dev));
  678. ret = v4l2_device_register(NULL, v4l2_dev);
  679. if (ret)
  680. goto err_info;
  681. vfd = video_device_alloc();
  682. if (!vfd) {
  683. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  684. goto err_v4l2_reg;
  685. }
  686. strlcpy(vfd->name, v4l2_dev->name, sizeof(vfd->name));
  687. vfd->fops = &fimc_capture_fops;
  688. vfd->ioctl_ops = &fimc_capture_ioctl_ops;
  689. vfd->v4l2_dev = v4l2_dev;
  690. vfd->minor = -1;
  691. vfd->release = video_device_release;
  692. vfd->lock = &fimc->lock;
  693. video_set_drvdata(vfd, fimc);
  694. vid_cap = &fimc->vid_cap;
  695. vid_cap->vfd = vfd;
  696. vid_cap->active_buf_cnt = 0;
  697. vid_cap->reqbufs_count = 0;
  698. vid_cap->refcnt = 0;
  699. /* Default color format for image sensor */
  700. vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8;
  701. INIT_LIST_HEAD(&vid_cap->pending_buf_q);
  702. INIT_LIST_HEAD(&vid_cap->active_buf_q);
  703. spin_lock_init(&ctx->slock);
  704. vid_cap->ctx = ctx;
  705. q = &fimc->vid_cap.vbq;
  706. memset(q, 0, sizeof(*q));
  707. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  708. q->io_modes = VB2_MMAP | VB2_USERPTR;
  709. q->drv_priv = fimc->vid_cap.ctx;
  710. q->ops = &fimc_capture_qops;
  711. q->mem_ops = &vb2_dma_contig_memops;
  712. q->buf_struct_size = sizeof(struct fimc_vid_buffer);
  713. vb2_queue_init(q);
  714. fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
  715. ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
  716. if (ret)
  717. goto err_ent;
  718. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  719. if (ret) {
  720. v4l2_err(v4l2_dev, "Failed to register video device\n");
  721. goto err_vd_reg;
  722. }
  723. v4l2_info(v4l2_dev,
  724. "FIMC capture driver registered as /dev/video%d\n",
  725. vfd->num);
  726. return 0;
  727. err_vd_reg:
  728. media_entity_cleanup(&vfd->entity);
  729. err_ent:
  730. video_device_release(vfd);
  731. err_v4l2_reg:
  732. v4l2_device_unregister(v4l2_dev);
  733. err_info:
  734. kfree(ctx);
  735. dev_err(&fimc->pdev->dev, "failed to install\n");
  736. return ret;
  737. }
  738. void fimc_unregister_capture_device(struct fimc_dev *fimc)
  739. {
  740. struct video_device *vfd = fimc->vid_cap.vfd;
  741. if (vfd) {
  742. media_entity_cleanup(&vfd->entity);
  743. video_unregister_device(vfd);
  744. }
  745. kfree(fimc->vid_cap.ctx);
  746. }