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