exynos_drm_fimc.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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/component.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/regmap.h>
  19. #include <linux/clk.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/of.h>
  22. #include <linux/spinlock.h>
  23. #include <drm/drmP.h>
  24. #include <drm/exynos_drm.h>
  25. #include "regs-fimc.h"
  26. #include "exynos_drm_drv.h"
  27. #include "exynos_drm_iommu.h"
  28. #include "exynos_drm_ipp.h"
  29. /*
  30. * FIMC stands for Fully Interactive Mobile Camera and
  31. * supports image scaler/rotator and input/output DMA operations.
  32. * input DMA reads image data from the memory.
  33. * output DMA writes image data to memory.
  34. * FIMC supports image rotation and image effect functions.
  35. */
  36. #define FIMC_MAX_DEVS 4
  37. #define FIMC_MAX_SRC 2
  38. #define FIMC_MAX_DST 32
  39. #define FIMC_SHFACTOR 10
  40. #define FIMC_BUF_STOP 1
  41. #define FIMC_BUF_START 2
  42. #define FIMC_WIDTH_ITU_709 1280
  43. #define FIMC_AUTOSUSPEND_DELAY 2000
  44. static unsigned int fimc_mask = 0xc;
  45. module_param_named(fimc_devs, fimc_mask, uint, 0644);
  46. MODULE_PARM_DESC(fimc_devs, "Alias mask for assigning FIMC devices to Exynos DRM");
  47. #define get_fimc_context(dev) platform_get_drvdata(to_platform_device(dev))
  48. enum {
  49. FIMC_CLK_LCLK,
  50. FIMC_CLK_GATE,
  51. FIMC_CLK_WB_A,
  52. FIMC_CLK_WB_B,
  53. FIMC_CLKS_MAX
  54. };
  55. static const char * const fimc_clock_names[] = {
  56. [FIMC_CLK_LCLK] = "sclk_fimc",
  57. [FIMC_CLK_GATE] = "fimc",
  58. [FIMC_CLK_WB_A] = "pxl_async0",
  59. [FIMC_CLK_WB_B] = "pxl_async1",
  60. };
  61. /*
  62. * A structure of scaler.
  63. *
  64. * @range: narrow, wide.
  65. * @bypass: unused scaler path.
  66. * @up_h: horizontal scale up.
  67. * @up_v: vertical scale up.
  68. * @hratio: horizontal ratio.
  69. * @vratio: vertical ratio.
  70. */
  71. struct fimc_scaler {
  72. bool range;
  73. bool bypass;
  74. bool up_h;
  75. bool up_v;
  76. u32 hratio;
  77. u32 vratio;
  78. };
  79. /*
  80. * A structure of fimc context.
  81. *
  82. * @regs_res: register resources.
  83. * @regs: memory mapped io registers.
  84. * @lock: locking of operations.
  85. * @clocks: fimc clocks.
  86. * @sc: scaler infomations.
  87. * @pol: porarity of writeback.
  88. * @id: fimc id.
  89. * @irq: irq number.
  90. */
  91. struct fimc_context {
  92. struct exynos_drm_ipp ipp;
  93. struct drm_device *drm_dev;
  94. struct device *dev;
  95. struct exynos_drm_ipp_task *task;
  96. struct exynos_drm_ipp_formats *formats;
  97. unsigned int num_formats;
  98. struct resource *regs_res;
  99. void __iomem *regs;
  100. spinlock_t lock;
  101. struct clk *clocks[FIMC_CLKS_MAX];
  102. struct fimc_scaler sc;
  103. int id;
  104. int irq;
  105. };
  106. static u32 fimc_read(struct fimc_context *ctx, u32 reg)
  107. {
  108. return readl(ctx->regs + reg);
  109. }
  110. static void fimc_write(struct fimc_context *ctx, u32 val, u32 reg)
  111. {
  112. writel(val, ctx->regs + reg);
  113. }
  114. static void fimc_set_bits(struct fimc_context *ctx, u32 reg, u32 bits)
  115. {
  116. void __iomem *r = ctx->regs + reg;
  117. writel(readl(r) | bits, r);
  118. }
  119. static void fimc_clear_bits(struct fimc_context *ctx, u32 reg, u32 bits)
  120. {
  121. void __iomem *r = ctx->regs + reg;
  122. writel(readl(r) & ~bits, r);
  123. }
  124. static void fimc_sw_reset(struct fimc_context *ctx)
  125. {
  126. u32 cfg;
  127. /* stop dma operation */
  128. cfg = fimc_read(ctx, EXYNOS_CISTATUS);
  129. if (EXYNOS_CISTATUS_GET_ENVID_STATUS(cfg))
  130. fimc_clear_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
  131. fimc_set_bits(ctx, EXYNOS_CISRCFMT, EXYNOS_CISRCFMT_ITU601_8BIT);
  132. /* disable image capture */
  133. fimc_clear_bits(ctx, EXYNOS_CIIMGCPT,
  134. EXYNOS_CIIMGCPT_IMGCPTEN_SC | EXYNOS_CIIMGCPT_IMGCPTEN);
  135. /* s/w reset */
  136. fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_SWRST);
  137. /* s/w reset complete */
  138. fimc_clear_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_SWRST);
  139. /* reset sequence */
  140. fimc_write(ctx, 0x0, EXYNOS_CIFCNTSEQ);
  141. }
  142. static void fimc_set_type_ctrl(struct fimc_context *ctx)
  143. {
  144. u32 cfg;
  145. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  146. cfg &= ~(EXYNOS_CIGCTRL_TESTPATTERN_MASK |
  147. EXYNOS_CIGCTRL_SELCAM_ITU_MASK |
  148. EXYNOS_CIGCTRL_SELCAM_MIPI_MASK |
  149. EXYNOS_CIGCTRL_SELCAM_FIMC_MASK |
  150. EXYNOS_CIGCTRL_SELWB_CAMIF_MASK |
  151. EXYNOS_CIGCTRL_SELWRITEBACK_MASK);
  152. cfg |= (EXYNOS_CIGCTRL_SELCAM_ITU_A |
  153. EXYNOS_CIGCTRL_SELWRITEBACK_A |
  154. EXYNOS_CIGCTRL_SELCAM_MIPI_A |
  155. EXYNOS_CIGCTRL_SELCAM_FIMC_ITU);
  156. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  157. }
  158. static void fimc_handle_jpeg(struct fimc_context *ctx, bool enable)
  159. {
  160. u32 cfg;
  161. DRM_DEBUG_KMS("enable[%d]\n", enable);
  162. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  163. if (enable)
  164. cfg |= EXYNOS_CIGCTRL_CAM_JPEG;
  165. else
  166. cfg &= ~EXYNOS_CIGCTRL_CAM_JPEG;
  167. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  168. }
  169. static void fimc_mask_irq(struct fimc_context *ctx, bool enable)
  170. {
  171. u32 cfg;
  172. DRM_DEBUG_KMS("enable[%d]\n", enable);
  173. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  174. if (enable) {
  175. cfg &= ~EXYNOS_CIGCTRL_IRQ_OVFEN;
  176. cfg |= EXYNOS_CIGCTRL_IRQ_ENABLE | EXYNOS_CIGCTRL_IRQ_LEVEL;
  177. } else
  178. cfg &= ~EXYNOS_CIGCTRL_IRQ_ENABLE;
  179. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  180. }
  181. static void fimc_clear_irq(struct fimc_context *ctx)
  182. {
  183. fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_CLR);
  184. }
  185. static bool fimc_check_ovf(struct fimc_context *ctx)
  186. {
  187. u32 status, flag;
  188. status = fimc_read(ctx, EXYNOS_CISTATUS);
  189. flag = EXYNOS_CISTATUS_OVFIY | EXYNOS_CISTATUS_OVFICB |
  190. EXYNOS_CISTATUS_OVFICR;
  191. DRM_DEBUG_KMS("flag[0x%x]\n", flag);
  192. if (status & flag) {
  193. fimc_set_bits(ctx, EXYNOS_CIWDOFST,
  194. EXYNOS_CIWDOFST_CLROVFIY | EXYNOS_CIWDOFST_CLROVFICB |
  195. EXYNOS_CIWDOFST_CLROVFICR);
  196. dev_err(ctx->dev, "occurred overflow at %d, status 0x%x.\n",
  197. ctx->id, status);
  198. return true;
  199. }
  200. return false;
  201. }
  202. static bool fimc_check_frame_end(struct fimc_context *ctx)
  203. {
  204. u32 cfg;
  205. cfg = fimc_read(ctx, EXYNOS_CISTATUS);
  206. DRM_DEBUG_KMS("cfg[0x%x]\n", cfg);
  207. if (!(cfg & EXYNOS_CISTATUS_FRAMEEND))
  208. return false;
  209. cfg &= ~(EXYNOS_CISTATUS_FRAMEEND);
  210. fimc_write(ctx, cfg, EXYNOS_CISTATUS);
  211. return true;
  212. }
  213. static int fimc_get_buf_id(struct fimc_context *ctx)
  214. {
  215. u32 cfg;
  216. int frame_cnt, buf_id;
  217. cfg = fimc_read(ctx, EXYNOS_CISTATUS2);
  218. frame_cnt = EXYNOS_CISTATUS2_GET_FRAMECOUNT_BEFORE(cfg);
  219. if (frame_cnt == 0)
  220. frame_cnt = EXYNOS_CISTATUS2_GET_FRAMECOUNT_PRESENT(cfg);
  221. DRM_DEBUG_KMS("present[%d]before[%d]\n",
  222. EXYNOS_CISTATUS2_GET_FRAMECOUNT_PRESENT(cfg),
  223. EXYNOS_CISTATUS2_GET_FRAMECOUNT_BEFORE(cfg));
  224. if (frame_cnt == 0) {
  225. DRM_ERROR("failed to get frame count.\n");
  226. return -EIO;
  227. }
  228. buf_id = frame_cnt - 1;
  229. DRM_DEBUG_KMS("buf_id[%d]\n", buf_id);
  230. return buf_id;
  231. }
  232. static void fimc_handle_lastend(struct fimc_context *ctx, bool enable)
  233. {
  234. u32 cfg;
  235. DRM_DEBUG_KMS("enable[%d]\n", enable);
  236. cfg = fimc_read(ctx, EXYNOS_CIOCTRL);
  237. if (enable)
  238. cfg |= EXYNOS_CIOCTRL_LASTENDEN;
  239. else
  240. cfg &= ~EXYNOS_CIOCTRL_LASTENDEN;
  241. fimc_write(ctx, cfg, EXYNOS_CIOCTRL);
  242. }
  243. static void fimc_src_set_fmt_order(struct fimc_context *ctx, u32 fmt)
  244. {
  245. u32 cfg;
  246. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  247. /* RGB */
  248. cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
  249. cfg &= ~EXYNOS_CISCCTRL_INRGB_FMT_RGB_MASK;
  250. switch (fmt) {
  251. case DRM_FORMAT_RGB565:
  252. cfg |= EXYNOS_CISCCTRL_INRGB_FMT_RGB565;
  253. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  254. return;
  255. case DRM_FORMAT_RGB888:
  256. case DRM_FORMAT_XRGB8888:
  257. cfg |= EXYNOS_CISCCTRL_INRGB_FMT_RGB888;
  258. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  259. return;
  260. default:
  261. /* bypass */
  262. break;
  263. }
  264. /* YUV */
  265. cfg = fimc_read(ctx, EXYNOS_MSCTRL);
  266. cfg &= ~(EXYNOS_MSCTRL_ORDER2P_SHIFT_MASK |
  267. EXYNOS_MSCTRL_C_INT_IN_2PLANE |
  268. EXYNOS_MSCTRL_ORDER422_YCBYCR);
  269. switch (fmt) {
  270. case DRM_FORMAT_YUYV:
  271. cfg |= EXYNOS_MSCTRL_ORDER422_YCBYCR;
  272. break;
  273. case DRM_FORMAT_YVYU:
  274. cfg |= EXYNOS_MSCTRL_ORDER422_YCRYCB;
  275. break;
  276. case DRM_FORMAT_UYVY:
  277. cfg |= EXYNOS_MSCTRL_ORDER422_CBYCRY;
  278. break;
  279. case DRM_FORMAT_VYUY:
  280. case DRM_FORMAT_YUV444:
  281. cfg |= EXYNOS_MSCTRL_ORDER422_CRYCBY;
  282. break;
  283. case DRM_FORMAT_NV21:
  284. case DRM_FORMAT_NV61:
  285. cfg |= (EXYNOS_MSCTRL_ORDER2P_LSB_CRCB |
  286. EXYNOS_MSCTRL_C_INT_IN_2PLANE);
  287. break;
  288. case DRM_FORMAT_YUV422:
  289. case DRM_FORMAT_YUV420:
  290. case DRM_FORMAT_YVU420:
  291. cfg |= EXYNOS_MSCTRL_C_INT_IN_3PLANE;
  292. break;
  293. case DRM_FORMAT_NV12:
  294. case DRM_FORMAT_NV16:
  295. cfg |= (EXYNOS_MSCTRL_ORDER2P_LSB_CBCR |
  296. EXYNOS_MSCTRL_C_INT_IN_2PLANE);
  297. break;
  298. }
  299. fimc_write(ctx, cfg, EXYNOS_MSCTRL);
  300. }
  301. static void fimc_src_set_fmt(struct fimc_context *ctx, u32 fmt, bool tiled)
  302. {
  303. u32 cfg;
  304. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  305. cfg = fimc_read(ctx, EXYNOS_MSCTRL);
  306. cfg &= ~EXYNOS_MSCTRL_INFORMAT_RGB;
  307. switch (fmt) {
  308. case DRM_FORMAT_RGB565:
  309. case DRM_FORMAT_RGB888:
  310. case DRM_FORMAT_XRGB8888:
  311. cfg |= EXYNOS_MSCTRL_INFORMAT_RGB;
  312. break;
  313. case DRM_FORMAT_YUV444:
  314. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR420;
  315. break;
  316. case DRM_FORMAT_YUYV:
  317. case DRM_FORMAT_YVYU:
  318. case DRM_FORMAT_UYVY:
  319. case DRM_FORMAT_VYUY:
  320. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR422_1PLANE;
  321. break;
  322. case DRM_FORMAT_NV16:
  323. case DRM_FORMAT_NV61:
  324. case DRM_FORMAT_YUV422:
  325. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR422;
  326. break;
  327. case DRM_FORMAT_YUV420:
  328. case DRM_FORMAT_YVU420:
  329. case DRM_FORMAT_NV12:
  330. case DRM_FORMAT_NV21:
  331. cfg |= EXYNOS_MSCTRL_INFORMAT_YCBCR420;
  332. break;
  333. }
  334. fimc_write(ctx, cfg, EXYNOS_MSCTRL);
  335. cfg = fimc_read(ctx, EXYNOS_CIDMAPARAM);
  336. cfg &= ~EXYNOS_CIDMAPARAM_R_MODE_MASK;
  337. if (tiled)
  338. cfg |= EXYNOS_CIDMAPARAM_R_MODE_64X32;
  339. else
  340. cfg |= EXYNOS_CIDMAPARAM_R_MODE_LINEAR;
  341. fimc_write(ctx, cfg, EXYNOS_CIDMAPARAM);
  342. fimc_src_set_fmt_order(ctx, fmt);
  343. }
  344. static void fimc_src_set_transf(struct fimc_context *ctx, unsigned int rotation)
  345. {
  346. unsigned int degree = rotation & DRM_MODE_ROTATE_MASK;
  347. u32 cfg1, cfg2;
  348. DRM_DEBUG_KMS("rotation[%x]\n", rotation);
  349. cfg1 = fimc_read(ctx, EXYNOS_MSCTRL);
  350. cfg1 &= ~(EXYNOS_MSCTRL_FLIP_X_MIRROR |
  351. EXYNOS_MSCTRL_FLIP_Y_MIRROR);
  352. cfg2 = fimc_read(ctx, EXYNOS_CITRGFMT);
  353. cfg2 &= ~EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
  354. switch (degree) {
  355. case DRM_MODE_ROTATE_0:
  356. if (rotation & DRM_MODE_REFLECT_X)
  357. cfg1 |= EXYNOS_MSCTRL_FLIP_X_MIRROR;
  358. if (rotation & DRM_MODE_REFLECT_Y)
  359. cfg1 |= EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  360. break;
  361. case DRM_MODE_ROTATE_90:
  362. cfg2 |= EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
  363. if (rotation & DRM_MODE_REFLECT_X)
  364. cfg1 |= EXYNOS_MSCTRL_FLIP_X_MIRROR;
  365. if (rotation & DRM_MODE_REFLECT_Y)
  366. cfg1 |= EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  367. break;
  368. case DRM_MODE_ROTATE_180:
  369. cfg1 |= (EXYNOS_MSCTRL_FLIP_X_MIRROR |
  370. EXYNOS_MSCTRL_FLIP_Y_MIRROR);
  371. if (rotation & DRM_MODE_REFLECT_X)
  372. cfg1 &= ~EXYNOS_MSCTRL_FLIP_X_MIRROR;
  373. if (rotation & DRM_MODE_REFLECT_Y)
  374. cfg1 &= ~EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  375. break;
  376. case DRM_MODE_ROTATE_270:
  377. cfg1 |= (EXYNOS_MSCTRL_FLIP_X_MIRROR |
  378. EXYNOS_MSCTRL_FLIP_Y_MIRROR);
  379. cfg2 |= EXYNOS_CITRGFMT_INROT90_CLOCKWISE;
  380. if (rotation & DRM_MODE_REFLECT_X)
  381. cfg1 &= ~EXYNOS_MSCTRL_FLIP_X_MIRROR;
  382. if (rotation & DRM_MODE_REFLECT_Y)
  383. cfg1 &= ~EXYNOS_MSCTRL_FLIP_Y_MIRROR;
  384. break;
  385. }
  386. fimc_write(ctx, cfg1, EXYNOS_MSCTRL);
  387. fimc_write(ctx, cfg2, EXYNOS_CITRGFMT);
  388. }
  389. static void fimc_set_window(struct fimc_context *ctx,
  390. struct exynos_drm_ipp_buffer *buf)
  391. {
  392. unsigned int real_width = buf->buf.pitch[0] / buf->format->cpp[0];
  393. u32 cfg, h1, h2, v1, v2;
  394. /* cropped image */
  395. h1 = buf->rect.x;
  396. h2 = real_width - buf->rect.w - buf->rect.x;
  397. v1 = buf->rect.y;
  398. v2 = buf->buf.height - buf->rect.h - buf->rect.y;
  399. DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]hsize[%d]vsize[%d]\n",
  400. buf->rect.x, buf->rect.y, buf->rect.w, buf->rect.h,
  401. real_width, buf->buf.height);
  402. DRM_DEBUG_KMS("h1[%d]h2[%d]v1[%d]v2[%d]\n", h1, h2, v1, v2);
  403. /*
  404. * set window offset 1, 2 size
  405. * check figure 43-21 in user manual
  406. */
  407. cfg = fimc_read(ctx, EXYNOS_CIWDOFST);
  408. cfg &= ~(EXYNOS_CIWDOFST_WINHOROFST_MASK |
  409. EXYNOS_CIWDOFST_WINVEROFST_MASK);
  410. cfg |= (EXYNOS_CIWDOFST_WINHOROFST(h1) |
  411. EXYNOS_CIWDOFST_WINVEROFST(v1));
  412. cfg |= EXYNOS_CIWDOFST_WINOFSEN;
  413. fimc_write(ctx, cfg, EXYNOS_CIWDOFST);
  414. cfg = (EXYNOS_CIWDOFST2_WINHOROFST2(h2) |
  415. EXYNOS_CIWDOFST2_WINVEROFST2(v2));
  416. fimc_write(ctx, cfg, EXYNOS_CIWDOFST2);
  417. }
  418. static void fimc_src_set_size(struct fimc_context *ctx,
  419. struct exynos_drm_ipp_buffer *buf)
  420. {
  421. unsigned int real_width = buf->buf.pitch[0] / buf->format->cpp[0];
  422. u32 cfg;
  423. DRM_DEBUG_KMS("hsize[%d]vsize[%d]\n", real_width, buf->buf.height);
  424. /* original size */
  425. cfg = (EXYNOS_ORGISIZE_HORIZONTAL(real_width) |
  426. EXYNOS_ORGISIZE_VERTICAL(buf->buf.height));
  427. fimc_write(ctx, cfg, EXYNOS_ORGISIZE);
  428. DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]\n", buf->rect.x, buf->rect.y,
  429. buf->rect.w, buf->rect.h);
  430. /* set input DMA image size */
  431. cfg = fimc_read(ctx, EXYNOS_CIREAL_ISIZE);
  432. cfg &= ~(EXYNOS_CIREAL_ISIZE_HEIGHT_MASK |
  433. EXYNOS_CIREAL_ISIZE_WIDTH_MASK);
  434. cfg |= (EXYNOS_CIREAL_ISIZE_WIDTH(buf->rect.w) |
  435. EXYNOS_CIREAL_ISIZE_HEIGHT(buf->rect.h));
  436. fimc_write(ctx, cfg, EXYNOS_CIREAL_ISIZE);
  437. /*
  438. * set input FIFO image size
  439. * for now, we support only ITU601 8 bit mode
  440. */
  441. cfg = (EXYNOS_CISRCFMT_ITU601_8BIT |
  442. EXYNOS_CISRCFMT_SOURCEHSIZE(real_width) |
  443. EXYNOS_CISRCFMT_SOURCEVSIZE(buf->buf.height));
  444. fimc_write(ctx, cfg, EXYNOS_CISRCFMT);
  445. /* offset Y(RGB), Cb, Cr */
  446. cfg = (EXYNOS_CIIYOFF_HORIZONTAL(buf->rect.x) |
  447. EXYNOS_CIIYOFF_VERTICAL(buf->rect.y));
  448. fimc_write(ctx, cfg, EXYNOS_CIIYOFF);
  449. cfg = (EXYNOS_CIICBOFF_HORIZONTAL(buf->rect.x) |
  450. EXYNOS_CIICBOFF_VERTICAL(buf->rect.y));
  451. fimc_write(ctx, cfg, EXYNOS_CIICBOFF);
  452. cfg = (EXYNOS_CIICROFF_HORIZONTAL(buf->rect.x) |
  453. EXYNOS_CIICROFF_VERTICAL(buf->rect.y));
  454. fimc_write(ctx, cfg, EXYNOS_CIICROFF);
  455. fimc_set_window(ctx, buf);
  456. }
  457. static void fimc_src_set_addr(struct fimc_context *ctx,
  458. struct exynos_drm_ipp_buffer *buf)
  459. {
  460. fimc_write(ctx, buf->dma_addr[0], EXYNOS_CIIYSA(0));
  461. fimc_write(ctx, buf->dma_addr[1], EXYNOS_CIICBSA(0));
  462. fimc_write(ctx, buf->dma_addr[2], EXYNOS_CIICRSA(0));
  463. }
  464. static void fimc_dst_set_fmt_order(struct fimc_context *ctx, u32 fmt)
  465. {
  466. u32 cfg;
  467. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  468. /* RGB */
  469. cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
  470. cfg &= ~EXYNOS_CISCCTRL_OUTRGB_FMT_RGB_MASK;
  471. switch (fmt) {
  472. case DRM_FORMAT_RGB565:
  473. cfg |= EXYNOS_CISCCTRL_OUTRGB_FMT_RGB565;
  474. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  475. return;
  476. case DRM_FORMAT_RGB888:
  477. cfg |= EXYNOS_CISCCTRL_OUTRGB_FMT_RGB888;
  478. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  479. return;
  480. case DRM_FORMAT_XRGB8888:
  481. cfg |= (EXYNOS_CISCCTRL_OUTRGB_FMT_RGB888 |
  482. EXYNOS_CISCCTRL_EXTRGB_EXTENSION);
  483. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  484. break;
  485. default:
  486. /* bypass */
  487. break;
  488. }
  489. /* YUV */
  490. cfg = fimc_read(ctx, EXYNOS_CIOCTRL);
  491. cfg &= ~(EXYNOS_CIOCTRL_ORDER2P_MASK |
  492. EXYNOS_CIOCTRL_ORDER422_MASK |
  493. EXYNOS_CIOCTRL_YCBCR_PLANE_MASK);
  494. switch (fmt) {
  495. case DRM_FORMAT_XRGB8888:
  496. cfg |= EXYNOS_CIOCTRL_ALPHA_OUT;
  497. break;
  498. case DRM_FORMAT_YUYV:
  499. cfg |= EXYNOS_CIOCTRL_ORDER422_YCBYCR;
  500. break;
  501. case DRM_FORMAT_YVYU:
  502. cfg |= EXYNOS_CIOCTRL_ORDER422_YCRYCB;
  503. break;
  504. case DRM_FORMAT_UYVY:
  505. cfg |= EXYNOS_CIOCTRL_ORDER422_CBYCRY;
  506. break;
  507. case DRM_FORMAT_VYUY:
  508. cfg |= EXYNOS_CIOCTRL_ORDER422_CRYCBY;
  509. break;
  510. case DRM_FORMAT_NV21:
  511. case DRM_FORMAT_NV61:
  512. cfg |= EXYNOS_CIOCTRL_ORDER2P_LSB_CRCB;
  513. cfg |= EXYNOS_CIOCTRL_YCBCR_2PLANE;
  514. break;
  515. case DRM_FORMAT_YUV422:
  516. case DRM_FORMAT_YUV420:
  517. case DRM_FORMAT_YVU420:
  518. cfg |= EXYNOS_CIOCTRL_YCBCR_3PLANE;
  519. break;
  520. case DRM_FORMAT_NV12:
  521. case DRM_FORMAT_NV16:
  522. cfg |= EXYNOS_CIOCTRL_ORDER2P_LSB_CBCR;
  523. cfg |= EXYNOS_CIOCTRL_YCBCR_2PLANE;
  524. break;
  525. }
  526. fimc_write(ctx, cfg, EXYNOS_CIOCTRL);
  527. }
  528. static void fimc_dst_set_fmt(struct fimc_context *ctx, u32 fmt, bool tiled)
  529. {
  530. u32 cfg;
  531. DRM_DEBUG_KMS("fmt[0x%x]\n", fmt);
  532. cfg = fimc_read(ctx, EXYNOS_CIEXTEN);
  533. if (fmt == DRM_FORMAT_AYUV) {
  534. cfg |= EXYNOS_CIEXTEN_YUV444_OUT;
  535. fimc_write(ctx, cfg, EXYNOS_CIEXTEN);
  536. } else {
  537. cfg &= ~EXYNOS_CIEXTEN_YUV444_OUT;
  538. fimc_write(ctx, cfg, EXYNOS_CIEXTEN);
  539. cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
  540. cfg &= ~EXYNOS_CITRGFMT_OUTFORMAT_MASK;
  541. switch (fmt) {
  542. case DRM_FORMAT_RGB565:
  543. case DRM_FORMAT_RGB888:
  544. case DRM_FORMAT_XRGB8888:
  545. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_RGB;
  546. break;
  547. case DRM_FORMAT_YUYV:
  548. case DRM_FORMAT_YVYU:
  549. case DRM_FORMAT_UYVY:
  550. case DRM_FORMAT_VYUY:
  551. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR422_1PLANE;
  552. break;
  553. case DRM_FORMAT_NV16:
  554. case DRM_FORMAT_NV61:
  555. case DRM_FORMAT_YUV422:
  556. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR422;
  557. break;
  558. case DRM_FORMAT_YUV420:
  559. case DRM_FORMAT_YVU420:
  560. case DRM_FORMAT_NV12:
  561. case DRM_FORMAT_NV21:
  562. cfg |= EXYNOS_CITRGFMT_OUTFORMAT_YCBCR420;
  563. break;
  564. }
  565. fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
  566. }
  567. cfg = fimc_read(ctx, EXYNOS_CIDMAPARAM);
  568. cfg &= ~EXYNOS_CIDMAPARAM_W_MODE_MASK;
  569. if (tiled)
  570. cfg |= EXYNOS_CIDMAPARAM_W_MODE_64X32;
  571. else
  572. cfg |= EXYNOS_CIDMAPARAM_W_MODE_LINEAR;
  573. fimc_write(ctx, cfg, EXYNOS_CIDMAPARAM);
  574. fimc_dst_set_fmt_order(ctx, fmt);
  575. }
  576. static void fimc_dst_set_transf(struct fimc_context *ctx, unsigned int rotation)
  577. {
  578. unsigned int degree = rotation & DRM_MODE_ROTATE_MASK;
  579. u32 cfg;
  580. DRM_DEBUG_KMS("rotation[0x%x]\n", rotation);
  581. cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
  582. cfg &= ~EXYNOS_CITRGFMT_FLIP_MASK;
  583. cfg &= ~EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE;
  584. switch (degree) {
  585. case DRM_MODE_ROTATE_0:
  586. if (rotation & DRM_MODE_REFLECT_X)
  587. cfg |= EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  588. if (rotation & DRM_MODE_REFLECT_Y)
  589. cfg |= EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  590. break;
  591. case DRM_MODE_ROTATE_90:
  592. cfg |= EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE;
  593. if (rotation & DRM_MODE_REFLECT_X)
  594. cfg |= EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  595. if (rotation & DRM_MODE_REFLECT_Y)
  596. cfg |= EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  597. break;
  598. case DRM_MODE_ROTATE_180:
  599. cfg |= (EXYNOS_CITRGFMT_FLIP_X_MIRROR |
  600. EXYNOS_CITRGFMT_FLIP_Y_MIRROR);
  601. if (rotation & DRM_MODE_REFLECT_X)
  602. cfg &= ~EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  603. if (rotation & DRM_MODE_REFLECT_Y)
  604. cfg &= ~EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  605. break;
  606. case DRM_MODE_ROTATE_270:
  607. cfg |= (EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE |
  608. EXYNOS_CITRGFMT_FLIP_X_MIRROR |
  609. EXYNOS_CITRGFMT_FLIP_Y_MIRROR);
  610. if (rotation & DRM_MODE_REFLECT_X)
  611. cfg &= ~EXYNOS_CITRGFMT_FLIP_X_MIRROR;
  612. if (rotation & DRM_MODE_REFLECT_Y)
  613. cfg &= ~EXYNOS_CITRGFMT_FLIP_Y_MIRROR;
  614. break;
  615. }
  616. fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
  617. }
  618. static int fimc_set_prescaler(struct fimc_context *ctx, struct fimc_scaler *sc,
  619. struct drm_exynos_ipp_task_rect *src,
  620. struct drm_exynos_ipp_task_rect *dst)
  621. {
  622. u32 cfg, cfg_ext, shfactor;
  623. u32 pre_dst_width, pre_dst_height;
  624. u32 hfactor, vfactor;
  625. int ret = 0;
  626. u32 src_w, src_h, dst_w, dst_h;
  627. cfg_ext = fimc_read(ctx, EXYNOS_CITRGFMT);
  628. if (cfg_ext & EXYNOS_CITRGFMT_INROT90_CLOCKWISE) {
  629. src_w = src->h;
  630. src_h = src->w;
  631. } else {
  632. src_w = src->w;
  633. src_h = src->h;
  634. }
  635. if (cfg_ext & EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE) {
  636. dst_w = dst->h;
  637. dst_h = dst->w;
  638. } else {
  639. dst_w = dst->w;
  640. dst_h = dst->h;
  641. }
  642. /* fimc_ippdrv_check_property assures that dividers are not null */
  643. hfactor = fls(src_w / dst_w / 2);
  644. if (hfactor > FIMC_SHFACTOR / 2) {
  645. dev_err(ctx->dev, "failed to get ratio horizontal.\n");
  646. return -EINVAL;
  647. }
  648. vfactor = fls(src_h / dst_h / 2);
  649. if (vfactor > FIMC_SHFACTOR / 2) {
  650. dev_err(ctx->dev, "failed to get ratio vertical.\n");
  651. return -EINVAL;
  652. }
  653. pre_dst_width = src_w >> hfactor;
  654. pre_dst_height = src_h >> vfactor;
  655. DRM_DEBUG_KMS("pre_dst_width[%d]pre_dst_height[%d]\n",
  656. pre_dst_width, pre_dst_height);
  657. DRM_DEBUG_KMS("hfactor[%d]vfactor[%d]\n", hfactor, vfactor);
  658. sc->hratio = (src_w << 14) / (dst_w << hfactor);
  659. sc->vratio = (src_h << 14) / (dst_h << vfactor);
  660. sc->up_h = (dst_w >= src_w) ? true : false;
  661. sc->up_v = (dst_h >= src_h) ? true : false;
  662. DRM_DEBUG_KMS("hratio[%d]vratio[%d]up_h[%d]up_v[%d]\n",
  663. sc->hratio, sc->vratio, sc->up_h, sc->up_v);
  664. shfactor = FIMC_SHFACTOR - (hfactor + vfactor);
  665. DRM_DEBUG_KMS("shfactor[%d]\n", shfactor);
  666. cfg = (EXYNOS_CISCPRERATIO_SHFACTOR(shfactor) |
  667. EXYNOS_CISCPRERATIO_PREHORRATIO(1 << hfactor) |
  668. EXYNOS_CISCPRERATIO_PREVERRATIO(1 << vfactor));
  669. fimc_write(ctx, cfg, EXYNOS_CISCPRERATIO);
  670. cfg = (EXYNOS_CISCPREDST_PREDSTWIDTH(pre_dst_width) |
  671. EXYNOS_CISCPREDST_PREDSTHEIGHT(pre_dst_height));
  672. fimc_write(ctx, cfg, EXYNOS_CISCPREDST);
  673. return ret;
  674. }
  675. static void fimc_set_scaler(struct fimc_context *ctx, struct fimc_scaler *sc)
  676. {
  677. u32 cfg, cfg_ext;
  678. DRM_DEBUG_KMS("range[%d]bypass[%d]up_h[%d]up_v[%d]\n",
  679. sc->range, sc->bypass, sc->up_h, sc->up_v);
  680. DRM_DEBUG_KMS("hratio[%d]vratio[%d]\n",
  681. sc->hratio, sc->vratio);
  682. cfg = fimc_read(ctx, EXYNOS_CISCCTRL);
  683. cfg &= ~(EXYNOS_CISCCTRL_SCALERBYPASS |
  684. EXYNOS_CISCCTRL_SCALEUP_H | EXYNOS_CISCCTRL_SCALEUP_V |
  685. EXYNOS_CISCCTRL_MAIN_V_RATIO_MASK |
  686. EXYNOS_CISCCTRL_MAIN_H_RATIO_MASK |
  687. EXYNOS_CISCCTRL_CSCR2Y_WIDE |
  688. EXYNOS_CISCCTRL_CSCY2R_WIDE);
  689. if (sc->range)
  690. cfg |= (EXYNOS_CISCCTRL_CSCR2Y_WIDE |
  691. EXYNOS_CISCCTRL_CSCY2R_WIDE);
  692. if (sc->bypass)
  693. cfg |= EXYNOS_CISCCTRL_SCALERBYPASS;
  694. if (sc->up_h)
  695. cfg |= EXYNOS_CISCCTRL_SCALEUP_H;
  696. if (sc->up_v)
  697. cfg |= EXYNOS_CISCCTRL_SCALEUP_V;
  698. cfg |= (EXYNOS_CISCCTRL_MAINHORRATIO((sc->hratio >> 6)) |
  699. EXYNOS_CISCCTRL_MAINVERRATIO((sc->vratio >> 6)));
  700. fimc_write(ctx, cfg, EXYNOS_CISCCTRL);
  701. cfg_ext = fimc_read(ctx, EXYNOS_CIEXTEN);
  702. cfg_ext &= ~EXYNOS_CIEXTEN_MAINHORRATIO_EXT_MASK;
  703. cfg_ext &= ~EXYNOS_CIEXTEN_MAINVERRATIO_EXT_MASK;
  704. cfg_ext |= (EXYNOS_CIEXTEN_MAINHORRATIO_EXT(sc->hratio) |
  705. EXYNOS_CIEXTEN_MAINVERRATIO_EXT(sc->vratio));
  706. fimc_write(ctx, cfg_ext, EXYNOS_CIEXTEN);
  707. }
  708. static void fimc_dst_set_size(struct fimc_context *ctx,
  709. struct exynos_drm_ipp_buffer *buf)
  710. {
  711. unsigned int real_width = buf->buf.pitch[0] / buf->format->cpp[0];
  712. u32 cfg, cfg_ext;
  713. DRM_DEBUG_KMS("hsize[%d]vsize[%d]\n", real_width, buf->buf.height);
  714. /* original size */
  715. cfg = (EXYNOS_ORGOSIZE_HORIZONTAL(real_width) |
  716. EXYNOS_ORGOSIZE_VERTICAL(buf->buf.height));
  717. fimc_write(ctx, cfg, EXYNOS_ORGOSIZE);
  718. DRM_DEBUG_KMS("x[%d]y[%d]w[%d]h[%d]\n", buf->rect.x, buf->rect.y,
  719. buf->rect.w, buf->rect.h);
  720. /* CSC ITU */
  721. cfg = fimc_read(ctx, EXYNOS_CIGCTRL);
  722. cfg &= ~EXYNOS_CIGCTRL_CSC_MASK;
  723. if (buf->buf.width >= FIMC_WIDTH_ITU_709)
  724. cfg |= EXYNOS_CIGCTRL_CSC_ITU709;
  725. else
  726. cfg |= EXYNOS_CIGCTRL_CSC_ITU601;
  727. fimc_write(ctx, cfg, EXYNOS_CIGCTRL);
  728. cfg_ext = fimc_read(ctx, EXYNOS_CITRGFMT);
  729. /* target image size */
  730. cfg = fimc_read(ctx, EXYNOS_CITRGFMT);
  731. cfg &= ~(EXYNOS_CITRGFMT_TARGETH_MASK |
  732. EXYNOS_CITRGFMT_TARGETV_MASK);
  733. if (cfg_ext & EXYNOS_CITRGFMT_OUTROT90_CLOCKWISE)
  734. cfg |= (EXYNOS_CITRGFMT_TARGETHSIZE(buf->rect.h) |
  735. EXYNOS_CITRGFMT_TARGETVSIZE(buf->rect.w));
  736. else
  737. cfg |= (EXYNOS_CITRGFMT_TARGETHSIZE(buf->rect.w) |
  738. EXYNOS_CITRGFMT_TARGETVSIZE(buf->rect.h));
  739. fimc_write(ctx, cfg, EXYNOS_CITRGFMT);
  740. /* target area */
  741. cfg = EXYNOS_CITAREA_TARGET_AREA(buf->rect.w * buf->rect.h);
  742. fimc_write(ctx, cfg, EXYNOS_CITAREA);
  743. /* offset Y(RGB), Cb, Cr */
  744. cfg = (EXYNOS_CIOYOFF_HORIZONTAL(buf->rect.x) |
  745. EXYNOS_CIOYOFF_VERTICAL(buf->rect.y));
  746. fimc_write(ctx, cfg, EXYNOS_CIOYOFF);
  747. cfg = (EXYNOS_CIOCBOFF_HORIZONTAL(buf->rect.x) |
  748. EXYNOS_CIOCBOFF_VERTICAL(buf->rect.y));
  749. fimc_write(ctx, cfg, EXYNOS_CIOCBOFF);
  750. cfg = (EXYNOS_CIOCROFF_HORIZONTAL(buf->rect.x) |
  751. EXYNOS_CIOCROFF_VERTICAL(buf->rect.y));
  752. fimc_write(ctx, cfg, EXYNOS_CIOCROFF);
  753. }
  754. static void fimc_dst_set_buf_seq(struct fimc_context *ctx, u32 buf_id,
  755. bool enqueue)
  756. {
  757. unsigned long flags;
  758. u32 buf_num;
  759. u32 cfg;
  760. DRM_DEBUG_KMS("buf_id[%d]enqueu[%d]\n", buf_id, enqueue);
  761. spin_lock_irqsave(&ctx->lock, flags);
  762. cfg = fimc_read(ctx, EXYNOS_CIFCNTSEQ);
  763. if (enqueue)
  764. cfg |= (1 << buf_id);
  765. else
  766. cfg &= ~(1 << buf_id);
  767. fimc_write(ctx, cfg, EXYNOS_CIFCNTSEQ);
  768. buf_num = hweight32(cfg);
  769. if (enqueue && buf_num >= FIMC_BUF_START)
  770. fimc_mask_irq(ctx, true);
  771. else if (!enqueue && buf_num <= FIMC_BUF_STOP)
  772. fimc_mask_irq(ctx, false);
  773. spin_unlock_irqrestore(&ctx->lock, flags);
  774. }
  775. static void fimc_dst_set_addr(struct fimc_context *ctx,
  776. struct exynos_drm_ipp_buffer *buf)
  777. {
  778. fimc_write(ctx, buf->dma_addr[0], EXYNOS_CIOYSA(0));
  779. fimc_write(ctx, buf->dma_addr[1], EXYNOS_CIOCBSA(0));
  780. fimc_write(ctx, buf->dma_addr[2], EXYNOS_CIOCRSA(0));
  781. fimc_dst_set_buf_seq(ctx, 0, true);
  782. }
  783. static void fimc_stop(struct fimc_context *ctx);
  784. static irqreturn_t fimc_irq_handler(int irq, void *dev_id)
  785. {
  786. struct fimc_context *ctx = dev_id;
  787. int buf_id;
  788. DRM_DEBUG_KMS("fimc id[%d]\n", ctx->id);
  789. fimc_clear_irq(ctx);
  790. if (fimc_check_ovf(ctx))
  791. return IRQ_NONE;
  792. if (!fimc_check_frame_end(ctx))
  793. return IRQ_NONE;
  794. buf_id = fimc_get_buf_id(ctx);
  795. if (buf_id < 0)
  796. return IRQ_HANDLED;
  797. DRM_DEBUG_KMS("buf_id[%d]\n", buf_id);
  798. if (ctx->task) {
  799. struct exynos_drm_ipp_task *task = ctx->task;
  800. ctx->task = NULL;
  801. pm_runtime_mark_last_busy(ctx->dev);
  802. pm_runtime_put_autosuspend(ctx->dev);
  803. exynos_drm_ipp_task_done(task, 0);
  804. }
  805. fimc_dst_set_buf_seq(ctx, buf_id, false);
  806. fimc_stop(ctx);
  807. return IRQ_HANDLED;
  808. }
  809. static void fimc_clear_addr(struct fimc_context *ctx)
  810. {
  811. int i;
  812. for (i = 0; i < FIMC_MAX_SRC; i++) {
  813. fimc_write(ctx, 0, EXYNOS_CIIYSA(i));
  814. fimc_write(ctx, 0, EXYNOS_CIICBSA(i));
  815. fimc_write(ctx, 0, EXYNOS_CIICRSA(i));
  816. }
  817. for (i = 0; i < FIMC_MAX_DST; i++) {
  818. fimc_write(ctx, 0, EXYNOS_CIOYSA(i));
  819. fimc_write(ctx, 0, EXYNOS_CIOCBSA(i));
  820. fimc_write(ctx, 0, EXYNOS_CIOCRSA(i));
  821. }
  822. }
  823. static void fimc_reset(struct fimc_context *ctx)
  824. {
  825. /* reset h/w block */
  826. fimc_sw_reset(ctx);
  827. /* reset scaler capability */
  828. memset(&ctx->sc, 0x0, sizeof(ctx->sc));
  829. fimc_clear_addr(ctx);
  830. }
  831. static void fimc_start(struct fimc_context *ctx)
  832. {
  833. u32 cfg0, cfg1;
  834. fimc_mask_irq(ctx, true);
  835. /* If set true, we can save jpeg about screen */
  836. fimc_handle_jpeg(ctx, false);
  837. fimc_set_scaler(ctx, &ctx->sc);
  838. fimc_set_type_ctrl(ctx);
  839. fimc_handle_lastend(ctx, false);
  840. /* setup dma */
  841. cfg0 = fimc_read(ctx, EXYNOS_MSCTRL);
  842. cfg0 &= ~EXYNOS_MSCTRL_INPUT_MASK;
  843. cfg0 |= EXYNOS_MSCTRL_INPUT_MEMORY;
  844. fimc_write(ctx, cfg0, EXYNOS_MSCTRL);
  845. /* Reset status */
  846. fimc_write(ctx, 0x0, EXYNOS_CISTATUS);
  847. cfg0 = fimc_read(ctx, EXYNOS_CIIMGCPT);
  848. cfg0 &= ~EXYNOS_CIIMGCPT_IMGCPTEN_SC;
  849. cfg0 |= EXYNOS_CIIMGCPT_IMGCPTEN_SC;
  850. /* Scaler */
  851. cfg1 = fimc_read(ctx, EXYNOS_CISCCTRL);
  852. cfg1 &= ~EXYNOS_CISCCTRL_SCAN_MASK;
  853. cfg1 |= (EXYNOS_CISCCTRL_PROGRESSIVE |
  854. EXYNOS_CISCCTRL_SCALERSTART);
  855. fimc_write(ctx, cfg1, EXYNOS_CISCCTRL);
  856. /* Enable image capture*/
  857. cfg0 |= EXYNOS_CIIMGCPT_IMGCPTEN;
  858. fimc_write(ctx, cfg0, EXYNOS_CIIMGCPT);
  859. /* Disable frame end irq */
  860. fimc_clear_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_END_DISABLE);
  861. fimc_clear_bits(ctx, EXYNOS_CIOCTRL, EXYNOS_CIOCTRL_WEAVE_MASK);
  862. fimc_set_bits(ctx, EXYNOS_MSCTRL, EXYNOS_MSCTRL_ENVID);
  863. }
  864. static void fimc_stop(struct fimc_context *ctx)
  865. {
  866. u32 cfg;
  867. /* Source clear */
  868. cfg = fimc_read(ctx, EXYNOS_MSCTRL);
  869. cfg &= ~EXYNOS_MSCTRL_INPUT_MASK;
  870. cfg &= ~EXYNOS_MSCTRL_ENVID;
  871. fimc_write(ctx, cfg, EXYNOS_MSCTRL);
  872. fimc_mask_irq(ctx, false);
  873. /* reset sequence */
  874. fimc_write(ctx, 0x0, EXYNOS_CIFCNTSEQ);
  875. /* Scaler disable */
  876. fimc_clear_bits(ctx, EXYNOS_CISCCTRL, EXYNOS_CISCCTRL_SCALERSTART);
  877. /* Disable image capture */
  878. fimc_clear_bits(ctx, EXYNOS_CIIMGCPT,
  879. EXYNOS_CIIMGCPT_IMGCPTEN_SC | EXYNOS_CIIMGCPT_IMGCPTEN);
  880. /* Enable frame end irq */
  881. fimc_set_bits(ctx, EXYNOS_CIGCTRL, EXYNOS_CIGCTRL_IRQ_END_DISABLE);
  882. }
  883. static int fimc_commit(struct exynos_drm_ipp *ipp,
  884. struct exynos_drm_ipp_task *task)
  885. {
  886. struct fimc_context *ctx =
  887. container_of(ipp, struct fimc_context, ipp);
  888. pm_runtime_get_sync(ctx->dev);
  889. ctx->task = task;
  890. fimc_src_set_fmt(ctx, task->src.buf.fourcc, task->src.buf.modifier);
  891. fimc_src_set_size(ctx, &task->src);
  892. fimc_src_set_transf(ctx, DRM_MODE_ROTATE_0);
  893. fimc_src_set_addr(ctx, &task->src);
  894. fimc_dst_set_fmt(ctx, task->dst.buf.fourcc, task->dst.buf.modifier);
  895. fimc_dst_set_transf(ctx, task->transform.rotation);
  896. fimc_dst_set_size(ctx, &task->dst);
  897. fimc_dst_set_addr(ctx, &task->dst);
  898. fimc_set_prescaler(ctx, &ctx->sc, &task->src.rect, &task->dst.rect);
  899. fimc_start(ctx);
  900. return 0;
  901. }
  902. static void fimc_abort(struct exynos_drm_ipp *ipp,
  903. struct exynos_drm_ipp_task *task)
  904. {
  905. struct fimc_context *ctx =
  906. container_of(ipp, struct fimc_context, ipp);
  907. fimc_reset(ctx);
  908. if (ctx->task) {
  909. struct exynos_drm_ipp_task *task = ctx->task;
  910. ctx->task = NULL;
  911. pm_runtime_mark_last_busy(ctx->dev);
  912. pm_runtime_put_autosuspend(ctx->dev);
  913. exynos_drm_ipp_task_done(task, -EIO);
  914. }
  915. }
  916. static struct exynos_drm_ipp_funcs ipp_funcs = {
  917. .commit = fimc_commit,
  918. .abort = fimc_abort,
  919. };
  920. static int fimc_bind(struct device *dev, struct device *master, void *data)
  921. {
  922. struct fimc_context *ctx = dev_get_drvdata(dev);
  923. struct drm_device *drm_dev = data;
  924. struct exynos_drm_ipp *ipp = &ctx->ipp;
  925. ctx->drm_dev = drm_dev;
  926. drm_iommu_attach_device(drm_dev, dev);
  927. exynos_drm_ipp_register(drm_dev, ipp, &ipp_funcs,
  928. DRM_EXYNOS_IPP_CAP_CROP | DRM_EXYNOS_IPP_CAP_ROTATE |
  929. DRM_EXYNOS_IPP_CAP_SCALE | DRM_EXYNOS_IPP_CAP_CONVERT,
  930. ctx->formats, ctx->num_formats, "fimc");
  931. dev_info(dev, "The exynos fimc has been probed successfully\n");
  932. return 0;
  933. }
  934. static void fimc_unbind(struct device *dev, struct device *master,
  935. void *data)
  936. {
  937. struct fimc_context *ctx = dev_get_drvdata(dev);
  938. struct drm_device *drm_dev = data;
  939. struct exynos_drm_ipp *ipp = &ctx->ipp;
  940. exynos_drm_ipp_unregister(drm_dev, ipp);
  941. drm_iommu_detach_device(drm_dev, dev);
  942. }
  943. static const struct component_ops fimc_component_ops = {
  944. .bind = fimc_bind,
  945. .unbind = fimc_unbind,
  946. };
  947. static void fimc_put_clocks(struct fimc_context *ctx)
  948. {
  949. int i;
  950. for (i = 0; i < FIMC_CLKS_MAX; i++) {
  951. if (IS_ERR(ctx->clocks[i]))
  952. continue;
  953. clk_put(ctx->clocks[i]);
  954. ctx->clocks[i] = ERR_PTR(-EINVAL);
  955. }
  956. }
  957. static int fimc_setup_clocks(struct fimc_context *ctx)
  958. {
  959. struct device *fimc_dev = ctx->dev;
  960. struct device *dev;
  961. int ret, i;
  962. for (i = 0; i < FIMC_CLKS_MAX; i++)
  963. ctx->clocks[i] = ERR_PTR(-EINVAL);
  964. for (i = 0; i < FIMC_CLKS_MAX; i++) {
  965. if (i == FIMC_CLK_WB_A || i == FIMC_CLK_WB_B)
  966. dev = fimc_dev->parent;
  967. else
  968. dev = fimc_dev;
  969. ctx->clocks[i] = clk_get(dev, fimc_clock_names[i]);
  970. if (IS_ERR(ctx->clocks[i])) {
  971. ret = PTR_ERR(ctx->clocks[i]);
  972. dev_err(fimc_dev, "failed to get clock: %s\n",
  973. fimc_clock_names[i]);
  974. goto e_clk_free;
  975. }
  976. }
  977. ret = clk_prepare_enable(ctx->clocks[FIMC_CLK_LCLK]);
  978. if (!ret)
  979. return ret;
  980. e_clk_free:
  981. fimc_put_clocks(ctx);
  982. return ret;
  983. }
  984. int exynos_drm_check_fimc_device(struct device *dev)
  985. {
  986. int id = of_alias_get_id(dev->of_node, "fimc");
  987. if (id >= 0 && (BIT(id) & fimc_mask))
  988. return 0;
  989. return -ENODEV;
  990. }
  991. static const unsigned int fimc_formats[] = {
  992. DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB565,
  993. DRM_FORMAT_NV12, DRM_FORMAT_NV16, DRM_FORMAT_NV21, DRM_FORMAT_NV61,
  994. DRM_FORMAT_UYVY, DRM_FORMAT_VYUY, DRM_FORMAT_YUYV, DRM_FORMAT_YVYU,
  995. DRM_FORMAT_YUV420, DRM_FORMAT_YVU420, DRM_FORMAT_YUV422,
  996. DRM_FORMAT_YUV444,
  997. };
  998. static const unsigned int fimc_tiled_formats[] = {
  999. DRM_FORMAT_NV12, DRM_FORMAT_NV21,
  1000. };
  1001. static const struct drm_exynos_ipp_limit fimc_4210_limits_v1[] = {
  1002. { IPP_SIZE_LIMIT(BUFFER, .h = { 16, 8192, 8 }, .v = { 16, 8192, 2 }) },
  1003. { IPP_SIZE_LIMIT(AREA, .h = { 16, 4224, 2 }, .v = { 16, 0, 2 }) },
  1004. { IPP_SIZE_LIMIT(ROTATED, .h = { 128, 1920 }, .v = { 128, 0 }) },
  1005. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 64, (1 << 16) * 64 },
  1006. .v = { (1 << 16) / 64, (1 << 16) * 64 }) },
  1007. };
  1008. static const struct drm_exynos_ipp_limit fimc_4210_limits_v2[] = {
  1009. { IPP_SIZE_LIMIT(BUFFER, .h = { 16, 8192, 8 }, .v = { 16, 8192, 2 }) },
  1010. { IPP_SIZE_LIMIT(AREA, .h = { 16, 1920, 2 }, .v = { 16, 0, 2 }) },
  1011. { IPP_SIZE_LIMIT(ROTATED, .h = { 128, 1366 }, .v = { 128, 0 }) },
  1012. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 64, (1 << 16) * 64 },
  1013. .v = { (1 << 16) / 64, (1 << 16) * 64 }) },
  1014. };
  1015. static const struct drm_exynos_ipp_limit fimc_4210_limits_tiled_v1[] = {
  1016. { IPP_SIZE_LIMIT(BUFFER, .h = { 128, 1920, 128 }, .v = { 32, 1920, 32 }) },
  1017. { IPP_SIZE_LIMIT(AREA, .h = { 128, 1920, 2 }, .v = { 128, 0, 2 }) },
  1018. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 64, (1 << 16) * 64 },
  1019. .v = { (1 << 16) / 64, (1 << 16) * 64 }) },
  1020. };
  1021. static const struct drm_exynos_ipp_limit fimc_4210_limits_tiled_v2[] = {
  1022. { IPP_SIZE_LIMIT(BUFFER, .h = { 128, 1920, 128 }, .v = { 32, 1920, 32 }) },
  1023. { IPP_SIZE_LIMIT(AREA, .h = { 128, 1366, 2 }, .v = { 128, 0, 2 }) },
  1024. { IPP_SCALE_LIMIT(.h = { (1 << 16) / 64, (1 << 16) * 64 },
  1025. .v = { (1 << 16) / 64, (1 << 16) * 64 }) },
  1026. };
  1027. static int fimc_probe(struct platform_device *pdev)
  1028. {
  1029. const struct drm_exynos_ipp_limit *limits;
  1030. struct exynos_drm_ipp_formats *formats;
  1031. struct device *dev = &pdev->dev;
  1032. struct fimc_context *ctx;
  1033. struct resource *res;
  1034. int ret;
  1035. int i, j, num_limits, num_formats;
  1036. if (exynos_drm_check_fimc_device(dev) != 0)
  1037. return -ENODEV;
  1038. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1039. if (!ctx)
  1040. return -ENOMEM;
  1041. ctx->dev = dev;
  1042. ctx->id = of_alias_get_id(dev->of_node, "fimc");
  1043. /* construct formats/limits array */
  1044. num_formats = ARRAY_SIZE(fimc_formats) + ARRAY_SIZE(fimc_tiled_formats);
  1045. formats = devm_kcalloc(dev, num_formats, sizeof(*formats),
  1046. GFP_KERNEL);
  1047. if (!formats)
  1048. return -ENOMEM;
  1049. /* linear formats */
  1050. if (ctx->id < 3) {
  1051. limits = fimc_4210_limits_v1;
  1052. num_limits = ARRAY_SIZE(fimc_4210_limits_v1);
  1053. } else {
  1054. limits = fimc_4210_limits_v2;
  1055. num_limits = ARRAY_SIZE(fimc_4210_limits_v2);
  1056. }
  1057. for (i = 0; i < ARRAY_SIZE(fimc_formats); i++) {
  1058. formats[i].fourcc = fimc_formats[i];
  1059. formats[i].type = DRM_EXYNOS_IPP_FORMAT_SOURCE |
  1060. DRM_EXYNOS_IPP_FORMAT_DESTINATION;
  1061. formats[i].limits = limits;
  1062. formats[i].num_limits = num_limits;
  1063. }
  1064. /* tiled formats */
  1065. if (ctx->id < 3) {
  1066. limits = fimc_4210_limits_tiled_v1;
  1067. num_limits = ARRAY_SIZE(fimc_4210_limits_tiled_v1);
  1068. } else {
  1069. limits = fimc_4210_limits_tiled_v2;
  1070. num_limits = ARRAY_SIZE(fimc_4210_limits_tiled_v2);
  1071. }
  1072. for (j = i, i = 0; i < ARRAY_SIZE(fimc_tiled_formats); j++, i++) {
  1073. formats[j].fourcc = fimc_tiled_formats[i];
  1074. formats[j].modifier = DRM_FORMAT_MOD_SAMSUNG_64_32_TILE;
  1075. formats[j].type = DRM_EXYNOS_IPP_FORMAT_SOURCE |
  1076. DRM_EXYNOS_IPP_FORMAT_DESTINATION;
  1077. formats[j].limits = limits;
  1078. formats[j].num_limits = num_limits;
  1079. }
  1080. ctx->formats = formats;
  1081. ctx->num_formats = num_formats;
  1082. /* resource memory */
  1083. ctx->regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1084. ctx->regs = devm_ioremap_resource(dev, ctx->regs_res);
  1085. if (IS_ERR(ctx->regs))
  1086. return PTR_ERR(ctx->regs);
  1087. /* resource irq */
  1088. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1089. if (!res) {
  1090. dev_err(dev, "failed to request irq resource.\n");
  1091. return -ENOENT;
  1092. }
  1093. ret = devm_request_irq(dev, res->start, fimc_irq_handler,
  1094. 0, dev_name(dev), ctx);
  1095. if (ret < 0) {
  1096. dev_err(dev, "failed to request irq.\n");
  1097. return ret;
  1098. }
  1099. ret = fimc_setup_clocks(ctx);
  1100. if (ret < 0)
  1101. return ret;
  1102. spin_lock_init(&ctx->lock);
  1103. platform_set_drvdata(pdev, ctx);
  1104. pm_runtime_use_autosuspend(dev);
  1105. pm_runtime_set_autosuspend_delay(dev, FIMC_AUTOSUSPEND_DELAY);
  1106. pm_runtime_enable(dev);
  1107. ret = component_add(dev, &fimc_component_ops);
  1108. if (ret)
  1109. goto err_pm_dis;
  1110. dev_info(dev, "drm fimc registered successfully.\n");
  1111. return 0;
  1112. err_pm_dis:
  1113. pm_runtime_dont_use_autosuspend(dev);
  1114. pm_runtime_disable(dev);
  1115. fimc_put_clocks(ctx);
  1116. return ret;
  1117. }
  1118. static int fimc_remove(struct platform_device *pdev)
  1119. {
  1120. struct device *dev = &pdev->dev;
  1121. struct fimc_context *ctx = get_fimc_context(dev);
  1122. component_del(dev, &fimc_component_ops);
  1123. pm_runtime_dont_use_autosuspend(dev);
  1124. pm_runtime_disable(dev);
  1125. fimc_put_clocks(ctx);
  1126. return 0;
  1127. }
  1128. #ifdef CONFIG_PM
  1129. static int fimc_runtime_suspend(struct device *dev)
  1130. {
  1131. struct fimc_context *ctx = get_fimc_context(dev);
  1132. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1133. clk_disable_unprepare(ctx->clocks[FIMC_CLK_GATE]);
  1134. return 0;
  1135. }
  1136. static int fimc_runtime_resume(struct device *dev)
  1137. {
  1138. struct fimc_context *ctx = get_fimc_context(dev);
  1139. DRM_DEBUG_KMS("id[%d]\n", ctx->id);
  1140. return clk_prepare_enable(ctx->clocks[FIMC_CLK_GATE]);
  1141. }
  1142. #endif
  1143. static const struct dev_pm_ops fimc_pm_ops = {
  1144. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1145. pm_runtime_force_resume)
  1146. SET_RUNTIME_PM_OPS(fimc_runtime_suspend, fimc_runtime_resume, NULL)
  1147. };
  1148. static const struct of_device_id fimc_of_match[] = {
  1149. { .compatible = "samsung,exynos4210-fimc" },
  1150. { .compatible = "samsung,exynos4212-fimc" },
  1151. { },
  1152. };
  1153. MODULE_DEVICE_TABLE(of, fimc_of_match);
  1154. struct platform_driver fimc_driver = {
  1155. .probe = fimc_probe,
  1156. .remove = fimc_remove,
  1157. .driver = {
  1158. .of_match_table = fimc_of_match,
  1159. .name = "exynos-drm-fimc",
  1160. .owner = THIS_MODULE,
  1161. .pm = &fimc_pm_ops,
  1162. },
  1163. };