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