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