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 #define ISC_IS_FORMAT_RAW(mbus_code) \
180 (((mbus_code) & 0xf000) == 0x3000)
182 #define ISC_IS_FORMAT_GREY(mbus_code) \
183 (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
184 (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
186 static inline void isc_update_v4l2_ctrls(struct isc_device *isc)
188 struct isc_ctrls *ctrls = &isc->ctrls;
190 /* In here we set the v4l2 controls w.r.t. our pipeline config */
191 v4l2_ctrl_s_ctrl(isc->r_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_R]);
192 v4l2_ctrl_s_ctrl(isc->b_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_B]);
193 v4l2_ctrl_s_ctrl(isc->gr_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GR]);
194 v4l2_ctrl_s_ctrl(isc->gb_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GB]);
196 v4l2_ctrl_s_ctrl(isc->r_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_R]);
197 v4l2_ctrl_s_ctrl(isc->b_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_B]);
198 v4l2_ctrl_s_ctrl(isc->gr_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GR]);
199 v4l2_ctrl_s_ctrl(isc->gb_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GB]);
202 static inline void isc_update_awb_ctrls(struct isc_device *isc)
204 struct isc_ctrls *ctrls = &isc->ctrls;
206 /* In here we set our actual hw pipeline config */
208 regmap_write(isc->regmap, ISC_WB_O_RGR,
209 ((ctrls->offset[ISC_HIS_CFG_MODE_R])) |
210 ((ctrls->offset[ISC_HIS_CFG_MODE_GR]) << 16));
211 regmap_write(isc->regmap, ISC_WB_O_BGB,
212 ((ctrls->offset[ISC_HIS_CFG_MODE_B])) |
213 ((ctrls->offset[ISC_HIS_CFG_MODE_GB]) << 16));
214 regmap_write(isc->regmap, ISC_WB_G_RGR,
215 ctrls->gain[ISC_HIS_CFG_MODE_R] |
216 (ctrls->gain[ISC_HIS_CFG_MODE_GR] << 16));
217 regmap_write(isc->regmap, ISC_WB_G_BGB,
218 ctrls->gain[ISC_HIS_CFG_MODE_B] |
219 (ctrls->gain[ISC_HIS_CFG_MODE_GB] << 16));
222 static inline void isc_reset_awb_ctrls(struct isc_device *isc)
226 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
227 /* gains have a fixed point at 9 decimals */
228 isc->ctrls.gain[c] = 1 << 9;
229 /* offsets are in 2's complements */
230 isc->ctrls.offset[c] = 0;
234 static int isc_wait_clk_stable(struct clk_hw *hw)
236 struct isc_clk *isc_clk = to_isc_clk(hw);
237 struct regmap *regmap = isc_clk->regmap;
238 unsigned long timeout = jiffies + usecs_to_jiffies(1000);
241 while (time_before(jiffies, timeout)) {
242 regmap_read(regmap, ISC_CLKSR, &status);
243 if (!(status & ISC_CLKSR_SIP))
246 usleep_range(10, 250);
252 static int isc_clk_prepare(struct clk_hw *hw)
254 struct isc_clk *isc_clk = to_isc_clk(hw);
257 if (isc_clk->id == ISC_ISPCK) {
258 ret = pm_runtime_resume_and_get(isc_clk->dev);
263 return isc_wait_clk_stable(hw);
266 static void isc_clk_unprepare(struct clk_hw *hw)
268 struct isc_clk *isc_clk = to_isc_clk(hw);
270 isc_wait_clk_stable(hw);
272 if (isc_clk->id == ISC_ISPCK)
273 pm_runtime_put_sync(isc_clk->dev);
276 static int isc_clk_enable(struct clk_hw *hw)
278 struct isc_clk *isc_clk = to_isc_clk(hw);
279 u32 id = isc_clk->id;
280 struct regmap *regmap = isc_clk->regmap;
284 dev_dbg(isc_clk->dev, "ISC CLK: %s, id = %d, div = %d, parent id = %d\n",
285 __func__, id, isc_clk->div, isc_clk->parent_id);
287 spin_lock_irqsave(&isc_clk->lock, flags);
288 regmap_update_bits(regmap, ISC_CLKCFG,
289 ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
290 (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
291 (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
293 regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
294 spin_unlock_irqrestore(&isc_clk->lock, flags);
296 regmap_read(regmap, ISC_CLKSR, &status);
297 if (status & ISC_CLK(id))
303 static void isc_clk_disable(struct clk_hw *hw)
305 struct isc_clk *isc_clk = to_isc_clk(hw);
306 u32 id = isc_clk->id;
309 spin_lock_irqsave(&isc_clk->lock, flags);
310 regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
311 spin_unlock_irqrestore(&isc_clk->lock, flags);
314 static int isc_clk_is_enabled(struct clk_hw *hw)
316 struct isc_clk *isc_clk = to_isc_clk(hw);
320 if (isc_clk->id == ISC_ISPCK) {
321 ret = pm_runtime_resume_and_get(isc_clk->dev);
326 regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
328 if (isc_clk->id == ISC_ISPCK)
329 pm_runtime_put_sync(isc_clk->dev);
331 return status & ISC_CLK(isc_clk->id) ? 1 : 0;
335 isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
337 struct isc_clk *isc_clk = to_isc_clk(hw);
339 return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
342 static int isc_clk_determine_rate(struct clk_hw *hw,
343 struct clk_rate_request *req)
345 struct isc_clk *isc_clk = to_isc_clk(hw);
346 long best_rate = -EINVAL;
350 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
351 struct clk_hw *parent;
352 unsigned long parent_rate;
354 parent = clk_hw_get_parent_by_index(hw, i);
358 parent_rate = clk_hw_get_rate(parent);
362 for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
366 rate = DIV_ROUND_CLOSEST(parent_rate, div);
367 diff = abs(req->rate - rate);
369 if (best_diff < 0 || best_diff > diff) {
372 req->best_parent_rate = parent_rate;
373 req->best_parent_hw = parent;
376 if (!best_diff || rate < req->rate)
384 dev_dbg(isc_clk->dev,
385 "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
387 __clk_get_name((req->best_parent_hw)->clk),
388 req->best_parent_rate);
393 req->rate = best_rate;
398 static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
400 struct isc_clk *isc_clk = to_isc_clk(hw);
402 if (index >= clk_hw_get_num_parents(hw))
405 isc_clk->parent_id = index;
410 static u8 isc_clk_get_parent(struct clk_hw *hw)
412 struct isc_clk *isc_clk = to_isc_clk(hw);
414 return isc_clk->parent_id;
417 static int isc_clk_set_rate(struct clk_hw *hw,
419 unsigned long parent_rate)
421 struct isc_clk *isc_clk = to_isc_clk(hw);
427 div = DIV_ROUND_CLOSEST(parent_rate, rate);
428 if (div > (ISC_CLK_MAX_DIV + 1) || !div)
431 isc_clk->div = div - 1;
436 static const struct clk_ops isc_clk_ops = {
437 .prepare = isc_clk_prepare,
438 .unprepare = isc_clk_unprepare,
439 .enable = isc_clk_enable,
440 .disable = isc_clk_disable,
441 .is_enabled = isc_clk_is_enabled,
442 .recalc_rate = isc_clk_recalc_rate,
443 .determine_rate = isc_clk_determine_rate,
444 .set_parent = isc_clk_set_parent,
445 .get_parent = isc_clk_get_parent,
446 .set_rate = isc_clk_set_rate,
449 static int isc_clk_register(struct isc_device *isc, unsigned int id)
451 struct regmap *regmap = isc->regmap;
452 struct device_node *np = isc->dev->of_node;
453 struct isc_clk *isc_clk;
454 struct clk_init_data init;
455 const char *clk_name = np->name;
456 const char *parent_names[3];
459 num_parents = of_clk_get_parent_count(np);
460 if (num_parents < 1 || num_parents > 3)
463 if (num_parents > 2 && id == ISC_ISPCK)
466 of_clk_parent_fill(np, parent_names, num_parents);
469 of_property_read_string(np, "clock-output-names", &clk_name);
471 clk_name = "isc-ispck";
473 init.parent_names = parent_names;
474 init.num_parents = num_parents;
475 init.name = clk_name;
476 init.ops = &isc_clk_ops;
477 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
479 isc_clk = &isc->isc_clks[id];
480 isc_clk->hw.init = &init;
481 isc_clk->regmap = regmap;
483 isc_clk->dev = isc->dev;
484 spin_lock_init(&isc_clk->lock);
486 isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
487 if (IS_ERR(isc_clk->clk)) {
488 dev_err(isc->dev, "%s: clock register fail\n", clk_name);
489 return PTR_ERR(isc_clk->clk);
490 } else if (id == ISC_MCK)
491 of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
496 int isc_clk_init(struct isc_device *isc)
501 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
502 isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
504 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
505 ret = isc_clk_register(isc, i);
513 void isc_clk_cleanup(struct isc_device *isc)
517 of_clk_del_provider(isc->dev->of_node);
519 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
520 struct isc_clk *isc_clk = &isc->isc_clks[i];
522 if (!IS_ERR(isc_clk->clk))
523 clk_unregister(isc_clk->clk);
527 static int isc_queue_setup(struct vb2_queue *vq,
528 unsigned int *nbuffers, unsigned int *nplanes,
529 unsigned int sizes[], struct device *alloc_devs[])
531 struct isc_device *isc = vb2_get_drv_priv(vq);
532 unsigned int size = isc->fmt.fmt.pix.sizeimage;
535 return sizes[0] < size ? -EINVAL : 0;
543 static int isc_buffer_prepare(struct vb2_buffer *vb)
545 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
546 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
547 unsigned long size = isc->fmt.fmt.pix.sizeimage;
549 if (vb2_plane_size(vb, 0) < size) {
550 v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
551 vb2_plane_size(vb, 0), size);
555 vb2_set_plane_payload(vb, 0, size);
557 vbuf->field = isc->fmt.fmt.pix.field;
562 static void isc_start_dma(struct isc_device *isc)
564 struct regmap *regmap = isc->regmap;
565 u32 sizeimage = isc->fmt.fmt.pix.sizeimage;
570 h = isc->fmt.fmt.pix.height;
571 w = isc->fmt.fmt.pix.width;
574 * In case the sensor is not RAW, it will output a pixel (12-16 bits)
575 * with two samples on the ISC Data bus (which is 8-12)
576 * ISC will count each sample, so, we need to multiply these values
577 * by two, to get the real number of samples for the required pixels.
579 if (!ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code)) {
585 * We limit the column/row count that the ISC will output according
586 * to the configured resolution that we want.
587 * This will avoid the situation where the sensor is misconfigured,
588 * sending more data, and the ISC will just take it and DMA to memory,
589 * causing corruption.
591 regmap_write(regmap, ISC_PFE_CFG1,
592 (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK) |
593 (ISC_PFE_CFG1_COLMAX(w - 1) & ISC_PFE_CFG1_COLMAX_MASK));
595 regmap_write(regmap, ISC_PFE_CFG2,
596 (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK) |
597 (ISC_PFE_CFG2_ROWMAX(h - 1) & ISC_PFE_CFG2_ROWMAX_MASK));
599 regmap_update_bits(regmap, ISC_PFE_CFG0,
600 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN,
601 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN);
603 addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
604 regmap_write(regmap, ISC_DAD0, addr0);
606 switch (isc->config.fourcc) {
607 case V4L2_PIX_FMT_YUV420:
608 regmap_write(regmap, ISC_DAD1, addr0 + (sizeimage * 2) / 3);
609 regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 5) / 6);
611 case V4L2_PIX_FMT_YUV422P:
612 regmap_write(regmap, ISC_DAD1, addr0 + sizeimage / 2);
613 regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 3) / 4);
619 dctrl_dview = isc->config.dctrl_dview;
621 regmap_write(regmap, ISC_DCTRL, dctrl_dview | ISC_DCTRL_IE_IS);
622 spin_lock(&isc->awb_lock);
623 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
624 spin_unlock(&isc->awb_lock);
627 static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
629 struct regmap *regmap = isc->regmap;
630 struct isc_ctrls *ctrls = &isc->ctrls;
635 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
636 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
637 val = pipeline & BIT(i) ? 1 : 0;
638 regmap_field_write(isc->pipeline[i], val);
644 bay_cfg = isc->config.sd_format->cfa_baycfg;
646 regmap_write(regmap, ISC_WB_CFG, bay_cfg);
647 isc_update_awb_ctrls(isc);
648 isc_update_v4l2_ctrls(isc);
650 regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
652 gamma = &isc->gamma_table[ctrls->gamma_index][0];
653 regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
654 regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
655 regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
657 isc->config_csc(isc);
658 isc->config_cbc(isc);
661 static int isc_update_profile(struct isc_device *isc)
663 struct regmap *regmap = isc->regmap;
667 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
669 regmap_read(regmap, ISC_CTRLSR, &sr);
670 while ((sr & ISC_CTRL_UPPRO) && counter--) {
671 usleep_range(1000, 2000);
672 regmap_read(regmap, ISC_CTRLSR, &sr);
676 v4l2_warn(&isc->v4l2_dev, "Time out to update profile\n");
683 static void isc_set_histogram(struct isc_device *isc, bool enable)
685 struct regmap *regmap = isc->regmap;
686 struct isc_ctrls *ctrls = &isc->ctrls;
689 regmap_write(regmap, ISC_HIS_CFG,
690 ISC_HIS_CFG_MODE_GR |
691 (isc->config.sd_format->cfa_baycfg
692 << ISC_HIS_CFG_BAYSEL_SHIFT) |
694 regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_EN);
695 regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
696 ctrls->hist_id = ISC_HIS_CFG_MODE_GR;
697 isc_update_profile(isc);
698 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
700 ctrls->hist_stat = HIST_ENABLED;
702 regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
703 regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_DIS);
705 ctrls->hist_stat = HIST_DISABLED;
709 static int isc_configure(struct isc_device *isc)
711 struct regmap *regmap = isc->regmap;
712 u32 pfe_cfg0, rlp_mode, dcfg, mask, pipeline;
713 struct isc_subdev_entity *subdev = isc->current_subdev;
715 pfe_cfg0 = isc->config.sd_format->pfe_cfg0_bps;
716 rlp_mode = isc->config.rlp_cfg_mode;
717 pipeline = isc->config.bits_pipeline;
719 dcfg = isc->config.dcfg_imode | isc->dcfg;
721 pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
722 mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
723 ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
724 ISC_PFE_CFG0_MODE_MASK | ISC_PFE_CFG0_CCIR_CRC |
725 ISC_PFE_CFG0_CCIR656;
727 regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
729 regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
732 regmap_write(regmap, ISC_DCFG, dcfg);
734 /* Set the pipeline */
735 isc_set_pipeline(isc, pipeline);
738 * The current implemented histogram is available for RAW R, B, GB, GR
739 * channels. We need to check if sensor is outputting RAW BAYER
741 if (isc->ctrls.awb &&
742 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
743 isc_set_histogram(isc, true);
745 isc_set_histogram(isc, false);
748 return isc_update_profile(isc);
751 static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
753 struct isc_device *isc = vb2_get_drv_priv(vq);
754 struct regmap *regmap = isc->regmap;
755 struct isc_buffer *buf;
759 /* Enable stream on the sub device */
760 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
761 if (ret && ret != -ENOIOCTLCMD) {
762 v4l2_err(&isc->v4l2_dev, "stream on failed in subdev %d\n",
764 goto err_start_stream;
767 ret = pm_runtime_resume_and_get(isc->dev);
769 v4l2_err(&isc->v4l2_dev, "RPM resume failed in subdev %d\n",
774 ret = isc_configure(isc);
778 /* Enable DMA interrupt */
779 regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
781 spin_lock_irqsave(&isc->dma_queue_lock, flags);
785 reinit_completion(&isc->comp);
787 isc->cur_frm = list_first_entry(&isc->dma_queue,
788 struct isc_buffer, list);
789 list_del(&isc->cur_frm->list);
793 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
795 /* if we streaming from RAW, we can do one-shot white balance adj */
796 if (ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
797 v4l2_ctrl_activate(isc->do_wb_ctrl, true);
802 pm_runtime_put_sync(isc->dev);
804 v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
807 spin_lock_irqsave(&isc->dma_queue_lock, flags);
808 list_for_each_entry(buf, &isc->dma_queue, list)
809 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
810 INIT_LIST_HEAD(&isc->dma_queue);
811 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
816 static void isc_stop_streaming(struct vb2_queue *vq)
818 struct isc_device *isc = vb2_get_drv_priv(vq);
820 struct isc_buffer *buf;
823 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
827 /* Wait until the end of the current frame */
828 if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
829 v4l2_err(&isc->v4l2_dev,
830 "Timeout waiting for end of the capture\n");
832 /* Disable DMA interrupt */
833 regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
835 pm_runtime_put_sync(isc->dev);
837 /* Disable stream on the sub device */
838 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
839 if (ret && ret != -ENOIOCTLCMD)
840 v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
842 /* Release all active buffers */
843 spin_lock_irqsave(&isc->dma_queue_lock, flags);
844 if (unlikely(isc->cur_frm)) {
845 vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
846 VB2_BUF_STATE_ERROR);
849 list_for_each_entry(buf, &isc->dma_queue, list)
850 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
851 INIT_LIST_HEAD(&isc->dma_queue);
852 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
855 static void isc_buffer_queue(struct vb2_buffer *vb)
857 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
858 struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
859 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
862 spin_lock_irqsave(&isc->dma_queue_lock, flags);
863 if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
864 vb2_is_streaming(vb->vb2_queue)) {
868 list_add_tail(&buf->list, &isc->dma_queue);
869 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
872 static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
875 unsigned int num_formats = isc->num_user_formats;
876 struct isc_format *fmt;
879 for (i = 0; i < num_formats; i++) {
880 fmt = isc->user_formats[i];
881 if (fmt->fourcc == fourcc)
888 static const struct vb2_ops isc_vb2_ops = {
889 .queue_setup = isc_queue_setup,
890 .wait_prepare = vb2_ops_wait_prepare,
891 .wait_finish = vb2_ops_wait_finish,
892 .buf_prepare = isc_buffer_prepare,
893 .start_streaming = isc_start_streaming,
894 .stop_streaming = isc_stop_streaming,
895 .buf_queue = isc_buffer_queue,
898 static int isc_querycap(struct file *file, void *priv,
899 struct v4l2_capability *cap)
901 struct isc_device *isc = video_drvdata(file);
903 strscpy(cap->driver, "microchip-isc", sizeof(cap->driver));
904 strscpy(cap->card, "Atmel Image Sensor Controller", sizeof(cap->card));
905 snprintf(cap->bus_info, sizeof(cap->bus_info),
906 "platform:%s", isc->v4l2_dev.name);
911 static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
912 struct v4l2_fmtdesc *f)
914 u32 index = f->index;
915 u32 i, supported_index;
917 if (index < ARRAY_SIZE(controller_formats)) {
918 f->pixelformat = controller_formats[index].fourcc;
922 index -= ARRAY_SIZE(controller_formats);
926 for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
927 if (!ISC_IS_FORMAT_RAW(formats_list[i].mbus_code) ||
928 !formats_list[i].sd_support)
930 if (supported_index == index) {
931 f->pixelformat = formats_list[i].fourcc;
940 static int isc_g_fmt_vid_cap(struct file *file, void *priv,
941 struct v4l2_format *fmt)
943 struct isc_device *isc = video_drvdata(file);
951 * Checks the current configured format, if ISC can output it,
952 * considering which type of format the ISC receives from the sensor
954 static int isc_try_validate_formats(struct isc_device *isc)
957 bool bayer = false, yuv = false, rgb = false, grey = false;
959 /* all formats supported by the RLP module are OK */
960 switch (isc->try_config.fourcc) {
961 case V4L2_PIX_FMT_SBGGR8:
962 case V4L2_PIX_FMT_SGBRG8:
963 case V4L2_PIX_FMT_SGRBG8:
964 case V4L2_PIX_FMT_SRGGB8:
965 case V4L2_PIX_FMT_SBGGR10:
966 case V4L2_PIX_FMT_SGBRG10:
967 case V4L2_PIX_FMT_SGRBG10:
968 case V4L2_PIX_FMT_SRGGB10:
969 case V4L2_PIX_FMT_SBGGR12:
970 case V4L2_PIX_FMT_SGBRG12:
971 case V4L2_PIX_FMT_SGRBG12:
972 case V4L2_PIX_FMT_SRGGB12:
977 case V4L2_PIX_FMT_YUV420:
978 case V4L2_PIX_FMT_YUV422P:
979 case V4L2_PIX_FMT_YUYV:
984 case V4L2_PIX_FMT_RGB565:
985 case V4L2_PIX_FMT_ABGR32:
986 case V4L2_PIX_FMT_XBGR32:
987 case V4L2_PIX_FMT_ARGB444:
988 case V4L2_PIX_FMT_ARGB555:
992 case V4L2_PIX_FMT_GREY:
993 case V4L2_PIX_FMT_Y10:
998 /* any other different formats are not supported */
1001 v4l2_dbg(1, debug, &isc->v4l2_dev,
1002 "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
1003 rgb, yuv, grey, bayer);
1005 /* we cannot output RAW if we do not receive RAW */
1006 if ((bayer) && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
1009 /* we cannot output GREY if we do not receive RAW/GREY */
1010 if (grey && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code) &&
1011 !ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code))
1018 * Configures the RLP and DMA modules, depending on the output format
1019 * configured for the ISC.
1020 * If direct_dump == true, just dump raw data 8/16 bits depending on format.
1022 static int isc_try_configure_rlp_dma(struct isc_device *isc, bool direct_dump)
1024 switch (isc->try_config.fourcc) {
1025 case V4L2_PIX_FMT_SBGGR8:
1026 case V4L2_PIX_FMT_SGBRG8:
1027 case V4L2_PIX_FMT_SGRBG8:
1028 case V4L2_PIX_FMT_SRGGB8:
1029 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1030 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1031 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1032 isc->try_config.bpp = 8;
1034 case V4L2_PIX_FMT_SBGGR10:
1035 case V4L2_PIX_FMT_SGBRG10:
1036 case V4L2_PIX_FMT_SGRBG10:
1037 case V4L2_PIX_FMT_SRGGB10:
1038 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10;
1039 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1040 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1041 isc->try_config.bpp = 16;
1043 case V4L2_PIX_FMT_SBGGR12:
1044 case V4L2_PIX_FMT_SGBRG12:
1045 case V4L2_PIX_FMT_SGRBG12:
1046 case V4L2_PIX_FMT_SRGGB12:
1047 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12;
1048 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1049 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1050 isc->try_config.bpp = 16;
1052 case V4L2_PIX_FMT_RGB565:
1053 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565;
1054 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1055 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1056 isc->try_config.bpp = 16;
1058 case V4L2_PIX_FMT_ARGB444:
1059 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444;
1060 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1061 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1062 isc->try_config.bpp = 16;
1064 case V4L2_PIX_FMT_ARGB555:
1065 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555;
1066 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1067 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1068 isc->try_config.bpp = 16;
1070 case V4L2_PIX_FMT_ABGR32:
1071 case V4L2_PIX_FMT_XBGR32:
1072 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32;
1073 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1074 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1075 isc->try_config.bpp = 32;
1077 case V4L2_PIX_FMT_YUV420:
1078 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1079 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC420P;
1080 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1081 isc->try_config.bpp = 12;
1083 case V4L2_PIX_FMT_YUV422P:
1084 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1085 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC422P;
1086 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1087 isc->try_config.bpp = 16;
1089 case V4L2_PIX_FMT_YUYV:
1090 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1091 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1092 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1093 isc->try_config.bpp = 16;
1095 case V4L2_PIX_FMT_GREY:
1096 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8;
1097 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1098 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1099 isc->try_config.bpp = 8;
1101 case V4L2_PIX_FMT_Y10:
1102 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY10;
1103 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1104 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1105 isc->try_config.bpp = 16;
1112 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1113 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1114 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1122 * Configuring pipeline modules, depending on which format the ISC outputs
1123 * and considering which format it has as input from the sensor.
1125 static int isc_try_configure_pipeline(struct isc_device *isc)
1127 switch (isc->try_config.fourcc) {
1128 case V4L2_PIX_FMT_RGB565:
1129 case V4L2_PIX_FMT_ARGB555:
1130 case V4L2_PIX_FMT_ARGB444:
1131 case V4L2_PIX_FMT_ABGR32:
1132 case V4L2_PIX_FMT_XBGR32:
1133 /* if sensor format is RAW, we convert inside ISC */
1134 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1135 isc->try_config.bits_pipeline = CFA_ENABLE |
1136 WB_ENABLE | GAM_ENABLES;
1138 isc->try_config.bits_pipeline = 0x0;
1141 case V4L2_PIX_FMT_YUV420:
1142 /* if sensor format is RAW, we convert inside ISC */
1143 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1144 isc->try_config.bits_pipeline = CFA_ENABLE |
1145 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1146 SUB420_ENABLE | SUB422_ENABLE | CBC_ENABLE;
1148 isc->try_config.bits_pipeline = 0x0;
1151 case V4L2_PIX_FMT_YUV422P:
1152 /* if sensor format is RAW, we convert inside ISC */
1153 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1154 isc->try_config.bits_pipeline = CFA_ENABLE |
1155 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1156 SUB422_ENABLE | CBC_ENABLE;
1158 isc->try_config.bits_pipeline = 0x0;
1161 case V4L2_PIX_FMT_YUYV:
1162 /* if sensor format is RAW, we convert inside ISC */
1163 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1164 isc->try_config.bits_pipeline = CFA_ENABLE |
1165 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1166 SUB422_ENABLE | CBC_ENABLE;
1168 isc->try_config.bits_pipeline = 0x0;
1171 case V4L2_PIX_FMT_GREY:
1172 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1173 /* if sensor format is RAW, we convert inside ISC */
1174 isc->try_config.bits_pipeline = CFA_ENABLE |
1175 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1178 isc->try_config.bits_pipeline = 0x0;
1182 isc->try_config.bits_pipeline = 0x0;
1187 static void isc_try_fse(struct isc_device *isc,
1188 struct v4l2_subdev_pad_config *pad_cfg)
1191 struct v4l2_subdev_frame_size_enum fse = {};
1194 * If we do not know yet which format the subdev is using, we cannot
1197 if (!isc->try_config.sd_format)
1200 fse.code = isc->try_config.sd_format->mbus_code;
1201 fse.which = V4L2_SUBDEV_FORMAT_TRY;
1203 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1206 * Attempt to obtain format size from subdev. If not available,
1207 * just use the maximum ISC can receive.
1210 pad_cfg->try_crop.width = isc->max_width;
1211 pad_cfg->try_crop.height = isc->max_height;
1213 pad_cfg->try_crop.width = fse.max_width;
1214 pad_cfg->try_crop.height = fse.max_height;
1218 static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
1222 struct isc_format *sd_fmt = NULL, *direct_fmt = NULL;
1223 struct v4l2_pix_format *pixfmt = &f->fmt.pix;
1224 struct v4l2_subdev_pad_config pad_cfg = {};
1225 struct v4l2_subdev_format format = {
1226 .which = V4L2_SUBDEV_FORMAT_TRY,
1230 bool rlp_dma_direct_dump = false;
1232 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1235 /* Step 1: find a RAW format that is supported */
1236 for (i = 0; i < isc->num_user_formats; i++) {
1237 if (ISC_IS_FORMAT_RAW(isc->user_formats[i]->mbus_code)) {
1238 sd_fmt = isc->user_formats[i];
1242 /* Step 2: We can continue with this RAW format, or we can look
1243 * for better: maybe sensor supports directly what we need.
1245 direct_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
1247 /* Step 3: We have both. We decide given the module parameter which
1250 if (direct_fmt && sd_fmt && sensor_preferred)
1251 sd_fmt = direct_fmt;
1253 /* Step 4: we do not have RAW but we have a direct format. Use it. */
1254 if (direct_fmt && !sd_fmt)
1255 sd_fmt = direct_fmt;
1257 /* Step 5: if we are using a direct format, we need to package
1258 * everything as 8 bit data and just dump it
1260 if (sd_fmt == direct_fmt)
1261 rlp_dma_direct_dump = true;
1263 /* Step 6: We have no format. This can happen if the userspace
1264 * requests some weird/invalid format.
1265 * In this case, default to whatever we have
1267 if (!sd_fmt && !direct_fmt) {
1268 sd_fmt = isc->user_formats[isc->num_user_formats - 1];
1269 v4l2_dbg(1, debug, &isc->v4l2_dev,
1270 "Sensor not supporting %.4s, using %.4s\n",
1271 (char *)&pixfmt->pixelformat, (char *)&sd_fmt->fourcc);
1276 goto isc_try_fmt_err;
1279 /* Step 7: Print out what we decided for debugging */
1280 v4l2_dbg(1, debug, &isc->v4l2_dev,
1281 "Preferring to have sensor using format %.4s\n",
1282 (char *)&sd_fmt->fourcc);
1284 /* Step 8: at this moment we decided which format the subdev will use */
1285 isc->try_config.sd_format = sd_fmt;
1287 /* Limit to Atmel ISC hardware capabilities */
1288 if (pixfmt->width > isc->max_width)
1289 pixfmt->width = isc->max_width;
1290 if (pixfmt->height > isc->max_height)
1291 pixfmt->height = isc->max_height;
1294 * The mbus format is the one the subdev outputs.
1295 * The pixels will be transferred in this format Sensor -> ISC
1297 mbus_code = sd_fmt->mbus_code;
1300 * Validate formats. If the required format is not OK, default to raw.
1303 isc->try_config.fourcc = pixfmt->pixelformat;
1305 if (isc_try_validate_formats(isc)) {
1306 pixfmt->pixelformat = isc->try_config.fourcc = sd_fmt->fourcc;
1307 /* Re-try to validate the new format */
1308 ret = isc_try_validate_formats(isc);
1310 goto isc_try_fmt_err;
1313 ret = isc_try_configure_rlp_dma(isc, rlp_dma_direct_dump);
1315 goto isc_try_fmt_err;
1317 ret = isc_try_configure_pipeline(isc);
1319 goto isc_try_fmt_err;
1321 /* Obtain frame sizes if possible to have crop requirements ready */
1322 isc_try_fse(isc, &pad_cfg);
1324 v4l2_fill_mbus_format(&format.format, pixfmt, mbus_code);
1325 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
1328 goto isc_try_fmt_subdev_err;
1330 v4l2_fill_pix_format(pixfmt, &format.format);
1332 /* Limit to Atmel ISC hardware capabilities */
1333 if (pixfmt->width > isc->max_width)
1334 pixfmt->width = isc->max_width;
1335 if (pixfmt->height > isc->max_height)
1336 pixfmt->height = isc->max_height;
1338 pixfmt->field = V4L2_FIELD_NONE;
1339 pixfmt->bytesperline = (pixfmt->width * isc->try_config.bpp) >> 3;
1340 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
1348 v4l2_err(&isc->v4l2_dev, "Could not find any possible format for a working pipeline\n");
1349 isc_try_fmt_subdev_err:
1350 memset(&isc->try_config, 0, sizeof(isc->try_config));
1355 static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
1357 struct v4l2_subdev_format format = {
1358 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1363 ret = isc_try_fmt(isc, f, &mbus_code);
1367 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, mbus_code);
1368 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1369 set_fmt, NULL, &format);
1373 /* Limit to Atmel ISC hardware capabilities */
1374 if (f->fmt.pix.width > isc->max_width)
1375 f->fmt.pix.width = isc->max_width;
1376 if (f->fmt.pix.height > isc->max_height)
1377 f->fmt.pix.height = isc->max_height;
1381 if (isc->try_config.sd_format && isc->config.sd_format &&
1382 isc->try_config.sd_format != isc->config.sd_format) {
1383 isc->ctrls.hist_stat = HIST_INIT;
1384 isc_reset_awb_ctrls(isc);
1385 isc_update_v4l2_ctrls(isc);
1387 /* make the try configuration active */
1388 isc->config = isc->try_config;
1390 v4l2_dbg(1, debug, &isc->v4l2_dev, "New ISC configuration in place\n");
1395 static int isc_s_fmt_vid_cap(struct file *file, void *priv,
1396 struct v4l2_format *f)
1398 struct isc_device *isc = video_drvdata(file);
1400 if (vb2_is_streaming(&isc->vb2_vidq))
1403 return isc_set_fmt(isc, f);
1406 static int isc_try_fmt_vid_cap(struct file *file, void *priv,
1407 struct v4l2_format *f)
1409 struct isc_device *isc = video_drvdata(file);
1411 return isc_try_fmt(isc, f, NULL);
1414 static int isc_enum_input(struct file *file, void *priv,
1415 struct v4l2_input *inp)
1417 if (inp->index != 0)
1420 inp->type = V4L2_INPUT_TYPE_CAMERA;
1422 strscpy(inp->name, "Camera", sizeof(inp->name));
1427 static int isc_g_input(struct file *file, void *priv, unsigned int *i)
1434 static int isc_s_input(struct file *file, void *priv, unsigned int i)
1442 static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1444 struct isc_device *isc = video_drvdata(file);
1446 return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1449 static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1451 struct isc_device *isc = video_drvdata(file);
1453 return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1456 static int isc_enum_framesizes(struct file *file, void *fh,
1457 struct v4l2_frmsizeenum *fsize)
1459 struct isc_device *isc = video_drvdata(file);
1460 struct v4l2_subdev_frame_size_enum fse = {
1461 .code = isc->config.sd_format->mbus_code,
1462 .index = fsize->index,
1463 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1468 for (i = 0; i < isc->num_user_formats; i++)
1469 if (isc->user_formats[i]->fourcc == fsize->pixel_format)
1472 for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1473 if (controller_formats[i].fourcc == fsize->pixel_format)
1479 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1484 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1485 fsize->discrete.width = fse.max_width;
1486 fsize->discrete.height = fse.max_height;
1491 static int isc_enum_frameintervals(struct file *file, void *fh,
1492 struct v4l2_frmivalenum *fival)
1494 struct isc_device *isc = video_drvdata(file);
1495 struct v4l2_subdev_frame_interval_enum fie = {
1496 .code = isc->config.sd_format->mbus_code,
1497 .index = fival->index,
1498 .width = fival->width,
1499 .height = fival->height,
1500 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1505 for (i = 0; i < isc->num_user_formats; i++)
1506 if (isc->user_formats[i]->fourcc == fival->pixel_format)
1509 for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1510 if (controller_formats[i].fourcc == fival->pixel_format)
1516 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1517 enum_frame_interval, NULL, &fie);
1521 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1522 fival->discrete = fie.interval;
1527 static const struct v4l2_ioctl_ops isc_ioctl_ops = {
1528 .vidioc_querycap = isc_querycap,
1529 .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
1530 .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
1531 .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
1532 .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
1534 .vidioc_enum_input = isc_enum_input,
1535 .vidioc_g_input = isc_g_input,
1536 .vidioc_s_input = isc_s_input,
1538 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1539 .vidioc_querybuf = vb2_ioctl_querybuf,
1540 .vidioc_qbuf = vb2_ioctl_qbuf,
1541 .vidioc_expbuf = vb2_ioctl_expbuf,
1542 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1543 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1544 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1545 .vidioc_streamon = vb2_ioctl_streamon,
1546 .vidioc_streamoff = vb2_ioctl_streamoff,
1548 .vidioc_g_parm = isc_g_parm,
1549 .vidioc_s_parm = isc_s_parm,
1550 .vidioc_enum_framesizes = isc_enum_framesizes,
1551 .vidioc_enum_frameintervals = isc_enum_frameintervals,
1553 .vidioc_log_status = v4l2_ctrl_log_status,
1554 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1555 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1558 static int isc_open(struct file *file)
1560 struct isc_device *isc = video_drvdata(file);
1561 struct v4l2_subdev *sd = isc->current_subdev->sd;
1564 if (mutex_lock_interruptible(&isc->lock))
1565 return -ERESTARTSYS;
1567 ret = v4l2_fh_open(file);
1571 if (!v4l2_fh_is_singular_file(file))
1574 ret = v4l2_subdev_call(sd, core, s_power, 1);
1575 if (ret < 0 && ret != -ENOIOCTLCMD) {
1576 v4l2_fh_release(file);
1580 ret = isc_set_fmt(isc, &isc->fmt);
1582 v4l2_subdev_call(sd, core, s_power, 0);
1583 v4l2_fh_release(file);
1587 mutex_unlock(&isc->lock);
1591 static int isc_release(struct file *file)
1593 struct isc_device *isc = video_drvdata(file);
1594 struct v4l2_subdev *sd = isc->current_subdev->sd;
1598 mutex_lock(&isc->lock);
1600 fh_singular = v4l2_fh_is_singular_file(file);
1602 ret = _vb2_fop_release(file, NULL);
1605 v4l2_subdev_call(sd, core, s_power, 0);
1607 mutex_unlock(&isc->lock);
1612 static const struct v4l2_file_operations isc_fops = {
1613 .owner = THIS_MODULE,
1615 .release = isc_release,
1616 .unlocked_ioctl = video_ioctl2,
1617 .read = vb2_fop_read,
1618 .mmap = vb2_fop_mmap,
1619 .poll = vb2_fop_poll,
1622 irqreturn_t isc_interrupt(int irq, void *dev_id)
1624 struct isc_device *isc = (struct isc_device *)dev_id;
1625 struct regmap *regmap = isc->regmap;
1626 u32 isc_intsr, isc_intmask, pending;
1627 irqreturn_t ret = IRQ_NONE;
1629 regmap_read(regmap, ISC_INTSR, &isc_intsr);
1630 regmap_read(regmap, ISC_INTMASK, &isc_intmask);
1632 pending = isc_intsr & isc_intmask;
1634 if (likely(pending & ISC_INT_DDONE)) {
1635 spin_lock(&isc->dma_queue_lock);
1637 struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
1638 struct vb2_buffer *vb = &vbuf->vb2_buf;
1640 vb->timestamp = ktime_get_ns();
1641 vbuf->sequence = isc->sequence++;
1642 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1643 isc->cur_frm = NULL;
1646 if (!list_empty(&isc->dma_queue) && !isc->stop) {
1647 isc->cur_frm = list_first_entry(&isc->dma_queue,
1648 struct isc_buffer, list);
1649 list_del(&isc->cur_frm->list);
1655 complete(&isc->comp);
1658 spin_unlock(&isc->dma_queue_lock);
1661 if (pending & ISC_INT_HISDONE) {
1662 schedule_work(&isc->awb_work);
1669 static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
1671 struct regmap *regmap = isc->regmap;
1672 struct isc_ctrls *ctrls = &isc->ctrls;
1673 u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
1674 u32 *hist_entry = &ctrls->hist_entry[0];
1678 *max = HIST_ENTRIES;
1680 regmap_bulk_read(regmap, ISC_HIS_ENTRY, hist_entry, HIST_ENTRIES);
1684 * we deliberately ignore the end of the histogram,
1685 * the most white pixels
1687 for (i = 1; i < HIST_ENTRIES; i++) {
1688 if (*hist_entry && !*min)
1692 *hist_count += i * (*hist_entry++);
1699 static void isc_wb_update(struct isc_ctrls *ctrls)
1701 u32 *hist_count = &ctrls->hist_count[0];
1704 /* We compute two gains, stretch gain and grey world gain */
1705 u32 s_gain[4], gw_gain[4];
1708 * According to Grey World, we need to set gains for R/B to normalize
1709 * them towards the green channel.
1710 * Thus we want to keep Green as fixed and adjust only Red/Blue
1711 * Compute the average of the both green channels first
1713 avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
1714 (u64)hist_count[ISC_HIS_CFG_MODE_GB];
1717 /* Green histogram is null, nothing to do */
1721 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
1723 * the color offset is the minimum value of the histogram.
1724 * we stretch this color to the full range by substracting
1725 * this value from the color component.
1727 offset[c] = ctrls->hist_minmax[c][HIST_MIN_INDEX];
1729 * The offset is always at least 1. If the offset is 1, we do
1730 * not need to adjust it, so our result must be zero.
1731 * the offset is computed in a histogram on 9 bits (0..512)
1732 * but the offset in register is based on
1733 * 12 bits pipeline (0..4096).
1734 * we need to shift with the 3 bits that the histogram is
1737 ctrls->offset[c] = (offset[c] - 1) << 3;
1740 * the offset is then taken and converted to 2's complements,
1741 * and must be negative, as we subtract this value from the
1744 ctrls->offset[c] = -ctrls->offset[c];
1747 * the stretch gain is the total number of histogram bins
1748 * divided by the actual range of color component (Max - Min)
1749 * If we compute gain like this, the actual color component
1750 * will be stretched to the full histogram.
1751 * We need to shift 9 bits for precision, we have 9 bits for
1754 s_gain[c] = (HIST_ENTRIES << 9) /
1755 (ctrls->hist_minmax[c][HIST_MAX_INDEX] -
1756 ctrls->hist_minmax[c][HIST_MIN_INDEX] + 1);
1759 * Now we have to compute the gain w.r.t. the average.
1760 * Add/lose gain to the component towards the average.
1761 * If it happens that the component is zero, use the
1762 * fixed point value : 1.0 gain.
1765 gw_gain[c] = div_u64(avg << 9, hist_count[c]);
1767 gw_gain[c] = 1 << 9;
1769 /* multiply both gains and adjust for decimals */
1770 ctrls->gain[c] = s_gain[c] * gw_gain[c];
1771 ctrls->gain[c] >>= 9;
1775 static void isc_awb_work(struct work_struct *w)
1777 struct isc_device *isc =
1778 container_of(w, struct isc_device, awb_work);
1779 struct regmap *regmap = isc->regmap;
1780 struct isc_ctrls *ctrls = &isc->ctrls;
1781 u32 hist_id = ctrls->hist_id;
1783 unsigned long flags;
1787 /* streaming is not active anymore */
1791 if (ctrls->hist_stat != HIST_ENABLED)
1794 isc_hist_count(isc, &min, &max);
1795 ctrls->hist_minmax[hist_id][HIST_MIN_INDEX] = min;
1796 ctrls->hist_minmax[hist_id][HIST_MAX_INDEX] = max;
1798 if (hist_id != ISC_HIS_CFG_MODE_B) {
1801 isc_wb_update(ctrls);
1802 hist_id = ISC_HIS_CFG_MODE_GR;
1805 ctrls->hist_id = hist_id;
1806 baysel = isc->config.sd_format->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
1808 ret = pm_runtime_resume_and_get(isc->dev);
1813 * only update if we have all the required histograms and controls
1814 * if awb has been disabled, we need to reset registers as well.
1816 if (hist_id == ISC_HIS_CFG_MODE_GR || ctrls->awb == ISC_WB_NONE) {
1818 * It may happen that DMA Done IRQ will trigger while we are
1819 * updating white balance registers here.
1820 * In that case, only parts of the controls have been updated.
1821 * We can avoid that by locking the section.
1823 spin_lock_irqsave(&isc->awb_lock, flags);
1824 isc_update_awb_ctrls(isc);
1825 spin_unlock_irqrestore(&isc->awb_lock, flags);
1828 * if we are doing just the one time white balance adjustment,
1829 * we are basically done.
1831 if (ctrls->awb == ISC_WB_ONETIME) {
1832 v4l2_info(&isc->v4l2_dev,
1833 "Completed one time white-balance adjustment.\n");
1834 /* update the v4l2 controls values */
1835 isc_update_v4l2_ctrls(isc);
1836 ctrls->awb = ISC_WB_NONE;
1839 regmap_write(regmap, ISC_HIS_CFG, hist_id | baysel | ISC_HIS_CFG_RAR);
1840 isc_update_profile(isc);
1841 /* if awb has been disabled, we don't need to start another histogram */
1843 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
1845 pm_runtime_put_sync(isc->dev);
1848 static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
1850 struct isc_device *isc = container_of(ctrl->handler,
1851 struct isc_device, ctrls.handler);
1852 struct isc_ctrls *ctrls = &isc->ctrls;
1854 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1858 case V4L2_CID_BRIGHTNESS:
1859 ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
1861 case V4L2_CID_CONTRAST:
1862 ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
1864 case V4L2_CID_GAMMA:
1865 ctrls->gamma_index = ctrl->val;
1874 static const struct v4l2_ctrl_ops isc_ctrl_ops = {
1875 .s_ctrl = isc_s_ctrl,
1878 static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
1880 struct isc_device *isc = container_of(ctrl->handler,
1881 struct isc_device, ctrls.handler);
1882 struct isc_ctrls *ctrls = &isc->ctrls;
1884 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1888 case V4L2_CID_AUTO_WHITE_BALANCE:
1890 ctrls->awb = ISC_WB_AUTO;
1892 ctrls->awb = ISC_WB_NONE;
1894 /* we did not configure ISC yet */
1895 if (!isc->config.sd_format)
1898 /* configure the controls with new values from v4l2 */
1899 if (ctrl->cluster[ISC_CTRL_R_GAIN]->is_new)
1900 ctrls->gain[ISC_HIS_CFG_MODE_R] = isc->r_gain_ctrl->val;
1901 if (ctrl->cluster[ISC_CTRL_B_GAIN]->is_new)
1902 ctrls->gain[ISC_HIS_CFG_MODE_B] = isc->b_gain_ctrl->val;
1903 if (ctrl->cluster[ISC_CTRL_GR_GAIN]->is_new)
1904 ctrls->gain[ISC_HIS_CFG_MODE_GR] = isc->gr_gain_ctrl->val;
1905 if (ctrl->cluster[ISC_CTRL_GB_GAIN]->is_new)
1906 ctrls->gain[ISC_HIS_CFG_MODE_GB] = isc->gb_gain_ctrl->val;
1908 if (ctrl->cluster[ISC_CTRL_R_OFF]->is_new)
1909 ctrls->offset[ISC_HIS_CFG_MODE_R] = isc->r_off_ctrl->val;
1910 if (ctrl->cluster[ISC_CTRL_B_OFF]->is_new)
1911 ctrls->offset[ISC_HIS_CFG_MODE_B] = isc->b_off_ctrl->val;
1912 if (ctrl->cluster[ISC_CTRL_GR_OFF]->is_new)
1913 ctrls->offset[ISC_HIS_CFG_MODE_GR] = isc->gr_off_ctrl->val;
1914 if (ctrl->cluster[ISC_CTRL_GB_OFF]->is_new)
1915 ctrls->offset[ISC_HIS_CFG_MODE_GB] = isc->gb_off_ctrl->val;
1917 isc_update_awb_ctrls(isc);
1919 if (vb2_is_streaming(&isc->vb2_vidq)) {
1921 * If we are streaming, we can update profile to
1922 * have the new settings in place.
1924 isc_update_profile(isc);
1927 * The auto cluster will activate automatically this
1928 * control. This has to be deactivated when not
1931 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
1934 /* if we have autowhitebalance on, start histogram procedure */
1935 if (ctrls->awb == ISC_WB_AUTO &&
1936 vb2_is_streaming(&isc->vb2_vidq) &&
1937 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
1938 isc_set_histogram(isc, true);
1941 * for one time whitebalance adjustment, check the button,
1942 * if it's pressed, perform the one time operation.
1944 if (ctrls->awb == ISC_WB_NONE &&
1945 ctrl->cluster[ISC_CTRL_DO_WB]->is_new &&
1946 !(ctrl->cluster[ISC_CTRL_DO_WB]->flags &
1947 V4L2_CTRL_FLAG_INACTIVE)) {
1948 ctrls->awb = ISC_WB_ONETIME;
1949 isc_set_histogram(isc, true);
1950 v4l2_dbg(1, debug, &isc->v4l2_dev,
1951 "One time white-balance started.\n");
1958 static int isc_g_volatile_awb_ctrl(struct v4l2_ctrl *ctrl)
1960 struct isc_device *isc = container_of(ctrl->handler,
1961 struct isc_device, ctrls.handler);
1962 struct isc_ctrls *ctrls = &isc->ctrls;
1965 /* being a cluster, this id will be called for every control */
1966 case V4L2_CID_AUTO_WHITE_BALANCE:
1967 ctrl->cluster[ISC_CTRL_R_GAIN]->val =
1968 ctrls->gain[ISC_HIS_CFG_MODE_R];
1969 ctrl->cluster[ISC_CTRL_B_GAIN]->val =
1970 ctrls->gain[ISC_HIS_CFG_MODE_B];
1971 ctrl->cluster[ISC_CTRL_GR_GAIN]->val =
1972 ctrls->gain[ISC_HIS_CFG_MODE_GR];
1973 ctrl->cluster[ISC_CTRL_GB_GAIN]->val =
1974 ctrls->gain[ISC_HIS_CFG_MODE_GB];
1976 ctrl->cluster[ISC_CTRL_R_OFF]->val =
1977 ctrls->offset[ISC_HIS_CFG_MODE_R];
1978 ctrl->cluster[ISC_CTRL_B_OFF]->val =
1979 ctrls->offset[ISC_HIS_CFG_MODE_B];
1980 ctrl->cluster[ISC_CTRL_GR_OFF]->val =
1981 ctrls->offset[ISC_HIS_CFG_MODE_GR];
1982 ctrl->cluster[ISC_CTRL_GB_OFF]->val =
1983 ctrls->offset[ISC_HIS_CFG_MODE_GB];
1989 static const struct v4l2_ctrl_ops isc_awb_ops = {
1990 .s_ctrl = isc_s_awb_ctrl,
1991 .g_volatile_ctrl = isc_g_volatile_awb_ctrl,
1994 #define ISC_CTRL_OFF(_name, _id, _name_str) \
1995 static const struct v4l2_ctrl_config _name = { \
1996 .ops = &isc_awb_ops, \
1998 .name = _name_str, \
1999 .type = V4L2_CTRL_TYPE_INTEGER, \
2000 .flags = V4L2_CTRL_FLAG_SLIDER, \
2007 ISC_CTRL_OFF(isc_r_off_ctrl, ISC_CID_R_OFFSET, "Red Component Offset");
2008 ISC_CTRL_OFF(isc_b_off_ctrl, ISC_CID_B_OFFSET, "Blue Component Offset");
2009 ISC_CTRL_OFF(isc_gr_off_ctrl, ISC_CID_GR_OFFSET, "Green Red Component Offset");
2010 ISC_CTRL_OFF(isc_gb_off_ctrl, ISC_CID_GB_OFFSET, "Green Blue Component Offset");
2012 #define ISC_CTRL_GAIN(_name, _id, _name_str) \
2013 static const struct v4l2_ctrl_config _name = { \
2014 .ops = &isc_awb_ops, \
2016 .name = _name_str, \
2017 .type = V4L2_CTRL_TYPE_INTEGER, \
2018 .flags = V4L2_CTRL_FLAG_SLIDER, \
2025 ISC_CTRL_GAIN(isc_r_gain_ctrl, ISC_CID_R_GAIN, "Red Component Gain");
2026 ISC_CTRL_GAIN(isc_b_gain_ctrl, ISC_CID_B_GAIN, "Blue Component Gain");
2027 ISC_CTRL_GAIN(isc_gr_gain_ctrl, ISC_CID_GR_GAIN, "Green Red Component Gain");
2028 ISC_CTRL_GAIN(isc_gb_gain_ctrl, ISC_CID_GB_GAIN, "Green Blue Component Gain");
2030 static int isc_ctrl_init(struct isc_device *isc)
2032 const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
2033 struct isc_ctrls *ctrls = &isc->ctrls;
2034 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2037 ctrls->hist_stat = HIST_INIT;
2038 isc_reset_awb_ctrls(isc);
2040 ret = v4l2_ctrl_handler_init(hdl, 13);
2044 ctrls->brightness = 0;
2045 ctrls->contrast = 256;
2047 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
2048 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -2048, 2047, 1, 256);
2049 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, isc->gamma_max, 1,
2051 isc->awb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
2052 V4L2_CID_AUTO_WHITE_BALANCE,
2055 /* do_white_balance is a button, so min,max,step,default are ignored */
2056 isc->do_wb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
2057 V4L2_CID_DO_WHITE_BALANCE,
2060 if (!isc->do_wb_ctrl) {
2062 v4l2_ctrl_handler_free(hdl);
2066 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
2068 isc->r_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_gain_ctrl, NULL);
2069 isc->b_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_gain_ctrl, NULL);
2070 isc->gr_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_gain_ctrl, NULL);
2071 isc->gb_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_gain_ctrl, NULL);
2072 isc->r_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_off_ctrl, NULL);
2073 isc->b_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_off_ctrl, NULL);
2074 isc->gr_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_off_ctrl, NULL);
2075 isc->gb_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_off_ctrl, NULL);
2078 * The cluster is in auto mode with autowhitebalance enabled
2079 * and manual mode otherwise.
2081 v4l2_ctrl_auto_cluster(10, &isc->awb_ctrl, 0, true);
2083 v4l2_ctrl_handler_setup(hdl);
2088 static int isc_async_bound(struct v4l2_async_notifier *notifier,
2089 struct v4l2_subdev *subdev,
2090 struct v4l2_async_subdev *asd)
2092 struct isc_device *isc = container_of(notifier->v4l2_dev,
2093 struct isc_device, v4l2_dev);
2094 struct isc_subdev_entity *subdev_entity =
2095 container_of(notifier, struct isc_subdev_entity, notifier);
2097 if (video_is_registered(&isc->video_dev)) {
2098 v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
2102 subdev_entity->sd = subdev;
2107 static void isc_async_unbind(struct v4l2_async_notifier *notifier,
2108 struct v4l2_subdev *subdev,
2109 struct v4l2_async_subdev *asd)
2111 struct isc_device *isc = container_of(notifier->v4l2_dev,
2112 struct isc_device, v4l2_dev);
2113 cancel_work_sync(&isc->awb_work);
2114 video_unregister_device(&isc->video_dev);
2115 v4l2_ctrl_handler_free(&isc->ctrls.handler);
2118 static struct isc_format *find_format_by_code(unsigned int code, int *index)
2120 struct isc_format *fmt = &formats_list[0];
2123 for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
2124 if (fmt->mbus_code == code) {
2135 static int isc_formats_init(struct isc_device *isc)
2137 struct isc_format *fmt;
2138 struct v4l2_subdev *subdev = isc->current_subdev->sd;
2139 unsigned int num_fmts, i, j;
2140 u32 list_size = ARRAY_SIZE(formats_list);
2141 struct v4l2_subdev_mbus_code_enum mbus_code = {
2142 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2146 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
2147 NULL, &mbus_code)) {
2150 fmt = find_format_by_code(mbus_code.code, &i);
2152 v4l2_warn(&isc->v4l2_dev, "Mbus code %x not supported\n",
2157 fmt->sd_support = true;
2164 isc->num_user_formats = num_fmts;
2165 isc->user_formats = devm_kcalloc(isc->dev,
2166 num_fmts, sizeof(*isc->user_formats),
2168 if (!isc->user_formats)
2171 fmt = &formats_list[0];
2172 for (i = 0, j = 0; i < list_size; i++) {
2173 if (fmt->sd_support)
2174 isc->user_formats[j++] = fmt;
2181 static int isc_set_default_fmt(struct isc_device *isc)
2183 struct v4l2_format f = {
2184 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2187 .height = VGA_HEIGHT,
2188 .field = V4L2_FIELD_NONE,
2189 .pixelformat = isc->user_formats[0]->fourcc,
2194 ret = isc_try_fmt(isc, &f, NULL);
2202 static int isc_async_complete(struct v4l2_async_notifier *notifier)
2204 struct isc_device *isc = container_of(notifier->v4l2_dev,
2205 struct isc_device, v4l2_dev);
2206 struct video_device *vdev = &isc->video_dev;
2207 struct vb2_queue *q = &isc->vb2_vidq;
2210 INIT_WORK(&isc->awb_work, isc_awb_work);
2212 ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
2214 v4l2_err(&isc->v4l2_dev, "Failed to register subdev nodes\n");
2218 isc->current_subdev = container_of(notifier,
2219 struct isc_subdev_entity, notifier);
2220 mutex_init(&isc->lock);
2221 init_completion(&isc->comp);
2223 /* Initialize videobuf2 queue */
2224 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2225 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2227 q->buf_struct_size = sizeof(struct isc_buffer);
2228 q->ops = &isc_vb2_ops;
2229 q->mem_ops = &vb2_dma_contig_memops;
2230 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2231 q->lock = &isc->lock;
2232 q->min_buffers_needed = 1;
2235 ret = vb2_queue_init(q);
2237 v4l2_err(&isc->v4l2_dev,
2238 "vb2_queue_init() failed: %d\n", ret);
2239 goto isc_async_complete_err;
2242 /* Init video dma queues */
2243 INIT_LIST_HEAD(&isc->dma_queue);
2244 spin_lock_init(&isc->dma_queue_lock);
2245 spin_lock_init(&isc->awb_lock);
2247 ret = isc_formats_init(isc);
2249 v4l2_err(&isc->v4l2_dev,
2250 "Init format failed: %d\n", ret);
2251 goto isc_async_complete_err;
2254 ret = isc_set_default_fmt(isc);
2256 v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
2257 goto isc_async_complete_err;
2260 ret = isc_ctrl_init(isc);
2262 v4l2_err(&isc->v4l2_dev, "Init isc ctrols failed: %d\n", ret);
2263 goto isc_async_complete_err;
2266 /* Register video device */
2267 strscpy(vdev->name, "microchip-isc", sizeof(vdev->name));
2268 vdev->release = video_device_release_empty;
2269 vdev->fops = &isc_fops;
2270 vdev->ioctl_ops = &isc_ioctl_ops;
2271 vdev->v4l2_dev = &isc->v4l2_dev;
2272 vdev->vfl_dir = VFL_DIR_RX;
2274 vdev->lock = &isc->lock;
2275 vdev->ctrl_handler = &isc->ctrls.handler;
2276 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
2277 video_set_drvdata(vdev, isc);
2279 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2281 v4l2_err(&isc->v4l2_dev,
2282 "video_register_device failed: %d\n", ret);
2283 goto isc_async_complete_err;
2288 isc_async_complete_err:
2289 mutex_destroy(&isc->lock);
2293 const struct v4l2_async_notifier_operations isc_async_ops = {
2294 .bound = isc_async_bound,
2295 .unbind = isc_async_unbind,
2296 .complete = isc_async_complete,
2299 void isc_subdev_cleanup(struct isc_device *isc)
2301 struct isc_subdev_entity *subdev_entity;
2303 list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
2304 v4l2_async_notifier_unregister(&subdev_entity->notifier);
2305 v4l2_async_notifier_cleanup(&subdev_entity->notifier);
2308 INIT_LIST_HEAD(&isc->subdev_entities);
2311 int isc_pipeline_init(struct isc_device *isc)
2313 struct device *dev = isc->dev;
2314 struct regmap *regmap = isc->regmap;
2315 struct regmap_field *regs;
2318 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
2319 const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
2320 REG_FIELD(ISC_WB_CTRL, 0, 0),
2321 REG_FIELD(ISC_CFA_CTRL, 0, 0),
2322 REG_FIELD(ISC_CC_CTRL, 0, 0),
2323 REG_FIELD(ISC_GAM_CTRL, 0, 0),
2324 REG_FIELD(ISC_GAM_CTRL, 1, 1),
2325 REG_FIELD(ISC_GAM_CTRL, 2, 2),
2326 REG_FIELD(ISC_GAM_CTRL, 3, 3),
2327 REG_FIELD(ISC_CSC_CTRL + isc->offsets.csc, 0, 0),
2328 REG_FIELD(ISC_CBC_CTRL + isc->offsets.cbc, 0, 0),
2329 REG_FIELD(ISC_SUB422_CTRL + isc->offsets.sub422, 0, 0),
2330 REG_FIELD(ISC_SUB420_CTRL + isc->offsets.sub420, 0, 0),
2333 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
2334 regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
2336 return PTR_ERR(regs);
2338 isc->pipeline[i] = regs;
2344 /* regmap configuration */
2345 #define ATMEL_ISC_REG_MAX 0xbfc
2346 const struct regmap_config isc_regmap_config = {
2350 .max_register = ATMEL_ISC_REG_MAX,