fimc-core.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  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. snprintf(vfd->name, sizeof(vfd->name), "%s.m2m", dev_name(&pdev->dev));
  1268. video_set_drvdata(vfd, fimc);
  1269. fimc->m2m.vfd = vfd;
  1270. fimc->m2m.m2m_dev = v4l2_m2m_init(&m2m_ops);
  1271. if (IS_ERR(fimc->m2m.m2m_dev)) {
  1272. v4l2_err(v4l2_dev, "failed to initialize v4l2-m2m device\n");
  1273. ret = PTR_ERR(fimc->m2m.m2m_dev);
  1274. goto err_init;
  1275. }
  1276. ret = media_entity_init(&vfd->entity, 0, NULL, 0);
  1277. if (!ret)
  1278. return 0;
  1279. v4l2_m2m_release(fimc->m2m.m2m_dev);
  1280. err_init:
  1281. video_device_release(fimc->m2m.vfd);
  1282. return ret;
  1283. }
  1284. void fimc_unregister_m2m_device(struct fimc_dev *fimc)
  1285. {
  1286. if (!fimc)
  1287. return;
  1288. if (fimc->m2m.m2m_dev)
  1289. v4l2_m2m_release(fimc->m2m.m2m_dev);
  1290. if (fimc->m2m.vfd) {
  1291. media_entity_cleanup(&fimc->m2m.vfd->entity);
  1292. /* Can also be called if video device wasn't registered */
  1293. video_unregister_device(fimc->m2m.vfd);
  1294. }
  1295. }
  1296. static void fimc_clk_put(struct fimc_dev *fimc)
  1297. {
  1298. int i;
  1299. for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
  1300. if (IS_ERR_OR_NULL(fimc->clock[i]))
  1301. continue;
  1302. clk_unprepare(fimc->clock[i]);
  1303. clk_put(fimc->clock[i]);
  1304. fimc->clock[i] = NULL;
  1305. }
  1306. }
  1307. static int fimc_clk_get(struct fimc_dev *fimc)
  1308. {
  1309. int i, ret;
  1310. for (i = 0; i < MAX_FIMC_CLOCKS; i++) {
  1311. fimc->clock[i] = clk_get(&fimc->pdev->dev, fimc_clocks[i]);
  1312. if (IS_ERR(fimc->clock[i]))
  1313. goto err;
  1314. ret = clk_prepare(fimc->clock[i]);
  1315. if (ret < 0) {
  1316. clk_put(fimc->clock[i]);
  1317. fimc->clock[i] = NULL;
  1318. goto err;
  1319. }
  1320. }
  1321. return 0;
  1322. err:
  1323. fimc_clk_put(fimc);
  1324. dev_err(&fimc->pdev->dev, "failed to get clock: %s\n",
  1325. fimc_clocks[i]);
  1326. return -ENXIO;
  1327. }
  1328. static int fimc_m2m_suspend(struct fimc_dev *fimc)
  1329. {
  1330. unsigned long flags;
  1331. int timeout;
  1332. spin_lock_irqsave(&fimc->slock, flags);
  1333. if (!fimc_m2m_pending(fimc)) {
  1334. spin_unlock_irqrestore(&fimc->slock, flags);
  1335. return 0;
  1336. }
  1337. clear_bit(ST_M2M_SUSPENDED, &fimc->state);
  1338. set_bit(ST_M2M_SUSPENDING, &fimc->state);
  1339. spin_unlock_irqrestore(&fimc->slock, flags);
  1340. timeout = wait_event_timeout(fimc->irq_queue,
  1341. test_bit(ST_M2M_SUSPENDED, &fimc->state),
  1342. FIMC_SHUTDOWN_TIMEOUT);
  1343. clear_bit(ST_M2M_SUSPENDING, &fimc->state);
  1344. return timeout == 0 ? -EAGAIN : 0;
  1345. }
  1346. static int fimc_m2m_resume(struct fimc_dev *fimc)
  1347. {
  1348. unsigned long flags;
  1349. spin_lock_irqsave(&fimc->slock, flags);
  1350. /* Clear for full H/W setup in first run after resume */
  1351. fimc->m2m.ctx = NULL;
  1352. spin_unlock_irqrestore(&fimc->slock, flags);
  1353. if (test_and_clear_bit(ST_M2M_SUSPENDED, &fimc->state))
  1354. fimc_m2m_job_finish(fimc->m2m.ctx,
  1355. VB2_BUF_STATE_ERROR);
  1356. return 0;
  1357. }
  1358. static int fimc_probe(struct platform_device *pdev)
  1359. {
  1360. struct fimc_dev *fimc;
  1361. struct resource *res;
  1362. struct samsung_fimc_driverdata *drv_data;
  1363. struct s5p_platform_fimc *pdata;
  1364. int ret = 0;
  1365. drv_data = (struct samsung_fimc_driverdata *)
  1366. platform_get_device_id(pdev)->driver_data;
  1367. if (pdev->id >= drv_data->num_entities) {
  1368. dev_err(&pdev->dev, "Invalid platform device id: %d\n",
  1369. pdev->id);
  1370. return -EINVAL;
  1371. }
  1372. fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
  1373. if (!fimc)
  1374. return -ENOMEM;
  1375. fimc->id = pdev->id;
  1376. fimc->variant = drv_data->variant[fimc->id];
  1377. fimc->pdev = pdev;
  1378. pdata = pdev->dev.platform_data;
  1379. fimc->pdata = pdata;
  1380. init_waitqueue_head(&fimc->irq_queue);
  1381. spin_lock_init(&fimc->slock);
  1382. mutex_init(&fimc->lock);
  1383. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1384. fimc->regs = devm_request_and_ioremap(&pdev->dev, res);
  1385. if (fimc->regs == NULL) {
  1386. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  1387. return -ENOENT;
  1388. }
  1389. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1390. if (res == NULL) {
  1391. dev_err(&pdev->dev, "Failed to get IRQ resource\n");
  1392. return -ENXIO;
  1393. }
  1394. ret = fimc_clk_get(fimc);
  1395. if (ret)
  1396. return ret;
  1397. clk_set_rate(fimc->clock[CLK_BUS], drv_data->lclk_frequency);
  1398. clk_enable(fimc->clock[CLK_BUS]);
  1399. platform_set_drvdata(pdev, fimc);
  1400. ret = devm_request_irq(&pdev->dev, res->start, fimc_irq_handler,
  1401. 0, pdev->name, fimc);
  1402. if (ret) {
  1403. dev_err(&pdev->dev, "failed to install irq (%d)\n", ret);
  1404. goto err_clk;
  1405. }
  1406. pm_runtime_enable(&pdev->dev);
  1407. ret = pm_runtime_get_sync(&pdev->dev);
  1408. if (ret < 0)
  1409. goto err_clk;
  1410. /* Initialize contiguous memory allocator */
  1411. fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1412. if (IS_ERR(fimc->alloc_ctx)) {
  1413. ret = PTR_ERR(fimc->alloc_ctx);
  1414. goto err_pm;
  1415. }
  1416. dev_dbg(&pdev->dev, "FIMC.%d registered successfully\n", fimc->id);
  1417. pm_runtime_put(&pdev->dev);
  1418. return 0;
  1419. err_pm:
  1420. pm_runtime_put(&pdev->dev);
  1421. err_clk:
  1422. fimc_clk_put(fimc);
  1423. return ret;
  1424. }
  1425. static int fimc_runtime_resume(struct device *dev)
  1426. {
  1427. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1428. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1429. /* Enable clocks and perform basic initalization */
  1430. clk_enable(fimc->clock[CLK_GATE]);
  1431. fimc_hw_reset(fimc);
  1432. /* Resume the capture or mem-to-mem device */
  1433. if (fimc_capture_busy(fimc))
  1434. return fimc_capture_resume(fimc);
  1435. return fimc_m2m_resume(fimc);
  1436. }
  1437. static int fimc_runtime_suspend(struct device *dev)
  1438. {
  1439. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1440. int ret = 0;
  1441. if (fimc_capture_busy(fimc))
  1442. ret = fimc_capture_suspend(fimc);
  1443. else
  1444. ret = fimc_m2m_suspend(fimc);
  1445. if (!ret)
  1446. clk_disable(fimc->clock[CLK_GATE]);
  1447. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1448. return ret;
  1449. }
  1450. #ifdef CONFIG_PM_SLEEP
  1451. static int fimc_resume(struct device *dev)
  1452. {
  1453. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1454. unsigned long flags;
  1455. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1456. /* Do not resume if the device was idle before system suspend */
  1457. spin_lock_irqsave(&fimc->slock, flags);
  1458. if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
  1459. (!fimc_m2m_active(fimc) && !fimc_capture_busy(fimc))) {
  1460. spin_unlock_irqrestore(&fimc->slock, flags);
  1461. return 0;
  1462. }
  1463. fimc_hw_reset(fimc);
  1464. spin_unlock_irqrestore(&fimc->slock, flags);
  1465. if (fimc_capture_busy(fimc))
  1466. return fimc_capture_resume(fimc);
  1467. return fimc_m2m_resume(fimc);
  1468. }
  1469. static int fimc_suspend(struct device *dev)
  1470. {
  1471. struct fimc_dev *fimc = dev_get_drvdata(dev);
  1472. dbg("fimc%d: state: 0x%lx", fimc->id, fimc->state);
  1473. if (test_and_set_bit(ST_LPM, &fimc->state))
  1474. return 0;
  1475. if (fimc_capture_busy(fimc))
  1476. return fimc_capture_suspend(fimc);
  1477. return fimc_m2m_suspend(fimc);
  1478. }
  1479. #endif /* CONFIG_PM_SLEEP */
  1480. static int __devexit fimc_remove(struct platform_device *pdev)
  1481. {
  1482. struct fimc_dev *fimc = platform_get_drvdata(pdev);
  1483. pm_runtime_disable(&pdev->dev);
  1484. pm_runtime_set_suspended(&pdev->dev);
  1485. vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
  1486. clk_disable(fimc->clock[CLK_BUS]);
  1487. fimc_clk_put(fimc);
  1488. dev_info(&pdev->dev, "driver unloaded\n");
  1489. return 0;
  1490. }
  1491. /* Image pixel limits, similar across several FIMC HW revisions. */
  1492. static struct fimc_pix_limit s5p_pix_limit[4] = {
  1493. [0] = {
  1494. .scaler_en_w = 3264,
  1495. .scaler_dis_w = 8192,
  1496. .in_rot_en_h = 1920,
  1497. .in_rot_dis_w = 8192,
  1498. .out_rot_en_w = 1920,
  1499. .out_rot_dis_w = 4224,
  1500. },
  1501. [1] = {
  1502. .scaler_en_w = 4224,
  1503. .scaler_dis_w = 8192,
  1504. .in_rot_en_h = 1920,
  1505. .in_rot_dis_w = 8192,
  1506. .out_rot_en_w = 1920,
  1507. .out_rot_dis_w = 4224,
  1508. },
  1509. [2] = {
  1510. .scaler_en_w = 1920,
  1511. .scaler_dis_w = 8192,
  1512. .in_rot_en_h = 1280,
  1513. .in_rot_dis_w = 8192,
  1514. .out_rot_en_w = 1280,
  1515. .out_rot_dis_w = 1920,
  1516. },
  1517. [3] = {
  1518. .scaler_en_w = 1920,
  1519. .scaler_dis_w = 8192,
  1520. .in_rot_en_h = 1366,
  1521. .in_rot_dis_w = 8192,
  1522. .out_rot_en_w = 1366,
  1523. .out_rot_dis_w = 1920,
  1524. },
  1525. };
  1526. static struct samsung_fimc_variant fimc0_variant_s5p = {
  1527. .has_inp_rot = 1,
  1528. .has_out_rot = 1,
  1529. .has_cam_if = 1,
  1530. .min_inp_pixsize = 16,
  1531. .min_out_pixsize = 16,
  1532. .hor_offs_align = 8,
  1533. .min_vsize_align = 16,
  1534. .out_buf_count = 4,
  1535. .pix_limit = &s5p_pix_limit[0],
  1536. };
  1537. static struct samsung_fimc_variant fimc2_variant_s5p = {
  1538. .has_cam_if = 1,
  1539. .min_inp_pixsize = 16,
  1540. .min_out_pixsize = 16,
  1541. .hor_offs_align = 8,
  1542. .min_vsize_align = 16,
  1543. .out_buf_count = 4,
  1544. .pix_limit = &s5p_pix_limit[1],
  1545. };
  1546. static struct samsung_fimc_variant fimc0_variant_s5pv210 = {
  1547. .pix_hoff = 1,
  1548. .has_inp_rot = 1,
  1549. .has_out_rot = 1,
  1550. .has_cam_if = 1,
  1551. .min_inp_pixsize = 16,
  1552. .min_out_pixsize = 16,
  1553. .hor_offs_align = 8,
  1554. .min_vsize_align = 16,
  1555. .out_buf_count = 4,
  1556. .pix_limit = &s5p_pix_limit[1],
  1557. };
  1558. static struct samsung_fimc_variant fimc1_variant_s5pv210 = {
  1559. .pix_hoff = 1,
  1560. .has_inp_rot = 1,
  1561. .has_out_rot = 1,
  1562. .has_cam_if = 1,
  1563. .has_mainscaler_ext = 1,
  1564. .min_inp_pixsize = 16,
  1565. .min_out_pixsize = 16,
  1566. .hor_offs_align = 1,
  1567. .min_vsize_align = 1,
  1568. .out_buf_count = 4,
  1569. .pix_limit = &s5p_pix_limit[2],
  1570. };
  1571. static struct samsung_fimc_variant fimc2_variant_s5pv210 = {
  1572. .has_cam_if = 1,
  1573. .pix_hoff = 1,
  1574. .min_inp_pixsize = 16,
  1575. .min_out_pixsize = 16,
  1576. .hor_offs_align = 8,
  1577. .min_vsize_align = 16,
  1578. .out_buf_count = 4,
  1579. .pix_limit = &s5p_pix_limit[2],
  1580. };
  1581. static struct samsung_fimc_variant fimc0_variant_exynos4 = {
  1582. .pix_hoff = 1,
  1583. .has_inp_rot = 1,
  1584. .has_out_rot = 1,
  1585. .has_cam_if = 1,
  1586. .has_cistatus2 = 1,
  1587. .has_mainscaler_ext = 1,
  1588. .has_alpha = 1,
  1589. .min_inp_pixsize = 16,
  1590. .min_out_pixsize = 16,
  1591. .hor_offs_align = 2,
  1592. .min_vsize_align = 1,
  1593. .out_buf_count = 32,
  1594. .pix_limit = &s5p_pix_limit[1],
  1595. };
  1596. static struct samsung_fimc_variant fimc3_variant_exynos4 = {
  1597. .pix_hoff = 1,
  1598. .has_cam_if = 1,
  1599. .has_cistatus2 = 1,
  1600. .has_mainscaler_ext = 1,
  1601. .has_alpha = 1,
  1602. .min_inp_pixsize = 16,
  1603. .min_out_pixsize = 16,
  1604. .hor_offs_align = 2,
  1605. .min_vsize_align = 1,
  1606. .out_buf_count = 32,
  1607. .pix_limit = &s5p_pix_limit[3],
  1608. };
  1609. /* S5PC100 */
  1610. static struct samsung_fimc_driverdata fimc_drvdata_s5p = {
  1611. .variant = {
  1612. [0] = &fimc0_variant_s5p,
  1613. [1] = &fimc0_variant_s5p,
  1614. [2] = &fimc2_variant_s5p,
  1615. },
  1616. .num_entities = 3,
  1617. .lclk_frequency = 133000000UL,
  1618. };
  1619. /* S5PV210, S5PC110 */
  1620. static struct samsung_fimc_driverdata fimc_drvdata_s5pv210 = {
  1621. .variant = {
  1622. [0] = &fimc0_variant_s5pv210,
  1623. [1] = &fimc1_variant_s5pv210,
  1624. [2] = &fimc2_variant_s5pv210,
  1625. },
  1626. .num_entities = 3,
  1627. .lclk_frequency = 166000000UL,
  1628. };
  1629. /* S5PV310, S5PC210 */
  1630. static struct samsung_fimc_driverdata fimc_drvdata_exynos4 = {
  1631. .variant = {
  1632. [0] = &fimc0_variant_exynos4,
  1633. [1] = &fimc0_variant_exynos4,
  1634. [2] = &fimc0_variant_exynos4,
  1635. [3] = &fimc3_variant_exynos4,
  1636. },
  1637. .num_entities = 4,
  1638. .lclk_frequency = 166000000UL,
  1639. };
  1640. static struct platform_device_id fimc_driver_ids[] = {
  1641. {
  1642. .name = "s5p-fimc",
  1643. .driver_data = (unsigned long)&fimc_drvdata_s5p,
  1644. }, {
  1645. .name = "s5pv210-fimc",
  1646. .driver_data = (unsigned long)&fimc_drvdata_s5pv210,
  1647. }, {
  1648. .name = "exynos4-fimc",
  1649. .driver_data = (unsigned long)&fimc_drvdata_exynos4,
  1650. },
  1651. {},
  1652. };
  1653. MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
  1654. static const struct dev_pm_ops fimc_pm_ops = {
  1655. SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
  1656. SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
  1657. };
  1658. static struct platform_driver fimc_driver = {
  1659. .probe = fimc_probe,
  1660. .remove = __devexit_p(fimc_remove),
  1661. .id_table = fimc_driver_ids,
  1662. .driver = {
  1663. .name = FIMC_MODULE_NAME,
  1664. .owner = THIS_MODULE,
  1665. .pm = &fimc_pm_ops,
  1666. }
  1667. };
  1668. int __init fimc_register_driver(void)
  1669. {
  1670. return platform_driver_probe(&fimc_driver, fimc_probe);
  1671. }
  1672. void __exit fimc_unregister_driver(void)
  1673. {
  1674. platform_driver_unregister(&fimc_driver);
  1675. }