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/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/vmalloc.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ctrls.h>
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))
37 static void iss_print_status(struct iss_device
*iss
)
39 dev_dbg(iss
->dev
, "-------------ISS HL Register dump-------------\n");
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
);
50 dev_dbg(iss
->dev
, "-----------------------------------------------\n");
54 * omap4iss_flush - Post pending L3 bus writes by doing a register readback
55 * @iss: OMAP4 ISS device
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.
60 * See this link for reference:
61 * http://www.mail-archive.com/linux-omap@vger.kernel.org/msg08149.html
63 void omap4iss_flush(struct iss_device
*iss
)
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
);
70 * iss_isp_enable_interrupts - Enable ISS ISP interrupts.
71 * @iss: OMAP4 ISS device
73 static void omap4iss_isp_enable_interrupts(struct iss_device
*iss
)
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
|
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),
88 * iss_isp_disable_interrupts - Disable ISS interrupts.
89 * @iss: OMAP4 ISS device
91 static void omap4iss_isp_disable_interrupts(struct iss_device
*iss
)
93 iss_reg_write(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_IRQENABLE_CLR(0), ~0);
97 * iss_enable_interrupts - Enable ISS interrupts.
98 * @iss: OMAP4 ISS device
100 static void iss_enable_interrupts(struct iss_device
*iss
)
102 static const u32 hl_irq
= ISS_HL_IRQ_CSIA
| ISS_HL_IRQ_CSIB
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
);
109 if (iss
->regs
[OMAP4_ISS_MEM_ISP_SYS1
])
110 omap4iss_isp_enable_interrupts(iss
);
114 * iss_disable_interrupts - Disable ISS interrupts.
115 * @iss: OMAP4 ISS device
117 static void iss_disable_interrupts(struct iss_device
*iss
)
119 if (iss
->regs
[OMAP4_ISS_MEM_ISP_SYS1
])
120 omap4iss_isp_disable_interrupts(iss
);
122 iss_reg_write(iss
, OMAP4_ISS_MEM_TOP
, ISS_HL_IRQENABLE_CLR(5), ~0);
125 int omap4iss_get_external_info(struct iss_pipeline
*pipe
,
126 struct media_link
*link
)
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
;
137 if (pipe
->external_rate
)
140 memset(&fmt
, 0, sizeof(fmt
));
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
);
149 pipe
->external_bpp
= omap4iss_video_format_info(fmt
.format
.code
)->bpp
;
151 ctrl
= v4l2_ctrl_find(pipe
->external
->ctrl_handler
,
152 V4L2_CID_PIXEL_RATE
);
154 dev_warn(iss
->dev
, "no pixel rate control in subdev %s\n",
155 pipe
->external
->name
);
159 pipe
->external_rate
= v4l2_ctrl_g_ctrl_int64(ctrl
);
165 * Configure the bridge. Valid inputs are
167 * IPIPEIF_INPUT_CSI2A: CSI2a receiver
168 * IPIPEIF_INPUT_CSI2B: CSI2b receiver
170 * The bridge and lane shifter are configured according to the selected input
171 * and the ISP platform data.
173 void omap4iss_configure_bridge(struct iss_device
*iss
,
174 enum ipipeif_input_entity input
)
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
;
183 isp5ctrl_val
= iss_reg_read(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_CTRL
);
186 case IPIPEIF_INPUT_CSI2A
:
187 issctrl_val
|= ISS_CTRL_INPUT_SEL_CSI2A
;
190 case IPIPEIF_INPUT_CSI2B
:
191 issctrl_val
|= ISS_CTRL_INPUT_SEL_CSI2B
;
198 issctrl_val
|= ISS_CTRL_SYNC_DETECT_VS_RAISING
;
200 isp5ctrl_val
|= ISP5_CTRL_VD_PULSE_EXT
| ISP5_CTRL_PSYNC_CLK_SEL
|
201 ISP5_CTRL_SYNC_ENABLE
;
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
);
208 static void iss_isr_dbg(struct iss_device
*iss
, u32 irqstatus
)
210 static const char * const name
[] = {
246 dev_dbg(iss
->dev
, "ISS IRQ: ");
248 for (i
= 0; i
< ARRAY_SIZE(name
); i
++) {
249 if ((1 << i
) & irqstatus
)
250 pr_cont("%s ", name
[i
]);
255 static void iss_isp_isr_dbg(struct iss_device
*iss
, u32 irqstatus
)
257 static const char * const name
[] = {
277 "RSZ_FIFO_IN_BLK_ERR",
293 dev_dbg(iss
->dev
, "ISP IRQ: ");
295 for (i
= 0; i
< ARRAY_SIZE(name
); i
++) {
296 if ((1 << i
) & irqstatus
)
297 pr_cont("%s ", name
[i
]);
304 * iss_isr - Interrupt Service Routine for ISS module.
305 * @irq: Not used currently.
306 * @_iss: Pointer to the OMAP4 ISS device
308 * Handles the corresponding callback if plugged in.
310 * Returns IRQ_HANDLED when IRQ was correctly handled, or IRQ_NONE when the
311 * IRQ wasn't handled.
313 static irqreturn_t
iss_isr(int irq
, void *_iss
)
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
;
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
);
326 if (irqstatus
& ISS_HL_IRQ_CSIA
)
327 omap4iss_csi2_isr(&iss
->csi2a
);
329 if (irqstatus
& ISS_HL_IRQ_CSIB
)
330 omap4iss_csi2_isr(&iss
->csi2b
);
332 if (irqstatus
& ISS_HL_IRQ_ISP(0)) {
333 u32 isp_irqstatus
= iss_reg_read(iss
, OMAP4_ISS_MEM_ISP_SYS1
,
335 iss_reg_write(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_IRQSTATUS(0),
338 if (isp_irqstatus
& ISP5_IRQ_OCP_ERR
)
339 dev_dbg(iss
->dev
, "ISP5 OCP Error!\n");
341 if (isp_irqstatus
& ipipeif_events
) {
342 omap4iss_ipipeif_isr(&iss
->ipipeif
,
343 isp_irqstatus
& ipipeif_events
);
346 if (isp_irqstatus
& resizer_events
)
347 omap4iss_resizer_isr(&iss
->resizer
,
348 isp_irqstatus
& resizer_events
);
351 iss_isp_isr_dbg(iss
, isp_irqstatus
);
358 iss_isr_dbg(iss
, irqstatus
);
364 /* -----------------------------------------------------------------------------
365 * Pipeline power management
367 * Entities must be powered up when part of a pipeline that contains at least
368 * one open video device node.
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
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.
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
386 * iss_pipeline_pm_use_count - Count the number of users of a pipeline
387 * @entity: The entity
389 * Return the total number of users of all video device nodes in the pipeline.
391 static int iss_pipeline_pm_use_count(struct media_entity
*entity
)
393 struct media_entity_graph graph
;
396 media_entity_graph_walk_start(&graph
, entity
);
398 while ((entity
= media_entity_graph_walk_next(&graph
))) {
399 if (media_entity_type(entity
) == MEDIA_ENT_T_DEVNODE
)
400 use
+= entity
->use_count
;
407 * iss_pipeline_pm_power_one - Apply power change to an entity
408 * @entity: The entity
409 * @change: Use count change
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.
415 * Return 0 on success or a negative error code on failure.
417 static int iss_pipeline_pm_power_one(struct media_entity
*entity
, int change
)
419 struct v4l2_subdev
*subdev
;
421 subdev
= media_entity_type(entity
) == MEDIA_ENT_T_V4L2_SUBDEV
422 ? media_entity_to_v4l2_subdev(entity
) : NULL
;
424 if (entity
->use_count
== 0 && change
> 0 && subdev
!= NULL
) {
427 ret
= v4l2_subdev_call(subdev
, core
, s_power
, 1);
428 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
432 entity
->use_count
+= change
;
433 WARN_ON(entity
->use_count
< 0);
435 if (entity
->use_count
== 0 && change
< 0 && subdev
!= NULL
)
436 v4l2_subdev_call(subdev
, core
, s_power
, 0);
442 * iss_pipeline_pm_power - Apply power change to all entities in a pipeline
443 * @entity: The entity
444 * @change: Use count change
446 * Walk the pipeline to update the use count and the power state of all non-node
449 * Return 0 on success or a negative error code on failure.
451 static int iss_pipeline_pm_power(struct media_entity
*entity
, int change
)
453 struct media_entity_graph graph
;
454 struct media_entity
*first
= entity
;
460 media_entity_graph_walk_start(&graph
, entity
);
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
);
469 media_entity_graph_walk_start(&graph
, first
);
471 while ((first
= media_entity_graph_walk_next(&graph
))
473 if (media_entity_type(first
) != MEDIA_ENT_T_DEVNODE
)
474 iss_pipeline_pm_power_one(first
, -change
);
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
484 * Update the use count of all entities in the pipeline and power entities on or
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
491 int omap4iss_pipeline_pm_use(struct media_entity
*entity
, int use
)
493 int change
= use
? 1 : -1;
496 mutex_lock(&entity
->parent
->graph_mutex
);
498 /* Apply use count to node. */
499 entity
->use_count
+= change
;
500 WARN_ON(entity
->use_count
< 0);
502 /* Apply power change to connected non-nodes. */
503 ret
= iss_pipeline_pm_power(entity
, change
);
505 entity
->use_count
-= change
;
507 mutex_unlock(&entity
->parent
->graph_mutex
);
513 * iss_pipeline_link_notify - Link management notification callback
515 * @flags: New link flags that will be applied
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.
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
525 static int iss_pipeline_link_notify(struct media_link
*link
, u32 flags
,
526 unsigned int notification
)
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
);
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
);
542 if (notification
== MEDIA_DEV_NOTIFY_POST_LINK_CH
&&
543 (flags
& MEDIA_LNK_FL_ENABLED
)) {
544 ret
= iss_pipeline_pm_power(source
, sink_use
);
548 ret
= iss_pipeline_pm_power(sink
, source_use
);
550 iss_pipeline_pm_power(source
, -sink_use
);
558 /* -----------------------------------------------------------------------------
559 * Pipeline stream management
563 * iss_pipeline_disable - Disable streaming on a pipeline
564 * @pipe: ISS pipeline
565 * @until: entity at which to stop pipeline walk
567 * Walk the entities chain starting at the pipeline output video node and stop
568 * all modules in the chain. Wait synchronously for the modules to be stopped if
571 * If the until argument isn't NULL, stop the pipeline walk when reaching the
572 * until entity. This is used to disable a partially started pipeline due to a
573 * subdev start error.
575 static int iss_pipeline_disable(struct iss_pipeline
*pipe
,
576 struct media_entity
*until
)
578 struct iss_device
*iss
= pipe
->output
->iss
;
579 struct media_entity
*entity
;
580 struct media_pad
*pad
;
581 struct v4l2_subdev
*subdev
;
585 entity
= &pipe
->output
->video
.entity
;
587 pad
= &entity
->pads
[0];
588 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
591 pad
= media_entity_remote_pad(pad
);
593 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
596 entity
= pad
->entity
;
600 subdev
= media_entity_to_v4l2_subdev(entity
);
601 ret
= v4l2_subdev_call(subdev
, video
, s_stream
, 0);
603 dev_dbg(iss
->dev
, "%s: module stop timeout.\n",
605 /* If the entity failed to stopped, assume it has
606 * crashed. Mark it as such, the ISS will be reset when
607 * applications will release it.
609 iss
->crashed
|= 1U << subdev
->entity
.id
;
610 failure
= -ETIMEDOUT
;
618 * iss_pipeline_enable - Enable streaming on a pipeline
619 * @pipe: ISS pipeline
620 * @mode: Stream mode (single shot or continuous)
622 * Walk the entities chain starting at the pipeline output video node and start
623 * all modules in the chain in the given mode.
625 * Return 0 if successful, or the return value of the failed video::s_stream
626 * operation otherwise.
628 static int iss_pipeline_enable(struct iss_pipeline
*pipe
,
629 enum iss_pipeline_stream_state mode
)
631 struct iss_device
*iss
= pipe
->output
->iss
;
632 struct media_entity
*entity
;
633 struct media_pad
*pad
;
634 struct v4l2_subdev
*subdev
;
638 /* If one of the entities in the pipeline has crashed it will not work
639 * properly. Refuse to start streaming in that case. This check must be
640 * performed before the loop below to avoid starting entities if the
641 * pipeline won't start anyway (those entities would then likely fail to
642 * stop, making the problem worse).
644 if (pipe
->entities
& iss
->crashed
)
647 spin_lock_irqsave(&pipe
->lock
, flags
);
648 pipe
->state
&= ~(ISS_PIPELINE_IDLE_INPUT
| ISS_PIPELINE_IDLE_OUTPUT
);
649 spin_unlock_irqrestore(&pipe
->lock
, flags
);
651 pipe
->do_propagation
= false;
653 entity
= &pipe
->output
->video
.entity
;
655 pad
= &entity
->pads
[0];
656 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
659 pad
= media_entity_remote_pad(pad
);
661 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
664 entity
= pad
->entity
;
665 subdev
= media_entity_to_v4l2_subdev(entity
);
667 ret
= v4l2_subdev_call(subdev
, video
, s_stream
, mode
);
668 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
669 iss_pipeline_disable(pipe
, entity
);
673 if (subdev
== &iss
->csi2a
.subdev
||
674 subdev
== &iss
->csi2b
.subdev
)
675 pipe
->do_propagation
= true;
678 iss_print_status(pipe
->output
->iss
);
683 * omap4iss_pipeline_set_stream - Enable/disable streaming on a pipeline
684 * @pipe: ISS pipeline
685 * @state: Stream state (stopped, single shot or continuous)
687 * Set the pipeline to the given stream state. Pipelines can be started in
688 * single-shot or continuous mode.
690 * Return 0 if successful, or the return value of the failed video::s_stream
691 * operation otherwise. The pipeline state is not updated when the operation
692 * fails, except when stopping the pipeline.
694 int omap4iss_pipeline_set_stream(struct iss_pipeline
*pipe
,
695 enum iss_pipeline_stream_state state
)
699 if (state
== ISS_PIPELINE_STREAM_STOPPED
)
700 ret
= iss_pipeline_disable(pipe
, NULL
);
702 ret
= iss_pipeline_enable(pipe
, state
);
704 if (ret
== 0 || state
== ISS_PIPELINE_STREAM_STOPPED
)
705 pipe
->stream_state
= state
;
711 * omap4iss_pipeline_cancel_stream - Cancel stream on a pipeline
712 * @pipe: ISS pipeline
714 * Cancelling a stream mark all buffers on all video nodes in the pipeline as
715 * erroneous and makes sure no new buffer can be queued. This function is called
716 * when a fatal error that prevents any further operation on the pipeline
719 void omap4iss_pipeline_cancel_stream(struct iss_pipeline
*pipe
)
722 omap4iss_video_cancel_stream(pipe
->input
);
724 omap4iss_video_cancel_stream(pipe
->output
);
728 * iss_pipeline_is_last - Verify if entity has an enabled link to the output
730 * @me: ISS module's media entity
732 * Returns 1 if the entity has an enabled link to the output video node or 0
733 * otherwise. It's true only while pipeline can have no more than one output
736 static int iss_pipeline_is_last(struct media_entity
*me
)
738 struct iss_pipeline
*pipe
;
739 struct media_pad
*pad
;
743 pipe
= to_iss_pipeline(me
);
744 if (pipe
->stream_state
== ISS_PIPELINE_STREAM_STOPPED
)
746 pad
= media_entity_remote_pad(&pipe
->output
->pad
);
747 return pad
->entity
== me
;
750 static int iss_reset(struct iss_device
*iss
)
752 unsigned int timeout
;
754 iss_reg_set(iss
, OMAP4_ISS_MEM_TOP
, ISS_HL_SYSCONFIG
,
755 ISS_HL_SYSCONFIG_SOFTRESET
);
757 timeout
= iss_poll_condition_timeout(
758 !(iss_reg_read(iss
, OMAP4_ISS_MEM_TOP
, ISS_HL_SYSCONFIG
) &
759 ISS_HL_SYSCONFIG_SOFTRESET
), 1000, 10, 100);
761 dev_err(iss
->dev
, "ISS reset timeout\n");
769 static int iss_isp_reset(struct iss_device
*iss
)
771 unsigned int timeout
;
773 /* Fist, ensure that the ISP is IDLE (no transactions happening) */
774 iss_reg_update(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_SYSCONFIG
,
775 ISP5_SYSCONFIG_STANDBYMODE_MASK
,
776 ISP5_SYSCONFIG_STANDBYMODE_SMART
);
778 iss_reg_set(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_CTRL
, ISP5_CTRL_MSTANDBY
);
780 timeout
= iss_poll_condition_timeout(
781 iss_reg_read(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_CTRL
) &
782 ISP5_CTRL_MSTANDBY_WAIT
, 1000000, 1000, 1500);
784 dev_err(iss
->dev
, "ISP5 standby timeout\n");
788 /* Now finally, do the reset */
789 iss_reg_set(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_SYSCONFIG
,
790 ISP5_SYSCONFIG_SOFTRESET
);
792 timeout
= iss_poll_condition_timeout(
793 !(iss_reg_read(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_SYSCONFIG
) &
794 ISP5_SYSCONFIG_SOFTRESET
), 1000000, 1000, 1500);
796 dev_err(iss
->dev
, "ISP5 reset timeout\n");
804 * iss_module_sync_idle - Helper to sync module with its idle state
805 * @me: ISS submodule's media entity
806 * @wait: ISS submodule's wait queue for streamoff/interrupt synchronization
807 * @stopping: flag which tells module wants to stop
809 * This function checks if ISS submodule needs to wait for next interrupt. If
810 * yes, makes the caller to sleep while waiting for such event.
812 int omap4iss_module_sync_idle(struct media_entity
*me
, wait_queue_head_t
*wait
,
815 struct iss_pipeline
*pipe
= to_iss_pipeline(me
);
816 struct iss_video
*video
= pipe
->output
;
819 if (pipe
->stream_state
== ISS_PIPELINE_STREAM_STOPPED
||
820 (pipe
->stream_state
== ISS_PIPELINE_STREAM_SINGLESHOT
&&
821 !iss_pipeline_ready(pipe
)))
825 * atomic_set() doesn't include memory barrier on ARM platform for SMP
826 * scenario. We'll call it here to avoid race conditions.
828 atomic_set(stopping
, 1);
832 * If module is the last one, it's writing to memory. In this case,
833 * it's necessary to check if the module is already paused due to
834 * DMA queue underrun or if it has to wait for next interrupt to be
836 * If it isn't the last one, the function won't sleep but *stopping
837 * will still be set to warn next submodule caller's interrupt the
838 * module wants to be idle.
840 if (!iss_pipeline_is_last(me
))
843 spin_lock_irqsave(&video
->qlock
, flags
);
844 if (video
->dmaqueue_flags
& ISS_VIDEO_DMAQUEUE_UNDERRUN
) {
845 spin_unlock_irqrestore(&video
->qlock
, flags
);
846 atomic_set(stopping
, 0);
850 spin_unlock_irqrestore(&video
->qlock
, flags
);
851 if (!wait_event_timeout(*wait
, !atomic_read(stopping
),
852 msecs_to_jiffies(1000))) {
853 atomic_set(stopping
, 0);
862 * omap4iss_module_sync_is_stopped - Helper to verify if module was stopping
863 * @wait: ISS submodule's wait queue for streamoff/interrupt synchronization
864 * @stopping: flag which tells module wants to stop
866 * This function checks if ISS submodule was stopping. In case of yes, it
867 * notices the caller by setting stopping to 0 and waking up the wait queue.
868 * Returns 1 if it was stopping or 0 otherwise.
870 int omap4iss_module_sync_is_stopping(wait_queue_head_t
*wait
,
873 if (atomic_cmpxchg(stopping
, 1, 0)) {
881 /* --------------------------------------------------------------------------
885 #define ISS_CLKCTRL_MASK (ISS_CLKCTRL_CSI2_A |\
886 ISS_CLKCTRL_CSI2_B |\
889 static int __iss_subclk_update(struct iss_device
*iss
)
892 int ret
= 0, timeout
= 1000;
894 if (iss
->subclk_resources
& OMAP4_ISS_SUBCLK_CSI2_A
)
895 clk
|= ISS_CLKCTRL_CSI2_A
;
897 if (iss
->subclk_resources
& OMAP4_ISS_SUBCLK_CSI2_B
)
898 clk
|= ISS_CLKCTRL_CSI2_B
;
900 if (iss
->subclk_resources
& OMAP4_ISS_SUBCLK_ISP
)
901 clk
|= ISS_CLKCTRL_ISP
;
903 iss_reg_update(iss
, OMAP4_ISS_MEM_TOP
, ISS_CLKCTRL
,
904 ISS_CLKCTRL_MASK
, clk
);
906 /* Wait for HW assertion */
907 while (--timeout
> 0) {
909 if ((iss_reg_read(iss
, OMAP4_ISS_MEM_TOP
, ISS_CLKSTAT
) &
910 ISS_CLKCTRL_MASK
) == clk
)
920 int omap4iss_subclk_enable(struct iss_device
*iss
,
921 enum iss_subclk_resource res
)
923 iss
->subclk_resources
|= res
;
925 return __iss_subclk_update(iss
);
928 int omap4iss_subclk_disable(struct iss_device
*iss
,
929 enum iss_subclk_resource res
)
931 iss
->subclk_resources
&= ~res
;
933 return __iss_subclk_update(iss
);
936 #define ISS_ISP5_CLKCTRL_MASK (ISP5_CTRL_BL_CLK_ENABLE |\
937 ISP5_CTRL_ISIF_CLK_ENABLE |\
938 ISP5_CTRL_H3A_CLK_ENABLE |\
939 ISP5_CTRL_RSZ_CLK_ENABLE |\
940 ISP5_CTRL_IPIPE_CLK_ENABLE |\
941 ISP5_CTRL_IPIPEIF_CLK_ENABLE)
943 static void __iss_isp_subclk_update(struct iss_device
*iss
)
947 if (iss
->isp_subclk_resources
& OMAP4_ISS_ISP_SUBCLK_ISIF
)
948 clk
|= ISP5_CTRL_ISIF_CLK_ENABLE
;
950 if (iss
->isp_subclk_resources
& OMAP4_ISS_ISP_SUBCLK_H3A
)
951 clk
|= ISP5_CTRL_H3A_CLK_ENABLE
;
953 if (iss
->isp_subclk_resources
& OMAP4_ISS_ISP_SUBCLK_RSZ
)
954 clk
|= ISP5_CTRL_RSZ_CLK_ENABLE
;
956 if (iss
->isp_subclk_resources
& OMAP4_ISS_ISP_SUBCLK_IPIPE
)
957 clk
|= ISP5_CTRL_IPIPE_CLK_ENABLE
;
959 if (iss
->isp_subclk_resources
& OMAP4_ISS_ISP_SUBCLK_IPIPEIF
)
960 clk
|= ISP5_CTRL_IPIPEIF_CLK_ENABLE
;
963 clk
|= ISP5_CTRL_BL_CLK_ENABLE
;
965 iss_reg_update(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_CTRL
,
966 ISS_ISP5_CLKCTRL_MASK
, clk
);
969 void omap4iss_isp_subclk_enable(struct iss_device
*iss
,
970 enum iss_isp_subclk_resource res
)
972 iss
->isp_subclk_resources
|= res
;
974 __iss_isp_subclk_update(iss
);
977 void omap4iss_isp_subclk_disable(struct iss_device
*iss
,
978 enum iss_isp_subclk_resource res
)
980 iss
->isp_subclk_resources
&= ~res
;
982 __iss_isp_subclk_update(iss
);
986 * iss_enable_clocks - Enable ISS clocks
987 * @iss: OMAP4 ISS device
989 * Return 0 if successful, or clk_enable return value if any of tthem fails.
991 static int iss_enable_clocks(struct iss_device
*iss
)
995 ret
= clk_enable(iss
->iss_fck
);
997 dev_err(iss
->dev
, "clk_enable iss_fck failed\n");
1001 ret
= clk_enable(iss
->iss_ctrlclk
);
1003 dev_err(iss
->dev
, "clk_enable iss_ctrlclk failed\n");
1004 clk_disable(iss
->iss_fck
);
1012 * iss_disable_clocks - Disable ISS clocks
1013 * @iss: OMAP4 ISS device
1015 static void iss_disable_clocks(struct iss_device
*iss
)
1017 clk_disable(iss
->iss_ctrlclk
);
1018 clk_disable(iss
->iss_fck
);
1021 static int iss_get_clocks(struct iss_device
*iss
)
1023 iss
->iss_fck
= devm_clk_get(iss
->dev
, "iss_fck");
1024 if (IS_ERR(iss
->iss_fck
)) {
1025 dev_err(iss
->dev
, "Unable to get iss_fck clock info\n");
1026 return PTR_ERR(iss
->iss_fck
);
1029 iss
->iss_ctrlclk
= devm_clk_get(iss
->dev
, "iss_ctrlclk");
1030 if (IS_ERR(iss
->iss_ctrlclk
)) {
1031 dev_err(iss
->dev
, "Unable to get iss_ctrlclk clock info\n");
1032 return PTR_ERR(iss
->iss_ctrlclk
);
1039 * omap4iss_get - Acquire the ISS resource.
1041 * Initializes the clocks for the first acquire.
1043 * Increment the reference count on the ISS. If the first reference is taken,
1044 * enable clocks and power-up all submodules.
1046 * Return a pointer to the ISS device structure, or NULL if an error occurred.
1048 struct iss_device
*omap4iss_get(struct iss_device
*iss
)
1050 struct iss_device
*__iss
= iss
;
1055 mutex_lock(&iss
->iss_mutex
);
1056 if (iss
->ref_count
> 0)
1059 if (iss_enable_clocks(iss
) < 0) {
1064 iss_enable_interrupts(iss
);
1069 mutex_unlock(&iss
->iss_mutex
);
1075 * omap4iss_put - Release the ISS
1077 * Decrement the reference count on the ISS. If the last reference is released,
1078 * power-down all submodules, disable clocks and free temporary buffers.
1080 void omap4iss_put(struct iss_device
*iss
)
1085 mutex_lock(&iss
->iss_mutex
);
1086 BUG_ON(iss
->ref_count
== 0);
1087 if (--iss
->ref_count
== 0) {
1088 iss_disable_interrupts(iss
);
1089 /* Reset the ISS if an entity has failed to stop. This is the
1090 * only way to recover from such conditions, although it would
1091 * be worth investigating whether resetting the ISP only can't
1092 * fix the problem in some cases.
1096 iss_disable_clocks(iss
);
1098 mutex_unlock(&iss
->iss_mutex
);
1101 static int iss_map_mem_resource(struct platform_device
*pdev
,
1102 struct iss_device
*iss
,
1103 enum iss_mem_resources res
)
1105 struct resource
*mem
;
1107 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, res
);
1109 iss
->regs
[res
] = devm_ioremap_resource(iss
->dev
, mem
);
1111 return PTR_ERR_OR_ZERO(iss
->regs
[res
]);
1114 static void iss_unregister_entities(struct iss_device
*iss
)
1116 omap4iss_resizer_unregister_entities(&iss
->resizer
);
1117 omap4iss_ipipe_unregister_entities(&iss
->ipipe
);
1118 omap4iss_ipipeif_unregister_entities(&iss
->ipipeif
);
1119 omap4iss_csi2_unregister_entities(&iss
->csi2a
);
1120 omap4iss_csi2_unregister_entities(&iss
->csi2b
);
1122 v4l2_device_unregister(&iss
->v4l2_dev
);
1123 media_device_unregister(&iss
->media_dev
);
1127 * iss_register_subdev_group - Register a group of subdevices
1128 * @iss: OMAP4 ISS device
1129 * @board_info: I2C subdevs board information array
1131 * Register all I2C subdevices in the board_info array. The array must be
1132 * terminated by a NULL entry, and the first entry must be the sensor.
1134 * Return a pointer to the sensor media entity if it has been successfully
1135 * registered, or NULL otherwise.
1137 static struct v4l2_subdev
*
1138 iss_register_subdev_group(struct iss_device
*iss
,
1139 struct iss_subdev_i2c_board_info
*board_info
)
1141 struct v4l2_subdev
*sensor
= NULL
;
1144 if (board_info
->board_info
== NULL
)
1147 for (first
= 1; board_info
->board_info
; ++board_info
, first
= 0) {
1148 struct v4l2_subdev
*subdev
;
1149 struct i2c_adapter
*adapter
;
1151 adapter
= i2c_get_adapter(board_info
->i2c_adapter_id
);
1152 if (adapter
== NULL
) {
1154 "%s: Unable to get I2C adapter %d for device %s\n",
1155 __func__
, board_info
->i2c_adapter_id
,
1156 board_info
->board_info
->type
);
1160 subdev
= v4l2_i2c_new_subdev_board(&iss
->v4l2_dev
, adapter
,
1161 board_info
->board_info
, NULL
);
1162 if (subdev
== NULL
) {
1163 dev_err(iss
->dev
, "%s: Unable to register subdev %s\n",
1164 __func__
, board_info
->board_info
->type
);
1175 static int iss_register_entities(struct iss_device
*iss
)
1177 struct iss_platform_data
*pdata
= iss
->pdata
;
1178 struct iss_v4l2_subdevs_group
*subdevs
;
1181 iss
->media_dev
.dev
= iss
->dev
;
1182 strlcpy(iss
->media_dev
.model
, "TI OMAP4 ISS",
1183 sizeof(iss
->media_dev
.model
));
1184 iss
->media_dev
.hw_revision
= iss
->revision
;
1185 iss
->media_dev
.link_notify
= iss_pipeline_link_notify
;
1186 ret
= media_device_register(&iss
->media_dev
);
1188 dev_err(iss
->dev
, "%s: Media device registration failed (%d)\n",
1193 iss
->v4l2_dev
.mdev
= &iss
->media_dev
;
1194 ret
= v4l2_device_register(iss
->dev
, &iss
->v4l2_dev
);
1196 dev_err(iss
->dev
, "%s: V4L2 device registration failed (%d)\n",
1201 /* Register internal entities */
1202 ret
= omap4iss_csi2_register_entities(&iss
->csi2a
, &iss
->v4l2_dev
);
1206 ret
= omap4iss_csi2_register_entities(&iss
->csi2b
, &iss
->v4l2_dev
);
1210 ret
= omap4iss_ipipeif_register_entities(&iss
->ipipeif
, &iss
->v4l2_dev
);
1214 ret
= omap4iss_ipipe_register_entities(&iss
->ipipe
, &iss
->v4l2_dev
);
1218 ret
= omap4iss_resizer_register_entities(&iss
->resizer
, &iss
->v4l2_dev
);
1222 /* Register external entities */
1223 for (subdevs
= pdata
->subdevs
; subdevs
&& subdevs
->subdevs
; ++subdevs
) {
1224 struct v4l2_subdev
*sensor
;
1225 struct media_entity
*input
;
1229 sensor
= iss_register_subdev_group(iss
, subdevs
->subdevs
);
1233 sensor
->host_priv
= subdevs
;
1235 /* Connect the sensor to the correct interface module.
1236 * CSI2a receiver through CSIPHY1, or
1237 * CSI2b receiver through CSIPHY2
1239 switch (subdevs
->interface
) {
1240 case ISS_INTERFACE_CSI2A_PHY1
:
1241 input
= &iss
->csi2a
.subdev
.entity
;
1242 pad
= CSI2_PAD_SINK
;
1243 flags
= MEDIA_LNK_FL_IMMUTABLE
1244 | MEDIA_LNK_FL_ENABLED
;
1247 case ISS_INTERFACE_CSI2B_PHY2
:
1248 input
= &iss
->csi2b
.subdev
.entity
;
1249 pad
= CSI2_PAD_SINK
;
1250 flags
= MEDIA_LNK_FL_IMMUTABLE
1251 | MEDIA_LNK_FL_ENABLED
;
1255 dev_err(iss
->dev
, "%s: invalid interface type %u\n",
1256 __func__
, subdevs
->interface
);
1261 ret
= media_entity_create_link(&sensor
->entity
, 0, input
, pad
,
1267 ret
= v4l2_device_register_subdev_nodes(&iss
->v4l2_dev
);
1271 iss_unregister_entities(iss
);
1276 static void iss_cleanup_modules(struct iss_device
*iss
)
1278 omap4iss_csi2_cleanup(iss
);
1279 omap4iss_ipipeif_cleanup(iss
);
1280 omap4iss_ipipe_cleanup(iss
);
1281 omap4iss_resizer_cleanup(iss
);
1284 static int iss_initialize_modules(struct iss_device
*iss
)
1288 ret
= omap4iss_csiphy_init(iss
);
1290 dev_err(iss
->dev
, "CSI PHY initialization failed\n");
1294 ret
= omap4iss_csi2_init(iss
);
1296 dev_err(iss
->dev
, "CSI2 initialization failed\n");
1300 ret
= omap4iss_ipipeif_init(iss
);
1302 dev_err(iss
->dev
, "ISP IPIPEIF initialization failed\n");
1306 ret
= omap4iss_ipipe_init(iss
);
1308 dev_err(iss
->dev
, "ISP IPIPE initialization failed\n");
1312 ret
= omap4iss_resizer_init(iss
);
1314 dev_err(iss
->dev
, "ISP RESIZER initialization failed\n");
1318 /* Connect the submodules. */
1319 ret
= media_entity_create_link(
1320 &iss
->csi2a
.subdev
.entity
, CSI2_PAD_SOURCE
,
1321 &iss
->ipipeif
.subdev
.entity
, IPIPEIF_PAD_SINK
, 0);
1325 ret
= media_entity_create_link(
1326 &iss
->csi2b
.subdev
.entity
, CSI2_PAD_SOURCE
,
1327 &iss
->ipipeif
.subdev
.entity
, IPIPEIF_PAD_SINK
, 0);
1331 ret
= media_entity_create_link(
1332 &iss
->ipipeif
.subdev
.entity
, IPIPEIF_PAD_SOURCE_VP
,
1333 &iss
->resizer
.subdev
.entity
, RESIZER_PAD_SINK
, 0);
1337 ret
= media_entity_create_link(
1338 &iss
->ipipeif
.subdev
.entity
, IPIPEIF_PAD_SOURCE_VP
,
1339 &iss
->ipipe
.subdev
.entity
, IPIPE_PAD_SINK
, 0);
1343 ret
= media_entity_create_link(
1344 &iss
->ipipe
.subdev
.entity
, IPIPE_PAD_SOURCE_VP
,
1345 &iss
->resizer
.subdev
.entity
, RESIZER_PAD_SINK
, 0);
1352 omap4iss_resizer_cleanup(iss
);
1354 omap4iss_ipipe_cleanup(iss
);
1356 omap4iss_ipipeif_cleanup(iss
);
1358 omap4iss_csi2_cleanup(iss
);
1364 static int iss_probe(struct platform_device
*pdev
)
1366 struct iss_platform_data
*pdata
= pdev
->dev
.platform_data
;
1367 struct iss_device
*iss
;
1374 iss
= devm_kzalloc(&pdev
->dev
, sizeof(*iss
), GFP_KERNEL
);
1378 mutex_init(&iss
->iss_mutex
);
1380 iss
->dev
= &pdev
->dev
;
1383 iss
->raw_dmamask
= DMA_BIT_MASK(32);
1384 iss
->dev
->dma_mask
= &iss
->raw_dmamask
;
1385 iss
->dev
->coherent_dma_mask
= DMA_BIT_MASK(32);
1387 platform_set_drvdata(pdev
, iss
);
1390 ret
= iss_map_mem_resource(pdev
, iss
, OMAP4_ISS_MEM_TOP
);
1394 ret
= iss_get_clocks(iss
);
1398 if (omap4iss_get(iss
) == NULL
)
1401 ret
= iss_reset(iss
);
1405 iss
->revision
= iss_reg_read(iss
, OMAP4_ISS_MEM_TOP
, ISS_HL_REVISION
);
1406 dev_info(iss
->dev
, "Revision %08x found\n", iss
->revision
);
1408 for (i
= 1; i
< OMAP4_ISS_MEM_LAST
; i
++) {
1409 ret
= iss_map_mem_resource(pdev
, iss
, i
);
1414 /* Configure BTE BW_LIMITER field to max recommended value (1 GB) */
1415 iss_reg_update(iss
, OMAP4_ISS_MEM_BTE
, BTE_CTRL
,
1416 BTE_CTRL_BW_LIMITER_MASK
,
1417 18 << BTE_CTRL_BW_LIMITER_SHIFT
);
1419 /* Perform ISP reset */
1420 ret
= omap4iss_subclk_enable(iss
, OMAP4_ISS_SUBCLK_ISP
);
1424 ret
= iss_isp_reset(iss
);
1428 dev_info(iss
->dev
, "ISP Revision %08x found\n",
1429 iss_reg_read(iss
, OMAP4_ISS_MEM_ISP_SYS1
, ISP5_REVISION
));
1432 iss
->irq_num
= platform_get_irq(pdev
, 0);
1433 if (iss
->irq_num
<= 0) {
1434 dev_err(iss
->dev
, "No IRQ resource\n");
1439 if (devm_request_irq(iss
->dev
, iss
->irq_num
, iss_isr
, IRQF_SHARED
,
1440 "OMAP4 ISS", iss
)) {
1441 dev_err(iss
->dev
, "Unable to request IRQ\n");
1447 ret
= iss_initialize_modules(iss
);
1451 ret
= iss_register_entities(iss
);
1460 iss_cleanup_modules(iss
);
1464 platform_set_drvdata(pdev
, NULL
);
1466 mutex_destroy(&iss
->iss_mutex
);
1471 static int iss_remove(struct platform_device
*pdev
)
1473 struct iss_device
*iss
= platform_get_drvdata(pdev
);
1475 iss_unregister_entities(iss
);
1476 iss_cleanup_modules(iss
);
1481 static struct platform_device_id omap4iss_id_table
[] = {
1485 MODULE_DEVICE_TABLE(platform
, omap4iss_id_table
);
1487 static struct platform_driver iss_driver
= {
1489 .remove
= iss_remove
,
1490 .id_table
= omap4iss_id_table
,
1496 module_platform_driver(iss_driver
);
1498 MODULE_DESCRIPTION("TI OMAP4 ISS driver");
1499 MODULE_AUTHOR("Sergio Aguirre <sergio.a.aguirre@gmail.com>");
1500 MODULE_LICENSE("GPL");
1501 MODULE_VERSION(ISS_VIDEO_DRIVER_VERSION
);