fimc-capture.c 22 KB

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