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