saa7146_hlp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. #include <linux/kernel.h>
  2. #include <media/saa7146_vv.h>
  3. static void calculate_output_format_register(struct saa7146_dev* saa, u32 palette, u32* clip_format)
  4. {
  5. /* clear out the necessary bits */
  6. *clip_format &= 0x0000ffff;
  7. /* set these bits new */
  8. *clip_format |= (( ((palette&0xf00)>>8) << 30) | ((palette&0x00f) << 24) | (((palette&0x0f0)>>4) << 16));
  9. }
  10. static void calculate_hps_source_and_sync(struct saa7146_dev *dev, int source, int sync, u32* hps_ctrl)
  11. {
  12. *hps_ctrl &= ~(MASK_30 | MASK_31 | MASK_28);
  13. *hps_ctrl |= (source << 30) | (sync << 28);
  14. }
  15. static void calculate_hxo_and_hyo(struct saa7146_vv *vv, u32* hps_h_scale, u32* hps_ctrl)
  16. {
  17. int hyo = 0, hxo = 0;
  18. hyo = vv->standard->v_offset;
  19. hxo = vv->standard->h_offset;
  20. *hps_h_scale &= ~(MASK_B0 | 0xf00);
  21. *hps_h_scale |= (hxo << 0);
  22. *hps_ctrl &= ~(MASK_W0 | MASK_B2);
  23. *hps_ctrl |= (hyo << 12);
  24. }
  25. /* helper functions for the calculation of the horizontal- and vertical
  26. scaling registers, clip-format-register etc ...
  27. these functions take pointers to the (most-likely read-out
  28. original-values) and manipulate them according to the requested
  29. changes.
  30. */
  31. /* hps_coeff used for CXY and CXUV; scale 1/1 -> scale 1/64 */
  32. static struct {
  33. u16 hps_coeff;
  34. u16 weight_sum;
  35. } hps_h_coeff_tab [] = {
  36. {0x00, 2}, {0x02, 4}, {0x00, 4}, {0x06, 8}, {0x02, 8},
  37. {0x08, 8}, {0x00, 8}, {0x1E, 16}, {0x0E, 8}, {0x26, 8},
  38. {0x06, 8}, {0x42, 8}, {0x02, 8}, {0x80, 8}, {0x00, 8},
  39. {0xFE, 16}, {0xFE, 8}, {0x7E, 8}, {0x7E, 8}, {0x3E, 8},
  40. {0x3E, 8}, {0x1E, 8}, {0x1E, 8}, {0x0E, 8}, {0x0E, 8},
  41. {0x06, 8}, {0x06, 8}, {0x02, 8}, {0x02, 8}, {0x00, 8},
  42. {0x00, 8}, {0xFE, 16}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8},
  43. {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8},
  44. {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8},
  45. {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0x7E, 8},
  46. {0x7E, 8}, {0x3E, 8}, {0x3E, 8}, {0x1E, 8}, {0x1E, 8},
  47. {0x0E, 8}, {0x0E, 8}, {0x06, 8}, {0x06, 8}, {0x02, 8},
  48. {0x02, 8}, {0x00, 8}, {0x00, 8}, {0xFE, 16}
  49. };
  50. /* table of attenuation values for horizontal scaling */
  51. static u8 h_attenuation[] = { 1, 2, 4, 8, 2, 4, 8, 16, 0};
  52. /* calculate horizontal scale registers */
  53. static int calculate_h_scale_registers(struct saa7146_dev *dev,
  54. int in_x, int out_x, int flip_lr,
  55. u32* hps_ctrl, u32* hps_v_gain, u32* hps_h_prescale, u32* hps_h_scale)
  56. {
  57. /* horizontal prescaler */
  58. u32 dcgx = 0, xpsc = 0, xacm = 0, cxy = 0, cxuv = 0;
  59. /* horizontal scaler */
  60. u32 xim = 0, xp = 0, xsci =0;
  61. /* vertical scale & gain */
  62. u32 pfuv = 0;
  63. /* helper variables */
  64. u32 h_atten = 0, i = 0;
  65. if ( 0 == out_x ) {
  66. return -EINVAL;
  67. }
  68. /* mask out vanity-bit */
  69. *hps_ctrl &= ~MASK_29;
  70. /* calculate prescale-(xspc)-value: [n .. 1/2) : 1
  71. [1/2 .. 1/3) : 2
  72. [1/3 .. 1/4) : 3
  73. ... */
  74. if (in_x > out_x) {
  75. xpsc = in_x / out_x;
  76. }
  77. else {
  78. /* zooming */
  79. xpsc = 1;
  80. }
  81. /* if flip_lr-bit is set, number of pixels after
  82. horizontal prescaling must be < 384 */
  83. if ( 0 != flip_lr ) {
  84. /* set vanity bit */
  85. *hps_ctrl |= MASK_29;
  86. while (in_x / xpsc >= 384 )
  87. xpsc++;
  88. }
  89. /* if zooming is wanted, number of pixels after
  90. horizontal prescaling must be < 768 */
  91. else {
  92. while ( in_x / xpsc >= 768 )
  93. xpsc++;
  94. }
  95. /* maximum prescale is 64 (p.69) */
  96. if ( xpsc > 64 )
  97. xpsc = 64;
  98. /* keep xacm clear*/
  99. xacm = 0;
  100. /* set horizontal filter parameters (CXY = CXUV) */
  101. cxy = hps_h_coeff_tab[( (xpsc - 1) < 63 ? (xpsc - 1) : 63 )].hps_coeff;
  102. cxuv = cxy;
  103. /* calculate and set horizontal fine scale (xsci) */
  104. /* bypass the horizontal scaler ? */
  105. if ( (in_x == out_x) && ( 1 == xpsc ) )
  106. xsci = 0x400;
  107. else
  108. xsci = ( (1024 * in_x) / (out_x * xpsc) ) + xpsc;
  109. /* set start phase for horizontal fine scale (xp) to 0 */
  110. xp = 0;
  111. /* set xim, if we bypass the horizontal scaler */
  112. if ( 0x400 == xsci )
  113. xim = 1;
  114. else
  115. xim = 0;
  116. /* if the prescaler is bypassed, enable horizontal
  117. accumulation mode (xacm) and clear dcgx */
  118. if( 1 == xpsc ) {
  119. xacm = 1;
  120. dcgx = 0;
  121. } else {
  122. xacm = 0;
  123. /* get best match in the table of attenuations
  124. for horizontal scaling */
  125. h_atten = hps_h_coeff_tab[( (xpsc - 1) < 63 ? (xpsc - 1) : 63 )].weight_sum;
  126. for (i = 0; h_attenuation[i] != 0; i++) {
  127. if (h_attenuation[i] >= h_atten)
  128. break;
  129. }
  130. dcgx = i;
  131. }
  132. /* the horizontal scaling increment controls the UV filter
  133. to reduce the bandwith to improve the display quality,
  134. so set it ... */
  135. if ( xsci == 0x400)
  136. pfuv = 0x00;
  137. else if ( xsci < 0x600)
  138. pfuv = 0x01;
  139. else if ( xsci < 0x680)
  140. pfuv = 0x11;
  141. else if ( xsci < 0x700)
  142. pfuv = 0x22;
  143. else
  144. pfuv = 0x33;
  145. *hps_v_gain &= MASK_W0|MASK_B2;
  146. *hps_v_gain |= (pfuv << 24);
  147. *hps_h_scale &= ~(MASK_W1 | 0xf000);
  148. *hps_h_scale |= (xim << 31) | (xp << 24) | (xsci << 12);
  149. *hps_h_prescale |= (dcgx << 27) | ((xpsc-1) << 18) | (xacm << 17) | (cxy << 8) | (cxuv << 0);
  150. return 0;
  151. }
  152. static struct {
  153. u16 hps_coeff;
  154. u16 weight_sum;
  155. } hps_v_coeff_tab [] = {
  156. {0x0100, 2}, {0x0102, 4}, {0x0300, 4}, {0x0106, 8}, {0x0502, 8},
  157. {0x0708, 8}, {0x0F00, 8}, {0x011E, 16}, {0x110E, 16}, {0x1926, 16},
  158. {0x3906, 16}, {0x3D42, 16}, {0x7D02, 16}, {0x7F80, 16}, {0xFF00, 16},
  159. {0x01FE, 32}, {0x01FE, 32}, {0x817E, 32}, {0x817E, 32}, {0xC13E, 32},
  160. {0xC13E, 32}, {0xE11E, 32}, {0xE11E, 32}, {0xF10E, 32}, {0xF10E, 32},
  161. {0xF906, 32}, {0xF906, 32}, {0xFD02, 32}, {0xFD02, 32}, {0xFF00, 32},
  162. {0xFF00, 32}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64},
  163. {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64},
  164. {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64},
  165. {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x817E, 64},
  166. {0x817E, 64}, {0xC13E, 64}, {0xC13E, 64}, {0xE11E, 64}, {0xE11E, 64},
  167. {0xF10E, 64}, {0xF10E, 64}, {0xF906, 64}, {0xF906, 64}, {0xFD02, 64},
  168. {0xFD02, 64}, {0xFF00, 64}, {0xFF00, 64}, {0x01FE, 128}
  169. };
  170. /* table of attenuation values for vertical scaling */
  171. static u16 v_attenuation[] = { 2, 4, 8, 16, 32, 64, 128, 256, 0};
  172. /* calculate vertical scale registers */
  173. static int calculate_v_scale_registers(struct saa7146_dev *dev, enum v4l2_field field,
  174. int in_y, int out_y, u32* hps_v_scale, u32* hps_v_gain)
  175. {
  176. int lpi = 0;
  177. /* vertical scaling */
  178. u32 yacm = 0, ysci = 0, yacl = 0, ypo = 0, ype = 0;
  179. /* vertical scale & gain */
  180. u32 dcgy = 0, cya_cyb = 0;
  181. /* helper variables */
  182. u32 v_atten = 0, i = 0;
  183. /* error, if vertical zooming */
  184. if ( in_y < out_y ) {
  185. return -EINVAL;
  186. }
  187. /* linear phase interpolation may be used
  188. if scaling is between 1 and 1/2 (both fields used)
  189. or scaling is between 1/2 and 1/4 (if only one field is used) */
  190. if (V4L2_FIELD_HAS_BOTH(field)) {
  191. if( 2*out_y >= in_y) {
  192. lpi = 1;
  193. }
  194. } else if (field == V4L2_FIELD_TOP
  195. || field == V4L2_FIELD_ALTERNATE
  196. || field == V4L2_FIELD_BOTTOM) {
  197. if( 4*out_y >= in_y ) {
  198. lpi = 1;
  199. }
  200. out_y *= 2;
  201. }
  202. if( 0 != lpi ) {
  203. yacm = 0;
  204. yacl = 0;
  205. cya_cyb = 0x00ff;
  206. /* calculate scaling increment */
  207. if ( in_y > out_y )
  208. ysci = ((1024 * in_y) / (out_y + 1)) - 1024;
  209. else
  210. ysci = 0;
  211. dcgy = 0;
  212. /* calculate ype and ypo */
  213. ype = ysci / 16;
  214. ypo = ype + (ysci / 64);
  215. } else {
  216. yacm = 1;
  217. /* calculate scaling increment */
  218. ysci = (((10 * 1024 * (in_y - out_y - 1)) / in_y) + 9) / 10;
  219. /* calculate ype and ypo */
  220. ypo = ype = ((ysci + 15) / 16);
  221. /* the sequence length interval (yacl) has to be set according
  222. to the prescale value, e.g. [n .. 1/2) : 0
  223. [1/2 .. 1/3) : 1
  224. [1/3 .. 1/4) : 2
  225. ... */
  226. if ( ysci < 512) {
  227. yacl = 0;
  228. } else {
  229. yacl = ( ysci / (1024 - ysci) );
  230. }
  231. /* get filter coefficients for cya, cyb from table hps_v_coeff_tab */
  232. cya_cyb = hps_v_coeff_tab[ (yacl < 63 ? yacl : 63 ) ].hps_coeff;
  233. /* get best match in the table of attenuations for vertical scaling */
  234. v_atten = hps_v_coeff_tab[ (yacl < 63 ? yacl : 63 ) ].weight_sum;
  235. for (i = 0; v_attenuation[i] != 0; i++) {
  236. if (v_attenuation[i] >= v_atten)
  237. break;
  238. }
  239. dcgy = i;
  240. }
  241. /* ypo and ype swapped in spec ? */
  242. *hps_v_scale |= (yacm << 31) | (ysci << 21) | (yacl << 15) | (ypo << 8 ) | (ype << 1);
  243. *hps_v_gain &= ~(MASK_W0|MASK_B2);
  244. *hps_v_gain |= (dcgy << 16) | (cya_cyb << 0);
  245. return 0;
  246. }
  247. /* simple bubble-sort algorithm with duplicate elimination */
  248. static int sort_and_eliminate(u32* values, int* count)
  249. {
  250. int low = 0, high = 0, top = 0, temp = 0;
  251. int cur = 0, next = 0;
  252. /* sanity checks */
  253. if( (0 > *count) || (NULL == values) ) {
  254. return -EINVAL;
  255. }
  256. /* bubble sort the first ´count´ items of the array ´values´ */
  257. for( top = *count; top > 0; top--) {
  258. for( low = 0, high = 1; high < top; low++, high++) {
  259. if( values[low] > values[high] ) {
  260. temp = values[low];
  261. values[low] = values[high];
  262. values[high] = temp;
  263. }
  264. }
  265. }
  266. /* remove duplicate items */
  267. for( cur = 0, next = 1; next < *count; next++) {
  268. if( values[cur] != values[next])
  269. values[++cur] = values[next];
  270. }
  271. *count = cur + 1;
  272. return 0;
  273. }
  274. static void calculate_clipping_registers_rect(struct saa7146_dev *dev, struct saa7146_fh *fh,
  275. struct saa7146_video_dma *vdma2, u32* clip_format, u32* arbtr_ctrl, enum v4l2_field field)
  276. {
  277. struct saa7146_vv *vv = dev->vv_data;
  278. u32 *clipping = vv->d_clipping.cpu_addr;
  279. int width = fh->ov.win.w.width;
  280. int height = fh->ov.win.w.height;
  281. int clipcount = fh->ov.nclips;
  282. u32 line_list[32];
  283. u32 pixel_list[32];
  284. int numdwords = 0;
  285. int i = 0, j = 0;
  286. int cnt_line = 0, cnt_pixel = 0;
  287. int x[32], y[32], w[32], h[32];
  288. /* clear out memory */
  289. memset(&line_list[0], 0x00, sizeof(u32)*32);
  290. memset(&pixel_list[0], 0x00, sizeof(u32)*32);
  291. memset(clipping, 0x00, SAA7146_CLIPPING_MEM);
  292. /* fill the line and pixel-lists */
  293. for(i = 0; i < clipcount; i++) {
  294. int l = 0, r = 0, t = 0, b = 0;
  295. x[i] = fh->ov.clips[i].c.left;
  296. y[i] = fh->ov.clips[i].c.top;
  297. w[i] = fh->ov.clips[i].c.width;
  298. h[i] = fh->ov.clips[i].c.height;
  299. if( w[i] < 0) {
  300. x[i] += w[i]; w[i] = -w[i];
  301. }
  302. if( h[i] < 0) {
  303. y[i] += h[i]; h[i] = -h[i];
  304. }
  305. if( x[i] < 0) {
  306. w[i] += x[i]; x[i] = 0;
  307. }
  308. if( y[i] < 0) {
  309. h[i] += y[i]; y[i] = 0;
  310. }
  311. if( 0 != vv->vflip ) {
  312. y[i] = height - y[i] - h[i];
  313. }
  314. l = x[i];
  315. r = x[i]+w[i];
  316. t = y[i];
  317. b = y[i]+h[i];
  318. /* insert left/right coordinates */
  319. pixel_list[ 2*i ] = min_t(int, l, width);
  320. pixel_list[(2*i)+1] = min_t(int, r, width);
  321. /* insert top/bottom coordinates */
  322. line_list[ 2*i ] = min_t(int, t, height);
  323. line_list[(2*i)+1] = min_t(int, b, height);
  324. }
  325. /* sort and eliminate lists */
  326. cnt_line = cnt_pixel = 2*clipcount;
  327. sort_and_eliminate( &pixel_list[0], &cnt_pixel );
  328. sort_and_eliminate( &line_list[0], &cnt_line );
  329. /* calculate the number of used u32s */
  330. numdwords = max_t(int, (cnt_line+1), (cnt_pixel+1))*2;
  331. numdwords = max_t(int, 4, numdwords);
  332. numdwords = min_t(int, 64, numdwords);
  333. /* fill up cliptable */
  334. for(i = 0; i < cnt_pixel; i++) {
  335. clipping[2*i] |= cpu_to_le32(pixel_list[i] << 16);
  336. }
  337. for(i = 0; i < cnt_line; i++) {
  338. clipping[(2*i)+1] |= cpu_to_le32(line_list[i] << 16);
  339. }
  340. /* fill up cliptable with the display infos */
  341. for(j = 0; j < clipcount; j++) {
  342. for(i = 0; i < cnt_pixel; i++) {
  343. if( x[j] < 0)
  344. x[j] = 0;
  345. if( pixel_list[i] < (x[j] + w[j])) {
  346. if ( pixel_list[i] >= x[j] ) {
  347. clipping[2*i] |= cpu_to_le32(1 << j);
  348. }
  349. }
  350. }
  351. for(i = 0; i < cnt_line; i++) {
  352. if( y[j] < 0)
  353. y[j] = 0;
  354. if( line_list[i] < (y[j] + h[j]) ) {
  355. if( line_list[i] >= y[j] ) {
  356. clipping[(2*i)+1] |= cpu_to_le32(1 << j);
  357. }
  358. }
  359. }
  360. }
  361. /* adjust arbitration control register */
  362. *arbtr_ctrl &= 0xffff00ff;
  363. *arbtr_ctrl |= 0x00001c00;
  364. vdma2->base_even = vv->d_clipping.dma_handle;
  365. vdma2->base_odd = vv->d_clipping.dma_handle;
  366. vdma2->prot_addr = vv->d_clipping.dma_handle+((sizeof(u32))*(numdwords));
  367. vdma2->base_page = 0x04;
  368. vdma2->pitch = 0x00;
  369. vdma2->num_line_byte = (0 << 16 | (sizeof(u32))*(numdwords-1) );
  370. /* set clipping-mode. this depends on the field(s) used */
  371. *clip_format &= 0xfffffff7;
  372. if (V4L2_FIELD_HAS_BOTH(field)) {
  373. *clip_format |= 0x00000008;
  374. } else {
  375. *clip_format |= 0x00000000;
  376. }
  377. }
  378. /* disable clipping */
  379. static void saa7146_disable_clipping(struct saa7146_dev *dev)
  380. {
  381. u32 clip_format = saa7146_read(dev, CLIP_FORMAT_CTRL);
  382. /* mask out relevant bits (=lower word)*/
  383. clip_format &= MASK_W1;
  384. /* upload clipping-registers*/
  385. saa7146_write(dev, CLIP_FORMAT_CTRL,clip_format);
  386. saa7146_write(dev, MC2, (MASK_05 | MASK_21));
  387. /* disable video dma2 */
  388. saa7146_write(dev, MC1, MASK_21);
  389. }
  390. static void saa7146_set_clipping_rect(struct saa7146_fh *fh)
  391. {
  392. struct saa7146_dev *dev = fh->dev;
  393. enum v4l2_field field = fh->ov.win.field;
  394. struct saa7146_video_dma vdma2;
  395. u32 clip_format;
  396. u32 arbtr_ctrl;
  397. /* check clipcount, disable clipping if clipcount == 0*/
  398. if( fh->ov.nclips == 0 ) {
  399. saa7146_disable_clipping(dev);
  400. return;
  401. }
  402. clip_format = saa7146_read(dev, CLIP_FORMAT_CTRL);
  403. arbtr_ctrl = saa7146_read(dev, PCI_BT_V1);
  404. calculate_clipping_registers_rect(dev, fh, &vdma2, &clip_format, &arbtr_ctrl, field);
  405. /* set clipping format */
  406. clip_format &= 0xffff0008;
  407. clip_format |= (SAA7146_CLIPPING_RECT << 4);
  408. /* prepare video dma2 */
  409. saa7146_write(dev, BASE_EVEN2, vdma2.base_even);
  410. saa7146_write(dev, BASE_ODD2, vdma2.base_odd);
  411. saa7146_write(dev, PROT_ADDR2, vdma2.prot_addr);
  412. saa7146_write(dev, BASE_PAGE2, vdma2.base_page);
  413. saa7146_write(dev, PITCH2, vdma2.pitch);
  414. saa7146_write(dev, NUM_LINE_BYTE2, vdma2.num_line_byte);
  415. /* prepare the rest */
  416. saa7146_write(dev, CLIP_FORMAT_CTRL,clip_format);
  417. saa7146_write(dev, PCI_BT_V1, arbtr_ctrl);
  418. /* upload clip_control-register, clipping-registers, enable video dma2 */
  419. saa7146_write(dev, MC2, (MASK_05 | MASK_21 | MASK_03 | MASK_19));
  420. saa7146_write(dev, MC1, (MASK_05 | MASK_21));
  421. }
  422. static void saa7146_set_window(struct saa7146_dev *dev, int width, int height, enum v4l2_field field)
  423. {
  424. struct saa7146_vv *vv = dev->vv_data;
  425. int source = vv->current_hps_source;
  426. int sync = vv->current_hps_sync;
  427. u32 hps_v_scale = 0, hps_v_gain = 0, hps_ctrl = 0, hps_h_prescale = 0, hps_h_scale = 0;
  428. /* set vertical scale */
  429. hps_v_scale = 0; /* all bits get set by the function-call */
  430. hps_v_gain = 0; /* fixme: saa7146_read(dev, HPS_V_GAIN);*/
  431. calculate_v_scale_registers(dev, field, vv->standard->v_field*2, height, &hps_v_scale, &hps_v_gain);
  432. /* set horizontal scale */
  433. hps_ctrl = 0;
  434. hps_h_prescale = 0; /* all bits get set in the function */
  435. hps_h_scale = 0;
  436. calculate_h_scale_registers(dev, vv->standard->h_pixels, width, vv->hflip, &hps_ctrl, &hps_v_gain, &hps_h_prescale, &hps_h_scale);
  437. /* set hyo and hxo */
  438. calculate_hxo_and_hyo(vv, &hps_h_scale, &hps_ctrl);
  439. calculate_hps_source_and_sync(dev, source, sync, &hps_ctrl);
  440. /* write out new register contents */
  441. saa7146_write(dev, HPS_V_SCALE, hps_v_scale);
  442. saa7146_write(dev, HPS_V_GAIN, hps_v_gain);
  443. saa7146_write(dev, HPS_CTRL, hps_ctrl);
  444. saa7146_write(dev, HPS_H_PRESCALE,hps_h_prescale);
  445. saa7146_write(dev, HPS_H_SCALE, hps_h_scale);
  446. /* upload shadow-ram registers */
  447. saa7146_write(dev, MC2, (MASK_05 | MASK_06 | MASK_21 | MASK_22) );
  448. }
  449. /* calculate the new memory offsets for a desired position */
  450. static void saa7146_set_position(struct saa7146_dev *dev, int w_x, int w_y, int w_height, enum v4l2_field field, u32 pixelformat)
  451. {
  452. struct saa7146_vv *vv = dev->vv_data;
  453. struct saa7146_format *sfmt = format_by_fourcc(dev, pixelformat);
  454. int b_depth = vv->ov_fmt->depth;
  455. int b_bpl = vv->ov_fb.fmt.bytesperline;
  456. u32 base = (u32)vv->ov_fb.base;
  457. struct saa7146_video_dma vdma1;
  458. /* calculate memory offsets for picture, look if we shall top-down-flip */
  459. vdma1.pitch = 2*b_bpl;
  460. if ( 0 == vv->vflip ) {
  461. vdma1.base_even = (u32)base + (w_y * (vdma1.pitch/2)) + (w_x * (b_depth / 8));
  462. vdma1.base_odd = vdma1.base_even + (vdma1.pitch / 2);
  463. vdma1.prot_addr = vdma1.base_even + (w_height * (vdma1.pitch / 2));
  464. }
  465. else {
  466. vdma1.base_even = (u32)base + ((w_y+w_height) * (vdma1.pitch/2)) + (w_x * (b_depth / 8));
  467. vdma1.base_odd = vdma1.base_even - (vdma1.pitch / 2);
  468. vdma1.prot_addr = vdma1.base_odd - (w_height * (vdma1.pitch / 2));
  469. }
  470. if (V4L2_FIELD_HAS_BOTH(field)) {
  471. } else if (field == V4L2_FIELD_ALTERNATE) {
  472. /* fixme */
  473. vdma1.base_odd = vdma1.prot_addr;
  474. vdma1.pitch /= 2;
  475. } else if (field == V4L2_FIELD_TOP) {
  476. vdma1.base_odd = vdma1.prot_addr;
  477. vdma1.pitch /= 2;
  478. } else if (field == V4L2_FIELD_BOTTOM) {
  479. vdma1.base_odd = vdma1.base_even;
  480. vdma1.base_even = vdma1.prot_addr;
  481. vdma1.pitch /= 2;
  482. }
  483. if ( 0 != vv->vflip ) {
  484. vdma1.pitch *= -1;
  485. }
  486. vdma1.base_page = sfmt->swap;
  487. vdma1.num_line_byte = (vv->standard->v_field<<16)+vv->standard->h_pixels;
  488. saa7146_write_out_dma(dev, 1, &vdma1);
  489. }
  490. static void saa7146_set_output_format(struct saa7146_dev *dev, unsigned long palette)
  491. {
  492. u32 clip_format = saa7146_read(dev, CLIP_FORMAT_CTRL);
  493. /* call helper function */
  494. calculate_output_format_register(dev,palette,&clip_format);
  495. /* update the hps registers */
  496. saa7146_write(dev, CLIP_FORMAT_CTRL, clip_format);
  497. saa7146_write(dev, MC2, (MASK_05 | MASK_21));
  498. }
  499. /* select input-source */
  500. void saa7146_set_hps_source_and_sync(struct saa7146_dev *dev, int source, int sync)
  501. {
  502. struct saa7146_vv *vv = dev->vv_data;
  503. u32 hps_ctrl = 0;
  504. /* read old state */
  505. hps_ctrl = saa7146_read(dev, HPS_CTRL);
  506. hps_ctrl &= ~( MASK_31 | MASK_30 | MASK_28 );
  507. hps_ctrl |= (source << 30) | (sync << 28);
  508. /* write back & upload register */
  509. saa7146_write(dev, HPS_CTRL, hps_ctrl);
  510. saa7146_write(dev, MC2, (MASK_05 | MASK_21));
  511. vv->current_hps_source = source;
  512. vv->current_hps_sync = sync;
  513. }
  514. int saa7146_enable_overlay(struct saa7146_fh *fh)
  515. {
  516. struct saa7146_dev *dev = fh->dev;
  517. struct saa7146_vv *vv = dev->vv_data;
  518. saa7146_set_window(dev, fh->ov.win.w.width, fh->ov.win.w.height, fh->ov.win.field);
  519. saa7146_set_position(dev, fh->ov.win.w.left, fh->ov.win.w.top, fh->ov.win.w.height, fh->ov.win.field, vv->ov_fmt->pixelformat);
  520. saa7146_set_output_format(dev, vv->ov_fmt->trans);
  521. saa7146_set_clipping_rect(fh);
  522. /* enable video dma1 */
  523. saa7146_write(dev, MC1, (MASK_06 | MASK_22));
  524. return 0;
  525. }
  526. void saa7146_disable_overlay(struct saa7146_fh *fh)
  527. {
  528. struct saa7146_dev *dev = fh->dev;
  529. /* disable clipping + video dma1 */
  530. saa7146_disable_clipping(dev);
  531. saa7146_write(dev, MC1, MASK_22);
  532. }
  533. void saa7146_write_out_dma(struct saa7146_dev* dev, int which, struct saa7146_video_dma* vdma)
  534. {
  535. int where = 0;
  536. if( which < 1 || which > 3) {
  537. return;
  538. }
  539. /* calculate starting address */
  540. where = (which-1)*0x18;
  541. saa7146_write(dev, where, vdma->base_odd);
  542. saa7146_write(dev, where+0x04, vdma->base_even);
  543. saa7146_write(dev, where+0x08, vdma->prot_addr);
  544. saa7146_write(dev, where+0x0c, vdma->pitch);
  545. saa7146_write(dev, where+0x10, vdma->base_page);
  546. saa7146_write(dev, where+0x14, vdma->num_line_byte);
  547. /* upload */
  548. saa7146_write(dev, MC2, (MASK_02<<(which-1))|(MASK_18<<(which-1)));
  549. /*
  550. printk("vdma%d.base_even: 0x%08x\n", which,vdma->base_even);
  551. printk("vdma%d.base_odd: 0x%08x\n", which,vdma->base_odd);
  552. printk("vdma%d.prot_addr: 0x%08x\n", which,vdma->prot_addr);
  553. printk("vdma%d.base_page: 0x%08x\n", which,vdma->base_page);
  554. printk("vdma%d.pitch: 0x%08x\n", which,vdma->pitch);
  555. printk("vdma%d.num_line_byte: 0x%08x\n", which,vdma->num_line_byte);
  556. */
  557. }
  558. static int calculate_video_dma_grab_packed(struct saa7146_dev* dev, struct saa7146_buf *buf)
  559. {
  560. struct saa7146_vv *vv = dev->vv_data;
  561. struct saa7146_video_dma vdma1;
  562. struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
  563. int width = buf->fmt->width;
  564. int height = buf->fmt->height;
  565. int bytesperline = buf->fmt->bytesperline;
  566. enum v4l2_field field = buf->fmt->field;
  567. int depth = sfmt->depth;
  568. DEB_CAP(("[size=%dx%d,fields=%s]\n",
  569. width,height,v4l2_field_names[field]));
  570. if( bytesperline != 0) {
  571. vdma1.pitch = bytesperline*2;
  572. } else {
  573. vdma1.pitch = (width*depth*2)/8;
  574. }
  575. vdma1.num_line_byte = ((vv->standard->v_field<<16) + vv->standard->h_pixels);
  576. vdma1.base_page = buf->pt[0].dma | ME1 | sfmt->swap;
  577. if( 0 != vv->vflip ) {
  578. vdma1.prot_addr = buf->pt[0].offset;
  579. vdma1.base_even = buf->pt[0].offset+(vdma1.pitch/2)*height;
  580. vdma1.base_odd = vdma1.base_even - (vdma1.pitch/2);
  581. } else {
  582. vdma1.base_even = buf->pt[0].offset;
  583. vdma1.base_odd = vdma1.base_even + (vdma1.pitch/2);
  584. vdma1.prot_addr = buf->pt[0].offset+(vdma1.pitch/2)*height;
  585. }
  586. if (V4L2_FIELD_HAS_BOTH(field)) {
  587. } else if (field == V4L2_FIELD_ALTERNATE) {
  588. /* fixme */
  589. if ( vv->last_field == V4L2_FIELD_TOP ) {
  590. vdma1.base_odd = vdma1.prot_addr;
  591. vdma1.pitch /= 2;
  592. } else if ( vv->last_field == V4L2_FIELD_BOTTOM ) {
  593. vdma1.base_odd = vdma1.base_even;
  594. vdma1.base_even = vdma1.prot_addr;
  595. vdma1.pitch /= 2;
  596. }
  597. } else if (field == V4L2_FIELD_TOP) {
  598. vdma1.base_odd = vdma1.prot_addr;
  599. vdma1.pitch /= 2;
  600. } else if (field == V4L2_FIELD_BOTTOM) {
  601. vdma1.base_odd = vdma1.base_even;
  602. vdma1.base_even = vdma1.prot_addr;
  603. vdma1.pitch /= 2;
  604. }
  605. if( 0 != vv->vflip ) {
  606. vdma1.pitch *= -1;
  607. }
  608. saa7146_write_out_dma(dev, 1, &vdma1);
  609. return 0;
  610. }
  611. static int calc_planar_422(struct saa7146_vv *vv, struct saa7146_buf *buf, struct saa7146_video_dma *vdma2, struct saa7146_video_dma *vdma3)
  612. {
  613. int height = buf->fmt->height;
  614. int width = buf->fmt->width;
  615. vdma2->pitch = width;
  616. vdma3->pitch = width;
  617. /* fixme: look at bytesperline! */
  618. if( 0 != vv->vflip ) {
  619. vdma2->prot_addr = buf->pt[1].offset;
  620. vdma2->base_even = ((vdma2->pitch/2)*height)+buf->pt[1].offset;
  621. vdma2->base_odd = vdma2->base_even - (vdma2->pitch/2);
  622. vdma3->prot_addr = buf->pt[2].offset;
  623. vdma3->base_even = ((vdma3->pitch/2)*height)+buf->pt[2].offset;
  624. vdma3->base_odd = vdma3->base_even - (vdma3->pitch/2);
  625. } else {
  626. vdma3->base_even = buf->pt[2].offset;
  627. vdma3->base_odd = vdma3->base_even + (vdma3->pitch/2);
  628. vdma3->prot_addr = (vdma3->pitch/2)*height+buf->pt[2].offset;
  629. vdma2->base_even = buf->pt[1].offset;
  630. vdma2->base_odd = vdma2->base_even + (vdma2->pitch/2);
  631. vdma2->prot_addr = (vdma2->pitch/2)*height+buf->pt[1].offset;
  632. }
  633. return 0;
  634. }
  635. static int calc_planar_420(struct saa7146_vv *vv, struct saa7146_buf *buf, struct saa7146_video_dma *vdma2, struct saa7146_video_dma *vdma3)
  636. {
  637. int height = buf->fmt->height;
  638. int width = buf->fmt->width;
  639. vdma2->pitch = width/2;
  640. vdma3->pitch = width/2;
  641. if( 0 != vv->vflip ) {
  642. vdma2->prot_addr = buf->pt[2].offset;
  643. vdma2->base_even = ((vdma2->pitch/2)*height)+buf->pt[2].offset;
  644. vdma2->base_odd = vdma2->base_even - (vdma2->pitch/2);
  645. vdma3->prot_addr = buf->pt[1].offset;
  646. vdma3->base_even = ((vdma3->pitch/2)*height)+buf->pt[1].offset;
  647. vdma3->base_odd = vdma3->base_even - (vdma3->pitch/2);
  648. } else {
  649. vdma3->base_even = buf->pt[2].offset;
  650. vdma3->base_odd = vdma3->base_even + (vdma3->pitch);
  651. vdma3->prot_addr = (vdma3->pitch/2)*height+buf->pt[2].offset;
  652. vdma2->base_even = buf->pt[1].offset;
  653. vdma2->base_odd = vdma2->base_even + (vdma2->pitch);
  654. vdma2->prot_addr = (vdma2->pitch/2)*height+buf->pt[1].offset;
  655. }
  656. return 0;
  657. }
  658. static int calculate_video_dma_grab_planar(struct saa7146_dev* dev, struct saa7146_buf *buf)
  659. {
  660. struct saa7146_vv *vv = dev->vv_data;
  661. struct saa7146_video_dma vdma1;
  662. struct saa7146_video_dma vdma2;
  663. struct saa7146_video_dma vdma3;
  664. struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
  665. int width = buf->fmt->width;
  666. int height = buf->fmt->height;
  667. enum v4l2_field field = buf->fmt->field;
  668. BUG_ON(0 == buf->pt[0].dma);
  669. BUG_ON(0 == buf->pt[1].dma);
  670. BUG_ON(0 == buf->pt[2].dma);
  671. DEB_CAP(("[size=%dx%d,fields=%s]\n",
  672. width,height,v4l2_field_names[field]));
  673. /* fixme: look at bytesperline! */
  674. /* fixme: what happens for user space buffers here?. The offsets are
  675. most likely wrong, this version here only works for page-aligned
  676. buffers, modifications to the pagetable-functions are necessary...*/
  677. vdma1.pitch = width*2;
  678. vdma1.num_line_byte = ((vv->standard->v_field<<16) + vv->standard->h_pixels);
  679. vdma1.base_page = buf->pt[0].dma | ME1;
  680. if( 0 != vv->vflip ) {
  681. vdma1.prot_addr = buf->pt[0].offset;
  682. vdma1.base_even = ((vdma1.pitch/2)*height)+buf->pt[0].offset;
  683. vdma1.base_odd = vdma1.base_even - (vdma1.pitch/2);
  684. } else {
  685. vdma1.base_even = buf->pt[0].offset;
  686. vdma1.base_odd = vdma1.base_even + (vdma1.pitch/2);
  687. vdma1.prot_addr = (vdma1.pitch/2)*height+buf->pt[0].offset;
  688. }
  689. vdma2.num_line_byte = 0; /* unused */
  690. vdma2.base_page = buf->pt[1].dma | ME1;
  691. vdma3.num_line_byte = 0; /* unused */
  692. vdma3.base_page = buf->pt[2].dma | ME1;
  693. switch( sfmt->depth ) {
  694. case 12: {
  695. calc_planar_420(vv,buf,&vdma2,&vdma3);
  696. break;
  697. }
  698. case 16: {
  699. calc_planar_422(vv,buf,&vdma2,&vdma3);
  700. break;
  701. }
  702. default: {
  703. return -1;
  704. }
  705. }
  706. if (V4L2_FIELD_HAS_BOTH(field)) {
  707. } else if (field == V4L2_FIELD_ALTERNATE) {
  708. /* fixme */
  709. vdma1.base_odd = vdma1.prot_addr;
  710. vdma1.pitch /= 2;
  711. vdma2.base_odd = vdma2.prot_addr;
  712. vdma2.pitch /= 2;
  713. vdma3.base_odd = vdma3.prot_addr;
  714. vdma3.pitch /= 2;
  715. } else if (field == V4L2_FIELD_TOP) {
  716. vdma1.base_odd = vdma1.prot_addr;
  717. vdma1.pitch /= 2;
  718. vdma2.base_odd = vdma2.prot_addr;
  719. vdma2.pitch /= 2;
  720. vdma3.base_odd = vdma3.prot_addr;
  721. vdma3.pitch /= 2;
  722. } else if (field == V4L2_FIELD_BOTTOM) {
  723. vdma1.base_odd = vdma1.base_even;
  724. vdma1.base_even = vdma1.prot_addr;
  725. vdma1.pitch /= 2;
  726. vdma2.base_odd = vdma2.base_even;
  727. vdma2.base_even = vdma2.prot_addr;
  728. vdma2.pitch /= 2;
  729. vdma3.base_odd = vdma3.base_even;
  730. vdma3.base_even = vdma3.prot_addr;
  731. vdma3.pitch /= 2;
  732. }
  733. if( 0 != vv->vflip ) {
  734. vdma1.pitch *= -1;
  735. vdma2.pitch *= -1;
  736. vdma3.pitch *= -1;
  737. }
  738. saa7146_write_out_dma(dev, 1, &vdma1);
  739. if( (sfmt->flags & FORMAT_BYTE_SWAP) != 0 ) {
  740. saa7146_write_out_dma(dev, 3, &vdma2);
  741. saa7146_write_out_dma(dev, 2, &vdma3);
  742. } else {
  743. saa7146_write_out_dma(dev, 2, &vdma2);
  744. saa7146_write_out_dma(dev, 3, &vdma3);
  745. }
  746. return 0;
  747. }
  748. static void program_capture_engine(struct saa7146_dev *dev, int planar)
  749. {
  750. struct saa7146_vv *vv = dev->vv_data;
  751. int count = 0;
  752. unsigned long e_wait = vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? CMD_E_FID_A : CMD_E_FID_B;
  753. unsigned long o_wait = vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? CMD_O_FID_A : CMD_O_FID_B;
  754. /* wait for o_fid_a/b / e_fid_a/b toggle only if rps register 0 is not set*/
  755. WRITE_RPS0(CMD_PAUSE | CMD_OAN | CMD_SIG0 | o_wait);
  756. WRITE_RPS0(CMD_PAUSE | CMD_OAN | CMD_SIG0 | e_wait);
  757. /* set rps register 0 */
  758. WRITE_RPS0(CMD_WR_REG | (1 << 8) | (MC2/4));
  759. WRITE_RPS0(MASK_27 | MASK_11);
  760. /* turn on video-dma1 */
  761. WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));
  762. WRITE_RPS0(MASK_06 | MASK_22); /* => mask */
  763. WRITE_RPS0(MASK_06 | MASK_22); /* => values */
  764. if( 0 != planar ) {
  765. /* turn on video-dma2 */
  766. WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));
  767. WRITE_RPS0(MASK_05 | MASK_21); /* => mask */
  768. WRITE_RPS0(MASK_05 | MASK_21); /* => values */
  769. /* turn on video-dma3 */
  770. WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));
  771. WRITE_RPS0(MASK_04 | MASK_20); /* => mask */
  772. WRITE_RPS0(MASK_04 | MASK_20); /* => values */
  773. }
  774. /* wait for o_fid_a/b / e_fid_a/b toggle */
  775. if ( vv->last_field == V4L2_FIELD_INTERLACED ) {
  776. WRITE_RPS0(CMD_PAUSE | o_wait);
  777. WRITE_RPS0(CMD_PAUSE | e_wait);
  778. } else if ( vv->last_field == V4L2_FIELD_TOP ) {
  779. WRITE_RPS0(CMD_PAUSE | (vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? MASK_10 : MASK_09));
  780. WRITE_RPS0(CMD_PAUSE | o_wait);
  781. } else if ( vv->last_field == V4L2_FIELD_BOTTOM ) {
  782. WRITE_RPS0(CMD_PAUSE | (vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? MASK_10 : MASK_09));
  783. WRITE_RPS0(CMD_PAUSE | e_wait);
  784. }
  785. /* turn off video-dma1 */
  786. WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));
  787. WRITE_RPS0(MASK_22 | MASK_06); /* => mask */
  788. WRITE_RPS0(MASK_22); /* => values */
  789. if( 0 != planar ) {
  790. /* turn off video-dma2 */
  791. WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));
  792. WRITE_RPS0(MASK_05 | MASK_21); /* => mask */
  793. WRITE_RPS0(MASK_21); /* => values */
  794. /* turn off video-dma3 */
  795. WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));
  796. WRITE_RPS0(MASK_04 | MASK_20); /* => mask */
  797. WRITE_RPS0(MASK_20); /* => values */
  798. }
  799. /* generate interrupt */
  800. WRITE_RPS0(CMD_INTERRUPT);
  801. /* stop */
  802. WRITE_RPS0(CMD_STOP);
  803. }
  804. void saa7146_set_capture(struct saa7146_dev *dev, struct saa7146_buf *buf, struct saa7146_buf *next)
  805. {
  806. struct saa7146_format *sfmt = format_by_fourcc(dev,buf->fmt->pixelformat);
  807. struct saa7146_vv *vv = dev->vv_data;
  808. u32 vdma1_prot_addr;
  809. DEB_CAP(("buf:%p, next:%p\n",buf,next));
  810. vdma1_prot_addr = saa7146_read(dev, PROT_ADDR1);
  811. if( 0 == vdma1_prot_addr ) {
  812. /* clear out beginning of streaming bit (rps register 0)*/
  813. DEB_CAP(("forcing sync to new frame\n"));
  814. saa7146_write(dev, MC2, MASK_27 );
  815. }
  816. saa7146_set_window(dev, buf->fmt->width, buf->fmt->height, buf->fmt->field);
  817. saa7146_set_output_format(dev, sfmt->trans);
  818. saa7146_disable_clipping(dev);
  819. if ( vv->last_field == V4L2_FIELD_INTERLACED ) {
  820. } else if ( vv->last_field == V4L2_FIELD_TOP ) {
  821. vv->last_field = V4L2_FIELD_BOTTOM;
  822. } else if ( vv->last_field == V4L2_FIELD_BOTTOM ) {
  823. vv->last_field = V4L2_FIELD_TOP;
  824. }
  825. if( 0 != IS_PLANAR(sfmt->trans)) {
  826. calculate_video_dma_grab_planar(dev, buf);
  827. program_capture_engine(dev,1);
  828. } else {
  829. calculate_video_dma_grab_packed(dev, buf);
  830. program_capture_engine(dev,0);
  831. }
  832. /*
  833. printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
  834. printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
  835. printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
  836. printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
  837. printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
  838. printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
  839. printk("vdma%d => vptr : 0x%08x\n", 1,saa7146_read(dev,PCI_VDP1));
  840. */
  841. /* write the address of the rps-program */
  842. saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
  843. /* turn on rps */
  844. saa7146_write(dev, MC1, (MASK_12 | MASK_28));
  845. }