Full support for Ginger Console
[linux-ginger.git] / drivers / media / video / isp / isp.c
blob6714bd09863a7e3c7f603ce61b7b37bdbc591784
1 /*
2 * isp.c
4 * Driver Library for ISP Control module in TI's OMAP3 Camera ISP
5 * ISP interface and IRQ related APIs are defined here.
7 * Copyright (C) 2009 Texas Instruments.
8 * Copyright (C) 2009 Nokia.
10 * Contributors:
11 * Sameer Venkatraman <sameerv@ti.com>
12 * Mohit Jalori <mjalori@ti.com>
13 * Sergio Aguirre <saaguirre@ti.com>
14 * Sakari Ailus <sakari.ailus@nokia.com>
15 * Tuukka Toivonen <tuukka.o.toivonen@nokia.com>
16 * Toni Leinonen <toni.leinonen@nokia.com>
18 * This package is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
22 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
24 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <asm/cacheflush.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/clk.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/vmalloc.h>
34 #include <linux/platform_device.h>
35 #include <linux/device.h>
37 #include "isp.h"
38 #include "ispreg.h"
39 #include "ispccdc.h"
40 #include "isph3a.h"
41 #include "isphist.h"
42 #include "isp_af.h"
43 #include "isppreview.h"
44 #include "ispresizer.h"
45 #include "ispcsi2.h"
47 static struct platform_device *omap3isp_pdev;
49 static void isp_save_ctx(struct device *dev);
51 static void isp_restore_ctx(struct device *dev);
53 static void isp_buf_init(struct device *dev);
55 /* List of image formats supported via OMAP ISP */
56 const static struct v4l2_fmtdesc isp_formats[] = {
58 .description = "UYVY, packed",
59 .pixelformat = V4L2_PIX_FMT_UYVY,
62 .description = "YUYV (YUV 4:2:2), packed",
63 .pixelformat = V4L2_PIX_FMT_YUYV,
66 .description = "Bayer10 (GrR/BGb)",
67 .pixelformat = V4L2_PIX_FMT_SGRBG10,
71 /**
72 * struct vcontrol - Video control structure.
73 * @qc: V4L2 Query control structure.
74 * @current_value: Current value of the control.
76 static struct vcontrol {
77 struct v4l2_queryctrl qc;
78 int current_value;
79 } video_control[] = {
82 .id = V4L2_CID_BRIGHTNESS,
83 .type = V4L2_CTRL_TYPE_INTEGER,
84 .name = "Brightness",
85 .minimum = ISPPRV_BRIGHT_LOW,
86 .maximum = ISPPRV_BRIGHT_HIGH,
87 .step = ISPPRV_BRIGHT_STEP,
88 .default_value = ISPPRV_BRIGHT_DEF,
90 .current_value = ISPPRV_BRIGHT_DEF,
94 .id = V4L2_CID_CONTRAST,
95 .type = V4L2_CTRL_TYPE_INTEGER,
96 .name = "Contrast",
97 .minimum = ISPPRV_CONTRAST_LOW,
98 .maximum = ISPPRV_CONTRAST_HIGH,
99 .step = ISPPRV_CONTRAST_STEP,
100 .default_value = ISPPRV_CONTRAST_DEF,
102 .current_value = ISPPRV_CONTRAST_DEF,
106 .id = V4L2_CID_COLORFX,
107 .type = V4L2_CTRL_TYPE_MENU,
108 .name = "Color Effects",
109 .minimum = V4L2_COLORFX_NONE,
110 .maximum = V4L2_COLORFX_SEPIA,
111 .step = 1,
112 .default_value = V4L2_COLORFX_NONE,
114 .current_value = V4L2_COLORFX_NONE,
118 static struct v4l2_querymenu video_menu[] = {
120 .id = V4L2_CID_COLORFX,
121 .index = 0,
122 .name = "None",
125 .id = V4L2_CID_COLORFX,
126 .index = 1,
127 .name = "B&W",
130 .id = V4L2_CID_COLORFX,
131 .index = 2,
132 .name = "Sepia",
136 /* Structure for saving/restoring ISP module registers */
137 static struct isp_reg isp_reg_list[] = {
138 {OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG, 0},
139 {OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE, 0},
140 {OMAP3_ISP_IOMEM_MAIN, ISP_IRQ1ENABLE, 0},
141 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_GRESET_LENGTH, 0},
142 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_PSTRB_REPLAY, 0},
143 {OMAP3_ISP_IOMEM_MAIN, ISP_CTRL, 0},
144 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL, 0},
145 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_FRAME, 0},
146 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_PSTRB_DELAY, 0},
147 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_STRB_DELAY, 0},
148 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_SHUT_DELAY, 0},
149 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_PSTRB_LENGTH, 0},
150 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_STRB_LENGTH, 0},
151 {OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_SHUT_LENGTH, 0},
152 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF_SYSCONFIG, 0},
153 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF_IRQENABLE, 0},
154 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF0_CTRL, 0},
155 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF1_CTRL, 0},
156 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF0_START, 0},
157 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF1_START, 0},
158 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF0_END, 0},
159 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF1_END, 0},
160 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF0_WINDOWSIZE, 0},
161 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF1_WINDOWSIZE, 0},
162 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF0_THRESHOLD, 0},
163 {OMAP3_ISP_IOMEM_CBUFF, ISP_CBUFF1_THRESHOLD, 0},
164 {0, ISP_TOK_TERM, 0}
168 * isp_flush - Post pending L3 bus writes by doing a register readback
169 * @dev: Device pointer specific to the OMAP3 ISP.
171 * In order to force posting of pending writes, we need to write and
172 * readback the same register, in this case the revision register.
174 * See this link for reference:
175 * http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
177 void isp_flush(struct device *dev)
179 isp_reg_writel(dev, 0, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
180 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN, ISP_REVISION);
184 * isp_reg_readl - Read value of an OMAP3 ISP register
185 * @dev: Device pointer specific to the OMAP3 ISP.
186 * @isp_mmio_range: Range to which the register offset refers to.
187 * @reg_offset: Register offset to read from.
189 * Returns an unsigned 32 bit value with the required register contents.
191 u32 isp_reg_readl(struct device *dev, enum isp_mem_resources isp_mmio_range,
192 u32 reg_offset)
194 struct isp_device *isp = dev_get_drvdata(dev);
196 return __raw_readl(isp->mmio_base[isp_mmio_range] + reg_offset);
198 EXPORT_SYMBOL(isp_reg_readl);
201 * isp_reg_writel - Write value to an OMAP3 ISP register
202 * @dev: Device pointer specific to the OMAP3 ISP.
203 * @reg_value: 32 bit value to write to the register.
204 * @isp_mmio_range: Range to which the register offset refers to.
205 * @reg_offset: Register offset to write into.
207 void isp_reg_writel(struct device *dev, u32 reg_value,
208 enum isp_mem_resources isp_mmio_range, u32 reg_offset)
210 struct isp_device *isp = dev_get_drvdata(dev);
212 __raw_writel(reg_value, isp->mmio_base[isp_mmio_range] + reg_offset);
214 EXPORT_SYMBOL(isp_reg_writel);
217 * isp_reg_and - Do AND binary operation within an OMAP3 ISP register value
218 * @dev: Device pointer specific to the OMAP3 ISP.
219 * @mmio_range: Range to which the register offset refers to.
220 * @reg: Register offset to work on.
221 * @and_bits: 32 bit value which would be 'ANDed' with current register value.
223 void isp_reg_and(struct device *dev, enum isp_mem_resources mmio_range, u32 reg,
224 u32 and_bits)
226 u32 v = isp_reg_readl(dev, mmio_range, reg);
228 isp_reg_writel(dev, v & and_bits, mmio_range, reg);
230 EXPORT_SYMBOL(isp_reg_and);
233 * isp_reg_or - Do OR binary operation within an OMAP3 ISP register value
234 * @dev: Device pointer specific to the OMAP3 ISP.
235 * @mmio_range: Range to which the register offset refers to.
236 * @reg: Register offset to work on.
237 * @or_bits: 32 bit value which would be 'ORed' with current register value.
239 void isp_reg_or(struct device *dev, enum isp_mem_resources mmio_range, u32 reg,
240 u32 or_bits)
242 u32 v = isp_reg_readl(dev, mmio_range, reg);
244 isp_reg_writel(dev, v | or_bits, mmio_range, reg);
246 EXPORT_SYMBOL(isp_reg_or);
249 * isp_reg_and_or - Do AND and OR binary ops within an OMAP3 ISP register value
250 * @dev: Device pointer specific to the OMAP3 ISP.
251 * @mmio_range: Range to which the register offset refers to.
252 * @reg: Register offset to work on.
253 * @and_bits: 32 bit value which would be 'ANDed' with current register value.
254 * @or_bits: 32 bit value which would be 'ORed' with current register value.
256 * The AND operation is done first, and then the OR operation. Mostly useful
257 * when clearing a group of bits before setting a value.
259 void isp_reg_and_or(struct device *dev, enum isp_mem_resources mmio_range,
260 u32 reg, u32 and_bits, u32 or_bits)
262 u32 v = isp_reg_readl(dev, mmio_range, reg);
264 isp_reg_writel(dev, (v & and_bits) | or_bits, mmio_range, reg);
266 EXPORT_SYMBOL(isp_reg_and_or);
269 * find_vctrl - Return the index of the ctrl array of the requested ctrl ID.
270 * @id: Requested control ID.
272 * Returns 0 if successful, -EINVAL if not found, or -EDOM if its out of
273 * domain.
275 static int find_vctrl(int id)
277 int i;
279 if (id < V4L2_CID_BASE)
280 return -EDOM;
282 for (i = (ARRAY_SIZE(video_control) - 1); i >= 0; i--)
283 if (video_control[i].qc.id == id)
284 break;
286 if (i < 0)
287 i = -EINVAL;
289 return i;
293 * find_next_vctrl - Return next v4l2 ctrl ID available after the specified ID
294 * @id: Reference V4L2 control ID.
296 * Returns 0 if successful, or -EINVAL if not found.
298 static int find_next_vctrl(int id)
300 int i;
301 u32 best = (u32)-1;
303 for (i = 0; i < ARRAY_SIZE(video_control); i++) {
304 if (video_control[i].qc.id > id &&
305 (best == (u32)-1 ||
306 video_control[i].qc.id <
307 video_control[best].qc.id)) {
308 best = i;
312 if (best == (u32)-1)
313 return -EINVAL;
315 return best;
319 * find_vmenu - Return index of the menu array of the requested ctrl option.
320 * @id: Requested control ID.
321 * @index: Requested menu option index.
323 * Returns 0 if successful, -EINVAL if not found, or -EDOM if its out of
324 * domain.
326 static int find_vmenu(int id, int index)
328 int i;
330 if (id < V4L2_CID_BASE)
331 return -EDOM;
333 for (i = (ARRAY_SIZE(video_menu) - 1); i >= 0; i--) {
334 if (video_menu[i].id != id || video_menu[i].index != index)
335 continue;
336 return i;
339 return -EINVAL;
343 * isp_release_resources - Free all currently requested ISP submodules.
344 * @dev: Device pointer specific to the OMAP3 ISP.
346 static void isp_release_resources(struct device *dev)
348 struct isp_device *isp = dev_get_drvdata(dev);
350 if (isp->pipeline.modules & OMAP_ISP_CCDC)
351 ispccdc_free(&isp->isp_ccdc);
353 if (isp->pipeline.modules & OMAP_ISP_PREVIEW)
354 isppreview_free(&isp->isp_prev);
356 if (isp->pipeline.modules & OMAP_ISP_RESIZER)
357 ispresizer_free(&isp->isp_res);
358 return;
362 * isp_wait - Wait for idle or busy state transition with a time limit
363 * @dev: Device pointer specific to the OMAP3 ISP.
364 * @busy: Function pointer which determines if submodule is busy.
365 * @wait_for_busy: If 0, waits for idle state, if 1, waits for busy state.
366 * @max_wait: Max retry count in us for wait for idle/busy transition.
367 * @priv: Function parameter to send to busy check function.
369 static int isp_wait(struct device *dev, int (*busy)(void *), int wait_for_busy,
370 int max_wait, void *priv)
372 int wait = 0;
374 if (max_wait == 0)
375 max_wait = 10000; /* 10 ms */
377 while ((wait_for_busy && !busy(priv))
378 || (!wait_for_busy && busy(priv))) {
379 rmb();
380 udelay(1);
381 wait++;
382 if (wait > max_wait) {
383 dev_alert(dev, "%s: wait is too much\n", __func__);
384 return -EBUSY;
387 DPRINTK_ISPCTRL(KERN_ALERT "%s: wait %d\n", __func__, wait);
389 return 0;
393 * ispccdc_sbl_wait_idle - Wrapper to wait for ccdc sbl status bits to be idle.
394 * @isp_ccdc: Pointer to ISP CCDC device.
395 * @max_wait: Max retry count for wait for idle transition of the CCDC SBL bits
397 static int ispccdc_sbl_wait_idle(struct isp_ccdc_device *isp_ccdc, int max_wait)
399 return isp_wait(isp_ccdc->dev, ispccdc_sbl_busy, 0, max_wait, isp_ccdc);
403 * isp_enable_interrupts - Enable ISP interrupts.
404 * @dev: Device pointer specific to the OMAP3 ISP.
405 * @is_raw: Determines if current pipeline ends in CCDC (!0) or Resizer (0)
407 static void isp_enable_interrupts(struct device *dev, int is_raw)
409 struct isp_device *isp = dev_get_drvdata(dev);
410 u32 irq0enable;
412 irq0enable = IRQ0ENABLE_CCDC_LSC_PREF_ERR_IRQ
413 | IRQ0ENABLE_HS_VS_IRQ
414 | IRQ0ENABLE_CCDC_VD0_IRQ | IRQ0ENABLE_CCDC_VD1_IRQ
415 | IRQ0ENABLE_CSIA_IRQ
416 | IRQ0ENABLE_CSIB_IRQ
417 | IRQ0ENABLE_H3A_AWB_DONE_IRQ | IRQ0ENABLE_H3A_AF_DONE_IRQ
418 | isp->interrupts;
420 if (!is_raw)
421 irq0enable |= IRQ0ENABLE_PRV_DONE_IRQ | IRQ0ENABLE_RSZ_DONE_IRQ;
423 isp_reg_writel(dev, -1, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
424 isp_reg_writel(dev, irq0enable, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
426 return;
430 * isp_disable_interrupts - Disable all ISP interrupts.
431 * @dev: Device pointer specific to the OMAP3 ISP.
433 static void isp_disable_interrupts(struct device *dev)
435 struct isp_device *isp = dev_get_drvdata(dev);
437 if (isp->bt656ifen == 0)
438 isp_reg_writel(dev, 0, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
439 else
440 isp_reg_writel(dev, 0 | IRQ0ENABLE_RSZ_DONE_IRQ,
441 OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE);
445 * isp_set_callback - Set an external callback for an ISP interrupt.
446 * @dev: Device pointer specific to the OMAP3 ISP.
447 * @type: Type of the event for which callback is requested.
448 * @callback: Method to be called as callback in the ISR context.
449 * @arg1: First argument to be passed when callback is called in ISR.
450 * @arg2: Second argument to be passed when callback is called in ISR.
452 * This function sets a callback function for a done event in the ISP
453 * module, and enables the corresponding interrupt.
455 int isp_set_callback(struct device *dev, enum isp_callback_type type,
456 isp_callback_t callback, isp_vbq_callback_ptr arg1,
457 void *arg2)
459 struct isp_device *isp = dev_get_drvdata(dev);
460 unsigned long irqflags = 0;
462 if (callback == NULL) {
463 DPRINTK_ISPCTRL("ISP_ERR : Null Callback\n");
464 return -EINVAL;
467 spin_lock_irqsave(&isp->lock, irqflags);
468 isp->irq.isp_callbk[type] = callback;
469 isp->irq.isp_callbk_arg1[type] = arg1;
470 isp->irq.isp_callbk_arg2[type] = arg2;
471 spin_unlock_irqrestore(&isp->lock, irqflags);
473 switch (type) {
474 case CBK_HIST_DONE:
475 isp->interrupts |= IRQ0ENABLE_HIST_DONE_IRQ;
476 if (isp->running != ISP_RUNNING)
477 break;
478 isp_reg_writel(dev, IRQ0ENABLE_HIST_DONE_IRQ,
479 OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
480 isp_reg_or(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE,
481 IRQ0ENABLE_HIST_DONE_IRQ);
482 break;
483 case CBK_PREV_DONE:
484 isp_reg_writel(dev, IRQ0ENABLE_PRV_DONE_IRQ,
485 OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
486 isp_reg_or(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE,
487 IRQ0ENABLE_PRV_DONE_IRQ);
488 break;
489 case CBK_RESZ_DONE:
490 isp_reg_writel(dev, IRQ0ENABLE_RSZ_DONE_IRQ,
491 OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
492 isp_reg_or(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE,
493 IRQ0ENABLE_RSZ_DONE_IRQ);
494 break;
495 default:
496 break;
499 return 0;
501 EXPORT_SYMBOL(isp_set_callback);
504 * isp_unset_callback - Clears the callback for the ISP module done events.
505 * @dev: Device pointer specific to the OMAP3 ISP.
506 * @type: Type of the event for which callback to be cleared.
508 * This function clears a callback function for a done event in the ISP
509 * module, and disables the corresponding interrupt.
511 int isp_unset_callback(struct device *dev, enum isp_callback_type type)
513 struct isp_device *isp = dev_get_drvdata(dev);
514 unsigned long irqflags = 0;
516 spin_lock_irqsave(&isp->lock, irqflags);
517 isp->irq.isp_callbk[type] = NULL;
518 isp->irq.isp_callbk_arg1[type] = NULL;
519 isp->irq.isp_callbk_arg2[type] = NULL;
520 spin_unlock_irqrestore(&isp->lock, irqflags);
522 switch (type) {
523 case CBK_HIST_DONE:
524 isp->interrupts &= ~IRQ0ENABLE_HIST_DONE_IRQ;
525 if (isp->running != ISP_RUNNING)
526 break;
527 isp_reg_and(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE,
528 ~IRQ0ENABLE_HIST_DONE_IRQ);
529 break;
530 case CBK_PREV_DONE:
531 isp_reg_and(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE,
532 ~IRQ0ENABLE_PRV_DONE_IRQ);
533 break;
534 case CBK_RESZ_DONE:
535 isp_reg_and(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0ENABLE,
536 ~IRQ0ENABLE_RSZ_DONE_IRQ);
537 break;
538 default:
539 break;
542 return 0;
544 EXPORT_SYMBOL(isp_unset_callback);
547 * isp_set_xclk - Configures the specified cam_xclk to the desired frequency.
548 * @dev: Device pointer specific to the OMAP3 ISP.
549 * @xclk: Desired frequency of the clock in Hz.
550 * @xclksel: XCLK to configure (0 = A, 1 = B).
552 * Configures the specified MCLK divisor in the ISP timing control register
553 * (TCTRL_CTRL) to generate the desired xclk clock value.
555 * Divisor = CM_CAM_MCLK_HZ / xclk
557 * Returns the final frequency that is actually being generated
559 u32 isp_set_xclk(struct device *dev, u32 xclk, u8 xclksel)
561 u32 divisor;
562 u32 currentxclk;
564 if (xclk >= CM_CAM_MCLK_HZ) {
565 divisor = ISPTCTRL_CTRL_DIV_BYPASS;
566 currentxclk = CM_CAM_MCLK_HZ;
567 } else if (xclk >= 2) {
568 divisor = CM_CAM_MCLK_HZ / xclk;
569 if (divisor >= ISPTCTRL_CTRL_DIV_BYPASS)
570 divisor = ISPTCTRL_CTRL_DIV_BYPASS - 1;
571 currentxclk = CM_CAM_MCLK_HZ / divisor;
572 } else {
573 divisor = xclk;
574 currentxclk = 0;
577 switch (xclksel) {
578 case 0:
579 isp_reg_and_or(dev, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
580 ~ISPTCTRL_CTRL_DIVA_MASK,
581 divisor << ISPTCTRL_CTRL_DIVA_SHIFT);
582 DPRINTK_ISPCTRL("isp_set_xclk(): cam_xclka set to %d Hz\n",
583 currentxclk);
584 break;
585 case 1:
586 isp_reg_and_or(dev, OMAP3_ISP_IOMEM_MAIN, ISP_TCTRL_CTRL,
587 ~ISPTCTRL_CTRL_DIVB_MASK,
588 divisor << ISPTCTRL_CTRL_DIVB_SHIFT);
589 DPRINTK_ISPCTRL("isp_set_xclk(): cam_xclkb set to %d Hz\n",
590 currentxclk);
591 break;
592 default:
593 DPRINTK_ISPCTRL("ISP_ERR: isp_set_xclk(): Invalid requested "
594 "xclk. Must be 0 (A) or 1 (B).\n");
595 return -EINVAL;
598 return currentxclk;
600 EXPORT_SYMBOL(isp_set_xclk);
603 * isp_power_settings - Sysconfig settings, for Power Management.
604 * @dev: Device pointer specific to the OMAP3 ISP.
605 * @idle: Consider idle state.
607 * Sets the power settings for the ISP, and SBL bus.
609 static void isp_power_settings(struct device *dev, int idle)
611 if (idle) {
612 isp_reg_writel(dev, ISP_SYSCONFIG_AUTOIDLE |
613 (ISP_SYSCONFIG_MIDLEMODE_SMARTSTANDBY <<
614 ISP_SYSCONFIG_MIDLEMODE_SHIFT),
615 OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
616 if (omap_rev() == OMAP3430_REV_ES1_0) {
617 isp_reg_writel(dev, ISPCSI1_AUTOIDLE |
618 (ISPCSI1_MIDLEMODE_SMARTSTANDBY <<
619 ISPCSI1_MIDLEMODE_SHIFT),
620 OMAP3_ISP_IOMEM_CSI2A,
621 ISP_CSIA_SYSCONFIG);
622 isp_reg_writel(dev, ISPCSI1_AUTOIDLE |
623 (ISPCSI1_MIDLEMODE_SMARTSTANDBY <<
624 ISPCSI1_MIDLEMODE_SHIFT),
625 OMAP3_ISP_IOMEM_CCP2,
626 ISP_CSIB_SYSCONFIG);
628 isp_reg_writel(dev, ISPCTRL_SBL_AUTOIDLE, OMAP3_ISP_IOMEM_MAIN,
629 ISP_CTRL);
631 } else {
632 isp_reg_writel(dev, ISP_SYSCONFIG_AUTOIDLE |
633 (ISP_SYSCONFIG_MIDLEMODE_FORCESTANDBY <<
634 ISP_SYSCONFIG_MIDLEMODE_SHIFT),
635 OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
636 if (omap_rev() == OMAP3430_REV_ES1_0) {
637 isp_reg_writel(dev, ISPCSI1_AUTOIDLE |
638 (ISPCSI1_MIDLEMODE_FORCESTANDBY <<
639 ISPCSI1_MIDLEMODE_SHIFT),
640 OMAP3_ISP_IOMEM_CSI2A,
641 ISP_CSIA_SYSCONFIG);
643 isp_reg_writel(dev, ISPCSI1_AUTOIDLE |
644 (ISPCSI1_MIDLEMODE_FORCESTANDBY <<
645 ISPCSI1_MIDLEMODE_SHIFT),
646 OMAP3_ISP_IOMEM_CCP2,
647 ISP_CSIB_SYSCONFIG);
650 isp_reg_writel(dev, ISPCTRL_SBL_AUTOIDLE, OMAP3_ISP_IOMEM_MAIN,
651 ISP_CTRL);
655 #define BIT_SET(var, shift, mask, val) \
656 do { \
657 var = (var & ~(mask << shift)) \
658 | (val << shift); \
659 } while (0)
662 * isp_csi_enable - Enable CSI1/CCP2 interface.
663 * @dev: Device pointer specific to the OMAP3 ISP.
664 * @enable: Enable flag.
666 static void isp_csi_enable(struct device *dev, u8 enable)
668 isp_reg_and_or(dev, OMAP3_ISP_IOMEM_CCP2, ISPCSI1_CTRL,
669 ~(BIT(0) | BIT(4)),
670 enable ? (BIT(0) | BIT(4)) : 0);
674 * isp_init_csi - Initialize CSI1/CCP2 interface.
675 * @dev: Device pointer specific to the OMAP3 ISP.
676 * @config: Pointer to ISP interface config structure.
678 * This will analize the parameters passed by the interface config
679 * structure, and configure the respective registers for proper CSI1/CCP2
680 * config.
682 * Returns -EINVAL if wrong format, -EIO if strobe is choosen in CSI1 mode, or
683 * 0 on success.
685 static int isp_init_csi(struct device *dev, struct isp_interface_config *config)
687 u32 i = 0, val, reg;
688 int format;
690 switch (config->u.csi.format) {
691 case V4L2_PIX_FMT_SGRBG10:
692 format = 0x16; /* RAW10+VP */
693 break;
694 case V4L2_PIX_FMT_SGRBG10DPCM8:
695 format = 0x12; /* RAW8+DPCM10+VP */
696 break;
697 default:
698 dev_err(dev, "isp_init_csi: bad csi format\n");
699 return -EINVAL;
702 /* Reset the CSI and wait for reset to complete */
703 isp_reg_writel(dev, isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2,
704 ISPCSI1_SYSCONFIG) | BIT(1),
705 OMAP3_ISP_IOMEM_CCP2, ISPCSI1_SYSCONFIG);
706 while (!(isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2, ISPCSI1_SYSSTATUS) &
707 BIT(0))) {
708 udelay(10);
709 if (i++ > 10)
710 break;
712 if (!(isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2, ISPCSI1_SYSSTATUS) &
713 BIT(0))) {
714 dev_warn(dev,
715 "omap3_isp: timeout waiting for csi reset\n");
718 /* ISPCSI1_CTRL */
719 val = isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2, ISPCSI1_CTRL);
720 val &= ~BIT(11); /* Enable VP only off ->
721 extract embedded data to interconnect */
722 BIT_SET(val, 8, 0x3, config->u.csi.vpclk); /* Video port clock */
723 /* val |= BIT(3); */ /* Wait for FEC before disabling interface */
724 val |= BIT(2); /* I/O cell output is parallel
725 (no effect, but errata says should be enabled
726 for class 1/2) */
727 val |= BIT(12); /* VP clock polarity to falling edge
728 (needed or bad picture!) */
730 /* Data/strobe physical layer */
731 BIT_SET(val, 1, 1, config->u.csi.signalling);
732 BIT_SET(val, 10, 1, config->u.csi.strobe_clock_inv);
733 val |= BIT(4); /* Magic bit to enable CSI1 and strobe mode */
734 isp_reg_writel(dev, val, OMAP3_ISP_IOMEM_CCP2, ISPCSI1_CTRL);
736 /* ISPCSI1_LCx_CTRL logical channel #0 */
737 reg = ISPCSI1_LCx_CTRL(0); /* reg = ISPCSI1_CTRL1; */
738 val = isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2, reg);
739 /* Format = RAW10+VP or RAW8+DPCM10+VP*/
740 BIT_SET(val, 3, 0x1f, format);
741 /* Enable setting of frame regions of interest */
742 BIT_SET(val, 1, 1, 1);
743 BIT_SET(val, 2, 1, config->u.csi.crc);
744 isp_reg_writel(dev, val, OMAP3_ISP_IOMEM_CCP2, reg);
746 /* ISPCSI1_DAT_START for logical channel #0 */
747 reg = ISPCSI1_LCx_DAT_START(0); /* reg = ISPCSI1_DAT_START; */
748 val = isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2, reg);
749 BIT_SET(val, 16, 0xfff, config->u.csi.data_start);
750 isp_reg_writel(dev, val, OMAP3_ISP_IOMEM_CCP2, reg);
752 /* ISPCSI1_DAT_SIZE for logical channel #0 */
753 reg = ISPCSI1_LCx_DAT_SIZE(0); /* reg = ISPCSI1_DAT_SIZE; */
754 val = isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2, reg);
755 BIT_SET(val, 16, 0xfff, config->u.csi.data_size);
756 isp_reg_writel(dev, val, OMAP3_ISP_IOMEM_CCP2, reg);
758 /* Clear status bits for logical channel #0 */
759 val = ISPCSI1_LC01_IRQSTATUS_LC0_FIFO_OVF_IRQ |
760 ISPCSI1_LC01_IRQSTATUS_LC0_CRC_IRQ |
761 ISPCSI1_LC01_IRQSTATUS_LC0_FSP_IRQ |
762 ISPCSI1_LC01_IRQSTATUS_LC0_FW_IRQ |
763 ISPCSI1_LC01_IRQSTATUS_LC0_FSC_IRQ |
764 ISPCSI1_LC01_IRQSTATUS_LC0_SSC_IRQ;
766 /* Clear IRQ status bits for logical channel #0 */
767 isp_reg_writel(dev, val, OMAP3_ISP_IOMEM_CCP2,
768 ISPCSI1_LC01_IRQSTATUS);
770 /* Enable IRQs for logical channel #0 */
771 isp_reg_or(dev, OMAP3_ISP_IOMEM_CCP2, ISPCSI1_LC01_IRQENABLE, val);
773 /* Enable CSI1 */
774 isp_csi_enable(dev, 1);
776 if (!(isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2,
777 ISPCSI1_CTRL) & BIT(4))) {
778 dev_warn(dev, "OMAP3 CSI1 bus not available\n");
779 if (config->u.csi.signalling) {
780 /* Strobe mode requires CCP2 */
781 return -EIO;
785 return 0;
789 * isp_configure_interface - Configures ISP Control I/F related parameters.
790 * @dev: Device pointer specific to the OMAP3 ISP.
791 * @config: Pointer to structure containing the desired configuration for the
792 * ISP.
794 * Configures ISP control register (ISP_CTRL) with the values specified inside
795 * the config structure. Controls:
796 * - Selection of parallel or serial input to the preview hardware.
797 * - Data lane shifter.
798 * - Pixel clock polarity.
799 * - 8 to 16-bit bridge at the input of CCDC module.
800 * - HS or VS synchronization signal detection
802 * Returns 0 on success, otherwise, will return other negative error value.
804 int isp_configure_interface(struct device *dev,
805 struct isp_interface_config *config)
807 struct isp_device *isp = dev_get_drvdata(dev);
808 u32 ispctrl_val = isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
809 u32 fmtcfg;
810 int r;
812 isp->config = config;
814 ispctrl_val &= ISPCTRL_SHIFT_MASK;
815 ispctrl_val |= config->dataline_shift << ISPCTRL_SHIFT_SHIFT;
816 ispctrl_val &= ~ISPCTRL_PAR_CLK_POL_INV;
818 ispctrl_val &= ISPCTRL_PAR_SER_CLK_SEL_MASK;
820 isp_buf_init(dev);
822 switch (config->ccdc_par_ser) {
823 case ISP_PARLL:
824 case ISP_PARLL_YUV_BT:
825 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_PARALLEL;
826 ispctrl_val |= config->u.par.par_clk_pol
827 << ISPCTRL_PAR_CLK_POL_SHIFT;
828 ispctrl_val &= ~ISPCTRL_PAR_BRIDGE_BENDIAN;
829 ispctrl_val |= config->u.par.par_bridge
830 << ISPCTRL_PAR_BRIDGE_SHIFT;
831 if (config->ccdc_par_ser == ISP_PARLL_YUV_BT)
832 isp->bt656ifen = 1;
833 break;
834 case ISP_CSIA:
835 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIA;
836 ispctrl_val &= ~ISPCTRL_PAR_BRIDGE_BENDIAN;
838 if (config->u.csi.crc)
839 isp_csi2_ctrl_config_ecc_enable(true);
841 isp_csi2_ctrl_config_vp_out_ctrl(config->u.csi.vpclk);
842 isp_csi2_ctrl_config_vp_only_enable(true);
843 isp_csi2_ctrl_config_vp_clk_enable(true);
844 isp_csi2_ctrl_update(false);
846 isp_csi2_ctx_config_format(0, config->u.csi.format);
847 isp_csi2_ctx_update(0, false);
849 isp_csi2_irq_complexio1_set(1);
850 isp_csi2_irq_status_set(1);
852 isp_csi2_enable(1);
853 mdelay(3);
854 break;
855 case ISP_CSIB:
856 ispctrl_val |= ISPCTRL_PAR_SER_CLK_SEL_CSIB;
857 r = isp_init_csi(dev, config);
858 if (r)
859 return r;
860 break;
861 case ISP_NONE:
862 return 0;
863 default:
864 return -EINVAL;
867 ispctrl_val &= ~ISPCTRL_SYNC_DETECT_VSRISE;
868 ispctrl_val |= config->hsvs_syncdetect;
870 isp_reg_writel(dev, ispctrl_val, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL);
872 /* Set sensor specific fields in CCDC and Previewer module. */
873 ispccdc_set_wenlog(&isp->isp_ccdc, config->wenlog);
875 /* FIXME: this should be set in ispccdc_config_vp() */
876 fmtcfg = isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
877 fmtcfg &= ISPCCDC_FMTCFG_VPIF_FRQ_MASK;
878 if (config->pixelclk) {
879 unsigned long l3_ick = clk_get_rate(isp->l3_ick);
880 unsigned long div = l3_ick / config->pixelclk;
881 if (div < 2)
882 div = 2;
883 if (div > 6)
884 div = 6;
885 fmtcfg |= (div - 2) << ISPCCDC_FMTCFG_VPIF_FRQ_SHIFT;
887 isp_reg_writel(dev, fmtcfg, OMAP3_ISP_IOMEM_CCDC, ISPCCDC_FMTCFG);
889 return 0;
891 EXPORT_SYMBOL(isp_configure_interface);
893 static int isp_buf_process(struct device *dev, struct isp_bufs *bufs);
896 * omap34xx_isp_isr - Interrupt Service Routine for Camera ISP module.
897 * @irq: Not used currently.
898 * @_pdev: Pointer to the platform device associated with the OMAP3 ISP.
900 * Returns IRQ_HANDLED when IRQ was correctly handled, or IRQ_NONE when the
901 * IRQ wasn't handled.
903 static irqreturn_t omap34xx_isp_isr(int irq, void *_pdev)
905 struct device *dev = &((struct platform_device *)_pdev)->dev;
906 struct isp_device *isp = dev_get_drvdata(dev);
907 struct isp_irq *irqdis = &isp->irq;
908 struct isp_bufs *bufs = &isp->bufs;
909 unsigned long flags;
910 u32 irqstatus = 0;
911 u32 sbl_pcr;
912 unsigned long irqflags = 0;
913 int wait_hs_vs = 0;
914 u8 fld_stat;
916 if (isp->running == ISP_STOPPED) {
917 dev_err(dev, "ouch %8.8x!\n",isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS));
918 return IRQ_NONE;
921 irqstatus = isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
922 isp_reg_writel(dev, irqstatus, OMAP3_ISP_IOMEM_MAIN, ISP_IRQ0STATUS);
924 if (isp->running == ISP_STOPPING)
925 return IRQ_HANDLED;
927 spin_lock_irqsave(&bufs->lock, flags);
928 wait_hs_vs = bufs->wait_hs_vs;
929 if (irqstatus & HS_VS) {
930 if (bufs->wait_hs_vs) {
931 bufs->wait_hs_vs--;
932 } else {
933 if (isp->pipeline.pix.field == V4L2_FIELD_INTERLACED) {
934 fld_stat = (isp_reg_readl(dev,
935 OMAP3_ISP_IOMEM_CCDC,
936 ISPCCDC_SYN_MODE) &
937 ISPCCDC_SYN_MODE_FLDSTAT) ? 1 : 0;
938 isp->current_field = fld_stat;
942 spin_unlock_irqrestore(&bufs->lock, flags);
944 spin_lock_irqsave(&isp->lock, irqflags);
946 if (irqstatus & RESZ_DONE) {
947 if (irqdis->isp_callbk[CBK_RESZ_DONE])
948 irqdis->isp_callbk[CBK_RESZ_DONE](
949 RESZ_DONE,
950 irqdis->isp_callbk_arg1[CBK_RESZ_DONE],
951 irqdis->isp_callbk_arg2[CBK_RESZ_DONE]);
952 else if (!RAW_CAPTURE(isp)) {
953 ispresizer_config_shadow_registers(&isp->isp_res);
954 isp_buf_process(dev, bufs);
958 * We need to wait for the first HS_VS interrupt from CCDC.
959 * Otherwise our frame (and everything else) might be bad.
961 switch (wait_hs_vs) {
962 case 1:
964 * Enable preview for the first time. We just have
965 * missed the start-of-frame so we can do it now.
967 if (irqstatus & HS_VS && !RAW_CAPTURE(isp))
968 isppreview_enable(&isp->isp_prev);
969 default:
970 goto out_ignore_buff;
971 case 0:
972 break;
975 if (irqstatus & CCDC_VD0) {
976 if (isp->pipeline.pix.field == V4L2_FIELD_INTERLACED) {
977 /* Skip even fields, and process only odd fields */
978 if (isp->current_field != 0)
979 if (RAW_CAPTURE(isp))
980 isp_buf_process(dev, bufs);
982 if (!ispccdc_busy(&isp->isp_ccdc))
983 ispccdc_config_shadow_registers(&isp->isp_ccdc);
986 if (irqstatus & PREV_DONE) {
987 if (irqdis->isp_callbk[CBK_PREV_DONE])
988 irqdis->isp_callbk[CBK_PREV_DONE](
989 PREV_DONE,
990 irqdis->isp_callbk_arg1[CBK_PREV_DONE],
991 irqdis->isp_callbk_arg2[CBK_PREV_DONE]);
992 else if (!RAW_CAPTURE(isp)) {
993 if (ispresizer_busy(&isp->isp_res)) {
994 ISP_BUF_DONE(bufs)->vb_state =
995 VIDEOBUF_ERROR;
996 dev_err(dev, "%s: resizer busy!\n", __func__);
997 } else {
998 ispresizer_enable(&isp->isp_res, 1);
1000 isppreview_config_shadow_registers(&isp->isp_prev);
1001 isppreview_enable(&isp->isp_prev);
1005 if (irqstatus & H3A_AWB_DONE)
1006 isph3a_aewb_isr(&isp->isp_h3a);
1008 if (irqstatus & HIST_DONE) {
1009 if (irqdis->isp_callbk[CBK_HIST_DONE])
1010 irqdis->isp_callbk[CBK_HIST_DONE](
1011 HIST_DONE,
1012 irqdis->isp_callbk_arg1[CBK_HIST_DONE],
1013 irqdis->isp_callbk_arg2[CBK_HIST_DONE]);
1016 if (irqstatus & H3A_AF_DONE)
1017 isp_af_isr(&isp->isp_af);
1019 /* Handle shared buffer logic overflows for video buffers. */
1020 /* ISPSBL_PCR_CCDCPRV_2_RSZ_OVF can be safely ignored. */
1021 sbl_pcr = isp_reg_readl(dev, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR) &
1022 ~ISPSBL_PCR_CCDCPRV_2_RSZ_OVF;
1023 isp_reg_writel(dev, sbl_pcr, OMAP3_ISP_IOMEM_SBL, ISPSBL_PCR);
1024 if (sbl_pcr & (ISPSBL_PCR_RSZ1_WBL_OVF
1025 | ISPSBL_PCR_RSZ2_WBL_OVF
1026 | ISPSBL_PCR_RSZ3_WBL_OVF
1027 | ISPSBL_PCR_RSZ4_WBL_OVF
1028 | ISPSBL_PCR_PRV_WBL_OVF
1029 | ISPSBL_PCR_CCDC_WBL_OVF
1030 | ISPSBL_PCR_CSIA_WBL_OVF
1031 | ISPSBL_PCR_CSIB_WBL_OVF)) {
1032 struct isp_buf *buf = ISP_BUF_DONE(bufs);
1033 buf->vb_state = VIDEOBUF_ERROR;
1034 dev_info(dev, "%s: sbl overflow, sbl_pcr = %8.8x\n",
1035 __func__, sbl_pcr);
1038 out_ignore_buff:
1039 if (irqstatus & LSC_PRE_ERR) {
1040 struct isp_buf *buf = ISP_BUF_DONE(bufs);
1041 /* Mark buffer faulty. */
1042 buf->vb_state = VIDEOBUF_ERROR;
1043 ispccdc_lsc_error_handler(&isp->isp_ccdc);
1044 dev_err(dev, "%s: lsc prefetch error\n", __func__);
1047 if (irqstatus & CSIA) {
1048 struct isp_buf *buf = ISP_BUF_DONE(bufs);
1049 int ret = isp_csi2_isr();
1050 if (ret)
1051 buf->vb_state = VIDEOBUF_ERROR;
1054 if (irqstatus & IRQ0STATUS_CSIB_IRQ) {
1055 struct isp_buf *buf = ISP_BUF_DONE(bufs);
1056 u32 ispcsi1_irqstatus;
1058 ispcsi1_irqstatus = isp_reg_readl(dev, OMAP3_ISP_IOMEM_CCP2,
1059 ISPCSI1_LC01_IRQSTATUS);
1060 isp_reg_writel(dev, ispcsi1_irqstatus, OMAP3_ISP_IOMEM_CCP2,
1061 ISPCSI1_LC01_IRQSTATUS);
1062 buf->vb_state = VIDEOBUF_ERROR;
1063 dev_err(dev, "CCP2 err:%x\n", ispcsi1_irqstatus);
1066 if (irqdis->isp_callbk[CBK_CATCHALL]) {
1067 irqdis->isp_callbk[CBK_CATCHALL](
1068 irqstatus,
1069 irqdis->isp_callbk_arg1[CBK_CATCHALL],
1070 irqdis->isp_callbk_arg2[CBK_CATCHALL]);
1073 spin_unlock_irqrestore(&isp->lock, irqflags);
1075 isp_flush(dev);
1077 #if 1
1079 static const struct {
1080 int num;
1081 char *name;
1082 } bits[] = {
1083 { 31, "HS_VS_IRQ" },
1084 { 30, "SEC_ERR_IRQ" },
1085 { 29, "OCP_ERR_IRQ" },
1086 { 28, "MMU_ERR_IRQ" },
1087 { 27, "res27" },
1088 { 26, "res26" },
1089 { 25, "OVF_IRQ" },
1090 { 24, "RSZ_DONE_IRQ" },
1091 { 23, "res23" },
1092 { 22, "res22" },
1093 { 21, "CBUFF_IRQ" },
1094 { 20, "PRV_DONE_IRQ" },
1095 { 19, "CCDC_LSC_PREFETCH_ERROR" },
1096 { 18, "CCDC_LSC_PREFETCH_COMPLETED" },
1097 { 17, "CCDC_LSC_DONE" },
1098 { 16, "HIST_DONE_IRQ" },
1099 { 15, "res15" },
1100 { 14, "res14" },
1101 { 13, "H3A_AWB_DONE_IRQ" },
1102 { 12, "H3A_AF_DONE_IRQ" },
1103 { 11, "CCDC_ERR_IRQ" },
1104 { 10, "CCDC_VD2_IRQ" },
1105 { 9, "CCDC_VD1_IRQ" },
1106 { 8, "CCDC_VD0_IRQ" },
1107 { 7, "res7" },
1108 { 6, "res6" },
1109 { 5, "res5" },
1110 { 4, "CSIB_IRQ" },
1111 { 3, "CSIB_LCM_IRQ" },
1112 { 2, "res2" },
1113 { 1, "res1" },
1114 { 0, "CSIA_IRQ" },
1116 int i;
1117 for (i = 0; i < ARRAY_SIZE(bits); i++) {
1118 if ((1 << bits[i].num) & irqstatus)
1119 DPRINTK_ISPCTRL("%s ", bits[i].name);
1121 DPRINTK_ISPCTRL("\n");
1123 #endif
1125 return IRQ_HANDLED;
1128 /* Device name, needed for resource tracking layer */
1129 struct device_driver camera_drv = {
1130 .name = "camera"
1133 struct device camera_dev = {
1134 .driver = &camera_drv,
1138 * isp_tmp_buf_free - Free buffer for CCDC->PRV->RSZ datapath workaround.
1139 * @dev: Device pointer specific to the OMAP3 ISP.
1141 static void isp_tmp_buf_free(struct device *dev)
1143 struct isp_device *isp = dev_get_drvdata(dev);
1145 if (isp->tmp_buf) {
1146 iommu_vfree(isp->iommu, isp->tmp_buf);
1147 isp->tmp_buf = 0;
1148 isp->tmp_buf_size = 0;
1153 * isp_tmp_buf_alloc - Allocate buffer for CCDC->PRV->RSZ datapath workaround.
1154 * @dev: Device pointer specific to the OMAP3 ISP.
1155 * @size: Byte size of the buffer to allocate
1157 * Returns 0 if successful, or -ENOMEM if there's no available memory.
1159 static u32 isp_tmp_buf_alloc(struct device *dev, size_t size)
1161 struct isp_device *isp = dev_get_drvdata(dev);
1162 u32 da;
1164 isp_tmp_buf_free(dev);
1166 dev_dbg(dev, "%s: allocating %d bytes\n", __func__, size);
1168 da = iommu_vmalloc(isp->iommu, 0, size, IOMMU_FLAG);
1169 if (IS_ERR_VALUE(da)) {
1170 dev_err(dev, "iommu_vmap mapping failed ");
1171 return -ENOMEM;
1173 isp->tmp_buf = da;
1174 isp->tmp_buf_size = size;
1176 isppreview_set_outaddr(&isp->isp_prev, isp->tmp_buf);
1177 ispresizer_set_inaddr(&isp->isp_res, isp->tmp_buf);
1179 return 0;
1183 * isp_start - Set ISP in running state
1184 * @dev: Device pointer specific to the OMAP3 ISP.
1186 void isp_start(struct device *dev)
1188 struct isp_device *isp = dev_get_drvdata(dev);
1190 isp->running = ISP_RUNNING;
1192 return;
1194 EXPORT_SYMBOL(isp_start);
1196 #define ISP_STATISTICS_BUSY \
1198 #define ISP_STOP_TIMEOUT msecs_to_jiffies(1000)
1201 * __isp_disable_modules - Disable ISP submodules with a timeout to be idle.
1202 * @dev: Device pointer specific to the OMAP3 ISP.
1203 * @suspend: If 0, disable modules; if 1, send modules to suspend state.
1205 * Returns 0 if stop/suspend left in idle state all the submodules properly,
1206 * or returns 1 if a general Reset is required to stop/suspend the submodules.
1208 static int __isp_disable_modules(struct device *dev, int suspend)
1210 struct isp_device *isp = dev_get_drvdata(dev);
1211 unsigned long timeout = jiffies + ISP_STOP_TIMEOUT;
1212 int reset = 0;
1215 * We need to stop all the modules after CCDC first or they'll
1216 * never stop since they may not get a full frame from CCDC.
1218 if (suspend) {
1219 isp_af_suspend(&isp->isp_af);
1220 isph3a_aewb_suspend(&isp->isp_h3a);
1221 isp_hist_suspend(&isp->isp_hist);
1222 } else {
1223 isp_af_enable(&isp->isp_af, 0);
1224 isph3a_aewb_enable(&isp->isp_h3a, 0);
1225 isp_hist_enable(&isp->isp_hist, 0);
1227 ispresizer_enable(&isp->isp_res, 0);
1229 timeout = jiffies + ISP_STOP_TIMEOUT;
1230 while (isp_af_busy(&isp->isp_af)
1231 || isph3a_aewb_busy(&isp->isp_h3a)
1232 || isp_hist_busy(&isp->isp_hist)
1233 || isppreview_busy(&isp->isp_prev)
1234 || ispresizer_busy(&isp->isp_res)) {
1235 if (time_after(jiffies, timeout)) {
1236 dev_err(dev, "%s: can't stop non-ccdc modules\n",
1237 __func__);
1238 reset = 1;
1239 break;
1241 msleep(1);
1244 /* Let's stop CCDC now. */
1245 ispccdc_enable(&isp->isp_ccdc, 0);
1247 timeout = jiffies + ISP_STOP_TIMEOUT;
1248 while (ispccdc_busy(&isp->isp_ccdc)) {
1249 if (time_after(jiffies, timeout)) {
1250 dev_err(dev, "%s: can't stop ccdc\n", __func__);
1251 reset = 1;
1252 break;
1254 msleep(1);
1257 isp_csi_enable(dev, 0);
1258 isp_csi2_enable(0);
1259 isp_buf_init(dev);
1261 return reset;
1265 * isp_stop_modules - Stop ISP submodules.
1266 * @dev: Device pointer specific to the OMAP3 ISP.
1268 * Returns 0 if stop left in idle state all the submodules properly,
1269 * or returns 1 if a general Reset is required to stop the submodules.
1271 static int isp_stop_modules(struct device *dev)
1273 return __isp_disable_modules(dev, 0);
1276 #ifdef CONFIG_PM
1278 * isp_suspend_modules - Suspend ISP submodules.
1279 * @dev: Device pointer specific to the OMAP3 ISP.
1281 * Returns 0 if suspend left in idle state all the submodules properly,
1282 * or returns 1 if a general Reset is required to suspend the submodules.
1284 static int isp_suspend_modules(struct device *dev)
1286 return __isp_disable_modules(dev, 1);
1290 * isp_resume_modules - Resume ISP submodules.
1291 * @dev: Device pointer specific to the OMAP3 ISP.
1293 static void isp_resume_modules(struct device *dev)
1295 struct isp_device *isp = dev_get_drvdata(dev);
1297 isp_hist_resume(&isp->isp_hist);
1298 isph3a_aewb_resume(&isp->isp_h3a);
1299 isp_af_resume(&isp->isp_af);
1301 #endif /* CONFIG_PM */
1304 * isp_reset - Reset ISP with a timeout wait for idle.
1305 * @dev: Device pointer specific to the OMAP3 ISP.
1307 static void isp_reset(struct device *dev)
1309 unsigned long timeout = 0;
1311 isp_reg_writel(dev,
1312 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG)
1313 | ISP_SYSCONFIG_SOFTRESET,
1314 OMAP3_ISP_IOMEM_MAIN, ISP_SYSCONFIG);
1315 while (!(isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN,
1316 ISP_SYSSTATUS) & 0x1)) {
1317 if (timeout++ > 10000) {
1318 dev_alert(dev, "%s: cannot reset ISP\n", __func__);
1319 break;
1321 udelay(1);
1326 * isp_stop - Stop ISP.
1327 * @dev: Device pointer specific to the OMAP3 ISP.
1329 void isp_stop(struct device *dev)
1331 struct isp_device *isp = dev_get_drvdata(dev);
1332 int reset;
1334 isp->running = ISP_STOPPING;
1335 isp_disable_interrupts(dev);
1336 synchronize_irq(((struct isp_device *)dev_get_drvdata(dev))->irq_num);
1337 isp->running = ISP_STOPPED;
1338 reset = isp_stop_modules(dev);
1339 if (!reset)
1340 return;
1342 isp_save_ctx(dev);
1343 isp_reset(dev);
1344 isp_restore_ctx(dev);
1346 EXPORT_SYMBOL(isp_stop);
1349 * isp_set_buf - Program output buffer address based on current pipeline.
1350 * @dev: Device pointer specific to the OMAP3 ISP.
1351 * @buf: Pointer to ISP buffer structure.
1353 static void isp_set_buf(struct device *dev, struct isp_buf *buf)
1355 struct isp_device *isp = dev_get_drvdata(dev);
1357 if (isp->pipeline.modules & OMAP_ISP_RESIZER
1358 && is_ispresizer_enabled())
1359 ispresizer_set_outaddr(&isp->isp_res, buf->isp_addr);
1360 else if (isp->pipeline.modules & OMAP_ISP_CCDC)
1361 ispccdc_set_outaddr(&isp->isp_ccdc, buf->isp_addr);
1366 * isp_try_pipeline - Retrieve and simulate resulting internal ISP pipeline.
1367 * @dev: Device pointer specific to the OMAP3 ISP.
1368 * @pix_input: Pointer to pixel format to use as input in the ISP.
1369 * @pipe: Pointer to ISP pipeline structure to fill back.
1371 * Returns the closest possible output size based on silicon limitations
1372 * detailed through the pipe structure.
1374 * If the input can't be read, it'll return -EINVAL. Returns 0 on success.
1376 static int isp_try_pipeline(struct device *dev,
1377 struct v4l2_pix_format *pix_input,
1378 struct isp_pipeline *pipe)
1380 struct isp_device *isp = dev_get_drvdata(dev);
1381 struct v4l2_pix_format *pix_output = &pipe->pix;
1382 unsigned int wanted_width = pix_output->width;
1383 unsigned int wanted_height = pix_output->height;
1384 int ifmt;
1385 int rval;
1387 if ((pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
1388 pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10DPCM8 ||
1389 pix_input->pixelformat == V4L2_PIX_FMT_SRGGB10 ||
1390 pix_input->pixelformat == V4L2_PIX_FMT_SBGGR10 ||
1391 pix_input->pixelformat == V4L2_PIX_FMT_SGBRG10) &&
1392 (pix_output->pixelformat == V4L2_PIX_FMT_YUYV ||
1393 pix_output->pixelformat == V4L2_PIX_FMT_UYVY)) {
1394 pipe->modules = OMAP_ISP_CCDC | OMAP_ISP_PREVIEW
1395 | OMAP_ISP_RESIZER;
1396 if (pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
1397 pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10DPCM8)
1398 pipe->ccdc_in = CCDC_RAW_GRBG;
1399 if (pix_input->pixelformat == V4L2_PIX_FMT_SRGGB10)
1400 pipe->ccdc_in = CCDC_RAW_RGGB;
1401 if (pix_input->pixelformat == V4L2_PIX_FMT_SBGGR10)
1402 pipe->ccdc_in = CCDC_RAW_BGGR;
1403 if (pix_input->pixelformat == V4L2_PIX_FMT_SGBRG10)
1404 pipe->ccdc_in = CCDC_RAW_GBRG;
1405 pipe->ccdc_out = CCDC_OTHERS_VP;
1406 pipe->prv_in = PRV_RAW_CCDC;
1407 pipe->prv_out = PREVIEW_MEM;
1408 pipe->rsz_in = RSZ_MEM_YUV;
1409 } else {
1410 pipe->modules = OMAP_ISP_CCDC;
1411 if (pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
1412 pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10DPCM8 ||
1413 pix_input->pixelformat == V4L2_PIX_FMT_SRGGB10 ||
1414 pix_input->pixelformat == V4L2_PIX_FMT_SBGGR10 ||
1415 pix_input->pixelformat == V4L2_PIX_FMT_SGBRG10) {
1416 pipe->ccdc_out = CCDC_OTHERS_VP_MEM;
1417 if (pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
1418 pix_input->pixelformat == V4L2_PIX_FMT_SGRBG10DPCM8)
1419 pipe->ccdc_in = CCDC_RAW_GRBG;
1420 if (pix_input->pixelformat == V4L2_PIX_FMT_SRGGB10)
1421 pipe->ccdc_in = CCDC_RAW_RGGB;
1422 if (pix_input->pixelformat == V4L2_PIX_FMT_SBGGR10)
1423 pipe->ccdc_in = CCDC_RAW_BGGR;
1424 if (pix_input->pixelformat == V4L2_PIX_FMT_SGBRG10)
1425 pipe->ccdc_in = CCDC_RAW_GBRG;
1426 } else if (pix_input->pixelformat == V4L2_PIX_FMT_YUYV ||
1427 pix_input->pixelformat == V4L2_PIX_FMT_UYVY) {
1428 if (isp->bt656ifen)
1429 pipe->ccdc_in = CCDC_YUV_BT;
1430 else
1431 pipe->ccdc_in = CCDC_YUV_SYNC;
1432 pipe->ccdc_out = CCDC_OTHERS_MEM;
1433 } else
1434 return -EINVAL;
1437 if (pipe->modules & OMAP_ISP_CCDC) {
1438 pipe->ccdc_in_w = pix_input->width;
1439 pipe->ccdc_in_h = pix_input->height;
1440 rval = ispccdc_try_pipeline(&isp->isp_ccdc, pipe);
1441 if (rval) {
1442 dev_err(dev, "the dimensions %dx%d are not"
1443 " supported\n", pix_input->width,
1444 pix_input->height);
1445 return rval;
1447 pix_output->width = pipe->ccdc_out_w_img;
1448 pix_output->height = pipe->ccdc_out_h;
1449 pix_output->bytesperline =
1450 pipe->ccdc_out_w * ISP_BYTES_PER_PIXEL;
1453 if (pipe->modules & OMAP_ISP_PREVIEW) {
1454 rval = isppreview_try_pipeline(&isp->isp_prev, pipe);
1455 if (rval) {
1456 dev_err(dev, "the dimensions %dx%d are not"
1457 " supported\n", pix_input->width,
1458 pix_input->height);
1459 return rval;
1461 pix_output->width = pipe->prv_out_w;
1462 pix_output->height = pipe->prv_out_h;
1465 if (pipe->modules & OMAP_ISP_RESIZER) {
1466 pipe->rsz_out_w = wanted_width;
1467 pipe->rsz_out_h = wanted_height;
1469 pipe->rsz_crop.left = pipe->rsz_crop.top = 0;
1470 pipe->rsz_crop.width = pipe->prv_out_w_img;
1471 pipe->rsz_crop.height = pipe->prv_out_h_img;
1473 rval = ispresizer_try_pipeline(&isp->isp_res, pipe);
1474 if (rval) {
1475 dev_err(dev, "The dimensions %dx%d are not"
1476 " supported\n", pix_input->width,
1477 pix_input->height);
1478 return rval;
1481 pix_output->width = pipe->rsz_out_w;
1482 pix_output->height = pipe->rsz_out_h;
1483 pix_output->bytesperline =
1484 pipe->rsz_out_w * ISP_BYTES_PER_PIXEL;
1487 if (isp->bt656ifen)
1488 pix_output->field = pix_input->field;
1489 else {
1490 pix_output->field = V4L2_FIELD_NONE;
1491 pix_output->sizeimage =
1492 PAGE_ALIGN(pix_output->bytesperline *
1493 pix_output->height);
1495 pix_output->priv = 0;
1497 for (ifmt = 0; ifmt < NUM_ISP_CAPTURE_FORMATS; ifmt++) {
1498 if (pix_output->pixelformat == isp_formats[ifmt].pixelformat)
1499 break;
1501 if (ifmt == NUM_ISP_CAPTURE_FORMATS)
1502 pix_output->pixelformat = V4L2_PIX_FMT_YUYV;
1504 switch (pix_output->pixelformat) {
1505 case V4L2_PIX_FMT_YUYV:
1506 case V4L2_PIX_FMT_UYVY:
1507 if (isp->bt656ifen)
1508 pix_output->colorspace = pix_input->colorspace;
1509 else
1510 pix_output->colorspace = V4L2_COLORSPACE_JPEG;
1511 break;
1512 default:
1513 pix_output->colorspace = V4L2_COLORSPACE_SRGB;
1516 return 0;
1520 * isp_s_pipeline - Configure internal ISP pipeline.
1521 * @dev: Device pointer specific to the OMAP3 ISP.
1522 * @pix_input: Pointer to pixel format to use as input in the ISP.
1523 * @pix_output: Pointer to pixel format to use as output in the ISP.
1525 * Returns the closest possible output size based on silicon limitations.
1527 * If the input can't be read, it'll return -EINVAL. Returns 0 on success.
1529 static int isp_s_pipeline(struct device *dev,
1530 struct v4l2_pix_format *pix_input,
1531 struct v4l2_pix_format *pix_output)
1533 struct isp_device *isp = dev_get_drvdata(dev);
1534 struct isp_pipeline pipe;
1535 int rval;
1537 isp_release_resources(dev);
1539 pipe.pix = *pix_output;
1541 rval = isp_try_pipeline(dev, pix_input, &pipe);
1542 if (rval)
1543 return rval;
1545 ispccdc_request(&isp->isp_ccdc);
1546 ispccdc_s_pipeline(&isp->isp_ccdc, &pipe);
1548 if (pix_input->pixelformat == V4L2_PIX_FMT_UYVY)
1549 ispccdc_config_y8pos(&isp->isp_ccdc, Y8POS_ODD);
1550 else if (pix_input->pixelformat == V4L2_PIX_FMT_YUYV)
1551 ispccdc_config_y8pos(&isp->isp_ccdc, Y8POS_EVEN);
1553 if (((pix_input->pixelformat == V4L2_PIX_FMT_UYVY) &&
1554 (pix_output->pixelformat == V4L2_PIX_FMT_UYVY)) ||
1555 ((pix_input->pixelformat == V4L2_PIX_FMT_YUYV) &&
1556 (pix_output->pixelformat == V4L2_PIX_FMT_YUYV)))
1557 /* input and output formats are in same order */
1558 ispccdc_config_byteswap(&isp->isp_ccdc, 0);
1559 else if (((pix_input->pixelformat == V4L2_PIX_FMT_YUYV) &&
1560 (pix_output->pixelformat == V4L2_PIX_FMT_UYVY)) ||
1561 ((pix_input->pixelformat == V4L2_PIX_FMT_UYVY) &&
1562 (pix_output->pixelformat == V4L2_PIX_FMT_YUYV)))
1563 /* input and output formats are in reverse order */
1564 ispccdc_config_byteswap(&isp->isp_ccdc, 1);
1566 * Configure Pitch - This enables application to use a
1567 * different pitch
1568 * other than active pixels per line.
1570 if (isp->bt656ifen)
1571 ispccdc_config_outlineoffset(&isp->isp_ccdc,
1572 pipe.pix.bytesperline, 0, 0);
1573 if (pipe.modules & OMAP_ISP_PREVIEW) {
1574 isppreview_request(&isp->isp_prev);
1575 isppreview_s_pipeline(&isp->isp_prev, &pipe);
1578 if (pipe.modules & OMAP_ISP_RESIZER) {
1579 ispresizer_request(&isp->isp_res);
1580 ispresizer_s_pipeline(&isp->isp_res, &pipe);
1583 isp->pipeline = pipe;
1584 *pix_output = isp->pipeline.pix;
1586 return 0;
1590 * isp_vbq_sync - Flush the entire cache
1591 * @vb: Videobuffer to sync. (Not used)
1592 * @when: (Not used)
1594 * FIXME: This impacts the performance on the other systems when camera is
1595 * running, but seems to be needed to ensure coherency of DMA transfers
1596 * somehow. Investigation ongoing...
1598 static int isp_vbq_sync(struct videobuf_buffer *vb, int when)
1600 flush_cache_all();
1602 return 0;
1606 * isp_buf_init - Initialize the internal buffer queue handling.
1607 * @dev: Device pointer specific to the OMAP3 ISP.
1609 static void isp_buf_init(struct device *dev)
1611 struct isp_device *isp = dev_get_drvdata(dev);
1612 struct isp_bufs *bufs = &isp->bufs;
1613 int sg;
1615 bufs->queue = 0;
1616 bufs->done = 0;
1617 bufs->wait_hs_vs = isp->config->wait_hs_vs;
1618 for (sg = 0; sg < NUM_BUFS; sg++) {
1619 if (bufs->buf[sg].vb) {
1620 isp_vbq_sync(bufs->buf[sg].vb, DMA_FROM_DEVICE);
1621 bufs->buf[sg].vb->state = VIDEOBUF_ERROR;
1622 bufs->buf[sg].complete(bufs->buf[sg].vb,
1623 bufs->buf[sg].priv);
1625 bufs->buf[sg].complete = NULL;
1626 bufs->buf[sg].vb = NULL;
1627 bufs->buf[sg].priv = NULL;
1632 * isp_buf_process - Do final handling when a buffer has been processed.
1633 * @dev: Device pointer specific to the OMAP3 ISP.
1634 * @bufs: Pointer to ISP buffer handling structure.
1636 * Updates the pointers accordingly depending of the internal pipeline.
1638 static int isp_buf_process(struct device *dev, struct isp_bufs *bufs)
1640 struct isp_device *isp = dev_get_drvdata(dev);
1641 struct isp_buf *buf = NULL;
1642 unsigned long flags;
1643 int last;
1645 spin_lock_irqsave(&bufs->lock, flags);
1647 if (ISP_BUFS_IS_EMPTY(bufs))
1648 goto out;
1650 if (RAW_CAPTURE(isp) && ispccdc_sbl_wait_idle(&isp->isp_ccdc, 1000)) {
1651 dev_err(dev, "ccdc %d won't become idle!\n",
1652 RAW_CAPTURE(isp));
1653 goto out;
1656 /* We had at least one buffer in queue. */
1657 buf = ISP_BUF_DONE(bufs);
1658 last = ISP_BUFS_IS_LAST(bufs);
1660 if (!last) {
1661 /* Set new buffer address. */
1662 isp_set_buf(dev, ISP_BUF_NEXT_DONE(bufs));
1663 } else {
1664 /* Tell ISP not to write any of our buffers. */
1665 isp_disable_interrupts(dev);
1666 if (RAW_CAPTURE(isp))
1667 ispccdc_enable(&isp->isp_ccdc, 0);
1668 else if (isp->bt656ifen == 0)
1669 ispresizer_enable(&isp->isp_res, 0);
1671 * We must wait for the HS_VS since before that the
1672 * CCDC may trigger interrupts even if it's not
1673 * receiving a frame.
1675 bufs->wait_hs_vs = isp->config->wait_hs_vs;
1677 if ((RAW_CAPTURE(isp) && ispccdc_busy(&isp->isp_ccdc))
1678 || (!RAW_CAPTURE(isp) && ispresizer_busy(&isp->isp_res))) {
1680 * Next buffer available: for the transfer to succeed, the
1681 * CCDC (RAW capture) or resizer (YUV capture) must be idle
1682 * for the duration of transfer setup. Bad things happen
1683 * otherwise!
1685 * Next buffer not available: if we fail to stop the
1686 * ISP the buffer is probably going to be bad.
1688 /* Mark this buffer faulty. */
1689 buf->vb_state = VIDEOBUF_ERROR;
1690 /* Mark next faulty, too, in case we have one. */
1691 if (!last) {
1692 ISP_BUF_NEXT_DONE(bufs)->vb_state = VIDEOBUF_ERROR;
1693 dev_alert(dev, "OUCH!!!\n");
1694 } else {
1695 dev_alert(dev, "Ouch!\n");
1699 /* Mark the current buffer as done. */
1700 ISP_BUF_MARK_DONE(bufs);
1702 DPRINTK_ISPCTRL(KERN_ALERT "%s: finish %d mmu %p\n", __func__,
1703 (bufs->done - 1 + NUM_BUFS) % NUM_BUFS,
1704 (bufs->buf+((bufs->done - 1 + NUM_BUFS)
1705 % NUM_BUFS))->isp_addr);
1707 out:
1708 spin_unlock_irqrestore(&bufs->lock, flags);
1710 if (buf && buf->vb) {
1712 * We want to dequeue a buffer from the video buffer
1713 * queue. Let's do it!
1715 isp_vbq_sync(buf->vb, DMA_FROM_DEVICE);
1716 buf->vb->state = buf->vb_state;
1717 buf->complete(buf->vb, buf->priv);
1718 buf->vb = NULL;
1721 return 0;
1725 * isp_buf_queue - Queue a buffer into the internal ISP queue list.
1726 * @dev: Device pointer specific to the OMAP3 ISP.
1727 * @vb: Pointer to video buffer to queue.
1728 * @complete: Pointer to function to call when buffer is completely processed.
1729 * @priv: Pointer to private paramemter to send to complete function.
1731 * Always returns 0.
1733 int isp_buf_queue(struct device *dev, struct videobuf_buffer *vb,
1734 void (*complete)(struct videobuf_buffer *vb, void *priv),
1735 void *priv)
1737 struct isp_device *isp = dev_get_drvdata(dev);
1738 unsigned long flags;
1739 struct isp_buf *buf;
1740 struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
1741 const struct scatterlist *sglist = dma->sglist;
1742 struct isp_bufs *bufs = &isp->bufs;
1743 int sglen = dma->sglen;
1745 if (isp->running != ISP_RUNNING) {
1746 vb->state = VIDEOBUF_ERROR;
1747 complete(vb, priv);
1749 return 0;
1752 BUG_ON(sglen < 0 || !sglist);
1754 isp_vbq_sync(vb, DMA_TO_DEVICE);
1756 spin_lock_irqsave(&bufs->lock, flags);
1758 BUG_ON(ISP_BUFS_IS_FULL(bufs));
1760 buf = ISP_BUF_QUEUE(bufs);
1762 buf->isp_addr = bufs->isp_addr_capture[vb->i];
1763 buf->complete = complete;
1764 buf->vb = vb;
1765 buf->priv = priv;
1766 buf->vb_state = VIDEOBUF_DONE;
1767 buf->vb->state = VIDEOBUF_ACTIVE;
1769 if (ISP_BUFS_IS_EMPTY(bufs)) {
1770 isp_enable_interrupts(dev, RAW_CAPTURE(isp));
1771 isp_set_buf(dev, buf);
1772 ispccdc_enable(&isp->isp_ccdc, 1);
1775 ISP_BUF_MARK_QUEUED(bufs);
1777 spin_unlock_irqrestore(&bufs->lock, flags);
1779 DPRINTK_ISPCTRL(KERN_ALERT "%s: queue %d vb %d, mmu %p\n", __func__,
1780 (bufs->queue - 1 + NUM_BUFS) % NUM_BUFS, vb->i,
1781 buf->isp_addr);
1783 return 0;
1785 EXPORT_SYMBOL(isp_buf_queue);
1788 * isp_vbq_setup - Do ISP specific actions when the VB wueue is set.
1789 * @dev: Device pointer specific to the OMAP3 ISP.
1790 * @vbq: Pointer to video buffer queue.
1791 * @cnt: Pointer to buffer count size of the queue list.
1792 * @size: Pointer to the bytesize of every video buffer queue entry.
1794 * Currently, this just allocates the temporary buffer used for the
1795 * ISP Workaround when having CCDC->PRV->RSZ internal datapath.
1797 int isp_vbq_setup(struct device *dev, struct videobuf_queue *vbq,
1798 unsigned int *cnt, unsigned int *size)
1800 struct isp_device *isp = dev_get_drvdata(dev);
1801 size_t tmp_size = PAGE_ALIGN(isp->pipeline.prv_out_w
1802 * isp->pipeline.prv_out_h
1803 * ISP_BYTES_PER_PIXEL);
1805 if (isp->pipeline.modules & OMAP_ISP_PREVIEW
1806 && isp->tmp_buf_size < tmp_size)
1807 return isp_tmp_buf_alloc(dev, tmp_size);
1809 return 0;
1811 EXPORT_SYMBOL(isp_vbq_setup);
1814 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
1815 * @dev: Device pointer specific to the OMAP3 ISP.
1816 * @sglist: Pointer to source Scatter gather list to allocate.
1817 * @sglen: Number of elements of the scatter-gatter list.
1819 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
1820 * we ran out of memory.
1822 dma_addr_t ispmmu_vmap(struct device *dev, const struct scatterlist *sglist,
1823 int sglen)
1825 struct isp_device *isp = dev_get_drvdata(dev);
1826 int err;
1827 u32 da;
1828 struct sg_table *sgt;
1829 unsigned int i;
1830 struct scatterlist *sg, *src = (struct scatterlist *)sglist;
1833 * convert isp sglist to iommu sgt
1834 * FIXME: should be fixed in the upper layer?
1836 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
1837 if (!sgt)
1838 return -ENOMEM;
1839 err = sg_alloc_table(sgt, sglen, GFP_KERNEL);
1840 if (err)
1841 goto err_sg_alloc;
1843 for_each_sg(sgt->sgl, sg, sgt->nents, i)
1844 sg_set_buf(sg, phys_to_virt(sg_dma_address(src + i)),
1845 sg_dma_len(src + i));
1847 da = iommu_vmap(isp->iommu, 0, sgt, IOMMU_FLAG);
1848 if (IS_ERR_VALUE(da))
1849 goto err_vmap;
1851 return (dma_addr_t)da;
1853 err_vmap:
1854 sg_free_table(sgt);
1855 err_sg_alloc:
1856 kfree(sgt);
1857 return -ENOMEM;
1859 EXPORT_SYMBOL_GPL(ispmmu_vmap);
1862 * ispmmu_vunmap - Unmap a device address from the ISP MMU
1863 * @dev: Device pointer specific to the OMAP3 ISP.
1864 * @da: Device address generated from a ispmmu_vmap call.
1866 void ispmmu_vunmap(struct device *dev, dma_addr_t da)
1868 struct isp_device *isp = dev_get_drvdata(dev);
1869 struct sg_table *sgt;
1871 sgt = iommu_vunmap(isp->iommu, (u32)da);
1872 if (!sgt)
1873 return;
1874 sg_free_table(sgt);
1875 kfree(sgt);
1877 EXPORT_SYMBOL_GPL(ispmmu_vunmap);
1880 * isp_vbq_prepare - Videobuffer queue prepare.
1881 * @dev: Device pointer specific to the OMAP3 ISP.
1882 * @vbq: Pointer to videobuf_queue structure.
1883 * @vb: Pointer to videobuf_buffer structure.
1884 * @field: Requested Field order for the videobuffer.
1886 * Returns 0 if successful, or -EIO if the ispmmu was unable to map a
1887 * scatter-gather linked list data space.
1889 int isp_vbq_prepare(struct device *dev, struct videobuf_queue *vbq,
1890 struct videobuf_buffer *vb, enum v4l2_field field)
1892 struct isp_device *isp = dev_get_drvdata(dev);
1893 unsigned int isp_addr;
1894 struct videobuf_dmabuf *vdma;
1895 struct isp_bufs *bufs = &isp->bufs;
1897 int err = 0;
1899 vdma = videobuf_to_dma(vb);
1901 isp_addr = ispmmu_vmap(dev, vdma->sglist, vdma->sglen);
1903 if (IS_ERR_VALUE(isp_addr))
1904 err = -EIO;
1905 else
1906 bufs->isp_addr_capture[vb->i] = isp_addr;
1908 return err;
1910 EXPORT_SYMBOL(isp_vbq_prepare);
1913 * isp_vbq_release - Videobuffer queue release.
1914 * @dev: Device pointer specific to the OMAP3 ISP.
1915 * @vbq: Pointer to videobuf_queue structure.
1916 * @vb: Pointer to videobuf_buffer structure.
1918 void isp_vbq_release(struct device *dev, struct videobuf_queue *vbq,
1919 struct videobuf_buffer *vb)
1921 struct isp_device *isp = dev_get_drvdata(dev);
1922 struct isp_bufs *bufs = &isp->bufs;
1924 ispmmu_vunmap(dev, bufs->isp_addr_capture[vb->i]);
1925 bufs->isp_addr_capture[vb->i] = (dma_addr_t)NULL;
1926 return;
1928 EXPORT_SYMBOL(isp_vbq_release);
1931 * isp_queryctrl - Query V4L2 control from existing controls in ISP.
1932 * @a: Pointer to v4l2_queryctrl structure. It only needs the id field filled.
1934 * Returns 0 if successful, or -EINVAL if not found in ISP.
1936 int isp_queryctrl(struct v4l2_queryctrl *a)
1938 int i;
1940 if (a->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
1941 a->id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
1942 i = find_next_vctrl(a->id);
1943 } else {
1944 i = find_vctrl(a->id);
1947 if (i < 0)
1948 return -EINVAL;
1950 *a = video_control[i].qc;
1951 return 0;
1953 EXPORT_SYMBOL(isp_queryctrl);
1956 * isp_queryctrl - Query V4L2 control from existing controls in ISP.
1957 * @a: Pointer to v4l2_queryctrl structure. It only needs the id field filled.
1959 * Returns 0 if successful, or -EINVAL if not found in ISP.
1961 int isp_querymenu(struct v4l2_querymenu *a)
1963 int i;
1965 i = find_vmenu(a->id, a->index);
1967 if (i < 0)
1968 return -EINVAL;
1970 *a = video_menu[i];
1971 return 0;
1973 EXPORT_SYMBOL(isp_querymenu);
1976 * isp_g_ctrl - Get value of the desired V4L2 control.
1977 * @dev: Device pointer specific to the OMAP3 ISP.
1978 * @a: V4L2 control to read actual value from.
1980 * Return 0 if successful, or -EINVAL if chosen control is not found.
1982 int isp_g_ctrl(struct device *dev, struct v4l2_control *a)
1984 struct isp_device *isp = dev_get_drvdata(dev);
1985 u8 current_value;
1986 int rval = 0;
1988 if (!isp->ref_count)
1989 return -EINVAL;
1991 switch (a->id) {
1992 case V4L2_CID_BRIGHTNESS:
1993 isppreview_query_brightness(&isp->isp_prev, &current_value);
1994 a->value = current_value / ISPPRV_BRIGHT_UNITS;
1995 break;
1996 case V4L2_CID_CONTRAST:
1997 isppreview_query_contrast(&isp->isp_prev, &current_value);
1998 a->value = current_value / ISPPRV_CONTRAST_UNITS;
1999 break;
2000 case V4L2_CID_COLORFX:
2001 isppreview_get_color(&isp->isp_prev, &current_value);
2002 a->value = current_value;
2003 break;
2004 default:
2005 rval = -EINVAL;
2006 break;
2009 return rval;
2011 EXPORT_SYMBOL(isp_g_ctrl);
2014 * isp_s_ctrl - Set value of the desired V4L2 control.
2015 * @dev: Device pointer specific to the OMAP3 ISP.
2016 * @a: V4L2 control to read actual value from.
2018 * Return 0 if successful, -EINVAL if chosen control is not found or value
2019 * is out of bounds, -EFAULT if copy_from_user or copy_to_user operation fails
2020 * from camera abstraction layer related controls or the transfered user space
2021 * pointer via the value field is not set properly.
2023 int isp_s_ctrl(struct device *dev, struct v4l2_control *a)
2025 struct isp_device *isp = dev_get_drvdata(dev);
2026 int rval = 0;
2027 u8 new_value = a->value;
2029 if (!isp->ref_count)
2030 return -EINVAL;
2032 switch (a->id) {
2033 case V4L2_CID_BRIGHTNESS:
2034 if (a->value > ISPPRV_BRIGHT_HIGH)
2035 rval = -EINVAL;
2036 else
2037 isppreview_update_brightness(&isp->isp_prev,
2038 &new_value);
2039 break;
2040 case V4L2_CID_CONTRAST:
2041 if (a->value > ISPPRV_CONTRAST_HIGH)
2042 rval = -EINVAL;
2043 else
2044 isppreview_update_contrast(&isp->isp_prev, &new_value);
2045 break;
2046 case V4L2_CID_COLORFX:
2047 if (a->value > V4L2_COLORFX_SEPIA)
2048 rval = -EINVAL;
2049 else
2050 isppreview_set_color(&isp->isp_prev, &new_value);
2051 break;
2052 default:
2053 rval = -EINVAL;
2054 break;
2057 return rval;
2059 EXPORT_SYMBOL(isp_s_ctrl);
2062 * isp_handle_private - Handle all private ioctls for isp module.
2063 * @dev: Device pointer specific to the OMAP3 ISP.
2064 * @cmd: ioctl cmd value
2065 * @arg: ioctl arg value
2067 * Return 0 if successful, -EINVAL if chosen cmd value is not handled or value
2068 * is out of bounds, -EFAULT if ioctl arg value is not valid.
2069 * Function simply routes the input ioctl cmd id to the appropriate handler in
2070 * the isp module.
2072 int isp_handle_private(struct device *dev, int cmd, void *arg)
2074 struct isp_device *isp = dev_get_drvdata(dev);
2075 int rval = 0;
2077 if (!isp->ref_count)
2078 return -EINVAL;
2080 switch (cmd) {
2081 case VIDIOC_PRIVATE_ISP_CCDC_CFG:
2082 rval = omap34xx_isp_ccdc_config(&isp->isp_ccdc, arg);
2083 break;
2084 case VIDIOC_PRIVATE_ISP_PRV_CFG:
2085 rval = omap34xx_isp_preview_config(&isp->isp_prev, arg);
2086 break;
2087 case VIDIOC_PRIVATE_ISP_AEWB_CFG: {
2088 struct isph3a_aewb_config *params;
2089 params = (struct isph3a_aewb_config *)arg;
2090 rval = isph3a_aewb_configure(&isp->isp_h3a, params);
2092 break;
2093 case VIDIOC_PRIVATE_ISP_AEWB_REQ: {
2094 struct isph3a_aewb_data *data;
2095 data = (struct isph3a_aewb_data *)arg;
2096 rval = isph3a_aewb_request_statistics(&isp->isp_h3a, data);
2098 break;
2099 case VIDIOC_PRIVATE_ISP_HIST_CFG: {
2100 struct isp_hist_config *params;
2101 params = (struct isp_hist_config *)arg;
2102 rval = isp_hist_configure(&isp->isp_hist, params);
2104 break;
2105 case VIDIOC_PRIVATE_ISP_HIST_REQ: {
2106 struct isp_hist_data *data;
2107 data = (struct isp_hist_data *)arg;
2108 rval = isp_hist_request_statistics(&isp->isp_hist, data);
2110 break;
2111 case VIDIOC_PRIVATE_ISP_AF_CFG: {
2112 struct af_configuration *params;
2113 params = (struct af_configuration *)arg;
2114 rval = isp_af_configure(&isp->isp_af, params);
2116 break;
2117 case VIDIOC_PRIVATE_ISP_AF_REQ: {
2118 struct isp_af_data *data;
2119 data = (struct isp_af_data *)arg;
2120 rval = isp_af_request_statistics(&isp->isp_af, data);
2122 break;
2123 default:
2124 rval = -EINVAL;
2125 break;
2127 return rval;
2129 EXPORT_SYMBOL(isp_handle_private);
2132 * isp_enum_fmt_cap - Get more information of chosen format index and type
2133 * @f: Pointer to structure containing index and type of format to read from.
2135 * Returns 0 if successful, or -EINVAL if format index or format type is
2136 * invalid.
2138 int isp_enum_fmt_cap(struct v4l2_fmtdesc *f)
2140 int index = f->index;
2141 enum v4l2_buf_type type = f->type;
2142 int rval = -EINVAL;
2144 if (index >= NUM_ISP_CAPTURE_FORMATS)
2145 goto err;
2147 memset(f, 0, sizeof(*f));
2148 f->index = index;
2149 f->type = type;
2151 switch (f->type) {
2152 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
2153 rval = 0;
2154 break;
2155 default:
2156 goto err;
2159 f->flags = isp_formats[index].flags;
2160 strncpy(f->description, isp_formats[index].description,
2161 sizeof(f->description));
2162 f->pixelformat = isp_formats[index].pixelformat;
2163 err:
2164 return rval;
2166 EXPORT_SYMBOL(isp_enum_fmt_cap);
2169 * isp_g_fmt_cap - Get current output image format.
2170 * @dev: Device pointer specific to the OMAP3 ISP.
2171 * @pix: Pointer to V4L2 format structure to return current output format
2173 void isp_g_fmt_cap(struct device *dev, struct v4l2_pix_format *pix)
2175 struct isp_device *isp = dev_get_drvdata(dev);
2177 *pix = isp->pipeline.pix;
2178 return;
2180 EXPORT_SYMBOL(isp_g_fmt_cap);
2183 * isp_s_fmt_cap - Set I/O formats and crop, and configure pipeline in ISP
2184 * @dev: Device pointer specific to the OMAP3 ISP.
2185 * @pix_input: Pointer to V4L2 format structure to represent current input.
2186 * @pix_output: Pointer to V4L2 format structure to represent current output.
2188 * Returns 0 if successful, -EINVAL if ISP hasn't been opened, or return
2189 * value of isp_s_pipeline if there is an error.
2191 int isp_s_fmt_cap(struct device *dev, struct v4l2_pix_format *pix_input,
2192 struct v4l2_pix_format *pix_output)
2194 struct isp_device *isp = dev_get_drvdata(dev);
2196 if (!isp->ref_count)
2197 return -EINVAL;
2199 return isp_s_pipeline(dev, pix_input, pix_output);
2201 EXPORT_SYMBOL(isp_s_fmt_cap);
2204 * isp_g_crop - Get crop rectangle size and position.
2205 * @dev: Device pointer specific to the OMAP3 ISP.
2206 * @crop: Pointer to V4L2 crop structure to be filled.
2208 * Always returns 0.
2210 int isp_g_crop(struct device *dev, struct v4l2_crop *crop)
2212 struct isp_device *isp = dev_get_drvdata(dev);
2214 if (isp->pipeline.modules & OMAP_ISP_RESIZER) {
2215 crop->c = isp->pipeline.rsz_crop;
2216 } else {
2217 crop->c.left = 0;
2218 crop->c.top = 0;
2219 crop->c.width = isp->pipeline.ccdc_out_w_img;
2220 crop->c.height = isp->pipeline.ccdc_out_h;
2223 return 0;
2225 EXPORT_SYMBOL(isp_g_crop);
2228 * isp_s_crop - Set crop rectangle size and position.
2229 * @dev: Device pointer specific to the OMAP3 ISP.
2230 * @a: Pointer to V4L2 crop structure with desired parameters.
2232 * Always returns 0.
2234 * FIXME: Hardcoded to configure always the resizer, which could not be always
2235 * the case.
2237 int isp_s_crop(struct device *dev, struct v4l2_crop *a)
2239 struct isp_device *isp = dev_get_drvdata(dev);
2241 ispresizer_config_crop(&isp->isp_res, a);
2243 return 0;
2245 EXPORT_SYMBOL(isp_s_crop);
2248 * isp_try_fmt_cap - Try desired input/output image formats
2249 * @dev: Device pointer specific to the OMAP3 ISP.
2250 * @pix_input: Pointer to V4L2 pixel format structure for input image.
2251 * @pix_output: Pointer to V4L2 pixel format structure for output image.
2253 * Returns 0 if successful, or return value of either isp_try_size or
2254 * isp_try_fmt if there is an error.
2256 int isp_try_fmt_cap(struct device *dev, struct v4l2_pix_format *pix_input,
2257 struct v4l2_pix_format *pix_output)
2259 struct isp_pipeline pipe;
2260 int rval;
2262 pipe.pix = *pix_output;
2264 rval = isp_try_pipeline(dev, pix_input, &pipe);
2265 if (rval)
2266 return rval;
2268 *pix_output = pipe.pix;
2270 return 0;
2272 EXPORT_SYMBOL(isp_try_fmt_cap);
2275 * isp_save_ctx - Saves ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
2276 * @dev: Device pointer specific to the OMAP3 ISP.
2278 * Routine for saving the context of each module in the ISP.
2279 * CCDC, HIST, H3A, PREV, RESZ and IOMMU.
2281 static void isp_save_ctx(struct device *dev)
2283 struct isp_device *isp = dev_get_drvdata(dev);
2285 isp_save_context(dev, isp_reg_list);
2286 ispccdc_save_context(dev);
2287 if (isp->iommu)
2288 iommu_save_ctx(isp->iommu);
2289 isphist_save_context(dev);
2290 isph3a_save_context(dev);
2291 isppreview_save_context(dev);
2292 ispresizer_save_context(dev);
2293 ispcsi2_save_context(dev);
2297 * isp_restore_ctx - Restores ISP, CCDC, HIST, H3A, PREV, RESZ & MMU context.
2298 * @dev: Device pointer specific to the OMAP3 ISP.
2300 * Routine for restoring the context of each module in the ISP.
2301 * CCDC, HIST, H3A, PREV, RESZ and IOMMU.
2303 static void isp_restore_ctx(struct device *dev)
2305 struct isp_device *isp = dev_get_drvdata(dev);
2307 isp_restore_context(dev, isp_reg_list);
2308 ispccdc_restore_context(dev);
2309 if (isp->iommu)
2310 iommu_restore_ctx(isp->iommu);
2311 isphist_restore_context(dev);
2312 isph3a_restore_context(dev);
2313 isppreview_restore_context(dev);
2314 ispresizer_restore_context(dev);
2315 ispcsi2_restore_context(dev);
2319 * isp_enable_clocks - Enable ISP clocks
2320 * @dev: Device pointer specific to the OMAP3 ISP.
2322 * Return 0 if successful, or clk_enable return value if any of tthem fails.
2324 static int isp_enable_clocks(struct device *dev)
2326 struct isp_device *isp = dev_get_drvdata(dev);
2327 int r;
2329 r = clk_enable(isp->cam_ick);
2330 if (r) {
2331 dev_err(dev, "clk_enable cam_ick failed\n");
2332 goto out_clk_enable_ick;
2334 r = clk_enable(isp->cam_mclk);
2335 if (r) {
2336 dev_err(dev, "clk_enable cam_mclk failed\n");
2337 goto out_clk_enable_mclk;
2339 r = clk_enable(isp->csi2_fck);
2340 if (r) {
2341 dev_err(dev, "clk_enable csi2_fck failed\n");
2342 goto out_clk_enable_csi2_fclk;
2344 return 0;
2346 out_clk_enable_csi2_fclk:
2347 clk_disable(isp->cam_mclk);
2348 out_clk_enable_mclk:
2349 clk_disable(isp->cam_ick);
2350 out_clk_enable_ick:
2351 return r;
2355 * isp_disable_clocks - Disable ISP clocks
2356 * @dev: Device pointer specific to the OMAP3 ISP.
2358 static void isp_disable_clocks(struct device *dev)
2360 struct isp_device *isp = dev_get_drvdata(dev);
2362 clk_disable(isp->cam_ick);
2363 clk_disable(isp->cam_mclk);
2364 clk_disable(isp->csi2_fck);
2368 * isp_get - Acquire the ISP resource.
2370 * Initializes the clocks for the first acquire.
2372 * Returns pointer for isp device structure.
2374 struct device *isp_get(void)
2376 struct platform_device *pdev = omap3isp_pdev;
2377 struct isp_device *isp;
2378 static int has_context;
2379 int ret_err = 0;
2381 if (!pdev)
2382 return NULL;
2383 isp = platform_get_drvdata(pdev);
2385 DPRINTK_ISPCTRL("isp_get: old %d\n", isp->ref_count);
2386 mutex_lock(&(isp->isp_mutex));
2387 if (isp->ref_count == 0) {
2388 ret_err = isp_enable_clocks(&pdev->dev);
2389 if (ret_err)
2390 goto out_err;
2391 /* We don't want to restore context before saving it! */
2392 if (has_context)
2393 isp_restore_ctx(&pdev->dev);
2394 else
2395 has_context = 1;
2396 } else {
2397 mutex_unlock(&isp->isp_mutex);
2398 return NULL;
2400 isp->ref_count++;
2401 mutex_unlock(&(isp->isp_mutex));
2403 DPRINTK_ISPCTRL("isp_get: new %d\n", isp->ref_count);
2404 /* FIXME: ISP should register as v4l2 device to store its priv data */
2405 return &pdev->dev;
2407 out_err:
2408 mutex_unlock(&(isp->isp_mutex));
2409 return NULL;
2411 EXPORT_SYMBOL(isp_get);
2414 * isp_put - Release the ISP resource.
2416 * Releases the clocks also for the last release.
2418 * Return resulting reference count, or -EBUSY if ISP structure is not
2419 * allocated.
2421 int isp_put(void)
2423 struct platform_device *pdev = omap3isp_pdev;
2424 struct isp_device *isp = platform_get_drvdata(pdev);
2426 if (!isp)
2427 return -EBUSY;
2429 DPRINTK_ISPCTRL("isp_put: old %d\n", isp->ref_count);
2430 mutex_lock(&(isp->isp_mutex));
2431 if (isp->ref_count) {
2432 if (--isp->ref_count == 0) {
2433 isp_save_ctx(&pdev->dev);
2434 isp_tmp_buf_free(&pdev->dev);
2435 isp_release_resources(&pdev->dev);
2436 isp_disable_clocks(&pdev->dev);
2439 mutex_unlock(&(isp->isp_mutex));
2440 DPRINTK_ISPCTRL("isp_put: new %d\n", isp->ref_count);
2441 return isp->ref_count;
2443 EXPORT_SYMBOL(isp_put);
2446 * isp_save_context - Saves the values of the ISP module registers.
2447 * @dev: Device pointer specific to the OMAP3 ISP.
2448 * @reg_list: Structure containing pairs of register address and value to
2449 * modify on OMAP.
2451 void isp_save_context(struct device *dev, struct isp_reg *reg_list)
2453 struct isp_reg *next = reg_list;
2455 for (; next->reg != ISP_TOK_TERM; next++)
2456 next->val = isp_reg_readl(dev, next->mmio_range, next->reg);
2458 EXPORT_SYMBOL(isp_save_context);
2461 * isp_restore_context - Restores the values of the ISP module registers.
2462 * @dev: Device pointer specific to the OMAP3 ISP.
2463 * @reg_list: Structure containing pairs of register address and value to
2464 * modify on OMAP.
2466 void isp_restore_context(struct device *dev, struct isp_reg *reg_list)
2468 struct isp_reg *next = reg_list;
2470 for (; next->reg != ISP_TOK_TERM; next++)
2471 isp_reg_writel(dev, next->val, next->mmio_range, next->reg);
2473 EXPORT_SYMBOL(isp_restore_context);
2476 * isp_remove - Remove ISP platform device
2477 * @pdev: Pointer to ISP platform device
2479 * Always returns 0.
2481 static int isp_remove(struct platform_device *pdev)
2483 struct isp_device *isp = platform_get_drvdata(pdev);
2484 int i;
2486 if (!isp)
2487 return 0;
2489 isp_csi2_cleanup(&pdev->dev);
2490 isp_af_exit(&pdev->dev);
2491 isp_preview_cleanup(&pdev->dev);
2492 isp_resizer_cleanup(&pdev->dev);
2493 isp_get();
2494 if (isp->iommu)
2495 iommu_put(isp->iommu);
2496 isp_put();
2497 isph3a_aewb_cleanup(&pdev->dev);
2498 isp_hist_cleanup(&pdev->dev);
2499 isp_ccdc_cleanup(&pdev->dev);
2501 clk_put(isp->cam_ick);
2502 clk_put(isp->cam_mclk);
2503 clk_put(isp->csi2_fck);
2504 clk_put(isp->l3_ick);
2506 free_irq(isp->irq_num, isp);
2508 for (i = 0; i <= OMAP3_ISP_IOMEM_CSI2PHY; i++) {
2509 if (isp->mmio_base[i]) {
2510 iounmap((void *)isp->mmio_base[i]);
2511 isp->mmio_base[i] = 0;
2514 if (isp->mmio_base_phys[i]) {
2515 release_mem_region(isp->mmio_base_phys[i],
2516 isp->mmio_size[i]);
2517 isp->mmio_base_phys[i] = 0;
2521 omap3isp_pdev = NULL;
2522 kfree(isp);
2524 return 0;
2527 #ifdef CONFIG_PM
2530 * isp_suspend - Suspend routine for the ISP
2531 * @dev: Pointer to ISP device
2533 * Always returns 0.
2535 static int isp_suspend(struct device *dev)
2537 struct isp_device *isp = dev_get_drvdata(dev);
2538 int reset;
2540 DPRINTK_ISPCTRL("isp_suspend: starting\n");
2542 if (mutex_is_locked(&isp->isp_mutex))
2543 dev_err(dev, "%s: bug: isp_mutex is locked\n", __func__);
2545 if (isp->ref_count == 0)
2546 goto out;
2548 isp_disable_interrupts(dev);
2549 reset = isp_suspend_modules(dev);
2550 isp_save_ctx(dev);
2551 if (reset)
2552 isp_reset(dev);
2554 isp_disable_clocks(dev);
2556 out:
2557 DPRINTK_ISPCTRL("isp_suspend: done\n");
2559 return 0;
2563 * isp_resume - Resume routine for the ISP
2564 * @dev: Pointer to ISP device
2566 * Returns 0 if successful, or isp_enable_clocks return value otherwise.
2568 static int isp_resume(struct device *dev)
2570 struct isp_device *isp = dev_get_drvdata(dev);
2571 int ret_err = 0;
2573 DPRINTK_ISPCTRL("isp_resume: starting\n");
2575 if (mutex_is_locked(&isp->isp_mutex))
2576 dev_err(dev, "%s: bug: isp_mutex is locked\n", __func__);
2578 if (isp->ref_count == 0)
2579 goto out;
2581 ret_err = isp_enable_clocks(dev);
2582 if (ret_err)
2583 goto out;
2584 isp_restore_ctx(dev);
2585 isp_resume_modules(dev);
2587 out:
2588 DPRINTK_ISPCTRL("isp_resume: done \n");
2590 return ret_err;
2593 #else
2595 #define isp_suspend NULL
2596 #define isp_resume NULL
2598 #endif /* CONFIG_PM */
2601 * isp_probe - Probe ISP platform device
2602 * @pdev: Pointer to ISP platform device
2604 * Returns 0 if successful,
2605 * -ENOMEM if no memory available,
2606 * -ENODEV if no platform device resources found
2607 * or no space for remapping registers,
2608 * -EINVAL if couldn't install ISR,
2609 * or clk_get return error value.
2611 static int isp_probe(struct platform_device *pdev)
2613 struct isp_device *isp;
2614 int ret_err = 0;
2615 int i;
2617 isp = kzalloc(sizeof(*isp), GFP_KERNEL);
2618 if (!isp) {
2619 dev_err(&pdev->dev, "could not allocate memory\n");
2620 return -ENOMEM;
2623 platform_set_drvdata(pdev, isp);
2625 isp->dev = &pdev->dev;
2627 for (i = 0; i <= OMAP3_ISP_IOMEM_CSI2PHY; i++) {
2628 struct resource *mem;
2629 /* request the mem region for the camera registers */
2630 mem = platform_get_resource(pdev, IORESOURCE_MEM, i);
2631 if (!mem) {
2632 dev_err(isp->dev, "no mem resource?\n");
2633 ret_err = -ENODEV;
2634 goto out_free_mmio;
2637 if (!request_mem_region(mem->start, mem->end - mem->start + 1,
2638 pdev->name)) {
2639 dev_err(isp->dev,
2640 "cannot reserve camera register I/O region\n");
2641 ret_err = -ENODEV;
2642 goto out_free_mmio;
2644 isp->mmio_base_phys[i] = mem->start;
2645 isp->mmio_size[i] = mem->end - mem->start + 1;
2647 /* map the region */
2648 isp->mmio_base[i] = (unsigned long)
2649 ioremap_nocache(isp->mmio_base_phys[i],
2650 isp->mmio_size[i]);
2651 if (!isp->mmio_base[i]) {
2652 dev_err(isp->dev,
2653 "cannot map camera register I/O region\n");
2654 ret_err = -ENODEV;
2655 goto out_free_mmio;
2659 isp->irq_num = platform_get_irq(pdev, 0);
2660 if (isp->irq_num <= 0) {
2661 dev_err(isp->dev, "no irq for camera?\n");
2662 ret_err = -ENODEV;
2663 goto out_free_mmio;
2666 isp->cam_ick = clk_get(&camera_dev, "cam_ick");
2667 if (IS_ERR(isp->cam_ick)) {
2668 dev_err(isp->dev, "clk_get cam_ick failed\n");
2669 ret_err = PTR_ERR(isp->cam_ick);
2670 goto out_free_mmio;
2672 isp->cam_mclk = clk_get(&camera_dev, "cam_mclk");
2673 if (IS_ERR(isp->cam_mclk)) {
2674 dev_err(isp->dev, "clk_get cam_mclk failed\n");
2675 ret_err = PTR_ERR(isp->cam_mclk);
2676 goto out_clk_get_mclk;
2678 isp->csi2_fck = clk_get(&camera_dev, "csi2_96m_fck");
2679 if (IS_ERR(isp->csi2_fck)) {
2680 dev_err(isp->dev, "clk_get csi2_96m_fck failed\n");
2681 ret_err = PTR_ERR(isp->csi2_fck);
2682 goto out_clk_get_csi2_fclk;
2684 isp->l3_ick = clk_get(&camera_dev, "l3_ick");
2685 if (IS_ERR(isp->l3_ick)) {
2686 dev_err(isp->dev, "clk_get l3_ick failed\n");
2687 ret_err = PTR_ERR(isp->l3_ick);
2688 goto out_clk_get_l3_ick;
2691 if (request_irq(isp->irq_num, omap34xx_isp_isr, IRQF_SHARED,
2692 "Omap 3 Camera ISP", pdev)) {
2693 dev_err(isp->dev, "could not install isr\n");
2694 ret_err = -EINVAL;
2695 goto out_request_irq;
2698 isp->ref_count = 0;
2699 omap3isp_pdev = pdev;
2701 mutex_init(&(isp->isp_mutex));
2702 spin_lock_init(&isp->lock);
2703 spin_lock_init(&isp->bufs.lock);
2704 spin_lock_init(&isp->h3a_lock);
2706 isp_get();
2707 isp->iommu = iommu_get("isp");
2708 if (IS_ERR(isp->iommu)) {
2709 ret_err = PTR_ERR(isp->iommu);
2710 isp->iommu = NULL;
2712 isp_put();
2713 if (!isp->iommu)
2714 goto out_iommu_get;
2716 isp_ccdc_init(&pdev->dev);
2717 isp_hist_init(&pdev->dev);
2718 isph3a_aewb_init(&pdev->dev);
2719 isp_preview_init(&pdev->dev);
2720 isp_resizer_init(&pdev->dev);
2721 isp_af_init(&pdev->dev);
2722 isp_csi2_init(&pdev->dev);
2724 isp_get();
2725 isp_power_settings(&pdev->dev, 1);
2726 isp_put();
2728 return 0;
2730 out_iommu_get:
2731 free_irq(isp->irq_num, isp);
2732 omap3isp_pdev = NULL;
2733 out_request_irq:
2734 clk_put(isp->l3_ick);
2735 out_clk_get_l3_ick:
2736 clk_put(isp->csi2_fck);
2737 out_clk_get_csi2_fclk:
2738 clk_put(isp->cam_mclk);
2739 out_clk_get_mclk:
2740 clk_put(isp->cam_ick);
2741 out_free_mmio:
2742 for (i = 0; i <= OMAP3_ISP_IOMEM_CSI2PHY; i++) {
2743 if (isp->mmio_base[i]) {
2744 iounmap((void *)isp->mmio_base[i]);
2745 isp->mmio_base[i] = 0;
2748 if (isp->mmio_base_phys[i]) {
2749 release_mem_region(isp->mmio_base_phys[i],
2750 isp->mmio_size[i]);
2751 isp->mmio_base_phys[i] = 0;
2755 kfree(isp);
2756 return ret_err;
2759 static struct dev_pm_ops omap3isp_pm_ops = {
2760 .suspend = isp_suspend,
2761 .resume = isp_resume,
2764 static struct platform_driver omap3isp_driver = {
2765 .probe = isp_probe,
2766 .remove = isp_remove,
2767 .driver = {
2768 .name = "omap3isp",
2769 .pm = &omap3isp_pm_ops,
2774 * isp_init - ISP module initialization.
2776 static int __init isp_init(void)
2778 return platform_driver_register(&omap3isp_driver);
2782 * isp_cleanup - ISP module cleanup.
2784 static void __exit isp_cleanup(void)
2786 platform_driver_unregister(&omap3isp_driver);
2790 * isp_print_status - Prints the values of the ISP Control Module registers
2791 * @dev: Device pointer specific to the OMAP3 ISP.
2793 void isp_print_status(struct device *dev)
2795 if (!is_ispctrl_debug_enabled())
2796 return;
2798 DPRINTK_ISPCTRL("###ISP_CTRL=0x%x\n",
2799 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN, ISP_CTRL));
2800 DPRINTK_ISPCTRL("###ISP_TCTRL_CTRL=0x%x\n",
2801 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN,
2802 ISP_TCTRL_CTRL));
2803 DPRINTK_ISPCTRL("###ISP_SYSCONFIG=0x%x\n",
2804 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN,
2805 ISP_SYSCONFIG));
2806 DPRINTK_ISPCTRL("###ISP_SYSSTATUS=0x%x\n",
2807 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN,
2808 ISP_SYSSTATUS));
2809 DPRINTK_ISPCTRL("###ISP_IRQ0ENABLE=0x%x\n",
2810 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN,
2811 ISP_IRQ0ENABLE));
2812 DPRINTK_ISPCTRL("###ISP_IRQ0STATUS=0x%x\n",
2813 isp_reg_readl(dev, OMAP3_ISP_IOMEM_MAIN,
2814 ISP_IRQ0STATUS));
2816 EXPORT_SYMBOL(isp_print_status);
2818 module_init(isp_init);
2819 module_exit(isp_cleanup);
2821 MODULE_AUTHOR("Texas Instruments");
2822 MODULE_DESCRIPTION("ISP Control Module Library");
2823 MODULE_LICENSE("GPL");