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, reg, val | bit_mask); 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, reg, val & ~bit_mask); 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, RKISP1_CIF_ISP_DPCC_MODE, mode); 64 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_OUTPUT_MODE, 65 arg->output_mode); 66 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_SET_USE, 67 arg->set_use); 68 69 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_METHODS_SET_1, 70 arg->methods[0].method); 71 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_METHODS_SET_2, 72 arg->methods[1].method); 73 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_METHODS_SET_3, 74 arg->methods[2].method); 75 for (i = 0; i < RKISP1_CIF_ISP_DPCC_METHODS_MAX; i++) { 76 rkisp1_write(params->rkisp1, RKISP1_ISP_DPCC_LINE_THRESH(i), 77 arg->methods[i].line_thresh); 78 rkisp1_write(params->rkisp1, RKISP1_ISP_DPCC_LINE_MAD_FAC(i), 79 arg->methods[i].line_mad_fac); 80 rkisp1_write(params->rkisp1, RKISP1_ISP_DPCC_PG_FAC(i), 81 arg->methods[i].pg_fac); 82 rkisp1_write(params->rkisp1, RKISP1_ISP_DPCC_RND_THRESH(i), 83 arg->methods[i].rnd_thresh); 84 rkisp1_write(params->rkisp1, RKISP1_ISP_DPCC_RG_FAC(i), 85 arg->methods[i].rg_fac); 86 } 87 88 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_RND_OFFS, 89 arg->rnd_offs); 90 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPCC_RO_LIMITS, 91 arg->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, RKISP1_CIF_ISP_BLS_D_FIXED, 111 pval->r); 112 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_C_FIXED, 113 pval->gr); 114 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_B_FIXED, 115 pval->gb); 116 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_A_FIXED, 117 pval->b); 118 break; 119 case RKISP1_RAW_GBRG: 120 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_C_FIXED, 121 pval->r); 122 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_D_FIXED, 123 pval->gr); 124 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_A_FIXED, 125 pval->gb); 126 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_B_FIXED, 127 pval->b); 128 break; 129 case RKISP1_RAW_GRBG: 130 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_B_FIXED, 131 pval->r); 132 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_A_FIXED, 133 pval->gr); 134 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_D_FIXED, 135 pval->gb); 136 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_C_FIXED, 137 pval->b); 138 break; 139 case RKISP1_RAW_RGGB: 140 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_A_FIXED, 141 pval->r); 142 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_B_FIXED, 143 pval->gr); 144 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_C_FIXED, 145 pval->gb); 146 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_D_FIXED, 147 pval->b); 148 break; 149 default: 150 break; 151 } 152 153 } else { 154 if (arg->en_windows & BIT(1)) { 155 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_H2_START, 156 arg->bls_window2.h_offs); 157 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_H2_STOP, 158 arg->bls_window2.h_size); 159 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_V2_START, 160 arg->bls_window2.v_offs); 161 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_V2_STOP, 162 arg->bls_window2.v_size); 163 new_control |= RKISP1_CIF_ISP_BLS_WINDOW_2; 164 } 165 166 if (arg->en_windows & BIT(0)) { 167 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_H1_START, 168 arg->bls_window1.h_offs); 169 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_H1_STOP, 170 arg->bls_window1.h_size); 171 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_V1_START, 172 arg->bls_window1.v_offs); 173 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_V1_STOP, 174 arg->bls_window1.v_size); 175 new_control |= RKISP1_CIF_ISP_BLS_WINDOW_1; 176 } 177 178 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_SAMPLES, 179 arg->bls_samples); 180 181 new_control |= RKISP1_CIF_ISP_BLS_MODE_MEASURED; 182 } 183 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_BLS_CTRL, new_control); 184 } 185 186 /* ISP LS correction interface function */ 187 static void 188 rkisp1_lsc_matrix_config_v10(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, RKISP1_CIF_ISP_LSC_R_TABLE_ADDR, sram_addr); 200 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GR_TABLE_ADDR, sram_addr); 201 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GB_TABLE_ADDR, sram_addr); 202 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_B_TABLE_ADDR, sram_addr); 203 204 /* program data tables (table size is 9 * 17 = 153) */ 205 for (i = 0; i < RKISP1_CIF_ISP_LSC_SAMPLES_MAX; i++) { 206 /* 207 * 17 sectors with 2 values in one DWORD = 9 208 * DWORDs (2nd value of last DWORD unused) 209 */ 210 for (j = 0; j < RKISP1_CIF_ISP_LSC_SAMPLES_MAX - 1; j += 2) { 211 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->r_data_tbl[i][j], 212 pconfig->r_data_tbl[i][j + 1]); 213 rkisp1_write(params->rkisp1, 214 RKISP1_CIF_ISP_LSC_R_TABLE_DATA, data); 215 216 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->gr_data_tbl[i][j], 217 pconfig->gr_data_tbl[i][j + 1]); 218 rkisp1_write(params->rkisp1, 219 RKISP1_CIF_ISP_LSC_GR_TABLE_DATA, data); 220 221 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->gb_data_tbl[i][j], 222 pconfig->gb_data_tbl[i][j + 1]); 223 rkisp1_write(params->rkisp1, 224 RKISP1_CIF_ISP_LSC_GB_TABLE_DATA, data); 225 226 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->b_data_tbl[i][j], 227 pconfig->b_data_tbl[i][j + 1]); 228 rkisp1_write(params->rkisp1, 229 RKISP1_CIF_ISP_LSC_B_TABLE_DATA, data); 230 } 231 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->r_data_tbl[i][j], 0); 232 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_R_TABLE_DATA, 233 data); 234 235 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->gr_data_tbl[i][j], 0); 236 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GR_TABLE_DATA, 237 data); 238 239 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->gb_data_tbl[i][j], 0); 240 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GB_TABLE_DATA, 241 data); 242 243 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V10(pconfig->b_data_tbl[i][j], 0); 244 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_B_TABLE_DATA, 245 data); 246 } 247 isp_lsc_table_sel = (isp_lsc_status & RKISP1_CIF_ISP_LSC_ACTIVE_TABLE) ? 248 RKISP1_CIF_ISP_LSC_TABLE_0 : 249 RKISP1_CIF_ISP_LSC_TABLE_1; 250 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_TABLE_SEL, 251 isp_lsc_table_sel); 252 } 253 254 static void 255 rkisp1_lsc_matrix_config_v12(struct rkisp1_params *params, 256 const struct rkisp1_cif_isp_lsc_config *pconfig) 257 { 258 unsigned int isp_lsc_status, sram_addr, isp_lsc_table_sel, i, j, data; 259 260 isp_lsc_status = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_LSC_STATUS); 261 262 /* RKISP1_CIF_ISP_LSC_TABLE_ADDRESS_153 = ( 17 * 18 ) >> 1 */ 263 sram_addr = (isp_lsc_status & RKISP1_CIF_ISP_LSC_ACTIVE_TABLE) ? 264 RKISP1_CIF_ISP_LSC_TABLE_ADDRESS_0 : 265 RKISP1_CIF_ISP_LSC_TABLE_ADDRESS_153; 266 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_R_TABLE_ADDR, sram_addr); 267 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GR_TABLE_ADDR, sram_addr); 268 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GB_TABLE_ADDR, sram_addr); 269 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_B_TABLE_ADDR, sram_addr); 270 271 /* program data tables (table size is 9 * 17 = 153) */ 272 for (i = 0; i < RKISP1_CIF_ISP_LSC_SAMPLES_MAX; i++) { 273 /* 274 * 17 sectors with 2 values in one DWORD = 9 275 * DWORDs (2nd value of last DWORD unused) 276 */ 277 for (j = 0; j < RKISP1_CIF_ISP_LSC_SAMPLES_MAX - 1; j += 2) { 278 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12( 279 pconfig->r_data_tbl[i][j], 280 pconfig->r_data_tbl[i][j + 1]); 281 rkisp1_write(params->rkisp1, 282 RKISP1_CIF_ISP_LSC_R_TABLE_DATA, data); 283 284 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12( 285 pconfig->gr_data_tbl[i][j], 286 pconfig->gr_data_tbl[i][j + 1]); 287 rkisp1_write(params->rkisp1, 288 RKISP1_CIF_ISP_LSC_GR_TABLE_DATA, data); 289 290 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12( 291 pconfig->gb_data_tbl[i][j], 292 pconfig->gb_data_tbl[i][j + 1]); 293 rkisp1_write(params->rkisp1, 294 RKISP1_CIF_ISP_LSC_GB_TABLE_DATA, data); 295 296 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12( 297 pconfig->b_data_tbl[i][j], 298 pconfig->b_data_tbl[i][j + 1]); 299 rkisp1_write(params->rkisp1, 300 RKISP1_CIF_ISP_LSC_B_TABLE_DATA, data); 301 } 302 303 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12(pconfig->r_data_tbl[i][j], 0); 304 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_R_TABLE_DATA, 305 data); 306 307 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12(pconfig->gr_data_tbl[i][j], 0); 308 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GR_TABLE_DATA, 309 data); 310 311 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12(pconfig->gb_data_tbl[i][j], 0); 312 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_GB_TABLE_DATA, 313 data); 314 315 data = RKISP1_CIF_ISP_LSC_TABLE_DATA_V12(pconfig->b_data_tbl[i][j], 0); 316 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_B_TABLE_DATA, 317 data); 318 } 319 isp_lsc_table_sel = (isp_lsc_status & RKISP1_CIF_ISP_LSC_ACTIVE_TABLE) ? 320 RKISP1_CIF_ISP_LSC_TABLE_0 : 321 RKISP1_CIF_ISP_LSC_TABLE_1; 322 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_LSC_TABLE_SEL, 323 isp_lsc_table_sel); 324 } 325 326 static void rkisp1_lsc_config(struct rkisp1_params *params, 327 const struct rkisp1_cif_isp_lsc_config *arg) 328 { 329 unsigned int i, data; 330 u32 lsc_ctrl; 331 332 /* To config must be off , store the current status firstly */ 333 lsc_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_LSC_CTRL); 334 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_LSC_CTRL, 335 RKISP1_CIF_ISP_LSC_CTRL_ENA); 336 params->ops->lsc_matrix_config(params, arg); 337 338 for (i = 0; i < RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE / 2; i++) { 339 /* program x size tables */ 340 data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->x_size_tbl[i * 2], 341 arg->x_size_tbl[i * 2 + 1]); 342 rkisp1_write(params->rkisp1, 343 RKISP1_CIF_ISP_LSC_XSIZE_01 + i * 4, data); 344 345 /* program x grad tables */ 346 data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->x_grad_tbl[i * 2], 347 arg->x_grad_tbl[i * 2 + 1]); 348 rkisp1_write(params->rkisp1, 349 RKISP1_CIF_ISP_LSC_XGRAD_01 + i * 4, data); 350 351 /* program y size tables */ 352 data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->y_size_tbl[i * 2], 353 arg->y_size_tbl[i * 2 + 1]); 354 rkisp1_write(params->rkisp1, 355 RKISP1_CIF_ISP_LSC_YSIZE_01 + i * 4, data); 356 357 /* program y grad tables */ 358 data = RKISP1_CIF_ISP_LSC_SECT_SIZE(arg->y_grad_tbl[i * 2], 359 arg->y_grad_tbl[i * 2 + 1]); 360 rkisp1_write(params->rkisp1, 361 RKISP1_CIF_ISP_LSC_YGRAD_01 + i * 4, data); 362 } 363 364 /* restore the lsc ctrl status */ 365 if (lsc_ctrl & RKISP1_CIF_ISP_LSC_CTRL_ENA) { 366 rkisp1_param_set_bits(params, 367 RKISP1_CIF_ISP_LSC_CTRL, 368 RKISP1_CIF_ISP_LSC_CTRL_ENA); 369 } else { 370 rkisp1_param_clear_bits(params, 371 RKISP1_CIF_ISP_LSC_CTRL, 372 RKISP1_CIF_ISP_LSC_CTRL_ENA); 373 } 374 } 375 376 /* ISP Filtering function */ 377 static void rkisp1_flt_config(struct rkisp1_params *params, 378 const struct rkisp1_cif_isp_flt_config *arg) 379 { 380 u32 filt_mode; 381 382 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_THRESH_BL0, 383 arg->thresh_bl0); 384 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_THRESH_BL1, 385 arg->thresh_bl1); 386 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_THRESH_SH0, 387 arg->thresh_sh0); 388 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_THRESH_SH1, 389 arg->thresh_sh1); 390 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_FAC_BL0, 391 arg->fac_bl0); 392 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_FAC_BL1, 393 arg->fac_bl1); 394 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_FAC_MID, 395 arg->fac_mid); 396 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_FAC_SH0, 397 arg->fac_sh0); 398 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_FAC_SH1, 399 arg->fac_sh1); 400 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_LUM_WEIGHT, 401 arg->lum_weight); 402 403 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_MODE, 404 (arg->mode ? RKISP1_CIF_ISP_FLT_MODE_DNR : 0) | 405 RKISP1_CIF_ISP_FLT_CHROMA_V_MODE(arg->chr_v_mode) | 406 RKISP1_CIF_ISP_FLT_CHROMA_H_MODE(arg->chr_h_mode) | 407 RKISP1_CIF_ISP_FLT_GREEN_STAGE1(arg->grn_stage1)); 408 409 /* avoid to override the old enable value */ 410 filt_mode = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_FILT_MODE); 411 filt_mode &= RKISP1_CIF_ISP_FLT_ENA; 412 if (arg->mode) 413 filt_mode |= RKISP1_CIF_ISP_FLT_MODE_DNR; 414 filt_mode |= RKISP1_CIF_ISP_FLT_CHROMA_V_MODE(arg->chr_v_mode) | 415 RKISP1_CIF_ISP_FLT_CHROMA_H_MODE(arg->chr_h_mode) | 416 RKISP1_CIF_ISP_FLT_GREEN_STAGE1(arg->grn_stage1); 417 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_FILT_MODE, filt_mode); 418 } 419 420 /* ISP demosaic interface function */ 421 static int rkisp1_bdm_config(struct rkisp1_params *params, 422 const struct rkisp1_cif_isp_bdm_config *arg) 423 { 424 u32 bdm_th; 425 426 /* avoid to override the old enable value */ 427 bdm_th = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_DEMOSAIC); 428 bdm_th &= RKISP1_CIF_ISP_DEMOSAIC_BYPASS; 429 bdm_th |= arg->demosaic_th & ~RKISP1_CIF_ISP_DEMOSAIC_BYPASS; 430 /* set demosaic threshold */ 431 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DEMOSAIC, bdm_th); 432 return 0; 433 } 434 435 /* ISP GAMMA correction interface function */ 436 static void rkisp1_sdg_config(struct rkisp1_params *params, 437 const struct rkisp1_cif_isp_sdg_config *arg) 438 { 439 unsigned int i; 440 441 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_GAMMA_DX_LO, 442 arg->xa_pnts.gamma_dx0); 443 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_GAMMA_DX_HI, 444 arg->xa_pnts.gamma_dx1); 445 446 for (i = 0; i < RKISP1_CIF_ISP_DEGAMMA_CURVE_SIZE; i++) { 447 rkisp1_write(params->rkisp1, 448 RKISP1_CIF_ISP_GAMMA_R_Y0 + i * 4, 449 arg->curve_r.gamma_y[i]); 450 rkisp1_write(params->rkisp1, 451 RKISP1_CIF_ISP_GAMMA_G_Y0 + i * 4, 452 arg->curve_g.gamma_y[i]); 453 rkisp1_write(params->rkisp1, 454 RKISP1_CIF_ISP_GAMMA_B_Y0 + i * 4, 455 arg->curve_b.gamma_y[i]); 456 } 457 } 458 459 /* ISP GAMMA correction interface function */ 460 static void rkisp1_goc_config_v10(struct rkisp1_params *params, 461 const struct rkisp1_cif_isp_goc_config *arg) 462 { 463 unsigned int i; 464 465 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 466 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 467 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_GAMMA_OUT_MODE_V10, 468 arg->mode); 469 470 for (i = 0; i < RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V10; i++) 471 rkisp1_write(params->rkisp1, 472 RKISP1_CIF_ISP_GAMMA_OUT_Y_0_V10 + i * 4, 473 arg->gamma_y[i]); 474 } 475 476 static void rkisp1_goc_config_v12(struct rkisp1_params *params, 477 const struct rkisp1_cif_isp_goc_config *arg) 478 { 479 unsigned int i; 480 u32 value; 481 482 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 483 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 484 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_GAMMA_OUT_MODE_V12, 485 arg->mode); 486 487 for (i = 0; i < RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V12 / 2; i++) { 488 value = RKISP1_CIF_ISP_GAMMA_VALUE_V12( 489 arg->gamma_y[2 * i + 1], 490 arg->gamma_y[2 * i]); 491 rkisp1_write(params->rkisp1, 492 RKISP1_CIF_ISP_GAMMA_OUT_Y_0_V12 + i * 4, value); 493 } 494 } 495 496 /* ISP Cross Talk */ 497 static void rkisp1_ctk_config(struct rkisp1_params *params, 498 const struct rkisp1_cif_isp_ctk_config *arg) 499 { 500 unsigned int i, j, k = 0; 501 502 for (i = 0; i < 3; i++) 503 for (j = 0; j < 3; j++) 504 rkisp1_write(params->rkisp1, 505 RKISP1_CIF_ISP_CT_COEFF_0 + 4 * k++, 506 arg->coeff[i][j]); 507 for (i = 0; i < 3; i++) 508 rkisp1_write(params->rkisp1, 509 RKISP1_CIF_ISP_CT_OFFSET_R + i * 4, 510 arg->ct_offset[i]); 511 } 512 513 static void rkisp1_ctk_enable(struct rkisp1_params *params, bool en) 514 { 515 if (en) 516 return; 517 518 /* Write back the default values. */ 519 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_0, 0x80); 520 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_1, 0); 521 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_2, 0); 522 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_3, 0); 523 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_4, 0x80); 524 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_5, 0); 525 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_6, 0); 526 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_7, 0); 527 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_COEFF_8, 0x80); 528 529 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_OFFSET_R, 0); 530 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_OFFSET_G, 0); 531 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_CT_OFFSET_B, 0); 532 } 533 534 /* ISP White Balance Mode */ 535 static void rkisp1_awb_meas_config_v10(struct rkisp1_params *params, 536 const struct rkisp1_cif_isp_awb_meas_config *arg) 537 { 538 u32 reg_val = 0; 539 /* based on the mode,configure the awb module */ 540 if (arg->awb_mode == RKISP1_CIF_ISP_AWB_MODE_YCBCR) { 541 /* Reference Cb and Cr */ 542 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_REF_V10, 543 RKISP1_CIF_ISP_AWB_REF_CR_SET(arg->awb_ref_cr) | 544 arg->awb_ref_cb); 545 /* Yc Threshold */ 546 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_THRESH_V10, 547 RKISP1_CIF_ISP_AWB_MAX_Y_SET(arg->max_y) | 548 RKISP1_CIF_ISP_AWB_MIN_Y_SET(arg->min_y) | 549 RKISP1_CIF_ISP_AWB_MAX_CS_SET(arg->max_csum) | 550 arg->min_c); 551 } 552 553 reg_val = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V10); 554 if (arg->enable_ymax_cmp) 555 reg_val |= RKISP1_CIF_ISP_AWB_YMAX_CMP_EN; 556 else 557 reg_val &= ~RKISP1_CIF_ISP_AWB_YMAX_CMP_EN; 558 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V10, reg_val); 559 560 /* window offset */ 561 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_WND_V_OFFS_V10, 562 arg->awb_wnd.v_offs); 563 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_WND_H_OFFS_V10, 564 arg->awb_wnd.h_offs); 565 /* AWB window size */ 566 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_WND_V_SIZE_V10, 567 arg->awb_wnd.v_size); 568 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_WND_H_SIZE_V10, 569 arg->awb_wnd.h_size); 570 /* Number of frames */ 571 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_FRAMES_V10, 572 arg->frames); 573 } 574 575 static void rkisp1_awb_meas_config_v12(struct rkisp1_params *params, 576 const struct rkisp1_cif_isp_awb_meas_config *arg) 577 { 578 u32 reg_val = 0; 579 /* based on the mode,configure the awb module */ 580 if (arg->awb_mode == RKISP1_CIF_ISP_AWB_MODE_YCBCR) { 581 /* Reference Cb and Cr */ 582 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_REF_V12, 583 RKISP1_CIF_ISP_AWB_REF_CR_SET(arg->awb_ref_cr) | 584 arg->awb_ref_cb); 585 /* Yc Threshold */ 586 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_THRESH_V12, 587 RKISP1_CIF_ISP_AWB_MAX_Y_SET(arg->max_y) | 588 RKISP1_CIF_ISP_AWB_MIN_Y_SET(arg->min_y) | 589 RKISP1_CIF_ISP_AWB_MAX_CS_SET(arg->max_csum) | 590 arg->min_c); 591 } 592 593 reg_val = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V12); 594 if (arg->enable_ymax_cmp) 595 reg_val |= RKISP1_CIF_ISP_AWB_YMAX_CMP_EN; 596 else 597 reg_val &= ~RKISP1_CIF_ISP_AWB_YMAX_CMP_EN; 598 reg_val &= ~RKISP1_CIF_ISP_AWB_SET_FRAMES_MASK_V12; 599 reg_val |= RKISP1_CIF_ISP_AWB_SET_FRAMES_V12(arg->frames); 600 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V12, reg_val); 601 602 /* window offset */ 603 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_OFFS_V12, 604 arg->awb_wnd.v_offs << 16 | arg->awb_wnd.h_offs); 605 /* AWB window size */ 606 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_SIZE_V12, 607 arg->awb_wnd.v_size << 16 | arg->awb_wnd.h_size); 608 } 609 610 static void 611 rkisp1_awb_meas_enable_v10(struct rkisp1_params *params, 612 const struct rkisp1_cif_isp_awb_meas_config *arg, 613 bool en) 614 { 615 u32 reg_val = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V10); 616 617 /* switch off */ 618 reg_val &= RKISP1_CIF_ISP_AWB_MODE_MASK_NONE; 619 620 if (en) { 621 if (arg->awb_mode == RKISP1_CIF_ISP_AWB_MODE_RGB) 622 reg_val |= RKISP1_CIF_ISP_AWB_MODE_RGB_EN; 623 else 624 reg_val |= RKISP1_CIF_ISP_AWB_MODE_YCBCR_EN; 625 626 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V10, 627 reg_val); 628 629 /* Measurements require AWB block be active. */ 630 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL, 631 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 632 } else { 633 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V10, 634 reg_val); 635 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 636 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 637 } 638 } 639 640 static void 641 rkisp1_awb_meas_enable_v12(struct rkisp1_params *params, 642 const struct rkisp1_cif_isp_awb_meas_config *arg, 643 bool en) 644 { 645 u32 reg_val = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V12); 646 647 /* switch off */ 648 reg_val &= RKISP1_CIF_ISP_AWB_MODE_MASK_NONE; 649 650 if (en) { 651 if (arg->awb_mode == RKISP1_CIF_ISP_AWB_MODE_RGB) 652 reg_val |= RKISP1_CIF_ISP_AWB_MODE_RGB_EN; 653 else 654 reg_val |= RKISP1_CIF_ISP_AWB_MODE_YCBCR_EN; 655 656 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V12, 657 reg_val); 658 659 /* Measurements require AWB block be active. */ 660 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL, 661 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 662 } else { 663 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_PROP_V12, 664 reg_val); 665 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 666 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 667 } 668 } 669 670 static void 671 rkisp1_awb_gain_config_v10(struct rkisp1_params *params, 672 const struct rkisp1_cif_isp_awb_gain_config *arg) 673 { 674 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_GAIN_G_V10, 675 RKISP1_CIF_ISP_AWB_GAIN_R_SET(arg->gain_green_r) | 676 arg->gain_green_b); 677 678 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_GAIN_RB_V10, 679 RKISP1_CIF_ISP_AWB_GAIN_R_SET(arg->gain_red) | 680 arg->gain_blue); 681 } 682 683 static void 684 rkisp1_awb_gain_config_v12(struct rkisp1_params *params, 685 const struct rkisp1_cif_isp_awb_gain_config *arg) 686 { 687 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_GAIN_G_V12, 688 RKISP1_CIF_ISP_AWB_GAIN_R_SET(arg->gain_green_r) | 689 arg->gain_green_b); 690 691 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AWB_GAIN_RB_V12, 692 RKISP1_CIF_ISP_AWB_GAIN_R_SET(arg->gain_red) | 693 arg->gain_blue); 694 } 695 696 static void rkisp1_aec_config_v10(struct rkisp1_params *params, 697 const struct rkisp1_cif_isp_aec_config *arg) 698 { 699 unsigned int block_hsize, block_vsize; 700 u32 exp_ctrl; 701 702 /* avoid to override the old enable value */ 703 exp_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_EXP_CTRL); 704 exp_ctrl &= RKISP1_CIF_ISP_EXP_ENA; 705 if (arg->autostop) 706 exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP; 707 if (arg->mode == RKISP1_CIF_ISP_EXP_MEASURING_MODE_1) 708 exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_MEASMODE_1; 709 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_CTRL, exp_ctrl); 710 711 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_H_OFFSET_V10, 712 arg->meas_window.h_offs); 713 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_V_OFFSET_V10, 714 arg->meas_window.v_offs); 715 716 block_hsize = arg->meas_window.h_size / 717 RKISP1_CIF_ISP_EXP_COLUMN_NUM_V10 - 1; 718 block_vsize = arg->meas_window.v_size / 719 RKISP1_CIF_ISP_EXP_ROW_NUM_V10 - 1; 720 721 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_H_SIZE_V10, 722 RKISP1_CIF_ISP_EXP_H_SIZE_SET_V10(block_hsize)); 723 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_V_SIZE_V10, 724 RKISP1_CIF_ISP_EXP_V_SIZE_SET_V10(block_vsize)); 725 } 726 727 static void rkisp1_aec_config_v12(struct rkisp1_params *params, 728 const struct rkisp1_cif_isp_aec_config *arg) 729 { 730 u32 exp_ctrl; 731 u32 block_hsize, block_vsize; 732 u32 wnd_num_idx = 1; 733 const u32 ae_wnd_num[] = { 5, 9, 15, 15 }; 734 735 /* avoid to override the old enable value */ 736 exp_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_EXP_CTRL); 737 exp_ctrl &= RKISP1_CIF_ISP_EXP_ENA; 738 if (arg->autostop) 739 exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP; 740 if (arg->mode == RKISP1_CIF_ISP_EXP_MEASURING_MODE_1) 741 exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_MEASMODE_1; 742 exp_ctrl |= RKISP1_CIF_ISP_EXP_CTRL_WNDNUM_SET_V12(wnd_num_idx); 743 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_CTRL, exp_ctrl); 744 745 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_OFFS_V12, 746 RKISP1_CIF_ISP_EXP_V_OFFSET_SET_V12(arg->meas_window.v_offs) | 747 RKISP1_CIF_ISP_EXP_H_OFFSET_SET_V12(arg->meas_window.h_offs)); 748 749 block_hsize = arg->meas_window.h_size / ae_wnd_num[wnd_num_idx] - 1; 750 block_vsize = arg->meas_window.v_size / ae_wnd_num[wnd_num_idx] - 1; 751 752 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_EXP_SIZE_V12, 753 RKISP1_CIF_ISP_EXP_V_SIZE_SET_V12(block_vsize) | 754 RKISP1_CIF_ISP_EXP_H_SIZE_SET_V12(block_hsize)); 755 } 756 757 static void rkisp1_cproc_config(struct rkisp1_params *params, 758 const struct rkisp1_cif_isp_cproc_config *arg) 759 { 760 struct rkisp1_cif_isp_isp_other_cfg *cur_other_cfg = 761 container_of(arg, struct rkisp1_cif_isp_isp_other_cfg, cproc_config); 762 struct rkisp1_cif_isp_ie_config *cur_ie_config = 763 &cur_other_cfg->ie_config; 764 u32 effect = cur_ie_config->effect; 765 u32 quantization = params->quantization; 766 767 rkisp1_write(params->rkisp1, RKISP1_CIF_C_PROC_CONTRAST, 768 arg->contrast); 769 rkisp1_write(params->rkisp1, RKISP1_CIF_C_PROC_HUE, arg->hue); 770 rkisp1_write(params->rkisp1, RKISP1_CIF_C_PROC_SATURATION, arg->sat); 771 rkisp1_write(params->rkisp1, RKISP1_CIF_C_PROC_BRIGHTNESS, 772 arg->brightness); 773 774 if (quantization != V4L2_QUANTIZATION_FULL_RANGE || 775 effect != V4L2_COLORFX_NONE) { 776 rkisp1_param_clear_bits(params, RKISP1_CIF_C_PROC_CTRL, 777 RKISP1_CIF_C_PROC_YOUT_FULL | 778 RKISP1_CIF_C_PROC_YIN_FULL | 779 RKISP1_CIF_C_PROC_COUT_FULL); 780 } else { 781 rkisp1_param_set_bits(params, RKISP1_CIF_C_PROC_CTRL, 782 RKISP1_CIF_C_PROC_YOUT_FULL | 783 RKISP1_CIF_C_PROC_YIN_FULL | 784 RKISP1_CIF_C_PROC_COUT_FULL); 785 } 786 } 787 788 static void rkisp1_hst_config_v10(struct rkisp1_params *params, 789 const struct rkisp1_cif_isp_hst_config *arg) 790 { 791 unsigned int block_hsize, block_vsize; 792 static const u32 hist_weight_regs[] = { 793 RKISP1_CIF_ISP_HIST_WEIGHT_00TO30_V10, 794 RKISP1_CIF_ISP_HIST_WEIGHT_40TO21_V10, 795 RKISP1_CIF_ISP_HIST_WEIGHT_31TO12_V10, 796 RKISP1_CIF_ISP_HIST_WEIGHT_22TO03_V10, 797 RKISP1_CIF_ISP_HIST_WEIGHT_13TO43_V10, 798 RKISP1_CIF_ISP_HIST_WEIGHT_04TO34_V10, 799 }; 800 const u8 *weight; 801 unsigned int i; 802 u32 hist_prop; 803 804 /* avoid to override the old enable value */ 805 hist_prop = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_HIST_PROP_V10); 806 hist_prop &= RKISP1_CIF_ISP_HIST_PROP_MODE_MASK_V10; 807 hist_prop |= RKISP1_CIF_ISP_HIST_PREDIV_SET_V10(arg->histogram_predivider); 808 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_PROP_V10, hist_prop); 809 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_H_OFFS_V10, 810 arg->meas_window.h_offs); 811 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_V_OFFS_V10, 812 arg->meas_window.v_offs); 813 814 block_hsize = arg->meas_window.h_size / 815 RKISP1_CIF_ISP_HIST_COLUMN_NUM_V10 - 1; 816 block_vsize = arg->meas_window.v_size / RKISP1_CIF_ISP_HIST_ROW_NUM_V10 - 1; 817 818 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_H_SIZE_V10, 819 block_hsize); 820 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_V_SIZE_V10, 821 block_vsize); 822 823 weight = arg->hist_weight; 824 for (i = 0; i < ARRAY_SIZE(hist_weight_regs); ++i, weight += 4) 825 rkisp1_write(params->rkisp1, hist_weight_regs[i], 826 RKISP1_CIF_ISP_HIST_WEIGHT_SET_V10(weight[0], weight[1], 827 weight[2], weight[3])); 828 829 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_WEIGHT_44_V10, 830 weight[0] & 0x1F); 831 } 832 833 static void rkisp1_hst_config_v12(struct rkisp1_params *params, 834 const struct rkisp1_cif_isp_hst_config *arg) 835 { 836 unsigned int i, j; 837 u32 block_hsize, block_vsize; 838 u32 wnd_num_idx, hist_weight_num, hist_ctrl, value; 839 u8 weight15x15[RKISP1_CIF_ISP_HIST_WEIGHT_REG_SIZE_V12]; 840 const u32 hist_wnd_num[] = { 5, 9, 15, 15 }; 841 842 /* now we just support 9x9 window */ 843 wnd_num_idx = 1; 844 memset(weight15x15, 0x00, sizeof(weight15x15)); 845 /* avoid to override the old enable value */ 846 hist_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_HIST_CTRL_V12); 847 hist_ctrl &= RKISP1_CIF_ISP_HIST_CTRL_MODE_MASK_V12 | 848 RKISP1_CIF_ISP_HIST_CTRL_EN_MASK_V12; 849 hist_ctrl = hist_ctrl | 850 RKISP1_CIF_ISP_HIST_CTRL_INTRSEL_SET_V12(1) | 851 RKISP1_CIF_ISP_HIST_CTRL_DATASEL_SET_V12(0) | 852 RKISP1_CIF_ISP_HIST_CTRL_WATERLINE_SET_V12(0) | 853 RKISP1_CIF_ISP_HIST_CTRL_AUTOSTOP_SET_V12(0) | 854 RKISP1_CIF_ISP_HIST_CTRL_WNDNUM_SET_V12(1) | 855 RKISP1_CIF_ISP_HIST_CTRL_STEPSIZE_SET_V12(arg->histogram_predivider); 856 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_CTRL_V12, hist_ctrl); 857 858 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_OFFS_V12, 859 RKISP1_CIF_ISP_HIST_OFFS_SET_V12(arg->meas_window.h_offs, 860 arg->meas_window.v_offs)); 861 862 block_hsize = arg->meas_window.h_size / hist_wnd_num[wnd_num_idx] - 1; 863 block_vsize = arg->meas_window.v_size / hist_wnd_num[wnd_num_idx] - 1; 864 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_SIZE_V12, 865 RKISP1_CIF_ISP_HIST_SIZE_SET_V12(block_hsize, block_vsize)); 866 867 for (i = 0; i < hist_wnd_num[wnd_num_idx]; i++) { 868 for (j = 0; j < hist_wnd_num[wnd_num_idx]; j++) { 869 weight15x15[i * RKISP1_CIF_ISP_HIST_ROW_NUM_V12 + j] = 870 arg->hist_weight[i * hist_wnd_num[wnd_num_idx] + j]; 871 } 872 } 873 874 hist_weight_num = RKISP1_CIF_ISP_HIST_WEIGHT_REG_SIZE_V12; 875 for (i = 0; i < (hist_weight_num / 4); i++) { 876 value = RKISP1_CIF_ISP_HIST_WEIGHT_SET_V12( 877 weight15x15[4 * i + 0], 878 weight15x15[4 * i + 1], 879 weight15x15[4 * i + 2], 880 weight15x15[4 * i + 3]); 881 rkisp1_write(params->rkisp1, 882 RKISP1_CIF_ISP_HIST_WEIGHT_V12 + 4 * i, value); 883 } 884 value = RKISP1_CIF_ISP_HIST_WEIGHT_SET_V12(weight15x15[4 * i + 0], 0, 0, 0); 885 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_HIST_WEIGHT_V12 + 4 * i, 886 value); 887 } 888 889 static void 890 rkisp1_hst_enable_v10(struct rkisp1_params *params, 891 const struct rkisp1_cif_isp_hst_config *arg, bool en) 892 { 893 if (en) { 894 u32 hist_prop = rkisp1_read(params->rkisp1, 895 RKISP1_CIF_ISP_HIST_PROP_V10); 896 897 hist_prop &= ~RKISP1_CIF_ISP_HIST_PROP_MODE_MASK_V10; 898 hist_prop |= arg->mode; 899 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_HIST_PROP_V10, 900 hist_prop); 901 } else { 902 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_HIST_PROP_V10, 903 RKISP1_CIF_ISP_HIST_PROP_MODE_MASK_V10); 904 } 905 } 906 907 static void 908 rkisp1_hst_enable_v12(struct rkisp1_params *params, 909 const struct rkisp1_cif_isp_hst_config *arg, bool en) 910 { 911 if (en) { 912 u32 hist_ctrl = rkisp1_read(params->rkisp1, 913 RKISP1_CIF_ISP_HIST_CTRL_V12); 914 915 hist_ctrl &= ~RKISP1_CIF_ISP_HIST_CTRL_MODE_MASK_V12; 916 hist_ctrl |= RKISP1_CIF_ISP_HIST_CTRL_MODE_SET_V12(arg->mode); 917 hist_ctrl |= RKISP1_CIF_ISP_HIST_CTRL_EN_SET_V12(1); 918 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_HIST_CTRL_V12, 919 hist_ctrl); 920 } else { 921 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_HIST_CTRL_V12, 922 RKISP1_CIF_ISP_HIST_CTRL_MODE_MASK_V12 | 923 RKISP1_CIF_ISP_HIST_CTRL_EN_MASK_V12); 924 } 925 } 926 927 static void rkisp1_afm_config_v10(struct rkisp1_params *params, 928 const struct rkisp1_cif_isp_afc_config *arg) 929 { 930 size_t num_of_win = min_t(size_t, ARRAY_SIZE(arg->afm_win), 931 arg->num_afm_win); 932 u32 afm_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AFM_CTRL); 933 unsigned int i; 934 935 /* Switch off to configure. */ 936 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_AFM_CTRL, 937 RKISP1_CIF_ISP_AFM_ENA); 938 939 for (i = 0; i < num_of_win; i++) { 940 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_LT_A + i * 8, 941 RKISP1_CIF_ISP_AFM_WINDOW_X(arg->afm_win[i].h_offs) | 942 RKISP1_CIF_ISP_AFM_WINDOW_Y(arg->afm_win[i].v_offs)); 943 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_RB_A + i * 8, 944 RKISP1_CIF_ISP_AFM_WINDOW_X(arg->afm_win[i].h_size + 945 arg->afm_win[i].h_offs) | 946 RKISP1_CIF_ISP_AFM_WINDOW_Y(arg->afm_win[i].v_size + 947 arg->afm_win[i].v_offs)); 948 } 949 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_THRES, arg->thres); 950 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_VAR_SHIFT, 951 arg->var_shift); 952 /* restore afm status */ 953 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_CTRL, afm_ctrl); 954 } 955 956 static void rkisp1_afm_config_v12(struct rkisp1_params *params, 957 const struct rkisp1_cif_isp_afc_config *arg) 958 { 959 size_t num_of_win = min_t(size_t, ARRAY_SIZE(arg->afm_win), 960 arg->num_afm_win); 961 u32 afm_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_ISP_AFM_CTRL); 962 u32 lum_var_shift, afm_var_shift; 963 unsigned int i; 964 965 /* Switch off to configure. */ 966 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_AFM_CTRL, 967 RKISP1_CIF_ISP_AFM_ENA); 968 969 for (i = 0; i < num_of_win; i++) { 970 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_LT_A + i * 8, 971 RKISP1_CIF_ISP_AFM_WINDOW_X(arg->afm_win[i].h_offs) | 972 RKISP1_CIF_ISP_AFM_WINDOW_Y(arg->afm_win[i].v_offs)); 973 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_RB_A + i * 8, 974 RKISP1_CIF_ISP_AFM_WINDOW_X(arg->afm_win[i].h_size + 975 arg->afm_win[i].h_offs) | 976 RKISP1_CIF_ISP_AFM_WINDOW_Y(arg->afm_win[i].v_size + 977 arg->afm_win[i].v_offs)); 978 } 979 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_THRES, arg->thres); 980 981 lum_var_shift = RKISP1_CIF_ISP_AFM_GET_LUM_SHIFT_a_V12(arg->var_shift); 982 afm_var_shift = RKISP1_CIF_ISP_AFM_GET_AFM_SHIFT_a_V12(arg->var_shift); 983 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_VAR_SHIFT, 984 RKISP1_CIF_ISP_AFM_SET_SHIFT_a_V12(lum_var_shift, afm_var_shift) | 985 RKISP1_CIF_ISP_AFM_SET_SHIFT_b_V12(lum_var_shift, afm_var_shift) | 986 RKISP1_CIF_ISP_AFM_SET_SHIFT_c_V12(lum_var_shift, afm_var_shift)); 987 988 /* restore afm status */ 989 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_AFM_CTRL, afm_ctrl); 990 } 991 992 static void rkisp1_ie_config(struct rkisp1_params *params, 993 const struct rkisp1_cif_isp_ie_config *arg) 994 { 995 u32 eff_ctrl; 996 997 eff_ctrl = rkisp1_read(params->rkisp1, RKISP1_CIF_IMG_EFF_CTRL); 998 eff_ctrl &= ~RKISP1_CIF_IMG_EFF_CTRL_MODE_MASK; 999 1000 if (params->quantization == V4L2_QUANTIZATION_FULL_RANGE) 1001 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_YCBCR_FULL; 1002 1003 switch (arg->effect) { 1004 case V4L2_COLORFX_SEPIA: 1005 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_SEPIA; 1006 break; 1007 case V4L2_COLORFX_SET_CBCR: 1008 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_TINT, 1009 arg->eff_tint); 1010 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_SEPIA; 1011 break; 1012 /* 1013 * Color selection is similar to water color(AQUA): 1014 * grayscale + selected color w threshold 1015 */ 1016 case V4L2_COLORFX_AQUA: 1017 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_COLOR_SEL; 1018 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_COLOR_SEL, 1019 arg->color_sel); 1020 break; 1021 case V4L2_COLORFX_EMBOSS: 1022 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_EMBOSS; 1023 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_MAT_1, 1024 arg->eff_mat_1); 1025 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_MAT_2, 1026 arg->eff_mat_2); 1027 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_MAT_3, 1028 arg->eff_mat_3); 1029 break; 1030 case V4L2_COLORFX_SKETCH: 1031 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_SKETCH; 1032 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_MAT_3, 1033 arg->eff_mat_3); 1034 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_MAT_4, 1035 arg->eff_mat_4); 1036 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_MAT_5, 1037 arg->eff_mat_5); 1038 break; 1039 case V4L2_COLORFX_BW: 1040 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_BLACKWHITE; 1041 break; 1042 case V4L2_COLORFX_NEGATIVE: 1043 eff_ctrl |= RKISP1_CIF_IMG_EFF_CTRL_MODE_NEGATIVE; 1044 break; 1045 default: 1046 break; 1047 } 1048 1049 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_CTRL, eff_ctrl); 1050 } 1051 1052 static void rkisp1_ie_enable(struct rkisp1_params *params, bool en) 1053 { 1054 if (en) { 1055 rkisp1_param_set_bits(params, RKISP1_CIF_VI_ICCL, 1056 RKISP1_CIF_VI_ICCL_IE_CLK); 1057 rkisp1_write(params->rkisp1, RKISP1_CIF_IMG_EFF_CTRL, 1058 RKISP1_CIF_IMG_EFF_CTRL_ENABLE); 1059 rkisp1_param_set_bits(params, RKISP1_CIF_IMG_EFF_CTRL, 1060 RKISP1_CIF_IMG_EFF_CTRL_CFG_UPD); 1061 } else { 1062 rkisp1_param_clear_bits(params, RKISP1_CIF_IMG_EFF_CTRL, 1063 RKISP1_CIF_IMG_EFF_CTRL_ENABLE); 1064 rkisp1_param_clear_bits(params, RKISP1_CIF_VI_ICCL, 1065 RKISP1_CIF_VI_ICCL_IE_CLK); 1066 } 1067 } 1068 1069 static void rkisp1_csm_config(struct rkisp1_params *params, bool full_range) 1070 { 1071 static const u16 full_range_coeff[] = { 1072 0x0026, 0x004b, 0x000f, 1073 0x01ea, 0x01d6, 0x0040, 1074 0x0040, 0x01ca, 0x01f6 1075 }; 1076 static const u16 limited_range_coeff[] = { 1077 0x0021, 0x0040, 0x000d, 1078 0x01ed, 0x01db, 0x0038, 1079 0x0038, 0x01d1, 0x01f7, 1080 }; 1081 unsigned int i; 1082 1083 if (full_range) { 1084 for (i = 0; i < ARRAY_SIZE(full_range_coeff); i++) 1085 rkisp1_write(params->rkisp1, 1086 RKISP1_CIF_ISP_CC_COEFF_0 + i * 4, 1087 full_range_coeff[i]); 1088 1089 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL, 1090 RKISP1_CIF_ISP_CTRL_ISP_CSM_Y_FULL_ENA | 1091 RKISP1_CIF_ISP_CTRL_ISP_CSM_C_FULL_ENA); 1092 } else { 1093 for (i = 0; i < ARRAY_SIZE(limited_range_coeff); i++) 1094 rkisp1_write(params->rkisp1, 1095 RKISP1_CIF_ISP_CC_COEFF_0 + i * 4, 1096 limited_range_coeff[i]); 1097 1098 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1099 RKISP1_CIF_ISP_CTRL_ISP_CSM_Y_FULL_ENA | 1100 RKISP1_CIF_ISP_CTRL_ISP_CSM_C_FULL_ENA); 1101 } 1102 } 1103 1104 /* ISP De-noise Pre-Filter(DPF) function */ 1105 static void rkisp1_dpf_config(struct rkisp1_params *params, 1106 const struct rkisp1_cif_isp_dpf_config *arg) 1107 { 1108 unsigned int isp_dpf_mode, spatial_coeff, i; 1109 1110 switch (arg->gain.mode) { 1111 case RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_GAINS: 1112 isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_USE_NF_GAIN | 1113 RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP; 1114 break; 1115 case RKISP1_CIF_ISP_DPF_GAIN_USAGE_LSC_GAINS: 1116 isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_LSC_GAIN_COMP; 1117 break; 1118 case RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_LSC_GAINS: 1119 isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_USE_NF_GAIN | 1120 RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP | 1121 RKISP1_CIF_ISP_DPF_MODE_LSC_GAIN_COMP; 1122 break; 1123 case RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_GAINS: 1124 isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP; 1125 break; 1126 case RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_LSC_GAINS: 1127 isp_dpf_mode = RKISP1_CIF_ISP_DPF_MODE_LSC_GAIN_COMP | 1128 RKISP1_CIF_ISP_DPF_MODE_AWB_GAIN_COMP; 1129 break; 1130 case RKISP1_CIF_ISP_DPF_GAIN_USAGE_DISABLED: 1131 default: 1132 isp_dpf_mode = 0; 1133 break; 1134 } 1135 1136 if (arg->nll.scale_mode == RKISP1_CIF_ISP_NLL_SCALE_LOGARITHMIC) 1137 isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_NLL_SEGMENTATION; 1138 if (arg->rb_flt.fltsize == RKISP1_CIF_ISP_DPF_RB_FILTERSIZE_9x9) 1139 isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_RB_FLTSIZE_9x9; 1140 if (!arg->rb_flt.r_enable) 1141 isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_R_FLT_DIS; 1142 if (!arg->rb_flt.b_enable) 1143 isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_B_FLT_DIS; 1144 if (!arg->g_flt.gb_enable) 1145 isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_GB_FLT_DIS; 1146 if (!arg->g_flt.gr_enable) 1147 isp_dpf_mode |= RKISP1_CIF_ISP_DPF_MODE_GR_FLT_DIS; 1148 1149 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_DPF_MODE, 1150 isp_dpf_mode); 1151 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_NF_GAIN_B, 1152 arg->gain.nf_b_gain); 1153 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_NF_GAIN_R, 1154 arg->gain.nf_r_gain); 1155 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_NF_GAIN_GB, 1156 arg->gain.nf_gb_gain); 1157 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_NF_GAIN_GR, 1158 arg->gain.nf_gr_gain); 1159 1160 for (i = 0; i < RKISP1_CIF_ISP_DPF_MAX_NLF_COEFFS; i++) { 1161 rkisp1_write(params->rkisp1, 1162 RKISP1_CIF_ISP_DPF_NULL_COEFF_0 + i * 4, 1163 arg->nll.coeff[i]); 1164 } 1165 1166 spatial_coeff = arg->g_flt.spatial_coeff[0] | 1167 (arg->g_flt.spatial_coeff[1] << 8) | 1168 (arg->g_flt.spatial_coeff[2] << 16) | 1169 (arg->g_flt.spatial_coeff[3] << 24); 1170 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_S_WEIGHT_G_1_4, 1171 spatial_coeff); 1172 1173 spatial_coeff = arg->g_flt.spatial_coeff[4] | 1174 (arg->g_flt.spatial_coeff[5] << 8); 1175 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_S_WEIGHT_G_5_6, 1176 spatial_coeff); 1177 1178 spatial_coeff = arg->rb_flt.spatial_coeff[0] | 1179 (arg->rb_flt.spatial_coeff[1] << 8) | 1180 (arg->rb_flt.spatial_coeff[2] << 16) | 1181 (arg->rb_flt.spatial_coeff[3] << 24); 1182 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_S_WEIGHT_RB_1_4, 1183 spatial_coeff); 1184 1185 spatial_coeff = arg->rb_flt.spatial_coeff[4] | 1186 (arg->rb_flt.spatial_coeff[5] << 8); 1187 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_S_WEIGHT_RB_5_6, 1188 spatial_coeff); 1189 } 1190 1191 static void 1192 rkisp1_dpf_strength_config(struct rkisp1_params *params, 1193 const struct rkisp1_cif_isp_dpf_strength_config *arg) 1194 { 1195 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_STRENGTH_B, arg->b); 1196 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_STRENGTH_G, arg->g); 1197 rkisp1_write(params->rkisp1, RKISP1_CIF_ISP_DPF_STRENGTH_R, arg->r); 1198 } 1199 1200 static void 1201 rkisp1_isp_isr_other_config(struct rkisp1_params *params, 1202 const struct rkisp1_params_cfg *new_params) 1203 { 1204 unsigned int module_en_update, module_cfg_update, module_ens; 1205 1206 module_en_update = new_params->module_en_update; 1207 module_cfg_update = new_params->module_cfg_update; 1208 module_ens = new_params->module_ens; 1209 1210 /* update dpc config */ 1211 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPCC) 1212 rkisp1_dpcc_config(params, 1213 &new_params->others.dpcc_config); 1214 1215 if (module_en_update & RKISP1_CIF_ISP_MODULE_DPCC) { 1216 if (module_ens & RKISP1_CIF_ISP_MODULE_DPCC) 1217 rkisp1_param_set_bits(params, 1218 RKISP1_CIF_ISP_DPCC_MODE, 1219 RKISP1_CIF_ISP_DPCC_ENA); 1220 else 1221 rkisp1_param_clear_bits(params, 1222 RKISP1_CIF_ISP_DPCC_MODE, 1223 RKISP1_CIF_ISP_DPCC_ENA); 1224 } 1225 1226 /* update bls config */ 1227 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_BLS) 1228 rkisp1_bls_config(params, 1229 &new_params->others.bls_config); 1230 1231 if (module_en_update & RKISP1_CIF_ISP_MODULE_BLS) { 1232 if (module_ens & RKISP1_CIF_ISP_MODULE_BLS) 1233 rkisp1_param_set_bits(params, 1234 RKISP1_CIF_ISP_BLS_CTRL, 1235 RKISP1_CIF_ISP_BLS_ENA); 1236 else 1237 rkisp1_param_clear_bits(params, 1238 RKISP1_CIF_ISP_BLS_CTRL, 1239 RKISP1_CIF_ISP_BLS_ENA); 1240 } 1241 1242 /* update sdg config */ 1243 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_SDG) 1244 rkisp1_sdg_config(params, 1245 &new_params->others.sdg_config); 1246 1247 if (module_en_update & RKISP1_CIF_ISP_MODULE_SDG) { 1248 if (module_ens & RKISP1_CIF_ISP_MODULE_SDG) 1249 rkisp1_param_set_bits(params, 1250 RKISP1_CIF_ISP_CTRL, 1251 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA); 1252 else 1253 rkisp1_param_clear_bits(params, 1254 RKISP1_CIF_ISP_CTRL, 1255 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA); 1256 } 1257 1258 /* update lsc config */ 1259 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_LSC) 1260 rkisp1_lsc_config(params, 1261 &new_params->others.lsc_config); 1262 1263 if (module_en_update & RKISP1_CIF_ISP_MODULE_LSC) { 1264 if (module_ens & RKISP1_CIF_ISP_MODULE_LSC) 1265 rkisp1_param_set_bits(params, 1266 RKISP1_CIF_ISP_LSC_CTRL, 1267 RKISP1_CIF_ISP_LSC_CTRL_ENA); 1268 else 1269 rkisp1_param_clear_bits(params, 1270 RKISP1_CIF_ISP_LSC_CTRL, 1271 RKISP1_CIF_ISP_LSC_CTRL_ENA); 1272 } 1273 1274 /* update awb gains */ 1275 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN) 1276 params->ops->awb_gain_config(params, &new_params->others.awb_gain_config); 1277 1278 if (module_en_update & RKISP1_CIF_ISP_MODULE_AWB_GAIN) { 1279 if (module_ens & RKISP1_CIF_ISP_MODULE_AWB_GAIN) 1280 rkisp1_param_set_bits(params, 1281 RKISP1_CIF_ISP_CTRL, 1282 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 1283 else 1284 rkisp1_param_clear_bits(params, 1285 RKISP1_CIF_ISP_CTRL, 1286 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 1287 } 1288 1289 /* update bdm config */ 1290 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_BDM) 1291 rkisp1_bdm_config(params, 1292 &new_params->others.bdm_config); 1293 1294 if (module_en_update & RKISP1_CIF_ISP_MODULE_BDM) { 1295 if (module_ens & RKISP1_CIF_ISP_MODULE_BDM) 1296 rkisp1_param_set_bits(params, 1297 RKISP1_CIF_ISP_DEMOSAIC, 1298 RKISP1_CIF_ISP_DEMOSAIC_BYPASS); 1299 else 1300 rkisp1_param_clear_bits(params, 1301 RKISP1_CIF_ISP_DEMOSAIC, 1302 RKISP1_CIF_ISP_DEMOSAIC_BYPASS); 1303 } 1304 1305 /* update filter config */ 1306 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_FLT) 1307 rkisp1_flt_config(params, 1308 &new_params->others.flt_config); 1309 1310 if (module_en_update & RKISP1_CIF_ISP_MODULE_FLT) { 1311 if (module_ens & RKISP1_CIF_ISP_MODULE_FLT) 1312 rkisp1_param_set_bits(params, 1313 RKISP1_CIF_ISP_FILT_MODE, 1314 RKISP1_CIF_ISP_FLT_ENA); 1315 else 1316 rkisp1_param_clear_bits(params, 1317 RKISP1_CIF_ISP_FILT_MODE, 1318 RKISP1_CIF_ISP_FLT_ENA); 1319 } 1320 1321 /* update ctk config */ 1322 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_CTK) 1323 rkisp1_ctk_config(params, 1324 &new_params->others.ctk_config); 1325 1326 if (module_en_update & RKISP1_CIF_ISP_MODULE_CTK) 1327 rkisp1_ctk_enable(params, !!(module_ens & RKISP1_CIF_ISP_MODULE_CTK)); 1328 1329 /* update goc config */ 1330 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_GOC) 1331 params->ops->goc_config(params, &new_params->others.goc_config); 1332 1333 if (module_en_update & RKISP1_CIF_ISP_MODULE_GOC) { 1334 if (module_ens & RKISP1_CIF_ISP_MODULE_GOC) 1335 rkisp1_param_set_bits(params, 1336 RKISP1_CIF_ISP_CTRL, 1337 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 1338 else 1339 rkisp1_param_clear_bits(params, 1340 RKISP1_CIF_ISP_CTRL, 1341 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 1342 } 1343 1344 /* update cproc config */ 1345 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_CPROC) 1346 rkisp1_cproc_config(params, 1347 &new_params->others.cproc_config); 1348 1349 if (module_en_update & RKISP1_CIF_ISP_MODULE_CPROC) { 1350 if (module_ens & RKISP1_CIF_ISP_MODULE_CPROC) 1351 rkisp1_param_set_bits(params, 1352 RKISP1_CIF_C_PROC_CTRL, 1353 RKISP1_CIF_C_PROC_CTR_ENABLE); 1354 else 1355 rkisp1_param_clear_bits(params, 1356 RKISP1_CIF_C_PROC_CTRL, 1357 RKISP1_CIF_C_PROC_CTR_ENABLE); 1358 } 1359 1360 /* update ie config */ 1361 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_IE) 1362 rkisp1_ie_config(params, &new_params->others.ie_config); 1363 1364 if (module_en_update & RKISP1_CIF_ISP_MODULE_IE) 1365 rkisp1_ie_enable(params, !!(module_ens & RKISP1_CIF_ISP_MODULE_IE)); 1366 1367 /* update dpf config */ 1368 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF) 1369 rkisp1_dpf_config(params, &new_params->others.dpf_config); 1370 1371 if (module_en_update & RKISP1_CIF_ISP_MODULE_DPF) { 1372 if (module_ens & RKISP1_CIF_ISP_MODULE_DPF) 1373 rkisp1_param_set_bits(params, 1374 RKISP1_CIF_ISP_DPF_MODE, 1375 RKISP1_CIF_ISP_DPF_MODE_EN); 1376 else 1377 rkisp1_param_clear_bits(params, 1378 RKISP1_CIF_ISP_DPF_MODE, 1379 RKISP1_CIF_ISP_DPF_MODE_EN); 1380 } 1381 1382 if ((module_en_update & RKISP1_CIF_ISP_MODULE_DPF_STRENGTH) || 1383 (module_cfg_update & RKISP1_CIF_ISP_MODULE_DPF_STRENGTH)) { 1384 /* update dpf strength config */ 1385 rkisp1_dpf_strength_config(params, 1386 &new_params->others.dpf_strength_config); 1387 } 1388 } 1389 1390 static void rkisp1_isp_isr_meas_config(struct rkisp1_params *params, 1391 struct rkisp1_params_cfg *new_params) 1392 { 1393 unsigned int module_en_update, module_cfg_update, module_ens; 1394 1395 module_en_update = new_params->module_en_update; 1396 module_cfg_update = new_params->module_cfg_update; 1397 module_ens = new_params->module_ens; 1398 1399 /* update awb config */ 1400 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AWB) 1401 params->ops->awb_meas_config(params, &new_params->meas.awb_meas_config); 1402 1403 if (module_en_update & RKISP1_CIF_ISP_MODULE_AWB) 1404 params->ops->awb_meas_enable(params, 1405 &new_params->meas.awb_meas_config, 1406 !!(module_ens & RKISP1_CIF_ISP_MODULE_AWB)); 1407 1408 /* update afc config */ 1409 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AFC) 1410 params->ops->afm_config(params, 1411 &new_params->meas.afc_config); 1412 1413 if (module_en_update & RKISP1_CIF_ISP_MODULE_AFC) { 1414 if (module_ens & RKISP1_CIF_ISP_MODULE_AFC) 1415 rkisp1_param_set_bits(params, 1416 RKISP1_CIF_ISP_AFM_CTRL, 1417 RKISP1_CIF_ISP_AFM_ENA); 1418 else 1419 rkisp1_param_clear_bits(params, 1420 RKISP1_CIF_ISP_AFM_CTRL, 1421 RKISP1_CIF_ISP_AFM_ENA); 1422 } 1423 1424 /* update hst config */ 1425 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_HST) 1426 params->ops->hst_config(params, 1427 &new_params->meas.hst_config); 1428 1429 if (module_en_update & RKISP1_CIF_ISP_MODULE_HST) 1430 params->ops->hst_enable(params, 1431 &new_params->meas.hst_config, 1432 !!(module_ens & RKISP1_CIF_ISP_MODULE_HST)); 1433 1434 /* update aec config */ 1435 if (module_cfg_update & RKISP1_CIF_ISP_MODULE_AEC) 1436 params->ops->aec_config(params, 1437 &new_params->meas.aec_config); 1438 1439 if (module_en_update & RKISP1_CIF_ISP_MODULE_AEC) { 1440 if (module_ens & RKISP1_CIF_ISP_MODULE_AEC) 1441 rkisp1_param_set_bits(params, 1442 RKISP1_CIF_ISP_EXP_CTRL, 1443 RKISP1_CIF_ISP_EXP_ENA); 1444 else 1445 rkisp1_param_clear_bits(params, 1446 RKISP1_CIF_ISP_EXP_CTRL, 1447 RKISP1_CIF_ISP_EXP_ENA); 1448 } 1449 } 1450 1451 static void rkisp1_params_apply_params_cfg(struct rkisp1_params *params, 1452 unsigned int frame_sequence) 1453 { 1454 struct rkisp1_params_cfg *new_params; 1455 struct rkisp1_buffer *cur_buf = NULL; 1456 1457 if (list_empty(¶ms->params)) 1458 return; 1459 1460 cur_buf = list_first_entry(¶ms->params, 1461 struct rkisp1_buffer, queue); 1462 1463 new_params = (struct rkisp1_params_cfg *)vb2_plane_vaddr(&cur_buf->vb.vb2_buf, 0); 1464 1465 rkisp1_isp_isr_other_config(params, new_params); 1466 rkisp1_isp_isr_meas_config(params, new_params); 1467 1468 /* update shadow register immediately */ 1469 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_CTRL, RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD); 1470 1471 list_del(&cur_buf->queue); 1472 1473 cur_buf->vb.sequence = frame_sequence; 1474 vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); 1475 } 1476 1477 void rkisp1_params_isr(struct rkisp1_device *rkisp1) 1478 { 1479 /* 1480 * This isr is called when the ISR finishes processing a frame (RKISP1_CIF_ISP_FRAME). 1481 * Configurations performed here will be applied on the next frame. 1482 * Since frame_sequence is updated on the vertical sync signal, we should use 1483 * frame_sequence + 1 here to indicate to userspace on which frame these parameters 1484 * are being applied. 1485 */ 1486 unsigned int frame_sequence = rkisp1->isp.frame_sequence + 1; 1487 struct rkisp1_params *params = &rkisp1->params; 1488 1489 spin_lock(¶ms->config_lock); 1490 rkisp1_params_apply_params_cfg(params, frame_sequence); 1491 1492 spin_unlock(¶ms->config_lock); 1493 } 1494 1495 static const struct rkisp1_cif_isp_awb_meas_config rkisp1_awb_params_default_config = { 1496 { 1497 0, 0, RKISP1_DEFAULT_WIDTH, RKISP1_DEFAULT_HEIGHT 1498 }, 1499 RKISP1_CIF_ISP_AWB_MODE_YCBCR, 200, 30, 20, 20, 0, 128, 128 1500 }; 1501 1502 static const struct rkisp1_cif_isp_aec_config rkisp1_aec_params_default_config = { 1503 RKISP1_CIF_ISP_EXP_MEASURING_MODE_0, 1504 RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_0, 1505 { 1506 RKISP1_DEFAULT_WIDTH >> 2, RKISP1_DEFAULT_HEIGHT >> 2, 1507 RKISP1_DEFAULT_WIDTH >> 1, RKISP1_DEFAULT_HEIGHT >> 1 1508 } 1509 }; 1510 1511 static const struct rkisp1_cif_isp_hst_config rkisp1_hst_params_default_config = { 1512 RKISP1_CIF_ISP_HISTOGRAM_MODE_RGB_COMBINED, 1513 3, 1514 { 1515 RKISP1_DEFAULT_WIDTH >> 2, RKISP1_DEFAULT_HEIGHT >> 2, 1516 RKISP1_DEFAULT_WIDTH >> 1, RKISP1_DEFAULT_HEIGHT >> 1 1517 }, 1518 { 1519 0, /* To be filled in with 0x01 at runtime. */ 1520 } 1521 }; 1522 1523 static const struct rkisp1_cif_isp_afc_config rkisp1_afc_params_default_config = { 1524 1, 1525 { 1526 { 1527 300, 225, 200, 150 1528 } 1529 }, 1530 4, 1531 14 1532 }; 1533 1534 static void rkisp1_params_config_parameter(struct rkisp1_params *params) 1535 { 1536 struct rkisp1_cif_isp_hst_config hst = rkisp1_hst_params_default_config; 1537 1538 params->ops->awb_meas_config(params, &rkisp1_awb_params_default_config); 1539 params->ops->awb_meas_enable(params, &rkisp1_awb_params_default_config, 1540 true); 1541 1542 params->ops->aec_config(params, &rkisp1_aec_params_default_config); 1543 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_EXP_CTRL, 1544 RKISP1_CIF_ISP_EXP_ENA); 1545 1546 params->ops->afm_config(params, &rkisp1_afc_params_default_config); 1547 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_AFM_CTRL, 1548 RKISP1_CIF_ISP_AFM_ENA); 1549 1550 memset(hst.hist_weight, 0x01, sizeof(hst.hist_weight)); 1551 params->ops->hst_config(params, &hst); 1552 rkisp1_param_set_bits(params, RKISP1_CIF_ISP_HIST_PROP_V10, 1553 rkisp1_hst_params_default_config.mode); 1554 1555 /* set the range */ 1556 if (params->quantization == V4L2_QUANTIZATION_FULL_RANGE) 1557 rkisp1_csm_config(params, true); 1558 else 1559 rkisp1_csm_config(params, false); 1560 1561 spin_lock_irq(¶ms->config_lock); 1562 1563 /* apply the first buffer if there is one already */ 1564 rkisp1_params_apply_params_cfg(params, 0); 1565 1566 spin_unlock_irq(¶ms->config_lock); 1567 } 1568 1569 void rkisp1_params_configure(struct rkisp1_params *params, 1570 enum rkisp1_fmt_raw_pat_type bayer_pat, 1571 enum v4l2_quantization quantization) 1572 { 1573 params->quantization = quantization; 1574 params->raw_type = bayer_pat; 1575 rkisp1_params_config_parameter(params); 1576 } 1577 1578 /* 1579 * Not called when the camera is active, therefore there is no need to acquire 1580 * a lock. 1581 */ 1582 void rkisp1_params_disable(struct rkisp1_params *params) 1583 { 1584 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DPCC_MODE, 1585 RKISP1_CIF_ISP_DPCC_ENA); 1586 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_LSC_CTRL, 1587 RKISP1_CIF_ISP_LSC_CTRL_ENA); 1588 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_BLS_CTRL, 1589 RKISP1_CIF_ISP_BLS_ENA); 1590 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1591 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_IN_ENA); 1592 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1593 RKISP1_CIF_ISP_CTRL_ISP_GAMMA_OUT_ENA); 1594 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DEMOSAIC, 1595 RKISP1_CIF_ISP_DEMOSAIC_BYPASS); 1596 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_FILT_MODE, 1597 RKISP1_CIF_ISP_FLT_ENA); 1598 params->ops->awb_meas_enable(params, NULL, false); 1599 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_CTRL, 1600 RKISP1_CIF_ISP_CTRL_ISP_AWB_ENA); 1601 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_EXP_CTRL, 1602 RKISP1_CIF_ISP_EXP_ENA); 1603 rkisp1_ctk_enable(params, false); 1604 rkisp1_param_clear_bits(params, RKISP1_CIF_C_PROC_CTRL, 1605 RKISP1_CIF_C_PROC_CTR_ENABLE); 1606 params->ops->hst_enable(params, NULL, false); 1607 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_AFM_CTRL, 1608 RKISP1_CIF_ISP_AFM_ENA); 1609 rkisp1_ie_enable(params, false); 1610 rkisp1_param_clear_bits(params, RKISP1_CIF_ISP_DPF_MODE, 1611 RKISP1_CIF_ISP_DPF_MODE_EN); 1612 } 1613 1614 static const struct rkisp1_params_ops rkisp1_v10_params_ops = { 1615 .lsc_matrix_config = rkisp1_lsc_matrix_config_v10, 1616 .goc_config = rkisp1_goc_config_v10, 1617 .awb_meas_config = rkisp1_awb_meas_config_v10, 1618 .awb_meas_enable = rkisp1_awb_meas_enable_v10, 1619 .awb_gain_config = rkisp1_awb_gain_config_v10, 1620 .aec_config = rkisp1_aec_config_v10, 1621 .hst_config = rkisp1_hst_config_v10, 1622 .hst_enable = rkisp1_hst_enable_v10, 1623 .afm_config = rkisp1_afm_config_v10, 1624 }; 1625 1626 static struct rkisp1_params_ops rkisp1_v12_params_ops = { 1627 .lsc_matrix_config = rkisp1_lsc_matrix_config_v12, 1628 .goc_config = rkisp1_goc_config_v12, 1629 .awb_meas_config = rkisp1_awb_meas_config_v12, 1630 .awb_meas_enable = rkisp1_awb_meas_enable_v12, 1631 .awb_gain_config = rkisp1_awb_gain_config_v12, 1632 .aec_config = rkisp1_aec_config_v12, 1633 .hst_config = rkisp1_hst_config_v12, 1634 .hst_enable = rkisp1_hst_enable_v12, 1635 .afm_config = rkisp1_afm_config_v12, 1636 }; 1637 1638 static int rkisp1_params_enum_fmt_meta_out(struct file *file, void *priv, 1639 struct v4l2_fmtdesc *f) 1640 { 1641 struct video_device *video = video_devdata(file); 1642 struct rkisp1_params *params = video_get_drvdata(video); 1643 1644 if (f->index > 0 || f->type != video->queue->type) 1645 return -EINVAL; 1646 1647 f->pixelformat = params->vdev_fmt.fmt.meta.dataformat; 1648 1649 return 0; 1650 } 1651 1652 static int rkisp1_params_g_fmt_meta_out(struct file *file, void *fh, 1653 struct v4l2_format *f) 1654 { 1655 struct video_device *video = video_devdata(file); 1656 struct rkisp1_params *params = video_get_drvdata(video); 1657 struct v4l2_meta_format *meta = &f->fmt.meta; 1658 1659 if (f->type != video->queue->type) 1660 return -EINVAL; 1661 1662 memset(meta, 0, sizeof(*meta)); 1663 meta->dataformat = params->vdev_fmt.fmt.meta.dataformat; 1664 meta->buffersize = params->vdev_fmt.fmt.meta.buffersize; 1665 1666 return 0; 1667 } 1668 1669 static int rkisp1_params_querycap(struct file *file, 1670 void *priv, struct v4l2_capability *cap) 1671 { 1672 struct video_device *vdev = video_devdata(file); 1673 1674 strscpy(cap->driver, RKISP1_DRIVER_NAME, sizeof(cap->driver)); 1675 strscpy(cap->card, vdev->name, sizeof(cap->card)); 1676 strscpy(cap->bus_info, RKISP1_BUS_INFO, sizeof(cap->bus_info)); 1677 1678 return 0; 1679 } 1680 1681 /* ISP params video device IOCTLs */ 1682 static const struct v4l2_ioctl_ops rkisp1_params_ioctl = { 1683 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1684 .vidioc_querybuf = vb2_ioctl_querybuf, 1685 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1686 .vidioc_qbuf = vb2_ioctl_qbuf, 1687 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1688 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1689 .vidioc_expbuf = vb2_ioctl_expbuf, 1690 .vidioc_streamon = vb2_ioctl_streamon, 1691 .vidioc_streamoff = vb2_ioctl_streamoff, 1692 .vidioc_enum_fmt_meta_out = rkisp1_params_enum_fmt_meta_out, 1693 .vidioc_g_fmt_meta_out = rkisp1_params_g_fmt_meta_out, 1694 .vidioc_s_fmt_meta_out = rkisp1_params_g_fmt_meta_out, 1695 .vidioc_try_fmt_meta_out = rkisp1_params_g_fmt_meta_out, 1696 .vidioc_querycap = rkisp1_params_querycap, 1697 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1698 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1699 }; 1700 1701 static int rkisp1_params_vb2_queue_setup(struct vb2_queue *vq, 1702 unsigned int *num_buffers, 1703 unsigned int *num_planes, 1704 unsigned int sizes[], 1705 struct device *alloc_devs[]) 1706 { 1707 *num_buffers = clamp_t(u32, *num_buffers, 1708 RKISP1_ISP_PARAMS_REQ_BUFS_MIN, 1709 RKISP1_ISP_PARAMS_REQ_BUFS_MAX); 1710 1711 *num_planes = 1; 1712 1713 sizes[0] = sizeof(struct rkisp1_params_cfg); 1714 1715 return 0; 1716 } 1717 1718 static void rkisp1_params_vb2_buf_queue(struct vb2_buffer *vb) 1719 { 1720 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1721 struct rkisp1_buffer *params_buf = 1722 container_of(vbuf, struct rkisp1_buffer, vb); 1723 struct vb2_queue *vq = vb->vb2_queue; 1724 struct rkisp1_params *params = vq->drv_priv; 1725 1726 spin_lock_irq(¶ms->config_lock); 1727 list_add_tail(¶ms_buf->queue, ¶ms->params); 1728 spin_unlock_irq(¶ms->config_lock); 1729 } 1730 1731 static int rkisp1_params_vb2_buf_prepare(struct vb2_buffer *vb) 1732 { 1733 if (vb2_plane_size(vb, 0) < sizeof(struct rkisp1_params_cfg)) 1734 return -EINVAL; 1735 1736 vb2_set_plane_payload(vb, 0, sizeof(struct rkisp1_params_cfg)); 1737 1738 return 0; 1739 } 1740 1741 static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) 1742 { 1743 struct rkisp1_params *params = vq->drv_priv; 1744 struct rkisp1_buffer *buf; 1745 LIST_HEAD(tmp_list); 1746 1747 /* 1748 * we first move the buffers into a local list 'tmp_list' 1749 * and then we can iterate it and call vb2_buffer_done 1750 * without holding the lock 1751 */ 1752 spin_lock_irq(¶ms->config_lock); 1753 list_splice_init(¶ms->params, &tmp_list); 1754 spin_unlock_irq(¶ms->config_lock); 1755 1756 list_for_each_entry(buf, &tmp_list, queue) 1757 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 1758 } 1759 1760 static const struct vb2_ops rkisp1_params_vb2_ops = { 1761 .queue_setup = rkisp1_params_vb2_queue_setup, 1762 .wait_prepare = vb2_ops_wait_prepare, 1763 .wait_finish = vb2_ops_wait_finish, 1764 .buf_queue = rkisp1_params_vb2_buf_queue, 1765 .buf_prepare = rkisp1_params_vb2_buf_prepare, 1766 .stop_streaming = rkisp1_params_vb2_stop_streaming, 1767 1768 }; 1769 1770 static const struct v4l2_file_operations rkisp1_params_fops = { 1771 .mmap = vb2_fop_mmap, 1772 .unlocked_ioctl = video_ioctl2, 1773 .poll = vb2_fop_poll, 1774 .open = v4l2_fh_open, 1775 .release = vb2_fop_release 1776 }; 1777 1778 static int rkisp1_params_init_vb2_queue(struct vb2_queue *q, 1779 struct rkisp1_params *params) 1780 { 1781 struct rkisp1_vdev_node *node; 1782 1783 node = container_of(q, struct rkisp1_vdev_node, buf_queue); 1784 1785 q->type = V4L2_BUF_TYPE_META_OUTPUT; 1786 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1787 q->drv_priv = params; 1788 q->ops = &rkisp1_params_vb2_ops; 1789 q->mem_ops = &vb2_vmalloc_memops; 1790 q->buf_struct_size = sizeof(struct rkisp1_buffer); 1791 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1792 q->lock = &node->vlock; 1793 1794 return vb2_queue_init(q); 1795 } 1796 1797 static void rkisp1_init_params(struct rkisp1_params *params) 1798 { 1799 params->vdev_fmt.fmt.meta.dataformat = 1800 V4L2_META_FMT_RK_ISP1_PARAMS; 1801 params->vdev_fmt.fmt.meta.buffersize = 1802 sizeof(struct rkisp1_params_cfg); 1803 1804 if (params->rkisp1->info->isp_ver == RKISP1_V12) 1805 params->ops = &rkisp1_v12_params_ops; 1806 else 1807 params->ops = &rkisp1_v10_params_ops; 1808 } 1809 1810 int rkisp1_params_register(struct rkisp1_device *rkisp1) 1811 { 1812 struct rkisp1_params *params = &rkisp1->params; 1813 struct rkisp1_vdev_node *node = ¶ms->vnode; 1814 struct video_device *vdev = &node->vdev; 1815 int ret; 1816 1817 params->rkisp1 = rkisp1; 1818 mutex_init(&node->vlock); 1819 INIT_LIST_HEAD(¶ms->params); 1820 spin_lock_init(¶ms->config_lock); 1821 1822 strscpy(vdev->name, RKISP1_PARAMS_DEV_NAME, sizeof(vdev->name)); 1823 1824 video_set_drvdata(vdev, params); 1825 vdev->ioctl_ops = &rkisp1_params_ioctl; 1826 vdev->fops = &rkisp1_params_fops; 1827 vdev->release = video_device_release_empty; 1828 /* 1829 * Provide a mutex to v4l2 core. It will be used 1830 * to protect all fops and v4l2 ioctls. 1831 */ 1832 vdev->lock = &node->vlock; 1833 vdev->v4l2_dev = &rkisp1->v4l2_dev; 1834 vdev->queue = &node->buf_queue; 1835 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_META_OUTPUT; 1836 vdev->vfl_dir = VFL_DIR_TX; 1837 rkisp1_params_init_vb2_queue(vdev->queue, params); 1838 rkisp1_init_params(params); 1839 video_set_drvdata(vdev, params); 1840 1841 node->pad.flags = MEDIA_PAD_FL_SOURCE; 1842 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad); 1843 if (ret) 1844 goto error; 1845 1846 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1847 if (ret) { 1848 dev_err(rkisp1->dev, 1849 "failed to register %s, ret=%d\n", vdev->name, ret); 1850 goto error; 1851 } 1852 1853 return 0; 1854 1855 error: 1856 media_entity_cleanup(&vdev->entity); 1857 mutex_destroy(&node->vlock); 1858 return ret; 1859 } 1860 1861 void rkisp1_params_unregister(struct rkisp1_device *rkisp1) 1862 { 1863 struct rkisp1_params *params = &rkisp1->params; 1864 struct rkisp1_vdev_node *node = ¶ms->vnode; 1865 struct video_device *vdev = &node->vdev; 1866 1867 if (!video_is_registered(vdev)) 1868 return; 1869 1870 vb2_video_unregister_device(vdev); 1871 media_entity_cleanup(&vdev->entity); 1872 mutex_destroy(&node->vlock); 1873 } 1874