Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us