fimc-core.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  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_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. if (ctx->state & FIMC_CTX_SHUT) {
  377. ctx->state &= ~FIMC_CTX_SHUT;
  378. wake_up(&fimc->irq_queue);
  379. }
  380. return IRQ_HANDLED;
  381. }
  382. } else if (test_bit(ST_CAPT_PEND, &fimc->state)) {
  383. fimc_capture_irq_handler(fimc,
  384. !test_bit(ST_CAPT_JPEG, &fimc->state));
  385. if (cap->active_buf_cnt == 1) {
  386. fimc_deactivate_capture(fimc);
  387. clear_bit(ST_CAPT_STREAM, &fimc->state);
  388. }
  389. }
  390. out:
  391. spin_unlock(&fimc->slock);
  392. return IRQ_HANDLED;
  393. }
  394. /* The color format (colplanes, memplanes) must be already configured. */
  395. int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
  396. struct fimc_frame *frame, struct fimc_addr *paddr)
  397. {
  398. int ret = 0;
  399. u32 pix_size;
  400. if (vb == NULL || frame == NULL)
  401. return -EINVAL;
  402. pix_size = frame->width * frame->height;
  403. dbg("memplanes= %d, colplanes= %d, pix_size= %d",
  404. frame->fmt->memplanes, frame->fmt->colplanes, pix_size);
  405. paddr->y = vb2_dma_contig_plane_dma_addr(vb, 0);
  406. if (frame->fmt->memplanes == 1) {
  407. switch (frame->fmt->colplanes) {
  408. case 1:
  409. paddr->cb = 0;
  410. paddr->cr = 0;
  411. break;
  412. case 2:
  413. /* decompose Y into Y/Cb */
  414. paddr->cb = (u32)(paddr->y + pix_size);
  415. paddr->cr = 0;
  416. break;
  417. case 3:
  418. paddr->cb = (u32)(paddr->y + pix_size);
  419. /* decompose Y into Y/Cb/Cr */
  420. if (S5P_FIMC_YCBCR420 == frame->fmt->color)
  421. paddr->cr = (u32)(paddr->cb
  422. + (pix_size >> 2));
  423. else /* 422 */
  424. paddr->cr = (u32)(paddr->cb
  425. + (pix_size >> 1));
  426. break;
  427. default:
  428. return -EINVAL;
  429. }
  430. } else {
  431. if (frame->fmt->memplanes >= 2)
  432. paddr->cb = vb2_dma_contig_plane_dma_addr(vb, 1);
  433. if (frame->fmt->memplanes == 3)
  434. paddr->cr = vb2_dma_contig_plane_dma_addr(vb, 2);
  435. }
  436. dbg("PHYS_ADDR: y= 0x%X cb= 0x%X cr= 0x%X ret= %d",
  437. paddr->y, paddr->cb, paddr->cr, ret);
  438. return ret;
  439. }
  440. /* Set order for 1 and 2 plane YCBCR 4:2:2 formats. */
  441. void fimc_set_yuv_order(struct fimc_ctx *ctx)
  442. {
  443. /* The one only mode supported in SoC. */
  444. ctx->in_order_2p = S5P_FIMC_LSB_CRCB;
  445. ctx->out_order_2p = S5P_FIMC_LSB_CRCB;
  446. /* Set order for 1 plane input formats. */
  447. switch (ctx->s_frame.fmt->color) {
  448. case S5P_FIMC_YCRYCB422:
  449. ctx->in_order_1p = S5P_MSCTRL_ORDER422_CBYCRY;
  450. break;
  451. case S5P_FIMC_CBYCRY422:
  452. ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCRYCB;
  453. break;
  454. case S5P_FIMC_CRYCBY422:
  455. ctx->in_order_1p = S5P_MSCTRL_ORDER422_YCBYCR;
  456. break;
  457. case S5P_FIMC_YCBYCR422:
  458. default:
  459. ctx->in_order_1p = S5P_MSCTRL_ORDER422_CRYCBY;
  460. break;
  461. }
  462. dbg("ctx->in_order_1p= %d", ctx->in_order_1p);
  463. switch (ctx->d_frame.fmt->color) {
  464. case S5P_FIMC_YCRYCB422:
  465. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CBYCRY;
  466. break;
  467. case S5P_FIMC_CBYCRY422:
  468. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCRYCB;
  469. break;
  470. case S5P_FIMC_CRYCBY422:
  471. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_YCBYCR;
  472. break;
  473. case S5P_FIMC_YCBYCR422:
  474. default:
  475. ctx->out_order_1p = S5P_CIOCTRL_ORDER422_CRYCBY;
  476. break;
  477. }
  478. dbg("ctx->out_order_1p= %d", ctx->out_order_1p);
  479. }
  480. void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f)
  481. {
  482. struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
  483. u32 i, depth = 0;
  484. for (i = 0; i < f->fmt->colplanes; i++)
  485. depth += f->fmt->depth[i];
  486. f->dma_offset.y_h = f->offs_h;
  487. if (!variant->pix_hoff)
  488. f->dma_offset.y_h *= (depth >> 3);
  489. f->dma_offset.y_v = f->offs_v;
  490. f->dma_offset.cb_h = f->offs_h;
  491. f->dma_offset.cb_v = f->offs_v;
  492. f->dma_offset.cr_h = f->offs_h;
  493. f->dma_offset.cr_v = f->offs_v;
  494. if (!variant->pix_hoff) {
  495. if (f->fmt->colplanes == 3) {
  496. f->dma_offset.cb_h >>= 1;
  497. f->dma_offset.cr_h >>= 1;
  498. }
  499. if (f->fmt->color == S5P_FIMC_YCBCR420) {
  500. f->dma_offset.cb_v >>= 1;
  501. f->dma_offset.cr_v >>= 1;
  502. }
  503. }
  504. dbg("in_offset: color= %d, y_h= %d, y_v= %d",
  505. f->fmt->color, f->dma_offset.y_h, f->dma_offset.y_v);
  506. }
  507. static void fimc_dma_run(void *priv)
  508. {
  509. struct vb2_buffer *vb = NULL;
  510. struct fimc_ctx *ctx = priv;
  511. struct fimc_frame *sf, *df;
  512. struct fimc_dev *fimc;
  513. unsigned long flags;
  514. u32 ret;
  515. if (WARN(!ctx, "null hardware context\n"))
  516. return;
  517. fimc = ctx->fimc_dev;
  518. spin_lock_irqsave(&fimc->slock, flags);
  519. set_bit(ST_M2M_PEND, &fimc->state);
  520. sf = &ctx->s_frame;
  521. df = &ctx->d_frame;
  522. if (ctx->state & FIMC_PARAMS) {
  523. /* Prepare the DMA offsets for scaler */
  524. fimc_prepare_dma_offset(ctx, sf);
  525. fimc_prepare_dma_offset(ctx, df);
  526. }
  527. vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  528. ret = fimc_prepare_addr(ctx, vb, sf, &sf->paddr);
  529. if (ret)
  530. goto dma_unlock;
  531. vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  532. ret = fimc_prepare_addr(ctx, vb, df, &df->paddr);
  533. if (ret)
  534. goto dma_unlock;
  535. /* Reconfigure hardware if the context has changed. */
  536. if (fimc->m2m.ctx != ctx) {
  537. ctx->state |= FIMC_PARAMS;
  538. fimc->m2m.ctx = ctx;
  539. }
  540. if (ctx->state & FIMC_PARAMS) {
  541. fimc_set_yuv_order(ctx);
  542. fimc_hw_set_input_path(ctx);
  543. fimc_hw_set_in_dma(ctx);
  544. ret = fimc_set_scaler_info(ctx);
  545. if (ret)
  546. goto dma_unlock;
  547. fimc_hw_set_prescaler(ctx);
  548. fimc_hw_set_mainscaler(ctx);
  549. fimc_hw_set_target_format(ctx);
  550. fimc_hw_set_rotation(ctx);
  551. fimc_hw_set_effect(ctx, false);
  552. fimc_hw_set_out_dma(ctx);
  553. if (fimc->variant->has_alpha)
  554. fimc_hw_set_rgb_alpha(ctx);
  555. fimc_hw_set_output_path(ctx);
  556. }
  557. fimc_hw_set_input_addr(fimc, &sf->paddr);
  558. fimc_hw_set_output_addr(fimc, &df->paddr, -1);
  559. fimc_activate_capture(ctx);
  560. ctx->state &= (FIMC_CTX_M2M | FIMC_CTX_CAP |
  561. FIMC_SRC_FMT | FIMC_DST_FMT);
  562. fimc_hw_activate_input_dma(fimc, true);
  563. dma_unlock:
  564. spin_unlock_irqrestore(&fimc->slock, flags);
  565. }
  566. static void fimc_job_abort(void *priv)
  567. {
  568. fimc_m2m_shutdown(priv);
  569. }
  570. static int fimc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  571. unsigned int *num_buffers, unsigned int *num_planes,
  572. unsigned int sizes[], void *allocators[])
  573. {
  574. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  575. struct fimc_frame *f;
  576. int i;
  577. f = ctx_get_frame(ctx, vq->type);
  578. if (IS_ERR(f))
  579. return PTR_ERR(f);
  580. /*
  581. * Return number of non-contigous planes (plane buffers)
  582. * depending on the configured color format.
  583. */
  584. if (!f->fmt)
  585. return -EINVAL;
  586. *num_planes = f->fmt->memplanes;
  587. for (i = 0; i < f->fmt->memplanes; i++) {
  588. sizes[i] = (f->f_width * f->f_height * f->fmt->depth[i]) / 8;
  589. allocators[i] = ctx->fimc_dev->alloc_ctx;
  590. }
  591. return 0;
  592. }
  593. static int fimc_buf_prepare(struct vb2_buffer *vb)
  594. {
  595. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  596. struct fimc_frame *frame;
  597. int i;
  598. frame = ctx_get_frame(ctx, vb->vb2_queue->type);
  599. if (IS_ERR(frame))
  600. return PTR_ERR(frame);
  601. for (i = 0; i < frame->fmt->memplanes; i++)
  602. vb2_set_plane_payload(vb, i, frame->payload[i]);
  603. return 0;
  604. }
  605. static void fimc_buf_queue(struct vb2_buffer *vb)
  606. {
  607. struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  608. dbg("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
  609. if (ctx->m2m_ctx)
  610. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  611. }
  612. static void fimc_lock(struct vb2_queue *vq)
  613. {
  614. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  615. mutex_lock(&ctx->fimc_dev->lock);
  616. }
  617. static void fimc_unlock(struct vb2_queue *vq)
  618. {
  619. struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
  620. mutex_unlock(&ctx->fimc_dev->lock);
  621. }
  622. static struct vb2_ops fimc_qops = {
  623. .queue_setup = fimc_queue_setup,
  624. .buf_prepare = fimc_buf_prepare,
  625. .buf_queue = fimc_buf_queue,
  626. .wait_prepare = fimc_unlock,
  627. .wait_finish = fimc_lock,
  628. .stop_streaming = stop_streaming,
  629. .start_streaming = start_streaming,
  630. };
  631. /*
  632. * V4L2 controls handling
  633. */
  634. #define ctrl_to_ctx(__ctrl) \
  635. container_of((__ctrl)->handler, struct fimc_ctx, ctrl_handler)
  636. static int __fimc_s_ctrl(struct fimc_ctx *ctx, struct v4l2_ctrl *ctrl)
  637. {
  638. struct fimc_dev *fimc = ctx->fimc_dev;
  639. struct samsung_fimc_variant *variant = fimc->variant;
  640. unsigned int flags = FIMC_DST_FMT | FIMC_SRC_FMT;
  641. int ret = 0;
  642. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  643. return 0;
  644. switch (ctrl->id) {
  645. case V4L2_CID_HFLIP:
  646. ctx->hflip = ctrl->val;
  647. break;
  648. case V4L2_CID_VFLIP:
  649. ctx->vflip = ctrl->val;
  650. break;
  651. case V4L2_CID_ROTATE:
  652. if (fimc_capture_pending(fimc) ||
  653. (ctx->state & flags) == flags) {
  654. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  655. ctx->s_frame.height, ctx->d_frame.width,
  656. ctx->d_frame.height, ctrl->val);
  657. if (ret)
  658. return -EINVAL;
  659. }
  660. if ((ctrl->val == 90 || ctrl->val == 270) &&
  661. !variant->has_out_rot)
  662. return -EINVAL;
  663. ctx->rotation = ctrl->val;
  664. break;
  665. case V4L2_CID_ALPHA_COMPONENT:
  666. ctx->d_frame.alpha = ctrl->val;
  667. break;
  668. }
  669. ctx->state |= FIMC_PARAMS;
  670. set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
  671. return 0;
  672. }
  673. static int fimc_s_ctrl(struct v4l2_ctrl *ctrl)
  674. {
  675. struct fimc_ctx *ctx = ctrl_to_ctx(ctrl);
  676. unsigned long flags;
  677. int ret;
  678. spin_lock_irqsave(&ctx->fimc_dev->slock, flags);
  679. ret = __fimc_s_ctrl(ctx, ctrl);
  680. spin_unlock_irqrestore(&ctx->fimc_dev->slock, flags);
  681. return ret;
  682. }
  683. static const struct v4l2_ctrl_ops fimc_ctrl_ops = {
  684. .s_ctrl = fimc_s_ctrl,
  685. };
  686. int fimc_ctrls_create(struct fimc_ctx *ctx)
  687. {
  688. struct samsung_fimc_variant *variant = ctx->fimc_dev->variant;
  689. unsigned int max_alpha = fimc_get_alpha_mask(ctx->d_frame.fmt);
  690. if (ctx->ctrls_rdy)
  691. return 0;
  692. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 4);
  693. ctx->ctrl_rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
  694. V4L2_CID_ROTATE, 0, 270, 90, 0);
  695. ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
  696. V4L2_CID_HFLIP, 0, 1, 1, 0);
  697. ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &fimc_ctrl_ops,
  698. V4L2_CID_VFLIP, 0, 1, 1, 0);
  699. if (variant->has_alpha)
  700. ctx->ctrl_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  701. &fimc_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
  702. 0, max_alpha, 1, 0);
  703. else
  704. ctx->ctrl_alpha = NULL;
  705. ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
  706. return ctx->ctrl_handler.error;
  707. }
  708. void fimc_ctrls_delete(struct fimc_ctx *ctx)
  709. {
  710. if (ctx->ctrls_rdy) {
  711. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  712. ctx->ctrls_rdy = false;
  713. ctx->ctrl_alpha = NULL;
  714. }
  715. }
  716. void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active)
  717. {
  718. unsigned int has_alpha = ctx->d_frame.fmt->flags & FMT_HAS_ALPHA;
  719. if (!ctx->ctrls_rdy)
  720. return;
  721. mutex_lock(&ctx->ctrl_handler.lock);
  722. v4l2_ctrl_activate(ctx->ctrl_rotate, active);
  723. v4l2_ctrl_activate(ctx->ctrl_hflip, active);
  724. v4l2_ctrl_activate(ctx->ctrl_vflip, active);
  725. if (ctx->ctrl_alpha)
  726. v4l2_ctrl_activate(ctx->ctrl_alpha, active && has_alpha);
  727. if (active) {
  728. ctx->rotation = ctx->ctrl_rotate->val;
  729. ctx->hflip = ctx->ctrl_hflip->val;
  730. ctx->vflip = ctx->ctrl_vflip->val;
  731. } else {
  732. ctx->rotation = 0;
  733. ctx->hflip = 0;
  734. ctx->vflip = 0;
  735. }
  736. mutex_unlock(&ctx->ctrl_handler.lock);
  737. }
  738. /* Update maximum value of the alpha color control */
  739. void fimc_alpha_ctrl_update(struct fimc_ctx *ctx)
  740. {
  741. struct fimc_dev *fimc = ctx->fimc_dev;
  742. struct v4l2_ctrl *ctrl = ctx->ctrl_alpha;
  743. if (ctrl == NULL || !fimc->variant->has_alpha)
  744. return;
  745. v4l2_ctrl_lock(ctrl);
  746. ctrl->maximum = fimc_get_alpha_mask(ctx->d_frame.fmt);
  747. if (ctrl->cur.val > ctrl->maximum)
  748. ctrl->cur.val = ctrl->maximum;
  749. v4l2_ctrl_unlock(ctrl);
  750. }
  751. /*
  752. * V4L2 ioctl handlers
  753. */
  754. static int fimc_m2m_querycap(struct file *file, void *fh,
  755. struct v4l2_capability *cap)
  756. {
  757. struct fimc_ctx *ctx = fh_to_ctx(fh);
  758. struct fimc_dev *fimc = ctx->fimc_dev;
  759. strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
  760. strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
  761. cap->bus_info[0] = 0;
  762. cap->capabilities = V4L2_CAP_STREAMING |
  763. V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  764. return 0;
  765. }
  766. static int fimc_m2m_enum_fmt_mplane(struct file *file, void *priv,
  767. struct v4l2_fmtdesc *f)
  768. {
  769. struct fimc_fmt *fmt;
  770. fmt = fimc_find_format(NULL, NULL, get_m2m_fmt_flags(f->type),
  771. f->index);
  772. if (!fmt)
  773. return -EINVAL;
  774. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  775. f->pixelformat = fmt->fourcc;
  776. return 0;
  777. }
  778. int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f)
  779. {
  780. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  781. int i;
  782. pixm->width = frame->o_width;
  783. pixm->height = frame->o_height;
  784. pixm->field = V4L2_FIELD_NONE;
  785. pixm->pixelformat = frame->fmt->fourcc;
  786. pixm->colorspace = V4L2_COLORSPACE_JPEG;
  787. pixm->num_planes = frame->fmt->memplanes;
  788. for (i = 0; i < pixm->num_planes; ++i) {
  789. int bpl = frame->f_width;
  790. if (frame->fmt->colplanes == 1) /* packed formats */
  791. bpl = (bpl * frame->fmt->depth[0]) / 8;
  792. pixm->plane_fmt[i].bytesperline = bpl;
  793. pixm->plane_fmt[i].sizeimage = (frame->o_width *
  794. frame->o_height * frame->fmt->depth[i]) / 8;
  795. }
  796. return 0;
  797. }
  798. void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f)
  799. {
  800. struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
  801. frame->f_width = pixm->plane_fmt[0].bytesperline;
  802. if (frame->fmt->colplanes == 1)
  803. frame->f_width = (frame->f_width * 8) / frame->fmt->depth[0];
  804. frame->f_height = pixm->height;
  805. frame->width = pixm->width;
  806. frame->height = pixm->height;
  807. frame->o_width = pixm->width;
  808. frame->o_height = pixm->height;
  809. frame->offs_h = 0;
  810. frame->offs_v = 0;
  811. }
  812. /**
  813. * fimc_adjust_mplane_format - adjust bytesperline/sizeimage for each plane
  814. * @fmt: fimc pixel format description (input)
  815. * @width: requested pixel width
  816. * @height: requested pixel height
  817. * @pix: multi-plane format to adjust
  818. */
  819. void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
  820. struct v4l2_pix_format_mplane *pix)
  821. {
  822. u32 bytesperline = 0;
  823. int i;
  824. pix->colorspace = V4L2_COLORSPACE_JPEG;
  825. pix->field = V4L2_FIELD_NONE;
  826. pix->num_planes = fmt->memplanes;
  827. pix->pixelformat = fmt->fourcc;
  828. pix->height = height;
  829. pix->width = width;
  830. for (i = 0; i < pix->num_planes; ++i) {
  831. u32 bpl = pix->plane_fmt[i].bytesperline;
  832. u32 *sizeimage = &pix->plane_fmt[i].sizeimage;
  833. if (fmt->colplanes > 1 && (bpl == 0 || bpl < pix->width))
  834. bpl = pix->width; /* Planar */
  835. if (fmt->colplanes == 1 && /* Packed */
  836. (bpl == 0 || ((bpl * 8) / fmt->depth[i]) < pix->width))
  837. bpl = (pix->width * fmt->depth[0]) / 8;
  838. if (i == 0) /* Same bytesperline for each plane. */
  839. bytesperline = bpl;
  840. pix->plane_fmt[i].bytesperline = bytesperline;
  841. *sizeimage = (pix->width * pix->height * fmt->depth[i]) / 8;
  842. }
  843. }
  844. static int fimc_m2m_g_fmt_mplane(struct file *file, void *fh,
  845. struct v4l2_format *f)
  846. {
  847. struct fimc_ctx *ctx = fh_to_ctx(fh);
  848. struct fimc_frame *frame = ctx_get_frame(ctx, f->type);
  849. if (IS_ERR(frame))
  850. return PTR_ERR(frame);
  851. return fimc_fill_format(frame, f);
  852. }
  853. /**
  854. * fimc_find_format - lookup fimc color format by fourcc or media bus format
  855. * @pixelformat: fourcc to match, ignored if null
  856. * @mbus_code: media bus code to match, ignored if null
  857. * @mask: the color flags to match
  858. * @index: offset in the fimc_formats array, ignored if negative
  859. */
  860. struct fimc_fmt *fimc_find_format(u32 *pixelformat, u32 *mbus_code,
  861. unsigned int mask, int index)
  862. {
  863. struct fimc_fmt *fmt, *def_fmt = NULL;
  864. unsigned int i;
  865. int id = 0;
  866. if (index >= ARRAY_SIZE(fimc_formats))
  867. return NULL;
  868. for (i = 0; i < ARRAY_SIZE(fimc_formats); ++i) {
  869. fmt = &fimc_formats[i];
  870. if (!(fmt->flags & mask))
  871. continue;
  872. if (pixelformat && fmt->fourcc == *pixelformat)
  873. return fmt;
  874. if (mbus_code && fmt->mbus_code == *mbus_code)
  875. return fmt;
  876. if (index == id)
  877. def_fmt = fmt;
  878. id++;
  879. }
  880. return def_fmt;
  881. }
  882. static int fimc_try_fmt_mplane(struct fimc_ctx *ctx, struct v4l2_format *f)
  883. {
  884. struct fimc_dev *fimc = ctx->fimc_dev;
  885. struct samsung_fimc_variant *variant = fimc->variant;
  886. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  887. struct fimc_fmt *fmt;
  888. u32 max_w, mod_x, mod_y;
  889. if (!IS_M2M(f->type))
  890. return -EINVAL;
  891. dbg("w: %d, h: %d", pix->width, pix->height);
  892. fmt = fimc_find_format(&pix->pixelformat, NULL,
  893. get_m2m_fmt_flags(f->type), 0);
  894. if (WARN(fmt == NULL, "Pixel format lookup failed"))
  895. return -EINVAL;
  896. if (pix->field == V4L2_FIELD_ANY)
  897. pix->field = V4L2_FIELD_NONE;
  898. else if (pix->field != V4L2_FIELD_NONE)
  899. return -EINVAL;
  900. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  901. max_w = variant->pix_limit->scaler_dis_w;
  902. mod_x = ffs(variant->min_inp_pixsize) - 1;
  903. } else {
  904. max_w = variant->pix_limit->out_rot_dis_w;
  905. mod_x = ffs(variant->min_out_pixsize) - 1;
  906. }
  907. if (tiled_fmt(fmt)) {
  908. mod_x = 6; /* 64 x 32 pixels tile */
  909. mod_y = 5;
  910. } else {
  911. if (variant->min_vsize_align == 1)
  912. mod_y = fimc_fmt_is_rgb(fmt->color) ? 0 : 1;
  913. else
  914. mod_y = ffs(variant->min_vsize_align) - 1;
  915. }
  916. v4l_bound_align_image(&pix->width, 16, max_w, mod_x,
  917. &pix->height, 8, variant->pix_limit->scaler_dis_w, mod_y, 0);
  918. fimc_adjust_mplane_format(fmt, pix->width, pix->height, &f->fmt.pix_mp);
  919. return 0;
  920. }
  921. static int fimc_m2m_try_fmt_mplane(struct file *file, void *fh,
  922. struct v4l2_format *f)
  923. {
  924. struct fimc_ctx *ctx = fh_to_ctx(fh);
  925. return fimc_try_fmt_mplane(ctx, f);
  926. }
  927. static int fimc_m2m_s_fmt_mplane(struct file *file, void *fh,
  928. struct v4l2_format *f)
  929. {
  930. struct fimc_ctx *ctx = fh_to_ctx(fh);
  931. struct fimc_dev *fimc = ctx->fimc_dev;
  932. struct vb2_queue *vq;
  933. struct fimc_frame *frame;
  934. struct v4l2_pix_format_mplane *pix;
  935. int i, ret = 0;
  936. ret = fimc_try_fmt_mplane(ctx, f);
  937. if (ret)
  938. return ret;
  939. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  940. if (vb2_is_busy(vq)) {
  941. v4l2_err(fimc->m2m.vfd, "queue (%d) busy\n", f->type);
  942. return -EBUSY;
  943. }
  944. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  945. frame = &ctx->s_frame;
  946. else
  947. frame = &ctx->d_frame;
  948. pix = &f->fmt.pix_mp;
  949. frame->fmt = fimc_find_format(&pix->pixelformat, NULL,
  950. get_m2m_fmt_flags(f->type), 0);
  951. if (!frame->fmt)
  952. return -EINVAL;
  953. /* Update RGB Alpha control state and value range */
  954. fimc_alpha_ctrl_update(ctx);
  955. for (i = 0; i < frame->fmt->colplanes; i++) {
  956. frame->payload[i] =
  957. (pix->width * pix->height * frame->fmt->depth[i]) / 8;
  958. }
  959. fimc_fill_frame(frame, f);
  960. ctx->scaler.enabled = 1;
  961. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  962. fimc_ctx_state_set(FIMC_PARAMS | FIMC_DST_FMT, ctx);
  963. else
  964. fimc_ctx_state_set(FIMC_PARAMS | FIMC_SRC_FMT, ctx);
  965. dbg("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
  966. return 0;
  967. }
  968. static int fimc_m2m_reqbufs(struct file *file, void *fh,
  969. struct v4l2_requestbuffers *reqbufs)
  970. {
  971. struct fimc_ctx *ctx = fh_to_ctx(fh);
  972. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  973. }
  974. static int fimc_m2m_querybuf(struct file *file, void *fh,
  975. struct v4l2_buffer *buf)
  976. {
  977. struct fimc_ctx *ctx = fh_to_ctx(fh);
  978. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  979. }
  980. static int fimc_m2m_qbuf(struct file *file, void *fh,
  981. struct v4l2_buffer *buf)
  982. {
  983. struct fimc_ctx *ctx = fh_to_ctx(fh);
  984. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  985. }
  986. static int fimc_m2m_dqbuf(struct file *file, void *fh,
  987. struct v4l2_buffer *buf)
  988. {
  989. struct fimc_ctx *ctx = fh_to_ctx(fh);
  990. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  991. }
  992. static int fimc_m2m_streamon(struct file *file, void *fh,
  993. enum v4l2_buf_type type)
  994. {
  995. struct fimc_ctx *ctx = fh_to_ctx(fh);
  996. /* The source and target color format need to be set */
  997. if (V4L2_TYPE_IS_OUTPUT(type)) {
  998. if (!fimc_ctx_state_is_set(FIMC_SRC_FMT, ctx))
  999. return -EINVAL;
  1000. } else if (!fimc_ctx_state_is_set(FIMC_DST_FMT, ctx)) {
  1001. return -EINVAL;
  1002. }
  1003. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  1004. }
  1005. static int fimc_m2m_streamoff(struct file *file, void *fh,
  1006. enum v4l2_buf_type type)
  1007. {
  1008. struct fimc_ctx *ctx = fh_to_ctx(fh);
  1009. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  1010. }
  1011. static int fimc_m2m_cropcap(struct file *file, void *fh,
  1012. struct v4l2_cropcap *cr)
  1013. {
  1014. struct fimc_ctx *ctx = fh_to_ctx(fh);
  1015. struct fimc_frame *frame;
  1016. frame = ctx_get_frame(ctx, cr->type);
  1017. if (IS_ERR(frame))
  1018. return PTR_ERR(frame);
  1019. cr->bounds.left = 0;
  1020. cr->bounds.top = 0;
  1021. cr->bounds.width = frame->o_width;
  1022. cr->bounds.height = frame->o_height;
  1023. cr->defrect = cr->bounds;
  1024. return 0;
  1025. }
  1026. static int fimc_m2m_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  1027. {
  1028. struct fimc_ctx *ctx = fh_to_ctx(fh);
  1029. struct fimc_frame *frame;
  1030. frame = ctx_get_frame(ctx, cr->type);
  1031. if (IS_ERR(frame))
  1032. return PTR_ERR(frame);
  1033. cr->c.left = frame->offs_h;
  1034. cr->c.top = frame->offs_v;
  1035. cr->c.width = frame->width;
  1036. cr->c.height = frame->height;
  1037. return 0;
  1038. }
  1039. static int fimc_m2m_try_crop(struct fimc_ctx *ctx, struct v4l2_crop *cr)
  1040. {
  1041. struct fimc_dev *fimc = ctx->fimc_dev;
  1042. struct fimc_frame *f;
  1043. u32 min_size, halign, depth = 0;
  1044. int i;
  1045. if (cr->c.top < 0 || cr->c.left < 0) {
  1046. v4l2_err(fimc->m2m.vfd,
  1047. "doesn't support negative values for top & left\n");
  1048. return -EINVAL;
  1049. }
  1050. if (cr->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1051. f = &ctx->d_frame;
  1052. else if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  1053. f = &ctx->s_frame;
  1054. else
  1055. return -EINVAL;
  1056. min_size = (f == &ctx->s_frame) ?
  1057. fimc->variant->min_inp_pixsize : fimc->variant->min_out_pixsize;
  1058. /* Get pixel alignment constraints. */
  1059. if (fimc->variant->min_vsize_align == 1)
  1060. halign = fimc_fmt_is_rgb(f->fmt->color) ? 0 : 1;
  1061. else
  1062. halign = ffs(fimc->variant->min_vsize_align) - 1;
  1063. for (i = 0; i < f->fmt->colplanes; i++)
  1064. depth += f->fmt->depth[i];
  1065. v4l_bound_align_image(&cr->c.width, min_size, f->o_width,
  1066. ffs(min_size) - 1,
  1067. &cr->c.height, min_size, f->o_height,
  1068. halign, 64/(ALIGN(depth, 8)));
  1069. /* adjust left/top if cropping rectangle is out of bounds */
  1070. if (cr->c.left + cr->c.width > f->o_width)
  1071. cr->c.left = f->o_width - cr->c.width;
  1072. if (cr->c.top + cr->c.height > f->o_height)
  1073. cr->c.top = f->o_height - cr->c.height;
  1074. cr->c.left = round_down(cr->c.left, min_size);
  1075. cr->c.top = round_down(cr->c.top, fimc->variant->hor_offs_align);
  1076. dbg("l:%d, t:%d, w:%d, h:%d, f_w: %d, f_h: %d",
  1077. cr->c.left, cr->c.top, cr->c.width, cr->c.height,
  1078. f->f_width, f->f_height);
  1079. return 0;
  1080. }
  1081. static int fimc_m2m_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
  1082. {
  1083. struct fimc_ctx *ctx = fh_to_ctx(fh);
  1084. struct fimc_dev *fimc = ctx->fimc_dev;
  1085. struct fimc_frame *f;
  1086. int ret;
  1087. ret = fimc_m2m_try_crop(ctx, cr);
  1088. if (ret)
  1089. return ret;
  1090. f = (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
  1091. &ctx->s_frame : &ctx->d_frame;
  1092. /* Check to see if scaling ratio is within supported range */
  1093. if (fimc_ctx_state_is_set(FIMC_DST_FMT | FIMC_SRC_FMT, ctx)) {
  1094. if (cr->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1095. ret = fimc_check_scaler_ratio(ctx, cr->c.width,
  1096. cr->c.height, ctx->d_frame.width,
  1097. ctx->d_frame.height, ctx->rotation);
  1098. } else {
  1099. ret = fimc_check_scaler_ratio(ctx, ctx->s_frame.width,
  1100. ctx->s_frame.height, cr->c.width,
  1101. cr->c.height, ctx->rotation);
  1102. }
  1103. if (ret) {
  1104. v4l2_err(fimc->m2m.vfd, "Out of scaler range\n");
  1105. return -EINVAL;
  1106. }
  1107. }
  1108. f->offs_h = cr->c.left;
  1109. f->offs_v = cr->c.top;
  1110. f->width = cr->c.width;
  1111. f->height = cr->c.height;
  1112. fimc_ctx_state_set(FIMC_PARAMS, ctx);
  1113. return 0;
  1114. }
  1115. static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
  1116. .vidioc_querycap = fimc_m2m_querycap,
  1117. .vidioc_enum_fmt_vid_cap_mplane = fimc_m2m_enum_fmt_mplane,
  1118. .vidioc_enum_fmt_vid_out_mplane = fimc_m2m_enum_fmt_mplane,
  1119. .vidioc_g_fmt_vid_cap_mplane = fimc_m2m_g_fmt_mplane,
  1120. .vidioc_g_fmt_vid_out_mplane = fimc_m2m_g_fmt_mplane,
  1121. .vidioc_try_fmt_vid_cap_mplane = fimc_m2m_try_fmt_mplane,
  1122. .vidioc_try_fmt_vid_out_mplane = fimc_m2m_try_fmt_mplane,
  1123. .vidioc_s_fmt_vid_cap_mplane = fimc_m2m_s_fmt_mplane,
  1124. .vidioc_s_fmt_vid_out_mplane = fimc_m2m_s_fmt_mplane,
  1125. .vidioc_reqbufs = fimc_m2m_reqbufs,
  1126. .vidioc_querybuf = fimc_m2m_querybuf,
  1127. .vidioc_qbuf = fimc_m2m_qbuf,
  1128. .vidioc_dqbuf = fimc_m2m_dqbuf,
  1129. .vidioc_streamon = fimc_m2m_streamon,
  1130. .vidioc_streamoff = fimc_m2m_streamoff,
  1131. .vidioc_g_crop = fimc_m2m_g_crop,
  1132. .vidioc_s_crop = fimc_m2m_s_crop,
  1133. .vidioc_cropcap = fimc_m2m_cropcap
  1134. };
  1135. static int queue_init(void *priv, struct vb2_queue *src_vq,
  1136. struct vb2_queue *dst_vq)
  1137. {
  1138. struct fimc_ctx *ctx = priv;
  1139. int ret;
  1140. memset(src_vq, 0, sizeof(*src_vq));
  1141. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1142. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1143. src_vq->drv_priv = ctx;
  1144. src_vq->ops = &fimc_qops;
  1145. src_vq->mem_ops = &vb2_dma_contig_memops;
  1146. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1147. ret = vb2_queue_init(src_vq);
  1148. if (ret)
  1149. return ret;
  1150. memset(dst_vq, 0, sizeof(*dst_vq));
  1151. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  1152. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1153. dst_vq->drv_priv = ctx;
  1154. dst_vq->ops = &fimc_qops;
  1155. dst_vq->mem_ops = &vb2_dma_contig_memops;
  1156. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  1157. return vb2_queue_init(dst_vq);
  1158. }
  1159. static int fimc_m2m_open(struct file *file)
  1160. {
  1161. struct fimc_dev *fimc = video_drvdata(file);
  1162. struct fimc_ctx *ctx;
  1163. int ret;
  1164. dbg("pid: %d, state: 0x%lx, refcnt: %d",
  1165. task_pid_nr(current), fimc->state, fimc->vid_cap.refcnt);
  1166. /*
  1167. * Return if the corresponding video capture node
  1168. * is already opened.
  1169. */
  1170. if (fimc->vid_cap.refcnt > 0)
  1171. return -EBUSY;
  1172. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  1173. if (!ctx)
  1174. return -ENOMEM;
  1175. v4l2_fh_init(&ctx->fh, fimc->m2m.vfd);
  1176. ctx->fimc_dev = fimc;
  1177. /* Default color format */
  1178. ctx->s_frame.fmt = &fimc_formats[0];
  1179. ctx->d_frame.fmt = &fimc_formats[0];
  1180. ret = fimc_ctrls_create(ctx);
  1181. if (ret)
  1182. goto error_fh;
  1183. /* Use separate control handler per file handle */
  1184. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  1185. file->private_data = &ctx->fh;
  1186. v4l2_fh_add(&ctx->fh);
  1187. /* Setup the device context for memory-to-memory mode */
  1188. ctx->state = FIMC_CTX_M2M;
  1189. ctx->flags = 0;
  1190. ctx->in_path = FIMC_DMA;
  1191. ctx->out_path = FIMC_DMA;
  1192. ctx->m2m_ctx = v4l2_m2m_ctx_init(fimc->m2m.m2m_dev, ctx, queue_init);
  1193. if (IS_ERR(ctx->m2m_ctx)) {
  1194. ret = PTR_ERR(ctx->m2m_ctx);
  1195. goto error_c;
  1196. }
  1197. if (fimc->m2m.refcnt++ == 0)
  1198. set_bit(ST_M2M_RUN, &fimc->state);
  1199. return 0;
  1200. error_c:
  1201. fimc_ctrls_delete(ctx);
  1202. error_fh:
  1203. v4l2_fh_del(&ctx->fh);
  1204. v4l2_fh_exit(&ctx->fh);
  1205. kfree(ctx);
  1206. return ret;
  1207. }
  1208. static int fimc_m2m_release(struct file *file)
  1209. {
  1210. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  1211. struct fimc_dev *fimc = ctx->fimc_dev;
  1212. dbg("pid: %d, state: 0x%lx, refcnt= %d",
  1213. task_pid_nr(current), fimc->state, fimc->m2m.refcnt);
  1214. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  1215. fimc_ctrls_delete(ctx);
  1216. v4l2_fh_del(&ctx->fh);
  1217. v4l2_fh_exit(&ctx->fh);
  1218. if (--fimc->m2m.refcnt <= 0)
  1219. clear_bit(ST_M2M_RUN, &fimc->state);
  1220. kfree(ctx);
  1221. return 0;
  1222. }
  1223. static unsigned int fimc_m2m_poll(struct file *file,
  1224. struct poll_table_struct *wait)
  1225. {
  1226. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  1227. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  1228. }
  1229. static int fimc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
  1230. {
  1231. struct fimc_ctx *ctx = fh_to_ctx(file->private_data);
  1232. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  1233. }
  1234. static const struct v4l2_file_operations fimc_m2m_fops = {
  1235. .owner = THIS_MODULE,
  1236. .open = fimc_m2m_open,
  1237. .release = fimc_m2m_release,
  1238. .poll = fimc_m2m_poll,
  1239. .unlocked_ioctl = video_ioctl2,
  1240. .mmap = fimc_m2m_mmap,
  1241. };
  1242. static struct v4l2_m2m_ops m2m_ops = {
  1243. .device_run = fimc_dma_run,
  1244. .job_abort = fimc_job_abort,
  1245. };
  1246. int fimc_register_m2m_device(struct fimc_dev *fimc,
  1247. struct v4l2_device *v4l2_dev)
  1248. {
  1249. struct video_device *vfd;
  1250. struct platform_device *pdev;
  1251. int ret = 0;
  1252. if (!fimc)
  1253. return -ENODEV;
  1254. pdev = fimc->pdev;
  1255. fimc->v4l2_dev = v4l2_dev;
  1256. vfd = video_device_alloc();
  1257. if (!vfd) {
  1258. v4l2_err(v4l2_dev, "Failed to allocate video device\n");
  1259. return -ENOMEM;
  1260. }
  1261. vfd->fops = &fimc_m2m_fops;
  1262. vfd->ioctl_ops = &fimc_m2m_ioctl_ops;
  1263. vfd->v4l2_dev = v4l2_dev;
  1264. vfd->minor = -1;
  1265. vfd->release = video_device_release;
  1266. vfd->lock = &fimc->lock;
  1267. /* Locking in file operations other than ioctl should be done
  1268. by the driver, not the V4L2 core.
  1269. This driver needs auditing so that this flag can be removed. */
  1270. set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
  1271. snprintf(vfd->name, sizeof(vfd->name), "%s.m2m", dev_name(&pdev->dev));
  1272. video_set_drvdata(vfd, fimc);
  1273. fimc->m2m.vfd = vfd;
  1274. fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
  1275. if (IS_ERR(fimc->m2m.m2m_dev)) {
  1276. v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
  1277. ret = PTR_ERR(fimc->m2m.m2m_dev);
  1278. goto err_init;
  1279. }
  1280. ret = media_entity_init(&vfd->entity, 0, NULL, 0);
  1281. if (!ret)
  1282. return 0;
  1283. v4l2_m2m_release(fimc->m2m.m2m_dev);
  1284. err_init:
  1285. video_device_release(fimc->m2m.vfd);
  1286. return ret;
  1287. }
  1288. void fimc_unregister_m2m_device(struct fimc_dev *fimc)
  1289. {
  1290. if (!fimc)
  1291. return;
  1292. if (fimc->m2m.m2m_dev)
  1293. v4l2_m2m_release(fimc->m2m.m2m_dev);
  1294. if (fimc->m2m.vfd) {
  1295. media_entity_cleanup(&fimc->m2m.vfd->entity);
  1296. /* Can also be called if video device wasn't registered */
  1297. video_unregister_device(fimc->m2m.vfd);
  1298. }
  1299. }
  1300. static void fimc_clk_put(struct fimc_dev *fimc)
  1301. {
  1302. int i;
  1303. for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
  1304. if (IS_ERR_OR_NULL(fimc->clock[i]))
  1305. continue;
  1306. clk_unprepare(fimc->clock[i]);
  1307. clk_put(fimc->clock[i]);
  1308. fimc->clock[i] = NULL;
  1309. }
  1310. }
  1311. static int fimc_clk_get(struct fimc_dev *fimc)
  1312. {
  1313. int i, ret;
  1314. for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
  1315. fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
  1316. if (IS_ERR(fimc->clock[i]))
  1317. goto err;
  1318. ret = clk_prepare(fimc->clock[i]);
  1319. if (ret < 0) {
  1320. clk_put(fimc->clock[i]);
  1321. fimc->clock[i] = NULL;
  1322. goto err;
  1323. }
  1324. }
  1325. return 0;
  1326. err:
  1327. fimc_clk_put(fimc);
  1328. dev_err(&fimc->pdev->dev, "failed to get clock: %s\n",
  1329. fimc_clocks[i]);
  1330. return -ENXIO;
  1331. }
  1332. static int fimc_m2m_suspend(struct fimc_dev *fimc)
  1333. {
  1334. unsigned long flags;
  1335. int timeout;
  1336. spin_lock_irqsave(&fimc->slock, flags);
  1337. if (!fimc_m2m_pending(fimc)) {
  1338. spin_unlock_irqrestore(&fimc->slock, flags);
  1339. return 0;
  1340. }
  1341. clear_bit(ST_M2M_SUSPENDED, &fimc->state);
  1342. set_bit(ST_M2M_SUSPENDING, &fimc->state);
  1343. spin_unlock_irqrestore(&fimc->slock, flags);
  1344. timeout = wait_event_timeout(fimc->irq_queue,
  1345. test_bit(ST_M2M_SUSPENDED, &fimc->state),
  1346. FIMC_SHUTDOWN_TIMEOUT);
  1347. clear_bit(ST_M2M_SUSPENDING, &fimc->state);
  1348. return timeout == 0 ? -EAGAIN : 0;
  1349. }
  1350. static int fimc_m2m_resume(struct fimc_dev *fimc)
  1351. {
  1352. unsigned long flags;
  1353. spin_lock_irqsave(&fimc->slock, flags);
  1354. /* Clear for full H/W setup in first run after resume */
  1355. fimc->m2m.ctx = NULL;
  1356. spin_unlock_irqrestore(&fimc->slock, flags);
  1357. if (test_and_clear_bit(ST_M2M_SUSPENDED, &fimc->state))
  1358. fimc_m2m_job_finish(fimc->m2m.ctx,
  1359. VB2_BUF_STATE_ERROR);
  1360. return 0;
  1361. }
  1362. static int fimc_probe(struct platform_device *pdev)
  1363. {
  1364. struct fimc_dev *fimc;
  1365. struct resource *res;
  1366. struct samsung_fimc_driverdata *drv_data;
  1367. struct s5p_platform_fimc *pdata;
  1368. int ret = 0;
  1369. drv_data = (struct samsung_fimc_driverdata *)
  1370. platform_get_device_id(pdev)->driver_data;
  1371. if (pdev->id >= drv_data->num_entities) {
  1372. dev_err(&pdev->dev, "Invalid platform device id: %d\n",
  1373. pdev->id);
  1374. return -EINVAL;
  1375. }
  1376. fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
  1377. if (!fimc)
  1378. return -ENOMEM;
  1379. fimc->id = pdev->id;
  1380. fimc->variant = drv_data->variant[fimc->id];
  1381. fimc->pdev = pdev;
  1382. pdata = pdev->dev.platform_data;
  1383. fimc->pdata = pdata;
  1384. init_waitqueue_head(&fimc->irq_queue);
  1385. spin_lock_init(&fimc->slock);
  1386. mutex_init(&fimc->lock);
  1387. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1388. fimc->regs = devm_request_and_ioremap(&pdev->dev, res);
  1389. if (fimc->regs == NULL) {
  1390. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  1391. return -ENOENT;
  1392. }
  1393. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1394. if (res == NULL) {
  1395. dev_err(&pdev->dev, "Failed to get IRQ resource\n");
  1396. return -ENXIO;
  1397. }
  1398. ret = fimc_clk_get(fimc);
  1399. if (ret)
  1400. return ret;
  1401. clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
  1402. clk_enable(fimc->clock[CLK_BUS]);
  1403. platform_set_drvdata(pdev, fimc);
  1404. ret = devm_request_irq(&pdev->dev, res->start, fimc_irq_handler,
  1405. 0, pdev->name, fimc);
  1406. if (ret) {
  1407. dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
  1408. goto err_clk;
  1409. }
  1410. pm_runtime_enable(&pdev->dev);
  1411. ret = pm_runtime_get_sync(&pdev->dev);
  1412. if (ret < 0)
  1413. goto err_clk;
  1414. /* Initialize contiguous memory allocator */
  1415. fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1416. if (IS_ERR(fimc->alloc_ctx)) {
  1417. ret = PTR_ERR(fimc->alloc_ctx);
  1418. goto err_pm;
  1419. }
  1420. dev_dbg(&pdev->dev, "FIMC.%d registered successfully\n", fimc->id);
  1421. pm_runtime_put(&pdev->dev);
  1422. return 0;
  1423. err_pm:
  1424. pm_runtime_put(&pdev->dev);
  1425. err_clk:
  1426. fimc_clk_put(fimc);
  1427. return ret;
  1428. }
  1429. static int fimc_runtime_resume(struct device *dev)
  1430. {
  1431. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1432. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1433. /* Enable clocks and perform basic initalization */
  1434. clk_enable(fimc->clock[CLK_GATE]);
  1435. fimc_hw_reset(fimc);
  1436. /* Resume the capture or mem-to-mem device */
  1437. if (fimc_capture_busy(fimc))
  1438. return fimc_capture_resume(fimc);
  1439. return fimc_m2m_resume(fimc);
  1440. }
  1441. static int fimc_runtime_suspend(struct device *dev)
  1442. {
  1443. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1444. int ret = 0;
  1445. if (fimc_capture_busy(fimc))
  1446. ret = fimc_capture_suspend(fimc);
  1447. else
  1448. ret = fimc_m2m_suspend(fimc);
  1449. if (!ret)
  1450. clk_disable(fimc->clock[CLK_GATE]);
  1451. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1452. return ret;
  1453. }
  1454. #ifdef CONFIG_PM_SLEEP
  1455. static int fimc_resume(struct device *dev)
  1456. {
  1457. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1458. unsigned long flags;
  1459. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1460. /* Do not resume if the device was idle before system suspend */
  1461. spin_lock_irqsave(&fimc->slock, flags);
  1462. if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
  1463. (!fimc_m2m_active(fimc) && !fimc_capture_busy(fimc))) {
  1464. spin_unlock_irqrestore(&fimc->slock, flags);
  1465. return 0;
  1466. }
  1467. fimc_hw_reset(fimc);
  1468. spin_unlock_irqrestore(&fimc->slock, flags);
  1469. if (fimc_capture_busy(fimc))
  1470. return fimc_capture_resume(fimc);
  1471. return fimc_m2m_resume(fimc);
  1472. }
  1473. static int fimc_suspend(struct device *dev)
  1474. {
  1475. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1476. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1477. if (test_and_set_bit(ST_LPM, &fimc->state))
  1478. return 0;
  1479. if (fimc_capture_busy(fimc))
  1480. return fimc_capture_suspend(fimc);
  1481. return fimc_m2m_suspend(fimc);
  1482. }
  1483. #endif /* CONFIG_PM_SLEEP */
  1484. static int __devexit fimc_remove(struct platform_device *pdev)
  1485. {
  1486. struct fimc_dev *fimc = platform_get_drvdata(pdev);
  1487. pm_runtime_disable(&pdev->dev);
  1488. pm_runtime_set_suspended(&pdev->dev);
  1489. vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
  1490. clk_disable(fimc->clock[CLK_BUS]);
  1491. fimc_clk_put(fimc);
  1492. dev_info(&pdev->dev, "driver unloaded\n");
  1493. return 0;
  1494. }
  1495. /* Image pixel limits, similar across several FIMC HW revisions. */
  1496. static struct fimc_pix_limit s5p_pix_limit[4] = {
  1497. [0] = {
  1498. .scaler_en_w = 3264,
  1499. .scaler_dis_w = 8192,
  1500. .in_rot_en_h = 1920,
  1501. .in_rot_dis_w = 8192,
  1502. .out_rot_en_w = 1920,
  1503. .out_rot_dis_w = 4224,
  1504. },
  1505. [1] = {
  1506. .scaler_en_w = 4224,
  1507. .scaler_dis_w = 8192,
  1508. .in_rot_en_h = 1920,
  1509. .in_rot_dis_w = 8192,
  1510. .out_rot_en_w = 1920,
  1511. .out_rot_dis_w = 4224,
  1512. },
  1513. [2] = {
  1514. .scaler_en_w = 1920,
  1515. .scaler_dis_w = 8192,
  1516. .in_rot_en_h = 1280,
  1517. .in_rot_dis_w = 8192,
  1518. .out_rot_en_w = 1280,
  1519. .out_rot_dis_w = 1920,
  1520. },
  1521. [3] = {
  1522. .scaler_en_w = 1920,
  1523. .scaler_dis_w = 8192,
  1524. .in_rot_en_h = 1366,
  1525. .in_rot_dis_w = 8192,
  1526. .out_rot_en_w = 1366,
  1527. .out_rot_dis_w = 1920,
  1528. },
  1529. };
  1530. static struct samsung_fimc_variant fimc0_variant_s5p = {
  1531. .has_inp_rot = 1,
  1532. .has_out_rot = 1,
  1533. .has_cam_if = 1,
  1534. .min_inp_pixsize = 16,
  1535. .min_out_pixsize = 16,
  1536. .hor_offs_align = 8,
  1537. .min_vsize_align = 16,
  1538. .out_buf_count = 4,
  1539. .pix_limit = &s5p_pix_limit[0],
  1540. };
  1541. static struct samsung_fimc_variant fimc2_variant_s5p = {
  1542. .has_cam_if = 1,
  1543. .min_inp_pixsize = 16,
  1544. .min_out_pixsize = 16,
  1545. .hor_offs_align = 8,
  1546. .min_vsize_align = 16,
  1547. .out_buf_count = 4,
  1548. .pix_limit = &s5p_pix_limit[1],
  1549. };
  1550. static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
  1551. .pix_hoff = 1,
  1552. .has_inp_rot = 1,
  1553. .has_out_rot = 1,
  1554. .has_cam_if = 1,
  1555. .min_inp_pixsize = 16,
  1556. .min_out_pixsize = 16,
  1557. .hor_offs_align = 8,
  1558. .min_vsize_align = 16,
  1559. .out_buf_count = 4,
  1560. .pix_limit = &s5p_pix_limit[1],
  1561. };
  1562. static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
  1563. .pix_hoff = 1,
  1564. .has_inp_rot = 1,
  1565. .has_out_rot = 1,
  1566. .has_cam_if = 1,
  1567. .has_mainscaler_ext = 1,
  1568. .min_inp_pixsize = 16,
  1569. .min_out_pixsize = 16,
  1570. .hor_offs_align = 1,
  1571. .min_vsize_align = 1,
  1572. .out_buf_count = 4,
  1573. .pix_limit = &s5p_pix_limit[2],
  1574. };
  1575. static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
  1576. .has_cam_if = 1,
  1577. .pix_hoff = 1,
  1578. .min_inp_pixsize = 16,
  1579. .min_out_pixsize = 16,
  1580. .hor_offs_align = 8,
  1581. .min_vsize_align = 16,
  1582. .out_buf_count = 4,
  1583. .pix_limit = &s5p_pix_limit[2],
  1584. };
  1585. static struct samsung_fimc_variant fimc0_variant_exynos4 = {
  1586. .pix_hoff = 1,
  1587. .has_inp_rot = 1,
  1588. .has_out_rot = 1,
  1589. .has_cam_if = 1,
  1590. .has_cistatus2 = 1,
  1591. .has_mainscaler_ext = 1,
  1592. .has_alpha = 1,
  1593. .min_inp_pixsize = 16,
  1594. .min_out_pixsize = 16,
  1595. .hor_offs_align = 2,
  1596. .min_vsize_align = 1,
  1597. .out_buf_count = 32,
  1598. .pix_limit = &s5p_pix_limit[1],
  1599. };
  1600. static struct samsung_fimc_variant fimc3_variant_exynos4 = {
  1601. .pix_hoff = 1,
  1602. .has_cam_if = 1,
  1603. .has_cistatus2 = 1,
  1604. .has_mainscaler_ext = 1,
  1605. .has_alpha = 1,
  1606. .min_inp_pixsize = 16,
  1607. .min_out_pixsize = 16,
  1608. .hor_offs_align = 2,
  1609. .min_vsize_align = 1,
  1610. .out_buf_count = 32,
  1611. .pix_limit = &s5p_pix_limit[3],
  1612. };
  1613. /* S5PC100 */
  1614. static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
  1615. .variant = {
  1616. [0] = &fimc0_variant_s5p,
  1617. [1] = &fimc0_variant_s5p,
  1618. [2] = &fimc2_variant_s5p,
  1619. },
  1620. .num_entities = 3,
  1621. .lclk_frequency = 133000000UL,
  1622. };
  1623. /* S5PV210, S5PC110 */
  1624. static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
  1625. .variant = {
  1626. [0] = &fimc0_variant_s5pv210,
  1627. [1] = &fimc1_variant_s5pv210,
  1628. [2] = &fimc2_variant_s5pv210,
  1629. },
  1630. .num_entities = 3,
  1631. .lclk_frequency = 166000000UL,
  1632. };
  1633. /* S5PV310, S5PC210 */
  1634. static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
  1635. .variant = {
  1636. [0] = &fimc0_variant_exynos4,
  1637. [1] = &fimc0_variant_exynos4,
  1638. [2] = &fimc0_variant_exynos4,
  1639. [3] = &fimc3_variant_exynos4,
  1640. },
  1641. .num_entities = 4,
  1642. .lclk_frequency = 166000000UL,
  1643. };
  1644. static struct platform_device_id fimc_driver_ids[] = {
  1645. {
  1646. .name = "s5p-fimc",
  1647. .driver_data = (unsigned long)&fimc_drvdata_s5p,
  1648. }, {
  1649. .name = "s5pv210-fimc",
  1650. .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
  1651. }, {
  1652. .name = "exynos4-fimc",
  1653. .driver_data = (unsigned long)&fimc_drvdata_exynos4,
  1654. },
  1655. {},
  1656. };
  1657. MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
  1658. static const struct dev_pm_ops fimc_pm_ops = {
  1659. SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
  1660. SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
  1661. };
  1662. static struct platform_driver fimc_driver = {
  1663. .probe = fimc_probe,
  1664. .remove = __devexit_p(fimc_remove),
  1665. .id_table = fimc_driver_ids,
  1666. .driver = {
  1667. .name = FIMC_MODULE_NAME,
  1668. .owner = THIS_MODULE,
  1669. .pm = &fimc_pm_ops,
  1670. }
  1671. };
  1672. int __init fimc_register_driver(void)
  1673. {
  1674. return platform_driver_register(&fimc_driver);
  1675. }
  1676. void __exit fimc_unregister_driver(void)
  1677. {
  1678. platform_driver_unregister(&fimc_driver);
  1679. }