fimc-core.c 45 KB

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