linux/drivers/media/platform/rockchip/rkisp1/rkisp1-params.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
   2/*
   3 * Rockchip ISP1 Driver - Params subdevice
   4 *
   5 * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
   6 */
   7
   8#include <media/v4l2-common.h>
   9#include <media/v4l2-event.h>
  10#include <media/v4l2-ioctl.h>
  11#include <media/videobuf2-core.h>
  12#include <media/videobuf2-vmalloc.h>    /* for ISP params */
  13
  14#include "rkisp1-common.h"
  15
  16#define RKISP1_PARAMS_DEV_NAME  RKISP1_DRIVER_NAME "_params"
  17
  18#define RKISP1_ISP_PARAMS_REQ_BUFS_MIN  2
  19#define RKISP1_ISP_PARAMS_REQ_BUFS_MAX  8
  20
  21#define RKISP1_ISP_DPCC_LINE_THRESH(n) \
  22                        (RKISP1_CIF_ISP_DPCC_LINE_THRESH_1 + 0x14 * (n))
  23#define RKISP1_ISP_DPCC_LINE_MAD_FAC(n) \
  24                        (RKISP1_CIF_ISP_DPCC_LINE_MAD_FAC_1 + 0x14 * (n))
  25#define RKISP1_ISP_DPCC_PG_FAC(n) \
  26                        (RKISP1_CIF_ISP_DPCC_PG_FAC_1 + 0x14 * (n))
  27#define RKISP1_ISP_DPCC_RND_THRESH(n) \
  28                        (RKISP1_CIF_ISP_DPCC_RND_THRESH_1 + 0x14 * (n))
  29#define RKISP1_ISP_DPCC_RG_FAC(n) \
  30                        (RKISP1_CIF_ISP_DPCC_RG_FAC_1 + 0x14 * (n))
  31#define RKISP1_ISP_CC_COEFF(n) \
  32                        (RKISP1_CIF_ISP_CC_COEFF_0 + (n) * 4)
  33
  34static inline void
  35rkisp1_param_set_bits(struct rkisp1_params *params, u32 reg, u32 bit_mask)
  36{
  37        u32 val;
  38
  39        val = rkisp1_read(params->rkisp1, reg);
  40        rkisp1_write(params->rkisp1, val | bit_mask, reg);
  41}
  42
  43static inline void
  44rkisp1_param_clear_bits(struct rkisp1_params *params, u32 reg, u32 bit_mask)
  45{
  46        u32 val;
  47
  48        val = rkisp1_read(params->rkisp1, reg);
  49        rkisp1_write(params->rkisp1, val & ~bit_mask, reg);
  50}
  51
  52/* ISP BP interface function */
  53static void rkisp1_dpcc_config(struct rkisp1_params *params,
  54                               const struct rkisp1_cif_isp_dpcc_config *arg)
  55{
  56        unsigned int i;
  57        u32 mode;
  58
  59        /* avoid to override the old enable value */
  60        mode = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_DPCC_MODE);
  61        mode &= RKISP1_CIF_ISP_DPCC_ENA;
  62        mode |= arg->mode & ~RKISP1_CIF_ISP_DPCC_ENA;
  63        rkisp1_write(params->rkisp1, mode, RKISP1_CIF_ISP_DPCC_MODE);
  64        rkisp1_write(params->rkisp1, arg->output_mode,
  65                     RKISP1_CIF_ISP_DPCC_OUTPUT_MODE);
  66        rkisp1_write(params->rkisp1, arg->set_use,
  67                     RKISP1_CIF_ISP_DPCC_SET_USE);
  68
  69        rkisp1_write(params->rkisp1, arg->methods[0].method,
  70                     RKISP1_CIF_ISP_DPCC_METHODS_SET_1);
  71        rkisp1_write(params->rkisp1, arg->methods[1].method,
  72                     RKISP1_CIF_ISP_DPCC_METHODS_SET_2);
  73        rkisp1_write(params->rkisp1, arg->methods[2].method,
  74                     RKISP1_CIF_ISP_DPCC_METHODS_SET_3);
  75        for (i = 0; i < RKISP1_CIF_ISP_DPCC_METHODS_MAX; i++) {
  76                rkisp1_write(params->rkisp1, arg->methods[i].line_thresh,
  77                             RKISP1_ISP_DPCC_LINE_THRESH(i));
  78                rkisp1_write(params->rkisp1, arg->methods[i].line_mad_fac,
  79                             RKISP1_ISP_DPCC_LINE_MAD_FAC(i));
  80                rkisp1_write(params->rkisp1, arg->methods[i].pg_fac,
  81                             RKISP1_ISP_DPCC_PG_FAC(i));
  82                rkisp1_write(params->rkisp1, arg->methods[i].rnd_thresh,
  83                             RKISP1_ISP_DPCC_RND_THRESH(i));
  84                rkisp1_write(params->rkisp1, arg->methods[i].rg_fac,
  85                             RKISP1_ISP_DPCC_RG_FAC(i));
  86        }
  87
  88        rkisp1_write(params->rkisp1, arg->rnd_offs,
  89                     RKISP1_CIF_ISP_DPCC_RND_OFFS);
  90        rkisp1_write(params->rkisp1, arg->ro_limits,
  91                     RKISP1_CIF_ISP_DPCC_RO_LIMITS);
  92}
  93
  94/* ISP black level subtraction interface function */
  95static void rkisp1_bls_config(struct rkisp1_params *params,
  96                              const struct rkisp1_cif_isp_bls_config *arg)
  97{
  98        /* avoid to override the old enable value */
  99        u32 new_control;
 100
 101        new_control = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_BLS_CTRL);
 102        new_control &= RKISP1_CIF_ISP_BLS_ENA;
 103        /* fixed subtraction values */
 104        if (!arg->enable_auto) {
 105                const struct rkisp1_cif_isp_bls_fixed_val *pval =
 106                                                                &arg->fixed_val;
 107
 108                switch (params->raw_type) {
 109                case RKISP1_RAW_BGGR:
 110                        rkisp1_write(params->rkisp1,
 111                                     pval->r, RKISP1_CIF_ISP_BLS_D_FIXED);
 112                        rkisp1_write(params->rkisp1,
 113                                     pval->gr, RKISP1_CIF_ISP_BLS_C_FIXED);
 114                        rkisp1_write(params->rkisp1,
 115                                     pval->gb, RKISP1_CIF_ISP_BLS_B_FIXED);
 116                        rkisp1_write(params->rkisp1,
 117                                     pval->b, RKISP1_CIF_ISP_BLS_A_FIXED);
 118                        break;
 119                case RKISP1_RAW_GBRG:
 120                        rkisp1_write(params->rkisp1,
 121                                     pval->r, RKISP1_CIF_ISP_BLS_C_FIXED);
 122                        rkisp1_write(params->rkisp1,
 123                                     pval->gr, RKISP1_CIF_ISP_BLS_D_FIXED);
 124                        rkisp1_write(params->rkisp1,
 125                                     pval->gb, RKISP1_CIF_ISP_BLS_A_FIXED);
 126                        rkisp1_write(params->rkisp1,
 127                                     pval->b, RKISP1_CIF_ISP_BLS_B_FIXED);
 128                        break;
 129                case RKISP1_RAW_GRBG:
 130                        rkisp1_write(params->rkisp1,
 131                                     pval->r, RKISP1_CIF_ISP_BLS_B_FIXED);
 132                        rkisp1_write(params->rkisp1,
 133                                     pval->gr, RKISP1_CIF_ISP_BLS_A_FIXED);
 134                        rkisp1_write(params->rkisp1,
 135                                     pval->gb, RKISP1_CIF_ISP_BLS_D_FIXED);
 136                        rkisp1_write(params->rkisp1,
 137                                     pval->b, RKISP1_CIF_ISP_BLS_C_FIXED);
 138                        break;
 139                case RKISP1_RAW_RGGB:
 140                        rkisp1_write(params->rkisp1,
 141                                     pval->r, RKISP1_CIF_ISP_BLS_A_FIXED);
 142                        rkisp1_write(params->rkisp1,
 143                                     pval->gr, RKISP1_CIF_ISP_BLS_B_FIXED);
 144                        rkisp1_write(params->rkisp1,
 145                                     pval->gb, RKISP1_CIF_ISP_BLS_C_FIXED);
 146                        rkisp1_write(params->rkisp1,
 147                                     pval->b, RKISP1_CIF_ISP_BLS_D_FIXED);
 148                        break;
 149                default:
 150                        break;
 151                }
 152
 153        } else {
 154                if (arg->en_windows & BIT(1)) {
 155                        rkisp1_write(params->rkisp1, arg->bls_window2.h_offs,
 156                                     RKISP1_CIF_ISP_BLS_H2_START);
 157                        rkisp1_write(params->rkisp1, arg->bls_window2.h_size,
 158                                     RKISP1_CIF_ISP_BLS_H2_STOP);
 159                        rkisp1_write(params->rkisp1, arg->bls_window2.v_offs,
 160                                     RKISP1_CIF_ISP_BLS_V2_START);
 161                        rkisp1_write(params->rkisp1, arg->bls_window2.v_size,
 162                                     RKISP1_CIF_ISP_BLS_V2_STOP);
 163                        new_control |= RKISP1_CIF_ISP_BLS_WINDOW_2;
 164                }
 165
 166                if (arg->en_windows & BIT(0)) {
 167                        rkisp1_write(params->rkisp1, arg->bls_window1.h_offs,
 168                                     RKISP1_CIF_ISP_BLS_H1_START);
 169                        rkisp1_write(params->rkisp1, arg->bls_window1.h_size,
 170                                     RKISP1_CIF_ISP_BLS_H1_STOP);
 171                        rkisp1_write(params->rkisp1, arg->bls_window1.v_offs,
 172                                     RKISP1_CIF_ISP_BLS_V1_START);
 173                        rkisp1_write(params->rkisp1, arg->bls_window1.v_size,
 174                                     RKISP1_CIF_ISP_BLS_V1_STOP);
 175                        new_control |= RKISP1_CIF_ISP_BLS_WINDOW_1;
 176                }
 177
 178                rkisp1_write(params->rkisp1, arg->bls_samples,
 179                             RKISP1_CIF_ISP_BLS_SAMPLES);
 180
 181                new_control |= RKISP1_CIF_ISP_BLS_MODE_MEASURED;
 182        }
 183        rkisp1_write(params->rkisp1, new_control, RKISP1_CIF_ISP_BLS_CTRL);
 184}
 185
 186/* ISP LS correction interface function */
 187static void
 188rkisp1_lsc_correct_matrix_config(struct rkisp1_params *params,
 189                                 const struct rkisp1_cif_isp_lsc_config *pconfig)
 190{
 191        unsigned int isp_lsc_status, sram_addr, isp_lsc_table_sel, i, j, data;
 192
 193        isp_lsc_status = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_LSC_STATUS);
 194
 195        /* RKISP1_CIF_ISP_LSC_TABLE_ADDRESS_153 = ( 17 * 18 ) >> 1 */
 196        sram_addr = (isp_lsc_status & RKISP1_CIF_ISP_LSC_ACTIVE_TABLE) ?
 197                    RKISP1_CIF_ISP_LSC_TABLE_ADDRESS_0 :
 198                    RKISP1_CIF_ISP_LSC_TABLE_ADDRESS_153;
 199        rkisp1_write(params->rkisp1, sram_addr,
 200                     RKISP1_CIF_ISP_LSC_R_TABLE_ADDR);
 201        rkisp1_write(params->rkisp1, sram_addr,
 202                     RKISP1_CIF_ISP_LSC_GR_TABLE_ADDR);
 203        rkisp1_write(params->rkisp1, sram_addr,
 204                     RKISP1_CIF_ISP_LSC_GB_TABLE_ADDR);
 205        rkisp1_write(params->rkisp1, sram_addr,
 206                     RKISP1_CIF_ISP_LSC_B_TABLE_ADDR);
 207
 208        /* program data tables (table size is 9 * 17 = 153) */
 209        for (i = 0; i < RKISP1_CIF_ISP_LSC_SAMPLES_MAX; i++) {
 210                /*
 211                 * 17 sectors with 2 values in one DWORD = 9
 212                 * DWORDs (2nd value of last DWORD unused)
 213                 */
 214                for (j = 0; j < RKISP1_CIF_ISP_LSC_SAMPLES_MAX - 1; j += 2) {
 215                        data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->r_data_tbl[i][j],
 216                                                             pconfig->r_data_tbl[i][j + 1]);
 217                        rkisp1_write(params->rkisp1, data,
 218                                     RKISP1_CIF_ISP_LSC_R_TABLE_DATA);
 219
 220                        data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->gr_data_tbl[i][j],
 221                                                             pconfig->gr_data_tbl[i][j + 1]);
 222                        rkisp1_write(params->rkisp1, data,
 223                                     RKISP1_CIF_ISP_LSC_GR_TABLE_DATA);
 224
 225                        data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->gb_data_tbl[i][j],
 226                                                             pconfig->gb_data_tbl[i][j + 1]);
 227                        rkisp1_write(params->rkisp1, data,
 228                                     RKISP1_CIF_ISP_LSC_GB_TABLE_DATA);
 229
 230                        data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->b_data_tbl[i][j],
 231                                                             pconfig->b_data_tbl[i][j + 1]);
 232                        rkisp1_write(params->rkisp1, data,
 233                                     RKISP1_CIF_ISP_LSC_B_TABLE_DATA);
 234                }
 235                data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->r_data_tbl[i][j], 0);
 236                rkisp1_write(params->rkisp1, data,
 237                             RKISP1_CIF_ISP_LSC_R_TABLE_DATA);
 238
 239                data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->gr_data_tbl[i][j], 0);
 240                rkisp1_write(params->rkisp1, data,
 241                             RKISP1_CIF_ISP_LSC_GR_TABLE_DATA);
 242
 243                data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->gb_data_tbl[i][j], 0);
 244                rkisp1_write(params->rkisp1, data,
 245                             RKISP1_CIF_ISP_LSC_GB_TABLE_DATA);
 246
 247                data = RKISP1_CIF_ISP_LSC_TABLE_DATA(pconfig->b_data_tbl[i][j], 0);
 248                rkisp1_write(params->rkisp1, data,
 249                             RKISP1_CIF_ISP_LSC_B_TABLE_DATA);
 250        }
 251        isp_lsc_table_sel = (isp_lsc_status & RKISP1_CIF_ISP_LSC_ACTIVE_TABLE) ?
 252                            RKISP1_CIF_ISP_LSC_TABLE_0 :
 253                            RKISP1_CIF_ISP_LSC_TABLE_1;
 254        rkisp1_write(params->rkisp1, isp_lsc_table_sel,
 255                     RKISP1_CIF_ISP_LSC_TABLE_SEL);
 256}
 257
 258static void rkisp1_lsc_config(struct rkisp1_params *params,
 259                              const struct rkisp1_cif_isp_lsc_config *arg)
 260{
 261        unsigned int i, data;
 262        u32 lsc_ctrl;
 263
 264        /* To config must be off , store the current status firstly */
 265        lsc_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_LSC_CTRL);
 266        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_LSC_CTRL,
 267                                RKISP1_CIF_ISP_LSC_CTRL_ENA);
 268        rkisp1_lsc_correct_matrix_config(params, arg);
 269
 270        for (i = 0; i < RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE / 2; i++) {
 271                /* program x size tables */
 272                data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->x_size_tbl[i * 2],
 273                                                    arg->x_size_tbl[i * 2 + 1]);
 274                rkisp1_write(params->rkisp1, data,
 275                             RKISP1_CIF_ISP_LSC_XSIZE_01 + i * 4);
 276
 277                /* program x grad tables */
 278                data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->x_grad_tbl[i * 2],
 279                                                    arg->x_grad_tbl[i * 2 + 1]);
 280                rkisp1_write(params->rkisp1, data,
 281                             RKISP1_CIF_ISP_LSC_XGRAD_01 + i * 4);
 282
 283                /* program y size tables */
 284                data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->y_size_tbl[i * 2],
 285                                                    arg->y_size_tbl[i * 2 + 1]);
 286                rkisp1_write(params->rkisp1, data,
 287                             RKISP1_CIF_ISP_LSC_YSIZE_01 + i * 4);
 288
 289                /* program y grad tables */
 290                data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->y_grad_tbl[i * 2],
 291                                                    arg->y_grad_tbl[i * 2 + 1]);
 292                rkisp1_write(params->rkisp1, data,
 293                             RKISP1_CIF_ISP_LSC_YGRAD_01 + i * 4);
 294        }
 295
 296        /* restore the lsc ctrl status */
 297        if (lsc_ctrl & RKISP1_CIF_ISP_LSC_CTRL_ENA) {
 298                rkisp1_param_set_bits(params,
 299                                      RKISP1_CIF_ISP_LSC_CTRL,
 300                                      RKISP1_CIF_ISP_LSC_CTRL_ENA);
 301        } else {
 302                rkisp1_param_clear_bits(params,
 303                                        RKISP1_CIF_ISP_LSC_CTRL,
 304                                        RKISP1_CIF_ISP_LSC_CTRL_ENA);
 305        }
 306}
 307
 308/* ISP Filtering function */
 309static void rkisp1_flt_config(struct rkisp1_params *params,
 310                              const struct rkisp1_cif_isp_flt_config *arg)
 311{
 312        u32 filt_mode;
 313
 314        rkisp1_write(params->rkisp1,
 315                     arg->thresh_bl0, RKISP1_CIF_ISP_FILT_THRESH_BL0);
 316        rkisp1_write(params->rkisp1,
 317                     arg->thresh_bl1, RKISP1_CIF_ISP_FILT_THRESH_BL1);
 318        rkisp1_write(params->rkisp1,
 319                     arg->thresh_sh0, RKISP1_CIF_ISP_FILT_THRESH_SH0);
 320        rkisp1_write(params->rkisp1,
 321                     arg->thresh_sh1, RKISP1_CIF_ISP_FILT_THRESH_SH1);
 322        rkisp1_write(params->rkisp1, arg->fac_bl0, RKISP1_CIF_ISP_FILT_FAC_BL0);
 323        rkisp1_write(params->rkisp1, arg->fac_bl1, RKISP1_CIF_ISP_FILT_FAC_BL1);
 324        rkisp1_write(params->rkisp1, arg->fac_mid, RKISP1_CIF_ISP_FILT_FAC_MID);
 325        rkisp1_write(params->rkisp1, arg->fac_sh0, RKISP1_CIF_ISP_FILT_FAC_SH0);
 326        rkisp1_write(params->rkisp1, arg->fac_sh1, RKISP1_CIF_ISP_FILT_FAC_SH1);
 327        rkisp1_write(params->rkisp1,
 328                     arg->lum_weight, RKISP1_CIF_ISP_FILT_LUM_WEIGHT);
 329
 330        rkisp1_write(params->rkisp1,
 331                     (arg->mode ? RKISP1_CIF_ISP_FLT_MODE_DNR : 0) |
 332                     RKISP1_CIF_ISP_FLT_CHROMA_V_MODE(arg->chr_v_mode) |
 333                     RKISP1_CIF_ISP_FLT_CHROMA_H_MODE(arg->chr_h_mode) |
 334                     RKISP1_CIF_ISP_FLT_GREEN_STAGE1(arg->grn_stage1),
 335                     RKISP1_CIF_ISP_FILT_MODE);
 336
 337        /* avoid to override the old enable value */
 338        filt_mode = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_FILT_MODE);
 339        filt_mode &= RKISP1_CIF_ISP_FLT_ENA;
 340        if (arg->mode)
 341                filt_mode |= RKISP1_CIF_ISP_FLT_MODE_DNR;
 342        filt_mode |= RKISP1_CIF_ISP_FLT_CHROMA_V_MODE(arg->chr_v_mode) |
 343                     RKISP1_CIF_ISP_FLT_CHROMA_H_MODE(arg->chr_h_mode) |
 344                     RKISP1_CIF_ISP_FLT_GREEN_STAGE1(arg->grn_stage1);
 345        rkisp1_write(params->rkisp1, filt_mode, RKISP1_CIF_ISP_FILT_MODE);
 346}
 347
 348/* ISP demosaic interface function */
 349static int rkisp1_bdm_config(struct rkisp1_params *params,
 350                             const struct rkisp1_cif_isp_bdm_config *arg)
 351{
 352        u32 bdm_th;
 353
 354        /* avoid to override the old enable value */
 355        bdm_th = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_DEMOSAIC);
 356        bdm_th &= RKISP1_CIF_ISP_DEMOSAIC_BYPASS;
 357        bdm_th |= arg->demosaic_th & ~RKISP1_CIF_ISP_DEMOSAIC_BYPASS;
 358        /* set demosaic threshold */
 359        rkisp1_write(params->rkisp1, bdm_th, RKISP1_CIF_ISP_DEMOSAIC);
 360        return 0;
 361}
 362
 363/* ISP GAMMA correction interface function */
 364static void rkisp1_sdg_config(struct rkisp1_params *params,
 365                              const struct rkisp1_cif_isp_sdg_config *arg)
 366{
 367        unsigned int i;
 368
 369        rkisp1_write(params->rkisp1,
 370                     arg->xa_pnts.gamma_dx0, RKISP1_CIF_ISP_GAMMA_DX_LO);
 371        rkisp1_write(params->rkisp1,
 372                     arg->xa_pnts.gamma_dx1, RKISP1_CIF_ISP_GAMMA_DX_HI);
 373
 374        for (i = 0; i < RKISP1_CIF_ISP_DEGAMMA_CURVE_SIZE; i++) {
 375                rkisp1_write(params->rkisp1, arg->curve_r.gamma_y[i],
 376                             RKISP1_CIF_ISP_GAMMA_R_Y0 + i * 4);
 377                rkisp1_write(params->rkisp1, arg->curve_g.gamma_y[i],
 378                             RKISP1_CIF_ISP_GAMMA_G_Y0 + i * 4);
 379                rkisp1_write(params->rkisp1, arg->curve_b.gamma_y[i],
 380                             RKISP1_CIF_ISP_GAMMA_B_Y0 + i * 4);
 381        }
 382}
 383
 384/* ISP GAMMA correction interface function */
 385static void rkisp1_goc_config(struct rkisp1_params *params,
 386                              const struct rkisp1_cif_isp_goc_config *arg)
 387{
 388        unsigned int i;
 389
 390        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL,
 391                                RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA);
 392        rkisp1_write(params->rkisp1, arg->mode, RKISP1_CIF_ISP_GAMMA_OUT_MODE);
 393
 394        for (i = 0; i < RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V10; i++)
 395                rkisp1_write(params->rkisp1, arg->gamma_y[i],
 396                             RKISP1_CIF_ISP_GAMMA_OUT_Y_0 + i * 4);
 397}
 398
 399/* ISP Cross Talk */
 400static void rkisp1_ctk_config(struct rkisp1_params *params,
 401                              const struct rkisp1_cif_isp_ctk_config *arg)
 402{
 403        unsigned int i, j, k = 0;
 404
 405        for (i = 0; i < 3; i++)
 406                for (j = 0; j < 3; j++)
 407                        rkisp1_write(params->rkisp1, arg->coeff[i][j],
 408                                     RKISP1_CIF_ISP_CT_COEFF_0 + 4 * k++);
 409        for (i = 0; i < 3; i++)
 410                rkisp1_write(params->rkisp1, arg->ct_offset[i],
 411                             RKISP1_CIF_ISP_CT_OFFSET_R + i * 4);
 412}
 413
 414static void rkisp1_ctk_enable(struct rkisp1_params *params, bool en)
 415{
 416        if (en)
 417                return;
 418
 419        /* Write back the default values. */
 420        rkisp1_write(params->rkisp1, 0x80, RKISP1_CIF_ISP_CT_COEFF_0);
 421        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_COEFF_1);
 422        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_COEFF_2);
 423        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_COEFF_3);
 424        rkisp1_write(params->rkisp1, 0x80, RKISP1_CIF_ISP_CT_COEFF_4);
 425        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_COEFF_5);
 426        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_COEFF_6);
 427        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_COEFF_7);
 428        rkisp1_write(params->rkisp1, 0x80, RKISP1_CIF_ISP_CT_COEFF_8);
 429
 430        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_OFFSET_R);
 431        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_OFFSET_G);
 432        rkisp1_write(params->rkisp1, 0, RKISP1_CIF_ISP_CT_OFFSET_B);
 433}
 434
 435/* ISP White Balance Mode */
 436static void rkisp1_awb_meas_config(struct rkisp1_params *params,
 437                                   const struct rkisp1_cif_isp_awb_meas_config *arg)
 438{
 439        u32 reg_val = 0;
 440        /* based on the mode,configure the awb module */
 441        if (arg->awb_mode == RKISP1_CIF_ISP_AWB_MODE_YCBCR) {
 442                /* Reference Cb and Cr */
 443                rkisp1_write(params->rkisp1,
 444                             RKISP1_CIF_ISP_AWB_REF_CR_SET(arg->awb_ref_cr) |
 445                             arg->awb_ref_cb, RKISP1_CIF_ISP_AWB_REF);
 446                /* Yc Threshold */
 447                rkisp1_write(params->rkisp1,
 448                             RKISP1_CIF_ISP_AWB_MAX_Y_SET(arg->max_y) |
 449                             RKISP1_CIF_ISP_AWB_MIN_Y_SET(arg->min_y) |
 450                             RKISP1_CIF_ISP_AWB_MAX_CS_SET(arg->max_csum) |
 451                             arg->min_c, RKISP1_CIF_ISP_AWB_THRESH);
 452        }
 453
 454        reg_val = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP);
 455        if (arg->enable_ymax_cmp)
 456                reg_val |= RKISP1_CIF_ISP_AWB_YMAX_CMP_EN;
 457        else
 458                reg_val &= ~RKISP1_CIF_ISP_AWB_YMAX_CMP_EN;
 459        rkisp1_write(params->rkisp1, reg_val, RKISP1_CIF_ISP_AWB_PROP);
 460
 461        /* window offset */
 462        rkisp1_write(params->rkisp1,
 463                     arg->awb_wnd.v_offs, RKISP1_CIF_ISP_AWB_WND_V_OFFS);
 464        rkisp1_write(params->rkisp1,
 465                     arg->awb_wnd.h_offs, RKISP1_CIF_ISP_AWB_WND_H_OFFS);
 466        /* AWB window size */
 467        rkisp1_write(params->rkisp1,
 468                     arg->awb_wnd.v_size, RKISP1_CIF_ISP_AWB_WND_V_SIZE);
 469        rkisp1_write(params->rkisp1,
 470                     arg->awb_wnd.h_size, RKISP1_CIF_ISP_AWB_WND_H_SIZE);
 471        /* Number of frames */
 472        rkisp1_write(params->rkisp1,
 473                     arg->frames, RKISP1_CIF_ISP_AWB_FRAMES);
 474}
 475
 476static void
 477rkisp1_awb_meas_enable(struct rkisp1_params *params,
 478                       const struct rkisp1_cif_isp_awb_meas_config *arg,
 479                       bool en)
 480{
 481        u32 reg_val = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP);
 482
 483        /* switch off */
 484        reg_val &= RKISP1_CIF_ISP_AWB_MODE_MASK_NONE;
 485
 486        if (en) {
 487                if (arg->awb_mode == RKISP1_CIF_ISP_AWB_MODE_RGB)
 488                        reg_val |= RKISP1_CIF_ISP_AWB_MODE_RGB_EN;
 489                else
 490                        reg_val |= RKISP1_CIF_ISP_AWB_MODE_YCBCR_EN;
 491
 492                rkisp1_write(params->rkisp1, reg_val, RKISP1_CIF_ISP_AWB_PROP);
 493
 494                /* Measurements require AWB block be active. */
 495                rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL,
 496                                      RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA);
 497        } else {
 498                rkisp1_write(params->rkisp1,
 499                             reg_val, RKISP1_CIF_ISP_AWB_PROP);
 500                rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL,
 501                                        RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA);
 502        }
 503}
 504
 505static void
 506rkisp1_awb_gain_config(struct rkisp1_params *params,
 507                       const struct rkisp1_cif_isp_awb_gain_config *arg)
 508{
 509        rkisp1_write(params->rkisp1,
 510                     RKISP1_CIF_ISP_AWB_GAIN_R_SET(arg->gain_green_r) |
 511                     arg->gain_green_b, RKISP1_CIF_ISP_AWB_GAIN_G);
 512
 513        rkisp1_write(params->rkisp1,
 514                     RKISP1_CIF_ISP_AWB_GAIN_R_SET(arg->gain_red) |
 515                     arg->gain_blue, RKISP1_CIF_ISP_AWB_GAIN_RB);
 516}
 517
 518static void rkisp1_aec_config(struct rkisp1_params *params,
 519                              const struct rkisp1_cif_isp_aec_config *arg)
 520{
 521        unsigned int block_hsize, block_vsize;
 522        u32 exp_ctrl;
 523
 524        /* avoid to override the old enable value */
 525        exp_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_EXP_CTRL);
 526        exp_ctrl &= RKISP1_CIF_ISP_EXP_ENA;
 527        if (arg->autostop)
 528                exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP;
 529        if (arg->mode == RKISP1_CIF_ISP_EXP_MEASURING_MODE_1)
 530                exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_MEASMODE_1;
 531        rkisp1_write(params->rkisp1, exp_ctrl, RKISP1_CIF_ISP_EXP_CTRL);
 532
 533        rkisp1_write(params->rkisp1,
 534                     arg->meas_window.h_offs, RKISP1_CIF_ISP_EXP_H_OFFSET);
 535        rkisp1_write(params->rkisp1,
 536                     arg->meas_window.v_offs, RKISP1_CIF_ISP_EXP_V_OFFSET);
 537
 538        block_hsize = arg->meas_window.h_size /
 539                      RKISP1_CIF_ISP_EXP_COLUMN_NUM - 1;
 540        block_vsize = arg->meas_window.v_size /
 541                      RKISP1_CIF_ISP_EXP_ROW_NUM - 1;
 542
 543        rkisp1_write(params->rkisp1,
 544                     RKISP1_CIF_ISP_EXP_H_SIZE_SET(block_hsize),
 545                     RKISP1_CIF_ISP_EXP_H_SIZE);
 546        rkisp1_write(params->rkisp1,
 547                     RKISP1_CIF_ISP_EXP_V_SIZE_SET(block_vsize),
 548                     RKISP1_CIF_ISP_EXP_V_SIZE);
 549}
 550
 551static void rkisp1_cproc_config(struct rkisp1_params *params,
 552                                const struct rkisp1_cif_isp_cproc_config *arg)
 553{
 554        struct rkisp1_cif_isp_isp_other_cfg *cur_other_cfg =
 555                container_of(arg, struct rkisp1_cif_isp_isp_other_cfg, cproc_config);
 556        struct rkisp1_cif_isp_ie_config *cur_ie_config =
 557                                                &cur_other_cfg->ie_config;
 558        u32 effect = cur_ie_config->effect;
 559        u32 quantization = params->quantization;
 560
 561        rkisp1_write(params->rkisp1, arg->contrast, RKISP1_CIF_C_PROC_CONTRAST);
 562        rkisp1_write(params->rkisp1, arg->hue, RKISP1_CIF_C_PROC_HUE);
 563        rkisp1_write(params->rkisp1, arg->sat, RKISP1_CIF_C_PROC_SATURATION);
 564        rkisp1_write(params->rkisp1, arg->brightness,
 565                     RKISP1_CIF_C_PROC_BRIGHTNESS);
 566
 567        if (quantization != V4L2_QUANTIZATION_FULL_RANGE ||
 568            effect != V4L2_COLORFX_NONE) {
 569                rkisp1_param_clear_bits(params, RKISP1_CIF_C_PROC_CTRL,
 570                                        RKISP1_CIF_C_PROC_YOUT_FULL |
 571                                        RKISP1_CIF_C_PROC_YIN_FULL |
 572                                        RKISP1_CIF_C_PROC_COUT_FULL);
 573        } else {
 574                rkisp1_param_set_bits(params, RKISP1_CIF_C_PROC_CTRL,
 575                                      RKISP1_CIF_C_PROC_YOUT_FULL |
 576                                      RKISP1_CIF_C_PROC_YIN_FULL |
 577                                      RKISP1_CIF_C_PROC_COUT_FULL);
 578        }
 579}
 580
 581static void rkisp1_hst_config(struct rkisp1_params *params,
 582                              const struct rkisp1_cif_isp_hst_config *arg)
 583{
 584        unsigned int block_hsize, block_vsize;
 585        static const u32 hist_weight_regs[] = {
 586                RKISP1_CIF_ISP_HIST_WEIGHT_00TO30,
 587                RKISP1_CIF_ISP_HIST_WEIGHT_40TO21,
 588                RKISP1_CIF_ISP_HIST_WEIGHT_31TO12,
 589                RKISP1_CIF_ISP_HIST_WEIGHT_22TO03,
 590                RKISP1_CIF_ISP_HIST_WEIGHT_13TO43,
 591                RKISP1_CIF_ISP_HIST_WEIGHT_04TO34,
 592        };
 593        const u8 *weight;
 594        unsigned int i;
 595        u32 hist_prop;
 596
 597        /* avoid to override the old enable value */
 598        hist_prop = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_HIST_PROP);
 599        hist_prop &= RKISP1_CIF_ISP_HIST_PROP_MODE_MASK;
 600        hist_prop |= RKISP1_CIF_ISP_HIST_PREDIV_SET(arg->histogram_predivider);
 601        rkisp1_write(params->rkisp1, hist_prop, RKISP1_CIF_ISP_HIST_PROP);
 602        rkisp1_write(params->rkisp1,
 603                     arg->meas_window.h_offs,
 604                     RKISP1_CIF_ISP_HIST_H_OFFS);
 605        rkisp1_write(params->rkisp1,
 606                     arg->meas_window.v_offs,
 607                     RKISP1_CIF_ISP_HIST_V_OFFS);
 608
 609        block_hsize = arg->meas_window.h_size /
 610                      RKISP1_CIF_ISP_HIST_COLUMN_NUM - 1;
 611        block_vsize = arg->meas_window.v_size / RKISP1_CIF_ISP_HIST_ROW_NUM - 1;
 612
 613        rkisp1_write(params->rkisp1, block_hsize, RKISP1_CIF_ISP_HIST_H_SIZE);
 614        rkisp1_write(params->rkisp1, block_vsize, RKISP1_CIF_ISP_HIST_V_SIZE);
 615
 616        weight = arg->hist_weight;
 617        for (i = 0; i < ARRAY_SIZE(hist_weight_regs); ++i, weight += 4)
 618                rkisp1_write(params->rkisp1,
 619                             RKISP1_CIF_ISP_HIST_WEIGHT_SET(weight[0],
 620                                                            weight[1],
 621                                                            weight[2],
 622                                                            weight[3]),
 623                                 hist_weight_regs[i]);
 624
 625        rkisp1_write(params->rkisp1, weight[0] & 0x1F, RKISP1_CIF_ISP_HIST_WEIGHT_44);
 626}
 627
 628static void
 629rkisp1_hst_enable(struct rkisp1_params *params,
 630                  const struct rkisp1_cif_isp_hst_config *arg, bool en)
 631{
 632        if (en) {
 633                u32 hist_prop = rkisp1_read(params->rkisp1,
 634                                            RKISP1_CIF_ISP_HIST_PROP);
 635
 636                hist_prop &= ~RKISP1_CIF_ISP_HIST_PROP_MODE_MASK;
 637                hist_prop |= arg->mode;
 638                rkisp1_param_set_bits(params, RKISP1_CIF_ISP_HIST_PROP,
 639                                      hist_prop);
 640        } else {
 641                rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_HIST_PROP,
 642                                        RKISP1_CIF_ISP_HIST_PROP_MODE_MASK);
 643        }
 644}
 645
 646static void rkisp1_afm_config(struct rkisp1_params *params,
 647                              const struct rkisp1_cif_isp_afc_config *arg)
 648{
 649        size_t num_of_win = min_t(size_t, ARRAY_SIZE(arg->afm_win),
 650                                  arg->num_afm_win);
 651        u32 afm_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AFM_CTRL);
 652        unsigned int i;
 653
 654        /* Switch off to configure. */
 655        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_AFM_CTRL,
 656                                RKISP1_CIF_ISP_AFM_ENA);
 657
 658        for (i = 0; i < num_of_win; i++) {
 659                rkisp1_write(params->rkisp1,
 660                             RKISP1_CIF_ISP_AFM_WINDOW_X(arg->afm_win[i].h_offs) |
 661                             RKISP1_CIF_ISP_AFM_WINDOW_Y(arg->afm_win[i].v_offs),
 662                             RKISP1_CIF_ISP_AFM_LT_A + i * 8);
 663                rkisp1_write(params->rkisp1,
 664                             RKISP1_CIF_ISP_AFM_WINDOW_X(arg->afm_win[i].h_size +
 665                                                         arg->afm_win[i].h_offs) |
 666                             RKISP1_CIF_ISP_AFM_WINDOW_Y(arg->afm_win[i].v_size +
 667                                                         arg->afm_win[i].v_offs),
 668                             RKISP1_CIF_ISP_AFM_RB_A + i * 8);
 669        }
 670        rkisp1_write(params->rkisp1, arg->thres, RKISP1_CIF_ISP_AFM_THRES);
 671        rkisp1_write(params->rkisp1, arg->var_shift,
 672                     RKISP1_CIF_ISP_AFM_VAR_SHIFT);
 673        /* restore afm status */
 674        rkisp1_write(params->rkisp1, afm_ctrl, RKISP1_CIF_ISP_AFM_CTRL);
 675}
 676
 677static void rkisp1_ie_config(struct rkisp1_params *params,
 678                             const struct rkisp1_cif_isp_ie_config *arg)
 679{
 680        u32 eff_ctrl;
 681
 682        eff_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_IMG_EFF_CTRL);
 683        eff_ctrl &= ~RKISP1_CIF_IMG_EFF_CTRL_MODE_MASK;
 684
 685        if (params->quantization == V4L2_QUANTIZATION_FULL_RANGE)
 686                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_YCBCR_FULL;
 687
 688        switch (arg->effect) {
 689        case V4L2_COLORFX_SEPIA:
 690                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_SEPIA;
 691                break;
 692        case V4L2_COLORFX_SET_CBCR:
 693                rkisp1_write(params->rkisp1, arg->eff_tint,
 694                             RKISP1_CIF_IMG_EFF_TINT);
 695                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_SEPIA;
 696                break;
 697                /*
 698                 * Color selection is similar to water color(AQUA):
 699                 * grayscale + selected color w threshold
 700                 */
 701        case V4L2_COLORFX_AQUA:
 702                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_COLOR_SEL;
 703                rkisp1_write(params->rkisp1, arg->color_sel,
 704                             RKISP1_CIF_IMG_EFF_COLOR_SEL);
 705                break;
 706        case V4L2_COLORFX_EMBOSS:
 707                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_EMBOSS;
 708                rkisp1_write(params->rkisp1, arg->eff_mat_1,
 709                             RKISP1_CIF_IMG_EFF_MAT_1);
 710                rkisp1_write(params->rkisp1, arg->eff_mat_2,
 711                             RKISP1_CIF_IMG_EFF_MAT_2);
 712                rkisp1_write(params->rkisp1, arg->eff_mat_3,
 713                             RKISP1_CIF_IMG_EFF_MAT_3);
 714                break;
 715        case V4L2_COLORFX_SKETCH:
 716                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_SKETCH;
 717                rkisp1_write(params->rkisp1, arg->eff_mat_3,
 718                             RKISP1_CIF_IMG_EFF_MAT_3);
 719                rkisp1_write(params->rkisp1, arg->eff_mat_4,
 720                             RKISP1_CIF_IMG_EFF_MAT_4);
 721                rkisp1_write(params->rkisp1, arg->eff_mat_5,
 722                             RKISP1_CIF_IMG_EFF_MAT_5);
 723                break;
 724        case V4L2_COLORFX_BW:
 725                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_BLACKWHITE;
 726                break;
 727        case V4L2_COLORFX_NEGATIVE:
 728                eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_NEGATIVE;
 729                break;
 730        default:
 731                break;
 732        }
 733
 734        rkisp1_write(params->rkisp1, eff_ctrl, RKISP1_CIF_IMG_EFF_CTRL);
 735}
 736
 737static void rkisp1_ie_enable(struct rkisp1_params *params, bool en)
 738{
 739        if (en) {
 740                rkisp1_param_set_bits(params, RKISP1_CIF_ICCL,
 741                                      RKISP1_CIF_ICCL_IE_CLK);
 742                rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_CTRL_ENABLE,
 743                             RKISP1_CIF_IMG_EFF_CTRL);
 744                rkisp1_param_set_bits(params, RKISP1_CIF_IMG_EFF_CTRL,
 745                                      RKISP1_CIF_IMG_EFF_CTRL_CFG_UPD);
 746        } else {
 747                rkisp1_param_clear_bits(params, RKISP1_CIF_IMG_EFF_CTRL,
 748                                        RKISP1_CIF_IMG_EFF_CTRL_ENABLE);
 749                rkisp1_param_clear_bits(params, RKISP1_CIF_ICCL,
 750                                        RKISP1_CIF_ICCL_IE_CLK);
 751        }
 752}
 753
 754static void rkisp1_csm_config(struct rkisp1_params *params, bool full_range)
 755{
 756        static const u16 full_range_coeff[] = {
 757                0x0026, 0x004b, 0x000f,
 758                0x01ea, 0x01d6, 0x0040,
 759                0x0040, 0x01ca, 0x01f6
 760        };
 761        static const u16 limited_range_coeff[] = {
 762                0x0021, 0x0040, 0x000d,
 763                0x01ed, 0x01db, 0x0038,
 764                0x0038, 0x01d1, 0x01f7,
 765        };
 766        unsigned int i;
 767
 768        if (full_range) {
 769                for (i = 0; i < ARRAY_SIZE(full_range_coeff); i++)
 770                        rkisp1_write(params->rkisp1, full_range_coeff[i],
 771                                     RKISP1_CIF_ISP_CC_COEFF_0 + i * 4);
 772
 773                rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL,
 774                                      RKISP1_CIF_ISP_CTRL_ISP_CSM_Y_FULL_ENA |
 775                                      RKISP1_CIF_ISP_CTRL_ISP_CSM_C_FULL_ENA);
 776        } else {
 777                for (i = 0; i < ARRAY_SIZE(limited_range_coeff); i++)
 778                        rkisp1_write(params->rkisp1, limited_range_coeff[i],
 779                                     RKISP1_CIF_ISP_CC_COEFF_0 + i * 4);
 780
 781                rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL,
 782                                        RKISP1_CIF_ISP_CTRL_ISP_CSM_Y_FULL_ENA |
 783                                        RKISP1_CIF_ISP_CTRL_ISP_CSM_C_FULL_ENA);
 784        }
 785}
 786
 787/* ISP De-noise Pre-Filter(DPF) function */
 788static void rkisp1_dpf_config(struct rkisp1_params *params,
 789                              const struct rkisp1_cif_isp_dpf_config *arg)
 790{
 791        unsigned int isp_dpf_mode, spatial_coeff, i;
 792
 793        switch (arg->gain.mode) {
 794        case RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_GAINS:
 795                isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_USE_NF_GAIN |
 796                               RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP;
 797                break;
 798        case RKISP1_CIF_ISP_DPF_GAIN_USAGE_LSC_GAINS:
 799                isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_LSC_GAIN_COMP;
 800                break;
 801        case RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_LSC_GAINS:
 802                isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_USE_NF_GAIN |
 803                               RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP |
 804                               RKISP1_CIF_ISP_DPF_MODE_LSC_GAIN_COMP;
 805                break;
 806        case RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_GAINS:
 807                isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP;
 808                break;
 809        case RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_LSC_GAINS:
 810                isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_LSC_GAIN_COMP |
 811                               RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP;
 812                break;
 813        case RKISP1_CIF_ISP_DPF_GAIN_USAGE_DISABLED:
 814        default:
 815                isp_dpf_mode = 0;
 816                break;
 817        }
 818
 819        if (arg->nll.scale_mode == RKISP1_CIF_ISP_NLL_SCALE_LOGARITHMIC)
 820                isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_NLL_SEGMENTATION;
 821        if (arg->rb_flt.fltsize == RKISP1_CIF_ISP_DPF_RB_FILTERSIZE_9x9)
 822                isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_RB_FLTSIZE_9x9;
 823        if (!arg->rb_flt.r_enable)
 824                isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_R_FLT_DIS;
 825        if (!arg->rb_flt.b_enable)
 826                isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_B_FLT_DIS;
 827        if (!arg->g_flt.gb_enable)
 828                isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_GB_FLT_DIS;
 829        if (!arg->g_flt.gr_enable)
 830                isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_GR_FLT_DIS;
 831
 832        rkisp1_param_set_bits(params, RKISP1_CIF_ISP_DPF_MODE,
 833                              isp_dpf_mode);
 834        rkisp1_write(params->rkisp1, arg->gain.nf_b_gain,
 835                     RKISP1_CIF_ISP_DPF_NF_GAIN_B);
 836        rkisp1_write(params->rkisp1, arg->gain.nf_r_gain,
 837                     RKISP1_CIF_ISP_DPF_NF_GAIN_R);
 838        rkisp1_write(params->rkisp1, arg->gain.nf_gb_gain,
 839                     RKISP1_CIF_ISP_DPF_NF_GAIN_GB);
 840        rkisp1_write(params->rkisp1, arg->gain.nf_gr_gain,
 841                     RKISP1_CIF_ISP_DPF_NF_GAIN_GR);
 842
 843        for (i = 0; i < RKISP1_CIF_ISP_DPF_MAX_NLF_COEFFS; i++) {
 844                rkisp1_write(params->rkisp1, arg->nll.coeff[i],
 845                             RKISP1_CIF_ISP_DPF_NULL_COEFF_0 + i * 4);
 846        }
 847
 848        spatial_coeff = arg->g_flt.spatial_coeff[0] |
 849                        (arg->g_flt.spatial_coeff[1] << 8) |
 850                        (arg->g_flt.spatial_coeff[2] << 16) |
 851                        (arg->g_flt.spatial_coeff[3] << 24);
 852        rkisp1_write(params->rkisp1, spatial_coeff,
 853                     RKISP1_CIF_ISP_DPF_S_WEIGHT_G_1_4);
 854
 855        spatial_coeff = arg->g_flt.spatial_coeff[4] |
 856                        (arg->g_flt.spatial_coeff[5] << 8);
 857        rkisp1_write(params->rkisp1, spatial_coeff,
 858                     RKISP1_CIF_ISP_DPF_S_WEIGHT_G_5_6);
 859
 860        spatial_coeff = arg->rb_flt.spatial_coeff[0] |
 861                        (arg->rb_flt.spatial_coeff[1] << 8) |
 862                        (arg->rb_flt.spatial_coeff[2] << 16) |
 863                        (arg->rb_flt.spatial_coeff[3] << 24);
 864        rkisp1_write(params->rkisp1, spatial_coeff,
 865                     RKISP1_CIF_ISP_DPF_S_WEIGHT_RB_1_4);
 866
 867        spatial_coeff = arg->rb_flt.spatial_coeff[4] |
 868                        (arg->rb_flt.spatial_coeff[5] << 8);
 869        rkisp1_write(params->rkisp1, spatial_coeff,
 870                     RKISP1_CIF_ISP_DPF_S_WEIGHT_RB_5_6);
 871}
 872
 873static void
 874rkisp1_dpf_strength_config(struct rkisp1_params *params,
 875                           const struct rkisp1_cif_isp_dpf_strength_config *arg)
 876{
 877        rkisp1_write(params->rkisp1, arg->b, RKISP1_CIF_ISP_DPF_STRENGTH_B);
 878        rkisp1_write(params->rkisp1, arg->g, RKISP1_CIF_ISP_DPF_STRENGTH_G);
 879        rkisp1_write(params->rkisp1, arg->r, RKISP1_CIF_ISP_DPF_STRENGTH_R);
 880}
 881
 882static void
 883rkisp1_isp_isr_other_config(struct rkisp1_params *params,
 884                            const struct rkisp1_params_cfg *new_params)
 885{
 886        unsigned int module_en_update, module_cfg_update, module_ens;
 887
 888        module_en_update = new_params->module_en_update;
 889        module_cfg_update = new_params->module_cfg_update;
 890        module_ens = new_params->module_ens;
 891
 892        /* update dpc config */
 893        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPCC)
 894                rkisp1_dpcc_config(params,
 895                                   &new_params->others.dpcc_config);
 896
 897        if (module_en_update & RKISP1_CIF_ISP_MODULE_DPCC) {
 898                if (module_ens & RKISP1_CIF_ISP_MODULE_DPCC)
 899                        rkisp1_param_set_bits(params,
 900                                              RKISP1_CIF_ISP_DPCC_MODE,
 901                                              RKISP1_CIF_ISP_DPCC_ENA);
 902                else
 903                        rkisp1_param_clear_bits(params,
 904                                                RKISP1_CIF_ISP_DPCC_MODE,
 905                                                RKISP1_CIF_ISP_DPCC_ENA);
 906        }
 907
 908        /* update bls config */
 909        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_BLS)
 910                rkisp1_bls_config(params,
 911                                  &new_params->others.bls_config);
 912
 913        if (module_en_update & RKISP1_CIF_ISP_MODULE_BLS) {
 914                if (module_ens & RKISP1_CIF_ISP_MODULE_BLS)
 915                        rkisp1_param_set_bits(params,
 916                                              RKISP1_CIF_ISP_BLS_CTRL,
 917                                              RKISP1_CIF_ISP_BLS_ENA);
 918                else
 919                        rkisp1_param_clear_bits(params,
 920                                                RKISP1_CIF_ISP_BLS_CTRL,
 921                                                RKISP1_CIF_ISP_BLS_ENA);
 922        }
 923
 924        /* update sdg config */
 925        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_SDG)
 926                rkisp1_sdg_config(params,
 927                                  &new_params->others.sdg_config);
 928
 929        if (module_en_update & RKISP1_CIF_ISP_MODULE_SDG) {
 930                if (module_ens & RKISP1_CIF_ISP_MODULE_SDG)
 931                        rkisp1_param_set_bits(params,
 932                                              RKISP1_CIF_ISP_CTRL,
 933                                              RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA);
 934                else
 935                        rkisp1_param_clear_bits(params,
 936                                                RKISP1_CIF_ISP_CTRL,
 937                                                RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA);
 938        }
 939
 940        /* update lsc config */
 941        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_LSC)
 942                rkisp1_lsc_config(params,
 943                                  &new_params->others.lsc_config);
 944
 945        if (module_en_update & RKISP1_CIF_ISP_MODULE_LSC) {
 946                if (module_ens & RKISP1_CIF_ISP_MODULE_LSC)
 947                        rkisp1_param_set_bits(params,
 948                                              RKISP1_CIF_ISP_LSC_CTRL,
 949                                              RKISP1_CIF_ISP_LSC_CTRL_ENA);
 950                else
 951                        rkisp1_param_clear_bits(params,
 952                                                RKISP1_CIF_ISP_LSC_CTRL,
 953                                                RKISP1_CIF_ISP_LSC_CTRL_ENA);
 954        }
 955
 956        /* update awb gains */
 957        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN)
 958                rkisp1_awb_gain_config(params, &new_params->others.awb_gain_config);
 959
 960        if (module_en_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN) {
 961                if (module_ens & RKISP1_CIF_ISP_MODULE_AWB_GAIN)
 962                        rkisp1_param_set_bits(params,
 963                                              RKISP1_CIF_ISP_CTRL,
 964                                              RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA);
 965                else
 966                        rkisp1_param_clear_bits(params,
 967                                                RKISP1_CIF_ISP_CTRL,
 968                                                RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA);
 969        }
 970
 971        /* update bdm config */
 972        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_BDM)
 973                rkisp1_bdm_config(params,
 974                                  &new_params->others.bdm_config);
 975
 976        if (module_en_update & RKISP1_CIF_ISP_MODULE_BDM) {
 977                if (module_ens & RKISP1_CIF_ISP_MODULE_BDM)
 978                        rkisp1_param_set_bits(params,
 979                                              RKISP1_CIF_ISP_DEMOSAIC,
 980                                              RKISP1_CIF_ISP_DEMOSAIC_BYPASS);
 981                else
 982                        rkisp1_param_clear_bits(params,
 983                                                RKISP1_CIF_ISP_DEMOSAIC,
 984                                                RKISP1_CIF_ISP_DEMOSAIC_BYPASS);
 985        }
 986
 987        /* update filter config */
 988        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_FLT)
 989                rkisp1_flt_config(params,
 990                                  &new_params->others.flt_config);
 991
 992        if (module_en_update & RKISP1_CIF_ISP_MODULE_FLT) {
 993                if (module_ens & RKISP1_CIF_ISP_MODULE_FLT)
 994                        rkisp1_param_set_bits(params,
 995                                              RKISP1_CIF_ISP_FILT_MODE,
 996                                              RKISP1_CIF_ISP_FLT_ENA);
 997                else
 998                        rkisp1_param_clear_bits(params,
 999                                                RKISP1_CIF_ISP_FILT_MODE,
1000                                                RKISP1_CIF_ISP_FLT_ENA);
1001        }
1002
1003        /* update ctk config */
1004        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_CTK)
1005                rkisp1_ctk_config(params,
1006                                  &new_params->others.ctk_config);
1007
1008        if (module_en_update & RKISP1_CIF_ISP_MODULE_CTK)
1009                rkisp1_ctk_enable(params, !!(module_ens & RKISP1_CIF_ISP_MODULE_CTK));
1010
1011        /* update goc config */
1012        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_GOC)
1013                rkisp1_goc_config(params,
1014                                  &new_params->others.goc_config);
1015
1016        if (module_en_update & RKISP1_CIF_ISP_MODULE_GOC) {
1017                if (module_ens & RKISP1_CIF_ISP_MODULE_GOC)
1018                        rkisp1_param_set_bits(params,
1019                                              RKISP1_CIF_ISP_CTRL,
1020                                              RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA);
1021                else
1022                        rkisp1_param_clear_bits(params,
1023                                                RKISP1_CIF_ISP_CTRL,
1024                                                RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA);
1025        }
1026
1027        /* update cproc config */
1028        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_CPROC)
1029                rkisp1_cproc_config(params,
1030                                    &new_params->others.cproc_config);
1031
1032        if (module_en_update & RKISP1_CIF_ISP_MODULE_CPROC) {
1033                if (module_ens & RKISP1_CIF_ISP_MODULE_CPROC)
1034                        rkisp1_param_set_bits(params,
1035                                              RKISP1_CIF_C_PROC_CTRL,
1036                                              RKISP1_CIF_C_PROC_CTR_ENABLE);
1037                else
1038                        rkisp1_param_clear_bits(params,
1039                                                RKISP1_CIF_C_PROC_CTRL,
1040                                                RKISP1_CIF_C_PROC_CTR_ENABLE);
1041        }
1042
1043        /* update ie config */
1044        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_IE)
1045                rkisp1_ie_config(params, &new_params->others.ie_config);
1046
1047        if (module_en_update & RKISP1_CIF_ISP_MODULE_IE)
1048                rkisp1_ie_enable(params, !!(module_ens & RKISP1_CIF_ISP_MODULE_IE));
1049
1050        /* update dpf config */
1051        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF)
1052                rkisp1_dpf_config(params, &new_params->others.dpf_config);
1053
1054        if (module_en_update & RKISP1_CIF_ISP_MODULE_DPF) {
1055                if (module_ens & RKISP1_CIF_ISP_MODULE_DPF)
1056                        rkisp1_param_set_bits(params,
1057                                              RKISP1_CIF_ISP_DPF_MODE,
1058                                              RKISP1_CIF_ISP_DPF_MODE_EN);
1059                else
1060                        rkisp1_param_clear_bits(params,
1061                                                RKISP1_CIF_ISP_DPF_MODE,
1062                                                RKISP1_CIF_ISP_DPF_MODE_EN);
1063        }
1064
1065        if ((module_en_update & RKISP1_CIF_ISP_MODULE_DPF_STRENGTH) ||
1066            (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF_STRENGTH)) {
1067                /* update dpf strength config */
1068                rkisp1_dpf_strength_config(params,
1069                                           &new_params->others.dpf_strength_config);
1070        }
1071}
1072
1073static void rkisp1_isp_isr_meas_config(struct rkisp1_params *params,
1074                                       struct  rkisp1_params_cfg *new_params)
1075{
1076        unsigned int module_en_update, module_cfg_update, module_ens;
1077
1078        module_en_update = new_params->module_en_update;
1079        module_cfg_update = new_params->module_cfg_update;
1080        module_ens = new_params->module_ens;
1081
1082        /* update awb config */
1083        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB)
1084                rkisp1_awb_meas_config(params, &new_params->meas.awb_meas_config);
1085
1086        if (module_en_update & RKISP1_CIF_ISP_MODULE_AWB)
1087                rkisp1_awb_meas_enable(params,
1088                                       &new_params->meas.awb_meas_config,
1089                                       !!(module_ens & RKISP1_CIF_ISP_MODULE_AWB));
1090
1091        /* update afc config */
1092        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AFC)
1093                rkisp1_afm_config(params,
1094                                  &new_params->meas.afc_config);
1095
1096        if (module_en_update & RKISP1_CIF_ISP_MODULE_AFC) {
1097                if (module_ens & RKISP1_CIF_ISP_MODULE_AFC)
1098                        rkisp1_param_set_bits(params,
1099                                              RKISP1_CIF_ISP_AFM_CTRL,
1100                                              RKISP1_CIF_ISP_AFM_ENA);
1101                else
1102                        rkisp1_param_clear_bits(params,
1103                                                RKISP1_CIF_ISP_AFM_CTRL,
1104                                                RKISP1_CIF_ISP_AFM_ENA);
1105        }
1106
1107        /* update hst config */
1108        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_HST)
1109                rkisp1_hst_config(params,
1110                                  &new_params->meas.hst_config);
1111
1112        if (module_en_update & RKISP1_CIF_ISP_MODULE_HST)
1113                rkisp1_hst_enable(params,
1114                                  &new_params->meas.hst_config,
1115                                  !!(module_ens & RKISP1_CIF_ISP_MODULE_HST));
1116
1117        /* update aec config */
1118        if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AEC)
1119                rkisp1_aec_config(params,
1120                                  &new_params->meas.aec_config);
1121
1122        if (module_en_update & RKISP1_CIF_ISP_MODULE_AEC) {
1123                if (module_ens & RKISP1_CIF_ISP_MODULE_AEC)
1124                        rkisp1_param_set_bits(params,
1125                                              RKISP1_CIF_ISP_EXP_CTRL,
1126                                              RKISP1_CIF_ISP_EXP_ENA);
1127                else
1128                        rkisp1_param_clear_bits(params,
1129                                                RKISP1_CIF_ISP_EXP_CTRL,
1130                                                RKISP1_CIF_ISP_EXP_ENA);
1131        }
1132}
1133
1134static void rkisp1_params_apply_params_cfg(struct rkisp1_params *params,
1135                                           unsigned int frame_sequence)
1136{
1137        struct rkisp1_params_cfg *new_params;
1138        struct rkisp1_buffer *cur_buf = NULL;
1139
1140        if (list_empty(&params->params))
1141                return;
1142
1143        cur_buf = list_first_entry(&params->params,
1144                                   struct rkisp1_buffer, queue);
1145
1146        new_params = (struct rkisp1_params_cfg *)(cur_buf->vaddr);
1147
1148        rkisp1_isp_isr_other_config(params, new_params);
1149        rkisp1_isp_isr_meas_config(params, new_params);
1150
1151        /* update shadow register immediately */
1152        rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL, RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD);
1153
1154        list_del(&cur_buf->queue);
1155
1156        cur_buf->vb.sequence = frame_sequence;
1157        vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
1158}
1159
1160void rkisp1_params_isr(struct rkisp1_device *rkisp1)
1161{
1162        /*
1163         * This isr is called when the ISR finishes processing a frame (RKISP1_CIF_ISP_FRAME).
1164         * Configurations performed here will be applied on the next frame.
1165         * Since frame_sequence is updated on the vertical sync signal, we should use
1166         * frame_sequence + 1 here to indicate to userspace on which frame these parameters
1167         * are being applied.
1168         */
1169        unsigned int frame_sequence = rkisp1->isp.frame_sequence + 1;
1170        struct rkisp1_params *params = &rkisp1->params;
1171
1172        spin_lock(&params->config_lock);
1173        rkisp1_params_apply_params_cfg(params, frame_sequence);
1174
1175        spin_unlock(&params->config_lock);
1176}
1177
1178static const struct rkisp1_cif_isp_awb_meas_config rkisp1_awb_params_default_config = {
1179        {
1180                0, 0, RKISP1_DEFAULT_WIDTH, RKISP1_DEFAULT_HEIGHT
1181        },
1182        RKISP1_CIF_ISP_AWB_MODE_YCBCR, 200, 30, 20, 20, 0, 128, 128
1183};
1184
1185static const struct rkisp1_cif_isp_aec_config rkisp1_aec_params_default_config = {
1186        RKISP1_CIF_ISP_EXP_MEASURING_MODE_0,
1187        RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_0,
1188        {
1189                RKISP1_DEFAULT_WIDTH >> 2, RKISP1_DEFAULT_HEIGHT >> 2,
1190                RKISP1_DEFAULT_WIDTH >> 1, RKISP1_DEFAULT_HEIGHT >> 1
1191        }
1192};
1193
1194static const struct rkisp1_cif_isp_hst_config rkisp1_hst_params_default_config = {
1195        RKISP1_CIF_ISP_HISTOGRAM_MODE_RGB_COMBINED,
1196        3,
1197        {
1198                RKISP1_DEFAULT_WIDTH >> 2, RKISP1_DEFAULT_HEIGHT >> 2,
1199                RKISP1_DEFAULT_WIDTH >> 1, RKISP1_DEFAULT_HEIGHT >> 1
1200        },
1201        {
1202                0, /* To be filled in with 0x01 at runtime. */
1203        }
1204};
1205
1206static const struct rkisp1_cif_isp_afc_config rkisp1_afc_params_default_config = {
1207        1,
1208        {
1209                {
1210                        300, 225, 200, 150
1211                }
1212        },
1213        4,
1214        14
1215};
1216
1217static void rkisp1_params_config_parameter(struct rkisp1_params *params)
1218{
1219        struct rkisp1_cif_isp_hst_config hst = rkisp1_hst_params_default_config;
1220
1221        rkisp1_awb_meas_config(params, &rkisp1_awb_params_default_config);
1222        rkisp1_awb_meas_enable(params, &rkisp1_awb_params_default_config,
1223                               true);
1224
1225        rkisp1_aec_config(params, &rkisp1_aec_params_default_config);
1226        rkisp1_param_set_bits(params, RKISP1_CIF_ISP_EXP_CTRL,
1227                              RKISP1_CIF_ISP_EXP_ENA);
1228
1229        rkisp1_afm_config(params, &rkisp1_afc_params_default_config);
1230        rkisp1_param_set_bits(params, RKISP1_CIF_ISP_AFM_CTRL,
1231                              RKISP1_CIF_ISP_AFM_ENA);
1232
1233        memset(hst.hist_weight, 0x01, sizeof(hst.hist_weight));
1234        rkisp1_hst_config(params, &hst);
1235        rkisp1_param_set_bits(params, RKISP1_CIF_ISP_HIST_PROP,
1236                              rkisp1_hst_params_default_config.mode);
1237
1238        /* set the  range */
1239        if (params->quantization == V4L2_QUANTIZATION_FULL_RANGE)
1240                rkisp1_csm_config(params, true);
1241        else
1242                rkisp1_csm_config(params, false);
1243
1244        spin_lock_irq(&params->config_lock);
1245
1246        /* apply the first buffer if there is one already */
1247        rkisp1_params_apply_params_cfg(params, 0);
1248
1249        spin_unlock_irq(&params->config_lock);
1250}
1251
1252void rkisp1_params_configure(struct rkisp1_params *params,
1253                             enum rkisp1_fmt_raw_pat_type bayer_pat,
1254                             enum v4l2_quantization quantization)
1255{
1256        params->quantization = quantization;
1257        params->raw_type = bayer_pat;
1258        rkisp1_params_config_parameter(params);
1259}
1260
1261/*
1262 * Not called when the camera is active, therefore there is no need to acquire
1263 * a lock.
1264 */
1265void rkisp1_params_disable(struct rkisp1_params *params)
1266{
1267        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DPCC_MODE,
1268                                RKISP1_CIF_ISP_DPCC_ENA);
1269        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_LSC_CTRL,
1270                                RKISP1_CIF_ISP_LSC_CTRL_ENA);
1271        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_BLS_CTRL,
1272                                RKISP1_CIF_ISP_BLS_ENA);
1273        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL,
1274                                RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA);
1275        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL,
1276                                RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA);
1277        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DEMOSAIC,
1278                                RKISP1_CIF_ISP_DEMOSAIC_BYPASS);
1279        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_FILT_MODE,
1280                                RKISP1_CIF_ISP_FLT_ENA);
1281        rkisp1_awb_meas_enable(params, NULL, false);
1282        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL,
1283                                RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA);
1284        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_EXP_CTRL,
1285                                RKISP1_CIF_ISP_EXP_ENA);
1286        rkisp1_ctk_enable(params, false);
1287        rkisp1_param_clear_bits(params, RKISP1_CIF_C_PROC_CTRL,
1288                                RKISP1_CIF_C_PROC_CTR_ENABLE);
1289        rkisp1_hst_enable(params, NULL, false);
1290        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_AFM_CTRL,
1291                                RKISP1_CIF_ISP_AFM_ENA);
1292        rkisp1_ie_enable(params, false);
1293        rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DPF_MODE,
1294                                RKISP1_CIF_ISP_DPF_MODE_EN);
1295}
1296
1297static int rkisp1_params_enum_fmt_meta_out(struct file *file, void *priv,
1298                                           struct v4l2_fmtdesc *f)
1299{
1300        struct video_device *video = video_devdata(file);
1301        struct rkisp1_params *params = video_get_drvdata(video);
1302
1303        if (f->index > 0 || f->type != video->queue->type)
1304                return -EINVAL;
1305
1306        f->pixelformat = params->vdev_fmt.fmt.meta.dataformat;
1307
1308        return 0;
1309}
1310
1311static int rkisp1_params_g_fmt_meta_out(struct file *file, void *fh,
1312                                        struct v4l2_format *f)
1313{
1314        struct video_device *video = video_devdata(file);
1315        struct rkisp1_params *params = video_get_drvdata(video);
1316        struct v4l2_meta_format *meta = &f->fmt.meta;
1317
1318        if (f->type != video->queue->type)
1319                return -EINVAL;
1320
1321        memset(meta, 0, sizeof(*meta));
1322        meta->dataformat = params->vdev_fmt.fmt.meta.dataformat;
1323        meta->buffersize = params->vdev_fmt.fmt.meta.buffersize;
1324
1325        return 0;
1326}
1327
1328static int rkisp1_params_querycap(struct file *file,
1329                                  void *priv, struct v4l2_capability *cap)
1330{
1331        struct video_device *vdev = video_devdata(file);
1332
1333        strscpy(cap->driver, RKISP1_DRIVER_NAME, sizeof(cap->driver));
1334        strscpy(cap->card, vdev->name, sizeof(cap->card));
1335        strscpy(cap->bus_info, RKISP1_BUS_INFO, sizeof(cap->bus_info));
1336
1337        return 0;
1338}
1339
1340/* ISP params video device IOCTLs */
1341static const struct v4l2_ioctl_ops rkisp1_params_ioctl = {
1342        .vidioc_reqbufs = vb2_ioctl_reqbufs,
1343        .vidioc_querybuf = vb2_ioctl_querybuf,
1344        .vidioc_create_bufs = vb2_ioctl_create_bufs,
1345        .vidioc_qbuf = vb2_ioctl_qbuf,
1346        .vidioc_dqbuf = vb2_ioctl_dqbuf,
1347        .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1348        .vidioc_expbuf = vb2_ioctl_expbuf,
1349        .vidioc_streamon = vb2_ioctl_streamon,
1350        .vidioc_streamoff = vb2_ioctl_streamoff,
1351        .vidioc_enum_fmt_meta_out = rkisp1_params_enum_fmt_meta_out,
1352        .vidioc_g_fmt_meta_out = rkisp1_params_g_fmt_meta_out,
1353        .vidioc_s_fmt_meta_out = rkisp1_params_g_fmt_meta_out,
1354        .vidioc_try_fmt_meta_out = rkisp1_params_g_fmt_meta_out,
1355        .vidioc_querycap = rkisp1_params_querycap,
1356        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1357        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1358};
1359
1360static int rkisp1_params_vb2_queue_setup(struct vb2_queue *vq,
1361                                         unsigned int *num_buffers,
1362                                         unsigned int *num_planes,
1363                                         unsigned int sizes[],
1364                                         struct device *alloc_devs[])
1365{
1366        *num_buffers = clamp_t(u32, *num_buffers,
1367                               RKISP1_ISP_PARAMS_REQ_BUFS_MIN,
1368                               RKISP1_ISP_PARAMS_REQ_BUFS_MAX);
1369
1370        *num_planes = 1;
1371
1372        sizes[0] = sizeof(struct rkisp1_params_cfg);
1373
1374        return 0;
1375}
1376
1377static void rkisp1_params_vb2_buf_queue(struct vb2_buffer *vb)
1378{
1379        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1380        struct rkisp1_buffer *params_buf =
1381                container_of(vbuf, struct rkisp1_buffer, vb);
1382        struct vb2_queue *vq = vb->vb2_queue;
1383        struct rkisp1_params *params = vq->drv_priv;
1384
1385        params_buf->vaddr = vb2_plane_vaddr(vb, 0);
1386        spin_lock_irq(&params->config_lock);
1387        list_add_tail(&params_buf->queue, &params->params);
1388        spin_unlock_irq(&params->config_lock);
1389}
1390
1391static int rkisp1_params_vb2_buf_prepare(struct vb2_buffer *vb)
1392{
1393        if (vb2_plane_size(vb, 0) < sizeof(struct rkisp1_params_cfg))
1394                return -EINVAL;
1395
1396        vb2_set_plane_payload(vb, 0, sizeof(struct rkisp1_params_cfg));
1397
1398        return 0;
1399}
1400
1401static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
1402{
1403        struct rkisp1_params *params = vq->drv_priv;
1404        struct rkisp1_buffer *buf;
1405        LIST_HEAD(tmp_list);
1406
1407        /*
1408         * we first move the buffers into a local list 'tmp_list'
1409         * and then we can iterate it and call vb2_buffer_done
1410         * without holding the lock
1411         */
1412        spin_lock_irq(&params->config_lock);
1413        list_splice_init(&params->params, &tmp_list);
1414        spin_unlock_irq(&params->config_lock);
1415
1416        list_for_each_entry(buf, &tmp_list, queue)
1417                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1418}
1419
1420static const struct vb2_ops rkisp1_params_vb2_ops = {
1421        .queue_setup = rkisp1_params_vb2_queue_setup,
1422        .wait_prepare = vb2_ops_wait_prepare,
1423        .wait_finish = vb2_ops_wait_finish,
1424        .buf_queue = rkisp1_params_vb2_buf_queue,
1425        .buf_prepare = rkisp1_params_vb2_buf_prepare,
1426        .stop_streaming = rkisp1_params_vb2_stop_streaming,
1427
1428};
1429
1430static const struct v4l2_file_operations rkisp1_params_fops = {
1431        .mmap = vb2_fop_mmap,
1432        .unlocked_ioctl = video_ioctl2,
1433        .poll = vb2_fop_poll,
1434        .open = v4l2_fh_open,
1435        .release = vb2_fop_release
1436};
1437
1438static int rkisp1_params_init_vb2_queue(struct vb2_queue *q,
1439                                        struct rkisp1_params *params)
1440{
1441        struct rkisp1_vdev_node *node;
1442
1443        node = container_of(q, struct rkisp1_vdev_node, buf_queue);
1444
1445        q->type = V4L2_BUF_TYPE_META_OUTPUT;
1446        q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1447        q->drv_priv = params;
1448        q->ops = &rkisp1_params_vb2_ops;
1449        q->mem_ops = &vb2_vmalloc_memops;
1450        q->buf_struct_size = sizeof(struct rkisp1_buffer);
1451        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1452        q->lock = &node->vlock;
1453
1454        return vb2_queue_init(q);
1455}
1456
1457static void rkisp1_init_params(struct rkisp1_params *params)
1458{
1459        params->vdev_fmt.fmt.meta.dataformat =
1460                V4L2_META_FMT_RK_ISP1_PARAMS;
1461        params->vdev_fmt.fmt.meta.buffersize =
1462                sizeof(struct rkisp1_params_cfg);
1463}
1464
1465int rkisp1_params_register(struct rkisp1_device *rkisp1)
1466{
1467        struct rkisp1_params *params = &rkisp1->params;
1468        struct rkisp1_vdev_node *node = &params->vnode;
1469        struct video_device *vdev = &node->vdev;
1470        int ret;
1471
1472        params->rkisp1 = rkisp1;
1473        mutex_init(&node->vlock);
1474        INIT_LIST_HEAD(&params->params);
1475        spin_lock_init(&params->config_lock);
1476
1477        strscpy(vdev->name, RKISP1_PARAMS_DEV_NAME, sizeof(vdev->name));
1478
1479        video_set_drvdata(vdev, params);
1480        vdev->ioctl_ops = &rkisp1_params_ioctl;
1481        vdev->fops = &rkisp1_params_fops;
1482        vdev->release = video_device_release_empty;
1483        /*
1484         * Provide a mutex to v4l2 core. It will be used
1485         * to protect all fops and v4l2 ioctls.
1486         */
1487        vdev->lock = &node->vlock;
1488        vdev->v4l2_dev = &rkisp1->v4l2_dev;
1489        vdev->queue = &node->buf_queue;
1490        vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_META_OUTPUT;
1491        vdev->vfl_dir = VFL_DIR_TX;
1492        rkisp1_params_init_vb2_queue(vdev->queue, params);
1493        rkisp1_init_params(params);
1494        video_set_drvdata(vdev, params);
1495
1496        node->pad.flags = MEDIA_PAD_FL_SOURCE;
1497        ret = media_entity_pads_init(&vdev->entity, 1, &node->pad);
1498        if (ret)
1499                return ret;
1500        ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1501        if (ret) {
1502                dev_err(rkisp1->dev,
1503                        "failed to register %s, ret=%d\n", vdev->name, ret);
1504                goto err_cleanup_media_entity;
1505        }
1506        return 0;
1507err_cleanup_media_entity:
1508        media_entity_cleanup(&vdev->entity);
1509        return ret;
1510}
1511
1512void rkisp1_params_unregister(struct rkisp1_device *rkisp1)
1513{
1514        struct rkisp1_params *params = &rkisp1->params;
1515        struct rkisp1_vdev_node *node = &params->vnode;
1516        struct video_device *vdev = &node->vdev;
1517
1518        vb2_video_unregister_device(vdev);
1519        media_entity_cleanup(&vdev->entity);
1520}
1521