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 #define ISC_IS_FORMAT_RAW(mbus_code) \
49 (((mbus_code) & 0xf000) == 0x3000)
51 #define ISC_IS_FORMAT_GREY(mbus_code) \
52 (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
53 (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
55 static inline void isc_update_v4l2_ctrls(struct isc_device *isc)
57 struct isc_ctrls *ctrls = &isc->ctrls;
59 /* In here we set the v4l2 controls w.r.t. our pipeline config */
60 v4l2_ctrl_s_ctrl(isc->r_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_R]);
61 v4l2_ctrl_s_ctrl(isc->b_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_B]);
62 v4l2_ctrl_s_ctrl(isc->gr_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GR]);
63 v4l2_ctrl_s_ctrl(isc->gb_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GB]);
65 v4l2_ctrl_s_ctrl(isc->r_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_R]);
66 v4l2_ctrl_s_ctrl(isc->b_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_B]);
67 v4l2_ctrl_s_ctrl(isc->gr_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GR]);
68 v4l2_ctrl_s_ctrl(isc->gb_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GB]);
71 static inline void isc_update_awb_ctrls(struct isc_device *isc)
73 struct isc_ctrls *ctrls = &isc->ctrls;
75 /* In here we set our actual hw pipeline config */
77 regmap_write(isc->regmap, ISC_WB_O_RGR,
78 ((ctrls->offset[ISC_HIS_CFG_MODE_R])) |
79 ((ctrls->offset[ISC_HIS_CFG_MODE_GR]) << 16));
80 regmap_write(isc->regmap, ISC_WB_O_BGB,
81 ((ctrls->offset[ISC_HIS_CFG_MODE_B])) |
82 ((ctrls->offset[ISC_HIS_CFG_MODE_GB]) << 16));
83 regmap_write(isc->regmap, ISC_WB_G_RGR,
84 ctrls->gain[ISC_HIS_CFG_MODE_R] |
85 (ctrls->gain[ISC_HIS_CFG_MODE_GR] << 16));
86 regmap_write(isc->regmap, ISC_WB_G_BGB,
87 ctrls->gain[ISC_HIS_CFG_MODE_B] |
88 (ctrls->gain[ISC_HIS_CFG_MODE_GB] << 16));
91 static inline void isc_reset_awb_ctrls(struct isc_device *isc)
95 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
96 /* gains have a fixed point at 9 decimals */
97 isc->ctrls.gain[c] = 1 << 9;
98 /* offsets are in 2's complements */
99 isc->ctrls.offset[c] = 0;
103 static int isc_wait_clk_stable(struct clk_hw *hw)
105 struct isc_clk *isc_clk = to_isc_clk(hw);
106 struct regmap *regmap = isc_clk->regmap;
107 unsigned long timeout = jiffies + usecs_to_jiffies(1000);
110 while (time_before(jiffies, timeout)) {
111 regmap_read(regmap, ISC_CLKSR, &status);
112 if (!(status & ISC_CLKSR_SIP))
115 usleep_range(10, 250);
121 static int isc_clk_prepare(struct clk_hw *hw)
123 struct isc_clk *isc_clk = to_isc_clk(hw);
126 ret = pm_runtime_resume_and_get(isc_clk->dev);
130 return isc_wait_clk_stable(hw);
133 static void isc_clk_unprepare(struct clk_hw *hw)
135 struct isc_clk *isc_clk = to_isc_clk(hw);
137 isc_wait_clk_stable(hw);
139 pm_runtime_put_sync(isc_clk->dev);
142 static int isc_clk_enable(struct clk_hw *hw)
144 struct isc_clk *isc_clk = to_isc_clk(hw);
145 u32 id = isc_clk->id;
146 struct regmap *regmap = isc_clk->regmap;
150 dev_dbg(isc_clk->dev, "ISC CLK: %s, id = %d, div = %d, parent id = %d\n",
151 __func__, id, isc_clk->div, isc_clk->parent_id);
153 spin_lock_irqsave(&isc_clk->lock, flags);
154 regmap_update_bits(regmap, ISC_CLKCFG,
155 ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
156 (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
157 (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
159 regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
160 spin_unlock_irqrestore(&isc_clk->lock, flags);
162 regmap_read(regmap, ISC_CLKSR, &status);
163 if (status & ISC_CLK(id))
169 static void isc_clk_disable(struct clk_hw *hw)
171 struct isc_clk *isc_clk = to_isc_clk(hw);
172 u32 id = isc_clk->id;
175 spin_lock_irqsave(&isc_clk->lock, flags);
176 regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
177 spin_unlock_irqrestore(&isc_clk->lock, flags);
180 static int isc_clk_is_enabled(struct clk_hw *hw)
182 struct isc_clk *isc_clk = to_isc_clk(hw);
186 ret = pm_runtime_resume_and_get(isc_clk->dev);
190 regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
192 pm_runtime_put_sync(isc_clk->dev);
194 return status & ISC_CLK(isc_clk->id) ? 1 : 0;
198 isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
200 struct isc_clk *isc_clk = to_isc_clk(hw);
202 return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
205 static int isc_clk_determine_rate(struct clk_hw *hw,
206 struct clk_rate_request *req)
208 struct isc_clk *isc_clk = to_isc_clk(hw);
209 long best_rate = -EINVAL;
213 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
214 struct clk_hw *parent;
215 unsigned long parent_rate;
217 parent = clk_hw_get_parent_by_index(hw, i);
221 parent_rate = clk_hw_get_rate(parent);
225 for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
229 rate = DIV_ROUND_CLOSEST(parent_rate, div);
230 diff = abs(req->rate - rate);
232 if (best_diff < 0 || best_diff > diff) {
235 req->best_parent_rate = parent_rate;
236 req->best_parent_hw = parent;
239 if (!best_diff || rate < req->rate)
247 dev_dbg(isc_clk->dev,
248 "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
250 __clk_get_name((req->best_parent_hw)->clk),
251 req->best_parent_rate);
256 req->rate = best_rate;
261 static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
263 struct isc_clk *isc_clk = to_isc_clk(hw);
265 if (index >= clk_hw_get_num_parents(hw))
268 isc_clk->parent_id = index;
273 static u8 isc_clk_get_parent(struct clk_hw *hw)
275 struct isc_clk *isc_clk = to_isc_clk(hw);
277 return isc_clk->parent_id;
280 static int isc_clk_set_rate(struct clk_hw *hw,
282 unsigned long parent_rate)
284 struct isc_clk *isc_clk = to_isc_clk(hw);
290 div = DIV_ROUND_CLOSEST(parent_rate, rate);
291 if (div > (ISC_CLK_MAX_DIV + 1) || !div)
294 isc_clk->div = div - 1;
299 static const struct clk_ops isc_clk_ops = {
300 .prepare = isc_clk_prepare,
301 .unprepare = isc_clk_unprepare,
302 .enable = isc_clk_enable,
303 .disable = isc_clk_disable,
304 .is_enabled = isc_clk_is_enabled,
305 .recalc_rate = isc_clk_recalc_rate,
306 .determine_rate = isc_clk_determine_rate,
307 .set_parent = isc_clk_set_parent,
308 .get_parent = isc_clk_get_parent,
309 .set_rate = isc_clk_set_rate,
312 static int isc_clk_register(struct isc_device *isc, unsigned int id)
314 struct regmap *regmap = isc->regmap;
315 struct device_node *np = isc->dev->of_node;
316 struct isc_clk *isc_clk;
317 struct clk_init_data init;
318 const char *clk_name = np->name;
319 const char *parent_names[3];
322 if (id == ISC_ISPCK && !isc->ispck_required)
325 num_parents = of_clk_get_parent_count(np);
326 if (num_parents < 1 || num_parents > 3)
329 if (num_parents > 2 && id == ISC_ISPCK)
332 of_clk_parent_fill(np, parent_names, num_parents);
335 of_property_read_string(np, "clock-output-names", &clk_name);
337 clk_name = "isc-ispck";
339 init.parent_names = parent_names;
340 init.num_parents = num_parents;
341 init.name = clk_name;
342 init.ops = &isc_clk_ops;
343 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
345 isc_clk = &isc->isc_clks[id];
346 isc_clk->hw.init = &init;
347 isc_clk->regmap = regmap;
349 isc_clk->dev = isc->dev;
350 spin_lock_init(&isc_clk->lock);
352 isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
353 if (IS_ERR(isc_clk->clk)) {
354 dev_err(isc->dev, "%s: clock register fail\n", clk_name);
355 return PTR_ERR(isc_clk->clk);
356 } else if (id == ISC_MCK)
357 of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
362 int isc_clk_init(struct isc_device *isc)
367 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
368 isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
370 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
371 ret = isc_clk_register(isc, i);
378 EXPORT_SYMBOL_GPL(isc_clk_init);
380 void isc_clk_cleanup(struct isc_device *isc)
384 of_clk_del_provider(isc->dev->of_node);
386 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
387 struct isc_clk *isc_clk = &isc->isc_clks[i];
389 if (!IS_ERR(isc_clk->clk))
390 clk_unregister(isc_clk->clk);
393 EXPORT_SYMBOL_GPL(isc_clk_cleanup);
395 static int isc_queue_setup(struct vb2_queue *vq,
396 unsigned int *nbuffers, unsigned int *nplanes,
397 unsigned int sizes[], struct device *alloc_devs[])
399 struct isc_device *isc = vb2_get_drv_priv(vq);
400 unsigned int size = isc->fmt.fmt.pix.sizeimage;
403 return sizes[0] < size ? -EINVAL : 0;
411 static int isc_buffer_prepare(struct vb2_buffer *vb)
413 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
414 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
415 unsigned long size = isc->fmt.fmt.pix.sizeimage;
417 if (vb2_plane_size(vb, 0) < size) {
418 v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
419 vb2_plane_size(vb, 0), size);
423 vb2_set_plane_payload(vb, 0, size);
425 vbuf->field = isc->fmt.fmt.pix.field;
430 static void isc_start_dma(struct isc_device *isc)
432 struct regmap *regmap = isc->regmap;
433 u32 sizeimage = isc->fmt.fmt.pix.sizeimage;
438 h = isc->fmt.fmt.pix.height;
439 w = isc->fmt.fmt.pix.width;
442 * In case the sensor is not RAW, it will output a pixel (12-16 bits)
443 * with two samples on the ISC Data bus (which is 8-12)
444 * ISC will count each sample, so, we need to multiply these values
445 * by two, to get the real number of samples for the required pixels.
447 if (!ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code)) {
453 * We limit the column/row count that the ISC will output according
454 * to the configured resolution that we want.
455 * This will avoid the situation where the sensor is misconfigured,
456 * sending more data, and the ISC will just take it and DMA to memory,
457 * causing corruption.
459 regmap_write(regmap, ISC_PFE_CFG1,
460 (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK) |
461 (ISC_PFE_CFG1_COLMAX(w - 1) & ISC_PFE_CFG1_COLMAX_MASK));
463 regmap_write(regmap, ISC_PFE_CFG2,
464 (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK) |
465 (ISC_PFE_CFG2_ROWMAX(h - 1) & ISC_PFE_CFG2_ROWMAX_MASK));
467 regmap_update_bits(regmap, ISC_PFE_CFG0,
468 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN,
469 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN);
471 addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
472 regmap_write(regmap, ISC_DAD0 + isc->offsets.dma, addr0);
474 switch (isc->config.fourcc) {
475 case V4L2_PIX_FMT_YUV420:
476 regmap_write(regmap, ISC_DAD1 + isc->offsets.dma,
477 addr0 + (sizeimage * 2) / 3);
478 regmap_write(regmap, ISC_DAD2 + isc->offsets.dma,
479 addr0 + (sizeimage * 5) / 6);
481 case V4L2_PIX_FMT_YUV422P:
482 regmap_write(regmap, ISC_DAD1 + isc->offsets.dma,
483 addr0 + sizeimage / 2);
484 regmap_write(regmap, ISC_DAD2 + isc->offsets.dma,
485 addr0 + (sizeimage * 3) / 4);
491 dctrl_dview = isc->config.dctrl_dview;
493 regmap_write(regmap, ISC_DCTRL + isc->offsets.dma,
494 dctrl_dview | ISC_DCTRL_IE_IS);
495 spin_lock(&isc->awb_lock);
496 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
497 spin_unlock(&isc->awb_lock);
500 static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
502 struct regmap *regmap = isc->regmap;
503 struct isc_ctrls *ctrls = &isc->ctrls;
508 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
509 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
510 val = pipeline & BIT(i) ? 1 : 0;
511 regmap_field_write(isc->pipeline[i], val);
517 bay_cfg = isc->config.sd_format->cfa_baycfg;
519 regmap_write(regmap, ISC_WB_CFG, bay_cfg);
520 isc_update_awb_ctrls(isc);
521 isc_update_v4l2_ctrls(isc);
523 regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
525 gamma = &isc->gamma_table[ctrls->gamma_index][0];
526 regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
527 regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
528 regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
530 isc->config_dpc(isc);
531 isc->config_csc(isc);
532 isc->config_cbc(isc);
534 isc->config_gam(isc);
537 static int isc_update_profile(struct isc_device *isc)
539 struct regmap *regmap = isc->regmap;
543 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
545 regmap_read(regmap, ISC_CTRLSR, &sr);
546 while ((sr & ISC_CTRL_UPPRO) && counter--) {
547 usleep_range(1000, 2000);
548 regmap_read(regmap, ISC_CTRLSR, &sr);
552 v4l2_warn(&isc->v4l2_dev, "Time out to update profile\n");
559 static void isc_set_histogram(struct isc_device *isc, bool enable)
561 struct regmap *regmap = isc->regmap;
562 struct isc_ctrls *ctrls = &isc->ctrls;
565 regmap_write(regmap, ISC_HIS_CFG + isc->offsets.his,
566 ISC_HIS_CFG_MODE_GR |
567 (isc->config.sd_format->cfa_baycfg
568 << ISC_HIS_CFG_BAYSEL_SHIFT) |
570 regmap_write(regmap, ISC_HIS_CTRL + isc->offsets.his,
572 regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
573 ctrls->hist_id = ISC_HIS_CFG_MODE_GR;
574 isc_update_profile(isc);
575 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
577 ctrls->hist_stat = HIST_ENABLED;
579 regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
580 regmap_write(regmap, ISC_HIS_CTRL + isc->offsets.his,
583 ctrls->hist_stat = HIST_DISABLED;
587 static int isc_configure(struct isc_device *isc)
589 struct regmap *regmap = isc->regmap;
590 u32 pfe_cfg0, dcfg, mask, pipeline;
591 struct isc_subdev_entity *subdev = isc->current_subdev;
593 pfe_cfg0 = isc->config.sd_format->pfe_cfg0_bps;
594 pipeline = isc->config.bits_pipeline;
596 dcfg = isc->config.dcfg_imode | isc->dcfg;
598 pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
599 mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
600 ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
601 ISC_PFE_CFG0_MODE_MASK | ISC_PFE_CFG0_CCIR_CRC |
602 ISC_PFE_CFG0_CCIR656 | ISC_PFE_CFG0_MIPI;
604 regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
606 isc->config_rlp(isc);
608 regmap_write(regmap, ISC_DCFG + isc->offsets.dma, dcfg);
610 /* Set the pipeline */
611 isc_set_pipeline(isc, pipeline);
614 * The current implemented histogram is available for RAW R, B, GB, GR
615 * channels. We need to check if sensor is outputting RAW BAYER
617 if (isc->ctrls.awb &&
618 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
619 isc_set_histogram(isc, true);
621 isc_set_histogram(isc, false);
624 return isc_update_profile(isc);
627 static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
629 struct isc_device *isc = vb2_get_drv_priv(vq);
630 struct regmap *regmap = isc->regmap;
631 struct isc_buffer *buf;
635 /* Enable stream on the sub device */
636 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
637 if (ret && ret != -ENOIOCTLCMD) {
638 v4l2_err(&isc->v4l2_dev, "stream on failed in subdev %d\n",
640 goto err_start_stream;
643 ret = pm_runtime_resume_and_get(isc->dev);
645 v4l2_err(&isc->v4l2_dev, "RPM resume failed in subdev %d\n",
650 ret = isc_configure(isc);
654 /* Enable DMA interrupt */
655 regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
657 spin_lock_irqsave(&isc->dma_queue_lock, flags);
661 reinit_completion(&isc->comp);
663 isc->cur_frm = list_first_entry(&isc->dma_queue,
664 struct isc_buffer, list);
665 list_del(&isc->cur_frm->list);
669 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
671 /* if we streaming from RAW, we can do one-shot white balance adj */
672 if (ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
673 v4l2_ctrl_activate(isc->do_wb_ctrl, true);
678 pm_runtime_put_sync(isc->dev);
680 v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
683 spin_lock_irqsave(&isc->dma_queue_lock, flags);
684 list_for_each_entry(buf, &isc->dma_queue, list)
685 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
686 INIT_LIST_HEAD(&isc->dma_queue);
687 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
692 static void isc_stop_streaming(struct vb2_queue *vq)
694 struct isc_device *isc = vb2_get_drv_priv(vq);
696 struct isc_buffer *buf;
699 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
703 /* Wait until the end of the current frame */
704 if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
705 v4l2_err(&isc->v4l2_dev,
706 "Timeout waiting for end of the capture\n");
708 /* Disable DMA interrupt */
709 regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
711 pm_runtime_put_sync(isc->dev);
713 /* Disable stream on the sub device */
714 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
715 if (ret && ret != -ENOIOCTLCMD)
716 v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
718 /* Release all active buffers */
719 spin_lock_irqsave(&isc->dma_queue_lock, flags);
720 if (unlikely(isc->cur_frm)) {
721 vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
722 VB2_BUF_STATE_ERROR);
725 list_for_each_entry(buf, &isc->dma_queue, list)
726 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
727 INIT_LIST_HEAD(&isc->dma_queue);
728 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
731 static void isc_buffer_queue(struct vb2_buffer *vb)
733 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
734 struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
735 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
738 spin_lock_irqsave(&isc->dma_queue_lock, flags);
739 if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
740 vb2_is_streaming(vb->vb2_queue)) {
744 list_add_tail(&buf->list, &isc->dma_queue);
745 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
748 static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
751 unsigned int num_formats = isc->num_user_formats;
752 struct isc_format *fmt;
755 for (i = 0; i < num_formats; i++) {
756 fmt = isc->user_formats[i];
757 if (fmt->fourcc == fourcc)
764 static const struct vb2_ops isc_vb2_ops = {
765 .queue_setup = isc_queue_setup,
766 .wait_prepare = vb2_ops_wait_prepare,
767 .wait_finish = vb2_ops_wait_finish,
768 .buf_prepare = isc_buffer_prepare,
769 .start_streaming = isc_start_streaming,
770 .stop_streaming = isc_stop_streaming,
771 .buf_queue = isc_buffer_queue,
774 static int isc_querycap(struct file *file, void *priv,
775 struct v4l2_capability *cap)
777 struct isc_device *isc = video_drvdata(file);
779 strscpy(cap->driver, "microchip-isc", sizeof(cap->driver));
780 strscpy(cap->card, "Atmel Image Sensor Controller", sizeof(cap->card));
781 snprintf(cap->bus_info, sizeof(cap->bus_info),
782 "platform:%s", isc->v4l2_dev.name);
787 static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
788 struct v4l2_fmtdesc *f)
790 struct isc_device *isc = video_drvdata(file);
791 u32 index = f->index;
792 u32 i, supported_index;
794 if (index < isc->controller_formats_size) {
795 f->pixelformat = isc->controller_formats[index].fourcc;
799 index -= isc->controller_formats_size;
803 for (i = 0; i < isc->formats_list_size; i++) {
804 if (!ISC_IS_FORMAT_RAW(isc->formats_list[i].mbus_code) ||
805 !isc->formats_list[i].sd_support)
807 if (supported_index == index) {
808 f->pixelformat = isc->formats_list[i].fourcc;
817 static int isc_g_fmt_vid_cap(struct file *file, void *priv,
818 struct v4l2_format *fmt)
820 struct isc_device *isc = video_drvdata(file);
828 * Checks the current configured format, if ISC can output it,
829 * considering which type of format the ISC receives from the sensor
831 static int isc_try_validate_formats(struct isc_device *isc)
834 bool bayer = false, yuv = false, rgb = false, grey = false;
836 /* all formats supported by the RLP module are OK */
837 switch (isc->try_config.fourcc) {
838 case V4L2_PIX_FMT_SBGGR8:
839 case V4L2_PIX_FMT_SGBRG8:
840 case V4L2_PIX_FMT_SGRBG8:
841 case V4L2_PIX_FMT_SRGGB8:
842 case V4L2_PIX_FMT_SBGGR10:
843 case V4L2_PIX_FMT_SGBRG10:
844 case V4L2_PIX_FMT_SGRBG10:
845 case V4L2_PIX_FMT_SRGGB10:
846 case V4L2_PIX_FMT_SBGGR12:
847 case V4L2_PIX_FMT_SGBRG12:
848 case V4L2_PIX_FMT_SGRBG12:
849 case V4L2_PIX_FMT_SRGGB12:
854 case V4L2_PIX_FMT_YUV420:
855 case V4L2_PIX_FMT_YUV422P:
856 case V4L2_PIX_FMT_YUYV:
857 case V4L2_PIX_FMT_UYVY:
858 case V4L2_PIX_FMT_VYUY:
863 case V4L2_PIX_FMT_RGB565:
864 case V4L2_PIX_FMT_ABGR32:
865 case V4L2_PIX_FMT_XBGR32:
866 case V4L2_PIX_FMT_ARGB444:
867 case V4L2_PIX_FMT_ARGB555:
871 case V4L2_PIX_FMT_GREY:
872 case V4L2_PIX_FMT_Y10:
873 case V4L2_PIX_FMT_Y16:
878 /* any other different formats are not supported */
881 v4l2_dbg(1, debug, &isc->v4l2_dev,
882 "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
883 rgb, yuv, grey, bayer);
885 /* we cannot output RAW if we do not receive RAW */
886 if ((bayer) && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
889 /* we cannot output GREY if we do not receive RAW/GREY */
890 if (grey && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code) &&
891 !ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code))
898 * Configures the RLP and DMA modules, depending on the output format
899 * configured for the ISC.
900 * If direct_dump == true, just dump raw data 8/16 bits depending on format.
902 static int isc_try_configure_rlp_dma(struct isc_device *isc, bool direct_dump)
904 isc->try_config.rlp_cfg_mode = 0;
906 switch (isc->try_config.fourcc) {
907 case V4L2_PIX_FMT_SBGGR8:
908 case V4L2_PIX_FMT_SGBRG8:
909 case V4L2_PIX_FMT_SGRBG8:
910 case V4L2_PIX_FMT_SRGGB8:
911 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
912 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
913 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
914 isc->try_config.bpp = 8;
916 case V4L2_PIX_FMT_SBGGR10:
917 case V4L2_PIX_FMT_SGBRG10:
918 case V4L2_PIX_FMT_SGRBG10:
919 case V4L2_PIX_FMT_SRGGB10:
920 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10;
921 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
922 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
923 isc->try_config.bpp = 16;
925 case V4L2_PIX_FMT_SBGGR12:
926 case V4L2_PIX_FMT_SGBRG12:
927 case V4L2_PIX_FMT_SGRBG12:
928 case V4L2_PIX_FMT_SRGGB12:
929 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12;
930 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
931 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
932 isc->try_config.bpp = 16;
934 case V4L2_PIX_FMT_RGB565:
935 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565;
936 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
937 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
938 isc->try_config.bpp = 16;
940 case V4L2_PIX_FMT_ARGB444:
941 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444;
942 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
943 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
944 isc->try_config.bpp = 16;
946 case V4L2_PIX_FMT_ARGB555:
947 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555;
948 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
949 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
950 isc->try_config.bpp = 16;
952 case V4L2_PIX_FMT_ABGR32:
953 case V4L2_PIX_FMT_XBGR32:
954 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32;
955 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
956 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
957 isc->try_config.bpp = 32;
959 case V4L2_PIX_FMT_YUV420:
960 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
961 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC420P;
962 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
963 isc->try_config.bpp = 12;
965 case V4L2_PIX_FMT_YUV422P:
966 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
967 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC422P;
968 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
969 isc->try_config.bpp = 16;
971 case V4L2_PIX_FMT_YUYV:
972 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YCYC | ISC_RLP_CFG_YMODE_YUYV;
973 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
974 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
975 isc->try_config.bpp = 16;
977 case V4L2_PIX_FMT_UYVY:
978 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YCYC | ISC_RLP_CFG_YMODE_UYVY;
979 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
980 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
981 isc->try_config.bpp = 16;
983 case V4L2_PIX_FMT_VYUY:
984 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YCYC | ISC_RLP_CFG_YMODE_VYUY;
985 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
986 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
987 isc->try_config.bpp = 16;
989 case V4L2_PIX_FMT_GREY:
990 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8;
991 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
992 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
993 isc->try_config.bpp = 8;
995 case V4L2_PIX_FMT_Y16:
996 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY10 | ISC_RLP_CFG_LSH;
998 case V4L2_PIX_FMT_Y10:
999 isc->try_config.rlp_cfg_mode |= ISC_RLP_CFG_MODE_DATY10;
1000 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1001 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1002 isc->try_config.bpp = 16;
1009 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1010 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1011 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1019 * Configuring pipeline modules, depending on which format the ISC outputs
1020 * and considering which format it has as input from the sensor.
1022 static int isc_try_configure_pipeline(struct isc_device *isc)
1024 switch (isc->try_config.fourcc) {
1025 case V4L2_PIX_FMT_RGB565:
1026 case V4L2_PIX_FMT_ARGB555:
1027 case V4L2_PIX_FMT_ARGB444:
1028 case V4L2_PIX_FMT_ABGR32:
1029 case V4L2_PIX_FMT_XBGR32:
1030 /* if sensor format is RAW, we convert inside ISC */
1031 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1032 isc->try_config.bits_pipeline = CFA_ENABLE |
1033 WB_ENABLE | GAM_ENABLES | DPC_BLCENABLE |
1036 isc->try_config.bits_pipeline = 0x0;
1039 case V4L2_PIX_FMT_YUV420:
1040 /* if sensor format is RAW, we convert inside ISC */
1041 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1042 isc->try_config.bits_pipeline = CFA_ENABLE |
1043 CSC_ENABLE | GAM_ENABLES | WB_ENABLE |
1044 SUB420_ENABLE | SUB422_ENABLE | CBC_ENABLE |
1047 isc->try_config.bits_pipeline = 0x0;
1050 case V4L2_PIX_FMT_YUV422P:
1051 /* if sensor format is RAW, we convert inside ISC */
1052 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1053 isc->try_config.bits_pipeline = CFA_ENABLE |
1054 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1055 SUB422_ENABLE | CBC_ENABLE | DPC_BLCENABLE;
1057 isc->try_config.bits_pipeline = 0x0;
1060 case V4L2_PIX_FMT_YUYV:
1061 case V4L2_PIX_FMT_UYVY:
1062 case V4L2_PIX_FMT_VYUY:
1063 /* if sensor format is RAW, we convert inside ISC */
1064 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1065 isc->try_config.bits_pipeline = CFA_ENABLE |
1066 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1067 SUB422_ENABLE | CBC_ENABLE | DPC_BLCENABLE;
1069 isc->try_config.bits_pipeline = 0x0;
1072 case V4L2_PIX_FMT_GREY:
1073 case V4L2_PIX_FMT_Y16:
1074 /* if sensor format is RAW, we convert inside ISC */
1075 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1076 isc->try_config.bits_pipeline = CFA_ENABLE |
1077 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1078 CBC_ENABLE | DPC_BLCENABLE;
1080 isc->try_config.bits_pipeline = 0x0;
1084 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
1085 isc->try_config.bits_pipeline = WB_ENABLE | DPC_BLCENABLE;
1087 isc->try_config.bits_pipeline = 0x0;
1090 /* Tune the pipeline to product specific */
1091 isc->adapt_pipeline(isc);
1096 static void isc_try_fse(struct isc_device *isc,
1097 struct v4l2_subdev_state *sd_state)
1100 struct v4l2_subdev_frame_size_enum fse = {};
1103 * If we do not know yet which format the subdev is using, we cannot
1106 if (!isc->try_config.sd_format)
1109 fse.code = isc->try_config.sd_format->mbus_code;
1110 fse.which = V4L2_SUBDEV_FORMAT_TRY;
1112 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1115 * Attempt to obtain format size from subdev. If not available,
1116 * just use the maximum ISC can receive.
1119 sd_state->pads->try_crop.width = isc->max_width;
1120 sd_state->pads->try_crop.height = isc->max_height;
1122 sd_state->pads->try_crop.width = fse.max_width;
1123 sd_state->pads->try_crop.height = fse.max_height;
1127 static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
1131 struct isc_format *sd_fmt = NULL, *direct_fmt = NULL;
1132 struct v4l2_pix_format *pixfmt = &f->fmt.pix;
1133 struct v4l2_subdev_pad_config pad_cfg = {};
1134 struct v4l2_subdev_state pad_state = {
1137 struct v4l2_subdev_format format = {
1138 .which = V4L2_SUBDEV_FORMAT_TRY,
1142 bool rlp_dma_direct_dump = false;
1144 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1147 /* Step 1: find a RAW format that is supported */
1148 for (i = 0; i < isc->num_user_formats; i++) {
1149 if (ISC_IS_FORMAT_RAW(isc->user_formats[i]->mbus_code)) {
1150 sd_fmt = isc->user_formats[i];
1154 /* Step 2: We can continue with this RAW format, or we can look
1155 * for better: maybe sensor supports directly what we need.
1157 direct_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
1159 /* Step 3: We have both. We decide given the module parameter which
1162 if (direct_fmt && sd_fmt && sensor_preferred)
1163 sd_fmt = direct_fmt;
1165 /* Step 4: we do not have RAW but we have a direct format. Use it. */
1166 if (direct_fmt && !sd_fmt)
1167 sd_fmt = direct_fmt;
1169 /* Step 5: if we are using a direct format, we need to package
1170 * everything as 8 bit data and just dump it
1172 if (sd_fmt == direct_fmt)
1173 rlp_dma_direct_dump = true;
1175 /* Step 6: We have no format. This can happen if the userspace
1176 * requests some weird/invalid format.
1177 * In this case, default to whatever we have
1179 if (!sd_fmt && !direct_fmt) {
1180 sd_fmt = isc->user_formats[isc->num_user_formats - 1];
1181 v4l2_dbg(1, debug, &isc->v4l2_dev,
1182 "Sensor not supporting %.4s, using %.4s\n",
1183 (char *)&pixfmt->pixelformat, (char *)&sd_fmt->fourcc);
1188 goto isc_try_fmt_err;
1191 /* Step 7: Print out what we decided for debugging */
1192 v4l2_dbg(1, debug, &isc->v4l2_dev,
1193 "Preferring to have sensor using format %.4s\n",
1194 (char *)&sd_fmt->fourcc);
1196 /* Step 8: at this moment we decided which format the subdev will use */
1197 isc->try_config.sd_format = sd_fmt;
1199 /* Limit to Atmel ISC hardware capabilities */
1200 if (pixfmt->width > isc->max_width)
1201 pixfmt->width = isc->max_width;
1202 if (pixfmt->height > isc->max_height)
1203 pixfmt->height = isc->max_height;
1206 * The mbus format is the one the subdev outputs.
1207 * The pixels will be transferred in this format Sensor -> ISC
1209 mbus_code = sd_fmt->mbus_code;
1212 * Validate formats. If the required format is not OK, default to raw.
1215 isc->try_config.fourcc = pixfmt->pixelformat;
1217 if (isc_try_validate_formats(isc)) {
1218 pixfmt->pixelformat = isc->try_config.fourcc = sd_fmt->fourcc;
1219 /* Re-try to validate the new format */
1220 ret = isc_try_validate_formats(isc);
1222 goto isc_try_fmt_err;
1225 ret = isc_try_configure_rlp_dma(isc, rlp_dma_direct_dump);
1227 goto isc_try_fmt_err;
1229 ret = isc_try_configure_pipeline(isc);
1231 goto isc_try_fmt_err;
1233 /* Obtain frame sizes if possible to have crop requirements ready */
1234 isc_try_fse(isc, &pad_state);
1236 v4l2_fill_mbus_format(&format.format, pixfmt, mbus_code);
1237 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
1238 &pad_state, &format);
1240 goto isc_try_fmt_subdev_err;
1242 v4l2_fill_pix_format(pixfmt, &format.format);
1244 /* Limit to Atmel ISC hardware capabilities */
1245 if (pixfmt->width > isc->max_width)
1246 pixfmt->width = isc->max_width;
1247 if (pixfmt->height > isc->max_height)
1248 pixfmt->height = isc->max_height;
1250 pixfmt->field = V4L2_FIELD_NONE;
1251 pixfmt->bytesperline = (pixfmt->width * isc->try_config.bpp) >> 3;
1252 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
1260 v4l2_err(&isc->v4l2_dev, "Could not find any possible format for a working pipeline\n");
1261 isc_try_fmt_subdev_err:
1262 memset(&isc->try_config, 0, sizeof(isc->try_config));
1267 static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
1269 struct v4l2_subdev_format format = {
1270 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1275 ret = isc_try_fmt(isc, f, &mbus_code);
1279 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, mbus_code);
1280 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1281 set_fmt, NULL, &format);
1285 /* Limit to Atmel ISC hardware capabilities */
1286 if (f->fmt.pix.width > isc->max_width)
1287 f->fmt.pix.width = isc->max_width;
1288 if (f->fmt.pix.height > isc->max_height)
1289 f->fmt.pix.height = isc->max_height;
1293 if (isc->try_config.sd_format && isc->config.sd_format &&
1294 isc->try_config.sd_format != isc->config.sd_format) {
1295 isc->ctrls.hist_stat = HIST_INIT;
1296 isc_reset_awb_ctrls(isc);
1297 isc_update_v4l2_ctrls(isc);
1299 /* make the try configuration active */
1300 isc->config = isc->try_config;
1302 v4l2_dbg(1, debug, &isc->v4l2_dev, "New ISC configuration in place\n");
1307 static int isc_s_fmt_vid_cap(struct file *file, void *priv,
1308 struct v4l2_format *f)
1310 struct isc_device *isc = video_drvdata(file);
1312 if (vb2_is_streaming(&isc->vb2_vidq))
1315 return isc_set_fmt(isc, f);
1318 static int isc_try_fmt_vid_cap(struct file *file, void *priv,
1319 struct v4l2_format *f)
1321 struct isc_device *isc = video_drvdata(file);
1323 return isc_try_fmt(isc, f, NULL);
1326 static int isc_enum_input(struct file *file, void *priv,
1327 struct v4l2_input *inp)
1329 if (inp->index != 0)
1332 inp->type = V4L2_INPUT_TYPE_CAMERA;
1334 strscpy(inp->name, "Camera", sizeof(inp->name));
1339 static int isc_g_input(struct file *file, void *priv, unsigned int *i)
1346 static int isc_s_input(struct file *file, void *priv, unsigned int i)
1354 static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1356 struct isc_device *isc = video_drvdata(file);
1358 return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1361 static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1363 struct isc_device *isc = video_drvdata(file);
1365 return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1368 static int isc_enum_framesizes(struct file *file, void *fh,
1369 struct v4l2_frmsizeenum *fsize)
1371 struct isc_device *isc = video_drvdata(file);
1372 struct v4l2_subdev_frame_size_enum fse = {
1373 .code = isc->config.sd_format->mbus_code,
1374 .index = fsize->index,
1375 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1380 for (i = 0; i < isc->num_user_formats; i++)
1381 if (isc->user_formats[i]->fourcc == fsize->pixel_format)
1384 for (i = 0; i < isc->controller_formats_size; i++)
1385 if (isc->controller_formats[i].fourcc == fsize->pixel_format)
1391 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1396 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1397 fsize->discrete.width = fse.max_width;
1398 fsize->discrete.height = fse.max_height;
1403 static int isc_enum_frameintervals(struct file *file, void *fh,
1404 struct v4l2_frmivalenum *fival)
1406 struct isc_device *isc = video_drvdata(file);
1407 struct v4l2_subdev_frame_interval_enum fie = {
1408 .code = isc->config.sd_format->mbus_code,
1409 .index = fival->index,
1410 .width = fival->width,
1411 .height = fival->height,
1412 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1417 for (i = 0; i < isc->num_user_formats; i++)
1418 if (isc->user_formats[i]->fourcc == fival->pixel_format)
1421 for (i = 0; i < isc->controller_formats_size; i++)
1422 if (isc->controller_formats[i].fourcc == fival->pixel_format)
1428 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1429 enum_frame_interval, NULL, &fie);
1433 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1434 fival->discrete = fie.interval;
1439 static const struct v4l2_ioctl_ops isc_ioctl_ops = {
1440 .vidioc_querycap = isc_querycap,
1441 .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
1442 .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
1443 .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
1444 .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
1446 .vidioc_enum_input = isc_enum_input,
1447 .vidioc_g_input = isc_g_input,
1448 .vidioc_s_input = isc_s_input,
1450 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1451 .vidioc_querybuf = vb2_ioctl_querybuf,
1452 .vidioc_qbuf = vb2_ioctl_qbuf,
1453 .vidioc_expbuf = vb2_ioctl_expbuf,
1454 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1455 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1456 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1457 .vidioc_streamon = vb2_ioctl_streamon,
1458 .vidioc_streamoff = vb2_ioctl_streamoff,
1460 .vidioc_g_parm = isc_g_parm,
1461 .vidioc_s_parm = isc_s_parm,
1462 .vidioc_enum_framesizes = isc_enum_framesizes,
1463 .vidioc_enum_frameintervals = isc_enum_frameintervals,
1465 .vidioc_log_status = v4l2_ctrl_log_status,
1466 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1467 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1470 static int isc_open(struct file *file)
1472 struct isc_device *isc = video_drvdata(file);
1473 struct v4l2_subdev *sd = isc->current_subdev->sd;
1476 if (mutex_lock_interruptible(&isc->lock))
1477 return -ERESTARTSYS;
1479 ret = v4l2_fh_open(file);
1483 if (!v4l2_fh_is_singular_file(file))
1486 ret = v4l2_subdev_call(sd, core, s_power, 1);
1487 if (ret < 0 && ret != -ENOIOCTLCMD) {
1488 v4l2_fh_release(file);
1492 ret = isc_set_fmt(isc, &isc->fmt);
1494 v4l2_subdev_call(sd, core, s_power, 0);
1495 v4l2_fh_release(file);
1499 mutex_unlock(&isc->lock);
1503 static int isc_release(struct file *file)
1505 struct isc_device *isc = video_drvdata(file);
1506 struct v4l2_subdev *sd = isc->current_subdev->sd;
1510 mutex_lock(&isc->lock);
1512 fh_singular = v4l2_fh_is_singular_file(file);
1514 ret = _vb2_fop_release(file, NULL);
1517 v4l2_subdev_call(sd, core, s_power, 0);
1519 mutex_unlock(&isc->lock);
1524 static const struct v4l2_file_operations isc_fops = {
1525 .owner = THIS_MODULE,
1527 .release = isc_release,
1528 .unlocked_ioctl = video_ioctl2,
1529 .read = vb2_fop_read,
1530 .mmap = vb2_fop_mmap,
1531 .poll = vb2_fop_poll,
1534 irqreturn_t isc_interrupt(int irq, void *dev_id)
1536 struct isc_device *isc = (struct isc_device *)dev_id;
1537 struct regmap *regmap = isc->regmap;
1538 u32 isc_intsr, isc_intmask, pending;
1539 irqreturn_t ret = IRQ_NONE;
1541 regmap_read(regmap, ISC_INTSR, &isc_intsr);
1542 regmap_read(regmap, ISC_INTMASK, &isc_intmask);
1544 pending = isc_intsr & isc_intmask;
1546 if (likely(pending & ISC_INT_DDONE)) {
1547 spin_lock(&isc->dma_queue_lock);
1549 struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
1550 struct vb2_buffer *vb = &vbuf->vb2_buf;
1552 vb->timestamp = ktime_get_ns();
1553 vbuf->sequence = isc->sequence++;
1554 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1555 isc->cur_frm = NULL;
1558 if (!list_empty(&isc->dma_queue) && !isc->stop) {
1559 isc->cur_frm = list_first_entry(&isc->dma_queue,
1560 struct isc_buffer, list);
1561 list_del(&isc->cur_frm->list);
1567 complete(&isc->comp);
1570 spin_unlock(&isc->dma_queue_lock);
1573 if (pending & ISC_INT_HISDONE) {
1574 schedule_work(&isc->awb_work);
1580 EXPORT_SYMBOL_GPL(isc_interrupt);
1582 static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
1584 struct regmap *regmap = isc->regmap;
1585 struct isc_ctrls *ctrls = &isc->ctrls;
1586 u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
1587 u32 *hist_entry = &ctrls->hist_entry[0];
1591 *max = HIST_ENTRIES;
1593 regmap_bulk_read(regmap, ISC_HIS_ENTRY + isc->offsets.his_entry,
1594 hist_entry, HIST_ENTRIES);
1598 * we deliberately ignore the end of the histogram,
1599 * the most white pixels
1601 for (i = 1; i < HIST_ENTRIES; i++) {
1602 if (*hist_entry && !*min)
1606 *hist_count += i * (*hist_entry++);
1613 static void isc_wb_update(struct isc_ctrls *ctrls)
1615 u32 *hist_count = &ctrls->hist_count[0];
1618 /* We compute two gains, stretch gain and grey world gain */
1619 u32 s_gain[4], gw_gain[4];
1622 * According to Grey World, we need to set gains for R/B to normalize
1623 * them towards the green channel.
1624 * Thus we want to keep Green as fixed and adjust only Red/Blue
1625 * Compute the average of the both green channels first
1627 avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
1628 (u64)hist_count[ISC_HIS_CFG_MODE_GB];
1631 /* Green histogram is null, nothing to do */
1635 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
1637 * the color offset is the minimum value of the histogram.
1638 * we stretch this color to the full range by substracting
1639 * this value from the color component.
1641 offset[c] = ctrls->hist_minmax[c][HIST_MIN_INDEX];
1643 * The offset is always at least 1. If the offset is 1, we do
1644 * not need to adjust it, so our result must be zero.
1645 * the offset is computed in a histogram on 9 bits (0..512)
1646 * but the offset in register is based on
1647 * 12 bits pipeline (0..4096).
1648 * we need to shift with the 3 bits that the histogram is
1651 ctrls->offset[c] = (offset[c] - 1) << 3;
1654 * the offset is then taken and converted to 2's complements,
1655 * and must be negative, as we subtract this value from the
1658 ctrls->offset[c] = -ctrls->offset[c];
1661 * the stretch gain is the total number of histogram bins
1662 * divided by the actual range of color component (Max - Min)
1663 * If we compute gain like this, the actual color component
1664 * will be stretched to the full histogram.
1665 * We need to shift 9 bits for precision, we have 9 bits for
1668 s_gain[c] = (HIST_ENTRIES << 9) /
1669 (ctrls->hist_minmax[c][HIST_MAX_INDEX] -
1670 ctrls->hist_minmax[c][HIST_MIN_INDEX] + 1);
1673 * Now we have to compute the gain w.r.t. the average.
1674 * Add/lose gain to the component towards the average.
1675 * If it happens that the component is zero, use the
1676 * fixed point value : 1.0 gain.
1679 gw_gain[c] = div_u64(avg << 9, hist_count[c]);
1681 gw_gain[c] = 1 << 9;
1683 /* multiply both gains and adjust for decimals */
1684 ctrls->gain[c] = s_gain[c] * gw_gain[c];
1685 ctrls->gain[c] >>= 9;
1689 static void isc_awb_work(struct work_struct *w)
1691 struct isc_device *isc =
1692 container_of(w, struct isc_device, awb_work);
1693 struct regmap *regmap = isc->regmap;
1694 struct isc_ctrls *ctrls = &isc->ctrls;
1695 u32 hist_id = ctrls->hist_id;
1697 unsigned long flags;
1701 /* streaming is not active anymore */
1705 if (ctrls->hist_stat != HIST_ENABLED)
1708 isc_hist_count(isc, &min, &max);
1709 ctrls->hist_minmax[hist_id][HIST_MIN_INDEX] = min;
1710 ctrls->hist_minmax[hist_id][HIST_MAX_INDEX] = max;
1712 if (hist_id != ISC_HIS_CFG_MODE_B) {
1715 isc_wb_update(ctrls);
1716 hist_id = ISC_HIS_CFG_MODE_GR;
1719 ctrls->hist_id = hist_id;
1720 baysel = isc->config.sd_format->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
1722 ret = pm_runtime_resume_and_get(isc->dev);
1727 * only update if we have all the required histograms and controls
1728 * if awb has been disabled, we need to reset registers as well.
1730 if (hist_id == ISC_HIS_CFG_MODE_GR || ctrls->awb == ISC_WB_NONE) {
1732 * It may happen that DMA Done IRQ will trigger while we are
1733 * updating white balance registers here.
1734 * In that case, only parts of the controls have been updated.
1735 * We can avoid that by locking the section.
1737 spin_lock_irqsave(&isc->awb_lock, flags);
1738 isc_update_awb_ctrls(isc);
1739 spin_unlock_irqrestore(&isc->awb_lock, flags);
1742 * if we are doing just the one time white balance adjustment,
1743 * we are basically done.
1745 if (ctrls->awb == ISC_WB_ONETIME) {
1746 v4l2_info(&isc->v4l2_dev,
1747 "Completed one time white-balance adjustment.\n");
1748 /* update the v4l2 controls values */
1749 isc_update_v4l2_ctrls(isc);
1750 ctrls->awb = ISC_WB_NONE;
1753 regmap_write(regmap, ISC_HIS_CFG + isc->offsets.his,
1754 hist_id | baysel | ISC_HIS_CFG_RAR);
1755 isc_update_profile(isc);
1756 /* if awb has been disabled, we don't need to start another histogram */
1758 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
1760 pm_runtime_put_sync(isc->dev);
1763 static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
1765 struct isc_device *isc = container_of(ctrl->handler,
1766 struct isc_device, ctrls.handler);
1767 struct isc_ctrls *ctrls = &isc->ctrls;
1769 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1773 case V4L2_CID_BRIGHTNESS:
1774 ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
1776 case V4L2_CID_CONTRAST:
1777 ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
1779 case V4L2_CID_GAMMA:
1780 ctrls->gamma_index = ctrl->val;
1789 static const struct v4l2_ctrl_ops isc_ctrl_ops = {
1790 .s_ctrl = isc_s_ctrl,
1793 static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
1795 struct isc_device *isc = container_of(ctrl->handler,
1796 struct isc_device, ctrls.handler);
1797 struct isc_ctrls *ctrls = &isc->ctrls;
1799 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1803 case V4L2_CID_AUTO_WHITE_BALANCE:
1805 ctrls->awb = ISC_WB_AUTO;
1807 ctrls->awb = ISC_WB_NONE;
1809 /* we did not configure ISC yet */
1810 if (!isc->config.sd_format)
1813 /* configure the controls with new values from v4l2 */
1814 if (ctrl->cluster[ISC_CTRL_R_GAIN]->is_new)
1815 ctrls->gain[ISC_HIS_CFG_MODE_R] = isc->r_gain_ctrl->val;
1816 if (ctrl->cluster[ISC_CTRL_B_GAIN]->is_new)
1817 ctrls->gain[ISC_HIS_CFG_MODE_B] = isc->b_gain_ctrl->val;
1818 if (ctrl->cluster[ISC_CTRL_GR_GAIN]->is_new)
1819 ctrls->gain[ISC_HIS_CFG_MODE_GR] = isc->gr_gain_ctrl->val;
1820 if (ctrl->cluster[ISC_CTRL_GB_GAIN]->is_new)
1821 ctrls->gain[ISC_HIS_CFG_MODE_GB] = isc->gb_gain_ctrl->val;
1823 if (ctrl->cluster[ISC_CTRL_R_OFF]->is_new)
1824 ctrls->offset[ISC_HIS_CFG_MODE_R] = isc->r_off_ctrl->val;
1825 if (ctrl->cluster[ISC_CTRL_B_OFF]->is_new)
1826 ctrls->offset[ISC_HIS_CFG_MODE_B] = isc->b_off_ctrl->val;
1827 if (ctrl->cluster[ISC_CTRL_GR_OFF]->is_new)
1828 ctrls->offset[ISC_HIS_CFG_MODE_GR] = isc->gr_off_ctrl->val;
1829 if (ctrl->cluster[ISC_CTRL_GB_OFF]->is_new)
1830 ctrls->offset[ISC_HIS_CFG_MODE_GB] = isc->gb_off_ctrl->val;
1832 isc_update_awb_ctrls(isc);
1834 if (vb2_is_streaming(&isc->vb2_vidq)) {
1836 * If we are streaming, we can update profile to
1837 * have the new settings in place.
1839 isc_update_profile(isc);
1842 * The auto cluster will activate automatically this
1843 * control. This has to be deactivated when not
1846 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
1849 /* if we have autowhitebalance on, start histogram procedure */
1850 if (ctrls->awb == ISC_WB_AUTO &&
1851 vb2_is_streaming(&isc->vb2_vidq) &&
1852 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
1853 isc_set_histogram(isc, true);
1856 * for one time whitebalance adjustment, check the button,
1857 * if it's pressed, perform the one time operation.
1859 if (ctrls->awb == ISC_WB_NONE &&
1860 ctrl->cluster[ISC_CTRL_DO_WB]->is_new &&
1861 !(ctrl->cluster[ISC_CTRL_DO_WB]->flags &
1862 V4L2_CTRL_FLAG_INACTIVE)) {
1863 ctrls->awb = ISC_WB_ONETIME;
1864 isc_set_histogram(isc, true);
1865 v4l2_dbg(1, debug, &isc->v4l2_dev,
1866 "One time white-balance started.\n");
1873 static int isc_g_volatile_awb_ctrl(struct v4l2_ctrl *ctrl)
1875 struct isc_device *isc = container_of(ctrl->handler,
1876 struct isc_device, ctrls.handler);
1877 struct isc_ctrls *ctrls = &isc->ctrls;
1880 /* being a cluster, this id will be called for every control */
1881 case V4L2_CID_AUTO_WHITE_BALANCE:
1882 ctrl->cluster[ISC_CTRL_R_GAIN]->val =
1883 ctrls->gain[ISC_HIS_CFG_MODE_R];
1884 ctrl->cluster[ISC_CTRL_B_GAIN]->val =
1885 ctrls->gain[ISC_HIS_CFG_MODE_B];
1886 ctrl->cluster[ISC_CTRL_GR_GAIN]->val =
1887 ctrls->gain[ISC_HIS_CFG_MODE_GR];
1888 ctrl->cluster[ISC_CTRL_GB_GAIN]->val =
1889 ctrls->gain[ISC_HIS_CFG_MODE_GB];
1891 ctrl->cluster[ISC_CTRL_R_OFF]->val =
1892 ctrls->offset[ISC_HIS_CFG_MODE_R];
1893 ctrl->cluster[ISC_CTRL_B_OFF]->val =
1894 ctrls->offset[ISC_HIS_CFG_MODE_B];
1895 ctrl->cluster[ISC_CTRL_GR_OFF]->val =
1896 ctrls->offset[ISC_HIS_CFG_MODE_GR];
1897 ctrl->cluster[ISC_CTRL_GB_OFF]->val =
1898 ctrls->offset[ISC_HIS_CFG_MODE_GB];
1904 static const struct v4l2_ctrl_ops isc_awb_ops = {
1905 .s_ctrl = isc_s_awb_ctrl,
1906 .g_volatile_ctrl = isc_g_volatile_awb_ctrl,
1909 #define ISC_CTRL_OFF(_name, _id, _name_str) \
1910 static const struct v4l2_ctrl_config _name = { \
1911 .ops = &isc_awb_ops, \
1913 .name = _name_str, \
1914 .type = V4L2_CTRL_TYPE_INTEGER, \
1915 .flags = V4L2_CTRL_FLAG_SLIDER, \
1922 ISC_CTRL_OFF(isc_r_off_ctrl, ISC_CID_R_OFFSET, "Red Component Offset");
1923 ISC_CTRL_OFF(isc_b_off_ctrl, ISC_CID_B_OFFSET, "Blue Component Offset");
1924 ISC_CTRL_OFF(isc_gr_off_ctrl, ISC_CID_GR_OFFSET, "Green Red Component Offset");
1925 ISC_CTRL_OFF(isc_gb_off_ctrl, ISC_CID_GB_OFFSET, "Green Blue Component Offset");
1927 #define ISC_CTRL_GAIN(_name, _id, _name_str) \
1928 static const struct v4l2_ctrl_config _name = { \
1929 .ops = &isc_awb_ops, \
1931 .name = _name_str, \
1932 .type = V4L2_CTRL_TYPE_INTEGER, \
1933 .flags = V4L2_CTRL_FLAG_SLIDER, \
1940 ISC_CTRL_GAIN(isc_r_gain_ctrl, ISC_CID_R_GAIN, "Red Component Gain");
1941 ISC_CTRL_GAIN(isc_b_gain_ctrl, ISC_CID_B_GAIN, "Blue Component Gain");
1942 ISC_CTRL_GAIN(isc_gr_gain_ctrl, ISC_CID_GR_GAIN, "Green Red Component Gain");
1943 ISC_CTRL_GAIN(isc_gb_gain_ctrl, ISC_CID_GB_GAIN, "Green Blue Component Gain");
1945 static int isc_ctrl_init(struct isc_device *isc)
1947 const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
1948 struct isc_ctrls *ctrls = &isc->ctrls;
1949 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1952 ctrls->hist_stat = HIST_INIT;
1953 isc_reset_awb_ctrls(isc);
1955 ret = v4l2_ctrl_handler_init(hdl, 13);
1959 /* Initialize product specific controls. For example, contrast */
1960 isc->config_ctrls(isc, ops);
1962 ctrls->brightness = 0;
1964 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
1965 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, isc->gamma_max, 1,
1967 isc->awb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
1968 V4L2_CID_AUTO_WHITE_BALANCE,
1971 /* do_white_balance is a button, so min,max,step,default are ignored */
1972 isc->do_wb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
1973 V4L2_CID_DO_WHITE_BALANCE,
1976 if (!isc->do_wb_ctrl) {
1978 v4l2_ctrl_handler_free(hdl);
1982 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
1984 isc->r_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_gain_ctrl, NULL);
1985 isc->b_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_gain_ctrl, NULL);
1986 isc->gr_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_gain_ctrl, NULL);
1987 isc->gb_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_gain_ctrl, NULL);
1988 isc->r_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_off_ctrl, NULL);
1989 isc->b_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_off_ctrl, NULL);
1990 isc->gr_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_off_ctrl, NULL);
1991 isc->gb_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_off_ctrl, NULL);
1994 * The cluster is in auto mode with autowhitebalance enabled
1995 * and manual mode otherwise.
1997 v4l2_ctrl_auto_cluster(10, &isc->awb_ctrl, 0, true);
1999 v4l2_ctrl_handler_setup(hdl);
2004 static int isc_async_bound(struct v4l2_async_notifier *notifier,
2005 struct v4l2_subdev *subdev,
2006 struct v4l2_async_subdev *asd)
2008 struct isc_device *isc = container_of(notifier->v4l2_dev,
2009 struct isc_device, v4l2_dev);
2010 struct isc_subdev_entity *subdev_entity =
2011 container_of(notifier, struct isc_subdev_entity, notifier);
2013 if (video_is_registered(&isc->video_dev)) {
2014 v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
2018 subdev_entity->sd = subdev;
2023 static void isc_async_unbind(struct v4l2_async_notifier *notifier,
2024 struct v4l2_subdev *subdev,
2025 struct v4l2_async_subdev *asd)
2027 struct isc_device *isc = container_of(notifier->v4l2_dev,
2028 struct isc_device, v4l2_dev);
2029 cancel_work_sync(&isc->awb_work);
2030 video_unregister_device(&isc->video_dev);
2031 v4l2_ctrl_handler_free(&isc->ctrls.handler);
2034 static struct isc_format *find_format_by_code(struct isc_device *isc,
2035 unsigned int code, int *index)
2037 struct isc_format *fmt = &isc->formats_list[0];
2040 for (i = 0; i < isc->formats_list_size; i++) {
2041 if (fmt->mbus_code == code) {
2052 static int isc_formats_init(struct isc_device *isc)
2054 struct isc_format *fmt;
2055 struct v4l2_subdev *subdev = isc->current_subdev->sd;
2056 unsigned int num_fmts, i, j;
2057 u32 list_size = isc->formats_list_size;
2058 struct v4l2_subdev_mbus_code_enum mbus_code = {
2059 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2063 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
2064 NULL, &mbus_code)) {
2067 fmt = find_format_by_code(isc, mbus_code.code, &i);
2069 v4l2_warn(&isc->v4l2_dev, "Mbus code %x not supported\n",
2074 fmt->sd_support = true;
2081 isc->num_user_formats = num_fmts;
2082 isc->user_formats = devm_kcalloc(isc->dev,
2083 num_fmts, sizeof(*isc->user_formats),
2085 if (!isc->user_formats)
2088 fmt = &isc->formats_list[0];
2089 for (i = 0, j = 0; i < list_size; i++) {
2090 if (fmt->sd_support)
2091 isc->user_formats[j++] = fmt;
2098 static int isc_set_default_fmt(struct isc_device *isc)
2100 struct v4l2_format f = {
2101 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2104 .height = VGA_HEIGHT,
2105 .field = V4L2_FIELD_NONE,
2106 .pixelformat = isc->user_formats[0]->fourcc,
2111 ret = isc_try_fmt(isc, &f, NULL);
2119 static int isc_async_complete(struct v4l2_async_notifier *notifier)
2121 struct isc_device *isc = container_of(notifier->v4l2_dev,
2122 struct isc_device, v4l2_dev);
2123 struct video_device *vdev = &isc->video_dev;
2124 struct vb2_queue *q = &isc->vb2_vidq;
2127 INIT_WORK(&isc->awb_work, isc_awb_work);
2129 ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
2131 v4l2_err(&isc->v4l2_dev, "Failed to register subdev nodes\n");
2135 isc->current_subdev = container_of(notifier,
2136 struct isc_subdev_entity, notifier);
2137 mutex_init(&isc->lock);
2138 init_completion(&isc->comp);
2140 /* Initialize videobuf2 queue */
2141 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2142 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2144 q->buf_struct_size = sizeof(struct isc_buffer);
2145 q->ops = &isc_vb2_ops;
2146 q->mem_ops = &vb2_dma_contig_memops;
2147 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2148 q->lock = &isc->lock;
2149 q->min_buffers_needed = 1;
2152 ret = vb2_queue_init(q);
2154 v4l2_err(&isc->v4l2_dev,
2155 "vb2_queue_init() failed: %d\n", ret);
2156 goto isc_async_complete_err;
2159 /* Init video dma queues */
2160 INIT_LIST_HEAD(&isc->dma_queue);
2161 spin_lock_init(&isc->dma_queue_lock);
2162 spin_lock_init(&isc->awb_lock);
2164 ret = isc_formats_init(isc);
2166 v4l2_err(&isc->v4l2_dev,
2167 "Init format failed: %d\n", ret);
2168 goto isc_async_complete_err;
2171 ret = isc_set_default_fmt(isc);
2173 v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
2174 goto isc_async_complete_err;
2177 ret = isc_ctrl_init(isc);
2179 v4l2_err(&isc->v4l2_dev, "Init isc ctrols failed: %d\n", ret);
2180 goto isc_async_complete_err;
2183 /* Register video device */
2184 strscpy(vdev->name, "microchip-isc", sizeof(vdev->name));
2185 vdev->release = video_device_release_empty;
2186 vdev->fops = &isc_fops;
2187 vdev->ioctl_ops = &isc_ioctl_ops;
2188 vdev->v4l2_dev = &isc->v4l2_dev;
2189 vdev->vfl_dir = VFL_DIR_RX;
2191 vdev->lock = &isc->lock;
2192 vdev->ctrl_handler = &isc->ctrls.handler;
2193 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
2194 video_set_drvdata(vdev, isc);
2196 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2198 v4l2_err(&isc->v4l2_dev,
2199 "video_register_device failed: %d\n", ret);
2200 goto isc_async_complete_err;
2205 isc_async_complete_err:
2206 mutex_destroy(&isc->lock);
2210 const struct v4l2_async_notifier_operations isc_async_ops = {
2211 .bound = isc_async_bound,
2212 .unbind = isc_async_unbind,
2213 .complete = isc_async_complete,
2215 EXPORT_SYMBOL_GPL(isc_async_ops);
2217 void isc_subdev_cleanup(struct isc_device *isc)
2219 struct isc_subdev_entity *subdev_entity;
2221 list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
2222 v4l2_async_nf_unregister(&subdev_entity->notifier);
2223 v4l2_async_nf_cleanup(&subdev_entity->notifier);
2226 INIT_LIST_HEAD(&isc->subdev_entities);
2228 EXPORT_SYMBOL_GPL(isc_subdev_cleanup);
2230 int isc_pipeline_init(struct isc_device *isc)
2232 struct device *dev = isc->dev;
2233 struct regmap *regmap = isc->regmap;
2234 struct regmap_field *regs;
2238 * DPCEN-->GDCEN-->BLCEN-->WB-->CFA-->CC-->
2239 * GAM-->VHXS-->CSC-->CBC-->SUB422-->SUB420
2241 const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
2242 REG_FIELD(ISC_DPC_CTRL, 0, 0),
2243 REG_FIELD(ISC_DPC_CTRL, 1, 1),
2244 REG_FIELD(ISC_DPC_CTRL, 2, 2),
2245 REG_FIELD(ISC_WB_CTRL, 0, 0),
2246 REG_FIELD(ISC_CFA_CTRL, 0, 0),
2247 REG_FIELD(ISC_CC_CTRL, 0, 0),
2248 REG_FIELD(ISC_GAM_CTRL, 0, 0),
2249 REG_FIELD(ISC_GAM_CTRL, 1, 1),
2250 REG_FIELD(ISC_GAM_CTRL, 2, 2),
2251 REG_FIELD(ISC_GAM_CTRL, 3, 3),
2252 REG_FIELD(ISC_VHXS_CTRL, 0, 0),
2253 REG_FIELD(ISC_CSC_CTRL + isc->offsets.csc, 0, 0),
2254 REG_FIELD(ISC_CBC_CTRL + isc->offsets.cbc, 0, 0),
2255 REG_FIELD(ISC_SUB422_CTRL + isc->offsets.sub422, 0, 0),
2256 REG_FIELD(ISC_SUB420_CTRL + isc->offsets.sub420, 0, 0),
2259 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
2260 regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
2262 return PTR_ERR(regs);
2264 isc->pipeline[i] = regs;
2269 EXPORT_SYMBOL_GPL(isc_pipeline_init);
2271 /* regmap configuration */
2272 #define ATMEL_ISC_REG_MAX 0xd5c
2273 const struct regmap_config isc_regmap_config = {
2277 .max_register = ATMEL_ISC_REG_MAX,
2279 EXPORT_SYMBOL_GPL(isc_regmap_config);
2281 MODULE_AUTHOR("Songjun Wu");
2282 MODULE_AUTHOR("Eugen Hristev");
2283 MODULE_DESCRIPTION("Atmel ISC common code base");
2284 MODULE_LICENSE("GPL v2");