fimc-capture.c 22 KB

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