1 // SPDX-License-Identifier: GPL-2.0-only
3 * Microchip Image Sensor Controller (ISC) common driver base
5 * Copyright (C) 2016-2019 Microchip Technology, Inc.
8 * Author: Eugen Hristev <eugen.hristev@microchip.com>
12 #include <linux/clk.h>
13 #include <linux/clkdev.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
20 #include <linux/of_graph.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/videodev2.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-image-sizes.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-fwnode.h>
32 #include <media/v4l2-subdev.h>
33 #include <media/videobuf2-dma-contig.h>
35 #include "atmel-isc-regs.h"
36 #include "atmel-isc.h"
38 static unsigned int debug
;
39 module_param(debug
, int, 0644);
40 MODULE_PARM_DESC(debug
, "debug level (0-2)");
42 static unsigned int sensor_preferred
= 1;
43 module_param(sensor_preferred
, uint
, 0644);
44 MODULE_PARM_DESC(sensor_preferred
,
45 "Sensor is preferred to output the specified format (1-on 0-off), default 1");
47 /* This is a list of the formats that the ISC can *output* */
48 const struct isc_format controller_formats
[] = {
50 .fourcc
= V4L2_PIX_FMT_ARGB444
,
53 .fourcc
= V4L2_PIX_FMT_ARGB555
,
56 .fourcc
= V4L2_PIX_FMT_RGB565
,
59 .fourcc
= V4L2_PIX_FMT_ABGR32
,
62 .fourcc
= V4L2_PIX_FMT_XBGR32
,
65 .fourcc
= V4L2_PIX_FMT_YUV420
,
68 .fourcc
= V4L2_PIX_FMT_YUYV
,
71 .fourcc
= V4L2_PIX_FMT_YUV422P
,
74 .fourcc
= V4L2_PIX_FMT_GREY
,
77 .fourcc
= V4L2_PIX_FMT_Y10
,
81 /* This is a list of formats that the ISC can receive as *input* */
82 struct isc_format formats_list
[] = {
84 .fourcc
= V4L2_PIX_FMT_SBGGR8
,
85 .mbus_code
= MEDIA_BUS_FMT_SBGGR8_1X8
,
86 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
87 .cfa_baycfg
= ISC_BAY_CFG_BGBG
,
90 .fourcc
= V4L2_PIX_FMT_SGBRG8
,
91 .mbus_code
= MEDIA_BUS_FMT_SGBRG8_1X8
,
92 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
93 .cfa_baycfg
= ISC_BAY_CFG_GBGB
,
96 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
97 .mbus_code
= MEDIA_BUS_FMT_SGRBG8_1X8
,
98 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
99 .cfa_baycfg
= ISC_BAY_CFG_GRGR
,
102 .fourcc
= V4L2_PIX_FMT_SRGGB8
,
103 .mbus_code
= MEDIA_BUS_FMT_SRGGB8_1X8
,
104 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
105 .cfa_baycfg
= ISC_BAY_CFG_RGRG
,
108 .fourcc
= V4L2_PIX_FMT_SBGGR10
,
109 .mbus_code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
110 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TEN
,
111 .cfa_baycfg
= ISC_BAY_CFG_RGRG
,
114 .fourcc
= V4L2_PIX_FMT_SGBRG10
,
115 .mbus_code
= MEDIA_BUS_FMT_SGBRG10_1X10
,
116 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TEN
,
117 .cfa_baycfg
= ISC_BAY_CFG_GBGB
,
120 .fourcc
= V4L2_PIX_FMT_SGRBG10
,
121 .mbus_code
= MEDIA_BUS_FMT_SGRBG10_1X10
,
122 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TEN
,
123 .cfa_baycfg
= ISC_BAY_CFG_GRGR
,
126 .fourcc
= V4L2_PIX_FMT_SRGGB10
,
127 .mbus_code
= MEDIA_BUS_FMT_SRGGB10_1X10
,
128 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TEN
,
129 .cfa_baycfg
= ISC_BAY_CFG_RGRG
,
132 .fourcc
= V4L2_PIX_FMT_SBGGR12
,
133 .mbus_code
= MEDIA_BUS_FMT_SBGGR12_1X12
,
134 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TWELVE
,
135 .cfa_baycfg
= ISC_BAY_CFG_BGBG
,
138 .fourcc
= V4L2_PIX_FMT_SGBRG12
,
139 .mbus_code
= MEDIA_BUS_FMT_SGBRG12_1X12
,
140 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TWELVE
,
141 .cfa_baycfg
= ISC_BAY_CFG_GBGB
,
144 .fourcc
= V4L2_PIX_FMT_SGRBG12
,
145 .mbus_code
= MEDIA_BUS_FMT_SGRBG12_1X12
,
146 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TWELVE
,
147 .cfa_baycfg
= ISC_BAY_CFG_GRGR
,
150 .fourcc
= V4L2_PIX_FMT_SRGGB12
,
151 .mbus_code
= MEDIA_BUS_FMT_SRGGB12_1X12
,
152 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TWELVE
,
153 .cfa_baycfg
= ISC_BAY_CFG_RGRG
,
156 .fourcc
= V4L2_PIX_FMT_GREY
,
157 .mbus_code
= MEDIA_BUS_FMT_Y8_1X8
,
158 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
161 .fourcc
= V4L2_PIX_FMT_YUYV
,
162 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
163 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
166 .fourcc
= V4L2_PIX_FMT_RGB565
,
167 .mbus_code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
168 .pfe_cfg0_bps
= ISC_PFE_CFG0_BPS_EIGHT
,
171 .fourcc
= V4L2_PIX_FMT_Y10
,
172 .mbus_code
= MEDIA_BUS_FMT_Y10_1X10
,
173 .pfe_cfg0_bps
= ISC_PFG_CFG0_BPS_TEN
,
178 /* Gamma table with gamma 1/2.2 */
179 const u32 isc_gamma_table
[GAMMA_MAX
+ 1][GAMMA_ENTRIES
] = {
180 /* 0 --> gamma 1/1.8 */
181 { 0x65, 0x66002F, 0x950025, 0xBB0020, 0xDB001D, 0xF8001A,
182 0x1130018, 0x12B0017, 0x1420016, 0x1580014, 0x16D0013, 0x1810012,
183 0x1940012, 0x1A60012, 0x1B80011, 0x1C90010, 0x1DA0010, 0x1EA000F,
184 0x1FA000F, 0x209000F, 0x218000F, 0x227000E, 0x235000E, 0x243000E,
185 0x251000E, 0x25F000D, 0x26C000D, 0x279000D, 0x286000D, 0x293000C,
186 0x2A0000C, 0x2AC000C, 0x2B8000C, 0x2C4000C, 0x2D0000B, 0x2DC000B,
187 0x2E7000B, 0x2F3000B, 0x2FE000B, 0x309000B, 0x314000B, 0x31F000A,
188 0x32A000A, 0x334000B, 0x33F000A, 0x349000A, 0x354000A, 0x35E000A,
189 0x368000A, 0x372000A, 0x37C000A, 0x386000A, 0x3900009, 0x399000A,
190 0x3A30009, 0x3AD0009, 0x3B60009, 0x3BF000A, 0x3C90009, 0x3D20009,
191 0x3DB0009, 0x3E40009, 0x3ED0009, 0x3F60009 },
193 /* 1 --> gamma 1/2 */
194 { 0x7F, 0x800034, 0xB50028, 0xDE0021, 0x100001E, 0x11E001B,
195 0x1390019, 0x1520017, 0x16A0015, 0x1800014, 0x1940014, 0x1A80013,
196 0x1BB0012, 0x1CD0011, 0x1DF0010, 0x1EF0010, 0x200000F, 0x20F000F,
197 0x21F000E, 0x22D000F, 0x23C000E, 0x24A000E, 0x258000D, 0x265000D,
198 0x273000C, 0x27F000D, 0x28C000C, 0x299000C, 0x2A5000C, 0x2B1000B,
199 0x2BC000C, 0x2C8000B, 0x2D3000C, 0x2DF000B, 0x2EA000A, 0x2F5000A,
200 0x2FF000B, 0x30A000A, 0x314000B, 0x31F000A, 0x329000A, 0x333000A,
201 0x33D0009, 0x3470009, 0x350000A, 0x35A0009, 0x363000A, 0x36D0009,
202 0x3760009, 0x37F0009, 0x3880009, 0x3910009, 0x39A0009, 0x3A30009,
203 0x3AC0008, 0x3B40009, 0x3BD0008, 0x3C60008, 0x3CE0008, 0x3D60009,
204 0x3DF0008, 0x3E70008, 0x3EF0008, 0x3F70008 },
206 /* 2 --> gamma 1/2.2 */
207 { 0x99, 0x9B0038, 0xD4002A, 0xFF0023, 0x122001F, 0x141001B,
208 0x15D0019, 0x1760017, 0x18E0015, 0x1A30015, 0x1B80013, 0x1CC0012,
209 0x1DE0011, 0x1F00010, 0x2010010, 0x2110010, 0x221000F, 0x230000F,
210 0x23F000E, 0x24D000E, 0x25B000D, 0x269000C, 0x276000C, 0x283000C,
211 0x28F000C, 0x29B000C, 0x2A7000C, 0x2B3000B, 0x2BF000B, 0x2CA000B,
212 0x2D5000B, 0x2E0000A, 0x2EB000A, 0x2F5000A, 0x2FF000A, 0x30A000A,
213 0x3140009, 0x31E0009, 0x327000A, 0x3310009, 0x33A0009, 0x3440009,
214 0x34D0009, 0x3560009, 0x35F0009, 0x3680008, 0x3710008, 0x3790009,
215 0x3820008, 0x38A0008, 0x3930008, 0x39B0008, 0x3A30008, 0x3AB0008,
216 0x3B30008, 0x3BB0008, 0x3C30008, 0x3CB0007, 0x3D20008, 0x3DA0007,
217 0x3E20007, 0x3E90007, 0x3F00008, 0x3F80007 },
220 #define ISC_IS_FORMAT_RAW(mbus_code) \
221 (((mbus_code) & 0xf000) == 0x3000)
223 #define ISC_IS_FORMAT_GREY(mbus_code) \
224 (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
225 (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
227 static inline void isc_update_awb_ctrls(struct isc_device
*isc
)
229 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
231 regmap_write(isc
->regmap
, ISC_WB_O_RGR
,
232 (ISC_WB_O_ZERO_VAL
- (ctrls
->offset
[ISC_HIS_CFG_MODE_R
])) |
233 ((ISC_WB_O_ZERO_VAL
- ctrls
->offset
[ISC_HIS_CFG_MODE_GR
]) << 16));
234 regmap_write(isc
->regmap
, ISC_WB_O_BGB
,
235 (ISC_WB_O_ZERO_VAL
- (ctrls
->offset
[ISC_HIS_CFG_MODE_B
])) |
236 ((ISC_WB_O_ZERO_VAL
- ctrls
->offset
[ISC_HIS_CFG_MODE_GB
]) << 16));
237 regmap_write(isc
->regmap
, ISC_WB_G_RGR
,
238 ctrls
->gain
[ISC_HIS_CFG_MODE_R
] |
239 (ctrls
->gain
[ISC_HIS_CFG_MODE_GR
] << 16));
240 regmap_write(isc
->regmap
, ISC_WB_G_BGB
,
241 ctrls
->gain
[ISC_HIS_CFG_MODE_B
] |
242 (ctrls
->gain
[ISC_HIS_CFG_MODE_GB
] << 16));
245 static inline void isc_reset_awb_ctrls(struct isc_device
*isc
)
249 for (c
= ISC_HIS_CFG_MODE_GR
; c
<= ISC_HIS_CFG_MODE_B
; c
++) {
250 /* gains have a fixed point at 9 decimals */
251 isc
->ctrls
.gain
[c
] = 1 << 9;
252 /* offsets are in 2's complements, the value
253 * will be substracted from ISC_WB_O_ZERO_VAL to obtain
254 * 2's complement of a value between 0 and
255 * ISC_WB_O_ZERO_VAL >> 1
257 isc
->ctrls
.offset
[c
] = ISC_WB_O_ZERO_VAL
;
261 static int isc_wait_clk_stable(struct clk_hw
*hw
)
263 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
264 struct regmap
*regmap
= isc_clk
->regmap
;
265 unsigned long timeout
= jiffies
+ usecs_to_jiffies(1000);
268 while (time_before(jiffies
, timeout
)) {
269 regmap_read(regmap
, ISC_CLKSR
, &status
);
270 if (!(status
& ISC_CLKSR_SIP
))
273 usleep_range(10, 250);
279 static int isc_clk_prepare(struct clk_hw
*hw
)
281 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
283 if (isc_clk
->id
== ISC_ISPCK
)
284 pm_runtime_get_sync(isc_clk
->dev
);
286 return isc_wait_clk_stable(hw
);
289 static void isc_clk_unprepare(struct clk_hw
*hw
)
291 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
293 isc_wait_clk_stable(hw
);
295 if (isc_clk
->id
== ISC_ISPCK
)
296 pm_runtime_put_sync(isc_clk
->dev
);
299 static int isc_clk_enable(struct clk_hw
*hw
)
301 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
302 u32 id
= isc_clk
->id
;
303 struct regmap
*regmap
= isc_clk
->regmap
;
307 dev_dbg(isc_clk
->dev
, "ISC CLK: %s, div = %d, parent id = %d\n",
308 __func__
, isc_clk
->div
, isc_clk
->parent_id
);
310 spin_lock_irqsave(&isc_clk
->lock
, flags
);
311 regmap_update_bits(regmap
, ISC_CLKCFG
,
312 ISC_CLKCFG_DIV_MASK(id
) | ISC_CLKCFG_SEL_MASK(id
),
313 (isc_clk
->div
<< ISC_CLKCFG_DIV_SHIFT(id
)) |
314 (isc_clk
->parent_id
<< ISC_CLKCFG_SEL_SHIFT(id
)));
316 regmap_write(regmap
, ISC_CLKEN
, ISC_CLK(id
));
317 spin_unlock_irqrestore(&isc_clk
->lock
, flags
);
319 regmap_read(regmap
, ISC_CLKSR
, &status
);
320 if (status
& ISC_CLK(id
))
326 static void isc_clk_disable(struct clk_hw
*hw
)
328 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
329 u32 id
= isc_clk
->id
;
332 spin_lock_irqsave(&isc_clk
->lock
, flags
);
333 regmap_write(isc_clk
->regmap
, ISC_CLKDIS
, ISC_CLK(id
));
334 spin_unlock_irqrestore(&isc_clk
->lock
, flags
);
337 static int isc_clk_is_enabled(struct clk_hw
*hw
)
339 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
342 if (isc_clk
->id
== ISC_ISPCK
)
343 pm_runtime_get_sync(isc_clk
->dev
);
345 regmap_read(isc_clk
->regmap
, ISC_CLKSR
, &status
);
347 if (isc_clk
->id
== ISC_ISPCK
)
348 pm_runtime_put_sync(isc_clk
->dev
);
350 return status
& ISC_CLK(isc_clk
->id
) ? 1 : 0;
354 isc_clk_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
356 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
358 return DIV_ROUND_CLOSEST(parent_rate
, isc_clk
->div
+ 1);
361 static int isc_clk_determine_rate(struct clk_hw
*hw
,
362 struct clk_rate_request
*req
)
364 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
365 long best_rate
= -EINVAL
;
369 for (i
= 0; i
< clk_hw_get_num_parents(hw
); i
++) {
370 struct clk_hw
*parent
;
371 unsigned long parent_rate
;
373 parent
= clk_hw_get_parent_by_index(hw
, i
);
377 parent_rate
= clk_hw_get_rate(parent
);
381 for (div
= 1; div
< ISC_CLK_MAX_DIV
+ 2; div
++) {
385 rate
= DIV_ROUND_CLOSEST(parent_rate
, div
);
386 diff
= abs(req
->rate
- rate
);
388 if (best_diff
< 0 || best_diff
> diff
) {
391 req
->best_parent_rate
= parent_rate
;
392 req
->best_parent_hw
= parent
;
395 if (!best_diff
|| rate
< req
->rate
)
403 dev_dbg(isc_clk
->dev
,
404 "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
406 __clk_get_name((req
->best_parent_hw
)->clk
),
407 req
->best_parent_rate
);
412 req
->rate
= best_rate
;
417 static int isc_clk_set_parent(struct clk_hw
*hw
, u8 index
)
419 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
421 if (index
>= clk_hw_get_num_parents(hw
))
424 isc_clk
->parent_id
= index
;
429 static u8
isc_clk_get_parent(struct clk_hw
*hw
)
431 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
433 return isc_clk
->parent_id
;
436 static int isc_clk_set_rate(struct clk_hw
*hw
,
438 unsigned long parent_rate
)
440 struct isc_clk
*isc_clk
= to_isc_clk(hw
);
446 div
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
447 if (div
> (ISC_CLK_MAX_DIV
+ 1) || !div
)
450 isc_clk
->div
= div
- 1;
455 static const struct clk_ops isc_clk_ops
= {
456 .prepare
= isc_clk_prepare
,
457 .unprepare
= isc_clk_unprepare
,
458 .enable
= isc_clk_enable
,
459 .disable
= isc_clk_disable
,
460 .is_enabled
= isc_clk_is_enabled
,
461 .recalc_rate
= isc_clk_recalc_rate
,
462 .determine_rate
= isc_clk_determine_rate
,
463 .set_parent
= isc_clk_set_parent
,
464 .get_parent
= isc_clk_get_parent
,
465 .set_rate
= isc_clk_set_rate
,
468 static int isc_clk_register(struct isc_device
*isc
, unsigned int id
)
470 struct regmap
*regmap
= isc
->regmap
;
471 struct device_node
*np
= isc
->dev
->of_node
;
472 struct isc_clk
*isc_clk
;
473 struct clk_init_data init
;
474 const char *clk_name
= np
->name
;
475 const char *parent_names
[3];
478 num_parents
= of_clk_get_parent_count(np
);
479 if (num_parents
< 1 || num_parents
> 3)
482 if (num_parents
> 2 && id
== ISC_ISPCK
)
485 of_clk_parent_fill(np
, parent_names
, num_parents
);
488 of_property_read_string(np
, "clock-output-names", &clk_name
);
490 clk_name
= "isc-ispck";
492 init
.parent_names
= parent_names
;
493 init
.num_parents
= num_parents
;
494 init
.name
= clk_name
;
495 init
.ops
= &isc_clk_ops
;
496 init
.flags
= CLK_SET_RATE_GATE
| CLK_SET_PARENT_GATE
;
498 isc_clk
= &isc
->isc_clks
[id
];
499 isc_clk
->hw
.init
= &init
;
500 isc_clk
->regmap
= regmap
;
502 isc_clk
->dev
= isc
->dev
;
503 spin_lock_init(&isc_clk
->lock
);
505 isc_clk
->clk
= clk_register(isc
->dev
, &isc_clk
->hw
);
506 if (IS_ERR(isc_clk
->clk
)) {
507 dev_err(isc
->dev
, "%s: clock register fail\n", clk_name
);
508 return PTR_ERR(isc_clk
->clk
);
509 } else if (id
== ISC_MCK
)
510 of_clk_add_provider(np
, of_clk_src_simple_get
, isc_clk
->clk
);
515 int isc_clk_init(struct isc_device
*isc
)
520 for (i
= 0; i
< ARRAY_SIZE(isc
->isc_clks
); i
++)
521 isc
->isc_clks
[i
].clk
= ERR_PTR(-EINVAL
);
523 for (i
= 0; i
< ARRAY_SIZE(isc
->isc_clks
); i
++) {
524 ret
= isc_clk_register(isc
, i
);
532 void isc_clk_cleanup(struct isc_device
*isc
)
536 of_clk_del_provider(isc
->dev
->of_node
);
538 for (i
= 0; i
< ARRAY_SIZE(isc
->isc_clks
); i
++) {
539 struct isc_clk
*isc_clk
= &isc
->isc_clks
[i
];
541 if (!IS_ERR(isc_clk
->clk
))
542 clk_unregister(isc_clk
->clk
);
546 static int isc_queue_setup(struct vb2_queue
*vq
,
547 unsigned int *nbuffers
, unsigned int *nplanes
,
548 unsigned int sizes
[], struct device
*alloc_devs
[])
550 struct isc_device
*isc
= vb2_get_drv_priv(vq
);
551 unsigned int size
= isc
->fmt
.fmt
.pix
.sizeimage
;
554 return sizes
[0] < size
? -EINVAL
: 0;
562 static int isc_buffer_prepare(struct vb2_buffer
*vb
)
564 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
565 struct isc_device
*isc
= vb2_get_drv_priv(vb
->vb2_queue
);
566 unsigned long size
= isc
->fmt
.fmt
.pix
.sizeimage
;
568 if (vb2_plane_size(vb
, 0) < size
) {
569 v4l2_err(&isc
->v4l2_dev
, "buffer too small (%lu < %lu)\n",
570 vb2_plane_size(vb
, 0), size
);
574 vb2_set_plane_payload(vb
, 0, size
);
576 vbuf
->field
= isc
->fmt
.fmt
.pix
.field
;
581 static void isc_start_dma(struct isc_device
*isc
)
583 struct regmap
*regmap
= isc
->regmap
;
584 u32 sizeimage
= isc
->fmt
.fmt
.pix
.sizeimage
;
589 h
= isc
->fmt
.fmt
.pix
.height
;
590 w
= isc
->fmt
.fmt
.pix
.width
;
593 * In case the sensor is not RAW, it will output a pixel (12-16 bits)
594 * with two samples on the ISC Data bus (which is 8-12)
595 * ISC will count each sample, so, we need to multiply these values
596 * by two, to get the real number of samples for the required pixels.
598 if (!ISC_IS_FORMAT_RAW(isc
->config
.sd_format
->mbus_code
)) {
604 * We limit the column/row count that the ISC will output according
605 * to the configured resolution that we want.
606 * This will avoid the situation where the sensor is misconfigured,
607 * sending more data, and the ISC will just take it and DMA to memory,
608 * causing corruption.
610 regmap_write(regmap
, ISC_PFE_CFG1
,
611 (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK
) |
612 (ISC_PFE_CFG1_COLMAX(w
- 1) & ISC_PFE_CFG1_COLMAX_MASK
));
614 regmap_write(regmap
, ISC_PFE_CFG2
,
615 (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK
) |
616 (ISC_PFE_CFG2_ROWMAX(h
- 1) & ISC_PFE_CFG2_ROWMAX_MASK
));
618 regmap_update_bits(regmap
, ISC_PFE_CFG0
,
619 ISC_PFE_CFG0_COLEN
| ISC_PFE_CFG0_ROWEN
,
620 ISC_PFE_CFG0_COLEN
| ISC_PFE_CFG0_ROWEN
);
622 addr0
= vb2_dma_contig_plane_dma_addr(&isc
->cur_frm
->vb
.vb2_buf
, 0);
623 regmap_write(regmap
, ISC_DAD0
, addr0
);
625 switch (isc
->config
.fourcc
) {
626 case V4L2_PIX_FMT_YUV420
:
627 regmap_write(regmap
, ISC_DAD1
, addr0
+ (sizeimage
* 2) / 3);
628 regmap_write(regmap
, ISC_DAD2
, addr0
+ (sizeimage
* 5) / 6);
630 case V4L2_PIX_FMT_YUV422P
:
631 regmap_write(regmap
, ISC_DAD1
, addr0
+ sizeimage
/ 2);
632 regmap_write(regmap
, ISC_DAD2
, addr0
+ (sizeimage
* 3) / 4);
638 dctrl_dview
= isc
->config
.dctrl_dview
;
640 regmap_write(regmap
, ISC_DCTRL
, dctrl_dview
| ISC_DCTRL_IE_IS
);
641 spin_lock(&isc
->awb_lock
);
642 regmap_write(regmap
, ISC_CTRLEN
, ISC_CTRL_CAPTURE
);
643 spin_unlock(&isc
->awb_lock
);
646 static void isc_set_pipeline(struct isc_device
*isc
, u32 pipeline
)
648 struct regmap
*regmap
= isc
->regmap
;
649 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
654 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
655 for (i
= 0; i
< ISC_PIPE_LINE_NODE_NUM
; i
++) {
656 val
= pipeline
& BIT(i
) ? 1 : 0;
657 regmap_field_write(isc
->pipeline
[i
], val
);
663 bay_cfg
= isc
->config
.sd_format
->cfa_baycfg
;
665 if (ctrls
->awb
== ISC_WB_NONE
)
666 isc_reset_awb_ctrls(isc
);
668 regmap_write(regmap
, ISC_WB_CFG
, bay_cfg
);
669 isc_update_awb_ctrls(isc
);
671 regmap_write(regmap
, ISC_CFA_CFG
, bay_cfg
| ISC_CFA_CFG_EITPOL
);
673 gamma
= &isc_gamma_table
[ctrls
->gamma_index
][0];
674 regmap_bulk_write(regmap
, ISC_GAM_BENTRY
, gamma
, GAMMA_ENTRIES
);
675 regmap_bulk_write(regmap
, ISC_GAM_GENTRY
, gamma
, GAMMA_ENTRIES
);
676 regmap_bulk_write(regmap
, ISC_GAM_RENTRY
, gamma
, GAMMA_ENTRIES
);
678 /* Convert RGB to YUV */
679 regmap_write(regmap
, ISC_CSC_YR_YG
, 0x42 | (0x81 << 16));
680 regmap_write(regmap
, ISC_CSC_YB_OY
, 0x19 | (0x10 << 16));
681 regmap_write(regmap
, ISC_CSC_CBR_CBG
, 0xFDA | (0xFB6 << 16));
682 regmap_write(regmap
, ISC_CSC_CBB_OCB
, 0x70 | (0x80 << 16));
683 regmap_write(regmap
, ISC_CSC_CRR_CRG
, 0x70 | (0xFA2 << 16));
684 regmap_write(regmap
, ISC_CSC_CRB_OCR
, 0xFEE | (0x80 << 16));
686 regmap_write(regmap
, ISC_CBC_BRIGHT
, ctrls
->brightness
);
687 regmap_write(regmap
, ISC_CBC_CONTRAST
, ctrls
->contrast
);
690 static int isc_update_profile(struct isc_device
*isc
)
692 struct regmap
*regmap
= isc
->regmap
;
696 regmap_write(regmap
, ISC_CTRLEN
, ISC_CTRL_UPPRO
);
698 regmap_read(regmap
, ISC_CTRLSR
, &sr
);
699 while ((sr
& ISC_CTRL_UPPRO
) && counter
--) {
700 usleep_range(1000, 2000);
701 regmap_read(regmap
, ISC_CTRLSR
, &sr
);
705 v4l2_warn(&isc
->v4l2_dev
, "Time out to update profile\n");
712 static void isc_set_histogram(struct isc_device
*isc
, bool enable
)
714 struct regmap
*regmap
= isc
->regmap
;
715 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
718 regmap_write(regmap
, ISC_HIS_CFG
,
719 ISC_HIS_CFG_MODE_GR
|
720 (isc
->config
.sd_format
->cfa_baycfg
721 << ISC_HIS_CFG_BAYSEL_SHIFT
) |
723 regmap_write(regmap
, ISC_HIS_CTRL
, ISC_HIS_CTRL_EN
);
724 regmap_write(regmap
, ISC_INTEN
, ISC_INT_HISDONE
);
725 ctrls
->hist_id
= ISC_HIS_CFG_MODE_GR
;
726 isc_update_profile(isc
);
727 regmap_write(regmap
, ISC_CTRLEN
, ISC_CTRL_HISREQ
);
729 ctrls
->hist_stat
= HIST_ENABLED
;
731 regmap_write(regmap
, ISC_INTDIS
, ISC_INT_HISDONE
);
732 regmap_write(regmap
, ISC_HIS_CTRL
, ISC_HIS_CTRL_DIS
);
734 ctrls
->hist_stat
= HIST_DISABLED
;
738 static int isc_configure(struct isc_device
*isc
)
740 struct regmap
*regmap
= isc
->regmap
;
741 u32 pfe_cfg0
, rlp_mode
, dcfg
, mask
, pipeline
;
742 struct isc_subdev_entity
*subdev
= isc
->current_subdev
;
744 pfe_cfg0
= isc
->config
.sd_format
->pfe_cfg0_bps
;
745 rlp_mode
= isc
->config
.rlp_cfg_mode
;
746 pipeline
= isc
->config
.bits_pipeline
;
748 dcfg
= isc
->config
.dcfg_imode
|
749 ISC_DCFG_YMBSIZE_BEATS8
| ISC_DCFG_CMBSIZE_BEATS8
;
751 pfe_cfg0
|= subdev
->pfe_cfg0
| ISC_PFE_CFG0_MODE_PROGRESSIVE
;
752 mask
= ISC_PFE_CFG0_BPS_MASK
| ISC_PFE_CFG0_HPOL_LOW
|
753 ISC_PFE_CFG0_VPOL_LOW
| ISC_PFE_CFG0_PPOL_LOW
|
754 ISC_PFE_CFG0_MODE_MASK
| ISC_PFE_CFG0_CCIR_CRC
|
755 ISC_PFE_CFG0_CCIR656
;
757 regmap_update_bits(regmap
, ISC_PFE_CFG0
, mask
, pfe_cfg0
);
759 regmap_update_bits(regmap
, ISC_RLP_CFG
, ISC_RLP_CFG_MODE_MASK
,
762 regmap_write(regmap
, ISC_DCFG
, dcfg
);
764 /* Set the pipeline */
765 isc_set_pipeline(isc
, pipeline
);
768 * The current implemented histogram is available for RAW R, B, GB, GR
769 * channels. We need to check if sensor is outputting RAW BAYER
771 if (isc
->ctrls
.awb
&&
772 ISC_IS_FORMAT_RAW(isc
->config
.sd_format
->mbus_code
))
773 isc_set_histogram(isc
, true);
775 isc_set_histogram(isc
, false);
778 return isc_update_profile(isc
);
781 static int isc_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
783 struct isc_device
*isc
= vb2_get_drv_priv(vq
);
784 struct regmap
*regmap
= isc
->regmap
;
785 struct isc_buffer
*buf
;
789 /* Enable stream on the sub device */
790 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, video
, s_stream
, 1);
791 if (ret
&& ret
!= -ENOIOCTLCMD
) {
792 v4l2_err(&isc
->v4l2_dev
, "stream on failed in subdev %d\n",
794 goto err_start_stream
;
797 pm_runtime_get_sync(isc
->dev
);
799 ret
= isc_configure(isc
);
803 /* Enable DMA interrupt */
804 regmap_write(regmap
, ISC_INTEN
, ISC_INT_DDONE
);
806 spin_lock_irqsave(&isc
->dma_queue_lock
, flags
);
810 reinit_completion(&isc
->comp
);
812 isc
->cur_frm
= list_first_entry(&isc
->dma_queue
,
813 struct isc_buffer
, list
);
814 list_del(&isc
->cur_frm
->list
);
818 spin_unlock_irqrestore(&isc
->dma_queue_lock
, flags
);
820 /* if we streaming from RAW, we can do one-shot white balance adj */
821 if (ISC_IS_FORMAT_RAW(isc
->config
.sd_format
->mbus_code
))
822 v4l2_ctrl_activate(isc
->do_wb_ctrl
, true);
827 pm_runtime_put_sync(isc
->dev
);
829 v4l2_subdev_call(isc
->current_subdev
->sd
, video
, s_stream
, 0);
832 spin_lock_irqsave(&isc
->dma_queue_lock
, flags
);
833 list_for_each_entry(buf
, &isc
->dma_queue
, list
)
834 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_QUEUED
);
835 INIT_LIST_HEAD(&isc
->dma_queue
);
836 spin_unlock_irqrestore(&isc
->dma_queue_lock
, flags
);
841 static void isc_stop_streaming(struct vb2_queue
*vq
)
843 struct isc_device
*isc
= vb2_get_drv_priv(vq
);
845 struct isc_buffer
*buf
;
848 v4l2_ctrl_activate(isc
->do_wb_ctrl
, false);
852 /* Wait until the end of the current frame */
853 if (isc
->cur_frm
&& !wait_for_completion_timeout(&isc
->comp
, 5 * HZ
))
854 v4l2_err(&isc
->v4l2_dev
,
855 "Timeout waiting for end of the capture\n");
857 /* Disable DMA interrupt */
858 regmap_write(isc
->regmap
, ISC_INTDIS
, ISC_INT_DDONE
);
860 pm_runtime_put_sync(isc
->dev
);
862 /* Disable stream on the sub device */
863 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, video
, s_stream
, 0);
864 if (ret
&& ret
!= -ENOIOCTLCMD
)
865 v4l2_err(&isc
->v4l2_dev
, "stream off failed in subdev\n");
867 /* Release all active buffers */
868 spin_lock_irqsave(&isc
->dma_queue_lock
, flags
);
869 if (unlikely(isc
->cur_frm
)) {
870 vb2_buffer_done(&isc
->cur_frm
->vb
.vb2_buf
,
871 VB2_BUF_STATE_ERROR
);
874 list_for_each_entry(buf
, &isc
->dma_queue
, list
)
875 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
876 INIT_LIST_HEAD(&isc
->dma_queue
);
877 spin_unlock_irqrestore(&isc
->dma_queue_lock
, flags
);
880 static void isc_buffer_queue(struct vb2_buffer
*vb
)
882 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
883 struct isc_buffer
*buf
= container_of(vbuf
, struct isc_buffer
, vb
);
884 struct isc_device
*isc
= vb2_get_drv_priv(vb
->vb2_queue
);
887 spin_lock_irqsave(&isc
->dma_queue_lock
, flags
);
888 if (!isc
->cur_frm
&& list_empty(&isc
->dma_queue
) &&
889 vb2_is_streaming(vb
->vb2_queue
)) {
893 list_add_tail(&buf
->list
, &isc
->dma_queue
);
894 spin_unlock_irqrestore(&isc
->dma_queue_lock
, flags
);
897 static struct isc_format
*find_format_by_fourcc(struct isc_device
*isc
,
900 unsigned int num_formats
= isc
->num_user_formats
;
901 struct isc_format
*fmt
;
904 for (i
= 0; i
< num_formats
; i
++) {
905 fmt
= isc
->user_formats
[i
];
906 if (fmt
->fourcc
== fourcc
)
913 static const struct vb2_ops isc_vb2_ops
= {
914 .queue_setup
= isc_queue_setup
,
915 .wait_prepare
= vb2_ops_wait_prepare
,
916 .wait_finish
= vb2_ops_wait_finish
,
917 .buf_prepare
= isc_buffer_prepare
,
918 .start_streaming
= isc_start_streaming
,
919 .stop_streaming
= isc_stop_streaming
,
920 .buf_queue
= isc_buffer_queue
,
923 static int isc_querycap(struct file
*file
, void *priv
,
924 struct v4l2_capability
*cap
)
926 struct isc_device
*isc
= video_drvdata(file
);
928 strscpy(cap
->driver
, ATMEL_ISC_NAME
, sizeof(cap
->driver
));
929 strscpy(cap
->card
, "Atmel Image Sensor Controller", sizeof(cap
->card
));
930 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
931 "platform:%s", isc
->v4l2_dev
.name
);
936 static int isc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
937 struct v4l2_fmtdesc
*f
)
939 u32 index
= f
->index
;
940 u32 i
, supported_index
;
942 if (index
< ARRAY_SIZE(controller_formats
)) {
943 f
->pixelformat
= controller_formats
[index
].fourcc
;
947 index
-= ARRAY_SIZE(controller_formats
);
952 for (i
= 0; i
< ARRAY_SIZE(formats_list
); i
++) {
953 if (!ISC_IS_FORMAT_RAW(formats_list
[i
].mbus_code
) ||
954 !formats_list
[i
].sd_support
)
956 if (supported_index
== index
) {
957 f
->pixelformat
= formats_list
[i
].fourcc
;
966 static int isc_g_fmt_vid_cap(struct file
*file
, void *priv
,
967 struct v4l2_format
*fmt
)
969 struct isc_device
*isc
= video_drvdata(file
);
977 * Checks the current configured format, if ISC can output it,
978 * considering which type of format the ISC receives from the sensor
980 static int isc_try_validate_formats(struct isc_device
*isc
)
983 bool bayer
= false, yuv
= false, rgb
= false, grey
= false;
985 /* all formats supported by the RLP module are OK */
986 switch (isc
->try_config
.fourcc
) {
987 case V4L2_PIX_FMT_SBGGR8
:
988 case V4L2_PIX_FMT_SGBRG8
:
989 case V4L2_PIX_FMT_SGRBG8
:
990 case V4L2_PIX_FMT_SRGGB8
:
991 case V4L2_PIX_FMT_SBGGR10
:
992 case V4L2_PIX_FMT_SGBRG10
:
993 case V4L2_PIX_FMT_SGRBG10
:
994 case V4L2_PIX_FMT_SRGGB10
:
995 case V4L2_PIX_FMT_SBGGR12
:
996 case V4L2_PIX_FMT_SGBRG12
:
997 case V4L2_PIX_FMT_SGRBG12
:
998 case V4L2_PIX_FMT_SRGGB12
:
1003 case V4L2_PIX_FMT_YUV420
:
1004 case V4L2_PIX_FMT_YUV422P
:
1005 case V4L2_PIX_FMT_YUYV
:
1010 case V4L2_PIX_FMT_RGB565
:
1011 case V4L2_PIX_FMT_ABGR32
:
1012 case V4L2_PIX_FMT_XBGR32
:
1013 case V4L2_PIX_FMT_ARGB444
:
1014 case V4L2_PIX_FMT_ARGB555
:
1018 case V4L2_PIX_FMT_GREY
:
1019 case V4L2_PIX_FMT_Y10
:
1024 /* any other different formats are not supported */
1027 v4l2_dbg(1, debug
, &isc
->v4l2_dev
,
1028 "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
1029 rgb
, yuv
, grey
, bayer
);
1031 /* we cannot output RAW if we do not receive RAW */
1032 if ((bayer
) && !ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
))
1035 /* we cannot output GREY if we do not receive RAW/GREY */
1036 if (grey
&& !ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
) &&
1037 !ISC_IS_FORMAT_GREY(isc
->try_config
.sd_format
->mbus_code
))
1044 * Configures the RLP and DMA modules, depending on the output format
1045 * configured for the ISC.
1046 * If direct_dump == true, just dump raw data 8/16 bits depending on format.
1048 static int isc_try_configure_rlp_dma(struct isc_device
*isc
, bool direct_dump
)
1050 switch (isc
->try_config
.fourcc
) {
1051 case V4L2_PIX_FMT_SBGGR8
:
1052 case V4L2_PIX_FMT_SGBRG8
:
1053 case V4L2_PIX_FMT_SGRBG8
:
1054 case V4L2_PIX_FMT_SRGGB8
:
1055 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_DAT8
;
1056 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED8
;
1057 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1058 isc
->try_config
.bpp
= 8;
1060 case V4L2_PIX_FMT_SBGGR10
:
1061 case V4L2_PIX_FMT_SGBRG10
:
1062 case V4L2_PIX_FMT_SGRBG10
:
1063 case V4L2_PIX_FMT_SRGGB10
:
1064 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_DAT10
;
1065 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED16
;
1066 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1067 isc
->try_config
.bpp
= 16;
1069 case V4L2_PIX_FMT_SBGGR12
:
1070 case V4L2_PIX_FMT_SGBRG12
:
1071 case V4L2_PIX_FMT_SGRBG12
:
1072 case V4L2_PIX_FMT_SRGGB12
:
1073 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_DAT12
;
1074 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED16
;
1075 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1076 isc
->try_config
.bpp
= 16;
1078 case V4L2_PIX_FMT_RGB565
:
1079 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_RGB565
;
1080 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED16
;
1081 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1082 isc
->try_config
.bpp
= 16;
1084 case V4L2_PIX_FMT_ARGB444
:
1085 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_ARGB444
;
1086 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED16
;
1087 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1088 isc
->try_config
.bpp
= 16;
1090 case V4L2_PIX_FMT_ARGB555
:
1091 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_ARGB555
;
1092 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED16
;
1093 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1094 isc
->try_config
.bpp
= 16;
1096 case V4L2_PIX_FMT_ABGR32
:
1097 case V4L2_PIX_FMT_XBGR32
:
1098 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_ARGB32
;
1099 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED32
;
1100 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1101 isc
->try_config
.bpp
= 32;
1103 case V4L2_PIX_FMT_YUV420
:
1104 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_YYCC
;
1105 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_YC420P
;
1106 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PLANAR
;
1107 isc
->try_config
.bpp
= 12;
1109 case V4L2_PIX_FMT_YUV422P
:
1110 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_YYCC
;
1111 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_YC422P
;
1112 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PLANAR
;
1113 isc
->try_config
.bpp
= 16;
1115 case V4L2_PIX_FMT_YUYV
:
1116 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_YYCC
;
1117 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED32
;
1118 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1119 isc
->try_config
.bpp
= 16;
1121 case V4L2_PIX_FMT_GREY
:
1122 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_DATY8
;
1123 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED8
;
1124 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1125 isc
->try_config
.bpp
= 8;
1127 case V4L2_PIX_FMT_Y10
:
1128 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_DATY10
;
1129 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED16
;
1130 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1131 isc
->try_config
.bpp
= 16;
1138 isc
->try_config
.rlp_cfg_mode
= ISC_RLP_CFG_MODE_DAT8
;
1139 isc
->try_config
.dcfg_imode
= ISC_DCFG_IMODE_PACKED8
;
1140 isc
->try_config
.dctrl_dview
= ISC_DCTRL_DVIEW_PACKED
;
1148 * Configuring pipeline modules, depending on which format the ISC outputs
1149 * and considering which format it has as input from the sensor.
1151 static int isc_try_configure_pipeline(struct isc_device
*isc
)
1153 switch (isc
->try_config
.fourcc
) {
1154 case V4L2_PIX_FMT_RGB565
:
1155 case V4L2_PIX_FMT_ARGB555
:
1156 case V4L2_PIX_FMT_ARGB444
:
1157 case V4L2_PIX_FMT_ABGR32
:
1158 case V4L2_PIX_FMT_XBGR32
:
1159 /* if sensor format is RAW, we convert inside ISC */
1160 if (ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
)) {
1161 isc
->try_config
.bits_pipeline
= CFA_ENABLE
|
1162 WB_ENABLE
| GAM_ENABLES
;
1164 isc
->try_config
.bits_pipeline
= 0x0;
1167 case V4L2_PIX_FMT_YUV420
:
1168 /* if sensor format is RAW, we convert inside ISC */
1169 if (ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
)) {
1170 isc
->try_config
.bits_pipeline
= CFA_ENABLE
|
1171 CSC_ENABLE
| WB_ENABLE
| GAM_ENABLES
|
1172 SUB420_ENABLE
| SUB422_ENABLE
| CBC_ENABLE
;
1174 isc
->try_config
.bits_pipeline
= 0x0;
1177 case V4L2_PIX_FMT_YUV422P
:
1178 /* if sensor format is RAW, we convert inside ISC */
1179 if (ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
)) {
1180 isc
->try_config
.bits_pipeline
= CFA_ENABLE
|
1181 CSC_ENABLE
| WB_ENABLE
| GAM_ENABLES
|
1182 SUB422_ENABLE
| CBC_ENABLE
;
1184 isc
->try_config
.bits_pipeline
= 0x0;
1187 case V4L2_PIX_FMT_YUYV
:
1188 /* if sensor format is RAW, we convert inside ISC */
1189 if (ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
)) {
1190 isc
->try_config
.bits_pipeline
= CFA_ENABLE
|
1191 CSC_ENABLE
| WB_ENABLE
| GAM_ENABLES
|
1192 SUB422_ENABLE
| CBC_ENABLE
;
1194 isc
->try_config
.bits_pipeline
= 0x0;
1197 case V4L2_PIX_FMT_GREY
:
1198 if (ISC_IS_FORMAT_RAW(isc
->try_config
.sd_format
->mbus_code
)) {
1199 /* if sensor format is RAW, we convert inside ISC */
1200 isc
->try_config
.bits_pipeline
= CFA_ENABLE
|
1201 CSC_ENABLE
| WB_ENABLE
| GAM_ENABLES
|
1204 isc
->try_config
.bits_pipeline
= 0x0;
1208 isc
->try_config
.bits_pipeline
= 0x0;
1213 static void isc_try_fse(struct isc_device
*isc
,
1214 struct v4l2_subdev_pad_config
*pad_cfg
)
1217 struct v4l2_subdev_frame_size_enum fse
= {};
1220 * If we do not know yet which format the subdev is using, we cannot
1223 if (!isc
->try_config
.sd_format
)
1226 fse
.code
= isc
->try_config
.sd_format
->mbus_code
;
1227 fse
.which
= V4L2_SUBDEV_FORMAT_TRY
;
1229 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, pad
, enum_frame_size
,
1232 * Attempt to obtain format size from subdev. If not available,
1233 * just use the maximum ISC can receive.
1236 pad_cfg
->try_crop
.width
= ISC_MAX_SUPPORT_WIDTH
;
1237 pad_cfg
->try_crop
.height
= ISC_MAX_SUPPORT_HEIGHT
;
1239 pad_cfg
->try_crop
.width
= fse
.max_width
;
1240 pad_cfg
->try_crop
.height
= fse
.max_height
;
1244 static int isc_try_fmt(struct isc_device
*isc
, struct v4l2_format
*f
,
1248 struct isc_format
*sd_fmt
= NULL
, *direct_fmt
= NULL
;
1249 struct v4l2_pix_format
*pixfmt
= &f
->fmt
.pix
;
1250 struct v4l2_subdev_pad_config pad_cfg
= {};
1251 struct v4l2_subdev_format format
= {
1252 .which
= V4L2_SUBDEV_FORMAT_TRY
,
1256 bool rlp_dma_direct_dump
= false;
1258 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1261 /* Step 1: find a RAW format that is supported */
1262 for (i
= 0; i
< isc
->num_user_formats
; i
++) {
1263 if (ISC_IS_FORMAT_RAW(isc
->user_formats
[i
]->mbus_code
)) {
1264 sd_fmt
= isc
->user_formats
[i
];
1268 /* Step 2: We can continue with this RAW format, or we can look
1269 * for better: maybe sensor supports directly what we need.
1271 direct_fmt
= find_format_by_fourcc(isc
, pixfmt
->pixelformat
);
1273 /* Step 3: We have both. We decide given the module parameter which
1276 if (direct_fmt
&& sd_fmt
&& sensor_preferred
)
1277 sd_fmt
= direct_fmt
;
1279 /* Step 4: we do not have RAW but we have a direct format. Use it. */
1280 if (direct_fmt
&& !sd_fmt
)
1281 sd_fmt
= direct_fmt
;
1283 /* Step 5: if we are using a direct format, we need to package
1284 * everything as 8 bit data and just dump it
1286 if (sd_fmt
== direct_fmt
)
1287 rlp_dma_direct_dump
= true;
1289 /* Step 6: We have no format. This can happen if the userspace
1290 * requests some weird/invalid format.
1291 * In this case, default to whatever we have
1293 if (!sd_fmt
&& !direct_fmt
) {
1294 sd_fmt
= isc
->user_formats
[isc
->num_user_formats
- 1];
1295 v4l2_dbg(1, debug
, &isc
->v4l2_dev
,
1296 "Sensor not supporting %.4s, using %.4s\n",
1297 (char *)&pixfmt
->pixelformat
, (char *)&sd_fmt
->fourcc
);
1302 goto isc_try_fmt_err
;
1305 /* Step 7: Print out what we decided for debugging */
1306 v4l2_dbg(1, debug
, &isc
->v4l2_dev
,
1307 "Preferring to have sensor using format %.4s\n",
1308 (char *)&sd_fmt
->fourcc
);
1310 /* Step 8: at this moment we decided which format the subdev will use */
1311 isc
->try_config
.sd_format
= sd_fmt
;
1313 /* Limit to Atmel ISC hardware capabilities */
1314 if (pixfmt
->width
> ISC_MAX_SUPPORT_WIDTH
)
1315 pixfmt
->width
= ISC_MAX_SUPPORT_WIDTH
;
1316 if (pixfmt
->height
> ISC_MAX_SUPPORT_HEIGHT
)
1317 pixfmt
->height
= ISC_MAX_SUPPORT_HEIGHT
;
1320 * The mbus format is the one the subdev outputs.
1321 * The pixels will be transferred in this format Sensor -> ISC
1323 mbus_code
= sd_fmt
->mbus_code
;
1326 * Validate formats. If the required format is not OK, default to raw.
1329 isc
->try_config
.fourcc
= pixfmt
->pixelformat
;
1331 if (isc_try_validate_formats(isc
)) {
1332 pixfmt
->pixelformat
= isc
->try_config
.fourcc
= sd_fmt
->fourcc
;
1333 /* Re-try to validate the new format */
1334 ret
= isc_try_validate_formats(isc
);
1336 goto isc_try_fmt_err
;
1339 ret
= isc_try_configure_rlp_dma(isc
, rlp_dma_direct_dump
);
1341 goto isc_try_fmt_err
;
1343 ret
= isc_try_configure_pipeline(isc
);
1345 goto isc_try_fmt_err
;
1347 /* Obtain frame sizes if possible to have crop requirements ready */
1348 isc_try_fse(isc
, &pad_cfg
);
1350 v4l2_fill_mbus_format(&format
.format
, pixfmt
, mbus_code
);
1351 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, pad
, set_fmt
,
1354 goto isc_try_fmt_subdev_err
;
1356 v4l2_fill_pix_format(pixfmt
, &format
.format
);
1358 pixfmt
->field
= V4L2_FIELD_NONE
;
1359 pixfmt
->bytesperline
= (pixfmt
->width
* isc
->try_config
.bpp
) >> 3;
1360 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
1368 v4l2_err(&isc
->v4l2_dev
, "Could not find any possible format for a working pipeline\n");
1369 isc_try_fmt_subdev_err
:
1370 memset(&isc
->try_config
, 0, sizeof(isc
->try_config
));
1375 static int isc_set_fmt(struct isc_device
*isc
, struct v4l2_format
*f
)
1377 struct v4l2_subdev_format format
= {
1378 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1383 ret
= isc_try_fmt(isc
, f
, &mbus_code
);
1387 v4l2_fill_mbus_format(&format
.format
, &f
->fmt
.pix
, mbus_code
);
1388 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, pad
,
1389 set_fmt
, NULL
, &format
);
1395 if (isc
->try_config
.sd_format
&& isc
->config
.sd_format
&&
1396 isc
->try_config
.sd_format
!= isc
->config
.sd_format
) {
1397 isc
->ctrls
.hist_stat
= HIST_INIT
;
1398 isc_reset_awb_ctrls(isc
);
1400 /* make the try configuration active */
1401 isc
->config
= isc
->try_config
;
1403 v4l2_dbg(1, debug
, &isc
->v4l2_dev
, "New ISC configuration in place\n");
1408 static int isc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1409 struct v4l2_format
*f
)
1411 struct isc_device
*isc
= video_drvdata(file
);
1413 if (vb2_is_streaming(&isc
->vb2_vidq
))
1416 return isc_set_fmt(isc
, f
);
1419 static int isc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1420 struct v4l2_format
*f
)
1422 struct isc_device
*isc
= video_drvdata(file
);
1424 return isc_try_fmt(isc
, f
, NULL
);
1427 static int isc_enum_input(struct file
*file
, void *priv
,
1428 struct v4l2_input
*inp
)
1430 if (inp
->index
!= 0)
1433 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1435 strscpy(inp
->name
, "Camera", sizeof(inp
->name
));
1440 static int isc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1447 static int isc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1455 static int isc_g_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
1457 struct isc_device
*isc
= video_drvdata(file
);
1459 return v4l2_g_parm_cap(video_devdata(file
), isc
->current_subdev
->sd
, a
);
1462 static int isc_s_parm(struct file
*file
, void *fh
, struct v4l2_streamparm
*a
)
1464 struct isc_device
*isc
= video_drvdata(file
);
1466 return v4l2_s_parm_cap(video_devdata(file
), isc
->current_subdev
->sd
, a
);
1469 static int isc_enum_framesizes(struct file
*file
, void *fh
,
1470 struct v4l2_frmsizeenum
*fsize
)
1472 struct isc_device
*isc
= video_drvdata(file
);
1473 struct v4l2_subdev_frame_size_enum fse
= {
1474 .code
= isc
->config
.sd_format
->mbus_code
,
1475 .index
= fsize
->index
,
1476 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1481 for (i
= 0; i
< isc
->num_user_formats
; i
++)
1482 if (isc
->user_formats
[i
]->fourcc
== fsize
->pixel_format
)
1485 for (i
= 0; i
< ARRAY_SIZE(controller_formats
); i
++)
1486 if (controller_formats
[i
].fourcc
== fsize
->pixel_format
)
1492 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, pad
, enum_frame_size
,
1497 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1498 fsize
->discrete
.width
= fse
.max_width
;
1499 fsize
->discrete
.height
= fse
.max_height
;
1504 static int isc_enum_frameintervals(struct file
*file
, void *fh
,
1505 struct v4l2_frmivalenum
*fival
)
1507 struct isc_device
*isc
= video_drvdata(file
);
1508 struct v4l2_subdev_frame_interval_enum fie
= {
1509 .code
= isc
->config
.sd_format
->mbus_code
,
1510 .index
= fival
->index
,
1511 .width
= fival
->width
,
1512 .height
= fival
->height
,
1513 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
1518 for (i
= 0; i
< isc
->num_user_formats
; i
++)
1519 if (isc
->user_formats
[i
]->fourcc
== fival
->pixel_format
)
1522 for (i
= 0; i
< ARRAY_SIZE(controller_formats
); i
++)
1523 if (controller_formats
[i
].fourcc
== fival
->pixel_format
)
1529 ret
= v4l2_subdev_call(isc
->current_subdev
->sd
, pad
,
1530 enum_frame_interval
, NULL
, &fie
);
1534 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1535 fival
->discrete
= fie
.interval
;
1540 static const struct v4l2_ioctl_ops isc_ioctl_ops
= {
1541 .vidioc_querycap
= isc_querycap
,
1542 .vidioc_enum_fmt_vid_cap
= isc_enum_fmt_vid_cap
,
1543 .vidioc_g_fmt_vid_cap
= isc_g_fmt_vid_cap
,
1544 .vidioc_s_fmt_vid_cap
= isc_s_fmt_vid_cap
,
1545 .vidioc_try_fmt_vid_cap
= isc_try_fmt_vid_cap
,
1547 .vidioc_enum_input
= isc_enum_input
,
1548 .vidioc_g_input
= isc_g_input
,
1549 .vidioc_s_input
= isc_s_input
,
1551 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1552 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1553 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1554 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1555 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1556 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1557 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1558 .vidioc_streamon
= vb2_ioctl_streamon
,
1559 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1561 .vidioc_g_parm
= isc_g_parm
,
1562 .vidioc_s_parm
= isc_s_parm
,
1563 .vidioc_enum_framesizes
= isc_enum_framesizes
,
1564 .vidioc_enum_frameintervals
= isc_enum_frameintervals
,
1566 .vidioc_log_status
= v4l2_ctrl_log_status
,
1567 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1568 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1571 static int isc_open(struct file
*file
)
1573 struct isc_device
*isc
= video_drvdata(file
);
1574 struct v4l2_subdev
*sd
= isc
->current_subdev
->sd
;
1577 if (mutex_lock_interruptible(&isc
->lock
))
1578 return -ERESTARTSYS
;
1580 ret
= v4l2_fh_open(file
);
1584 if (!v4l2_fh_is_singular_file(file
))
1587 ret
= v4l2_subdev_call(sd
, core
, s_power
, 1);
1588 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
1589 v4l2_fh_release(file
);
1593 ret
= isc_set_fmt(isc
, &isc
->fmt
);
1595 v4l2_subdev_call(sd
, core
, s_power
, 0);
1596 v4l2_fh_release(file
);
1600 mutex_unlock(&isc
->lock
);
1604 static int isc_release(struct file
*file
)
1606 struct isc_device
*isc
= video_drvdata(file
);
1607 struct v4l2_subdev
*sd
= isc
->current_subdev
->sd
;
1611 mutex_lock(&isc
->lock
);
1613 fh_singular
= v4l2_fh_is_singular_file(file
);
1615 ret
= _vb2_fop_release(file
, NULL
);
1618 v4l2_subdev_call(sd
, core
, s_power
, 0);
1620 mutex_unlock(&isc
->lock
);
1625 static const struct v4l2_file_operations isc_fops
= {
1626 .owner
= THIS_MODULE
,
1628 .release
= isc_release
,
1629 .unlocked_ioctl
= video_ioctl2
,
1630 .read
= vb2_fop_read
,
1631 .mmap
= vb2_fop_mmap
,
1632 .poll
= vb2_fop_poll
,
1635 irqreturn_t
isc_interrupt(int irq
, void *dev_id
)
1637 struct isc_device
*isc
= (struct isc_device
*)dev_id
;
1638 struct regmap
*regmap
= isc
->regmap
;
1639 u32 isc_intsr
, isc_intmask
, pending
;
1640 irqreturn_t ret
= IRQ_NONE
;
1642 regmap_read(regmap
, ISC_INTSR
, &isc_intsr
);
1643 regmap_read(regmap
, ISC_INTMASK
, &isc_intmask
);
1645 pending
= isc_intsr
& isc_intmask
;
1647 if (likely(pending
& ISC_INT_DDONE
)) {
1648 spin_lock(&isc
->dma_queue_lock
);
1650 struct vb2_v4l2_buffer
*vbuf
= &isc
->cur_frm
->vb
;
1651 struct vb2_buffer
*vb
= &vbuf
->vb2_buf
;
1653 vb
->timestamp
= ktime_get_ns();
1654 vbuf
->sequence
= isc
->sequence
++;
1655 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
1656 isc
->cur_frm
= NULL
;
1659 if (!list_empty(&isc
->dma_queue
) && !isc
->stop
) {
1660 isc
->cur_frm
= list_first_entry(&isc
->dma_queue
,
1661 struct isc_buffer
, list
);
1662 list_del(&isc
->cur_frm
->list
);
1668 complete(&isc
->comp
);
1671 spin_unlock(&isc
->dma_queue_lock
);
1674 if (pending
& ISC_INT_HISDONE
) {
1675 schedule_work(&isc
->awb_work
);
1682 static void isc_hist_count(struct isc_device
*isc
, u32
*min
, u32
*max
)
1684 struct regmap
*regmap
= isc
->regmap
;
1685 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
1686 u32
*hist_count
= &ctrls
->hist_count
[ctrls
->hist_id
];
1687 u32
*hist_entry
= &ctrls
->hist_entry
[0];
1691 *max
= HIST_ENTRIES
;
1693 regmap_bulk_read(regmap
, ISC_HIS_ENTRY
, hist_entry
, HIST_ENTRIES
);
1697 * we deliberately ignore the end of the histogram,
1698 * the most white pixels
1700 for (i
= 1; i
< HIST_ENTRIES
; i
++) {
1701 if (*hist_entry
&& !*min
)
1705 *hist_count
+= i
* (*hist_entry
++);
1712 static void isc_wb_update(struct isc_ctrls
*ctrls
)
1714 u32
*hist_count
= &ctrls
->hist_count
[0];
1717 /* We compute two gains, stretch gain and grey world gain */
1718 u32 s_gain
[4], gw_gain
[4];
1721 * According to Grey World, we need to set gains for R/B to normalize
1722 * them towards the green channel.
1723 * Thus we want to keep Green as fixed and adjust only Red/Blue
1724 * Compute the average of the both green channels first
1726 avg
= (u64
)hist_count
[ISC_HIS_CFG_MODE_GR
] +
1727 (u64
)hist_count
[ISC_HIS_CFG_MODE_GB
];
1730 /* Green histogram is null, nothing to do */
1734 for (c
= ISC_HIS_CFG_MODE_GR
; c
<= ISC_HIS_CFG_MODE_B
; c
++) {
1736 * the color offset is the minimum value of the histogram.
1737 * we stretch this color to the full range by substracting
1738 * this value from the color component.
1740 offset
[c
] = ctrls
->hist_minmax
[c
][HIST_MIN_INDEX
];
1742 * The offset is always at least 1. If the offset is 1, we do
1743 * not need to adjust it, so our result must be zero.
1744 * the offset is computed in a histogram on 9 bits (0..512)
1745 * but the offset in register is based on
1746 * 12 bits pipeline (0..4096).
1747 * we need to shift with the 3 bits that the histogram is
1750 ctrls
->offset
[c
] = (offset
[c
] - 1) << 3;
1752 /* the offset is then taken and converted to 2's complements */
1753 if (!ctrls
->offset
[c
])
1754 ctrls
->offset
[c
] = ISC_WB_O_ZERO_VAL
;
1757 * the stretch gain is the total number of histogram bins
1758 * divided by the actual range of color component (Max - Min)
1759 * If we compute gain like this, the actual color component
1760 * will be stretched to the full histogram.
1761 * We need to shift 9 bits for precision, we have 9 bits for
1764 s_gain
[c
] = (HIST_ENTRIES
<< 9) /
1765 (ctrls
->hist_minmax
[c
][HIST_MAX_INDEX
] -
1766 ctrls
->hist_minmax
[c
][HIST_MIN_INDEX
] + 1);
1769 * Now we have to compute the gain w.r.t. the average.
1770 * Add/lose gain to the component towards the average.
1771 * If it happens that the component is zero, use the
1772 * fixed point value : 1.0 gain.
1775 gw_gain
[c
] = div_u64(avg
<< 9, hist_count
[c
]);
1777 gw_gain
[c
] = 1 << 9;
1779 /* multiply both gains and adjust for decimals */
1780 ctrls
->gain
[c
] = s_gain
[c
] * gw_gain
[c
];
1781 ctrls
->gain
[c
] >>= 9;
1785 static void isc_awb_work(struct work_struct
*w
)
1787 struct isc_device
*isc
=
1788 container_of(w
, struct isc_device
, awb_work
);
1789 struct regmap
*regmap
= isc
->regmap
;
1790 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
1791 u32 hist_id
= ctrls
->hist_id
;
1793 unsigned long flags
;
1796 /* streaming is not active anymore */
1800 if (ctrls
->hist_stat
!= HIST_ENABLED
)
1803 isc_hist_count(isc
, &min
, &max
);
1804 ctrls
->hist_minmax
[hist_id
][HIST_MIN_INDEX
] = min
;
1805 ctrls
->hist_minmax
[hist_id
][HIST_MAX_INDEX
] = max
;
1807 if (hist_id
!= ISC_HIS_CFG_MODE_B
) {
1810 isc_wb_update(ctrls
);
1811 hist_id
= ISC_HIS_CFG_MODE_GR
;
1814 ctrls
->hist_id
= hist_id
;
1815 baysel
= isc
->config
.sd_format
->cfa_baycfg
<< ISC_HIS_CFG_BAYSEL_SHIFT
;
1817 /* if no more auto white balance, reset controls. */
1818 if (ctrls
->awb
== ISC_WB_NONE
)
1819 isc_reset_awb_ctrls(isc
);
1821 pm_runtime_get_sync(isc
->dev
);
1824 * only update if we have all the required histograms and controls
1825 * if awb has been disabled, we need to reset registers as well.
1827 if (hist_id
== ISC_HIS_CFG_MODE_GR
|| ctrls
->awb
== ISC_WB_NONE
) {
1829 * It may happen that DMA Done IRQ will trigger while we are
1830 * updating white balance registers here.
1831 * In that case, only parts of the controls have been updated.
1832 * We can avoid that by locking the section.
1834 spin_lock_irqsave(&isc
->awb_lock
, flags
);
1835 isc_update_awb_ctrls(isc
);
1836 spin_unlock_irqrestore(&isc
->awb_lock
, flags
);
1839 * if we are doing just the one time white balance adjustment,
1840 * we are basically done.
1842 if (ctrls
->awb
== ISC_WB_ONETIME
) {
1843 v4l2_info(&isc
->v4l2_dev
,
1844 "Completed one time white-balance adjustment.\n");
1845 ctrls
->awb
= ISC_WB_NONE
;
1848 regmap_write(regmap
, ISC_HIS_CFG
, hist_id
| baysel
| ISC_HIS_CFG_RAR
);
1849 isc_update_profile(isc
);
1850 /* if awb has been disabled, we don't need to start another histogram */
1852 regmap_write(regmap
, ISC_CTRLEN
, ISC_CTRL_HISREQ
);
1854 pm_runtime_put_sync(isc
->dev
);
1857 static int isc_s_ctrl(struct v4l2_ctrl
*ctrl
)
1859 struct isc_device
*isc
= container_of(ctrl
->handler
,
1860 struct isc_device
, ctrls
.handler
);
1861 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
1863 if (ctrl
->flags
& V4L2_CTRL_FLAG_INACTIVE
)
1867 case V4L2_CID_BRIGHTNESS
:
1868 ctrls
->brightness
= ctrl
->val
& ISC_CBC_BRIGHT_MASK
;
1870 case V4L2_CID_CONTRAST
:
1871 ctrls
->contrast
= ctrl
->val
& ISC_CBC_CONTRAST_MASK
;
1873 case V4L2_CID_GAMMA
:
1874 ctrls
->gamma_index
= ctrl
->val
;
1876 case V4L2_CID_AUTO_WHITE_BALANCE
:
1878 ctrls
->awb
= ISC_WB_AUTO
;
1880 ctrls
->awb
= ISC_WB_NONE
;
1882 /* we did not configure ISC yet */
1883 if (!isc
->config
.sd_format
)
1886 if (ctrls
->hist_stat
!= HIST_ENABLED
)
1887 isc_reset_awb_ctrls(isc
);
1889 if (isc
->ctrls
.awb
== ISC_WB_AUTO
&&
1890 vb2_is_streaming(&isc
->vb2_vidq
) &&
1891 ISC_IS_FORMAT_RAW(isc
->config
.sd_format
->mbus_code
))
1892 isc_set_histogram(isc
, true);
1895 case V4L2_CID_DO_WHITE_BALANCE
:
1896 /* if AWB is enabled, do nothing */
1897 if (ctrls
->awb
== ISC_WB_AUTO
)
1900 ctrls
->awb
= ISC_WB_ONETIME
;
1901 isc_set_histogram(isc
, true);
1902 v4l2_dbg(1, debug
, &isc
->v4l2_dev
,
1903 "One time white-balance started.\n");
1912 static const struct v4l2_ctrl_ops isc_ctrl_ops
= {
1913 .s_ctrl
= isc_s_ctrl
,
1916 static int isc_ctrl_init(struct isc_device
*isc
)
1918 const struct v4l2_ctrl_ops
*ops
= &isc_ctrl_ops
;
1919 struct isc_ctrls
*ctrls
= &isc
->ctrls
;
1920 struct v4l2_ctrl_handler
*hdl
= &ctrls
->handler
;
1923 ctrls
->hist_stat
= HIST_INIT
;
1924 isc_reset_awb_ctrls(isc
);
1926 ret
= v4l2_ctrl_handler_init(hdl
, 5);
1930 ctrls
->brightness
= 0;
1931 ctrls
->contrast
= 256;
1933 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_BRIGHTNESS
, -1024, 1023, 1, 0);
1934 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_CONTRAST
, -2048, 2047, 1, 256);
1935 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_GAMMA
, 0, GAMMA_MAX
, 1, 2);
1936 v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1938 /* do_white_balance is a button, so min,max,step,default are ignored */
1939 isc
->do_wb_ctrl
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_DO_WHITE_BALANCE
,
1942 if (!isc
->do_wb_ctrl
) {
1944 v4l2_ctrl_handler_free(hdl
);
1948 v4l2_ctrl_activate(isc
->do_wb_ctrl
, false);
1950 v4l2_ctrl_handler_setup(hdl
);
1955 static int isc_async_bound(struct v4l2_async_notifier
*notifier
,
1956 struct v4l2_subdev
*subdev
,
1957 struct v4l2_async_subdev
*asd
)
1959 struct isc_device
*isc
= container_of(notifier
->v4l2_dev
,
1960 struct isc_device
, v4l2_dev
);
1961 struct isc_subdev_entity
*subdev_entity
=
1962 container_of(notifier
, struct isc_subdev_entity
, notifier
);
1964 if (video_is_registered(&isc
->video_dev
)) {
1965 v4l2_err(&isc
->v4l2_dev
, "only supports one sub-device.\n");
1969 subdev_entity
->sd
= subdev
;
1974 static void isc_async_unbind(struct v4l2_async_notifier
*notifier
,
1975 struct v4l2_subdev
*subdev
,
1976 struct v4l2_async_subdev
*asd
)
1978 struct isc_device
*isc
= container_of(notifier
->v4l2_dev
,
1979 struct isc_device
, v4l2_dev
);
1980 cancel_work_sync(&isc
->awb_work
);
1981 video_unregister_device(&isc
->video_dev
);
1982 v4l2_ctrl_handler_free(&isc
->ctrls
.handler
);
1985 static struct isc_format
*find_format_by_code(unsigned int code
, int *index
)
1987 struct isc_format
*fmt
= &formats_list
[0];
1990 for (i
= 0; i
< ARRAY_SIZE(formats_list
); i
++) {
1991 if (fmt
->mbus_code
== code
) {
2002 static int isc_formats_init(struct isc_device
*isc
)
2004 struct isc_format
*fmt
;
2005 struct v4l2_subdev
*subdev
= isc
->current_subdev
->sd
;
2006 unsigned int num_fmts
, i
, j
;
2007 u32 list_size
= ARRAY_SIZE(formats_list
);
2008 struct v4l2_subdev_mbus_code_enum mbus_code
= {
2009 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
2013 while (!v4l2_subdev_call(subdev
, pad
, enum_mbus_code
,
2014 NULL
, &mbus_code
)) {
2017 fmt
= find_format_by_code(mbus_code
.code
, &i
);
2019 v4l2_warn(&isc
->v4l2_dev
, "Mbus code %x not supported\n",
2024 fmt
->sd_support
= true;
2031 isc
->num_user_formats
= num_fmts
;
2032 isc
->user_formats
= devm_kcalloc(isc
->dev
,
2033 num_fmts
, sizeof(*isc
->user_formats
),
2035 if (!isc
->user_formats
)
2038 fmt
= &formats_list
[0];
2039 for (i
= 0, j
= 0; i
< list_size
; i
++) {
2040 if (fmt
->sd_support
)
2041 isc
->user_formats
[j
++] = fmt
;
2048 static int isc_set_default_fmt(struct isc_device
*isc
)
2050 struct v4l2_format f
= {
2051 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2054 .height
= VGA_HEIGHT
,
2055 .field
= V4L2_FIELD_NONE
,
2056 .pixelformat
= isc
->user_formats
[0]->fourcc
,
2061 ret
= isc_try_fmt(isc
, &f
, NULL
);
2069 static int isc_async_complete(struct v4l2_async_notifier
*notifier
)
2071 struct isc_device
*isc
= container_of(notifier
->v4l2_dev
,
2072 struct isc_device
, v4l2_dev
);
2073 struct video_device
*vdev
= &isc
->video_dev
;
2074 struct vb2_queue
*q
= &isc
->vb2_vidq
;
2077 INIT_WORK(&isc
->awb_work
, isc_awb_work
);
2079 ret
= v4l2_device_register_subdev_nodes(&isc
->v4l2_dev
);
2081 v4l2_err(&isc
->v4l2_dev
, "Failed to register subdev nodes\n");
2085 isc
->current_subdev
= container_of(notifier
,
2086 struct isc_subdev_entity
, notifier
);
2087 mutex_init(&isc
->lock
);
2088 init_completion(&isc
->comp
);
2090 /* Initialize videobuf2 queue */
2091 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2092 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
| VB2_READ
;
2094 q
->buf_struct_size
= sizeof(struct isc_buffer
);
2095 q
->ops
= &isc_vb2_ops
;
2096 q
->mem_ops
= &vb2_dma_contig_memops
;
2097 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
2098 q
->lock
= &isc
->lock
;
2099 q
->min_buffers_needed
= 1;
2102 ret
= vb2_queue_init(q
);
2104 v4l2_err(&isc
->v4l2_dev
,
2105 "vb2_queue_init() failed: %d\n", ret
);
2106 goto isc_async_complete_err
;
2109 /* Init video dma queues */
2110 INIT_LIST_HEAD(&isc
->dma_queue
);
2111 spin_lock_init(&isc
->dma_queue_lock
);
2112 spin_lock_init(&isc
->awb_lock
);
2114 ret
= isc_formats_init(isc
);
2116 v4l2_err(&isc
->v4l2_dev
,
2117 "Init format failed: %d\n", ret
);
2118 goto isc_async_complete_err
;
2121 ret
= isc_set_default_fmt(isc
);
2123 v4l2_err(&isc
->v4l2_dev
, "Could not set default format\n");
2124 goto isc_async_complete_err
;
2127 ret
= isc_ctrl_init(isc
);
2129 v4l2_err(&isc
->v4l2_dev
, "Init isc ctrols failed: %d\n", ret
);
2130 goto isc_async_complete_err
;
2133 /* Register video device */
2134 strscpy(vdev
->name
, ATMEL_ISC_NAME
, sizeof(vdev
->name
));
2135 vdev
->release
= video_device_release_empty
;
2136 vdev
->fops
= &isc_fops
;
2137 vdev
->ioctl_ops
= &isc_ioctl_ops
;
2138 vdev
->v4l2_dev
= &isc
->v4l2_dev
;
2139 vdev
->vfl_dir
= VFL_DIR_RX
;
2141 vdev
->lock
= &isc
->lock
;
2142 vdev
->ctrl_handler
= &isc
->ctrls
.handler
;
2143 vdev
->device_caps
= V4L2_CAP_STREAMING
| V4L2_CAP_VIDEO_CAPTURE
;
2144 video_set_drvdata(vdev
, isc
);
2146 ret
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
2148 v4l2_err(&isc
->v4l2_dev
,
2149 "video_register_device failed: %d\n", ret
);
2150 goto isc_async_complete_err
;
2155 isc_async_complete_err
:
2156 mutex_destroy(&isc
->lock
);
2160 const struct v4l2_async_notifier_operations isc_async_ops
= {
2161 .bound
= isc_async_bound
,
2162 .unbind
= isc_async_unbind
,
2163 .complete
= isc_async_complete
,
2166 void isc_subdev_cleanup(struct isc_device
*isc
)
2168 struct isc_subdev_entity
*subdev_entity
;
2170 list_for_each_entry(subdev_entity
, &isc
->subdev_entities
, list
) {
2171 v4l2_async_notifier_unregister(&subdev_entity
->notifier
);
2172 v4l2_async_notifier_cleanup(&subdev_entity
->notifier
);
2175 INIT_LIST_HEAD(&isc
->subdev_entities
);
2178 int isc_pipeline_init(struct isc_device
*isc
)
2180 struct device
*dev
= isc
->dev
;
2181 struct regmap
*regmap
= isc
->regmap
;
2182 struct regmap_field
*regs
;
2185 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
2186 const struct reg_field regfields
[ISC_PIPE_LINE_NODE_NUM
] = {
2187 REG_FIELD(ISC_WB_CTRL
, 0, 0),
2188 REG_FIELD(ISC_CFA_CTRL
, 0, 0),
2189 REG_FIELD(ISC_CC_CTRL
, 0, 0),
2190 REG_FIELD(ISC_GAM_CTRL
, 0, 0),
2191 REG_FIELD(ISC_GAM_CTRL
, 1, 1),
2192 REG_FIELD(ISC_GAM_CTRL
, 2, 2),
2193 REG_FIELD(ISC_GAM_CTRL
, 3, 3),
2194 REG_FIELD(ISC_CSC_CTRL
, 0, 0),
2195 REG_FIELD(ISC_CBC_CTRL
, 0, 0),
2196 REG_FIELD(ISC_SUB422_CTRL
, 0, 0),
2197 REG_FIELD(ISC_SUB420_CTRL
, 0, 0),
2200 for (i
= 0; i
< ISC_PIPE_LINE_NODE_NUM
; i
++) {
2201 regs
= devm_regmap_field_alloc(dev
, regmap
, regfields
[i
]);
2203 return PTR_ERR(regs
);
2205 isc
->pipeline
[i
] = regs
;
2211 /* regmap configuration */
2212 #define ATMEL_ISC_REG_MAX 0xbfc
2213 const struct regmap_config isc_regmap_config
= {
2217 .max_register
= ATMEL_ISC_REG_MAX
,