sc.c 7.4 KB

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