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