Path: blob/master/drivers/media/common/saa7146_hlp.c
15112 views
#include <linux/kernel.h>1#include <media/saa7146_vv.h>23static 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}1011static void calculate_hps_source_and_sync(struct saa7146_dev *dev, int source, int sync, u32* hps_ctrl)12{13*hps_ctrl &= ~(MASK_30 | MASK_31 | MASK_28);14*hps_ctrl |= (source << 30) | (sync << 28);15}1617static void calculate_hxo_and_hyo(struct saa7146_vv *vv, u32* hps_h_scale, u32* hps_ctrl)18{19int hyo = 0, hxo = 0;2021hyo = vv->standard->v_offset;22hxo = vv->standard->h_offset;2324*hps_h_scale &= ~(MASK_B0 | 0xf00);25*hps_h_scale |= (hxo << 0);2627*hps_ctrl &= ~(MASK_W0 | MASK_B2);28*hps_ctrl |= (hyo << 12);29}3031/* helper functions for the calculation of the horizontal- and vertical32scaling registers, clip-format-register etc ...33these functions take pointers to the (most-likely read-out34original-values) and manipulate them according to the requested35changes.36*/3738/* hps_coeff used for CXY and CXUV; scale 1/1 -> scale 1/64 */39static struct {40u16 hps_coeff;41u16 weight_sum;42} hps_h_coeff_tab [] = {43{0x00, 2}, {0x02, 4}, {0x00, 4}, {0x06, 8}, {0x02, 8},44{0x08, 8}, {0x00, 8}, {0x1E, 16}, {0x0E, 8}, {0x26, 8},45{0x06, 8}, {0x42, 8}, {0x02, 8}, {0x80, 8}, {0x00, 8},46{0xFE, 16}, {0xFE, 8}, {0x7E, 8}, {0x7E, 8}, {0x3E, 8},47{0x3E, 8}, {0x1E, 8}, {0x1E, 8}, {0x0E, 8}, {0x0E, 8},48{0x06, 8}, {0x06, 8}, {0x02, 8}, {0x02, 8}, {0x00, 8},49{0x00, 8}, {0xFE, 16}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8},50{0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8},51{0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8},52{0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0xFE, 8}, {0x7E, 8},53{0x7E, 8}, {0x3E, 8}, {0x3E, 8}, {0x1E, 8}, {0x1E, 8},54{0x0E, 8}, {0x0E, 8}, {0x06, 8}, {0x06, 8}, {0x02, 8},55{0x02, 8}, {0x00, 8}, {0x00, 8}, {0xFE, 16}56};5758/* table of attenuation values for horizontal scaling */59static u8 h_attenuation[] = { 1, 2, 4, 8, 2, 4, 8, 16, 0};6061/* calculate horizontal scale registers */62static int calculate_h_scale_registers(struct saa7146_dev *dev,63int in_x, int out_x, int flip_lr,64u32* hps_ctrl, u32* hps_v_gain, u32* hps_h_prescale, u32* hps_h_scale)65{66/* horizontal prescaler */67u32 dcgx = 0, xpsc = 0, xacm = 0, cxy = 0, cxuv = 0;68/* horizontal scaler */69u32 xim = 0, xp = 0, xsci =0;70/* vertical scale & gain */71u32 pfuv = 0;7273/* helper variables */74u32 h_atten = 0, i = 0;7576if ( 0 == out_x ) {77return -EINVAL;78}7980/* mask out vanity-bit */81*hps_ctrl &= ~MASK_29;8283/* calculate prescale-(xspc)-value: [n .. 1/2) : 184[1/2 .. 1/3) : 285[1/3 .. 1/4) : 386... */87if (in_x > out_x) {88xpsc = in_x / out_x;89}90else {91/* zooming */92xpsc = 1;93}9495/* if flip_lr-bit is set, number of pixels after96horizontal prescaling must be < 384 */97if ( 0 != flip_lr ) {9899/* set vanity bit */100*hps_ctrl |= MASK_29;101102while (in_x / xpsc >= 384 )103xpsc++;104}105/* if zooming is wanted, number of pixels after106horizontal prescaling must be < 768 */107else {108while ( in_x / xpsc >= 768 )109xpsc++;110}111112/* maximum prescale is 64 (p.69) */113if ( xpsc > 64 )114xpsc = 64;115116/* keep xacm clear*/117xacm = 0;118119/* set horizontal filter parameters (CXY = CXUV) */120cxy = hps_h_coeff_tab[( (xpsc - 1) < 63 ? (xpsc - 1) : 63 )].hps_coeff;121cxuv = cxy;122123/* calculate and set horizontal fine scale (xsci) */124125/* bypass the horizontal scaler ? */126if ( (in_x == out_x) && ( 1 == xpsc ) )127xsci = 0x400;128else129xsci = ( (1024 * in_x) / (out_x * xpsc) ) + xpsc;130131/* set start phase for horizontal fine scale (xp) to 0 */132xp = 0;133134/* set xim, if we bypass the horizontal scaler */135if ( 0x400 == xsci )136xim = 1;137else138xim = 0;139140/* if the prescaler is bypassed, enable horizontal141accumulation mode (xacm) and clear dcgx */142if( 1 == xpsc ) {143xacm = 1;144dcgx = 0;145} else {146xacm = 0;147/* get best match in the table of attenuations148for horizontal scaling */149h_atten = hps_h_coeff_tab[( (xpsc - 1) < 63 ? (xpsc - 1) : 63 )].weight_sum;150151for (i = 0; h_attenuation[i] != 0; i++) {152if (h_attenuation[i] >= h_atten)153break;154}155156dcgx = i;157}158159/* the horizontal scaling increment controls the UV filter160to reduce the bandwidth to improve the display quality,161so set it ... */162if ( xsci == 0x400)163pfuv = 0x00;164else if ( xsci < 0x600)165pfuv = 0x01;166else if ( xsci < 0x680)167pfuv = 0x11;168else if ( xsci < 0x700)169pfuv = 0x22;170else171pfuv = 0x33;172173174*hps_v_gain &= MASK_W0|MASK_B2;175*hps_v_gain |= (pfuv << 24);176177*hps_h_scale &= ~(MASK_W1 | 0xf000);178*hps_h_scale |= (xim << 31) | (xp << 24) | (xsci << 12);179180*hps_h_prescale |= (dcgx << 27) | ((xpsc-1) << 18) | (xacm << 17) | (cxy << 8) | (cxuv << 0);181182return 0;183}184185static struct {186u16 hps_coeff;187u16 weight_sum;188} hps_v_coeff_tab [] = {189{0x0100, 2}, {0x0102, 4}, {0x0300, 4}, {0x0106, 8}, {0x0502, 8},190{0x0708, 8}, {0x0F00, 8}, {0x011E, 16}, {0x110E, 16}, {0x1926, 16},191{0x3906, 16}, {0x3D42, 16}, {0x7D02, 16}, {0x7F80, 16}, {0xFF00, 16},192{0x01FE, 32}, {0x01FE, 32}, {0x817E, 32}, {0x817E, 32}, {0xC13E, 32},193{0xC13E, 32}, {0xE11E, 32}, {0xE11E, 32}, {0xF10E, 32}, {0xF10E, 32},194{0xF906, 32}, {0xF906, 32}, {0xFD02, 32}, {0xFD02, 32}, {0xFF00, 32},195{0xFF00, 32}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64},196{0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64},197{0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64},198{0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x01FE, 64}, {0x817E, 64},199{0x817E, 64}, {0xC13E, 64}, {0xC13E, 64}, {0xE11E, 64}, {0xE11E, 64},200{0xF10E, 64}, {0xF10E, 64}, {0xF906, 64}, {0xF906, 64}, {0xFD02, 64},201{0xFD02, 64}, {0xFF00, 64}, {0xFF00, 64}, {0x01FE, 128}202};203204/* table of attenuation values for vertical scaling */205static u16 v_attenuation[] = { 2, 4, 8, 16, 32, 64, 128, 256, 0};206207/* calculate vertical scale registers */208static int calculate_v_scale_registers(struct saa7146_dev *dev, enum v4l2_field field,209int in_y, int out_y, u32* hps_v_scale, u32* hps_v_gain)210{211int lpi = 0;212213/* vertical scaling */214u32 yacm = 0, ysci = 0, yacl = 0, ypo = 0, ype = 0;215/* vertical scale & gain */216u32 dcgy = 0, cya_cyb = 0;217218/* helper variables */219u32 v_atten = 0, i = 0;220221/* error, if vertical zooming */222if ( in_y < out_y ) {223return -EINVAL;224}225226/* linear phase interpolation may be used227if scaling is between 1 and 1/2 (both fields used)228or scaling is between 1/2 and 1/4 (if only one field is used) */229230if (V4L2_FIELD_HAS_BOTH(field)) {231if( 2*out_y >= in_y) {232lpi = 1;233}234} else if (field == V4L2_FIELD_TOP235|| field == V4L2_FIELD_ALTERNATE236|| field == V4L2_FIELD_BOTTOM) {237if( 4*out_y >= in_y ) {238lpi = 1;239}240out_y *= 2;241}242if( 0 != lpi ) {243244yacm = 0;245yacl = 0;246cya_cyb = 0x00ff;247248/* calculate scaling increment */249if ( in_y > out_y )250ysci = ((1024 * in_y) / (out_y + 1)) - 1024;251else252ysci = 0;253254dcgy = 0;255256/* calculate ype and ypo */257ype = ysci / 16;258ypo = ype + (ysci / 64);259260} else {261yacm = 1;262263/* calculate scaling increment */264ysci = (((10 * 1024 * (in_y - out_y - 1)) / in_y) + 9) / 10;265266/* calculate ype and ypo */267ypo = ype = ((ysci + 15) / 16);268269/* the sequence length interval (yacl) has to be set according270to the prescale value, e.g. [n .. 1/2) : 0271[1/2 .. 1/3) : 1272[1/3 .. 1/4) : 2273... */274if ( ysci < 512) {275yacl = 0;276} else {277yacl = ( ysci / (1024 - ysci) );278}279280/* get filter coefficients for cya, cyb from table hps_v_coeff_tab */281cya_cyb = hps_v_coeff_tab[ (yacl < 63 ? yacl : 63 ) ].hps_coeff;282283/* get best match in the table of attenuations for vertical scaling */284v_atten = hps_v_coeff_tab[ (yacl < 63 ? yacl : 63 ) ].weight_sum;285286for (i = 0; v_attenuation[i] != 0; i++) {287if (v_attenuation[i] >= v_atten)288break;289}290291dcgy = i;292}293294/* ypo and ype swapped in spec ? */295*hps_v_scale |= (yacm << 31) | (ysci << 21) | (yacl << 15) | (ypo << 8 ) | (ype << 1);296297*hps_v_gain &= ~(MASK_W0|MASK_B2);298*hps_v_gain |= (dcgy << 16) | (cya_cyb << 0);299300return 0;301}302303/* simple bubble-sort algorithm with duplicate elimination */304static int sort_and_eliminate(u32* values, int* count)305{306int low = 0, high = 0, top = 0, temp = 0;307int cur = 0, next = 0;308309/* sanity checks */310if( (0 > *count) || (NULL == values) ) {311return -EINVAL;312}313314/* bubble sort the first @count items of the array @values */315for( top = *count; top > 0; top--) {316for( low = 0, high = 1; high < top; low++, high++) {317if( values[low] > values[high] ) {318temp = values[low];319values[low] = values[high];320values[high] = temp;321}322}323}324325/* remove duplicate items */326for( cur = 0, next = 1; next < *count; next++) {327if( values[cur] != values[next])328values[++cur] = values[next];329}330331*count = cur + 1;332333return 0;334}335336static void calculate_clipping_registers_rect(struct saa7146_dev *dev, struct saa7146_fh *fh,337struct saa7146_video_dma *vdma2, u32* clip_format, u32* arbtr_ctrl, enum v4l2_field field)338{339struct saa7146_vv *vv = dev->vv_data;340__le32 *clipping = vv->d_clipping.cpu_addr;341342int width = fh->ov.win.w.width;343int height = fh->ov.win.w.height;344int clipcount = fh->ov.nclips;345346u32 line_list[32];347u32 pixel_list[32];348int numdwords = 0;349350int i = 0, j = 0;351int cnt_line = 0, cnt_pixel = 0;352353int x[32], y[32], w[32], h[32];354355/* clear out memory */356memset(&line_list[0], 0x00, sizeof(u32)*32);357memset(&pixel_list[0], 0x00, sizeof(u32)*32);358memset(clipping, 0x00, SAA7146_CLIPPING_MEM);359360/* fill the line and pixel-lists */361for(i = 0; i < clipcount; i++) {362int l = 0, r = 0, t = 0, b = 0;363364x[i] = fh->ov.clips[i].c.left;365y[i] = fh->ov.clips[i].c.top;366w[i] = fh->ov.clips[i].c.width;367h[i] = fh->ov.clips[i].c.height;368369if( w[i] < 0) {370x[i] += w[i]; w[i] = -w[i];371}372if( h[i] < 0) {373y[i] += h[i]; h[i] = -h[i];374}375if( x[i] < 0) {376w[i] += x[i]; x[i] = 0;377}378if( y[i] < 0) {379h[i] += y[i]; y[i] = 0;380}381if( 0 != vv->vflip ) {382y[i] = height - y[i] - h[i];383}384385l = x[i];386r = x[i]+w[i];387t = y[i];388b = y[i]+h[i];389390/* insert left/right coordinates */391pixel_list[ 2*i ] = min_t(int, l, width);392pixel_list[(2*i)+1] = min_t(int, r, width);393/* insert top/bottom coordinates */394line_list[ 2*i ] = min_t(int, t, height);395line_list[(2*i)+1] = min_t(int, b, height);396}397398/* sort and eliminate lists */399cnt_line = cnt_pixel = 2*clipcount;400sort_and_eliminate( &pixel_list[0], &cnt_pixel );401sort_and_eliminate( &line_list[0], &cnt_line );402403/* calculate the number of used u32s */404numdwords = max_t(int, (cnt_line+1), (cnt_pixel+1))*2;405numdwords = max_t(int, 4, numdwords);406numdwords = min_t(int, 64, numdwords);407408/* fill up cliptable */409for(i = 0; i < cnt_pixel; i++) {410clipping[2*i] |= cpu_to_le32(pixel_list[i] << 16);411}412for(i = 0; i < cnt_line; i++) {413clipping[(2*i)+1] |= cpu_to_le32(line_list[i] << 16);414}415416/* fill up cliptable with the display infos */417for(j = 0; j < clipcount; j++) {418419for(i = 0; i < cnt_pixel; i++) {420421if( x[j] < 0)422x[j] = 0;423424if( pixel_list[i] < (x[j] + w[j])) {425426if ( pixel_list[i] >= x[j] ) {427clipping[2*i] |= cpu_to_le32(1 << j);428}429}430}431for(i = 0; i < cnt_line; i++) {432433if( y[j] < 0)434y[j] = 0;435436if( line_list[i] < (y[j] + h[j]) ) {437438if( line_list[i] >= y[j] ) {439clipping[(2*i)+1] |= cpu_to_le32(1 << j);440}441}442}443}444445/* adjust arbitration control register */446*arbtr_ctrl &= 0xffff00ff;447*arbtr_ctrl |= 0x00001c00;448449vdma2->base_even = vv->d_clipping.dma_handle;450vdma2->base_odd = vv->d_clipping.dma_handle;451vdma2->prot_addr = vv->d_clipping.dma_handle+((sizeof(u32))*(numdwords));452vdma2->base_page = 0x04;453vdma2->pitch = 0x00;454vdma2->num_line_byte = (0 << 16 | (sizeof(u32))*(numdwords-1) );455456/* set clipping-mode. this depends on the field(s) used */457*clip_format &= 0xfffffff7;458if (V4L2_FIELD_HAS_BOTH(field)) {459*clip_format |= 0x00000008;460} else {461*clip_format |= 0x00000000;462}463}464465/* disable clipping */466static void saa7146_disable_clipping(struct saa7146_dev *dev)467{468u32 clip_format = saa7146_read(dev, CLIP_FORMAT_CTRL);469470/* mask out relevant bits (=lower word)*/471clip_format &= MASK_W1;472473/* upload clipping-registers*/474saa7146_write(dev, CLIP_FORMAT_CTRL,clip_format);475saa7146_write(dev, MC2, (MASK_05 | MASK_21));476477/* disable video dma2 */478saa7146_write(dev, MC1, MASK_21);479}480481static void saa7146_set_clipping_rect(struct saa7146_fh *fh)482{483struct saa7146_dev *dev = fh->dev;484enum v4l2_field field = fh->ov.win.field;485struct saa7146_video_dma vdma2;486u32 clip_format;487u32 arbtr_ctrl;488489/* check clipcount, disable clipping if clipcount == 0*/490if( fh->ov.nclips == 0 ) {491saa7146_disable_clipping(dev);492return;493}494495clip_format = saa7146_read(dev, CLIP_FORMAT_CTRL);496arbtr_ctrl = saa7146_read(dev, PCI_BT_V1);497498calculate_clipping_registers_rect(dev, fh, &vdma2, &clip_format, &arbtr_ctrl, field);499500/* set clipping format */501clip_format &= 0xffff0008;502clip_format |= (SAA7146_CLIPPING_RECT << 4);503504/* prepare video dma2 */505saa7146_write(dev, BASE_EVEN2, vdma2.base_even);506saa7146_write(dev, BASE_ODD2, vdma2.base_odd);507saa7146_write(dev, PROT_ADDR2, vdma2.prot_addr);508saa7146_write(dev, BASE_PAGE2, vdma2.base_page);509saa7146_write(dev, PITCH2, vdma2.pitch);510saa7146_write(dev, NUM_LINE_BYTE2, vdma2.num_line_byte);511512/* prepare the rest */513saa7146_write(dev, CLIP_FORMAT_CTRL,clip_format);514saa7146_write(dev, PCI_BT_V1, arbtr_ctrl);515516/* upload clip_control-register, clipping-registers, enable video dma2 */517saa7146_write(dev, MC2, (MASK_05 | MASK_21 | MASK_03 | MASK_19));518saa7146_write(dev, MC1, (MASK_05 | MASK_21));519}520521static void saa7146_set_window(struct saa7146_dev *dev, int width, int height, enum v4l2_field field)522{523struct saa7146_vv *vv = dev->vv_data;524525int source = vv->current_hps_source;526int sync = vv->current_hps_sync;527528u32 hps_v_scale = 0, hps_v_gain = 0, hps_ctrl = 0, hps_h_prescale = 0, hps_h_scale = 0;529530/* set vertical scale */531hps_v_scale = 0; /* all bits get set by the function-call */532hps_v_gain = 0; /* fixme: saa7146_read(dev, HPS_V_GAIN);*/533calculate_v_scale_registers(dev, field, vv->standard->v_field*2, height, &hps_v_scale, &hps_v_gain);534535/* set horizontal scale */536hps_ctrl = 0;537hps_h_prescale = 0; /* all bits get set in the function */538hps_h_scale = 0;539calculate_h_scale_registers(dev, vv->standard->h_pixels, width, vv->hflip, &hps_ctrl, &hps_v_gain, &hps_h_prescale, &hps_h_scale);540541/* set hyo and hxo */542calculate_hxo_and_hyo(vv, &hps_h_scale, &hps_ctrl);543calculate_hps_source_and_sync(dev, source, sync, &hps_ctrl);544545/* write out new register contents */546saa7146_write(dev, HPS_V_SCALE, hps_v_scale);547saa7146_write(dev, HPS_V_GAIN, hps_v_gain);548saa7146_write(dev, HPS_CTRL, hps_ctrl);549saa7146_write(dev, HPS_H_PRESCALE,hps_h_prescale);550saa7146_write(dev, HPS_H_SCALE, hps_h_scale);551552/* upload shadow-ram registers */553saa7146_write(dev, MC2, (MASK_05 | MASK_06 | MASK_21 | MASK_22) );554}555556/* calculate the new memory offsets for a desired position */557static void saa7146_set_position(struct saa7146_dev *dev, int w_x, int w_y, int w_height, enum v4l2_field field, u32 pixelformat)558{559struct saa7146_vv *vv = dev->vv_data;560struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev, pixelformat);561562int b_depth = vv->ov_fmt->depth;563int b_bpl = vv->ov_fb.fmt.bytesperline;564/* The unsigned long cast is to remove a 64-bit compile warning since565it looks like a 64-bit address is cast to a 32-bit value, even566though the base pointer is really a 32-bit physical address that567goes into a 32-bit DMA register.568FIXME: might not work on some 64-bit platforms, but see the FIXME569in struct v4l2_framebuffer (videodev2.h) for that.570*/571u32 base = (u32)(unsigned long)vv->ov_fb.base;572573struct saa7146_video_dma vdma1;574575/* calculate memory offsets for picture, look if we shall top-down-flip */576vdma1.pitch = 2*b_bpl;577if ( 0 == vv->vflip ) {578vdma1.base_even = base + (w_y * (vdma1.pitch/2)) + (w_x * (b_depth / 8));579vdma1.base_odd = vdma1.base_even + (vdma1.pitch / 2);580vdma1.prot_addr = vdma1.base_even + (w_height * (vdma1.pitch / 2));581}582else {583vdma1.base_even = base + ((w_y+w_height) * (vdma1.pitch/2)) + (w_x * (b_depth / 8));584vdma1.base_odd = vdma1.base_even - (vdma1.pitch / 2);585vdma1.prot_addr = vdma1.base_odd - (w_height * (vdma1.pitch / 2));586}587588if (V4L2_FIELD_HAS_BOTH(field)) {589} else if (field == V4L2_FIELD_ALTERNATE) {590/* fixme */591vdma1.base_odd = vdma1.prot_addr;592vdma1.pitch /= 2;593} else if (field == V4L2_FIELD_TOP) {594vdma1.base_odd = vdma1.prot_addr;595vdma1.pitch /= 2;596} else if (field == V4L2_FIELD_BOTTOM) {597vdma1.base_odd = vdma1.base_even;598vdma1.base_even = vdma1.prot_addr;599vdma1.pitch /= 2;600}601602if ( 0 != vv->vflip ) {603vdma1.pitch *= -1;604}605606vdma1.base_page = sfmt->swap;607vdma1.num_line_byte = (vv->standard->v_field<<16)+vv->standard->h_pixels;608609saa7146_write_out_dma(dev, 1, &vdma1);610}611612static void saa7146_set_output_format(struct saa7146_dev *dev, unsigned long palette)613{614u32 clip_format = saa7146_read(dev, CLIP_FORMAT_CTRL);615616/* call helper function */617calculate_output_format_register(dev,palette,&clip_format);618619/* update the hps registers */620saa7146_write(dev, CLIP_FORMAT_CTRL, clip_format);621saa7146_write(dev, MC2, (MASK_05 | MASK_21));622}623624/* select input-source */625void saa7146_set_hps_source_and_sync(struct saa7146_dev *dev, int source, int sync)626{627struct saa7146_vv *vv = dev->vv_data;628u32 hps_ctrl = 0;629630/* read old state */631hps_ctrl = saa7146_read(dev, HPS_CTRL);632633hps_ctrl &= ~( MASK_31 | MASK_30 | MASK_28 );634hps_ctrl |= (source << 30) | (sync << 28);635636/* write back & upload register */637saa7146_write(dev, HPS_CTRL, hps_ctrl);638saa7146_write(dev, MC2, (MASK_05 | MASK_21));639640vv->current_hps_source = source;641vv->current_hps_sync = sync;642}643EXPORT_SYMBOL_GPL(saa7146_set_hps_source_and_sync);644645int saa7146_enable_overlay(struct saa7146_fh *fh)646{647struct saa7146_dev *dev = fh->dev;648struct saa7146_vv *vv = dev->vv_data;649650saa7146_set_window(dev, fh->ov.win.w.width, fh->ov.win.w.height, fh->ov.win.field);651saa7146_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);652saa7146_set_output_format(dev, vv->ov_fmt->trans);653saa7146_set_clipping_rect(fh);654655/* enable video dma1 */656saa7146_write(dev, MC1, (MASK_06 | MASK_22));657return 0;658}659660void saa7146_disable_overlay(struct saa7146_fh *fh)661{662struct saa7146_dev *dev = fh->dev;663664/* disable clipping + video dma1 */665saa7146_disable_clipping(dev);666saa7146_write(dev, MC1, MASK_22);667}668669void saa7146_write_out_dma(struct saa7146_dev* dev, int which, struct saa7146_video_dma* vdma)670{671int where = 0;672673if( which < 1 || which > 3) {674return;675}676677/* calculate starting address */678where = (which-1)*0x18;679680saa7146_write(dev, where, vdma->base_odd);681saa7146_write(dev, where+0x04, vdma->base_even);682saa7146_write(dev, where+0x08, vdma->prot_addr);683saa7146_write(dev, where+0x0c, vdma->pitch);684saa7146_write(dev, where+0x10, vdma->base_page);685saa7146_write(dev, where+0x14, vdma->num_line_byte);686687/* upload */688saa7146_write(dev, MC2, (MASK_02<<(which-1))|(MASK_18<<(which-1)));689/*690printk("vdma%d.base_even: 0x%08x\n", which,vdma->base_even);691printk("vdma%d.base_odd: 0x%08x\n", which,vdma->base_odd);692printk("vdma%d.prot_addr: 0x%08x\n", which,vdma->prot_addr);693printk("vdma%d.base_page: 0x%08x\n", which,vdma->base_page);694printk("vdma%d.pitch: 0x%08x\n", which,vdma->pitch);695printk("vdma%d.num_line_byte: 0x%08x\n", which,vdma->num_line_byte);696*/697}698699static int calculate_video_dma_grab_packed(struct saa7146_dev* dev, struct saa7146_buf *buf)700{701struct saa7146_vv *vv = dev->vv_data;702struct saa7146_video_dma vdma1;703704struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);705706int width = buf->fmt->width;707int height = buf->fmt->height;708int bytesperline = buf->fmt->bytesperline;709enum v4l2_field field = buf->fmt->field;710711int depth = sfmt->depth;712713DEB_CAP(("[size=%dx%d,fields=%s]\n",714width,height,v4l2_field_names[field]));715716if( bytesperline != 0) {717vdma1.pitch = bytesperline*2;718} else {719vdma1.pitch = (width*depth*2)/8;720}721vdma1.num_line_byte = ((vv->standard->v_field<<16) + vv->standard->h_pixels);722vdma1.base_page = buf->pt[0].dma | ME1 | sfmt->swap;723724if( 0 != vv->vflip ) {725vdma1.prot_addr = buf->pt[0].offset;726vdma1.base_even = buf->pt[0].offset+(vdma1.pitch/2)*height;727vdma1.base_odd = vdma1.base_even - (vdma1.pitch/2);728} else {729vdma1.base_even = buf->pt[0].offset;730vdma1.base_odd = vdma1.base_even + (vdma1.pitch/2);731vdma1.prot_addr = buf->pt[0].offset+(vdma1.pitch/2)*height;732}733734if (V4L2_FIELD_HAS_BOTH(field)) {735} else if (field == V4L2_FIELD_ALTERNATE) {736/* fixme */737if ( vv->last_field == V4L2_FIELD_TOP ) {738vdma1.base_odd = vdma1.prot_addr;739vdma1.pitch /= 2;740} else if ( vv->last_field == V4L2_FIELD_BOTTOM ) {741vdma1.base_odd = vdma1.base_even;742vdma1.base_even = vdma1.prot_addr;743vdma1.pitch /= 2;744}745} else if (field == V4L2_FIELD_TOP) {746vdma1.base_odd = vdma1.prot_addr;747vdma1.pitch /= 2;748} else if (field == V4L2_FIELD_BOTTOM) {749vdma1.base_odd = vdma1.base_even;750vdma1.base_even = vdma1.prot_addr;751vdma1.pitch /= 2;752}753754if( 0 != vv->vflip ) {755vdma1.pitch *= -1;756}757758saa7146_write_out_dma(dev, 1, &vdma1);759return 0;760}761762static int calc_planar_422(struct saa7146_vv *vv, struct saa7146_buf *buf, struct saa7146_video_dma *vdma2, struct saa7146_video_dma *vdma3)763{764int height = buf->fmt->height;765int width = buf->fmt->width;766767vdma2->pitch = width;768vdma3->pitch = width;769770/* fixme: look at bytesperline! */771772if( 0 != vv->vflip ) {773vdma2->prot_addr = buf->pt[1].offset;774vdma2->base_even = ((vdma2->pitch/2)*height)+buf->pt[1].offset;775vdma2->base_odd = vdma2->base_even - (vdma2->pitch/2);776777vdma3->prot_addr = buf->pt[2].offset;778vdma3->base_even = ((vdma3->pitch/2)*height)+buf->pt[2].offset;779vdma3->base_odd = vdma3->base_even - (vdma3->pitch/2);780} else {781vdma3->base_even = buf->pt[2].offset;782vdma3->base_odd = vdma3->base_even + (vdma3->pitch/2);783vdma3->prot_addr = (vdma3->pitch/2)*height+buf->pt[2].offset;784785vdma2->base_even = buf->pt[1].offset;786vdma2->base_odd = vdma2->base_even + (vdma2->pitch/2);787vdma2->prot_addr = (vdma2->pitch/2)*height+buf->pt[1].offset;788}789790return 0;791}792793static int calc_planar_420(struct saa7146_vv *vv, struct saa7146_buf *buf, struct saa7146_video_dma *vdma2, struct saa7146_video_dma *vdma3)794{795int height = buf->fmt->height;796int width = buf->fmt->width;797798vdma2->pitch = width/2;799vdma3->pitch = width/2;800801if( 0 != vv->vflip ) {802vdma2->prot_addr = buf->pt[2].offset;803vdma2->base_even = ((vdma2->pitch/2)*height)+buf->pt[2].offset;804vdma2->base_odd = vdma2->base_even - (vdma2->pitch/2);805806vdma3->prot_addr = buf->pt[1].offset;807vdma3->base_even = ((vdma3->pitch/2)*height)+buf->pt[1].offset;808vdma3->base_odd = vdma3->base_even - (vdma3->pitch/2);809810} else {811vdma3->base_even = buf->pt[2].offset;812vdma3->base_odd = vdma3->base_even + (vdma3->pitch);813vdma3->prot_addr = (vdma3->pitch/2)*height+buf->pt[2].offset;814815vdma2->base_even = buf->pt[1].offset;816vdma2->base_odd = vdma2->base_even + (vdma2->pitch);817vdma2->prot_addr = (vdma2->pitch/2)*height+buf->pt[1].offset;818}819return 0;820}821822static int calculate_video_dma_grab_planar(struct saa7146_dev* dev, struct saa7146_buf *buf)823{824struct saa7146_vv *vv = dev->vv_data;825struct saa7146_video_dma vdma1;826struct saa7146_video_dma vdma2;827struct saa7146_video_dma vdma3;828829struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);830831int width = buf->fmt->width;832int height = buf->fmt->height;833enum v4l2_field field = buf->fmt->field;834835BUG_ON(0 == buf->pt[0].dma);836BUG_ON(0 == buf->pt[1].dma);837BUG_ON(0 == buf->pt[2].dma);838839DEB_CAP(("[size=%dx%d,fields=%s]\n",840width,height,v4l2_field_names[field]));841842/* fixme: look at bytesperline! */843844/* fixme: what happens for user space buffers here?. The offsets are845most likely wrong, this version here only works for page-aligned846buffers, modifications to the pagetable-functions are necessary...*/847848vdma1.pitch = width*2;849vdma1.num_line_byte = ((vv->standard->v_field<<16) + vv->standard->h_pixels);850vdma1.base_page = buf->pt[0].dma | ME1;851852if( 0 != vv->vflip ) {853vdma1.prot_addr = buf->pt[0].offset;854vdma1.base_even = ((vdma1.pitch/2)*height)+buf->pt[0].offset;855vdma1.base_odd = vdma1.base_even - (vdma1.pitch/2);856} else {857vdma1.base_even = buf->pt[0].offset;858vdma1.base_odd = vdma1.base_even + (vdma1.pitch/2);859vdma1.prot_addr = (vdma1.pitch/2)*height+buf->pt[0].offset;860}861862vdma2.num_line_byte = 0; /* unused */863vdma2.base_page = buf->pt[1].dma | ME1;864865vdma3.num_line_byte = 0; /* unused */866vdma3.base_page = buf->pt[2].dma | ME1;867868switch( sfmt->depth ) {869case 12: {870calc_planar_420(vv,buf,&vdma2,&vdma3);871break;872}873case 16: {874calc_planar_422(vv,buf,&vdma2,&vdma3);875break;876}877default: {878return -1;879}880}881882if (V4L2_FIELD_HAS_BOTH(field)) {883} else if (field == V4L2_FIELD_ALTERNATE) {884/* fixme */885vdma1.base_odd = vdma1.prot_addr;886vdma1.pitch /= 2;887vdma2.base_odd = vdma2.prot_addr;888vdma2.pitch /= 2;889vdma3.base_odd = vdma3.prot_addr;890vdma3.pitch /= 2;891} else if (field == V4L2_FIELD_TOP) {892vdma1.base_odd = vdma1.prot_addr;893vdma1.pitch /= 2;894vdma2.base_odd = vdma2.prot_addr;895vdma2.pitch /= 2;896vdma3.base_odd = vdma3.prot_addr;897vdma3.pitch /= 2;898} else if (field == V4L2_FIELD_BOTTOM) {899vdma1.base_odd = vdma1.base_even;900vdma1.base_even = vdma1.prot_addr;901vdma1.pitch /= 2;902vdma2.base_odd = vdma2.base_even;903vdma2.base_even = vdma2.prot_addr;904vdma2.pitch /= 2;905vdma3.base_odd = vdma3.base_even;906vdma3.base_even = vdma3.prot_addr;907vdma3.pitch /= 2;908}909910if( 0 != vv->vflip ) {911vdma1.pitch *= -1;912vdma2.pitch *= -1;913vdma3.pitch *= -1;914}915916saa7146_write_out_dma(dev, 1, &vdma1);917if( (sfmt->flags & FORMAT_BYTE_SWAP) != 0 ) {918saa7146_write_out_dma(dev, 3, &vdma2);919saa7146_write_out_dma(dev, 2, &vdma3);920} else {921saa7146_write_out_dma(dev, 2, &vdma2);922saa7146_write_out_dma(dev, 3, &vdma3);923}924return 0;925}926927static void program_capture_engine(struct saa7146_dev *dev, int planar)928{929struct saa7146_vv *vv = dev->vv_data;930int count = 0;931932unsigned long e_wait = vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? CMD_E_FID_A : CMD_E_FID_B;933unsigned long o_wait = vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? CMD_O_FID_A : CMD_O_FID_B;934935/* wait for o_fid_a/b / e_fid_a/b toggle only if rps register 0 is not set*/936WRITE_RPS0(CMD_PAUSE | CMD_OAN | CMD_SIG0 | o_wait);937WRITE_RPS0(CMD_PAUSE | CMD_OAN | CMD_SIG0 | e_wait);938939/* set rps register 0 */940WRITE_RPS0(CMD_WR_REG | (1 << 8) | (MC2/4));941WRITE_RPS0(MASK_27 | MASK_11);942943/* turn on video-dma1 */944WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));945WRITE_RPS0(MASK_06 | MASK_22); /* => mask */946WRITE_RPS0(MASK_06 | MASK_22); /* => values */947if( 0 != planar ) {948/* turn on video-dma2 */949WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));950WRITE_RPS0(MASK_05 | MASK_21); /* => mask */951WRITE_RPS0(MASK_05 | MASK_21); /* => values */952953/* turn on video-dma3 */954WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));955WRITE_RPS0(MASK_04 | MASK_20); /* => mask */956WRITE_RPS0(MASK_04 | MASK_20); /* => values */957}958959/* wait for o_fid_a/b / e_fid_a/b toggle */960if ( vv->last_field == V4L2_FIELD_INTERLACED ) {961WRITE_RPS0(CMD_PAUSE | o_wait);962WRITE_RPS0(CMD_PAUSE | e_wait);963} else if ( vv->last_field == V4L2_FIELD_TOP ) {964WRITE_RPS0(CMD_PAUSE | (vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? MASK_10 : MASK_09));965WRITE_RPS0(CMD_PAUSE | o_wait);966} else if ( vv->last_field == V4L2_FIELD_BOTTOM ) {967WRITE_RPS0(CMD_PAUSE | (vv->current_hps_sync == SAA7146_HPS_SYNC_PORT_A ? MASK_10 : MASK_09));968WRITE_RPS0(CMD_PAUSE | e_wait);969}970971/* turn off video-dma1 */972WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));973WRITE_RPS0(MASK_22 | MASK_06); /* => mask */974WRITE_RPS0(MASK_22); /* => values */975if( 0 != planar ) {976/* turn off video-dma2 */977WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));978WRITE_RPS0(MASK_05 | MASK_21); /* => mask */979WRITE_RPS0(MASK_21); /* => values */980981/* turn off video-dma3 */982WRITE_RPS0(CMD_WR_REG_MASK | (MC1/4));983WRITE_RPS0(MASK_04 | MASK_20); /* => mask */984WRITE_RPS0(MASK_20); /* => values */985}986987/* generate interrupt */988WRITE_RPS0(CMD_INTERRUPT);989990/* stop */991WRITE_RPS0(CMD_STOP);992}993994void saa7146_set_capture(struct saa7146_dev *dev, struct saa7146_buf *buf, struct saa7146_buf *next)995{996struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev,buf->fmt->pixelformat);997struct saa7146_vv *vv = dev->vv_data;998u32 vdma1_prot_addr;9991000DEB_CAP(("buf:%p, next:%p\n",buf,next));10011002vdma1_prot_addr = saa7146_read(dev, PROT_ADDR1);1003if( 0 == vdma1_prot_addr ) {1004/* clear out beginning of streaming bit (rps register 0)*/1005DEB_CAP(("forcing sync to new frame\n"));1006saa7146_write(dev, MC2, MASK_27 );1007}10081009saa7146_set_window(dev, buf->fmt->width, buf->fmt->height, buf->fmt->field);1010saa7146_set_output_format(dev, sfmt->trans);1011saa7146_disable_clipping(dev);10121013if ( vv->last_field == V4L2_FIELD_INTERLACED ) {1014} else if ( vv->last_field == V4L2_FIELD_TOP ) {1015vv->last_field = V4L2_FIELD_BOTTOM;1016} else if ( vv->last_field == V4L2_FIELD_BOTTOM ) {1017vv->last_field = V4L2_FIELD_TOP;1018}10191020if( 0 != IS_PLANAR(sfmt->trans)) {1021calculate_video_dma_grab_planar(dev, buf);1022program_capture_engine(dev,1);1023} else {1024calculate_video_dma_grab_packed(dev, buf);1025program_capture_engine(dev,0);1026}10271028/*1029printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));1030printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));1031printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));1032printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));1033printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));1034printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));1035printk("vdma%d => vptr : 0x%08x\n", 1,saa7146_read(dev,PCI_VDP1));1036*/10371038/* write the address of the rps-program */1039saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);10401041/* turn on rps */1042saa7146_write(dev, MC1, (MASK_12 | MASK_28));1043}104410451046