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