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