fimc-core.c 45 KB

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