fimc-core.c 50 KB

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