fimc-core.c 50 KB

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