sc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Scaler library
  4. *
  5. * Copyright (c) 2013 Texas Instruments Inc.
  6. *
  7. * David Griego, <dagriego@biglakesoftware.com>
  8. * Dale Farnsworth, <dale@farnsworth.org>
  9. * Archit Taneja, <archit@ti.com>
  10. */
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include "sc.h"
  17. #include "sc_coeff.h"
  18. void sc_dump_regs(struct sc_data *sc)
  19. {
  20. struct device *dev = &sc->pdev->dev;
  21. #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, \
  22. ioread32(sc->base + CFG_##r))
  23. dev_dbg(dev, "SC Registers @ %pa:\n", &sc->res->start);
  24. DUMPREG(SC0);
  25. DUMPREG(SC1);
  26. DUMPREG(SC2);
  27. DUMPREG(SC3);
  28. DUMPREG(SC4);
  29. DUMPREG(SC5);
  30. DUMPREG(SC6);
  31. DUMPREG(SC8);
  32. DUMPREG(SC9);
  33. DUMPREG(SC10);
  34. DUMPREG(SC11);
  35. DUMPREG(SC12);
  36. DUMPREG(SC13);
  37. DUMPREG(SC17);
  38. DUMPREG(SC18);
  39. DUMPREG(SC19);
  40. DUMPREG(SC20);
  41. DUMPREG(SC21);
  42. DUMPREG(SC22);
  43. DUMPREG(SC23);
  44. DUMPREG(SC24);
  45. DUMPREG(SC25);
  46. #undef DUMPREG
  47. }
  48. EXPORT_SYMBOL(sc_dump_regs);
  49. /*
  50. * set the horizontal scaler coefficients according to the ratio of output to
  51. * input widths, after accounting for up to two levels of decimation
  52. */
  53. void sc_set_hs_coeffs(struct sc_data *sc, void *addr, unsigned int src_w,
  54. unsigned int dst_w)
  55. {
  56. int sixteenths;
  57. int idx;
  58. int i, j;
  59. u16 *coeff_h = addr;
  60. const u16 *cp;
  61. if (dst_w > src_w) {
  62. idx = HS_UP_SCALE;
  63. } else {
  64. if ((dst_w << 1) < src_w)
  65. dst_w <<= 1; /* first level decimation */
  66. if ((dst_w << 1) < src_w)
  67. dst_w <<= 1; /* second level decimation */
  68. if (dst_w == src_w) {
  69. idx = HS_LE_16_16_SCALE;
  70. } else {
  71. sixteenths = (dst_w << 4) / src_w;
  72. if (sixteenths < 8)
  73. sixteenths = 8;
  74. idx = HS_LT_9_16_SCALE + sixteenths - 8;
  75. }
  76. }
  77. cp = scaler_hs_coeffs[idx];
  78. for (i = 0; i < SC_NUM_PHASES * 2; i++) {
  79. for (j = 0; j < SC_H_NUM_TAPS; j++)
  80. *coeff_h++ = *cp++;
  81. /*
  82. * for each phase, the scaler expects space for 8 coefficients
  83. * in it's memory. For the horizontal scaler, we copy the first
  84. * 7 coefficients and skip the last slot to move to the next
  85. * row to hold coefficients for the next phase
  86. */
  87. coeff_h += SC_NUM_TAPS_MEM_ALIGN - SC_H_NUM_TAPS;
  88. }
  89. sc->load_coeff_h = true;
  90. }
  91. EXPORT_SYMBOL(sc_set_hs_coeffs);
  92. /*
  93. * set the vertical scaler coefficients according to the ratio of output to
  94. * input heights
  95. */
  96. void sc_set_vs_coeffs(struct sc_data *sc, void *addr, unsigned int src_h,
  97. unsigned int dst_h)
  98. {
  99. int sixteenths;
  100. int idx;
  101. int i, j;
  102. u16 *coeff_v = addr;
  103. const u16 *cp;
  104. if (dst_h > src_h) {
  105. idx = VS_UP_SCALE;
  106. } else if (dst_h == src_h) {
  107. idx = VS_1_TO_1_SCALE;
  108. } else {
  109. sixteenths = (dst_h << 4) / src_h;
  110. if (sixteenths < 8)
  111. sixteenths = 8;
  112. idx = VS_LT_9_16_SCALE + sixteenths - 8;
  113. }
  114. cp = scaler_vs_coeffs[idx];
  115. for (i = 0; i < SC_NUM_PHASES * 2; i++) {
  116. for (j = 0; j < SC_V_NUM_TAPS; j++)
  117. *coeff_v++ = *cp++;
  118. /*
  119. * for the vertical scaler, we copy the first 5 coefficients and
  120. * skip the last 3 slots to move to the next row to hold
  121. * coefficients for the next phase
  122. */
  123. coeff_v += SC_NUM_TAPS_MEM_ALIGN - SC_V_NUM_TAPS;
  124. }
  125. sc->load_coeff_v = true;
  126. }
  127. EXPORT_SYMBOL(sc_set_vs_coeffs);
  128. void sc_config_scaler(struct sc_data *sc, u32 *sc_reg0, u32 *sc_reg8,
  129. u32 *sc_reg17, unsigned int src_w, unsigned int src_h,
  130. unsigned int dst_w, unsigned int dst_h)
  131. {
  132. struct device *dev = &sc->pdev->dev;
  133. u32 val;
  134. int dcm_x, dcm_shift;
  135. bool use_rav;
  136. unsigned long lltmp;
  137. u32 lin_acc_inc, lin_acc_inc_u;
  138. u32 col_acc_offset;
  139. u16 factor = 0;
  140. int row_acc_init_rav = 0, row_acc_init_rav_b = 0;
  141. u32 row_acc_inc = 0, row_acc_offset = 0, row_acc_offset_b = 0;
  142. /*
  143. * location of SC register in payload memory with respect to the first
  144. * register in the mmr address data block
  145. */
  146. u32 *sc_reg9 = sc_reg8 + 1;
  147. u32 *sc_reg12 = sc_reg8 + 4;
  148. u32 *sc_reg13 = sc_reg8 + 5;
  149. u32 *sc_reg24 = sc_reg17 + 7;
  150. val = sc_reg0[0];
  151. /* clear all the features(they may get enabled elsewhere later) */
  152. val &= ~(CFG_SELFGEN_FID | CFG_TRIM | CFG_ENABLE_SIN2_VER_INTP |
  153. CFG_INTERLACE_I | CFG_DCM_4X | CFG_DCM_2X | CFG_AUTO_HS |
  154. CFG_ENABLE_EV | CFG_USE_RAV | CFG_INVT_FID | CFG_SC_BYPASS |
  155. CFG_INTERLACE_O | CFG_Y_PK_EN | CFG_HP_BYPASS | CFG_LINEAR);
  156. if (src_w == dst_w && src_h == dst_h) {
  157. val |= CFG_SC_BYPASS;
  158. sc_reg0[0] = val;
  159. return;
  160. }
  161. /* we only support linear scaling for now */
  162. val |= CFG_LINEAR;
  163. /* configure horizontal scaler */
  164. /* enable 2X or 4X decimation */
  165. dcm_x = src_w / dst_w;
  166. if (dcm_x > 4) {
  167. val |= CFG_DCM_4X;
  168. dcm_shift = 2;
  169. } else if (dcm_x > 2) {
  170. val |= CFG_DCM_2X;
  171. dcm_shift = 1;
  172. } else {
  173. dcm_shift = 0;
  174. }
  175. lltmp = dst_w - 1;
  176. lin_acc_inc = div64_u64(((u64)(src_w >> dcm_shift) - 1) << 24, lltmp);
  177. lin_acc_inc_u = 0;
  178. col_acc_offset = 0;
  179. dev_dbg(dev, "hs config: src_w = %d, dst_w = %d, decimation = %s, lin_acc_inc = %08x\n",
  180. src_w, dst_w, dcm_shift == 2 ? "4x" :
  181. (dcm_shift == 1 ? "2x" : "none"), lin_acc_inc);
  182. /* configure vertical scaler */
  183. /* use RAV for vertical scaler if vertical downscaling is > 4x */
  184. if (dst_h < (src_h >> 2)) {
  185. use_rav = true;
  186. val |= CFG_USE_RAV;
  187. } else {
  188. use_rav = false;
  189. }
  190. if (use_rav) {
  191. /* use RAV */
  192. factor = (u16) ((dst_h << 10) / src_h);
  193. row_acc_init_rav = factor + ((1 + factor) >> 1);
  194. if (row_acc_init_rav >= 1024)
  195. row_acc_init_rav -= 1024;
  196. row_acc_init_rav_b = row_acc_init_rav +
  197. (1 + (row_acc_init_rav >> 1)) -
  198. (1024 >> 1);
  199. if (row_acc_init_rav_b < 0) {
  200. row_acc_init_rav_b += row_acc_init_rav;
  201. row_acc_init_rav *= 2;
  202. }
  203. dev_dbg(dev, "vs config(RAV): src_h = %d, dst_h = %d, factor = %d, acc_init = %08x, acc_init_b = %08x\n",
  204. src_h, dst_h, factor, row_acc_init_rav,
  205. row_acc_init_rav_b);
  206. } else {
  207. /* use polyphase */
  208. row_acc_inc = ((src_h - 1) << 16) / (dst_h - 1);
  209. row_acc_offset = 0;
  210. row_acc_offset_b = 0;
  211. dev_dbg(dev, "vs config(POLY): src_h = %d, dst_h = %d,row_acc_inc = %08x\n",
  212. src_h, dst_h, row_acc_inc);
  213. }
  214. sc_reg0[0] = val;
  215. sc_reg0[1] = row_acc_inc;
  216. sc_reg0[2] = row_acc_offset;
  217. sc_reg0[3] = row_acc_offset_b;
  218. sc_reg0[4] = ((lin_acc_inc_u & CFG_LIN_ACC_INC_U_MASK) <<
  219. CFG_LIN_ACC_INC_U_SHIFT) | (dst_w << CFG_TAR_W_SHIFT) |
  220. (dst_h << CFG_TAR_H_SHIFT);
  221. sc_reg0[5] = (src_w << CFG_SRC_W_SHIFT) | (src_h << CFG_SRC_H_SHIFT);
  222. sc_reg0[6] = (row_acc_init_rav_b << CFG_ROW_ACC_INIT_RAV_B_SHIFT) |
  223. (row_acc_init_rav << CFG_ROW_ACC_INIT_RAV_SHIFT);
  224. *sc_reg9 = lin_acc_inc;
  225. *sc_reg12 = col_acc_offset << CFG_COL_ACC_OFFSET_SHIFT;
  226. *sc_reg13 = factor;
  227. *sc_reg24 = (src_w << CFG_ORG_W_SHIFT) | (src_h << CFG_ORG_H_SHIFT);
  228. }
  229. EXPORT_SYMBOL(sc_config_scaler);
  230. struct sc_data *sc_create(struct platform_device *pdev, const char *res_name)
  231. {
  232. struct sc_data *sc;
  233. dev_dbg(&pdev->dev, "sc_create\n");
  234. sc = devm_kzalloc(&pdev->dev, sizeof(*sc), GFP_KERNEL);
  235. if (!sc) {
  236. dev_err(&pdev->dev, "couldn't alloc sc_data\n");
  237. return ERR_PTR(-ENOMEM);
  238. }
  239. sc->pdev = pdev;
  240. sc->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
  241. if (!sc->res) {
  242. dev_err(&pdev->dev, "missing '%s' platform resources data\n",
  243. res_name);
  244. return ERR_PTR(-ENODEV);
  245. }
  246. sc->base = devm_ioremap_resource(&pdev->dev, sc->res);
  247. if (IS_ERR(sc->base))
  248. return ERR_CAST(sc->base);
  249. return sc;
  250. }
  251. EXPORT_SYMBOL(sc_create);
  252. MODULE_DESCRIPTION("TI VIP/VPE Scaler");
  253. MODULE_AUTHOR("Texas Instruments Inc.");
  254. MODULE_LICENSE("GPL v2");