ipu-cpmem.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Copyright (C) 2012 Mentor Graphics Inc.
  3. * Copyright 2005-2012 Freescale Semiconductor, Inc. All Rights Reserved.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/types.h>
  13. #include <linux/bitrev.h>
  14. #include <linux/io.h>
  15. #include <drm/drm_fourcc.h>
  16. #include "ipu-prv.h"
  17. struct ipu_cpmem_word {
  18. u32 data[5];
  19. u32 res[3];
  20. };
  21. struct ipu_ch_param {
  22. struct ipu_cpmem_word word[2];
  23. };
  24. struct ipu_cpmem {
  25. struct ipu_ch_param __iomem *base;
  26. u32 module;
  27. spinlock_t lock;
  28. int use_count;
  29. struct ipu_soc *ipu;
  30. };
  31. #define IPU_CPMEM_WORD(word, ofs, size) ((((word) * 160 + (ofs)) << 8) | (size))
  32. #define IPU_FIELD_UBO IPU_CPMEM_WORD(0, 46, 22)
  33. #define IPU_FIELD_VBO IPU_CPMEM_WORD(0, 68, 22)
  34. #define IPU_FIELD_IOX IPU_CPMEM_WORD(0, 90, 4)
  35. #define IPU_FIELD_RDRW IPU_CPMEM_WORD(0, 94, 1)
  36. #define IPU_FIELD_SO IPU_CPMEM_WORD(0, 113, 1)
  37. #define IPU_FIELD_SLY IPU_CPMEM_WORD(1, 102, 14)
  38. #define IPU_FIELD_SLUV IPU_CPMEM_WORD(1, 128, 14)
  39. #define IPU_FIELD_XV IPU_CPMEM_WORD(0, 0, 10)
  40. #define IPU_FIELD_YV IPU_CPMEM_WORD(0, 10, 9)
  41. #define IPU_FIELD_XB IPU_CPMEM_WORD(0, 19, 13)
  42. #define IPU_FIELD_YB IPU_CPMEM_WORD(0, 32, 12)
  43. #define IPU_FIELD_NSB_B IPU_CPMEM_WORD(0, 44, 1)
  44. #define IPU_FIELD_CF IPU_CPMEM_WORD(0, 45, 1)
  45. #define IPU_FIELD_SX IPU_CPMEM_WORD(0, 46, 12)
  46. #define IPU_FIELD_SY IPU_CPMEM_WORD(0, 58, 11)
  47. #define IPU_FIELD_NS IPU_CPMEM_WORD(0, 69, 10)
  48. #define IPU_FIELD_SDX IPU_CPMEM_WORD(0, 79, 7)
  49. #define IPU_FIELD_SM IPU_CPMEM_WORD(0, 86, 10)
  50. #define IPU_FIELD_SCC IPU_CPMEM_WORD(0, 96, 1)
  51. #define IPU_FIELD_SCE IPU_CPMEM_WORD(0, 97, 1)
  52. #define IPU_FIELD_SDY IPU_CPMEM_WORD(0, 98, 7)
  53. #define IPU_FIELD_SDRX IPU_CPMEM_WORD(0, 105, 1)
  54. #define IPU_FIELD_SDRY IPU_CPMEM_WORD(0, 106, 1)
  55. #define IPU_FIELD_BPP IPU_CPMEM_WORD(0, 107, 3)
  56. #define IPU_FIELD_DEC_SEL IPU_CPMEM_WORD(0, 110, 2)
  57. #define IPU_FIELD_DIM IPU_CPMEM_WORD(0, 112, 1)
  58. #define IPU_FIELD_BNDM IPU_CPMEM_WORD(0, 114, 3)
  59. #define IPU_FIELD_BM IPU_CPMEM_WORD(0, 117, 2)
  60. #define IPU_FIELD_ROT IPU_CPMEM_WORD(0, 119, 1)
  61. #define IPU_FIELD_ROT_HF_VF IPU_CPMEM_WORD(0, 119, 3)
  62. #define IPU_FIELD_HF IPU_CPMEM_WORD(0, 120, 1)
  63. #define IPU_FIELD_VF IPU_CPMEM_WORD(0, 121, 1)
  64. #define IPU_FIELD_THE IPU_CPMEM_WORD(0, 122, 1)
  65. #define IPU_FIELD_CAP IPU_CPMEM_WORD(0, 123, 1)
  66. #define IPU_FIELD_CAE IPU_CPMEM_WORD(0, 124, 1)
  67. #define IPU_FIELD_FW IPU_CPMEM_WORD(0, 125, 13)
  68. #define IPU_FIELD_FH IPU_CPMEM_WORD(0, 138, 12)
  69. #define IPU_FIELD_EBA0 IPU_CPMEM_WORD(1, 0, 29)
  70. #define IPU_FIELD_EBA1 IPU_CPMEM_WORD(1, 29, 29)
  71. #define IPU_FIELD_ILO IPU_CPMEM_WORD(1, 58, 20)
  72. #define IPU_FIELD_NPB IPU_CPMEM_WORD(1, 78, 7)
  73. #define IPU_FIELD_PFS IPU_CPMEM_WORD(1, 85, 4)
  74. #define IPU_FIELD_ALU IPU_CPMEM_WORD(1, 89, 1)
  75. #define IPU_FIELD_ALBM IPU_CPMEM_WORD(1, 90, 3)
  76. #define IPU_FIELD_ID IPU_CPMEM_WORD(1, 93, 2)
  77. #define IPU_FIELD_TH IPU_CPMEM_WORD(1, 95, 7)
  78. #define IPU_FIELD_SL IPU_CPMEM_WORD(1, 102, 14)
  79. #define IPU_FIELD_WID0 IPU_CPMEM_WORD(1, 116, 3)
  80. #define IPU_FIELD_WID1 IPU_CPMEM_WORD(1, 119, 3)
  81. #define IPU_FIELD_WID2 IPU_CPMEM_WORD(1, 122, 3)
  82. #define IPU_FIELD_WID3 IPU_CPMEM_WORD(1, 125, 3)
  83. #define IPU_FIELD_OFS0 IPU_CPMEM_WORD(1, 128, 5)
  84. #define IPU_FIELD_OFS1 IPU_CPMEM_WORD(1, 133, 5)
  85. #define IPU_FIELD_OFS2 IPU_CPMEM_WORD(1, 138, 5)
  86. #define IPU_FIELD_OFS3 IPU_CPMEM_WORD(1, 143, 5)
  87. #define IPU_FIELD_SXYS IPU_CPMEM_WORD(1, 148, 1)
  88. #define IPU_FIELD_CRE IPU_CPMEM_WORD(1, 149, 1)
  89. #define IPU_FIELD_DEC_SEL2 IPU_CPMEM_WORD(1, 150, 1)
  90. static inline struct ipu_ch_param __iomem *
  91. ipu_get_cpmem(struct ipuv3_channel *ch)
  92. {
  93. struct ipu_cpmem *cpmem = ch->ipu->cpmem_priv;
  94. return cpmem->base + ch->num;
  95. }
  96. static void ipu_ch_param_write_field(struct ipuv3_channel *ch, u32 wbs, u32 v)
  97. {
  98. struct ipu_ch_param __iomem *base = ipu_get_cpmem(ch);
  99. u32 bit = (wbs >> 8) % 160;
  100. u32 size = wbs & 0xff;
  101. u32 word = (wbs >> 8) / 160;
  102. u32 i = bit / 32;
  103. u32 ofs = bit % 32;
  104. u32 mask = (1 << size) - 1;
  105. u32 val;
  106. pr_debug("%s %d %d %d\n", __func__, word, bit , size);
  107. val = readl(&base->word[word].data[i]);
  108. val &= ~(mask << ofs);
  109. val |= v << ofs;
  110. writel(val, &base->word[word].data[i]);
  111. if ((bit + size - 1) / 32 > i) {
  112. val = readl(&base->word[word].data[i + 1]);
  113. val &= ~(mask >> (ofs ? (32 - ofs) : 0));
  114. val |= v >> (ofs ? (32 - ofs) : 0);
  115. writel(val, &base->word[word].data[i + 1]);
  116. }
  117. }
  118. static u32 ipu_ch_param_read_field(struct ipuv3_channel *ch, u32 wbs)
  119. {
  120. struct ipu_ch_param __iomem *base = ipu_get_cpmem(ch);
  121. u32 bit = (wbs >> 8) % 160;
  122. u32 size = wbs & 0xff;
  123. u32 word = (wbs >> 8) / 160;
  124. u32 i = bit / 32;
  125. u32 ofs = bit % 32;
  126. u32 mask = (1 << size) - 1;
  127. u32 val = 0;
  128. pr_debug("%s %d %d %d\n", __func__, word, bit , size);
  129. val = (readl(&base->word[word].data[i]) >> ofs) & mask;
  130. if ((bit + size - 1) / 32 > i) {
  131. u32 tmp;
  132. tmp = readl(&base->word[word].data[i + 1]);
  133. tmp &= mask >> (ofs ? (32 - ofs) : 0);
  134. val |= tmp << (ofs ? (32 - ofs) : 0);
  135. }
  136. return val;
  137. }
  138. /*
  139. * The V4L2 spec defines packed RGB formats in memory byte order, which from
  140. * point of view of the IPU corresponds to little-endian words with the first
  141. * component in the least significant bits.
  142. * The DRM pixel formats and IPU internal representation are ordered the other
  143. * way around, with the first named component ordered at the most significant
  144. * bits. Further, V4L2 formats are not well defined:
  145. * http://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html
  146. * We choose the interpretation which matches GStreamer behavior.
  147. */
  148. static int v4l2_pix_fmt_to_drm_fourcc(u32 pixelformat)
  149. {
  150. switch (pixelformat) {
  151. case V4L2_PIX_FMT_RGB565:
  152. /*
  153. * Here we choose the 'corrected' interpretation of RGBP, a
  154. * little-endian 16-bit word with the red component at the most
  155. * significant bits:
  156. * g[2:0]b[4:0] r[4:0]g[5:3] <=> [16:0] R:G:B
  157. */
  158. return DRM_FORMAT_RGB565;
  159. case V4L2_PIX_FMT_BGR24:
  160. /* B G R <=> [24:0] R:G:B */
  161. return DRM_FORMAT_RGB888;
  162. case V4L2_PIX_FMT_RGB24:
  163. /* R G B <=> [24:0] B:G:R */
  164. return DRM_FORMAT_BGR888;
  165. case V4L2_PIX_FMT_BGR32:
  166. /* B G R A <=> [32:0] A:B:G:R */
  167. return DRM_FORMAT_XRGB8888;
  168. case V4L2_PIX_FMT_RGB32:
  169. /* R G B A <=> [32:0] A:B:G:R */
  170. return DRM_FORMAT_XBGR8888;
  171. case V4L2_PIX_FMT_UYVY:
  172. return DRM_FORMAT_UYVY;
  173. case V4L2_PIX_FMT_YUYV:
  174. return DRM_FORMAT_YUYV;
  175. case V4L2_PIX_FMT_YUV420:
  176. return DRM_FORMAT_YUV420;
  177. case V4L2_PIX_FMT_YUV422P:
  178. return DRM_FORMAT_YUV422;
  179. case V4L2_PIX_FMT_YVU420:
  180. return DRM_FORMAT_YVU420;
  181. case V4L2_PIX_FMT_NV12:
  182. return DRM_FORMAT_NV12;
  183. case V4L2_PIX_FMT_NV16:
  184. return DRM_FORMAT_NV16;
  185. }
  186. return -EINVAL;
  187. }
  188. void ipu_cpmem_zero(struct ipuv3_channel *ch)
  189. {
  190. struct ipu_ch_param __iomem *p = ipu_get_cpmem(ch);
  191. void __iomem *base = p;
  192. int i;
  193. for (i = 0; i < sizeof(*p) / sizeof(u32); i++)
  194. writel(0, base + i * sizeof(u32));
  195. }
  196. EXPORT_SYMBOL_GPL(ipu_cpmem_zero);
  197. void ipu_cpmem_set_resolution(struct ipuv3_channel *ch, int xres, int yres)
  198. {
  199. ipu_ch_param_write_field(ch, IPU_FIELD_FW, xres - 1);
  200. ipu_ch_param_write_field(ch, IPU_FIELD_FH, yres - 1);
  201. }
  202. EXPORT_SYMBOL_GPL(ipu_cpmem_set_resolution);
  203. void ipu_cpmem_set_stride(struct ipuv3_channel *ch, int stride)
  204. {
  205. ipu_ch_param_write_field(ch, IPU_FIELD_SLY, stride - 1);
  206. }
  207. EXPORT_SYMBOL_GPL(ipu_cpmem_set_stride);
  208. void ipu_cpmem_set_high_priority(struct ipuv3_channel *ch)
  209. {
  210. struct ipu_soc *ipu = ch->ipu;
  211. u32 val;
  212. if (ipu->ipu_type == IPUV3EX)
  213. ipu_ch_param_write_field(ch, IPU_FIELD_ID, 1);
  214. val = ipu_idmac_read(ipu, IDMAC_CHA_PRI(ch->num));
  215. val |= 1 << (ch->num % 32);
  216. ipu_idmac_write(ipu, val, IDMAC_CHA_PRI(ch->num));
  217. };
  218. EXPORT_SYMBOL_GPL(ipu_cpmem_set_high_priority);
  219. void ipu_cpmem_set_buffer(struct ipuv3_channel *ch, int bufnum, dma_addr_t buf)
  220. {
  221. if (bufnum)
  222. ipu_ch_param_write_field(ch, IPU_FIELD_EBA1, buf >> 3);
  223. else
  224. ipu_ch_param_write_field(ch, IPU_FIELD_EBA0, buf >> 3);
  225. }
  226. EXPORT_SYMBOL_GPL(ipu_cpmem_set_buffer);
  227. void ipu_cpmem_interlaced_scan(struct ipuv3_channel *ch, int stride)
  228. {
  229. ipu_ch_param_write_field(ch, IPU_FIELD_SO, 1);
  230. ipu_ch_param_write_field(ch, IPU_FIELD_ILO, stride / 8);
  231. ipu_ch_param_write_field(ch, IPU_FIELD_SLY, (stride * 2) - 1);
  232. };
  233. EXPORT_SYMBOL_GPL(ipu_cpmem_interlaced_scan);
  234. void ipu_cpmem_set_axi_id(struct ipuv3_channel *ch, u32 id)
  235. {
  236. id &= 0x3;
  237. ipu_ch_param_write_field(ch, IPU_FIELD_ID, id);
  238. }
  239. EXPORT_SYMBOL_GPL(ipu_cpmem_set_axi_id);
  240. void ipu_cpmem_set_burstsize(struct ipuv3_channel *ch, int burstsize)
  241. {
  242. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, burstsize - 1);
  243. };
  244. EXPORT_SYMBOL_GPL(ipu_cpmem_set_burstsize);
  245. void ipu_cpmem_set_block_mode(struct ipuv3_channel *ch)
  246. {
  247. ipu_ch_param_write_field(ch, IPU_FIELD_BM, 1);
  248. }
  249. EXPORT_SYMBOL_GPL(ipu_cpmem_set_block_mode);
  250. void ipu_cpmem_set_rotation(struct ipuv3_channel *ch,
  251. enum ipu_rotate_mode rot)
  252. {
  253. u32 temp_rot = bitrev8(rot) >> 5;
  254. ipu_ch_param_write_field(ch, IPU_FIELD_ROT_HF_VF, temp_rot);
  255. }
  256. EXPORT_SYMBOL_GPL(ipu_cpmem_set_rotation);
  257. int ipu_cpmem_set_format_rgb(struct ipuv3_channel *ch,
  258. const struct ipu_rgb *rgb)
  259. {
  260. int bpp = 0, npb = 0, ro, go, bo, to;
  261. ro = rgb->bits_per_pixel - rgb->red.length - rgb->red.offset;
  262. go = rgb->bits_per_pixel - rgb->green.length - rgb->green.offset;
  263. bo = rgb->bits_per_pixel - rgb->blue.length - rgb->blue.offset;
  264. to = rgb->bits_per_pixel - rgb->transp.length - rgb->transp.offset;
  265. ipu_ch_param_write_field(ch, IPU_FIELD_WID0, rgb->red.length - 1);
  266. ipu_ch_param_write_field(ch, IPU_FIELD_OFS0, ro);
  267. ipu_ch_param_write_field(ch, IPU_FIELD_WID1, rgb->green.length - 1);
  268. ipu_ch_param_write_field(ch, IPU_FIELD_OFS1, go);
  269. ipu_ch_param_write_field(ch, IPU_FIELD_WID2, rgb->blue.length - 1);
  270. ipu_ch_param_write_field(ch, IPU_FIELD_OFS2, bo);
  271. if (rgb->transp.length) {
  272. ipu_ch_param_write_field(ch, IPU_FIELD_WID3,
  273. rgb->transp.length - 1);
  274. ipu_ch_param_write_field(ch, IPU_FIELD_OFS3, to);
  275. } else {
  276. ipu_ch_param_write_field(ch, IPU_FIELD_WID3, 7);
  277. ipu_ch_param_write_field(ch, IPU_FIELD_OFS3,
  278. rgb->bits_per_pixel);
  279. }
  280. switch (rgb->bits_per_pixel) {
  281. case 32:
  282. bpp = 0;
  283. npb = 15;
  284. break;
  285. case 24:
  286. bpp = 1;
  287. npb = 19;
  288. break;
  289. case 16:
  290. bpp = 3;
  291. npb = 31;
  292. break;
  293. case 8:
  294. bpp = 5;
  295. npb = 63;
  296. break;
  297. default:
  298. return -EINVAL;
  299. }
  300. ipu_ch_param_write_field(ch, IPU_FIELD_BPP, bpp);
  301. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, npb);
  302. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 7); /* rgb mode */
  303. return 0;
  304. }
  305. EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_rgb);
  306. int ipu_cpmem_set_format_passthrough(struct ipuv3_channel *ch, int width)
  307. {
  308. int bpp = 0, npb = 0;
  309. switch (width) {
  310. case 32:
  311. bpp = 0;
  312. npb = 15;
  313. break;
  314. case 24:
  315. bpp = 1;
  316. npb = 19;
  317. break;
  318. case 16:
  319. bpp = 3;
  320. npb = 31;
  321. break;
  322. case 8:
  323. bpp = 5;
  324. npb = 63;
  325. break;
  326. default:
  327. return -EINVAL;
  328. }
  329. ipu_ch_param_write_field(ch, IPU_FIELD_BPP, bpp);
  330. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, npb);
  331. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 6); /* raw mode */
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(ipu_cpmem_set_format_passthrough);
  335. void ipu_cpmem_set_yuv_interleaved(struct ipuv3_channel *ch, u32 pixel_format)
  336. {
  337. switch (pixel_format) {
  338. case V4L2_PIX_FMT_UYVY:
  339. ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3); /* bits/pixel */
  340. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0xA);/* pix fmt */
  341. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);/* burst size */
  342. break;
  343. case V4L2_PIX_FMT_YUYV:
  344. ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3); /* bits/pixel */
  345. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0x8);/* pix fmt */
  346. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);/* burst size */
  347. break;
  348. }
  349. }
  350. EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_interleaved);
  351. void ipu_cpmem_set_yuv_planar_full(struct ipuv3_channel *ch,
  352. u32 pixel_format, int stride,
  353. int u_offset, int v_offset)
  354. {
  355. switch (pixel_format) {
  356. case V4L2_PIX_FMT_YUV420:
  357. case V4L2_PIX_FMT_YUV422P:
  358. ipu_ch_param_write_field(ch, IPU_FIELD_SLUV, (stride / 2) - 1);
  359. ipu_ch_param_write_field(ch, IPU_FIELD_UBO, u_offset / 8);
  360. ipu_ch_param_write_field(ch, IPU_FIELD_VBO, v_offset / 8);
  361. break;
  362. case V4L2_PIX_FMT_YVU420:
  363. ipu_ch_param_write_field(ch, IPU_FIELD_SLUV, (stride / 2) - 1);
  364. ipu_ch_param_write_field(ch, IPU_FIELD_UBO, v_offset / 8);
  365. ipu_ch_param_write_field(ch, IPU_FIELD_VBO, u_offset / 8);
  366. break;
  367. case V4L2_PIX_FMT_NV12:
  368. case V4L2_PIX_FMT_NV16:
  369. ipu_ch_param_write_field(ch, IPU_FIELD_SLUV, stride - 1);
  370. ipu_ch_param_write_field(ch, IPU_FIELD_UBO, u_offset / 8);
  371. ipu_ch_param_write_field(ch, IPU_FIELD_VBO, u_offset / 8);
  372. break;
  373. }
  374. }
  375. EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar_full);
  376. void ipu_cpmem_set_yuv_planar(struct ipuv3_channel *ch,
  377. u32 pixel_format, int stride, int height)
  378. {
  379. int u_offset, v_offset;
  380. int uv_stride = 0;
  381. switch (pixel_format) {
  382. case V4L2_PIX_FMT_YUV420:
  383. case V4L2_PIX_FMT_YVU420:
  384. uv_stride = stride / 2;
  385. u_offset = stride * height;
  386. v_offset = u_offset + (uv_stride * height / 2);
  387. ipu_cpmem_set_yuv_planar_full(ch, pixel_format, stride,
  388. u_offset, v_offset);
  389. break;
  390. case V4L2_PIX_FMT_YUV422P:
  391. uv_stride = stride / 2;
  392. u_offset = stride * height;
  393. v_offset = u_offset + (uv_stride * height);
  394. ipu_cpmem_set_yuv_planar_full(ch, pixel_format, stride,
  395. u_offset, v_offset);
  396. break;
  397. case V4L2_PIX_FMT_NV12:
  398. case V4L2_PIX_FMT_NV16:
  399. u_offset = stride * height;
  400. ipu_cpmem_set_yuv_planar_full(ch, pixel_format, stride,
  401. u_offset, 0);
  402. break;
  403. }
  404. }
  405. EXPORT_SYMBOL_GPL(ipu_cpmem_set_yuv_planar);
  406. static const struct ipu_rgb def_xrgb_32 = {
  407. .red = { .offset = 16, .length = 8, },
  408. .green = { .offset = 8, .length = 8, },
  409. .blue = { .offset = 0, .length = 8, },
  410. .transp = { .offset = 24, .length = 8, },
  411. .bits_per_pixel = 32,
  412. };
  413. static const struct ipu_rgb def_xbgr_32 = {
  414. .red = { .offset = 0, .length = 8, },
  415. .green = { .offset = 8, .length = 8, },
  416. .blue = { .offset = 16, .length = 8, },
  417. .transp = { .offset = 24, .length = 8, },
  418. .bits_per_pixel = 32,
  419. };
  420. static const struct ipu_rgb def_rgbx_32 = {
  421. .red = { .offset = 24, .length = 8, },
  422. .green = { .offset = 16, .length = 8, },
  423. .blue = { .offset = 8, .length = 8, },
  424. .transp = { .offset = 0, .length = 8, },
  425. .bits_per_pixel = 32,
  426. };
  427. static const struct ipu_rgb def_bgrx_32 = {
  428. .red = { .offset = 8, .length = 8, },
  429. .green = { .offset = 16, .length = 8, },
  430. .blue = { .offset = 24, .length = 8, },
  431. .transp = { .offset = 0, .length = 8, },
  432. .bits_per_pixel = 32,
  433. };
  434. static const struct ipu_rgb def_rgb_24 = {
  435. .red = { .offset = 16, .length = 8, },
  436. .green = { .offset = 8, .length = 8, },
  437. .blue = { .offset = 0, .length = 8, },
  438. .transp = { .offset = 0, .length = 0, },
  439. .bits_per_pixel = 24,
  440. };
  441. static const struct ipu_rgb def_bgr_24 = {
  442. .red = { .offset = 0, .length = 8, },
  443. .green = { .offset = 8, .length = 8, },
  444. .blue = { .offset = 16, .length = 8, },
  445. .transp = { .offset = 0, .length = 0, },
  446. .bits_per_pixel = 24,
  447. };
  448. static const struct ipu_rgb def_rgb_16 = {
  449. .red = { .offset = 11, .length = 5, },
  450. .green = { .offset = 5, .length = 6, },
  451. .blue = { .offset = 0, .length = 5, },
  452. .transp = { .offset = 0, .length = 0, },
  453. .bits_per_pixel = 16,
  454. };
  455. static const struct ipu_rgb def_bgr_16 = {
  456. .red = { .offset = 0, .length = 5, },
  457. .green = { .offset = 5, .length = 6, },
  458. .blue = { .offset = 11, .length = 5, },
  459. .transp = { .offset = 0, .length = 0, },
  460. .bits_per_pixel = 16,
  461. };
  462. static const struct ipu_rgb def_argb_16 = {
  463. .red = { .offset = 10, .length = 5, },
  464. .green = { .offset = 5, .length = 5, },
  465. .blue = { .offset = 0, .length = 5, },
  466. .transp = { .offset = 15, .length = 1, },
  467. .bits_per_pixel = 16,
  468. };
  469. static const struct ipu_rgb def_argb_16_4444 = {
  470. .red = { .offset = 8, .length = 4, },
  471. .green = { .offset = 4, .length = 4, },
  472. .blue = { .offset = 0, .length = 4, },
  473. .transp = { .offset = 12, .length = 4, },
  474. .bits_per_pixel = 16,
  475. };
  476. static const struct ipu_rgb def_abgr_16 = {
  477. .red = { .offset = 0, .length = 5, },
  478. .green = { .offset = 5, .length = 5, },
  479. .blue = { .offset = 10, .length = 5, },
  480. .transp = { .offset = 15, .length = 1, },
  481. .bits_per_pixel = 16,
  482. };
  483. static const struct ipu_rgb def_rgba_16 = {
  484. .red = { .offset = 11, .length = 5, },
  485. .green = { .offset = 6, .length = 5, },
  486. .blue = { .offset = 1, .length = 5, },
  487. .transp = { .offset = 0, .length = 1, },
  488. .bits_per_pixel = 16,
  489. };
  490. static const struct ipu_rgb def_bgra_16 = {
  491. .red = { .offset = 1, .length = 5, },
  492. .green = { .offset = 6, .length = 5, },
  493. .blue = { .offset = 11, .length = 5, },
  494. .transp = { .offset = 0, .length = 1, },
  495. .bits_per_pixel = 16,
  496. };
  497. #define Y_OFFSET(pix, x, y) ((x) + pix->width * (y))
  498. #define U_OFFSET(pix, x, y) ((pix->width * pix->height) + \
  499. (pix->width * (y) / 4) + (x) / 2)
  500. #define V_OFFSET(pix, x, y) ((pix->width * pix->height) + \
  501. (pix->width * pix->height / 4) + \
  502. (pix->width * (y) / 4) + (x) / 2)
  503. #define U2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
  504. (pix->width * (y) / 2) + (x) / 2)
  505. #define V2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
  506. (pix->width * pix->height / 2) + \
  507. (pix->width * (y) / 2) + (x) / 2)
  508. #define UV_OFFSET(pix, x, y) ((pix->width * pix->height) + \
  509. (pix->width * (y) / 2) + (x))
  510. #define UV2_OFFSET(pix, x, y) ((pix->width * pix->height) + \
  511. (pix->width * y) + (x))
  512. int ipu_cpmem_set_fmt(struct ipuv3_channel *ch, u32 drm_fourcc)
  513. {
  514. switch (drm_fourcc) {
  515. case DRM_FORMAT_YUV420:
  516. case DRM_FORMAT_YVU420:
  517. /* pix format */
  518. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 2);
  519. /* burst size */
  520. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
  521. break;
  522. case DRM_FORMAT_YUV422:
  523. case DRM_FORMAT_YVU422:
  524. /* pix format */
  525. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 1);
  526. /* burst size */
  527. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
  528. break;
  529. case DRM_FORMAT_NV12:
  530. /* pix format */
  531. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 4);
  532. /* burst size */
  533. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
  534. break;
  535. case DRM_FORMAT_NV16:
  536. /* pix format */
  537. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 3);
  538. /* burst size */
  539. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
  540. break;
  541. case DRM_FORMAT_UYVY:
  542. /* bits/pixel */
  543. ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3);
  544. /* pix format */
  545. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0xA);
  546. /* burst size */
  547. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
  548. break;
  549. case DRM_FORMAT_YUYV:
  550. /* bits/pixel */
  551. ipu_ch_param_write_field(ch, IPU_FIELD_BPP, 3);
  552. /* pix format */
  553. ipu_ch_param_write_field(ch, IPU_FIELD_PFS, 0x8);
  554. /* burst size */
  555. ipu_ch_param_write_field(ch, IPU_FIELD_NPB, 31);
  556. break;
  557. case DRM_FORMAT_ABGR8888:
  558. case DRM_FORMAT_XBGR8888:
  559. ipu_cpmem_set_format_rgb(ch, &def_xbgr_32);
  560. break;
  561. case DRM_FORMAT_ARGB8888:
  562. case DRM_FORMAT_XRGB8888:
  563. ipu_cpmem_set_format_rgb(ch, &def_xrgb_32);
  564. break;
  565. case DRM_FORMAT_RGBA8888:
  566. case DRM_FORMAT_RGBX8888:
  567. ipu_cpmem_set_format_rgb(ch, &def_rgbx_32);
  568. break;
  569. case DRM_FORMAT_BGRA8888:
  570. case DRM_FORMAT_BGRX8888:
  571. ipu_cpmem_set_format_rgb(ch, &def_bgrx_32);
  572. break;
  573. case DRM_FORMAT_BGR888:
  574. ipu_cpmem_set_format_rgb(ch, &def_bgr_24);
  575. break;
  576. case DRM_FORMAT_RGB888:
  577. ipu_cpmem_set_format_rgb(ch, &def_rgb_24);
  578. break;
  579. case DRM_FORMAT_RGB565:
  580. ipu_cpmem_set_format_rgb(ch, &def_rgb_16);
  581. break;
  582. case DRM_FORMAT_BGR565:
  583. ipu_cpmem_set_format_rgb(ch, &def_bgr_16);
  584. break;
  585. case DRM_FORMAT_ARGB1555:
  586. ipu_cpmem_set_format_rgb(ch, &def_argb_16);
  587. break;
  588. case DRM_FORMAT_ABGR1555:
  589. ipu_cpmem_set_format_rgb(ch, &def_abgr_16);
  590. break;
  591. case DRM_FORMAT_RGBA5551:
  592. ipu_cpmem_set_format_rgb(ch, &def_rgba_16);
  593. break;
  594. case DRM_FORMAT_BGRA5551:
  595. ipu_cpmem_set_format_rgb(ch, &def_bgra_16);
  596. break;
  597. case DRM_FORMAT_ARGB4444:
  598. ipu_cpmem_set_format_rgb(ch, &def_argb_16_4444);
  599. break;
  600. default:
  601. return -EINVAL;
  602. }
  603. return 0;
  604. }
  605. EXPORT_SYMBOL_GPL(ipu_cpmem_set_fmt);
  606. int ipu_cpmem_set_image(struct ipuv3_channel *ch, struct ipu_image *image)
  607. {
  608. struct v4l2_pix_format *pix = &image->pix;
  609. int offset, u_offset, v_offset;
  610. pr_debug("%s: resolution: %dx%d stride: %d\n",
  611. __func__, pix->width, pix->height,
  612. pix->bytesperline);
  613. ipu_cpmem_set_resolution(ch, image->rect.width, image->rect.height);
  614. ipu_cpmem_set_stride(ch, pix->bytesperline);
  615. ipu_cpmem_set_fmt(ch, v4l2_pix_fmt_to_drm_fourcc(pix->pixelformat));
  616. switch (pix->pixelformat) {
  617. case V4L2_PIX_FMT_YUV420:
  618. case V4L2_PIX_FMT_YVU420:
  619. offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
  620. u_offset = U_OFFSET(pix, image->rect.left,
  621. image->rect.top) - offset;
  622. v_offset = V_OFFSET(pix, image->rect.left,
  623. image->rect.top) - offset;
  624. ipu_cpmem_set_yuv_planar_full(ch, pix->pixelformat,
  625. pix->bytesperline,
  626. u_offset, v_offset);
  627. break;
  628. case V4L2_PIX_FMT_YUV422P:
  629. offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
  630. u_offset = U2_OFFSET(pix, image->rect.left,
  631. image->rect.top) - offset;
  632. v_offset = V2_OFFSET(pix, image->rect.left,
  633. image->rect.top) - offset;
  634. ipu_cpmem_set_yuv_planar_full(ch, pix->pixelformat,
  635. pix->bytesperline,
  636. u_offset, v_offset);
  637. break;
  638. case V4L2_PIX_FMT_NV12:
  639. offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
  640. u_offset = UV_OFFSET(pix, image->rect.left,
  641. image->rect.top) - offset;
  642. v_offset = 0;
  643. ipu_cpmem_set_yuv_planar_full(ch, pix->pixelformat,
  644. pix->bytesperline,
  645. u_offset, v_offset);
  646. break;
  647. case V4L2_PIX_FMT_NV16:
  648. offset = Y_OFFSET(pix, image->rect.left, image->rect.top);
  649. u_offset = UV2_OFFSET(pix, image->rect.left,
  650. image->rect.top) - offset;
  651. v_offset = 0;
  652. ipu_cpmem_set_yuv_planar_full(ch, pix->pixelformat,
  653. pix->bytesperline,
  654. u_offset, v_offset);
  655. break;
  656. case V4L2_PIX_FMT_UYVY:
  657. case V4L2_PIX_FMT_YUYV:
  658. case V4L2_PIX_FMT_RGB565:
  659. offset = image->rect.left * 2 +
  660. image->rect.top * pix->bytesperline;
  661. break;
  662. case V4L2_PIX_FMT_RGB32:
  663. case V4L2_PIX_FMT_BGR32:
  664. offset = image->rect.left * 4 +
  665. image->rect.top * pix->bytesperline;
  666. break;
  667. case V4L2_PIX_FMT_RGB24:
  668. case V4L2_PIX_FMT_BGR24:
  669. offset = image->rect.left * 3 +
  670. image->rect.top * pix->bytesperline;
  671. break;
  672. default:
  673. return -EINVAL;
  674. }
  675. ipu_cpmem_set_buffer(ch, 0, image->phys0 + offset);
  676. ipu_cpmem_set_buffer(ch, 1, image->phys1 + offset);
  677. return 0;
  678. }
  679. EXPORT_SYMBOL_GPL(ipu_cpmem_set_image);
  680. void ipu_cpmem_dump(struct ipuv3_channel *ch)
  681. {
  682. struct ipu_ch_param __iomem *p = ipu_get_cpmem(ch);
  683. struct ipu_soc *ipu = ch->ipu;
  684. int chno = ch->num;
  685. dev_dbg(ipu->dev, "ch %d word 0 - %08X %08X %08X %08X %08X\n", chno,
  686. readl(&p->word[0].data[0]),
  687. readl(&p->word[0].data[1]),
  688. readl(&p->word[0].data[2]),
  689. readl(&p->word[0].data[3]),
  690. readl(&p->word[0].data[4]));
  691. dev_dbg(ipu->dev, "ch %d word 1 - %08X %08X %08X %08X %08X\n", chno,
  692. readl(&p->word[1].data[0]),
  693. readl(&p->word[1].data[1]),
  694. readl(&p->word[1].data[2]),
  695. readl(&p->word[1].data[3]),
  696. readl(&p->word[1].data[4]));
  697. dev_dbg(ipu->dev, "PFS 0x%x, ",
  698. ipu_ch_param_read_field(ch, IPU_FIELD_PFS));
  699. dev_dbg(ipu->dev, "BPP 0x%x, ",
  700. ipu_ch_param_read_field(ch, IPU_FIELD_BPP));
  701. dev_dbg(ipu->dev, "NPB 0x%x\n",
  702. ipu_ch_param_read_field(ch, IPU_FIELD_NPB));
  703. dev_dbg(ipu->dev, "FW %d, ",
  704. ipu_ch_param_read_field(ch, IPU_FIELD_FW));
  705. dev_dbg(ipu->dev, "FH %d, ",
  706. ipu_ch_param_read_field(ch, IPU_FIELD_FH));
  707. dev_dbg(ipu->dev, "EBA0 0x%x\n",
  708. ipu_ch_param_read_field(ch, IPU_FIELD_EBA0) << 3);
  709. dev_dbg(ipu->dev, "EBA1 0x%x\n",
  710. ipu_ch_param_read_field(ch, IPU_FIELD_EBA1) << 3);
  711. dev_dbg(ipu->dev, "Stride %d\n",
  712. ipu_ch_param_read_field(ch, IPU_FIELD_SL));
  713. dev_dbg(ipu->dev, "scan_order %d\n",
  714. ipu_ch_param_read_field(ch, IPU_FIELD_SO));
  715. dev_dbg(ipu->dev, "uv_stride %d\n",
  716. ipu_ch_param_read_field(ch, IPU_FIELD_SLUV));
  717. dev_dbg(ipu->dev, "u_offset 0x%x\n",
  718. ipu_ch_param_read_field(ch, IPU_FIELD_UBO) << 3);
  719. dev_dbg(ipu->dev, "v_offset 0x%x\n",
  720. ipu_ch_param_read_field(ch, IPU_FIELD_VBO) << 3);
  721. dev_dbg(ipu->dev, "Width0 %d+1, ",
  722. ipu_ch_param_read_field(ch, IPU_FIELD_WID0));
  723. dev_dbg(ipu->dev, "Width1 %d+1, ",
  724. ipu_ch_param_read_field(ch, IPU_FIELD_WID1));
  725. dev_dbg(ipu->dev, "Width2 %d+1, ",
  726. ipu_ch_param_read_field(ch, IPU_FIELD_WID2));
  727. dev_dbg(ipu->dev, "Width3 %d+1, ",
  728. ipu_ch_param_read_field(ch, IPU_FIELD_WID3));
  729. dev_dbg(ipu->dev, "Offset0 %d, ",
  730. ipu_ch_param_read_field(ch, IPU_FIELD_OFS0));
  731. dev_dbg(ipu->dev, "Offset1 %d, ",
  732. ipu_ch_param_read_field(ch, IPU_FIELD_OFS1));
  733. dev_dbg(ipu->dev, "Offset2 %d, ",
  734. ipu_ch_param_read_field(ch, IPU_FIELD_OFS2));
  735. dev_dbg(ipu->dev, "Offset3 %d\n",
  736. ipu_ch_param_read_field(ch, IPU_FIELD_OFS3));
  737. }
  738. EXPORT_SYMBOL_GPL(ipu_cpmem_dump);
  739. int ipu_cpmem_init(struct ipu_soc *ipu, struct device *dev, unsigned long base)
  740. {
  741. struct ipu_cpmem *cpmem;
  742. cpmem = devm_kzalloc(dev, sizeof(*cpmem), GFP_KERNEL);
  743. if (!cpmem)
  744. return -ENOMEM;
  745. ipu->cpmem_priv = cpmem;
  746. spin_lock_init(&cpmem->lock);
  747. cpmem->base = devm_ioremap(dev, base, SZ_128K);
  748. if (!cpmem->base)
  749. return -ENOMEM;
  750. dev_dbg(dev, "CPMEM base: 0x%08lx remapped to %p\n",
  751. base, cpmem->base);
  752. cpmem->ipu = ipu;
  753. return 0;
  754. }
  755. void ipu_cpmem_exit(struct ipu_soc *ipu)
  756. {
  757. }