fimc-core.c 45 KB

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