2 * TI OMAP4 ISS V4L2 Driver
4 * Copyright (C) 2012, Texas Instruments
6 * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
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.
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>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
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))
38 static void iss_print_status(struct iss_device *iss)
40 dev_dbg(iss->dev, "-------------ISS HL Register dump-------------\n");
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);
51 dev_dbg(iss->dev, "-----------------------------------------------\n");
55 * omap4iss_flush - Post pending L3 bus writes by doing a register readback
56 * @iss: OMAP4 ISS device
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.
61 * See this link for reference:
62 * http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
64 void omap4iss_flush(struct iss_device *iss)
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);
71 * iss_isp_enable_interrupts - Enable ISS ISP interrupts.
72 * @iss: OMAP4 ISS device
74 static void omap4iss_isp_enable_interrupts(struct iss_device *iss)
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 |
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),
89 * iss_isp_disable_interrupts - Disable ISS interrupts.
90 * @iss: OMAP4 ISS device
92 static void omap4iss_isp_disable_interrupts(struct iss_device *iss)
94 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_IRQENABLE_CLR(0), ~0);
98 * iss_enable_interrupts - Enable ISS interrupts.
99 * @iss: OMAP4 ISS device
101 static void iss_enable_interrupts(struct iss_device *iss)
103 static const u32 hl_irq = ISS_HL_IRQ_CSIA | ISS_HL_IRQ_CSIB
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);
110 if (iss->regs[OMAP4_ISS_MEM_ISP_SYS1])
111 omap4iss_isp_enable_interrupts(iss);
115 * iss_disable_interrupts - Disable ISS interrupts.
116 * @iss: OMAP4 ISS device
118 static void iss_disable_interrupts(struct iss_device *iss)
120 if (iss->regs[OMAP4_ISS_MEM_ISP_SYS1])
121 omap4iss_isp_disable_interrupts(iss);
123 iss_reg_write(iss, OMAP4_ISS_MEM_TOP, ISS_HL_IRQENABLE_CLR(5), ~0);
126 int omap4iss_get_external_info(struct iss_pipeline *pipe,
127 struct media_link *link)
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;
138 if (pipe->external_rate)
141 memset(&fmt, 0, sizeof(fmt));
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);
150 pipe->external_bpp = omap4iss_video_format_info(fmt.format.code)->bpp;
152 ctrl = v4l2_ctrl_find(pipe->external->ctrl_handler,
153 V4L2_CID_PIXEL_RATE);
155 dev_warn(iss->dev, "no pixel rate control in subdev %s\n",
156 pipe->external->name);
160 pipe->external_rate = v4l2_ctrl_g_ctrl_int64(ctrl);
166 * Configure the bridge. Valid inputs are
168 * IPIPEIF_INPUT_CSI2A: CSI2a receiver
169 * IPIPEIF_INPUT_CSI2B: CSI2b receiver
171 * The bridge and lane shifter are configured according to the selected input
172 * and the ISP platform data.
174 void omap4iss_configure_bridge(struct iss_device *iss,
175 enum ipipeif_input_entity input)
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;
184 isp5ctrl_val = iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL);
187 case IPIPEIF_INPUT_CSI2A:
188 issctrl_val |= ISS_CTRL_INPUT_SEL_CSI2A;
191 case IPIPEIF_INPUT_CSI2B:
192 issctrl_val |= ISS_CTRL_INPUT_SEL_CSI2B;
199 issctrl_val |= ISS_CTRL_SYNC_DETECT_VS_RAISING;
201 isp5ctrl_val |= ISP5_CTRL_VD_PULSE_EXT | ISP5_CTRL_PSYNC_CLK_SEL |
202 ISP5_CTRL_SYNC_ENABLE;
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);
209 static void iss_isr_dbg(struct iss_device *iss, u32 irqstatus)
211 static const char * const name[] = {
247 dev_dbg(iss->dev, "ISS IRQ: ");
249 for (i = 0; i < ARRAY_SIZE(name); i++) {
250 if ((1 << i) & irqstatus)
251 pr_cont("%s ", name[i]);
256 static void iss_isp_isr_dbg(struct iss_device *iss, u32 irqstatus)
258 static const char * const name[] = {
278 "RSZ_FIFO_IN_BLK_ERR",
294 dev_dbg(iss->dev, "ISP IRQ: ");
296 for (i = 0; i < ARRAY_SIZE(name); i++) {
297 if ((1 << i) & irqstatus)
298 pr_cont("%s ", name[i]);
305 * iss_isr - Interrupt Service Routine for ISS module.
306 * @irq: Not used currently.
307 * @_iss: Pointer to the OMAP4 ISS device
309 * Handles the corresponding callback if plugged in.
311 * Returns IRQ_HANDLED when IRQ was correctly handled, or IRQ_NONE when the
312 * IRQ wasn't handled.
314 static irqreturn_t iss_isr(int irq, void *_iss)
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;
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);
327 if (irqstatus & ISS_HL_IRQ_CSIA)
328 omap4iss_csi2_isr(&iss->csi2a);
330 if (irqstatus & ISS_HL_IRQ_CSIB)
331 omap4iss_csi2_isr(&iss->csi2b);
333 if (irqstatus & ISS_HL_IRQ_ISP(0)) {
334 u32 isp_irqstatus = iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1,
336 iss_reg_write(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_IRQSTATUS(0),
339 if (isp_irqstatus & ISP5_IRQ_OCP_ERR)
340 dev_dbg(iss->dev, "ISP5 OCP Error!\n");
342 if (isp_irqstatus & ipipeif_events) {
343 omap4iss_ipipeif_isr(&iss->ipipeif,
344 isp_irqstatus & ipipeif_events);
347 if (isp_irqstatus & resizer_events)
348 omap4iss_resizer_isr(&iss->resizer,
349 isp_irqstatus & resizer_events);
352 iss_isp_isr_dbg(iss, isp_irqstatus);
359 iss_isr_dbg(iss, irqstatus);
365 /* -----------------------------------------------------------------------------
366 * Pipeline power management
368 * Entities must be powered up when part of a pipeline that contains at least
369 * one open video device node.
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
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.
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
387 * iss_pipeline_pm_use_count - Count the number of users of a pipeline
388 * @entity: The entity
390 * Return the total number of users of all video device nodes in the pipeline.
392 static int iss_pipeline_pm_use_count(struct media_entity *entity,
393 struct media_entity_graph *graph)
397 media_entity_graph_walk_start(graph, entity);
399 while ((entity = media_entity_graph_walk_next(graph))) {
400 if (is_media_entity_v4l2_io(entity))
401 use += entity->use_count;
408 * iss_pipeline_pm_power_one - Apply power change to an entity
409 * @entity: The entity
410 * @change: Use count change
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.
416 * Return 0 on success or a negative error code on failure.
418 static int iss_pipeline_pm_power_one(struct media_entity *entity, int change)
420 struct v4l2_subdev *subdev;
422 subdev = is_media_entity_v4l2_subdev(entity)
423 ? media_entity_to_v4l2_subdev(entity) : NULL;
425 if (entity->use_count == 0 && change > 0 && subdev) {
428 ret = v4l2_subdev_call(subdev, core, s_power, 1);
429 if (ret < 0 && ret != -ENOIOCTLCMD)
433 entity->use_count += change;
434 WARN_ON(entity->use_count < 0);
436 if (entity->use_count == 0 && change < 0 && subdev)
437 v4l2_subdev_call(subdev, core, s_power, 0);
443 * iss_pipeline_pm_power - Apply power change to all entities in a pipeline
444 * @entity: The entity
445 * @change: Use count change
447 * Walk the pipeline to update the use count and the power state of all non-node
450 * Return 0 on success or a negative error code on failure.
452 static int iss_pipeline_pm_power(struct media_entity *entity, int change,
453 struct media_entity_graph *graph)
455 struct media_entity *first = entity;
461 media_entity_graph_walk_start(graph, entity);
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);
470 media_entity_graph_walk_start(graph, first);
472 while ((first = media_entity_graph_walk_next(graph)) &&
474 if (is_media_entity_v4l2_subdev(first))
475 iss_pipeline_pm_power_one(first, -change);
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
485 * Update the use count of all entities in the pipeline and power entities on or
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
492 int omap4iss_pipeline_pm_use(struct media_entity *entity, int use,
493 struct media_entity_graph *graph)
495 int change = use ? 1 : -1;
498 mutex_lock(&entity->graph_obj.mdev->graph_mutex);
500 /* Apply use count to node. */
501 entity->use_count += change;
502 WARN_ON(entity->use_count < 0);
504 /* Apply power change to connected non-nodes. */
505 ret = iss_pipeline_pm_power(entity, change, graph);
507 entity->use_count -= change;
509 mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
515 * iss_pipeline_link_notify - Link management notification callback
517 * @flags: New link flags that will be applied
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.
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
527 static int iss_pipeline_link_notify(struct media_link *link, u32 flags,
528 unsigned int notification)
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;
539 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
540 ret = media_entity_graph_walk_init(graph,
541 link->graph_obj.mdev);
546 source_use = iss_pipeline_pm_use_count(source, graph);
547 sink_use = iss_pipeline_pm_use_count(sink, graph);
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);
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);
563 ret = iss_pipeline_pm_power(sink, source_use, graph);
565 iss_pipeline_pm_power(source, -sink_use, graph);
568 if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH)
569 media_entity_graph_walk_cleanup(graph);
574 /* -----------------------------------------------------------------------------
575 * Pipeline stream management
579 * iss_pipeline_disable - Disable streaming on a pipeline
580 * @pipe: ISS pipeline
581 * @until: entity at which to stop pipeline walk
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
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.
591 static int iss_pipeline_disable(struct iss_pipeline *pipe,
592 struct media_entity *until)
594 struct iss_device *iss = pipe->output->iss;
595 struct media_entity *entity;
596 struct media_pad *pad;
597 struct v4l2_subdev *subdev;
601 entity = &pipe->output->video.entity;
603 pad = &entity->pads[0];
604 if (!(pad->flags & MEDIA_PAD_FL_SINK))
607 pad = media_entity_remote_pad(pad);
608 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
611 entity = pad->entity;
615 subdev = media_entity_to_v4l2_subdev(entity);
616 ret = v4l2_subdev_call(subdev, video, s_stream, 0);
618 dev_warn(iss->dev, "%s: module stop timeout.\n",
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.
624 media_entity_enum_set(&iss->crashed, &subdev->entity);
625 failure = -ETIMEDOUT;
633 * iss_pipeline_enable - Enable streaming on a pipeline
634 * @pipe: ISS pipeline
635 * @mode: Stream mode (single shot or continuous)
637 * Walk the entities chain starting at the pipeline output video node and start
638 * all modules in the chain in the given mode.
640 * Return 0 if successful, or the return value of the failed video::s_stream
641 * operation otherwise.
643 static int iss_pipeline_enable(struct iss_pipeline *pipe,
644 enum iss_pipeline_stream_state mode)
646 struct iss_device *iss = pipe->output->iss;
647 struct media_entity *entity;
648 struct media_pad *pad;
649 struct v4l2_subdev *subdev;
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).
659 if (media_entity_enum_intersects(&pipe->ent_enum, &iss->crashed))
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);
666 pipe->do_propagation = false;
668 entity = &pipe->output->video.entity;
670 pad = &entity->pads[0];
671 if (!(pad->flags & MEDIA_PAD_FL_SINK))
674 pad = media_entity_remote_pad(pad);
675 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
678 entity = pad->entity;
679 subdev = media_entity_to_v4l2_subdev(entity);
681 ret = v4l2_subdev_call(subdev, video, s_stream, mode);
682 if (ret < 0 && ret != -ENOIOCTLCMD) {
683 iss_pipeline_disable(pipe, entity);
687 if (subdev == &iss->csi2a.subdev ||
688 subdev == &iss->csi2b.subdev)
689 pipe->do_propagation = true;
692 iss_print_status(pipe->output->iss);
697 * omap4iss_pipeline_set_stream - Enable/disable streaming on a pipeline
698 * @pipe: ISS pipeline
699 * @state: Stream state (stopped, single shot or continuous)
701 * Set the pipeline to the given stream state. Pipelines can be started in
702 * single-shot or continuous mode.
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.
708 int omap4iss_pipeline_set_stream(struct iss_pipeline *pipe,
709 enum iss_pipeline_stream_state state)
713 if (state == ISS_PIPELINE_STREAM_STOPPED)
714 ret = iss_pipeline_disable(pipe, NULL);
716 ret = iss_pipeline_enable(pipe, state);
718 if (ret == 0 || state == ISS_PIPELINE_STREAM_STOPPED)
719 pipe->stream_state = state;
725 * omap4iss_pipeline_cancel_stream - Cancel stream on a pipeline
726 * @pipe: ISS pipeline
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
733 void omap4iss_pipeline_cancel_stream(struct iss_pipeline *pipe)
736 omap4iss_video_cancel_stream(pipe->input);
738 omap4iss_video_cancel_stream(pipe->output);
742 * iss_pipeline_is_last - Verify if entity has an enabled link to the output
744 * @me: ISS module's media entity
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
750 static int iss_pipeline_is_last(struct media_entity *me)
752 struct iss_pipeline *pipe;
753 struct media_pad *pad;
757 pipe = to_iss_pipeline(me);
758 if (pipe->stream_state == ISS_PIPELINE_STREAM_STOPPED)
760 pad = media_entity_remote_pad(&pipe->output->pad);
761 return pad->entity == me;
764 static int iss_reset(struct iss_device *iss)
766 unsigned int timeout;
768 iss_reg_set(iss, OMAP4_ISS_MEM_TOP, ISS_HL_SYSCONFIG,
769 ISS_HL_SYSCONFIG_SOFTRESET);
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);
775 dev_err(iss->dev, "ISS reset timeout\n");
779 media_entity_enum_zero(&iss->crashed);
784 static int iss_isp_reset(struct iss_device *iss)
786 unsigned int timeout;
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);
793 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL, ISP5_CTRL_MSTANDBY);
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);
799 dev_err(iss->dev, "ISP5 standby timeout\n");
803 /* Now finally, do the reset */
804 iss_reg_set(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_SYSCONFIG,
805 ISP5_SYSCONFIG_SOFTRESET);
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);
811 dev_err(iss->dev, "ISP5 reset timeout\n");
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
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.
827 int omap4iss_module_sync_idle(struct media_entity *me, wait_queue_head_t *wait,
830 struct iss_pipeline *pipe = to_iss_pipeline(me);
831 struct iss_video *video = pipe->output;
834 if (pipe->stream_state == ISS_PIPELINE_STREAM_STOPPED ||
835 (pipe->stream_state == ISS_PIPELINE_STREAM_SINGLESHOT &&
836 !iss_pipeline_ready(pipe)))
840 * atomic_set() doesn't include memory barrier on ARM platform for SMP
841 * scenario. We'll call it here to avoid race conditions.
843 atomic_set(stopping, 1);
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
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.
855 if (!iss_pipeline_is_last(me))
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);
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);
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
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.
885 int omap4iss_module_sync_is_stopping(wait_queue_head_t *wait,
888 if (atomic_cmpxchg(stopping, 1, 0)) {
896 /* --------------------------------------------------------------------------
900 #define ISS_CLKCTRL_MASK (ISS_CLKCTRL_CSI2_A |\
901 ISS_CLKCTRL_CSI2_B |\
904 static int __iss_subclk_update(struct iss_device *iss)
907 int ret = 0, timeout = 1000;
909 if (iss->subclk_resources & OMAP4_ISS_SUBCLK_CSI2_A)
910 clk |= ISS_CLKCTRL_CSI2_A;
912 if (iss->subclk_resources & OMAP4_ISS_SUBCLK_CSI2_B)
913 clk |= ISS_CLKCTRL_CSI2_B;
915 if (iss->subclk_resources & OMAP4_ISS_SUBCLK_ISP)
916 clk |= ISS_CLKCTRL_ISP;
918 iss_reg_update(iss, OMAP4_ISS_MEM_TOP, ISS_CLKCTRL,
919 ISS_CLKCTRL_MASK, clk);
921 /* Wait for HW assertion */
922 while (--timeout > 0) {
924 if ((iss_reg_read(iss, OMAP4_ISS_MEM_TOP, ISS_CLKSTAT) &
925 ISS_CLKCTRL_MASK) == clk)
935 int omap4iss_subclk_enable(struct iss_device *iss,
936 enum iss_subclk_resource res)
938 iss->subclk_resources |= res;
940 return __iss_subclk_update(iss);
943 int omap4iss_subclk_disable(struct iss_device *iss,
944 enum iss_subclk_resource res)
946 iss->subclk_resources &= ~res;
948 return __iss_subclk_update(iss);
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)
958 static void __iss_isp_subclk_update(struct iss_device *iss)
962 if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_ISIF)
963 clk |= ISP5_CTRL_ISIF_CLK_ENABLE;
965 if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_H3A)
966 clk |= ISP5_CTRL_H3A_CLK_ENABLE;
968 if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_RSZ)
969 clk |= ISP5_CTRL_RSZ_CLK_ENABLE;
971 if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_IPIPE)
972 clk |= ISP5_CTRL_IPIPE_CLK_ENABLE;
974 if (iss->isp_subclk_resources & OMAP4_ISS_ISP_SUBCLK_IPIPEIF)
975 clk |= ISP5_CTRL_IPIPEIF_CLK_ENABLE;
978 clk |= ISP5_CTRL_BL_CLK_ENABLE;
980 iss_reg_update(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_CTRL,
981 ISS_ISP5_CLKCTRL_MASK, clk);
984 void omap4iss_isp_subclk_enable(struct iss_device *iss,
985 enum iss_isp_subclk_resource res)
987 iss->isp_subclk_resources |= res;
989 __iss_isp_subclk_update(iss);
992 void omap4iss_isp_subclk_disable(struct iss_device *iss,
993 enum iss_isp_subclk_resource res)
995 iss->isp_subclk_resources &= ~res;
997 __iss_isp_subclk_update(iss);
1001 * iss_enable_clocks - Enable ISS clocks
1002 * @iss: OMAP4 ISS device
1004 * Return 0 if successful, or clk_enable return value if any of tthem fails.
1006 static int iss_enable_clocks(struct iss_device *iss)
1010 ret = clk_enable(iss->iss_fck);
1012 dev_err(iss->dev, "clk_enable iss_fck failed\n");
1016 ret = clk_enable(iss->iss_ctrlclk);
1018 dev_err(iss->dev, "clk_enable iss_ctrlclk failed\n");
1019 clk_disable(iss->iss_fck);
1027 * iss_disable_clocks - Disable ISS clocks
1028 * @iss: OMAP4 ISS device
1030 static void iss_disable_clocks(struct iss_device *iss)
1032 clk_disable(iss->iss_ctrlclk);
1033 clk_disable(iss->iss_fck);
1036 static int iss_get_clocks(struct iss_device *iss)
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);
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);
1054 * omap4iss_get - Acquire the ISS resource.
1056 * Initializes the clocks for the first acquire.
1058 * Increment the reference count on the ISS. If the first reference is taken,
1059 * enable clocks and power-up all submodules.
1061 * Return a pointer to the ISS device structure, or NULL if an error occurred.
1063 struct iss_device *omap4iss_get(struct iss_device *iss)
1065 struct iss_device *__iss = iss;
1070 mutex_lock(&iss->iss_mutex);
1071 if (iss->ref_count > 0)
1074 if (iss_enable_clocks(iss) < 0) {
1079 iss_enable_interrupts(iss);
1084 mutex_unlock(&iss->iss_mutex);
1090 * omap4iss_put - Release the ISS
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.
1095 void omap4iss_put(struct iss_device *iss)
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.
1109 if (!media_entity_enum_empty(&iss->crashed))
1111 iss_disable_clocks(iss);
1113 mutex_unlock(&iss->iss_mutex);
1116 static int iss_map_mem_resource(struct platform_device *pdev,
1117 struct iss_device *iss,
1118 enum iss_mem_resources res)
1120 struct resource *mem;
1122 mem = platform_get_resource(pdev, IORESOURCE_MEM, res);
1124 iss->regs[res] = devm_ioremap_resource(iss->dev, mem);
1126 return PTR_ERR_OR_ZERO(iss->regs[res]);
1129 static void iss_unregister_entities(struct iss_device *iss)
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);
1137 v4l2_device_unregister(&iss->v4l2_dev);
1138 media_device_unregister(&iss->media_dev);
1142 * iss_register_subdev_group - Register a group of subdevices
1143 * @iss: OMAP4 ISS device
1144 * @board_info: I2C subdevs board information array
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.
1149 * Return a pointer to the sensor media entity if it has been successfully
1150 * registered, or NULL otherwise.
1152 static struct v4l2_subdev *
1153 iss_register_subdev_group(struct iss_device *iss,
1154 struct iss_subdev_i2c_board_info *board_info)
1156 struct v4l2_subdev *sensor = NULL;
1159 if (!board_info->board_info)
1162 for (first = 1; board_info->board_info; ++board_info, first = 0) {
1163 struct v4l2_subdev *subdev;
1164 struct i2c_adapter *adapter;
1166 adapter = i2c_get_adapter(board_info->i2c_adapter_id);
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);
1175 subdev = v4l2_i2c_new_subdev_board(&iss->v4l2_dev, adapter,
1176 board_info->board_info, NULL);
1178 dev_err(iss->dev, "Unable to register subdev %s\n",
1179 board_info->board_info->type);
1190 static int iss_register_entities(struct iss_device *iss)
1192 struct iss_platform_data *pdata = iss->pdata;
1193 struct iss_v4l2_subdevs_group *subdevs;
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);
1203 dev_err(iss->dev, "Media device registration failed (%d)\n",
1208 iss->v4l2_dev.mdev = &iss->media_dev;
1209 ret = v4l2_device_register(iss->dev, &iss->v4l2_dev);
1211 dev_err(iss->dev, "V4L2 device registration failed (%d)\n",
1216 /* Register internal entities */
1217 ret = omap4iss_csi2_register_entities(&iss->csi2a, &iss->v4l2_dev);
1221 ret = omap4iss_csi2_register_entities(&iss->csi2b, &iss->v4l2_dev);
1225 ret = omap4iss_ipipeif_register_entities(&iss->ipipeif, &iss->v4l2_dev);
1229 ret = omap4iss_ipipe_register_entities(&iss->ipipe, &iss->v4l2_dev);
1233 ret = omap4iss_resizer_register_entities(&iss->resizer, &iss->v4l2_dev);
1237 /* Register external entities */
1238 for (subdevs = pdata->subdevs; subdevs && subdevs->subdevs; ++subdevs) {
1239 struct v4l2_subdev *sensor;
1240 struct media_entity *input;
1244 sensor = iss_register_subdev_group(iss, subdevs->subdevs);
1248 sensor->host_priv = subdevs;
1250 /* Connect the sensor to the correct interface module.
1251 * CSI2a receiver through CSIPHY1, or
1252 * CSI2b receiver through CSIPHY2
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;
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;
1270 dev_err(iss->dev, "invalid interface type %u\n",
1271 subdevs->interface);
1276 ret = media_create_pad_link(&sensor->entity, 0, input, pad,
1282 ret = v4l2_device_register_subdev_nodes(&iss->v4l2_dev);
1286 iss_unregister_entities(iss);
1292 * iss_create_links() - Pads links creation for the subdevices
1293 * @iss : Pointer to ISS device
1295 * return negative error code or zero on success
1297 static int iss_create_links(struct iss_device *iss)
1301 ret = omap4iss_csi2_create_links(iss);
1303 dev_err(iss->dev, "CSI2 pads links creation failed\n");
1307 ret = omap4iss_ipipeif_create_links(iss);
1309 dev_err(iss->dev, "ISP IPIPEIF pads links creation failed\n");
1313 ret = omap4iss_resizer_create_links(iss);
1315 dev_err(iss->dev, "ISP RESIZER pads links creation failed\n");
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);
1326 ret = media_create_pad_link(
1327 &iss->csi2b.subdev.entity, CSI2_PAD_SOURCE,
1328 &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SINK, 0);
1332 ret = media_create_pad_link(
1333 &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SOURCE_VP,
1334 &iss->resizer.subdev.entity, RESIZER_PAD_SINK, 0);
1338 ret = media_create_pad_link(
1339 &iss->ipipeif.subdev.entity, IPIPEIF_PAD_SOURCE_VP,
1340 &iss->ipipe.subdev.entity, IPIPE_PAD_SINK, 0);
1344 ret = media_create_pad_link(
1345 &iss->ipipe.subdev.entity, IPIPE_PAD_SOURCE_VP,
1346 &iss->resizer.subdev.entity, RESIZER_PAD_SINK, 0);
1353 static void iss_cleanup_modules(struct iss_device *iss)
1355 omap4iss_csi2_cleanup(iss);
1356 omap4iss_ipipeif_cleanup(iss);
1357 omap4iss_ipipe_cleanup(iss);
1358 omap4iss_resizer_cleanup(iss);
1361 static int iss_initialize_modules(struct iss_device *iss)
1365 ret = omap4iss_csiphy_init(iss);
1367 dev_err(iss->dev, "CSI PHY initialization failed\n");
1371 ret = omap4iss_csi2_init(iss);
1373 dev_err(iss->dev, "CSI2 initialization failed\n");
1377 ret = omap4iss_ipipeif_init(iss);
1379 dev_err(iss->dev, "ISP IPIPEIF initialization failed\n");
1383 ret = omap4iss_ipipe_init(iss);
1385 dev_err(iss->dev, "ISP IPIPE initialization failed\n");
1389 ret = omap4iss_resizer_init(iss);
1391 dev_err(iss->dev, "ISP RESIZER initialization failed\n");
1398 omap4iss_ipipe_cleanup(iss);
1400 omap4iss_ipipeif_cleanup(iss);
1402 omap4iss_csi2_cleanup(iss);
1408 static int iss_probe(struct platform_device *pdev)
1410 struct iss_platform_data *pdata = pdev->dev.platform_data;
1411 struct iss_device *iss;
1418 iss = devm_kzalloc(&pdev->dev, sizeof(*iss), GFP_KERNEL);
1422 mutex_init(&iss->iss_mutex);
1424 iss->dev = &pdev->dev;
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);
1431 platform_set_drvdata(pdev, iss);
1434 * TODO: When implementing DT support switch to syscon regmap lookup by
1437 iss->syscon = syscon_regmap_lookup_by_compatible("syscon");
1438 if (IS_ERR(iss->syscon)) {
1439 ret = PTR_ERR(iss->syscon);
1444 ret = iss_map_mem_resource(pdev, iss, OMAP4_ISS_MEM_TOP);
1448 ret = iss_get_clocks(iss);
1452 if (!omap4iss_get(iss))
1455 ret = iss_reset(iss);
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);
1462 for (i = 1; i < OMAP4_ISS_MEM_LAST; i++) {
1463 ret = iss_map_mem_resource(pdev, iss, i);
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);
1473 /* Perform ISP reset */
1474 ret = omap4iss_subclk_enable(iss, OMAP4_ISS_SUBCLK_ISP);
1478 ret = iss_isp_reset(iss);
1482 dev_info(iss->dev, "ISP Revision %08x found\n",
1483 iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_REVISION));
1486 ret = platform_get_irq(pdev, 0);
1488 dev_err(iss->dev, "No IRQ resource\n");
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");
1502 ret = iss_initialize_modules(iss);
1506 ret = iss_register_entities(iss);
1510 ret = media_entity_enum_init(&iss->crashed, &iss->media_dev);
1512 goto error_entities;
1514 ret = iss_create_links(iss);
1516 goto error_entities;
1523 iss_unregister_entities(iss);
1524 media_entity_enum_cleanup(&iss->crashed);
1526 iss_cleanup_modules(iss);
1530 platform_set_drvdata(pdev, NULL);
1532 mutex_destroy(&iss->iss_mutex);
1537 static int iss_remove(struct platform_device *pdev)
1539 struct iss_device *iss = platform_get_drvdata(pdev);
1541 iss_unregister_entities(iss);
1542 media_entity_enum_cleanup(&iss->crashed);
1543 iss_cleanup_modules(iss);
1548 static const struct platform_device_id omap4iss_id_table[] = {
1552 MODULE_DEVICE_TABLE(platform, omap4iss_id_table);
1554 static struct platform_driver iss_driver = {
1556 .remove = iss_remove,
1557 .id_table = omap4iss_id_table,
1563 module_platform_driver(iss_driver);
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);