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