goldfish: Fix build error of missing ioremap on UM
[linux-2.6-block.git] / drivers / staging / media / omap4iss / iss.c
1 /*
2  * TI OMAP4 ISS V4L2 Driver
3  *
4  * Copyright (C) 2012, Texas Instruments
5  *
6  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
25 #include <linux/vmalloc.h>
26
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
30
31 #include "iss.h"
32 #include "iss_regs.h"
33
34 #define ISS_PRINT_REGISTER(iss, name)\
35         dev_dbg(iss->dev, "###ISS " #name "=0x%08x\n", \
36                 iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_##name))
37
38 static void iss_print_status(struct iss_device *iss)
39 {
40         dev_dbg(iss->dev, "-------------ISS HL Register dump-------------\n");
41
42         ISS_PRINT_REGISTER(iss, HL_REVISION);
43         ISS_PRINT_REGISTER(iss, HL_SYSCONFIG);
44         ISS_PRINT_REGISTER(iss, HL_IRQSTATUS(5));
45         ISS_PRINT_REGISTER(iss, HL_IRQENABLE_SET(5));
46         ISS_PRINT_REGISTER(iss, HL_IRQENABLE_CLR(5));
47         ISS_PRINT_REGISTER(iss, CTRL);
48         ISS_PRINT_REGISTER(iss, CLKCTRL);
49         ISS_PRINT_REGISTER(iss, CLKSTAT);
50
51         dev_dbg(iss->dev, "-----------------------------------------------\n");
52 }
53
54 /*
55  * omap4iss_flush - Post pending L3 bus writes by doing a register readback
56  * @iss: OMAP4 ISS device
57  *
58  * In order to force posting of pending writes, we need to write and
59  * readback the same register, in this case the revision register.
60  *
61  * See this link for reference:
62  *   http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
63  */
64 void omap4iss_flush(struct iss_device *iss)
65 {
66         iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_HL_REVISION, 0);
67         iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_HL_REVISION);
68 }
69
70 /*
71  * iss_isp_enable_interrupts - Enable ISS ISP interrupts.
72  * @iss: OMAP4 ISS device
73  */
74 static void omap4iss_isp_enable_interrupts(struct iss_device *iss)
75 {
76         static const u32 isp_irq = ISP5_IRQ_OCP_ERR |
77                                    ISP5_IRQ_RSZ_FIFO_IN_BLK_ERR |
78                                    ISP5_IRQ_RSZ_FIFO_OVF |
79                                    ISP5_IRQ_RSZ_INT_DMA |
80                                    ISP5_IRQ_ISIF_INT(0);
81
82         /* Enable ISP interrupts */
83         iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_IRQSTATUS(0), isp_irq);
84         iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_IRQENABLE_SET(0),
85                       isp_irq);
86 }
87
88 /*
89  * iss_isp_disable_interrupts - Disable ISS interrupts.
90  * @iss: OMAP4 ISS device
91  */
92 static void omap4iss_isp_disable_interrupts(struct iss_device *iss)
93 {
94         iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_IRQENABLE_CLR(0), ~0);
95 }
96
97 /*
98  * iss_enable_interrupts - Enable ISS interrupts.
99  * @iss: OMAP4 ISS device
100  */
101 static void iss_enable_interrupts(struct iss_device *iss)
102 {
103         static const u32 hl_irq = ISS_HL_IRQ_CSIA | ISS_HL_IRQ_CSIB
104                                 | ISS_HL_IRQ_ISP(0);
105
106         /* Enable HL interrupts */
107         iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_HL_IRQSTATUS(5), hl_irq);
108         iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_HL_IRQENABLE_SET(5), hl_irq);
109
110         if (iss->regs[OMAP4_ISS_MEM_ISP_SYS1])
111                 omap4iss_isp_enable_interrupts(iss);
112 }
113
114 /*
115  * iss_disable_interrupts - Disable ISS interrupts.
116  * @iss: OMAP4 ISS device
117  */
118 static void iss_disable_interrupts(struct iss_device *iss)
119 {
120         if (iss->regs[OMAP4_ISS_MEM_ISP_SYS1])
121                 omap4iss_isp_disable_interrupts(iss);
122
123         iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_HL_IRQENABLE_CLR(5), ~0);
124 }
125
126 int omap4iss_get_external_info(struct iss_pipeline *pipe,
127                                struct media_link *link)
128 {
129         struct iss_device *iss =
130                 container_of(pipe, struct iss_video, pipe)->iss;
131         struct v4l2_subdev_format fmt;
132         struct v4l2_ctrl *ctrl;
133         int ret;
134
135         if (!pipe->external)
136                 return 0;
137
138         if (pipe->external_rate)
139                 return 0;
140
141         memset(&fmt, 0, sizeof(fmt));
142
143         fmt.pad = link->source->index;
144         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
145         ret = v4l2_subdev_call(media_entity_to_v4l2_subdev(link->sink->entity),
146                                pad, get_fmt, NULL, &fmt);
147         if (ret < 0)
148                 return -EPIPE;
149
150         pipe->external_bpp = omap4iss_video_format_info(fmt.format.code)->bpp;
151
152         ctrl = v4l2_ctrl_find(pipe->external->ctrl_handler,
153                               V4L2_CID_PIXEL_RATE);
154         if (!ctrl) {
155                 dev_warn(iss->dev, "no pixel rate control in subdev %s\n",
156                          pipe->external->name);
157                 return -EPIPE;
158         }
159
160         pipe->external_rate = v4l2_ctrl_g_ctrl_int64(ctrl);
161
162         return 0;
163 }
164
165 /*
166  * Configure the bridge. Valid inputs are
167  *
168  * IPIPEIF_INPUT_CSI2A: CSI2a receiver
169  * IPIPEIF_INPUT_CSI2B: CSI2b receiver
170  *
171  * The bridge and lane shifter are configured according to the selected input
172  * and the ISP platform data.
173  */
174 void omap4iss_configure_bridge(struct iss_device *iss,
175                                enum ipipeif_input_entity input)
176 {
177         u32 issctrl_val;
178         u32 isp5ctrl_val;
179
180         issctrl_val = iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_CTRL);
181         issctrl_val &= ~ISS_CTRL_INPUT_SEL_MASK;
182         issctrl_val &= ~ISS_CTRL_CLK_DIV_MASK;
183
184         isp5ctrl_val = iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL);
185
186         switch (input) {
187         case IPIPEIF_INPUT_CSI2A:
188                 issctrl_val |= ISS_CTRL_INPUT_SEL_CSI2A;
189                 break;
190
191         case IPIPEIF_INPUT_CSI2B:
192                 issctrl_val |= ISS_CTRL_INPUT_SEL_CSI2B;
193                 break;
194
195         default:
196                 return;
197         }
198
199         issctrl_val |= ISS_CTRL_SYNC_DETECT_VS_RAISING;
200
201         isp5ctrl_val |= ISP5_CTRL_VD_PULSE_EXT | ISP5_CTRL_PSYNC_CLK_SEL |
202                         ISP5_CTRL_SYNC_ENABLE;
203
204         iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_CTRL, issctrl_val);
205         iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL, isp5ctrl_val);
206 }
207
208 #ifdef ISS_ISR_DEBUG
209 static void iss_isr_dbg(struct iss_device *iss, u32 irqstatus)
210 {
211         static const char * const name[] = {
212                 "ISP_0",
213                 "ISP_1",
214                 "ISP_2",
215                 "ISP_3",
216                 "CSIA",
217                 "CSIB",
218                 "CCP2_0",
219                 "CCP2_1",
220                 "CCP2_2",
221                 "CCP2_3",
222                 "CBUFF",
223                 "BTE",
224                 "SIMCOP_0",
225                 "SIMCOP_1",
226                 "SIMCOP_2",
227                 "SIMCOP_3",
228                 "CCP2_8",
229                 "HS_VS",
230                 "18",
231                 "19",
232                 "20",
233                 "21",
234                 "22",
235                 "23",
236                 "24",
237                 "25",
238                 "26",
239                 "27",
240                 "28",
241                 "29",
242                 "30",
243                 "31",
244         };
245         unsigned int i;
246
247         dev_dbg(iss->dev, "ISS IRQ: ");
248
249         for (i = 0; i < ARRAY_SIZE(name); i++) {
250                 if ((1 << i) & irqstatus)
251                         pr_cont("%s ", name[i]);
252         }
253         pr_cont("\n");
254 }
255
256 static void iss_isp_isr_dbg(struct iss_device *iss, u32 irqstatus)
257 {
258         static const char * const name[] = {
259                 "ISIF_0",
260                 "ISIF_1",
261                 "ISIF_2",
262                 "ISIF_3",
263                 "IPIPEREQ",
264                 "IPIPELAST_PIX",
265                 "IPIPEDMA",
266                 "IPIPEBSC",
267                 "IPIPEHST",
268                 "IPIPEIF",
269                 "AEW",
270                 "AF",
271                 "H3A",
272                 "RSZ_REG",
273                 "RSZ_LAST_PIX",
274                 "RSZ_DMA",
275                 "RSZ_CYC_RZA",
276                 "RSZ_CYC_RZB",
277                 "RSZ_FIFO_OVF",
278                 "RSZ_FIFO_IN_BLK_ERR",
279                 "20",
280                 "21",
281                 "RSZ_EOF0",
282                 "RSZ_EOF1",
283                 "H3A_EOF",
284                 "IPIPE_EOF",
285                 "26",
286                 "IPIPE_DPC_INI",
287                 "IPIPE_DPC_RNEW0",
288                 "IPIPE_DPC_RNEW1",
289                 "30",
290                 "OCP_ERR",
291         };
292         unsigned int i;
293
294         dev_dbg(iss->dev, "ISP IRQ: ");
295
296         for (i = 0; i < ARRAY_SIZE(name); i++) {
297                 if ((1 << i) & irqstatus)
298                         pr_cont("%s ", name[i]);
299         }
300         pr_cont("\n");
301 }
302 #endif
303
304 /*
305  * iss_isr - Interrupt Service Routine for ISS module.
306  * @irq: Not used currently.
307  * @_iss: Pointer to the OMAP4 ISS device
308  *
309  * Handles the corresponding callback if plugged in.
310  *
311  * Returns IRQ_HANDLED when IRQ was correctly handled, or IRQ_NONE when the
312  * IRQ wasn't handled.
313  */
314 static irqreturn_t iss_isr(int irq, void *_iss)
315 {
316         static const u32 ipipeif_events = ISP5_IRQ_IPIPEIF_IRQ |
317                                           ISP5_IRQ_ISIF_INT(0);
318         static const u32 resizer_events = ISP5_IRQ_RSZ_FIFO_IN_BLK_ERR |
319                                           ISP5_IRQ_RSZ_FIFO_OVF |
320                                           ISP5_IRQ_RSZ_INT_DMA;
321         struct iss_device *iss = _iss;
322         u32 irqstatus;
323
324         irqstatus = iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_HL_IRQSTATUS(5));
325         iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_HL_IRQSTATUS(5), irqstatus);
326
327         if (irqstatus & ISS_HL_IRQ_CSIA)
328                 omap4iss_csi2_isr(&iss->csi2a);
329
330         if (irqstatus & ISS_HL_IRQ_CSIB)
331                 omap4iss_csi2_isr(&iss->csi2b);
332
333         if (irqstatus & ISS_HL_IRQ_ISP(0)) {
334                 u32 isp_irqstatus = iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1,
335                                                  ISP5_IRQSTATUS(0));
336                 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_IRQSTATUS(0),
337                               isp_irqstatus);
338
339                 if (isp_irqstatus & ISP5_IRQ_OCP_ERR)
340                         dev_dbg(iss->dev, "ISP5 OCP Error!\n");
341
342                 if (isp_irqstatus & ipipeif_events) {
343                         omap4iss_ipipeif_isr(&iss->ipipeif,
344                                              isp_irqstatus & ipipeif_events);
345                 }
346
347                 if (isp_irqstatus & resizer_events)
348                         omap4iss_resizer_isr(&iss->resizer,
349                                              isp_irqstatus & resizer_events);
350
351 #ifdef ISS_ISR_DEBUG
352                 iss_isp_isr_dbg(iss, isp_irqstatus);
353 #endif
354         }
355
356         omap4iss_flush(iss);
357
358 #ifdef ISS_ISR_DEBUG
359         iss_isr_dbg(iss, irqstatus);
360 #endif
361
362         return IRQ_HANDLED;
363 }
364
365 /* -----------------------------------------------------------------------------
366  * Pipeline power management
367  *
368  * Entities must be powered up when part of a pipeline that contains at least
369  * one open video device node.
370  *
371  * To achieve this use the entity use_count field to track the number of users.
372  * For entities corresponding to video device nodes the use_count field stores
373  * the users count of the node. For entities corresponding to subdevs the
374  * use_count field stores the total number of users of all video device nodes
375  * in the pipeline.
376  *
377  * The omap4iss_pipeline_pm_use() function must be called in the open() and
378  * close() handlers of video device nodes. It increments or decrements the use
379  * count of all subdev entities in the pipeline.
380  *
381  * To react to link management on powered pipelines, the link setup notification
382  * callback updates the use count of all entities in the source and sink sides
383  * of the link.
384  */
385
386 /*
387  * iss_pipeline_pm_use_count - Count the number of users of a pipeline
388  * @entity: The entity
389  *
390  * Return the total number of users of all video device nodes in the pipeline.
391  */
392 static int iss_pipeline_pm_use_count(struct media_entity *entity,
393                                      struct media_entity_graph *graph)
394 {
395         int use = 0;
396
397         media_entity_graph_walk_start(graph, entity);
398
399         while ((entity = media_entity_graph_walk_next(graph))) {
400                 if (is_media_entity_v4l2_io(entity))
401                         use += entity->use_count;
402         }
403
404         return use;
405 }
406
407 /*
408  * iss_pipeline_pm_power_one - Apply power change to an entity
409  * @entity: The entity
410  * @change: Use count change
411  *
412  * Change the entity use count by @change. If the entity is a subdev update its
413  * power state by calling the core::s_power operation when the use count goes
414  * from 0 to != 0 or from != 0 to 0.
415  *
416  * Return 0 on success or a negative error code on failure.
417  */
418 static int iss_pipeline_pm_power_one(struct media_entity *entity, int change)
419 {
420         struct v4l2_subdev *subdev;
421
422         subdev = is_media_entity_v4l2_subdev(entity)
423                ? media_entity_to_v4l2_subdev(entity) : NULL;
424
425         if (entity->use_count == 0 && change > 0 && subdev) {
426                 int ret;
427
428                 ret = v4l2_subdev_call(subdev, core, s_power, 1);
429                 if (ret < 0 && ret != -ENOIOCTLCMD)
430                         return ret;
431         }
432
433         entity->use_count += change;
434         WARN_ON(entity->use_count < 0);
435
436         if (entity->use_count == 0 && change < 0 && subdev)
437                 v4l2_subdev_call(subdev, core, s_power, 0);
438
439         return 0;
440 }
441
442 /*
443  * iss_pipeline_pm_power - Apply power change to all entities in a pipeline
444  * @entity: The entity
445  * @change: Use count change
446  *
447  * Walk the pipeline to update the use count and the power state of all non-node
448  * entities.
449  *
450  * Return 0 on success or a negative error code on failure.
451  */
452 static int iss_pipeline_pm_power(struct media_entity *entity, int change,
453                                  struct media_entity_graph *graph)
454 {
455         struct media_entity *first = entity;
456         int ret = 0;
457
458         if (!change)
459                 return 0;
460
461         media_entity_graph_walk_start(graph, entity);
462
463         while (!ret && (entity = media_entity_graph_walk_next(graph)))
464                 if (is_media_entity_v4l2_subdev(entity))
465                         ret = iss_pipeline_pm_power_one(entity, change);
466
467         if (!ret)
468                 return 0;
469
470         media_entity_graph_walk_start(graph, first);
471
472         while ((first = media_entity_graph_walk_next(graph)) &&
473                first != entity)
474                 if (is_media_entity_v4l2_subdev(first))
475                         iss_pipeline_pm_power_one(first, -change);
476
477         return ret;
478 }
479
480 /*
481  * omap4iss_pipeline_pm_use - Update the use count of an entity
482  * @entity: The entity
483  * @use: Use (1) or stop using (0) the entity
484  *
485  * Update the use count of all entities in the pipeline and power entities on or
486  * off accordingly.
487  *
488  * Return 0 on success or a negative error code on failure. Powering entities
489  * off is assumed to never fail. No failure can occur when the use parameter is
490  * set to 0.
491  */
492 int omap4iss_pipeline_pm_use(struct media_entity *entity, int use,
493                              struct media_entity_graph *graph)
494 {
495         int change = use ? 1 : -1;
496         int ret;
497
498         mutex_lock(&entity->graph_obj.mdev->graph_mutex);
499
500         /* Apply use count to node. */
501         entity->use_count += change;
502         WARN_ON(entity->use_count < 0);
503
504         /* Apply power change to connected non-nodes. */
505         ret = iss_pipeline_pm_power(entity, change, graph);
506         if (ret < 0)
507                 entity->use_count -= change;
508
509         mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
510
511         return ret;
512 }
513
514 /*
515  * iss_pipeline_link_notify - Link management notification callback
516  * @link: The link
517  * @flags: New link flags that will be applied
518  *
519  * React to link management on powered pipelines by updating the use count of
520  * all entities in the source and sink sides of the link. Entities are powered
521  * on or off accordingly.
522  *
523  * Return 0 on success or a negative error code on failure. Powering entities
524  * off is assumed to never fail. This function will not fail for disconnection
525  * events.
526  */
527 static int iss_pipeline_link_notify(struct media_link *link, u32 flags,
528                                     unsigned int notification)
529 {
530         struct media_entity_graph *graph =
531                 &container_of(link->graph_obj.mdev, struct iss_device,
532                               media_dev)->pm_count_graph;
533         struct media_entity *source = link->source->entity;
534         struct media_entity *sink = link->sink->entity;
535         int source_use;
536         int sink_use;
537         int ret;
538
539         if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
540                 ret = media_entity_graph_walk_init(graph,
541                                                    link->graph_obj.mdev);
542                 if (ret)
543                         return ret;
544         }
545
546         source_use = iss_pipeline_pm_use_count(source, graph);
547         sink_use = iss_pipeline_pm_use_count(sink, graph);
548
549         if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
550             !(flags & MEDIA_LNK_FL_ENABLED)) {
551                 /* Powering off entities is assumed to never fail. */
552                 iss_pipeline_pm_power(source, -sink_use, graph);
553                 iss_pipeline_pm_power(sink, -source_use, graph);
554                 return 0;
555         }
556
557         if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
558             (flags & MEDIA_LNK_FL_ENABLED)) {
559                 ret = iss_pipeline_pm_power(source, sink_use, graph);
560                 if (ret < 0)
561                         return ret;
562
563                 ret = iss_pipeline_pm_power(sink, source_use, graph);
564                 if (ret < 0)
565                         iss_pipeline_pm_power(source, -sink_use, graph);
566         }
567
568         if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH)
569                 media_entity_graph_walk_cleanup(graph);
570
571         return ret;
572 }
573
574 /* -----------------------------------------------------------------------------
575  * Pipeline stream management
576  */
577
578 /*
579  * iss_pipeline_disable - Disable streaming on a pipeline
580  * @pipe: ISS pipeline
581  * @until: entity at which to stop pipeline walk
582  *
583  * Walk the entities chain starting at the pipeline output video node and stop
584  * all modules in the chain. Wait synchronously for the modules to be stopped if
585  * necessary.
586  *
587  * If the until argument isn't NULL, stop the pipeline walk when reaching the
588  * until entity. This is used to disable a partially started pipeline due to a
589  * subdev start error.
590  */
591 static int iss_pipeline_disable(struct iss_pipeline *pipe,
592                                 struct media_entity *until)
593 {
594         struct iss_device *iss = pipe->output->iss;
595         struct media_entity *entity;
596         struct media_pad *pad;
597         struct v4l2_subdev *subdev;
598         int failure = 0;
599         int ret;
600
601         entity = &pipe->output->video.entity;
602         while (1) {
603                 pad = &entity->pads[0];
604                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
605                         break;
606
607                 pad = media_entity_remote_pad(pad);
608                 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
609                         break;
610
611                 entity = pad->entity;
612                 if (entity == until)
613                         break;
614
615                 subdev = media_entity_to_v4l2_subdev(entity);
616                 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
617                 if (ret < 0) {
618                         dev_warn(iss->dev, "%s: module stop timeout.\n",
619                                  subdev->name);
620                         /* If the entity failed to stopped, assume it has
621                          * crashed. Mark it as such, the ISS will be reset when
622                          * applications will release it.
623                          */
624                         media_entity_enum_set(&iss->crashed, &subdev->entity);
625                         failure = -ETIMEDOUT;
626                 }
627         }
628
629         return failure;
630 }
631
632 /*
633  * iss_pipeline_enable - Enable streaming on a pipeline
634  * @pipe: ISS pipeline
635  * @mode: Stream mode (single shot or continuous)
636  *
637  * Walk the entities chain starting at the pipeline output video node and start
638  * all modules in the chain in the given mode.
639  *
640  * Return 0 if successful, or the return value of the failed video::s_stream
641  * operation otherwise.
642  */
643 static int iss_pipeline_enable(struct iss_pipeline *pipe,
644                                enum iss_pipeline_stream_state mode)
645 {
646         struct iss_device *iss = pipe->output->iss;
647         struct media_entity *entity;
648         struct media_pad *pad;
649         struct v4l2_subdev *subdev;
650         unsigned long flags;
651         int ret;
652
653         /* If one of the entities in the pipeline has crashed it will not work
654          * properly. Refuse to start streaming in that case. This check must be
655          * performed before the loop below to avoid starting entities if the
656          * pipeline won't start anyway (those entities would then likely fail to
657          * stop, making the problem worse).
658          */
659         if (media_entity_enum_intersects(&pipe->ent_enum, &iss->crashed))
660                 return -EIO;
661
662         spin_lock_irqsave(&pipe->lock, flags);
663         pipe->state &= ~(ISS_PIPELINE_IDLE_INPUT | ISS_PIPELINE_IDLE_OUTPUT);
664         spin_unlock_irqrestore(&pipe->lock, flags);
665
666         pipe->do_propagation = false;
667
668         entity = &pipe->output->video.entity;
669         while (1) {
670                 pad = &entity->pads[0];
671                 if (!(pad->flags & MEDIA_PAD_FL_SINK))
672                         break;
673
674                 pad = media_entity_remote_pad(pad);
675                 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
676                         break;
677
678                 entity = pad->entity;
679                 subdev = media_entity_to_v4l2_subdev(entity);
680
681                 ret = v4l2_subdev_call(subdev, video, s_stream, mode);
682                 if (ret < 0 && ret != -ENOIOCTLCMD) {
683                         iss_pipeline_disable(pipe, entity);
684                         return ret;
685                 }
686
687                 if (subdev == &iss->csi2a.subdev ||
688                     subdev == &iss->csi2b.subdev)
689                         pipe->do_propagation = true;
690         }
691
692         iss_print_status(pipe->output->iss);
693         return 0;
694 }
695
696 /*
697  * omap4iss_pipeline_set_stream - Enable/disable streaming on a pipeline
698  * @pipe: ISS pipeline
699  * @state: Stream state (stopped, single shot or continuous)
700  *
701  * Set the pipeline to the given stream state. Pipelines can be started in
702  * single-shot or continuous mode.
703  *
704  * Return 0 if successful, or the return value of the failed video::s_stream
705  * operation otherwise. The pipeline state is not updated when the operation
706  * fails, except when stopping the pipeline.
707  */
708 int omap4iss_pipeline_set_stream(struct iss_pipeline *pipe,
709                                  enum iss_pipeline_stream_state state)
710 {
711         int ret;
712
713         if (state == ISS_PIPELINE_STREAM_STOPPED)
714                 ret = iss_pipeline_disable(pipe, NULL);
715         else
716                 ret = iss_pipeline_enable(pipe, state);
717
718         if (ret == 0 || state == ISS_PIPELINE_STREAM_STOPPED)
719                 pipe->stream_state = state;
720
721         return ret;
722 }
723
724 /*
725  * omap4iss_pipeline_cancel_stream - Cancel stream on a pipeline
726  * @pipe: ISS pipeline
727  *
728  * Cancelling a stream mark all buffers on all video nodes in the pipeline as
729  * erroneous and makes sure no new buffer can be queued. This function is called
730  * when a fatal error that prevents any further operation on the pipeline
731  * occurs.
732  */
733 void omap4iss_pipeline_cancel_stream(struct iss_pipeline *pipe)
734 {
735         if (pipe->input)
736                 omap4iss_video_cancel_stream(pipe->input);
737         if (pipe->output)
738                 omap4iss_video_cancel_stream(pipe->output);
739 }
740
741 /*
742  * iss_pipeline_is_last - Verify if entity has an enabled link to the output
743  *                        video node
744  * @me: ISS module's media entity
745  *
746  * Returns 1 if the entity has an enabled link to the output video node or 0
747  * otherwise. It's true only while pipeline can have no more than one output
748  * node.
749  */
750 static int iss_pipeline_is_last(struct media_entity *me)
751 {
752         struct iss_pipeline *pipe;
753         struct media_pad *pad;
754
755         if (!me->pipe)
756                 return 0;
757         pipe = to_iss_pipeline(me);
758         if (pipe->stream_state == ISS_PIPELINE_STREAM_STOPPED)
759                 return 0;
760         pad = media_entity_remote_pad(&pipe->output->pad);
761         return pad->entity == me;
762 }
763
764 static int iss_reset(struct iss_device *iss)
765 {
766         unsigned int timeout;
767
768         iss_reg_set(iss, OMAP4_ISS_MEM_TOP, ISS_HL_SYSCONFIG,
769                     ISS_HL_SYSCONFIG_SOFTRESET);
770
771         timeout = iss_poll_condition_timeout(
772                 !(iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_HL_SYSCONFIG) &
773                 ISS_HL_SYSCONFIG_SOFTRESET), 1000, 10, 100);
774         if (timeout) {
775                 dev_err(iss->dev, "ISS reset timeout\n");
776                 return -ETIMEDOUT;
777         }
778
779         media_entity_enum_zero(&iss->crashed);
780
781         return 0;
782 }
783
784 static int iss_isp_reset(struct iss_device *iss)
785 {
786         unsigned int timeout;
787
788         /* Fist, ensure that the ISP is IDLE (no transactions happening) */
789         iss_reg_update(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_SYSCONFIG,
790                        ISP5_SYSCONFIG_STANDBYMODE_MASK,
791                        ISP5_SYSCONFIG_STANDBYMODE_SMART);
792
793         iss_reg_set(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL, ISP5_CTRL_MSTANDBY);
794
795         timeout = iss_poll_condition_timeout(
796                 iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL) &
797                 ISP5_CTRL_MSTANDBY_WAIT, 1000000, 1000, 1500);
798         if (timeout) {
799                 dev_err(iss->dev, "ISP5 standby timeout\n");
800                 return -ETIMEDOUT;
801         }
802
803         /* Now finally, do the reset */
804         iss_reg_set(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_SYSCONFIG,
805                     ISP5_SYSCONFIG_SOFTRESET);
806
807         timeout = iss_poll_condition_timeout(
808                 !(iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_SYSCONFIG) &
809                 ISP5_SYSCONFIG_SOFTRESET), 1000000, 1000, 1500);
810         if (timeout) {
811                 dev_err(iss->dev, "ISP5 reset timeout\n");
812                 return -ETIMEDOUT;
813         }
814
815         return 0;
816 }
817
818 /*
819  * iss_module_sync_idle - Helper to sync module with its idle state
820  * @me: ISS submodule's media entity
821  * @wait: ISS submodule's wait queue for streamoff/interrupt synchronization
822  * @stopping: flag which tells module wants to stop
823  *
824  * This function checks if ISS submodule needs to wait for next interrupt. If
825  * yes, makes the caller to sleep while waiting for such event.
826  */
827 int omap4iss_module_sync_idle(struct media_entity *me, wait_queue_head_t *wait,
828                               atomic_t *stopping)
829 {
830         struct iss_pipeline *pipe = to_iss_pipeline(me);
831         struct iss_video *video = pipe->output;
832         unsigned long flags;
833
834         if (pipe->stream_state == ISS_PIPELINE_STREAM_STOPPED ||
835             (pipe->stream_state == ISS_PIPELINE_STREAM_SINGLESHOT &&
836              !iss_pipeline_ready(pipe)))
837                 return 0;
838
839         /*
840          * atomic_set() doesn't include memory barrier on ARM platform for SMP
841          * scenario. We'll call it here to avoid race conditions.
842          */
843         atomic_set(stopping, 1);
844         smp_wmb();
845
846         /*
847          * If module is the last one, it's writing to memory. In this case,
848          * it's necessary to check if the module is already paused due to
849          * DMA queue underrun or if it has to wait for next interrupt to be
850          * idle.
851          * If it isn't the last one, the function won't sleep but *stopping
852          * will still be set to warn next submodule caller's interrupt the
853          * module wants to be idle.
854          */
855         if (!iss_pipeline_is_last(me))
856                 return 0;
857
858         spin_lock_irqsave(&video->qlock, flags);
859         if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
860                 spin_unlock_irqrestore(&video->qlock, flags);
861                 atomic_set(stopping, 0);
862                 smp_wmb();
863                 return 0;
864         }
865         spin_unlock_irqrestore(&video->qlock, flags);
866         if (!wait_event_timeout(*wait, !atomic_read(stopping),
867                                 msecs_to_jiffies(1000))) {
868                 atomic_set(stopping, 0);
869                 smp_wmb();
870                 return -ETIMEDOUT;
871         }
872
873         return 0;
874 }
875
876 /*
877  * omap4iss_module_sync_is_stopped - Helper to verify if module was stopping
878  * @wait: ISS submodule's wait queue for streamoff/interrupt synchronization
879  * @stopping: flag which tells module wants to stop
880  *
881  * This function checks if ISS submodule was stopping. In case of yes, it
882  * notices the caller by setting stopping to 0 and waking up the wait queue.
883  * Returns 1 if it was stopping or 0 otherwise.
884  */
885 int omap4iss_module_sync_is_stopping(wait_queue_head_t *wait,
886                                      atomic_t *stopping)
887 {
888         if (atomic_cmpxchg(stopping, 1, 0)) {
889                 wake_up(wait);
890                 return 1;
891         }
892
893         return 0;
894 }
895
896 /* --------------------------------------------------------------------------
897  * Clock management
898  */
899
900 #define ISS_CLKCTRL_MASK        (ISS_CLKCTRL_CSI2_A |\
901                                  ISS_CLKCTRL_CSI2_B |\
902                                  ISS_CLKCTRL_ISP)
903
904 static int __iss_subclk_update(struct iss_device *iss)
905 {
906         u32 clk = 0;
907         int ret = 0, timeout = 1000;
908
909         if (iss->subclk_resources & OMAP4_ISS_SUBCLK_CSI2_A)
910                 clk |= ISS_CLKCTRL_CSI2_A;
911
912         if (iss->subclk_resources & OMAP4_ISS_SUBCLK_CSI2_B)
913                 clk |= ISS_CLKCTRL_CSI2_B;
914
915         if (iss->subclk_resources & OMAP4_ISS_SUBCLK_ISP)
916                 clk |= ISS_CLKCTRL_ISP;
917
918         iss_reg_update(iss, OMAP4_ISS_MEM_TOP, ISS_CLKCTRL,
919                        ISS_CLKCTRL_MASK, clk);
920
921         /* Wait for HW assertion */
922         while (--timeout > 0) {
923                 udelay(1);
924                 if ((iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_CLKSTAT) &
925                     ISS_CLKCTRL_MASK) == clk)
926                         break;
927         }
928
929         if (!timeout)
930                 ret = -EBUSY;
931
932         return ret;
933 }
934
935 int omap4iss_subclk_enable(struct iss_device *iss,
936                            enum iss_subclk_resource res)
937 {
938         iss->subclk_resources |= res;
939
940         return __iss_subclk_update(iss);
941 }
942
943 int omap4iss_subclk_disable(struct iss_device *iss,
944                             enum iss_subclk_resource res)
945 {
946         iss->subclk_resources &= ~res;
947
948         return __iss_subclk_update(iss);
949 }
950
951 #define ISS_ISP5_CLKCTRL_MASK   (ISP5_CTRL_BL_CLK_ENABLE |\
952                                  ISP5_CTRL_ISIF_CLK_ENABLE |\
953                                  ISP5_CTRL_H3A_CLK_ENABLE |\
954                                  ISP5_CTRL_RSZ_CLK_ENABLE |\
955                                  ISP5_CTRL_IPIPE_CLK_ENABLE |\
956                                  ISP5_CTRL_IPIPEIF_CLK_ENABLE)
957
958 static void __iss_isp_subclk_update(struct iss_device *iss)
959 {
960         u32 clk = 0;
961
962         if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_ISIF)
963                 clk |= ISP5_CTRL_ISIF_CLK_ENABLE;
964
965         if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_H3A)
966                 clk |= ISP5_CTRL_H3A_CLK_ENABLE;
967
968         if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_RSZ)
969                 clk |= ISP5_CTRL_RSZ_CLK_ENABLE;
970
971         if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_IPIPE)
972                 clk |= ISP5_CTRL_IPIPE_CLK_ENABLE;
973
974         if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_IPIPEIF)
975                 clk |= ISP5_CTRL_IPIPEIF_CLK_ENABLE;
976
977         if (clk)
978                 clk |= ISP5_CTRL_BL_CLK_ENABLE;
979
980         iss_reg_update(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL,
981                        ISS_ISP5_CLKCTRL_MASK, clk);
982 }
983
984 void omap4iss_isp_subclk_enable(struct iss_device *iss,
985                                 enum iss_isp_subclk_resource res)
986 {
987         iss->isp_subclk_resources |= res;
988
989         __iss_isp_subclk_update(iss);
990 }
991
992 void omap4iss_isp_subclk_disable(struct iss_device *iss,
993                                  enum iss_isp_subclk_resource res)
994 {
995         iss->isp_subclk_resources &= ~res;
996
997         __iss_isp_subclk_update(iss);
998 }
999
1000 /*
1001  * iss_enable_clocks - Enable ISS clocks
1002  * @iss: OMAP4 ISS device
1003  *
1004  * Return 0 if successful, or clk_enable return value if any of tthem fails.
1005  */
1006 static int iss_enable_clocks(struct iss_device *iss)
1007 {
1008         int ret;
1009
1010         ret = clk_enable(iss->iss_fck);
1011         if (ret) {
1012                 dev_err(iss->dev, "clk_enable iss_fck failed\n");
1013                 return ret;
1014         }
1015
1016         ret = clk_enable(iss->iss_ctrlclk);
1017         if (ret) {
1018                 dev_err(iss->dev, "clk_enable iss_ctrlclk failed\n");
1019                 clk_disable(iss->iss_fck);
1020                 return ret;
1021         }
1022
1023         return 0;
1024 }
1025
1026 /*
1027  * iss_disable_clocks - Disable ISS clocks
1028  * @iss: OMAP4 ISS device
1029  */
1030 static void iss_disable_clocks(struct iss_device *iss)
1031 {
1032         clk_disable(iss->iss_ctrlclk);
1033         clk_disable(iss->iss_fck);
1034 }
1035
1036 static int iss_get_clocks(struct iss_device *iss)
1037 {
1038         iss->iss_fck = devm_clk_get(iss->dev, "iss_fck");
1039         if (IS_ERR(iss->iss_fck)) {
1040                 dev_err(iss->dev, "Unable to get iss_fck clock info\n");
1041                 return PTR_ERR(iss->iss_fck);
1042         }
1043
1044         iss->iss_ctrlclk = devm_clk_get(iss->dev, "iss_ctrlclk");
1045         if (IS_ERR(iss->iss_ctrlclk)) {
1046                 dev_err(iss->dev, "Unable to get iss_ctrlclk clock info\n");
1047                 return PTR_ERR(iss->iss_ctrlclk);
1048         }
1049
1050         return 0;
1051 }
1052
1053 /*
1054  * omap4iss_get - Acquire the ISS resource.
1055  *
1056  * Initializes the clocks for the first acquire.
1057  *
1058  * Increment the reference count on the ISS. If the first reference is taken,
1059  * enable clocks and power-up all submodules.
1060  *
1061  * Return a pointer to the ISS device structure, or NULL if an error occurred.
1062  */
1063 struct iss_device *omap4iss_get(struct iss_device *iss)
1064 {
1065         struct iss_device *__iss = iss;
1066
1067         if (!iss)
1068                 return NULL;
1069
1070         mutex_lock(&iss->iss_mutex);
1071         if (iss->ref_count > 0)
1072                 goto out;
1073
1074         if (iss_enable_clocks(iss) < 0) {
1075                 __iss = NULL;
1076                 goto out;
1077         }
1078
1079         iss_enable_interrupts(iss);
1080
1081 out:
1082         if (__iss)
1083                 iss->ref_count++;
1084         mutex_unlock(&iss->iss_mutex);
1085
1086         return __iss;
1087 }
1088
1089 /*
1090  * omap4iss_put - Release the ISS
1091  *
1092  * Decrement the reference count on the ISS. If the last reference is released,
1093  * power-down all submodules, disable clocks and free temporary buffers.
1094  */
1095 void omap4iss_put(struct iss_device *iss)
1096 {
1097         if (!iss)
1098                 return;
1099
1100         mutex_lock(&iss->iss_mutex);
1101         BUG_ON(iss->ref_count == 0);
1102         if (--iss->ref_count == 0) {
1103                 iss_disable_interrupts(iss);
1104                 /* Reset the ISS if an entity has failed to stop. This is the
1105                  * only way to recover from such conditions, although it would
1106                  * be worth investigating whether resetting the ISP only can't
1107                  * fix the problem in some cases.
1108                  */
1109                 if (!media_entity_enum_empty(&iss->crashed))
1110                         iss_reset(iss);
1111                 iss_disable_clocks(iss);
1112         }
1113         mutex_unlock(&iss->iss_mutex);
1114 }
1115
1116 static int iss_map_mem_resource(struct platform_device *pdev,
1117                                 struct iss_device *iss,
1118                                 enum iss_mem_resources res)
1119 {
1120         struct resource *mem;
1121
1122         mem = platform_get_resource(pdev, IORESOURCE_MEM, res);
1123
1124         iss->regs[res] = devm_ioremap_resource(iss->dev, mem);
1125
1126         return PTR_ERR_OR_ZERO(iss->regs[res]);
1127 }
1128
1129 static void iss_unregister_entities(struct iss_device *iss)
1130 {
1131         omap4iss_resizer_unregister_entities(&iss->resizer);
1132         omap4iss_ipipe_unregister_entities(&iss->ipipe);
1133         omap4iss_ipipeif_unregister_entities(&iss->ipipeif);
1134         omap4iss_csi2_unregister_entities(&iss->csi2a);
1135         omap4iss_csi2_unregister_entities(&iss->csi2b);
1136
1137         v4l2_device_unregister(&iss->v4l2_dev);
1138         media_device_unregister(&iss->media_dev);
1139 }
1140
1141 /*
1142  * iss_register_subdev_group - Register a group of subdevices
1143  * @iss: OMAP4 ISS device
1144  * @board_info: I2C subdevs board information array
1145  *
1146  * Register all I2C subdevices in the board_info array. The array must be
1147  * terminated by a NULL entry, and the first entry must be the sensor.
1148  *
1149  * Return a pointer to the sensor media entity if it has been successfully
1150  * registered, or NULL otherwise.
1151  */
1152 static struct v4l2_subdev *
1153 iss_register_subdev_group(struct iss_device *iss,
1154                           struct iss_subdev_i2c_board_info *board_info)
1155 {
1156         struct v4l2_subdev *sensor = NULL;
1157         unsigned int first;
1158
1159         if (!board_info->board_info)
1160                 return NULL;
1161
1162         for (first = 1; board_info->board_info; ++board_info, first = 0) {
1163                 struct v4l2_subdev *subdev;
1164                 struct i2c_adapter *adapter;
1165
1166                 adapter = i2c_get_adapter(board_info->i2c_adapter_id);
1167                 if (!adapter) {
1168                         dev_err(iss->dev,
1169                                 "%s: Unable to get I2C adapter %d for device %s\n",
1170                                 __func__, board_info->i2c_adapter_id,
1171                                 board_info->board_info->type);
1172                         continue;
1173                 }
1174
1175                 subdev = v4l2_i2c_new_subdev_board(&iss->v4l2_dev, adapter,
1176                                 board_info->board_info, NULL);
1177                 if (!subdev) {
1178                         dev_err(iss->dev, "Unable to register subdev %s\n",
1179                                 board_info->board_info->type);
1180                         continue;
1181                 }
1182
1183                 if (first)
1184                         sensor = subdev;
1185         }
1186
1187         return sensor;
1188 }
1189
1190 static int iss_register_entities(struct iss_device *iss)
1191 {
1192         struct iss_platform_data *pdata = iss->pdata;
1193         struct iss_v4l2_subdevs_group *subdevs;
1194         int ret;
1195
1196         iss->media_dev.dev = iss->dev;
1197         strlcpy(iss->media_dev.model, "TI OMAP4 ISS",
1198                 sizeof(iss->media_dev.model));
1199         iss->media_dev.hw_revision = iss->revision;
1200         iss->media_dev.link_notify = iss_pipeline_link_notify;
1201         ret = media_device_register(&iss->media_dev);
1202         if (ret < 0) {
1203                 dev_err(iss->dev, "Media device registration failed (%d)\n",
1204                         ret);
1205                 return ret;
1206         }
1207
1208         iss->v4l2_dev.mdev = &iss->media_dev;
1209         ret = v4l2_device_register(iss->dev, &iss->v4l2_dev);
1210         if (ret < 0) {
1211                 dev_err(iss->dev, "V4L2 device registration failed (%d)\n",
1212                         ret);
1213                 goto done;
1214         }
1215
1216         /* Register internal entities */
1217         ret = omap4iss_csi2_register_entities(&iss->csi2a, &iss->v4l2_dev);
1218         if (ret < 0)
1219                 goto done;
1220
1221         ret = omap4iss_csi2_register_entities(&iss->csi2b, &iss->v4l2_dev);
1222         if (ret < 0)
1223                 goto done;
1224
1225         ret = omap4iss_ipipeif_register_entities(&iss->ipipeif, &iss->v4l2_dev);
1226         if (ret < 0)
1227                 goto done;
1228
1229         ret = omap4iss_ipipe_register_entities(&iss->ipipe, &iss->v4l2_dev);
1230         if (ret < 0)
1231                 goto done;
1232
1233         ret = omap4iss_resizer_register_entities(&iss->resizer, &iss->v4l2_dev);
1234         if (ret < 0)
1235                 goto done;
1236
1237         /* Register external entities */
1238         for (subdevs = pdata->subdevs; subdevs && subdevs->subdevs; ++subdevs) {
1239                 struct v4l2_subdev *sensor;
1240                 struct media_entity *input;
1241                 unsigned int flags;
1242                 unsigned int pad;
1243
1244                 sensor = iss_register_subdev_group(iss, subdevs->subdevs);
1245                 if (!sensor)
1246                         continue;
1247
1248                 sensor->host_priv = subdevs;
1249
1250                 /* Connect the sensor to the correct interface module.
1251                  * CSI2a receiver through CSIPHY1, or
1252                  * CSI2b receiver through CSIPHY2
1253                  */
1254                 switch (subdevs->interface) {
1255                 case ISS_INTERFACE_CSI2A_PHY1:
1256                         input = &iss->csi2a.subdev.entity;
1257                         pad = CSI2_PAD_SINK;
1258                         flags = MEDIA_LNK_FL_IMMUTABLE
1259                               | MEDIA_LNK_FL_ENABLED;
1260                         break;
1261
1262                 case ISS_INTERFACE_CSI2B_PHY2:
1263                         input = &iss->csi2b.subdev.entity;
1264                         pad = CSI2_PAD_SINK;
1265                         flags = MEDIA_LNK_FL_IMMUTABLE
1266                               | MEDIA_LNK_FL_ENABLED;
1267                         break;
1268
1269                 default:
1270                         dev_err(iss->dev, "invalid interface type %u\n",
1271                                 subdevs->interface);
1272                         ret = -EINVAL;
1273                         goto done;
1274                 }
1275
1276                 ret = media_create_pad_link(&sensor->entity, 0, input, pad,
1277                                                flags);
1278                 if (ret < 0)
1279                         goto done;
1280         }
1281
1282         ret = v4l2_device_register_subdev_nodes(&iss->v4l2_dev);
1283
1284 done:
1285         if (ret < 0)
1286                 iss_unregister_entities(iss);
1287
1288         return ret;
1289 }
1290
1291 /*
1292  * iss_create_links() - Pads links creation for the subdevices
1293  * @iss : Pointer to ISS device
1294  *
1295  * return negative error code or zero on success
1296  */
1297 static int iss_create_links(struct iss_device *iss)
1298 {
1299         int ret;
1300
1301         ret = omap4iss_csi2_create_links(iss);
1302         if (ret < 0) {
1303                 dev_err(iss->dev, "CSI2 pads links creation failed\n");
1304                 return ret;
1305         }
1306
1307         ret = omap4iss_ipipeif_create_links(iss);
1308         if (ret < 0) {
1309                 dev_err(iss->dev, "ISP IPIPEIF pads links creation failed\n");
1310                 return ret;
1311         }
1312
1313         ret = omap4iss_resizer_create_links(iss);
1314         if (ret < 0) {
1315                 dev_err(iss->dev, "ISP RESIZER pads links creation failed\n");
1316                 return ret;
1317         }
1318
1319         /* Connect the submodules. */
1320         ret = media_create_pad_link(
1321                         &iss->csi2a.subdev.entity, CSI2_PAD_SOURCE,
1322                         &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SINK, 0);
1323         if (ret < 0)
1324                 return ret;
1325
1326         ret = media_create_pad_link(
1327                         &iss->csi2b.subdev.entity, CSI2_PAD_SOURCE,
1328                         &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SINK, 0);
1329         if (ret < 0)
1330                 return ret;
1331
1332         ret = media_create_pad_link(
1333                         &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SOURCE_VP,
1334                         &iss->resizer.subdev.entity, RESIZER_PAD_SINK, 0);
1335         if (ret < 0)
1336                 return ret;
1337
1338         ret = media_create_pad_link(
1339                         &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SOURCE_VP,
1340                         &iss->ipipe.subdev.entity, IPIPE_PAD_SINK, 0);
1341         if (ret < 0)
1342                 return ret;
1343
1344         ret = media_create_pad_link(
1345                         &iss->ipipe.subdev.entity, IPIPE_PAD_SOURCE_VP,
1346                         &iss->resizer.subdev.entity, RESIZER_PAD_SINK, 0);
1347         if (ret < 0)
1348                 return ret;
1349
1350         return 0;
1351 };
1352
1353 static void iss_cleanup_modules(struct iss_device *iss)
1354 {
1355         omap4iss_csi2_cleanup(iss);
1356         omap4iss_ipipeif_cleanup(iss);
1357         omap4iss_ipipe_cleanup(iss);
1358         omap4iss_resizer_cleanup(iss);
1359 }
1360
1361 static int iss_initialize_modules(struct iss_device *iss)
1362 {
1363         int ret;
1364
1365         ret = omap4iss_csiphy_init(iss);
1366         if (ret < 0) {
1367                 dev_err(iss->dev, "CSI PHY initialization failed\n");
1368                 goto error_csiphy;
1369         }
1370
1371         ret = omap4iss_csi2_init(iss);
1372         if (ret < 0) {
1373                 dev_err(iss->dev, "CSI2 initialization failed\n");
1374                 goto error_csi2;
1375         }
1376
1377         ret = omap4iss_ipipeif_init(iss);
1378         if (ret < 0) {
1379                 dev_err(iss->dev, "ISP IPIPEIF initialization failed\n");
1380                 goto error_ipipeif;
1381         }
1382
1383         ret = omap4iss_ipipe_init(iss);
1384         if (ret < 0) {
1385                 dev_err(iss->dev, "ISP IPIPE initialization failed\n");
1386                 goto error_ipipe;
1387         }
1388
1389         ret = omap4iss_resizer_init(iss);
1390         if (ret < 0) {
1391                 dev_err(iss->dev, "ISP RESIZER initialization failed\n");
1392                 goto error_resizer;
1393         }
1394
1395         return 0;
1396
1397 error_resizer:
1398         omap4iss_ipipe_cleanup(iss);
1399 error_ipipe:
1400         omap4iss_ipipeif_cleanup(iss);
1401 error_ipipeif:
1402         omap4iss_csi2_cleanup(iss);
1403 error_csi2:
1404 error_csiphy:
1405         return ret;
1406 }
1407
1408 static int iss_probe(struct platform_device *pdev)
1409 {
1410         struct iss_platform_data *pdata = pdev->dev.platform_data;
1411         struct iss_device *iss;
1412         unsigned int i;
1413         int ret;
1414
1415         if (!pdata)
1416                 return -EINVAL;
1417
1418         iss = devm_kzalloc(&pdev->dev, sizeof(*iss), GFP_KERNEL);
1419         if (!iss)
1420                 return -ENOMEM;
1421
1422         mutex_init(&iss->iss_mutex);
1423
1424         iss->dev = &pdev->dev;
1425         iss->pdata = pdata;
1426
1427         iss->raw_dmamask = DMA_BIT_MASK(32);
1428         iss->dev->dma_mask = &iss->raw_dmamask;
1429         iss->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1430
1431         platform_set_drvdata(pdev, iss);
1432
1433         /*
1434          * TODO: When implementing DT support switch to syscon regmap lookup by
1435          * phandle.
1436          */
1437         iss->syscon = syscon_regmap_lookup_by_compatible("syscon");
1438         if (IS_ERR(iss->syscon)) {
1439                 ret = PTR_ERR(iss->syscon);
1440                 goto error;
1441         }
1442
1443         /* Clocks */
1444         ret = iss_map_mem_resource(pdev, iss, OMAP4_ISS_MEM_TOP);
1445         if (ret < 0)
1446                 goto error;
1447
1448         ret = iss_get_clocks(iss);
1449         if (ret < 0)
1450                 goto error;
1451
1452         if (!omap4iss_get(iss))
1453                 goto error;
1454
1455         ret = iss_reset(iss);
1456         if (ret < 0)
1457                 goto error_iss;
1458
1459         iss->revision = iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_HL_REVISION);
1460         dev_info(iss->dev, "Revision %08x found\n", iss->revision);
1461
1462         for (i = 1; i < OMAP4_ISS_MEM_LAST; i++) {
1463                 ret = iss_map_mem_resource(pdev, iss, i);
1464                 if (ret)
1465                         goto error_iss;
1466         }
1467
1468         /* Configure BTE BW_LIMITER field to max recommended value (1 GB) */
1469         iss_reg_update(iss, OMAP4_ISS_MEM_BTE, BTE_CTRL,
1470                        BTE_CTRL_BW_LIMITER_MASK,
1471                        18 << BTE_CTRL_BW_LIMITER_SHIFT);
1472
1473         /* Perform ISP reset */
1474         ret = omap4iss_subclk_enable(iss, OMAP4_ISS_SUBCLK_ISP);
1475         if (ret < 0)
1476                 goto error_iss;
1477
1478         ret = iss_isp_reset(iss);
1479         if (ret < 0)
1480                 goto error_iss;
1481
1482         dev_info(iss->dev, "ISP Revision %08x found\n",
1483                  iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_REVISION));
1484
1485         /* Interrupt */
1486         ret = platform_get_irq(pdev, 0);
1487         if (ret <= 0) {
1488                 dev_err(iss->dev, "No IRQ resource\n");
1489                 ret = -ENODEV;
1490                 goto error_iss;
1491         }
1492         iss->irq_num = ret;
1493
1494         if (devm_request_irq(iss->dev, iss->irq_num, iss_isr, IRQF_SHARED,
1495                              "OMAP4 ISS", iss)) {
1496                 dev_err(iss->dev, "Unable to request IRQ\n");
1497                 ret = -EINVAL;
1498                 goto error_iss;
1499         }
1500
1501         /* Entities */
1502         ret = iss_initialize_modules(iss);
1503         if (ret < 0)
1504                 goto error_iss;
1505
1506         ret = iss_register_entities(iss);
1507         if (ret < 0)
1508                 goto error_modules;
1509
1510         ret = media_entity_enum_init(&iss->crashed, &iss->media_dev);
1511         if (ret)
1512                 goto error_entities;
1513
1514         ret = iss_create_links(iss);
1515         if (ret < 0)
1516                 goto error_entities;
1517
1518         omap4iss_put(iss);
1519
1520         return 0;
1521
1522 error_entities:
1523         iss_unregister_entities(iss);
1524         media_entity_enum_cleanup(&iss->crashed);
1525 error_modules:
1526         iss_cleanup_modules(iss);
1527 error_iss:
1528         omap4iss_put(iss);
1529 error:
1530         platform_set_drvdata(pdev, NULL);
1531
1532         mutex_destroy(&iss->iss_mutex);
1533
1534         return ret;
1535 }
1536
1537 static int iss_remove(struct platform_device *pdev)
1538 {
1539         struct iss_device *iss = platform_get_drvdata(pdev);
1540
1541         iss_unregister_entities(iss);
1542         media_entity_enum_cleanup(&iss->crashed);
1543         iss_cleanup_modules(iss);
1544
1545         return 0;
1546 }
1547
1548 static const struct platform_device_id omap4iss_id_table[] = {
1549         { "omap4iss", 0 },
1550         { },
1551 };
1552 MODULE_DEVICE_TABLE(platform, omap4iss_id_table);
1553
1554 static struct platform_driver iss_driver = {
1555         .probe          = iss_probe,
1556         .remove         = iss_remove,
1557         .id_table       = omap4iss_id_table,
1558         .driver = {
1559                 .name   = "omap4iss",
1560         },
1561 };
1562
1563 module_platform_driver(iss_driver);
1564
1565 MODULE_DESCRIPTION("TI OMAP4 ISS driver");
1566 MODULE_AUTHOR("Sergio Aguirre <sergio.a.aguirre@gmail.com>");
1567 MODULE_LICENSE("GPL");
1568 MODULE_VERSION(ISS_VIDEO_DRIVER_VERSION);