fimc-core.c 49 KB

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