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 34 static inline void 35 rkisp1_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 43 static inline void 44 rkisp1_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 */ 53 static 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 */ 95 static 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 */ 187 static void 188 rkisp1_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 258 static 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 */ 309 static 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 */ 349 static 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 */ 364 static 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 */ 385 static 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 */ 400 static 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 414 static 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 */ 436 static 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 476 static void 477 rkisp1_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 505 static void 506 rkisp1_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 518 static 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 551 static 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 581 static 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 628 static void 629 rkisp1_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 646 static 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 677 static 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 737 static 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 754 static 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 */ 788 static 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 873 static void 874 rkisp1_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 882 static void 883 rkisp1_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 if ((module_en_update & RKISP1_CIF_ISP_MODULE_DPCC) || 893 (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPCC)) { 894 /*update dpc config */ 895 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPCC) 896 rkisp1_dpcc_config(params, 897 &new_params->others.dpcc_config); 898 899 if (module_en_update & RKISP1_CIF_ISP_MODULE_DPCC) { 900 if (module_ens & RKISP1_CIF_ISP_MODULE_DPCC) 901 rkisp1_param_set_bits(params, 902 RKISP1_CIF_ISP_DPCC_MODE, 903 RKISP1_CIF_ISP_DPCC_ENA); 904 else 905 rkisp1_param_clear_bits(params, 906 RKISP1_CIF_ISP_DPCC_MODE, 907 RKISP1_CIF_ISP_DPCC_ENA); 908 } 909 } 910 911 if ((module_en_update & RKISP1_CIF_ISP_MODULE_BLS) || 912 (module_cfg_update & RKISP1_CIF_ISP_MODULE_BLS)) { 913 /* update bls config */ 914 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_BLS) 915 rkisp1_bls_config(params, 916 &new_params->others.bls_config); 917 918 if (module_en_update & RKISP1_CIF_ISP_MODULE_BLS) { 919 if (module_ens & RKISP1_CIF_ISP_MODULE_BLS) 920 rkisp1_param_set_bits(params, 921 RKISP1_CIF_ISP_BLS_CTRL, 922 RKISP1_CIF_ISP_BLS_ENA); 923 else 924 rkisp1_param_clear_bits(params, 925 RKISP1_CIF_ISP_BLS_CTRL, 926 RKISP1_CIF_ISP_BLS_ENA); 927 } 928 } 929 930 if ((module_en_update & RKISP1_CIF_ISP_MODULE_SDG) || 931 (module_cfg_update & RKISP1_CIF_ISP_MODULE_SDG)) { 932 /* update sdg config */ 933 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_SDG) 934 rkisp1_sdg_config(params, 935 &new_params->others.sdg_config); 936 937 if (module_en_update & RKISP1_CIF_ISP_MODULE_SDG) { 938 if (module_ens & RKISP1_CIF_ISP_MODULE_SDG) 939 rkisp1_param_set_bits(params, 940 RKISP1_CIF_ISP_CTRL, 941 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA); 942 else 943 rkisp1_param_clear_bits(params, 944 RKISP1_CIF_ISP_CTRL, 945 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA); 946 } 947 } 948 949 if ((module_en_update & RKISP1_CIF_ISP_MODULE_LSC) || 950 (module_cfg_update & RKISP1_CIF_ISP_MODULE_LSC)) { 951 /* update lsc config */ 952 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_LSC) 953 rkisp1_lsc_config(params, 954 &new_params->others.lsc_config); 955 956 if (module_en_update & RKISP1_CIF_ISP_MODULE_LSC) { 957 if (module_ens & RKISP1_CIF_ISP_MODULE_LSC) 958 rkisp1_param_set_bits(params, 959 RKISP1_CIF_ISP_LSC_CTRL, 960 RKISP1_CIF_ISP_LSC_CTRL_ENA); 961 else 962 rkisp1_param_clear_bits(params, 963 RKISP1_CIF_ISP_LSC_CTRL, 964 RKISP1_CIF_ISP_LSC_CTRL_ENA); 965 } 966 } 967 968 if ((module_en_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN) || 969 (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN)) { 970 /* update awb gains */ 971 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN) 972 rkisp1_awb_gain_config(params, 973 &new_params->others.awb_gain_config); 974 975 if (module_en_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN) { 976 if (module_ens & RKISP1_CIF_ISP_MODULE_AWB_GAIN) 977 rkisp1_param_set_bits(params, 978 RKISP1_CIF_ISP_CTRL, 979 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 980 else 981 rkisp1_param_clear_bits(params, 982 RKISP1_CIF_ISP_CTRL, 983 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 984 } 985 } 986 987 if ((module_en_update & RKISP1_CIF_ISP_MODULE_BDM) || 988 (module_cfg_update & RKISP1_CIF_ISP_MODULE_BDM)) { 989 /* update bdm config */ 990 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_BDM) 991 rkisp1_bdm_config(params, 992 &new_params->others.bdm_config); 993 994 if (module_en_update & RKISP1_CIF_ISP_MODULE_BDM) { 995 if (module_ens & RKISP1_CIF_ISP_MODULE_BDM) 996 rkisp1_param_set_bits(params, 997 RKISP1_CIF_ISP_DEMOSAIC, 998 RKISP1_CIF_ISP_DEMOSAIC_BYPASS); 999 else 1000 rkisp1_param_clear_bits(params, 1001 RKISP1_CIF_ISP_DEMOSAIC, 1002 RKISP1_CIF_ISP_DEMOSAIC_BYPASS); 1003 } 1004 } 1005 1006 if ((module_en_update & RKISP1_CIF_ISP_MODULE_FLT) || 1007 (module_cfg_update & RKISP1_CIF_ISP_MODULE_FLT)) { 1008 /* update filter config */ 1009 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_FLT) 1010 rkisp1_flt_config(params, 1011 &new_params->others.flt_config); 1012 1013 if (module_en_update & RKISP1_CIF_ISP_MODULE_FLT) { 1014 if (module_ens & RKISP1_CIF_ISP_MODULE_FLT) 1015 rkisp1_param_set_bits(params, 1016 RKISP1_CIF_ISP_FILT_MODE, 1017 RKISP1_CIF_ISP_FLT_ENA); 1018 else 1019 rkisp1_param_clear_bits(params, 1020 RKISP1_CIF_ISP_FILT_MODE, 1021 RKISP1_CIF_ISP_FLT_ENA); 1022 } 1023 } 1024 1025 if ((module_en_update & RKISP1_CIF_ISP_MODULE_CTK) || 1026 (module_cfg_update & RKISP1_CIF_ISP_MODULE_CTK)) { 1027 /* update ctk config */ 1028 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_CTK) 1029 rkisp1_ctk_config(params, 1030 &new_params->others.ctk_config); 1031 1032 if (module_en_update & RKISP1_CIF_ISP_MODULE_CTK) 1033 rkisp1_ctk_enable(params, 1034 !!(module_ens & RKISP1_CIF_ISP_MODULE_CTK)); 1035 } 1036 1037 if ((module_en_update & RKISP1_CIF_ISP_MODULE_GOC) || 1038 (module_cfg_update & RKISP1_CIF_ISP_MODULE_GOC)) { 1039 /* update goc config */ 1040 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_GOC) 1041 rkisp1_goc_config(params, 1042 &new_params->others.goc_config); 1043 1044 if (module_en_update & RKISP1_CIF_ISP_MODULE_GOC) { 1045 if (module_ens & RKISP1_CIF_ISP_MODULE_GOC) 1046 rkisp1_param_set_bits(params, 1047 RKISP1_CIF_ISP_CTRL, 1048 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 1049 else 1050 rkisp1_param_clear_bits(params, 1051 RKISP1_CIF_ISP_CTRL, 1052 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 1053 } 1054 } 1055 1056 if ((module_en_update & RKISP1_CIF_ISP_MODULE_CPROC) || 1057 (module_cfg_update & RKISP1_CIF_ISP_MODULE_CPROC)) { 1058 /* update cproc config */ 1059 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_CPROC) { 1060 rkisp1_cproc_config(params, 1061 &new_params->others.cproc_config); 1062 } 1063 1064 if (module_en_update & RKISP1_CIF_ISP_MODULE_CPROC) { 1065 if (module_ens & RKISP1_CIF_ISP_MODULE_CPROC) 1066 rkisp1_param_set_bits(params, 1067 RKISP1_CIF_C_PROC_CTRL, 1068 RKISP1_CIF_C_PROC_CTR_ENABLE); 1069 else 1070 rkisp1_param_clear_bits(params, 1071 RKISP1_CIF_C_PROC_CTRL, 1072 RKISP1_CIF_C_PROC_CTR_ENABLE); 1073 } 1074 } 1075 1076 if ((module_en_update & RKISP1_CIF_ISP_MODULE_IE) || 1077 (module_cfg_update & RKISP1_CIF_ISP_MODULE_IE)) { 1078 /* update ie config */ 1079 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_IE) 1080 rkisp1_ie_config(params, 1081 &new_params->others.ie_config); 1082 1083 if (module_en_update & RKISP1_CIF_ISP_MODULE_IE) 1084 rkisp1_ie_enable(params, 1085 !!(module_ens & RKISP1_CIF_ISP_MODULE_IE)); 1086 } 1087 1088 if ((module_en_update & RKISP1_CIF_ISP_MODULE_DPF) || 1089 (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF)) { 1090 /* update dpf config */ 1091 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF) 1092 rkisp1_dpf_config(params, 1093 &new_params->others.dpf_config); 1094 1095 if (module_en_update & RKISP1_CIF_ISP_MODULE_DPF) { 1096 if (module_ens & RKISP1_CIF_ISP_MODULE_DPF) 1097 rkisp1_param_set_bits(params, 1098 RKISP1_CIF_ISP_DPF_MODE, 1099 RKISP1_CIF_ISP_DPF_MODE_EN); 1100 else 1101 rkisp1_param_clear_bits(params, 1102 RKISP1_CIF_ISP_DPF_MODE, 1103 RKISP1_CIF_ISP_DPF_MODE_EN); 1104 } 1105 } 1106 1107 if ((module_en_update & RKISP1_CIF_ISP_MODULE_DPF_STRENGTH) || 1108 (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF_STRENGTH)) { 1109 /* update dpf strength config */ 1110 rkisp1_dpf_strength_config(params, 1111 &new_params->others.dpf_strength_config); 1112 } 1113 } 1114 1115 static void rkisp1_isp_isr_meas_config(struct rkisp1_params *params, 1116 struct rkisp1_params_cfg *new_params) 1117 { 1118 unsigned int module_en_update, module_cfg_update, module_ens; 1119 1120 module_en_update = new_params->module_en_update; 1121 module_cfg_update = new_params->module_cfg_update; 1122 module_ens = new_params->module_ens; 1123 1124 if ((module_en_update & RKISP1_CIF_ISP_MODULE_AWB) || 1125 (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB)) { 1126 /* update awb config */ 1127 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB) 1128 rkisp1_awb_meas_config(params, 1129 &new_params->meas.awb_meas_config); 1130 1131 if (module_en_update & RKISP1_CIF_ISP_MODULE_AWB) 1132 rkisp1_awb_meas_enable(params, 1133 &new_params->meas.awb_meas_config, 1134 !!(module_ens & RKISP1_CIF_ISP_MODULE_AWB)); 1135 } 1136 1137 if ((module_en_update & RKISP1_CIF_ISP_MODULE_AFC) || 1138 (module_cfg_update & RKISP1_CIF_ISP_MODULE_AFC)) { 1139 /* update afc config */ 1140 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AFC) 1141 rkisp1_afm_config(params, 1142 &new_params->meas.afc_config); 1143 1144 if (module_en_update & RKISP1_CIF_ISP_MODULE_AFC) { 1145 if (module_ens & RKISP1_CIF_ISP_MODULE_AFC) 1146 rkisp1_param_set_bits(params, 1147 RKISP1_CIF_ISP_AFM_CTRL, 1148 RKISP1_CIF_ISP_AFM_ENA); 1149 else 1150 rkisp1_param_clear_bits(params, 1151 RKISP1_CIF_ISP_AFM_CTRL, 1152 RKISP1_CIF_ISP_AFM_ENA); 1153 } 1154 } 1155 1156 if ((module_en_update & RKISP1_CIF_ISP_MODULE_HST) || 1157 (module_cfg_update & RKISP1_CIF_ISP_MODULE_HST)) { 1158 /* update hst config */ 1159 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_HST) 1160 rkisp1_hst_config(params, 1161 &new_params->meas.hst_config); 1162 1163 if (module_en_update & RKISP1_CIF_ISP_MODULE_HST) 1164 rkisp1_hst_enable(params, 1165 &new_params->meas.hst_config, 1166 !!(module_ens & RKISP1_CIF_ISP_MODULE_HST)); 1167 } 1168 1169 if ((module_en_update & RKISP1_CIF_ISP_MODULE_AEC) || 1170 (module_cfg_update & RKISP1_CIF_ISP_MODULE_AEC)) { 1171 /* update aec config */ 1172 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AEC) 1173 rkisp1_aec_config(params, 1174 &new_params->meas.aec_config); 1175 1176 if (module_en_update & RKISP1_CIF_ISP_MODULE_AEC) { 1177 if (module_ens & RKISP1_CIF_ISP_MODULE_AEC) 1178 rkisp1_param_set_bits(params, 1179 RKISP1_CIF_ISP_EXP_CTRL, 1180 RKISP1_CIF_ISP_EXP_ENA); 1181 else 1182 rkisp1_param_clear_bits(params, 1183 RKISP1_CIF_ISP_EXP_CTRL, 1184 RKISP1_CIF_ISP_EXP_ENA); 1185 } 1186 } 1187 } 1188 1189 static void rkisp1_params_apply_params_cfg(struct rkisp1_params *params, 1190 unsigned int frame_sequence) 1191 { 1192 struct rkisp1_params_cfg *new_params; 1193 struct rkisp1_buffer *cur_buf = NULL; 1194 1195 if (list_empty(¶ms->params)) 1196 return; 1197 1198 cur_buf = list_first_entry(¶ms->params, 1199 struct rkisp1_buffer, queue); 1200 1201 new_params = (struct rkisp1_params_cfg *)(cur_buf->vaddr); 1202 1203 rkisp1_isp_isr_other_config(params, new_params); 1204 rkisp1_isp_isr_meas_config(params, new_params); 1205 1206 /* update shadow register immediately */ 1207 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL, RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD); 1208 1209 list_del(&cur_buf->queue); 1210 1211 cur_buf->vb.sequence = frame_sequence; 1212 vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); 1213 } 1214 1215 void rkisp1_params_isr(struct rkisp1_device *rkisp1) 1216 { 1217 /* 1218 * This isr is called when the ISR finishes processing a frame (RKISP1_CIF_ISP_FRAME). 1219 * Configurations performed here will be applied on the next frame. 1220 * Since frame_sequence is updated on the vertical sync signal, we should use 1221 * frame_sequence + 1 here to indicate to userspace on which frame these parameters 1222 * are being applied. 1223 */ 1224 unsigned int frame_sequence = rkisp1->isp.frame_sequence + 1; 1225 struct rkisp1_params *params = &rkisp1->params; 1226 1227 spin_lock(¶ms->config_lock); 1228 rkisp1_params_apply_params_cfg(params, frame_sequence); 1229 1230 spin_unlock(¶ms->config_lock); 1231 } 1232 1233 static const struct rkisp1_cif_isp_awb_meas_config rkisp1_awb_params_default_config = { 1234 { 1235 0, 0, RKISP1_DEFAULT_WIDTH, RKISP1_DEFAULT_HEIGHT 1236 }, 1237 RKISP1_CIF_ISP_AWB_MODE_YCBCR, 200, 30, 20, 20, 0, 128, 128 1238 }; 1239 1240 static const struct rkisp1_cif_isp_aec_config rkisp1_aec_params_default_config = { 1241 RKISP1_CIF_ISP_EXP_MEASURING_MODE_0, 1242 RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_0, 1243 { 1244 RKISP1_DEFAULT_WIDTH >> 2, RKISP1_DEFAULT_HEIGHT >> 2, 1245 RKISP1_DEFAULT_WIDTH >> 1, RKISP1_DEFAULT_HEIGHT >> 1 1246 } 1247 }; 1248 1249 static const struct rkisp1_cif_isp_hst_config rkisp1_hst_params_default_config = { 1250 RKISP1_CIF_ISP_HISTOGRAM_MODE_RGB_COMBINED, 1251 3, 1252 { 1253 RKISP1_DEFAULT_WIDTH >> 2, RKISP1_DEFAULT_HEIGHT >> 2, 1254 RKISP1_DEFAULT_WIDTH >> 1, RKISP1_DEFAULT_HEIGHT >> 1 1255 }, 1256 { 1257 0, /* To be filled in with 0x01 at runtime. */ 1258 } 1259 }; 1260 1261 static const struct rkisp1_cif_isp_afc_config rkisp1_afc_params_default_config = { 1262 1, 1263 { 1264 { 1265 300, 225, 200, 150 1266 } 1267 }, 1268 4, 1269 14 1270 }; 1271 1272 static void rkisp1_params_config_parameter(struct rkisp1_params *params) 1273 { 1274 struct rkisp1_cif_isp_hst_config hst = rkisp1_hst_params_default_config; 1275 1276 rkisp1_awb_meas_config(params, &rkisp1_awb_params_default_config); 1277 rkisp1_awb_meas_enable(params, &rkisp1_awb_params_default_config, 1278 true); 1279 1280 rkisp1_aec_config(params, &rkisp1_aec_params_default_config); 1281 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_EXP_CTRL, 1282 RKISP1_CIF_ISP_EXP_ENA); 1283 1284 rkisp1_afm_config(params, &rkisp1_afc_params_default_config); 1285 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_AFM_CTRL, 1286 RKISP1_CIF_ISP_AFM_ENA); 1287 1288 memset(hst.hist_weight, 0x01, sizeof(hst.hist_weight)); 1289 rkisp1_hst_config(params, &hst); 1290 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_HIST_PROP, 1291 rkisp1_hst_params_default_config.mode); 1292 1293 /* set the range */ 1294 if (params->quantization == V4L2_QUANTIZATION_FULL_RANGE) 1295 rkisp1_csm_config(params, true); 1296 else 1297 rkisp1_csm_config(params, false); 1298 1299 spin_lock_irq(¶ms->config_lock); 1300 1301 /* apply the first buffer if there is one already */ 1302 rkisp1_params_apply_params_cfg(params, 0); 1303 1304 spin_unlock_irq(¶ms->config_lock); 1305 } 1306 1307 void rkisp1_params_configure(struct rkisp1_params *params, 1308 enum rkisp1_fmt_raw_pat_type bayer_pat, 1309 enum v4l2_quantization quantization) 1310 { 1311 params->quantization = quantization; 1312 params->raw_type = bayer_pat; 1313 rkisp1_params_config_parameter(params); 1314 } 1315 1316 /* Not called when the camera active, thus not isr protection. */ 1317 void rkisp1_params_disable(struct rkisp1_params *params) 1318 { 1319 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DPCC_MODE, 1320 RKISP1_CIF_ISP_DPCC_ENA); 1321 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_LSC_CTRL, 1322 RKISP1_CIF_ISP_LSC_CTRL_ENA); 1323 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_BLS_CTRL, 1324 RKISP1_CIF_ISP_BLS_ENA); 1325 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1326 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA); 1327 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1328 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 1329 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DEMOSAIC, 1330 RKISP1_CIF_ISP_DEMOSAIC_BYPASS); 1331 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_FILT_MODE, 1332 RKISP1_CIF_ISP_FLT_ENA); 1333 rkisp1_awb_meas_enable(params, NULL, false); 1334 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1335 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 1336 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_EXP_CTRL, 1337 RKISP1_CIF_ISP_EXP_ENA); 1338 rkisp1_ctk_enable(params, false); 1339 rkisp1_param_clear_bits(params, RKISP1_CIF_C_PROC_CTRL, 1340 RKISP1_CIF_C_PROC_CTR_ENABLE); 1341 rkisp1_hst_enable(params, NULL, false); 1342 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_AFM_CTRL, 1343 RKISP1_CIF_ISP_AFM_ENA); 1344 rkisp1_ie_enable(params, false); 1345 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DPF_MODE, 1346 RKISP1_CIF_ISP_DPF_MODE_EN); 1347 } 1348 1349 static int rkisp1_params_enum_fmt_meta_out(struct file *file, void *priv, 1350 struct v4l2_fmtdesc *f) 1351 { 1352 struct video_device *video = video_devdata(file); 1353 struct rkisp1_params *params = video_get_drvdata(video); 1354 1355 if (f->index > 0 || f->type != video->queue->type) 1356 return -EINVAL; 1357 1358 f->pixelformat = params->vdev_fmt.fmt.meta.dataformat; 1359 1360 return 0; 1361 } 1362 1363 static int rkisp1_params_g_fmt_meta_out(struct file *file, void *fh, 1364 struct v4l2_format *f) 1365 { 1366 struct video_device *video = video_devdata(file); 1367 struct rkisp1_params *params = video_get_drvdata(video); 1368 struct v4l2_meta_format *meta = &f->fmt.meta; 1369 1370 if (f->type != video->queue->type) 1371 return -EINVAL; 1372 1373 memset(meta, 0, sizeof(*meta)); 1374 meta->dataformat = params->vdev_fmt.fmt.meta.dataformat; 1375 meta->buffersize = params->vdev_fmt.fmt.meta.buffersize; 1376 1377 return 0; 1378 } 1379 1380 static int rkisp1_params_querycap(struct file *file, 1381 void *priv, struct v4l2_capability *cap) 1382 { 1383 struct video_device *vdev = video_devdata(file); 1384 1385 strscpy(cap->driver, RKISP1_DRIVER_NAME, sizeof(cap->driver)); 1386 strscpy(cap->card, vdev->name, sizeof(cap->card)); 1387 strscpy(cap->bus_info, RKISP1_BUS_INFO, sizeof(cap->bus_info)); 1388 1389 return 0; 1390 } 1391 1392 /* ISP params video device IOCTLs */ 1393 static const struct v4l2_ioctl_ops rkisp1_params_ioctl = { 1394 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1395 .vidioc_querybuf = vb2_ioctl_querybuf, 1396 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1397 .vidioc_qbuf = vb2_ioctl_qbuf, 1398 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1399 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1400 .vidioc_expbuf = vb2_ioctl_expbuf, 1401 .vidioc_streamon = vb2_ioctl_streamon, 1402 .vidioc_streamoff = vb2_ioctl_streamoff, 1403 .vidioc_enum_fmt_meta_out = rkisp1_params_enum_fmt_meta_out, 1404 .vidioc_g_fmt_meta_out = rkisp1_params_g_fmt_meta_out, 1405 .vidioc_s_fmt_meta_out = rkisp1_params_g_fmt_meta_out, 1406 .vidioc_try_fmt_meta_out = rkisp1_params_g_fmt_meta_out, 1407 .vidioc_querycap = rkisp1_params_querycap, 1408 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1409 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1410 }; 1411 1412 static int rkisp1_params_vb2_queue_setup(struct vb2_queue *vq, 1413 unsigned int *num_buffers, 1414 unsigned int *num_planes, 1415 unsigned int sizes[], 1416 struct device *alloc_devs[]) 1417 { 1418 *num_buffers = clamp_t(u32, *num_buffers, 1419 RKISP1_ISP_PARAMS_REQ_BUFS_MIN, 1420 RKISP1_ISP_PARAMS_REQ_BUFS_MAX); 1421 1422 *num_planes = 1; 1423 1424 sizes[0] = sizeof(struct rkisp1_params_cfg); 1425 1426 return 0; 1427 } 1428 1429 static void rkisp1_params_vb2_buf_queue(struct vb2_buffer *vb) 1430 { 1431 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1432 struct rkisp1_buffer *params_buf = 1433 container_of(vbuf, struct rkisp1_buffer, vb); 1434 struct vb2_queue *vq = vb->vb2_queue; 1435 struct rkisp1_params *params = vq->drv_priv; 1436 1437 params_buf->vaddr = vb2_plane_vaddr(vb, 0); 1438 spin_lock_irq(¶ms->config_lock); 1439 list_add_tail(¶ms_buf->queue, ¶ms->params); 1440 spin_unlock_irq(¶ms->config_lock); 1441 } 1442 1443 static int rkisp1_params_vb2_buf_prepare(struct vb2_buffer *vb) 1444 { 1445 if (vb2_plane_size(vb, 0) < sizeof(struct rkisp1_params_cfg)) 1446 return -EINVAL; 1447 1448 vb2_set_plane_payload(vb, 0, sizeof(struct rkisp1_params_cfg)); 1449 1450 return 0; 1451 } 1452 1453 static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) 1454 { 1455 struct rkisp1_params *params = vq->drv_priv; 1456 struct rkisp1_buffer *buf; 1457 LIST_HEAD(tmp_list); 1458 1459 /* 1460 * we first move the buffers into a local list 'tmp_list' 1461 * and then we can iterate it and call vb2_buffer_done 1462 * without holding the lock 1463 */ 1464 spin_lock_irq(¶ms->config_lock); 1465 list_splice_init(¶ms->params, &tmp_list); 1466 spin_unlock_irq(¶ms->config_lock); 1467 1468 list_for_each_entry(buf, &tmp_list, queue) 1469 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 1470 } 1471 1472 static const struct vb2_ops rkisp1_params_vb2_ops = { 1473 .queue_setup = rkisp1_params_vb2_queue_setup, 1474 .wait_prepare = vb2_ops_wait_prepare, 1475 .wait_finish = vb2_ops_wait_finish, 1476 .buf_queue = rkisp1_params_vb2_buf_queue, 1477 .buf_prepare = rkisp1_params_vb2_buf_prepare, 1478 .stop_streaming = rkisp1_params_vb2_stop_streaming, 1479 1480 }; 1481 1482 static const struct v4l2_file_operations rkisp1_params_fops = { 1483 .mmap = vb2_fop_mmap, 1484 .unlocked_ioctl = video_ioctl2, 1485 .poll = vb2_fop_poll, 1486 .open = v4l2_fh_open, 1487 .release = vb2_fop_release 1488 }; 1489 1490 static int rkisp1_params_init_vb2_queue(struct vb2_queue *q, 1491 struct rkisp1_params *params) 1492 { 1493 struct rkisp1_vdev_node *node; 1494 1495 node = container_of(q, struct rkisp1_vdev_node, buf_queue); 1496 1497 q->type = V4L2_BUF_TYPE_META_OUTPUT; 1498 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1499 q->drv_priv = params; 1500 q->ops = &rkisp1_params_vb2_ops; 1501 q->mem_ops = &vb2_vmalloc_memops; 1502 q->buf_struct_size = sizeof(struct rkisp1_buffer); 1503 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1504 q->lock = &node->vlock; 1505 1506 return vb2_queue_init(q); 1507 } 1508 1509 static void rkisp1_init_params(struct rkisp1_params *params) 1510 { 1511 params->vdev_fmt.fmt.meta.dataformat = 1512 V4L2_META_FMT_RK_ISP1_PARAMS; 1513 params->vdev_fmt.fmt.meta.buffersize = 1514 sizeof(struct rkisp1_params_cfg); 1515 } 1516 1517 int rkisp1_params_register(struct rkisp1_device *rkisp1) 1518 { 1519 struct rkisp1_params *params = &rkisp1->params; 1520 struct rkisp1_vdev_node *node = ¶ms->vnode; 1521 struct video_device *vdev = &node->vdev; 1522 int ret; 1523 1524 params->rkisp1 = rkisp1; 1525 mutex_init(&node->vlock); 1526 INIT_LIST_HEAD(¶ms->params); 1527 spin_lock_init(¶ms->config_lock); 1528 1529 strscpy(vdev->name, RKISP1_PARAMS_DEV_NAME, sizeof(vdev->name)); 1530 1531 video_set_drvdata(vdev, params); 1532 vdev->ioctl_ops = &rkisp1_params_ioctl; 1533 vdev->fops = &rkisp1_params_fops; 1534 vdev->release = video_device_release_empty; 1535 /* 1536 * Provide a mutex to v4l2 core. It will be used 1537 * to protect all fops and v4l2 ioctls. 1538 */ 1539 vdev->lock = &node->vlock; 1540 vdev->v4l2_dev = &rkisp1->v4l2_dev; 1541 vdev->queue = &node->buf_queue; 1542 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_META_OUTPUT; 1543 vdev->vfl_dir = VFL_DIR_TX; 1544 rkisp1_params_init_vb2_queue(vdev->queue, params); 1545 rkisp1_init_params(params); 1546 video_set_drvdata(vdev, params); 1547 1548 node->pad.flags = MEDIA_PAD_FL_SOURCE; 1549 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad); 1550 if (ret) 1551 return ret; 1552 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1553 if (ret) { 1554 dev_err(rkisp1->dev, 1555 "failed to register %s, ret=%d\n", vdev->name, ret); 1556 goto err_cleanup_media_entity; 1557 } 1558 return 0; 1559 err_cleanup_media_entity: 1560 media_entity_cleanup(&vdev->entity); 1561 return ret; 1562 } 1563 1564 void rkisp1_params_unregister(struct rkisp1_device *rkisp1) 1565 { 1566 struct rkisp1_params *params = &rkisp1->params; 1567 struct rkisp1_vdev_node *node = ¶ms->vnode; 1568 struct video_device *vdev = &node->vdev; 1569 1570 vb2_video_unregister_device(vdev); 1571 media_entity_cleanup(&vdev->entity); 1572 } 1573