fimc-reg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Register interface file for Samsung Camera Interface (FIMC) driver
  4. *
  5. * Copyright (C) 2010 - 2013 Samsung Electronics Co., Ltd.
  6. * Sylwester Nawrocki <s.nawrocki@samsung.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/regmap.h>
  11. #include <media/drv-intf/exynos-fimc.h>
  12. #include "media-dev.h"
  13. #include "fimc-reg.h"
  14. #include "fimc-core.h"
  15. void fimc_hw_reset(struct fimc_dev *dev)
  16. {
  17. u32 cfg;
  18. cfg = readl(dev->regs + FIMC_REG_CISRCFMT);
  19. cfg |= FIMC_REG_CISRCFMT_ITU601_8BIT;
  20. writel(cfg, dev->regs + FIMC_REG_CISRCFMT);
  21. /* Software reset. */
  22. cfg = readl(dev->regs + FIMC_REG_CIGCTRL);
  23. cfg |= (FIMC_REG_CIGCTRL_SWRST | FIMC_REG_CIGCTRL_IRQ_LEVEL);
  24. writel(cfg, dev->regs + FIMC_REG_CIGCTRL);
  25. udelay(10);
  26. cfg = readl(dev->regs + FIMC_REG_CIGCTRL);
  27. cfg &= ~FIMC_REG_CIGCTRL_SWRST;
  28. writel(cfg, dev->regs + FIMC_REG_CIGCTRL);
  29. if (dev->drv_data->out_buf_count > 4)
  30. fimc_hw_set_dma_seq(dev, 0xF);
  31. }
  32. static u32 fimc_hw_get_in_flip(struct fimc_ctx *ctx)
  33. {
  34. u32 flip = FIMC_REG_MSCTRL_FLIP_NORMAL;
  35. if (ctx->hflip)
  36. flip = FIMC_REG_MSCTRL_FLIP_Y_MIRROR;
  37. if (ctx->vflip)
  38. flip = FIMC_REG_MSCTRL_FLIP_X_MIRROR;
  39. if (ctx->rotation <= 90)
  40. return flip;
  41. return (flip ^ FIMC_REG_MSCTRL_FLIP_180) & FIMC_REG_MSCTRL_FLIP_180;
  42. }
  43. static u32 fimc_hw_get_target_flip(struct fimc_ctx *ctx)
  44. {
  45. u32 flip = FIMC_REG_CITRGFMT_FLIP_NORMAL;
  46. if (ctx->hflip)
  47. flip |= FIMC_REG_CITRGFMT_FLIP_Y_MIRROR;
  48. if (ctx->vflip)
  49. flip |= FIMC_REG_CITRGFMT_FLIP_X_MIRROR;
  50. if (ctx->rotation <= 90)
  51. return flip;
  52. return (flip ^ FIMC_REG_CITRGFMT_FLIP_180) & FIMC_REG_CITRGFMT_FLIP_180;
  53. }
  54. void fimc_hw_set_rotation(struct fimc_ctx *ctx)
  55. {
  56. u32 cfg, flip;
  57. struct fimc_dev *dev = ctx->fimc_dev;
  58. cfg = readl(dev->regs + FIMC_REG_CITRGFMT);
  59. cfg &= ~(FIMC_REG_CITRGFMT_INROT90 | FIMC_REG_CITRGFMT_OUTROT90 |
  60. FIMC_REG_CITRGFMT_FLIP_180);
  61. /*
  62. * The input and output rotator cannot work simultaneously.
  63. * Use the output rotator in output DMA mode or the input rotator
  64. * in direct fifo output mode.
  65. */
  66. if (ctx->rotation == 90 || ctx->rotation == 270) {
  67. if (ctx->out_path == FIMC_IO_LCDFIFO)
  68. cfg |= FIMC_REG_CITRGFMT_INROT90;
  69. else
  70. cfg |= FIMC_REG_CITRGFMT_OUTROT90;
  71. }
  72. if (ctx->out_path == FIMC_IO_DMA) {
  73. cfg |= fimc_hw_get_target_flip(ctx);
  74. writel(cfg, dev->regs + FIMC_REG_CITRGFMT);
  75. } else {
  76. /* LCD FIFO path */
  77. flip = readl(dev->regs + FIMC_REG_MSCTRL);
  78. flip &= ~FIMC_REG_MSCTRL_FLIP_MASK;
  79. flip |= fimc_hw_get_in_flip(ctx);
  80. writel(flip, dev->regs + FIMC_REG_MSCTRL);
  81. }
  82. }
  83. void fimc_hw_set_target_format(struct fimc_ctx *ctx)
  84. {
  85. u32 cfg;
  86. struct fimc_dev *dev = ctx->fimc_dev;
  87. struct fimc_frame *frame = &ctx->d_frame;
  88. dbg("w= %d, h= %d color: %d", frame->width,
  89. frame->height, frame->fmt->color);
  90. cfg = readl(dev->regs + FIMC_REG_CITRGFMT);
  91. cfg &= ~(FIMC_REG_CITRGFMT_FMT_MASK | FIMC_REG_CITRGFMT_HSIZE_MASK |
  92. FIMC_REG_CITRGFMT_VSIZE_MASK);
  93. switch (frame->fmt->color) {
  94. case FIMC_FMT_RGB444...FIMC_FMT_RGB888:
  95. cfg |= FIMC_REG_CITRGFMT_RGB;
  96. break;
  97. case FIMC_FMT_YCBCR420:
  98. cfg |= FIMC_REG_CITRGFMT_YCBCR420;
  99. break;
  100. case FIMC_FMT_YCBYCR422...FIMC_FMT_CRYCBY422:
  101. if (frame->fmt->colplanes == 1)
  102. cfg |= FIMC_REG_CITRGFMT_YCBCR422_1P;
  103. else
  104. cfg |= FIMC_REG_CITRGFMT_YCBCR422;
  105. break;
  106. default:
  107. break;
  108. }
  109. if (ctx->rotation == 90 || ctx->rotation == 270)
  110. cfg |= (frame->height << 16) | frame->width;
  111. else
  112. cfg |= (frame->width << 16) | frame->height;
  113. writel(cfg, dev->regs + FIMC_REG_CITRGFMT);
  114. cfg = readl(dev->regs + FIMC_REG_CITAREA);
  115. cfg &= ~FIMC_REG_CITAREA_MASK;
  116. cfg |= (frame->width * frame->height);
  117. writel(cfg, dev->regs + FIMC_REG_CITAREA);
  118. }
  119. static void fimc_hw_set_out_dma_size(struct fimc_ctx *ctx)
  120. {
  121. struct fimc_dev *dev = ctx->fimc_dev;
  122. struct fimc_frame *frame = &ctx->d_frame;
  123. u32 cfg;
  124. cfg = (frame->f_height << 16) | frame->f_width;
  125. writel(cfg, dev->regs + FIMC_REG_ORGOSIZE);
  126. /* Select color space conversion equation (HD/SD size).*/
  127. cfg = readl(dev->regs + FIMC_REG_CIGCTRL);
  128. if (frame->f_width >= 1280) /* HD */
  129. cfg |= FIMC_REG_CIGCTRL_CSC_ITU601_709;
  130. else /* SD */
  131. cfg &= ~FIMC_REG_CIGCTRL_CSC_ITU601_709;
  132. writel(cfg, dev->regs + FIMC_REG_CIGCTRL);
  133. }
  134. void fimc_hw_set_out_dma(struct fimc_ctx *ctx)
  135. {
  136. struct fimc_dev *dev = ctx->fimc_dev;
  137. struct fimc_frame *frame = &ctx->d_frame;
  138. struct fimc_dma_offset *offset = &frame->dma_offset;
  139. struct fimc_fmt *fmt = frame->fmt;
  140. u32 cfg;
  141. /* Set the input dma offsets. */
  142. cfg = (offset->y_v << 16) | offset->y_h;
  143. writel(cfg, dev->regs + FIMC_REG_CIOYOFF);
  144. cfg = (offset->cb_v << 16) | offset->cb_h;
  145. writel(cfg, dev->regs + FIMC_REG_CIOCBOFF);
  146. cfg = (offset->cr_v << 16) | offset->cr_h;
  147. writel(cfg, dev->regs + FIMC_REG_CIOCROFF);
  148. fimc_hw_set_out_dma_size(ctx);
  149. /* Configure chroma components order. */
  150. cfg = readl(dev->regs + FIMC_REG_CIOCTRL);
  151. cfg &= ~(FIMC_REG_CIOCTRL_ORDER2P_MASK |
  152. FIMC_REG_CIOCTRL_ORDER422_MASK |
  153. FIMC_REG_CIOCTRL_YCBCR_PLANE_MASK |
  154. FIMC_REG_CIOCTRL_RGB16FMT_MASK);
  155. if (fmt->colplanes == 1)
  156. cfg |= ctx->out_order_1p;
  157. else if (fmt->colplanes == 2)
  158. cfg |= ctx->out_order_2p | FIMC_REG_CIOCTRL_YCBCR_2PLANE;
  159. else if (fmt->colplanes == 3)
  160. cfg |= FIMC_REG_CIOCTRL_YCBCR_3PLANE;
  161. if (fmt->color == FIMC_FMT_RGB565)
  162. cfg |= FIMC_REG_CIOCTRL_RGB565;
  163. else if (fmt->color == FIMC_FMT_RGB555)
  164. cfg |= FIMC_REG_CIOCTRL_ARGB1555;
  165. else if (fmt->color == FIMC_FMT_RGB444)
  166. cfg |= FIMC_REG_CIOCTRL_ARGB4444;
  167. writel(cfg, dev->regs + FIMC_REG_CIOCTRL);
  168. }
  169. static void fimc_hw_en_autoload(struct fimc_dev *dev, int enable)
  170. {
  171. u32 cfg = readl(dev->regs + FIMC_REG_ORGISIZE);
  172. if (enable)
  173. cfg |= FIMC_REG_CIREAL_ISIZE_AUTOLOAD_EN;
  174. else
  175. cfg &= ~FIMC_REG_CIREAL_ISIZE_AUTOLOAD_EN;
  176. writel(cfg, dev->regs + FIMC_REG_ORGISIZE);
  177. }
  178. void fimc_hw_en_lastirq(struct fimc_dev *dev, int enable)
  179. {
  180. u32 cfg = readl(dev->regs + FIMC_REG_CIOCTRL);
  181. if (enable)
  182. cfg |= FIMC_REG_CIOCTRL_LASTIRQ_ENABLE;
  183. else
  184. cfg &= ~FIMC_REG_CIOCTRL_LASTIRQ_ENABLE;
  185. writel(cfg, dev->regs + FIMC_REG_CIOCTRL);
  186. }
  187. void fimc_hw_set_prescaler(struct fimc_ctx *ctx)
  188. {
  189. struct fimc_dev *dev = ctx->fimc_dev;
  190. struct fimc_scaler *sc = &ctx->scaler;
  191. u32 cfg, shfactor;
  192. shfactor = 10 - (sc->hfactor + sc->vfactor);
  193. cfg = shfactor << 28;
  194. cfg |= (sc->pre_hratio << 16) | sc->pre_vratio;
  195. writel(cfg, dev->regs + FIMC_REG_CISCPRERATIO);
  196. cfg = (sc->pre_dst_width << 16) | sc->pre_dst_height;
  197. writel(cfg, dev->regs + FIMC_REG_CISCPREDST);
  198. }
  199. static void fimc_hw_set_scaler(struct fimc_ctx *ctx)
  200. {
  201. struct fimc_dev *dev = ctx->fimc_dev;
  202. struct fimc_scaler *sc = &ctx->scaler;
  203. struct fimc_frame *src_frame = &ctx->s_frame;
  204. struct fimc_frame *dst_frame = &ctx->d_frame;
  205. u32 cfg = readl(dev->regs + FIMC_REG_CISCCTRL);
  206. cfg &= ~(FIMC_REG_CISCCTRL_CSCR2Y_WIDE | FIMC_REG_CISCCTRL_CSCY2R_WIDE |
  207. FIMC_REG_CISCCTRL_SCALEUP_H | FIMC_REG_CISCCTRL_SCALEUP_V |
  208. FIMC_REG_CISCCTRL_SCALERBYPASS | FIMC_REG_CISCCTRL_ONE2ONE |
  209. FIMC_REG_CISCCTRL_INRGB_FMT_MASK | FIMC_REG_CISCCTRL_OUTRGB_FMT_MASK |
  210. FIMC_REG_CISCCTRL_INTERLACE | FIMC_REG_CISCCTRL_RGB_EXT);
  211. if (!(ctx->flags & FIMC_COLOR_RANGE_NARROW))
  212. cfg |= (FIMC_REG_CISCCTRL_CSCR2Y_WIDE |
  213. FIMC_REG_CISCCTRL_CSCY2R_WIDE);
  214. if (!sc->enabled)
  215. cfg |= FIMC_REG_CISCCTRL_SCALERBYPASS;
  216. if (sc->scaleup_h)
  217. cfg |= FIMC_REG_CISCCTRL_SCALEUP_H;
  218. if (sc->scaleup_v)
  219. cfg |= FIMC_REG_CISCCTRL_SCALEUP_V;
  220. if (sc->copy_mode)
  221. cfg |= FIMC_REG_CISCCTRL_ONE2ONE;
  222. if (ctx->in_path == FIMC_IO_DMA) {
  223. switch (src_frame->fmt->color) {
  224. case FIMC_FMT_RGB565:
  225. cfg |= FIMC_REG_CISCCTRL_INRGB_FMT_RGB565;
  226. break;
  227. case FIMC_FMT_RGB666:
  228. cfg |= FIMC_REG_CISCCTRL_INRGB_FMT_RGB666;
  229. break;
  230. case FIMC_FMT_RGB888:
  231. cfg |= FIMC_REG_CISCCTRL_INRGB_FMT_RGB888;
  232. break;
  233. }
  234. }
  235. if (ctx->out_path == FIMC_IO_DMA) {
  236. u32 color = dst_frame->fmt->color;
  237. if (color >= FIMC_FMT_RGB444 && color <= FIMC_FMT_RGB565)
  238. cfg |= FIMC_REG_CISCCTRL_OUTRGB_FMT_RGB565;
  239. else if (color == FIMC_FMT_RGB666)
  240. cfg |= FIMC_REG_CISCCTRL_OUTRGB_FMT_RGB666;
  241. else if (color == FIMC_FMT_RGB888)
  242. cfg |= FIMC_REG_CISCCTRL_OUTRGB_FMT_RGB888;
  243. } else {
  244. cfg |= FIMC_REG_CISCCTRL_OUTRGB_FMT_RGB888;
  245. if (ctx->flags & FIMC_SCAN_MODE_INTERLACED)
  246. cfg |= FIMC_REG_CISCCTRL_INTERLACE;
  247. }
  248. writel(cfg, dev->regs + FIMC_REG_CISCCTRL);
  249. }
  250. void fimc_hw_set_mainscaler(struct fimc_ctx *ctx)
  251. {
  252. struct fimc_dev *dev = ctx->fimc_dev;
  253. const struct fimc_variant *variant = dev->variant;
  254. struct fimc_scaler *sc = &ctx->scaler;
  255. u32 cfg;
  256. dbg("main_hratio= 0x%X main_vratio= 0x%X",
  257. sc->main_hratio, sc->main_vratio);
  258. fimc_hw_set_scaler(ctx);
  259. cfg = readl(dev->regs + FIMC_REG_CISCCTRL);
  260. cfg &= ~(FIMC_REG_CISCCTRL_MHRATIO_MASK |
  261. FIMC_REG_CISCCTRL_MVRATIO_MASK);
  262. if (variant->has_mainscaler_ext) {
  263. cfg |= FIMC_REG_CISCCTRL_MHRATIO_EXT(sc->main_hratio);
  264. cfg |= FIMC_REG_CISCCTRL_MVRATIO_EXT(sc->main_vratio);
  265. writel(cfg, dev->regs + FIMC_REG_CISCCTRL);
  266. cfg = readl(dev->regs + FIMC_REG_CIEXTEN);
  267. cfg &= ~(FIMC_REG_CIEXTEN_MVRATIO_EXT_MASK |
  268. FIMC_REG_CIEXTEN_MHRATIO_EXT_MASK);
  269. cfg |= FIMC_REG_CIEXTEN_MHRATIO_EXT(sc->main_hratio);
  270. cfg |= FIMC_REG_CIEXTEN_MVRATIO_EXT(sc->main_vratio);
  271. writel(cfg, dev->regs + FIMC_REG_CIEXTEN);
  272. } else {
  273. cfg |= FIMC_REG_CISCCTRL_MHRATIO(sc->main_hratio);
  274. cfg |= FIMC_REG_CISCCTRL_MVRATIO(sc->main_vratio);
  275. writel(cfg, dev->regs + FIMC_REG_CISCCTRL);
  276. }
  277. }
  278. void fimc_hw_enable_capture(struct fimc_ctx *ctx)
  279. {
  280. struct fimc_dev *dev = ctx->fimc_dev;
  281. u32 cfg;
  282. cfg = readl(dev->regs + FIMC_REG_CIIMGCPT);
  283. cfg |= FIMC_REG_CIIMGCPT_CPT_FREN_ENABLE;
  284. if (ctx->scaler.enabled)
  285. cfg |= FIMC_REG_CIIMGCPT_IMGCPTEN_SC;
  286. else
  287. cfg &= FIMC_REG_CIIMGCPT_IMGCPTEN_SC;
  288. cfg |= FIMC_REG_CIIMGCPT_IMGCPTEN;
  289. writel(cfg, dev->regs + FIMC_REG_CIIMGCPT);
  290. }
  291. void fimc_hw_disable_capture(struct fimc_dev *dev)
  292. {
  293. u32 cfg = readl(dev->regs + FIMC_REG_CIIMGCPT);
  294. cfg &= ~(FIMC_REG_CIIMGCPT_IMGCPTEN |
  295. FIMC_REG_CIIMGCPT_IMGCPTEN_SC);
  296. writel(cfg, dev->regs + FIMC_REG_CIIMGCPT);
  297. }
  298. void fimc_hw_set_effect(struct fimc_ctx *ctx)
  299. {
  300. struct fimc_dev *dev = ctx->fimc_dev;
  301. struct fimc_effect *effect = &ctx->effect;
  302. u32 cfg = 0;
  303. if (effect->type != FIMC_REG_CIIMGEFF_FIN_BYPASS) {
  304. cfg |= FIMC_REG_CIIMGEFF_IE_SC_AFTER |
  305. FIMC_REG_CIIMGEFF_IE_ENABLE;
  306. cfg |= effect->type;
  307. if (effect->type == FIMC_REG_CIIMGEFF_FIN_ARBITRARY)
  308. cfg |= (effect->pat_cb << 13) | effect->pat_cr;
  309. }
  310. writel(cfg, dev->regs + FIMC_REG_CIIMGEFF);
  311. }
  312. void fimc_hw_set_rgb_alpha(struct fimc_ctx *ctx)
  313. {
  314. struct fimc_dev *dev = ctx->fimc_dev;
  315. struct fimc_frame *frame = &ctx->d_frame;
  316. u32 cfg;
  317. if (!(frame->fmt->flags & FMT_HAS_ALPHA))
  318. return;
  319. cfg = readl(dev->regs + FIMC_REG_CIOCTRL);
  320. cfg &= ~FIMC_REG_CIOCTRL_ALPHA_OUT_MASK;
  321. cfg |= (frame->alpha << 4);
  322. writel(cfg, dev->regs + FIMC_REG_CIOCTRL);
  323. }
  324. static void fimc_hw_set_in_dma_size(struct fimc_ctx *ctx)
  325. {
  326. struct fimc_dev *dev = ctx->fimc_dev;
  327. struct fimc_frame *frame = &ctx->s_frame;
  328. u32 cfg_o = 0;
  329. u32 cfg_r = 0;
  330. if (FIMC_IO_LCDFIFO == ctx->out_path)
  331. cfg_r |= FIMC_REG_CIREAL_ISIZE_AUTOLOAD_EN;
  332. cfg_o |= (frame->f_height << 16) | frame->f_width;
  333. cfg_r |= (frame->height << 16) | frame->width;
  334. writel(cfg_o, dev->regs + FIMC_REG_ORGISIZE);
  335. writel(cfg_r, dev->regs + FIMC_REG_CIREAL_ISIZE);
  336. }
  337. void fimc_hw_set_in_dma(struct fimc_ctx *ctx)
  338. {
  339. struct fimc_dev *dev = ctx->fimc_dev;
  340. struct fimc_frame *frame = &ctx->s_frame;
  341. struct fimc_dma_offset *offset = &frame->dma_offset;
  342. u32 cfg;
  343. /* Set the pixel offsets. */
  344. cfg = (offset->y_v << 16) | offset->y_h;
  345. writel(cfg, dev->regs + FIMC_REG_CIIYOFF);
  346. cfg = (offset->cb_v << 16) | offset->cb_h;
  347. writel(cfg, dev->regs + FIMC_REG_CIICBOFF);
  348. cfg = (offset->cr_v << 16) | offset->cr_h;
  349. writel(cfg, dev->regs + FIMC_REG_CIICROFF);
  350. /* Input original and real size. */
  351. fimc_hw_set_in_dma_size(ctx);
  352. /* Use DMA autoload only in FIFO mode. */
  353. fimc_hw_en_autoload(dev, ctx->out_path == FIMC_IO_LCDFIFO);
  354. /* Set the input DMA to process single frame only. */
  355. cfg = readl(dev->regs + FIMC_REG_MSCTRL);
  356. cfg &= ~(FIMC_REG_MSCTRL_INFORMAT_MASK
  357. | FIMC_REG_MSCTRL_IN_BURST_COUNT_MASK
  358. | FIMC_REG_MSCTRL_INPUT_MASK
  359. | FIMC_REG_MSCTRL_C_INT_IN_MASK
  360. | FIMC_REG_MSCTRL_2P_IN_ORDER_MASK
  361. | FIMC_REG_MSCTRL_ORDER422_MASK);
  362. cfg |= (FIMC_REG_MSCTRL_IN_BURST_COUNT(4)
  363. | FIMC_REG_MSCTRL_INPUT_MEMORY
  364. | FIMC_REG_MSCTRL_FIFO_CTRL_FULL);
  365. switch (frame->fmt->color) {
  366. case FIMC_FMT_RGB565...FIMC_FMT_RGB888:
  367. cfg |= FIMC_REG_MSCTRL_INFORMAT_RGB;
  368. break;
  369. case FIMC_FMT_YCBCR420:
  370. cfg |= FIMC_REG_MSCTRL_INFORMAT_YCBCR420;
  371. if (frame->fmt->colplanes == 2)
  372. cfg |= ctx->in_order_2p | FIMC_REG_MSCTRL_C_INT_IN_2PLANE;
  373. else
  374. cfg |= FIMC_REG_MSCTRL_C_INT_IN_3PLANE;
  375. break;
  376. case FIMC_FMT_YCBYCR422...FIMC_FMT_CRYCBY422:
  377. if (frame->fmt->colplanes == 1) {
  378. cfg |= ctx->in_order_1p
  379. | FIMC_REG_MSCTRL_INFORMAT_YCBCR422_1P;
  380. } else {
  381. cfg |= FIMC_REG_MSCTRL_INFORMAT_YCBCR422;
  382. if (frame->fmt->colplanes == 2)
  383. cfg |= ctx->in_order_2p
  384. | FIMC_REG_MSCTRL_C_INT_IN_2PLANE;
  385. else
  386. cfg |= FIMC_REG_MSCTRL_C_INT_IN_3PLANE;
  387. }
  388. break;
  389. default:
  390. break;
  391. }
  392. writel(cfg, dev->regs + FIMC_REG_MSCTRL);
  393. /* Input/output DMA linear/tiled mode. */
  394. cfg = readl(dev->regs + FIMC_REG_CIDMAPARAM);
  395. cfg &= ~FIMC_REG_CIDMAPARAM_TILE_MASK;
  396. if (tiled_fmt(ctx->s_frame.fmt))
  397. cfg |= FIMC_REG_CIDMAPARAM_R_64X32;
  398. if (tiled_fmt(ctx->d_frame.fmt))
  399. cfg |= FIMC_REG_CIDMAPARAM_W_64X32;
  400. writel(cfg, dev->regs + FIMC_REG_CIDMAPARAM);
  401. }
  402. void fimc_hw_set_input_path(struct fimc_ctx *ctx)
  403. {
  404. struct fimc_dev *dev = ctx->fimc_dev;
  405. u32 cfg = readl(dev->regs + FIMC_REG_MSCTRL);
  406. cfg &= ~FIMC_REG_MSCTRL_INPUT_MASK;
  407. if (ctx->in_path == FIMC_IO_DMA)
  408. cfg |= FIMC_REG_MSCTRL_INPUT_MEMORY;
  409. else
  410. cfg |= FIMC_REG_MSCTRL_INPUT_EXTCAM;
  411. writel(cfg, dev->regs + FIMC_REG_MSCTRL);
  412. }
  413. void fimc_hw_set_output_path(struct fimc_ctx *ctx)
  414. {
  415. struct fimc_dev *dev = ctx->fimc_dev;
  416. u32 cfg = readl(dev->regs + FIMC_REG_CISCCTRL);
  417. cfg &= ~FIMC_REG_CISCCTRL_LCDPATHEN_FIFO;
  418. if (ctx->out_path == FIMC_IO_LCDFIFO)
  419. cfg |= FIMC_REG_CISCCTRL_LCDPATHEN_FIFO;
  420. writel(cfg, dev->regs + FIMC_REG_CISCCTRL);
  421. }
  422. void fimc_hw_set_input_addr(struct fimc_dev *dev, struct fimc_addr *paddr)
  423. {
  424. u32 cfg = readl(dev->regs + FIMC_REG_CIREAL_ISIZE);
  425. cfg |= FIMC_REG_CIREAL_ISIZE_ADDR_CH_DIS;
  426. writel(cfg, dev->regs + FIMC_REG_CIREAL_ISIZE);
  427. writel(paddr->y, dev->regs + FIMC_REG_CIIYSA(0));
  428. writel(paddr->cb, dev->regs + FIMC_REG_CIICBSA(0));
  429. writel(paddr->cr, dev->regs + FIMC_REG_CIICRSA(0));
  430. cfg &= ~FIMC_REG_CIREAL_ISIZE_ADDR_CH_DIS;
  431. writel(cfg, dev->regs + FIMC_REG_CIREAL_ISIZE);
  432. }
  433. void fimc_hw_set_output_addr(struct fimc_dev *dev,
  434. struct fimc_addr *paddr, int index)
  435. {
  436. int i = (index == -1) ? 0 : index;
  437. do {
  438. writel(paddr->y, dev->regs + FIMC_REG_CIOYSA(i));
  439. writel(paddr->cb, dev->regs + FIMC_REG_CIOCBSA(i));
  440. writel(paddr->cr, dev->regs + FIMC_REG_CIOCRSA(i));
  441. dbg("dst_buf[%d]: 0x%X, cb: 0x%X, cr: 0x%X",
  442. i, paddr->y, paddr->cb, paddr->cr);
  443. } while (index == -1 && ++i < FIMC_MAX_OUT_BUFS);
  444. }
  445. int fimc_hw_set_camera_polarity(struct fimc_dev *fimc,
  446. struct fimc_source_info *cam)
  447. {
  448. u32 cfg = readl(fimc->regs + FIMC_REG_CIGCTRL);
  449. cfg &= ~(FIMC_REG_CIGCTRL_INVPOLPCLK | FIMC_REG_CIGCTRL_INVPOLVSYNC |
  450. FIMC_REG_CIGCTRL_INVPOLHREF | FIMC_REG_CIGCTRL_INVPOLHSYNC |
  451. FIMC_REG_CIGCTRL_INVPOLFIELD);
  452. if (cam->flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  453. cfg |= FIMC_REG_CIGCTRL_INVPOLPCLK;
  454. if (cam->flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  455. cfg |= FIMC_REG_CIGCTRL_INVPOLVSYNC;
  456. if (cam->flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  457. cfg |= FIMC_REG_CIGCTRL_INVPOLHREF;
  458. if (cam->flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  459. cfg |= FIMC_REG_CIGCTRL_INVPOLHSYNC;
  460. if (cam->flags & V4L2_MBUS_FIELD_EVEN_LOW)
  461. cfg |= FIMC_REG_CIGCTRL_INVPOLFIELD;
  462. writel(cfg, fimc->regs + FIMC_REG_CIGCTRL);
  463. return 0;
  464. }
  465. struct mbus_pixfmt_desc {
  466. u32 pixelcode;
  467. u32 cisrcfmt;
  468. u16 bus_width;
  469. };
  470. static const struct mbus_pixfmt_desc pix_desc[] = {
  471. { MEDIA_BUS_FMT_YUYV8_2X8, FIMC_REG_CISRCFMT_ORDER422_YCBYCR, 8 },
  472. { MEDIA_BUS_FMT_YVYU8_2X8, FIMC_REG_CISRCFMT_ORDER422_YCRYCB, 8 },
  473. { MEDIA_BUS_FMT_VYUY8_2X8, FIMC_REG_CISRCFMT_ORDER422_CRYCBY, 8 },
  474. { MEDIA_BUS_FMT_UYVY8_2X8, FIMC_REG_CISRCFMT_ORDER422_CBYCRY, 8 },
  475. };
  476. int fimc_hw_set_camera_source(struct fimc_dev *fimc,
  477. struct fimc_source_info *source)
  478. {
  479. struct fimc_vid_cap *vc = &fimc->vid_cap;
  480. struct fimc_frame *f = &vc->ctx->s_frame;
  481. u32 bus_width, cfg = 0;
  482. int i;
  483. switch (source->fimc_bus_type) {
  484. case FIMC_BUS_TYPE_ITU_601:
  485. case FIMC_BUS_TYPE_ITU_656:
  486. for (i = 0; i < ARRAY_SIZE(pix_desc); i++) {
  487. if (vc->ci_fmt.code == pix_desc[i].pixelcode) {
  488. cfg = pix_desc[i].cisrcfmt;
  489. bus_width = pix_desc[i].bus_width;
  490. break;
  491. }
  492. }
  493. if (i == ARRAY_SIZE(pix_desc)) {
  494. v4l2_err(&vc->ve.vdev,
  495. "Camera color format not supported: %d\n",
  496. vc->ci_fmt.code);
  497. return -EINVAL;
  498. }
  499. if (source->fimc_bus_type == FIMC_BUS_TYPE_ITU_601) {
  500. if (bus_width == 8)
  501. cfg |= FIMC_REG_CISRCFMT_ITU601_8BIT;
  502. else if (bus_width == 16)
  503. cfg |= FIMC_REG_CISRCFMT_ITU601_16BIT;
  504. } /* else defaults to ITU-R BT.656 8-bit */
  505. break;
  506. case FIMC_BUS_TYPE_MIPI_CSI2:
  507. if (fimc_fmt_is_user_defined(f->fmt->color))
  508. cfg |= FIMC_REG_CISRCFMT_ITU601_8BIT;
  509. break;
  510. default:
  511. case FIMC_BUS_TYPE_ISP_WRITEBACK:
  512. /* Anything to do here ? */
  513. break;
  514. }
  515. cfg |= (f->o_width << 16) | f->o_height;
  516. writel(cfg, fimc->regs + FIMC_REG_CISRCFMT);
  517. return 0;
  518. }
  519. void fimc_hw_set_camera_offset(struct fimc_dev *fimc, struct fimc_frame *f)
  520. {
  521. u32 hoff2, voff2;
  522. u32 cfg = readl(fimc->regs + FIMC_REG_CIWDOFST);
  523. cfg &= ~(FIMC_REG_CIWDOFST_HOROFF_MASK | FIMC_REG_CIWDOFST_VEROFF_MASK);
  524. cfg |= FIMC_REG_CIWDOFST_OFF_EN |
  525. (f->offs_h << 16) | f->offs_v;
  526. writel(cfg, fimc->regs + FIMC_REG_CIWDOFST);
  527. /* See CIWDOFSTn register description in the datasheet for details. */
  528. hoff2 = f->o_width - f->width - f->offs_h;
  529. voff2 = f->o_height - f->height - f->offs_v;
  530. cfg = (hoff2 << 16) | voff2;
  531. writel(cfg, fimc->regs + FIMC_REG_CIWDOFST2);
  532. }
  533. int fimc_hw_set_camera_type(struct fimc_dev *fimc,
  534. struct fimc_source_info *source)
  535. {
  536. struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
  537. u32 csis_data_alignment = 32;
  538. u32 cfg, tmp;
  539. cfg = readl(fimc->regs + FIMC_REG_CIGCTRL);
  540. /* Select ITU B interface, disable Writeback path and test pattern. */
  541. cfg &= ~(FIMC_REG_CIGCTRL_TESTPAT_MASK | FIMC_REG_CIGCTRL_SELCAM_ITU_A |
  542. FIMC_REG_CIGCTRL_SELCAM_MIPI | FIMC_REG_CIGCTRL_CAMIF_SELWB |
  543. FIMC_REG_CIGCTRL_SELCAM_MIPI_A | FIMC_REG_CIGCTRL_CAM_JPEG |
  544. FIMC_REG_CIGCTRL_SELWB_A);
  545. switch (source->fimc_bus_type) {
  546. case FIMC_BUS_TYPE_MIPI_CSI2:
  547. cfg |= FIMC_REG_CIGCTRL_SELCAM_MIPI;
  548. if (source->mux_id == 0)
  549. cfg |= FIMC_REG_CIGCTRL_SELCAM_MIPI_A;
  550. /* TODO: add remaining supported formats. */
  551. switch (vid_cap->ci_fmt.code) {
  552. case MEDIA_BUS_FMT_VYUY8_2X8:
  553. tmp = FIMC_REG_CSIIMGFMT_YCBCR422_8BIT;
  554. break;
  555. case MEDIA_BUS_FMT_JPEG_1X8:
  556. case MEDIA_BUS_FMT_S5C_UYVY_JPEG_1X8:
  557. tmp = FIMC_REG_CSIIMGFMT_USER(1);
  558. cfg |= FIMC_REG_CIGCTRL_CAM_JPEG;
  559. break;
  560. default:
  561. v4l2_err(&vid_cap->ve.vdev,
  562. "Not supported camera pixel format: %#x\n",
  563. vid_cap->ci_fmt.code);
  564. return -EINVAL;
  565. }
  566. tmp |= (csis_data_alignment == 32) << 8;
  567. writel(tmp, fimc->regs + FIMC_REG_CSIIMGFMT);
  568. break;
  569. case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
  570. if (source->mux_id == 0) /* ITU-A, ITU-B: 0, 1 */
  571. cfg |= FIMC_REG_CIGCTRL_SELCAM_ITU_A;
  572. break;
  573. case FIMC_BUS_TYPE_LCD_WRITEBACK_A:
  574. cfg |= FIMC_REG_CIGCTRL_CAMIF_SELWB;
  575. /* fall through */
  576. case FIMC_BUS_TYPE_ISP_WRITEBACK:
  577. if (fimc->variant->has_isp_wb)
  578. cfg |= FIMC_REG_CIGCTRL_CAMIF_SELWB;
  579. else
  580. WARN_ONCE(1, "ISP Writeback input is not supported\n");
  581. break;
  582. default:
  583. v4l2_err(&vid_cap->ve.vdev,
  584. "Invalid FIMC bus type selected: %d\n",
  585. source->fimc_bus_type);
  586. return -EINVAL;
  587. }
  588. writel(cfg, fimc->regs + FIMC_REG_CIGCTRL);
  589. return 0;
  590. }
  591. void fimc_hw_clear_irq(struct fimc_dev *dev)
  592. {
  593. u32 cfg = readl(dev->regs + FIMC_REG_CIGCTRL);
  594. cfg |= FIMC_REG_CIGCTRL_IRQ_CLR;
  595. writel(cfg, dev->regs + FIMC_REG_CIGCTRL);
  596. }
  597. void fimc_hw_enable_scaler(struct fimc_dev *dev, bool on)
  598. {
  599. u32 cfg = readl(dev->regs + FIMC_REG_CISCCTRL);
  600. if (on)
  601. cfg |= FIMC_REG_CISCCTRL_SCALERSTART;
  602. else
  603. cfg &= ~FIMC_REG_CISCCTRL_SCALERSTART;
  604. writel(cfg, dev->regs + FIMC_REG_CISCCTRL);
  605. }
  606. void fimc_hw_activate_input_dma(struct fimc_dev *dev, bool on)
  607. {
  608. u32 cfg = readl(dev->regs + FIMC_REG_MSCTRL);
  609. if (on)
  610. cfg |= FIMC_REG_MSCTRL_ENVID;
  611. else
  612. cfg &= ~FIMC_REG_MSCTRL_ENVID;
  613. writel(cfg, dev->regs + FIMC_REG_MSCTRL);
  614. }
  615. /* Return an index to the buffer actually being written. */
  616. s32 fimc_hw_get_frame_index(struct fimc_dev *dev)
  617. {
  618. s32 reg;
  619. if (dev->drv_data->cistatus2) {
  620. reg = readl(dev->regs + FIMC_REG_CISTATUS2) & 0x3f;
  621. return reg - 1;
  622. }
  623. reg = readl(dev->regs + FIMC_REG_CISTATUS);
  624. return (reg & FIMC_REG_CISTATUS_FRAMECNT_MASK) >>
  625. FIMC_REG_CISTATUS_FRAMECNT_SHIFT;
  626. }
  627. /* Return an index to the buffer being written previously. */
  628. s32 fimc_hw_get_prev_frame_index(struct fimc_dev *dev)
  629. {
  630. s32 reg;
  631. if (!dev->drv_data->cistatus2)
  632. return -1;
  633. reg = readl(dev->regs + FIMC_REG_CISTATUS2);
  634. return ((reg >> 7) & 0x3f) - 1;
  635. }
  636. /* Locking: the caller holds fimc->slock */
  637. void fimc_activate_capture(struct fimc_ctx *ctx)
  638. {
  639. fimc_hw_enable_scaler(ctx->fimc_dev, ctx->scaler.enabled);
  640. fimc_hw_enable_capture(ctx);
  641. }
  642. void fimc_deactivate_capture(struct fimc_dev *fimc)
  643. {
  644. fimc_hw_en_lastirq(fimc, true);
  645. fimc_hw_disable_capture(fimc);
  646. fimc_hw_enable_scaler(fimc, false);
  647. fimc_hw_en_lastirq(fimc, false);
  648. }
  649. int fimc_hw_camblk_cfg_writeback(struct fimc_dev *fimc)
  650. {
  651. struct regmap *map = fimc->sysreg;
  652. unsigned int mask, val, camblk_cfg;
  653. int ret;
  654. if (map == NULL)
  655. return 0;
  656. ret = regmap_read(map, SYSREG_CAMBLK, &camblk_cfg);
  657. if (ret < 0 || ((camblk_cfg & 0x00700000) >> 20 != 0x3))
  658. return ret;
  659. if (!WARN(fimc->id >= 3, "not supported id: %d\n", fimc->id))
  660. val = 0x1 << (fimc->id + 20);
  661. else
  662. val = 0;
  663. mask = SYSREG_CAMBLK_FIFORST_ISP | SYSREG_CAMBLK_ISPWB_FULL_EN;
  664. ret = regmap_update_bits(map, SYSREG_CAMBLK, mask, val);
  665. if (ret < 0)
  666. return ret;
  667. usleep_range(1000, 2000);
  668. val |= SYSREG_CAMBLK_FIFORST_ISP;
  669. ret = regmap_update_bits(map, SYSREG_CAMBLK, mask, val);
  670. if (ret < 0)
  671. return ret;
  672. mask = SYSREG_ISPBLK_FIFORST_CAM_BLK;
  673. ret = regmap_update_bits(map, SYSREG_ISPBLK, mask, ~mask);
  674. if (ret < 0)
  675. return ret;
  676. usleep_range(1000, 2000);
  677. return regmap_update_bits(map, SYSREG_ISPBLK, mask, mask);
  678. }