]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - drivers/gpu/drm/arm/display/komeda/d71/d71_component.c
drm/komeda: Rename main engine clk name "mclk" to "aclk"
[linux.git] / drivers / gpu / drm / arm / display / komeda / d71 / d71_component.c
index 031e5f305a3c2d77a47ae175e0d24c12f86b39ea..5212988c438c42904d44d70068254b31ad39d650 100644 (file)
@@ -10,6 +10,7 @@
 #include "komeda_kms.h"
 #include "malidp_io.h"
 #include "komeda_framebuffer.h"
+#include "komeda_color_mgmt.h"
 
 static void get_resources_id(u32 hw_id, u32 *pipe_id, u32 *comp_id)
 {
@@ -134,6 +135,27 @@ static u32 to_rot_ctrl(u32 rot)
        return lr_ctrl;
 }
 
+static u32 to_ad_ctrl(u64 modifier)
+{
+       u32 afbc_ctrl = AD_AEN;
+
+       if (!modifier)
+               return 0;
+
+       if ((modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) ==
+           AFBC_FORMAT_MOD_BLOCK_SIZE_32x8)
+               afbc_ctrl |= AD_WB;
+
+       if (modifier & AFBC_FORMAT_MOD_YTR)
+               afbc_ctrl |= AD_YT;
+       if (modifier & AFBC_FORMAT_MOD_SPLIT)
+               afbc_ctrl |= AD_BS;
+       if (modifier & AFBC_FORMAT_MOD_TILED)
+               afbc_ctrl |= AD_TH;
+
+       return afbc_ctrl;
+}
+
 static inline u32 to_d71_input_id(struct komeda_component_output *output)
 {
        struct komeda_component *comp = output->component;
@@ -141,6 +163,30 @@ static inline u32 to_d71_input_id(struct komeda_component_output *output)
        return comp ? (comp->hw_id + output->output_port) : 0;
 }
 
