fimc-core.c 45 KB

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