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