1 // SPDX-License-Identifier: GPL-2.0-only
7 * Copyright (C) 2006-2010 Nokia Corporation
8 * Copyright (C) 2007-2009 Texas Instruments, Inc.
10 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
14 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
15 * Sakari Ailus <sakari.ailus@iki.fi>
16 * David Cohen <dacohen@gmail.com>
17 * Stanimir Varbanov <svarbanov@mm-sol.com>
18 * Vimarsh Zutshi <vimarsh.zutshi@gmail.com>
19 * Tuukka Toivonen <tuukkat76@gmail.com>
20 * Sergio Aguirre <saaguirre@ti.com>
21 * Antti Koskipaa <akoskipa@gmail.com>
22 * Ivan T. Ivanov <iivanov@mm-sol.com>
23 * RaniSuneela <r-m@ti.com>
24 * Atanas Filipov <afilipov@mm-sol.com>
25 * Gjorgji Rosikopulos <grosikopulos@mm-sol.com>
26 * Hiroshi DOYU <hiroshi.doyu@nokia.com>
27 * Nayden Kanchev <nkanchev@mm-sol.com>
28 * Phil Carmody <ext-phil.2.carmody@nokia.com>
29 * Artem Bityutskiy <artem.bityutskiy@nokia.com>
30 * Dominic Curran <dcurran@ti.com>
31 * Ilkka Myllyperkio <ilkka.myllyperkio@sofica.fi>
32 * Pallavi Kulkarni <p-kulkarni@ti.com>
33 * Vaibhav Hiremath <hvaibhav@ti.com>
34 * Mohit Jalori <mjalori@ti.com>
35 * Sameer Venkatraman <sameerv@ti.com>
36 * Senthilvadivu Guruswamy <svadivu@ti.com>
37 * Thara Gopinath <thara@ti.com>
38 * Toni Leinonen <toni.leinonen@nokia.com>
39 * Troy Laramy <t-laramy@ti.com>
42 #include <asm/cacheflush.h>
44 #include <linux/clk.h>
45 #include <linux/clkdev.h>
46 #include <linux/delay.h>
47 #include <linux/device.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/i2c.h>
50 #include <linux/interrupt.h>
51 #include <linux/mfd/syscon.h>
52 #include <linux/module.h>
53 #include <linux/omap-iommu.h>
54 #include <linux/platform_device.h>
55 #include <linux/property.h>
56 #include <linux/regulator/consumer.h>
57 #include <linux/slab.h>
58 #include <linux/sched.h>
59 #include <linux/vmalloc.h>
61 #ifdef CONFIG_ARM_DMA_USE_IOMMU
62 #include <asm/dma-iommu.h>
65 #include <media/v4l2-common.h>
66 #include <media/v4l2-fwnode.h>
67 #include <media/v4l2-device.h>
68 #include <media/v4l2-mc.h>
73 #include "isppreview.h"
74 #include "ispresizer.h"
80 static unsigned int autoidle;
81 module_param(autoidle, int, 0444);
82 MODULE_PARM_DESC(autoidle, "Enable OMAP3ISP AUTOIDLE support");
84 static void isp_save_ctx(struct isp_device *isp);
86 static void isp_restore_ctx(struct isp_device *isp);
88 static const struct isp_res_mapping isp_res_maps[] = {
90 .isp_rev = ISP_REVISION_2_0,
93 0x0000, /* base, len 0x0070 */
94 0x0400, /* ccp2, len 0x01f0 */
95 0x0600, /* ccdc, len 0x00a8 */
96 0x0a00, /* hist, len 0x0048 */
97 0x0c00, /* h3a, len 0x0060 */
98 0x0e00, /* preview, len 0x00a0 */
99 0x1000, /* resizer, len 0x00ac */
100 0x1200, /* sbl, len 0x00fc */
101 /* second MMIO area */
102 0x0000, /* csi2a, len 0x0170 */
103 0x0170, /* csiphy2, len 0x000c */
105 .phy_type = ISP_PHY_TYPE_3430,
108 .isp_rev = ISP_REVISION_15_0,
110 /* first MMIO area */
111 0x0000, /* base, len 0x0070 */
112 0x0400, /* ccp2, len 0x01f0 */
113 0x0600, /* ccdc, len 0x00a8 */
114 0x0a00, /* hist, len 0x0048 */
115 0x0c00, /* h3a, len 0x0060 */
116 0x0e00, /* preview, len 0x00a0 */
117 0x1000, /* resizer, len 0x00ac */
118 0x1200, /* sbl, len 0x00fc */
119 /* second MMIO area */
120 0x0000, /* csi2a, len 0x0170 (1st area) */
121 0x0170, /* csiphy2, len 0x000c */
122 0x01c0, /* csi2a, len 0x0040 (2nd area) */
123 0x0400, /* csi2c, len 0x0170 (1st area) */
124 0x0570, /* csiphy1, len 0x000c */
125 0x05c0, /* csi2c, len 0x0040 (2nd area) */
127 .phy_type = ISP_PHY_TYPE_3630,
131 /* Structure for saving/restoring ISP module registers */
132 static struct isp_reg isp_reg_list[] = {
133 {OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG, 0},
134 {OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, 0},
135 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL, 0},
140 * omap3isp_flush - Post pending L3 bus writes by doing a register readback
141 * @isp: OMAP3 ISP device
143 * In order to force posting of pending writes, we need to write and
144 * readback the same register, in this case the revision register.
146 * See this link for reference:
147 * http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
149 void omap3isp_flush(struct isp_device *isp)
151 isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
152 isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
155 /* -----------------------------------------------------------------------------
159 #define to_isp_xclk(_hw) container_of(_hw, struct isp_xclk, hw)
161 static void isp_xclk_update(struct isp_xclk *xclk, u32 divider)
165 isp_reg_clr_set(xclk->isp, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
166 ISPTCTRL_CTRL_DIVA_MASK,
167 divider << ISPTCTRL_CTRL_DIVA_SHIFT);
170 isp_reg_clr_set(xclk->isp, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
171 ISPTCTRL_CTRL_DIVB_MASK,
172 divider << ISPTCTRL_CTRL_DIVB_SHIFT);
177 static int isp_xclk_prepare(struct clk_hw *hw)
179 struct isp_xclk *xclk = to_isp_xclk(hw);
181 omap3isp_get(xclk->isp);
186 static void isp_xclk_unprepare(struct clk_hw *hw)
188 struct isp_xclk *xclk = to_isp_xclk(hw);
190 omap3isp_put(xclk->isp);
193 static int isp_xclk_enable(struct clk_hw *hw)
195 struct isp_xclk *xclk = to_isp_xclk(hw);
198 spin_lock_irqsave(&xclk->lock, flags);
199 isp_xclk_update(xclk, xclk->divider);
200 xclk->enabled = true;
201 spin_unlock_irqrestore(&xclk->lock, flags);
206 static void isp_xclk_disable(struct clk_hw *hw)
208 struct isp_xclk *xclk = to_isp_xclk(hw);
211 spin_lock_irqsave(&xclk->lock, flags);
212 isp_xclk_update(xclk, 0);
213 xclk->enabled = false;
214 spin_unlock_irqrestore(&xclk->lock, flags);
217 static unsigned long isp_xclk_recalc_rate(struct clk_hw *hw,
218 unsigned long parent_rate)
220 struct isp_xclk *xclk = to_isp_xclk(hw);
222 return parent_rate / xclk->divider;
225 static u32 isp_xclk_calc_divider(unsigned long *rate, unsigned long parent_rate)
229 if (*rate >= parent_rate) {
231 return ISPTCTRL_CTRL_DIV_BYPASS;
237 divider = DIV_ROUND_CLOSEST(parent_rate, *rate);
238 if (divider >= ISPTCTRL_CTRL_DIV_BYPASS)
239 divider = ISPTCTRL_CTRL_DIV_BYPASS - 1;
241 *rate = parent_rate / divider;
245 static long isp_xclk_round_rate(struct clk_hw *hw, unsigned long rate,
246 unsigned long *parent_rate)
248 isp_xclk_calc_divider(&rate, *parent_rate);
252 static int isp_xclk_set_rate(struct clk_hw *hw, unsigned long rate,
253 unsigned long parent_rate)
255 struct isp_xclk *xclk = to_isp_xclk(hw);
259 divider = isp_xclk_calc_divider(&rate, parent_rate);
261 spin_lock_irqsave(&xclk->lock, flags);
263 xclk->divider = divider;
265 isp_xclk_update(xclk, divider);
267 spin_unlock_irqrestore(&xclk->lock, flags);
269 dev_dbg(xclk->isp->dev, "%s: cam_xclk%c set to %lu Hz (div %u)\n",
270 __func__, xclk->id == ISP_XCLK_A ? 'a' : 'b', rate, divider);
274 static const struct clk_ops isp_xclk_ops = {
275 .prepare = isp_xclk_prepare,
276 .unprepare = isp_xclk_unprepare,
277 .enable = isp_xclk_enable,
278 .disable = isp_xclk_disable,
279 .recalc_rate = isp_xclk_recalc_rate,
280 .round_rate = isp_xclk_round_rate,
281 .set_rate = isp_xclk_set_rate,
284 static const char *isp_xclk_parent_name = "cam_mclk";
286 static struct clk *isp_xclk_src_get(struct of_phandle_args *clkspec, void *data)
288 unsigned int idx = clkspec->args[0];
289 struct isp_device *isp = data;
291 if (idx >= ARRAY_SIZE(isp->xclks))
292 return ERR_PTR(-ENOENT);
294 return isp->xclks[idx].clk;
297 static int isp_xclk_init(struct isp_device *isp)
299 struct device_node *np = isp->dev->of_node;
300 struct clk_init_data init = {};
303 for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i)
304 isp->xclks[i].clk = ERR_PTR(-EINVAL);
306 for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i) {
307 struct isp_xclk *xclk = &isp->xclks[i];
310 xclk->id = i == 0 ? ISP_XCLK_A : ISP_XCLK_B;
312 spin_lock_init(&xclk->lock);
314 init.name = i == 0 ? "cam_xclka" : "cam_xclkb";
315 init.ops = &isp_xclk_ops;
316 init.parent_names = &isp_xclk_parent_name;
317 init.num_parents = 1;
319 xclk->hw.init = &init;
321 * The first argument is NULL in order to avoid circular
322 * reference, as this driver takes reference on the
323 * sensor subdevice modules and the sensors would take
324 * reference on this module through clk_get().
326 xclk->clk = clk_register(NULL, &xclk->hw);
327 if (IS_ERR(xclk->clk))
328 return PTR_ERR(xclk->clk);
332 of_clk_add_provider(np, isp_xclk_src_get, isp);
337 static void isp_xclk_cleanup(struct isp_device *isp)
339 struct device_node *np = isp->dev->of_node;
343 of_clk_del_provider(np);
345 for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i) {
346 struct isp_xclk *xclk = &isp->xclks[i];
348 if (!IS_ERR(xclk->clk))
349 clk_unregister(xclk->clk);
353 /* -----------------------------------------------------------------------------
358 * isp_enable_interrupts - Enable ISP interrupts.
359 * @isp: OMAP3 ISP device
361 static void isp_enable_interrupts(struct isp_device *isp)
363 static const u32 irq = IRQ0ENABLE_CSIA_IRQ
364 | IRQ0ENABLE_CSIB_IRQ
365 | IRQ0ENABLE_CCDC_LSC_PREF_ERR_IRQ
366 | IRQ0ENABLE_CCDC_LSC_DONE_IRQ
367 | IRQ0ENABLE_CCDC_VD0_IRQ
368 | IRQ0ENABLE_CCDC_VD1_IRQ
369 | IRQ0ENABLE_HS_VS_IRQ
370 | IRQ0ENABLE_HIST_DONE_IRQ
371 | IRQ0ENABLE_H3A_AWB_DONE_IRQ
372 | IRQ0ENABLE_H3A_AF_DONE_IRQ
373 | IRQ0ENABLE_PRV_DONE_IRQ
374 | IRQ0ENABLE_RSZ_DONE_IRQ;
376 isp_reg_writel(isp, irq, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
377 isp_reg_writel(isp, irq, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
381 * isp_disable_interrupts - Disable ISP interrupts.
382 * @isp: OMAP3 ISP device
384 static void isp_disable_interrupts(struct isp_device *isp)
386 isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
390 * isp_core_init - ISP core settings
391 * @isp: OMAP3 ISP device
392 * @idle: Consider idle state.
394 * Set the power settings for the ISP and SBL bus and configure the HS/VS
397 * We need to configure the HS/VS interrupt source before interrupts get
398 * enabled, as the sensor might be free-running and the ISP default setting
399 * (HS edge) would put an unnecessary burden on the CPU.
401 static void isp_core_init(struct isp_device *isp, int idle)
404 ((idle ? ISP_SYSCONFIG_MIDLEMODE_SMARTSTANDBY :
405 ISP_SYSCONFIG_MIDLEMODE_FORCESTANDBY) <<
406 ISP_SYSCONFIG_MIDLEMODE_SHIFT) |
407 ((isp->revision == ISP_REVISION_15_0) ?
408 ISP_SYSCONFIG_AUTOIDLE : 0),
409 OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
412 (isp->autoidle ? ISPCTRL_SBL_AUTOIDLE : 0) |
413 ISPCTRL_SYNC_DETECT_VSRISE,
414 OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
418 * Configure the bridge and lane shifter. Valid inputs are
420 * CCDC_INPUT_PARALLEL: Parallel interface
421 * CCDC_INPUT_CSI2A: CSI2a receiver
422 * CCDC_INPUT_CCP2B: CCP2b receiver
423 * CCDC_INPUT_CSI2C: CSI2c receiver
425 * The bridge and lane shifter are configured according to the selected input
426 * and the ISP platform data.
428 void omap3isp_configure_bridge(struct isp_device *isp,
429 enum ccdc_input_entity input,
430 const struct isp_parallel_cfg *parcfg,
431 unsigned int shift, unsigned int bridge)
435 ispctrl_val = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
436 ispctrl_val &= ~ISPCTRL_SHIFT_MASK;
437 ispctrl_val &= ~ISPCTRL_PAR_CLK_POL_INV;
438 ispctrl_val &= ~ISPCTRL_PAR_SER_CLK_SEL_MASK;
439 ispctrl_val &= ~ISPCTRL_PAR_BRIDGE_MASK;
440 ispctrl_val |= bridge;
443 case CCDC_INPUT_PARALLEL:
444 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_PARALLEL;
445 ispctrl_val |= parcfg->clk_pol << ISPCTRL_PAR_CLK_POL_SHIFT;
446 shift += parcfg->data_lane_shift;
449 case CCDC_INPUT_CSI2A:
450 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIA;
453 case CCDC_INPUT_CCP2B:
454 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIB;
457 case CCDC_INPUT_CSI2C:
458 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIC;
465 ispctrl_val |= ((shift/2) << ISPCTRL_SHIFT_SHIFT) & ISPCTRL_SHIFT_MASK;
467 isp_reg_writel(isp, ispctrl_val, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
470 void omap3isp_hist_dma_done(struct isp_device *isp)
472 if (omap3isp_ccdc_busy(&isp->isp_ccdc) ||
473 omap3isp_stat_pcr_busy(&isp->isp_hist)) {
474 /* Histogram cannot be enabled in this frame anymore */
475 atomic_set(&isp->isp_hist.buf_err, 1);
477 "hist: Out of synchronization with CCDC. Ignoring next buffer.\n");
481 static inline void isp_isr_dbg(struct isp_device *isp, u32 irqstatus)
483 static const char *name[] = {
502 "CCDC_LSC_PREFETCH_COMPLETED",
503 "CCDC_LSC_PREFETCH_ERROR",
519 dev_dbg(isp->dev, "ISP IRQ: ");
521 for (i = 0; i < ARRAY_SIZE(name); i++) {
522 if ((1 << i) & irqstatus)
523 printk(KERN_CONT "%s ", name[i]);
525 printk(KERN_CONT "\n");
528 static void isp_isr_sbl(struct isp_device *isp)
530 struct device *dev = isp->dev;
531 struct isp_pipeline *pipe;
535 * Handle shared buffer logic overflows for video buffers.
536 * ISPSBL_PCR_CCDCPRV_2_RSZ_OVF can be safely ignored.
538 sbl_pcr = isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
539 isp_reg_writel(isp, sbl_pcr, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
540 sbl_pcr &= ~ISPSBL_PCR_CCDCPRV_2_RSZ_OVF;
543 dev_dbg(dev, "SBL overflow (PCR = 0x%08x)\n", sbl_pcr);
545 if (sbl_pcr & ISPSBL_PCR_CSIB_WBL_OVF) {
546 pipe = to_isp_pipeline(&isp->isp_ccp2.subdev.entity);
551 if (sbl_pcr & ISPSBL_PCR_CSIA_WBL_OVF) {
552 pipe = to_isp_pipeline(&isp->isp_csi2a.subdev.entity);
557 if (sbl_pcr & ISPSBL_PCR_CCDC_WBL_OVF) {
558 pipe = to_isp_pipeline(&isp->isp_ccdc.subdev.entity);
563 if (sbl_pcr & ISPSBL_PCR_PRV_WBL_OVF) {
564 pipe = to_isp_pipeline(&isp->isp_prev.subdev.entity);
569 if (sbl_pcr & (ISPSBL_PCR_RSZ1_WBL_OVF
570 | ISPSBL_PCR_RSZ2_WBL_OVF
571 | ISPSBL_PCR_RSZ3_WBL_OVF
572 | ISPSBL_PCR_RSZ4_WBL_OVF)) {
573 pipe = to_isp_pipeline(&isp->isp_res.subdev.entity);
578 if (sbl_pcr & ISPSBL_PCR_H3A_AF_WBL_OVF)
579 omap3isp_stat_sbl_overflow(&isp->isp_af);
581 if (sbl_pcr & ISPSBL_PCR_H3A_AEAWB_WBL_OVF)
582 omap3isp_stat_sbl_overflow(&isp->isp_aewb);
586 * isp_isr - Interrupt Service Routine for Camera ISP module.
587 * @irq: Not used currently.
588 * @_isp: Pointer to the OMAP3 ISP device
590 * Handles the corresponding callback if plugged in.
592 static irqreturn_t isp_isr(int irq, void *_isp)
594 static const u32 ccdc_events = IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ |
595 IRQ0STATUS_CCDC_LSC_DONE_IRQ |
596 IRQ0STATUS_CCDC_VD0_IRQ |
597 IRQ0STATUS_CCDC_VD1_IRQ |
598 IRQ0STATUS_HS_VS_IRQ;
599 struct isp_device *isp = _isp;
602 irqstatus = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
603 isp_reg_writel(isp, irqstatus, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
607 if (irqstatus & IRQ0STATUS_CSIA_IRQ)
608 omap3isp_csi2_isr(&isp->isp_csi2a);
610 if (irqstatus & IRQ0STATUS_CSIB_IRQ)
611 omap3isp_ccp2_isr(&isp->isp_ccp2);
613 if (irqstatus & IRQ0STATUS_CCDC_VD0_IRQ) {
614 if (isp->isp_ccdc.output & CCDC_OUTPUT_PREVIEW)
615 omap3isp_preview_isr_frame_sync(&isp->isp_prev);
616 if (isp->isp_ccdc.output & CCDC_OUTPUT_RESIZER)
617 omap3isp_resizer_isr_frame_sync(&isp->isp_res);
618 omap3isp_stat_isr_frame_sync(&isp->isp_aewb);
619 omap3isp_stat_isr_frame_sync(&isp->isp_af);
620 omap3isp_stat_isr_frame_sync(&isp->isp_hist);
623 if (irqstatus & ccdc_events)
624 omap3isp_ccdc_isr(&isp->isp_ccdc, irqstatus & ccdc_events);
626 if (irqstatus & IRQ0STATUS_PRV_DONE_IRQ) {
627 if (isp->isp_prev.output & PREVIEW_OUTPUT_RESIZER)
628 omap3isp_resizer_isr_frame_sync(&isp->isp_res);
629 omap3isp_preview_isr(&isp->isp_prev);
632 if (irqstatus & IRQ0STATUS_RSZ_DONE_IRQ)
633 omap3isp_resizer_isr(&isp->isp_res);
635 if (irqstatus & IRQ0STATUS_H3A_AWB_DONE_IRQ)
636 omap3isp_stat_isr(&isp->isp_aewb);
638 if (irqstatus & IRQ0STATUS_H3A_AF_DONE_IRQ)
639 omap3isp_stat_isr(&isp->isp_af);
641 if (irqstatus & IRQ0STATUS_HIST_DONE_IRQ)
642 omap3isp_stat_isr(&isp->isp_hist);
646 #if defined(DEBUG) && defined(ISP_ISR_DEBUG)
647 isp_isr_dbg(isp, irqstatus);
653 static const struct media_device_ops isp_media_ops = {
654 .link_notify = v4l2_pipeline_link_notify,
657 /* -----------------------------------------------------------------------------
658 * Pipeline stream management
662 * isp_pipeline_enable - Enable streaming on a pipeline
663 * @pipe: ISP pipeline
664 * @mode: Stream mode (single shot or continuous)
666 * Walk the entities chain starting at the pipeline output video node and start
667 * all modules in the chain in the given mode.
669 * Return 0 if successful, or the return value of the failed video::s_stream
670 * operation otherwise.
672 static int isp_pipeline_enable(struct isp_pipeline *pipe,
673 enum isp_pipeline_stream_state mode)
675 struct isp_device *isp = pipe->output->isp;
676 struct media_entity *entity;
677 struct media_pad *pad;
678 struct v4l2_subdev *subdev;
682 /* Refuse to start streaming if an entity included in the pipeline has
683 * crashed. This check must be performed before the loop below to avoid
684 * starting entities if the pipeline won't start anyway (those entities
685 * would then likely fail to stop, making the problem worse).
687 if (media_entity_enum_intersects(&pipe->ent_enum, &isp->crashed))
690 spin_lock_irqsave(&pipe->lock, flags);
691 pipe->state &= ~(ISP_PIPELINE_IDLE_INPUT | ISP_PIPELINE_IDLE_OUTPUT);
692 spin_unlock_irqrestore(&pipe->lock, flags);
694 pipe->do_propagation = false;
696 entity = &pipe->output->video.entity;
698 pad = &entity->pads[0];
699 if (!(pad->flags & MEDIA_PAD_FL_SINK))
702 pad = media_entity_remote_pad(pad);
703 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
706 entity = pad->entity;
707 subdev = media_entity_to_v4l2_subdev(entity);
709 ret = v4l2_subdev_call(subdev, video, s_stream, mode);
710 if (ret < 0 && ret != -ENOIOCTLCMD)
713 if (subdev == &isp->isp_ccdc.subdev) {
714 v4l2_subdev_call(&isp->isp_aewb.subdev, video,
716 v4l2_subdev_call(&isp->isp_af.subdev, video,
718 v4l2_subdev_call(&isp->isp_hist.subdev, video,
720 pipe->do_propagation = true;
727 static int isp_pipeline_wait_resizer(struct isp_device *isp)
729 return omap3isp_resizer_busy(&isp->isp_res);
732 static int isp_pipeline_wait_preview(struct isp_device *isp)
734 return omap3isp_preview_busy(&isp->isp_prev);
737 static int isp_pipeline_wait_ccdc(struct isp_device *isp)
739 return omap3isp_stat_busy(&isp->isp_af)
740 || omap3isp_stat_busy(&isp->isp_aewb)
741 || omap3isp_stat_busy(&isp->isp_hist)
742 || omap3isp_ccdc_busy(&isp->isp_ccdc);
745 #define ISP_STOP_TIMEOUT msecs_to_jiffies(1000)
747 static int isp_pipeline_wait(struct isp_device *isp,
748 int(*busy)(struct isp_device *isp))
750 unsigned long timeout = jiffies + ISP_STOP_TIMEOUT;
752 while (!time_after(jiffies, timeout)) {
761 * isp_pipeline_disable - Disable streaming on a pipeline
762 * @pipe: ISP pipeline
764 * Walk the entities chain starting at the pipeline output video node and stop
765 * all modules in the chain. Wait synchronously for the modules to be stopped if
768 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
769 * can't be stopped (in which case a software reset of the ISP is probably
772 static int isp_pipeline_disable(struct isp_pipeline *pipe)
774 struct isp_device *isp = pipe->output->isp;
775 struct media_entity *entity;
776 struct media_pad *pad;
777 struct v4l2_subdev *subdev;
782 * We need to stop all the modules after CCDC first or they'll
783 * never stop since they may not get a full frame from CCDC.
785 entity = &pipe->output->video.entity;
787 pad = &entity->pads[0];
788 if (!(pad->flags & MEDIA_PAD_FL_SINK))
791 pad = media_entity_remote_pad(pad);
792 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
795 entity = pad->entity;
796 subdev = media_entity_to_v4l2_subdev(entity);
798 if (subdev == &isp->isp_ccdc.subdev) {
799 v4l2_subdev_call(&isp->isp_aewb.subdev,
801 v4l2_subdev_call(&isp->isp_af.subdev,
803 v4l2_subdev_call(&isp->isp_hist.subdev,
807 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
809 if (subdev == &isp->isp_res.subdev)
810 ret |= isp_pipeline_wait(isp, isp_pipeline_wait_resizer);
811 else if (subdev == &isp->isp_prev.subdev)
812 ret |= isp_pipeline_wait(isp, isp_pipeline_wait_preview);
813 else if (subdev == &isp->isp_ccdc.subdev)
814 ret |= isp_pipeline_wait(isp, isp_pipeline_wait_ccdc);
816 /* Handle stop failures. An entity that fails to stop can
817 * usually just be restarted. Flag the stop failure nonetheless
818 * to trigger an ISP reset the next time the device is released,
821 * The preview engine is a special case. A failure to stop can
822 * mean a hardware crash. When that happens the preview engine
823 * won't respond to read/write operations on the L4 bus anymore,
824 * resulting in a bus fault and a kernel oops next time it gets
825 * accessed. Mark it as crashed to prevent pipelines including
826 * it from being started.
829 dev_info(isp->dev, "Unable to stop %s\n", subdev->name);
830 isp->stop_failure = true;
831 if (subdev == &isp->isp_prev.subdev)
832 media_entity_enum_set(&isp->crashed,
834 failure = -ETIMEDOUT;
842 * omap3isp_pipeline_set_stream - Enable/disable streaming on a pipeline
843 * @pipe: ISP pipeline
844 * @state: Stream state (stopped, single shot or continuous)
846 * Set the pipeline to the given stream state. Pipelines can be started in
847 * single-shot or continuous mode.
849 * Return 0 if successful, or the return value of the failed video::s_stream
850 * operation otherwise. The pipeline state is not updated when the operation
851 * fails, except when stopping the pipeline.
853 int omap3isp_pipeline_set_stream(struct isp_pipeline *pipe,
854 enum isp_pipeline_stream_state state)
858 if (state == ISP_PIPELINE_STREAM_STOPPED)
859 ret = isp_pipeline_disable(pipe);
861 ret = isp_pipeline_enable(pipe, state);
863 if (ret == 0 || state == ISP_PIPELINE_STREAM_STOPPED)
864 pipe->stream_state = state;
870 * omap3isp_pipeline_cancel_stream - Cancel stream on a pipeline
871 * @pipe: ISP pipeline
873 * Cancelling a stream mark all buffers on all video nodes in the pipeline as
874 * erroneous and makes sure no new buffer can be queued. This function is called
875 * when a fatal error that prevents any further operation on the pipeline
878 void omap3isp_pipeline_cancel_stream(struct isp_pipeline *pipe)
881 omap3isp_video_cancel_stream(pipe->input);
883 omap3isp_video_cancel_stream(pipe->output);
887 * isp_pipeline_resume - Resume streaming on a pipeline
888 * @pipe: ISP pipeline
890 * Resume video output and input and re-enable pipeline.
892 static void isp_pipeline_resume(struct isp_pipeline *pipe)
894 int singleshot = pipe->stream_state == ISP_PIPELINE_STREAM_SINGLESHOT;
896 omap3isp_video_resume(pipe->output, !singleshot);
898 omap3isp_video_resume(pipe->input, 0);
899 isp_pipeline_enable(pipe, pipe->stream_state);
903 * isp_pipeline_suspend - Suspend streaming on a pipeline
904 * @pipe: ISP pipeline
908 static void isp_pipeline_suspend(struct isp_pipeline *pipe)
910 isp_pipeline_disable(pipe);
914 * isp_pipeline_is_last - Verify if entity has an enabled link to the output
916 * @me: ISP module's media entity
918 * Returns 1 if the entity has an enabled link to the output video node or 0
919 * otherwise. It's true only while pipeline can have no more than one output
922 static int isp_pipeline_is_last(struct media_entity *me)
924 struct isp_pipeline *pipe;
925 struct media_pad *pad;
929 pipe = to_isp_pipeline(me);
930 if (pipe->stream_state == ISP_PIPELINE_STREAM_STOPPED)
932 pad = media_entity_remote_pad(&pipe->output->pad);
933 return pad->entity == me;
937 * isp_suspend_module_pipeline - Suspend pipeline to which belongs the module
938 * @me: ISP module's media entity
940 * Suspend the whole pipeline if module's entity has an enabled link to the
941 * output video node. It works only while pipeline can have no more than one
944 static void isp_suspend_module_pipeline(struct media_entity *me)
946 if (isp_pipeline_is_last(me))
947 isp_pipeline_suspend(to_isp_pipeline(me));
951 * isp_resume_module_pipeline - Resume pipeline to which belongs the module
952 * @me: ISP module's media entity
954 * Resume the whole pipeline if module's entity has an enabled link to the
955 * output video node. It works only while pipeline can have no more than one
958 static void isp_resume_module_pipeline(struct media_entity *me)
960 if (isp_pipeline_is_last(me))
961 isp_pipeline_resume(to_isp_pipeline(me));
965 * isp_suspend_modules - Suspend ISP submodules.
966 * @isp: OMAP3 ISP device
968 * Returns 0 if suspend left in idle state all the submodules properly,
969 * or returns 1 if a general Reset is required to suspend the submodules.
971 static int __maybe_unused isp_suspend_modules(struct isp_device *isp)
973 unsigned long timeout;
975 omap3isp_stat_suspend(&isp->isp_aewb);
976 omap3isp_stat_suspend(&isp->isp_af);
977 omap3isp_stat_suspend(&isp->isp_hist);
978 isp_suspend_module_pipeline(&isp->isp_res.subdev.entity);
979 isp_suspend_module_pipeline(&isp->isp_prev.subdev.entity);
980 isp_suspend_module_pipeline(&isp->isp_ccdc.subdev.entity);
981 isp_suspend_module_pipeline(&isp->isp_csi2a.subdev.entity);
982 isp_suspend_module_pipeline(&isp->isp_ccp2.subdev.entity);
984 timeout = jiffies + ISP_STOP_TIMEOUT;
985 while (omap3isp_stat_busy(&isp->isp_af)
986 || omap3isp_stat_busy(&isp->isp_aewb)
987 || omap3isp_stat_busy(&isp->isp_hist)
988 || omap3isp_preview_busy(&isp->isp_prev)
989 || omap3isp_resizer_busy(&isp->isp_res)
990 || omap3isp_ccdc_busy(&isp->isp_ccdc)) {
991 if (time_after(jiffies, timeout)) {
992 dev_info(isp->dev, "can't stop modules.\n");
1002 * isp_resume_modules - Resume ISP submodules.
1003 * @isp: OMAP3 ISP device
1005 static void __maybe_unused isp_resume_modules(struct isp_device *isp)
1007 omap3isp_stat_resume(&isp->isp_aewb);
1008 omap3isp_stat_resume(&isp->isp_af);
1009 omap3isp_stat_resume(&isp->isp_hist);
1010 isp_resume_module_pipeline(&isp->isp_res.subdev.entity);
1011 isp_resume_module_pipeline(&isp->isp_prev.subdev.entity);
1012 isp_resume_module_pipeline(&isp->isp_ccdc.subdev.entity);
1013 isp_resume_module_pipeline(&isp->isp_csi2a.subdev.entity);
1014 isp_resume_module_pipeline(&isp->isp_ccp2.subdev.entity);
1018 * isp_reset - Reset ISP with a timeout wait for idle.
1019 * @isp: OMAP3 ISP device
1021 static int isp_reset(struct isp_device *isp)
1023 unsigned long timeout = 0;
1026 isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG)
1027 | ISP_SYSCONFIG_SOFTRESET,
1028 OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
1029 while (!(isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN,
1030 ISP_SYSSTATUS) & 0x1)) {
1031 if (timeout++ > 10000) {
1032 dev_alert(isp->dev, "cannot reset ISP\n");
1038 isp->stop_failure = false;
1039 media_entity_enum_zero(&isp->crashed);
1044 * isp_save_context - Saves the values of the ISP module registers.
1045 * @isp: OMAP3 ISP device
1046 * @reg_list: Structure containing pairs of register address and value to
1050 isp_save_context(struct isp_device *isp, struct isp_reg *reg_list)
1052 struct isp_reg *next = reg_list;
1054 for (; next->reg != ISP_TOK_TERM; next++)
1055 next->val = isp_reg_readl(isp, next->mmio_range, next->reg);
1059 * isp_restore_context - Restores the values of the ISP module registers.
1060 * @isp: OMAP3 ISP device
1061 * @reg_list: Structure containing pairs of register address and value to
1065 isp_restore_context(struct isp_device *isp, struct isp_reg *reg_list)
1067 struct isp_reg *next = reg_list;
1069 for (; next->reg != ISP_TOK_TERM; next++)
1070 isp_reg_writel(isp, next->val, next->mmio_range, next->reg);
1074 * isp_save_ctx - Saves ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
1075 * @isp: OMAP3 ISP device
1077 * Routine for saving the context of each module in the ISP.
1078 * CCDC, HIST, H3A, PREV, RESZ and MMU.
1080 static void isp_save_ctx(struct isp_device *isp)
1082 isp_save_context(isp, isp_reg_list);
1083 omap_iommu_save_ctx(isp->dev);
1087 * isp_restore_ctx - Restores ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
1088 * @isp: OMAP3 ISP device
1090 * Routine for restoring the context of each module in the ISP.
1091 * CCDC, HIST, H3A, PREV, RESZ and MMU.
1093 static void isp_restore_ctx(struct isp_device *isp)
1095 isp_restore_context(isp, isp_reg_list);
1096 omap_iommu_restore_ctx(isp->dev);
1097 omap3isp_ccdc_restore_context(isp);
1098 omap3isp_preview_restore_context(isp);
1101 /* -----------------------------------------------------------------------------
1102 * SBL resources management
1104 #define OMAP3_ISP_SBL_READ (OMAP3_ISP_SBL_CSI1_READ | \
1105 OMAP3_ISP_SBL_CCDC_LSC_READ | \
1106 OMAP3_ISP_SBL_PREVIEW_READ | \
1107 OMAP3_ISP_SBL_RESIZER_READ)
1108 #define OMAP3_ISP_SBL_WRITE (OMAP3_ISP_SBL_CSI1_WRITE | \
1109 OMAP3_ISP_SBL_CSI2A_WRITE | \
1110 OMAP3_ISP_SBL_CSI2C_WRITE | \
1111 OMAP3_ISP_SBL_CCDC_WRITE | \
1112 OMAP3_ISP_SBL_PREVIEW_WRITE)
1114 void omap3isp_sbl_enable(struct isp_device *isp, enum isp_sbl_resource res)
1118 isp->sbl_resources |= res;
1120 if (isp->sbl_resources & OMAP3_ISP_SBL_CSI1_READ)
1121 sbl |= ISPCTRL_SBL_SHARED_RPORTA;
1123 if (isp->sbl_resources & OMAP3_ISP_SBL_CCDC_LSC_READ)
1124 sbl |= ISPCTRL_SBL_SHARED_RPORTB;
1126 if (isp->sbl_resources & OMAP3_ISP_SBL_CSI2C_WRITE)
1127 sbl |= ISPCTRL_SBL_SHARED_WPORTC;
1129 if (isp->sbl_resources & OMAP3_ISP_SBL_RESIZER_WRITE)
1130 sbl |= ISPCTRL_SBL_WR0_RAM_EN;
1132 if (isp->sbl_resources & OMAP3_ISP_SBL_WRITE)
1133 sbl |= ISPCTRL_SBL_WR1_RAM_EN;
1135 if (isp->sbl_resources & OMAP3_ISP_SBL_READ)
1136 sbl |= ISPCTRL_SBL_RD_RAM_EN;
1138 isp_reg_set(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, sbl);
1141 void omap3isp_sbl_disable(struct isp_device *isp, enum isp_sbl_resource res)
1145 isp->sbl_resources &= ~res;
1147 if (!(isp->sbl_resources & OMAP3_ISP_SBL_CSI1_READ))
1148 sbl |= ISPCTRL_SBL_SHARED_RPORTA;
1150 if (!(isp->sbl_resources & OMAP3_ISP_SBL_CCDC_LSC_READ))
1151 sbl |= ISPCTRL_SBL_SHARED_RPORTB;
1153 if (!(isp->sbl_resources & OMAP3_ISP_SBL_CSI2C_WRITE))
1154 sbl |= ISPCTRL_SBL_SHARED_WPORTC;
1156 if (!(isp->sbl_resources & OMAP3_ISP_SBL_RESIZER_WRITE))
1157 sbl |= ISPCTRL_SBL_WR0_RAM_EN;
1159 if (!(isp->sbl_resources & OMAP3_ISP_SBL_WRITE))
1160 sbl |= ISPCTRL_SBL_WR1_RAM_EN;
1162 if (!(isp->sbl_resources & OMAP3_ISP_SBL_READ))
1163 sbl |= ISPCTRL_SBL_RD_RAM_EN;
1165 isp_reg_clr(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, sbl);
1169 * isp_module_sync_idle - Helper to sync module with its idle state
1170 * @me: ISP submodule's media entity
1171 * @wait: ISP submodule's wait queue for streamoff/interrupt synchronization
1172 * @stopping: flag which tells module wants to stop
1174 * This function checks if ISP submodule needs to wait for next interrupt. If
1175 * yes, makes the caller to sleep while waiting for such event.
1177 int omap3isp_module_sync_idle(struct media_entity *me, wait_queue_head_t *wait,
1180 struct isp_pipeline *pipe = to_isp_pipeline(me);
1182 if (pipe->stream_state == ISP_PIPELINE_STREAM_STOPPED ||
1183 (pipe->stream_state == ISP_PIPELINE_STREAM_SINGLESHOT &&
1184 !isp_pipeline_ready(pipe)))
1188 * atomic_set() doesn't include memory barrier on ARM platform for SMP
1189 * scenario. We'll call it here to avoid race conditions.
1191 atomic_set(stopping, 1);
1195 * If module is the last one, it's writing to memory. In this case,
1196 * it's necessary to check if the module is already paused due to
1197 * DMA queue underrun or if it has to wait for next interrupt to be
1199 * If it isn't the last one, the function won't sleep but *stopping
1200 * will still be set to warn next submodule caller's interrupt the
1201 * module wants to be idle.
1203 if (isp_pipeline_is_last(me)) {
1204 struct isp_video *video = pipe->output;
1205 unsigned long flags;
1206 spin_lock_irqsave(&video->irqlock, flags);
1207 if (video->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) {
1208 spin_unlock_irqrestore(&video->irqlock, flags);
1209 atomic_set(stopping, 0);
1213 spin_unlock_irqrestore(&video->irqlock, flags);
1214 if (!wait_event_timeout(*wait, !atomic_read(stopping),
1215 msecs_to_jiffies(1000))) {
1216 atomic_set(stopping, 0);
1226 * omap3isp_module_sync_is_stopping - Helper to verify if module was stopping
1227 * @wait: ISP submodule's wait queue for streamoff/interrupt synchronization
1228 * @stopping: flag which tells module wants to stop
1230 * This function checks if ISP submodule was stopping. In case of yes, it
1231 * notices the caller by setting stopping to 0 and waking up the wait queue.
1232 * Returns 1 if it was stopping or 0 otherwise.
1234 int omap3isp_module_sync_is_stopping(wait_queue_head_t *wait,
1237 if (atomic_cmpxchg(stopping, 1, 0)) {
1245 /* --------------------------------------------------------------------------
1249 #define ISPCTRL_CLKS_MASK (ISPCTRL_H3A_CLK_EN | \
1250 ISPCTRL_HIST_CLK_EN | \
1251 ISPCTRL_RSZ_CLK_EN | \
1252 (ISPCTRL_CCDC_CLK_EN | ISPCTRL_CCDC_RAM_EN) | \
1253 (ISPCTRL_PREV_CLK_EN | ISPCTRL_PREV_RAM_EN))
1255 static void __isp_subclk_update(struct isp_device *isp)
1259 /* AEWB and AF share the same clock. */
1260 if (isp->subclk_resources &
1261 (OMAP3_ISP_SUBCLK_AEWB | OMAP3_ISP_SUBCLK_AF))
1262 clk |= ISPCTRL_H3A_CLK_EN;
1264 if (isp->subclk_resources & OMAP3_ISP_SUBCLK_HIST)
1265 clk |= ISPCTRL_HIST_CLK_EN;
1267 if (isp->subclk_resources & OMAP3_ISP_SUBCLK_RESIZER)
1268 clk |= ISPCTRL_RSZ_CLK_EN;
1270 /* NOTE: For CCDC & Preview submodules, we need to affect internal
1273 if (isp->subclk_resources & OMAP3_ISP_SUBCLK_CCDC)
1274 clk |= ISPCTRL_CCDC_CLK_EN | ISPCTRL_CCDC_RAM_EN;
1276 if (isp->subclk_resources & OMAP3_ISP_SUBCLK_PREVIEW)
1277 clk |= ISPCTRL_PREV_CLK_EN | ISPCTRL_PREV_RAM_EN;
1279 isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
1280 ISPCTRL_CLKS_MASK, clk);
1283 void omap3isp_subclk_enable(struct isp_device *isp,
1284 enum isp_subclk_resource res)
1286 isp->subclk_resources |= res;
1288 __isp_subclk_update(isp);
1291 void omap3isp_subclk_disable(struct isp_device *isp,
1292 enum isp_subclk_resource res)
1294 isp->subclk_resources &= ~res;
1296 __isp_subclk_update(isp);
1300 * isp_enable_clocks - Enable ISP clocks
1301 * @isp: OMAP3 ISP device
1303 * Return 0 if successful, or clk_prepare_enable return value if any of them
1306 static int isp_enable_clocks(struct isp_device *isp)
1311 r = clk_prepare_enable(isp->clock[ISP_CLK_CAM_ICK]);
1313 dev_err(isp->dev, "failed to enable cam_ick clock\n");
1314 goto out_clk_enable_ick;
1316 r = clk_set_rate(isp->clock[ISP_CLK_CAM_MCLK], CM_CAM_MCLK_HZ);
1318 dev_err(isp->dev, "clk_set_rate for cam_mclk failed\n");
1319 goto out_clk_enable_mclk;
1321 r = clk_prepare_enable(isp->clock[ISP_CLK_CAM_MCLK]);
1323 dev_err(isp->dev, "failed to enable cam_mclk clock\n");
1324 goto out_clk_enable_mclk;
1326 rate = clk_get_rate(isp->clock[ISP_CLK_CAM_MCLK]);
1327 if (rate != CM_CAM_MCLK_HZ)
1328 dev_warn(isp->dev, "unexpected cam_mclk rate:\n"
1330 " actual : %ld\n", CM_CAM_MCLK_HZ, rate);
1331 r = clk_prepare_enable(isp->clock[ISP_CLK_CSI2_FCK]);
1333 dev_err(isp->dev, "failed to enable csi2_fck clock\n");
1334 goto out_clk_enable_csi2_fclk;
1338 out_clk_enable_csi2_fclk:
1339 clk_disable_unprepare(isp->clock[ISP_CLK_CAM_MCLK]);
1340 out_clk_enable_mclk:
1341 clk_disable_unprepare(isp->clock[ISP_CLK_CAM_ICK]);
1347 * isp_disable_clocks - Disable ISP clocks
1348 * @isp: OMAP3 ISP device
1350 static void isp_disable_clocks(struct isp_device *isp)
1352 clk_disable_unprepare(isp->clock[ISP_CLK_CAM_ICK]);
1353 clk_disable_unprepare(isp->clock[ISP_CLK_CAM_MCLK]);
1354 clk_disable_unprepare(isp->clock[ISP_CLK_CSI2_FCK]);
1357 static const char *isp_clocks[] = {
1364 static int isp_get_clocks(struct isp_device *isp)
1369 for (i = 0; i < ARRAY_SIZE(isp_clocks); ++i) {
1370 clk = devm_clk_get(isp->dev, isp_clocks[i]);
1372 dev_err(isp->dev, "clk_get %s failed\n", isp_clocks[i]);
1373 return PTR_ERR(clk);
1376 isp->clock[i] = clk;
1383 * omap3isp_get - Acquire the ISP resource.
1385 * Initializes the clocks for the first acquire.
1387 * Increment the reference count on the ISP. If the first reference is taken,
1388 * enable clocks and power-up all submodules.
1390 * Return a pointer to the ISP device structure, or NULL if an error occurred.
1392 static struct isp_device *__omap3isp_get(struct isp_device *isp, bool irq)
1394 struct isp_device *__isp = isp;
1399 mutex_lock(&isp->isp_mutex);
1400 if (isp->ref_count > 0)
1403 if (isp_enable_clocks(isp) < 0) {
1408 /* We don't want to restore context before saving it! */
1409 if (isp->has_context)
1410 isp_restore_ctx(isp);
1413 isp_enable_interrupts(isp);
1418 mutex_unlock(&isp->isp_mutex);
1423 struct isp_device *omap3isp_get(struct isp_device *isp)
1425 return __omap3isp_get(isp, true);
1429 * omap3isp_put - Release the ISP
1431 * Decrement the reference count on the ISP. If the last reference is released,
1432 * power-down all submodules, disable clocks and free temporary buffers.
1434 static void __omap3isp_put(struct isp_device *isp, bool save_ctx)
1439 mutex_lock(&isp->isp_mutex);
1440 BUG_ON(isp->ref_count == 0);
1441 if (--isp->ref_count == 0) {
1442 isp_disable_interrupts(isp);
1445 isp->has_context = 1;
1447 /* Reset the ISP if an entity has failed to stop. This is the
1448 * only way to recover from such conditions.
1450 if (!media_entity_enum_empty(&isp->crashed) ||
1453 isp_disable_clocks(isp);
1455 mutex_unlock(&isp->isp_mutex);
1458 void omap3isp_put(struct isp_device *isp)
1460 __omap3isp_put(isp, true);
1463 /* --------------------------------------------------------------------------
1464 * Platform device driver
1468 * omap3isp_print_status - Prints the values of the ISP Control Module registers
1469 * @isp: OMAP3 ISP device
1471 #define ISP_PRINT_REGISTER(isp, name)\
1472 dev_dbg(isp->dev, "###ISP " #name "=0x%08x\n", \
1473 isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_##name))
1474 #define SBL_PRINT_REGISTER(isp, name)\
1475 dev_dbg(isp->dev, "###SBL " #name "=0x%08x\n", \
1476 isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_##name))
1478 void omap3isp_print_status(struct isp_device *isp)
1480 dev_dbg(isp->dev, "-------------ISP Register dump--------------\n");
1482 ISP_PRINT_REGISTER(isp, SYSCONFIG);
1483 ISP_PRINT_REGISTER(isp, SYSSTATUS);
1484 ISP_PRINT_REGISTER(isp, IRQ0ENABLE);
1485 ISP_PRINT_REGISTER(isp, IRQ0STATUS);
1486 ISP_PRINT_REGISTER(isp, TCTRL_GRESET_LENGTH);
1487 ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_REPLAY);
1488 ISP_PRINT_REGISTER(isp, CTRL);
1489 ISP_PRINT_REGISTER(isp, TCTRL_CTRL);
1490 ISP_PRINT_REGISTER(isp, TCTRL_FRAME);
1491 ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_DELAY);
1492 ISP_PRINT_REGISTER(isp, TCTRL_STRB_DELAY);
1493 ISP_PRINT_REGISTER(isp, TCTRL_SHUT_DELAY);
1494 ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_LENGTH);
1495 ISP_PRINT_REGISTER(isp, TCTRL_STRB_LENGTH);
1496 ISP_PRINT_REGISTER(isp, TCTRL_SHUT_LENGTH);
1498 SBL_PRINT_REGISTER(isp, PCR);
1499 SBL_PRINT_REGISTER(isp, SDR_REQ_EXP);
1501 dev_dbg(isp->dev, "--------------------------------------------\n");
1507 * Power management support.
1509 * As the ISP can't properly handle an input video stream interruption on a non
1510 * frame boundary, the ISP pipelines need to be stopped before sensors get
1511 * suspended. However, as suspending the sensors can require a running clock,
1512 * which can be provided by the ISP, the ISP can't be completely suspended
1513 * before the sensor.
1515 * To solve this problem power management support is split into prepare/complete
1516 * and suspend/resume operations. The pipelines are stopped in prepare() and the
1517 * ISP clocks get disabled in suspend(). Similarly, the clocks are re-enabled in
1518 * resume(), and the the pipelines are restarted in complete().
1520 * TODO: PM dependencies between the ISP and sensors are not modelled explicitly
1523 static int isp_pm_prepare(struct device *dev)
1525 struct isp_device *isp = dev_get_drvdata(dev);
1528 WARN_ON(mutex_is_locked(&isp->isp_mutex));
1530 if (isp->ref_count == 0)
1533 reset = isp_suspend_modules(isp);
1534 isp_disable_interrupts(isp);
1542 static int isp_pm_suspend(struct device *dev)
1544 struct isp_device *isp = dev_get_drvdata(dev);
1546 WARN_ON(mutex_is_locked(&isp->isp_mutex));
1549 isp_disable_clocks(isp);
1554 static int isp_pm_resume(struct device *dev)
1556 struct isp_device *isp = dev_get_drvdata(dev);
1558 if (isp->ref_count == 0)
1561 return isp_enable_clocks(isp);
1564 static void isp_pm_complete(struct device *dev)
1566 struct isp_device *isp = dev_get_drvdata(dev);
1568 if (isp->ref_count == 0)
1571 isp_restore_ctx(isp);
1572 isp_enable_interrupts(isp);
1573 isp_resume_modules(isp);
1578 #define isp_pm_prepare NULL
1579 #define isp_pm_suspend NULL
1580 #define isp_pm_resume NULL
1581 #define isp_pm_complete NULL
1583 #endif /* CONFIG_PM */
1585 static void isp_unregister_entities(struct isp_device *isp)
1587 media_device_unregister(&isp->media_dev);
1589 omap3isp_csi2_unregister_entities(&isp->isp_csi2a);
1590 omap3isp_ccp2_unregister_entities(&isp->isp_ccp2);
1591 omap3isp_ccdc_unregister_entities(&isp->isp_ccdc);
1592 omap3isp_preview_unregister_entities(&isp->isp_prev);
1593 omap3isp_resizer_unregister_entities(&isp->isp_res);
1594 omap3isp_stat_unregister_entities(&isp->isp_aewb);
1595 omap3isp_stat_unregister_entities(&isp->isp_af);
1596 omap3isp_stat_unregister_entities(&isp->isp_hist);
1598 v4l2_device_unregister(&isp->v4l2_dev);
1599 media_device_cleanup(&isp->media_dev);
1602 static int isp_link_entity(
1603 struct isp_device *isp, struct media_entity *entity,
1604 enum isp_interface_type interface)
1606 struct media_entity *input;
1611 /* Connect the sensor to the correct interface module.
1612 * Parallel sensors are connected directly to the CCDC, while
1613 * serial sensors are connected to the CSI2a, CCP2b or CSI2c
1614 * receiver through CSIPHY1 or CSIPHY2.
1616 switch (interface) {
1617 case ISP_INTERFACE_PARALLEL:
1618 input = &isp->isp_ccdc.subdev.entity;
1619 pad = CCDC_PAD_SINK;
1623 case ISP_INTERFACE_CSI2A_PHY2:
1624 input = &isp->isp_csi2a.subdev.entity;
1625 pad = CSI2_PAD_SINK;
1626 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1629 case ISP_INTERFACE_CCP2B_PHY1:
1630 case ISP_INTERFACE_CCP2B_PHY2:
1631 input = &isp->isp_ccp2.subdev.entity;
1632 pad = CCP2_PAD_SINK;
1636 case ISP_INTERFACE_CSI2C_PHY1:
1637 input = &isp->isp_csi2c.subdev.entity;
1638 pad = CSI2_PAD_SINK;
1639 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1643 dev_err(isp->dev, "%s: invalid interface type %u\n", __func__,
1649 * Not all interfaces are available on all revisions of the
1650 * ISP. The sub-devices of those interfaces aren't initialised
1651 * in such a case. Check this by ensuring the num_pads is
1654 if (!input->num_pads) {
1655 dev_err(isp->dev, "%s: invalid input %u\n", entity->name,
1660 for (i = 0; i < entity->num_pads; i++) {
1661 if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
1664 if (i == entity->num_pads) {
1665 dev_err(isp->dev, "%s: no source pad in external entity %s\n",
1666 __func__, entity->name);
1670 return media_create_pad_link(entity, i, input, pad, flags);
1673 static int isp_register_entities(struct isp_device *isp)
1677 isp->media_dev.dev = isp->dev;
1678 strscpy(isp->media_dev.model, "TI OMAP3 ISP",
1679 sizeof(isp->media_dev.model));
1680 isp->media_dev.hw_revision = isp->revision;
1681 isp->media_dev.ops = &isp_media_ops;
1682 media_device_init(&isp->media_dev);
1684 isp->v4l2_dev.mdev = &isp->media_dev;
1685 ret = v4l2_device_register(isp->dev, &isp->v4l2_dev);
1687 dev_err(isp->dev, "%s: V4L2 device registration failed (%d)\n",
1692 /* Register internal entities */
1693 ret = omap3isp_ccp2_register_entities(&isp->isp_ccp2, &isp->v4l2_dev);
1697 ret = omap3isp_csi2_register_entities(&isp->isp_csi2a, &isp->v4l2_dev);
1701 ret = omap3isp_ccdc_register_entities(&isp->isp_ccdc, &isp->v4l2_dev);
1705 ret = omap3isp_preview_register_entities(&isp->isp_prev,
1710 ret = omap3isp_resizer_register_entities(&isp->isp_res, &isp->v4l2_dev);
1714 ret = omap3isp_stat_register_entities(&isp->isp_aewb, &isp->v4l2_dev);
1718 ret = omap3isp_stat_register_entities(&isp->isp_af, &isp->v4l2_dev);
1722 ret = omap3isp_stat_register_entities(&isp->isp_hist, &isp->v4l2_dev);
1728 isp_unregister_entities(isp);
1734 * isp_create_links() - Create links for internal and external ISP entities
1735 * @isp : Pointer to ISP device
1737 * This function creates all links between ISP internal and external entities.
1739 * Return: A negative error code on failure or zero on success. Possible error
1740 * codes are those returned by media_create_pad_link().
1742 static int isp_create_links(struct isp_device *isp)
1746 /* Create links between entities and video nodes. */
1747 ret = media_create_pad_link(
1748 &isp->isp_csi2a.subdev.entity, CSI2_PAD_SOURCE,
1749 &isp->isp_csi2a.video_out.video.entity, 0, 0);
1753 ret = media_create_pad_link(
1754 &isp->isp_ccp2.video_in.video.entity, 0,
1755 &isp->isp_ccp2.subdev.entity, CCP2_PAD_SINK, 0);
1759 ret = media_create_pad_link(
1760 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_OF,
1761 &isp->isp_ccdc.video_out.video.entity, 0, 0);
1765 ret = media_create_pad_link(
1766 &isp->isp_prev.video_in.video.entity, 0,
1767 &isp->isp_prev.subdev.entity, PREV_PAD_SINK, 0);
1771 ret = media_create_pad_link(
1772 &isp->isp_prev.subdev.entity, PREV_PAD_SOURCE,
1773 &isp->isp_prev.video_out.video.entity, 0, 0);
1777 ret = media_create_pad_link(
1778 &isp->isp_res.video_in.video.entity, 0,
1779 &isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
1783 ret = media_create_pad_link(
1784 &isp->isp_res.subdev.entity, RESZ_PAD_SOURCE,
1785 &isp->isp_res.video_out.video.entity, 0, 0);
1790 /* Create links between entities. */
1791 ret = media_create_pad_link(
1792 &isp->isp_csi2a.subdev.entity, CSI2_PAD_SOURCE,
1793 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SINK, 0);
1797 ret = media_create_pad_link(
1798 &isp->isp_ccp2.subdev.entity, CCP2_PAD_SOURCE,
1799 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SINK, 0);
1803 ret = media_create_pad_link(
1804 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1805 &isp->isp_prev.subdev.entity, PREV_PAD_SINK, 0);
1809 ret = media_create_pad_link(
1810 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_OF,
1811 &isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
1815 ret = media_create_pad_link(
1816 &isp->isp_prev.subdev.entity, PREV_PAD_SOURCE,
1817 &isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
1821 ret = media_create_pad_link(
1822 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1823 &isp->isp_aewb.subdev.entity, 0,
1824 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1828 ret = media_create_pad_link(
1829 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1830 &isp->isp_af.subdev.entity, 0,
1831 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1835 ret = media_create_pad_link(
1836 &isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1837 &isp->isp_hist.subdev.entity, 0,
1838 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1845 static void isp_cleanup_modules(struct isp_device *isp)
1847 omap3isp_h3a_aewb_cleanup(isp);
1848 omap3isp_h3a_af_cleanup(isp);
1849 omap3isp_hist_cleanup(isp);
1850 omap3isp_resizer_cleanup(isp);
1851 omap3isp_preview_cleanup(isp);
1852 omap3isp_ccdc_cleanup(isp);
1853 omap3isp_ccp2_cleanup(isp);
1854 omap3isp_csi2_cleanup(isp);
1855 omap3isp_csiphy_cleanup(isp);
1858 static int isp_initialize_modules(struct isp_device *isp)
1862 ret = omap3isp_csiphy_init(isp);
1864 dev_err(isp->dev, "CSI PHY initialization failed\n");
1868 ret = omap3isp_csi2_init(isp);
1870 dev_err(isp->dev, "CSI2 initialization failed\n");
1874 ret = omap3isp_ccp2_init(isp);
1876 if (ret != -EPROBE_DEFER)
1877 dev_err(isp->dev, "CCP2 initialization failed\n");
1881 ret = omap3isp_ccdc_init(isp);
1883 dev_err(isp->dev, "CCDC initialization failed\n");
1887 ret = omap3isp_preview_init(isp);
1889 dev_err(isp->dev, "Preview initialization failed\n");
1893 ret = omap3isp_resizer_init(isp);
1895 dev_err(isp->dev, "Resizer initialization failed\n");
1899 ret = omap3isp_hist_init(isp);
1901 dev_err(isp->dev, "Histogram initialization failed\n");
1905 ret = omap3isp_h3a_aewb_init(isp);
1907 dev_err(isp->dev, "H3A AEWB initialization failed\n");
1908 goto error_h3a_aewb;
1911 ret = omap3isp_h3a_af_init(isp);
1913 dev_err(isp->dev, "H3A AF initialization failed\n");
1920 omap3isp_h3a_aewb_cleanup(isp);
1922 omap3isp_hist_cleanup(isp);
1924 omap3isp_resizer_cleanup(isp);
1926 omap3isp_preview_cleanup(isp);
1928 omap3isp_ccdc_cleanup(isp);
1930 omap3isp_ccp2_cleanup(isp);
1932 omap3isp_csi2_cleanup(isp);
1934 omap3isp_csiphy_cleanup(isp);
1939 static void isp_detach_iommu(struct isp_device *isp)
1941 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1942 arm_iommu_detach_device(isp->dev);
1943 arm_iommu_release_mapping(isp->mapping);
1944 isp->mapping = NULL;
1948 static int isp_attach_iommu(struct isp_device *isp)
1950 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1951 struct dma_iommu_mapping *mapping;
1955 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
1956 * VAs. This will allocate a corresponding IOMMU domain.
1958 mapping = arm_iommu_create_mapping(&platform_bus_type, SZ_1G, SZ_2G);
1959 if (IS_ERR(mapping)) {
1960 dev_err(isp->dev, "failed to create ARM IOMMU mapping\n");
1961 return PTR_ERR(mapping);
1964 isp->mapping = mapping;
1966 /* Attach the ARM VA mapping to the device. */
1967 ret = arm_iommu_attach_device(isp->dev, mapping);
1969 dev_err(isp->dev, "failed to attach device to VA mapping\n");
1976 arm_iommu_release_mapping(isp->mapping);
1977 isp->mapping = NULL;
1985 * isp_remove - Remove ISP platform device
1986 * @pdev: Pointer to ISP platform device
1990 static int isp_remove(struct platform_device *pdev)
1992 struct isp_device *isp = platform_get_drvdata(pdev);
1994 v4l2_async_notifier_unregister(&isp->notifier);
1995 isp_unregister_entities(isp);
1996 isp_cleanup_modules(isp);
1997 isp_xclk_cleanup(isp);
1999 __omap3isp_get(isp, false);
2000 isp_detach_iommu(isp);
2001 __omap3isp_put(isp, false);
2003 media_entity_enum_cleanup(&isp->crashed);
2004 v4l2_async_notifier_cleanup(&isp->notifier);
2010 ISP_OF_PHY_PARALLEL = 0,
2015 static int isp_fwnode_parse(struct device *dev,
2016 struct v4l2_fwnode_endpoint *vep,
2017 struct v4l2_async_subdev *asd)
2019 struct isp_async_subdev *isd =
2020 container_of(asd, struct isp_async_subdev, asd);
2021 struct isp_bus_cfg *buscfg = &isd->bus;
2025 dev_dbg(dev, "parsing endpoint %pOF, interface %u\n",
2026 to_of_node(vep->base.local_fwnode), vep->base.port);
2028 switch (vep->base.port) {
2029 case ISP_OF_PHY_PARALLEL:
2030 buscfg->interface = ISP_INTERFACE_PARALLEL;
2031 buscfg->bus.parallel.data_lane_shift =
2032 vep->bus.parallel.data_shift;
2033 buscfg->bus.parallel.clk_pol =
2034 !!(vep->bus.parallel.flags
2035 & V4L2_MBUS_PCLK_SAMPLE_FALLING);
2036 buscfg->bus.parallel.hs_pol =
2037 !!(vep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW);
2038 buscfg->bus.parallel.vs_pol =
2039 !!(vep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW);
2040 buscfg->bus.parallel.fld_pol =
2041 !!(vep->bus.parallel.flags & V4L2_MBUS_FIELD_EVEN_LOW);
2042 buscfg->bus.parallel.data_pol =
2043 !!(vep->bus.parallel.flags & V4L2_MBUS_DATA_ACTIVE_LOW);
2044 buscfg->bus.parallel.bt656 = vep->bus_type == V4L2_MBUS_BT656;
2047 case ISP_OF_PHY_CSIPHY1:
2048 case ISP_OF_PHY_CSIPHY2:
2049 switch (vep->bus_type) {
2050 case V4L2_MBUS_CCP2:
2051 case V4L2_MBUS_CSI1:
2052 dev_dbg(dev, "CSI-1/CCP-2 configuration\n");
2055 case V4L2_MBUS_CSI2_DPHY:
2056 dev_dbg(dev, "CSI-2 configuration\n");
2060 dev_err(dev, "unsupported bus type %u\n",
2065 switch (vep->base.port) {
2066 case ISP_OF_PHY_CSIPHY1:
2068 buscfg->interface = ISP_INTERFACE_CCP2B_PHY1;
2070 buscfg->interface = ISP_INTERFACE_CSI2C_PHY1;
2072 case ISP_OF_PHY_CSIPHY2:
2074 buscfg->interface = ISP_INTERFACE_CCP2B_PHY2;
2076 buscfg->interface = ISP_INTERFACE_CSI2A_PHY2;
2080 buscfg->bus.ccp2.lanecfg.clk.pos =
2081 vep->bus.mipi_csi1.clock_lane;
2082 buscfg->bus.ccp2.lanecfg.clk.pol =
2083 vep->bus.mipi_csi1.lane_polarity[0];
2084 dev_dbg(dev, "clock lane polarity %u, pos %u\n",
2085 buscfg->bus.ccp2.lanecfg.clk.pol,
2086 buscfg->bus.ccp2.lanecfg.clk.pos);
2088 buscfg->bus.ccp2.lanecfg.data[0].pos =
2089 vep->bus.mipi_csi1.data_lane;
2090 buscfg->bus.ccp2.lanecfg.data[0].pol =
2091 vep->bus.mipi_csi1.lane_polarity[1];
2093 dev_dbg(dev, "data lane polarity %u, pos %u\n",
2094 buscfg->bus.ccp2.lanecfg.data[0].pol,
2095 buscfg->bus.ccp2.lanecfg.data[0].pos);
2097 buscfg->bus.ccp2.strobe_clk_pol =
2098 vep->bus.mipi_csi1.clock_inv;
2099 buscfg->bus.ccp2.phy_layer = vep->bus.mipi_csi1.strobe;
2100 buscfg->bus.ccp2.ccp2_mode =
2101 vep->bus_type == V4L2_MBUS_CCP2;
2102 buscfg->bus.ccp2.vp_clk_pol = 1;
2104 buscfg->bus.ccp2.crc = 1;
2106 buscfg->bus.csi2.lanecfg.clk.pos =
2107 vep->bus.mipi_csi2.clock_lane;
2108 buscfg->bus.csi2.lanecfg.clk.pol =
2109 vep->bus.mipi_csi2.lane_polarities[0];
2110 dev_dbg(dev, "clock lane polarity %u, pos %u\n",
2111 buscfg->bus.csi2.lanecfg.clk.pol,
2112 buscfg->bus.csi2.lanecfg.clk.pos);
2114 buscfg->bus.csi2.num_data_lanes =
2115 vep->bus.mipi_csi2.num_data_lanes;
2117 for (i = 0; i < buscfg->bus.csi2.num_data_lanes; i++) {
2118 buscfg->bus.csi2.lanecfg.data[i].pos =
2119 vep->bus.mipi_csi2.data_lanes[i];
2120 buscfg->bus.csi2.lanecfg.data[i].pol =
2121 vep->bus.mipi_csi2.lane_polarities[i + 1];
2123 "data lane %u polarity %u, pos %u\n", i,
2124 buscfg->bus.csi2.lanecfg.data[i].pol,
2125 buscfg->bus.csi2.lanecfg.data[i].pos);
2128 * FIXME: now we assume the CRC is always there.
2129 * Implement a way to obtain this information from the
2130 * sensor. Frame descriptors, perhaps?
2132 buscfg->bus.csi2.crc = 1;
2137 dev_warn(dev, "%pOF: invalid interface %u\n",
2138 to_of_node(vep->base.local_fwnode), vep->base.port);
2145 static int isp_subdev_notifier_complete(struct v4l2_async_notifier *async)
2147 struct isp_device *isp = container_of(async, struct isp_device,
2149 struct v4l2_device *v4l2_dev = &isp->v4l2_dev;
2150 struct v4l2_subdev *sd;
2153 ret = media_entity_enum_init(&isp->crashed, &isp->media_dev);
2157 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
2158 if (sd->notifier != &isp->notifier)
2161 ret = isp_link_entity(isp, &sd->entity,
2162 v4l2_subdev_to_bus_cfg(sd)->interface);
2167 ret = v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
2171 return media_device_register(&isp->media_dev);
2174 static const struct v4l2_async_notifier_operations isp_subdev_notifier_ops = {
2175 .complete = isp_subdev_notifier_complete,
2179 * isp_probe - Probe ISP platform device
2180 * @pdev: Pointer to ISP platform device
2182 * Returns 0 if successful,
2183 * -ENOMEM if no memory available,
2184 * -ENODEV if no platform device resources found
2185 * or no space for remapping registers,
2186 * -EINVAL if couldn't install ISR,
2187 * or clk_get return error value.
2189 static int isp_probe(struct platform_device *pdev)
2191 struct isp_device *isp;
2192 struct resource *mem;
2196 isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
2198 dev_err(&pdev->dev, "could not allocate memory\n");
2202 ret = fwnode_property_read_u32(of_fwnode_handle(pdev->dev.of_node),
2203 "ti,phy-type", &isp->phy_type);
2207 isp->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
2209 if (IS_ERR(isp->syscon))
2210 return PTR_ERR(isp->syscon);
2212 ret = of_property_read_u32_index(pdev->dev.of_node,
2213 "syscon", 1, &isp->syscon_offset);
2217 isp->autoidle = autoidle;
2219 mutex_init(&isp->isp_mutex);
2220 spin_lock_init(&isp->stat_lock);
2221 v4l2_async_notifier_init(&isp->notifier);
2223 ret = v4l2_async_notifier_parse_fwnode_endpoints(
2224 &pdev->dev, &isp->notifier, sizeof(struct isp_async_subdev),
2229 isp->dev = &pdev->dev;
2232 ret = dma_coerce_mask_and_coherent(isp->dev, DMA_BIT_MASK(32));
2236 platform_set_drvdata(pdev, isp);
2239 isp->isp_csiphy1.vdd = devm_regulator_get(&pdev->dev, "vdd-csiphy1");
2240 isp->isp_csiphy2.vdd = devm_regulator_get(&pdev->dev, "vdd-csiphy2");
2244 * The ISP clock tree is revision-dependent. We thus need to enable ICLK
2245 * manually to read the revision before calling __omap3isp_get().
2247 * Start by mapping the ISP MMIO area, which is in two pieces.
2248 * The ISP IOMMU is in between. Map both now, and fill in the
2249 * ISP revision specific portions a little later in the
2252 for (i = 0; i < 2; i++) {
2253 unsigned int map_idx = i ? OMAP3_ISP_IOMEM_CSI2A_REGS1 : 0;
2255 mem = platform_get_resource(pdev, IORESOURCE_MEM, i);
2256 isp->mmio_base[map_idx] =
2257 devm_ioremap_resource(isp->dev, mem);
2258 if (IS_ERR(isp->mmio_base[map_idx]))
2259 return PTR_ERR(isp->mmio_base[map_idx]);
2262 ret = isp_get_clocks(isp);
2266 ret = clk_enable(isp->clock[ISP_CLK_CAM_ICK]);
2270 isp->revision = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
2271 dev_info(isp->dev, "Revision %d.%d found\n",
2272 (isp->revision & 0xf0) >> 4, isp->revision & 0x0f);
2274 clk_disable(isp->clock[ISP_CLK_CAM_ICK]);
2276 if (__omap3isp_get(isp, false) == NULL) {
2281 ret = isp_reset(isp);
2285 ret = isp_xclk_init(isp);
2289 /* Memory resources */
2290 for (m = 0; m < ARRAY_SIZE(isp_res_maps); m++)
2291 if (isp->revision == isp_res_maps[m].isp_rev)
2294 if (m == ARRAY_SIZE(isp_res_maps)) {
2295 dev_err(isp->dev, "No resource map found for ISP rev %d.%d\n",
2296 (isp->revision & 0xf0) >> 4, isp->revision & 0xf);
2301 for (i = 1; i < OMAP3_ISP_IOMEM_CSI2A_REGS1; i++)
2303 isp->mmio_base[0] + isp_res_maps[m].offset[i];
2305 for (i = OMAP3_ISP_IOMEM_CSIPHY2; i < OMAP3_ISP_IOMEM_LAST; i++)
2307 isp->mmio_base[OMAP3_ISP_IOMEM_CSI2A_REGS1]
2308 + isp_res_maps[m].offset[i];
2310 isp->mmio_hist_base_phys =
2311 mem->start + isp_res_maps[m].offset[OMAP3_ISP_IOMEM_HIST];
2314 ret = isp_attach_iommu(isp);
2316 dev_err(&pdev->dev, "unable to attach to IOMMU\n");
2321 ret = platform_get_irq(pdev, 0);
2323 dev_err(isp->dev, "No IRQ resource\n");
2329 if (devm_request_irq(isp->dev, isp->irq_num, isp_isr, IRQF_SHARED,
2330 "OMAP3 ISP", isp)) {
2331 dev_err(isp->dev, "Unable to request IRQ\n");
2337 ret = isp_initialize_modules(isp);
2341 ret = isp_register_entities(isp);
2345 ret = isp_create_links(isp);
2347 goto error_register_entities;
2349 isp->notifier.ops = &isp_subdev_notifier_ops;
2351 ret = v4l2_async_notifier_register(&isp->v4l2_dev, &isp->notifier);
2353 goto error_register_entities;
2355 isp_core_init(isp, 1);
2360 error_register_entities:
2361 isp_unregister_entities(isp);
2363 isp_cleanup_modules(isp);
2365 isp_detach_iommu(isp);
2367 isp_xclk_cleanup(isp);
2368 __omap3isp_put(isp, false);
2370 v4l2_async_notifier_cleanup(&isp->notifier);
2371 mutex_destroy(&isp->isp_mutex);
2376 static const struct dev_pm_ops omap3isp_pm_ops = {
2377 .prepare = isp_pm_prepare,
2378 .suspend = isp_pm_suspend,
2379 .resume = isp_pm_resume,
2380 .complete = isp_pm_complete,
2383 static struct platform_device_id omap3isp_id_table[] = {
2387 MODULE_DEVICE_TABLE(platform, omap3isp_id_table);
2389 static const struct of_device_id omap3isp_of_table[] = {
2390 { .compatible = "ti,omap3-isp" },
2393 MODULE_DEVICE_TABLE(of, omap3isp_of_table);
2395 static struct platform_driver omap3isp_driver = {
2397 .remove = isp_remove,
2398 .id_table = omap3isp_id_table,
2401 .pm = &omap3isp_pm_ops,
2402 .of_match_table = omap3isp_of_table,
2406 module_platform_driver(omap3isp_driver);
2408 MODULE_AUTHOR("Nokia Corporation");
2409 MODULE_DESCRIPTION("TI OMAP3 ISP driver");
2410 MODULE_LICENSE("GPL");
2411 MODULE_VERSION(ISP_VIDEO_DRIVER_VERSION);