sc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/platform_device.h>
  17. #include <linux/slab.h>
  18. #include "sc.h"
  19. #include "sc_coeff.h"
  20. void sc_dump_regs(struct sc_data *sc)
  21. {
  22. struct device *dev = &sc->pdev->dev;
  23. #define DUMPREG(r) dev_dbg(dev, "%-35s %08x\n", #r, \
  24. ioread32(sc->base + CFG_##r))
  25. DUMPREG(SC0);
  26. DUMPREG(SC1);
  27. DUMPREG(SC2);
  28. DUMPREG(SC3);
  29. DUMPREG(SC4);
  30. DUMPREG(SC5);
  31. DUMPREG(SC6);
  32. DUMPREG(SC8);
  33. DUMPREG(SC9);
  34. DUMPREG(SC10);
  35. DUMPREG(SC11);
  36. DUMPREG(SC12);
  37. DUMPREG(SC13);
  38. DUMPREG(SC17);
  39. DUMPREG(SC18);
  40. DUMPREG(SC19);
  41. DUMPREG(SC20);
  42. DUMPREG(SC21);
  43. DUMPREG(SC22);
  44. DUMPREG(SC23);
  45. DUMPREG(SC24);
  46. DUMPREG(SC25);
  47. #undef DUMPREG
  48. }
  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. if (idx == sc->hs_index)
  78. return;
  79. cp = scaler_hs_coeffs[idx];
  80. for (i = 0; i < SC_NUM_PHASES * 2; i++) {
  81. for (j = 0; j < SC_H_NUM_TAPS; j++)
  82. *coeff_h++ = *cp++;
  83. /*
  84. * for each phase, the scaler expects space for 8 coefficients
  85. * in it's memory. For the horizontal scaler, we copy the first
  86. * 7 coefficients and skip the last slot to move to the next
  87. * row to hold coefficients for the next phase
  88. */
  89. coeff_h += SC_NUM_TAPS_MEM_ALIGN - SC_H_NUM_TAPS;
  90. }
  91. sc->hs_index = idx;
  92. sc->load_coeff_h = true;
  93. }
  94. /*
  95. * set the vertical scaler coefficients according to the ratio of output to
  96. * input heights
  97. */
  98. void sc_set_vs_coeffs(struct sc_data *sc, void *addr, unsigned int src_h,
  99. unsigned int dst_h)
  100. {
  101. int sixteenths;
  102. int idx;
  103. int i, j;
  104. u16 *coeff_v = addr;
  105. const u16 *cp;
  106. if (dst_h > src_h) {
  107. idx = VS_UP_SCALE;
  108. } else if (dst_h == src_h) {
  109. idx = VS_1_TO_1_SCALE;
  110. } else {
  111. sixteenths = (dst_h << 4) / src_h;
  112. if (sixteenths < 8)
  113. sixteenths = 8;
  114. idx = VS_LT_9_16_SCALE + sixteenths - 8;
  115. }
  116. if (idx == sc->vs_index)
  117. return;
  118. cp = scaler_vs_coeffs[idx];
  119. for (i = 0; i < SC_NUM_PHASES * 2; i++) {
  120. for (j = 0; j < SC_V_NUM_TAPS; j++)
  121. *coeff_v++ = *cp++;
  122. /*
  123. * for the vertical scaler, we copy the first 5 coefficients and
  124. * skip the last 3 slots to move to the next row to hold
  125. * coefficients for the next phase
  126. */
  127. coeff_v += SC_NUM_TAPS_MEM_ALIGN - SC_V_NUM_TAPS;
  128. }
  129. sc->vs_index = idx;
  130. sc->load_coeff_v = true;
  131. }
  132. void sc_config_scaler(struct sc_data *sc, u32 *sc_reg0, u32 *sc_reg8,
  133. u32 *sc_reg17, unsigned int src_w, unsigned int src_h,
  134. unsigned int dst_w, unsigned int dst_h)
  135. {
  136. struct device *dev = &sc->pdev->dev;
  137. u32 val;
  138. int dcm_x, dcm_shift;
  139. bool use_rav;
  140. unsigned long lltmp;
  141. u32 lin_acc_inc, lin_acc_inc_u;
  142. u32 col_acc_offset;
  143. u16 factor = 0;
  144. int row_acc_init_rav = 0, row_acc_init_rav_b = 0;
  145. u32 row_acc_inc = 0, row_acc_offset = 0, row_acc_offset_b = 0;
  146. /*
  147. * location of SC register in payload memory with respect to the first
  148. * register in the mmr address data block
  149. */
  150. u32 *sc_reg9 = sc_reg8 + 1;
  151. u32 *sc_reg12 = sc_reg8 + 4;
  152. u32 *sc_reg13 = sc_reg8 + 5;
  153. u32 *sc_reg24 = sc_reg17 + 7;
  154. val = sc_reg0[0];
  155. /* clear all the features(they may get enabled elsewhere later) */
  156. val &= ~(CFG_SELFGEN_FID | CFG_TRIM | CFG_ENABLE_SIN2_VER_INTP |
  157. CFG_INTERLACE_I | CFG_DCM_4X | CFG_DCM_2X | CFG_AUTO_HS |
  158. CFG_ENABLE_EV | CFG_USE_RAV | CFG_INVT_FID | CFG_SC_BYPASS |
  159. CFG_INTERLACE_O | CFG_Y_PK_EN | CFG_HP_BYPASS | CFG_LINEAR);
  160. if (src_w == dst_w && src_h == dst_h) {
  161. val |= CFG_SC_BYPASS;
  162. sc_reg0[0] = val;
  163. return;
  164. }
  165. /* we only support linear scaling for now */
  166. val |= CFG_LINEAR;
  167. /* configure horizontal scaler */
  168. /* enable 2X or 4X decimation */
  169. dcm_x = src_w / dst_w;
  170. if (dcm_x > 4) {
  171. val |= CFG_DCM_4X;
  172. dcm_shift = 2;
  173. } else if (dcm_x > 2) {
  174. val |= CFG_DCM_2X;
  175. dcm_shift = 1;
  176. } else {
  177. dcm_shift = 0;
  178. }
  179. lltmp = dst_w - 1;
  180. lin_acc_inc = div64_u64(((u64)(src_w >> dcm_shift) - 1) << 24, lltmp);
  181. lin_acc_inc_u = 0;
  182. col_acc_offset = 0;
  183. dev_dbg(dev, "hs config: src_w = %d, dst_w = %d, decimation = %s, lin_acc_inc = %08x\n",
  184. src_w, dst_w, dcm_shift == 2 ? "4x" :
  185. (dcm_shift == 1 ? "2x" : "none"), lin_acc_inc);
  186. /* configure vertical scaler */
  187. /* use RAV for vertical scaler if vertical downscaling is > 4x */
  188. if (dst_h < (src_h >> 2)) {
  189. use_rav = true;
  190. val |= CFG_USE_RAV;
  191. } else {
  192. use_rav = false;
  193. }
  194. if (use_rav) {
  195. /* use RAV */
  196. factor = (u16) ((dst_h << 10) / src_h);
  197. row_acc_init_rav = factor + ((1 + factor) >> 1);
  198. if (row_acc_init_rav >= 1024)
  199. row_acc_init_rav -= 1024;
  200. row_acc_init_rav_b = row_acc_init_rav +
  201. (1 + (row_acc_init_rav >> 1)) -
  202. (1024 >> 1);
  203. if (row_acc_init_rav_b < 0) {
  204. row_acc_init_rav_b += row_acc_init_rav;
  205. row_acc_init_rav *= 2;
  206. }
  207. dev_dbg(dev, "vs config(RAV): src_h = %d, dst_h = %d, factor = %d, acc_init = %08x, acc_init_b = %08x\n",
  208. src_h, dst_h, factor, row_acc_init_rav,
  209. row_acc_init_rav_b);
  210. } else {
  211. /* use polyphase */
  212. row_acc_inc = ((src_h - 1) << 16) / (dst_h - 1);
  213. row_acc_offset = 0;
  214. row_acc_offset_b = 0;
  215. dev_dbg(dev, "vs config(POLY): src_h = %d, dst_h = %d,row_acc_inc = %08x\n",
  216. src_h, dst_h, row_acc_inc);
  217. }
  218. sc_reg0[0] = val;
  219. sc_reg0[1] = row_acc_inc;
  220. sc_reg0[2] = row_acc_offset;
  221. sc_reg0[3] = row_acc_offset_b;
  222. sc_reg0[4] = ((lin_acc_inc_u & CFG_LIN_ACC_INC_U_MASK) <<
  223. CFG_LIN_ACC_INC_U_SHIFT) | (dst_w << CFG_TAR_W_SHIFT) |
  224. (dst_h << CFG_TAR_H_SHIFT);
  225. sc_reg0[5] = (src_w << CFG_SRC_W_SHIFT) | (src_h << CFG_SRC_H_SHIFT);
  226. sc_reg0[6] = (row_acc_init_rav_b << CFG_ROW_ACC_INIT_RAV_B_SHIFT) |
  227. (row_acc_init_rav << CFG_ROW_ACC_INIT_RAV_SHIFT);
  228. *sc_reg9 = lin_acc_inc;
  229. *sc_reg12 = col_acc_offset << CFG_COL_ACC_OFFSET_SHIFT;
  230. *sc_reg13 = factor;
  231. *sc_reg24 = (src_w << CFG_ORG_W_SHIFT) | (src_h << CFG_ORG_H_SHIFT);
  232. }
  233. struct sc_data *sc_create(struct platform_device *pdev)
  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, "sc");
  244. if (!sc->res) {
  245. dev_err(&pdev->dev, "missing platform resources data\n");
  246. return ERR_PTR(-ENODEV);
  247. }
  248. sc->base = devm_ioremap_resource(&pdev->dev, sc->res);
  249. if (IS_ERR(sc->base)) {
  250. dev_err(&pdev->dev, "failed to ioremap\n");
  251. return ERR_CAST(sc->base);
  252. }
  253. return sc;
  254. }