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>
25 #include <linux/atmel-isc-media.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-image-sizes.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-fwnode.h>
33 #include <media/v4l2-subdev.h>
34 #include <media/videobuf2-dma-contig.h>
36 #include "atmel-isc-regs.h"
37 #include "atmel-isc.h"
39 static unsigned int debug;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "debug level (0-2)");
43 static unsigned int sensor_preferred = 1;
44 module_param(sensor_preferred, uint, 0644);
45 MODULE_PARM_DESC(sensor_preferred,
46 "Sensor is preferred to output the specified format (1-on 0-off), default 1");
48 /* This is a list of the formats that the ISC can *output* */
49 const struct isc_format controller_formats[] = {
51 .fourcc = V4L2_PIX_FMT_ARGB444,
54 .fourcc = V4L2_PIX_FMT_ARGB555,
57 .fourcc = V4L2_PIX_FMT_RGB565,
60 .fourcc = V4L2_PIX_FMT_ABGR32,
63 .fourcc = V4L2_PIX_FMT_XBGR32,
66 .fourcc = V4L2_PIX_FMT_YUV420,
69 .fourcc = V4L2_PIX_FMT_YUYV,
72 .fourcc = V4L2_PIX_FMT_YUV422P,
75 .fourcc = V4L2_PIX_FMT_GREY,
78 .fourcc = V4L2_PIX_FMT_Y10,
82 /* This is a list of formats that the ISC can receive as *input* */
83 struct isc_format formats_list[] = {
85 .fourcc = V4L2_PIX_FMT_SBGGR8,
86 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
87 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
88 .cfa_baycfg = ISC_BAY_CFG_BGBG,
91 .fourcc = V4L2_PIX_FMT_SGBRG8,
92 .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
93 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
94 .cfa_baycfg = ISC_BAY_CFG_GBGB,
97 .fourcc = V4L2_PIX_FMT_SGRBG8,
98 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
99 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
100 .cfa_baycfg = ISC_BAY_CFG_GRGR,
103 .fourcc = V4L2_PIX_FMT_SRGGB8,
104 .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
105 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
106 .cfa_baycfg = ISC_BAY_CFG_RGRG,
109 .fourcc = V4L2_PIX_FMT_SBGGR10,
110 .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
111 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
112 .cfa_baycfg = ISC_BAY_CFG_RGRG,
115 .fourcc = V4L2_PIX_FMT_SGBRG10,
116 .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
117 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
118 .cfa_baycfg = ISC_BAY_CFG_GBGB,
121 .fourcc = V4L2_PIX_FMT_SGRBG10,
122 .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
123 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
124 .cfa_baycfg = ISC_BAY_CFG_GRGR,
127 .fourcc = V4L2_PIX_FMT_SRGGB10,
128 .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
129 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
130 .cfa_baycfg = ISC_BAY_CFG_RGRG,
133 .fourcc = V4L2_PIX_FMT_SBGGR12,
134 .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
135 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
136 .cfa_baycfg = ISC_BAY_CFG_BGBG,
139 .fourcc = V4L2_PIX_FMT_SGBRG12,
140 .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
141 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
142 .cfa_baycfg = ISC_BAY_CFG_GBGB,
145 .fourcc = V4L2_PIX_FMT_SGRBG12,
146 .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
147 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
148 .cfa_baycfg = ISC_BAY_CFG_GRGR,
151 .fourcc = V4L2_PIX_FMT_SRGGB12,
152 .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
153 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
154 .cfa_baycfg = ISC_BAY_CFG_RGRG,
157 .fourcc = V4L2_PIX_FMT_GREY,
158 .mbus_code = MEDIA_BUS_FMT_Y8_1X8,
159 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
162 .fourcc = V4L2_PIX_FMT_YUYV,
163 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
164 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
167 .fourcc = V4L2_PIX_FMT_RGB565,
168 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
169 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
172 .fourcc = V4L2_PIX_FMT_Y10,
173 .mbus_code = MEDIA_BUS_FMT_Y10_1X10,
174 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
179 /* Gamma table with gamma 1/2.2 */
180 const u32 isc_gamma_table[GAMMA_MAX + 1][GAMMA_ENTRIES] = {
181 /* 0 --> gamma 1/1.8 */
182 { 0x65, 0x66002F, 0x950025, 0xBB0020, 0xDB001D, 0xF8001A,
183 0x1130018, 0x12B0017, 0x1420016, 0x1580014, 0x16D0013, 0x1810012,
184 0x1940012, 0x1A60012, 0x1B80011, 0x1C90010, 0x1DA0010, 0x1EA000F,
185 0x1FA000F, 0x209000F, 0x218000F, 0x227000E, 0x235000E, 0x243000E,
186 0x251000E, 0x25F000D, 0x26C000D, 0x279000D, 0x286000D, 0x293000C,
187 0x2A0000C, 0x2AC000C, 0x2B8000C, 0x2C4000C, 0x2D0000B, 0x2DC000B,
188 0x2E7000B, 0x2F3000B, 0x2FE000B, 0x309000B, 0x314000B, 0x31F000A,
189 0x32A000A, 0x334000B, 0x33F000A, 0x349000A, 0x354000A, 0x35E000A,
190 0x368000A, 0x372000A, 0x37C000A, 0x386000A, 0x3900009, 0x399000A,
191 0x3A30009, 0x3AD0009, 0x3B60009, 0x3BF000A, 0x3C90009, 0x3D20009,
192 0x3DB0009, 0x3E40009, 0x3ED0009, 0x3F60009 },
194 /* 1 --> gamma 1/2 */
195 { 0x7F, 0x800034, 0xB50028, 0xDE0021, 0x100001E, 0x11E001B,
196 0x1390019, 0x1520017, 0x16A0015, 0x1800014, 0x1940014, 0x1A80013,
197 0x1BB0012, 0x1CD0011, 0x1DF0010, 0x1EF0010, 0x200000F, 0x20F000F,
198 0x21F000E, 0x22D000F, 0x23C000E, 0x24A000E, 0x258000D, 0x265000D,
199 0x273000C, 0x27F000D, 0x28C000C, 0x299000C, 0x2A5000C, 0x2B1000B,
200 0x2BC000C, 0x2C8000B, 0x2D3000C, 0x2DF000B, 0x2EA000A, 0x2F5000A,
201 0x2FF000B, 0x30A000A, 0x314000B, 0x31F000A, 0x329000A, 0x333000A,
202 0x33D0009, 0x3470009, 0x350000A, 0x35A0009, 0x363000A, 0x36D0009,
203 0x3760009, 0x37F0009, 0x3880009, 0x3910009, 0x39A0009, 0x3A30009,
204 0x3AC0008, 0x3B40009, 0x3BD0008, 0x3C60008, 0x3CE0008, 0x3D60009,
205 0x3DF0008, 0x3E70008, 0x3EF0008, 0x3F70008 },
207 /* 2 --> gamma 1/2.2 */
208 { 0x99, 0x9B0038, 0xD4002A, 0xFF0023, 0x122001F, 0x141001B,
209 0x15D0019, 0x1760017, 0x18E0015, 0x1A30015, 0x1B80013, 0x1CC0012,
210 0x1DE0011, 0x1F00010, 0x2010010, 0x2110010, 0x221000F, 0x230000F,
211 0x23F000E, 0x24D000E, 0x25B000D, 0x269000C, 0x276000C, 0x283000C,
212 0x28F000C, 0x29B000C, 0x2A7000C, 0x2B3000B, 0x2BF000B, 0x2CA000B,
213 0x2D5000B, 0x2E0000A, 0x2EB000A, 0x2F5000A, 0x2FF000A, 0x30A000A,
214 0x3140009, 0x31E0009, 0x327000A, 0x3310009, 0x33A0009, 0x3440009,
215 0x34D0009, 0x3560009, 0x35F0009, 0x3680008, 0x3710008, 0x3790009,
216 0x3820008, 0x38A0008, 0x3930008, 0x39B0008, 0x3A30008, 0x3AB0008,
217 0x3B30008, 0x3BB0008, 0x3C30008, 0x3CB0007, 0x3D20008, 0x3DA0007,
218 0x3E20007, 0x3E90007, 0x3F00008, 0x3F80007 },
221 #define ISC_IS_FORMAT_RAW(mbus_code) \
222 (((mbus_code) & 0xf000) == 0x3000)
224 #define ISC_IS_FORMAT_GREY(mbus_code) \
225 (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
226 (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
228 static inline void isc_update_v4l2_ctrls(struct isc_device *isc)
230 struct isc_ctrls *ctrls = &isc->ctrls;
232 /* In here we set the v4l2 controls w.r.t. our pipeline config */
233 v4l2_ctrl_s_ctrl(isc->r_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_R]);
234 v4l2_ctrl_s_ctrl(isc->b_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_B]);
235 v4l2_ctrl_s_ctrl(isc->gr_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GR]);
236 v4l2_ctrl_s_ctrl(isc->gb_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GB]);
238 v4l2_ctrl_s_ctrl(isc->r_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_R]);
239 v4l2_ctrl_s_ctrl(isc->b_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_B]);
240 v4l2_ctrl_s_ctrl(isc->gr_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GR]);
241 v4l2_ctrl_s_ctrl(isc->gb_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GB]);
244 static inline void isc_update_awb_ctrls(struct isc_device *isc)
246 struct isc_ctrls *ctrls = &isc->ctrls;
248 /* In here we set our actual hw pipeline config */
250 regmap_write(isc->regmap, ISC_WB_O_RGR,
251 ((ctrls->offset[ISC_HIS_CFG_MODE_R])) |
252 ((ctrls->offset[ISC_HIS_CFG_MODE_GR]) << 16));
253 regmap_write(isc->regmap, ISC_WB_O_BGB,
254 ((ctrls->offset[ISC_HIS_CFG_MODE_B])) |
255 ((ctrls->offset[ISC_HIS_CFG_MODE_GB]) << 16));
256 regmap_write(isc->regmap, ISC_WB_G_RGR,
257 ctrls->gain[ISC_HIS_CFG_MODE_R] |
258 (ctrls->gain[ISC_HIS_CFG_MODE_GR] << 16));
259 regmap_write(isc->regmap, ISC_WB_G_BGB,
260 ctrls->gain[ISC_HIS_CFG_MODE_B] |
261 (ctrls->gain[ISC_HIS_CFG_MODE_GB] << 16));
264 static inline void isc_reset_awb_ctrls(struct isc_device *isc)
268 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
269 /* gains have a fixed point at 9 decimals */
270 isc->ctrls.gain[c] = 1 << 9;
271 /* offsets are in 2's complements */
272 isc->ctrls.offset[c] = 0;
276 static int isc_wait_clk_stable(struct clk_hw *hw)
278 struct isc_clk *isc_clk = to_isc_clk(hw);
279 struct regmap *regmap = isc_clk->regmap;
280 unsigned long timeout = jiffies + usecs_to_jiffies(1000);
283 while (time_before(jiffies, timeout)) {
284 regmap_read(regmap, ISC_CLKSR, &status);
285 if (!(status & ISC_CLKSR_SIP))
288 usleep_range(10, 250);
294 static int isc_clk_prepare(struct clk_hw *hw)
296 struct isc_clk *isc_clk = to_isc_clk(hw);
299 if (isc_clk->id == ISC_ISPCK) {
300 ret = pm_runtime_resume_and_get(isc_clk->dev);
305 return isc_wait_clk_stable(hw);
308 static void isc_clk_unprepare(struct clk_hw *hw)
310 struct isc_clk *isc_clk = to_isc_clk(hw);
312 isc_wait_clk_stable(hw);
314 if (isc_clk->id == ISC_ISPCK)
315 pm_runtime_put_sync(isc_clk->dev);
318 static int isc_clk_enable(struct clk_hw *hw)
320 struct isc_clk *isc_clk = to_isc_clk(hw);
321 u32 id = isc_clk->id;
322 struct regmap *regmap = isc_clk->regmap;
326 dev_dbg(isc_clk->dev, "ISC CLK: %s, div = %d, parent id = %d\n",
327 __func__, isc_clk->div, isc_clk->parent_id);
329 spin_lock_irqsave(&isc_clk->lock, flags);
330 regmap_update_bits(regmap, ISC_CLKCFG,
331 ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
332 (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
333 (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
335 regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
336 spin_unlock_irqrestore(&isc_clk->lock, flags);
338 regmap_read(regmap, ISC_CLKSR, &status);
339 if (status & ISC_CLK(id))
345 static void isc_clk_disable(struct clk_hw *hw)
347 struct isc_clk *isc_clk = to_isc_clk(hw);
348 u32 id = isc_clk->id;
351 spin_lock_irqsave(&isc_clk->lock, flags);
352 regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
353 spin_unlock_irqrestore(&isc_clk->lock, flags);
356 static int isc_clk_is_enabled(struct clk_hw *hw)
358 struct isc_clk *isc_clk = to_isc_clk(hw);
362 if (isc_clk->id == ISC_ISPCK) {
363 ret = pm_runtime_resume_and_get(isc_clk->dev);
368 regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
370 if (isc_clk->id == ISC_ISPCK)
371 pm_runtime_put_sync(isc_clk->dev);
373 return status & ISC_CLK(isc_clk->id) ? 1 : 0;
377 isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
379 struct isc_clk *isc_clk = to_isc_clk(hw);
381 return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
384 static int isc_clk_determine_rate(struct clk_hw *hw,
385 struct clk_rate_request *req)
387 struct isc_clk *isc_clk = to_isc_clk(hw);
388 long best_rate = -EINVAL;
392 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
393 struct clk_hw *parent;
394 unsigned long parent_rate;
396 parent = clk_hw_get_parent_by_index(hw, i);
400 parent_rate = clk_hw_get_rate(parent);
404 for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
408 rate = DIV_ROUND_CLOSEST(parent_rate, div);
409 diff = abs(req->rate - rate);
411 if (best_diff < 0 || best_diff > diff) {
414 req->best_parent_rate = parent_rate;
415 req->best_parent_hw = parent;
418 if (!best_diff || rate < req->rate)
426 dev_dbg(isc_clk->dev,
427 "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
429 __clk_get_name((req->best_parent_hw)->clk),
430 req->best_parent_rate);
435 req->rate = best_rate;
440 static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
442 struct isc_clk *isc_clk = to_isc_clk(hw);
444 if (index >= clk_hw_get_num_parents(hw))
447 isc_clk->parent_id = index;
452 static u8 isc_clk_get_parent(struct clk_hw *hw)
454 struct isc_clk *isc_clk = to_isc_clk(hw);
456 return isc_clk->parent_id;
459 static int isc_clk_set_rate(struct clk_hw *hw,
461 unsigned long parent_rate)
463 struct isc_clk *isc_clk = to_isc_clk(hw);
469 div = DIV_ROUND_CLOSEST(parent_rate, rate);
470 if (div > (ISC_CLK_MAX_DIV + 1) || !div)
473 isc_clk->div = div - 1;
478 static const struct clk_ops isc_clk_ops = {
479 .prepare = isc_clk_prepare,
480 .unprepare = isc_clk_unprepare,
481 .enable = isc_clk_enable,
482 .disable = isc_clk_disable,
483 .is_enabled = isc_clk_is_enabled,
484 .recalc_rate = isc_clk_recalc_rate,
485 .determine_rate = isc_clk_determine_rate,
486 .set_parent = isc_clk_set_parent,
487 .get_parent = isc_clk_get_parent,
488 .set_rate = isc_clk_set_rate,
491 static int isc_clk_register(struct isc_device *isc, unsigned int id)
493 struct regmap *regmap = isc->regmap;
494 struct device_node *np = isc->dev->of_node;
495 struct isc_clk *isc_clk;
496 struct clk_init_data init;
497 const char *clk_name = np->name;
498 const char *parent_names[3];
501 num_parents = of_clk_get_parent_count(np);
502 if (num_parents < 1 || num_parents > 3)
505 if (num_parents > 2 && id == ISC_ISPCK)
508 of_clk_parent_fill(np, parent_names, num_parents);
511 of_property_read_string(np, "clock-output-names", &clk_name);
513 clk_name = "isc-ispck";
515 init.parent_names = parent_names;
516 init.num_parents = num_parents;
517 init.name = clk_name;
518 init.ops = &isc_clk_ops;
519 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
521 isc_clk = &isc->isc_clks[id];
522 isc_clk->hw.init = &init;
523 isc_clk->regmap = regmap;
525 isc_clk->dev = isc->dev;
526 spin_lock_init(&isc_clk->lock);
528 isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
529 if (IS_ERR(isc_clk->clk)) {
530 dev_err(isc->dev, "%s: clock register fail\n", clk_name);
531 return PTR_ERR(isc_clk->clk);
532 } else if (id == ISC_MCK)
533 of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
538 int isc_clk_init(struct isc_device *isc)
543 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
544 isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
546 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
547 ret = isc_clk_register(isc, i);
555 void isc_clk_cleanup(struct isc_device *isc)
559 of_clk_del_provider(isc->dev->of_node);
561 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
562 struct isc_clk *isc_clk = &isc->isc_clks[i];
564 if (!IS_ERR(isc_clk->clk))
565 clk_unregister(isc_clk->clk);
569 static int isc_queue_setup(struct vb2_queue *vq,
570 unsigned int *nbuffers, unsigned int *nplanes,
571 unsigned int sizes[], struct device *alloc_devs[])
573 struct isc_device *isc = vb2_get_drv_priv(vq);
574 unsigned int size = isc->fmt.fmt.pix.sizeimage;
577 return sizes[0] < size ? -EINVAL : 0;
585 static int isc_buffer_prepare(struct vb2_buffer *vb)
587 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
588 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
589 unsigned long size = isc->fmt.fmt.pix.sizeimage;
591 if (vb2_plane_size(vb, 0) < size) {
592 v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
593 vb2_plane_size(vb, 0), size);
597 vb2_set_plane_payload(vb, 0, size);
599 vbuf->field = isc->fmt.fmt.pix.field;
604 static void isc_start_dma(struct isc_device *isc)
606 struct regmap *regmap = isc->regmap;
607 u32 sizeimage = isc->fmt.fmt.pix.sizeimage;
612 h = isc->fmt.fmt.pix.height;
613 w = isc->fmt.fmt.pix.width;
616 * In case the sensor is not RAW, it will output a pixel (12-16 bits)
617 * with two samples on the ISC Data bus (which is 8-12)
618 * ISC will count each sample, so, we need to multiply these values
619 * by two, to get the real number of samples for the required pixels.
621 if (!ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code)) {
627 * We limit the column/row count that the ISC will output according
628 * to the configured resolution that we want.
629 * This will avoid the situation where the sensor is misconfigured,
630 * sending more data, and the ISC will just take it and DMA to memory,
631 * causing corruption.
633 regmap_write(regmap, ISC_PFE_CFG1,
634 (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK) |
635 (ISC_PFE_CFG1_COLMAX(w - 1) & ISC_PFE_CFG1_COLMAX_MASK));
637 regmap_write(regmap, ISC_PFE_CFG2,
638 (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK) |
639 (ISC_PFE_CFG2_ROWMAX(h - 1) & ISC_PFE_CFG2_ROWMAX_MASK));
641 regmap_update_bits(regmap, ISC_PFE_CFG0,
642 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN,
643 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN);
645 addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
646 regmap_write(regmap, ISC_DAD0, addr0);
648 switch (isc->config.fourcc) {
649 case V4L2_PIX_FMT_YUV420:
650 regmap_write(regmap, ISC_DAD1, addr0 + (sizeimage * 2) / 3);
651 regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 5) / 6);
653 case V4L2_PIX_FMT_YUV422P:
654 regmap_write(regmap, ISC_DAD1, addr0 + sizeimage / 2);
655 regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 3) / 4);
661 dctrl_dview = isc->config.dctrl_dview;
663 regmap_write(regmap, ISC_DCTRL, dctrl_dview | ISC_DCTRL_IE_IS);
664 spin_lock(&isc->awb_lock);
665 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
666 spin_unlock(&isc->awb_lock);
669 static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
671 struct regmap *regmap = isc->regmap;
672 struct isc_ctrls *ctrls = &isc->ctrls;
677 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
678 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
679 val = pipeline & BIT(i) ? 1 : 0;
680 regmap_field_write(isc->pipeline[i], val);
686 bay_cfg = isc->config.sd_format->cfa_baycfg;
688 regmap_write(regmap, ISC_WB_CFG, bay_cfg);
689 isc_update_awb_ctrls(isc);
690 isc_update_v4l2_ctrls(isc);
692 regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
694 gamma = &isc_gamma_table[ctrls->gamma_index][0];
695 regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
696 regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
697 regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
699 /* Convert RGB to YUV */
700 regmap_write(regmap, ISC_CSC_YR_YG, 0x42 | (0x81 << 16));
701 regmap_write(regmap, ISC_CSC_YB_OY, 0x19 | (0x10 << 16));
702 regmap_write(regmap, ISC_CSC_CBR_CBG, 0xFDA | (0xFB6 << 16));
703 regmap_write(regmap, ISC_CSC_CBB_OCB, 0x70 | (0x80 << 16));
704 regmap_write(regmap, ISC_CSC_CRR_CRG, 0x70 | (0xFA2 << 16));
705 regmap_write(regmap, ISC_CSC_CRB_OCR, 0xFEE | (0x80 << 16));
707 regmap_write(regmap, ISC_CBC_BRIGHT, ctrls->brightness);
708 regmap_write(regmap, ISC_CBC_CONTRAST, ctrls->contrast);
711 static int isc_update_profile(struct isc_device *isc)
713 struct regmap *regmap = isc->regmap;
717 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
719 regmap_read(regmap, ISC_CTRLSR, &sr);
720 while ((sr & ISC_CTRL_UPPRO) && counter--) {
721 usleep_range(1000, 2000);
722 regmap_read(regmap, ISC_CTRLSR, &sr);
726 v4l2_warn(&isc->v4l2_dev, "Time out to update profile\n");
733 static void isc_set_histogram(struct isc_device *isc, bool enable)
735 struct regmap *regmap = isc->regmap;
736 struct isc_ctrls *ctrls = &isc->ctrls;
739 regmap_write(regmap, ISC_HIS_CFG,
740 ISC_HIS_CFG_MODE_GR |
741 (isc->config.sd_format->cfa_baycfg
742 << ISC_HIS_CFG_BAYSEL_SHIFT) |
744 regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_EN);
745 regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
746 ctrls->hist_id = ISC_HIS_CFG_MODE_GR;
747 isc_update_profile(isc);
748 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
750 ctrls->hist_stat = HIST_ENABLED;
752 regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
753 regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_DIS);
755 ctrls->hist_stat = HIST_DISABLED;
759 static int isc_configure(struct isc_device *isc)
761 struct regmap *regmap = isc->regmap;
762 u32 pfe_cfg0, rlp_mode, dcfg, mask, pipeline;
763 struct isc_subdev_entity *subdev = isc->current_subdev;
765 pfe_cfg0 = isc->config.sd_format->pfe_cfg0_bps;
766 rlp_mode = isc->config.rlp_cfg_mode;
767 pipeline = isc->config.bits_pipeline;
769 dcfg = isc->config.dcfg_imode |
770 ISC_DCFG_YMBSIZE_BEATS8 | ISC_DCFG_CMBSIZE_BEATS8;
772 pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
773 mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
774 ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
775 ISC_PFE_CFG0_MODE_MASK | ISC_PFE_CFG0_CCIR_CRC |
776 ISC_PFE_CFG0_CCIR656;
778 regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
780 regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
783 regmap_write(regmap, ISC_DCFG, dcfg);
785 /* Set the pipeline */
786 isc_set_pipeline(isc, pipeline);
789 * The current implemented histogram is available for RAW R, B, GB, GR
790 * channels. We need to check if sensor is outputting RAW BAYER
792 if (isc->ctrls.awb &&
793 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
794 isc_set_histogram(isc, true);
796 isc_set_histogram(isc, false);
799 return isc_update_profile(isc);
802 static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
804 struct isc_device *isc = vb2_get_drv_priv(vq);
805 struct regmap *regmap = isc->regmap;
806 struct isc_buffer *buf;
810 /* Enable stream on the sub device */
811 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
812 if (ret && ret != -ENOIOCTLCMD) {
813 v4l2_err(&isc->v4l2_dev, "stream on failed in subdev %d\n",
815 goto err_start_stream;
818 ret = pm_runtime_resume_and_get(isc->dev);
820 v4l2_err(&isc->v4l2_dev, "RPM resume failed in subdev %d\n",
825 ret = isc_configure(isc);
829 /* Enable DMA interrupt */
830 regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
832 spin_lock_irqsave(&isc->dma_queue_lock, flags);
836 reinit_completion(&isc->comp);
838 isc->cur_frm = list_first_entry(&isc->dma_queue,
839 struct isc_buffer, list);
840 list_del(&isc->cur_frm->list);
844 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
846 /* if we streaming from RAW, we can do one-shot white balance adj */
847 if (ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
848 v4l2_ctrl_activate(isc->do_wb_ctrl, true);
853 pm_runtime_put_sync(isc->dev);
855 v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
858 spin_lock_irqsave(&isc->dma_queue_lock, flags);
859 list_for_each_entry(buf, &isc->dma_queue, list)
860 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
861 INIT_LIST_HEAD(&isc->dma_queue);
862 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
867 static void isc_stop_streaming(struct vb2_queue *vq)
869 struct isc_device *isc = vb2_get_drv_priv(vq);
871 struct isc_buffer *buf;
874 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
878 /* Wait until the end of the current frame */
879 if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
880 v4l2_err(&isc->v4l2_dev,
881 "Timeout waiting for end of the capture\n");
883 /* Disable DMA interrupt */
884 regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
886 pm_runtime_put_sync(isc->dev);
888 /* Disable stream on the sub device */
889 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
890 if (ret && ret != -ENOIOCTLCMD)
891 v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
893 /* Release all active buffers */
894 spin_lock_irqsave(&isc->dma_queue_lock, flags);
895 if (unlikely(isc->cur_frm)) {
896 vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
897 VB2_BUF_STATE_ERROR);
900 list_for_each_entry(buf, &isc->dma_queue, list)
901 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
902 INIT_LIST_HEAD(&isc->dma_queue);
903 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
906 static void isc_buffer_queue(struct vb2_buffer *vb)
908 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
909 struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
910 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
913 spin_lock_irqsave(&isc->dma_queue_lock, flags);
914 if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
915 vb2_is_streaming(vb->vb2_queue)) {
919 list_add_tail(&buf->list, &isc->dma_queue);
920 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
923 static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
926 unsigned int num_formats = isc->num_user_formats;
927 struct isc_format *fmt;
930 for (i = 0; i < num_formats; i++) {
931 fmt = isc->user_formats[i];
932 if (fmt->fourcc == fourcc)
939 static const struct vb2_ops isc_vb2_ops = {
940 .queue_setup = isc_queue_setup,
941 .wait_prepare = vb2_ops_wait_prepare,
942 .wait_finish = vb2_ops_wait_finish,
943 .buf_prepare = isc_buffer_prepare,
944 .start_streaming = isc_start_streaming,
945 .stop_streaming = isc_stop_streaming,
946 .buf_queue = isc_buffer_queue,
949 static int isc_querycap(struct file *file, void *priv,
950 struct v4l2_capability *cap)
952 struct isc_device *isc = video_drvdata(file);
954 strscpy(cap->driver, ATMEL_ISC_NAME, sizeof(cap->driver));
955 strscpy(cap->card, "Atmel Image Sensor Controller", sizeof(cap->card));
956 snprintf(cap->bus_info, sizeof(cap->bus_info),
957 "platform:%s", isc->v4l2_dev.name);
962 static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
963 struct v4l2_fmtdesc *f)
965 u32 index = f->index;
966 u32 i, supported_index;
968 if (index < ARRAY_SIZE(controller_formats)) {
969 f->pixelformat = controller_formats[index].fourcc;
973 index -= ARRAY_SIZE(controller_formats);
978 for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
979 if (!ISC_IS_FORMAT_RAW(formats_list[i].mbus_code) ||
980 !formats_list[i].sd_support)
982 if (supported_index == index) {
983 f->pixelformat = formats_list[i].fourcc;
992 static int isc_g_fmt_vid_cap(struct file *file, void *priv,
993 struct v4l2_format *fmt)
995 struct isc_device *isc = video_drvdata(file);
1003 * Checks the current configured format, if ISC can output it,
1004 * considering which type of format the ISC receives from the sensor
1006 static int isc_try_validate_formats(struct isc_device *isc)
1009 bool bayer = false, yuv = false, rgb = false, grey = false;
1011 /* all formats supported by the RLP module are OK */
1012 switch (isc->try_config.fourcc) {
1013 case V4L2_PIX_FMT_SBGGR8:
1014 case V4L2_PIX_FMT_SGBRG8:
1015 case V4L2_PIX_FMT_SGRBG8:
1016 case V4L2_PIX_FMT_SRGGB8:
1017 case V4L2_PIX_FMT_SBGGR10:
1018 case V4L2_PIX_FMT_SGBRG10:
1019 case V4L2_PIX_FMT_SGRBG10:
1020 case V4L2_PIX_FMT_SRGGB10:
1021 case V4L2_PIX_FMT_SBGGR12:
1022 case V4L2_PIX_FMT_SGBRG12:
1023 case V4L2_PIX_FMT_SGRBG12:
1024 case V4L2_PIX_FMT_SRGGB12:
1029 case V4L2_PIX_FMT_YUV420:
1030 case V4L2_PIX_FMT_YUV422P:
1031 case V4L2_PIX_FMT_YUYV:
1036 case V4L2_PIX_FMT_RGB565:
1037 case V4L2_PIX_FMT_ABGR32:
1038 case V4L2_PIX_FMT_XBGR32:
1039 case V4L2_PIX_FMT_ARGB444:
1040 case V4L2_PIX_FMT_ARGB555:
1044 case V4L2_PIX_FMT_GREY:
1045 case V4L2_PIX_FMT_Y10:
1050 /* any other different formats are not supported */
1053 v4l2_dbg(1, debug, &isc->v4l2_dev,
1054 "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
1055 rgb, yuv, grey, bayer);
1057 /* we cannot output RAW if we do not receive RAW */
1058 if ((bayer) && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
1061 /* we cannot output GREY if we do not receive RAW/GREY */
1062 if (grey && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code) &&
1063 !ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code))
1070 * Configures the RLP and DMA modules, depending on the output format
1071 * configured for the ISC.
1072 * If direct_dump == true, just dump raw data 8/16 bits depending on format.
1074 static int isc_try_configure_rlp_dma(struct isc_device *isc, bool direct_dump)
1076 switch (isc->try_config.fourcc) {
1077 case V4L2_PIX_FMT_SBGGR8:
1078 case V4L2_PIX_FMT_SGBRG8:
1079 case V4L2_PIX_FMT_SGRBG8:
1080 case V4L2_PIX_FMT_SRGGB8:
1081 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1082 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1083 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1084 isc->try_config.bpp = 8;
1086 case V4L2_PIX_FMT_SBGGR10:
1087 case V4L2_PIX_FMT_SGBRG10:
1088 case V4L2_PIX_FMT_SGRBG10:
1089 case V4L2_PIX_FMT_SRGGB10:
1090 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10;
1091 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1092 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1093 isc->try_config.bpp = 16;
1095 case V4L2_PIX_FMT_SBGGR12:
1096 case V4L2_PIX_FMT_SGBRG12:
1097 case V4L2_PIX_FMT_SGRBG12:
1098 case V4L2_PIX_FMT_SRGGB12:
1099 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12;
1100 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1101 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1102 isc->try_config.bpp = 16;
1104 case V4L2_PIX_FMT_RGB565:
1105 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565;
1106 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1107 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1108 isc->try_config.bpp = 16;
1110 case V4L2_PIX_FMT_ARGB444:
1111 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444;
1112 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1113 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1114 isc->try_config.bpp = 16;
1116 case V4L2_PIX_FMT_ARGB555:
1117 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555;
1118 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1119 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1120 isc->try_config.bpp = 16;
1122 case V4L2_PIX_FMT_ABGR32:
1123 case V4L2_PIX_FMT_XBGR32:
1124 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32;
1125 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1126 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1127 isc->try_config.bpp = 32;
1129 case V4L2_PIX_FMT_YUV420:
1130 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1131 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC420P;
1132 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1133 isc->try_config.bpp = 12;
1135 case V4L2_PIX_FMT_YUV422P:
1136 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1137 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC422P;
1138 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1139 isc->try_config.bpp = 16;
1141 case V4L2_PIX_FMT_YUYV:
1142 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1143 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1144 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1145 isc->try_config.bpp = 16;
1147 case V4L2_PIX_FMT_GREY:
1148 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8;
1149 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1150 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1151 isc->try_config.bpp = 8;
1153 case V4L2_PIX_FMT_Y10:
1154 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY10;
1155 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1156 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1157 isc->try_config.bpp = 16;
1164 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1165 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1166 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1174 * Configuring pipeline modules, depending on which format the ISC outputs
1175 * and considering which format it has as input from the sensor.
1177 static int isc_try_configure_pipeline(struct isc_device *isc)
1179 switch (isc->try_config.fourcc) {
1180 case V4L2_PIX_FMT_RGB565:
1181 case V4L2_PIX_FMT_ARGB555:
1182 case V4L2_PIX_FMT_ARGB444:
1183 case V4L2_PIX_FMT_ABGR32:
1184 case V4L2_PIX_FMT_XBGR32:
1185 /* if sensor format is RAW, we convert inside ISC */
1186 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1187 isc->try_config.bits_pipeline = CFA_ENABLE |
1188 WB_ENABLE | GAM_ENABLES;
1190 isc->try_config.bits_pipeline = 0x0;
1193 case V4L2_PIX_FMT_YUV420:
1194 /* if sensor format is RAW, we convert inside ISC */
1195 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1196 isc->try_config.bits_pipeline = CFA_ENABLE |
1197 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1198 SUB420_ENABLE | SUB422_ENABLE | CBC_ENABLE;
1200 isc->try_config.bits_pipeline = 0x0;
1203 case V4L2_PIX_FMT_YUV422P:
1204 /* if sensor format is RAW, we convert inside ISC */
1205 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1206 isc->try_config.bits_pipeline = CFA_ENABLE |
1207 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1208 SUB422_ENABLE | CBC_ENABLE;
1210 isc->try_config.bits_pipeline = 0x0;
1213 case V4L2_PIX_FMT_YUYV:
1214 /* if sensor format is RAW, we convert inside ISC */
1215 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1216 isc->try_config.bits_pipeline = CFA_ENABLE |
1217 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1218 SUB422_ENABLE | CBC_ENABLE;
1220 isc->try_config.bits_pipeline = 0x0;
1223 case V4L2_PIX_FMT_GREY:
1224 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1225 /* if sensor format is RAW, we convert inside ISC */
1226 isc->try_config.bits_pipeline = CFA_ENABLE |
1227 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1230 isc->try_config.bits_pipeline = 0x0;
1234 isc->try_config.bits_pipeline = 0x0;
1239 static void isc_try_fse(struct isc_device *isc,
1240 struct v4l2_subdev_pad_config *pad_cfg)
1243 struct v4l2_subdev_frame_size_enum fse = {};
1246 * If we do not know yet which format the subdev is using, we cannot
1249 if (!isc->try_config.sd_format)
1252 fse.code = isc->try_config.sd_format->mbus_code;
1253 fse.which = V4L2_SUBDEV_FORMAT_TRY;
1255 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1258 * Attempt to obtain format size from subdev. If not available,
1259 * just use the maximum ISC can receive.
1262 pad_cfg->try_crop.width = ISC_MAX_SUPPORT_WIDTH;
1263 pad_cfg->try_crop.height = ISC_MAX_SUPPORT_HEIGHT;
1265 pad_cfg->try_crop.width = fse.max_width;
1266 pad_cfg->try_crop.height = fse.max_height;
1270 static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
1274 struct isc_format *sd_fmt = NULL, *direct_fmt = NULL;
1275 struct v4l2_pix_format *pixfmt = &f->fmt.pix;
1276 struct v4l2_subdev_pad_config pad_cfg = {};
1277 struct v4l2_subdev_format format = {
1278 .which = V4L2_SUBDEV_FORMAT_TRY,
1282 bool rlp_dma_direct_dump = false;
1284 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1287 /* Step 1: find a RAW format that is supported */
1288 for (i = 0; i < isc->num_user_formats; i++) {
1289 if (ISC_IS_FORMAT_RAW(isc->user_formats[i]->mbus_code)) {
1290 sd_fmt = isc->user_formats[i];
1294 /* Step 2: We can continue with this RAW format, or we can look
1295 * for better: maybe sensor supports directly what we need.
1297 direct_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
1299 /* Step 3: We have both. We decide given the module parameter which
1302 if (direct_fmt && sd_fmt && sensor_preferred)
1303 sd_fmt = direct_fmt;
1305 /* Step 4: we do not have RAW but we have a direct format. Use it. */
1306 if (direct_fmt && !sd_fmt)
1307 sd_fmt = direct_fmt;
1309 /* Step 5: if we are using a direct format, we need to package
1310 * everything as 8 bit data and just dump it
1312 if (sd_fmt == direct_fmt)
1313 rlp_dma_direct_dump = true;
1315 /* Step 6: We have no format. This can happen if the userspace
1316 * requests some weird/invalid format.
1317 * In this case, default to whatever we have
1319 if (!sd_fmt && !direct_fmt) {
1320 sd_fmt = isc->user_formats[isc->num_user_formats - 1];
1321 v4l2_dbg(1, debug, &isc->v4l2_dev,
1322 "Sensor not supporting %.4s, using %.4s\n",
1323 (char *)&pixfmt->pixelformat, (char *)&sd_fmt->fourcc);
1328 goto isc_try_fmt_err;
1331 /* Step 7: Print out what we decided for debugging */
1332 v4l2_dbg(1, debug, &isc->v4l2_dev,
1333 "Preferring to have sensor using format %.4s\n",
1334 (char *)&sd_fmt->fourcc);
1336 /* Step 8: at this moment we decided which format the subdev will use */
1337 isc->try_config.sd_format = sd_fmt;
1339 /* Limit to Atmel ISC hardware capabilities */
1340 if (pixfmt->width > ISC_MAX_SUPPORT_WIDTH)
1341 pixfmt->width = ISC_MAX_SUPPORT_WIDTH;
1342 if (pixfmt->height > ISC_MAX_SUPPORT_HEIGHT)
1343 pixfmt->height = ISC_MAX_SUPPORT_HEIGHT;
1346 * The mbus format is the one the subdev outputs.
1347 * The pixels will be transferred in this format Sensor -> ISC
1349 mbus_code = sd_fmt->mbus_code;
1352 * Validate formats. If the required format is not OK, default to raw.
1355 isc->try_config.fourcc = pixfmt->pixelformat;
1357 if (isc_try_validate_formats(isc)) {
1358 pixfmt->pixelformat = isc->try_config.fourcc = sd_fmt->fourcc;
1359 /* Re-try to validate the new format */
1360 ret = isc_try_validate_formats(isc);
1362 goto isc_try_fmt_err;
1365 ret = isc_try_configure_rlp_dma(isc, rlp_dma_direct_dump);
1367 goto isc_try_fmt_err;
1369 ret = isc_try_configure_pipeline(isc);
1371 goto isc_try_fmt_err;
1373 /* Obtain frame sizes if possible to have crop requirements ready */
1374 isc_try_fse(isc, &pad_cfg);
1376 v4l2_fill_mbus_format(&format.format, pixfmt, mbus_code);
1377 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
1380 goto isc_try_fmt_subdev_err;
1382 v4l2_fill_pix_format(pixfmt, &format.format);
1384 pixfmt->field = V4L2_FIELD_NONE;
1385 pixfmt->bytesperline = (pixfmt->width * isc->try_config.bpp) >> 3;
1386 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
1394 v4l2_err(&isc->v4l2_dev, "Could not find any possible format for a working pipeline\n");
1395 isc_try_fmt_subdev_err:
1396 memset(&isc->try_config, 0, sizeof(isc->try_config));
1401 static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
1403 struct v4l2_subdev_format format = {
1404 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1409 ret = isc_try_fmt(isc, f, &mbus_code);
1413 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, mbus_code);
1414 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1415 set_fmt, NULL, &format);
1421 if (isc->try_config.sd_format && isc->config.sd_format &&
1422 isc->try_config.sd_format != isc->config.sd_format) {
1423 isc->ctrls.hist_stat = HIST_INIT;
1424 isc_reset_awb_ctrls(isc);
1425 isc_update_v4l2_ctrls(isc);
1427 /* make the try configuration active */
1428 isc->config = isc->try_config;
1430 v4l2_dbg(1, debug, &isc->v4l2_dev, "New ISC configuration in place\n");
1435 static int isc_s_fmt_vid_cap(struct file *file, void *priv,
1436 struct v4l2_format *f)
1438 struct isc_device *isc = video_drvdata(file);
1440 if (vb2_is_streaming(&isc->vb2_vidq))
1443 return isc_set_fmt(isc, f);
1446 static int isc_try_fmt_vid_cap(struct file *file, void *priv,
1447 struct v4l2_format *f)
1449 struct isc_device *isc = video_drvdata(file);
1451 return isc_try_fmt(isc, f, NULL);
1454 static int isc_enum_input(struct file *file, void *priv,
1455 struct v4l2_input *inp)
1457 if (inp->index != 0)
1460 inp->type = V4L2_INPUT_TYPE_CAMERA;
1462 strscpy(inp->name, "Camera", sizeof(inp->name));
1467 static int isc_g_input(struct file *file, void *priv, unsigned int *i)
1474 static int isc_s_input(struct file *file, void *priv, unsigned int i)
1482 static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1484 struct isc_device *isc = video_drvdata(file);
1486 return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1489 static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1491 struct isc_device *isc = video_drvdata(file);
1493 return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1496 static int isc_enum_framesizes(struct file *file, void *fh,
1497 struct v4l2_frmsizeenum *fsize)
1499 struct isc_device *isc = video_drvdata(file);
1500 struct v4l2_subdev_frame_size_enum fse = {
1501 .code = isc->config.sd_format->mbus_code,
1502 .index = fsize->index,
1503 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1508 for (i = 0; i < isc->num_user_formats; i++)
1509 if (isc->user_formats[i]->fourcc == fsize->pixel_format)
1512 for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1513 if (controller_formats[i].fourcc == fsize->pixel_format)
1519 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1524 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1525 fsize->discrete.width = fse.max_width;
1526 fsize->discrete.height = fse.max_height;
1531 static int isc_enum_frameintervals(struct file *file, void *fh,
1532 struct v4l2_frmivalenum *fival)
1534 struct isc_device *isc = video_drvdata(file);
1535 struct v4l2_subdev_frame_interval_enum fie = {
1536 .code = isc->config.sd_format->mbus_code,
1537 .index = fival->index,
1538 .width = fival->width,
1539 .height = fival->height,
1540 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1545 for (i = 0; i < isc->num_user_formats; i++)
1546 if (isc->user_formats[i]->fourcc == fival->pixel_format)
1549 for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1550 if (controller_formats[i].fourcc == fival->pixel_format)
1556 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1557 enum_frame_interval, NULL, &fie);
1561 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1562 fival->discrete = fie.interval;
1567 static const struct v4l2_ioctl_ops isc_ioctl_ops = {
1568 .vidioc_querycap = isc_querycap,
1569 .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
1570 .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
1571 .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
1572 .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
1574 .vidioc_enum_input = isc_enum_input,
1575 .vidioc_g_input = isc_g_input,
1576 .vidioc_s_input = isc_s_input,
1578 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1579 .vidioc_querybuf = vb2_ioctl_querybuf,
1580 .vidioc_qbuf = vb2_ioctl_qbuf,
1581 .vidioc_expbuf = vb2_ioctl_expbuf,
1582 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1583 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1584 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1585 .vidioc_streamon = vb2_ioctl_streamon,
1586 .vidioc_streamoff = vb2_ioctl_streamoff,
1588 .vidioc_g_parm = isc_g_parm,
1589 .vidioc_s_parm = isc_s_parm,
1590 .vidioc_enum_framesizes = isc_enum_framesizes,
1591 .vidioc_enum_frameintervals = isc_enum_frameintervals,
1593 .vidioc_log_status = v4l2_ctrl_log_status,
1594 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1595 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1598 static int isc_open(struct file *file)
1600 struct isc_device *isc = video_drvdata(file);
1601 struct v4l2_subdev *sd = isc->current_subdev->sd;
1604 if (mutex_lock_interruptible(&isc->lock))
1605 return -ERESTARTSYS;
1607 ret = v4l2_fh_open(file);
1611 if (!v4l2_fh_is_singular_file(file))
1614 ret = v4l2_subdev_call(sd, core, s_power, 1);
1615 if (ret < 0 && ret != -ENOIOCTLCMD) {
1616 v4l2_fh_release(file);
1620 ret = isc_set_fmt(isc, &isc->fmt);
1622 v4l2_subdev_call(sd, core, s_power, 0);
1623 v4l2_fh_release(file);
1627 mutex_unlock(&isc->lock);
1631 static int isc_release(struct file *file)
1633 struct isc_device *isc = video_drvdata(file);
1634 struct v4l2_subdev *sd = isc->current_subdev->sd;
1638 mutex_lock(&isc->lock);
1640 fh_singular = v4l2_fh_is_singular_file(file);
1642 ret = _vb2_fop_release(file, NULL);
1645 v4l2_subdev_call(sd, core, s_power, 0);
1647 mutex_unlock(&isc->lock);
1652 static const struct v4l2_file_operations isc_fops = {
1653 .owner = THIS_MODULE,
1655 .release = isc_release,
1656 .unlocked_ioctl = video_ioctl2,
1657 .read = vb2_fop_read,
1658 .mmap = vb2_fop_mmap,
1659 .poll = vb2_fop_poll,
1662 irqreturn_t isc_interrupt(int irq, void *dev_id)
1664 struct isc_device *isc = (struct isc_device *)dev_id;
1665 struct regmap *regmap = isc->regmap;
1666 u32 isc_intsr, isc_intmask, pending;
1667 irqreturn_t ret = IRQ_NONE;
1669 regmap_read(regmap, ISC_INTSR, &isc_intsr);
1670 regmap_read(regmap, ISC_INTMASK, &isc_intmask);
1672 pending = isc_intsr & isc_intmask;
1674 if (likely(pending & ISC_INT_DDONE)) {
1675 spin_lock(&isc->dma_queue_lock);
1677 struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
1678 struct vb2_buffer *vb = &vbuf->vb2_buf;
1680 vb->timestamp = ktime_get_ns();
1681 vbuf->sequence = isc->sequence++;
1682 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1683 isc->cur_frm = NULL;
1686 if (!list_empty(&isc->dma_queue) && !isc->stop) {
1687 isc->cur_frm = list_first_entry(&isc->dma_queue,
1688 struct isc_buffer, list);
1689 list_del(&isc->cur_frm->list);
1695 complete(&isc->comp);
1698 spin_unlock(&isc->dma_queue_lock);
1701 if (pending & ISC_INT_HISDONE) {
1702 schedule_work(&isc->awb_work);
1709 static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
1711 struct regmap *regmap = isc->regmap;
1712 struct isc_ctrls *ctrls = &isc->ctrls;
1713 u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
1714 u32 *hist_entry = &ctrls->hist_entry[0];
1718 *max = HIST_ENTRIES;
1720 regmap_bulk_read(regmap, ISC_HIS_ENTRY, hist_entry, HIST_ENTRIES);
1724 * we deliberately ignore the end of the histogram,
1725 * the most white pixels
1727 for (i = 1; i < HIST_ENTRIES; i++) {
1728 if (*hist_entry && !*min)
1732 *hist_count += i * (*hist_entry++);
1739 static void isc_wb_update(struct isc_ctrls *ctrls)
1741 u32 *hist_count = &ctrls->hist_count[0];
1744 /* We compute two gains, stretch gain and grey world gain */
1745 u32 s_gain[4], gw_gain[4];
1748 * According to Grey World, we need to set gains for R/B to normalize
1749 * them towards the green channel.
1750 * Thus we want to keep Green as fixed and adjust only Red/Blue
1751 * Compute the average of the both green channels first
1753 avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
1754 (u64)hist_count[ISC_HIS_CFG_MODE_GB];
1757 /* Green histogram is null, nothing to do */
1761 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
1763 * the color offset is the minimum value of the histogram.
1764 * we stretch this color to the full range by substracting
1765 * this value from the color component.
1767 offset[c] = ctrls->hist_minmax[c][HIST_MIN_INDEX];
1769 * The offset is always at least 1. If the offset is 1, we do
1770 * not need to adjust it, so our result must be zero.
1771 * the offset is computed in a histogram on 9 bits (0..512)
1772 * but the offset in register is based on
1773 * 12 bits pipeline (0..4096).
1774 * we need to shift with the 3 bits that the histogram is
1777 ctrls->offset[c] = (offset[c] - 1) << 3;
1780 * the offset is then taken and converted to 2's complements,
1781 * and must be negative, as we subtract this value from the
1784 ctrls->offset[c] = -ctrls->offset[c];
1787 * the stretch gain is the total number of histogram bins
1788 * divided by the actual range of color component (Max - Min)
1789 * If we compute gain like this, the actual color component
1790 * will be stretched to the full histogram.
1791 * We need to shift 9 bits for precision, we have 9 bits for
1794 s_gain[c] = (HIST_ENTRIES << 9) /
1795 (ctrls->hist_minmax[c][HIST_MAX_INDEX] -
1796 ctrls->hist_minmax[c][HIST_MIN_INDEX] + 1);
1799 * Now we have to compute the gain w.r.t. the average.
1800 * Add/lose gain to the component towards the average.
1801 * If it happens that the component is zero, use the
1802 * fixed point value : 1.0 gain.
1805 gw_gain[c] = div_u64(avg << 9, hist_count[c]);
1807 gw_gain[c] = 1 << 9;
1809 /* multiply both gains and adjust for decimals */
1810 ctrls->gain[c] = s_gain[c] * gw_gain[c];
1811 ctrls->gain[c] >>= 9;
1815 static void isc_awb_work(struct work_struct *w)
1817 struct isc_device *isc =
1818 container_of(w, struct isc_device, awb_work);
1819 struct regmap *regmap = isc->regmap;
1820 struct isc_ctrls *ctrls = &isc->ctrls;
1821 u32 hist_id = ctrls->hist_id;
1823 unsigned long flags;
1827 /* streaming is not active anymore */
1831 if (ctrls->hist_stat != HIST_ENABLED)
1834 isc_hist_count(isc, &min, &max);
1835 ctrls->hist_minmax[hist_id][HIST_MIN_INDEX] = min;
1836 ctrls->hist_minmax[hist_id][HIST_MAX_INDEX] = max;
1838 if (hist_id != ISC_HIS_CFG_MODE_B) {
1841 isc_wb_update(ctrls);
1842 hist_id = ISC_HIS_CFG_MODE_GR;
1845 ctrls->hist_id = hist_id;
1846 baysel = isc->config.sd_format->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
1848 ret = pm_runtime_resume_and_get(isc->dev);
1853 * only update if we have all the required histograms and controls
1854 * if awb has been disabled, we need to reset registers as well.
1856 if (hist_id == ISC_HIS_CFG_MODE_GR || ctrls->awb == ISC_WB_NONE) {
1858 * It may happen that DMA Done IRQ will trigger while we are
1859 * updating white balance registers here.
1860 * In that case, only parts of the controls have been updated.
1861 * We can avoid that by locking the section.
1863 spin_lock_irqsave(&isc->awb_lock, flags);
1864 isc_update_awb_ctrls(isc);
1865 spin_unlock_irqrestore(&isc->awb_lock, flags);
1868 * if we are doing just the one time white balance adjustment,
1869 * we are basically done.
1871 if (ctrls->awb == ISC_WB_ONETIME) {
1872 v4l2_info(&isc->v4l2_dev,
1873 "Completed one time white-balance adjustment.\n");
1874 /* update the v4l2 controls values */
1875 isc_update_v4l2_ctrls(isc);
1876 ctrls->awb = ISC_WB_NONE;
1879 regmap_write(regmap, ISC_HIS_CFG, hist_id | baysel | ISC_HIS_CFG_RAR);
1880 isc_update_profile(isc);
1881 /* if awb has been disabled, we don't need to start another histogram */
1883 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
1885 pm_runtime_put_sync(isc->dev);
1888 static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
1890 struct isc_device *isc = container_of(ctrl->handler,
1891 struct isc_device, ctrls.handler);
1892 struct isc_ctrls *ctrls = &isc->ctrls;
1894 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1898 case V4L2_CID_BRIGHTNESS:
1899 ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
1901 case V4L2_CID_CONTRAST:
1902 ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
1904 case V4L2_CID_GAMMA:
1905 ctrls->gamma_index = ctrl->val;
1914 static const struct v4l2_ctrl_ops isc_ctrl_ops = {
1915 .s_ctrl = isc_s_ctrl,
1918 static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
1920 struct isc_device *isc = container_of(ctrl->handler,
1921 struct isc_device, ctrls.handler);
1922 struct isc_ctrls *ctrls = &isc->ctrls;
1924 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1928 case V4L2_CID_AUTO_WHITE_BALANCE:
1930 ctrls->awb = ISC_WB_AUTO;
1932 ctrls->awb = ISC_WB_NONE;
1934 /* we did not configure ISC yet */
1935 if (!isc->config.sd_format)
1938 /* configure the controls with new values from v4l2 */
1939 if (ctrl->cluster[ISC_CTRL_R_GAIN]->is_new)
1940 ctrls->gain[ISC_HIS_CFG_MODE_R] = isc->r_gain_ctrl->val;
1941 if (ctrl->cluster[ISC_CTRL_B_GAIN]->is_new)
1942 ctrls->gain[ISC_HIS_CFG_MODE_B] = isc->b_gain_ctrl->val;
1943 if (ctrl->cluster[ISC_CTRL_GR_GAIN]->is_new)
1944 ctrls->gain[ISC_HIS_CFG_MODE_GR] = isc->gr_gain_ctrl->val;
1945 if (ctrl->cluster[ISC_CTRL_GB_GAIN]->is_new)
1946 ctrls->gain[ISC_HIS_CFG_MODE_GB] = isc->gb_gain_ctrl->val;
1948 if (ctrl->cluster[ISC_CTRL_R_OFF]->is_new)
1949 ctrls->offset[ISC_HIS_CFG_MODE_R] = isc->r_off_ctrl->val;
1950 if (ctrl->cluster[ISC_CTRL_B_OFF]->is_new)
1951 ctrls->offset[ISC_HIS_CFG_MODE_B] = isc->b_off_ctrl->val;
1952 if (ctrl->cluster[ISC_CTRL_GR_OFF]->is_new)
1953 ctrls->offset[ISC_HIS_CFG_MODE_GR] = isc->gr_off_ctrl->val;
1954 if (ctrl->cluster[ISC_CTRL_GB_OFF]->is_new)
1955 ctrls->offset[ISC_HIS_CFG_MODE_GB] = isc->gb_off_ctrl->val;
1957 isc_update_awb_ctrls(isc);
1959 if (vb2_is_streaming(&isc->vb2_vidq)) {
1961 * If we are streaming, we can update profile to
1962 * have the new settings in place.
1964 isc_update_profile(isc);
1967 * The auto cluster will activate automatically this
1968 * control. This has to be deactivated when not
1971 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
1974 /* if we have autowhitebalance on, start histogram procedure */
1975 if (ctrls->awb == ISC_WB_AUTO &&
1976 vb2_is_streaming(&isc->vb2_vidq) &&
1977 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
1978 isc_set_histogram(isc, true);
1981 * for one time whitebalance adjustment, check the button,
1982 * if it's pressed, perform the one time operation.
1984 if (ctrls->awb == ISC_WB_NONE &&
1985 ctrl->cluster[ISC_CTRL_DO_WB]->is_new &&
1986 !(ctrl->cluster[ISC_CTRL_DO_WB]->flags &
1987 V4L2_CTRL_FLAG_INACTIVE)) {
1988 ctrls->awb = ISC_WB_ONETIME;
1989 isc_set_histogram(isc, true);
1990 v4l2_dbg(1, debug, &isc->v4l2_dev,
1991 "One time white-balance started.\n");
1998 static int isc_g_volatile_awb_ctrl(struct v4l2_ctrl *ctrl)
2000 struct isc_device *isc = container_of(ctrl->handler,
2001 struct isc_device, ctrls.handler);
2002 struct isc_ctrls *ctrls = &isc->ctrls;
2005 /* being a cluster, this id will be called for every control */
2006 case V4L2_CID_AUTO_WHITE_BALANCE:
2007 ctrl->cluster[ISC_CTRL_R_GAIN]->val =
2008 ctrls->gain[ISC_HIS_CFG_MODE_R];
2009 ctrl->cluster[ISC_CTRL_B_GAIN]->val =
2010 ctrls->gain[ISC_HIS_CFG_MODE_B];
2011 ctrl->cluster[ISC_CTRL_GR_GAIN]->val =
2012 ctrls->gain[ISC_HIS_CFG_MODE_GR];
2013 ctrl->cluster[ISC_CTRL_GB_GAIN]->val =
2014 ctrls->gain[ISC_HIS_CFG_MODE_GB];
2016 ctrl->cluster[ISC_CTRL_R_OFF]->val =
2017 ctrls->offset[ISC_HIS_CFG_MODE_R];
2018 ctrl->cluster[ISC_CTRL_B_OFF]->val =
2019 ctrls->offset[ISC_HIS_CFG_MODE_B];
2020 ctrl->cluster[ISC_CTRL_GR_OFF]->val =
2021 ctrls->offset[ISC_HIS_CFG_MODE_GR];
2022 ctrl->cluster[ISC_CTRL_GB_OFF]->val =
2023 ctrls->offset[ISC_HIS_CFG_MODE_GB];
2029 static const struct v4l2_ctrl_ops isc_awb_ops = {
2030 .s_ctrl = isc_s_awb_ctrl,
2031 .g_volatile_ctrl = isc_g_volatile_awb_ctrl,
2034 #define ISC_CTRL_OFF(_name, _id, _name_str) \
2035 static const struct v4l2_ctrl_config _name = { \
2036 .ops = &isc_awb_ops, \
2038 .name = _name_str, \
2039 .type = V4L2_CTRL_TYPE_INTEGER, \
2040 .flags = V4L2_CTRL_FLAG_SLIDER, \
2047 ISC_CTRL_OFF(isc_r_off_ctrl, ISC_CID_R_OFFSET, "Red Component Offset");
2048 ISC_CTRL_OFF(isc_b_off_ctrl, ISC_CID_B_OFFSET, "Blue Component Offset");
2049 ISC_CTRL_OFF(isc_gr_off_ctrl, ISC_CID_GR_OFFSET, "Green Red Component Offset");
2050 ISC_CTRL_OFF(isc_gb_off_ctrl, ISC_CID_GB_OFFSET, "Green Blue Component Offset");
2052 #define ISC_CTRL_GAIN(_name, _id, _name_str) \
2053 static const struct v4l2_ctrl_config _name = { \
2054 .ops = &isc_awb_ops, \
2056 .name = _name_str, \
2057 .type = V4L2_CTRL_TYPE_INTEGER, \
2058 .flags = V4L2_CTRL_FLAG_SLIDER, \
2065 ISC_CTRL_GAIN(isc_r_gain_ctrl, ISC_CID_R_GAIN, "Red Component Gain");
2066 ISC_CTRL_GAIN(isc_b_gain_ctrl, ISC_CID_B_GAIN, "Blue Component Gain");
2067 ISC_CTRL_GAIN(isc_gr_gain_ctrl, ISC_CID_GR_GAIN, "Green Red Component Gain");
2068 ISC_CTRL_GAIN(isc_gb_gain_ctrl, ISC_CID_GB_GAIN, "Green Blue Component Gain");
2070 static int isc_ctrl_init(struct isc_device *isc)
2072 const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
2073 struct isc_ctrls *ctrls = &isc->ctrls;
2074 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2077 ctrls->hist_stat = HIST_INIT;
2078 isc_reset_awb_ctrls(isc);
2080 ret = v4l2_ctrl_handler_init(hdl, 13);
2084 ctrls->brightness = 0;
2085 ctrls->contrast = 256;
2087 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
2088 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -2048, 2047, 1, 256);
2089 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, GAMMA_MAX, 1, 2);
2090 isc->awb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
2091 V4L2_CID_AUTO_WHITE_BALANCE,
2094 /* do_white_balance is a button, so min,max,step,default are ignored */
2095 isc->do_wb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
2096 V4L2_CID_DO_WHITE_BALANCE,
2099 if (!isc->do_wb_ctrl) {
2101 v4l2_ctrl_handler_free(hdl);
2105 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
2107 isc->r_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_gain_ctrl, NULL);
2108 isc->b_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_gain_ctrl, NULL);
2109 isc->gr_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_gain_ctrl, NULL);
2110 isc->gb_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_gain_ctrl, NULL);
2111 isc->r_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_off_ctrl, NULL);
2112 isc->b_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_off_ctrl, NULL);
2113 isc->gr_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_off_ctrl, NULL);
2114 isc->gb_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_off_ctrl, NULL);
2117 * The cluster is in auto mode with autowhitebalance enabled
2118 * and manual mode otherwise.
2120 v4l2_ctrl_auto_cluster(10, &isc->awb_ctrl, 0, true);
2122 v4l2_ctrl_handler_setup(hdl);
2127 static int isc_async_bound(struct v4l2_async_notifier *notifier,
2128 struct v4l2_subdev *subdev,
2129 struct v4l2_async_subdev *asd)
2131 struct isc_device *isc = container_of(notifier->v4l2_dev,
2132 struct isc_device, v4l2_dev);
2133 struct isc_subdev_entity *subdev_entity =
2134 container_of(notifier, struct isc_subdev_entity, notifier);
2136 if (video_is_registered(&isc->video_dev)) {
2137 v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
2141 subdev_entity->sd = subdev;
2146 static void isc_async_unbind(struct v4l2_async_notifier *notifier,
2147 struct v4l2_subdev *subdev,
2148 struct v4l2_async_subdev *asd)
2150 struct isc_device *isc = container_of(notifier->v4l2_dev,
2151 struct isc_device, v4l2_dev);
2152 cancel_work_sync(&isc->awb_work);
2153 video_unregister_device(&isc->video_dev);
2154 v4l2_ctrl_handler_free(&isc->ctrls.handler);
2157 static struct isc_format *find_format_by_code(unsigned int code, int *index)
2159 struct isc_format *fmt = &formats_list[0];
2162 for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
2163 if (fmt->mbus_code == code) {
2174 static int isc_formats_init(struct isc_device *isc)
2176 struct isc_format *fmt;
2177 struct v4l2_subdev *subdev = isc->current_subdev->sd;
2178 unsigned int num_fmts, i, j;
2179 u32 list_size = ARRAY_SIZE(formats_list);
2180 struct v4l2_subdev_mbus_code_enum mbus_code = {
2181 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2185 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
2186 NULL, &mbus_code)) {
2189 fmt = find_format_by_code(mbus_code.code, &i);
2191 v4l2_warn(&isc->v4l2_dev, "Mbus code %x not supported\n",
2196 fmt->sd_support = true;
2203 isc->num_user_formats = num_fmts;
2204 isc->user_formats = devm_kcalloc(isc->dev,
2205 num_fmts, sizeof(*isc->user_formats),
2207 if (!isc->user_formats)
2210 fmt = &formats_list[0];
2211 for (i = 0, j = 0; i < list_size; i++) {
2212 if (fmt->sd_support)
2213 isc->user_formats[j++] = fmt;
2220 static int isc_set_default_fmt(struct isc_device *isc)
2222 struct v4l2_format f = {
2223 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2226 .height = VGA_HEIGHT,
2227 .field = V4L2_FIELD_NONE,
2228 .pixelformat = isc->user_formats[0]->fourcc,
2233 ret = isc_try_fmt(isc, &f, NULL);
2241 static int isc_async_complete(struct v4l2_async_notifier *notifier)
2243 struct isc_device *isc = container_of(notifier->v4l2_dev,
2244 struct isc_device, v4l2_dev);
2245 struct video_device *vdev = &isc->video_dev;
2246 struct vb2_queue *q = &isc->vb2_vidq;
2249 INIT_WORK(&isc->awb_work, isc_awb_work);
2251 ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
2253 v4l2_err(&isc->v4l2_dev, "Failed to register subdev nodes\n");
2257 isc->current_subdev = container_of(notifier,
2258 struct isc_subdev_entity, notifier);
2259 mutex_init(&isc->lock);
2260 init_completion(&isc->comp);
2262 /* Initialize videobuf2 queue */
2263 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2264 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2266 q->buf_struct_size = sizeof(struct isc_buffer);
2267 q->ops = &isc_vb2_ops;
2268 q->mem_ops = &vb2_dma_contig_memops;
2269 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2270 q->lock = &isc->lock;
2271 q->min_buffers_needed = 1;
2274 ret = vb2_queue_init(q);
2276 v4l2_err(&isc->v4l2_dev,
2277 "vb2_queue_init() failed: %d\n", ret);
2278 goto isc_async_complete_err;
2281 /* Init video dma queues */
2282 INIT_LIST_HEAD(&isc->dma_queue);
2283 spin_lock_init(&isc->dma_queue_lock);
2284 spin_lock_init(&isc->awb_lock);
2286 ret = isc_formats_init(isc);
2288 v4l2_err(&isc->v4l2_dev,
2289 "Init format failed: %d\n", ret);
2290 goto isc_async_complete_err;
2293 ret = isc_set_default_fmt(isc);
2295 v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
2296 goto isc_async_complete_err;
2299 ret = isc_ctrl_init(isc);
2301 v4l2_err(&isc->v4l2_dev, "Init isc ctrols failed: %d\n", ret);
2302 goto isc_async_complete_err;
2305 /* Register video device */
2306 strscpy(vdev->name, ATMEL_ISC_NAME, sizeof(vdev->name));
2307 vdev->release = video_device_release_empty;
2308 vdev->fops = &isc_fops;
2309 vdev->ioctl_ops = &isc_ioctl_ops;
2310 vdev->v4l2_dev = &isc->v4l2_dev;
2311 vdev->vfl_dir = VFL_DIR_RX;
2313 vdev->lock = &isc->lock;
2314 vdev->ctrl_handler = &isc->ctrls.handler;
2315 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
2316 video_set_drvdata(vdev, isc);
2318 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2320 v4l2_err(&isc->v4l2_dev,
2321 "video_register_device failed: %d\n", ret);
2322 goto isc_async_complete_err;
2327 isc_async_complete_err:
2328 mutex_destroy(&isc->lock);
2332 const struct v4l2_async_notifier_operations isc_async_ops = {
2333 .bound = isc_async_bound,
2334 .unbind = isc_async_unbind,
2335 .complete = isc_async_complete,
2338 void isc_subdev_cleanup(struct isc_device *isc)
2340 struct isc_subdev_entity *subdev_entity;
2342 list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
2343 v4l2_async_notifier_unregister(&subdev_entity->notifier);
2344 v4l2_async_notifier_cleanup(&subdev_entity->notifier);
2347 INIT_LIST_HEAD(&isc->subdev_entities);
2350 int isc_pipeline_init(struct isc_device *isc)
2352 struct device *dev = isc->dev;
2353 struct regmap *regmap = isc->regmap;
2354 struct regmap_field *regs;
2357 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
2358 const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
2359 REG_FIELD(ISC_WB_CTRL, 0, 0),
2360 REG_FIELD(ISC_CFA_CTRL, 0, 0),
2361 REG_FIELD(ISC_CC_CTRL, 0, 0),
2362 REG_FIELD(ISC_GAM_CTRL, 0, 0),
2363 REG_FIELD(ISC_GAM_CTRL, 1, 1),
2364 REG_FIELD(ISC_GAM_CTRL, 2, 2),
2365 REG_FIELD(ISC_GAM_CTRL, 3, 3),
2366 REG_FIELD(ISC_CSC_CTRL, 0, 0),
2367 REG_FIELD(ISC_CBC_CTRL, 0, 0),
2368 REG_FIELD(ISC_SUB422_CTRL, 0, 0),
2369 REG_FIELD(ISC_SUB420_CTRL, 0, 0),
2372 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
2373 regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
2375 return PTR_ERR(regs);
2377 isc->pipeline[i] = regs;
2383 /* regmap configuration */
2384 #define ATMEL_ISC_REG_MAX 0xbfc
2385 const struct regmap_config isc_regmap_config = {
2389 .max_register = ATMEL_ISC_REG_MAX,