exynos_drm_fimc.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Eunchul Kim <chulspro.kim@samsung.com>
  5. * Jinyoung Jeon <jy0.jeon@samsung.com>
  6. * Sangmin Lee <lsmin.lee@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/regmap.h>
  18. #include <linux/clk.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/of.h>
  21. #include <linux/spinlock.h>
  22. #include <drm/drmP.h>
  23. #include <drm/exynos_drm.h>
  24. #include "regs-fimc.h"
  25. #include "exynos_drm_drv.h"
  26. #include "exynos_drm_ipp.h"
  27. #include "exynos_drm_fimc.h"
  28. /*
  29. * FIMC stands for Fully Interactive Mobile Camera and
  30. * supports image scaler/rotator and input/output DMA operations.
  31. * input DMA reads image data from the memory.
  32. * output DMA writes image data to memory.
  33. * FIMC supports image rotation and image effect functions.
  34. *
  35. * M2M operation : supports crop/scale/rotation/csc so on.
  36. * Memory ----> FIMC H/W ----> Memory.
  37. * Writeback operation : supports cloned screen with FIMD.
  38. * FIMD ----> FIMC H/W ----> Memory.
  39. * Output operation : supports direct display using local path.
  40. * Memory ----> FIMC H/W ----> FIMD.
  41. */
  42. /*
  43. * TODO
  44. * 1. check suspend/resume api if needed.
  45. * 2. need to check use case platform_device_id.
  46. * 3. check src/dst size with, height.
  47. * 4. added check_prepare api for right register.
  48. * 5. need to add supported list in prop_list.
  49. * 6. check prescaler/scaler optimization.
  50. */
  51. #define FIMC_MAX_DEVS 4
  52. #define FIMC_MAX_SRC 2
  53. #define FIMC_MAX_DST 32
  54. #define FIMC_SHFACTOR 10
  55. #define FIMC_BUF_STOP 1
  56. #define FIMC_BUF_START 2
  57. #define FIMC_WIDTH_ITU_709 1280
  58. #define FIMC_REFRESH_MAX 60
  59. #define FIMC_REFRESH_MIN 12
  60. #define FIMC_CROP_MAX 8192
  61. #define FIMC_CROP_MIN 32
  62. #define FIMC_SCALE_MAX 4224
  63. #define FIMC_SCALE_MIN 32
  64. #define get_fimc_context(dev) platform_get_drvdata(to_platform_device(dev))
  65. #define get_ctx_from_ippdrv(ippdrv) container_of(ippdrv,\
  66. struct fimc_context, ippdrv);
  67. enum fimc_wb {
  68. FIMC_WB_NONE,
  69. FIMC_WB_A,
  70. FIMC_WB_B,
  71. };
  72. enum {
  73. FIMC_CLK_LCLK,
  74. FIMC_CLK_GATE,
  75. FIMC_CLK_WB_A,
  76. FIMC_CLK_WB_B,
  77. FIMC_CLK_MUX,
  78. FIMC_CLK_PARENT,
  79. FIMC_CLKS_MAX
  80. };
  81. static const char * const fimc_clock_names[] = {
  82. [FIMC_CLK_LCLK] = "sclk_fimc",
  83. [FIMC_CLK_GATE] = "fimc",
  84. [FIMC_CLK_WB_A] = "pxl_async0",
  85. [FIMC_CLK_WB_B] = "pxl_async1",
  86. [FIMC_CLK_MUX] = "mux",
  87. [FIMC_CLK_PARENT] = "parent",
  88. };
  89. #define FIMC_DEFAULT_LCLK_FREQUENCY 133000000UL
  90. /*
  91. * A structure of scaler.
  92. *
  93. * @range: narrow, wide.
  94. * @bypass: unused scaler path.
  95. * @up_h: horizontal scale up.
  96. * @up_v: vertical scale up.
  97. * @hratio: horizontal ratio.
  98. * @vratio: vertical ratio.
  99. */
  100. struct fimc_scaler {
  101. bool range;
  102. bool bypass;
  103. bool up_h;
  104. bool up_v;
  105. u32 hratio;
  106. u32 vratio;
  107. };
  108. /*
  109. * A structure of scaler capability.
  110. *
  111. * find user manual table 43-1.
  112. * @in_hori: scaler input horizontal size.
  113. * @bypass: scaler bypass mode.
  114. * @dst_h_wo_rot: target horizontal size without output rotation.
  115. * @dst_h_rot: target horizontal size with output rotation.
  116. * @rl_w_wo_rot: real width without input rotation.
  117. * @rl_h_rot: real height without output rotation.
  118. */
  119. struct fimc_capability {
  120. /* scaler */
  121. u32 in_hori;
  122. u32 bypass;
  123. /* output rotator */
  124. u32 dst_h_wo_rot;
  125. u32 dst_h_rot;
  126. /* input rotator */
  127. u32 rl_w_wo_rot;
  128. u32 rl_h_rot;
  129. };
  130. /*
  131. * A structure of fimc context.
  132. *
  133. * @ippdrv: prepare initialization using ippdrv.
  134. * @regs_res: register resources.
  135. * @regs: memory mapped io registers.
  136. * @lock: locking of operations.
  137. * @clocks: fimc clocks.
  138. * @clk_frequency: LCLK clock frequency.
  139. * @sysreg: handle to SYSREG block regmap.
  140. * @sc: scaler infomations.
  141. * @pol: porarity of writeback.
  142. * @id: fimc id.
  143. * @irq: irq number.
  144. * @suspended: qos operations.
  145. */
  146. struct fimc_context {
  147. struct exynos_drm_ippdrv ippdrv;
  148. struct resource *regs_res;
  149. void __iomem *regs;
  150. spinlock_t lock;
  151. struct clk *clocks[FIMC_CLKS_MAX];
  152. u32 clk_frequency;
  153. struct regmap *sysreg;
  154. struct fimc_scaler sc;
  155. struct exynos_drm_ipp_pol pol;
  156. int id;
  157. int irq;
  158. bool suspended;
  159. };
  160. static u32 fimc_read(struct fimc_context *ctx, u32 reg)
  161. {
  162. return readl(ctx->regs + reg);
  163. }
  164. static void fimc_write(struct fimc_context *ctx, u32 val, u32 reg)
  165. {
  166. writel(val, ctx->regs + reg);
  167. }
  168. static void fimc_set_bits(struct fimc_context *ctx, u32 reg, u32 bits)
  169. {
  170. void __iomem *r = ctx->regs + reg;
  171. writel(readl(r) | bits, r);
  172. }
  173. static void fimc_clear_bits(struct fimc_context *ctx, u32 reg, u32 bits)
  174. {
  175. void __iomem *r = ctx->regs + reg;
  176. writel(readl(r) & ~bits, r);
  177. }
  178. static void fimc_sw_reset(struct fimc_context *ctx)
  179. {
  180. u32 cfg;
  181. /* stop dma operation */
  182. cfg = fimc_read(ctx, EXYNOS_CISTATUS);
  183. if (EXYNOS_CISTATUS_GET_ENVID_STATUS(cfg))
  184. fimc_clear_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
  185. fimc_set_bits(ctx, EXYNOS_CISRCFMT, EXYNOS_CISRCFMT_ITU601_8BIT);
  186. /* disable image capture */
  187. fimc_clear_bits(ctx, EXYNOS_CIIMGCPT,
  188. EXYNOS_CIIMGCPT_IMGCPTEN_SC | EXYNOS_CIIMGCPT_IMGCPTEN);
  189. /* s/w reset */
  190. fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_SWRST);
  191. /* s/w reset complete */
  192. fimc_clear_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_SWRST);
  193. /* reset sequence */
  194. fimc_write(ctx, 0x0, EXYNOS_CIFCNTSEQ);
  195. }
  196. static int fimc_set_camblk_fimd0_wb(struct fimc_context *ctx)
  197. {
  198. return regmap_update_bits(ctx->sysreg, SYSREG_CAMERA_BLK,
  199. SYSREG_FIMD0WB_DEST_MASK,
  200. ctx->id << SYSREG_FIMD0WB_DEST_SHIFT);
  201. }
  202. static void fimc_set_type_ctrl(struct fimc_context *ctx, enum fimc_wb wb)
  203. {
  204. u32 cfg;
  205. DRM_DEBUG_KMS("wb[%d]\n", wb);
  206. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  207. cfg &= ~(EXYNOS_CIGCTRL_TESTPATTERN_MASK |
  208. EXYNOS_CIGCTRL_SELCAM_ITU_MASK |
  209. EXYNOS_CIGCTRL_SELCAM_MIPI_MASK |
  210. EXYNOS_CIGCTRL_SELCAM_FIMC_MASK |
  211. EXYNOS_CIGCTRL_SELWB_CAMIF_MASK |
  212. EXYNOS_CIGCTRL_SELWRITEBACK_MASK);
  213. switch (wb) {
  214. case FIMC_WB_A:
  215. cfg |= (EXYNOS_CIGCTRL_SELWRITEBACK_A |
  216. EXYNOS_CIGCTRL_SELWB_CAMIF_WRITEBACK);
  217. break;
  218. case FIMC_WB_B:
  219. cfg |= (EXYNOS_CIGCTRL_SELWRITEBACK_B |
  220. EXYNOS_CIGCTRL_SELWB_CAMIF_WRITEBACK);
  221. break;
  222. case FIMC_WB_NONE:
  223. default:
  224. cfg |= (EXYNOS_CIGCTRL_SELCAM_ITU_A |
  225. EXYNOS_CIGCTRL_SELWRITEBACK_A |
  226. EXYNOS_CIGCTRL_SELCAM_MIPI_A |
  227. EXYNOS_CIGCTRL_SELCAM_FIMC_ITU);
  228. break;
  229. }
  230. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  231. }
  232. static void fimc_set_polarity(struct fimc_context *ctx,
  233. struct exynos_drm_ipp_pol *pol)
  234. {
  235. u32 cfg;
  236. DRM_DEBUG_KMS("inv_pclk[%d]inv_vsync[%d]\n",
  237. pol->inv_pclk, pol->inv_vsync);
  238. DRM_DEBUG_KMS("inv_href[%d]inv_hsync[%d]\n",
  239. pol->inv_href, pol->inv_hsync);
  240. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  241. cfg &= ~(EXYNOS_CIGCTRL_INVPOLPCLK | EXYNOS_CIGCTRL_INVPOLVSYNC |
  242. EXYNOS_CIGCTRL_INVPOLHREF | EXYNOS_CIGCTRL_INVPOLHSYNC);
  243. if (pol->inv_pclk)
  244. cfg |= EXYNOS_CIGCTRL_INVPOLPCLK;
  245. if (pol->inv_vsync)
  246. cfg |= EXYNOS_CIGCTRL_INVPOLVSYNC;
  247. if (pol->inv_href)
  248. cfg |= EXYNOS_CIGCTRL_INVPOLHREF;
  249. if (pol->inv_hsync)
  250. cfg |= EXYNOS_CIGCTRL_INVPOLHSYNC;
  251. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  252. }
  253. static void fimc_handle_jpeg(struct fimc_context *ctx, bool enable)
  254. {
  255. u32 cfg;
  256. DRM_DEBUG_KMS("enable[%d]\n", enable);
  257. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  258. if (enable)
  259. cfg |= EXYNOS_CIGCTRL_CAM_JPEG;
  260. else
  261. cfg &= ~EXYNOS_CIGCTRL_CAM_JPEG;
  262. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  263. }
  264. static void fimc_mask_irq(struct fimc_context *ctx, bool enable)
  265. {
  266. u32 cfg;
  267. DRM_DEBUG_KMS("enable[%d]\n", enable);
  268. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  269. if (enable) {
  270. cfg &= ~EXYNOS_CIGCTRL_IRQ_OVFEN;
  271. cfg |= EXYNOS_CIGCTRL_IRQ_ENABLE | EXYNOS_CIGCTRL_IRQ_LEVEL;
  272. } else
  273. cfg &= ~EXYNOS_CIGCTRL_IRQ_ENABLE;
  274. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  275. }
  276. static void fimc_clear_irq(struct fimc_context *ctx)
  277. {
  278. fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_CLR);
  279. }
  280. static bool fimc_check_ovf(struct fimc_context *ctx)
  281. {
  282. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  283. u32 status, flag;
  284. status = fimc_read(ctx, EXYNOS_CISTATUS);
  285. flag = EXYNOS_CISTATUS_OVFIY | EXYNOS_CISTATUS_OVFICB |
  286. EXYNOS_CISTATUS_OVFICR;
  287. DRM_DEBUG_KMS("flag[0x%x]\n", flag);
  288. if (status & flag) {
  289. fimc_set_bits(ctx, EXYNOS_CIWDOFST,
  290. EXYNOS_CIWDOFST_CLROVFIY | EXYNOS_CIWDOFST_CLROVFICB |
  291. EXYNOS_CIWDOFST_CLROVFICR);
  292. dev_err(ippdrv->dev, "occurred overflow at %d, status 0x%x.\n",
  293. ctx->id, status);
  294. return true;
  295. }
  296. return false;
  297. }
  298. static bool fimc_check_frame_end(struct fimc_context *ctx)
  299. {
  300. u32 cfg;
  301. cfg = fimc_read(ctx, EXYNOS_CISTATUS);
  302. DRM_DEBUG_KMS("cfg[0x%x]\n", cfg);
  303. if (!(cfg & EXYNOS_CISTATUS_FRAMEEND))
  304. return false;
  305. cfg &= ~(EXYNOS_CISTATUS_FRAMEEND);
  306. fimc_write(ctx, cfg, EXYNOS_CISTATUS);
  307. return true;
  308. }
  309. static int fimc_get_buf_id(struct fimc_context *ctx)
  310. {
  311. u32 cfg;
  312. int frame_cnt, buf_id;
  313. cfg = fimc_read(ctx, EXYNOS_CISTATUS2);
  314. frame_cnt = EXYNOS_CISTATUS2_GET_FRAMECOUNT_BEFORE(cfg);
  315. if (frame_cnt == 0)
  316. frame_cnt = EXYNOS_CISTATUS2_GET_FRAMECOUNT_PRESENT(cfg);
  317. DRM_DEBUG_KMS("present[%d]before[%d]\n",
  318. EXYNOS_CISTATUS2_GET_FRAMECOUNT_PRESENT(cfg),
  319. EXYNOS_CISTATUS2_GET_FRAMECOUNT_BEFORE(cfg));
  320. if (frame_cnt == 0) {
  321. DRM_ERROR("failed to get frame count.\n");
  322. return -EIO;
  323. }
  324. buf_id = frame_cnt - 1;
  325. DRM_DEBUG_KMS("buf_id[%d]\n", buf_id);
  326. return buf_id;
  327. }
  328. static void fimc_handle_lastend(struct fimc_context *ctx, bool enable)
  329. {
  330. u32 cfg;
  331. DRM_DEBUG_KMS("enable[%d]\n", enable);
  332. cfg = fimc_read(ctx, EXYNOS_CIOCTRL);
  333. if (enable)
  334. cfg |= EXYNOS_CIOCTRL_LASTENDEN;
  335. else
  336. cfg &= ~EXYNOS_CIOCTRL_LASTENDEN;
  337. fimc_write(ctx, cfg, EXYNOS_CIOCTRL);
  338. }
  339. static int fimc_src_set_fmt_order(struct fimc_context *ctx, u32 fmt)
  340. {
  341. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  342. u32 cfg;
  343. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  344. /* RGB */
  345. cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
  346. cfg &= ~EXYNOS_CISCCTRL_INRGB_FMT_RGB_MASK;
  347. switch (fmt) {
  348. case DRM_FORMAT_RGB565:
  349. cfg |= EXYNOS_CISCCTRL_INRGB_FMT_RGB565;
  350. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  351. return 0;
  352. case DRM_FORMAT_RGB888:
  353. case DRM_FORMAT_XRGB8888:
  354. cfg |= EXYNOS_CISCCTRL_INRGB_FMT_RGB888;
  355. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  356. return 0;
  357. default:
  358. /* bypass */
  359. break;
  360. }
  361. /* YUV */
  362. cfg = fimc_read(ctx, EXYNOS_MSCTRL);
  363. cfg &= ~(EXYNOS_MSCTRL_ORDER2P_SHIFT_MASK |
  364. EXYNOS_MSCTRL_C_INT_IN_2PLANE |
  365. EXYNOS_MSCTRL_ORDER422_YCBYCR);
  366. switch (fmt) {
  367. case DRM_FORMAT_YUYV:
  368. cfg |= EXYNOS_MSCTRL_ORDER422_YCBYCR;
  369. break;
  370. case DRM_FORMAT_YVYU:
  371. cfg |= EXYNOS_MSCTRL_ORDER422_YCRYCB;
  372. break;
  373. case DRM_FORMAT_UYVY:
  374. cfg |= EXYNOS_MSCTRL_ORDER422_CBYCRY;
  375. break;
  376. case DRM_FORMAT_VYUY:
  377. case DRM_FORMAT_YUV444:
  378. cfg |= EXYNOS_MSCTRL_ORDER422_CRYCBY;
  379. break;
  380. case DRM_FORMAT_NV21:
  381. case DRM_FORMAT_NV61:
  382. cfg |= (EXYNOS_MSCTRL_ORDER2P_LSB_CRCB |
  383. EXYNOS_MSCTRL_C_INT_IN_2PLANE);
  384. break;
  385. case DRM_FORMAT_YUV422:
  386. case DRM_FORMAT_YUV420:
  387. case DRM_FORMAT_YVU420:
  388. cfg |= EXYNOS_MSCTRL_C_INT_IN_3PLANE;
  389. break;
  390. case DRM_FORMAT_NV12:
  391. case DRM_FORMAT_NV16:
  392. cfg |= (EXYNOS_MSCTRL_ORDER2P_LSB_CBCR |
  393. EXYNOS_MSCTRL_C_INT_IN_2PLANE);
  394. break;
  395. default:
  396. dev_err(ippdrv->dev, "invalid source yuv order 0x%x.\n", fmt);
  397. return -EINVAL;
  398. }
  399. fimc_write(ctx, cfg, EXYNOS_MSCTRL);
  400. return 0;
  401. }
  402. static int fimc_src_set_fmt(struct device *dev, u32 fmt)
  403. {
  404. struct fimc_context *ctx = get_fimc_context(dev);
  405. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  406. u32 cfg;
  407. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  408. cfg = fimc_read(ctx, EXYNOS_MSCTRL);
  409. cfg &= ~EXYNOS_MSCTRL_INFORMAT_RGB;
  410. switch (fmt) {
  411. case DRM_FORMAT_RGB565:
  412. case DRM_FORMAT_RGB888:
  413. case DRM_FORMAT_XRGB8888:
  414. cfg |= EXYNOS_MSCTRL_INFORMAT_RGB;
  415. break;
  416. case DRM_FORMAT_YUV444:
  417. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR420;
  418. break;
  419. case DRM_FORMAT_YUYV:
  420. case DRM_FORMAT_YVYU:
  421. case DRM_FORMAT_UYVY:
  422. case DRM_FORMAT_VYUY:
  423. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR422_1PLANE;
  424. break;
  425. case DRM_FORMAT_NV16:
  426. case DRM_FORMAT_NV61:
  427. case DRM_FORMAT_YUV422:
  428. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR422;
  429. break;
  430. case DRM_FORMAT_YUV420:
  431. case DRM_FORMAT_YVU420:
  432. case DRM_FORMAT_NV12:
  433. case DRM_FORMAT_NV21:
  434. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR420;
  435. break;
  436. default:
  437. dev_err(ippdrv->dev, "invalid source format 0x%x.\n", fmt);
  438. return -EINVAL;
  439. }
  440. fimc_write(ctx, cfg, EXYNOS_MSCTRL);
  441. cfg = fimc_read(ctx, EXYNOS_CIDMAPARAM);
  442. cfg &= ~EXYNOS_CIDMAPARAM_R_MODE_MASK;
  443. cfg |= EXYNOS_CIDMAPARAM_R_MODE_LINEAR;
  444. fimc_write(ctx, cfg, EXYNOS_CIDMAPARAM);
  445. return fimc_src_set_fmt_order(ctx, fmt);
  446. }
  447. static int fimc_src_set_transf(struct device *dev,
  448. enum drm_exynos_degree degree,
  449. enum drm_exynos_flip flip, bool *swap)
  450. {
  451. struct fimc_context *ctx = get_fimc_context(dev);
  452. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  453. u32 cfg1, cfg2;
  454. DRM_DEBUG_KMS("degree[%d]flip[0x%x]\n", degree, flip);
  455. cfg1 = fimc_read(ctx, EXYNOS_MSCTRL);
  456. cfg1 &= ~(EXYNOS_MSCTRL_FLIP_X_MIRROR |
  457. EXYNOS_MSCTRL_FLIP_Y_MIRROR);
  458. cfg2 = fimc_read(ctx, EXYNOS_CITRGFMT);
  459. cfg2 &= ~EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
  460. switch (degree) {
  461. case EXYNOS_DRM_DEGREE_0:
  462. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  463. cfg1 |= EXYNOS_MSCTRL_FLIP_X_MIRROR;
  464. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  465. cfg1 |= EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  466. break;
  467. case EXYNOS_DRM_DEGREE_90:
  468. cfg2 |= EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
  469. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  470. cfg1 |= EXYNOS_MSCTRL_FLIP_X_MIRROR;
  471. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  472. cfg1 |= EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  473. break;
  474. case EXYNOS_DRM_DEGREE_180:
  475. cfg1 |= (EXYNOS_MSCTRL_FLIP_X_MIRROR |
  476. EXYNOS_MSCTRL_FLIP_Y_MIRROR);
  477. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  478. cfg1 &= ~EXYNOS_MSCTRL_FLIP_X_MIRROR;
  479. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  480. cfg1 &= ~EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  481. break;
  482. case EXYNOS_DRM_DEGREE_270:
  483. cfg1 |= (EXYNOS_MSCTRL_FLIP_X_MIRROR |
  484. EXYNOS_MSCTRL_FLIP_Y_MIRROR);
  485. cfg2 |= EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
  486. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  487. cfg1 &= ~EXYNOS_MSCTRL_FLIP_X_MIRROR;
  488. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  489. cfg1 &= ~EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  490. break;
  491. default:
  492. dev_err(ippdrv->dev, "invalid degree value %d.\n", degree);
  493. return -EINVAL;
  494. }
  495. fimc_write(ctx, cfg1, EXYNOS_MSCTRL);
  496. fimc_write(ctx, cfg2, EXYNOS_CITRGFMT);
  497. *swap = (cfg2 & EXYNOS_CITRGFMT_INROT90_CLOCKWISE) ? 1 : 0;
  498. return 0;
  499. }
  500. static int fimc_set_window(struct fimc_context *ctx,
  501. struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
  502. {
  503. u32 cfg, h1, h2, v1, v2;
  504. /* cropped image */
  505. h1 = pos->x;
  506. h2 = sz->hsize - pos->w - pos->x;
  507. v1 = pos->y;
  508. v2 = sz->vsize - pos->h - pos->y;
  509. DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]hsize[%d]vsize[%d]\n",
  510. pos->x, pos->y, pos->w, pos->h, sz->hsize, sz->vsize);
  511. DRM_DEBUG_KMS("h1[%d]h2[%d]v1[%d]v2[%d]\n", h1, h2, v1, v2);
  512. /*
  513. * set window offset 1, 2 size
  514. * check figure 43-21 in user manual
  515. */
  516. cfg = fimc_read(ctx, EXYNOS_CIWDOFST);
  517. cfg &= ~(EXYNOS_CIWDOFST_WINHOROFST_MASK |
  518. EXYNOS_CIWDOFST_WINVEROFST_MASK);
  519. cfg |= (EXYNOS_CIWDOFST_WINHOROFST(h1) |
  520. EXYNOS_CIWDOFST_WINVEROFST(v1));
  521. cfg |= EXYNOS_CIWDOFST_WINOFSEN;
  522. fimc_write(ctx, cfg, EXYNOS_CIWDOFST);
  523. cfg = (EXYNOS_CIWDOFST2_WINHOROFST2(h2) |
  524. EXYNOS_CIWDOFST2_WINVEROFST2(v2));
  525. fimc_write(ctx, cfg, EXYNOS_CIWDOFST2);
  526. return 0;
  527. }
  528. static int fimc_src_set_size(struct device *dev, int swap,
  529. struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
  530. {
  531. struct fimc_context *ctx = get_fimc_context(dev);
  532. struct drm_exynos_pos img_pos = *pos;
  533. struct drm_exynos_sz img_sz = *sz;
  534. u32 cfg;
  535. DRM_DEBUG_KMS("swap[%d]hsize[%d]vsize[%d]\n",
  536. swap, sz->hsize, sz->vsize);
  537. /* original size */
  538. cfg = (EXYNOS_ORGISIZE_HORIZONTAL(img_sz.hsize) |
  539. EXYNOS_ORGISIZE_VERTICAL(img_sz.vsize));
  540. fimc_write(ctx, cfg, EXYNOS_ORGISIZE);
  541. DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]\n", pos->x, pos->y, pos->w, pos->h);
  542. if (swap) {
  543. img_pos.w = pos->h;
  544. img_pos.h = pos->w;
  545. img_sz.hsize = sz->vsize;
  546. img_sz.vsize = sz->hsize;
  547. }
  548. /* set input DMA image size */
  549. cfg = fimc_read(ctx, EXYNOS_CIREAL_ISIZE);
  550. cfg &= ~(EXYNOS_CIREAL_ISIZE_HEIGHT_MASK |
  551. EXYNOS_CIREAL_ISIZE_WIDTH_MASK);
  552. cfg |= (EXYNOS_CIREAL_ISIZE_WIDTH(img_pos.w) |
  553. EXYNOS_CIREAL_ISIZE_HEIGHT(img_pos.h));
  554. fimc_write(ctx, cfg, EXYNOS_CIREAL_ISIZE);
  555. /*
  556. * set input FIFO image size
  557. * for now, we support only ITU601 8 bit mode
  558. */
  559. cfg = (EXYNOS_CISRCFMT_ITU601_8BIT |
  560. EXYNOS_CISRCFMT_SOURCEHSIZE(img_sz.hsize) |
  561. EXYNOS_CISRCFMT_SOURCEVSIZE(img_sz.vsize));
  562. fimc_write(ctx, cfg, EXYNOS_CISRCFMT);
  563. /* offset Y(RGB), Cb, Cr */
  564. cfg = (EXYNOS_CIIYOFF_HORIZONTAL(img_pos.x) |
  565. EXYNOS_CIIYOFF_VERTICAL(img_pos.y));
  566. fimc_write(ctx, cfg, EXYNOS_CIIYOFF);
  567. cfg = (EXYNOS_CIICBOFF_HORIZONTAL(img_pos.x) |
  568. EXYNOS_CIICBOFF_VERTICAL(img_pos.y));
  569. fimc_write(ctx, cfg, EXYNOS_CIICBOFF);
  570. cfg = (EXYNOS_CIICROFF_HORIZONTAL(img_pos.x) |
  571. EXYNOS_CIICROFF_VERTICAL(img_pos.y));
  572. fimc_write(ctx, cfg, EXYNOS_CIICROFF);
  573. return fimc_set_window(ctx, &img_pos, &img_sz);
  574. }
  575. static int fimc_src_set_addr(struct device *dev,
  576. struct drm_exynos_ipp_buf_info *buf_info, u32 buf_id,
  577. enum drm_exynos_ipp_buf_type buf_type)
  578. {
  579. struct fimc_context *ctx = get_fimc_context(dev);
  580. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  581. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  582. struct drm_exynos_ipp_property *property;
  583. struct drm_exynos_ipp_config *config;
  584. if (!c_node) {
  585. DRM_ERROR("failed to get c_node.\n");
  586. return -EINVAL;
  587. }
  588. property = &c_node->property;
  589. DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]buf_type[%d]\n",
  590. property->prop_id, buf_id, buf_type);
  591. if (buf_id > FIMC_MAX_SRC) {
  592. dev_info(ippdrv->dev, "invalid buf_id %d.\n", buf_id);
  593. return -ENOMEM;
  594. }
  595. /* address register set */
  596. switch (buf_type) {
  597. case IPP_BUF_ENQUEUE:
  598. config = &property->config[EXYNOS_DRM_OPS_SRC];
  599. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_Y],
  600. EXYNOS_CIIYSA0);
  601. if (config->fmt == DRM_FORMAT_YVU420) {
  602. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
  603. EXYNOS_CIICBSA0);
  604. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
  605. EXYNOS_CIICRSA0);
  606. } else {
  607. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
  608. EXYNOS_CIICBSA0);
  609. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
  610. EXYNOS_CIICRSA0);
  611. }
  612. break;
  613. case IPP_BUF_DEQUEUE:
  614. fimc_write(ctx, 0x0, EXYNOS_CIIYSA0);
  615. fimc_write(ctx, 0x0, EXYNOS_CIICBSA0);
  616. fimc_write(ctx, 0x0, EXYNOS_CIICRSA0);
  617. break;
  618. default:
  619. /* bypass */
  620. break;
  621. }
  622. return 0;
  623. }
  624. static struct exynos_drm_ipp_ops fimc_src_ops = {
  625. .set_fmt = fimc_src_set_fmt,
  626. .set_transf = fimc_src_set_transf,
  627. .set_size = fimc_src_set_size,
  628. .set_addr = fimc_src_set_addr,
  629. };
  630. static int fimc_dst_set_fmt_order(struct fimc_context *ctx, u32 fmt)
  631. {
  632. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  633. u32 cfg;
  634. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  635. /* RGB */
  636. cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
  637. cfg &= ~EXYNOS_CISCCTRL_OUTRGB_FMT_RGB_MASK;
  638. switch (fmt) {
  639. case DRM_FORMAT_RGB565:
  640. cfg |= EXYNOS_CISCCTRL_OUTRGB_FMT_RGB565;
  641. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  642. return 0;
  643. case DRM_FORMAT_RGB888:
  644. cfg |= EXYNOS_CISCCTRL_OUTRGB_FMT_RGB888;
  645. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  646. return 0;
  647. case DRM_FORMAT_XRGB8888:
  648. cfg |= (EXYNOS_CISCCTRL_OUTRGB_FMT_RGB888 |
  649. EXYNOS_CISCCTRL_EXTRGB_EXTENSION);
  650. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  651. break;
  652. default:
  653. /* bypass */
  654. break;
  655. }
  656. /* YUV */
  657. cfg = fimc_read(ctx, EXYNOS_CIOCTRL);
  658. cfg &= ~(EXYNOS_CIOCTRL_ORDER2P_MASK |
  659. EXYNOS_CIOCTRL_ORDER422_MASK |
  660. EXYNOS_CIOCTRL_YCBCR_PLANE_MASK);
  661. switch (fmt) {
  662. case DRM_FORMAT_XRGB8888:
  663. cfg |= EXYNOS_CIOCTRL_ALPHA_OUT;
  664. break;
  665. case DRM_FORMAT_YUYV:
  666. cfg |= EXYNOS_CIOCTRL_ORDER422_YCBYCR;
  667. break;
  668. case DRM_FORMAT_YVYU:
  669. cfg |= EXYNOS_CIOCTRL_ORDER422_YCRYCB;
  670. break;
  671. case DRM_FORMAT_UYVY:
  672. cfg |= EXYNOS_CIOCTRL_ORDER422_CBYCRY;
  673. break;
  674. case DRM_FORMAT_VYUY:
  675. cfg |= EXYNOS_CIOCTRL_ORDER422_CRYCBY;
  676. break;
  677. case DRM_FORMAT_NV21:
  678. case DRM_FORMAT_NV61:
  679. cfg |= EXYNOS_CIOCTRL_ORDER2P_LSB_CRCB;
  680. cfg |= EXYNOS_CIOCTRL_YCBCR_2PLANE;
  681. break;
  682. case DRM_FORMAT_YUV422:
  683. case DRM_FORMAT_YUV420:
  684. case DRM_FORMAT_YVU420:
  685. cfg |= EXYNOS_CIOCTRL_YCBCR_3PLANE;
  686. break;
  687. case DRM_FORMAT_NV12:
  688. case DRM_FORMAT_NV16:
  689. cfg |= EXYNOS_CIOCTRL_ORDER2P_LSB_CBCR;
  690. cfg |= EXYNOS_CIOCTRL_YCBCR_2PLANE;
  691. break;
  692. default:
  693. dev_err(ippdrv->dev, "invalid target yuv order 0x%x.\n", fmt);
  694. return -EINVAL;
  695. }
  696. fimc_write(ctx, cfg, EXYNOS_CIOCTRL);
  697. return 0;
  698. }
  699. static int fimc_dst_set_fmt(struct device *dev, u32 fmt)
  700. {
  701. struct fimc_context *ctx = get_fimc_context(dev);
  702. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  703. u32 cfg;
  704. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  705. cfg = fimc_read(ctx, EXYNOS_CIEXTEN);
  706. if (fmt == DRM_FORMAT_AYUV) {
  707. cfg |= EXYNOS_CIEXTEN_YUV444_OUT;
  708. fimc_write(ctx, cfg, EXYNOS_CIEXTEN);
  709. } else {
  710. cfg &= ~EXYNOS_CIEXTEN_YUV444_OUT;
  711. fimc_write(ctx, cfg, EXYNOS_CIEXTEN);
  712. cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
  713. cfg &= ~EXYNOS_CITRGFMT_OUTFORMAT_MASK;
  714. switch (fmt) {
  715. case DRM_FORMAT_RGB565:
  716. case DRM_FORMAT_RGB888:
  717. case DRM_FORMAT_XRGB8888:
  718. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_RGB;
  719. break;
  720. case DRM_FORMAT_YUYV:
  721. case DRM_FORMAT_YVYU:
  722. case DRM_FORMAT_UYVY:
  723. case DRM_FORMAT_VYUY:
  724. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR422_1PLANE;
  725. break;
  726. case DRM_FORMAT_NV16:
  727. case DRM_FORMAT_NV61:
  728. case DRM_FORMAT_YUV422:
  729. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR422;
  730. break;
  731. case DRM_FORMAT_YUV420:
  732. case DRM_FORMAT_YVU420:
  733. case DRM_FORMAT_NV12:
  734. case DRM_FORMAT_NV21:
  735. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR420;
  736. break;
  737. default:
  738. dev_err(ippdrv->dev, "invalid target format 0x%x.\n",
  739. fmt);
  740. return -EINVAL;
  741. }
  742. fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
  743. }
  744. cfg = fimc_read(ctx, EXYNOS_CIDMAPARAM);
  745. cfg &= ~EXYNOS_CIDMAPARAM_W_MODE_MASK;
  746. cfg |= EXYNOS_CIDMAPARAM_W_MODE_LINEAR;
  747. fimc_write(ctx, cfg, EXYNOS_CIDMAPARAM);
  748. return fimc_dst_set_fmt_order(ctx, fmt);
  749. }
  750. static int fimc_dst_set_transf(struct device *dev,
  751. enum drm_exynos_degree degree,
  752. enum drm_exynos_flip flip, bool *swap)
  753. {
  754. struct fimc_context *ctx = get_fimc_context(dev);
  755. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  756. u32 cfg;
  757. DRM_DEBUG_KMS("degree[%d]flip[0x%x]\n", degree, flip);
  758. cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
  759. cfg &= ~EXYNOS_CITRGFMT_FLIP_MASK;
  760. cfg &= ~EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE;
  761. switch (degree) {
  762. case EXYNOS_DRM_DEGREE_0:
  763. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  764. cfg |= EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  765. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  766. cfg |= EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  767. break;
  768. case EXYNOS_DRM_DEGREE_90:
  769. cfg |= EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE;
  770. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  771. cfg |= EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  772. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  773. cfg |= EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  774. break;
  775. case EXYNOS_DRM_DEGREE_180:
  776. cfg |= (EXYNOS_CITRGFMT_FLIP_X_MIRROR |
  777. EXYNOS_CITRGFMT_FLIP_Y_MIRROR);
  778. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  779. cfg &= ~EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  780. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  781. cfg &= ~EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  782. break;
  783. case EXYNOS_DRM_DEGREE_270:
  784. cfg |= (EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE |
  785. EXYNOS_CITRGFMT_FLIP_X_MIRROR |
  786. EXYNOS_CITRGFMT_FLIP_Y_MIRROR);
  787. if (flip & EXYNOS_DRM_FLIP_VERTICAL)
  788. cfg &= ~EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  789. if (flip & EXYNOS_DRM_FLIP_HORIZONTAL)
  790. cfg &= ~EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  791. break;
  792. default:
  793. dev_err(ippdrv->dev, "invalid degree value %d.\n", degree);
  794. return -EINVAL;
  795. }
  796. fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
  797. *swap = (cfg & EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE) ? 1 : 0;
  798. return 0;
  799. }
  800. static int fimc_set_prescaler(struct fimc_context *ctx, struct fimc_scaler *sc,
  801. struct drm_exynos_pos *src, struct drm_exynos_pos *dst)
  802. {
  803. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  804. u32 cfg, cfg_ext, shfactor;
  805. u32 pre_dst_width, pre_dst_height;
  806. u32 hfactor, vfactor;
  807. int ret = 0;
  808. u32 src_w, src_h, dst_w, dst_h;
  809. cfg_ext = fimc_read(ctx, EXYNOS_CITRGFMT);
  810. if (cfg_ext & EXYNOS_CITRGFMT_INROT90_CLOCKWISE) {
  811. src_w = src->h;
  812. src_h = src->w;
  813. } else {
  814. src_w = src->w;
  815. src_h = src->h;
  816. }
  817. if (cfg_ext & EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE) {
  818. dst_w = dst->h;
  819. dst_h = dst->w;
  820. } else {
  821. dst_w = dst->w;
  822. dst_h = dst->h;
  823. }
  824. /* fimc_ippdrv_check_property assures that dividers are not null */
  825. hfactor = fls(src_w / dst_w / 2);
  826. if (hfactor > FIMC_SHFACTOR / 2) {
  827. dev_err(ippdrv->dev, "failed to get ratio horizontal.\n");
  828. return -EINVAL;
  829. }
  830. vfactor = fls(src_h / dst_h / 2);
  831. if (vfactor > FIMC_SHFACTOR / 2) {
  832. dev_err(ippdrv->dev, "failed to get ratio vertical.\n");
  833. return -EINVAL;
  834. }
  835. pre_dst_width = src_w >> hfactor;
  836. pre_dst_height = src_h >> vfactor;
  837. DRM_DEBUG_KMS("pre_dst_width[%d]pre_dst_height[%d]\n",
  838. pre_dst_width, pre_dst_height);
  839. DRM_DEBUG_KMS("hfactor[%d]vfactor[%d]\n", hfactor, vfactor);
  840. sc->hratio = (src_w << 14) / (dst_w << hfactor);
  841. sc->vratio = (src_h << 14) / (dst_h << vfactor);
  842. sc->up_h = (dst_w >= src_w) ? true : false;
  843. sc->up_v = (dst_h >= src_h) ? true : false;
  844. DRM_DEBUG_KMS("hratio[%d]vratio[%d]up_h[%d]up_v[%d]\n",
  845. sc->hratio, sc->vratio, sc->up_h, sc->up_v);
  846. shfactor = FIMC_SHFACTOR - (hfactor + vfactor);
  847. DRM_DEBUG_KMS("shfactor[%d]\n", shfactor);
  848. cfg = (EXYNOS_CISCPRERATIO_SHFACTOR(shfactor) |
  849. EXYNOS_CISCPRERATIO_PREHORRATIO(1 << hfactor) |
  850. EXYNOS_CISCPRERATIO_PREVERRATIO(1 << vfactor));
  851. fimc_write(ctx, cfg, EXYNOS_CISCPRERATIO);
  852. cfg = (EXYNOS_CISCPREDST_PREDSTWIDTH(pre_dst_width) |
  853. EXYNOS_CISCPREDST_PREDSTHEIGHT(pre_dst_height));
  854. fimc_write(ctx, cfg, EXYNOS_CISCPREDST);
  855. return ret;
  856. }
  857. static void fimc_set_scaler(struct fimc_context *ctx, struct fimc_scaler *sc)
  858. {
  859. u32 cfg, cfg_ext;
  860. DRM_DEBUG_KMS("range[%d]bypass[%d]up_h[%d]up_v[%d]\n",
  861. sc->range, sc->bypass, sc->up_h, sc->up_v);
  862. DRM_DEBUG_KMS("hratio[%d]vratio[%d]\n",
  863. sc->hratio, sc->vratio);
  864. cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
  865. cfg &= ~(EXYNOS_CISCCTRL_SCALERBYPASS |
  866. EXYNOS_CISCCTRL_SCALEUP_H | EXYNOS_CISCCTRL_SCALEUP_V |
  867. EXYNOS_CISCCTRL_MAIN_V_RATIO_MASK |
  868. EXYNOS_CISCCTRL_MAIN_H_RATIO_MASK |
  869. EXYNOS_CISCCTRL_CSCR2Y_WIDE |
  870. EXYNOS_CISCCTRL_CSCY2R_WIDE);
  871. if (sc->range)
  872. cfg |= (EXYNOS_CISCCTRL_CSCR2Y_WIDE |
  873. EXYNOS_CISCCTRL_CSCY2R_WIDE);
  874. if (sc->bypass)
  875. cfg |= EXYNOS_CISCCTRL_SCALERBYPASS;
  876. if (sc->up_h)
  877. cfg |= EXYNOS_CISCCTRL_SCALEUP_H;
  878. if (sc->up_v)
  879. cfg |= EXYNOS_CISCCTRL_SCALEUP_V;
  880. cfg |= (EXYNOS_CISCCTRL_MAINHORRATIO((sc->hratio >> 6)) |
  881. EXYNOS_CISCCTRL_MAINVERRATIO((sc->vratio >> 6)));
  882. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  883. cfg_ext = fimc_read(ctx, EXYNOS_CIEXTEN);
  884. cfg_ext &= ~EXYNOS_CIEXTEN_MAINHORRATIO_EXT_MASK;
  885. cfg_ext &= ~EXYNOS_CIEXTEN_MAINVERRATIO_EXT_MASK;
  886. cfg_ext |= (EXYNOS_CIEXTEN_MAINHORRATIO_EXT(sc->hratio) |
  887. EXYNOS_CIEXTEN_MAINVERRATIO_EXT(sc->vratio));
  888. fimc_write(ctx, cfg_ext, EXYNOS_CIEXTEN);
  889. }
  890. static int fimc_dst_set_size(struct device *dev, int swap,
  891. struct drm_exynos_pos *pos, struct drm_exynos_sz *sz)
  892. {
  893. struct fimc_context *ctx = get_fimc_context(dev);
  894. struct drm_exynos_pos img_pos = *pos;
  895. struct drm_exynos_sz img_sz = *sz;
  896. u32 cfg;
  897. DRM_DEBUG_KMS("swap[%d]hsize[%d]vsize[%d]\n",
  898. swap, sz->hsize, sz->vsize);
  899. /* original size */
  900. cfg = (EXYNOS_ORGOSIZE_HORIZONTAL(img_sz.hsize) |
  901. EXYNOS_ORGOSIZE_VERTICAL(img_sz.vsize));
  902. fimc_write(ctx, cfg, EXYNOS_ORGOSIZE);
  903. DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]\n", pos->x, pos->y, pos->w, pos->h);
  904. /* CSC ITU */
  905. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  906. cfg &= ~EXYNOS_CIGCTRL_CSC_MASK;
  907. if (sz->hsize >= FIMC_WIDTH_ITU_709)
  908. cfg |= EXYNOS_CIGCTRL_CSC_ITU709;
  909. else
  910. cfg |= EXYNOS_CIGCTRL_CSC_ITU601;
  911. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  912. if (swap) {
  913. img_pos.w = pos->h;
  914. img_pos.h = pos->w;
  915. img_sz.hsize = sz->vsize;
  916. img_sz.vsize = sz->hsize;
  917. }
  918. /* target image size */
  919. cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
  920. cfg &= ~(EXYNOS_CITRGFMT_TARGETH_MASK |
  921. EXYNOS_CITRGFMT_TARGETV_MASK);
  922. cfg |= (EXYNOS_CITRGFMT_TARGETHSIZE(img_pos.w) |
  923. EXYNOS_CITRGFMT_TARGETVSIZE(img_pos.h));
  924. fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
  925. /* target area */
  926. cfg = EXYNOS_CITAREA_TARGET_AREA(img_pos.w * img_pos.h);
  927. fimc_write(ctx, cfg, EXYNOS_CITAREA);
  928. /* offset Y(RGB), Cb, Cr */
  929. cfg = (EXYNOS_CIOYOFF_HORIZONTAL(img_pos.x) |
  930. EXYNOS_CIOYOFF_VERTICAL(img_pos.y));
  931. fimc_write(ctx, cfg, EXYNOS_CIOYOFF);
  932. cfg = (EXYNOS_CIOCBOFF_HORIZONTAL(img_pos.x) |
  933. EXYNOS_CIOCBOFF_VERTICAL(img_pos.y));
  934. fimc_write(ctx, cfg, EXYNOS_CIOCBOFF);
  935. cfg = (EXYNOS_CIOCROFF_HORIZONTAL(img_pos.x) |
  936. EXYNOS_CIOCROFF_VERTICAL(img_pos.y));
  937. fimc_write(ctx, cfg, EXYNOS_CIOCROFF);
  938. return 0;
  939. }
  940. static void fimc_dst_set_buf_seq(struct fimc_context *ctx, u32 buf_id,
  941. enum drm_exynos_ipp_buf_type buf_type)
  942. {
  943. unsigned long flags;
  944. u32 buf_num;
  945. u32 cfg;
  946. DRM_DEBUG_KMS("buf_id[%d]buf_type[%d]\n", buf_id, buf_type);
  947. spin_lock_irqsave(&ctx->lock, flags);
  948. cfg = fimc_read(ctx, EXYNOS_CIFCNTSEQ);
  949. if (buf_type == IPP_BUF_ENQUEUE)
  950. cfg |= (1 << buf_id);
  951. else
  952. cfg &= ~(1 << buf_id);
  953. fimc_write(ctx, cfg, EXYNOS_CIFCNTSEQ);
  954. buf_num = hweight32(cfg);
  955. if (buf_type == IPP_BUF_ENQUEUE && buf_num >= FIMC_BUF_START)
  956. fimc_mask_irq(ctx, true);
  957. else if (buf_type == IPP_BUF_DEQUEUE && buf_num <= FIMC_BUF_STOP)
  958. fimc_mask_irq(ctx, false);
  959. spin_unlock_irqrestore(&ctx->lock, flags);
  960. }
  961. static int fimc_dst_set_addr(struct device *dev,
  962. struct drm_exynos_ipp_buf_info *buf_info, u32 buf_id,
  963. enum drm_exynos_ipp_buf_type buf_type)
  964. {
  965. struct fimc_context *ctx = get_fimc_context(dev);
  966. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  967. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  968. struct drm_exynos_ipp_property *property;
  969. struct drm_exynos_ipp_config *config;
  970. if (!c_node) {
  971. DRM_ERROR("failed to get c_node.\n");
  972. return -EINVAL;
  973. }
  974. property = &c_node->property;
  975. DRM_DEBUG_KMS("prop_id[%d]buf_id[%d]buf_type[%d]\n",
  976. property->prop_id, buf_id, buf_type);
  977. if (buf_id > FIMC_MAX_DST) {
  978. dev_info(ippdrv->dev, "invalid buf_id %d.\n", buf_id);
  979. return -ENOMEM;
  980. }
  981. /* address register set */
  982. switch (buf_type) {
  983. case IPP_BUF_ENQUEUE:
  984. config = &property->config[EXYNOS_DRM_OPS_DST];
  985. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_Y],
  986. EXYNOS_CIOYSA(buf_id));
  987. if (config->fmt == DRM_FORMAT_YVU420) {
  988. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
  989. EXYNOS_CIOCBSA(buf_id));
  990. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
  991. EXYNOS_CIOCRSA(buf_id));
  992. } else {
  993. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CB],
  994. EXYNOS_CIOCBSA(buf_id));
  995. fimc_write(ctx, buf_info->base[EXYNOS_DRM_PLANAR_CR],
  996. EXYNOS_CIOCRSA(buf_id));
  997. }
  998. break;
  999. case IPP_BUF_DEQUEUE:
  1000. fimc_write(ctx, 0x0, EXYNOS_CIOYSA(buf_id));
  1001. fimc_write(ctx, 0x0, EXYNOS_CIOCBSA(buf_id));
  1002. fimc_write(ctx, 0x0, EXYNOS_CIOCRSA(buf_id));
  1003. break;
  1004. default:
  1005. /* bypass */
  1006. break;
  1007. }
  1008. fimc_dst_set_buf_seq(ctx, buf_id, buf_type);
  1009. return 0;
  1010. }
  1011. static struct exynos_drm_ipp_ops fimc_dst_ops = {
  1012. .set_fmt = fimc_dst_set_fmt,
  1013. .set_transf = fimc_dst_set_transf,
  1014. .set_size = fimc_dst_set_size,
  1015. .set_addr = fimc_dst_set_addr,
  1016. };
  1017. static irqreturn_t fimc_irq_handler(int irq, void *dev_id)
  1018. {
  1019. struct fimc_context *ctx = dev_id;
  1020. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1021. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  1022. struct drm_exynos_ipp_event_work *event_work =
  1023. c_node->event_work;
  1024. int buf_id;
  1025. DRM_DEBUG_KMS("fimc id[%d]\n", ctx->id);
  1026. fimc_clear_irq(ctx);
  1027. if (fimc_check_ovf(ctx))
  1028. return IRQ_NONE;
  1029. if (!fimc_check_frame_end(ctx))
  1030. return IRQ_NONE;
  1031. buf_id = fimc_get_buf_id(ctx);
  1032. if (buf_id < 0)
  1033. return IRQ_HANDLED;
  1034. DRM_DEBUG_KMS("buf_id[%d]\n", buf_id);
  1035. fimc_dst_set_buf_seq(ctx, buf_id, IPP_BUF_DEQUEUE);
  1036. event_work->ippdrv = ippdrv;
  1037. event_work->buf_id[EXYNOS_DRM_OPS_DST] = buf_id;
  1038. queue_work(ippdrv->event_workq, &event_work->work);
  1039. return IRQ_HANDLED;
  1040. }
  1041. static int fimc_init_prop_list(struct exynos_drm_ippdrv *ippdrv)
  1042. {
  1043. struct drm_exynos_ipp_prop_list *prop_list = &ippdrv->prop_list;
  1044. prop_list->version = 1;
  1045. prop_list->writeback = 1;
  1046. prop_list->refresh_min = FIMC_REFRESH_MIN;
  1047. prop_list->refresh_max = FIMC_REFRESH_MAX;
  1048. prop_list->flip = (1 << EXYNOS_DRM_FLIP_NONE) |
  1049. (1 << EXYNOS_DRM_FLIP_VERTICAL) |
  1050. (1 << EXYNOS_DRM_FLIP_HORIZONTAL);
  1051. prop_list->degree = (1 << EXYNOS_DRM_DEGREE_0) |
  1052. (1 << EXYNOS_DRM_DEGREE_90) |
  1053. (1 << EXYNOS_DRM_DEGREE_180) |
  1054. (1 << EXYNOS_DRM_DEGREE_270);
  1055. prop_list->csc = 1;
  1056. prop_list->crop = 1;
  1057. prop_list->crop_max.hsize = FIMC_CROP_MAX;
  1058. prop_list->crop_max.vsize = FIMC_CROP_MAX;
  1059. prop_list->crop_min.hsize = FIMC_CROP_MIN;
  1060. prop_list->crop_min.vsize = FIMC_CROP_MIN;
  1061. prop_list->scale = 1;
  1062. prop_list->scale_max.hsize = FIMC_SCALE_MAX;
  1063. prop_list->scale_max.vsize = FIMC_SCALE_MAX;
  1064. prop_list->scale_min.hsize = FIMC_SCALE_MIN;
  1065. prop_list->scale_min.vsize = FIMC_SCALE_MIN;
  1066. return 0;
  1067. }
  1068. static inline bool fimc_check_drm_flip(enum drm_exynos_flip flip)
  1069. {
  1070. switch (flip) {
  1071. case EXYNOS_DRM_FLIP_NONE:
  1072. case EXYNOS_DRM_FLIP_VERTICAL:
  1073. case EXYNOS_DRM_FLIP_HORIZONTAL:
  1074. case EXYNOS_DRM_FLIP_BOTH:
  1075. return true;
  1076. default:
  1077. DRM_DEBUG_KMS("invalid flip\n");
  1078. return false;
  1079. }
  1080. }
  1081. static int fimc_ippdrv_check_property(struct device *dev,
  1082. struct drm_exynos_ipp_property *property)
  1083. {
  1084. struct fimc_context *ctx = get_fimc_context(dev);
  1085. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1086. struct drm_exynos_ipp_prop_list *pp = &ippdrv->prop_list;
  1087. struct drm_exynos_ipp_config *config;
  1088. struct drm_exynos_pos *pos;
  1089. struct drm_exynos_sz *sz;
  1090. bool swap;
  1091. int i;
  1092. for_each_ipp_ops(i) {
  1093. if ((i == EXYNOS_DRM_OPS_SRC) &&
  1094. (property->cmd == IPP_CMD_WB))
  1095. continue;
  1096. config = &property->config[i];
  1097. pos = &config->pos;
  1098. sz = &config->sz;
  1099. /* check for flip */
  1100. if (!fimc_check_drm_flip(config->flip)) {
  1101. DRM_ERROR("invalid flip.\n");
  1102. goto err_property;
  1103. }
  1104. /* check for degree */
  1105. switch (config->degree) {
  1106. case EXYNOS_DRM_DEGREE_90:
  1107. case EXYNOS_DRM_DEGREE_270:
  1108. swap = true;
  1109. break;
  1110. case EXYNOS_DRM_DEGREE_0:
  1111. case EXYNOS_DRM_DEGREE_180:
  1112. swap = false;
  1113. break;
  1114. default:
  1115. DRM_ERROR("invalid degree.\n");
  1116. goto err_property;
  1117. }
  1118. /* check for buffer bound */
  1119. if ((pos->x + pos->w > sz->hsize) ||
  1120. (pos->y + pos->h > sz->vsize)) {
  1121. DRM_ERROR("out of buf bound.\n");
  1122. goto err_property;
  1123. }
  1124. /* check for crop */
  1125. if ((i == EXYNOS_DRM_OPS_SRC) && (pp->crop)) {
  1126. if (swap) {
  1127. if ((pos->h < pp->crop_min.hsize) ||
  1128. (sz->vsize > pp->crop_max.hsize) ||
  1129. (pos->w < pp->crop_min.vsize) ||
  1130. (sz->hsize > pp->crop_max.vsize)) {
  1131. DRM_ERROR("out of crop size.\n");
  1132. goto err_property;
  1133. }
  1134. } else {
  1135. if ((pos->w < pp->crop_min.hsize) ||
  1136. (sz->hsize > pp->crop_max.hsize) ||
  1137. (pos->h < pp->crop_min.vsize) ||
  1138. (sz->vsize > pp->crop_max.vsize)) {
  1139. DRM_ERROR("out of crop size.\n");
  1140. goto err_property;
  1141. }
  1142. }
  1143. }
  1144. /* check for scale */
  1145. if ((i == EXYNOS_DRM_OPS_DST) && (pp->scale)) {
  1146. if (swap) {
  1147. if ((pos->h < pp->scale_min.hsize) ||
  1148. (sz->vsize > pp->scale_max.hsize) ||
  1149. (pos->w < pp->scale_min.vsize) ||
  1150. (sz->hsize > pp->scale_max.vsize)) {
  1151. DRM_ERROR("out of scale size.\n");
  1152. goto err_property;
  1153. }
  1154. } else {
  1155. if ((pos->w < pp->scale_min.hsize) ||
  1156. (sz->hsize > pp->scale_max.hsize) ||
  1157. (pos->h < pp->scale_min.vsize) ||
  1158. (sz->vsize > pp->scale_max.vsize)) {
  1159. DRM_ERROR("out of scale size.\n");
  1160. goto err_property;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. return 0;
  1166. err_property:
  1167. for_each_ipp_ops(i) {
  1168. if ((i == EXYNOS_DRM_OPS_SRC) &&
  1169. (property->cmd == IPP_CMD_WB))
  1170. continue;
  1171. config = &property->config[i];
  1172. pos = &config->pos;
  1173. sz = &config->sz;
  1174. DRM_ERROR("[%s]f[%d]r[%d]pos[%d %d %d %d]sz[%d %d]\n",
  1175. i ? "dst" : "src", config->flip, config->degree,
  1176. pos->x, pos->y, pos->w, pos->h,
  1177. sz->hsize, sz->vsize);
  1178. }
  1179. return -EINVAL;
  1180. }
  1181. static void fimc_clear_addr(struct fimc_context *ctx)
  1182. {
  1183. int i;
  1184. for (i = 0; i < FIMC_MAX_SRC; i++) {
  1185. fimc_write(ctx, 0, EXYNOS_CIIYSA(i));
  1186. fimc_write(ctx, 0, EXYNOS_CIICBSA(i));
  1187. fimc_write(ctx, 0, EXYNOS_CIICRSA(i));
  1188. }
  1189. for (i = 0; i < FIMC_MAX_DST; i++) {
  1190. fimc_write(ctx, 0, EXYNOS_CIOYSA(i));
  1191. fimc_write(ctx, 0, EXYNOS_CIOCBSA(i));
  1192. fimc_write(ctx, 0, EXYNOS_CIOCRSA(i));
  1193. }
  1194. }
  1195. static int fimc_ippdrv_reset(struct device *dev)
  1196. {
  1197. struct fimc_context *ctx = get_fimc_context(dev);
  1198. /* reset h/w block */
  1199. fimc_sw_reset(ctx);
  1200. /* reset scaler capability */
  1201. memset(&ctx->sc, 0x0, sizeof(ctx->sc));
  1202. fimc_clear_addr(ctx);
  1203. return 0;
  1204. }
  1205. static int fimc_ippdrv_start(struct device *dev, enum drm_exynos_ipp_cmd cmd)
  1206. {
  1207. struct fimc_context *ctx = get_fimc_context(dev);
  1208. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1209. struct drm_exynos_ipp_cmd_node *c_node = ippdrv->c_node;
  1210. struct drm_exynos_ipp_property *property;
  1211. struct drm_exynos_ipp_config *config;
  1212. struct drm_exynos_pos img_pos[EXYNOS_DRM_OPS_MAX];
  1213. struct drm_exynos_ipp_set_wb set_wb;
  1214. int ret, i;
  1215. u32 cfg0, cfg1;
  1216. DRM_DEBUG_KMS("cmd[%d]\n", cmd);
  1217. if (!c_node) {
  1218. DRM_ERROR("failed to get c_node.\n");
  1219. return -EINVAL;
  1220. }
  1221. property = &c_node->property;
  1222. fimc_mask_irq(ctx, true);
  1223. for_each_ipp_ops(i) {
  1224. config = &property->config[i];
  1225. img_pos[i] = config->pos;
  1226. }
  1227. ret = fimc_set_prescaler(ctx, &ctx->sc,
  1228. &img_pos[EXYNOS_DRM_OPS_SRC],
  1229. &img_pos[EXYNOS_DRM_OPS_DST]);
  1230. if (ret) {
  1231. dev_err(dev, "failed to set precalser.\n");
  1232. return ret;
  1233. }
  1234. /* If set ture, we can save jpeg about screen */
  1235. fimc_handle_jpeg(ctx, false);
  1236. fimc_set_scaler(ctx, &ctx->sc);
  1237. fimc_set_polarity(ctx, &ctx->pol);
  1238. switch (cmd) {
  1239. case IPP_CMD_M2M:
  1240. fimc_set_type_ctrl(ctx, FIMC_WB_NONE);
  1241. fimc_handle_lastend(ctx, false);
  1242. /* setup dma */
  1243. cfg0 = fimc_read(ctx, EXYNOS_MSCTRL);
  1244. cfg0 &= ~EXYNOS_MSCTRL_INPUT_MASK;
  1245. cfg0 |= EXYNOS_MSCTRL_INPUT_MEMORY;
  1246. fimc_write(ctx, cfg0, EXYNOS_MSCTRL);
  1247. break;
  1248. case IPP_CMD_WB:
  1249. fimc_set_type_ctrl(ctx, FIMC_WB_A);
  1250. fimc_handle_lastend(ctx, true);
  1251. /* setup FIMD */
  1252. ret = fimc_set_camblk_fimd0_wb(ctx);
  1253. if (ret < 0) {
  1254. dev_err(dev, "camblk setup failed.\n");
  1255. return ret;
  1256. }
  1257. set_wb.enable = 1;
  1258. set_wb.refresh = property->refresh_rate;
  1259. exynos_drm_ippnb_send_event(IPP_SET_WRITEBACK, (void *)&set_wb);
  1260. break;
  1261. case IPP_CMD_OUTPUT:
  1262. default:
  1263. ret = -EINVAL;
  1264. dev_err(dev, "invalid operations.\n");
  1265. return ret;
  1266. }
  1267. /* Reset status */
  1268. fimc_write(ctx, 0x0, EXYNOS_CISTATUS);
  1269. cfg0 = fimc_read(ctx, EXYNOS_CIIMGCPT);
  1270. cfg0 &= ~EXYNOS_CIIMGCPT_IMGCPTEN_SC;
  1271. cfg0 |= EXYNOS_CIIMGCPT_IMGCPTEN_SC;
  1272. /* Scaler */
  1273. cfg1 = fimc_read(ctx, EXYNOS_CISCCTRL);
  1274. cfg1 &= ~EXYNOS_CISCCTRL_SCAN_MASK;
  1275. cfg1 |= (EXYNOS_CISCCTRL_PROGRESSIVE |
  1276. EXYNOS_CISCCTRL_SCALERSTART);
  1277. fimc_write(ctx, cfg1, EXYNOS_CISCCTRL);
  1278. /* Enable image capture*/
  1279. cfg0 |= EXYNOS_CIIMGCPT_IMGCPTEN;
  1280. fimc_write(ctx, cfg0, EXYNOS_CIIMGCPT);
  1281. /* Disable frame end irq */
  1282. fimc_clear_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_END_DISABLE);
  1283. fimc_clear_bits(ctx, EXYNOS_CIOCTRL, EXYNOS_CIOCTRL_WEAVE_MASK);
  1284. if (cmd == IPP_CMD_M2M)
  1285. fimc_set_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
  1286. return 0;
  1287. }
  1288. static void fimc_ippdrv_stop(struct device *dev, enum drm_exynos_ipp_cmd cmd)
  1289. {
  1290. struct fimc_context *ctx = get_fimc_context(dev);
  1291. struct drm_exynos_ipp_set_wb set_wb = {0, 0};
  1292. u32 cfg;
  1293. DRM_DEBUG_KMS("cmd[%d]\n", cmd);
  1294. switch (cmd) {
  1295. case IPP_CMD_M2M:
  1296. /* Source clear */
  1297. cfg = fimc_read(ctx, EXYNOS_MSCTRL);
  1298. cfg &= ~EXYNOS_MSCTRL_INPUT_MASK;
  1299. cfg &= ~EXYNOS_MSCTRL_ENVID;
  1300. fimc_write(ctx, cfg, EXYNOS_MSCTRL);
  1301. break;
  1302. case IPP_CMD_WB:
  1303. exynos_drm_ippnb_send_event(IPP_SET_WRITEBACK, (void *)&set_wb);
  1304. break;
  1305. case IPP_CMD_OUTPUT:
  1306. default:
  1307. dev_err(dev, "invalid operations.\n");
  1308. break;
  1309. }
  1310. fimc_mask_irq(ctx, false);
  1311. /* reset sequence */
  1312. fimc_write(ctx, 0x0, EXYNOS_CIFCNTSEQ);
  1313. /* Scaler disable */
  1314. fimc_clear_bits(ctx, EXYNOS_CISCCTRL, EXYNOS_CISCCTRL_SCALERSTART);
  1315. /* Disable image capture */
  1316. fimc_clear_bits(ctx, EXYNOS_CIIMGCPT,
  1317. EXYNOS_CIIMGCPT_IMGCPTEN_SC | EXYNOS_CIIMGCPT_IMGCPTEN);
  1318. /* Enable frame end irq */
  1319. fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_END_DISABLE);
  1320. }
  1321. static void fimc_put_clocks(struct fimc_context *ctx)
  1322. {
  1323. int i;
  1324. for (i = 0; i < FIMC_CLKS_MAX; i++) {
  1325. if (IS_ERR(ctx->clocks[i]))
  1326. continue;
  1327. clk_put(ctx->clocks[i]);
  1328. ctx->clocks[i] = ERR_PTR(-EINVAL);
  1329. }
  1330. }
  1331. static int fimc_setup_clocks(struct fimc_context *ctx)
  1332. {
  1333. struct device *fimc_dev = ctx->ippdrv.dev;
  1334. struct device *dev;
  1335. int ret, i;
  1336. for (i = 0; i < FIMC_CLKS_MAX; i++)
  1337. ctx->clocks[i] = ERR_PTR(-EINVAL);
  1338. for (i = 0; i < FIMC_CLKS_MAX; i++) {
  1339. if (i == FIMC_CLK_WB_A || i == FIMC_CLK_WB_B)
  1340. dev = fimc_dev->parent;
  1341. else
  1342. dev = fimc_dev;
  1343. ctx->clocks[i] = clk_get(dev, fimc_clock_names[i]);
  1344. if (IS_ERR(ctx->clocks[i])) {
  1345. if (i >= FIMC_CLK_MUX)
  1346. break;
  1347. ret = PTR_ERR(ctx->clocks[i]);
  1348. dev_err(fimc_dev, "failed to get clock: %s\n",
  1349. fimc_clock_names[i]);
  1350. goto e_clk_free;
  1351. }
  1352. }
  1353. /* Optional FIMC LCLK parent clock setting */
  1354. if (!IS_ERR(ctx->clocks[FIMC_CLK_PARENT])) {
  1355. ret = clk_set_parent(ctx->clocks[FIMC_CLK_MUX],
  1356. ctx->clocks[FIMC_CLK_PARENT]);
  1357. if (ret < 0) {
  1358. dev_err(fimc_dev, "failed to set parent.\n");
  1359. goto e_clk_free;
  1360. }
  1361. }
  1362. ret = clk_set_rate(ctx->clocks[FIMC_CLK_LCLK], ctx->clk_frequency);
  1363. if (ret < 0)
  1364. goto e_clk_free;
  1365. ret = clk_prepare_enable(ctx->clocks[FIMC_CLK_LCLK]);
  1366. if (!ret)
  1367. return ret;
  1368. e_clk_free:
  1369. fimc_put_clocks(ctx);
  1370. return ret;
  1371. }
  1372. static int fimc_parse_dt(struct fimc_context *ctx)
  1373. {
  1374. struct device_node *node = ctx->ippdrv.dev->of_node;
  1375. /* Handle only devices that support the LCD Writeback data path */
  1376. if (!of_property_read_bool(node, "samsung,lcd-wb"))
  1377. return -ENODEV;
  1378. if (of_property_read_u32(node, "clock-frequency",
  1379. &ctx->clk_frequency))
  1380. ctx->clk_frequency = FIMC_DEFAULT_LCLK_FREQUENCY;
  1381. ctx->id = of_alias_get_id(node, "fimc");
  1382. if (ctx->id < 0) {
  1383. dev_err(ctx->ippdrv.dev, "failed to get node alias id.\n");
  1384. return -EINVAL;
  1385. }
  1386. return 0;
  1387. }
  1388. static int fimc_probe(struct platform_device *pdev)
  1389. {
  1390. struct device *dev = &pdev->dev;
  1391. struct fimc_context *ctx;
  1392. struct resource *res;
  1393. struct exynos_drm_ippdrv *ippdrv;
  1394. int ret;
  1395. if (!dev->of_node) {
  1396. dev_err(dev, "device tree node not found.\n");
  1397. return -ENODEV;
  1398. }
  1399. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1400. if (!ctx)
  1401. return -ENOMEM;
  1402. ctx->ippdrv.dev = dev;
  1403. ret = fimc_parse_dt(ctx);
  1404. if (ret < 0)
  1405. return ret;
  1406. ctx->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
  1407. "samsung,sysreg");
  1408. if (IS_ERR(ctx->sysreg)) {
  1409. dev_err(dev, "syscon regmap lookup failed.\n");
  1410. return PTR_ERR(ctx->sysreg);
  1411. }
  1412. /* resource memory */
  1413. ctx->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1414. ctx->regs = devm_ioremap_resource(dev, ctx->regs_res);
  1415. if (IS_ERR(ctx->regs))
  1416. return PTR_ERR(ctx->regs);
  1417. /* resource irq */
  1418. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1419. if (!res) {
  1420. dev_err(dev, "failed to request irq resource.\n");
  1421. return -ENOENT;
  1422. }
  1423. ctx->irq = res->start;
  1424. ret = devm_request_threaded_irq(dev, ctx->irq, NULL, fimc_irq_handler,
  1425. IRQF_ONESHOT, "drm_fimc", ctx);
  1426. if (ret < 0) {
  1427. dev_err(dev, "failed to request irq.\n");
  1428. return ret;
  1429. }
  1430. ret = fimc_setup_clocks(ctx);
  1431. if (ret < 0)
  1432. return ret;
  1433. ippdrv = &ctx->ippdrv;
  1434. ippdrv->ops[EXYNOS_DRM_OPS_SRC] = &fimc_src_ops;
  1435. ippdrv->ops[EXYNOS_DRM_OPS_DST] = &fimc_dst_ops;
  1436. ippdrv->check_property = fimc_ippdrv_check_property;
  1437. ippdrv->reset = fimc_ippdrv_reset;
  1438. ippdrv->start = fimc_ippdrv_start;
  1439. ippdrv->stop = fimc_ippdrv_stop;
  1440. ret = fimc_init_prop_list(ippdrv);
  1441. if (ret < 0) {
  1442. dev_err(dev, "failed to init property list.\n");
  1443. goto err_put_clk;
  1444. }
  1445. DRM_DEBUG_KMS("id[%d]ippdrv[0x%x]\n", ctx->id, (int)ippdrv);
  1446. spin_lock_init(&ctx->lock);
  1447. platform_set_drvdata(pdev, ctx);
  1448. pm_runtime_enable(dev);
  1449. ret = exynos_drm_ippdrv_register(ippdrv);
  1450. if (ret < 0) {
  1451. dev_err(dev, "failed to register drm fimc device.\n");
  1452. goto err_pm_dis;
  1453. }
  1454. dev_info(dev, "drm fimc registered successfully.\n");
  1455. return 0;
  1456. err_pm_dis:
  1457. pm_runtime_disable(dev);
  1458. err_put_clk:
  1459. fimc_put_clocks(ctx);
  1460. return ret;
  1461. }
  1462. static int fimc_remove(struct platform_device *pdev)
  1463. {
  1464. struct device *dev = &pdev->dev;
  1465. struct fimc_context *ctx = get_fimc_context(dev);
  1466. struct exynos_drm_ippdrv *ippdrv = &ctx->ippdrv;
  1467. exynos_drm_ippdrv_unregister(ippdrv);
  1468. fimc_put_clocks(ctx);
  1469. pm_runtime_set_suspended(dev);
  1470. pm_runtime_disable(dev);
  1471. return 0;
  1472. }
  1473. #ifdef CONFIG_PM
  1474. static int fimc_clk_ctrl(struct fimc_context *ctx, bool enable)
  1475. {
  1476. DRM_DEBUG_KMS("enable[%d]\n", enable);
  1477. if (enable) {
  1478. clk_prepare_enable(ctx->clocks[FIMC_CLK_GATE]);
  1479. clk_prepare_enable(ctx->clocks[FIMC_CLK_WB_A]);
  1480. ctx->suspended = false;
  1481. } else {
  1482. clk_disable_unprepare(ctx->clocks[FIMC_CLK_GATE]);
  1483. clk_disable_unprepare(ctx->clocks[FIMC_CLK_WB_A]);
  1484. ctx->suspended = true;
  1485. }
  1486. return 0;
  1487. }
  1488. #ifdef CONFIG_PM_SLEEP
  1489. static int fimc_suspend(struct device *dev)
  1490. {
  1491. struct fimc_context *ctx = get_fimc_context(dev);
  1492. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1493. if (pm_runtime_suspended(dev))
  1494. return 0;
  1495. return fimc_clk_ctrl(ctx, false);
  1496. }
  1497. static int fimc_resume(struct device *dev)
  1498. {
  1499. struct fimc_context *ctx = get_fimc_context(dev);
  1500. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1501. if (!pm_runtime_suspended(dev))
  1502. return fimc_clk_ctrl(ctx, true);
  1503. return 0;
  1504. }
  1505. #endif
  1506. static int fimc_runtime_suspend(struct device *dev)
  1507. {
  1508. struct fimc_context *ctx = get_fimc_context(dev);
  1509. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1510. return fimc_clk_ctrl(ctx, false);
  1511. }
  1512. static int fimc_runtime_resume(struct device *dev)
  1513. {
  1514. struct fimc_context *ctx = get_fimc_context(dev);
  1515. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1516. return fimc_clk_ctrl(ctx, true);
  1517. }
  1518. #endif
  1519. static const struct dev_pm_ops fimc_pm_ops = {
  1520. SET_SYSTEM_SLEEP_PM_OPS(fimc_suspend, fimc_resume)
  1521. SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
  1522. };
  1523. static const struct of_device_id fimc_of_match[] = {
  1524. { .compatible = "samsung,exynos4210-fimc" },
  1525. { .compatible = "samsung,exynos4212-fimc" },
  1526. { },
  1527. };
  1528. MODULE_DEVICE_TABLE(of, fimc_of_match);
  1529. struct platform_driver fimc_driver = {
  1530. .probe = fimc_probe,
  1531. .remove = fimc_remove,
  1532. .driver = {
  1533. .of_match_table = fimc_of_match,
  1534. .name = "exynos-drm-fimc",
  1535. .owner = THIS_MODULE,
  1536. .pm = &fimc_pm_ops,
  1537. },
  1538. };