fimc-core.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  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(const u32 *pixelformat, const 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 >= (int)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. drv_data = (struct samsung_fimc_driverdata *)
  1401. platform_get_device_id(pdev)->driver_data;
  1402. if (pdev->id >= drv_data->num_entities) {
  1403. dev_err(&pdev->dev, "Invalid platform device id: %d\n",
  1404. pdev->id);
  1405. return -EINVAL;
  1406. }
  1407. fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
  1408. if (!fimc)
  1409. return -ENOMEM;
  1410. fimc->id = pdev->id;
  1411. fimc->variant = drv_data->variant[fimc->id];
  1412. fimc->pdev = pdev;
  1413. pdata = pdev->dev.platform_data;
  1414. fimc->pdata = pdata;
  1415. init_waitqueue_head(&fimc->irq_queue);
  1416. spin_lock_init(&fimc->slock);
  1417. mutex_init(&fimc->lock);
  1418. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1419. fimc->regs = devm_request_and_ioremap(&pdev->dev, res);
  1420. if (fimc->regs == NULL) {
  1421. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  1422. return -ENOENT;
  1423. }
  1424. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1425. if (res == NULL) {
  1426. dev_err(&pdev->dev, "Failed to get IRQ resource\n");
  1427. return -ENXIO;
  1428. }
  1429. fimc->irq = res->start;
  1430. fimc->num_clocks = MAX_FIMC_CLOCKS;
  1431. ret = fimc_clk_get(fimc);
  1432. if (ret)
  1433. return ret;
  1434. clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
  1435. clk_enable(fimc->clock[CLK_BUS]);
  1436. platform_set_drvdata(pdev, fimc);
  1437. ret = devm_request_irq(&pdev->dev, fimc->irq, fimc_irq_handler,
  1438. 0, pdev->name, fimc);
  1439. if (ret) {
  1440. dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
  1441. goto err_clk;
  1442. }
  1443. pm_runtime_enable(&pdev->dev);
  1444. ret = pm_runtime_get_sync(&pdev->dev);
  1445. if (ret < 0)
  1446. goto err_clk;
  1447. /* Initialize contiguous memory allocator */
  1448. fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1449. if (IS_ERR(fimc->alloc_ctx)) {
  1450. ret = PTR_ERR(fimc->alloc_ctx);
  1451. goto err_pm;
  1452. }
  1453. dev_dbg(&pdev->dev, "FIMC.%d registered successfully\n", fimc->id);
  1454. pm_runtime_put(&pdev->dev);
  1455. return 0;
  1456. err_pm:
  1457. pm_runtime_put(&pdev->dev);
  1458. err_clk:
  1459. fimc_clk_put(fimc);
  1460. return ret;
  1461. }
  1462. static int fimc_runtime_resume(struct device *dev)
  1463. {
  1464. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1465. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1466. /* Enable clocks and perform basic initalization */
  1467. clk_enable(fimc->clock[CLK_GATE]);
  1468. fimc_hw_reset(fimc);
  1469. /* Resume the capture or mem-to-mem device */
  1470. if (fimc_capture_busy(fimc))
  1471. return fimc_capture_resume(fimc);
  1472. return fimc_m2m_resume(fimc);
  1473. }
  1474. static int fimc_runtime_suspend(struct device *dev)
  1475. {
  1476. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1477. int ret = 0;
  1478. if (fimc_capture_busy(fimc))
  1479. ret = fimc_capture_suspend(fimc);
  1480. else
  1481. ret = fimc_m2m_suspend(fimc);
  1482. if (!ret)
  1483. clk_disable(fimc->clock[CLK_GATE]);
  1484. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1485. return ret;
  1486. }
  1487. #ifdef CONFIG_PM_SLEEP
  1488. static int fimc_resume(struct device *dev)
  1489. {
  1490. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1491. unsigned long flags;
  1492. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1493. /* Do not resume if the device was idle before system suspend */
  1494. spin_lock_irqsave(&fimc->slock, flags);
  1495. if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
  1496. (!fimc_m2m_active(fimc) && !fimc_capture_busy(fimc))) {
  1497. spin_unlock_irqrestore(&fimc->slock, flags);
  1498. return 0;
  1499. }
  1500. fimc_hw_reset(fimc);
  1501. spin_unlock_irqrestore(&fimc->slock, flags);
  1502. if (fimc_capture_busy(fimc))
  1503. return fimc_capture_resume(fimc);
  1504. return fimc_m2m_resume(fimc);
  1505. }
  1506. static int fimc_suspend(struct device *dev)
  1507. {
  1508. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1509. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1510. if (test_and_set_bit(ST_LPM, &fimc->state))
  1511. return 0;
  1512. if (fimc_capture_busy(fimc))
  1513. return fimc_capture_suspend(fimc);
  1514. return fimc_m2m_suspend(fimc);
  1515. }
  1516. #endif /* CONFIG_PM_SLEEP */
  1517. static int __devexit fimc_remove(struct platform_device *pdev)
  1518. {
  1519. struct fimc_dev *fimc = platform_get_drvdata(pdev);
  1520. pm_runtime_disable(&pdev->dev);
  1521. pm_runtime_set_suspended(&pdev->dev);
  1522. vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
  1523. clk_disable(fimc->clock[CLK_BUS]);
  1524. fimc_clk_put(fimc);
  1525. dev_info(&pdev->dev, "driver unloaded\n");
  1526. return 0;
  1527. }
  1528. /* Image pixel limits, similar across several FIMC HW revisions. */
  1529. static struct fimc_pix_limit s5p_pix_limit[4] = {
  1530. [0] = {
  1531. .scaler_en_w = 3264,
  1532. .scaler_dis_w = 8192,
  1533. .in_rot_en_h = 1920,
  1534. .in_rot_dis_w = 8192,
  1535. .out_rot_en_w = 1920,
  1536. .out_rot_dis_w = 4224,
  1537. },
  1538. [1] = {
  1539. .scaler_en_w = 4224,
  1540. .scaler_dis_w = 8192,
  1541. .in_rot_en_h = 1920,
  1542. .in_rot_dis_w = 8192,
  1543. .out_rot_en_w = 1920,
  1544. .out_rot_dis_w = 4224,
  1545. },
  1546. [2] = {
  1547. .scaler_en_w = 1920,
  1548. .scaler_dis_w = 8192,
  1549. .in_rot_en_h = 1280,
  1550. .in_rot_dis_w = 8192,
  1551. .out_rot_en_w = 1280,
  1552. .out_rot_dis_w = 1920,
  1553. },
  1554. [3] = {
  1555. .scaler_en_w = 1920,
  1556. .scaler_dis_w = 8192,
  1557. .in_rot_en_h = 1366,
  1558. .in_rot_dis_w = 8192,
  1559. .out_rot_en_w = 1366,
  1560. .out_rot_dis_w = 1920,
  1561. },
  1562. };
  1563. static struct samsung_fimc_variant fimc0_variant_s5p = {
  1564. .has_inp_rot = 1,
  1565. .has_out_rot = 1,
  1566. .has_cam_if = 1,
  1567. .min_inp_pixsize = 16,
  1568. .min_out_pixsize = 16,
  1569. .hor_offs_align = 8,
  1570. .min_vsize_align = 16,
  1571. .out_buf_count = 4,
  1572. .pix_limit = &s5p_pix_limit[0],
  1573. };
  1574. static struct samsung_fimc_variant fimc2_variant_s5p = {
  1575. .has_cam_if = 1,
  1576. .min_inp_pixsize = 16,
  1577. .min_out_pixsize = 16,
  1578. .hor_offs_align = 8,
  1579. .min_vsize_align = 16,
  1580. .out_buf_count = 4,
  1581. .pix_limit = &s5p_pix_limit[1],
  1582. };
  1583. static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
  1584. .pix_hoff = 1,
  1585. .has_inp_rot = 1,
  1586. .has_out_rot = 1,
  1587. .has_cam_if = 1,
  1588. .min_inp_pixsize = 16,
  1589. .min_out_pixsize = 16,
  1590. .hor_offs_align = 8,
  1591. .min_vsize_align = 16,
  1592. .out_buf_count = 4,
  1593. .pix_limit = &s5p_pix_limit[1],
  1594. };
  1595. static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
  1596. .pix_hoff = 1,
  1597. .has_inp_rot = 1,
  1598. .has_out_rot = 1,
  1599. .has_cam_if = 1,
  1600. .has_mainscaler_ext = 1,
  1601. .min_inp_pixsize = 16,
  1602. .min_out_pixsize = 16,
  1603. .hor_offs_align = 1,
  1604. .min_vsize_align = 1,
  1605. .out_buf_count = 4,
  1606. .pix_limit = &s5p_pix_limit[2],
  1607. };
  1608. static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
  1609. .has_cam_if = 1,
  1610. .pix_hoff = 1,
  1611. .min_inp_pixsize = 16,
  1612. .min_out_pixsize = 16,
  1613. .hor_offs_align = 8,
  1614. .min_vsize_align = 16,
  1615. .out_buf_count = 4,
  1616. .pix_limit = &s5p_pix_limit[2],
  1617. };
  1618. static struct samsung_fimc_variant fimc0_variant_exynos4 = {
  1619. .pix_hoff = 1,
  1620. .has_inp_rot = 1,
  1621. .has_out_rot = 1,
  1622. .has_cam_if = 1,
  1623. .has_cistatus2 = 1,
  1624. .has_mainscaler_ext = 1,
  1625. .has_alpha = 1,
  1626. .min_inp_pixsize = 16,
  1627. .min_out_pixsize = 16,
  1628. .hor_offs_align = 2,
  1629. .min_vsize_align = 1,
  1630. .out_buf_count = 32,
  1631. .pix_limit = &s5p_pix_limit[1],
  1632. };
  1633. static struct samsung_fimc_variant fimc3_variant_exynos4 = {
  1634. .pix_hoff = 1,
  1635. .has_cam_if = 1,
  1636. .has_cistatus2 = 1,
  1637. .has_mainscaler_ext = 1,
  1638. .has_alpha = 1,
  1639. .min_inp_pixsize = 16,
  1640. .min_out_pixsize = 16,
  1641. .hor_offs_align = 2,
  1642. .min_vsize_align = 1,
  1643. .out_buf_count = 32,
  1644. .pix_limit = &s5p_pix_limit[3],
  1645. };
  1646. /* S5PC100 */
  1647. static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
  1648. .variant = {
  1649. [0] = &fimc0_variant_s5p,
  1650. [1] = &fimc0_variant_s5p,
  1651. [2] = &fimc2_variant_s5p,
  1652. },
  1653. .num_entities = 3,
  1654. .lclk_frequency = 133000000UL,
  1655. };
  1656. /* S5PV210, S5PC110 */
  1657. static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
  1658. .variant = {
  1659. [0] = &fimc0_variant_s5pv210,
  1660. [1] = &fimc1_variant_s5pv210,
  1661. [2] = &fimc2_variant_s5pv210,
  1662. },
  1663. .num_entities = 3,
  1664. .lclk_frequency = 166000000UL,
  1665. };
  1666. /* S5PV310, S5PC210 */
  1667. static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
  1668. .variant = {
  1669. [0] = &fimc0_variant_exynos4,
  1670. [1] = &fimc0_variant_exynos4,
  1671. [2] = &fimc0_variant_exynos4,
  1672. [3] = &fimc3_variant_exynos4,
  1673. },
  1674. .num_entities = 4,
  1675. .lclk_frequency = 166000000UL,
  1676. };
  1677. static struct platform_device_id fimc_driver_ids[] = {
  1678. {
  1679. .name = "s5p-fimc",
  1680. .driver_data = (unsigned long)&fimc_drvdata_s5p,
  1681. }, {
  1682. .name = "s5pv210-fimc",
  1683. .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
  1684. }, {
  1685. .name = "exynos4-fimc",
  1686. .driver_data = (unsigned long)&fimc_drvdata_exynos4,
  1687. },
  1688. {},
  1689. };
  1690. MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
  1691. static const struct dev_pm_ops fimc_pm_ops = {
  1692. SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
  1693. SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
  1694. };
  1695. static struct platform_driver fimc_driver = {
  1696. .probe = fimc_probe,
  1697. .remove = __devexit_p(fimc_remove),
  1698. .id_table = fimc_driver_ids,
  1699. .driver = {
  1700. .name = FIMC_MODULE_NAME,
  1701. .owner = THIS_MODULE,
  1702. .pm = &fimc_pm_ops,
  1703. }
  1704. };
  1705. int __init fimc_register_driver(void)
  1706. {
  1707. return platform_driver_probe(&fimc_driver, fimc_probe);
  1708. }
  1709. void __exit fimc_unregister_driver(void)
  1710. {
  1711. platform_driver_unregister(&fimc_driver);
  1712. }