+static void d71_layer_update_fb(struct komeda_component *c,
+                               struct komeda_fb *kfb,
+                               dma_addr_t *addr)
+{
+       struct drm_framebuffer *fb = &kfb->base;
+       const struct drm_format_info *info = fb->format;
+       u32 __iomem *reg = c->reg;
+       int block_h;
+
+       if (info->num_planes > 2)
+               malidp_write64(reg, BLK_P2_PTR_LOW, addr[2]);
+
+       if (info->num_planes > 1) {
+               block_h = drm_format_info_block_height(info, 1);
+               malidp_write32(reg, BLK_P1_STRIDE, fb->pitches[1] * block_h);
+               malidp_write64(reg, BLK_P1_PTR_LOW, addr[1]);
+       }
+
+       block_h = drm_format_info_block_height(info, 0);
+       malidp_write32(reg, BLK_P0_STRIDE, fb->pitches[0] * block_h);
+       malidp_write64(reg, BLK_P0_PTR_LOW, addr[0]);
+       malidp_write32(reg, LAYER_FMT, kfb->format_caps->hw_id);
+}
+
 static void d71_layer_disable(struct komeda_component *c)
 {
        malidp_write32_mask(c->reg, BLK_CONTROL, L_EN, 0);
@@ -156,26 +202,65 @@ static void d71_layer_update(struct komeda_component *c,
        u32 __iomem *reg = c->reg;
        u32 ctrl_mask = L_EN | L_ROT(L_ROT_R270) | L_HFLIP | L_VFLIP | L_TBU_EN;
        u32 ctrl = L_EN | to_rot_ctrl(st->rot);
-       int i;
 
-       for (i = 0; i < fb->format->num_planes; i++) {
-               malidp_write32(reg,
-                              BLK_P0_PTR_LOW + i * LAYER_PER_PLANE_REGS * 4,
-                              lower_32_bits(st->addr[i]));
-               malidp_write32(reg,
-                              BLK_P0_PTR_HIGH + i * LAYER_PER_PLANE_REGS * 4,
-                              upper_32_bits(st->addr[i]));
-               if (i >= 2)
+       d71_layer_update_fb(c, kfb, st->addr);
+
+       malidp_write32(reg, AD_CONTROL, to_ad_ctrl(fb->modifier));
+       if (fb->modifier) {
+               u64 addr;
+
+               malidp_write32(reg, LAYER_AD_H_CROP, HV_CROP(st->afbc_crop_l,
+                                                            st->afbc_crop_r));
+               malidp_write32(reg, LAYER_AD_V_CROP, HV_CROP(st->afbc_crop_t,
+                                                            st->afbc_crop_b));
+               /* afbc 1.2 wants payload, afbc 1.0/1.1 wants end_addr */
+               if (fb->modifier & AFBC_FORMAT_MOD_TILED)
+                       addr = st->addr[0] + kfb->offset_payload;
+               else
+                       addr = st->addr[0] + kfb->afbc_size - 1;
+
+               malidp_write32(reg, BLK_P1_PTR_LOW, lower_32_bits(addr));
+               malidp_write32(reg, BLK_P1_PTR_HIGH, upper_32_bits(addr));
+       }
+
+       if (fb->format->is_yuv) {
+               u32 upsampling = 0;
+
+               switch (kfb->format_caps->fourcc) {
+               case DRM_FORMAT_YUYV:
+                       upsampling = fb->modifier ? LR_CHI422_BILINEAR :
+                                    LR_CHI422_REPLICATION;
+                       break;
+               case DRM_FORMAT_UYVY:
+                       upsampling = LR_CHI422_REPLICATION;
                        break;
+               case DRM_FORMAT_NV12:
+               case DRM_FORMAT_YUV420_8BIT:
+               case DRM_FORMAT_YUV420_10BIT:
+               case DRM_FORMAT_YUV420:
+               case DRM_FORMAT_P010:
+               /* these fmt support MPGE/JPEG both, here perfer JPEG*/
+                       upsampling = LR_CHI420_JPEG;
+                       break;
+               case DRM_FORMAT_X0L2:
+                       upsampling = LR_CHI420_JPEG;
+                       break;
+               default:
+                       break;
+               }
 
-               malidp_write32(reg,
-                              BLK_P0_STRIDE + i * LAYER_PER_PLANE_REGS * 4,
-                              fb->pitches[i] & 0xFFFF);
+               malidp_write32(reg, LAYER_R_CONTROL, upsampling);
+               malidp_write_group(reg, LAYER_YUV_RGB_COEFF0,
+                                  KOMEDA_N_YUV2RGB_COEFFS,
+                                  komeda_select_yuv2rgb_coeffs(
+                                       plane_st->color_encoding,
+                                       plane_st->color_range));
        }
 
-       malidp_write32(reg, LAYER_FMT, kfb->format_caps->hw_id);
        malidp_write32(reg, BLK_IN_SIZE, HV_SIZE(st->hsize, st->vsize));
 
+       if (kfb->is_va)
+               ctrl |= L_TBU_EN;
        malidp_write32_mask(reg, BLK_CONTROL, ctrl_mask, ctrl);
 }
 
@@ -245,7 +330,7 @@ static void d71_layer_dump(struct komeda_component *c, struct seq_file *sf)
        seq_printf(sf, "%sAD_V_CROP:\t\t0x%X\n", prefix, v[2]);
 }
 
-static struct komeda_component_funcs d71_layer_funcs = {
+static const struct komeda_component_funcs d71_layer_funcs = {
        .update         = d71_layer_update,
        .disable        = d71_layer_disable,
        .dump_register  = d71_layer_dump,
@@ -288,10 +373,90 @@ static int d71_layer_init(struct d71_dev *d71,
        return 0;
 }
 
+static void d71_wb_layer_update(struct komeda_component *c,
+                               struct komeda_component_state *state)
+{
+       struct komeda_layer_state *st = to_layer_st(state);
+       struct drm_connector_state *conn_st = state->wb_conn->state;
+       struct komeda_fb *kfb = to_kfb(conn_st->writeback_job->fb);
+       u32 ctrl = L_EN | LW_OFM, mask = L_EN | LW_OFM | LW_TBU_EN;
+       u32 __iomem *reg = c->reg;
+
+       d71_layer_update_fb(c, kfb, st->addr);
+
+       if (kfb->is_va)
+               ctrl |= LW_TBU_EN;
+
+       malidp_write32(reg, BLK_IN_SIZE, HV_SIZE(st->hsize, st->vsize));
+       malidp_write32(reg, BLK_INPUT_ID0, to_d71_input_id(&state->inputs[0]));
+       malidp_write32_mask(reg, BLK_CONTROL, mask, ctrl);
+}
+
+static void d71_wb_layer_dump(struct komeda_component *c, struct seq_file *sf)
+{
+       u32 v[12], i;
+
+       dump_block_header(sf, c->reg);
+
+       get_values_from_reg(c->reg, 0x80, 1, v);
+       seq_printf(sf, "LW_INPUT_ID0:\t\t0x%X\n", v[0]);
+
+       get_values_from_reg(c->reg, 0xD0, 3, v);
+       seq_printf(sf, "LW_CONTROL:\t\t0x%X\n", v[0]);
+       seq_printf(sf, "LW_PROG_LINE:\t\t0x%X\n", v[1]);
+       seq_printf(sf, "LW_FORMAT:\t\t0x%X\n", v[2]);
+
+       get_values_from_reg(c->reg, 0xE0, 1, v);
+       seq_printf(sf, "LW_IN_SIZE:\t\t0x%X\n", v[0]);
+
+       for (i = 0; i < 2; i++) {
+               get_values_from_reg(c->reg, 0x100 + i * 0x10, 3, v);
+               seq_printf(sf, "LW_P%u_PTR_LOW:\t\t0x%X\n", i, v[0]);
+               seq_printf(sf, "LW_P%u_PTR_HIGH:\t\t0x%X\n", i, v[1]);
+               seq_printf(sf, "LW_P%u_STRIDE:\t\t0x%X\n", i, v[2]);
+       }
+
+       get_values_from_reg(c->reg, 0x130, 12, v);
+       for (i = 0; i < 12; i++)
+               seq_printf(sf, "LW_RGB_YUV_COEFF%u:\t0x%X\n", i, v[i]);
+}
+
+static void d71_wb_layer_disable(struct komeda_component *c)
+{
+       malidp_write32(c->reg, BLK_INPUT_ID0, 0);
+       malidp_write32_mask(c->reg, BLK_CONTROL, L_EN, 0);
+}
+
+static const struct komeda_component_funcs d71_wb_layer_funcs = {
+       .update         = d71_wb_layer_update,
+       .disable        = d71_wb_layer_disable,
+       .dump_register  = d71_wb_layer_dump,
+};
+
 static int d71_wb_layer_init(struct d71_dev *d71,
                             struct block_header *blk, u32 __iomem *reg)
 {
-       DRM_DEBUG("Detect D71_Wb_Layer.\n");
+       struct komeda_component *c;
+       struct komeda_layer *wb_layer;
+       u32 pipe_id, layer_id;
+
+       get_resources_id(blk->block_info, &pipe_id, &layer_id);
+
+       c = komeda_component_add(&d71->pipes[pipe_id]->base, sizeof(*wb_layer),
+                                layer_id, BLOCK_INFO_INPUT_ID(blk->block_info),
+                                &d71_wb_layer_funcs,
+                                1, get_valid_inputs(blk), 0, reg,
+                                "LPU%d_LAYER_WR", pipe_id);
+       if (IS_ERR(c)) {
+               DRM_ERROR("Failed to add wb_layer component\n");
+               return PTR_ERR(c);
+       }
+
+       wb_layer = to_layer(c);
+       wb_layer->layer_type = KOMEDA_FMT_WB_LAYER;
+
+       set_range(&wb_layer->hsize_in, D71_MIN_LINE_SIZE, d71->max_line_size);
+       set_range(&wb_layer->vsize_in, D71_MIN_VERTICAL_SIZE, d71->max_vsize);
 
        return 0;
 }
@@ -303,8 +468,18 @@ static void d71_component_disable(struct komeda_component *c)
 
        malidp_write32(reg, BLK_CONTROL, 0);
 
-       for (i = 0; i < c->max_active_inputs; i++)
+       for (i = 0; i < c->max_active_inputs; i++) {
                malidp_write32(reg, BLK_INPUT_ID0 + (i << 2), 0);
+
+               /* Besides clearing the input ID to zero, D71 compiz also has
+                * input enable bit in CU_INPUTx_CONTROL which need to be
+                * cleared.
+                */
+               if (has_bit(c->id, KOMEDA_PIPELINE_COMPIZS))
+                       malidp_write32(reg, CU_INPUT0_CONTROL +
+                                      i * CU_PER_INPUT_REGS * 4,
+                                      CU_INPUT_CTRL_ALPHA(0xFF));
+       }
 }
 
 static void compiz_enable_input(u32 __iomem *id_reg,
@@ -391,7 +566,7 @@ static void d71_compiz_dump(struct komeda_component *c, struct seq_file *sf)
        seq_printf(sf, "CU_USER_HIGH:\t\t0x%X\n", v[1]);
 }
 
-static struct komeda_component_funcs d71_compiz_funcs = {
+static const struct komeda_component_funcs d71_compiz_funcs = {
        .update         = d71_compiz_update,
        .disable        = d71_component_disable,
        .dump_register  = d71_compiz_dump,
@@ -424,6 +599,174 @@ static int d71_compiz_init(struct d71_dev *d71,
        return 0;
 }
 
+static void d71_scaler_update_filter_lut(u32 __iomem *reg, u32 hsize_in,
+                                        u32 vsize_in, u32 hsize_out,
+                                        u32 vsize_out)
+{
+       u32 val = 0;
+
+       if (hsize_in <= hsize_out)
+               val  |= 0x62;
+       else if (hsize_in <= (hsize_out + hsize_out / 2))
+               val |= 0x63;
+       else if (hsize_in <= hsize_out * 2)
+               val |= 0x64;
+       else if (hsize_in <= hsize_out * 2 + (hsize_out * 3) / 4)
+               val |= 0x65;
+       else
+               val |= 0x66;
+
+       if (vsize_in <= vsize_out)
+               val  |= SC_VTSEL(0x6A);
+       else if (vsize_in <= (vsize_out + vsize_out / 2))
+               val |= SC_VTSEL(0x6B);
+       else if (vsize_in <= vsize_out * 2)
+               val |= SC_VTSEL(0x6C);
+       else if (vsize_in <= vsize_out * 2 + vsize_out * 3 / 4)
+               val |= SC_VTSEL(0x6D);
+       else
+               val |= SC_VTSEL(0x6E);
+
+       malidp_write32(reg, SC_COEFFTAB, val);
+}
+
+static void d71_scaler_update(struct komeda_component *c,
+                             struct komeda_component_state *state)
+{
+       struct komeda_scaler_state *st = to_scaler_st(state);
+       u32 __iomem *reg = c->reg;
+       u32 init_ph, delta_ph, ctrl;
+
+       d71_scaler_update_filter_lut(reg, st->hsize_in, st->vsize_in,
+                                    st->hsize_out, st->vsize_out);
+
+       malidp_write32(reg, BLK_IN_SIZE, HV_SIZE(st->hsize_in, st->vsize_in));
+       malidp_write32(reg, SC_OUT_SIZE, HV_SIZE(st->hsize_out, st->vsize_out));
+
+       init_ph = (st->hsize_in << 15) / st->hsize_out;
+       malidp_write32(reg, SC_H_INIT_PH, init_ph);
+
+       delta_ph = (st->hsize_in << 16) / st->hsize_out;
+       malidp_write32(reg, SC_H_DELTA_PH, delta_ph);
+
+       init_ph = (st->vsize_in << 15) / st->vsize_out;
+       malidp_write32(reg, SC_V_INIT_PH, init_ph);
+
+       delta_ph = (st->vsize_in << 16) / st->vsize_out;
+       malidp_write32(reg, SC_V_DELTA_PH, delta_ph);
+
+       ctrl = 0;
+       ctrl |= st->en_scaling ? SC_CTRL_SCL : 0;
+       ctrl |= st->en_alpha ? SC_CTRL_AP : 0;
+       ctrl |= st->en_img_enhancement ? SC_CTRL_IENH : 0;
+
+       malidp_write32(reg, BLK_CONTROL, ctrl);
+       malidp_write32(reg, BLK_INPUT_ID0, to_d71_input_id(&state->inputs[0]));
+}
+
+static void d71_scaler_dump(struct komeda_component *c, struct seq_file *sf)
+{
+       u32 v[9];
+
+       dump_block_header(sf, c->reg);
+
+       get_values_from_reg(c->reg, 0x80, 1, v);
+       seq_printf(sf, "SC_INPUT_ID0:\t\t0x%X\n", v[0]);
+
+       get_values_from_reg(c->reg, 0xD0, 1, v);
+       seq_printf(sf, "SC_CONTROL:\t\t0x%X\n", v[0]);
+
+       get_values_from_reg(c->reg, 0xDC, 9, v);
+       seq_printf(sf, "SC_COEFFTAB:\t\t0x%X\n", v[0]);
+       seq_printf(sf, "SC_IN_SIZE:\t\t0x%X\n", v[1]);
+       seq_printf(sf, "SC_OUT_SIZE:\t\t0x%X\n", v[2]);
+       seq_printf(sf, "SC_H_CROP:\t\t0x%X\n", v[3]);
+       seq_printf(sf, "SC_V_CROP:\t\t0x%X\n", v[4]);
+       seq_printf(sf, "SC_H_INIT_PH:\t\t0x%X\n", v[5]);
+       seq_printf(sf, "SC_H_DELTA_PH:\t\t0x%X\n", v[6]);
+       seq_printf(sf, "SC_V_INIT_PH:\t\t0x%X\n", v[7]);
+       seq_printf(sf, "SC_V_DELTA_PH:\t\t0x%X\n", v[8]);
+}
+
+static const struct komeda_component_funcs d71_scaler_funcs = {
+       .update         = d71_scaler_update,
+       .disable        = d71_component_disable,
+       .dump_register  = d71_scaler_dump,
+};
+
+static int d71_scaler_init(struct d71_dev *d71,
+                          struct block_header *blk, u32 __iomem *reg)
+{
+       struct komeda_component *c;
+       struct komeda_scaler *scaler;
+       u32 pipe_id, comp_id;
+
+       get_resources_id(blk->block_info, &pipe_id, &comp_id);
+
+       c = komeda_component_add(&d71->pipes[pipe_id]->base, sizeof(*scaler),
+                                comp_id, BLOCK_INFO_INPUT_ID(blk->block_info),
+                                &d71_scaler_funcs,
+                                1, get_valid_inputs(blk), 1, reg,
+                                "CU%d_SCALER%d",
+                                pipe_id, BLOCK_INFO_BLK_ID(blk->block_info));
+
+       if (IS_ERR(c)) {
+               DRM_ERROR("Failed to initialize scaler");
+               return PTR_ERR(c);
+       }
+
+       scaler = to_scaler(c);
+       set_range(&scaler->hsize, 4, d71->max_line_size);
+       set_range(&scaler->vsize, 4, 4096);
+       scaler->max_downscaling = 6;
+       scaler->max_upscaling = 64;
+
+       malidp_write32(c->reg, BLK_CONTROL, 0);
+
+       return 0;
+}
+
+static int d71_downscaling_clk_check(struct komeda_pipeline *pipe,
+                                    struct drm_display_mode *mode,
+                                    unsigned long aclk_rate,
+                                    struct komeda_data_flow_cfg *dflow)
+{
+       u32 h_in = dflow->in_w;
+       u32 v_in = dflow->in_h;
+       u32 v_out = dflow->out_h;
+       u64 fraction, denominator;
+
+       /* D71 downscaling must satisfy the following equation
+        *
+        *   ACLK                   h_in * v_in
+        * ------- >= ---------------------------------------------
+        *  PXLCLK     (h_total - (1 + 2 * v_in / v_out)) * v_out
+        *
+        * In only horizontal downscaling situation, the right side should be
+        * multiplied by (h_total - 3) / (h_active - 3), then equation becomes
+        *
+        *   ACLK          h_in
+        * ------- >= ----------------
+        *  PXLCLK     (h_active - 3)
+        *
+        * To avoid precision lost the equation 1 will be convert to:
+        *
+        *   ACLK             h_in * v_in
+        * ------- >= -----------------------------------
+        *  PXLCLK     (h_total -1 ) * v_out -  2 * v_in
+        */
+       if (v_in == v_out) {
+               fraction = h_in;
+               denominator = mode->hdisplay - 3;
+       } else {
+               fraction = h_in * v_in;
+               denominator = (mode->htotal - 1) * v_out -  2 * v_in;
+       }
+
+       return aclk_rate * denominator >= mode->clock * 1000 * fraction ?
+              0 : -EINVAL;
+}
+
 static void d71_improc_update(struct komeda_component *c,
                              struct komeda_component_state *state)
 {
@@ -467,7 +810,7 @@ static void d71_improc_dump(struct komeda_component *c, struct seq_file *sf)
                seq_printf(sf, "IPS_RGB_YUV_COEFF%u:\t0x%X\n", i, v[i]);
 }
 
-static struct komeda_component_funcs d71_improc_funcs = {
+static const struct komeda_component_funcs d71_improc_funcs = {
        .update         = d71_improc_update,
        .disable        = d71_component_disable,
        .dump_register  = d71_improc_dump,
@@ -580,7 +923,7 @@ static void d71_timing_ctrlr_dump(struct komeda_component *c,
        seq_printf(sf, "BS_USER:\t\t0x%X\n", v[4]);
 }
 
-static struct komeda_component_funcs d71_timing_ctrlr_funcs = {
+static const struct komeda_component_funcs d71_timing_ctrlr_funcs = {
        .update         = d71_timing_ctrlr_update,
        .disable        = d71_timing_ctrlr_disable,
        .dump_register  = d71_timing_ctrlr_dump,
@@ -644,8 +987,11 @@ int d71_probe_block(struct d71_dev *d71,
                err = d71_compiz_init(d71, blk, reg);
                break;
 
-       case D71_BLK_TYPE_CU_SPLITTER:
        case D71_BLK_TYPE_CU_SCALER:
+               err = d71_scaler_init(d71, blk, reg);
+               break;
+
+       case D71_BLK_TYPE_CU_SPLITTER:
        case D71_BLK_TYPE_CU_MERGER:
                break;
 
@@ -683,3 +1029,7 @@ int d71_probe_block(struct d71_dev *d71,
 
        return err;
 }
+
+const struct komeda_pipeline_funcs d71_pipeline_funcs = {
+       .downscaling_clk_check = d71_downscaling_clk_check,
+};