fimc-core.c 45 KB

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