fimc-core.c 45 KB

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