exynos_drm_fimc.c 44 KB

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