• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * isp.c
3  *
4  * TI OMAP3 ISP - Core
5  *
6  * Copyright (C) 2006-2010 Nokia Corporation
7  * Copyright (C) 2007-2009 Texas Instruments, Inc.
8  *
9  * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10  *	     Sakari Ailus <sakari.ailus@iki.fi>
11  *
12  * Contributors:
13  *	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
14  *	Sakari Ailus <sakari.ailus@iki.fi>
15  *	David Cohen <dacohen@gmail.com>
16  *	Stanimir Varbanov <svarbanov@mm-sol.com>
17  *	Vimarsh Zutshi <vimarsh.zutshi@gmail.com>
18  *	Tuukka Toivonen <tuukkat76@gmail.com>
19  *	Sergio Aguirre <saaguirre@ti.com>
20  *	Antti Koskipaa <akoskipa@gmail.com>
21  *	Ivan T. Ivanov <iivanov@mm-sol.com>
22  *	RaniSuneela <r-m@ti.com>
23  *	Atanas Filipov <afilipov@mm-sol.com>
24  *	Gjorgji Rosikopulos <grosikopulos@mm-sol.com>
25  *	Hiroshi DOYU <hiroshi.doyu@nokia.com>
26  *	Nayden Kanchev <nkanchev@mm-sol.com>
27  *	Phil Carmody <ext-phil.2.carmody@nokia.com>
28  *	Artem Bityutskiy <artem.bityutskiy@nokia.com>
29  *	Dominic Curran <dcurran@ti.com>
30  *	Ilkka Myllyperkio <ilkka.myllyperkio@sofica.fi>
31  *	Pallavi Kulkarni <p-kulkarni@ti.com>
32  *	Vaibhav Hiremath <hvaibhav@ti.com>
33  *	Mohit Jalori <mjalori@ti.com>
34  *	Sameer Venkatraman <sameerv@ti.com>
35  *	Senthilvadivu Guruswamy <svadivu@ti.com>
36  *	Thara Gopinath <thara@ti.com>
37  *	Toni Leinonen <toni.leinonen@nokia.com>
38  *	Troy Laramy <t-laramy@ti.com>
39  *
40  * This program is free software; you can redistribute it and/or modify
41  * it under the terms of the GNU General Public License version 2 as
42  * published by the Free Software Foundation.
43  */
44 
45 #include <asm/cacheflush.h>
46 
47 #include <linux/clk.h>
48 #include <linux/clkdev.h>
49 #include <linux/delay.h>
50 #include <linux/device.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/i2c.h>
53 #include <linux/interrupt.h>
54 #include <linux/mfd/syscon.h>
55 #include <linux/module.h>
56 #include <linux/omap-iommu.h>
57 #include <linux/platform_device.h>
58 #include <linux/property.h>
59 #include <linux/regulator/consumer.h>
60 #include <linux/slab.h>
61 #include <linux/sched.h>
62 #include <linux/vmalloc.h>
63 
64 #ifdef CONFIG_ARM_DMA_USE_IOMMU
65 #include <asm/dma-iommu.h>
66 #endif
67 
68 #include <media/v4l2-common.h>
69 #include <media/v4l2-fwnode.h>
70 #include <media/v4l2-device.h>
71 #include <media/v4l2-mc.h>
72 
73 #include "isp.h"
74 #include "ispreg.h"
75 #include "ispccdc.h"
76 #include "isppreview.h"
77 #include "ispresizer.h"
78 #include "ispcsi2.h"
79 #include "ispccp2.h"
80 #include "isph3a.h"
81 #include "isphist.h"
82 
83 static unsigned int autoidle;
84 module_param(autoidle, int, 0444);
85 MODULE_PARM_DESC(autoidle, "Enable OMAP3ISP AUTOIDLE support");
86 
87 static void isp_save_ctx(struct isp_device *isp);
88 
89 static void isp_restore_ctx(struct isp_device *isp);
90 
91 static const struct isp_res_mapping isp_res_maps[] = {
92 	{
93 		.isp_rev = ISP_REVISION_2_0,
94 		.offset = {
95 			/* first MMIO area */
96 			0x0000, /* base, len 0x0070 */
97 			0x0400, /* ccp2, len 0x01f0 */
98 			0x0600, /* ccdc, len 0x00a8 */
99 			0x0a00, /* hist, len 0x0048 */
100 			0x0c00, /* h3a, len 0x0060 */
101 			0x0e00, /* preview, len 0x00a0 */
102 			0x1000, /* resizer, len 0x00ac */
103 			0x1200, /* sbl, len 0x00fc */
104 			/* second MMIO area */
105 			0x0000, /* csi2a, len 0x0170 */
106 			0x0170, /* csiphy2, len 0x000c */
107 		},
108 		.phy_type = ISP_PHY_TYPE_3430,
109 	},
110 	{
111 		.isp_rev = ISP_REVISION_15_0,
112 		.offset = {
113 			/* first MMIO area */
114 			0x0000, /* base, len 0x0070 */
115 			0x0400, /* ccp2, len 0x01f0 */
116 			0x0600, /* ccdc, len 0x00a8 */
117 			0x0a00, /* hist, len 0x0048 */
118 			0x0c00, /* h3a, len 0x0060 */
119 			0x0e00, /* preview, len 0x00a0 */
120 			0x1000, /* resizer, len 0x00ac */
121 			0x1200, /* sbl, len 0x00fc */
122 			/* second MMIO area */
123 			0x0000, /* csi2a, len 0x0170 (1st area) */
124 			0x0170, /* csiphy2, len 0x000c */
125 			0x01c0, /* csi2a, len 0x0040 (2nd area) */
126 			0x0400, /* csi2c, len 0x0170 (1st area) */
127 			0x0570, /* csiphy1, len 0x000c */
128 			0x05c0, /* csi2c, len 0x0040 (2nd area) */
129 		},
130 		.phy_type = ISP_PHY_TYPE_3630,
131 	},
132 };
133 
134 /* Structure for saving/restoring ISP module registers */
135 static struct isp_reg isp_reg_list[] = {
136 	{OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG, 0},
137 	{OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, 0},
138 	{OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL, 0},
139 	{0, ISP_TOK_TERM, 0}
140 };
141 
142 /*
143  * omap3isp_flush - Post pending L3 bus writes by doing a register readback
144  * @isp: OMAP3 ISP device
145  *
146  * In order to force posting of pending writes, we need to write and
147  * readback the same register, in this case the revision register.
148  *
149  * See this link for reference:
150  *   http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
151  */
omap3isp_flush(struct isp_device * isp)152 void omap3isp_flush(struct isp_device *isp)
153 {
154 	isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
155 	isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
156 }
157 
158 /* -----------------------------------------------------------------------------
159  * XCLK
160  */
161 
162 #define to_isp_xclk(_hw)	container_of(_hw, struct isp_xclk, hw)
163 
isp_xclk_update(struct isp_xclk * xclk,u32 divider)164 static void isp_xclk_update(struct isp_xclk *xclk, u32 divider)
165 {
166 	switch (xclk->id) {
167 	case ISP_XCLK_A:
168 		isp_reg_clr_set(xclk->isp, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
169 				ISPTCTRL_CTRL_DIVA_MASK,
170 				divider << ISPTCTRL_CTRL_DIVA_SHIFT);
171 		break;
172 	case ISP_XCLK_B:
173 		isp_reg_clr_set(xclk->isp, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
174 				ISPTCTRL_CTRL_DIVB_MASK,
175 				divider << ISPTCTRL_CTRL_DIVB_SHIFT);
176 		break;
177 	}
178 }
179 
isp_xclk_prepare(struct clk_hw * hw)180 static int isp_xclk_prepare(struct clk_hw *hw)
181 {
182 	struct isp_xclk *xclk = to_isp_xclk(hw);
183 
184 	omap3isp_get(xclk->isp);
185 
186 	return 0;
187 }
188 
isp_xclk_unprepare(struct clk_hw * hw)189 static void isp_xclk_unprepare(struct clk_hw *hw)
190 {
191 	struct isp_xclk *xclk = to_isp_xclk(hw);
192 
193 	omap3isp_put(xclk->isp);
194 }
195 
isp_xclk_enable(struct clk_hw * hw)196 static int isp_xclk_enable(struct clk_hw *hw)
197 {
198 	struct isp_xclk *xclk = to_isp_xclk(hw);
199 	unsigned long flags;
200 
201 	spin_lock_irqsave(&xclk->lock, flags);
202 	isp_xclk_update(xclk, xclk->divider);
203 	xclk->enabled = true;
204 	spin_unlock_irqrestore(&xclk->lock, flags);
205 
206 	return 0;
207 }
208 
isp_xclk_disable(struct clk_hw * hw)209 static void isp_xclk_disable(struct clk_hw *hw)
210 {
211 	struct isp_xclk *xclk = to_isp_xclk(hw);
212 	unsigned long flags;
213 
214 	spin_lock_irqsave(&xclk->lock, flags);
215 	isp_xclk_update(xclk, 0);
216 	xclk->enabled = false;
217 	spin_unlock_irqrestore(&xclk->lock, flags);
218 }
219 
isp_xclk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)220 static unsigned long isp_xclk_recalc_rate(struct clk_hw *hw,
221 					  unsigned long parent_rate)
222 {
223 	struct isp_xclk *xclk = to_isp_xclk(hw);
224 
225 	return parent_rate / xclk->divider;
226 }
227 
isp_xclk_calc_divider(unsigned long * rate,unsigned long parent_rate)228 static u32 isp_xclk_calc_divider(unsigned long *rate, unsigned long parent_rate)
229 {
230 	u32 divider;
231 
232 	if (*rate >= parent_rate) {
233 		*rate = parent_rate;
234 		return ISPTCTRL_CTRL_DIV_BYPASS;
235 	}
236 
237 	if (*rate == 0)
238 		*rate = 1;
239 
240 	divider = DIV_ROUND_CLOSEST(parent_rate, *rate);
241 	if (divider >= ISPTCTRL_CTRL_DIV_BYPASS)
242 		divider = ISPTCTRL_CTRL_DIV_BYPASS - 1;
243 
244 	*rate = parent_rate / divider;
245 	return divider;
246 }
247 
isp_xclk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)248 static long isp_xclk_round_rate(struct clk_hw *hw, unsigned long rate,
249 				unsigned long *parent_rate)
250 {
251 	isp_xclk_calc_divider(&rate, *parent_rate);
252 	return rate;
253 }
254 
isp_xclk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)255 static int isp_xclk_set_rate(struct clk_hw *hw, unsigned long rate,
256 			     unsigned long parent_rate)
257 {
258 	struct isp_xclk *xclk = to_isp_xclk(hw);
259 	unsigned long flags;
260 	u32 divider;
261 
262 	divider = isp_xclk_calc_divider(&rate, parent_rate);
263 
264 	spin_lock_irqsave(&xclk->lock, flags);
265 
266 	xclk->divider = divider;
267 	if (xclk->enabled)
268 		isp_xclk_update(xclk, divider);
269 
270 	spin_unlock_irqrestore(&xclk->lock, flags);
271 
272 	dev_dbg(xclk->isp->dev, "%s: cam_xclk%c set to %lu Hz (div %u)\n",
273 		__func__, xclk->id == ISP_XCLK_A ? 'a' : 'b', rate, divider);
274 	return 0;
275 }
276 
277 static const struct clk_ops isp_xclk_ops = {
278 	.prepare = isp_xclk_prepare,
279 	.unprepare = isp_xclk_unprepare,
280 	.enable = isp_xclk_enable,
281 	.disable = isp_xclk_disable,
282 	.recalc_rate = isp_xclk_recalc_rate,
283 	.round_rate = isp_xclk_round_rate,
284 	.set_rate = isp_xclk_set_rate,
285 };
286 
287 static const char *isp_xclk_parent_name = "cam_mclk";
288 
isp_xclk_src_get(struct of_phandle_args * clkspec,void * data)289 static struct clk *isp_xclk_src_get(struct of_phandle_args *clkspec, void *data)
290 {
291 	unsigned int idx = clkspec->args[0];
292 	struct isp_device *isp = data;
293 
294 	if (idx >= ARRAY_SIZE(isp->xclks))
295 		return ERR_PTR(-ENOENT);
296 
297 	return isp->xclks[idx].clk;
298 }
299 
isp_xclk_init(struct isp_device * isp)300 static int isp_xclk_init(struct isp_device *isp)
301 {
302 	struct device_node *np = isp->dev->of_node;
303 	struct clk_init_data init = {};
304 	unsigned int i;
305 
306 	for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i)
307 		isp->xclks[i].clk = ERR_PTR(-EINVAL);
308 
309 	for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i) {
310 		struct isp_xclk *xclk = &isp->xclks[i];
311 
312 		xclk->isp = isp;
313 		xclk->id = i == 0 ? ISP_XCLK_A : ISP_XCLK_B;
314 		xclk->divider = 1;
315 		spin_lock_init(&xclk->lock);
316 
317 		init.name = i == 0 ? "cam_xclka" : "cam_xclkb";
318 		init.ops = &isp_xclk_ops;
319 		init.parent_names = &isp_xclk_parent_name;
320 		init.num_parents = 1;
321 
322 		xclk->hw.init = &init;
323 		/*
324 		 * The first argument is NULL in order to avoid circular
325 		 * reference, as this driver takes reference on the
326 		 * sensor subdevice modules and the sensors would take
327 		 * reference on this module through clk_get().
328 		 */
329 		xclk->clk = clk_register(NULL, &xclk->hw);
330 		if (IS_ERR(xclk->clk))
331 			return PTR_ERR(xclk->clk);
332 	}
333 
334 	if (np)
335 		of_clk_add_provider(np, isp_xclk_src_get, isp);
336 
337 	return 0;
338 }
339 
isp_xclk_cleanup(struct isp_device * isp)340 static void isp_xclk_cleanup(struct isp_device *isp)
341 {
342 	struct device_node *np = isp->dev->of_node;
343 	unsigned int i;
344 
345 	if (np)
346 		of_clk_del_provider(np);
347 
348 	for (i = 0; i < ARRAY_SIZE(isp->xclks); ++i) {
349 		struct isp_xclk *xclk = &isp->xclks[i];
350 
351 		if (!IS_ERR(xclk->clk))
352 			clk_unregister(xclk->clk);
353 	}
354 }
355 
356 /* -----------------------------------------------------------------------------
357  * Interrupts
358  */
359 
360 /*
361  * isp_enable_interrupts - Enable ISP interrupts.
362  * @isp: OMAP3 ISP device
363  */
isp_enable_interrupts(struct isp_device * isp)364 static void isp_enable_interrupts(struct isp_device *isp)
365 {
366 	static const u32 irq = IRQ0ENABLE_CSIA_IRQ
367 			     | IRQ0ENABLE_CSIB_IRQ
368 			     | IRQ0ENABLE_CCDC_LSC_PREF_ERR_IRQ
369 			     | IRQ0ENABLE_CCDC_LSC_DONE_IRQ
370 			     | IRQ0ENABLE_CCDC_VD0_IRQ
371 			     | IRQ0ENABLE_CCDC_VD1_IRQ
372 			     | IRQ0ENABLE_HS_VS_IRQ
373 			     | IRQ0ENABLE_HIST_DONE_IRQ
374 			     | IRQ0ENABLE_H3A_AWB_DONE_IRQ
375 			     | IRQ0ENABLE_H3A_AF_DONE_IRQ
376 			     | IRQ0ENABLE_PRV_DONE_IRQ
377 			     | IRQ0ENABLE_RSZ_DONE_IRQ;
378 
379 	isp_reg_writel(isp, irq, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
380 	isp_reg_writel(isp, irq, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
381 }
382 
383 /*
384  * isp_disable_interrupts - Disable ISP interrupts.
385  * @isp: OMAP3 ISP device
386  */
isp_disable_interrupts(struct isp_device * isp)387 static void isp_disable_interrupts(struct isp_device *isp)
388 {
389 	isp_reg_writel(isp, 0, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
390 }
391 
392 /*
393  * isp_core_init - ISP core settings
394  * @isp: OMAP3 ISP device
395  * @idle: Consider idle state.
396  *
397  * Set the power settings for the ISP and SBL bus and configure the HS/VS
398  * interrupt source.
399  *
400  * We need to configure the HS/VS interrupt source before interrupts get
401  * enabled, as the sensor might be free-running and the ISP default setting
402  * (HS edge) would put an unnecessary burden on the CPU.
403  */
isp_core_init(struct isp_device * isp,int idle)404 static void isp_core_init(struct isp_device *isp, int idle)
405 {
406 	isp_reg_writel(isp,
407 		       ((idle ? ISP_SYSCONFIG_MIDLEMODE_SMARTSTANDBY :
408 				ISP_SYSCONFIG_MIDLEMODE_FORCESTANDBY) <<
409 			ISP_SYSCONFIG_MIDLEMODE_SHIFT) |
410 			((isp->revision == ISP_REVISION_15_0) ?
411 			  ISP_SYSCONFIG_AUTOIDLE : 0),
412 		       OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
413 
414 	isp_reg_writel(isp,
415 		       (isp->autoidle ? ISPCTRL_SBL_AUTOIDLE : 0) |
416 		       ISPCTRL_SYNC_DETECT_VSRISE,
417 		       OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
418 }
419 
420 /*
421  * Configure the bridge and lane shifter. Valid inputs are
422  *
423  * CCDC_INPUT_PARALLEL: Parallel interface
424  * CCDC_INPUT_CSI2A: CSI2a receiver
425  * CCDC_INPUT_CCP2B: CCP2b receiver
426  * CCDC_INPUT_CSI2C: CSI2c receiver
427  *
428  * The bridge and lane shifter are configured according to the selected input
429  * and the ISP platform data.
430  */
omap3isp_configure_bridge(struct isp_device * isp,enum ccdc_input_entity input,const struct isp_parallel_cfg * parcfg,unsigned int shift,unsigned int bridge)431 void omap3isp_configure_bridge(struct isp_device *isp,
432 			       enum ccdc_input_entity input,
433 			       const struct isp_parallel_cfg *parcfg,
434 			       unsigned int shift, unsigned int bridge)
435 {
436 	u32 ispctrl_val;
437 
438 	ispctrl_val  = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
439 	ispctrl_val &= ~ISPCTRL_SHIFT_MASK;
440 	ispctrl_val &= ~ISPCTRL_PAR_CLK_POL_INV;
441 	ispctrl_val &= ~ISPCTRL_PAR_SER_CLK_SEL_MASK;
442 	ispctrl_val &= ~ISPCTRL_PAR_BRIDGE_MASK;
443 	ispctrl_val |= bridge;
444 
445 	switch (input) {
446 	case CCDC_INPUT_PARALLEL:
447 		ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_PARALLEL;
448 		ispctrl_val |= parcfg->clk_pol << ISPCTRL_PAR_CLK_POL_SHIFT;
449 		shift += parcfg->data_lane_shift;
450 		break;
451 
452 	case CCDC_INPUT_CSI2A:
453 		ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIA;
454 		break;
455 
456 	case CCDC_INPUT_CCP2B:
457 		ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIB;
458 		break;
459 
460 	case CCDC_INPUT_CSI2C:
461 		ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIC;
462 		break;
463 
464 	default:
465 		return;
466 	}
467 
468 	ispctrl_val |= ((shift/2) << ISPCTRL_SHIFT_SHIFT) & ISPCTRL_SHIFT_MASK;
469 
470 	isp_reg_writel(isp, ispctrl_val, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
471 }
472 
omap3isp_hist_dma_done(struct isp_device * isp)473 void omap3isp_hist_dma_done(struct isp_device *isp)
474 {
475 	if (omap3isp_ccdc_busy(&isp->isp_ccdc) ||
476 	    omap3isp_stat_pcr_busy(&isp->isp_hist)) {
477 		/* Histogram cannot be enabled in this frame anymore */
478 		atomic_set(&isp->isp_hist.buf_err, 1);
479 		dev_dbg(isp->dev,
480 			"hist: Out of synchronization with CCDC. Ignoring next buffer.\n");
481 	}
482 }
483 
isp_isr_dbg(struct isp_device * isp,u32 irqstatus)484 static inline void isp_isr_dbg(struct isp_device *isp, u32 irqstatus)
485 {
486 	static const char *name[] = {
487 		"CSIA_IRQ",
488 		"res1",
489 		"res2",
490 		"CSIB_LCM_IRQ",
491 		"CSIB_IRQ",
492 		"res5",
493 		"res6",
494 		"res7",
495 		"CCDC_VD0_IRQ",
496 		"CCDC_VD1_IRQ",
497 		"CCDC_VD2_IRQ",
498 		"CCDC_ERR_IRQ",
499 		"H3A_AF_DONE_IRQ",
500 		"H3A_AWB_DONE_IRQ",
501 		"res14",
502 		"res15",
503 		"HIST_DONE_IRQ",
504 		"CCDC_LSC_DONE",
505 		"CCDC_LSC_PREFETCH_COMPLETED",
506 		"CCDC_LSC_PREFETCH_ERROR",
507 		"PRV_DONE_IRQ",
508 		"CBUFF_IRQ",
509 		"res22",
510 		"res23",
511 		"RSZ_DONE_IRQ",
512 		"OVF_IRQ",
513 		"res26",
514 		"res27",
515 		"MMU_ERR_IRQ",
516 		"OCP_ERR_IRQ",
517 		"SEC_ERR_IRQ",
518 		"HS_VS_IRQ",
519 	};
520 	int i;
521 
522 	dev_dbg(isp->dev, "ISP IRQ: ");
523 
524 	for (i = 0; i < ARRAY_SIZE(name); i++) {
525 		if ((1 << i) & irqstatus)
526 			printk(KERN_CONT "%s ", name[i]);
527 	}
528 	printk(KERN_CONT "\n");
529 }
530 
isp_isr_sbl(struct isp_device * isp)531 static void isp_isr_sbl(struct isp_device *isp)
532 {
533 	struct device *dev = isp->dev;
534 	struct isp_pipeline *pipe;
535 	u32 sbl_pcr;
536 
537 	/*
538 	 * Handle shared buffer logic overflows for video buffers.
539 	 * ISPSBL_PCR_CCDCPRV_2_RSZ_OVF can be safely ignored.
540 	 */
541 	sbl_pcr = isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
542 	isp_reg_writel(isp, sbl_pcr, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
543 	sbl_pcr &= ~ISPSBL_PCR_CCDCPRV_2_RSZ_OVF;
544 
545 	if (sbl_pcr)
546 		dev_dbg(dev, "SBL overflow (PCR = 0x%08x)\n", sbl_pcr);
547 
548 	if (sbl_pcr & ISPSBL_PCR_CSIB_WBL_OVF) {
549 		pipe = to_isp_pipeline(&isp->isp_ccp2.subdev.entity);
550 		if (pipe != NULL)
551 			pipe->error = true;
552 	}
553 
554 	if (sbl_pcr & ISPSBL_PCR_CSIA_WBL_OVF) {
555 		pipe = to_isp_pipeline(&isp->isp_csi2a.subdev.entity);
556 		if (pipe != NULL)
557 			pipe->error = true;
558 	}
559 
560 	if (sbl_pcr & ISPSBL_PCR_CCDC_WBL_OVF) {
561 		pipe = to_isp_pipeline(&isp->isp_ccdc.subdev.entity);
562 		if (pipe != NULL)
563 			pipe->error = true;
564 	}
565 
566 	if (sbl_pcr & ISPSBL_PCR_PRV_WBL_OVF) {
567 		pipe = to_isp_pipeline(&isp->isp_prev.subdev.entity);
568 		if (pipe != NULL)
569 			pipe->error = true;
570 	}
571 
572 	if (sbl_pcr & (ISPSBL_PCR_RSZ1_WBL_OVF
573 		       | ISPSBL_PCR_RSZ2_WBL_OVF
574 		       | ISPSBL_PCR_RSZ3_WBL_OVF
575 		       | ISPSBL_PCR_RSZ4_WBL_OVF)) {
576 		pipe = to_isp_pipeline(&isp->isp_res.subdev.entity);
577 		if (pipe != NULL)
578 			pipe->error = true;
579 	}
580 
581 	if (sbl_pcr & ISPSBL_PCR_H3A_AF_WBL_OVF)
582 		omap3isp_stat_sbl_overflow(&isp->isp_af);
583 
584 	if (sbl_pcr & ISPSBL_PCR_H3A_AEAWB_WBL_OVF)
585 		omap3isp_stat_sbl_overflow(&isp->isp_aewb);
586 }
587 
588 /*
589  * isp_isr - Interrupt Service Routine for Camera ISP module.
590  * @irq: Not used currently.
591  * @_isp: Pointer to the OMAP3 ISP device
592  *
593  * Handles the corresponding callback if plugged in.
594  */
isp_isr(int irq,void * _isp)595 static irqreturn_t isp_isr(int irq, void *_isp)
596 {
597 	static const u32 ccdc_events = IRQ0STATUS_CCDC_LSC_PREF_ERR_IRQ |
598 				       IRQ0STATUS_CCDC_LSC_DONE_IRQ |
599 				       IRQ0STATUS_CCDC_VD0_IRQ |
600 				       IRQ0STATUS_CCDC_VD1_IRQ |
601 				       IRQ0STATUS_HS_VS_IRQ;
602 	struct isp_device *isp = _isp;
603 	u32 irqstatus;
604 
605 	irqstatus = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
606 	isp_reg_writel(isp, irqstatus, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
607 
608 	isp_isr_sbl(isp);
609 
610 	if (irqstatus & IRQ0STATUS_CSIA_IRQ)
611 		omap3isp_csi2_isr(&isp->isp_csi2a);
612 
613 	if (irqstatus & IRQ0STATUS_CSIB_IRQ)
614 		omap3isp_ccp2_isr(&isp->isp_ccp2);
615 
616 	if (irqstatus & IRQ0STATUS_CCDC_VD0_IRQ) {
617 		if (isp->isp_ccdc.output & CCDC_OUTPUT_PREVIEW)
618 			omap3isp_preview_isr_frame_sync(&isp->isp_prev);
619 		if (isp->isp_ccdc.output & CCDC_OUTPUT_RESIZER)
620 			omap3isp_resizer_isr_frame_sync(&isp->isp_res);
621 		omap3isp_stat_isr_frame_sync(&isp->isp_aewb);
622 		omap3isp_stat_isr_frame_sync(&isp->isp_af);
623 		omap3isp_stat_isr_frame_sync(&isp->isp_hist);
624 	}
625 
626 	if (irqstatus & ccdc_events)
627 		omap3isp_ccdc_isr(&isp->isp_ccdc, irqstatus & ccdc_events);
628 
629 	if (irqstatus & IRQ0STATUS_PRV_DONE_IRQ) {
630 		if (isp->isp_prev.output & PREVIEW_OUTPUT_RESIZER)
631 			omap3isp_resizer_isr_frame_sync(&isp->isp_res);
632 		omap3isp_preview_isr(&isp->isp_prev);
633 	}
634 
635 	if (irqstatus & IRQ0STATUS_RSZ_DONE_IRQ)
636 		omap3isp_resizer_isr(&isp->isp_res);
637 
638 	if (irqstatus & IRQ0STATUS_H3A_AWB_DONE_IRQ)
639 		omap3isp_stat_isr(&isp->isp_aewb);
640 
641 	if (irqstatus & IRQ0STATUS_H3A_AF_DONE_IRQ)
642 		omap3isp_stat_isr(&isp->isp_af);
643 
644 	if (irqstatus & IRQ0STATUS_HIST_DONE_IRQ)
645 		omap3isp_stat_isr(&isp->isp_hist);
646 
647 	omap3isp_flush(isp);
648 
649 #if defined(DEBUG) && defined(ISP_ISR_DEBUG)
650 	isp_isr_dbg(isp, irqstatus);
651 #endif
652 
653 	return IRQ_HANDLED;
654 }
655 
656 static const struct media_device_ops isp_media_ops = {
657 	.link_notify = v4l2_pipeline_link_notify,
658 };
659 
660 /* -----------------------------------------------------------------------------
661  * Pipeline stream management
662  */
663 
664 /*
665  * isp_pipeline_enable - Enable streaming on a pipeline
666  * @pipe: ISP pipeline
667  * @mode: Stream mode (single shot or continuous)
668  *
669  * Walk the entities chain starting at the pipeline output video node and start
670  * all modules in the chain in the given mode.
671  *
672  * Return 0 if successful, or the return value of the failed video::s_stream
673  * operation otherwise.
674  */
isp_pipeline_enable(struct isp_pipeline * pipe,enum isp_pipeline_stream_state mode)675 static int isp_pipeline_enable(struct isp_pipeline *pipe,
676 			       enum isp_pipeline_stream_state mode)
677 {
678 	struct isp_device *isp = pipe->output->isp;
679 	struct media_entity *entity;
680 	struct media_pad *pad;
681 	struct v4l2_subdev *subdev;
682 	unsigned long flags;
683 	int ret;
684 
685 	/* Refuse to start streaming if an entity included in the pipeline has
686 	 * crashed. This check must be performed before the loop below to avoid
687 	 * starting entities if the pipeline won't start anyway (those entities
688 	 * would then likely fail to stop, making the problem worse).
689 	 */
690 	if (media_entity_enum_intersects(&pipe->ent_enum, &isp->crashed))
691 		return -EIO;
692 
693 	spin_lock_irqsave(&pipe->lock, flags);
694 	pipe->state &= ~(ISP_PIPELINE_IDLE_INPUT | ISP_PIPELINE_IDLE_OUTPUT);
695 	spin_unlock_irqrestore(&pipe->lock, flags);
696 
697 	pipe->do_propagation = false;
698 
699 	entity = &pipe->output->video.entity;
700 	while (1) {
701 		pad = &entity->pads[0];
702 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
703 			break;
704 
705 		pad = media_entity_remote_pad(pad);
706 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
707 			break;
708 
709 		entity = pad->entity;
710 		subdev = media_entity_to_v4l2_subdev(entity);
711 
712 		ret = v4l2_subdev_call(subdev, video, s_stream, mode);
713 		if (ret < 0 && ret != -ENOIOCTLCMD)
714 			return ret;
715 
716 		if (subdev == &isp->isp_ccdc.subdev) {
717 			v4l2_subdev_call(&isp->isp_aewb.subdev, video,
718 					s_stream, mode);
719 			v4l2_subdev_call(&isp->isp_af.subdev, video,
720 					s_stream, mode);
721 			v4l2_subdev_call(&isp->isp_hist.subdev, video,
722 					s_stream, mode);
723 			pipe->do_propagation = true;
724 		}
725 
726 		/* Stop at the first external sub-device. */
727 		if (subdev->dev != isp->dev)
728 			break;
729 	}
730 
731 	return 0;
732 }
733 
isp_pipeline_wait_resizer(struct isp_device * isp)734 static int isp_pipeline_wait_resizer(struct isp_device *isp)
735 {
736 	return omap3isp_resizer_busy(&isp->isp_res);
737 }
738 
isp_pipeline_wait_preview(struct isp_device * isp)739 static int isp_pipeline_wait_preview(struct isp_device *isp)
740 {
741 	return omap3isp_preview_busy(&isp->isp_prev);
742 }
743 
isp_pipeline_wait_ccdc(struct isp_device * isp)744 static int isp_pipeline_wait_ccdc(struct isp_device *isp)
745 {
746 	return omap3isp_stat_busy(&isp->isp_af)
747 	    || omap3isp_stat_busy(&isp->isp_aewb)
748 	    || omap3isp_stat_busy(&isp->isp_hist)
749 	    || omap3isp_ccdc_busy(&isp->isp_ccdc);
750 }
751 
752 #define ISP_STOP_TIMEOUT	msecs_to_jiffies(1000)
753 
isp_pipeline_wait(struct isp_device * isp,int (* busy)(struct isp_device * isp))754 static int isp_pipeline_wait(struct isp_device *isp,
755 			     int(*busy)(struct isp_device *isp))
756 {
757 	unsigned long timeout = jiffies + ISP_STOP_TIMEOUT;
758 
759 	while (!time_after(jiffies, timeout)) {
760 		if (!busy(isp))
761 			return 0;
762 	}
763 
764 	return 1;
765 }
766 
767 /*
768  * isp_pipeline_disable - Disable streaming on a pipeline
769  * @pipe: ISP pipeline
770  *
771  * Walk the entities chain starting at the pipeline output video node and stop
772  * all modules in the chain. Wait synchronously for the modules to be stopped if
773  * necessary.
774  *
775  * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
776  * can't be stopped (in which case a software reset of the ISP is probably
777  * necessary).
778  */
isp_pipeline_disable(struct isp_pipeline * pipe)779 static int isp_pipeline_disable(struct isp_pipeline *pipe)
780 {
781 	struct isp_device *isp = pipe->output->isp;
782 	struct media_entity *entity;
783 	struct media_pad *pad;
784 	struct v4l2_subdev *subdev;
785 	int failure = 0;
786 	int ret;
787 
788 	/*
789 	 * We need to stop all the modules after CCDC first or they'll
790 	 * never stop since they may not get a full frame from CCDC.
791 	 */
792 	entity = &pipe->output->video.entity;
793 	while (1) {
794 		pad = &entity->pads[0];
795 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
796 			break;
797 
798 		pad = media_entity_remote_pad(pad);
799 		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
800 			break;
801 
802 		entity = pad->entity;
803 		subdev = media_entity_to_v4l2_subdev(entity);
804 
805 		if (subdev == &isp->isp_ccdc.subdev) {
806 			v4l2_subdev_call(&isp->isp_aewb.subdev,
807 					 video, s_stream, 0);
808 			v4l2_subdev_call(&isp->isp_af.subdev,
809 					 video, s_stream, 0);
810 			v4l2_subdev_call(&isp->isp_hist.subdev,
811 					 video, s_stream, 0);
812 		}
813 
814 		ret = v4l2_subdev_call(subdev, video, s_stream, 0);
815 
816 		if (subdev == &isp->isp_res.subdev)
817 			ret |= isp_pipeline_wait(isp, isp_pipeline_wait_resizer);
818 		else if (subdev == &isp->isp_prev.subdev)
819 			ret |= isp_pipeline_wait(isp, isp_pipeline_wait_preview);
820 		else if (subdev == &isp->isp_ccdc.subdev)
821 			ret |= isp_pipeline_wait(isp, isp_pipeline_wait_ccdc);
822 
823 		/* Handle stop failures. An entity that fails to stop can
824 		 * usually just be restarted. Flag the stop failure nonetheless
825 		 * to trigger an ISP reset the next time the device is released,
826 		 * just in case.
827 		 *
828 		 * The preview engine is a special case. A failure to stop can
829 		 * mean a hardware crash. When that happens the preview engine
830 		 * won't respond to read/write operations on the L4 bus anymore,
831 		 * resulting in a bus fault and a kernel oops next time it gets
832 		 * accessed. Mark it as crashed to prevent pipelines including
833 		 * it from being started.
834 		 */
835 		if (ret) {
836 			dev_info(isp->dev, "Unable to stop %s\n", subdev->name);
837 			isp->stop_failure = true;
838 			if (subdev == &isp->isp_prev.subdev)
839 				media_entity_enum_set(&isp->crashed,
840 						      &subdev->entity);
841 			failure = -ETIMEDOUT;
842 		}
843 
844 		/* Stop at the first external sub-device. */
845 		if (subdev->dev != isp->dev)
846 			break;
847 	}
848 
849 	return failure;
850 }
851 
852 /*
853  * omap3isp_pipeline_set_stream - Enable/disable streaming on a pipeline
854  * @pipe: ISP pipeline
855  * @state: Stream state (stopped, single shot or continuous)
856  *
857  * Set the pipeline to the given stream state. Pipelines can be started in
858  * single-shot or continuous mode.
859  *
860  * Return 0 if successful, or the return value of the failed video::s_stream
861  * operation otherwise. The pipeline state is not updated when the operation
862  * fails, except when stopping the pipeline.
863  */
omap3isp_pipeline_set_stream(struct isp_pipeline * pipe,enum isp_pipeline_stream_state state)864 int omap3isp_pipeline_set_stream(struct isp_pipeline *pipe,
865 				 enum isp_pipeline_stream_state state)
866 {
867 	int ret;
868 
869 	if (state == ISP_PIPELINE_STREAM_STOPPED)
870 		ret = isp_pipeline_disable(pipe);
871 	else
872 		ret = isp_pipeline_enable(pipe, state);
873 
874 	if (ret == 0 || state == ISP_PIPELINE_STREAM_STOPPED)
875 		pipe->stream_state = state;
876 
877 	return ret;
878 }
879 
880 /*
881  * omap3isp_pipeline_cancel_stream - Cancel stream on a pipeline
882  * @pipe: ISP pipeline
883  *
884  * Cancelling a stream mark all buffers on all video nodes in the pipeline as
885  * erroneous and makes sure no new buffer can be queued. This function is called
886  * when a fatal error that prevents any further operation on the pipeline
887  * occurs.
888  */
omap3isp_pipeline_cancel_stream(struct isp_pipeline * pipe)889 void omap3isp_pipeline_cancel_stream(struct isp_pipeline *pipe)
890 {
891 	if (pipe->input)
892 		omap3isp_video_cancel_stream(pipe->input);
893 	if (pipe->output)
894 		omap3isp_video_cancel_stream(pipe->output);
895 }
896 
897 /*
898  * isp_pipeline_resume - Resume streaming on a pipeline
899  * @pipe: ISP pipeline
900  *
901  * Resume video output and input and re-enable pipeline.
902  */
isp_pipeline_resume(struct isp_pipeline * pipe)903 static void isp_pipeline_resume(struct isp_pipeline *pipe)
904 {
905 	int singleshot = pipe->stream_state == ISP_PIPELINE_STREAM_SINGLESHOT;
906 
907 	omap3isp_video_resume(pipe->output, !singleshot);
908 	if (singleshot)
909 		omap3isp_video_resume(pipe->input, 0);
910 	isp_pipeline_enable(pipe, pipe->stream_state);
911 }
912 
913 /*
914  * isp_pipeline_suspend - Suspend streaming on a pipeline
915  * @pipe: ISP pipeline
916  *
917  * Suspend pipeline.
918  */
isp_pipeline_suspend(struct isp_pipeline * pipe)919 static void isp_pipeline_suspend(struct isp_pipeline *pipe)
920 {
921 	isp_pipeline_disable(pipe);
922 }
923 
924 /*
925  * isp_pipeline_is_last - Verify if entity has an enabled link to the output
926  *			  video node
927  * @me: ISP module's media entity
928  *
929  * Returns 1 if the entity has an enabled link to the output video node or 0
930  * otherwise. It's true only while pipeline can have no more than one output
931  * node.
932  */
isp_pipeline_is_last(struct media_entity * me)933 static int isp_pipeline_is_last(struct media_entity *me)
934 {
935 	struct isp_pipeline *pipe;
936 	struct media_pad *pad;
937 
938 	if (!me->pipe)
939 		return 0;
940 	pipe = to_isp_pipeline(me);
941 	if (pipe->stream_state == ISP_PIPELINE_STREAM_STOPPED)
942 		return 0;
943 	pad = media_entity_remote_pad(&pipe->output->pad);
944 	return pad->entity == me;
945 }
946 
947 /*
948  * isp_suspend_module_pipeline - Suspend pipeline to which belongs the module
949  * @me: ISP module's media entity
950  *
951  * Suspend the whole pipeline if module's entity has an enabled link to the
952  * output video node. It works only while pipeline can have no more than one
953  * output node.
954  */
isp_suspend_module_pipeline(struct media_entity * me)955 static void isp_suspend_module_pipeline(struct media_entity *me)
956 {
957 	if (isp_pipeline_is_last(me))
958 		isp_pipeline_suspend(to_isp_pipeline(me));
959 }
960 
961 /*
962  * isp_resume_module_pipeline - Resume pipeline to which belongs the module
963  * @me: ISP module's media entity
964  *
965  * Resume the whole pipeline if module's entity has an enabled link to the
966  * output video node. It works only while pipeline can have no more than one
967  * output node.
968  */
isp_resume_module_pipeline(struct media_entity * me)969 static void isp_resume_module_pipeline(struct media_entity *me)
970 {
971 	if (isp_pipeline_is_last(me))
972 		isp_pipeline_resume(to_isp_pipeline(me));
973 }
974 
975 /*
976  * isp_suspend_modules - Suspend ISP submodules.
977  * @isp: OMAP3 ISP device
978  *
979  * Returns 0 if suspend left in idle state all the submodules properly,
980  * or returns 1 if a general Reset is required to suspend the submodules.
981  */
isp_suspend_modules(struct isp_device * isp)982 static int __maybe_unused isp_suspend_modules(struct isp_device *isp)
983 {
984 	unsigned long timeout;
985 
986 	omap3isp_stat_suspend(&isp->isp_aewb);
987 	omap3isp_stat_suspend(&isp->isp_af);
988 	omap3isp_stat_suspend(&isp->isp_hist);
989 	isp_suspend_module_pipeline(&isp->isp_res.subdev.entity);
990 	isp_suspend_module_pipeline(&isp->isp_prev.subdev.entity);
991 	isp_suspend_module_pipeline(&isp->isp_ccdc.subdev.entity);
992 	isp_suspend_module_pipeline(&isp->isp_csi2a.subdev.entity);
993 	isp_suspend_module_pipeline(&isp->isp_ccp2.subdev.entity);
994 
995 	timeout = jiffies + ISP_STOP_TIMEOUT;
996 	while (omap3isp_stat_busy(&isp->isp_af)
997 	    || omap3isp_stat_busy(&isp->isp_aewb)
998 	    || omap3isp_stat_busy(&isp->isp_hist)
999 	    || omap3isp_preview_busy(&isp->isp_prev)
1000 	    || omap3isp_resizer_busy(&isp->isp_res)
1001 	    || omap3isp_ccdc_busy(&isp->isp_ccdc)) {
1002 		if (time_after(jiffies, timeout)) {
1003 			dev_info(isp->dev, "can't stop modules.\n");
1004 			return 1;
1005 		}
1006 		msleep(1);
1007 	}
1008 
1009 	return 0;
1010 }
1011 
1012 /*
1013  * isp_resume_modules - Resume ISP submodules.
1014  * @isp: OMAP3 ISP device
1015  */
isp_resume_modules(struct isp_device * isp)1016 static void __maybe_unused isp_resume_modules(struct isp_device *isp)
1017 {
1018 	omap3isp_stat_resume(&isp->isp_aewb);
1019 	omap3isp_stat_resume(&isp->isp_af);
1020 	omap3isp_stat_resume(&isp->isp_hist);
1021 	isp_resume_module_pipeline(&isp->isp_res.subdev.entity);
1022 	isp_resume_module_pipeline(&isp->isp_prev.subdev.entity);
1023 	isp_resume_module_pipeline(&isp->isp_ccdc.subdev.entity);
1024 	isp_resume_module_pipeline(&isp->isp_csi2a.subdev.entity);
1025 	isp_resume_module_pipeline(&isp->isp_ccp2.subdev.entity);
1026 }
1027 
1028 /*
1029  * isp_reset - Reset ISP with a timeout wait for idle.
1030  * @isp: OMAP3 ISP device
1031  */
isp_reset(struct isp_device * isp)1032 static int isp_reset(struct isp_device *isp)
1033 {
1034 	unsigned long timeout = 0;
1035 
1036 	isp_reg_writel(isp,
1037 		       isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG)
1038 		       | ISP_SYSCONFIG_SOFTRESET,
1039 		       OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
1040 	while (!(isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN,
1041 			       ISP_SYSSTATUS) & 0x1)) {
1042 		if (timeout++ > 10000) {
1043 			dev_alert(isp->dev, "cannot reset ISP\n");
1044 			return -ETIMEDOUT;
1045 		}
1046 		udelay(1);
1047 	}
1048 
1049 	isp->stop_failure = false;
1050 	media_entity_enum_zero(&isp->crashed);
1051 	return 0;
1052 }
1053 
1054 /*
1055  * isp_save_context - Saves the values of the ISP module registers.
1056  * @isp: OMAP3 ISP device
1057  * @reg_list: Structure containing pairs of register address and value to
1058  *            modify on OMAP.
1059  */
1060 static void
isp_save_context(struct isp_device * isp,struct isp_reg * reg_list)1061 isp_save_context(struct isp_device *isp, struct isp_reg *reg_list)
1062 {
1063 	struct isp_reg *next = reg_list;
1064 
1065 	for (; next->reg != ISP_TOK_TERM; next++)
1066 		next->val = isp_reg_readl(isp, next->mmio_range, next->reg);
1067 }
1068 
1069 /*
1070  * isp_restore_context - Restores the values of the ISP module registers.
1071  * @isp: OMAP3 ISP device
1072  * @reg_list: Structure containing pairs of register address and value to
1073  *            modify on OMAP.
1074  */
1075 static void
isp_restore_context(struct isp_device * isp,struct isp_reg * reg_list)1076 isp_restore_context(struct isp_device *isp, struct isp_reg *reg_list)
1077 {
1078 	struct isp_reg *next = reg_list;
1079 
1080 	for (; next->reg != ISP_TOK_TERM; next++)
1081 		isp_reg_writel(isp, next->val, next->mmio_range, next->reg);
1082 }
1083 
1084 /*
1085  * isp_save_ctx - Saves ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
1086  * @isp: OMAP3 ISP device
1087  *
1088  * Routine for saving the context of each module in the ISP.
1089  * CCDC, HIST, H3A, PREV, RESZ and MMU.
1090  */
isp_save_ctx(struct isp_device * isp)1091 static void isp_save_ctx(struct isp_device *isp)
1092 {
1093 	isp_save_context(isp, isp_reg_list);
1094 	omap_iommu_save_ctx(isp->dev);
1095 }
1096 
1097 /*
1098  * isp_restore_ctx - Restores ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
1099  * @isp: OMAP3 ISP device
1100  *
1101  * Routine for restoring the context of each module in the ISP.
1102  * CCDC, HIST, H3A, PREV, RESZ and MMU.
1103  */
isp_restore_ctx(struct isp_device * isp)1104 static void isp_restore_ctx(struct isp_device *isp)
1105 {
1106 	isp_restore_context(isp, isp_reg_list);
1107 	omap_iommu_restore_ctx(isp->dev);
1108 	omap3isp_ccdc_restore_context(isp);
1109 	omap3isp_preview_restore_context(isp);
1110 }
1111 
1112 /* -----------------------------------------------------------------------------
1113  * SBL resources management
1114  */
1115 #define OMAP3_ISP_SBL_READ	(OMAP3_ISP_SBL_CSI1_READ | \
1116 				 OMAP3_ISP_SBL_CCDC_LSC_READ | \
1117 				 OMAP3_ISP_SBL_PREVIEW_READ | \
1118 				 OMAP3_ISP_SBL_RESIZER_READ)
1119 #define OMAP3_ISP_SBL_WRITE	(OMAP3_ISP_SBL_CSI1_WRITE | \
1120 				 OMAP3_ISP_SBL_CSI2A_WRITE | \
1121 				 OMAP3_ISP_SBL_CSI2C_WRITE | \
1122 				 OMAP3_ISP_SBL_CCDC_WRITE | \
1123 				 OMAP3_ISP_SBL_PREVIEW_WRITE)
1124 
omap3isp_sbl_enable(struct isp_device * isp,enum isp_sbl_resource res)1125 void omap3isp_sbl_enable(struct isp_device *isp, enum isp_sbl_resource res)
1126 {
1127 	u32 sbl = 0;
1128 
1129 	isp->sbl_resources |= res;
1130 
1131 	if (isp->sbl_resources & OMAP3_ISP_SBL_CSI1_READ)
1132 		sbl |= ISPCTRL_SBL_SHARED_RPORTA;
1133 
1134 	if (isp->sbl_resources & OMAP3_ISP_SBL_CCDC_LSC_READ)
1135 		sbl |= ISPCTRL_SBL_SHARED_RPORTB;
1136 
1137 	if (isp->sbl_resources & OMAP3_ISP_SBL_CSI2C_WRITE)
1138 		sbl |= ISPCTRL_SBL_SHARED_WPORTC;
1139 
1140 	if (isp->sbl_resources & OMAP3_ISP_SBL_RESIZER_WRITE)
1141 		sbl |= ISPCTRL_SBL_WR0_RAM_EN;
1142 
1143 	if (isp->sbl_resources & OMAP3_ISP_SBL_WRITE)
1144 		sbl |= ISPCTRL_SBL_WR1_RAM_EN;
1145 
1146 	if (isp->sbl_resources & OMAP3_ISP_SBL_READ)
1147 		sbl |= ISPCTRL_SBL_RD_RAM_EN;
1148 
1149 	isp_reg_set(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, sbl);
1150 }
1151 
omap3isp_sbl_disable(struct isp_device * isp,enum isp_sbl_resource res)1152 void omap3isp_sbl_disable(struct isp_device *isp, enum isp_sbl_resource res)
1153 {
1154 	u32 sbl = 0;
1155 
1156 	isp->sbl_resources &= ~res;
1157 
1158 	if (!(isp->sbl_resources & OMAP3_ISP_SBL_CSI1_READ))
1159 		sbl |= ISPCTRL_SBL_SHARED_RPORTA;
1160 
1161 	if (!(isp->sbl_resources & OMAP3_ISP_SBL_CCDC_LSC_READ))
1162 		sbl |= ISPCTRL_SBL_SHARED_RPORTB;
1163 
1164 	if (!(isp->sbl_resources & OMAP3_ISP_SBL_CSI2C_WRITE))
1165 		sbl |= ISPCTRL_SBL_SHARED_WPORTC;
1166 
1167 	if (!(isp->sbl_resources & OMAP3_ISP_SBL_RESIZER_WRITE))
1168 		sbl |= ISPCTRL_SBL_WR0_RAM_EN;
1169 
1170 	if (!(isp->sbl_resources & OMAP3_ISP_SBL_WRITE))
1171 		sbl |= ISPCTRL_SBL_WR1_RAM_EN;
1172 
1173 	if (!(isp->sbl_resources & OMAP3_ISP_SBL_READ))
1174 		sbl |= ISPCTRL_SBL_RD_RAM_EN;
1175 
1176 	isp_reg_clr(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, sbl);
1177 }
1178 
1179 /*
1180  * isp_module_sync_idle - Helper to sync module with its idle state
1181  * @me: ISP submodule's media entity
1182  * @wait: ISP submodule's wait queue for streamoff/interrupt synchronization
1183  * @stopping: flag which tells module wants to stop
1184  *
1185  * This function checks if ISP submodule needs to wait for next interrupt. If
1186  * yes, makes the caller to sleep while waiting for such event.
1187  */
omap3isp_module_sync_idle(struct media_entity * me,wait_queue_head_t * wait,atomic_t * stopping)1188 int omap3isp_module_sync_idle(struct media_entity *me, wait_queue_head_t *wait,
1189 			      atomic_t *stopping)
1190 {
1191 	struct isp_pipeline *pipe = to_isp_pipeline(me);
1192 
1193 	if (pipe->stream_state == ISP_PIPELINE_STREAM_STOPPED ||
1194 	    (pipe->stream_state == ISP_PIPELINE_STREAM_SINGLESHOT &&
1195 	     !isp_pipeline_ready(pipe)))
1196 		return 0;
1197 
1198 	/*
1199 	 * atomic_set() doesn't include memory barrier on ARM platform for SMP
1200 	 * scenario. We'll call it here to avoid race conditions.
1201 	 */
1202 	atomic_set(stopping, 1);
1203 	smp_mb();
1204 
1205 	/*
1206 	 * If module is the last one, it's writing to memory. In this case,
1207 	 * it's necessary to check if the module is already paused due to
1208 	 * DMA queue underrun or if it has to wait for next interrupt to be
1209 	 * idle.
1210 	 * If it isn't the last one, the function won't sleep but *stopping
1211 	 * will still be set to warn next submodule caller's interrupt the
1212 	 * module wants to be idle.
1213 	 */
1214 	if (isp_pipeline_is_last(me)) {
1215 		struct isp_video *video = pipe->output;
1216 		unsigned long flags;
1217 		spin_lock_irqsave(&video->irqlock, flags);
1218 		if (video->dmaqueue_flags & ISP_VIDEO_DMAQUEUE_UNDERRUN) {
1219 			spin_unlock_irqrestore(&video->irqlock, flags);
1220 			atomic_set(stopping, 0);
1221 			smp_mb();
1222 			return 0;
1223 		}
1224 		spin_unlock_irqrestore(&video->irqlock, flags);
1225 		if (!wait_event_timeout(*wait, !atomic_read(stopping),
1226 					msecs_to_jiffies(1000))) {
1227 			atomic_set(stopping, 0);
1228 			smp_mb();
1229 			return -ETIMEDOUT;
1230 		}
1231 	}
1232 
1233 	return 0;
1234 }
1235 
1236 /*
1237  * omap3isp_module_sync_is_stopping - Helper to verify if module was stopping
1238  * @wait: ISP submodule's wait queue for streamoff/interrupt synchronization
1239  * @stopping: flag which tells module wants to stop
1240  *
1241  * This function checks if ISP submodule was stopping. In case of yes, it
1242  * notices the caller by setting stopping to 0 and waking up the wait queue.
1243  * Returns 1 if it was stopping or 0 otherwise.
1244  */
omap3isp_module_sync_is_stopping(wait_queue_head_t * wait,atomic_t * stopping)1245 int omap3isp_module_sync_is_stopping(wait_queue_head_t *wait,
1246 				     atomic_t *stopping)
1247 {
1248 	if (atomic_cmpxchg(stopping, 1, 0)) {
1249 		wake_up(wait);
1250 		return 1;
1251 	}
1252 
1253 	return 0;
1254 }
1255 
1256 /* --------------------------------------------------------------------------
1257  * Clock management
1258  */
1259 
1260 #define ISPCTRL_CLKS_MASK	(ISPCTRL_H3A_CLK_EN | \
1261 				 ISPCTRL_HIST_CLK_EN | \
1262 				 ISPCTRL_RSZ_CLK_EN | \
1263 				 (ISPCTRL_CCDC_CLK_EN | ISPCTRL_CCDC_RAM_EN) | \
1264 				 (ISPCTRL_PREV_CLK_EN | ISPCTRL_PREV_RAM_EN))
1265 
__isp_subclk_update(struct isp_device * isp)1266 static void __isp_subclk_update(struct isp_device *isp)
1267 {
1268 	u32 clk = 0;
1269 
1270 	/* AEWB and AF share the same clock. */
1271 	if (isp->subclk_resources &
1272 	    (OMAP3_ISP_SUBCLK_AEWB | OMAP3_ISP_SUBCLK_AF))
1273 		clk |= ISPCTRL_H3A_CLK_EN;
1274 
1275 	if (isp->subclk_resources & OMAP3_ISP_SUBCLK_HIST)
1276 		clk |= ISPCTRL_HIST_CLK_EN;
1277 
1278 	if (isp->subclk_resources & OMAP3_ISP_SUBCLK_RESIZER)
1279 		clk |= ISPCTRL_RSZ_CLK_EN;
1280 
1281 	/* NOTE: For CCDC & Preview submodules, we need to affect internal
1282 	 *       RAM as well.
1283 	 */
1284 	if (isp->subclk_resources & OMAP3_ISP_SUBCLK_CCDC)
1285 		clk |= ISPCTRL_CCDC_CLK_EN | ISPCTRL_CCDC_RAM_EN;
1286 
1287 	if (isp->subclk_resources & OMAP3_ISP_SUBCLK_PREVIEW)
1288 		clk |= ISPCTRL_PREV_CLK_EN | ISPCTRL_PREV_RAM_EN;
1289 
1290 	isp_reg_clr_set(isp, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL,
1291 			ISPCTRL_CLKS_MASK, clk);
1292 }
1293 
omap3isp_subclk_enable(struct isp_device * isp,enum isp_subclk_resource res)1294 void omap3isp_subclk_enable(struct isp_device *isp,
1295 			    enum isp_subclk_resource res)
1296 {
1297 	isp->subclk_resources |= res;
1298 
1299 	__isp_subclk_update(isp);
1300 }
1301 
omap3isp_subclk_disable(struct isp_device * isp,enum isp_subclk_resource res)1302 void omap3isp_subclk_disable(struct isp_device *isp,
1303 			     enum isp_subclk_resource res)
1304 {
1305 	isp->subclk_resources &= ~res;
1306 
1307 	__isp_subclk_update(isp);
1308 }
1309 
1310 /*
1311  * isp_enable_clocks - Enable ISP clocks
1312  * @isp: OMAP3 ISP device
1313  *
1314  * Return 0 if successful, or clk_prepare_enable return value if any of them
1315  * fails.
1316  */
isp_enable_clocks(struct isp_device * isp)1317 static int isp_enable_clocks(struct isp_device *isp)
1318 {
1319 	int r;
1320 	unsigned long rate;
1321 
1322 	r = clk_prepare_enable(isp->clock[ISP_CLK_CAM_ICK]);
1323 	if (r) {
1324 		dev_err(isp->dev, "failed to enable cam_ick clock\n");
1325 		goto out_clk_enable_ick;
1326 	}
1327 	r = clk_set_rate(isp->clock[ISP_CLK_CAM_MCLK], CM_CAM_MCLK_HZ);
1328 	if (r) {
1329 		dev_err(isp->dev, "clk_set_rate for cam_mclk failed\n");
1330 		goto out_clk_enable_mclk;
1331 	}
1332 	r = clk_prepare_enable(isp->clock[ISP_CLK_CAM_MCLK]);
1333 	if (r) {
1334 		dev_err(isp->dev, "failed to enable cam_mclk clock\n");
1335 		goto out_clk_enable_mclk;
1336 	}
1337 	rate = clk_get_rate(isp->clock[ISP_CLK_CAM_MCLK]);
1338 	if (rate != CM_CAM_MCLK_HZ)
1339 		dev_warn(isp->dev, "unexpected cam_mclk rate:\n"
1340 				   " expected : %d\n"
1341 				   " actual   : %ld\n", CM_CAM_MCLK_HZ, rate);
1342 	r = clk_prepare_enable(isp->clock[ISP_CLK_CSI2_FCK]);
1343 	if (r) {
1344 		dev_err(isp->dev, "failed to enable csi2_fck clock\n");
1345 		goto out_clk_enable_csi2_fclk;
1346 	}
1347 	return 0;
1348 
1349 out_clk_enable_csi2_fclk:
1350 	clk_disable_unprepare(isp->clock[ISP_CLK_CAM_MCLK]);
1351 out_clk_enable_mclk:
1352 	clk_disable_unprepare(isp->clock[ISP_CLK_CAM_ICK]);
1353 out_clk_enable_ick:
1354 	return r;
1355 }
1356 
1357 /*
1358  * isp_disable_clocks - Disable ISP clocks
1359  * @isp: OMAP3 ISP device
1360  */
isp_disable_clocks(struct isp_device * isp)1361 static void isp_disable_clocks(struct isp_device *isp)
1362 {
1363 	clk_disable_unprepare(isp->clock[ISP_CLK_CAM_ICK]);
1364 	clk_disable_unprepare(isp->clock[ISP_CLK_CAM_MCLK]);
1365 	clk_disable_unprepare(isp->clock[ISP_CLK_CSI2_FCK]);
1366 }
1367 
1368 static const char *isp_clocks[] = {
1369 	"cam_ick",
1370 	"cam_mclk",
1371 	"csi2_96m_fck",
1372 	"l3_ick",
1373 };
1374 
isp_get_clocks(struct isp_device * isp)1375 static int isp_get_clocks(struct isp_device *isp)
1376 {
1377 	struct clk *clk;
1378 	unsigned int i;
1379 
1380 	for (i = 0; i < ARRAY_SIZE(isp_clocks); ++i) {
1381 		clk = devm_clk_get(isp->dev, isp_clocks[i]);
1382 		if (IS_ERR(clk)) {
1383 			dev_err(isp->dev, "clk_get %s failed\n", isp_clocks[i]);
1384 			return PTR_ERR(clk);
1385 		}
1386 
1387 		isp->clock[i] = clk;
1388 	}
1389 
1390 	return 0;
1391 }
1392 
1393 /*
1394  * omap3isp_get - Acquire the ISP resource.
1395  *
1396  * Initializes the clocks for the first acquire.
1397  *
1398  * Increment the reference count on the ISP. If the first reference is taken,
1399  * enable clocks and power-up all submodules.
1400  *
1401  * Return a pointer to the ISP device structure, or NULL if an error occurred.
1402  */
__omap3isp_get(struct isp_device * isp,bool irq)1403 static struct isp_device *__omap3isp_get(struct isp_device *isp, bool irq)
1404 {
1405 	struct isp_device *__isp = isp;
1406 
1407 	if (isp == NULL)
1408 		return NULL;
1409 
1410 	mutex_lock(&isp->isp_mutex);
1411 	if (isp->ref_count > 0)
1412 		goto out;
1413 
1414 	if (isp_enable_clocks(isp) < 0) {
1415 		__isp = NULL;
1416 		goto out;
1417 	}
1418 
1419 	/* We don't want to restore context before saving it! */
1420 	if (isp->has_context)
1421 		isp_restore_ctx(isp);
1422 
1423 	if (irq)
1424 		isp_enable_interrupts(isp);
1425 
1426 out:
1427 	if (__isp != NULL)
1428 		isp->ref_count++;
1429 	mutex_unlock(&isp->isp_mutex);
1430 
1431 	return __isp;
1432 }
1433 
omap3isp_get(struct isp_device * isp)1434 struct isp_device *omap3isp_get(struct isp_device *isp)
1435 {
1436 	return __omap3isp_get(isp, true);
1437 }
1438 
1439 /*
1440  * omap3isp_put - Release the ISP
1441  *
1442  * Decrement the reference count on the ISP. If the last reference is released,
1443  * power-down all submodules, disable clocks and free temporary buffers.
1444  */
__omap3isp_put(struct isp_device * isp,bool save_ctx)1445 static void __omap3isp_put(struct isp_device *isp, bool save_ctx)
1446 {
1447 	if (isp == NULL)
1448 		return;
1449 
1450 	mutex_lock(&isp->isp_mutex);
1451 	BUG_ON(isp->ref_count == 0);
1452 	if (--isp->ref_count == 0) {
1453 		isp_disable_interrupts(isp);
1454 		if (save_ctx) {
1455 			isp_save_ctx(isp);
1456 			isp->has_context = 1;
1457 		}
1458 		/* Reset the ISP if an entity has failed to stop. This is the
1459 		 * only way to recover from such conditions.
1460 		 */
1461 		if (!media_entity_enum_empty(&isp->crashed) ||
1462 		    isp->stop_failure)
1463 			isp_reset(isp);
1464 		isp_disable_clocks(isp);
1465 	}
1466 	mutex_unlock(&isp->isp_mutex);
1467 }
1468 
omap3isp_put(struct isp_device * isp)1469 void omap3isp_put(struct isp_device *isp)
1470 {
1471 	__omap3isp_put(isp, true);
1472 }
1473 
1474 /* --------------------------------------------------------------------------
1475  * Platform device driver
1476  */
1477 
1478 /*
1479  * omap3isp_print_status - Prints the values of the ISP Control Module registers
1480  * @isp: OMAP3 ISP device
1481  */
1482 #define ISP_PRINT_REGISTER(isp, name)\
1483 	dev_dbg(isp->dev, "###ISP " #name "=0x%08x\n", \
1484 		isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_##name))
1485 #define SBL_PRINT_REGISTER(isp, name)\
1486 	dev_dbg(isp->dev, "###SBL " #name "=0x%08x\n", \
1487 		isp_reg_readl(isp, OMAP3_ISP_IOMEM_SBL, ISPSBL_##name))
1488 
omap3isp_print_status(struct isp_device * isp)1489 void omap3isp_print_status(struct isp_device *isp)
1490 {
1491 	dev_dbg(isp->dev, "-------------ISP Register dump--------------\n");
1492 
1493 	ISP_PRINT_REGISTER(isp, SYSCONFIG);
1494 	ISP_PRINT_REGISTER(isp, SYSSTATUS);
1495 	ISP_PRINT_REGISTER(isp, IRQ0ENABLE);
1496 	ISP_PRINT_REGISTER(isp, IRQ0STATUS);
1497 	ISP_PRINT_REGISTER(isp, TCTRL_GRESET_LENGTH);
1498 	ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_REPLAY);
1499 	ISP_PRINT_REGISTER(isp, CTRL);
1500 	ISP_PRINT_REGISTER(isp, TCTRL_CTRL);
1501 	ISP_PRINT_REGISTER(isp, TCTRL_FRAME);
1502 	ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_DELAY);
1503 	ISP_PRINT_REGISTER(isp, TCTRL_STRB_DELAY);
1504 	ISP_PRINT_REGISTER(isp, TCTRL_SHUT_DELAY);
1505 	ISP_PRINT_REGISTER(isp, TCTRL_PSTRB_LENGTH);
1506 	ISP_PRINT_REGISTER(isp, TCTRL_STRB_LENGTH);
1507 	ISP_PRINT_REGISTER(isp, TCTRL_SHUT_LENGTH);
1508 
1509 	SBL_PRINT_REGISTER(isp, PCR);
1510 	SBL_PRINT_REGISTER(isp, SDR_REQ_EXP);
1511 
1512 	dev_dbg(isp->dev, "--------------------------------------------\n");
1513 }
1514 
1515 #ifdef CONFIG_PM
1516 
1517 /*
1518  * Power management support.
1519  *
1520  * As the ISP can't properly handle an input video stream interruption on a non
1521  * frame boundary, the ISP pipelines need to be stopped before sensors get
1522  * suspended. However, as suspending the sensors can require a running clock,
1523  * which can be provided by the ISP, the ISP can't be completely suspended
1524  * before the sensor.
1525  *
1526  * To solve this problem power management support is split into prepare/complete
1527  * and suspend/resume operations. The pipelines are stopped in prepare() and the
1528  * ISP clocks get disabled in suspend(). Similarly, the clocks are reenabled in
1529  * resume(), and the the pipelines are restarted in complete().
1530  *
1531  * TODO: PM dependencies between the ISP and sensors are not modelled explicitly
1532  * yet.
1533  */
isp_pm_prepare(struct device * dev)1534 static int isp_pm_prepare(struct device *dev)
1535 {
1536 	struct isp_device *isp = dev_get_drvdata(dev);
1537 	int reset;
1538 
1539 	WARN_ON(mutex_is_locked(&isp->isp_mutex));
1540 
1541 	if (isp->ref_count == 0)
1542 		return 0;
1543 
1544 	reset = isp_suspend_modules(isp);
1545 	isp_disable_interrupts(isp);
1546 	isp_save_ctx(isp);
1547 	if (reset)
1548 		isp_reset(isp);
1549 
1550 	return 0;
1551 }
1552 
isp_pm_suspend(struct device * dev)1553 static int isp_pm_suspend(struct device *dev)
1554 {
1555 	struct isp_device *isp = dev_get_drvdata(dev);
1556 
1557 	WARN_ON(mutex_is_locked(&isp->isp_mutex));
1558 
1559 	if (isp->ref_count)
1560 		isp_disable_clocks(isp);
1561 
1562 	return 0;
1563 }
1564 
isp_pm_resume(struct device * dev)1565 static int isp_pm_resume(struct device *dev)
1566 {
1567 	struct isp_device *isp = dev_get_drvdata(dev);
1568 
1569 	if (isp->ref_count == 0)
1570 		return 0;
1571 
1572 	return isp_enable_clocks(isp);
1573 }
1574 
isp_pm_complete(struct device * dev)1575 static void isp_pm_complete(struct device *dev)
1576 {
1577 	struct isp_device *isp = dev_get_drvdata(dev);
1578 
1579 	if (isp->ref_count == 0)
1580 		return;
1581 
1582 	isp_restore_ctx(isp);
1583 	isp_enable_interrupts(isp);
1584 	isp_resume_modules(isp);
1585 }
1586 
1587 #else
1588 
1589 #define isp_pm_prepare	NULL
1590 #define isp_pm_suspend	NULL
1591 #define isp_pm_resume	NULL
1592 #define isp_pm_complete	NULL
1593 
1594 #endif /* CONFIG_PM */
1595 
isp_unregister_entities(struct isp_device * isp)1596 static void isp_unregister_entities(struct isp_device *isp)
1597 {
1598 	media_device_unregister(&isp->media_dev);
1599 
1600 	omap3isp_csi2_unregister_entities(&isp->isp_csi2a);
1601 	omap3isp_ccp2_unregister_entities(&isp->isp_ccp2);
1602 	omap3isp_ccdc_unregister_entities(&isp->isp_ccdc);
1603 	omap3isp_preview_unregister_entities(&isp->isp_prev);
1604 	omap3isp_resizer_unregister_entities(&isp->isp_res);
1605 	omap3isp_stat_unregister_entities(&isp->isp_aewb);
1606 	omap3isp_stat_unregister_entities(&isp->isp_af);
1607 	omap3isp_stat_unregister_entities(&isp->isp_hist);
1608 
1609 	v4l2_device_unregister(&isp->v4l2_dev);
1610 	media_device_cleanup(&isp->media_dev);
1611 }
1612 
isp_link_entity(struct isp_device * isp,struct media_entity * entity,enum isp_interface_type interface)1613 static int isp_link_entity(
1614 	struct isp_device *isp, struct media_entity *entity,
1615 	enum isp_interface_type interface)
1616 {
1617 	struct media_entity *input;
1618 	unsigned int flags;
1619 	unsigned int pad;
1620 	unsigned int i;
1621 
1622 	/* Connect the sensor to the correct interface module.
1623 	 * Parallel sensors are connected directly to the CCDC, while
1624 	 * serial sensors are connected to the CSI2a, CCP2b or CSI2c
1625 	 * receiver through CSIPHY1 or CSIPHY2.
1626 	 */
1627 	switch (interface) {
1628 	case ISP_INTERFACE_PARALLEL:
1629 		input = &isp->isp_ccdc.subdev.entity;
1630 		pad = CCDC_PAD_SINK;
1631 		flags = 0;
1632 		break;
1633 
1634 	case ISP_INTERFACE_CSI2A_PHY2:
1635 		input = &isp->isp_csi2a.subdev.entity;
1636 		pad = CSI2_PAD_SINK;
1637 		flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1638 		break;
1639 
1640 	case ISP_INTERFACE_CCP2B_PHY1:
1641 	case ISP_INTERFACE_CCP2B_PHY2:
1642 		input = &isp->isp_ccp2.subdev.entity;
1643 		pad = CCP2_PAD_SINK;
1644 		flags = 0;
1645 		break;
1646 
1647 	case ISP_INTERFACE_CSI2C_PHY1:
1648 		input = &isp->isp_csi2c.subdev.entity;
1649 		pad = CSI2_PAD_SINK;
1650 		flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1651 		break;
1652 
1653 	default:
1654 		dev_err(isp->dev, "%s: invalid interface type %u\n", __func__,
1655 			interface);
1656 		return -EINVAL;
1657 	}
1658 
1659 	/*
1660 	 * Not all interfaces are available on all revisions of the
1661 	 * ISP. The sub-devices of those interfaces aren't initialised
1662 	 * in such a case. Check this by ensuring the num_pads is
1663 	 * non-zero.
1664 	 */
1665 	if (!input->num_pads) {
1666 		dev_err(isp->dev, "%s: invalid input %u\n", entity->name,
1667 			interface);
1668 		return -EINVAL;
1669 	}
1670 
1671 	for (i = 0; i < entity->num_pads; i++) {
1672 		if (entity->pads[i].flags & MEDIA_PAD_FL_SOURCE)
1673 			break;
1674 	}
1675 	if (i == entity->num_pads) {
1676 		dev_err(isp->dev, "%s: no source pad in external entity %s\n",
1677 			__func__, entity->name);
1678 		return -EINVAL;
1679 	}
1680 
1681 	return media_create_pad_link(entity, i, input, pad, flags);
1682 }
1683 
isp_register_entities(struct isp_device * isp)1684 static int isp_register_entities(struct isp_device *isp)
1685 {
1686 	int ret;
1687 
1688 	isp->media_dev.dev = isp->dev;
1689 	strlcpy(isp->media_dev.model, "TI OMAP3 ISP",
1690 		sizeof(isp->media_dev.model));
1691 	isp->media_dev.hw_revision = isp->revision;
1692 	isp->media_dev.ops = &isp_media_ops;
1693 	media_device_init(&isp->media_dev);
1694 
1695 	isp->v4l2_dev.mdev = &isp->media_dev;
1696 	ret = v4l2_device_register(isp->dev, &isp->v4l2_dev);
1697 	if (ret < 0) {
1698 		dev_err(isp->dev, "%s: V4L2 device registration failed (%d)\n",
1699 			__func__, ret);
1700 		goto done;
1701 	}
1702 
1703 	/* Register internal entities */
1704 	ret = omap3isp_ccp2_register_entities(&isp->isp_ccp2, &isp->v4l2_dev);
1705 	if (ret < 0)
1706 		goto done;
1707 
1708 	ret = omap3isp_csi2_register_entities(&isp->isp_csi2a, &isp->v4l2_dev);
1709 	if (ret < 0)
1710 		goto done;
1711 
1712 	ret = omap3isp_ccdc_register_entities(&isp->isp_ccdc, &isp->v4l2_dev);
1713 	if (ret < 0)
1714 		goto done;
1715 
1716 	ret = omap3isp_preview_register_entities(&isp->isp_prev,
1717 						 &isp->v4l2_dev);
1718 	if (ret < 0)
1719 		goto done;
1720 
1721 	ret = omap3isp_resizer_register_entities(&isp->isp_res, &isp->v4l2_dev);
1722 	if (ret < 0)
1723 		goto done;
1724 
1725 	ret = omap3isp_stat_register_entities(&isp->isp_aewb, &isp->v4l2_dev);
1726 	if (ret < 0)
1727 		goto done;
1728 
1729 	ret = omap3isp_stat_register_entities(&isp->isp_af, &isp->v4l2_dev);
1730 	if (ret < 0)
1731 		goto done;
1732 
1733 	ret = omap3isp_stat_register_entities(&isp->isp_hist, &isp->v4l2_dev);
1734 	if (ret < 0)
1735 		goto done;
1736 
1737 done:
1738 	if (ret < 0)
1739 		isp_unregister_entities(isp);
1740 
1741 	return ret;
1742 }
1743 
1744 /*
1745  * isp_create_links() - Create links for internal and external ISP entities
1746  * @isp : Pointer to ISP device
1747  *
1748  * This function creates all links between ISP internal and external entities.
1749  *
1750  * Return: A negative error code on failure or zero on success. Possible error
1751  * codes are those returned by media_create_pad_link().
1752  */
isp_create_links(struct isp_device * isp)1753 static int isp_create_links(struct isp_device *isp)
1754 {
1755 	int ret;
1756 
1757 	/* Create links between entities and video nodes. */
1758 	ret = media_create_pad_link(
1759 			&isp->isp_csi2a.subdev.entity, CSI2_PAD_SOURCE,
1760 			&isp->isp_csi2a.video_out.video.entity, 0, 0);
1761 	if (ret < 0)
1762 		return ret;
1763 
1764 	ret = media_create_pad_link(
1765 			&isp->isp_ccp2.video_in.video.entity, 0,
1766 			&isp->isp_ccp2.subdev.entity, CCP2_PAD_SINK, 0);
1767 	if (ret < 0)
1768 		return ret;
1769 
1770 	ret = media_create_pad_link(
1771 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_OF,
1772 			&isp->isp_ccdc.video_out.video.entity, 0, 0);
1773 	if (ret < 0)
1774 		return ret;
1775 
1776 	ret = media_create_pad_link(
1777 			&isp->isp_prev.video_in.video.entity, 0,
1778 			&isp->isp_prev.subdev.entity, PREV_PAD_SINK, 0);
1779 	if (ret < 0)
1780 		return ret;
1781 
1782 	ret = media_create_pad_link(
1783 			&isp->isp_prev.subdev.entity, PREV_PAD_SOURCE,
1784 			&isp->isp_prev.video_out.video.entity, 0, 0);
1785 	if (ret < 0)
1786 		return ret;
1787 
1788 	ret = media_create_pad_link(
1789 			&isp->isp_res.video_in.video.entity, 0,
1790 			&isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
1791 	if (ret < 0)
1792 		return ret;
1793 
1794 	ret = media_create_pad_link(
1795 			&isp->isp_res.subdev.entity, RESZ_PAD_SOURCE,
1796 			&isp->isp_res.video_out.video.entity, 0, 0);
1797 
1798 	if (ret < 0)
1799 		return ret;
1800 
1801 	/* Create links between entities. */
1802 	ret = media_create_pad_link(
1803 			&isp->isp_csi2a.subdev.entity, CSI2_PAD_SOURCE,
1804 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SINK, 0);
1805 	if (ret < 0)
1806 		return ret;
1807 
1808 	ret = media_create_pad_link(
1809 			&isp->isp_ccp2.subdev.entity, CCP2_PAD_SOURCE,
1810 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SINK, 0);
1811 	if (ret < 0)
1812 		return ret;
1813 
1814 	ret = media_create_pad_link(
1815 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1816 			&isp->isp_prev.subdev.entity, PREV_PAD_SINK, 0);
1817 	if (ret < 0)
1818 		return ret;
1819 
1820 	ret = media_create_pad_link(
1821 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_OF,
1822 			&isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
1823 	if (ret < 0)
1824 		return ret;
1825 
1826 	ret = media_create_pad_link(
1827 			&isp->isp_prev.subdev.entity, PREV_PAD_SOURCE,
1828 			&isp->isp_res.subdev.entity, RESZ_PAD_SINK, 0);
1829 	if (ret < 0)
1830 		return ret;
1831 
1832 	ret = media_create_pad_link(
1833 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1834 			&isp->isp_aewb.subdev.entity, 0,
1835 			MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1836 	if (ret < 0)
1837 		return ret;
1838 
1839 	ret = media_create_pad_link(
1840 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1841 			&isp->isp_af.subdev.entity, 0,
1842 			MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1843 	if (ret < 0)
1844 		return ret;
1845 
1846 	ret = media_create_pad_link(
1847 			&isp->isp_ccdc.subdev.entity, CCDC_PAD_SOURCE_VP,
1848 			&isp->isp_hist.subdev.entity, 0,
1849 			MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
1850 	if (ret < 0)
1851 		return ret;
1852 
1853 	return 0;
1854 }
1855 
isp_cleanup_modules(struct isp_device * isp)1856 static void isp_cleanup_modules(struct isp_device *isp)
1857 {
1858 	omap3isp_h3a_aewb_cleanup(isp);
1859 	omap3isp_h3a_af_cleanup(isp);
1860 	omap3isp_hist_cleanup(isp);
1861 	omap3isp_resizer_cleanup(isp);
1862 	omap3isp_preview_cleanup(isp);
1863 	omap3isp_ccdc_cleanup(isp);
1864 	omap3isp_ccp2_cleanup(isp);
1865 	omap3isp_csi2_cleanup(isp);
1866 	omap3isp_csiphy_cleanup(isp);
1867 }
1868 
isp_initialize_modules(struct isp_device * isp)1869 static int isp_initialize_modules(struct isp_device *isp)
1870 {
1871 	int ret;
1872 
1873 	ret = omap3isp_csiphy_init(isp);
1874 	if (ret < 0) {
1875 		dev_err(isp->dev, "CSI PHY initialization failed\n");
1876 		return ret;
1877 	}
1878 
1879 	ret = omap3isp_csi2_init(isp);
1880 	if (ret < 0) {
1881 		dev_err(isp->dev, "CSI2 initialization failed\n");
1882 		goto error_csi2;
1883 	}
1884 
1885 	ret = omap3isp_ccp2_init(isp);
1886 	if (ret < 0) {
1887 		if (ret != -EPROBE_DEFER)
1888 			dev_err(isp->dev, "CCP2 initialization failed\n");
1889 		goto error_ccp2;
1890 	}
1891 
1892 	ret = omap3isp_ccdc_init(isp);
1893 	if (ret < 0) {
1894 		dev_err(isp->dev, "CCDC initialization failed\n");
1895 		goto error_ccdc;
1896 	}
1897 
1898 	ret = omap3isp_preview_init(isp);
1899 	if (ret < 0) {
1900 		dev_err(isp->dev, "Preview initialization failed\n");
1901 		goto error_preview;
1902 	}
1903 
1904 	ret = omap3isp_resizer_init(isp);
1905 	if (ret < 0) {
1906 		dev_err(isp->dev, "Resizer initialization failed\n");
1907 		goto error_resizer;
1908 	}
1909 
1910 	ret = omap3isp_hist_init(isp);
1911 	if (ret < 0) {
1912 		dev_err(isp->dev, "Histogram initialization failed\n");
1913 		goto error_hist;
1914 	}
1915 
1916 	ret = omap3isp_h3a_aewb_init(isp);
1917 	if (ret < 0) {
1918 		dev_err(isp->dev, "H3A AEWB initialization failed\n");
1919 		goto error_h3a_aewb;
1920 	}
1921 
1922 	ret = omap3isp_h3a_af_init(isp);
1923 	if (ret < 0) {
1924 		dev_err(isp->dev, "H3A AF initialization failed\n");
1925 		goto error_h3a_af;
1926 	}
1927 
1928 	return 0;
1929 
1930 error_h3a_af:
1931 	omap3isp_h3a_aewb_cleanup(isp);
1932 error_h3a_aewb:
1933 	omap3isp_hist_cleanup(isp);
1934 error_hist:
1935 	omap3isp_resizer_cleanup(isp);
1936 error_resizer:
1937 	omap3isp_preview_cleanup(isp);
1938 error_preview:
1939 	omap3isp_ccdc_cleanup(isp);
1940 error_ccdc:
1941 	omap3isp_ccp2_cleanup(isp);
1942 error_ccp2:
1943 	omap3isp_csi2_cleanup(isp);
1944 error_csi2:
1945 	omap3isp_csiphy_cleanup(isp);
1946 
1947 	return ret;
1948 }
1949 
isp_detach_iommu(struct isp_device * isp)1950 static void isp_detach_iommu(struct isp_device *isp)
1951 {
1952 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1953 	arm_iommu_detach_device(isp->dev);
1954 	arm_iommu_release_mapping(isp->mapping);
1955 	isp->mapping = NULL;
1956 #endif
1957 }
1958 
isp_attach_iommu(struct isp_device * isp)1959 static int isp_attach_iommu(struct isp_device *isp)
1960 {
1961 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1962 	struct dma_iommu_mapping *mapping;
1963 	int ret;
1964 
1965 	/*
1966 	 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
1967 	 * VAs. This will allocate a corresponding IOMMU domain.
1968 	 */
1969 	mapping = arm_iommu_create_mapping(&platform_bus_type, SZ_1G, SZ_2G);
1970 	if (IS_ERR(mapping)) {
1971 		dev_err(isp->dev, "failed to create ARM IOMMU mapping\n");
1972 		return PTR_ERR(mapping);
1973 	}
1974 
1975 	isp->mapping = mapping;
1976 
1977 	/* Attach the ARM VA mapping to the device. */
1978 	ret = arm_iommu_attach_device(isp->dev, mapping);
1979 	if (ret < 0) {
1980 		dev_err(isp->dev, "failed to attach device to VA mapping\n");
1981 		goto error;
1982 	}
1983 
1984 	return 0;
1985 
1986 error:
1987 	arm_iommu_release_mapping(isp->mapping);
1988 	isp->mapping = NULL;
1989 	return ret;
1990 #else
1991 	return -ENODEV;
1992 #endif
1993 }
1994 
1995 /*
1996  * isp_remove - Remove ISP platform device
1997  * @pdev: Pointer to ISP platform device
1998  *
1999  * Always returns 0.
2000  */
isp_remove(struct platform_device * pdev)2001 static int isp_remove(struct platform_device *pdev)
2002 {
2003 	struct isp_device *isp = platform_get_drvdata(pdev);
2004 
2005 	v4l2_async_notifier_unregister(&isp->notifier);
2006 	isp_unregister_entities(isp);
2007 	isp_cleanup_modules(isp);
2008 	isp_xclk_cleanup(isp);
2009 
2010 	__omap3isp_get(isp, false);
2011 	isp_detach_iommu(isp);
2012 	__omap3isp_put(isp, false);
2013 
2014 	media_entity_enum_cleanup(&isp->crashed);
2015 	v4l2_async_notifier_cleanup(&isp->notifier);
2016 
2017 	return 0;
2018 }
2019 
2020 enum isp_of_phy {
2021 	ISP_OF_PHY_PARALLEL = 0,
2022 	ISP_OF_PHY_CSIPHY1,
2023 	ISP_OF_PHY_CSIPHY2,
2024 };
2025 
isp_fwnode_parse(struct device * dev,struct v4l2_fwnode_endpoint * vep,struct v4l2_async_subdev * asd)2026 static int isp_fwnode_parse(struct device *dev,
2027 			    struct v4l2_fwnode_endpoint *vep,
2028 			    struct v4l2_async_subdev *asd)
2029 {
2030 	struct isp_async_subdev *isd =
2031 		container_of(asd, struct isp_async_subdev, asd);
2032 	struct isp_bus_cfg *buscfg = &isd->bus;
2033 	bool csi1 = false;
2034 	unsigned int i;
2035 
2036 	dev_dbg(dev, "parsing endpoint %pOF, interface %u\n",
2037 		to_of_node(vep->base.local_fwnode), vep->base.port);
2038 
2039 	switch (vep->base.port) {
2040 	case ISP_OF_PHY_PARALLEL:
2041 		buscfg->interface = ISP_INTERFACE_PARALLEL;
2042 		buscfg->bus.parallel.data_lane_shift =
2043 			vep->bus.parallel.data_shift;
2044 		buscfg->bus.parallel.clk_pol =
2045 			!!(vep->bus.parallel.flags
2046 			   & V4L2_MBUS_PCLK_SAMPLE_FALLING);
2047 		buscfg->bus.parallel.hs_pol =
2048 			!!(vep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_LOW);
2049 		buscfg->bus.parallel.vs_pol =
2050 			!!(vep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_LOW);
2051 		buscfg->bus.parallel.fld_pol =
2052 			!!(vep->bus.parallel.flags & V4L2_MBUS_FIELD_EVEN_LOW);
2053 		buscfg->bus.parallel.data_pol =
2054 			!!(vep->bus.parallel.flags & V4L2_MBUS_DATA_ACTIVE_LOW);
2055 		buscfg->bus.parallel.bt656 = vep->bus_type == V4L2_MBUS_BT656;
2056 		break;
2057 
2058 	case ISP_OF_PHY_CSIPHY1:
2059 	case ISP_OF_PHY_CSIPHY2:
2060 		switch (vep->bus_type) {
2061 		case V4L2_MBUS_CCP2:
2062 		case V4L2_MBUS_CSI1:
2063 			dev_dbg(dev, "CSI-1/CCP-2 configuration\n");
2064 			csi1 = true;
2065 			break;
2066 		case V4L2_MBUS_CSI2:
2067 			dev_dbg(dev, "CSI-2 configuration\n");
2068 			csi1 = false;
2069 			break;
2070 		default:
2071 			dev_err(dev, "unsupported bus type %u\n",
2072 				vep->bus_type);
2073 			return -EINVAL;
2074 		}
2075 
2076 		switch (vep->base.port) {
2077 		case ISP_OF_PHY_CSIPHY1:
2078 			if (csi1)
2079 				buscfg->interface = ISP_INTERFACE_CCP2B_PHY1;
2080 			else
2081 				buscfg->interface = ISP_INTERFACE_CSI2C_PHY1;
2082 			break;
2083 		case ISP_OF_PHY_CSIPHY2:
2084 			if (csi1)
2085 				buscfg->interface = ISP_INTERFACE_CCP2B_PHY2;
2086 			else
2087 				buscfg->interface = ISP_INTERFACE_CSI2A_PHY2;
2088 			break;
2089 		}
2090 		if (csi1) {
2091 			buscfg->bus.ccp2.lanecfg.clk.pos =
2092 				vep->bus.mipi_csi1.clock_lane;
2093 			buscfg->bus.ccp2.lanecfg.clk.pol =
2094 				vep->bus.mipi_csi1.lane_polarity[0];
2095 			dev_dbg(dev, "clock lane polarity %u, pos %u\n",
2096 				buscfg->bus.ccp2.lanecfg.clk.pol,
2097 				buscfg->bus.ccp2.lanecfg.clk.pos);
2098 
2099 			buscfg->bus.ccp2.lanecfg.data[0].pos =
2100 				vep->bus.mipi_csi1.data_lane;
2101 			buscfg->bus.ccp2.lanecfg.data[0].pol =
2102 				vep->bus.mipi_csi1.lane_polarity[1];
2103 
2104 			dev_dbg(dev, "data lane polarity %u, pos %u\n",
2105 				buscfg->bus.ccp2.lanecfg.data[0].pol,
2106 				buscfg->bus.ccp2.lanecfg.data[0].pos);
2107 
2108 			buscfg->bus.ccp2.strobe_clk_pol =
2109 				vep->bus.mipi_csi1.clock_inv;
2110 			buscfg->bus.ccp2.phy_layer = vep->bus.mipi_csi1.strobe;
2111 			buscfg->bus.ccp2.ccp2_mode =
2112 				vep->bus_type == V4L2_MBUS_CCP2;
2113 			buscfg->bus.ccp2.vp_clk_pol = 1;
2114 
2115 			buscfg->bus.ccp2.crc = 1;
2116 		} else {
2117 			buscfg->bus.csi2.lanecfg.clk.pos =
2118 				vep->bus.mipi_csi2.clock_lane;
2119 			buscfg->bus.csi2.lanecfg.clk.pol =
2120 				vep->bus.mipi_csi2.lane_polarities[0];
2121 			dev_dbg(dev, "clock lane polarity %u, pos %u\n",
2122 				buscfg->bus.csi2.lanecfg.clk.pol,
2123 				buscfg->bus.csi2.lanecfg.clk.pos);
2124 
2125 			buscfg->bus.csi2.num_data_lanes =
2126 				vep->bus.mipi_csi2.num_data_lanes;
2127 
2128 			for (i = 0; i < buscfg->bus.csi2.num_data_lanes; i++) {
2129 				buscfg->bus.csi2.lanecfg.data[i].pos =
2130 					vep->bus.mipi_csi2.data_lanes[i];
2131 				buscfg->bus.csi2.lanecfg.data[i].pol =
2132 					vep->bus.mipi_csi2.lane_polarities[i + 1];
2133 				dev_dbg(dev,
2134 					"data lane %u polarity %u, pos %u\n", i,
2135 					buscfg->bus.csi2.lanecfg.data[i].pol,
2136 					buscfg->bus.csi2.lanecfg.data[i].pos);
2137 			}
2138 			/*
2139 			 * FIXME: now we assume the CRC is always there.
2140 			 * Implement a way to obtain this information from the
2141 			 * sensor. Frame descriptors, perhaps?
2142 			 */
2143 			buscfg->bus.csi2.crc = 1;
2144 		}
2145 		break;
2146 
2147 	default:
2148 		dev_warn(dev, "%pOF: invalid interface %u\n",
2149 			 to_of_node(vep->base.local_fwnode), vep->base.port);
2150 		return -EINVAL;
2151 	}
2152 
2153 	return 0;
2154 }
2155 
isp_subdev_notifier_complete(struct v4l2_async_notifier * async)2156 static int isp_subdev_notifier_complete(struct v4l2_async_notifier *async)
2157 {
2158 	struct isp_device *isp = container_of(async, struct isp_device,
2159 					      notifier);
2160 	struct v4l2_device *v4l2_dev = &isp->v4l2_dev;
2161 	struct v4l2_subdev *sd;
2162 	int ret;
2163 
2164 	ret = media_entity_enum_init(&isp->crashed, &isp->media_dev);
2165 	if (ret)
2166 		return ret;
2167 
2168 	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
2169 		if (sd->notifier != &isp->notifier)
2170 			continue;
2171 
2172 		ret = isp_link_entity(isp, &sd->entity,
2173 				      v4l2_subdev_to_bus_cfg(sd)->interface);
2174 		if (ret < 0)
2175 			return ret;
2176 	}
2177 
2178 	ret = v4l2_device_register_subdev_nodes(&isp->v4l2_dev);
2179 	if (ret < 0)
2180 		return ret;
2181 
2182 	return media_device_register(&isp->media_dev);
2183 }
2184 
2185 static const struct v4l2_async_notifier_operations isp_subdev_notifier_ops = {
2186 	.complete = isp_subdev_notifier_complete,
2187 };
2188 
2189 /*
2190  * isp_probe - Probe ISP platform device
2191  * @pdev: Pointer to ISP platform device
2192  *
2193  * Returns 0 if successful,
2194  *   -ENOMEM if no memory available,
2195  *   -ENODEV if no platform device resources found
2196  *     or no space for remapping registers,
2197  *   -EINVAL if couldn't install ISR,
2198  *   or clk_get return error value.
2199  */
isp_probe(struct platform_device * pdev)2200 static int isp_probe(struct platform_device *pdev)
2201 {
2202 	struct isp_device *isp;
2203 	struct resource *mem;
2204 	int ret;
2205 	int i, m;
2206 
2207 	isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
2208 	if (!isp) {
2209 		dev_err(&pdev->dev, "could not allocate memory\n");
2210 		return -ENOMEM;
2211 	}
2212 
2213 	ret = fwnode_property_read_u32(of_fwnode_handle(pdev->dev.of_node),
2214 				       "ti,phy-type", &isp->phy_type);
2215 	if (ret)
2216 		return ret;
2217 
2218 	isp->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
2219 						      "syscon");
2220 	if (IS_ERR(isp->syscon))
2221 		return PTR_ERR(isp->syscon);
2222 
2223 	ret = of_property_read_u32_index(pdev->dev.of_node,
2224 					 "syscon", 1, &isp->syscon_offset);
2225 	if (ret)
2226 		return ret;
2227 
2228 	isp->autoidle = autoidle;
2229 
2230 	mutex_init(&isp->isp_mutex);
2231 	spin_lock_init(&isp->stat_lock);
2232 
2233 	ret = v4l2_async_notifier_parse_fwnode_endpoints(
2234 		&pdev->dev, &isp->notifier, sizeof(struct isp_async_subdev),
2235 		isp_fwnode_parse);
2236 	if (ret < 0)
2237 		goto error;
2238 
2239 	isp->dev = &pdev->dev;
2240 	isp->ref_count = 0;
2241 
2242 	ret = dma_coerce_mask_and_coherent(isp->dev, DMA_BIT_MASK(32));
2243 	if (ret)
2244 		goto error;
2245 
2246 	platform_set_drvdata(pdev, isp);
2247 
2248 	/* Regulators */
2249 	isp->isp_csiphy1.vdd = devm_regulator_get(&pdev->dev, "vdd-csiphy1");
2250 	isp->isp_csiphy2.vdd = devm_regulator_get(&pdev->dev, "vdd-csiphy2");
2251 
2252 	/* Clocks
2253 	 *
2254 	 * The ISP clock tree is revision-dependent. We thus need to enable ICLK
2255 	 * manually to read the revision before calling __omap3isp_get().
2256 	 *
2257 	 * Start by mapping the ISP MMIO area, which is in two pieces.
2258 	 * The ISP IOMMU is in between. Map both now, and fill in the
2259 	 * ISP revision specific portions a little later in the
2260 	 * function.
2261 	 */
2262 	for (i = 0; i < 2; i++) {
2263 		unsigned int map_idx = i ? OMAP3_ISP_IOMEM_CSI2A_REGS1 : 0;
2264 
2265 		mem = platform_get_resource(pdev, IORESOURCE_MEM, i);
2266 		isp->mmio_base[map_idx] =
2267 			devm_ioremap_resource(isp->dev, mem);
2268 		if (IS_ERR(isp->mmio_base[map_idx])) {
2269 			ret = PTR_ERR(isp->mmio_base[map_idx]);
2270 			goto error;
2271 		}
2272 	}
2273 
2274 	ret = isp_get_clocks(isp);
2275 	if (ret < 0)
2276 		goto error;
2277 
2278 	ret = clk_enable(isp->clock[ISP_CLK_CAM_ICK]);
2279 	if (ret < 0)
2280 		goto error;
2281 
2282 	isp->revision = isp_reg_readl(isp, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
2283 	dev_info(isp->dev, "Revision %d.%d found\n",
2284 		 (isp->revision & 0xf0) >> 4, isp->revision & 0x0f);
2285 
2286 	clk_disable(isp->clock[ISP_CLK_CAM_ICK]);
2287 
2288 	if (__omap3isp_get(isp, false) == NULL) {
2289 		ret = -ENODEV;
2290 		goto error;
2291 	}
2292 
2293 	ret = isp_reset(isp);
2294 	if (ret < 0)
2295 		goto error_isp;
2296 
2297 	ret = isp_xclk_init(isp);
2298 	if (ret < 0)
2299 		goto error_isp;
2300 
2301 	/* Memory resources */
2302 	for (m = 0; m < ARRAY_SIZE(isp_res_maps); m++)
2303 		if (isp->revision == isp_res_maps[m].isp_rev)
2304 			break;
2305 
2306 	if (m == ARRAY_SIZE(isp_res_maps)) {
2307 		dev_err(isp->dev, "No resource map found for ISP rev %d.%d\n",
2308 			(isp->revision & 0xf0) >> 4, isp->revision & 0xf);
2309 		ret = -ENODEV;
2310 		goto error_isp;
2311 	}
2312 
2313 	for (i = 1; i < OMAP3_ISP_IOMEM_CSI2A_REGS1; i++)
2314 		isp->mmio_base[i] =
2315 			isp->mmio_base[0] + isp_res_maps[m].offset[i];
2316 
2317 	for (i = OMAP3_ISP_IOMEM_CSIPHY2; i < OMAP3_ISP_IOMEM_LAST; i++)
2318 		isp->mmio_base[i] =
2319 			isp->mmio_base[OMAP3_ISP_IOMEM_CSI2A_REGS1]
2320 			+ isp_res_maps[m].offset[i];
2321 
2322 	isp->mmio_hist_base_phys =
2323 		mem->start + isp_res_maps[m].offset[OMAP3_ISP_IOMEM_HIST];
2324 
2325 	/* IOMMU */
2326 	ret = isp_attach_iommu(isp);
2327 	if (ret < 0) {
2328 		dev_err(&pdev->dev, "unable to attach to IOMMU\n");
2329 		goto error_isp;
2330 	}
2331 
2332 	/* Interrupt */
2333 	ret = platform_get_irq(pdev, 0);
2334 	if (ret <= 0) {
2335 		dev_err(isp->dev, "No IRQ resource\n");
2336 		ret = -ENODEV;
2337 		goto error_iommu;
2338 	}
2339 	isp->irq_num = ret;
2340 
2341 	if (devm_request_irq(isp->dev, isp->irq_num, isp_isr, IRQF_SHARED,
2342 			     "OMAP3 ISP", isp)) {
2343 		dev_err(isp->dev, "Unable to request IRQ\n");
2344 		ret = -EINVAL;
2345 		goto error_iommu;
2346 	}
2347 
2348 	/* Entities */
2349 	ret = isp_initialize_modules(isp);
2350 	if (ret < 0)
2351 		goto error_iommu;
2352 
2353 	ret = isp_register_entities(isp);
2354 	if (ret < 0)
2355 		goto error_modules;
2356 
2357 	ret = isp_create_links(isp);
2358 	if (ret < 0)
2359 		goto error_register_entities;
2360 
2361 	isp->notifier.ops = &isp_subdev_notifier_ops;
2362 
2363 	ret = v4l2_async_notifier_register(&isp->v4l2_dev, &isp->notifier);
2364 	if (ret)
2365 		goto error_register_entities;
2366 
2367 	isp_core_init(isp, 1);
2368 	omap3isp_put(isp);
2369 
2370 	return 0;
2371 
2372 error_register_entities:
2373 	isp_unregister_entities(isp);
2374 error_modules:
2375 	isp_cleanup_modules(isp);
2376 error_iommu:
2377 	isp_detach_iommu(isp);
2378 error_isp:
2379 	isp_xclk_cleanup(isp);
2380 	__omap3isp_put(isp, false);
2381 error:
2382 	v4l2_async_notifier_cleanup(&isp->notifier);
2383 	mutex_destroy(&isp->isp_mutex);
2384 
2385 	return ret;
2386 }
2387 
2388 static const struct dev_pm_ops omap3isp_pm_ops = {
2389 	.prepare = isp_pm_prepare,
2390 	.suspend = isp_pm_suspend,
2391 	.resume = isp_pm_resume,
2392 	.complete = isp_pm_complete,
2393 };
2394 
2395 static struct platform_device_id omap3isp_id_table[] = {
2396 	{ "omap3isp", 0 },
2397 	{ },
2398 };
2399 MODULE_DEVICE_TABLE(platform, omap3isp_id_table);
2400 
2401 static const struct of_device_id omap3isp_of_table[] = {
2402 	{ .compatible = "ti,omap3-isp" },
2403 	{ },
2404 };
2405 MODULE_DEVICE_TABLE(of, omap3isp_of_table);
2406 
2407 static struct platform_driver omap3isp_driver = {
2408 	.probe = isp_probe,
2409 	.remove = isp_remove,
2410 	.id_table = omap3isp_id_table,
2411 	.driver = {
2412 		.name = "omap3isp",
2413 		.pm	= &omap3isp_pm_ops,
2414 		.of_match_table = omap3isp_of_table,
2415 	},
2416 };
2417 
2418 module_platform_driver(omap3isp_driver);
2419 
2420 MODULE_AUTHOR("Nokia Corporation");
2421 MODULE_DESCRIPTION("TI OMAP3 ISP driver");
2422 MODULE_LICENSE("GPL");
2423 MODULE_VERSION(ISP_VIDEO_DRIVER_VERSION);
2424