fimc-lite.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. /*
  2. * Samsung EXYNOS FIMC-LITE (camera host interface) driver
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co., Ltd.
  5. * 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. #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
  12. #include <linux/bug.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/v4l2-mem2mem.h>
  27. #include <media/videobuf2-core.h>
  28. #include <media/videobuf2-dma-contig.h>
  29. #include "fimc-mdevice.h"
  30. #include "fimc-core.h"
  31. #include "fimc-lite-reg.h"
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. static const struct fimc_fmt fimc_lite_formats[] = {
  35. {
  36. .name = "YUV 4:2:2 packed, YCbYCr",
  37. .fourcc = V4L2_PIX_FMT_YUYV,
  38. .depth = { 16 },
  39. .color = FIMC_FMT_YCBYCR422,
  40. .memplanes = 1,
  41. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  42. }, {
  43. .name = "YUV 4:2:2 packed, CbYCrY",
  44. .fourcc = V4L2_PIX_FMT_UYVY,
  45. .depth = { 16 },
  46. .color = FIMC_FMT_CBYCRY422,
  47. .memplanes = 1,
  48. .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
  49. }, {
  50. .name = "YUV 4:2:2 packed, CrYCbY",
  51. .fourcc = V4L2_PIX_FMT_VYUY,
  52. .depth = { 16 },
  53. .color = FIMC_FMT_CRYCBY422,
  54. .memplanes = 1,
  55. .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
  56. }, {
  57. .name = "YUV 4:2:2 packed, YCrYCb",
  58. .fourcc = V4L2_PIX_FMT_YVYU,
  59. .depth = { 16 },
  60. .color = FIMC_FMT_YCRYCB422,
  61. .memplanes = 1,
  62. .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
  63. }, {
  64. .name = "RAW8 (GRBG)",
  65. .fourcc = V4L2_PIX_FMT_SGRBG8,
  66. .depth = { 8 },
  67. .color = FIMC_FMT_RAW8,
  68. .memplanes = 1,
  69. .mbus_code = V4L2_MBUS_FMT_SGRBG8_1X8,
  70. }, {
  71. .name = "RAW10 (GRBG)",
  72. .fourcc = V4L2_PIX_FMT_SGRBG10,
  73. .depth = { 10 },
  74. .color = FIMC_FMT_RAW10,
  75. .memplanes = 1,
  76. .mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10,
  77. }, {
  78. .name = "RAW12 (GRBG)",
  79. .fourcc = V4L2_PIX_FMT_SGRBG12,
  80. .depth = { 12 },
  81. .color = FIMC_FMT_RAW12,
  82. .memplanes = 1,
  83. .mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12,
  84. },
  85. };
  86. /**
  87. * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
  88. * @pixelformat: fourcc to match, ignored if null
  89. * @mbus_code: media bus code to match, ignored if null
  90. * @index: index to the fimc_lite_formats array, ignored if negative
  91. */
  92. static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
  93. const u32 *mbus_code, int index)
  94. {
  95. const struct fimc_fmt *fmt, *def_fmt = NULL;
  96. unsigned int i;
  97. int id = 0;
  98. if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
  99. return NULL;
  100. for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
  101. fmt = &fimc_lite_formats[i];
  102. if (pixelformat && fmt->fourcc == *pixelformat)
  103. return fmt;
  104. if (mbus_code && fmt->mbus_code == *mbus_code)
  105. return fmt;
  106. if (index == id)
  107. def_fmt = fmt;
  108. id++;
  109. }
  110. return def_fmt;
  111. }
  112. static int fimc_lite_hw_init(struct fimc_lite *fimc)
  113. {
  114. struct fimc_pipeline *pipeline = &fimc->pipeline;
  115. struct fimc_sensor_info *sensor;
  116. unsigned long flags;
  117. if (pipeline->subdevs[IDX_SENSOR] == NULL)
  118. return -ENXIO;
  119. if (fimc->fmt == NULL)
  120. return -EINVAL;
  121. sensor = v4l2_get_subdev_hostdata(pipeline->subdevs[IDX_SENSOR]);
  122. spin_lock_irqsave(&fimc->slock, flags);
  123. flite_hw_set_camera_bus(fimc, sensor->pdata);
  124. flite_hw_set_source_format(fimc, &fimc->inp_frame);
  125. flite_hw_set_window_offset(fimc, &fimc->inp_frame);
  126. flite_hw_set_output_dma(fimc, &fimc->out_frame, true);
  127. flite_hw_set_interrupt_mask(fimc);
  128. flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
  129. if (debug > 0)
  130. flite_hw_dump_regs(fimc, __func__);
  131. spin_unlock_irqrestore(&fimc->slock, flags);
  132. return 0;
  133. }
  134. /*
  135. * Reinitialize the driver so it is ready to start the streaming again.
  136. * Set fimc->state to indicate stream off and the hardware shut down state.
  137. * If not suspending (@suspend is false), return any buffers to videobuf2.
  138. * Otherwise put any owned buffers onto the pending buffers queue, so they
  139. * can be re-spun when the device is being resumed. Also perform FIMC
  140. * software reset and disable streaming on the whole pipeline if required.
  141. */
  142. static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
  143. {
  144. struct flite_buffer *buf;
  145. unsigned long flags;
  146. bool streaming;
  147. spin_lock_irqsave(&fimc->slock, flags);
  148. streaming = fimc->state & (1 << ST_SENSOR_STREAM);
  149. fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
  150. 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
  151. if (suspend)
  152. fimc->state |= (1 << ST_FLITE_SUSPENDED);
  153. else
  154. fimc->state &= ~(1 << ST_FLITE_PENDING |
  155. 1 << ST_FLITE_SUSPENDED);
  156. /* Release unused buffers */
  157. while (!suspend && !list_empty(&fimc->pending_buf_q)) {
  158. buf = fimc_lite_pending_queue_pop(fimc);
  159. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  160. }
  161. /* If suspending put unused buffers onto pending queue */
  162. while (!list_empty(&fimc->active_buf_q)) {
  163. buf = fimc_lite_active_queue_pop(fimc);
  164. if (suspend)
  165. fimc_lite_pending_queue_add(fimc, buf);
  166. else
  167. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  168. }
  169. spin_unlock_irqrestore(&fimc->slock, flags);
  170. flite_hw_reset(fimc);
  171. if (!streaming)
  172. return 0;
  173. return fimc_pipeline_s_stream(&fimc->pipeline, 0);
  174. }
  175. static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
  176. {
  177. unsigned long flags;
  178. if (!fimc_lite_active(fimc))
  179. return 0;
  180. spin_lock_irqsave(&fimc->slock, flags);
  181. set_bit(ST_FLITE_OFF, &fimc->state);
  182. flite_hw_capture_stop(fimc);
  183. spin_unlock_irqrestore(&fimc->slock, flags);
  184. wait_event_timeout(fimc->irq_queue,
  185. !test_bit(ST_FLITE_OFF, &fimc->state),
  186. (2*HZ/10)); /* 200 ms */
  187. return fimc_lite_reinit(fimc, suspend);
  188. }
  189. /* Must be called with fimc.slock spinlock held. */
  190. static void fimc_lite_config_update(struct fimc_lite *fimc)
  191. {
  192. flite_hw_set_window_offset(fimc, &fimc->inp_frame);
  193. flite_hw_set_dma_window(fimc, &fimc->out_frame);
  194. flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
  195. clear_bit(ST_FLITE_CONFIG, &fimc->state);
  196. }
  197. static irqreturn_t flite_irq_handler(int irq, void *priv)
  198. {
  199. struct fimc_lite *fimc = priv;
  200. struct flite_buffer *vbuf;
  201. unsigned long flags;
  202. struct timeval *tv;
  203. struct timespec ts;
  204. u32 intsrc;
  205. spin_lock_irqsave(&fimc->slock, flags);
  206. intsrc = flite_hw_get_interrupt_source(fimc);
  207. flite_hw_clear_pending_irq(fimc);
  208. if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
  209. wake_up(&fimc->irq_queue);
  210. goto done;
  211. }
  212. if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
  213. clear_bit(ST_FLITE_RUN, &fimc->state);
  214. fimc->events.data_overflow++;
  215. }
  216. if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
  217. flite_hw_clear_last_capture_end(fimc);
  218. clear_bit(ST_FLITE_STREAM, &fimc->state);
  219. wake_up(&fimc->irq_queue);
  220. }
  221. if (fimc->out_path != FIMC_IO_DMA)
  222. goto done;
  223. if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
  224. test_bit(ST_FLITE_RUN, &fimc->state) &&
  225. !list_empty(&fimc->active_buf_q) &&
  226. !list_empty(&fimc->pending_buf_q)) {
  227. vbuf = fimc_lite_active_queue_pop(fimc);
  228. ktime_get_ts(&ts);
  229. tv = &vbuf->vb.v4l2_buf.timestamp;
  230. tv->tv_sec = ts.tv_sec;
  231. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  232. vbuf->vb.v4l2_buf.sequence = fimc->frame_count++;
  233. vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
  234. vbuf = fimc_lite_pending_queue_pop(fimc);
  235. flite_hw_set_output_addr(fimc, vbuf->paddr);
  236. fimc_lite_active_queue_add(fimc, vbuf);
  237. }
  238. if (test_bit(ST_FLITE_CONFIG, &fimc->state))
  239. fimc_lite_config_update(fimc);
  240. if (list_empty(&fimc->pending_buf_q)) {
  241. flite_hw_capture_stop(fimc);
  242. clear_bit(ST_FLITE_STREAM, &fimc->state);
  243. }
  244. done:
  245. set_bit(ST_FLITE_RUN, &fimc->state);
  246. spin_unlock_irqrestore(&fimc->slock, flags);
  247. return IRQ_HANDLED;
  248. }
  249. static int start_streaming(struct vb2_queue *q, unsigned int count)
  250. {
  251. struct fimc_lite *fimc = q->drv_priv;
  252. int ret;
  253. fimc->frame_count = 0;
  254. ret = fimc_lite_hw_init(fimc);
  255. if (ret) {
  256. fimc_lite_reinit(fimc, false);
  257. return ret;
  258. }
  259. set_bit(ST_FLITE_PENDING, &fimc->state);
  260. if (!list_empty(&fimc->active_buf_q) &&
  261. !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
  262. flite_hw_capture_start(fimc);
  263. if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
  264. fimc_pipeline_s_stream(&fimc->pipeline, 1);
  265. }
  266. if (debug > 0)
  267. flite_hw_dump_regs(fimc, __func__);
  268. return 0;
  269. }
  270. static int stop_streaming(struct vb2_queue *q)
  271. {
  272. struct fimc_lite *fimc = q->drv_priv;
  273. if (!fimc_lite_active(fimc))
  274. return -EINVAL;
  275. return fimc_lite_stop_capture(fimc, false);
  276. }
  277. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
  278. unsigned int *num_buffers, unsigned int *num_planes,
  279. unsigned int sizes[], void *allocators[])
  280. {
  281. const struct v4l2_pix_format_mplane *pixm = NULL;
  282. struct fimc_lite *fimc = vq->drv_priv;
  283. struct flite_frame *frame = &fimc->out_frame;
  284. const struct fimc_fmt *fmt = fimc->fmt;
  285. unsigned long wh;
  286. int i;
  287. if (pfmt) {
  288. pixm = &pfmt->fmt.pix_mp;
  289. fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, -1);
  290. wh = pixm->width * pixm->height;
  291. } else {
  292. wh = frame->f_width * frame->f_height;
  293. }
  294. if (fmt == NULL)
  295. return -EINVAL;
  296. *num_planes = fmt->memplanes;
  297. for (i = 0; i < fmt->memplanes; i++) {
  298. unsigned int size = (wh * fmt->depth[i]) / 8;
  299. if (pixm)
  300. sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
  301. else
  302. sizes[i] = size;
  303. allocators[i] = fimc->alloc_ctx;
  304. }
  305. return 0;
  306. }
  307. static int buffer_prepare(struct vb2_buffer *vb)
  308. {
  309. struct vb2_queue *vq = vb->vb2_queue;
  310. struct fimc_lite *fimc = vq->drv_priv;
  311. int i;
  312. if (fimc->fmt == NULL)
  313. return -EINVAL;
  314. for (i = 0; i < fimc->fmt->memplanes; i++) {
  315. unsigned long size = fimc->payload[i];
  316. if (vb2_plane_size(vb, i) < size) {
  317. v4l2_err(fimc->vfd,
  318. "User buffer too small (%ld < %ld)\n",
  319. vb2_plane_size(vb, i), size);
  320. return -EINVAL;
  321. }
  322. vb2_set_plane_payload(vb, i, size);
  323. }
  324. return 0;
  325. }
  326. static void buffer_queue(struct vb2_buffer *vb)
  327. {
  328. struct flite_buffer *buf
  329. = container_of(vb, struct flite_buffer, vb);
  330. struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
  331. unsigned long flags;
  332. spin_lock_irqsave(&fimc->slock, flags);
  333. buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
  334. if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
  335. !test_bit(ST_FLITE_STREAM, &fimc->state) &&
  336. list_empty(&fimc->active_buf_q)) {
  337. flite_hw_set_output_addr(fimc, buf->paddr);
  338. fimc_lite_active_queue_add(fimc, buf);
  339. } else {
  340. fimc_lite_pending_queue_add(fimc, buf);
  341. }
  342. if (vb2_is_streaming(&fimc->vb_queue) &&
  343. !list_empty(&fimc->pending_buf_q) &&
  344. !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
  345. flite_hw_capture_start(fimc);
  346. spin_unlock_irqrestore(&fimc->slock, flags);
  347. if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
  348. fimc_pipeline_s_stream(&fimc->pipeline, 1);
  349. return;
  350. }
  351. spin_unlock_irqrestore(&fimc->slock, flags);
  352. }
  353. static void fimc_lock(struct vb2_queue *vq)
  354. {
  355. struct fimc_lite *fimc = vb2_get_drv_priv(vq);
  356. mutex_lock(&fimc->lock);
  357. }
  358. static void fimc_unlock(struct vb2_queue *vq)
  359. {
  360. struct fimc_lite *fimc = vb2_get_drv_priv(vq);
  361. mutex_unlock(&fimc->lock);
  362. }
  363. static const struct vb2_ops fimc_lite_qops = {
  364. .queue_setup = queue_setup,
  365. .buf_prepare = buffer_prepare,
  366. .buf_queue = buffer_queue,
  367. .wait_prepare = fimc_unlock,
  368. .wait_finish = fimc_lock,
  369. .start_streaming = start_streaming,
  370. .stop_streaming = stop_streaming,
  371. };
  372. static void fimc_lite_clear_event_counters(struct fimc_lite *fimc)
  373. {
  374. unsigned long flags;
  375. spin_lock_irqsave(&fimc->slock, flags);
  376. memset(&fimc->events, 0, sizeof(fimc->events));
  377. spin_unlock_irqrestore(&fimc->slock, flags);
  378. }
  379. static int fimc_lite_open(struct file *file)
  380. {
  381. struct fimc_lite *fimc = video_drvdata(file);
  382. int ret = v4l2_fh_open(file);
  383. if (ret)
  384. return ret;
  385. set_bit(ST_FLITE_IN_USE, &fimc->state);
  386. pm_runtime_get_sync(&fimc->pdev->dev);
  387. if (++fimc->ref_count != 1 || fimc->out_path != FIMC_IO_DMA)
  388. return ret;
  389. ret = fimc_pipeline_initialize(&fimc->pipeline, &fimc->vfd->entity,
  390. true);
  391. if (ret < 0) {
  392. v4l2_err(fimc->vfd, "Video pipeline initialization failed\n");
  393. pm_runtime_put_sync(&fimc->pdev->dev);
  394. fimc->ref_count--;
  395. v4l2_fh_release(file);
  396. clear_bit(ST_FLITE_IN_USE, &fimc->state);
  397. }
  398. fimc_lite_clear_event_counters(fimc);
  399. return ret;
  400. }
  401. static int fimc_lite_close(struct file *file)
  402. {
  403. struct fimc_lite *fimc = video_drvdata(file);
  404. if (--fimc->ref_count == 0 && fimc->out_path == FIMC_IO_DMA) {
  405. clear_bit(ST_FLITE_IN_USE, &fimc->state);
  406. fimc_lite_stop_capture(fimc, false);
  407. fimc_pipeline_shutdown(&fimc->pipeline);
  408. clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
  409. }
  410. pm_runtime_put(&fimc->pdev->dev);
  411. if (fimc->ref_count == 0)
  412. vb2_queue_release(&fimc->vb_queue);
  413. return v4l2_fh_release(file);
  414. }
  415. static unsigned int fimc_lite_poll(struct file *file,
  416. struct poll_table_struct *wait)
  417. {
  418. struct fimc_lite *fimc = video_drvdata(file);
  419. return vb2_poll(&fimc->vb_queue, file, wait);
  420. }
  421. static int fimc_lite_mmap(struct file *file, struct vm_area_struct *vma)
  422. {
  423. struct fimc_lite *fimc = video_drvdata(file);
  424. return vb2_mmap(&fimc->vb_queue, vma);
  425. }
  426. static const struct v4l2_file_operations fimc_lite_fops = {
  427. .owner = THIS_MODULE,
  428. .open = fimc_lite_open,
  429. .release = fimc_lite_close,
  430. .poll = fimc_lite_poll,
  431. .unlocked_ioctl = video_ioctl2,
  432. .mmap = fimc_lite_mmap,
  433. };
  434. /*
  435. * Format and crop negotiation helpers
  436. */
  437. static const struct fimc_fmt *fimc_lite_try_format(struct fimc_lite *fimc,
  438. u32 *width, u32 *height,
  439. u32 *code, u32 *fourcc, int pad)
  440. {
  441. struct flite_variant *variant = fimc->variant;
  442. const struct fimc_fmt *fmt;
  443. fmt = fimc_lite_find_format(fourcc, code, 0);
  444. if (WARN_ON(!fmt))
  445. return NULL;
  446. if (code)
  447. *code = fmt->mbus_code;
  448. if (fourcc)
  449. *fourcc = fmt->fourcc;
  450. if (pad == FLITE_SD_PAD_SINK) {
  451. v4l_bound_align_image(width, 8, variant->max_width,
  452. ffs(variant->out_width_align) - 1,
  453. height, 0, variant->max_height, 0, 0);
  454. } else {
  455. v4l_bound_align_image(width, 8, fimc->inp_frame.rect.width,
  456. ffs(variant->out_width_align) - 1,
  457. height, 0, fimc->inp_frame.rect.height,
  458. 0, 0);
  459. }
  460. v4l2_dbg(1, debug, &fimc->subdev, "code: 0x%x, %dx%d\n",
  461. code ? *code : 0, *width, *height);
  462. return fmt;
  463. }
  464. static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
  465. {
  466. struct flite_frame *frame = &fimc->inp_frame;
  467. v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
  468. &r->height, 0, frame->f_height, 0, 0);
  469. /* Adjust left/top if cropping rectangle got out of bounds */
  470. r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
  471. r->left = round_down(r->left, fimc->variant->win_hor_offs_align);
  472. r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height);
  473. v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d",
  474. r->left, r->top, r->width, r->height,
  475. frame->f_width, frame->f_height);
  476. }
  477. static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
  478. {
  479. struct flite_frame *frame = &fimc->out_frame;
  480. struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;
  481. /* Scaling is not supported so we enforce compose rectangle size
  482. same as size of the sink crop rectangle. */
  483. r->width = crop_rect->width;
  484. r->height = crop_rect->height;
  485. /* Adjust left/top if the composing rectangle got out of bounds */
  486. r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
  487. r->left = round_down(r->left, fimc->variant->out_hor_offs_align);
  488. r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);
  489. v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d",
  490. r->left, r->top, r->width, r->height,
  491. frame->f_width, frame->f_height);
  492. }
  493. /*
  494. * Video node ioctl operations
  495. */
  496. static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
  497. struct v4l2_capability *cap)
  498. {
  499. strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
  500. cap->bus_info[0] = 0;
  501. cap->card[0] = 0;
  502. cap->capabilities = V4L2_CAP_STREAMING;
  503. return 0;
  504. }
  505. static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv,
  506. struct v4l2_fmtdesc *f)
  507. {
  508. const struct fimc_fmt *fmt;
  509. if (f->index >= ARRAY_SIZE(fimc_lite_formats))
  510. return -EINVAL;
  511. fmt = &fimc_lite_formats[f->index];
  512. strlcpy(f->description, fmt->name, sizeof(f->description));
  513. f->pixelformat = fmt->fourcc;
  514. return 0;
  515. }
  516. static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
  517. struct v4l2_format *f)
  518. {
  519. struct fimc_lite *fimc = video_drvdata(file);
  520. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  521. struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
  522. struct flite_frame *frame = &fimc->out_frame;
  523. const struct fimc_fmt *fmt = fimc->fmt;
  524. plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
  525. plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;
  526. pixm->num_planes = fmt->memplanes;
  527. pixm->pixelformat = fmt->fourcc;
  528. pixm->width = frame->f_width;
  529. pixm->height = frame->f_height;
  530. pixm->field = V4L2_FIELD_NONE;
  531. pixm->colorspace = V4L2_COLORSPACE_JPEG;
  532. return 0;
  533. }
  534. static int fimc_lite_try_fmt(struct fimc_lite *fimc,
  535. struct v4l2_pix_format_mplane *pixm,
  536. const struct fimc_fmt **ffmt)
  537. {
  538. struct flite_variant *variant = fimc->variant;
  539. u32 bpl = pixm->plane_fmt[0].bytesperline;
  540. const struct fimc_fmt *fmt;
  541. fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 0);
  542. if (WARN_ON(fmt == NULL))
  543. return -EINVAL;
  544. if (ffmt)
  545. *ffmt = fmt;
  546. v4l_bound_align_image(&pixm->width, 8, variant->max_width,
  547. ffs(variant->out_width_align) - 1,
  548. &pixm->height, 0, variant->max_height, 0, 0);
  549. if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
  550. pixm->plane_fmt[0].bytesperline = (pixm->width *
  551. fmt->depth[0]) / 8;
  552. if (pixm->plane_fmt[0].sizeimage == 0)
  553. pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
  554. fmt->depth[0]) / 8;
  555. pixm->num_planes = fmt->memplanes;
  556. pixm->pixelformat = fmt->fourcc;
  557. pixm->colorspace = V4L2_COLORSPACE_JPEG;
  558. pixm->field = V4L2_FIELD_NONE;
  559. return 0;
  560. }
  561. static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
  562. struct v4l2_format *f)
  563. {
  564. struct fimc_lite *fimc = video_drvdata(file);
  565. return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
  566. }
  567. static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
  568. struct v4l2_format *f)
  569. {
  570. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  571. struct fimc_lite *fimc = video_drvdata(file);
  572. struct flite_frame *frame = &fimc->out_frame;
  573. const struct fimc_fmt *fmt = NULL;
  574. int ret;
  575. if (vb2_is_busy(&fimc->vb_queue))
  576. return -EBUSY;
  577. ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
  578. if (ret < 0)
  579. return ret;
  580. fimc->fmt = fmt;
  581. fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
  582. pixm->plane_fmt[0].sizeimage);
  583. frame->f_width = pixm->width;
  584. frame->f_height = pixm->height;
  585. return 0;
  586. }
  587. static int fimc_pipeline_validate(struct fimc_lite *fimc)
  588. {
  589. struct v4l2_subdev *sd = &fimc->subdev;
  590. struct v4l2_subdev_format sink_fmt, src_fmt;
  591. struct media_pad *pad;
  592. int ret;
  593. while (1) {
  594. /* Retrieve format at the sink pad */
  595. pad = &sd->entity.pads[0];
  596. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  597. break;
  598. /* Don't call FIMC subdev operation to avoid nested locking */
  599. if (sd == &fimc->subdev) {
  600. struct flite_frame *ff = &fimc->out_frame;
  601. sink_fmt.format.width = ff->f_width;
  602. sink_fmt.format.height = ff->f_height;
  603. sink_fmt.format.code = fimc->fmt->mbus_code;
  604. } else {
  605. sink_fmt.pad = pad->index;
  606. sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  607. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
  608. &sink_fmt);
  609. if (ret < 0 && ret != -ENOIOCTLCMD)
  610. return -EPIPE;
  611. }
  612. /* Retrieve format at the source pad */
  613. pad = media_entity_remote_source(pad);
  614. if (pad == NULL ||
  615. media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  616. break;
  617. sd = media_entity_to_v4l2_subdev(pad->entity);
  618. src_fmt.pad = pad->index;
  619. src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  620. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
  621. if (ret < 0 && ret != -ENOIOCTLCMD)
  622. return -EPIPE;
  623. if (src_fmt.format.width != sink_fmt.format.width ||
  624. src_fmt.format.height != sink_fmt.format.height ||
  625. src_fmt.format.code != sink_fmt.format.code)
  626. return -EPIPE;
  627. }
  628. return 0;
  629. }
  630. static int fimc_lite_streamon(struct file *file, void *priv,
  631. enum v4l2_buf_type type)
  632. {
  633. struct fimc_lite *fimc = video_drvdata(file);
  634. struct v4l2_subdev *sensor = fimc->pipeline.subdevs[IDX_SENSOR];
  635. struct fimc_pipeline *p = &fimc->pipeline;
  636. int ret;
  637. if (fimc_lite_active(fimc))
  638. return -EBUSY;
  639. media_entity_pipeline_start(&sensor->entity, p->m_pipeline);
  640. ret = fimc_pipeline_validate(fimc);
  641. if (ret) {
  642. media_entity_pipeline_stop(&sensor->entity);
  643. return ret;
  644. }
  645. return vb2_streamon(&fimc->vb_queue, type);
  646. }
  647. static int fimc_lite_streamoff(struct file *file, void *priv,
  648. enum v4l2_buf_type type)
  649. {
  650. struct fimc_lite *fimc = video_drvdata(file);
  651. struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
  652. int ret;
  653. ret = vb2_streamoff(&fimc->vb_queue, type);
  654. if (ret == 0)
  655. media_entity_pipeline_stop(&sd->entity);
  656. return ret;
  657. }
  658. static int fimc_lite_reqbufs(struct file *file, void *priv,
  659. struct v4l2_requestbuffers *reqbufs)
  660. {
  661. struct fimc_lite *fimc = video_drvdata(file);
  662. int ret;
  663. reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
  664. ret = vb2_reqbufs(&fimc->vb_queue, reqbufs);
  665. if (!ret < 0)
  666. fimc->reqbufs_count = reqbufs->count;
  667. return ret;
  668. }
  669. static int fimc_lite_querybuf(struct file *file, void *priv,
  670. struct v4l2_buffer *buf)
  671. {
  672. struct fimc_lite *fimc = video_drvdata(file);
  673. return vb2_querybuf(&fimc->vb_queue, buf);
  674. }
  675. static int fimc_lite_qbuf(struct file *file, void *priv,
  676. struct v4l2_buffer *buf)
  677. {
  678. struct fimc_lite *fimc = video_drvdata(file);
  679. return vb2_qbuf(&fimc->vb_queue, buf);
  680. }
  681. static int fimc_lite_dqbuf(struct file *file, void *priv,
  682. struct v4l2_buffer *buf)
  683. {
  684. struct fimc_lite *fimc = video_drvdata(file);
  685. return vb2_dqbuf(&fimc->vb_queue, buf, file->f_flags & O_NONBLOCK);
  686. }
  687. static int fimc_lite_create_bufs(struct file *file, void *priv,
  688. struct v4l2_create_buffers *create)
  689. {
  690. struct fimc_lite *fimc = video_drvdata(file);
  691. return vb2_create_bufs(&fimc->vb_queue, create);
  692. }
  693. static int fimc_lite_prepare_buf(struct file *file, void *priv,
  694. struct v4l2_buffer *b)
  695. {
  696. struct fimc_lite *fimc = video_drvdata(file);
  697. return vb2_prepare_buf(&fimc->vb_queue, b);
  698. }
  699. /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
  700. static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
  701. {
  702. if (a->left < b->left || a->top < b->top)
  703. return 0;
  704. if (a->left + a->width > b->left + b->width)
  705. return 0;
  706. if (a->top + a->height > b->top + b->height)
  707. return 0;
  708. return 1;
  709. }
  710. static int fimc_lite_g_selection(struct file *file, void *fh,
  711. struct v4l2_selection *sel)
  712. {
  713. struct fimc_lite *fimc = video_drvdata(file);
  714. struct flite_frame *f = &fimc->out_frame;
  715. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  716. return -EINVAL;
  717. switch (sel->target) {
  718. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  719. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  720. sel->r.left = 0;
  721. sel->r.top = 0;
  722. sel->r.width = f->f_width;
  723. sel->r.height = f->f_height;
  724. return 0;
  725. case V4L2_SEL_TGT_COMPOSE:
  726. sel->r = f->rect;
  727. return 0;
  728. }
  729. return -EINVAL;
  730. }
  731. static int fimc_lite_s_selection(struct file *file, void *fh,
  732. struct v4l2_selection *sel)
  733. {
  734. struct fimc_lite *fimc = video_drvdata(file);
  735. struct flite_frame *f = &fimc->out_frame;
  736. struct v4l2_rect rect = sel->r;
  737. unsigned long flags;
  738. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
  739. sel->target != V4L2_SEL_TGT_COMPOSE)
  740. return -EINVAL;
  741. fimc_lite_try_compose(fimc, &rect);
  742. if ((sel->flags & V4L2_SEL_FLAG_LE) &&
  743. !enclosed_rectangle(&rect, &sel->r))
  744. return -ERANGE;
  745. if ((sel->flags & V4L2_SEL_FLAG_GE) &&
  746. !enclosed_rectangle(&sel->r, &rect))
  747. return -ERANGE;
  748. sel->r = rect;
  749. spin_lock_irqsave(&fimc->slock, flags);
  750. f->rect = rect;
  751. set_bit(ST_FLITE_CONFIG, &fimc->state);
  752. spin_unlock_irqrestore(&fimc->slock, flags);
  753. return 0;
  754. }
  755. static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
  756. .vidioc_querycap = fimc_vidioc_querycap_capture,
  757. .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane,
  758. .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
  759. .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
  760. .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
  761. .vidioc_g_selection = fimc_lite_g_selection,
  762. .vidioc_s_selection = fimc_lite_s_selection,
  763. .vidioc_reqbufs = fimc_lite_reqbufs,
  764. .vidioc_querybuf = fimc_lite_querybuf,
  765. .vidioc_prepare_buf = fimc_lite_prepare_buf,
  766. .vidioc_create_bufs = fimc_lite_create_bufs,
  767. .vidioc_qbuf = fimc_lite_qbuf,
  768. .vidioc_dqbuf = fimc_lite_dqbuf,
  769. .vidioc_streamon = fimc_lite_streamon,
  770. .vidioc_streamoff = fimc_lite_streamoff,
  771. };
  772. /* Capture subdev media entity operations */
  773. static int fimc_lite_link_setup(struct media_entity *entity,
  774. const struct media_pad *local,
  775. const struct media_pad *remote, u32 flags)
  776. {
  777. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  778. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  779. unsigned int remote_ent_type = media_entity_type(remote->entity);
  780. if (WARN_ON(fimc == NULL))
  781. return 0;
  782. v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x",
  783. __func__, local->entity->name, remote->entity->name,
  784. flags, fimc->source_subdev_grp_id);
  785. switch (local->index) {
  786. case FIMC_SD_PAD_SINK:
  787. if (remote_ent_type != MEDIA_ENT_T_V4L2_SUBDEV)
  788. return -EINVAL;
  789. if (flags & MEDIA_LNK_FL_ENABLED) {
  790. if (fimc->source_subdev_grp_id != 0)
  791. return -EBUSY;
  792. fimc->source_subdev_grp_id = sd->grp_id;
  793. return 0;
  794. }
  795. fimc->source_subdev_grp_id = 0;
  796. break;
  797. case FIMC_SD_PAD_SOURCE:
  798. if (!(flags & MEDIA_LNK_FL_ENABLED)) {
  799. fimc->out_path = FIMC_IO_NONE;
  800. return 0;
  801. }
  802. if (remote_ent_type == MEDIA_ENT_T_V4L2_SUBDEV)
  803. fimc->out_path = FIMC_IO_ISP;
  804. else
  805. fimc->out_path = FIMC_IO_DMA;
  806. break;
  807. default:
  808. v4l2_err(sd, "Invalid pad index\n");
  809. return -EINVAL;
  810. }
  811. return 0;
  812. }
  813. static const struct media_entity_operations fimc_lite_subdev_media_ops = {
  814. .link_setup = fimc_lite_link_setup,
  815. };
  816. static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
  817. struct v4l2_subdev_fh *fh,
  818. struct v4l2_subdev_mbus_code_enum *code)
  819. {
  820. const struct fimc_fmt *fmt;
  821. fmt = fimc_lite_find_format(NULL, NULL, code->index);
  822. if (!fmt)
  823. return -EINVAL;
  824. code->code = fmt->mbus_code;
  825. return 0;
  826. }
  827. static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
  828. struct v4l2_subdev_fh *fh,
  829. struct v4l2_subdev_format *fmt)
  830. {
  831. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  832. struct v4l2_mbus_framefmt *mf = &fmt->format;
  833. struct flite_frame *f = &fimc->out_frame;
  834. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  835. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  836. fmt->format = *mf;
  837. return 0;
  838. }
  839. mf->colorspace = V4L2_COLORSPACE_JPEG;
  840. mutex_lock(&fimc->lock);
  841. mf->code = fimc->fmt->mbus_code;
  842. if (fmt->pad == FLITE_SD_PAD_SINK) {
  843. /* full camera input frame size */
  844. mf->width = f->f_width;
  845. mf->height = f->f_height;
  846. } else {
  847. /* crop size */
  848. mf->width = f->rect.width;
  849. mf->height = f->rect.height;
  850. }
  851. mutex_unlock(&fimc->lock);
  852. return 0;
  853. }
  854. static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
  855. struct v4l2_subdev_fh *fh,
  856. struct v4l2_subdev_format *fmt)
  857. {
  858. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  859. struct v4l2_mbus_framefmt *mf = &fmt->format;
  860. struct flite_frame *sink = &fimc->inp_frame;
  861. const struct fimc_fmt *ffmt;
  862. v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d",
  863. fmt->pad, mf->code, mf->width, mf->height);
  864. mf->colorspace = V4L2_COLORSPACE_JPEG;
  865. mutex_lock(&fimc->lock);
  866. if ((fimc->out_path == FIMC_IO_ISP && sd->entity.stream_count > 0) ||
  867. (fimc->out_path == FIMC_IO_DMA && vb2_is_busy(&fimc->vb_queue))) {
  868. mutex_unlock(&fimc->lock);
  869. return -EBUSY;
  870. }
  871. ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height,
  872. &mf->code, NULL, fmt->pad);
  873. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  874. mf = v4l2_subdev_get_try_format(fh, fmt->pad);
  875. *mf = fmt->format;
  876. mutex_unlock(&fimc->lock);
  877. return 0;
  878. }
  879. if (fmt->pad == FLITE_SD_PAD_SINK) {
  880. sink->f_width = mf->width;
  881. sink->f_height = mf->height;
  882. fimc->fmt = ffmt;
  883. /* Set sink crop rectangle */
  884. sink->rect.width = mf->width;
  885. sink->rect.height = mf->height;
  886. sink->rect.left = 0;
  887. sink->rect.top = 0;
  888. /* Reset source crop rectangle */
  889. fimc->out_frame.rect = sink->rect;
  890. } else {
  891. /* Allow changing format only on sink pad */
  892. mf->code = fimc->fmt->mbus_code;
  893. mf->width = sink->rect.width;
  894. mf->height = sink->rect.height;
  895. }
  896. mutex_unlock(&fimc->lock);
  897. return 0;
  898. }
  899. static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
  900. struct v4l2_subdev_fh *fh,
  901. struct v4l2_subdev_selection *sel)
  902. {
  903. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  904. struct flite_frame *f = &fimc->inp_frame;
  905. if ((sel->target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL &&
  906. sel->target != V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS) ||
  907. sel->pad != FLITE_SD_PAD_SINK)
  908. return -EINVAL;
  909. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  910. sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
  911. return 0;
  912. }
  913. mutex_lock(&fimc->lock);
  914. if (sel->target == V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL) {
  915. sel->r = f->rect;
  916. } else {
  917. sel->r.left = 0;
  918. sel->r.top = 0;
  919. sel->r.width = f->f_width;
  920. sel->r.height = f->f_height;
  921. }
  922. mutex_unlock(&fimc->lock);
  923. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d",
  924. __func__, f->rect.left, f->rect.top, f->rect.width,
  925. f->rect.height, f->f_width, f->f_height);
  926. return 0;
  927. }
  928. static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
  929. struct v4l2_subdev_fh *fh,
  930. struct v4l2_subdev_selection *sel)
  931. {
  932. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  933. struct flite_frame *f = &fimc->inp_frame;
  934. int ret = 0;
  935. if (sel->target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL ||
  936. sel->pad != FLITE_SD_PAD_SINK)
  937. return -EINVAL;
  938. mutex_lock(&fimc->lock);
  939. fimc_lite_try_crop(fimc, &sel->r);
  940. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  941. *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
  942. } else {
  943. unsigned long flags;
  944. spin_lock_irqsave(&fimc->slock, flags);
  945. f->rect = sel->r;
  946. /* Same crop rectangle on the source pad */
  947. fimc->out_frame.rect = sel->r;
  948. set_bit(ST_FLITE_CONFIG, &fimc->state);
  949. spin_unlock_irqrestore(&fimc->slock, flags);
  950. }
  951. mutex_unlock(&fimc->lock);
  952. v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d",
  953. __func__, f->rect.left, f->rect.top, f->rect.width,
  954. f->rect.height, f->f_width, f->f_height);
  955. return ret;
  956. }
  957. static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
  958. {
  959. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  960. if (fimc->out_path == FIMC_IO_DMA)
  961. return -ENOIOCTLCMD;
  962. /* TODO: */
  963. return 0;
  964. }
  965. static int fimc_lite_subdev_s_power(struct v4l2_subdev *sd, int on)
  966. {
  967. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  968. if (fimc->out_path == FIMC_IO_DMA)
  969. return -ENOIOCTLCMD;
  970. /* TODO: */
  971. return 0;
  972. }
  973. static int fimc_lite_log_status(struct v4l2_subdev *sd)
  974. {
  975. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  976. flite_hw_dump_regs(fimc, __func__);
  977. return 0;
  978. }
  979. static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
  980. {
  981. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  982. struct vb2_queue *q = &fimc->vb_queue;
  983. struct video_device *vfd;
  984. int ret;
  985. fimc->fmt = &fimc_lite_formats[0];
  986. fimc->out_path = FIMC_IO_DMA;
  987. vfd = video_device_alloc();
  988. if (!vfd) {
  989. v4l2_err(sd->v4l2_dev, "Failed to allocate video device\n");
  990. return -ENOMEM;
  991. }
  992. snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
  993. fimc->index);
  994. vfd->fops = &fimc_lite_fops;
  995. vfd->ioctl_ops = &fimc_lite_ioctl_ops;
  996. vfd->v4l2_dev = sd->v4l2_dev;
  997. vfd->minor = -1;
  998. vfd->release = video_device_release;
  999. vfd->lock = &fimc->lock;
  1000. fimc->vfd = vfd;
  1001. fimc->ref_count = 0;
  1002. fimc->reqbufs_count = 0;
  1003. INIT_LIST_HEAD(&fimc->pending_buf_q);
  1004. INIT_LIST_HEAD(&fimc->active_buf_q);
  1005. memset(q, 0, sizeof(*q));
  1006. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1007. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1008. q->ops = &fimc_lite_qops;
  1009. q->mem_ops = &vb2_dma_contig_memops;
  1010. q->buf_struct_size = sizeof(struct flite_buffer);
  1011. q->drv_priv = fimc;
  1012. vb2_queue_init(q);
  1013. fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
  1014. ret = media_entity_init(&vfd->entity, 1, &fimc->vd_pad, 0);
  1015. if (ret)
  1016. goto err;
  1017. video_set_drvdata(vfd, fimc);
  1018. ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  1019. if (ret)
  1020. goto err_vd;
  1021. v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
  1022. vfd->name, video_device_node_name(vfd));
  1023. return 0;
  1024. err_vd:
  1025. media_entity_cleanup(&vfd->entity);
  1026. err:
  1027. video_device_release(vfd);
  1028. return ret;
  1029. }
  1030. static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
  1031. {
  1032. struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
  1033. if (fimc == NULL)
  1034. return;
  1035. if (fimc->vfd) {
  1036. video_unregister_device(fimc->vfd);
  1037. media_entity_cleanup(&fimc->vfd->entity);
  1038. fimc->vfd = NULL;
  1039. }
  1040. }
  1041. static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
  1042. .registered = fimc_lite_subdev_registered,
  1043. .unregistered = fimc_lite_subdev_unregistered,
  1044. };
  1045. static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
  1046. .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
  1047. .get_selection = fimc_lite_subdev_get_selection,
  1048. .set_selection = fimc_lite_subdev_set_selection,
  1049. .get_fmt = fimc_lite_subdev_get_fmt,
  1050. .set_fmt = fimc_lite_subdev_set_fmt,
  1051. };
  1052. static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
  1053. .s_stream = fimc_lite_subdev_s_stream,
  1054. };
  1055. static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
  1056. .s_power = fimc_lite_subdev_s_power,
  1057. .log_status = fimc_lite_log_status,
  1058. };
  1059. static struct v4l2_subdev_ops fimc_lite_subdev_ops = {
  1060. .core = &fimc_lite_core_ops,
  1061. .video = &fimc_lite_subdev_video_ops,
  1062. .pad = &fimc_lite_subdev_pad_ops,
  1063. };
  1064. static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
  1065. {
  1066. struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
  1067. ctrl_handler);
  1068. set_bit(ST_FLITE_CONFIG, &fimc->state);
  1069. return 0;
  1070. }
  1071. static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
  1072. .s_ctrl = fimc_lite_s_ctrl,
  1073. };
  1074. static const struct v4l2_ctrl_config fimc_lite_ctrl = {
  1075. .ops = &fimc_lite_ctrl_ops,
  1076. .id = V4L2_CTRL_CLASS_USER | 0x1001,
  1077. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1078. .name = "Test Pattern 640x480",
  1079. };
  1080. static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
  1081. {
  1082. struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
  1083. struct v4l2_subdev *sd = &fimc->subdev;
  1084. int ret;
  1085. v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
  1086. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
  1087. snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);
  1088. fimc->subdev_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
  1089. fimc->subdev_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
  1090. ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
  1091. fimc->subdev_pads, 0);
  1092. if (ret)
  1093. return ret;
  1094. v4l2_ctrl_handler_init(handler, 1);
  1095. fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
  1096. NULL);
  1097. if (handler->error) {
  1098. media_entity_cleanup(&sd->entity);
  1099. return handler->error;
  1100. }
  1101. sd->ctrl_handler = handler;
  1102. sd->internal_ops = &fimc_lite_subdev_internal_ops;
  1103. sd->entity.ops = &fimc_lite_subdev_media_ops;
  1104. v4l2_set_subdevdata(sd, fimc);
  1105. return 0;
  1106. }
  1107. static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
  1108. {
  1109. struct v4l2_subdev *sd = &fimc->subdev;
  1110. v4l2_device_unregister_subdev(sd);
  1111. media_entity_cleanup(&sd->entity);
  1112. v4l2_ctrl_handler_free(&fimc->ctrl_handler);
  1113. v4l2_set_subdevdata(sd, NULL);
  1114. }
  1115. static void fimc_lite_clk_put(struct fimc_lite *fimc)
  1116. {
  1117. if (IS_ERR_OR_NULL(fimc->clock))
  1118. return;
  1119. clk_unprepare(fimc->clock);
  1120. clk_put(fimc->clock);
  1121. fimc->clock = NULL;
  1122. }
  1123. static int fimc_lite_clk_get(struct fimc_lite *fimc)
  1124. {
  1125. int ret;
  1126. fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
  1127. if (IS_ERR(fimc->clock))
  1128. return PTR_ERR(fimc->clock);
  1129. ret = clk_prepare(fimc->clock);
  1130. if (ret < 0) {
  1131. clk_put(fimc->clock);
  1132. fimc->clock = NULL;
  1133. }
  1134. return ret;
  1135. }
  1136. static int __devinit fimc_lite_probe(struct platform_device *pdev)
  1137. {
  1138. struct flite_drvdata *drv_data = fimc_lite_get_drvdata(pdev);
  1139. struct fimc_lite *fimc;
  1140. struct resource *res;
  1141. int ret;
  1142. fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
  1143. if (!fimc)
  1144. return -ENOMEM;
  1145. fimc->index = pdev->id;
  1146. fimc->variant = drv_data->variant[fimc->index];
  1147. fimc->pdev = pdev;
  1148. init_waitqueue_head(&fimc->irq_queue);
  1149. spin_lock_init(&fimc->slock);
  1150. mutex_init(&fimc->lock);
  1151. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1152. fimc->regs = devm_request_and_ioremap(&pdev->dev, res);
  1153. if (fimc->regs == NULL) {
  1154. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  1155. return -ENOENT;
  1156. }
  1157. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1158. if (res == NULL) {
  1159. dev_err(&pdev->dev, "Failed to get IRQ resource\n");
  1160. return -ENXIO;
  1161. }
  1162. ret = fimc_lite_clk_get(fimc);
  1163. if (ret)
  1164. return ret;
  1165. ret = devm_request_irq(&pdev->dev, res->start, flite_irq_handler,
  1166. 0, dev_name(&pdev->dev), fimc);
  1167. if (ret) {
  1168. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  1169. goto err_clk;
  1170. }
  1171. /* The video node will be created within the subdev's registered() op */
  1172. ret = fimc_lite_create_capture_subdev(fimc);
  1173. if (ret)
  1174. goto err_clk;
  1175. platform_set_drvdata(pdev, fimc);
  1176. pm_runtime_enable(&pdev->dev);
  1177. ret = pm_runtime_get_sync(&pdev->dev);
  1178. if (ret < 0)
  1179. goto err_sd;
  1180. fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1181. if (IS_ERR(fimc->alloc_ctx)) {
  1182. ret = PTR_ERR(fimc->alloc_ctx);
  1183. goto err_pm;
  1184. }
  1185. pm_runtime_put(&pdev->dev);
  1186. dev_dbg(&pdev->dev, "FIMC-LITE.%d registered successfully\n",
  1187. fimc->index);
  1188. return 0;
  1189. err_pm:
  1190. pm_runtime_put(&pdev->dev);
  1191. err_sd:
  1192. fimc_lite_unregister_capture_subdev(fimc);
  1193. err_clk:
  1194. fimc_lite_clk_put(fimc);
  1195. return ret;
  1196. }
  1197. static int fimc_lite_runtime_resume(struct device *dev)
  1198. {
  1199. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1200. clk_enable(fimc->clock);
  1201. return 0;
  1202. }
  1203. static int fimc_lite_runtime_suspend(struct device *dev)
  1204. {
  1205. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1206. clk_disable(fimc->clock);
  1207. return 0;
  1208. }
  1209. #ifdef CONFIG_PM_SLEEP
  1210. static int fimc_lite_resume(struct device *dev)
  1211. {
  1212. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1213. struct flite_buffer *buf;
  1214. unsigned long flags;
  1215. int i;
  1216. spin_lock_irqsave(&fimc->slock, flags);
  1217. if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
  1218. !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
  1219. spin_unlock_irqrestore(&fimc->slock, flags);
  1220. return 0;
  1221. }
  1222. flite_hw_reset(fimc);
  1223. spin_unlock_irqrestore(&fimc->slock, flags);
  1224. if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
  1225. return 0;
  1226. INIT_LIST_HEAD(&fimc->active_buf_q);
  1227. fimc_pipeline_initialize(&fimc->pipeline, &fimc->vfd->entity, false);
  1228. fimc_lite_hw_init(fimc);
  1229. clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
  1230. for (i = 0; i < fimc->reqbufs_count; i++) {
  1231. if (list_empty(&fimc->pending_buf_q))
  1232. break;
  1233. buf = fimc_lite_pending_queue_pop(fimc);
  1234. buffer_queue(&buf->vb);
  1235. }
  1236. return 0;
  1237. }
  1238. static int fimc_lite_suspend(struct device *dev)
  1239. {
  1240. struct fimc_lite *fimc = dev_get_drvdata(dev);
  1241. bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
  1242. int ret;
  1243. if (test_and_set_bit(ST_LPM, &fimc->state))
  1244. return 0;
  1245. ret = fimc_lite_stop_capture(fimc, suspend);
  1246. if (ret)
  1247. return ret;
  1248. return fimc_pipeline_shutdown(&fimc->pipeline);
  1249. }
  1250. #endif /* CONFIG_PM_SLEEP */
  1251. static int __devexit fimc_lite_remove(struct platform_device *pdev)
  1252. {
  1253. struct fimc_lite *fimc = platform_get_drvdata(pdev);
  1254. struct device *dev = &pdev->dev;
  1255. pm_runtime_disable(dev);
  1256. pm_runtime_set_suspended(dev);
  1257. fimc_lite_unregister_capture_subdev(fimc);
  1258. vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
  1259. fimc_lite_clk_put(fimc);
  1260. dev_info(dev, "Driver unloaded\n");
  1261. return 0;
  1262. }
  1263. static struct flite_variant fimc_lite0_variant_exynos4 = {
  1264. .max_width = 8192,
  1265. .max_height = 8192,
  1266. .out_width_align = 8,
  1267. .win_hor_offs_align = 2,
  1268. .out_hor_offs_align = 8,
  1269. };
  1270. /* EXYNOS4212, EXYNOS4412 */
  1271. static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
  1272. .variant = {
  1273. [0] = &fimc_lite0_variant_exynos4,
  1274. [1] = &fimc_lite0_variant_exynos4,
  1275. },
  1276. };
  1277. static struct platform_device_id fimc_lite_driver_ids[] = {
  1278. {
  1279. .name = "exynos-fimc-lite",
  1280. .driver_data = (unsigned long)&fimc_lite_drvdata_exynos4,
  1281. },
  1282. { /* sentinel */ },
  1283. };
  1284. MODULE_DEVICE_TABLE(platform, fimc_lite_driver_ids);
  1285. static const struct dev_pm_ops fimc_lite_pm_ops = {
  1286. SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
  1287. SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
  1288. NULL)
  1289. };
  1290. static struct platform_driver fimc_lite_driver = {
  1291. .probe = fimc_lite_probe,
  1292. .remove = __devexit_p(fimc_lite_remove),
  1293. .id_table = fimc_lite_driver_ids,
  1294. .driver = {
  1295. .name = FIMC_LITE_DRV_NAME,
  1296. .owner = THIS_MODULE,
  1297. .pm = &fimc_lite_pm_ops,
  1298. }
  1299. };
  1300. module_platform_driver(fimc_lite_driver);
  1301. MODULE_LICENSE("GPL");
  1302. MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);