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