fimc-core.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. /*
  2. * Samsung S5P/EXYNOS4 SoC series camera interface (video postprocessor) driver
  3. *
  4. * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
  5. * Contact: 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 as published
  9. * by the Free Software Foundation, either version 2 of the License,
  10. * or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.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/pm_runtime.h>
  21. #include <linux/list.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <media/videobuf2-core.h>
  27. #include <media/videobuf2-dma-contig.h>
  28. #include "fimc-core.h"
  29. #include "fimc-mdevice.h"
  30. static char *fimc_clocks[MAX_FIMC_CLOCKS] = {
  31. "sclk_fimc", "fimc"
  32. };
  33. static struct fimc_fmt fimc_formats[] = {
  34. {
  35. .name = "RGB565",
  36. .fourcc = V4L2_PIX_FMT_RGB565X,
  37. .depth = { 16 },
  38. .color = S5P_FIMC_RGB565,
  39. .memplanes = 1,
  40. .colplanes = 1,
  41. .flags = FMT_FLAGS_M2M,
  42. }, {
  43. .name = "BGR666",
  44. .fourcc = V4L2_PIX_FMT_BGR666,
  45. .depth = { 32 },
  46. .color = S5P_FIMC_RGB666,
  47. .memplanes = 1,
  48. .colplanes = 1,
  49. .flags = FMT_FLAGS_M2M,
  50. }, {
  51. .name = "XRGB-8-8-8-8, 32 bpp",
  52. .fourcc = V4L2_PIX_FMT_RGB32,
  53. .depth = { 32 },
  54. .color = S5P_FIMC_RGB888,
  55. .memplanes = 1,
  56. .colplanes = 1,
  57. .flags = FMT_FLAGS_M2M,
  58. }, {
  59. .name = "YUV 4:2:2 packed, YCbYCr",
  60. .fourcc = V4L2_PIX_FMT_YUYV,
  61. .depth = { 16 },
  62. .color = S5P_FIMC_YCBYCR422,
  63. .memplanes = 1,
  64. .colplanes = 1,
  65. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  66. .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
  67. }, {
  68. .name = "YUV 4:2:2 packed, CbYCrY",
  69. .fourcc = V4L2_PIX_FMT_UYVY,
  70. .depth = { 16 },
  71. .color = S5P_FIMC_CBYCRY422,
  72. .memplanes = 1,
  73. .colplanes = 1,
  74. .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
  75. .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
  76. }, {
  77. .name = "YUV 4:2:2 packed, CrYCbY",
  78. .fourcc = V4L2_PIX_FMT_VYUY,
  79. .depth = { 16 },
  80. .color = S5P_FIMC_CRYCBY422,
  81. .memplanes = 1,
  82. .colplanes = 1,
  83. .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
  84. .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
  85. }, {
  86. .name = "YUV 4:2:2 packed, YCrYCb",
  87. .fourcc = V4L2_PIX_FMT_YVYU,
  88. .depth = { 16 },
  89. .color = S5P_FIMC_YCRYCB422,
  90. .memplanes = 1,
  91. .colplanes = 1,
  92. .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
  93. .flags = FMT_FLAGS_M2M | FMT_FLAGS_CAM,
  94. }, {
  95. .name = "YUV 4:2:2 planar, Y/Cb/Cr",
  96. .fourcc = V4L2_PIX_FMT_YUV422P,
  97. .depth = { 12 },
  98. .color = S5P_FIMC_YCBYCR422,
  99. .memplanes = 1,
  100. .colplanes = 3,
  101. .flags = FMT_FLAGS_M2M,
  102. }, {
  103. .name = "YUV 4:2:2 planar, Y/CbCr",
  104. .fourcc = V4L2_PIX_FMT_NV16,
  105. .depth = { 16 },
  106. .color = S5P_FIMC_YCBYCR422,
  107. .memplanes = 1,
  108. .colplanes = 2,
  109. .flags = FMT_FLAGS_M2M,
  110. }, {
  111. .name = "YUV 4:2:2 planar, Y/CrCb",
  112. .fourcc = V4L2_PIX_FMT_NV61,
  113. .depth = { 16 },
  114. .color = S5P_FIMC_YCRYCB422,
  115. .memplanes = 1,
  116. .colplanes = 2,
  117. .flags = FMT_FLAGS_M2M,
  118. }, {
  119. .name = "YUV 4:2:0 planar, YCbCr",
  120. .fourcc = V4L2_PIX_FMT_YUV420,
  121. .depth = { 12 },
  122. .color = S5P_FIMC_YCBCR420,
  123. .memplanes = 1,
  124. .colplanes = 3,
  125. .flags = FMT_FLAGS_M2M,
  126. }, {
  127. .name = "YUV 4:2:0 planar, Y/CbCr",
  128. .fourcc = V4L2_PIX_FMT_NV12,
  129. .depth = { 12 },
  130. .color = S5P_FIMC_YCBCR420,
  131. .memplanes = 1,
  132. .colplanes = 2,
  133. .flags = FMT_FLAGS_M2M,
  134. }, {
  135. .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr",
  136. .fourcc = V4L2_PIX_FMT_NV12M,
  137. .color = S5P_FIMC_YCBCR420,
  138. .depth = { 8, 4 },
  139. .memplanes = 2,
  140. .colplanes = 2,
  141. .flags = FMT_FLAGS_M2M,
  142. }, {
  143. .name = "YUV 4:2:0 non-contiguous 3-planar, Y/Cb/Cr",
  144. .fourcc = V4L2_PIX_FMT_YUV420M,
  145. .color = S5P_FIMC_YCBCR420,
  146. .depth = { 8, 2, 2 },
  147. .memplanes = 3,
  148. .colplanes = 3,
  149. .flags = FMT_FLAGS_M2M,
  150. }, {
  151. .name = "YUV 4:2:0 non-contiguous 2-planar, Y/CbCr, tiled",
  152. .fourcc = V4L2_PIX_FMT_NV12MT,
  153. .color = S5P_FIMC_YCBCR420,
  154. .depth = { 8, 4 },
  155. .memplanes = 2,
  156. .colplanes = 2,
  157. .flags = FMT_FLAGS_M2M,
  158. }, {
  159. .name = "JPEG encoded data",
  160. .fourcc = V4L2_PIX_FMT_JPEG,
  161. .color = S5P_FIMC_JPEG,
  162. .depth = { 8 },
  163. .memplanes = 1,
  164. .colplanes = 1,
  165. .mbus_code = V4L2_MBUS_FMT_JPEG_1X8,
  166. .flags = FMT_FLAGS_CAM,
  167. },
  168. };
  169. int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
  170. int dw, int dh, int rotation)
  171. {
  172. if (rotation == 90 || rotation == 270)
  173. swap(dw, dh);
  174. if (!ctx->scaler.enabled)
  175. return (sw == dw && sh == dh) ? 0 : -EINVAL;
  176. if ((sw >= SCALER_MAX_HRATIO * dw) || (sh >= SCALER_MAX_VRATIO * dh))
  177. return -EINVAL;
  178. return 0;
  179. }
  180. static int fimc_get_scaler_factor(u32 src, u32 tar, u32 *ratio, u32 *shift)
  181. {
  182. u32 sh = 6;
  183. if (src >= 64 * tar)
  184. return -EINVAL;
  185. while (sh--) {
  186. u32 tmp = 1 << sh;
  187. if (src >= tar * tmp) {
  188. *shift = sh, *ratio = tmp;
  189. return 0;
  190. }
  191. }
  192. *shift = 0, *ratio = 1;
  193. return 0;
  194. }
  195. int fimc_set_scaler_info(struct fimc_ctx *ctx)
  196. {
  197. struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
  198. struct device *dev = &ctx->fimc_dev->pdev->dev;
  199. struct fimc_scaler *sc = &ctx->scaler;
  200. struct fimc_frame *s_frame = &ctx->s_frame;
  201. struct fimc_frame *d_frame = &ctx->d_frame;
  202. int tx, ty, sx, sy;
  203. int ret;
  204. if (ctx->rotation == 90 || ctx->rotation == 270) {
  205. ty = d_frame->width;
  206. tx = d_frame->height;
  207. } else {
  208. tx = d_frame->width;
  209. ty = d_frame->height;
  210. }
  211. if (tx <= 0 || ty <= 0) {
  212. dev_err(dev, "Invalid target size: %dx%d", tx, ty);
  213. return -EINVAL;
  214. }
  215. sx = s_frame->width;
  216. sy = s_frame->height;
  217. if (sx <= 0 || sy <= 0) {
  218. dev_err(dev, "Invalid source size: %dx%d", sx, sy);
  219. return -EINVAL;
  220. }
  221. sc->real_width = sx;
  222. sc->real_height = sy;
  223. ret = fimc_get_scaler_factor(sx, tx, &sc->pre_hratio, &sc->hfactor);
  224. if (ret)
  225. return ret;
  226. ret = fimc_get_scaler_factor(sy, ty, &sc->pre_vratio, &sc->vfactor);
  227. if (ret)
  228. return ret;
  229. sc->pre_dst_width = sx / sc->pre_hratio;
  230. sc->pre_dst_height = sy / sc->pre_vratio;
  231. if (variant->has_mainscaler_ext) {
  232. sc->main_hratio = (sx << 14) / (tx << sc->hfactor);
  233. sc->main_vratio = (sy << 14) / (ty << sc->vfactor);
  234. } else {
  235. sc->main_hratio = (sx << 8) / (tx << sc->hfactor);
  236. sc->main_vratio = (sy << 8) / (ty << sc->vfactor);
  237. }
  238. sc->scaleup_h = (tx >= sx) ? 1 : 0;
  239. sc->scaleup_v = (ty >= sy) ? 1 : 0;
  240. /* check to see if input and output size/format differ */
  241. if (s_frame->fmt->color == d_frame->fmt->color
  242. && s_frame->width == d_frame->width
  243. && s_frame->height == d_frame->height)
  244. sc->copy_mode = 1;
  245. else
  246. sc->copy_mode = 0;
  247. return 0;
  248. }
  249. static void fimc_m2m_job_finish(struct fimc_ctx *ctx, int vb_state)
  250. {
  251. struct vb2_buffer *src_vb, *dst_vb;
  252. if (!ctx || !ctx->m2m_ctx)
  253. return;
  254. src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  255. dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  256. if (src_vb && dst_vb) {
  257. v4l2_m2m_buf_done(src_vb, vb_state);
  258. v4l2_m2m_buf_done(dst_vb, vb_state);
  259. v4l2_m2m_job_finish(ctx->fimc_dev->m2m.m2m_dev,
  260. ctx->m2m_ctx);
  261. }
  262. }
  263. /* Complete the transaction which has been scheduled for execution. */
  264. static int fimc_m2m_shutdown(struct fimc_ctx *ctx)
  265. {
  266. struct fimc_dev *fimc = ctx->fimc_dev;
  267. int ret;
  268. if (!fimc_m2m_pending(fimc))
  269. return 0;
  270. fimc_ctx_state_lock_set(FIMC_CTX_SHUT, ctx);
  271. ret = wait_event_timeout(fimc->irq_queue,
  272. !fimc_ctx_state_is_set(FIMC_CTX_SHUT, ctx),
  273. FIMC_SHUTDOWN_TIMEOUT);
  274. return ret == 0 ? -ETIMEDOUT : ret;
  275. }
  276. static int start_streaming(struct vb2_queue *q, unsigned int count)
  277. {
  278. struct fimc_ctx *ctx = q->drv_priv;
  279. int ret;
  280. ret = pm_runtime_get_sync(&ctx->fimc_dev->pdev->dev);
  281. return ret > 0 ? 0 : ret;
  282. }
  283. static int stop_streaming(struct vb2_queue *q)
  284. {
  285. struct fimc_ctx *ctx = q->drv_priv;
  286. int ret;
  287. ret = fimc_m2m_shutdown(ctx);
  288. if (ret == -ETIMEDOUT)
  289. fimc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
  290. pm_runtime_put(&ctx->fimc_dev->pdev->dev);
  291. return 0;
  292. }
  293. void fimc_capture_irq_handler(struct fimc_dev *fimc, bool final)
  294. {
  295. struct fimc_vid_cap *cap = &fimc->vid_cap;
  296. struct fimc_vid_buffer *v_buf;
  297. struct timeval *tv;
  298. struct timespec ts;
  299. if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
  300. wake_up(&fimc->irq_queue);
  301. return;
  302. }
  303. if (!list_empty(&cap->active_buf_q) &&
  304. test_bit(ST_CAPT_RUN, &fimc->state) && final) {
  305. ktime_get_real_ts(&ts);
  306. v_buf = fimc_active_queue_pop(cap);
  307. tv = &v_buf->vb.v4l2_buf.timestamp;
  308. tv->tv_sec = ts.tv_sec;
  309. tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  310. v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
  311. vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
  312. }
  313. if (!list_empty(&cap->pending_buf_q)) {
  314. v_buf = fimc_pending_queue_pop(cap);
  315. fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
  316. v_buf->index = cap->buf_index;
  317. /* Move the buffer to the capture active queue */
  318. fimc_active_queue_add(cap, v_buf);
  319. dbg("next frame: %d, done frame: %d",
  320. fimc_hw_get_frame_index(fimc), v_buf->index);
  321. if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
  322. cap->buf_index = 0;
  323. }
  324. if (cap->active_buf_cnt == 0) {
  325. if (final)
  326. clear_bit(ST_CAPT_RUN, &fimc->state);
  327. if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
  328. cap->buf_index = 0;
  329. } else {
  330. set_bit(ST_CAPT_RUN, &fimc->state);
  331. }
  332. fimc_capture_config_update(cap->ctx);
  333. dbg("frame: %d, active_buf_cnt: %d",
  334. fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
  335. }
  336. static irqreturn_t fimc_irq_handler(int irq, void *priv)
  337. {
  338. struct fimc_dev *fimc = priv;
  339. struct fimc_vid_cap *cap = &fimc->vid_cap;
  340. struct fimc_ctx *ctx;
  341. fimc_hw_clear_irq(fimc);
  342. spin_lock(&fimc->slock);
  343. if (test_and_clear_bit(ST_M2M_PEND, &fimc->state)) {
  344. if (test_and_clear_bit(ST_M2M_SUSPENDING, &fimc->state)) {
  345. set_bit(ST_M2M_SUSPENDED, &fimc->state);
  346. wake_up(&fimc->irq_queue);
  347. goto out;
  348. }
  349. ctx = v4l2_m2m_get_curr_priv(fimc->m2m.m2m_dev);
  350. if (ctx != NULL) {
  351. spin_unlock(&fimc->slock);
  352. fimc_m2m_job_finish(ctx, VB2_BUF_STATE_DONE);
  353. spin_lock(&ctx->slock);
  354. if (ctx->state & FIMC_CTX_SHUT) {
  355. ctx->state &= ~FIMC_CTX_SHUT;
  356. wake_up(&fimc->irq_queue);
  357. }
  358. spin_unlock(&ctx->slock);
  359. }
  360. return IRQ_HANDLED;
  361. } else if (test_bit(ST_CAPT_PEND, &fimc->state)) {
  362. fimc_capture_irq_handler(fimc,
  363. !test_bit(ST_CAPT_JPEG, &fimc->state));
  364. if (cap->active_buf_cnt == 1) {
  365. fimc_deactivate_capture(fimc);
  366. clear_bit(ST_CAPT_STREAM, &fimc->state);
  367. }
  368. }
  369. out:
  370. spin_unlock(&fimc->slock);
  371. return IRQ_HANDLED;
  372. }
  373. /* The color format (colplanes, memplanes) must be already configured. */
  374. int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
  375. struct fimc_frame *frame, struct fimc_addr *paddr)
  376. {
  377. int ret = 0;
  378. u32 pix_size;
  379. if (vb == NULL || frame == NULL)
  380. return -EINVAL;
  381. pix_size = frame->width * frame->height;
  382. dbg("memplanes= %d, colplanes= %d, pix_size= %d",
  383. frame->fmt->memplanes, frame->fmt->colplanes, pix_size);
  384. paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
  385. if (frame->fmt->memplanes == 1) {
  386. switch (frame->fmt->colplanes) {
  387. case 1:
  388. paddr->cb = 0;
  389. paddr->cr = 0;
  390. break;
  391. case 2:
  392. /* decompose Y into Y/Cb */
  393. paddr->cb = (u32)(paddr->y + pix_size);
  394. paddr->cr = 0;
  395. break;
  396. case 3:
  397. paddr->cb = (u32)(paddr->y + pix_size);
  398. /* decompose Y into Y/Cb/Cr */
  399. if (S5P_FIMC_YCBCR420 == frame->fmt->color)
  400. paddr->cr = (u32)(paddr->cb
  401. + (pix_size >> 2));
  402. else /* 422 */
  403. paddr->cr = (u32)(paddr->cb
  404. + (pix_size >> 1));
  405. break;
  406. default:
  407. return -EINVAL;
  408. }
  409. } else {
  410. if (frame->fmt->memplanes >= 2)
  411. paddr->cb = vb2_dma_contig_plane_dma_addr(vb, 1);
  412. if (frame->fmt->memplanes == 3)
  413. paddr->cr = vb2_dma_contig_plane_dma_addr(vb, 2);
  414. }
  415. dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
  416. paddr->y, paddr->cb, paddr->cr, ret);
  417. return ret;
  418. }
  419. /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
  420. void fimc_set_yuv_order(struct fimc_ctx *ctx)
  421. {
  422. /* The one only mode supported in SoC. */
  423. ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
  424. ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
  425. /* Set order for 1 plane input formats. */
  426. switch (ctx->s_frame.fmt->color) {
  427. case S5P_FIMC_YCRYCB422:
  428. ctx->in_order_1p = S5P_MSCTRL_ORDER422_CBYCRY;
  429. break;
  430. case S5P_FIMC_CBYCRY422:
  431. ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCRYCB;
  432. break;
  433. case S5P_FIMC_CRYCBY422:
  434. ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCBYCR;
  435. break;
  436. case S5P_FIMC_YCBYCR422:
  437. default:
  438. ctx->in_order_1p = S5P_MSCTRL_ORDER422_CRYCBY;
  439. break;
  440. }
  441. dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
  442. switch (ctx->d_frame.fmt->color) {
  443. case S5P_FIMC_YCRYCB422:
  444. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CBYCRY;
  445. break;
  446. case S5P_FIMC_CBYCRY422:
  447. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCRYCB;
  448. break;
  449. case S5P_FIMC_CRYCBY422:
  450. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCBYCR;
  451. break;
  452. case S5P_FIMC_YCBYCR422:
  453. default:
  454. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CRYCBY;
  455. break;
  456. }
  457. dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
  458. }
  459. void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
  460. {
  461. struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
  462. u32 i, depth = 0;
  463. for (i = 0; i < f->fmt->colplanes; i++)
  464. depth += f->fmt->depth[i];
  465. f->dma_offset.y_h = f->offs_h;
  466. if (!variant->pix_hoff)
  467. f->dma_offset.y_h *= (depth >> 3);
  468. f->dma_offset.y_v = f->offs_v;
  469. f->dma_offset.cb_h = f->offs_h;
  470. f->dma_offset.cb_v = f->offs_v;
  471. f->dma_offset.cr_h = f->offs_h;
  472. f->dma_offset.cr_v = f->offs_v;
  473. if (!variant->pix_hoff) {
  474. if (f->fmt->colplanes == 3) {
  475. f->dma_offset.cb_h >>= 1;
  476. f->dma_offset.cr_h >>= 1;
  477. }
  478. if (f->fmt->color == S5P_FIMC_YCBCR420) {
  479. f->dma_offset.cb_v >>= 1;
  480. f->dma_offset.cr_v >>= 1;
  481. }
  482. }
  483. dbg("in_offset: color= %d, y_h= %d, y_v= %d",
  484. f->fmt->color, f->dma_offset.y_h, f->dma_offset.y_v);
  485. }
  486. /**
  487. * fimc_prepare_config - check dimensions, operation and color mode
  488. * and pre-calculate offset and the scaling coefficients.
  489. *
  490. * @ctx: hardware context information
  491. * @flags: flags indicating which parameters to check/update
  492. *
  493. * Return: 0 if dimensions are valid or non zero otherwise.
  494. */
  495. int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags)
  496. {
  497. struct fimc_frame *s_frame, *d_frame;
  498. struct vb2_buffer *vb = NULL;
  499. int ret = 0;
  500. s_frame = &ctx->s_frame;
  501. d_frame = &ctx->d_frame;
  502. if (flags & FIMC_PARAMS) {
  503. /* Prepare the DMA offset ratios for scaler. */
  504. fimc_prepare_dma_offset(ctx, &ctx->s_frame);
  505. fimc_prepare_dma_offset(ctx, &ctx->d_frame);
  506. if (s_frame->height > (SCALER_MAX_VRATIO * d_frame->height) ||
  507. s_frame->width > (SCALER_MAX_HRATIO * d_frame->width)) {
  508. err("out of scaler range");
  509. return -EINVAL;
  510. }
  511. fimc_set_yuv_order(ctx);
  512. }
  513. if (flags & FIMC_SRC_ADDR) {
  514. vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  515. ret = fimc_prepare_addr(ctx, vb, s_frame, &s_frame->paddr);
  516. if (ret)
  517. return ret;
  518. }
  519. if (flags & FIMC_DST_ADDR) {
  520. vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  521. ret = fimc_prepare_addr(ctx, vb, d_frame, &d_frame->paddr);
  522. }
  523. return ret;
  524. }
  525. static void fimc_dma_run(void *priv)
  526. {
  527. struct fimc_ctx *ctx = priv;
  528. struct fimc_dev *fimc;
  529. unsigned long flags;
  530. u32 ret;
  531. if (WARN(!ctx, "null hardware context\n"))
  532. return;
  533. fimc = ctx->fimc_dev;
  534. spin_lock_irqsave(&fimc->slock, flags);
  535. set_bit(ST_M2M_PEND, &fimc->state);
  536. spin_lock(&ctx->slock);
  537. ctx->state |= (FIMC_SRC_ADDR | FIMC_DST_ADDR);
  538. ret = fimc_prepare_config(ctx, ctx->state);
  539. if (ret)
  540. goto dma_unlock;
  541. /* Reconfigure hardware if the context has changed. */
  542. if (fimc->m2m.ctx != ctx) {
  543. ctx->state |= FIMC_PARAMS;
  544. fimc->m2m.ctx = ctx;
  545. }
  546. fimc_hw_set_input_addr(fimc, &ctx->s_frame.paddr);
  547. if (ctx->state & FIMC_PARAMS) {
  548. fimc_hw_set_input_path(ctx);
  549. fimc_hw_set_in_dma(ctx);
  550. ret = fimc_set_scaler_info(ctx);
  551. if (ret) {
  552. spin_unlock(&fimc->slock);
  553. goto dma_unlock;
  554. }
  555. fimc_hw_set_prescaler(ctx);
  556. fimc_hw_set_mainscaler(ctx);
  557. fimc_hw_set_target_format(ctx);
  558. fimc_hw_set_rotation(ctx);
  559. fimc_hw_set_effect(ctx, false);
  560. }
  561. fimc_hw_set_output_path(ctx);
  562. if (ctx->state & (FIMC_DST_ADDR | FIMC_PARAMS))
  563. fimc_hw_set_output_addr(fimc, &ctx->d_frame.paddr, -1);
  564. if (ctx->state & FIMC_PARAMS)
  565. fimc_hw_set_out_dma(ctx);
  566. fimc_activate_capture(ctx);
  567. ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
  568. FIMC_SRC_FMT | FIMC_DST_FMT);
  569. fimc_hw_activate_input_dma(fimc, true);
  570. dma_unlock:
  571. spin_unlock(&ctx->slock);
  572. spin_unlock_irqrestore(&fimc->slock, flags);
  573. }
  574. static void fimc_job_abort(void *priv)
  575. {
  576. fimc_m2m_shutdown(priv);
  577. }
  578. static int fimc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  579. unsigned int *num_buffers, unsigned int *num_planes,
  580. unsigned int sizes[], void *allocators[])
  581. {
  582. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  583. struct fimc_frame *f;
  584. int i;
  585. f = ctx_get_frame(ctx, vq->type);
  586. if (IS_ERR(f))
  587. return PTR_ERR(f);
  588. /*
  589. * Return number of non-contigous planes (plane buffers)
  590. * depending on the configured color format.
  591. */
  592. if (!f->fmt)
  593. return -EINVAL;
  594. *num_planes = f->fmt->memplanes;
  595. for (i = 0; i < f->fmt->memplanes; i++) {
  596. sizes[i] = (f->f_width * f->f_height * f->fmt->depth[i]) / 8;
  597. allocators[i] = ctx->fimc_dev->alloc_ctx;
  598. }
  599. return 0;
  600. }
  601. static int fimc_buf_prepare(struct vb2_buffer *vb)
  602. {
  603. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  604. struct fimc_frame *frame;
  605. int i;
  606. frame = ctx_get_frame(ctx, vb->vb2_queue->type);
  607. if (IS_ERR(frame))
  608. return PTR_ERR(frame);
  609. for (i = 0; i < frame->fmt->memplanes; i++)
  610. vb2_set_plane_payload(vb, i, frame->payload[i]);
  611. return 0;
  612. }
  613. static void fimc_buf_queue(struct vb2_buffer *vb)
  614. {
  615. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  616. dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
  617. if (ctx->m2m_ctx)
  618. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  619. }
  620. static void fimc_lock(struct vb2_queue *vq)
  621. {
  622. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  623. mutex_lock(&ctx->fimc_dev->lock);
  624. }
  625. static void fimc_unlock(struct vb2_queue *vq)
  626. {
  627. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  628. mutex_unlock(&ctx->fimc_dev->lock);
  629. }
  630. static struct vb2_ops fimc_qops = {
  631. .queue_setup = fimc_queue_setup,
  632. .buf_prepare = fimc_buf_prepare,
  633. .buf_queue = fimc_buf_queue,
  634. .wait_prepare = fimc_unlock,
  635. .wait_finish = fimc_lock,
  636. .stop_streaming = stop_streaming,
  637. .start_streaming = start_streaming,
  638. };
  639. /*
  640. * V4L2 controls handling
  641. */
  642. #define ctrl_to_ctx(__ctrl) \
  643. container_of((__ctrl)->handler, struct fimc_ctx, ctrl_handler)
  644. static int fimc_s_ctrl(struct v4l2_ctrl *ctrl)
  645. {
  646. struct fimc_ctx *ctx = ctrl_to_ctx(ctrl);
  647. struct fimc_dev *fimc = ctx->fimc_dev;
  648. struct samsung_fimc_variant *variant = fimc->variant;
  649. unsigned long flags;
  650. int ret = 0;
  651. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  652. return 0;
  653. switch (ctrl->id) {
  654. case V4L2_CID_HFLIP:
  655. spin_lock_irqsave(&ctx->slock, flags);
  656. ctx->hflip = ctrl->val;
  657. break;
  658. case V4L2_CID_VFLIP:
  659. spin_lock_irqsave(&ctx->slock, flags);
  660. ctx->vflip = ctrl->val;
  661. break;
  662. case V4L2_CID_ROTATE:
  663. if (fimc_capture_pending(fimc) ||
  664. fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
  665. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  666. ctx->s_frame.height, ctx->d_frame.width,
  667. ctx->d_frame.height, ctrl->val);
  668. }
  669. if (ret) {
  670. v4l2_err(fimc->m2m.vfd, "Out of scaler range\n");
  671. return -EINVAL;
  672. }
  673. if ((ctrl->val == 90 || ctrl->val == 270) &&
  674. !variant->has_out_rot)
  675. return -EINVAL;
  676. spin_lock_irqsave(&ctx->slock, flags);
  677. ctx->rotation = ctrl->val;
  678. break;
  679. default:
  680. v4l2_err(fimc->v4l2_dev, "Invalid control: 0x%X\n", ctrl->id);
  681. return -EINVAL;
  682. }
  683. ctx->state |= FIMC_PARAMS;
  684. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  685. spin_unlock_irqrestore(&ctx->slock, flags);
  686. return 0;
  687. }
  688. static const struct v4l2_ctrl_ops fimc_ctrl_ops = {
  689. .s_ctrl = fimc_s_ctrl,
  690. };
  691. int fimc_ctrls_create(struct fimc_ctx *ctx)
  692. {
  693. if (ctx->ctrls_rdy)
  694. return 0;
  695. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
  696. ctx->ctrl_rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
  697. V4L2_CID_HFLIP, 0, 1, 1, 0);
  698. ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
  699. V4L2_CID_VFLIP, 0, 1, 1, 0);
  700. ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
  701. V4L2_CID_ROTATE, 0, 270, 90, 0);
  702. ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
  703. return ctx->ctrl_handler.error;
  704. }
  705. void fimc_ctrls_delete(struct fimc_ctx *ctx)
  706. {
  707. if (ctx->ctrls_rdy) {
  708. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  709. ctx->ctrls_rdy = false;
  710. }
  711. }
  712. void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active)
  713. {
  714. if (!ctx->ctrls_rdy)
  715. return;
  716. mutex_lock(&ctx->ctrl_handler.lock);
  717. v4l2_ctrl_activate(ctx->ctrl_rotate, active);
  718. v4l2_ctrl_activate(ctx->ctrl_hflip, active);
  719. v4l2_ctrl_activate(ctx->ctrl_vflip, active);
  720. if (active) {
  721. ctx->rotation = ctx->ctrl_rotate->val;
  722. ctx->hflip = ctx->ctrl_hflip->val;
  723. ctx->vflip = ctx->ctrl_vflip->val;
  724. } else {
  725. ctx->rotation = 0;
  726. ctx->hflip = 0;
  727. ctx->vflip = 0;
  728. }
  729. mutex_unlock(&ctx->ctrl_handler.lock);
  730. }
  731. /*
  732. * V4L2 ioctl handlers
  733. */
  734. static int fimc_m2m_querycap(struct file *file, void *fh,
  735. struct v4l2_capability *cap)
  736. {
  737. struct fimc_ctx *ctx = fh_to_ctx(fh);
  738. struct fimc_dev *fimc = ctx->fimc_dev;
  739. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  740. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  741. cap->bus_info[0] = 0;
  742. cap->capabilities = V4L2_CAP_STREAMING |
  743. V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  744. return 0;
  745. }
  746. static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  747. struct v4l2_fmtdesc *f)
  748. {
  749. struct fimc_fmt *fmt;
  750. fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_M2M, f->index);
  751. if (!fmt)
  752. return -EINVAL;
  753. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  754. f->pixelformat = fmt->fourcc;
  755. return 0;
  756. }
  757. int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f)
  758. {
  759. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  760. int i;
  761. pixm->width = frame->o_width;
  762. pixm->height = frame->o_height;
  763. pixm->field = V4L2_FIELD_NONE;
  764. pixm->pixelformat = frame->fmt->fourcc;
  765. pixm->colorspace = V4L2_COLORSPACE_JPEG;
  766. pixm->num_planes = frame->fmt->memplanes;
  767. for (i = 0; i < pixm->num_planes; ++i) {
  768. int bpl = frame->f_width;
  769. if (frame->fmt->colplanes == 1) /* packed formats */
  770. bpl = (bpl * frame->fmt->depth[0]) / 8;
  771. pixm->plane_fmt[i].bytesperline = bpl;
  772. pixm->plane_fmt[i].sizeimage = (frame->o_width *
  773. frame->o_height * frame->fmt->depth[i]) / 8;
  774. }
  775. return 0;
  776. }
  777. void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f)
  778. {
  779. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  780. frame->f_width = pixm->plane_fmt[0].bytesperline;
  781. if (frame->fmt->colplanes == 1)
  782. frame->f_width = (frame->f_width * 8) / frame->fmt->depth[0];
  783. frame->f_height = pixm->height;
  784. frame->width = pixm->width;
  785. frame->height = pixm->height;
  786. frame->o_width = pixm->width;
  787. frame->o_height = pixm->height;
  788. frame->offs_h = 0;
  789. frame->offs_v = 0;
  790. }
  791. /**
  792. * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
  793. * @fmt: fimc pixel format description (input)
  794. * @width: requested pixel width
  795. * @height: requested pixel height
  796. * @pix: multi-plane format to adjust
  797. */
  798. void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
  799. struct v4l2_pix_format_mplane *pix)
  800. {
  801. u32 bytesperline = 0;
  802. int i;
  803. pix->colorspace = V4L2_COLORSPACE_JPEG;
  804. pix->field = V4L2_FIELD_NONE;
  805. pix->num_planes = fmt->memplanes;
  806. pix->height = height;
  807. pix->width = width;
  808. for (i = 0; i < pix->num_planes; ++i) {
  809. u32 bpl = pix->plane_fmt[i].bytesperline;
  810. u32 *sizeimage = &pix->plane_fmt[i].sizeimage;
  811. if (fmt->colplanes > 1 && (bpl == 0 || bpl < pix->width))
  812. bpl = pix->width; /* Planar */
  813. if (fmt->colplanes == 1 && /* Packed */
  814. (bpl == 0 || ((bpl * 8) / fmt->depth[i]) < pix->width))
  815. bpl = (pix->width * fmt->depth[0]) / 8;
  816. if (i == 0) /* Same bytesperline for each plane. */
  817. bytesperline = bpl;
  818. pix->plane_fmt[i].bytesperline = bytesperline;
  819. *sizeimage = (pix->width * pix->height * fmt->depth[i]) / 8;
  820. }
  821. }
  822. static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
  823. struct v4l2_format *f)
  824. {
  825. struct fimc_ctx *ctx = fh_to_ctx(fh);
  826. struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
  827. if (IS_ERR(frame))
  828. return PTR_ERR(frame);
  829. return fimc_fill_format(frame, f);
  830. }
  831. /**
  832. * fimc_find_format - lookup fimc color format by fourcc or media bus format
  833. * @pixelformat: fourcc to match, ignored if null
  834. * @mbus_code: media bus code to match, ignored if null
  835. * @mask: the color flags to match
  836. * @index: offset in the fimc_formats array, ignored if negative
  837. */
  838. struct fimc_fmt *fimc_find_format(u32 *pixelformat, u32 *mbus_code,
  839. unsigned int mask, int index)
  840. {
  841. struct fimc_fmt *fmt, *def_fmt = NULL;
  842. unsigned int i;
  843. int id = 0;
  844. if (index >= ARRAY_SIZE(fimc_formats))
  845. return NULL;
  846. for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
  847. fmt = &fimc_formats[i];
  848. if (!(fmt->flags & mask))
  849. continue;
  850. if (pixelformat && fmt->fourcc == *pixelformat)
  851. return fmt;
  852. if (mbus_code && fmt->mbus_code == *mbus_code)
  853. return fmt;
  854. if (index == id)
  855. def_fmt = fmt;
  856. id++;
  857. }
  858. return def_fmt;
  859. }
  860. static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
  861. {
  862. struct fimc_dev *fimc = ctx->fimc_dev;
  863. struct samsung_fimc_variant *variant = fimc->variant;
  864. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  865. struct fimc_fmt *fmt;
  866. u32 max_w, mod_x, mod_y;
  867. if (!IS_M2M(f->type))
  868. return -EINVAL;
  869. dbg("w: %d, h: %d", pix->width, pix->height);
  870. fmt = fimc_find_format(&pix->pixelformat, NULL, FMT_FLAGS_M2M, 0);
  871. if (WARN(fmt == NULL, "Pixel format lookup failed"))
  872. return -EINVAL;
  873. if (pix->field == V4L2_FIELD_ANY)
  874. pix->field = V4L2_FIELD_NONE;
  875. else if (pix->field != V4L2_FIELD_NONE)
  876. return -EINVAL;
  877. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  878. max_w = variant->pix_limit->scaler_dis_w;
  879. mod_x = ffs(variant->min_inp_pixsize) - 1;
  880. } else {
  881. max_w = variant->pix_limit->out_rot_dis_w;
  882. mod_x = ffs(variant->min_out_pixsize) - 1;
  883. }
  884. if (tiled_fmt(fmt)) {
  885. mod_x = 6; /* 64 x 32 pixels tile */
  886. mod_y = 5;
  887. } else {
  888. if (fimc->id == 1 && variant->pix_hoff)
  889. mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
  890. else
  891. mod_y = mod_x;
  892. }
  893. dbg("mod_x: %d, mod_y: %d, max_w: %d", mod_x, mod_y, max_w);
  894. v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
  895. &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
  896. fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
  897. return 0;
  898. }
  899. static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
  900. struct v4l2_format *f)
  901. {
  902. struct fimc_ctx *ctx = fh_to_ctx(fh);
  903. return fimc_try_fmt_mplane(ctx, f);
  904. }
  905. static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
  906. struct v4l2_format *f)
  907. {
  908. struct fimc_ctx *ctx = fh_to_ctx(fh);
  909. struct fimc_dev *fimc = ctx->fimc_dev;
  910. struct vb2_queue *vq;
  911. struct fimc_frame *frame;
  912. struct v4l2_pix_format_mplane *pix;
  913. int i, ret = 0;
  914. ret = fimc_try_fmt_mplane(ctx, f);
  915. if (ret)
  916. return ret;
  917. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  918. if (vb2_is_busy(vq)) {
  919. v4l2_err(fimc->m2m.vfd, "queue (%d) busy\n", f->type);
  920. return -EBUSY;
  921. }
  922. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  923. frame = &ctx->s_frame;
  924. else
  925. frame = &ctx->d_frame;
  926. pix = &f->fmt.pix_mp;
  927. frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
  928. FMT_FLAGS_M2M, 0);
  929. if (!frame->fmt)
  930. return -EINVAL;
  931. for (i = 0; i < frame->fmt->colplanes; i++) {
  932. frame->payload[i] =
  933. (pix->width * pix->height * frame->fmt->depth[i]) / 8;
  934. }
  935. fimc_fill_frame(frame, f);
  936. ctx->scaler.enabled = 1;
  937. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  938. fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
  939. else
  940. fimc_ctx_state_lock_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
  941. dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
  942. return 0;
  943. }
  944. static int fimc_m2m_reqbufs(struct file *file, void *fh,
  945. struct v4l2_requestbuffers *reqbufs)
  946. {
  947. struct fimc_ctx *ctx = fh_to_ctx(fh);
  948. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  949. }
  950. static int fimc_m2m_querybuf(struct file *file, void *fh,
  951. struct v4l2_buffer *buf)
  952. {
  953. struct fimc_ctx *ctx = fh_to_ctx(fh);
  954. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  955. }
  956. static int fimc_m2m_qbuf(struct file *file, void *fh,
  957. struct v4l2_buffer *buf)
  958. {
  959. struct fimc_ctx *ctx = fh_to_ctx(fh);
  960. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  961. }
  962. static int fimc_m2m_dqbuf(struct file *file, void *fh,
  963. struct v4l2_buffer *buf)
  964. {
  965. struct fimc_ctx *ctx = fh_to_ctx(fh);
  966. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  967. }
  968. static int fimc_m2m_streamon(struct file *file, void *fh,
  969. enum v4l2_buf_type type)
  970. {
  971. struct fimc_ctx *ctx = fh_to_ctx(fh);
  972. /* The source and target color format need to be set */
  973. if (V4L2_TYPE_IS_OUTPUT(type)) {
  974. if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
  975. return -EINVAL;
  976. } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
  977. return -EINVAL;
  978. }
  979. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  980. }
  981. static int fimc_m2m_streamoff(struct file *file, void *fh,
  982. enum v4l2_buf_type type)
  983. {
  984. struct fimc_ctx *ctx = fh_to_ctx(fh);
  985. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  986. }
  987. static int fimc_m2m_cropcap(struct file *file, void *fh,
  988. struct v4l2_cropcap *cr)
  989. {
  990. struct fimc_ctx *ctx = fh_to_ctx(fh);
  991. struct fimc_frame *frame;
  992. frame = ctx_get_frame(ctx, cr->type);
  993. if (IS_ERR(frame))
  994. return PTR_ERR(frame);
  995. cr->bounds.left = 0;
  996. cr->bounds.top = 0;
  997. cr->bounds.width = frame->o_width;
  998. cr->bounds.height = frame->o_height;
  999. cr->defrect = cr->bounds;
  1000. return 0;
  1001. }
  1002. static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  1003. {
  1004. struct fimc_ctx *ctx = fh_to_ctx(fh);
  1005. struct fimc_frame *frame;
  1006. frame = ctx_get_frame(ctx, cr->type);
  1007. if (IS_ERR(frame))
  1008. return PTR_ERR(frame);
  1009. cr->c.left = frame->offs_h;
  1010. cr->c.top = frame->offs_v;
  1011. cr->c.width = frame->width;
  1012. cr->c.height = frame->height;
  1013. return 0;
  1014. }
  1015. static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
  1016. {
  1017. struct fimc_dev *fimc = ctx->fimc_dev;
  1018. struct fimc_frame *f;
  1019. u32 min_size, halign, depth = 0;
  1020. int i;
  1021. if (cr->c.top < 0 || cr->c.left < 0) {
  1022. v4l2_err(fimc->m2m.vfd,
  1023. "doesn't support negative values for top & left\n");
  1024. return -EINVAL;
  1025. }
  1026. if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1027. f = &ctx->d_frame;
  1028. else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1029. f = &ctx->s_frame;
  1030. else
  1031. return -EINVAL;
  1032. min_size = (f == &ctx->s_frame) ?
  1033. fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
  1034. /* Get pixel alignment constraints. */
  1035. if (fimc->id == 1 && fimc->variant->pix_hoff)
  1036. halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
  1037. else
  1038. halign = ffs(min_size) - 1;
  1039. for (i = 0; i < f->fmt->colplanes; i++)
  1040. depth += f->fmt->depth[i];
  1041. v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
  1042. ffs(min_size) - 1,
  1043. &cr->c.height, min_size, f->o_height,
  1044. halign, 64/(ALIGN(depth, 8)));
  1045. /* adjust left/top if cropping rectangle is out of bounds */
  1046. if (cr->c.left + cr->c.width > f->o_width)
  1047. cr->c.left = f->o_width - cr->c.width;
  1048. if (cr->c.top + cr->c.height > f->o_height)
  1049. cr->c.top = f->o_height - cr->c.height;
  1050. cr->c.left = round_down(cr->c.left, min_size);
  1051. cr->c.top = round_down(cr->c.top, fimc->variant->hor_offs_align);
  1052. dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
  1053. cr->c.left, cr->c.top, cr->c.width, cr->c.height,
  1054. f->f_width, f->f_height);
  1055. return 0;
  1056. }
  1057. static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  1058. {
  1059. struct fimc_ctx *ctx = fh_to_ctx(fh);
  1060. struct fimc_dev *fimc = ctx->fimc_dev;
  1061. struct fimc_frame *f;
  1062. int ret;
  1063. ret = fimc_m2m_try_crop(ctx, cr);
  1064. if (ret)
  1065. return ret;
  1066. f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  1067. &ctx->s_frame : &ctx->d_frame;
  1068. /* Check to see if scaling ratio is within supported range */
  1069. if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
  1070. if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1071. ret = fimc_check_scaler_ratio(ctx, cr->c.width,
  1072. cr->c.height, ctx->d_frame.width,
  1073. ctx->d_frame.height, ctx->rotation);
  1074. } else {
  1075. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  1076. ctx->s_frame.height, cr->c.width,
  1077. cr->c.height, ctx->rotation);
  1078. }
  1079. if (ret) {
  1080. v4l2_err(fimc->m2m.vfd, "Out of scaler range\n");
  1081. return -EINVAL;
  1082. }
  1083. }
  1084. f->offs_h = cr->c.left;
  1085. f->offs_v = cr->c.top;
  1086. f->width = cr->c.width;
  1087. f->height = cr->c.height;
  1088. fimc_ctx_state_lock_set(FIMC_PARAMS, ctx);
  1089. return 0;
  1090. }
  1091. static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
  1092. .vidioc_querycap = fimc_m2m_querycap,
  1093. .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
  1094. .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
  1095. .vidioc_g_fmt_vid_cap_mplane = fimc_m2m_g_fmt_mplane,
  1096. .vidioc_g_fmt_vid_out_mplane = fimc_m2m_g_fmt_mplane,
  1097. .vidioc_try_fmt_vid_cap_mplane = fimc_m2m_try_fmt_mplane,
  1098. .vidioc_try_fmt_vid_out_mplane = fimc_m2m_try_fmt_mplane,
  1099. .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
  1100. .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
  1101. .vidioc_reqbufs = fimc_m2m_reqbufs,
  1102. .vidioc_querybuf = fimc_m2m_querybuf,
  1103. .vidioc_qbuf = fimc_m2m_qbuf,
  1104. .vidioc_dqbuf = fimc_m2m_dqbuf,
  1105. .vidioc_streamon = fimc_m2m_streamon,
  1106. .vidioc_streamoff = fimc_m2m_streamoff,
  1107. .vidioc_g_crop = fimc_m2m_g_crop,
  1108. .vidioc_s_crop = fimc_m2m_s_crop,
  1109. .vidioc_cropcap = fimc_m2m_cropcap
  1110. };
  1111. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1112. struct vb2_queue *dst_vq)
  1113. {
  1114. struct fimc_ctx *ctx = priv;
  1115. int ret;
  1116. memset(src_vq, 0, sizeof(*src_vq));
  1117. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1118. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1119. src_vq->drv_priv = ctx;
  1120. src_vq->ops = &fimc_qops;
  1121. src_vq->mem_ops = &vb2_dma_contig_memops;
  1122. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1123. ret = vb2_queue_init(src_vq);
  1124. if (ret)
  1125. return ret;
  1126. memset(dst_vq, 0, sizeof(*dst_vq));
  1127. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1128. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1129. dst_vq->drv_priv = ctx;
  1130. dst_vq->ops = &fimc_qops;
  1131. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1132. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1133. return vb2_queue_init(dst_vq);
  1134. }
  1135. static int fimc_m2m_open(struct file *file)
  1136. {
  1137. struct fimc_dev *fimc = video_drvdata(file);
  1138. struct fimc_ctx *ctx;
  1139. int ret;
  1140. dbg("pid: %d, state: 0x%lx, refcnt: %d",
  1141. task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
  1142. /*
  1143. * Return if the corresponding video capture node
  1144. * is already opened.
  1145. */
  1146. if (fimc->vid_cap.refcnt > 0)
  1147. return -EBUSY;
  1148. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  1149. if (!ctx)
  1150. return -ENOMEM;
  1151. v4l2_fh_init(&ctx->fh, fimc->m2m.vfd);
  1152. ret = fimc_ctrls_create(ctx);
  1153. if (ret)
  1154. goto error_fh;
  1155. /* Use separate control handler per file handle */
  1156. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  1157. file->private_data = &ctx->fh;
  1158. v4l2_fh_add(&ctx->fh);
  1159. ctx->fimc_dev = fimc;
  1160. /* Default color format */
  1161. ctx->s_frame.fmt = &fimc_formats[0];
  1162. ctx->d_frame.fmt = &fimc_formats[0];
  1163. /* Setup the device context for memory-to-memory mode */
  1164. ctx->state = FIMC_CTX_M2M;
  1165. ctx->flags = 0;
  1166. ctx->in_path = FIMC_DMA;
  1167. ctx->out_path = FIMC_DMA;
  1168. spin_lock_init(&ctx->slock);
  1169. ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
  1170. if (IS_ERR(ctx->m2m_ctx)) {
  1171. ret = PTR_ERR(ctx->m2m_ctx);
  1172. goto error_c;
  1173. }
  1174. if (fimc->m2m.refcnt++ == 0)
  1175. set_bit(ST_M2M_RUN, &fimc->state);
  1176. return 0;
  1177. error_c:
  1178. fimc_ctrls_delete(ctx);
  1179. error_fh:
  1180. v4l2_fh_del(&ctx->fh);
  1181. v4l2_fh_exit(&ctx->fh);
  1182. kfree(ctx);
  1183. return ret;
  1184. }
  1185. static int fimc_m2m_release(struct file *file)
  1186. {
  1187. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  1188. struct fimc_dev *fimc = ctx->fimc_dev;
  1189. dbg("pid: %d, state: 0x%lx, refcnt= %d",
  1190. task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
  1191. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  1192. fimc_ctrls_delete(ctx);
  1193. v4l2_fh_del(&ctx->fh);
  1194. v4l2_fh_exit(&ctx->fh);
  1195. if (--fimc->m2m.refcnt <= 0)
  1196. clear_bit(ST_M2M_RUN, &fimc->state);
  1197. kfree(ctx);
  1198. return 0;
  1199. }
  1200. static unsigned int fimc_m2m_poll(struct file *file,
  1201. struct poll_table_struct *wait)
  1202. {
  1203. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  1204. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  1205. }
  1206. static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
  1207. {
  1208. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  1209. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  1210. }
  1211. static const struct v4l2_file_operations fimc_m2m_fops = {
  1212. .owner = THIS_MODULE,
  1213. .open = fimc_m2m_open,
  1214. .release = fimc_m2m_release,
  1215. .poll = fimc_m2m_poll,
  1216. .unlocked_ioctl = video_ioctl2,
  1217. .mmap = fimc_m2m_mmap,
  1218. };
  1219. static struct v4l2_m2m_ops m2m_ops = {
  1220. .device_run = fimc_dma_run,
  1221. .job_abort = fimc_job_abort,
  1222. };
  1223. int fimc_register_m2m_device(struct fimc_dev *fimc,
  1224. struct v4l2_device *v4l2_dev)
  1225. {
  1226. struct video_device *vfd;
  1227. struct platform_device *pdev;
  1228. int ret = 0;
  1229. if (!fimc)
  1230. return -ENODEV;
  1231. pdev = fimc->pdev;
  1232. fimc->v4l2_dev = v4l2_dev;
  1233. vfd = video_device_alloc();
  1234. if (!vfd) {
  1235. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  1236. return -ENOMEM;
  1237. }
  1238. vfd->fops = &fimc_m2m_fops;
  1239. vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
  1240. vfd->v4l2_dev = v4l2_dev;
  1241. vfd->minor = -1;
  1242. vfd->release = video_device_release;
  1243. vfd->lock = &fimc->lock;
  1244. snprintf(vfd->name, sizeof(vfd->name), "%s.m2m", dev_name(&pdev->dev));
  1245. video_set_drvdata(vfd, fimc);
  1246. fimc->m2m.vfd = vfd;
  1247. fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
  1248. if (IS_ERR(fimc->m2m.m2m_dev)) {
  1249. v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
  1250. ret = PTR_ERR(fimc->m2m.m2m_dev);
  1251. goto err_init;
  1252. }
  1253. ret = media_entity_init(&vfd->entity, 0, NULL, 0);
  1254. if (!ret)
  1255. return 0;
  1256. v4l2_m2m_release(fimc->m2m.m2m_dev);
  1257. err_init:
  1258. video_device_release(fimc->m2m.vfd);
  1259. return ret;
  1260. }
  1261. void fimc_unregister_m2m_device(struct fimc_dev *fimc)
  1262. {
  1263. if (!fimc)
  1264. return;
  1265. if (fimc->m2m.m2m_dev)
  1266. v4l2_m2m_release(fimc->m2m.m2m_dev);
  1267. if (fimc->m2m.vfd) {
  1268. media_entity_cleanup(&fimc->m2m.vfd->entity);
  1269. /* Can also be called if video device wasn't registered */
  1270. video_unregister_device(fimc->m2m.vfd);
  1271. }
  1272. }
  1273. static void fimc_clk_put(struct fimc_dev *fimc)
  1274. {
  1275. int i;
  1276. for (i = 0; i < fimc->num_clocks; i++) {
  1277. if (fimc->clock[i])
  1278. clk_put(fimc->clock[i]);
  1279. }
  1280. }
  1281. static int fimc_clk_get(struct fimc_dev *fimc)
  1282. {
  1283. int i;
  1284. for (i = 0; i < fimc->num_clocks; i++) {
  1285. fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
  1286. if (!IS_ERR_OR_NULL(fimc->clock[i]))
  1287. continue;
  1288. dev_err(&fimc->pdev->dev, "failed to get fimc clock: %s\n",
  1289. fimc_clocks[i]);
  1290. return -ENXIO;
  1291. }
  1292. return 0;
  1293. }
  1294. static int fimc_m2m_suspend(struct fimc_dev *fimc)
  1295. {
  1296. unsigned long flags;
  1297. int timeout;
  1298. spin_lock_irqsave(&fimc->slock, flags);
  1299. if (!fimc_m2m_pending(fimc)) {
  1300. spin_unlock_irqrestore(&fimc->slock, flags);
  1301. return 0;
  1302. }
  1303. clear_bit(ST_M2M_SUSPENDED, &fimc->state);
  1304. set_bit(ST_M2M_SUSPENDING, &fimc->state);
  1305. spin_unlock_irqrestore(&fimc->slock, flags);
  1306. timeout = wait_event_timeout(fimc->irq_queue,
  1307. test_bit(ST_M2M_SUSPENDED, &fimc->state),
  1308. FIMC_SHUTDOWN_TIMEOUT);
  1309. clear_bit(ST_M2M_SUSPENDING, &fimc->state);
  1310. return timeout == 0 ? -EAGAIN : 0;
  1311. }
  1312. static int fimc_m2m_resume(struct fimc_dev *fimc)
  1313. {
  1314. unsigned long flags;
  1315. spin_lock_irqsave(&fimc->slock, flags);
  1316. /* Clear for full H/W setup in first run after resume */
  1317. fimc->m2m.ctx = NULL;
  1318. spin_unlock_irqrestore(&fimc->slock, flags);
  1319. if (test_and_clear_bit(ST_M2M_SUSPENDED, &fimc->state))
  1320. fimc_m2m_job_finish(fimc->m2m.ctx,
  1321. VB2_BUF_STATE_ERROR);
  1322. return 0;
  1323. }
  1324. static int fimc_probe(struct platform_device *pdev)
  1325. {
  1326. struct fimc_dev *fimc;
  1327. struct resource *res;
  1328. struct samsung_fimc_driverdata *drv_data;
  1329. struct s5p_platform_fimc *pdata;
  1330. int ret = 0;
  1331. dev_dbg(&pdev->dev, "%s():\n", __func__);
  1332. drv_data = (struct samsung_fimc_driverdata *)
  1333. platform_get_device_id(pdev)->driver_data;
  1334. if (pdev->id >= drv_data->num_entities) {
  1335. dev_err(&pdev->dev, "Invalid platform device id: %d\n",
  1336. pdev->id);
  1337. return -EINVAL;
  1338. }
  1339. fimc = kzalloc(sizeof(struct fimc_dev), GFP_KERNEL);
  1340. if (!fimc)
  1341. return -ENOMEM;
  1342. fimc->id = pdev->id;
  1343. fimc->variant = drv_data->variant[fimc->id];
  1344. fimc->pdev = pdev;
  1345. pdata = pdev->dev.platform_data;
  1346. fimc->pdata = pdata;
  1347. init_waitqueue_head(&fimc->irq_queue);
  1348. spin_lock_init(&fimc->slock);
  1349. mutex_init(&fimc->lock);
  1350. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1351. if (!res) {
  1352. dev_err(&pdev->dev, "failed to find the registers\n");
  1353. ret = -ENOENT;
  1354. goto err_info;
  1355. }
  1356. fimc->regs_res = request_mem_region(res->start, resource_size(res),
  1357. dev_name(&pdev->dev));
  1358. if (!fimc->regs_res) {
  1359. dev_err(&pdev->dev, "failed to obtain register region\n");
  1360. ret = -ENOENT;
  1361. goto err_info;
  1362. }
  1363. fimc->regs = ioremap(res->start, resource_size(res));
  1364. if (!fimc->regs) {
  1365. dev_err(&pdev->dev, "failed to map registers\n");
  1366. ret = -ENXIO;
  1367. goto err_req_region;
  1368. }
  1369. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1370. if (!res) {
  1371. dev_err(&pdev->dev, "failed to get IRQ resource\n");
  1372. ret = -ENXIO;
  1373. goto err_regs_unmap;
  1374. }
  1375. fimc->irq = res->start;
  1376. fimc->num_clocks = MAX_FIMC_CLOCKS;
  1377. ret = fimc_clk_get(fimc);
  1378. if (ret)
  1379. goto err_regs_unmap;
  1380. clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
  1381. clk_enable(fimc->clock[CLK_BUS]);
  1382. platform_set_drvdata(pdev, fimc);
  1383. ret = request_irq(fimc->irq, fimc_irq_handler, 0, pdev->name, fimc);
  1384. if (ret) {
  1385. dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
  1386. goto err_clk;
  1387. }
  1388. pm_runtime_enable(&pdev->dev);
  1389. ret = pm_runtime_get_sync(&pdev->dev);
  1390. if (ret < 0)
  1391. goto err_irq;
  1392. /* Initialize contiguous memory allocator */
  1393. fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1394. if (IS_ERR(fimc->alloc_ctx)) {
  1395. ret = PTR_ERR(fimc->alloc_ctx);
  1396. goto err_pm;
  1397. }
  1398. dev_dbg(&pdev->dev, "FIMC.%d registered successfully\n", fimc->id);
  1399. pm_runtime_put(&pdev->dev);
  1400. return 0;
  1401. err_pm:
  1402. pm_runtime_put(&pdev->dev);
  1403. err_irq:
  1404. free_irq(fimc->irq, fimc);
  1405. err_clk:
  1406. fimc_clk_put(fimc);
  1407. err_regs_unmap:
  1408. iounmap(fimc->regs);
  1409. err_req_region:
  1410. release_resource(fimc->regs_res);
  1411. kfree(fimc->regs_res);
  1412. err_info:
  1413. kfree(fimc);
  1414. return ret;
  1415. }
  1416. static int fimc_runtime_resume(struct device *dev)
  1417. {
  1418. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1419. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1420. /* Enable clocks and perform basic initalization */
  1421. clk_enable(fimc->clock[CLK_GATE]);
  1422. fimc_hw_reset(fimc);
  1423. /* Resume the capture or mem-to-mem device */
  1424. if (fimc_capture_busy(fimc))
  1425. return fimc_capture_resume(fimc);
  1426. else if (fimc_m2m_pending(fimc))
  1427. return fimc_m2m_resume(fimc);
  1428. return 0;
  1429. }
  1430. static int fimc_runtime_suspend(struct device *dev)
  1431. {
  1432. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1433. int ret = 0;
  1434. if (fimc_capture_busy(fimc))
  1435. ret = fimc_capture_suspend(fimc);
  1436. else
  1437. ret = fimc_m2m_suspend(fimc);
  1438. if (!ret)
  1439. clk_disable(fimc->clock[CLK_GATE]);
  1440. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1441. return ret;
  1442. }
  1443. #ifdef CONFIG_PM_SLEEP
  1444. static int fimc_resume(struct device *dev)
  1445. {
  1446. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1447. unsigned long flags;
  1448. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1449. /* Do not resume if the device was idle before system suspend */
  1450. spin_lock_irqsave(&fimc->slock, flags);
  1451. if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
  1452. (!fimc_m2m_active(fimc) && !fimc_capture_busy(fimc))) {
  1453. spin_unlock_irqrestore(&fimc->slock, flags);
  1454. return 0;
  1455. }
  1456. fimc_hw_reset(fimc);
  1457. spin_unlock_irqrestore(&fimc->slock, flags);
  1458. if (fimc_capture_busy(fimc))
  1459. return fimc_capture_resume(fimc);
  1460. return fimc_m2m_resume(fimc);
  1461. }
  1462. static int fimc_suspend(struct device *dev)
  1463. {
  1464. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1465. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1466. if (test_and_set_bit(ST_LPM, &fimc->state))
  1467. return 0;
  1468. if (fimc_capture_busy(fimc))
  1469. return fimc_capture_suspend(fimc);
  1470. return fimc_m2m_suspend(fimc);
  1471. }
  1472. #endif /* CONFIG_PM_SLEEP */
  1473. static int __devexit fimc_remove(struct platform_device *pdev)
  1474. {
  1475. struct fimc_dev *fimc = platform_get_drvdata(pdev);
  1476. pm_runtime_disable(&pdev->dev);
  1477. pm_runtime_set_suspended(&pdev->dev);
  1478. vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
  1479. clk_disable(fimc->clock[CLK_BUS]);
  1480. fimc_clk_put(fimc);
  1481. free_irq(fimc->irq, fimc);
  1482. iounmap(fimc->regs);
  1483. release_resource(fimc->regs_res);
  1484. kfree(fimc->regs_res);
  1485. kfree(fimc);
  1486. dev_info(&pdev->dev, "driver unloaded\n");
  1487. return 0;
  1488. }
  1489. /* Image pixel limits, similar across several FIMC HW revisions. */
  1490. static struct fimc_pix_limit s5p_pix_limit[4] = {
  1491. [0] = {
  1492. .scaler_en_w = 3264,
  1493. .scaler_dis_w = 8192,
  1494. .in_rot_en_h = 1920,
  1495. .in_rot_dis_w = 8192,
  1496. .out_rot_en_w = 1920,
  1497. .out_rot_dis_w = 4224,
  1498. },
  1499. [1] = {
  1500. .scaler_en_w = 4224,
  1501. .scaler_dis_w = 8192,
  1502. .in_rot_en_h = 1920,
  1503. .in_rot_dis_w = 8192,
  1504. .out_rot_en_w = 1920,
  1505. .out_rot_dis_w = 4224,
  1506. },
  1507. [2] = {
  1508. .scaler_en_w = 1920,
  1509. .scaler_dis_w = 8192,
  1510. .in_rot_en_h = 1280,
  1511. .in_rot_dis_w = 8192,
  1512. .out_rot_en_w = 1280,
  1513. .out_rot_dis_w = 1920,
  1514. },
  1515. [3] = {
  1516. .scaler_en_w = 1920,
  1517. .scaler_dis_w = 8192,
  1518. .in_rot_en_h = 1366,
  1519. .in_rot_dis_w = 8192,
  1520. .out_rot_en_w = 1366,
  1521. .out_rot_dis_w = 1920,
  1522. },
  1523. };
  1524. static struct samsung_fimc_variant fimc0_variant_s5p = {
  1525. .has_inp_rot = 1,
  1526. .has_out_rot = 1,
  1527. .has_cam_if = 1,
  1528. .min_inp_pixsize = 16,
  1529. .min_out_pixsize = 16,
  1530. .hor_offs_align = 8,
  1531. .out_buf_count = 4,
  1532. .pix_limit = &s5p_pix_limit[0],
  1533. };
  1534. static struct samsung_fimc_variant fimc2_variant_s5p = {
  1535. .has_cam_if = 1,
  1536. .min_inp_pixsize = 16,
  1537. .min_out_pixsize = 16,
  1538. .hor_offs_align = 8,
  1539. .out_buf_count = 4,
  1540. .pix_limit = &s5p_pix_limit[1],
  1541. };
  1542. static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
  1543. .pix_hoff = 1,
  1544. .has_inp_rot = 1,
  1545. .has_out_rot = 1,
  1546. .has_cam_if = 1,
  1547. .min_inp_pixsize = 16,
  1548. .min_out_pixsize = 16,
  1549. .hor_offs_align = 8,
  1550. .out_buf_count = 4,
  1551. .pix_limit = &s5p_pix_limit[1],
  1552. };
  1553. static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
  1554. .pix_hoff = 1,
  1555. .has_inp_rot = 1,
  1556. .has_out_rot = 1,
  1557. .has_cam_if = 1,
  1558. .has_mainscaler_ext = 1,
  1559. .min_inp_pixsize = 16,
  1560. .min_out_pixsize = 16,
  1561. .hor_offs_align = 1,
  1562. .out_buf_count = 4,
  1563. .pix_limit = &s5p_pix_limit[2],
  1564. };
  1565. static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
  1566. .has_cam_if = 1,
  1567. .pix_hoff = 1,
  1568. .min_inp_pixsize = 16,
  1569. .min_out_pixsize = 16,
  1570. .hor_offs_align = 8,
  1571. .out_buf_count = 4,
  1572. .pix_limit = &s5p_pix_limit[2],
  1573. };
  1574. static struct samsung_fimc_variant fimc0_variant_exynos4 = {
  1575. .pix_hoff = 1,
  1576. .has_inp_rot = 1,
  1577. .has_out_rot = 1,
  1578. .has_cam_if = 1,
  1579. .has_cistatus2 = 1,
  1580. .has_mainscaler_ext = 1,
  1581. .min_inp_pixsize = 16,
  1582. .min_out_pixsize = 16,
  1583. .hor_offs_align = 2,
  1584. .out_buf_count = 32,
  1585. .pix_limit = &s5p_pix_limit[1],
  1586. };
  1587. static struct samsung_fimc_variant fimc3_variant_exynos4 = {
  1588. .pix_hoff = 1,
  1589. .has_cam_if = 1,
  1590. .has_cistatus2 = 1,
  1591. .has_mainscaler_ext = 1,
  1592. .min_inp_pixsize = 16,
  1593. .min_out_pixsize = 16,
  1594. .hor_offs_align = 2,
  1595. .out_buf_count = 32,
  1596. .pix_limit = &s5p_pix_limit[3],
  1597. };
  1598. /* S5PC100 */
  1599. static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
  1600. .variant = {
  1601. [0] = &fimc0_variant_s5p,
  1602. [1] = &fimc0_variant_s5p,
  1603. [2] = &fimc2_variant_s5p,
  1604. },
  1605. .num_entities = 3,
  1606. .lclk_frequency = 133000000UL,
  1607. };
  1608. /* S5PV210, S5PC110 */
  1609. static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
  1610. .variant = {
  1611. [0] = &fimc0_variant_s5pv210,
  1612. [1] = &fimc1_variant_s5pv210,
  1613. [2] = &fimc2_variant_s5pv210,
  1614. },
  1615. .num_entities = 3,
  1616. .lclk_frequency = 166000000UL,
  1617. };
  1618. /* S5PV310, S5PC210 */
  1619. static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
  1620. .variant = {
  1621. [0] = &fimc0_variant_exynos4,
  1622. [1] = &fimc0_variant_exynos4,
  1623. [2] = &fimc0_variant_exynos4,
  1624. [3] = &fimc3_variant_exynos4,
  1625. },
  1626. .num_entities = 4,
  1627. .lclk_frequency = 166000000UL,
  1628. };
  1629. static struct platform_device_id fimc_driver_ids[] = {
  1630. {
  1631. .name = "s5p-fimc",
  1632. .driver_data = (unsigned long)&fimc_drvdata_s5p,
  1633. }, {
  1634. .name = "s5pv210-fimc",
  1635. .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
  1636. }, {
  1637. .name = "exynos4-fimc",
  1638. .driver_data = (unsigned long)&fimc_drvdata_exynos4,
  1639. },
  1640. {},
  1641. };
  1642. MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
  1643. static const struct dev_pm_ops fimc_pm_ops = {
  1644. SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
  1645. SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
  1646. };
  1647. static struct platform_driver fimc_driver = {
  1648. .probe = fimc_probe,
  1649. .remove = __devexit_p(fimc_remove),
  1650. .id_table = fimc_driver_ids,
  1651. .driver = {
  1652. .name = FIMC_MODULE_NAME,
  1653. .owner = THIS_MODULE,
  1654. .pm = &fimc_pm_ops,
  1655. }
  1656. };
  1657. int __init fimc_register_driver(void)
  1658. {
  1659. return platform_driver_probe(&fimc_driver, fimc_probe);
  1660. }
  1661. void __exit fimc_unregister_driver(void)
  1662. {
  1663. platform_driver_unregister(&fimc_driver);
  1664. }